1 //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the IRBuilder class, which is used as a convenient way
10 // to create LLVM instructions with a consistent and simplified interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_IRBUILDER_H
15 #define LLVM_IR_IRBUILDER_H
16 
17 #include "llvm-c/Types.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constant.h"
24 #include "llvm/IR/ConstantFolder.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/FPEnv.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/IR/Value.h"
41 #include "llvm/IR/ValueHandle.h"
42 #include "llvm/Support/AtomicOrdering.h"
43 #include "llvm/Support/CBindingWrapping.h"
44 #include "llvm/Support/Casting.h"
45 #include <cassert>
46 #include <cstdint>
47 #include <functional>
48 #include <optional>
49 #include <utility>
50 
51 namespace llvm {
52 
53 class APInt;
54 class Use;
55 
56 /// This provides the default implementation of the IRBuilder
57 /// 'InsertHelper' method that is called whenever an instruction is created by
58 /// IRBuilder and needs to be inserted.
59 ///
60 /// By default, this inserts the instruction at the insertion point.
61 class IRBuilderDefaultInserter {
62 public:
63   virtual ~IRBuilderDefaultInserter();
64 
InsertHelper(Instruction * I,const Twine & Name,BasicBlock * BB,BasicBlock::iterator InsertPt)65   virtual void InsertHelper(Instruction *I, const Twine &Name,
66                             BasicBlock *BB,
67                             BasicBlock::iterator InsertPt) const {
68     if (BB)
69       I->insertInto(BB, InsertPt);
70     I->setName(Name);
71   }
72 };
73 
74 /// Provides an 'InsertHelper' that calls a user-provided callback after
75 /// performing the default insertion.
76 class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
77   std::function<void(Instruction *)> Callback;
78 
79 public:
80   ~IRBuilderCallbackInserter() override;
81 
IRBuilderCallbackInserter(std::function<void (Instruction *)> Callback)82   IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
83       : Callback(std::move(Callback)) {}
84 
InsertHelper(Instruction * I,const Twine & Name,BasicBlock * BB,BasicBlock::iterator InsertPt)85   void InsertHelper(Instruction *I, const Twine &Name,
86                     BasicBlock *BB,
87                     BasicBlock::iterator InsertPt) const override {
88     IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
89     Callback(I);
90   }
91 };
92 
93 /// Common base class shared among various IRBuilders.
94 class IRBuilderBase {
95   /// Pairs of (metadata kind, MDNode *) that should be added to all newly
96   /// created instructions, like !dbg metadata.
97   SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
98 
99   /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
100   /// null. If \p MD is null, remove the entry with \p Kind.
AddOrRemoveMetadataToCopy(unsigned Kind,MDNode * MD)101   void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
102     if (!MD) {
103       erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) {
104         return KV.first == Kind;
105       });
106       return;
107     }
108 
109     for (auto &KV : MetadataToCopy)
110       if (KV.first == Kind) {
111         KV.second = MD;
112         return;
113       }
114 
115     MetadataToCopy.emplace_back(Kind, MD);
116   }
117 
118 protected:
119   BasicBlock *BB;
120   BasicBlock::iterator InsertPt;
121   LLVMContext &Context;
122   const IRBuilderFolder &Folder;
123   const IRBuilderDefaultInserter &Inserter;
124 
125   MDNode *DefaultFPMathTag;
126   FastMathFlags FMF;
127 
128   bool IsFPConstrained = false;
129   fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict;
130   RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic;
131 
132   ArrayRef<OperandBundleDef> DefaultOperandBundles;
133 
134 public:
IRBuilderBase(LLVMContext & context,const IRBuilderFolder & Folder,const IRBuilderDefaultInserter & Inserter,MDNode * FPMathTag,ArrayRef<OperandBundleDef> OpBundles)135   IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
136                 const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag,
137                 ArrayRef<OperandBundleDef> OpBundles)
138       : Context(context), Folder(Folder), Inserter(Inserter),
139         DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) {
140     ClearInsertionPoint();
141   }
142 
143   /// Insert and return the specified instruction.
144   template<typename InstTy>
145   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
146     Inserter.InsertHelper(I, Name, BB, InsertPt);
147     AddMetadataToInst(I);
148     return I;
149   }
150 
151   /// No-op overload to handle constants.
152   Constant *Insert(Constant *C, const Twine& = "") const {
153     return C;
154   }
155 
156   Value *Insert(Value *V, const Twine &Name = "") const {
157     if (Instruction *I = dyn_cast<Instruction>(V))
158       return Insert(I, Name);
159     assert(isa<Constant>(V));
160     return V;
161   }
162 
163   //===--------------------------------------------------------------------===//
164   // Builder configuration methods
165   //===--------------------------------------------------------------------===//
166 
167   /// Clear the insertion point: created instructions will not be
168   /// inserted into a block.
ClearInsertionPoint()169   void ClearInsertionPoint() {
170     BB = nullptr;
171     InsertPt = BasicBlock::iterator();
172   }
173 
GetInsertBlock()174   BasicBlock *GetInsertBlock() const { return BB; }
GetInsertPoint()175   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
getContext()176   LLVMContext &getContext() const { return Context; }
177 
178   /// This specifies that created instructions should be appended to the
179   /// end of the specified block.
SetInsertPoint(BasicBlock * TheBB)180   void SetInsertPoint(BasicBlock *TheBB) {
181     BB = TheBB;
182     InsertPt = BB->end();
183   }
184 
185   /// This specifies that created instructions should be inserted before
186   /// the specified instruction.
SetInsertPoint(Instruction * I)187   void SetInsertPoint(Instruction *I) {
188     BB = I->getParent();
189     InsertPt = I->getIterator();
190     assert(InsertPt != BB->end() && "Can't read debug loc from end()");
191     SetCurrentDebugLocation(I->getStableDebugLoc());
192   }
193 
194   /// This specifies that created instructions should be inserted at the
195   /// specified point.
SetInsertPoint(BasicBlock * TheBB,BasicBlock::iterator IP)196   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
197     BB = TheBB;
198     InsertPt = IP;
199     if (IP != TheBB->end())
200       SetCurrentDebugLocation(IP->getStableDebugLoc());
201   }
202 
203   /// This specifies that created instructions should be inserted at
204   /// the specified point, but also requires that \p IP is dereferencable.
SetInsertPoint(BasicBlock::iterator IP)205   void SetInsertPoint(BasicBlock::iterator IP) {
206     BB = IP->getParent();
207     InsertPt = IP;
208     SetCurrentDebugLocation(IP->getStableDebugLoc());
209   }
210 
211   /// This specifies that created instructions should inserted at the beginning
212   /// end of the specified function, but after already existing static alloca
213   /// instructions that are at the start.
SetInsertPointPastAllocas(Function * F)214   void SetInsertPointPastAllocas(Function *F) {
215     BB = &F->getEntryBlock();
216     InsertPt = BB->getFirstNonPHIOrDbgOrAlloca();
217   }
218 
219   /// Set location information used by debugging information.
SetCurrentDebugLocation(DebugLoc L)220   void SetCurrentDebugLocation(DebugLoc L) {
221     AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode());
222   }
223 
224   /// Collect metadata with IDs \p MetadataKinds from \p Src which should be
225   /// added to all created instructions. Entries present in MedataDataToCopy but
226   /// not on \p Src will be dropped from MetadataToCopy.
CollectMetadataToCopy(Instruction * Src,ArrayRef<unsigned> MetadataKinds)227   void CollectMetadataToCopy(Instruction *Src,
228                              ArrayRef<unsigned> MetadataKinds) {
229     for (unsigned K : MetadataKinds)
230       AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
231   }
232 
233   /// Get location information used by debugging information.
234   DebugLoc getCurrentDebugLocation() const;
235 
236   /// If this builder has a current debug location, set it on the
237   /// specified instruction.
238   void SetInstDebugLocation(Instruction *I) const;
239 
240   /// Add all entries in MetadataToCopy to \p I.
AddMetadataToInst(Instruction * I)241   void AddMetadataToInst(Instruction *I) const {
242     for (const auto &KV : MetadataToCopy)
243       I->setMetadata(KV.first, KV.second);
244   }
245 
246   /// Get the return type of the current function that we're emitting
247   /// into.
248   Type *getCurrentFunctionReturnType() const;
249 
250   /// InsertPoint - A saved insertion point.
251   class InsertPoint {
252     BasicBlock *Block = nullptr;
253     BasicBlock::iterator Point;
254 
255   public:
256     /// Creates a new insertion point which doesn't point to anything.
257     InsertPoint() = default;
258 
259     /// Creates a new insertion point at the given location.
InsertPoint(BasicBlock * InsertBlock,BasicBlock::iterator InsertPoint)260     InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
261         : Block(InsertBlock), Point(InsertPoint) {}
262 
263     /// Returns true if this insert point is set.
isSet()264     bool isSet() const { return (Block != nullptr); }
265 
getBlock()266     BasicBlock *getBlock() const { return Block; }
getPoint()267     BasicBlock::iterator getPoint() const { return Point; }
268   };
269 
270   /// Returns the current insert point.
saveIP()271   InsertPoint saveIP() const {
272     return InsertPoint(GetInsertBlock(), GetInsertPoint());
273   }
274 
275   /// Returns the current insert point, clearing it in the process.
saveAndClearIP()276   InsertPoint saveAndClearIP() {
277     InsertPoint IP(GetInsertBlock(), GetInsertPoint());
278     ClearInsertionPoint();
279     return IP;
280   }
281 
282   /// Sets the current insert point to a previously-saved location.
restoreIP(InsertPoint IP)283   void restoreIP(InsertPoint IP) {
284     if (IP.isSet())
285       SetInsertPoint(IP.getBlock(), IP.getPoint());
286     else
287       ClearInsertionPoint();
288   }
289 
290   /// Get the floating point math metadata being used.
getDefaultFPMathTag()291   MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
292 
293   /// Get the flags to be applied to created floating point ops
getFastMathFlags()294   FastMathFlags getFastMathFlags() const { return FMF; }
295 
getFastMathFlags()296   FastMathFlags &getFastMathFlags() { return FMF; }
297 
298   /// Clear the fast-math flags.
clearFastMathFlags()299   void clearFastMathFlags() { FMF.clear(); }
300 
301   /// Set the floating point math metadata to be used.
setDefaultFPMathTag(MDNode * FPMathTag)302   void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
303 
304   /// Set the fast-math flags to be used with generated fp-math operators
setFastMathFlags(FastMathFlags NewFMF)305   void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
306 
307   /// Enable/Disable use of constrained floating point math. When
308   /// enabled the CreateF<op>() calls instead create constrained
309   /// floating point intrinsic calls. Fast math flags are unaffected
310   /// by this setting.
setIsFPConstrained(bool IsCon)311   void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
312 
313   /// Query for the use of constrained floating point math
getIsFPConstrained()314   bool getIsFPConstrained() { return IsFPConstrained; }
315 
316   /// Set the exception handling to be used with constrained floating point
setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept)317   void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
318 #ifndef NDEBUG
319     std::optional<StringRef> ExceptStr =
320         convertExceptionBehaviorToStr(NewExcept);
321     assert(ExceptStr && "Garbage strict exception behavior!");
322 #endif
323     DefaultConstrainedExcept = NewExcept;
324   }
325 
326   /// Set the rounding mode handling to be used with constrained floating point
setDefaultConstrainedRounding(RoundingMode NewRounding)327   void setDefaultConstrainedRounding(RoundingMode NewRounding) {
328 #ifndef NDEBUG
329     std::optional<StringRef> RoundingStr =
330         convertRoundingModeToStr(NewRounding);
331     assert(RoundingStr && "Garbage strict rounding mode!");
332 #endif
333     DefaultConstrainedRounding = NewRounding;
334   }
335 
336   /// Get the exception handling used with constrained floating point
getDefaultConstrainedExcept()337   fp::ExceptionBehavior getDefaultConstrainedExcept() {
338     return DefaultConstrainedExcept;
339   }
340 
341   /// Get the rounding mode handling used with constrained floating point
getDefaultConstrainedRounding()342   RoundingMode getDefaultConstrainedRounding() {
343     return DefaultConstrainedRounding;
344   }
345 
setConstrainedFPFunctionAttr()346   void setConstrainedFPFunctionAttr() {
347     assert(BB && "Must have a basic block to set any function attributes!");
348 
349     Function *F = BB->getParent();
350     if (!F->hasFnAttribute(Attribute::StrictFP)) {
351       F->addFnAttr(Attribute::StrictFP);
352     }
353   }
354 
setConstrainedFPCallAttr(CallBase * I)355   void setConstrainedFPCallAttr(CallBase *I) {
356     I->addFnAttr(Attribute::StrictFP);
357   }
358 
setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles)359   void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
360     DefaultOperandBundles = OpBundles;
361   }
362 
363   //===--------------------------------------------------------------------===//
364   // RAII helpers.
365   //===--------------------------------------------------------------------===//
366 
367   // RAII object that stores the current insertion point and restores it
368   // when the object is destroyed. This includes the debug location.
369   class InsertPointGuard {
370     IRBuilderBase &Builder;
371     AssertingVH<BasicBlock> Block;
372     BasicBlock::iterator Point;
373     DebugLoc DbgLoc;
374 
375   public:
InsertPointGuard(IRBuilderBase & B)376     InsertPointGuard(IRBuilderBase &B)
377         : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
378           DbgLoc(B.getCurrentDebugLocation()) {}
379 
380     InsertPointGuard(const InsertPointGuard &) = delete;
381     InsertPointGuard &operator=(const InsertPointGuard &) = delete;
382 
~InsertPointGuard()383     ~InsertPointGuard() {
384       Builder.restoreIP(InsertPoint(Block, Point));
385       Builder.SetCurrentDebugLocation(DbgLoc);
386     }
387   };
388 
389   // RAII object that stores the current fast math settings and restores
390   // them when the object is destroyed.
391   class FastMathFlagGuard {
392     IRBuilderBase &Builder;
393     FastMathFlags FMF;
394     MDNode *FPMathTag;
395     bool IsFPConstrained;
396     fp::ExceptionBehavior DefaultConstrainedExcept;
397     RoundingMode DefaultConstrainedRounding;
398 
399   public:
FastMathFlagGuard(IRBuilderBase & B)400     FastMathFlagGuard(IRBuilderBase &B)
401         : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
402           IsFPConstrained(B.IsFPConstrained),
403           DefaultConstrainedExcept(B.DefaultConstrainedExcept),
404           DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
405 
406     FastMathFlagGuard(const FastMathFlagGuard &) = delete;
407     FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
408 
~FastMathFlagGuard()409     ~FastMathFlagGuard() {
410       Builder.FMF = FMF;
411       Builder.DefaultFPMathTag = FPMathTag;
412       Builder.IsFPConstrained = IsFPConstrained;
413       Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
414       Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
415     }
416   };
417 
418   // RAII object that stores the current default operand bundles and restores
419   // them when the object is destroyed.
420   class OperandBundlesGuard {
421     IRBuilderBase &Builder;
422     ArrayRef<OperandBundleDef> DefaultOperandBundles;
423 
424   public:
OperandBundlesGuard(IRBuilderBase & B)425     OperandBundlesGuard(IRBuilderBase &B)
426         : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
427 
428     OperandBundlesGuard(const OperandBundlesGuard &) = delete;
429     OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
430 
~OperandBundlesGuard()431     ~OperandBundlesGuard() {
432       Builder.DefaultOperandBundles = DefaultOperandBundles;
433     }
434   };
435 
436 
437   //===--------------------------------------------------------------------===//
438   // Miscellaneous creation methods.
439   //===--------------------------------------------------------------------===//
440 
441   /// Make a new global variable with initializer type i8*
442   ///
443   /// Make a new global variable with an initializer that has array of i8 type
444   /// filled in with the null terminated string value specified.  The new global
445   /// variable will be marked mergable with any others of the same contents.  If
446   /// Name is specified, it is the name of the global variable created.
447   ///
448   /// If no module is given via \p M, it is take from the insertion point basic
449   /// block.
450   GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
451                                      unsigned AddressSpace = 0,
452                                      Module *M = nullptr);
453 
454   /// Get a constant value representing either true or false.
getInt1(bool V)455   ConstantInt *getInt1(bool V) {
456     return ConstantInt::get(getInt1Ty(), V);
457   }
458 
459   /// Get the constant value for i1 true.
getTrue()460   ConstantInt *getTrue() {
461     return ConstantInt::getTrue(Context);
462   }
463 
464   /// Get the constant value for i1 false.
getFalse()465   ConstantInt *getFalse() {
466     return ConstantInt::getFalse(Context);
467   }
468 
469   /// Get a constant 8-bit value.
getInt8(uint8_t C)470   ConstantInt *getInt8(uint8_t C) {
471     return ConstantInt::get(getInt8Ty(), C);
472   }
473 
474   /// Get a constant 16-bit value.
getInt16(uint16_t C)475   ConstantInt *getInt16(uint16_t C) {
476     return ConstantInt::get(getInt16Ty(), C);
477   }
478 
479   /// Get a constant 32-bit value.
getInt32(uint32_t C)480   ConstantInt *getInt32(uint32_t C) {
481     return ConstantInt::get(getInt32Ty(), C);
482   }
483 
484   /// Get a constant 64-bit value.
getInt64(uint64_t C)485   ConstantInt *getInt64(uint64_t C) {
486     return ConstantInt::get(getInt64Ty(), C);
487   }
488 
489   /// Get a constant N-bit value, zero extended or truncated from
490   /// a 64-bit value.
getIntN(unsigned N,uint64_t C)491   ConstantInt *getIntN(unsigned N, uint64_t C) {
492     return ConstantInt::get(getIntNTy(N), C);
493   }
494 
495   /// Get a constant integer value.
getInt(const APInt & AI)496   ConstantInt *getInt(const APInt &AI) {
497     return ConstantInt::get(Context, AI);
498   }
499 
500   //===--------------------------------------------------------------------===//
501   // Type creation methods
502   //===--------------------------------------------------------------------===//
503 
504   /// Fetch the type representing a single bit
getInt1Ty()505   IntegerType *getInt1Ty() {
506     return Type::getInt1Ty(Context);
507   }
508 
509   /// Fetch the type representing an 8-bit integer.
getInt8Ty()510   IntegerType *getInt8Ty() {
511     return Type::getInt8Ty(Context);
512   }
513 
514   /// Fetch the type representing a 16-bit integer.
getInt16Ty()515   IntegerType *getInt16Ty() {
516     return Type::getInt16Ty(Context);
517   }
518 
519   /// Fetch the type representing a 32-bit integer.
getInt32Ty()520   IntegerType *getInt32Ty() {
521     return Type::getInt32Ty(Context);
522   }
523 
524   /// Fetch the type representing a 64-bit integer.
getInt64Ty()525   IntegerType *getInt64Ty() {
526     return Type::getInt64Ty(Context);
527   }
528 
529   /// Fetch the type representing a 128-bit integer.
getInt128Ty()530   IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
531 
532   /// Fetch the type representing an N-bit integer.
getIntNTy(unsigned N)533   IntegerType *getIntNTy(unsigned N) {
534     return Type::getIntNTy(Context, N);
535   }
536 
537   /// Fetch the type representing a 16-bit floating point value.
getHalfTy()538   Type *getHalfTy() {
539     return Type::getHalfTy(Context);
540   }
541 
542   /// Fetch the type representing a 16-bit brain floating point value.
getBFloatTy()543   Type *getBFloatTy() {
544     return Type::getBFloatTy(Context);
545   }
546 
547   /// Fetch the type representing a 32-bit floating point value.
getFloatTy()548   Type *getFloatTy() {
549     return Type::getFloatTy(Context);
550   }
551 
552   /// Fetch the type representing a 64-bit floating point value.
getDoubleTy()553   Type *getDoubleTy() {
554     return Type::getDoubleTy(Context);
555   }
556 
557   /// Fetch the type representing void.
getVoidTy()558   Type *getVoidTy() {
559     return Type::getVoidTy(Context);
560   }
561 
562   /// Fetch the type representing a pointer.
563   PointerType *getPtrTy(unsigned AddrSpace = 0) {
564     return PointerType::get(Context, AddrSpace);
565   }
566 
567   /// Fetch the type of an integer with size at least as big as that of a
568   /// pointer in the given address space.
569   IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
570     return DL.getIntPtrType(Context, AddrSpace);
571   }
572 
573   /// Fetch the type of an integer that should be used to index GEP operations
574   /// within AddressSpace.
getIndexTy(const DataLayout & DL,unsigned AddrSpace)575   IntegerType *getIndexTy(const DataLayout &DL, unsigned AddrSpace) {
576     return DL.getIndexType(Context, AddrSpace);
577   }
578 
579   //===--------------------------------------------------------------------===//
580   // Intrinsic creation methods
581   //===--------------------------------------------------------------------===//
582 
583   /// Create and insert a memset to the specified pointer and the
584   /// specified value.
585   ///
586   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
587   /// specified, it will be added to the instruction. Likewise with alias.scope
588   /// and noalias tags.
589   CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
590                          MaybeAlign Align, bool isVolatile = false,
591                          MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
592                          MDNode *NoAliasTag = nullptr) {
593     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
594                         TBAATag, ScopeTag, NoAliasTag);
595   }
596 
597   CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
598                          bool isVolatile = false, MDNode *TBAATag = nullptr,
599                          MDNode *ScopeTag = nullptr,
600                          MDNode *NoAliasTag = nullptr);
601 
602   CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val,
603                                Value *Size, bool IsVolatile = false,
604                                MDNode *TBAATag = nullptr,
605                                MDNode *ScopeTag = nullptr,
606                                MDNode *NoAliasTag = nullptr);
607 
608   /// Create and insert an element unordered-atomic memset of the region of
609   /// memory starting at the given pointer to the given value.
610   ///
611   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
612   /// specified, it will be added to the instruction. Likewise with alias.scope
613   /// and noalias tags.
614   CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
615                                                uint64_t Size, Align Alignment,
616                                                uint32_t ElementSize,
617                                                MDNode *TBAATag = nullptr,
618                                                MDNode *ScopeTag = nullptr,
619                                                MDNode *NoAliasTag = nullptr) {
620     return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
621                                               Align(Alignment), ElementSize,
622                                               TBAATag, ScopeTag, NoAliasTag);
623   }
624 
625   CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
626                          Value *ArraySize, ArrayRef<OperandBundleDef> OpB,
627                          Function *MallocF = nullptr, const Twine &Name = "");
628 
629   /// CreateMalloc - Generate the IR for a call to malloc:
630   /// 1. Compute the malloc call's argument as the specified type's size,
631   ///    possibly multiplied by the array size if the array size is not
632   ///    constant 1.
633   /// 2. Call malloc with that argument.
634   CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
635                          Value *ArraySize, Function *MallocF = nullptr,
636                          const Twine &Name = "");
637   /// Generate the IR for a call to the builtin free function.
638   CallInst *CreateFree(Value *Source,
639                        ArrayRef<OperandBundleDef> Bundles = std::nullopt);
640 
641   CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
642                                                Value *Size, Align Alignment,
643                                                uint32_t ElementSize,
644                                                MDNode *TBAATag = nullptr,
645                                                MDNode *ScopeTag = nullptr,
646                                                MDNode *NoAliasTag = nullptr);
647 
648   /// Create and insert a memcpy between the specified pointers.
649   ///
650   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
651   /// specified, it will be added to the instruction. Likewise with alias.scope
652   /// and noalias tags.
653   CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
654                          MaybeAlign SrcAlign, uint64_t Size,
655                          bool isVolatile = false, MDNode *TBAATag = nullptr,
656                          MDNode *TBAAStructTag = nullptr,
657                          MDNode *ScopeTag = nullptr,
658                          MDNode *NoAliasTag = nullptr) {
659     return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
660                         isVolatile, TBAATag, TBAAStructTag, ScopeTag,
661                         NoAliasTag);
662   }
663 
664   CallInst *CreateMemTransferInst(
665       Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
666       MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
667       MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
668       MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
669 
670   CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
671                          MaybeAlign SrcAlign, Value *Size,
672                          bool isVolatile = false, MDNode *TBAATag = nullptr,
673                          MDNode *TBAAStructTag = nullptr,
674                          MDNode *ScopeTag = nullptr,
675                          MDNode *NoAliasTag = nullptr) {
676     return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
677                                  SrcAlign, Size, isVolatile, TBAATag,
678                                  TBAAStructTag, ScopeTag, NoAliasTag);
679   }
680 
681   CallInst *
682   CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
683                      MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
684                      MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
685                      MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
686     return CreateMemTransferInst(Intrinsic::memcpy_inline, Dst, DstAlign, Src,
687                                  SrcAlign, Size, isVolatile, TBAATag,
688                                  TBAAStructTag, ScopeTag, NoAliasTag);
689   }
690 
691   /// Create and insert an element unordered-atomic memcpy between the
692   /// specified pointers.
693   ///
694   /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
695   ///
696   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
697   /// specified, it will be added to the instruction. Likewise with alias.scope
698   /// and noalias tags.
699   CallInst *CreateElementUnorderedAtomicMemCpy(
700       Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
701       uint32_t ElementSize, MDNode *TBAATag = nullptr,
702       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
703       MDNode *NoAliasTag = nullptr);
704 
705   CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
706                           MaybeAlign SrcAlign, uint64_t Size,
707                           bool isVolatile = false, MDNode *TBAATag = nullptr,
708                           MDNode *ScopeTag = nullptr,
709                           MDNode *NoAliasTag = nullptr) {
710     return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
711                          isVolatile, TBAATag, ScopeTag, NoAliasTag);
712   }
713 
714   CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
715                           MaybeAlign SrcAlign, Value *Size,
716                           bool isVolatile = false, MDNode *TBAATag = nullptr,
717                           MDNode *ScopeTag = nullptr,
718                           MDNode *NoAliasTag = nullptr) {
719     return CreateMemTransferInst(Intrinsic::memmove, Dst, DstAlign, Src,
720                                  SrcAlign, Size, isVolatile, TBAATag,
721                                  /*TBAAStructTag=*/nullptr, ScopeTag,
722                                  NoAliasTag);
723   }
724 
725   /// \brief Create and insert an element unordered-atomic memmove between the
726   /// specified pointers.
727   ///
728   /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
729   /// respectively.
730   ///
731   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
732   /// specified, it will be added to the instruction. Likewise with alias.scope
733   /// and noalias tags.
734   CallInst *CreateElementUnorderedAtomicMemMove(
735       Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
736       uint32_t ElementSize, MDNode *TBAATag = nullptr,
737       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
738       MDNode *NoAliasTag = nullptr);
739 
740 private:
741   CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src);
742 
743 public:
744   /// Create a sequential vector fadd reduction intrinsic of the source vector.
745   /// The first parameter is a scalar accumulator value. An unordered reduction
746   /// can be created by adding the reassoc fast-math flag to the resulting
747   /// sequential reduction.
748   CallInst *CreateFAddReduce(Value *Acc, Value *Src);
749 
750   /// Create a sequential vector fmul reduction intrinsic of the source vector.
751   /// The first parameter is a scalar accumulator value. An unordered reduction
752   /// can be created by adding the reassoc fast-math flag to the resulting
753   /// sequential reduction.
754   CallInst *CreateFMulReduce(Value *Acc, Value *Src);
755 
756   /// Create a vector int add reduction intrinsic of the source vector.
757   CallInst *CreateAddReduce(Value *Src);
758 
759   /// Create a vector int mul reduction intrinsic of the source vector.
760   CallInst *CreateMulReduce(Value *Src);
761 
762   /// Create a vector int AND reduction intrinsic of the source vector.
763   CallInst *CreateAndReduce(Value *Src);
764 
765   /// Create a vector int OR reduction intrinsic of the source vector.
766   CallInst *CreateOrReduce(Value *Src);
767 
768   /// Create a vector int XOR reduction intrinsic of the source vector.
769   CallInst *CreateXorReduce(Value *Src);
770 
771   /// Create a vector integer max reduction intrinsic of the source
772   /// vector.
773   CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
774 
775   /// Create a vector integer min reduction intrinsic of the source
776   /// vector.
777   CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
778 
779   /// Create a vector float max reduction intrinsic of the source
780   /// vector.
781   CallInst *CreateFPMaxReduce(Value *Src);
782 
783   /// Create a vector float min reduction intrinsic of the source
784   /// vector.
785   CallInst *CreateFPMinReduce(Value *Src);
786 
787   /// Create a vector float maximum reduction intrinsic of the source
788   /// vector. This variant follows the NaN and signed zero semantic of
789   /// llvm.maximum intrinsic.
790   CallInst *CreateFPMaximumReduce(Value *Src);
791 
792   /// Create a vector float minimum reduction intrinsic of the source
793   /// vector. This variant follows the NaN and signed zero semantic of
794   /// llvm.minimum intrinsic.
795   CallInst *CreateFPMinimumReduce(Value *Src);
796 
797   /// Create a lifetime.start intrinsic.
798   ///
799   /// If the pointer isn't i8* it will be converted.
800   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
801 
802   /// Create a lifetime.end intrinsic.
803   ///
804   /// If the pointer isn't i8* it will be converted.
805   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
806 
807   /// Create a call to invariant.start intrinsic.
808   ///
809   /// If the pointer isn't i8* it will be converted.
810   CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
811 
812   /// Create a call to llvm.threadlocal.address intrinsic.
813   CallInst *CreateThreadLocalAddress(Value *Ptr);
814 
815   /// Create a call to Masked Load intrinsic
816   CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask,
817                              Value *PassThru = nullptr, const Twine &Name = "");
818 
819   /// Create a call to Masked Store intrinsic
820   CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
821                               Value *Mask);
822 
823   /// Create a call to Masked Gather intrinsic
824   CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment,
825                                Value *Mask = nullptr, Value *PassThru = nullptr,
826                                const Twine &Name = "");
827 
828   /// Create a call to Masked Scatter intrinsic
829   CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
830                                 Value *Mask = nullptr);
831 
832   /// Create a call to Masked Expand Load intrinsic
833   CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, Value *Mask = nullptr,
834                                    Value *PassThru = nullptr,
835                                    const Twine &Name = "");
836 
837   /// Create a call to Masked Compress Store intrinsic
838   CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr,
839                                       Value *Mask = nullptr);
840 
841   /// Return an all true boolean vector (mask) with \p NumElts lanes.
getAllOnesMask(ElementCount NumElts)842   Value *getAllOnesMask(ElementCount NumElts) {
843     VectorType *VTy = VectorType::get(Type::getInt1Ty(Context), NumElts);
844     return Constant::getAllOnesValue(VTy);
845   }
846 
847   /// Create an assume intrinsic call that allows the optimizer to
848   /// assume that the provided condition will be true.
849   ///
850   /// The optional argument \p OpBundles specifies operand bundles that are
851   /// added to the call instruction.
852   CallInst *
853   CreateAssumption(Value *Cond,
854                    ArrayRef<OperandBundleDef> OpBundles = std::nullopt);
855 
856   /// Create a llvm.experimental.noalias.scope.decl intrinsic call.
857   Instruction *CreateNoAliasScopeDeclaration(Value *Scope);
CreateNoAliasScopeDeclaration(MDNode * ScopeTag)858   Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) {
859     return CreateNoAliasScopeDeclaration(
860         MetadataAsValue::get(Context, ScopeTag));
861   }
862 
863   /// Create a call to the experimental.gc.statepoint intrinsic to
864   /// start a new statepoint sequence.
865   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
866                                    FunctionCallee ActualCallee,
867                                    ArrayRef<Value *> CallArgs,
868                                    std::optional<ArrayRef<Value *>> DeoptArgs,
869                                    ArrayRef<Value *> GCArgs,
870                                    const Twine &Name = "");
871 
872   /// Create a call to the experimental.gc.statepoint intrinsic to
873   /// start a new statepoint sequence.
874   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
875                                    FunctionCallee ActualCallee, uint32_t Flags,
876                                    ArrayRef<Value *> CallArgs,
877                                    std::optional<ArrayRef<Use>> TransitionArgs,
878                                    std::optional<ArrayRef<Use>> DeoptArgs,
879                                    ArrayRef<Value *> GCArgs,
880                                    const Twine &Name = "");
881 
882   /// Conveninence function for the common case when CallArgs are filled
883   /// in using ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
884   /// .get()'ed to get the Value pointer.
885   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
886                                    FunctionCallee ActualCallee,
887                                    ArrayRef<Use> CallArgs,
888                                    std::optional<ArrayRef<Value *>> DeoptArgs,
889                                    ArrayRef<Value *> GCArgs,
890                                    const Twine &Name = "");
891 
892   /// Create an invoke to the experimental.gc.statepoint intrinsic to
893   /// start a new statepoint sequence.
894   InvokeInst *
895   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
896                            FunctionCallee ActualInvokee, BasicBlock *NormalDest,
897                            BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
898                            std::optional<ArrayRef<Value *>> DeoptArgs,
899                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
900 
901   /// Create an invoke to the experimental.gc.statepoint intrinsic to
902   /// start a new statepoint sequence.
903   InvokeInst *CreateGCStatepointInvoke(
904       uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee,
905       BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
906       ArrayRef<Value *> InvokeArgs, std::optional<ArrayRef<Use>> TransitionArgs,
907       std::optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
908       const Twine &Name = "");
909 
910   // Convenience function for the common case when CallArgs are filled in using
911   // ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
912   // get the Value *.
913   InvokeInst *
914   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
915                            FunctionCallee ActualInvokee, BasicBlock *NormalDest,
916                            BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
917                            std::optional<ArrayRef<Value *>> DeoptArgs,
918                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
919 
920   /// Create a call to the experimental.gc.result intrinsic to extract
921   /// the result from a call wrapped in a statepoint.
922   CallInst *CreateGCResult(Instruction *Statepoint,
923                            Type *ResultType,
924                            const Twine &Name = "");
925 
926   /// Create a call to the experimental.gc.relocate intrinsics to
927   /// project the relocated value of one pointer from the statepoint.
928   CallInst *CreateGCRelocate(Instruction *Statepoint,
929                              int BaseOffset,
930                              int DerivedOffset,
931                              Type *ResultType,
932                              const Twine &Name = "");
933 
934   /// Create a call to the experimental.gc.pointer.base intrinsic to get the
935   /// base pointer for the specified derived pointer.
936   CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = "");
937 
938   /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get
939   /// the offset of the specified derived pointer from its base.
940   CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = "");
941 
942   /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
943   /// will be the same type as that of \p Scaling.
944   Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
945 
946   /// Create an expression which evaluates to the number of elements in \p EC
947   /// at runtime.
948   Value *CreateElementCount(Type *DstType, ElementCount EC);
949 
950   /// Create an expression which evaluates to the number of units in \p Size
951   /// at runtime.  This works for both units of bits and bytes.
952   Value *CreateTypeSize(Type *DstType, TypeSize Size);
953 
954   /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
955   Value *CreateStepVector(Type *DstType, const Twine &Name = "");
956 
957   /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
958   /// type.
959   CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
960                                  Instruction *FMFSource = nullptr,
961                                  const Twine &Name = "");
962 
963   /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
964   /// first type.
965   CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
966                                   Instruction *FMFSource = nullptr,
967                                   const Twine &Name = "");
968 
969   /// Create a call to intrinsic \p ID with \p Args, mangled using \p Types. If
970   /// \p FMFSource is provided, copy fast-math-flags from that instruction to
971   /// the intrinsic.
972   CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
973                             ArrayRef<Value *> Args,
974                             Instruction *FMFSource = nullptr,
975                             const Twine &Name = "");
976 
977   /// Create a call to intrinsic \p ID with \p RetTy and \p Args. If
978   /// \p FMFSource is provided, copy fast-math-flags from that instruction to
979   /// the intrinsic.
980   CallInst *CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
981                             ArrayRef<Value *> Args,
982                             Instruction *FMFSource = nullptr,
983                             const Twine &Name = "");
984 
985   /// Create call to the minnum intrinsic.
986   CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
987     if (IsFPConstrained) {
988       return CreateConstrainedFPUnroundedBinOp(
989           Intrinsic::experimental_constrained_minnum, LHS, RHS, nullptr, Name);
990     }
991 
992     return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
993   }
994 
995   /// Create call to the maxnum intrinsic.
996   CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
997     if (IsFPConstrained) {
998       return CreateConstrainedFPUnroundedBinOp(
999           Intrinsic::experimental_constrained_maxnum, LHS, RHS, nullptr, Name);
1000     }
1001 
1002     return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
1003   }
1004 
1005   /// Create call to the minimum intrinsic.
1006   CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
1007     return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
1008   }
1009 
1010   /// Create call to the maximum intrinsic.
1011   CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
1012     return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
1013   }
1014 
1015   /// Create call to the copysign intrinsic.
1016   CallInst *CreateCopySign(Value *LHS, Value *RHS,
1017                            Instruction *FMFSource = nullptr,
1018                            const Twine &Name = "") {
1019     return CreateBinaryIntrinsic(Intrinsic::copysign, LHS, RHS, FMFSource,
1020                                  Name);
1021   }
1022 
1023   /// Create a call to the arithmetic_fence intrinsic.
1024   CallInst *CreateArithmeticFence(Value *Val, Type *DstType,
1025                                   const Twine &Name = "") {
1026     return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr,
1027                            Name);
1028   }
1029 
1030   /// Create a call to the vector.extract intrinsic.
1031   CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx,
1032                                 const Twine &Name = "") {
1033     return CreateIntrinsic(Intrinsic::vector_extract,
1034                            {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr,
1035                            Name);
1036   }
1037 
1038   /// Create a call to the vector.insert intrinsic.
1039   CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
1040                                Value *Idx, const Twine &Name = "") {
1041     return CreateIntrinsic(Intrinsic::vector_insert,
1042                            {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx},
1043                            nullptr, Name);
1044   }
1045 
1046   /// Create a call to llvm.stacksave
1047   CallInst *CreateStackSave(const Twine &Name = "") {
1048     const DataLayout &DL = BB->getModule()->getDataLayout();
1049     return CreateIntrinsic(Intrinsic::stacksave, {DL.getAllocaPtrType(Context)},
1050                            {}, nullptr, Name);
1051   }
1052 
1053   /// Create a call to llvm.stackrestore
1054   CallInst *CreateStackRestore(Value *Ptr, const Twine &Name = "") {
1055     return CreateIntrinsic(Intrinsic::stackrestore, {Ptr->getType()}, {Ptr},
1056                            nullptr, Name);
1057   }
1058 
1059 private:
1060   /// Create a call to a masked intrinsic with given Id.
1061   CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
1062                                   ArrayRef<Type *> OverloadedTypes,
1063                                   const Twine &Name = "");
1064 
1065   //===--------------------------------------------------------------------===//
1066   // Instruction creation methods: Terminators
1067   //===--------------------------------------------------------------------===//
1068 
1069 private:
1070   /// Helper to add branch weight and unpredictable metadata onto an
1071   /// instruction.
1072   /// \returns The annotated instruction.
1073   template <typename InstTy>
addBranchMetadata(InstTy * I,MDNode * Weights,MDNode * Unpredictable)1074   InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
1075     if (Weights)
1076       I->setMetadata(LLVMContext::MD_prof, Weights);
1077     if (Unpredictable)
1078       I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
1079     return I;
1080   }
1081 
1082 public:
1083   /// Create a 'ret void' instruction.
CreateRetVoid()1084   ReturnInst *CreateRetVoid() {
1085     return Insert(ReturnInst::Create(Context));
1086   }
1087 
1088   /// Create a 'ret <val>' instruction.
CreateRet(Value * V)1089   ReturnInst *CreateRet(Value *V) {
1090     return Insert(ReturnInst::Create(Context, V));
1091   }
1092 
1093   /// Create a sequence of N insertvalue instructions,
1094   /// with one Value from the retVals array each, that build a aggregate
1095   /// return value one value at a time, and a ret instruction to return
1096   /// the resulting aggregate value.
1097   ///
1098   /// This is a convenience function for code that uses aggregate return values
1099   /// as a vehicle for having multiple return values.
CreateAggregateRet(Value * const * retVals,unsigned N)1100   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
1101     Value *V = PoisonValue::get(getCurrentFunctionReturnType());
1102     for (unsigned i = 0; i != N; ++i)
1103       V = CreateInsertValue(V, retVals[i], i, "mrv");
1104     return Insert(ReturnInst::Create(Context, V));
1105   }
1106 
1107   /// Create an unconditional 'br label X' instruction.
CreateBr(BasicBlock * Dest)1108   BranchInst *CreateBr(BasicBlock *Dest) {
1109     return Insert(BranchInst::Create(Dest));
1110   }
1111 
1112   /// Create a conditional 'br Cond, TrueDest, FalseDest'
1113   /// instruction.
1114   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1115                            MDNode *BranchWeights = nullptr,
1116                            MDNode *Unpredictable = nullptr) {
1117     return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
1118                                     BranchWeights, Unpredictable));
1119   }
1120 
1121   /// Create a conditional 'br Cond, TrueDest, FalseDest'
1122   /// instruction. Copy branch meta data if available.
CreateCondBr(Value * Cond,BasicBlock * True,BasicBlock * False,Instruction * MDSrc)1123   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1124                            Instruction *MDSrc) {
1125     BranchInst *Br = BranchInst::Create(True, False, Cond);
1126     if (MDSrc) {
1127       unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
1128                         LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
1129       Br->copyMetadata(*MDSrc, WL);
1130     }
1131     return Insert(Br);
1132   }
1133 
1134   /// Create a switch instruction with the specified value, default dest,
1135   /// and with a hint for the number of cases that will be added (for efficient
1136   /// allocation).
1137   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1138                            MDNode *BranchWeights = nullptr,
1139                            MDNode *Unpredictable = nullptr) {
1140     return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
1141                                     BranchWeights, Unpredictable));
1142   }
1143 
1144   /// Create an indirect branch instruction with the specified address
1145   /// operand, with an optional hint for the number of destinations that will be
1146   /// added (for efficient allocation).
1147   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1148     return Insert(IndirectBrInst::Create(Addr, NumDests));
1149   }
1150 
1151   /// Create an invoke instruction.
1152   InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1153                            BasicBlock *NormalDest, BasicBlock *UnwindDest,
1154                            ArrayRef<Value *> Args,
1155                            ArrayRef<OperandBundleDef> OpBundles,
1156                            const Twine &Name = "") {
1157     InvokeInst *II =
1158         InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles);
1159     if (IsFPConstrained)
1160       setConstrainedFPCallAttr(II);
1161     return Insert(II, Name);
1162   }
1163   InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1164                            BasicBlock *NormalDest, BasicBlock *UnwindDest,
1165                            ArrayRef<Value *> Args = std::nullopt,
1166                            const Twine &Name = "") {
1167     InvokeInst *II =
1168         InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args);
1169     if (IsFPConstrained)
1170       setConstrainedFPCallAttr(II);
1171     return Insert(II, Name);
1172   }
1173 
1174   InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1175                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1176                            ArrayRef<OperandBundleDef> OpBundles,
1177                            const Twine &Name = "") {
1178     return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1179                         NormalDest, UnwindDest, Args, OpBundles, Name);
1180   }
1181 
1182   InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1183                            BasicBlock *UnwindDest,
1184                            ArrayRef<Value *> Args = std::nullopt,
1185                            const Twine &Name = "") {
1186     return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1187                         NormalDest, UnwindDest, Args, Name);
1188   }
1189 
1190   /// \brief Create a callbr instruction.
1191   CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1192                            BasicBlock *DefaultDest,
1193                            ArrayRef<BasicBlock *> IndirectDests,
1194                            ArrayRef<Value *> Args = std::nullopt,
1195                            const Twine &Name = "") {
1196     return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
1197                                      Args), Name);
1198   }
1199   CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1200                            BasicBlock *DefaultDest,
1201                            ArrayRef<BasicBlock *> IndirectDests,
1202                            ArrayRef<Value *> Args,
1203                            ArrayRef<OperandBundleDef> OpBundles,
1204                            const Twine &Name = "") {
1205     return Insert(
1206         CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1207                            OpBundles), Name);
1208   }
1209 
1210   CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1211                            ArrayRef<BasicBlock *> IndirectDests,
1212                            ArrayRef<Value *> Args = std::nullopt,
1213                            const Twine &Name = "") {
1214     return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1215                         DefaultDest, IndirectDests, Args, Name);
1216   }
1217   CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1218                            ArrayRef<BasicBlock *> IndirectDests,
1219                            ArrayRef<Value *> Args,
1220                            ArrayRef<OperandBundleDef> OpBundles,
1221                            const Twine &Name = "") {
1222     return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1223                         DefaultDest, IndirectDests, Args, Name);
1224   }
1225 
CreateResume(Value * Exn)1226   ResumeInst *CreateResume(Value *Exn) {
1227     return Insert(ResumeInst::Create(Exn));
1228   }
1229 
1230   CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1231                                       BasicBlock *UnwindBB = nullptr) {
1232     return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1233   }
1234 
1235   CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1236                                      unsigned NumHandlers,
1237                                      const Twine &Name = "") {
1238     return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1239                   Name);
1240   }
1241 
1242   CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1243                                const Twine &Name = "") {
1244     return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1245   }
1246 
1247   CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1248                                    ArrayRef<Value *> Args = std::nullopt,
1249                                    const Twine &Name = "") {
1250     return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1251   }
1252 
CreateCatchRet(CatchPadInst * CatchPad,BasicBlock * BB)1253   CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1254     return Insert(CatchReturnInst::Create(CatchPad, BB));
1255   }
1256 
CreateUnreachable()1257   UnreachableInst *CreateUnreachable() {
1258     return Insert(new UnreachableInst(Context));
1259   }
1260 
1261   //===--------------------------------------------------------------------===//
1262   // Instruction creation methods: Binary Operators
1263   //===--------------------------------------------------------------------===//
1264 private:
CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,Value * LHS,Value * RHS,const Twine & Name,bool HasNUW,bool HasNSW)1265   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1266                                           Value *LHS, Value *RHS,
1267                                           const Twine &Name,
1268                                           bool HasNUW, bool HasNSW) {
1269     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1270     if (HasNUW) BO->setHasNoUnsignedWrap();
1271     if (HasNSW) BO->setHasNoSignedWrap();
1272     return BO;
1273   }
1274 
setFPAttrs(Instruction * I,MDNode * FPMD,FastMathFlags FMF)1275   Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1276                           FastMathFlags FMF) const {
1277     if (!FPMD)
1278       FPMD = DefaultFPMathTag;
1279     if (FPMD)
1280       I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1281     I->setFastMathFlags(FMF);
1282     return I;
1283   }
1284 
getConstrainedFPRounding(std::optional<RoundingMode> Rounding)1285   Value *getConstrainedFPRounding(std::optional<RoundingMode> Rounding) {
1286     RoundingMode UseRounding = DefaultConstrainedRounding;
1287 
1288     if (Rounding)
1289       UseRounding = *Rounding;
1290 
1291     std::optional<StringRef> RoundingStr =
1292         convertRoundingModeToStr(UseRounding);
1293     assert(RoundingStr && "Garbage strict rounding mode!");
1294     auto *RoundingMDS = MDString::get(Context, *RoundingStr);
1295 
1296     return MetadataAsValue::get(Context, RoundingMDS);
1297   }
1298 
getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except)1299   Value *getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except) {
1300     std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(
1301         Except.value_or(DefaultConstrainedExcept));
1302     assert(ExceptStr && "Garbage strict exception behavior!");
1303     auto *ExceptMDS = MDString::get(Context, *ExceptStr);
1304 
1305     return MetadataAsValue::get(Context, ExceptMDS);
1306   }
1307 
getConstrainedFPPredicate(CmpInst::Predicate Predicate)1308   Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1309     assert(CmpInst::isFPPredicate(Predicate) &&
1310            Predicate != CmpInst::FCMP_FALSE &&
1311            Predicate != CmpInst::FCMP_TRUE &&
1312            "Invalid constrained FP comparison predicate!");
1313 
1314     StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
1315     auto *PredicateMDS = MDString::get(Context, PredicateStr);
1316 
1317     return MetadataAsValue::get(Context, PredicateMDS);
1318   }
1319 
1320 public:
1321   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1322                    bool HasNUW = false, bool HasNSW = false) {
1323     if (Value *V =
1324             Folder.FoldNoWrapBinOp(Instruction::Add, LHS, RHS, HasNUW, HasNSW))
1325       return V;
1326     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, HasNUW,
1327                                    HasNSW);
1328   }
1329 
1330   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1331     return CreateAdd(LHS, RHS, Name, false, true);
1332   }
1333 
1334   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1335     return CreateAdd(LHS, RHS, Name, true, false);
1336   }
1337 
1338   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1339                    bool HasNUW = false, bool HasNSW = false) {
1340     if (Value *V =
1341             Folder.FoldNoWrapBinOp(Instruction::Sub, LHS, RHS, HasNUW, HasNSW))
1342       return V;
1343     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, HasNUW,
1344                                    HasNSW);
1345   }
1346 
1347   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1348     return CreateSub(LHS, RHS, Name, false, true);
1349   }
1350 
1351   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1352     return CreateSub(LHS, RHS, Name, true, false);
1353   }
1354 
1355   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1356                    bool HasNUW = false, bool HasNSW = false) {
1357     if (Value *V =
1358             Folder.FoldNoWrapBinOp(Instruction::Mul, LHS, RHS, HasNUW, HasNSW))
1359       return V;
1360     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, HasNUW,
1361                                    HasNSW);
1362   }
1363 
1364   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1365     return CreateMul(LHS, RHS, Name, false, true);
1366   }
1367 
1368   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1369     return CreateMul(LHS, RHS, Name, true, false);
1370   }
1371 
1372   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1373                     bool isExact = false) {
1374     if (Value *V = Folder.FoldExactBinOp(Instruction::UDiv, LHS, RHS, isExact))
1375       return V;
1376     if (!isExact)
1377       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1378     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1379   }
1380 
1381   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1382     return CreateUDiv(LHS, RHS, Name, true);
1383   }
1384 
1385   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1386                     bool isExact = false) {
1387     if (Value *V = Folder.FoldExactBinOp(Instruction::SDiv, LHS, RHS, isExact))
1388       return V;
1389     if (!isExact)
1390       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1391     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1392   }
1393 
1394   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1395     return CreateSDiv(LHS, RHS, Name, true);
1396   }
1397 
1398   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1399     if (Value *V = Folder.FoldBinOp(Instruction::URem, LHS, RHS))
1400       return V;
1401     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1402   }
1403 
1404   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1405     if (Value *V = Folder.FoldBinOp(Instruction::SRem, LHS, RHS))
1406       return V;
1407     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1408   }
1409 
1410   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1411                    bool HasNUW = false, bool HasNSW = false) {
1412     if (Value *V =
1413             Folder.FoldNoWrapBinOp(Instruction::Shl, LHS, RHS, HasNUW, HasNSW))
1414       return V;
1415     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1416                                    HasNUW, HasNSW);
1417   }
1418 
1419   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1420                    bool HasNUW = false, bool HasNSW = false) {
1421     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1422                      HasNUW, HasNSW);
1423   }
1424 
1425   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1426                    bool HasNUW = false, bool HasNSW = false) {
1427     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1428                      HasNUW, HasNSW);
1429   }
1430 
1431   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1432                     bool isExact = false) {
1433     if (Value *V = Folder.FoldExactBinOp(Instruction::LShr, LHS, RHS, isExact))
1434       return V;
1435     if (!isExact)
1436       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1437     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1438   }
1439 
1440   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1441                     bool isExact = false) {
1442     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1443   }
1444 
1445   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1446                     bool isExact = false) {
1447     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1448   }
1449 
1450   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1451                     bool isExact = false) {
1452     if (Value *V = Folder.FoldExactBinOp(Instruction::AShr, LHS, RHS, isExact))
1453       return V;
1454     if (!isExact)
1455       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1456     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1457   }
1458 
1459   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1460                     bool isExact = false) {
1461     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1462   }
1463 
1464   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1465                     bool isExact = false) {
1466     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1467   }
1468 
1469   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1470     if (auto *V = Folder.FoldBinOp(Instruction::And, LHS, RHS))
1471       return V;
1472     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1473   }
1474 
1475   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1476     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1477   }
1478 
1479   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1480     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1481   }
1482 
CreateAnd(ArrayRef<Value * > Ops)1483   Value *CreateAnd(ArrayRef<Value*> Ops) {
1484     assert(!Ops.empty());
1485     Value *Accum = Ops[0];
1486     for (unsigned i = 1; i < Ops.size(); i++)
1487       Accum = CreateAnd(Accum, Ops[i]);
1488     return Accum;
1489   }
1490 
1491   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1492     if (auto *V = Folder.FoldBinOp(Instruction::Or, LHS, RHS))
1493       return V;
1494     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1495   }
1496 
1497   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1498     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1499   }
1500 
1501   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1502     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1503   }
1504 
CreateOr(ArrayRef<Value * > Ops)1505   Value *CreateOr(ArrayRef<Value*> Ops) {
1506     assert(!Ops.empty());
1507     Value *Accum = Ops[0];
1508     for (unsigned i = 1; i < Ops.size(); i++)
1509       Accum = CreateOr(Accum, Ops[i]);
1510     return Accum;
1511   }
1512 
1513   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1514     if (Value *V = Folder.FoldBinOp(Instruction::Xor, LHS, RHS))
1515       return V;
1516     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1517   }
1518 
1519   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1520     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1521   }
1522 
1523   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1524     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1525   }
1526 
1527   Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1528                     MDNode *FPMD = nullptr) {
1529     if (IsFPConstrained)
1530       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1531                                       L, R, nullptr, Name, FPMD);
1532 
1533     if (Value *V = Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMF))
1534       return V;
1535     Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1536     return Insert(I, Name);
1537   }
1538 
1539   /// Copy fast-math-flags from an instruction rather than using the builder's
1540   /// default FMF.
1541   Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1542                        const Twine &Name = "") {
1543     if (IsFPConstrained)
1544       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1545                                       L, R, FMFSource, Name);
1546 
1547     FastMathFlags FMF = FMFSource->getFastMathFlags();
1548     if (Value *V = Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMF))
1549       return V;
1550     Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr, FMF);
1551     return Insert(I, Name);
1552   }
1553 
1554   Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1555                     MDNode *FPMD = nullptr) {
1556     if (IsFPConstrained)
1557       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1558                                       L, R, nullptr, Name, FPMD);
1559 
1560     if (Value *V = Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMF))
1561       return V;
1562     Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1563     return Insert(I, Name);
1564   }
1565 
1566   /// Copy fast-math-flags from an instruction rather than using the builder's
1567   /// default FMF.
1568   Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1569                        const Twine &Name = "") {
1570     if (IsFPConstrained)
1571       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1572                                       L, R, FMFSource, Name);
1573 
1574     FastMathFlags FMF = FMFSource->getFastMathFlags();
1575     if (Value *V = Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMF))
1576       return V;
1577     Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr, FMF);
1578     return Insert(I, Name);
1579   }
1580 
1581   Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1582                     MDNode *FPMD = nullptr) {
1583     if (IsFPConstrained)
1584       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1585                                       L, R, nullptr, Name, FPMD);
1586 
1587     if (Value *V = Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMF))
1588       return V;
1589     Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1590     return Insert(I, Name);
1591   }
1592 
1593   /// Copy fast-math-flags from an instruction rather than using the builder's
1594   /// default FMF.
1595   Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1596                        const Twine &Name = "") {
1597     if (IsFPConstrained)
1598       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1599                                       L, R, FMFSource, Name);
1600 
1601     FastMathFlags FMF = FMFSource->getFastMathFlags();
1602     if (Value *V = Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMF))
1603       return V;
1604     Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr, FMF);
1605     return Insert(I, Name);
1606   }
1607 
1608   Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1609                     MDNode *FPMD = nullptr) {
1610     if (IsFPConstrained)
1611       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1612                                       L, R, nullptr, Name, FPMD);
1613 
1614     if (Value *V = Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMF))
1615       return V;
1616     Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1617     return Insert(I, Name);
1618   }
1619 
1620   /// Copy fast-math-flags from an instruction rather than using the builder's
1621   /// default FMF.
1622   Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1623                        const Twine &Name = "") {
1624     if (IsFPConstrained)
1625       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1626                                       L, R, FMFSource, Name);
1627 
1628     FastMathFlags FMF = FMFSource->getFastMathFlags();
1629     if (Value *V = Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMF))
1630       return V;
1631     Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr, FMF);
1632     return Insert(I, Name);
1633   }
1634 
1635   Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1636                     MDNode *FPMD = nullptr) {
1637     if (IsFPConstrained)
1638       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1639                                       L, R, nullptr, Name, FPMD);
1640 
1641     if (Value *V = Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMF)) return V;
1642     Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1643     return Insert(I, Name);
1644   }
1645 
1646   /// Copy fast-math-flags from an instruction rather than using the builder's
1647   /// default FMF.
1648   Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1649                        const Twine &Name = "") {
1650     if (IsFPConstrained)
1651       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1652                                       L, R, FMFSource, Name);
1653 
1654     FastMathFlags FMF = FMFSource->getFastMathFlags();
1655     if (Value *V = Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMF)) return V;
1656     Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr, FMF);
1657     return Insert(I, Name);
1658   }
1659 
1660   Value *CreateBinOp(Instruction::BinaryOps Opc,
1661                      Value *LHS, Value *RHS, const Twine &Name = "",
1662                      MDNode *FPMathTag = nullptr) {
1663     if (Value *V = Folder.FoldBinOp(Opc, LHS, RHS)) return V;
1664     Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1665     if (isa<FPMathOperator>(BinOp))
1666       setFPAttrs(BinOp, FPMathTag, FMF);
1667     return Insert(BinOp, Name);
1668   }
1669 
1670   Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1671     assert(Cond2->getType()->isIntOrIntVectorTy(1));
1672     return CreateSelect(Cond1, Cond2,
1673                         ConstantInt::getNullValue(Cond2->getType()), Name);
1674   }
1675 
1676   Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1677     assert(Cond2->getType()->isIntOrIntVectorTy(1));
1678     return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()),
1679                         Cond2, Name);
1680   }
1681 
1682   Value *CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2,
1683                          const Twine &Name = "") {
1684     switch (Opc) {
1685     case Instruction::And:
1686       return CreateLogicalAnd(Cond1, Cond2, Name);
1687     case Instruction::Or:
1688       return CreateLogicalOr(Cond1, Cond2, Name);
1689     default:
1690       break;
1691     }
1692     llvm_unreachable("Not a logical operation.");
1693   }
1694 
1695   // NOTE: this is sequential, non-commutative, ordered reduction!
CreateLogicalOr(ArrayRef<Value * > Ops)1696   Value *CreateLogicalOr(ArrayRef<Value *> Ops) {
1697     assert(!Ops.empty());
1698     Value *Accum = Ops[0];
1699     for (unsigned i = 1; i < Ops.size(); i++)
1700       Accum = CreateLogicalOr(Accum, Ops[i]);
1701     return Accum;
1702   }
1703 
1704   CallInst *CreateConstrainedFPBinOp(
1705       Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1706       const Twine &Name = "", MDNode *FPMathTag = nullptr,
1707       std::optional<RoundingMode> Rounding = std::nullopt,
1708       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1709 
1710   CallInst *CreateConstrainedFPUnroundedBinOp(
1711       Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1712       const Twine &Name = "", MDNode *FPMathTag = nullptr,
1713       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1714 
1715   Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNUW = false,
1716                    bool HasNSW = false) {
1717     return CreateSub(Constant::getNullValue(V->getType()), V, Name, HasNUW,
1718                      HasNSW);
1719   }
1720 
1721   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1722     return CreateNeg(V, Name, false, true);
1723   }
1724 
1725   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1726     return CreateNeg(V, Name, true, false);
1727   }
1728 
1729   Value *CreateFNeg(Value *V, const Twine &Name = "",
1730                     MDNode *FPMathTag = nullptr) {
1731     if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF))
1732       return Res;
1733     return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMF),
1734                   Name);
1735   }
1736 
1737   /// Copy fast-math-flags from an instruction rather than using the builder's
1738   /// default FMF.
1739   Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
1740                        const Twine &Name = "") {
1741    FastMathFlags FMF = FMFSource->getFastMathFlags();
1742     if (Value *Res = Folder.FoldUnOpFMF(Instruction::FNeg, V, FMF))
1743       return Res;
1744    return Insert(setFPAttrs(UnaryOperator::CreateFNeg(V), nullptr, FMF),
1745                  Name);
1746   }
1747 
1748   Value *CreateNot(Value *V, const Twine &Name = "") {
1749     return CreateXor(V, Constant::getAllOnesValue(V->getType()), Name);
1750   }
1751 
1752   Value *CreateUnOp(Instruction::UnaryOps Opc,
1753                     Value *V, const Twine &Name = "",
1754                     MDNode *FPMathTag = nullptr) {
1755     if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF))
1756       return Res;
1757     Instruction *UnOp = UnaryOperator::Create(Opc, V);
1758     if (isa<FPMathOperator>(UnOp))
1759       setFPAttrs(UnOp, FPMathTag, FMF);
1760     return Insert(UnOp, Name);
1761   }
1762 
1763   /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1764   /// Correct number of operands must be passed accordingly.
1765   Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1766                       const Twine &Name = "", MDNode *FPMathTag = nullptr);
1767 
1768   //===--------------------------------------------------------------------===//
1769   // Instruction creation methods: Memory Instructions
1770   //===--------------------------------------------------------------------===//
1771 
1772   AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1773                            Value *ArraySize = nullptr, const Twine &Name = "") {
1774     const DataLayout &DL = BB->getModule()->getDataLayout();
1775     Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1776     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1777   }
1778 
1779   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1780                            const Twine &Name = "") {
1781     const DataLayout &DL = BB->getModule()->getDataLayout();
1782     Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1783     unsigned AddrSpace = DL.getAllocaAddrSpace();
1784     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1785   }
1786 
1787   /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1788   /// converting the string to 'bool' for the isVolatile parameter.
CreateLoad(Type * Ty,Value * Ptr,const char * Name)1789   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1790     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1791   }
1792 
1793   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1794     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1795   }
1796 
1797   LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1798                        const Twine &Name = "") {
1799     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name);
1800   }
1801 
1802   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1803     return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile);
1804   }
1805 
CreateAlignedLoad(Type * Ty,Value * Ptr,MaybeAlign Align,const char * Name)1806   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1807                               const char *Name) {
1808     return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1809   }
1810 
1811   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1812                               const Twine &Name = "") {
1813     return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1814   }
1815 
1816   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1817                               bool isVolatile, const Twine &Name = "") {
1818     if (!Align) {
1819       const DataLayout &DL = BB->getModule()->getDataLayout();
1820       Align = DL.getABITypeAlign(Ty);
1821     }
1822     return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
1823   }
1824 
1825   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
1826                                 bool isVolatile = false) {
1827     if (!Align) {
1828       const DataLayout &DL = BB->getModule()->getDataLayout();
1829       Align = DL.getABITypeAlign(Val->getType());
1830     }
1831     return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
1832   }
1833   FenceInst *CreateFence(AtomicOrdering Ordering,
1834                          SyncScope::ID SSID = SyncScope::System,
1835                          const Twine &Name = "") {
1836     return Insert(new FenceInst(Context, Ordering, SSID), Name);
1837   }
1838 
1839   AtomicCmpXchgInst *
1840   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1841                       AtomicOrdering SuccessOrdering,
1842                       AtomicOrdering FailureOrdering,
1843                       SyncScope::ID SSID = SyncScope::System) {
1844     if (!Align) {
1845       const DataLayout &DL = BB->getModule()->getDataLayout();
1846       Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
1847     }
1848 
1849     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering,
1850                                         FailureOrdering, SSID));
1851   }
1852 
1853   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr,
1854                                  Value *Val, MaybeAlign Align,
1855                                  AtomicOrdering Ordering,
1856                                  SyncScope::ID SSID = SyncScope::System) {
1857     if (!Align) {
1858       const DataLayout &DL = BB->getModule()->getDataLayout();
1859       Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
1860     }
1861 
1862     return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID));
1863   }
1864 
1865   Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1866                    const Twine &Name = "", bool IsInBounds = false) {
1867     if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, IsInBounds))
1868       return V;
1869     return Insert(IsInBounds
1870                       ? GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList)
1871                       : GetElementPtrInst::Create(Ty, Ptr, IdxList),
1872                   Name);
1873   }
1874 
1875   Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1876                            const Twine &Name = "") {
1877     return CreateGEP(Ty, Ptr, IdxList, Name, /* IsInBounds */ true);
1878   }
1879 
1880   Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1881                             const Twine &Name = "") {
1882     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1883 
1884     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false))
1885       return V;
1886 
1887     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1888   }
1889 
1890   Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1891                                     const Twine &Name = "") {
1892     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1893 
1894     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true))
1895       return V;
1896 
1897     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1898   }
1899 
1900   Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1901                             const Twine &Name = "") {
1902     Value *Idxs[] = {
1903       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1904       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1905     };
1906 
1907     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false))
1908       return V;
1909 
1910     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1911   }
1912 
1913   Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1914                                     unsigned Idx1, const Twine &Name = "") {
1915     Value *Idxs[] = {
1916       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1917       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1918     };
1919 
1920     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true))
1921       return V;
1922 
1923     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1924   }
1925 
1926   Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1927                             const Twine &Name = "") {
1928     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1929 
1930     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/false))
1931       return V;
1932 
1933     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1934   }
1935 
1936   Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1937                                     const Twine &Name = "") {
1938     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1939 
1940     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, /*IsInBounds=*/true))
1941       return V;
1942 
1943     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1944   }
1945 
1946   Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1947                             const Twine &Name = "") {
1948     Value *Idxs[] = {
1949       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1950       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1951     };
1952 
1953     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/false))
1954       return V;
1955 
1956     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1957   }
1958 
1959   Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1960                                     uint64_t Idx1, const Twine &Name = "") {
1961     Value *Idxs[] = {
1962       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1963       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1964     };
1965 
1966     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, /*IsInBounds=*/true))
1967       return V;
1968 
1969     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1970   }
1971 
1972   Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1973                          const Twine &Name = "") {
1974     return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1975   }
1976 
1977   /// Same as CreateGlobalString, but return a pointer with "i8*" type
1978   /// instead of a pointer to array of i8.
1979   ///
1980   /// If no module is given via \p M, it is take from the insertion point basic
1981   /// block.
1982   Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1983                                   unsigned AddressSpace = 0,
1984                                   Module *M = nullptr) {
1985     GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace, M);
1986     Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1987     Constant *Indices[] = {Zero, Zero};
1988     return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
1989                                                   Indices);
1990   }
1991 
1992   //===--------------------------------------------------------------------===//
1993   // Instruction creation methods: Cast/Conversion Operators
1994   //===--------------------------------------------------------------------===//
1995 
1996   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1997     return CreateCast(Instruction::Trunc, V, DestTy, Name);
1998   }
1999 
2000   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "",
2001                     bool IsNonNeg = false) {
2002     if (V->getType() == DestTy)
2003       return V;
2004     if (Value *Folded = Folder.FoldCast(Instruction::ZExt, V, DestTy))
2005       return Folded;
2006     Instruction *I = Insert(new ZExtInst(V, DestTy), Name);
2007     if (IsNonNeg)
2008       I->setNonNeg();
2009     return I;
2010   }
2011 
2012   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
2013     return CreateCast(Instruction::SExt, V, DestTy, Name);
2014   }
2015 
2016   /// Create a ZExt or Trunc from the integer value V to DestTy. Return
2017   /// the value untouched if the type of V is already DestTy.
2018   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
2019                            const Twine &Name = "") {
2020     assert(V->getType()->isIntOrIntVectorTy() &&
2021            DestTy->isIntOrIntVectorTy() &&
2022            "Can only zero extend/truncate integers!");
2023     Type *VTy = V->getType();
2024     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2025       return CreateZExt(V, DestTy, Name);
2026     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2027       return CreateTrunc(V, DestTy, Name);
2028     return V;
2029   }
2030 
2031   /// Create a SExt or Trunc from the integer value V to DestTy. Return
2032   /// the value untouched if the type of V is already DestTy.
2033   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
2034                            const Twine &Name = "") {
2035     assert(V->getType()->isIntOrIntVectorTy() &&
2036            DestTy->isIntOrIntVectorTy() &&
2037            "Can only sign extend/truncate integers!");
2038     Type *VTy = V->getType();
2039     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2040       return CreateSExt(V, DestTy, Name);
2041     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2042       return CreateTrunc(V, DestTy, Name);
2043     return V;
2044   }
2045 
2046   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
2047     if (IsFPConstrained)
2048       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
2049                                      V, DestTy, nullptr, Name);
2050     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
2051   }
2052 
2053   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
2054     if (IsFPConstrained)
2055       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
2056                                      V, DestTy, nullptr, Name);
2057     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
2058   }
2059 
2060   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2061     if (IsFPConstrained)
2062       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp,
2063                                      V, DestTy, nullptr, Name);
2064     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
2065   }
2066 
2067   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2068     if (IsFPConstrained)
2069       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp,
2070                                      V, DestTy, nullptr, Name);
2071     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
2072   }
2073 
2074   Value *CreateFPTrunc(Value *V, Type *DestTy,
2075                        const Twine &Name = "") {
2076     if (IsFPConstrained)
2077       return CreateConstrainedFPCast(
2078           Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr,
2079           Name);
2080     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
2081   }
2082 
2083   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
2084     if (IsFPConstrained)
2085       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
2086                                      V, DestTy, nullptr, Name);
2087     return CreateCast(Instruction::FPExt, V, DestTy, Name);
2088   }
2089 
2090   Value *CreatePtrToInt(Value *V, Type *DestTy,
2091                         const Twine &Name = "") {
2092     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
2093   }
2094 
2095   Value *CreateIntToPtr(Value *V, Type *DestTy,
2096                         const Twine &Name = "") {
2097     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
2098   }
2099 
2100   Value *CreateBitCast(Value *V, Type *DestTy,
2101                        const Twine &Name = "") {
2102     return CreateCast(Instruction::BitCast, V, DestTy, Name);
2103   }
2104 
2105   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
2106                              const Twine &Name = "") {
2107     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
2108   }
2109 
2110   Value *CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2111     Instruction::CastOps CastOp =
2112         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2113             ? Instruction::BitCast
2114             : Instruction::ZExt;
2115     return CreateCast(CastOp, V, DestTy, Name);
2116   }
2117 
2118   Value *CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2119     Instruction::CastOps CastOp =
2120         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2121             ? Instruction::BitCast
2122             : Instruction::SExt;
2123     return CreateCast(CastOp, V, DestTy, Name);
2124   }
2125 
2126   Value *CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2127     Instruction::CastOps CastOp =
2128         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2129             ? Instruction::BitCast
2130             : Instruction::Trunc;
2131     return CreateCast(CastOp, V, DestTy, Name);
2132   }
2133 
2134   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2135                     const Twine &Name = "") {
2136     if (V->getType() == DestTy)
2137       return V;
2138     if (Value *Folded = Folder.FoldCast(Op, V, DestTy))
2139       return Folded;
2140     return Insert(CastInst::Create(Op, V, DestTy), Name);
2141   }
2142 
2143   Value *CreatePointerCast(Value *V, Type *DestTy,
2144                            const Twine &Name = "") {
2145     if (V->getType() == DestTy)
2146       return V;
2147     if (auto *VC = dyn_cast<Constant>(V))
2148       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2149     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2150   }
2151 
2152   // With opaque pointers enabled, this can be substituted with
2153   // CreateAddrSpaceCast.
2154   // TODO: Replace uses of this method and remove the method itself.
2155   Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2156                                              const Twine &Name = "") {
2157     if (V->getType() == DestTy)
2158       return V;
2159 
2160     if (auto *VC = dyn_cast<Constant>(V)) {
2161       return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2162                     Name);
2163     }
2164 
2165     return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2166                   Name);
2167   }
2168 
2169   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2170                        const Twine &Name = "") {
2171     Instruction::CastOps CastOp =
2172         V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2173             ? Instruction::Trunc
2174             : (isSigned ? Instruction::SExt : Instruction::ZExt);
2175     return CreateCast(CastOp, V, DestTy, Name);
2176   }
2177 
2178   Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2179                                 const Twine &Name = "") {
2180     if (V->getType() == DestTy)
2181       return V;
2182     if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2183       return CreatePtrToInt(V, DestTy, Name);
2184     if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2185       return CreateIntToPtr(V, DestTy, Name);
2186 
2187     return CreateBitCast(V, DestTy, Name);
2188   }
2189 
2190   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
2191     Instruction::CastOps CastOp =
2192         V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2193             ? Instruction::FPTrunc
2194             : Instruction::FPExt;
2195     return CreateCast(CastOp, V, DestTy, Name);
2196   }
2197 
2198   CallInst *CreateConstrainedFPCast(
2199       Intrinsic::ID ID, Value *V, Type *DestTy,
2200       Instruction *FMFSource = nullptr, const Twine &Name = "",
2201       MDNode *FPMathTag = nullptr,
2202       std::optional<RoundingMode> Rounding = std::nullopt,
2203       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2204 
2205   // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2206   // compile time error, instead of converting the string to bool for the
2207   // isSigned parameter.
2208   Value *CreateIntCast(Value *, Type *, const char *) = delete;
2209 
2210   //===--------------------------------------------------------------------===//
2211   // Instruction creation methods: Compare Instructions
2212   //===--------------------------------------------------------------------===//
2213 
2214   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2215     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2216   }
2217 
2218   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2219     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2220   }
2221 
2222   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2223     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2224   }
2225 
2226   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2227     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2228   }
2229 
2230   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2231     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2232   }
2233 
2234   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2235     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2236   }
2237 
2238   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2239     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2240   }
2241 
2242   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2243     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
2244   }
2245 
2246   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2247     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
2248   }
2249 
2250   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2251     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
2252   }
2253 
2254   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2255                        MDNode *FPMathTag = nullptr) {
2256     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2257   }
2258 
2259   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2260                        MDNode *FPMathTag = nullptr) {
2261     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2262   }
2263 
2264   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2265                        MDNode *FPMathTag = nullptr) {
2266     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2267   }
2268 
2269   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2270                        MDNode *FPMathTag = nullptr) {
2271     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2272   }
2273 
2274   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2275                        MDNode *FPMathTag = nullptr) {
2276     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2277   }
2278 
2279   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2280                        MDNode *FPMathTag = nullptr) {
2281     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2282   }
2283 
2284   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2285                        MDNode *FPMathTag = nullptr) {
2286     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2287   }
2288 
2289   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2290                        MDNode *FPMathTag = nullptr) {
2291     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2292   }
2293 
2294   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2295                        MDNode *FPMathTag = nullptr) {
2296     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2297   }
2298 
2299   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2300                        MDNode *FPMathTag = nullptr) {
2301     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2302   }
2303 
2304   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2305                        MDNode *FPMathTag = nullptr) {
2306     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2307   }
2308 
2309   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2310                        MDNode *FPMathTag = nullptr) {
2311     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2312   }
2313 
2314   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2315                        MDNode *FPMathTag = nullptr) {
2316     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2317   }
2318 
2319   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2320                        MDNode *FPMathTag = nullptr) {
2321     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2322   }
2323 
2324   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2325                     const Twine &Name = "") {
2326     if (auto *V = Folder.FoldICmp(P, LHS, RHS))
2327       return V;
2328     return Insert(new ICmpInst(P, LHS, RHS), Name);
2329   }
2330 
2331   // Create a quiet floating-point comparison (i.e. one that raises an FP
2332   // exception only in the case where an input is a signaling NaN).
2333   // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
2334   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2335                     const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2336     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, false);
2337   }
2338 
2339   Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2340                    const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2341     return CmpInst::isFPPredicate(Pred)
2342                ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag)
2343                : CreateICmp(Pred, LHS, RHS, Name);
2344   }
2345 
2346   // Create a signaling floating-point comparison (i.e. one that raises an FP
2347   // exception whenever an input is any NaN, signaling or quiet).
2348   // Note that this differs from CreateFCmp only if IsFPConstrained is true.
2349   Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS,
2350                      const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2351     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, true);
2352   }
2353 
2354 private:
2355   // Helper routine to create either a signaling or a quiet FP comparison.
2356   Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS,
2357                           const Twine &Name, MDNode *FPMathTag,
2358                           bool IsSignaling);
2359 
2360 public:
2361   CallInst *CreateConstrainedFPCmp(
2362       Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R,
2363       const Twine &Name = "",
2364       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2365 
2366   //===--------------------------------------------------------------------===//
2367   // Instruction creation methods: Other Instructions
2368   //===--------------------------------------------------------------------===//
2369 
2370   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2371                      const Twine &Name = "") {
2372     PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
2373     if (isa<FPMathOperator>(Phi))
2374       setFPAttrs(Phi, nullptr /* MDNode* */, FMF);
2375     return Insert(Phi, Name);
2376   }
2377 
2378 private:
2379   CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
2380                              const Twine &Name = "",
2381                              Instruction *FMFSource = nullptr,
2382                              ArrayRef<OperandBundleDef> OpBundles = {});
2383 
2384 public:
2385   CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2386                        ArrayRef<Value *> Args = std::nullopt,
2387                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2388     CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2389     if (IsFPConstrained)
2390       setConstrainedFPCallAttr(CI);
2391     if (isa<FPMathOperator>(CI))
2392       setFPAttrs(CI, FPMathTag, FMF);
2393     return Insert(CI, Name);
2394   }
2395 
2396   CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2397                        ArrayRef<OperandBundleDef> OpBundles,
2398                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2399     CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2400     if (IsFPConstrained)
2401       setConstrainedFPCallAttr(CI);
2402     if (isa<FPMathOperator>(CI))
2403       setFPAttrs(CI, FPMathTag, FMF);
2404     return Insert(CI, Name);
2405   }
2406 
2407   CallInst *CreateCall(FunctionCallee Callee,
2408                        ArrayRef<Value *> Args = std::nullopt,
2409                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2410     return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2411                       FPMathTag);
2412   }
2413 
2414   CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2415                        ArrayRef<OperandBundleDef> OpBundles,
2416                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2417     return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2418                       OpBundles, Name, FPMathTag);
2419   }
2420 
2421   CallInst *CreateConstrainedFPCall(
2422       Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "",
2423       std::optional<RoundingMode> Rounding = std::nullopt,
2424       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2425 
2426   Value *CreateSelect(Value *C, Value *True, Value *False,
2427                       const Twine &Name = "", Instruction *MDFrom = nullptr);
2428 
2429   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2430     return Insert(new VAArgInst(List, Ty), Name);
2431   }
2432 
2433   Value *CreateExtractElement(Value *Vec, Value *Idx,
2434                               const Twine &Name = "") {
2435     if (Value *V = Folder.FoldExtractElement(Vec, Idx))
2436       return V;
2437     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2438   }
2439 
2440   Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2441                               const Twine &Name = "") {
2442     return CreateExtractElement(Vec, getInt64(Idx), Name);
2443   }
2444 
2445   Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx,
2446                              const Twine &Name = "") {
2447     return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2448   }
2449 
2450   Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx,
2451                              const Twine &Name = "") {
2452     return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2453   }
2454 
2455   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2456                              const Twine &Name = "") {
2457     if (Value *V = Folder.FoldInsertElement(Vec, NewElt, Idx))
2458       return V;
2459     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2460   }
2461 
2462   Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2463                              const Twine &Name = "") {
2464     return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2465   }
2466 
2467   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2468                              const Twine &Name = "") {
2469     SmallVector<int, 16> IntMask;
2470     ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask);
2471     return CreateShuffleVector(V1, V2, IntMask, Name);
2472   }
2473 
2474   /// See class ShuffleVectorInst for a description of the mask representation.
2475   Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask,
2476                              const Twine &Name = "") {
2477     if (Value *V = Folder.FoldShuffleVector(V1, V2, Mask))
2478       return V;
2479     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2480   }
2481 
2482   /// Create a unary shuffle. The second vector operand of the IR instruction
2483   /// is poison.
2484   Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask,
2485                              const Twine &Name = "") {
2486     return CreateShuffleVector(V, PoisonValue::get(V->getType()), Mask, Name);
2487   }
2488 
2489   Value *CreateExtractValue(Value *Agg, ArrayRef<unsigned> Idxs,
2490                             const Twine &Name = "") {
2491     if (auto *V = Folder.FoldExtractValue(Agg, Idxs))
2492       return V;
2493     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2494   }
2495 
2496   Value *CreateInsertValue(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2497                            const Twine &Name = "") {
2498     if (auto *V = Folder.FoldInsertValue(Agg, Val, Idxs))
2499       return V;
2500     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2501   }
2502 
2503   LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2504                                    const Twine &Name = "") {
2505     return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2506   }
2507 
2508   Value *CreateFreeze(Value *V, const Twine &Name = "") {
2509     return Insert(new FreezeInst(V), Name);
2510   }
2511 
2512   //===--------------------------------------------------------------------===//
2513   // Utility creation methods
2514   //===--------------------------------------------------------------------===//
2515 
2516   /// Return a boolean value testing if \p Arg == 0.
2517   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2518     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), Name);
2519   }
2520 
2521   /// Return a boolean value testing if \p Arg != 0.
2522   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2523     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), Name);
2524   }
2525 
2526   /// Return a boolean value testing if \p Arg < 0.
2527   Value *CreateIsNeg(Value *Arg, const Twine &Name = "") {
2528     return CreateICmpSLT(Arg, ConstantInt::getNullValue(Arg->getType()), Name);
2529   }
2530 
2531   /// Return a boolean value testing if \p Arg > -1.
2532   Value *CreateIsNotNeg(Value *Arg, const Twine &Name = "") {
2533     return CreateICmpSGT(Arg, ConstantInt::getAllOnesValue(Arg->getType()),
2534                          Name);
2535   }
2536 
2537   /// Return the i64 difference between two pointer values, dividing out
2538   /// the size of the pointed-to objects.
2539   ///
2540   /// This is intended to implement C-style pointer subtraction. As such, the
2541   /// pointers must be appropriately aligned for their element types and
2542   /// pointing into the same object.
2543   Value *CreatePtrDiff(Type *ElemTy, Value *LHS, Value *RHS,
2544                        const Twine &Name = "");
2545 
2546   /// Create a launder.invariant.group intrinsic call. If Ptr type is
2547   /// different from pointer to i8, it's casted to pointer to i8 in the same
2548   /// address space before call and casted back to Ptr type after call.
2549   Value *CreateLaunderInvariantGroup(Value *Ptr);
2550 
2551   /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2552   /// different from pointer to i8, it's casted to pointer to i8 in the same
2553   /// address space before call and casted back to Ptr type after call.
2554   Value *CreateStripInvariantGroup(Value *Ptr);
2555 
2556   /// Return a vector value that contains the vector V reversed
2557   Value *CreateVectorReverse(Value *V, const Twine &Name = "");
2558 
2559   /// Return a vector splice intrinsic if using scalable vectors, otherwise
2560   /// return a shufflevector. If the immediate is positive, a vector is
2561   /// extracted from concat(V1, V2), starting at Imm. If the immediate
2562   /// is negative, we extract -Imm elements from V1 and the remaining
2563   /// elements from V2. Imm is a signed integer in the range
2564   /// -VL <= Imm < VL (where VL is the runtime vector length of the
2565   /// source/result vector)
2566   Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm,
2567                             const Twine &Name = "");
2568 
2569   /// Return a vector value that contains \arg V broadcasted to \p
2570   /// NumElts elements.
2571   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
2572 
2573   /// Return a vector value that contains \arg V broadcasted to \p
2574   /// EC elements.
2575   Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
2576 
2577   Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base,
2578                                         unsigned Dimension, unsigned LastIndex,
2579                                         MDNode *DbgInfo);
2580 
2581   Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2582                                         MDNode *DbgInfo);
2583 
2584   Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base,
2585                                          unsigned Index, unsigned FieldIndex,
2586                                          MDNode *DbgInfo);
2587 
2588   Value *createIsFPClass(Value *FPNum, unsigned Test);
2589 
2590 private:
2591   /// Helper function that creates an assume intrinsic call that
2592   /// represents an alignment assumption on the provided pointer \p PtrValue
2593   /// with offset \p OffsetValue and alignment value \p AlignValue.
2594   CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2595                                             Value *PtrValue, Value *AlignValue,
2596                                             Value *OffsetValue);
2597 
2598 public:
2599   /// Create an assume intrinsic call that represents an alignment
2600   /// assumption on the provided pointer.
2601   ///
2602   /// An optional offset can be provided, and if it is provided, the offset
2603   /// must be subtracted from the provided pointer to get the pointer with the
2604   /// specified alignment.
2605   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2606                                       unsigned Alignment,
2607                                       Value *OffsetValue = nullptr);
2608 
2609   /// Create an assume intrinsic call that represents an alignment
2610   /// assumption on the provided pointer.
2611   ///
2612   /// An optional offset can be provided, and if it is provided, the offset
2613   /// must be subtracted from the provided pointer to get the pointer with the
2614   /// specified alignment.
2615   ///
2616   /// This overload handles the condition where the Alignment is dependent
2617   /// on an existing value rather than a static value.
2618   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2619                                       Value *Alignment,
2620                                       Value *OffsetValue = nullptr);
2621 };
2622 
2623 /// This provides a uniform API for creating instructions and inserting
2624 /// them into a basic block: either at the end of a BasicBlock, or at a specific
2625 /// iterator location in a block.
2626 ///
2627 /// Note that the builder does not expose the full generality of LLVM
2628 /// instructions.  For access to extra instruction properties, use the mutators
2629 /// (e.g. setVolatile) on the instructions after they have been
2630 /// created. Convenience state exists to specify fast-math flags and fp-math
2631 /// tags.
2632 ///
2633 /// The first template argument specifies a class to use for creating constants.
2634 /// This defaults to creating minimally folded constants.  The second template
2635 /// argument allows clients to specify custom insertion hooks that are called on
2636 /// every newly created insertion.
2637 template <typename FolderTy = ConstantFolder,
2638           typename InserterTy = IRBuilderDefaultInserter>
2639 class IRBuilder : public IRBuilderBase {
2640 private:
2641   FolderTy Folder;
2642   InserterTy Inserter;
2643 
2644 public:
2645   IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(),
2646             MDNode *FPMathTag = nullptr,
2647             ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2648       : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles),
2649         Folder(Folder), Inserter(Inserter) {}
2650 
2651   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
2652                      ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2653       : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
2654 
2655   explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
2656                      MDNode *FPMathTag = nullptr,
2657                      ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2658       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2659                       FPMathTag, OpBundles),
2660         Folder(Folder) {
2661     SetInsertPoint(TheBB);
2662   }
2663 
2664   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
2665                      ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2666       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2667                       FPMathTag, OpBundles) {
2668     SetInsertPoint(TheBB);
2669   }
2670 
2671   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
2672                      ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2673       : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, FPMathTag,
2674                       OpBundles) {
2675     SetInsertPoint(IP);
2676   }
2677 
2678   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
2679             MDNode *FPMathTag = nullptr,
2680             ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2681       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2682                       FPMathTag, OpBundles),
2683         Folder(Folder) {
2684     SetInsertPoint(TheBB, IP);
2685   }
2686 
2687   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
2688             MDNode *FPMathTag = nullptr,
2689             ArrayRef<OperandBundleDef> OpBundles = std::nullopt)
2690       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2691                       FPMathTag, OpBundles) {
2692     SetInsertPoint(TheBB, IP);
2693   }
2694 
2695   /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard
2696   /// or FastMathFlagGuard instead.
2697   IRBuilder(const IRBuilder &) = delete;
2698 
getInserter()2699   InserterTy &getInserter() { return Inserter; }
2700 };
2701 
2702 template <typename FolderTy, typename InserterTy>
2703 IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *,
2704           ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy, InserterTy>;
2705 IRBuilder(LLVMContext &, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2706 template <typename FolderTy>
2707 IRBuilder(BasicBlock *, FolderTy, MDNode *, ArrayRef<OperandBundleDef>)
2708     -> IRBuilder<FolderTy>;
2709 IRBuilder(BasicBlock *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2710 IRBuilder(Instruction *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2711 template <typename FolderTy>
2712 IRBuilder(BasicBlock *, BasicBlock::iterator, FolderTy, MDNode *,
2713           ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy>;
2714 IRBuilder(BasicBlock *, BasicBlock::iterator, MDNode *,
2715           ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2716 
2717 
2718 // Create wrappers for C Binding types (see CBindingWrapping.h).
2719 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2720 
2721 } // end namespace llvm
2722 
2723 #endif // LLVM_IR_IRBUILDER_H
2724