Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:07

0001 //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file defines the IRBuilder class, which is used as a convenient way
0010 // to create LLVM instructions with a consistent and simplified interface.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_IR_IRBUILDER_H
0015 #define LLVM_IR_IRBUILDER_H
0016 
0017 #include "llvm-c/Types.h"
0018 #include "llvm/ADT/ArrayRef.h"
0019 #include "llvm/ADT/STLExtras.h"
0020 #include "llvm/ADT/StringRef.h"
0021 #include "llvm/ADT/Twine.h"
0022 #include "llvm/IR/BasicBlock.h"
0023 #include "llvm/IR/Constant.h"
0024 #include "llvm/IR/ConstantFolder.h"
0025 #include "llvm/IR/Constants.h"
0026 #include "llvm/IR/DataLayout.h"
0027 #include "llvm/IR/DebugLoc.h"
0028 #include "llvm/IR/DerivedTypes.h"
0029 #include "llvm/IR/FPEnv.h"
0030 #include "llvm/IR/Function.h"
0031 #include "llvm/IR/GlobalVariable.h"
0032 #include "llvm/IR/InstrTypes.h"
0033 #include "llvm/IR/Instruction.h"
0034 #include "llvm/IR/Instructions.h"
0035 #include "llvm/IR/Intrinsics.h"
0036 #include "llvm/IR/LLVMContext.h"
0037 #include "llvm/IR/Operator.h"
0038 #include "llvm/IR/Type.h"
0039 #include "llvm/IR/Value.h"
0040 #include "llvm/IR/ValueHandle.h"
0041 #include "llvm/Support/AtomicOrdering.h"
0042 #include "llvm/Support/CBindingWrapping.h"
0043 #include "llvm/Support/Casting.h"
0044 #include <cassert>
0045 #include <cstdint>
0046 #include <functional>
0047 #include <optional>
0048 #include <utility>
0049 
0050 namespace llvm {
0051 
0052 class APInt;
0053 class Use;
0054 
0055 /// This provides the default implementation of the IRBuilder
0056 /// 'InsertHelper' method that is called whenever an instruction is created by
0057 /// IRBuilder and needs to be inserted.
0058 ///
0059 /// By default, this inserts the instruction at the insertion point.
0060 class IRBuilderDefaultInserter {
0061 public:
0062   virtual ~IRBuilderDefaultInserter();
0063 
0064   virtual void InsertHelper(Instruction *I, const Twine &Name,
0065                             BasicBlock::iterator InsertPt) const {
0066     if (InsertPt.isValid())
0067       I->insertInto(InsertPt.getNodeParent(), InsertPt);
0068     I->setName(Name);
0069   }
0070 };
0071 
0072 /// Provides an 'InsertHelper' that calls a user-provided callback after
0073 /// performing the default insertion.
0074 class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
0075   std::function<void(Instruction *)> Callback;
0076 
0077 public:
0078   ~IRBuilderCallbackInserter() override;
0079 
0080   IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
0081       : Callback(std::move(Callback)) {}
0082 
0083   void InsertHelper(Instruction *I, const Twine &Name,
0084                     BasicBlock::iterator InsertPt) const override {
0085     IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
0086     Callback(I);
0087   }
0088 };
0089 
0090 /// This provides a helper for copying FMF from an instruction or setting
0091 /// specified flags.
0092 class FMFSource {
0093   std::optional<FastMathFlags> FMF;
0094 
0095 public:
0096   FMFSource() = default;
0097   FMFSource(Instruction *Source) {
0098     if (Source)
0099       FMF = Source->getFastMathFlags();
0100   }
0101   FMFSource(FastMathFlags FMF) : FMF(FMF) {}
0102   FastMathFlags get(FastMathFlags Default) const {
0103     return FMF.value_or(Default);
0104   }
0105   /// Intersect the FMF from two instructions.
0106   static FMFSource intersect(Value *A, Value *B) {
0107     return FMFSource(cast<FPMathOperator>(A)->getFastMathFlags() &
0108                      cast<FPMathOperator>(B)->getFastMathFlags());
0109   }
0110 };
0111 
0112 /// Common base class shared among various IRBuilders.
0113 class IRBuilderBase {
0114   /// Pairs of (metadata kind, MDNode *) that should be added to all newly
0115   /// created instructions, like !dbg metadata.
0116   SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
0117 
0118   /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
0119   /// null. If \p MD is null, remove the entry with \p Kind.
0120   void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
0121     if (!MD) {
0122       erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) {
0123         return KV.first == Kind;
0124       });
0125       return;
0126     }
0127 
0128     for (auto &KV : MetadataToCopy)
0129       if (KV.first == Kind) {
0130         KV.second = MD;
0131         return;
0132       }
0133 
0134     MetadataToCopy.emplace_back(Kind, MD);
0135   }
0136 
0137 protected:
0138   BasicBlock *BB;
0139   BasicBlock::iterator InsertPt;
0140   LLVMContext &Context;
0141   const IRBuilderFolder &Folder;
0142   const IRBuilderDefaultInserter &Inserter;
0143 
0144   MDNode *DefaultFPMathTag;
0145   FastMathFlags FMF;
0146 
0147   bool IsFPConstrained = false;
0148   fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict;
0149   RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic;
0150 
0151   ArrayRef<OperandBundleDef> DefaultOperandBundles;
0152 
0153 public:
0154   IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
0155                 const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag,
0156                 ArrayRef<OperandBundleDef> OpBundles)
0157       : Context(context), Folder(Folder), Inserter(Inserter),
0158         DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) {
0159     ClearInsertionPoint();
0160   }
0161 
0162   /// Insert and return the specified instruction.
0163   template<typename InstTy>
0164   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
0165     Inserter.InsertHelper(I, Name, InsertPt);
0166     AddMetadataToInst(I);
0167     return I;
0168   }
0169 
0170   /// No-op overload to handle constants.
0171   Constant *Insert(Constant *C, const Twine& = "") const {
0172     return C;
0173   }
0174 
0175   Value *Insert(Value *V, const Twine &Name = "") const {
0176     if (Instruction *I = dyn_cast<Instruction>(V))
0177       return Insert(I, Name);
0178     assert(isa<Constant>(V));
0179     return V;
0180   }
0181 
0182   //===--------------------------------------------------------------------===//
0183   // Builder configuration methods
0184   //===--------------------------------------------------------------------===//
0185 
0186   /// Clear the insertion point: created instructions will not be
0187   /// inserted into a block.
0188   void ClearInsertionPoint() {
0189     BB = nullptr;
0190     InsertPt = BasicBlock::iterator();
0191   }
0192 
0193   BasicBlock *GetInsertBlock() const { return BB; }
0194   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
0195   LLVMContext &getContext() const { return Context; }
0196 
0197   /// This specifies that created instructions should be appended to the
0198   /// end of the specified block.
0199   void SetInsertPoint(BasicBlock *TheBB) {
0200     BB = TheBB;
0201     InsertPt = BB->end();
0202   }
0203 
0204   /// This specifies that created instructions should be inserted before
0205   /// the specified instruction.
0206   void SetInsertPoint(Instruction *I) {
0207     BB = I->getParent();
0208     InsertPt = I->getIterator();
0209     assert(InsertPt != BB->end() && "Can't read debug loc from end()");
0210     SetCurrentDebugLocation(I->getStableDebugLoc());
0211   }
0212 
0213   /// This specifies that created instructions should be inserted at the
0214   /// specified point.
0215   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
0216     BB = TheBB;
0217     InsertPt = IP;
0218     if (IP != TheBB->end())
0219       SetCurrentDebugLocation(IP->getStableDebugLoc());
0220   }
0221 
0222   /// This specifies that created instructions should be inserted at
0223   /// the specified point, but also requires that \p IP is dereferencable.
0224   void SetInsertPoint(BasicBlock::iterator IP) {
0225     BB = IP->getParent();
0226     InsertPt = IP;
0227     SetCurrentDebugLocation(IP->getStableDebugLoc());
0228   }
0229 
0230   /// This specifies that created instructions should inserted at the beginning
0231   /// end of the specified function, but after already existing static alloca
0232   /// instructions that are at the start.
0233   void SetInsertPointPastAllocas(Function *F) {
0234     BB = &F->getEntryBlock();
0235     InsertPt = BB->getFirstNonPHIOrDbgOrAlloca();
0236   }
0237 
0238   /// Set location information used by debugging information.
0239   void SetCurrentDebugLocation(DebugLoc L) {
0240     AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode());
0241   }
0242 
0243   /// Set nosanitize metadata.
0244   void SetNoSanitizeMetadata() {
0245     AddOrRemoveMetadataToCopy(llvm::LLVMContext::MD_nosanitize,
0246                               llvm::MDNode::get(getContext(), {}));
0247   }
0248 
0249   /// Collect metadata with IDs \p MetadataKinds from \p Src which should be
0250   /// added to all created instructions. Entries present in MedataDataToCopy but
0251   /// not on \p Src will be dropped from MetadataToCopy.
0252   void CollectMetadataToCopy(Instruction *Src,
0253                              ArrayRef<unsigned> MetadataKinds) {
0254     for (unsigned K : MetadataKinds)
0255       AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
0256   }
0257 
0258   /// Get location information used by debugging information.
0259   DebugLoc getCurrentDebugLocation() const;
0260 
0261   /// If this builder has a current debug location, set it on the
0262   /// specified instruction.
0263   void SetInstDebugLocation(Instruction *I) const;
0264 
0265   /// Add all entries in MetadataToCopy to \p I.
0266   void AddMetadataToInst(Instruction *I) const {
0267     for (const auto &KV : MetadataToCopy)
0268       I->setMetadata(KV.first, KV.second);
0269   }
0270 
0271   /// Get the return type of the current function that we're emitting
0272   /// into.
0273   Type *getCurrentFunctionReturnType() const;
0274 
0275   /// InsertPoint - A saved insertion point.
0276   class InsertPoint {
0277     BasicBlock *Block = nullptr;
0278     BasicBlock::iterator Point;
0279 
0280   public:
0281     /// Creates a new insertion point which doesn't point to anything.
0282     InsertPoint() = default;
0283 
0284     /// Creates a new insertion point at the given location.
0285     InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
0286         : Block(InsertBlock), Point(InsertPoint) {}
0287 
0288     /// Returns true if this insert point is set.
0289     bool isSet() const { return (Block != nullptr); }
0290 
0291     BasicBlock *getBlock() const { return Block; }
0292     BasicBlock::iterator getPoint() const { return Point; }
0293   };
0294 
0295   /// Returns the current insert point.
0296   InsertPoint saveIP() const {
0297     return InsertPoint(GetInsertBlock(), GetInsertPoint());
0298   }
0299 
0300   /// Returns the current insert point, clearing it in the process.
0301   InsertPoint saveAndClearIP() {
0302     InsertPoint IP(GetInsertBlock(), GetInsertPoint());
0303     ClearInsertionPoint();
0304     return IP;
0305   }
0306 
0307   /// Sets the current insert point to a previously-saved location.
0308   void restoreIP(InsertPoint IP) {
0309     if (IP.isSet())
0310       SetInsertPoint(IP.getBlock(), IP.getPoint());
0311     else
0312       ClearInsertionPoint();
0313   }
0314 
0315   /// Get the floating point math metadata being used.
0316   MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
0317 
0318   /// Get the flags to be applied to created floating point ops
0319   FastMathFlags getFastMathFlags() const { return FMF; }
0320 
0321   FastMathFlags &getFastMathFlags() { return FMF; }
0322 
0323   /// Clear the fast-math flags.
0324   void clearFastMathFlags() { FMF.clear(); }
0325 
0326   /// Set the floating point math metadata to be used.
0327   void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
0328 
0329   /// Set the fast-math flags to be used with generated fp-math operators
0330   void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
0331 
0332   /// Enable/Disable use of constrained floating point math. When
0333   /// enabled the CreateF<op>() calls instead create constrained
0334   /// floating point intrinsic calls. Fast math flags are unaffected
0335   /// by this setting.
0336   void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
0337 
0338   /// Query for the use of constrained floating point math
0339   bool getIsFPConstrained() { return IsFPConstrained; }
0340 
0341   /// Set the exception handling to be used with constrained floating point
0342   void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
0343 #ifndef NDEBUG
0344     std::optional<StringRef> ExceptStr =
0345         convertExceptionBehaviorToStr(NewExcept);
0346     assert(ExceptStr && "Garbage strict exception behavior!");
0347 #endif
0348     DefaultConstrainedExcept = NewExcept;
0349   }
0350 
0351   /// Set the rounding mode handling to be used with constrained floating point
0352   void setDefaultConstrainedRounding(RoundingMode NewRounding) {
0353 #ifndef NDEBUG
0354     std::optional<StringRef> RoundingStr =
0355         convertRoundingModeToStr(NewRounding);
0356     assert(RoundingStr && "Garbage strict rounding mode!");
0357 #endif
0358     DefaultConstrainedRounding = NewRounding;
0359   }
0360 
0361   /// Get the exception handling used with constrained floating point
0362   fp::ExceptionBehavior getDefaultConstrainedExcept() {
0363     return DefaultConstrainedExcept;
0364   }
0365 
0366   /// Get the rounding mode handling used with constrained floating point
0367   RoundingMode getDefaultConstrainedRounding() {
0368     return DefaultConstrainedRounding;
0369   }
0370 
0371   void setConstrainedFPFunctionAttr() {
0372     assert(BB && "Must have a basic block to set any function attributes!");
0373 
0374     Function *F = BB->getParent();
0375     if (!F->hasFnAttribute(Attribute::StrictFP)) {
0376       F->addFnAttr(Attribute::StrictFP);
0377     }
0378   }
0379 
0380   void setConstrainedFPCallAttr(CallBase *I) {
0381     I->addFnAttr(Attribute::StrictFP);
0382   }
0383 
0384   void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
0385     DefaultOperandBundles = OpBundles;
0386   }
0387 
0388   //===--------------------------------------------------------------------===//
0389   // RAII helpers.
0390   //===--------------------------------------------------------------------===//
0391 
0392   // RAII object that stores the current insertion point and restores it
0393   // when the object is destroyed. This includes the debug location.
0394   class InsertPointGuard {
0395     IRBuilderBase &Builder;
0396     AssertingVH<BasicBlock> Block;
0397     BasicBlock::iterator Point;
0398     DebugLoc DbgLoc;
0399 
0400   public:
0401     InsertPointGuard(IRBuilderBase &B)
0402         : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
0403           DbgLoc(B.getCurrentDebugLocation()) {}
0404 
0405     InsertPointGuard(const InsertPointGuard &) = delete;
0406     InsertPointGuard &operator=(const InsertPointGuard &) = delete;
0407 
0408     ~InsertPointGuard() {
0409       Builder.restoreIP(InsertPoint(Block, Point));
0410       Builder.SetCurrentDebugLocation(DbgLoc);
0411     }
0412   };
0413 
0414   // RAII object that stores the current fast math settings and restores
0415   // them when the object is destroyed.
0416   class FastMathFlagGuard {
0417     IRBuilderBase &Builder;
0418     FastMathFlags FMF;
0419     MDNode *FPMathTag;
0420     bool IsFPConstrained;
0421     fp::ExceptionBehavior DefaultConstrainedExcept;
0422     RoundingMode DefaultConstrainedRounding;
0423 
0424   public:
0425     FastMathFlagGuard(IRBuilderBase &B)
0426         : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
0427           IsFPConstrained(B.IsFPConstrained),
0428           DefaultConstrainedExcept(B.DefaultConstrainedExcept),
0429           DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
0430 
0431     FastMathFlagGuard(const FastMathFlagGuard &) = delete;
0432     FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
0433 
0434     ~FastMathFlagGuard() {
0435       Builder.FMF = FMF;
0436       Builder.DefaultFPMathTag = FPMathTag;
0437       Builder.IsFPConstrained = IsFPConstrained;
0438       Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
0439       Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
0440     }
0441   };
0442 
0443   // RAII object that stores the current default operand bundles and restores
0444   // them when the object is destroyed.
0445   class OperandBundlesGuard {
0446     IRBuilderBase &Builder;
0447     ArrayRef<OperandBundleDef> DefaultOperandBundles;
0448 
0449   public:
0450     OperandBundlesGuard(IRBuilderBase &B)
0451         : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
0452 
0453     OperandBundlesGuard(const OperandBundlesGuard &) = delete;
0454     OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
0455 
0456     ~OperandBundlesGuard() {
0457       Builder.DefaultOperandBundles = DefaultOperandBundles;
0458     }
0459   };
0460 
0461 
0462   //===--------------------------------------------------------------------===//
0463   // Miscellaneous creation methods.
0464   //===--------------------------------------------------------------------===//
0465 
0466   /// Make a new global variable with initializer type i8*
0467   ///
0468   /// Make a new global variable with an initializer that has array of i8 type
0469   /// filled in with the null terminated string value specified.  The new global
0470   /// variable will be marked mergable with any others of the same contents.  If
0471   /// Name is specified, it is the name of the global variable created.
0472   ///
0473   /// If no module is given via \p M, it is take from the insertion point basic
0474   /// block.
0475   GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
0476                                      unsigned AddressSpace = 0,
0477                                      Module *M = nullptr, bool AddNull = true);
0478 
0479   /// Get a constant value representing either true or false.
0480   ConstantInt *getInt1(bool V) {
0481     return ConstantInt::get(getInt1Ty(), V);
0482   }
0483 
0484   /// Get the constant value for i1 true.
0485   ConstantInt *getTrue() {
0486     return ConstantInt::getTrue(Context);
0487   }
0488 
0489   /// Get the constant value for i1 false.
0490   ConstantInt *getFalse() {
0491     return ConstantInt::getFalse(Context);
0492   }
0493 
0494   /// Get a constant 8-bit value.
0495   ConstantInt *getInt8(uint8_t C) {
0496     return ConstantInt::get(getInt8Ty(), C);
0497   }
0498 
0499   /// Get a constant 16-bit value.
0500   ConstantInt *getInt16(uint16_t C) {
0501     return ConstantInt::get(getInt16Ty(), C);
0502   }
0503 
0504   /// Get a constant 32-bit value.
0505   ConstantInt *getInt32(uint32_t C) {
0506     return ConstantInt::get(getInt32Ty(), C);
0507   }
0508 
0509   /// Get a constant 64-bit value.
0510   ConstantInt *getInt64(uint64_t C) {
0511     return ConstantInt::get(getInt64Ty(), C);
0512   }
0513 
0514   /// Get a constant N-bit value, zero extended or truncated from
0515   /// a 64-bit value.
0516   ConstantInt *getIntN(unsigned N, uint64_t C) {
0517     return ConstantInt::get(getIntNTy(N), C);
0518   }
0519 
0520   /// Get a constant integer value.
0521   ConstantInt *getInt(const APInt &AI) {
0522     return ConstantInt::get(Context, AI);
0523   }
0524 
0525   //===--------------------------------------------------------------------===//
0526   // Type creation methods
0527   //===--------------------------------------------------------------------===//
0528 
0529   /// Fetch the type representing a single bit
0530   IntegerType *getInt1Ty() {
0531     return Type::getInt1Ty(Context);
0532   }
0533 
0534   /// Fetch the type representing an 8-bit integer.
0535   IntegerType *getInt8Ty() {
0536     return Type::getInt8Ty(Context);
0537   }
0538 
0539   /// Fetch the type representing a 16-bit integer.
0540   IntegerType *getInt16Ty() {
0541     return Type::getInt16Ty(Context);
0542   }
0543 
0544   /// Fetch the type representing a 32-bit integer.
0545   IntegerType *getInt32Ty() {
0546     return Type::getInt32Ty(Context);
0547   }
0548 
0549   /// Fetch the type representing a 64-bit integer.
0550   IntegerType *getInt64Ty() {
0551     return Type::getInt64Ty(Context);
0552   }
0553 
0554   /// Fetch the type representing a 128-bit integer.
0555   IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
0556 
0557   /// Fetch the type representing an N-bit integer.
0558   IntegerType *getIntNTy(unsigned N) {
0559     return Type::getIntNTy(Context, N);
0560   }
0561 
0562   /// Fetch the type representing a 16-bit floating point value.
0563   Type *getHalfTy() {
0564     return Type::getHalfTy(Context);
0565   }
0566 
0567   /// Fetch the type representing a 16-bit brain floating point value.
0568   Type *getBFloatTy() {
0569     return Type::getBFloatTy(Context);
0570   }
0571 
0572   /// Fetch the type representing a 32-bit floating point value.
0573   Type *getFloatTy() {
0574     return Type::getFloatTy(Context);
0575   }
0576 
0577   /// Fetch the type representing a 64-bit floating point value.
0578   Type *getDoubleTy() {
0579     return Type::getDoubleTy(Context);
0580   }
0581 
0582   /// Fetch the type representing void.
0583   Type *getVoidTy() {
0584     return Type::getVoidTy(Context);
0585   }
0586 
0587   /// Fetch the type representing a pointer.
0588   PointerType *getPtrTy(unsigned AddrSpace = 0) {
0589     return PointerType::get(Context, AddrSpace);
0590   }
0591 
0592   /// Fetch the type of an integer with size at least as big as that of a
0593   /// pointer in the given address space.
0594   IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
0595     return DL.getIntPtrType(Context, AddrSpace);
0596   }
0597 
0598   /// Fetch the type of an integer that should be used to index GEP operations
0599   /// within AddressSpace.
0600   IntegerType *getIndexTy(const DataLayout &DL, unsigned AddrSpace) {
0601     return DL.getIndexType(Context, AddrSpace);
0602   }
0603 
0604   //===--------------------------------------------------------------------===//
0605   // Intrinsic creation methods
0606   //===--------------------------------------------------------------------===//
0607 
0608   /// Create and insert a memset to the specified pointer and the
0609   /// specified value.
0610   ///
0611   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
0612   /// specified, it will be added to the instruction. Likewise with alias.scope
0613   /// and noalias tags.
0614   CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
0615                          MaybeAlign Align, bool isVolatile = false,
0616                          MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
0617                          MDNode *NoAliasTag = nullptr) {
0618     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
0619                         TBAATag, ScopeTag, NoAliasTag);
0620   }
0621 
0622   CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
0623                          bool isVolatile = false, MDNode *TBAATag = nullptr,
0624                          MDNode *ScopeTag = nullptr,
0625                          MDNode *NoAliasTag = nullptr);
0626 
0627   CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val,
0628                                Value *Size, bool IsVolatile = false,
0629                                MDNode *TBAATag = nullptr,
0630                                MDNode *ScopeTag = nullptr,
0631                                MDNode *NoAliasTag = nullptr);
0632 
0633   /// Create and insert an element unordered-atomic memset of the region of
0634   /// memory starting at the given pointer to the given value.
0635   ///
0636   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
0637   /// specified, it will be added to the instruction. Likewise with alias.scope
0638   /// and noalias tags.
0639   CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
0640                                                uint64_t Size, Align Alignment,
0641                                                uint32_t ElementSize,
0642                                                MDNode *TBAATag = nullptr,
0643                                                MDNode *ScopeTag = nullptr,
0644                                                MDNode *NoAliasTag = nullptr) {
0645     return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
0646                                               Align(Alignment), ElementSize,
0647                                               TBAATag, ScopeTag, NoAliasTag);
0648   }
0649 
0650   CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
0651                          Value *ArraySize, ArrayRef<OperandBundleDef> OpB,
0652                          Function *MallocF = nullptr, const Twine &Name = "");
0653 
0654   /// CreateMalloc - Generate the IR for a call to malloc:
0655   /// 1. Compute the malloc call's argument as the specified type's size,
0656   ///    possibly multiplied by the array size if the array size is not
0657   ///    constant 1.
0658   /// 2. Call malloc with that argument.
0659   CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
0660                          Value *ArraySize, Function *MallocF = nullptr,
0661                          const Twine &Name = "");
0662   /// Generate the IR for a call to the builtin free function.
0663   CallInst *CreateFree(Value *Source, ArrayRef<OperandBundleDef> Bundles = {});
0664 
0665   CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
0666                                                Value *Size, Align Alignment,
0667                                                uint32_t ElementSize,
0668                                                MDNode *TBAATag = nullptr,
0669                                                MDNode *ScopeTag = nullptr,
0670                                                MDNode *NoAliasTag = nullptr);
0671 
0672   /// Create and insert a memcpy between the specified pointers.
0673   ///
0674   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
0675   /// specified, it will be added to the instruction. Likewise with alias.scope
0676   /// and noalias tags.
0677   CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
0678                          MaybeAlign SrcAlign, uint64_t Size,
0679                          bool isVolatile = false, MDNode *TBAATag = nullptr,
0680                          MDNode *TBAAStructTag = nullptr,
0681                          MDNode *ScopeTag = nullptr,
0682                          MDNode *NoAliasTag = nullptr) {
0683     return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
0684                         isVolatile, TBAATag, TBAAStructTag, ScopeTag,
0685                         NoAliasTag);
0686   }
0687 
0688   CallInst *CreateMemTransferInst(
0689       Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
0690       MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
0691       MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
0692       MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
0693 
0694   CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
0695                          MaybeAlign SrcAlign, Value *Size,
0696                          bool isVolatile = false, MDNode *TBAATag = nullptr,
0697                          MDNode *TBAAStructTag = nullptr,
0698                          MDNode *ScopeTag = nullptr,
0699                          MDNode *NoAliasTag = nullptr) {
0700     return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
0701                                  SrcAlign, Size, isVolatile, TBAATag,
0702                                  TBAAStructTag, ScopeTag, NoAliasTag);
0703   }
0704 
0705   CallInst *
0706   CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
0707                      MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
0708                      MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
0709                      MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
0710     return CreateMemTransferInst(Intrinsic::memcpy_inline, Dst, DstAlign, Src,
0711                                  SrcAlign, Size, isVolatile, TBAATag,
0712                                  TBAAStructTag, ScopeTag, NoAliasTag);
0713   }
0714 
0715   /// Create and insert an element unordered-atomic memcpy between the
0716   /// specified pointers.
0717   ///
0718   /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
0719   ///
0720   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
0721   /// specified, it will be added to the instruction. Likewise with alias.scope
0722   /// and noalias tags.
0723   CallInst *CreateElementUnorderedAtomicMemCpy(
0724       Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
0725       uint32_t ElementSize, MDNode *TBAATag = nullptr,
0726       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
0727       MDNode *NoAliasTag = nullptr);
0728 
0729   CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
0730                           MaybeAlign SrcAlign, uint64_t Size,
0731                           bool isVolatile = false, MDNode *TBAATag = nullptr,
0732                           MDNode *ScopeTag = nullptr,
0733                           MDNode *NoAliasTag = nullptr) {
0734     return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
0735                          isVolatile, TBAATag, ScopeTag, NoAliasTag);
0736   }
0737 
0738   CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
0739                           MaybeAlign SrcAlign, Value *Size,
0740                           bool isVolatile = false, MDNode *TBAATag = nullptr,
0741                           MDNode *ScopeTag = nullptr,
0742                           MDNode *NoAliasTag = nullptr) {
0743     return CreateMemTransferInst(Intrinsic::memmove, Dst, DstAlign, Src,
0744                                  SrcAlign, Size, isVolatile, TBAATag,
0745                                  /*TBAAStructTag=*/nullptr, ScopeTag,
0746                                  NoAliasTag);
0747   }
0748 
0749   /// \brief Create and insert an element unordered-atomic memmove between the
0750   /// specified pointers.
0751   ///
0752   /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
0753   /// respectively.
0754   ///
0755   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
0756   /// specified, it will be added to the instruction. Likewise with alias.scope
0757   /// and noalias tags.
0758   CallInst *CreateElementUnorderedAtomicMemMove(
0759       Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
0760       uint32_t ElementSize, MDNode *TBAATag = nullptr,
0761       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
0762       MDNode *NoAliasTag = nullptr);
0763 
0764 private:
0765   CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src);
0766 
0767 public:
0768   /// Create a sequential vector fadd reduction intrinsic of the source vector.
0769   /// The first parameter is a scalar accumulator value. An unordered reduction
0770   /// can be created by adding the reassoc fast-math flag to the resulting
0771   /// sequential reduction.
0772   CallInst *CreateFAddReduce(Value *Acc, Value *Src);
0773 
0774   /// Create a sequential vector fmul reduction intrinsic of the source vector.
0775   /// The first parameter is a scalar accumulator value. An unordered reduction
0776   /// can be created by adding the reassoc fast-math flag to the resulting
0777   /// sequential reduction.
0778   CallInst *CreateFMulReduce(Value *Acc, Value *Src);
0779 
0780   /// Create a vector int add reduction intrinsic of the source vector.
0781   CallInst *CreateAddReduce(Value *Src);
0782 
0783   /// Create a vector int mul reduction intrinsic of the source vector.
0784   CallInst *CreateMulReduce(Value *Src);
0785 
0786   /// Create a vector int AND reduction intrinsic of the source vector.
0787   CallInst *CreateAndReduce(Value *Src);
0788 
0789   /// Create a vector int OR reduction intrinsic of the source vector.
0790   CallInst *CreateOrReduce(Value *Src);
0791 
0792   /// Create a vector int XOR reduction intrinsic of the source vector.
0793   CallInst *CreateXorReduce(Value *Src);
0794 
0795   /// Create a vector integer max reduction intrinsic of the source
0796   /// vector.
0797   CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
0798 
0799   /// Create a vector integer min reduction intrinsic of the source
0800   /// vector.
0801   CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
0802 
0803   /// Create a vector float max reduction intrinsic of the source
0804   /// vector.
0805   CallInst *CreateFPMaxReduce(Value *Src);
0806 
0807   /// Create a vector float min reduction intrinsic of the source
0808   /// vector.
0809   CallInst *CreateFPMinReduce(Value *Src);
0810 
0811   /// Create a vector float maximum reduction intrinsic of the source
0812   /// vector. This variant follows the NaN and signed zero semantic of
0813   /// llvm.maximum intrinsic.
0814   CallInst *CreateFPMaximumReduce(Value *Src);
0815 
0816   /// Create a vector float minimum reduction intrinsic of the source
0817   /// vector. This variant follows the NaN and signed zero semantic of
0818   /// llvm.minimum intrinsic.
0819   CallInst *CreateFPMinimumReduce(Value *Src);
0820 
0821   /// Create a lifetime.start intrinsic.
0822   ///
0823   /// If the pointer isn't i8* it will be converted.
0824   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
0825 
0826   /// Create a lifetime.end intrinsic.
0827   ///
0828   /// If the pointer isn't i8* it will be converted.
0829   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
0830 
0831   /// Create a call to invariant.start intrinsic.
0832   ///
0833   /// If the pointer isn't i8* it will be converted.
0834   CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
0835 
0836   /// Create a call to llvm.threadlocal.address intrinsic.
0837   CallInst *CreateThreadLocalAddress(Value *Ptr);
0838 
0839   /// Create a call to Masked Load intrinsic
0840   CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask,
0841                              Value *PassThru = nullptr, const Twine &Name = "");
0842 
0843   /// Create a call to Masked Store intrinsic
0844   CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
0845                               Value *Mask);
0846 
0847   /// Create a call to Masked Gather intrinsic
0848   CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment,
0849                                Value *Mask = nullptr, Value *PassThru = nullptr,
0850                                const Twine &Name = "");
0851 
0852   /// Create a call to Masked Scatter intrinsic
0853   CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
0854                                 Value *Mask = nullptr);
0855 
0856   /// Create a call to Masked Expand Load intrinsic
0857   CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
0858                                    Value *Mask = nullptr,
0859                                    Value *PassThru = nullptr,
0860                                    const Twine &Name = "");
0861 
0862   /// Create a call to Masked Compress Store intrinsic
0863   CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr, MaybeAlign Align,
0864                                       Value *Mask = nullptr);
0865 
0866   /// Return an all true boolean vector (mask) with \p NumElts lanes.
0867   Value *getAllOnesMask(ElementCount NumElts) {
0868     VectorType *VTy = VectorType::get(Type::getInt1Ty(Context), NumElts);
0869     return Constant::getAllOnesValue(VTy);
0870   }
0871 
0872   /// Create an assume intrinsic call that allows the optimizer to
0873   /// assume that the provided condition will be true.
0874   ///
0875   /// The optional argument \p OpBundles specifies operand bundles that are
0876   /// added to the call instruction.
0877   CallInst *CreateAssumption(Value *Cond,
0878                              ArrayRef<OperandBundleDef> OpBundles = {});
0879 
0880   /// Create a llvm.experimental.noalias.scope.decl intrinsic call.
0881   Instruction *CreateNoAliasScopeDeclaration(Value *Scope);
0882   Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) {
0883     return CreateNoAliasScopeDeclaration(
0884         MetadataAsValue::get(Context, ScopeTag));
0885   }
0886 
0887   /// Create a call to the experimental.gc.statepoint intrinsic to
0888   /// start a new statepoint sequence.
0889   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
0890                                    FunctionCallee ActualCallee,
0891                                    ArrayRef<Value *> CallArgs,
0892                                    std::optional<ArrayRef<Value *>> DeoptArgs,
0893                                    ArrayRef<Value *> GCArgs,
0894                                    const Twine &Name = "");
0895 
0896   /// Create a call to the experimental.gc.statepoint intrinsic to
0897   /// start a new statepoint sequence.
0898   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
0899                                    FunctionCallee ActualCallee, uint32_t Flags,
0900                                    ArrayRef<Value *> CallArgs,
0901                                    std::optional<ArrayRef<Use>> TransitionArgs,
0902                                    std::optional<ArrayRef<Use>> DeoptArgs,
0903                                    ArrayRef<Value *> GCArgs,
0904                                    const Twine &Name = "");
0905 
0906   /// Conveninence function for the common case when CallArgs are filled
0907   /// in using ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
0908   /// .get()'ed to get the Value pointer.
0909   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
0910                                    FunctionCallee ActualCallee,
0911                                    ArrayRef<Use> CallArgs,
0912                                    std::optional<ArrayRef<Value *>> DeoptArgs,
0913                                    ArrayRef<Value *> GCArgs,
0914                                    const Twine &Name = "");
0915 
0916   /// Create an invoke to the experimental.gc.statepoint intrinsic to
0917   /// start a new statepoint sequence.
0918   InvokeInst *
0919   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
0920                            FunctionCallee ActualInvokee, BasicBlock *NormalDest,
0921                            BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
0922                            std::optional<ArrayRef<Value *>> DeoptArgs,
0923                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
0924 
0925   /// Create an invoke to the experimental.gc.statepoint intrinsic to
0926   /// start a new statepoint sequence.
0927   InvokeInst *CreateGCStatepointInvoke(
0928       uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee,
0929       BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
0930       ArrayRef<Value *> InvokeArgs, std::optional<ArrayRef<Use>> TransitionArgs,
0931       std::optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
0932       const Twine &Name = "");
0933 
0934   // Convenience function for the common case when CallArgs are filled in using
0935   // ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
0936   // get the Value *.
0937   InvokeInst *
0938   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
0939                            FunctionCallee ActualInvokee, BasicBlock *NormalDest,
0940                            BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
0941                            std::optional<ArrayRef<Value *>> DeoptArgs,
0942                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
0943 
0944   /// Create a call to the experimental.gc.result intrinsic to extract
0945   /// the result from a call wrapped in a statepoint.
0946   CallInst *CreateGCResult(Instruction *Statepoint,
0947                            Type *ResultType,
0948                            const Twine &Name = "");
0949 
0950   /// Create a call to the experimental.gc.relocate intrinsics to
0951   /// project the relocated value of one pointer from the statepoint.
0952   CallInst *CreateGCRelocate(Instruction *Statepoint,
0953                              int BaseOffset,
0954                              int DerivedOffset,
0955                              Type *ResultType,
0956                              const Twine &Name = "");
0957 
0958   /// Create a call to the experimental.gc.pointer.base intrinsic to get the
0959   /// base pointer for the specified derived pointer.
0960   CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = "");
0961 
0962   /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get
0963   /// the offset of the specified derived pointer from its base.
0964   CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = "");
0965 
0966   /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
0967   /// will be the same type as that of \p Scaling.
0968   Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
0969 
0970   /// Create an expression which evaluates to the number of elements in \p EC
0971   /// at runtime.
0972   Value *CreateElementCount(Type *DstType, ElementCount EC);
0973 
0974   /// Create an expression which evaluates to the number of units in \p Size
0975   /// at runtime.  This works for both units of bits and bytes.
0976   Value *CreateTypeSize(Type *DstType, TypeSize Size);
0977 
0978   /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
0979   Value *CreateStepVector(Type *DstType, const Twine &Name = "");
0980 
0981   /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
0982   /// type.
0983   CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
0984                                  FMFSource FMFSource = {},
0985                                  const Twine &Name = "");
0986 
0987   /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
0988   /// first type.
0989   Value *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
0990                                FMFSource FMFSource = {},
0991                                const Twine &Name = "");
0992 
0993   /// Create a call to intrinsic \p ID with \p Args, mangled using \p Types. If
0994   /// \p FMFSource is provided, copy fast-math-flags from that instruction to
0995   /// the intrinsic.
0996   CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
0997                             ArrayRef<Value *> Args, FMFSource FMFSource = {},
0998                             const Twine &Name = "");
0999 
1000   /// Create a call to intrinsic \p ID with \p RetTy and \p Args. If
1001   /// \p FMFSource is provided, copy fast-math-flags from that instruction to
1002   /// the intrinsic.
1003   CallInst *CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
1004                             ArrayRef<Value *> Args, FMFSource FMFSource = {},
1005                             const Twine &Name = "");
1006 
1007   /// Create call to the minnum intrinsic.
1008   Value *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1009     if (IsFPConstrained) {
1010       return CreateConstrainedFPUnroundedBinOp(
1011           Intrinsic::experimental_constrained_minnum, LHS, RHS, nullptr, Name);
1012     }
1013 
1014     return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
1015   }
1016 
1017   /// Create call to the maxnum intrinsic.
1018   Value *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1019     if (IsFPConstrained) {
1020       return CreateConstrainedFPUnroundedBinOp(
1021           Intrinsic::experimental_constrained_maxnum, LHS, RHS, nullptr, Name);
1022     }
1023 
1024     return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
1025   }
1026 
1027   /// Create call to the minimum intrinsic.
1028   Value *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
1029     return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
1030   }
1031 
1032   /// Create call to the maximum intrinsic.
1033   Value *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
1034     return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
1035   }
1036 
1037   /// Create call to the minimumnum intrinsic.
1038   Value *CreateMinimumNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1039     return CreateBinaryIntrinsic(Intrinsic::minimumnum, LHS, RHS, nullptr,
1040                                  Name);
1041   }
1042 
1043   /// Create call to the maximum intrinsic.
1044   Value *CreateMaximumNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1045     return CreateBinaryIntrinsic(Intrinsic::maximumnum, LHS, RHS, nullptr,
1046                                  Name);
1047   }
1048 
1049   /// Create call to the copysign intrinsic.
1050   Value *CreateCopySign(Value *LHS, Value *RHS, FMFSource FMFSource = {},
1051                         const Twine &Name = "") {
1052     return CreateBinaryIntrinsic(Intrinsic::copysign, LHS, RHS, FMFSource,
1053                                  Name);
1054   }
1055 
1056   /// Create call to the ldexp intrinsic.
1057   Value *CreateLdexp(Value *Src, Value *Exp, FMFSource FMFSource = {},
1058                      const Twine &Name = "") {
1059     assert(!IsFPConstrained && "TODO: Support strictfp");
1060     return CreateIntrinsic(Intrinsic::ldexp, {Src->getType(), Exp->getType()},
1061                            {Src, Exp}, FMFSource, Name);
1062   }
1063 
1064   /// Create a call to the arithmetic_fence intrinsic.
1065   CallInst *CreateArithmeticFence(Value *Val, Type *DstType,
1066                                   const Twine &Name = "") {
1067     return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr,
1068                            Name);
1069   }
1070 
1071   /// Create a call to the vector.extract intrinsic.
1072   CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx,
1073                                 const Twine &Name = "") {
1074     return CreateIntrinsic(Intrinsic::vector_extract,
1075                            {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr,
1076                            Name);
1077   }
1078 
1079   /// Create a call to the vector.insert intrinsic.
1080   CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
1081                                Value *Idx, const Twine &Name = "") {
1082     return CreateIntrinsic(Intrinsic::vector_insert,
1083                            {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx},
1084                            nullptr, Name);
1085   }
1086 
1087   /// Create a call to llvm.stacksave
1088   CallInst *CreateStackSave(const Twine &Name = "") {
1089     const DataLayout &DL = BB->getDataLayout();
1090     return CreateIntrinsic(Intrinsic::stacksave, {DL.getAllocaPtrType(Context)},
1091                            {}, nullptr, Name);
1092   }
1093 
1094   /// Create a call to llvm.stackrestore
1095   CallInst *CreateStackRestore(Value *Ptr, const Twine &Name = "") {
1096     return CreateIntrinsic(Intrinsic::stackrestore, {Ptr->getType()}, {Ptr},
1097                            nullptr, Name);
1098   }
1099 
1100   /// Create a call to llvm.experimental_cttz_elts
1101   Value *CreateCountTrailingZeroElems(Type *ResTy, Value *Mask,
1102                                       bool ZeroIsPoison = true,
1103                                       const Twine &Name = "") {
1104     return CreateIntrinsic(Intrinsic::experimental_cttz_elts,
1105                            {ResTy, Mask->getType()},
1106                            {Mask, getInt1(ZeroIsPoison)}, nullptr, Name);
1107   }
1108 
1109 private:
1110   /// Create a call to a masked intrinsic with given Id.
1111   CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
1112                                   ArrayRef<Type *> OverloadedTypes,
1113                                   const Twine &Name = "");
1114 
1115   //===--------------------------------------------------------------------===//
1116   // Instruction creation methods: Terminators
1117   //===--------------------------------------------------------------------===//
1118 
1119 private:
1120   /// Helper to add branch weight and unpredictable metadata onto an
1121   /// instruction.
1122   /// \returns The annotated instruction.
1123   template <typename InstTy>
1124   InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
1125     if (Weights)
1126       I->setMetadata(LLVMContext::MD_prof, Weights);
1127     if (Unpredictable)
1128       I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
1129     return I;
1130   }
1131 
1132 public:
1133   /// Create a 'ret void' instruction.
1134   ReturnInst *CreateRetVoid() {
1135     return Insert(ReturnInst::Create(Context));
1136   }
1137 
1138   /// Create a 'ret <val>' instruction.
1139   ReturnInst *CreateRet(Value *V) {
1140     return Insert(ReturnInst::Create(Context, V));
1141   }
1142 
1143   /// Create a sequence of N insertvalue instructions,
1144   /// with one Value from the retVals array each, that build a aggregate
1145   /// return value one value at a time, and a ret instruction to return
1146   /// the resulting aggregate value.
1147   ///
1148   /// This is a convenience function for code that uses aggregate return values
1149   /// as a vehicle for having multiple return values.
1150   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
1151     Value *V = PoisonValue::get(getCurrentFunctionReturnType());
1152     for (unsigned i = 0; i != N; ++i)
1153       V = CreateInsertValue(V, retVals[i], i, "mrv");
1154     return Insert(ReturnInst::Create(Context, V));
1155   }
1156 
1157   /// Create an unconditional 'br label X' instruction.
1158   BranchInst *CreateBr(BasicBlock *Dest) {
1159     return Insert(BranchInst::Create(Dest));
1160   }
1161 
1162   /// Create a conditional 'br Cond, TrueDest, FalseDest'
1163   /// instruction.
1164   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1165                            MDNode *BranchWeights = nullptr,
1166                            MDNode *Unpredictable = nullptr) {
1167     return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
1168                                     BranchWeights, Unpredictable));
1169   }
1170 
1171   /// Create a conditional 'br Cond, TrueDest, FalseDest'
1172   /// instruction. Copy branch meta data if available.
1173   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1174                            Instruction *MDSrc) {
1175     BranchInst *Br = BranchInst::Create(True, False, Cond);
1176     if (MDSrc) {
1177       unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
1178                         LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
1179       Br->copyMetadata(*MDSrc, WL);
1180     }
1181     return Insert(Br);
1182   }
1183 
1184   /// Create a switch instruction with the specified value, default dest,
1185   /// and with a hint for the number of cases that will be added (for efficient
1186   /// allocation).
1187   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1188                            MDNode *BranchWeights = nullptr,
1189                            MDNode *Unpredictable = nullptr) {
1190     return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
1191                                     BranchWeights, Unpredictable));
1192   }
1193 
1194   /// Create an indirect branch instruction with the specified address
1195   /// operand, with an optional hint for the number of destinations that will be
1196   /// added (for efficient allocation).
1197   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1198     return Insert(IndirectBrInst::Create(Addr, NumDests));
1199   }
1200 
1201   /// Create an invoke instruction.
1202   InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1203                            BasicBlock *NormalDest, BasicBlock *UnwindDest,
1204                            ArrayRef<Value *> Args,
1205                            ArrayRef<OperandBundleDef> OpBundles,
1206                            const Twine &Name = "") {
1207     InvokeInst *II =
1208         InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles);
1209     if (IsFPConstrained)
1210       setConstrainedFPCallAttr(II);
1211     return Insert(II, Name);
1212   }
1213   InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1214                            BasicBlock *NormalDest, BasicBlock *UnwindDest,
1215                            ArrayRef<Value *> Args = {},
1216                            const Twine &Name = "") {
1217     InvokeInst *II =
1218         InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args);
1219     if (IsFPConstrained)
1220       setConstrainedFPCallAttr(II);
1221     return Insert(II, Name);
1222   }
1223 
1224   InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1225                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1226                            ArrayRef<OperandBundleDef> OpBundles,
1227                            const Twine &Name = "") {
1228     return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1229                         NormalDest, UnwindDest, Args, OpBundles, Name);
1230   }
1231 
1232   InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1233                            BasicBlock *UnwindDest, ArrayRef<Value *> Args = {},
1234                            const Twine &Name = "") {
1235     return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1236                         NormalDest, UnwindDest, Args, Name);
1237   }
1238 
1239   /// \brief Create a callbr instruction.
1240   CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1241                            BasicBlock *DefaultDest,
1242                            ArrayRef<BasicBlock *> IndirectDests,
1243                            ArrayRef<Value *> Args = {},
1244                            const Twine &Name = "") {
1245     return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
1246                                      Args), Name);
1247   }
1248   CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1249                            BasicBlock *DefaultDest,
1250                            ArrayRef<BasicBlock *> IndirectDests,
1251                            ArrayRef<Value *> Args,
1252                            ArrayRef<OperandBundleDef> OpBundles,
1253                            const Twine &Name = "") {
1254     return Insert(
1255         CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1256                            OpBundles), Name);
1257   }
1258 
1259   CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1260                            ArrayRef<BasicBlock *> IndirectDests,
1261                            ArrayRef<Value *> Args = {},
1262                            const Twine &Name = "") {
1263     return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1264                         DefaultDest, IndirectDests, Args, Name);
1265   }
1266   CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1267                            ArrayRef<BasicBlock *> IndirectDests,
1268                            ArrayRef<Value *> Args,
1269                            ArrayRef<OperandBundleDef> OpBundles,
1270                            const Twine &Name = "") {
1271     return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1272                         DefaultDest, IndirectDests, Args, Name);
1273   }
1274 
1275   ResumeInst *CreateResume(Value *Exn) {
1276     return Insert(ResumeInst::Create(Exn));
1277   }
1278 
1279   CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1280                                       BasicBlock *UnwindBB = nullptr) {
1281     return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1282   }
1283 
1284   CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1285                                      unsigned NumHandlers,
1286                                      const Twine &Name = "") {
1287     return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1288                   Name);
1289   }
1290 
1291   CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1292                                const Twine &Name = "") {
1293     return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1294   }
1295 
1296   CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1297                                    ArrayRef<Value *> Args = {},
1298                                    const Twine &Name = "") {
1299     return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1300   }
1301 
1302   CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1303     return Insert(CatchReturnInst::Create(CatchPad, BB));
1304   }
1305 
1306   UnreachableInst *CreateUnreachable() {
1307     return Insert(new UnreachableInst(Context));
1308   }
1309 
1310   //===--------------------------------------------------------------------===//
1311   // Instruction creation methods: Binary Operators
1312   //===--------------------------------------------------------------------===//
1313 private:
1314   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1315                                           Value *LHS, Value *RHS,
1316                                           const Twine &Name,
1317                                           bool HasNUW, bool HasNSW) {
1318     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1319     if (HasNUW) BO->setHasNoUnsignedWrap();
1320     if (HasNSW) BO->setHasNoSignedWrap();
1321     return BO;
1322   }
1323 
1324   Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1325                           FastMathFlags FMF) const {
1326     if (!FPMD)
1327       FPMD = DefaultFPMathTag;
1328     if (FPMD)
1329       I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1330     I->setFastMathFlags(FMF);
1331     return I;
1332   }
1333 
1334   Value *getConstrainedFPRounding(std::optional<RoundingMode> Rounding) {
1335     RoundingMode UseRounding = DefaultConstrainedRounding;
1336 
1337     if (Rounding)
1338       UseRounding = *Rounding;
1339 
1340     std::optional<StringRef> RoundingStr =
1341         convertRoundingModeToStr(UseRounding);
1342     assert(RoundingStr && "Garbage strict rounding mode!");
1343     auto *RoundingMDS = MDString::get(Context, *RoundingStr);
1344 
1345     return MetadataAsValue::get(Context, RoundingMDS);
1346   }
1347 
1348   Value *getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except) {
1349     std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(
1350         Except.value_or(DefaultConstrainedExcept));
1351     assert(ExceptStr && "Garbage strict exception behavior!");
1352     auto *ExceptMDS = MDString::get(Context, *ExceptStr);
1353 
1354     return MetadataAsValue::get(Context, ExceptMDS);
1355   }
1356 
1357   Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1358     assert(CmpInst::isFPPredicate(Predicate) &&
1359            Predicate != CmpInst::FCMP_FALSE &&
1360            Predicate != CmpInst::FCMP_TRUE &&
1361            "Invalid constrained FP comparison predicate!");
1362 
1363     StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
1364     auto *PredicateMDS = MDString::get(Context, PredicateStr);
1365 
1366     return MetadataAsValue::get(Context, PredicateMDS);
1367   }
1368 
1369 public:
1370   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1371                    bool HasNUW = false, bool HasNSW = false) {
1372     if (Value *V =
1373             Folder.FoldNoWrapBinOp(Instruction::Add, LHS, RHS, HasNUW, HasNSW))
1374       return V;
1375     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, HasNUW,
1376                                    HasNSW);
1377   }
1378 
1379   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1380     return CreateAdd(LHS, RHS, Name, false, true);
1381   }
1382 
1383   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1384     return CreateAdd(LHS, RHS, Name, true, false);
1385   }
1386 
1387   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1388                    bool HasNUW = false, bool HasNSW = false) {
1389     if (Value *V =
1390             Folder.FoldNoWrapBinOp(Instruction::Sub, LHS, RHS, HasNUW, HasNSW))
1391       return V;
1392     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, HasNUW,
1393                                    HasNSW);
1394   }
1395 
1396   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1397     return CreateSub(LHS, RHS, Name, false, true);
1398   }
1399 
1400   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1401     return CreateSub(LHS, RHS, Name, true, false);
1402   }
1403 
1404   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1405                    bool HasNUW = false, bool HasNSW = false) {
1406     if (Value *V =
1407             Folder.FoldNoWrapBinOp(Instruction::Mul, LHS, RHS, HasNUW, HasNSW))
1408       return V;
1409     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, HasNUW,
1410                                    HasNSW);
1411   }
1412 
1413   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1414     return CreateMul(LHS, RHS, Name, false, true);
1415   }
1416 
1417   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1418     return CreateMul(LHS, RHS, Name, true, false);
1419   }
1420 
1421   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1422                     bool isExact = false) {
1423     if (Value *V = Folder.FoldExactBinOp(Instruction::UDiv, LHS, RHS, isExact))
1424       return V;
1425     if (!isExact)
1426       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1427     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1428   }
1429 
1430   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1431     return CreateUDiv(LHS, RHS, Name, true);
1432   }
1433 
1434   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1435                     bool isExact = false) {
1436     if (Value *V = Folder.FoldExactBinOp(Instruction::SDiv, LHS, RHS, isExact))
1437       return V;
1438     if (!isExact)
1439       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1440     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1441   }
1442 
1443   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1444     return CreateSDiv(LHS, RHS, Name, true);
1445   }
1446 
1447   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1448     if (Value *V = Folder.FoldBinOp(Instruction::URem, LHS, RHS))
1449       return V;
1450     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1451   }
1452 
1453   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1454     if (Value *V = Folder.FoldBinOp(Instruction::SRem, LHS, RHS))
1455       return V;
1456     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1457   }
1458 
1459   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1460                    bool HasNUW = false, bool HasNSW = false) {
1461     if (Value *V =
1462             Folder.FoldNoWrapBinOp(Instruction::Shl, LHS, RHS, HasNUW, HasNSW))
1463       return V;
1464     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1465                                    HasNUW, HasNSW);
1466   }
1467 
1468   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1469                    bool HasNUW = false, bool HasNSW = false) {
1470     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1471                      HasNUW, HasNSW);
1472   }
1473 
1474   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1475                    bool HasNUW = false, bool HasNSW = false) {
1476     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1477                      HasNUW, HasNSW);
1478   }
1479 
1480   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1481                     bool isExact = false) {
1482     if (Value *V = Folder.FoldExactBinOp(Instruction::LShr, LHS, RHS, isExact))
1483       return V;
1484     if (!isExact)
1485       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1486     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1487   }
1488 
1489   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1490                     bool isExact = false) {
1491     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1492   }
1493 
1494   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1495                     bool isExact = false) {
1496     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1497   }
1498 
1499   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1500                     bool isExact = false) {
1501     if (Value *V = Folder.FoldExactBinOp(Instruction::AShr, LHS, RHS, isExact))
1502       return V;
1503     if (!isExact)
1504       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1505     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1506   }
1507 
1508   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1509                     bool isExact = false) {
1510     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1511   }
1512 
1513   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1514                     bool isExact = false) {
1515     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1516   }
1517 
1518   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1519     if (auto *V = Folder.FoldBinOp(Instruction::And, LHS, RHS))
1520       return V;
1521     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1522   }
1523 
1524   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1525     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1526   }
1527 
1528   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1529     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1530   }
1531 
1532   Value *CreateAnd(ArrayRef<Value*> Ops) {
1533     assert(!Ops.empty());
1534     Value *Accum = Ops[0];
1535     for (unsigned i = 1; i < Ops.size(); i++)
1536       Accum = CreateAnd(Accum, Ops[i]);
1537     return Accum;
1538   }
1539 
1540   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1541     if (auto *V = Folder.FoldBinOp(Instruction::Or, LHS, RHS))
1542       return V;
1543     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1544   }
1545 
1546   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1547     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1548   }
1549 
1550   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1551     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1552   }
1553 
1554   Value *CreateOr(ArrayRef<Value*> Ops) {
1555     assert(!Ops.empty());
1556     Value *Accum = Ops[0];
1557     for (unsigned i = 1; i < Ops.size(); i++)
1558       Accum = CreateOr(Accum, Ops[i]);
1559     return Accum;
1560   }
1561 
1562   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1563     if (Value *V = Folder.FoldBinOp(Instruction::Xor, LHS, RHS))
1564       return V;
1565     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1566   }
1567 
1568   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1569     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1570   }
1571 
1572   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1573     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1574   }
1575 
1576   Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1577                     MDNode *FPMD = nullptr) {
1578     return CreateFAddFMF(L, R, {}, Name, FPMD);
1579   }
1580 
1581   Value *CreateFAddFMF(Value *L, Value *R, FMFSource FMFSource,
1582                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1583     if (IsFPConstrained)
1584       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1585                                       L, R, FMFSource, Name, FPMD);
1586 
1587     if (Value *V =
1588             Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMFSource.get(FMF)))
1589       return V;
1590     Instruction *I =
1591         setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMFSource.get(FMF));
1592     return Insert(I, Name);
1593   }
1594 
1595   Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1596                     MDNode *FPMD = nullptr) {
1597     return CreateFSubFMF(L, R, {}, Name, FPMD);
1598   }
1599 
1600   Value *CreateFSubFMF(Value *L, Value *R, FMFSource FMFSource,
1601                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1602     if (IsFPConstrained)
1603       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1604                                       L, R, FMFSource, Name, FPMD);
1605 
1606     if (Value *V =
1607             Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMFSource.get(FMF)))
1608       return V;
1609     Instruction *I =
1610         setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMFSource.get(FMF));
1611     return Insert(I, Name);
1612   }
1613 
1614   Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1615                     MDNode *FPMD = nullptr) {
1616     return CreateFMulFMF(L, R, {}, Name, FPMD);
1617   }
1618 
1619   Value *CreateFMulFMF(Value *L, Value *R, FMFSource FMFSource,
1620                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1621     if (IsFPConstrained)
1622       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1623                                       L, R, FMFSource, Name, FPMD);
1624 
1625     if (Value *V =
1626             Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMFSource.get(FMF)))
1627       return V;
1628     Instruction *I =
1629         setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMFSource.get(FMF));
1630     return Insert(I, Name);
1631   }
1632 
1633   Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1634                     MDNode *FPMD = nullptr) {
1635     return CreateFDivFMF(L, R, {}, Name, FPMD);
1636   }
1637 
1638   Value *CreateFDivFMF(Value *L, Value *R, FMFSource FMFSource,
1639                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1640     if (IsFPConstrained)
1641       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1642                                       L, R, FMFSource, Name, FPMD);
1643 
1644     if (Value *V =
1645             Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMFSource.get(FMF)))
1646       return V;
1647     Instruction *I =
1648         setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMFSource.get(FMF));
1649     return Insert(I, Name);
1650   }
1651 
1652   Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1653                     MDNode *FPMD = nullptr) {
1654     return CreateFRemFMF(L, R, {}, Name, FPMD);
1655   }
1656 
1657   Value *CreateFRemFMF(Value *L, Value *R, FMFSource FMFSource,
1658                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1659     if (IsFPConstrained)
1660       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1661                                       L, R, FMFSource, Name, FPMD);
1662 
1663     if (Value *V =
1664             Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMFSource.get(FMF)))
1665       return V;
1666     Instruction *I =
1667         setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMFSource.get(FMF));
1668     return Insert(I, Name);
1669   }
1670 
1671   Value *CreateBinOp(Instruction::BinaryOps Opc,
1672                      Value *LHS, Value *RHS, const Twine &Name = "",
1673                      MDNode *FPMathTag = nullptr) {
1674     return CreateBinOpFMF(Opc, LHS, RHS, {}, Name, FPMathTag);
1675   }
1676 
1677   Value *CreateBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
1678                         FMFSource FMFSource, const Twine &Name = "",
1679                         MDNode *FPMathTag = nullptr) {
1680     if (Value *V = Folder.FoldBinOp(Opc, LHS, RHS))
1681       return V;
1682     Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1683     if (isa<FPMathOperator>(BinOp))
1684       setFPAttrs(BinOp, FPMathTag, FMFSource.get(FMF));
1685     return Insert(BinOp, Name);
1686   }
1687 
1688   Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1689     assert(Cond2->getType()->isIntOrIntVectorTy(1));
1690     return CreateSelect(Cond1, Cond2,
1691                         ConstantInt::getNullValue(Cond2->getType()), Name);
1692   }
1693 
1694   Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1695     assert(Cond2->getType()->isIntOrIntVectorTy(1));
1696     return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()),
1697                         Cond2, Name);
1698   }
1699 
1700   Value *CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2,
1701                          const Twine &Name = "") {
1702     switch (Opc) {
1703     case Instruction::And:
1704       return CreateLogicalAnd(Cond1, Cond2, Name);
1705     case Instruction::Or:
1706       return CreateLogicalOr(Cond1, Cond2, Name);
1707     default:
1708       break;
1709     }
1710     llvm_unreachable("Not a logical operation.");
1711   }
1712 
1713   // NOTE: this is sequential, non-commutative, ordered reduction!
1714   Value *CreateLogicalOr(ArrayRef<Value *> Ops) {
1715     assert(!Ops.empty());
1716     Value *Accum = Ops[0];
1717     for (unsigned i = 1; i < Ops.size(); i++)
1718       Accum = CreateLogicalOr(Accum, Ops[i]);
1719     return Accum;
1720   }
1721 
1722   CallInst *CreateConstrainedFPBinOp(
1723       Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource = {},
1724       const Twine &Name = "", MDNode *FPMathTag = nullptr,
1725       std::optional<RoundingMode> Rounding = std::nullopt,
1726       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1727 
1728   CallInst *CreateConstrainedFPUnroundedBinOp(
1729       Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource = {},
1730       const Twine &Name = "", MDNode *FPMathTag = nullptr,
1731       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1732 
1733   Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNSW = false) {
1734     return CreateSub(Constant::getNullValue(V->getType()), V, Name,
1735                      /*HasNUW=*/0, HasNSW);
1736   }
1737 
1738   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1739     return CreateNeg(V, Name, /*HasNSW=*/true);
1740   }
1741 
1742   Value *CreateFNeg(Value *V, const Twine &Name = "",
1743                     MDNode *FPMathTag = nullptr) {
1744     return CreateFNegFMF(V, {}, Name, FPMathTag);
1745   }
1746 
1747   Value *CreateFNegFMF(Value *V, FMFSource FMFSource, const Twine &Name = "",
1748                        MDNode *FPMathTag = nullptr) {
1749     if (Value *Res =
1750             Folder.FoldUnOpFMF(Instruction::FNeg, V, FMFSource.get(FMF)))
1751       return Res;
1752     return Insert(
1753         setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMFSource.get(FMF)),
1754         Name);
1755   }
1756 
1757   Value *CreateNot(Value *V, const Twine &Name = "") {
1758     return CreateXor(V, Constant::getAllOnesValue(V->getType()), Name);
1759   }
1760 
1761   Value *CreateUnOp(Instruction::UnaryOps Opc,
1762                     Value *V, const Twine &Name = "",
1763                     MDNode *FPMathTag = nullptr) {
1764     if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF))
1765       return Res;
1766     Instruction *UnOp = UnaryOperator::Create(Opc, V);
1767     if (isa<FPMathOperator>(UnOp))
1768       setFPAttrs(UnOp, FPMathTag, FMF);
1769     return Insert(UnOp, Name);
1770   }
1771 
1772   /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1773   /// Correct number of operands must be passed accordingly.
1774   Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1775                       const Twine &Name = "", MDNode *FPMathTag = nullptr);
1776 
1777   //===--------------------------------------------------------------------===//
1778   // Instruction creation methods: Memory Instructions
1779   //===--------------------------------------------------------------------===//
1780 
1781   AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1782                            Value *ArraySize = nullptr, const Twine &Name = "") {
1783     const DataLayout &DL = BB->getDataLayout();
1784     Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1785     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1786   }
1787 
1788   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1789                            const Twine &Name = "") {
1790     const DataLayout &DL = BB->getDataLayout();
1791     Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1792     unsigned AddrSpace = DL.getAllocaAddrSpace();
1793     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1794   }
1795 
1796   /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1797   /// converting the string to 'bool' for the isVolatile parameter.
1798   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1799     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1800   }
1801 
1802   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1803     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1804   }
1805 
1806   LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1807                        const Twine &Name = "") {
1808     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name);
1809   }
1810 
1811   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1812     return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile);
1813   }
1814 
1815   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1816                               const char *Name) {
1817     return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1818   }
1819 
1820   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1821                               const Twine &Name = "") {
1822     return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1823   }
1824 
1825   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1826                               bool isVolatile, const Twine &Name = "") {
1827     if (!Align) {
1828       const DataLayout &DL = BB->getDataLayout();
1829       Align = DL.getABITypeAlign(Ty);
1830     }
1831     return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
1832   }
1833 
1834   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
1835                                 bool isVolatile = false) {
1836     if (!Align) {
1837       const DataLayout &DL = BB->getDataLayout();
1838       Align = DL.getABITypeAlign(Val->getType());
1839     }
1840     return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
1841   }
1842   FenceInst *CreateFence(AtomicOrdering Ordering,
1843                          SyncScope::ID SSID = SyncScope::System,
1844                          const Twine &Name = "") {
1845     return Insert(new FenceInst(Context, Ordering, SSID), Name);
1846   }
1847 
1848   AtomicCmpXchgInst *
1849   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1850                       AtomicOrdering SuccessOrdering,
1851                       AtomicOrdering FailureOrdering,
1852                       SyncScope::ID SSID = SyncScope::System) {
1853     if (!Align) {
1854       const DataLayout &DL = BB->getDataLayout();
1855       Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
1856     }
1857 
1858     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering,
1859                                         FailureOrdering, SSID));
1860   }
1861 
1862   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr,
1863                                  Value *Val, MaybeAlign Align,
1864                                  AtomicOrdering Ordering,
1865                                  SyncScope::ID SSID = SyncScope::System) {
1866     if (!Align) {
1867       const DataLayout &DL = BB->getDataLayout();
1868       Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
1869     }
1870 
1871     return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID));
1872   }
1873 
1874   Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1875                    const Twine &Name = "",
1876                    GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1877     if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, NW))
1878       return V;
1879     return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList, NW), Name);
1880   }
1881 
1882   Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1883                            const Twine &Name = "") {
1884     return CreateGEP(Ty, Ptr, IdxList, Name, GEPNoWrapFlags::inBounds());
1885   }
1886 
1887   Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1888                             const Twine &Name = "") {
1889     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1890 
1891     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::none()))
1892       return V;
1893 
1894     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1895   }
1896 
1897   Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1898                                     const Twine &Name = "") {
1899     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1900 
1901     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::inBounds()))
1902       return V;
1903 
1904     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1905   }
1906 
1907   Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1908                             const Twine &Name = "",
1909                             GEPNoWrapFlags NWFlags = GEPNoWrapFlags::none()) {
1910     Value *Idxs[] = {
1911       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1912       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1913     };
1914 
1915     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, NWFlags))
1916       return V;
1917 
1918     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs, NWFlags), Name);
1919   }
1920 
1921   Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1922                                     unsigned Idx1, const Twine &Name = "") {
1923     Value *Idxs[] = {
1924       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1925       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1926     };
1927 
1928     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::inBounds()))
1929       return V;
1930 
1931     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1932   }
1933 
1934   Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1935                             const Twine &Name = "") {
1936     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1937 
1938     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::none()))
1939       return V;
1940 
1941     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1942   }
1943 
1944   Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1945                                     const Twine &Name = "") {
1946     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1947 
1948     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::inBounds()))
1949       return V;
1950 
1951     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1952   }
1953 
1954   Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1955                             const Twine &Name = "") {
1956     Value *Idxs[] = {
1957       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1958       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1959     };
1960 
1961     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::none()))
1962       return V;
1963 
1964     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1965   }
1966 
1967   Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1968                                     uint64_t Idx1, const Twine &Name = "") {
1969     Value *Idxs[] = {
1970       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1971       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1972     };
1973 
1974     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::inBounds()))
1975       return V;
1976 
1977     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1978   }
1979 
1980   Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1981                          const Twine &Name = "") {
1982     GEPNoWrapFlags NWFlags =
1983         GEPNoWrapFlags::inBounds() | GEPNoWrapFlags::noUnsignedWrap();
1984     return CreateConstGEP2_32(Ty, Ptr, 0, Idx, Name, NWFlags);
1985   }
1986 
1987   Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
1988                       GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1989     return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
1990   }
1991 
1992   Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
1993                               const Twine &Name = "") {
1994     return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
1995                      GEPNoWrapFlags::inBounds());
1996   }
1997 
1998   /// Same as CreateGlobalString, but return a pointer with "i8*" type
1999   /// instead of a pointer to array of i8.
2000   ///
2001   /// If no module is given via \p M, it is take from the insertion point basic
2002   /// block.
2003   LLVM_DEPRECATED("Use CreateGlobalString instead", "CreateGlobalString")
2004   Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
2005                                   unsigned AddressSpace = 0,
2006                                   Module *M = nullptr, bool AddNull = true) {
2007     GlobalVariable *GV =
2008         CreateGlobalString(Str, Name, AddressSpace, M, AddNull);
2009     Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
2010     Constant *Indices[] = {Zero, Zero};
2011     return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
2012                                                   Indices);
2013   }
2014 
2015   //===--------------------------------------------------------------------===//
2016   // Instruction creation methods: Cast/Conversion Operators
2017   //===--------------------------------------------------------------------===//
2018 
2019   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2020                      bool IsNUW = false, bool IsNSW = false) {
2021     if (V->getType() == DestTy)
2022       return V;
2023     if (Value *Folded = Folder.FoldCast(Instruction::Trunc, V, DestTy))
2024       return Folded;
2025     Instruction *I = CastInst::Create(Instruction::Trunc, V, DestTy);
2026     if (IsNUW)
2027       I->setHasNoUnsignedWrap();
2028     if (IsNSW)
2029       I->setHasNoSignedWrap();
2030     return Insert(I, Name);
2031   }
2032 
2033   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "",
2034                     bool IsNonNeg = false) {
2035     if (V->getType() == DestTy)
2036       return V;
2037     if (Value *Folded = Folder.FoldCast(Instruction::ZExt, V, DestTy))
2038       return Folded;
2039     Instruction *I = Insert(new ZExtInst(V, DestTy), Name);
2040     if (IsNonNeg)
2041       I->setNonNeg();
2042     return I;
2043   }
2044 
2045   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
2046     return CreateCast(Instruction::SExt, V, DestTy, Name);
2047   }
2048 
2049   /// Create a ZExt or Trunc from the integer value V to DestTy. Return
2050   /// the value untouched if the type of V is already DestTy.
2051   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
2052                            const Twine &Name = "") {
2053     assert(V->getType()->isIntOrIntVectorTy() &&
2054            DestTy->isIntOrIntVectorTy() &&
2055            "Can only zero extend/truncate integers!");
2056     Type *VTy = V->getType();
2057     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2058       return CreateZExt(V, DestTy, Name);
2059     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2060       return CreateTrunc(V, DestTy, Name);
2061     return V;
2062   }
2063 
2064   /// Create a SExt or Trunc from the integer value V to DestTy. Return
2065   /// the value untouched if the type of V is already DestTy.
2066   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
2067                            const Twine &Name = "") {
2068     assert(V->getType()->isIntOrIntVectorTy() &&
2069            DestTy->isIntOrIntVectorTy() &&
2070            "Can only sign extend/truncate integers!");
2071     Type *VTy = V->getType();
2072     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2073       return CreateSExt(V, DestTy, Name);
2074     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2075       return CreateTrunc(V, DestTy, Name);
2076     return V;
2077   }
2078 
2079   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
2080     if (IsFPConstrained)
2081       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
2082                                      V, DestTy, nullptr, Name);
2083     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
2084   }
2085 
2086   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
2087     if (IsFPConstrained)
2088       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
2089                                      V, DestTy, nullptr, Name);
2090     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
2091   }
2092 
2093   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = "",
2094                       bool IsNonNeg = false) {
2095     if (IsFPConstrained)
2096       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp,
2097                                      V, DestTy, nullptr, Name);
2098     if (Value *Folded = Folder.FoldCast(Instruction::UIToFP, V, DestTy))
2099       return Folded;
2100     Instruction *I = Insert(new UIToFPInst(V, DestTy), Name);
2101     if (IsNonNeg)
2102       I->setNonNeg();
2103     return I;
2104   }
2105 
2106   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2107     if (IsFPConstrained)
2108       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp,
2109                                      V, DestTy, nullptr, Name);
2110     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
2111   }
2112 
2113   Value *CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2114                        MDNode *FPMathTag = nullptr) {
2115     return CreateFPTruncFMF(V, DestTy, {}, Name, FPMathTag);
2116   }
2117 
2118   Value *CreateFPTruncFMF(Value *V, Type *DestTy, FMFSource FMFSource,
2119                           const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2120     if (IsFPConstrained)
2121       return CreateConstrainedFPCast(
2122           Intrinsic::experimental_constrained_fptrunc, V, DestTy, FMFSource,
2123           Name, FPMathTag);
2124     return CreateCast(Instruction::FPTrunc, V, DestTy, Name, FPMathTag,
2125                       FMFSource);
2126   }
2127 
2128   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "",
2129                      MDNode *FPMathTag = nullptr) {
2130     return CreateFPExtFMF(V, DestTy, {}, Name, FPMathTag);
2131   }
2132 
2133   Value *CreateFPExtFMF(Value *V, Type *DestTy, FMFSource FMFSource,
2134                         const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2135     if (IsFPConstrained)
2136       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
2137                                      V, DestTy, FMFSource, Name, FPMathTag);
2138     return CreateCast(Instruction::FPExt, V, DestTy, Name, FPMathTag,
2139                       FMFSource);
2140   }
2141 
2142   Value *CreatePtrToInt(Value *V, Type *DestTy,
2143                         const Twine &Name = "") {
2144     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
2145   }
2146 
2147   Value *CreateIntToPtr(Value *V, Type *DestTy,
2148                         const Twine &Name = "") {
2149     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
2150   }
2151 
2152   Value *CreateBitCast(Value *V, Type *DestTy,
2153                        const Twine &Name = "") {
2154     return CreateCast(Instruction::BitCast, V, DestTy, Name);
2155   }
2156 
2157   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
2158                              const Twine &Name = "") {
2159     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
2160   }
2161 
2162   Value *CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2163     Instruction::CastOps CastOp =
2164         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2165             ? Instruction::BitCast
2166             : Instruction::ZExt;
2167     return CreateCast(CastOp, V, DestTy, Name);
2168   }
2169 
2170   Value *CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2171     Instruction::CastOps CastOp =
2172         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2173             ? Instruction::BitCast
2174             : Instruction::SExt;
2175     return CreateCast(CastOp, V, DestTy, Name);
2176   }
2177 
2178   Value *CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2179     Instruction::CastOps CastOp =
2180         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2181             ? Instruction::BitCast
2182             : Instruction::Trunc;
2183     return CreateCast(CastOp, V, DestTy, Name);
2184   }
2185 
2186   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2187                     const Twine &Name = "", MDNode *FPMathTag = nullptr,
2188                     FMFSource FMFSource = {}) {
2189     if (V->getType() == DestTy)
2190       return V;
2191     if (Value *Folded = Folder.FoldCast(Op, V, DestTy))
2192       return Folded;
2193     Instruction *Cast = CastInst::Create(Op, V, DestTy);
2194     if (isa<FPMathOperator>(Cast))
2195       setFPAttrs(Cast, FPMathTag, FMFSource.get(FMF));
2196     return Insert(Cast, Name);
2197   }
2198 
2199   Value *CreatePointerCast(Value *V, Type *DestTy,
2200                            const Twine &Name = "") {
2201     if (V->getType() == DestTy)
2202       return V;
2203     if (auto *VC = dyn_cast<Constant>(V))
2204       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2205     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2206   }
2207 
2208   // With opaque pointers enabled, this can be substituted with
2209   // CreateAddrSpaceCast.
2210   // TODO: Replace uses of this method and remove the method itself.
2211   Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2212                                              const Twine &Name = "") {
2213     if (V->getType() == DestTy)
2214       return V;
2215 
2216     if (auto *VC = dyn_cast<Constant>(V)) {
2217       return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2218                     Name);
2219     }
2220 
2221     return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2222                   Name);
2223   }
2224 
2225   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2226                        const Twine &Name = "") {
2227     Instruction::CastOps CastOp =
2228         V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2229             ? Instruction::Trunc
2230             : (isSigned ? Instruction::SExt : Instruction::ZExt);
2231     return CreateCast(CastOp, V, DestTy, Name);
2232   }
2233 
2234   Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2235                                 const Twine &Name = "") {
2236     if (V->getType() == DestTy)
2237       return V;
2238     if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2239       return CreatePtrToInt(V, DestTy, Name);
2240     if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2241       return CreateIntToPtr(V, DestTy, Name);
2242 
2243     return CreateBitCast(V, DestTy, Name);
2244   }
2245 
2246   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "",
2247                       MDNode *FPMathTag = nullptr) {
2248     Instruction::CastOps CastOp =
2249         V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2250             ? Instruction::FPTrunc
2251             : Instruction::FPExt;
2252     return CreateCast(CastOp, V, DestTy, Name, FPMathTag);
2253   }
2254 
2255   CallInst *CreateConstrainedFPCast(
2256       Intrinsic::ID ID, Value *V, Type *DestTy, FMFSource FMFSource = {},
2257       const Twine &Name = "", MDNode *FPMathTag = nullptr,
2258       std::optional<RoundingMode> Rounding = std::nullopt,
2259       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2260 
2261   // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2262   // compile time error, instead of converting the string to bool for the
2263   // isSigned parameter.
2264   Value *CreateIntCast(Value *, Type *, const char *) = delete;
2265 
2266   //===--------------------------------------------------------------------===//
2267   // Instruction creation methods: Compare Instructions
2268   //===--------------------------------------------------------------------===//
2269 
2270   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2271     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2272   }
2273 
2274   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2275     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2276   }
2277 
2278   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2279     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2280   }
2281 
2282   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2283     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2284   }
2285 
2286   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2287     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2288   }
2289 
2290   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2291     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2292   }
2293 
2294   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2295     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2296   }
2297 
2298   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2299     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
2300   }
2301 
2302   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2303     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
2304   }
2305 
2306   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2307     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
2308   }
2309 
2310   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2311                        MDNode *FPMathTag = nullptr) {
2312     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2313   }
2314 
2315   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2316                        MDNode *FPMathTag = nullptr) {
2317     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2318   }
2319 
2320   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2321                        MDNode *FPMathTag = nullptr) {
2322     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2323   }
2324 
2325   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2326                        MDNode *FPMathTag = nullptr) {
2327     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2328   }
2329 
2330   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2331                        MDNode *FPMathTag = nullptr) {
2332     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2333   }
2334 
2335   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2336                        MDNode *FPMathTag = nullptr) {
2337     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2338   }
2339 
2340   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2341                        MDNode *FPMathTag = nullptr) {
2342     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2343   }
2344 
2345   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2346                        MDNode *FPMathTag = nullptr) {
2347     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2348   }
2349 
2350   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2351                        MDNode *FPMathTag = nullptr) {
2352     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2353   }
2354 
2355   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2356                        MDNode *FPMathTag = nullptr) {
2357     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2358   }
2359 
2360   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2361                        MDNode *FPMathTag = nullptr) {
2362     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2363   }
2364 
2365   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2366                        MDNode *FPMathTag = nullptr) {
2367     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2368   }
2369 
2370   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2371                        MDNode *FPMathTag = nullptr) {
2372     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2373   }
2374 
2375   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2376                        MDNode *FPMathTag = nullptr) {
2377     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2378   }
2379 
2380   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2381                     const Twine &Name = "") {
2382     if (auto *V = Folder.FoldCmp(P, LHS, RHS))
2383       return V;
2384     return Insert(new ICmpInst(P, LHS, RHS), Name);
2385   }
2386 
2387   // Create a quiet floating-point comparison (i.e. one that raises an FP
2388   // exception only in the case where an input is a signaling NaN).
2389   // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
2390   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2391                     const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2392     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, {}, false);
2393   }
2394 
2395   // Create a quiet floating-point comparison (i.e. one that raises an FP
2396   // exception only in the case where an input is a signaling NaN).
2397   // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
2398   Value *CreateFCmpFMF(CmpInst::Predicate P, Value *LHS, Value *RHS,
2399                        FMFSource FMFSource, const Twine &Name = "",
2400                        MDNode *FPMathTag = nullptr) {
2401     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, FMFSource, false);
2402   }
2403 
2404   Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2405                    const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2406     return CmpInst::isFPPredicate(Pred)
2407                ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag)
2408                : CreateICmp(Pred, LHS, RHS, Name);
2409   }
2410 
2411   // Create a signaling floating-point comparison (i.e. one that raises an FP
2412   // exception whenever an input is any NaN, signaling or quiet).
2413   // Note that this differs from CreateFCmp only if IsFPConstrained is true.
2414   Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS,
2415                      const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2416     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, {}, true);
2417   }
2418 
2419 private:
2420   // Helper routine to create either a signaling or a quiet FP comparison.
2421   Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS,
2422                           const Twine &Name, MDNode *FPMathTag,
2423                           FMFSource FMFSource, bool IsSignaling);
2424 
2425 public:
2426   CallInst *CreateConstrainedFPCmp(
2427       Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R,
2428       const Twine &Name = "",
2429       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2430 
2431   //===--------------------------------------------------------------------===//
2432   // Instruction creation methods: Other Instructions
2433   //===--------------------------------------------------------------------===//
2434 
2435   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2436                      const Twine &Name = "") {
2437     PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
2438     if (isa<FPMathOperator>(Phi))
2439       setFPAttrs(Phi, nullptr /* MDNode* */, FMF);
2440     return Insert(Phi, Name);
2441   }
2442 
2443 private:
2444   CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
2445                              const Twine &Name = "", FMFSource FMFSource = {},
2446                              ArrayRef<OperandBundleDef> OpBundles = {});
2447 
2448 public:
2449   CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2450                        ArrayRef<Value *> Args = {}, const Twine &Name = "",
2451                        MDNode *FPMathTag = nullptr) {
2452     CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2453     if (IsFPConstrained)
2454       setConstrainedFPCallAttr(CI);
2455     if (isa<FPMathOperator>(CI))
2456       setFPAttrs(CI, FPMathTag, FMF);
2457     return Insert(CI, Name);
2458   }
2459 
2460   CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2461                        ArrayRef<OperandBundleDef> OpBundles,
2462                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2463     CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2464     if (IsFPConstrained)
2465       setConstrainedFPCallAttr(CI);
2466     if (isa<FPMathOperator>(CI))
2467       setFPAttrs(CI, FPMathTag, FMF);
2468     return Insert(CI, Name);
2469   }
2470 
2471   CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = {},
2472                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2473     return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2474                       FPMathTag);
2475   }
2476 
2477   CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2478                        ArrayRef<OperandBundleDef> OpBundles,
2479                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2480     return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2481                       OpBundles, Name, FPMathTag);
2482   }
2483 
2484   CallInst *CreateConstrainedFPCall(
2485       Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "",
2486       std::optional<RoundingMode> Rounding = std::nullopt,
2487       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2488 
2489   Value *CreateSelect(Value *C, Value *True, Value *False,
2490                       const Twine &Name = "", Instruction *MDFrom = nullptr);
2491   Value *CreateSelectFMF(Value *C, Value *True, Value *False,
2492                          FMFSource FMFSource, const Twine &Name = "",
2493                          Instruction *MDFrom = nullptr);
2494 
2495   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2496     return Insert(new VAArgInst(List, Ty), Name);
2497   }
2498 
2499   Value *CreateExtractElement(Value *Vec, Value *Idx,
2500                               const Twine &Name = "") {
2501     if (Value *V = Folder.FoldExtractElement(Vec, Idx))
2502       return V;
2503     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2504   }
2505 
2506   Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2507                               const Twine &Name = "") {
2508     return CreateExtractElement(Vec, getInt64(Idx), Name);
2509   }
2510 
2511   Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx,
2512                              const Twine &Name = "") {
2513     return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2514   }
2515 
2516   Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx,
2517                              const Twine &Name = "") {
2518     return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2519   }
2520 
2521   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2522                              const Twine &Name = "") {
2523     if (Value *V = Folder.FoldInsertElement(Vec, NewElt, Idx))
2524       return V;
2525     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2526   }
2527 
2528   Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2529                              const Twine &Name = "") {
2530     return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2531   }
2532 
2533   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2534                              const Twine &Name = "") {
2535     SmallVector<int, 16> IntMask;
2536     ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask);
2537     return CreateShuffleVector(V1, V2, IntMask, Name);
2538   }
2539 
2540   /// See class ShuffleVectorInst for a description of the mask representation.
2541   Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask,
2542                              const Twine &Name = "") {
2543     if (Value *V = Folder.FoldShuffleVector(V1, V2, Mask))
2544       return V;
2545     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2546   }
2547 
2548   /// Create a unary shuffle. The second vector operand of the IR instruction
2549   /// is poison.
2550   Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask,
2551                              const Twine &Name = "") {
2552     return CreateShuffleVector(V, PoisonValue::get(V->getType()), Mask, Name);
2553   }
2554 
2555   Value *CreateExtractValue(Value *Agg, ArrayRef<unsigned> Idxs,
2556                             const Twine &Name = "") {
2557     if (auto *V = Folder.FoldExtractValue(Agg, Idxs))
2558       return V;
2559     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2560   }
2561 
2562   Value *CreateInsertValue(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2563                            const Twine &Name = "") {
2564     if (auto *V = Folder.FoldInsertValue(Agg, Val, Idxs))
2565       return V;
2566     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2567   }
2568 
2569   LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2570                                    const Twine &Name = "") {
2571     return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2572   }
2573 
2574   Value *CreateFreeze(Value *V, const Twine &Name = "") {
2575     return Insert(new FreezeInst(V), Name);
2576   }
2577 
2578   //===--------------------------------------------------------------------===//
2579   // Utility creation methods
2580   //===--------------------------------------------------------------------===//
2581 
2582   /// Return a boolean value testing if \p Arg == 0.
2583   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2584     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), Name);
2585   }
2586 
2587   /// Return a boolean value testing if \p Arg != 0.
2588   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2589     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), Name);
2590   }
2591 
2592   /// Return a boolean value testing if \p Arg < 0.
2593   Value *CreateIsNeg(Value *Arg, const Twine &Name = "") {
2594     return CreateICmpSLT(Arg, ConstantInt::getNullValue(Arg->getType()), Name);
2595   }
2596 
2597   /// Return a boolean value testing if \p Arg > -1.
2598   Value *CreateIsNotNeg(Value *Arg, const Twine &Name = "") {
2599     return CreateICmpSGT(Arg, ConstantInt::getAllOnesValue(Arg->getType()),
2600                          Name);
2601   }
2602 
2603   /// Return the i64 difference between two pointer values, dividing out
2604   /// the size of the pointed-to objects.
2605   ///
2606   /// This is intended to implement C-style pointer subtraction. As such, the
2607   /// pointers must be appropriately aligned for their element types and
2608   /// pointing into the same object.
2609   Value *CreatePtrDiff(Type *ElemTy, Value *LHS, Value *RHS,
2610                        const Twine &Name = "");
2611 
2612   /// Create a launder.invariant.group intrinsic call. If Ptr type is
2613   /// different from pointer to i8, it's casted to pointer to i8 in the same
2614   /// address space before call and casted back to Ptr type after call.
2615   Value *CreateLaunderInvariantGroup(Value *Ptr);
2616 
2617   /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2618   /// different from pointer to i8, it's casted to pointer to i8 in the same
2619   /// address space before call and casted back to Ptr type after call.
2620   Value *CreateStripInvariantGroup(Value *Ptr);
2621 
2622   /// Return a vector value that contains the vector V reversed
2623   Value *CreateVectorReverse(Value *V, const Twine &Name = "");
2624 
2625   /// Return a vector splice intrinsic if using scalable vectors, otherwise
2626   /// return a shufflevector. If the immediate is positive, a vector is
2627   /// extracted from concat(V1, V2), starting at Imm. If the immediate
2628   /// is negative, we extract -Imm elements from V1 and the remaining
2629   /// elements from V2. Imm is a signed integer in the range
2630   /// -VL <= Imm < VL (where VL is the runtime vector length of the
2631   /// source/result vector)
2632   Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm,
2633                             const Twine &Name = "");
2634 
2635   /// Return a vector value that contains \arg V broadcasted to \p
2636   /// NumElts elements.
2637   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
2638 
2639   /// Return a vector value that contains \arg V broadcasted to \p
2640   /// EC elements.
2641   Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
2642 
2643   Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base,
2644                                         unsigned Dimension, unsigned LastIndex,
2645                                         MDNode *DbgInfo);
2646 
2647   Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2648                                         MDNode *DbgInfo);
2649 
2650   Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base,
2651                                          unsigned Index, unsigned FieldIndex,
2652                                          MDNode *DbgInfo);
2653 
2654   Value *createIsFPClass(Value *FPNum, unsigned Test);
2655 
2656 private:
2657   /// Helper function that creates an assume intrinsic call that
2658   /// represents an alignment assumption on the provided pointer \p PtrValue
2659   /// with offset \p OffsetValue and alignment value \p AlignValue.
2660   CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2661                                             Value *PtrValue, Value *AlignValue,
2662                                             Value *OffsetValue);
2663 
2664 public:
2665   /// Create an assume intrinsic call that represents an alignment
2666   /// assumption on the provided pointer.
2667   ///
2668   /// An optional offset can be provided, and if it is provided, the offset
2669   /// must be subtracted from the provided pointer to get the pointer with the
2670   /// specified alignment.
2671   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2672                                       unsigned Alignment,
2673                                       Value *OffsetValue = nullptr);
2674 
2675   /// Create an assume intrinsic call that represents an alignment
2676   /// assumption on the provided pointer.
2677   ///
2678   /// An optional offset can be provided, and if it is provided, the offset
2679   /// must be subtracted from the provided pointer to get the pointer with the
2680   /// specified alignment.
2681   ///
2682   /// This overload handles the condition where the Alignment is dependent
2683   /// on an existing value rather than a static value.
2684   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2685                                       Value *Alignment,
2686                                       Value *OffsetValue = nullptr);
2687 };
2688 
2689 /// This provides a uniform API for creating instructions and inserting
2690 /// them into a basic block: either at the end of a BasicBlock, or at a specific
2691 /// iterator location in a block.
2692 ///
2693 /// Note that the builder does not expose the full generality of LLVM
2694 /// instructions.  For access to extra instruction properties, use the mutators
2695 /// (e.g. setVolatile) on the instructions after they have been
2696 /// created. Convenience state exists to specify fast-math flags and fp-math
2697 /// tags.
2698 ///
2699 /// The first template argument specifies a class to use for creating constants.
2700 /// This defaults to creating minimally folded constants.  The second template
2701 /// argument allows clients to specify custom insertion hooks that are called on
2702 /// every newly created insertion.
2703 template <typename FolderTy = ConstantFolder,
2704           typename InserterTy = IRBuilderDefaultInserter>
2705 class IRBuilder : public IRBuilderBase {
2706 private:
2707   FolderTy Folder;
2708   InserterTy Inserter;
2709 
2710 public:
2711   IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(),
2712             MDNode *FPMathTag = nullptr,
2713             ArrayRef<OperandBundleDef> OpBundles = {})
2714       : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles),
2715         Folder(Folder), Inserter(Inserter) {}
2716 
2717   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
2718                      ArrayRef<OperandBundleDef> OpBundles = {})
2719       : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
2720 
2721   explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
2722                      MDNode *FPMathTag = nullptr,
2723                      ArrayRef<OperandBundleDef> OpBundles = {})
2724       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2725                       FPMathTag, OpBundles),
2726         Folder(Folder) {
2727     SetInsertPoint(TheBB);
2728   }
2729 
2730   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
2731                      ArrayRef<OperandBundleDef> OpBundles = {})
2732       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2733                       FPMathTag, OpBundles) {
2734     SetInsertPoint(TheBB);
2735   }
2736 
2737   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
2738                      ArrayRef<OperandBundleDef> OpBundles = {})
2739       : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, FPMathTag,
2740                       OpBundles) {
2741     SetInsertPoint(IP);
2742   }
2743 
2744   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
2745             MDNode *FPMathTag = nullptr,
2746             ArrayRef<OperandBundleDef> OpBundles = {})
2747       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2748                       FPMathTag, OpBundles),
2749         Folder(Folder) {
2750     SetInsertPoint(TheBB, IP);
2751   }
2752 
2753   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
2754             MDNode *FPMathTag = nullptr,
2755             ArrayRef<OperandBundleDef> OpBundles = {})
2756       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2757                       FPMathTag, OpBundles) {
2758     SetInsertPoint(TheBB, IP);
2759   }
2760 
2761   /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard
2762   /// or FastMathFlagGuard instead.
2763   IRBuilder(const IRBuilder &) = delete;
2764 
2765   InserterTy &getInserter() { return Inserter; }
2766   const InserterTy &getInserter() const { return Inserter; }
2767 };
2768 
2769 template <typename FolderTy, typename InserterTy>
2770 IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *,
2771           ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy, InserterTy>;
2772 IRBuilder(LLVMContext &, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2773 template <typename FolderTy>
2774 IRBuilder(BasicBlock *, FolderTy, MDNode *, ArrayRef<OperandBundleDef>)
2775     -> IRBuilder<FolderTy>;
2776 IRBuilder(BasicBlock *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2777 IRBuilder(Instruction *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2778 template <typename FolderTy>
2779 IRBuilder(BasicBlock *, BasicBlock::iterator, FolderTy, MDNode *,
2780           ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy>;
2781 IRBuilder(BasicBlock *, BasicBlock::iterator, MDNode *,
2782           ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2783 
2784 
2785 // Create wrappers for C Binding types (see CBindingWrapping.h).
2786 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2787 
2788 } // end namespace llvm
2789 
2790 #endif // LLVM_IR_IRBUILDER_H