LCOV - code coverage report
Current view: top level - include/llvm/IR - IRBuilder.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 559 572 97.7 %
Date: 2018-02-17 17:14:17 Functions: 662 826 80.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- C++ -*-===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This file defines the IRBuilder class, which is used as a convenient way
      11             : // to create LLVM instructions with a consistent and simplified interface.
      12             : //
      13             : //===----------------------------------------------------------------------===//
      14             : 
      15             : #ifndef LLVM_IR_IRBUILDER_H
      16             : #define LLVM_IR_IRBUILDER_H
      17             : 
      18             : #include "llvm-c/Types.h"
      19             : #include "llvm/ADT/ArrayRef.h"
      20             : #include "llvm/ADT/None.h"
      21             : #include "llvm/ADT/StringRef.h"
      22             : #include "llvm/ADT/Twine.h"
      23             : #include "llvm/IR/BasicBlock.h"
      24             : #include "llvm/IR/Constant.h"
      25             : #include "llvm/IR/ConstantFolder.h"
      26             : #include "llvm/IR/Constants.h"
      27             : #include "llvm/IR/DataLayout.h"
      28             : #include "llvm/IR/DebugLoc.h"
      29             : #include "llvm/IR/DerivedTypes.h"
      30             : #include "llvm/IR/Function.h"
      31             : #include "llvm/IR/GlobalVariable.h"
      32             : #include "llvm/IR/InstrTypes.h"
      33             : #include "llvm/IR/Instruction.h"
      34             : #include "llvm/IR/Instructions.h"
      35             : #include "llvm/IR/Intrinsics.h"
      36             : #include "llvm/IR/LLVMContext.h"
      37             : #include "llvm/IR/Module.h"
      38             : #include "llvm/IR/Operator.h"
      39             : #include "llvm/IR/Type.h"
      40             : #include "llvm/IR/Value.h"
      41             : #include "llvm/IR/ValueHandle.h"
      42             : #include "llvm/Support/AtomicOrdering.h"
      43             : #include "llvm/Support/CBindingWrapping.h"
      44             : #include "llvm/Support/Casting.h"
      45             : #include <algorithm>
      46             : #include <cassert>
      47             : #include <cstddef>
      48             : #include <cstdint>
      49             : #include <functional>
      50             : 
      51             : namespace llvm {
      52             : 
      53             : class APInt;
      54             : class MDNode;
      55             : class Module;
      56             : class Use;
      57             : 
      58             : /// \brief This provides the default implementation of the IRBuilder
      59             : /// 'InsertHelper' method that is called whenever an instruction is created by
      60             : /// IRBuilder and needs to be inserted.
      61             : ///
      62             : /// By default, this inserts the instruction at the insertion point.
      63             : class IRBuilderDefaultInserter {
      64             : protected:
      65     4423681 :   void InsertHelper(Instruction *I, const Twine &Name,
      66             :                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
      67     4423681 :     if (BB) BB->getInstList().insert(InsertPt, I);
      68     4423681 :     I->setName(Name);
      69     4423681 :   }
      70             : };
      71             : 
      72             : /// Provides an 'InsertHelper' that calls a user-provided callback after
      73             : /// performing the default insertion.
      74      375242 : class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
      75             :   std::function<void(Instruction *)> Callback;
      76             : 
      77             : public:
      78             :   IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
      79             :       : Callback(std::move(Callback)) {}
      80             : 
      81             : protected:
      82      191631 :   void InsertHelper(Instruction *I, const Twine &Name,
      83             :                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
      84      191631 :     IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
      85             :     Callback(I);
      86      191631 :   }
      87             : };
      88             : 
      89             : /// \brief Common base class shared among various IRBuilders.
      90      335194 : class IRBuilderBase {
      91             :   DebugLoc CurDbgLocation;
      92             : 
      93             : protected:
      94             :   BasicBlock *BB;
      95             :   BasicBlock::iterator InsertPt;
      96             :   LLVMContext &Context;
      97             : 
      98             :   MDNode *DefaultFPMathTag;
      99             :   FastMathFlags FMF;
     100             : 
     101             :   ArrayRef<OperandBundleDef> DefaultOperandBundles;
     102             : 
     103             : public:
     104             :   IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
     105             :                 ArrayRef<OperandBundleDef> OpBundles = None)
     106    12293621 :       : Context(context), DefaultFPMathTag(FPMathTag),
     107    24772142 :         DefaultOperandBundles(OpBundles) {
     108             :     ClearInsertionPoint();
     109             :   }
     110             : 
     111             :   //===--------------------------------------------------------------------===//
     112             :   // Builder configuration methods
     113             :   //===--------------------------------------------------------------------===//
     114             : 
     115             :   /// \brief Clear the insertion point: created instructions will not be
     116             :   /// inserted into a block.
     117             :   void ClearInsertionPoint() {
     118    11338754 :     BB = nullptr;
     119    11169717 :     InsertPt = BasicBlock::iterator();
     120             :   }
     121             : 
     122             :   BasicBlock *GetInsertBlock() const { return BB; }
     123             :   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
     124             :   LLVMContext &getContext() const { return Context; }
     125             : 
     126             :   /// \brief This specifies that created instructions should be appended to the
     127             :   /// end of the specified block.
     128             :   void SetInsertPoint(BasicBlock *TheBB) {
     129     2318408 :     BB = TheBB;
     130     2318408 :     InsertPt = BB->end();
     131             :   }
     132             : 
     133             :   /// \brief This specifies that created instructions should be inserted before
     134             :   /// the specified instruction.
     135    32217839 :   void SetInsertPoint(Instruction *I) {
     136    32217839 :     BB = I->getParent();
     137    32217839 :     InsertPt = I->getIterator();
     138             :     assert(InsertPt != BB->end() && "Can't read debug loc from end()");
     139    32217839 :     SetCurrentDebugLocation(I->getDebugLoc());
     140    32217839 :   }
     141             : 
     142             :   /// \brief This specifies that created instructions should be inserted at the
     143             :   /// specified point.
     144      593735 :   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
     145      593735 :     BB = TheBB;
     146      593735 :     InsertPt = IP;
     147      593735 :     if (IP != TheBB->end())
     148      523880 :       SetCurrentDebugLocation(IP->getDebugLoc());
     149      593735 :   }
     150             : 
     151             :   /// \brief Set location information used by debugging information.
     152             :   void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
     153             : 
     154             :   /// \brief Get location information used by debugging information.
     155           3 :   const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
     156             : 
     157             :   /// \brief If this builder has a current debug location, set it on the
     158             :   /// specified instruction.
     159     4528799 :   void SetInstDebugLocation(Instruction *I) const {
     160     4528799 :     if (CurDbgLocation)
     161     2178444 :       I->setDebugLoc(CurDbgLocation);
     162     4528799 :   }
     163             : 
     164             :   /// \brief Get the return type of the current function that we're emitting
     165             :   /// into.
     166             :   Type *getCurrentFunctionReturnType() const;
     167             : 
     168             :   /// InsertPoint - A saved insertion point.
     169             :   class InsertPoint {
     170             :     BasicBlock *Block = nullptr;
     171             :     BasicBlock::iterator Point;
     172             : 
     173             :   public:
     174             :     /// \brief Creates a new insertion point which doesn't point to anything.
     175             :     InsertPoint() = default;
     176             : 
     177             :     /// \brief Creates a new insertion point at the given location.
     178             :     InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
     179             :       : Block(InsertBlock), Point(InsertPoint) {}
     180             : 
     181             :     /// \brief Returns true if this insert point is set.
     182             :     bool isSet() const { return (Block != nullptr); }
     183             : 
     184             :     BasicBlock *getBlock() const { return Block; }
     185             :     BasicBlock::iterator getPoint() const { return Point; }
     186             :   };
     187             : 
     188             :   /// \brief Returns the current insert point.
     189             :   InsertPoint saveIP() const {
     190             :     return InsertPoint(GetInsertBlock(), GetInsertPoint());
     191             :   }
     192             : 
     193             :   /// \brief Returns the current insert point, clearing it in the process.
     194             :   InsertPoint saveAndClearIP() {
     195             :     InsertPoint IP(GetInsertBlock(), GetInsertPoint());
     196             :     ClearInsertionPoint();
     197             :     return IP;
     198             :   }
     199             : 
     200             :   /// \brief Sets the current insert point to a previously-saved location.
     201             :   void restoreIP(InsertPoint IP) {
     202      376729 :     if (IP.isSet())
     203      371868 :       SetInsertPoint(IP.getBlock(), IP.getPoint());
     204             :     else
     205             :       ClearInsertionPoint();
     206             :   }
     207             : 
     208             :   /// \brief Get the floating point math metadata being used.
     209             :   MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
     210             : 
     211             :   /// \brief Get the flags to be applied to created floating point ops
     212             :   FastMathFlags getFastMathFlags() const { return FMF; }
     213             : 
     214             :   /// \brief Clear the fast-math flags.
     215             :   void clearFastMathFlags() { FMF.clear(); }
     216             : 
     217             :   /// \brief Set the floating point math metadata to be used.
     218           1 :   void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
     219             : 
     220             :   /// \brief Set the fast-math flags to be used with generated fp-math operators
     221      170104 :   void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
     222             : 
     223             :   //===--------------------------------------------------------------------===//
     224             :   // RAII helpers.
     225             :   //===--------------------------------------------------------------------===//
     226             : 
     227             :   // \brief RAII object that stores the current insertion point and restores it
     228             :   // when the object is destroyed. This includes the debug location.
     229             :   class InsertPointGuard {
     230             :     IRBuilderBase &Builder;
     231             :     AssertingVH<BasicBlock> Block;
     232             :     BasicBlock::iterator Point;
     233             :     DebugLoc DbgLoc;
     234             : 
     235             :   public:
     236      217916 :     InsertPointGuard(IRBuilderBase &B)
     237      217916 :         : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
     238      653748 :           DbgLoc(B.getCurrentDebugLocation()) {}
     239             : 
     240             :     InsertPointGuard(const InsertPointGuard &) = delete;
     241             :     InsertPointGuard &operator=(const InsertPointGuard &) = delete;
     242             : 
     243      435832 :     ~InsertPointGuard() {
     244      217916 :       Builder.restoreIP(InsertPoint(Block, Point));
     245      435832 :       Builder.SetCurrentDebugLocation(DbgLoc);
     246      217916 :     }
     247             :   };
     248             : 
     249             :   // \brief RAII object that stores the current fast math settings and restores
     250             :   // them when the object is destroyed.
     251             :   class FastMathFlagGuard {
     252             :     IRBuilderBase &Builder;
     253             :     FastMathFlags FMF;
     254             :     MDNode *FPMathTag;
     255             : 
     256             :   public:
     257             :     FastMathFlagGuard(IRBuilderBase &B)
     258         604 :         : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
     259             : 
     260             :     FastMathFlagGuard(const FastMathFlagGuard &) = delete;
     261             :     FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
     262             : 
     263             :     ~FastMathFlagGuard() {
     264         604 :       Builder.FMF = FMF;
     265         604 :       Builder.DefaultFPMathTag = FPMathTag;
     266             :     }
     267             :   };
     268             : 
     269             :   //===--------------------------------------------------------------------===//
     270             :   // Miscellaneous creation methods.
     271             :   //===--------------------------------------------------------------------===//
     272             : 
     273             :   /// \brief Make a new global variable with initializer type i8*
     274             :   ///
     275             :   /// Make a new global variable with an initializer that has array of i8 type
     276             :   /// filled in with the null terminated string value specified.  The new global
     277             :   /// variable will be marked mergable with any others of the same contents.  If
     278             :   /// Name is specified, it is the name of the global variable created.
     279             :   GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
     280             :                                      unsigned AddressSpace = 0);
     281             : 
     282             :   /// \brief Get a constant value representing either true or false.
     283       39721 :   ConstantInt *getInt1(bool V) {
     284       79442 :     return ConstantInt::get(getInt1Ty(), V);
     285             :   }
     286             : 
     287             :   /// \brief Get the constant value for i1 true.
     288             :   ConstantInt *getTrue() {
     289        1340 :     return ConstantInt::getTrue(Context);
     290             :   }
     291             : 
     292             :   /// \brief Get the constant value for i1 false.
     293             :   ConstantInt *getFalse() {
     294        1596 :     return ConstantInt::getFalse(Context);
     295             :   }
     296             : 
     297             :   /// \brief Get a constant 8-bit value.
     298       23842 :   ConstantInt *getInt8(uint8_t C) {
     299       47684 :     return ConstantInt::get(getInt8Ty(), C);
     300             :   }
     301             : 
     302             :   /// \brief Get a constant 16-bit value.
     303         970 :   ConstantInt *getInt16(uint16_t C) {
     304        1940 :     return ConstantInt::get(getInt16Ty(), C);
     305             :   }
     306             : 
     307             :   /// \brief Get a constant 32-bit value.
     308      372994 :   ConstantInt *getInt32(uint32_t C) {
     309      745988 :     return ConstantInt::get(getInt32Ty(), C);
     310             :   }
     311             : 
     312             :   /// \brief Get a constant 64-bit value.
     313      168859 :   ConstantInt *getInt64(uint64_t C) {
     314      168859 :     return ConstantInt::get(getInt64Ty(), C);
     315             :   }
     316             : 
     317             :   /// \brief Get a constant N-bit value, zero extended or truncated from
     318             :   /// a 64-bit value.
     319       10926 :   ConstantInt *getIntN(unsigned N, uint64_t C) {
     320       10926 :     return ConstantInt::get(getIntNTy(N), C);
     321             :   }
     322             : 
     323             :   /// \brief Get a constant integer value.
     324             :   ConstantInt *getInt(const APInt &AI) {
     325      302349 :     return ConstantInt::get(Context, AI);
     326             :   }
     327             : 
     328             :   //===--------------------------------------------------------------------===//
     329             :   // Type creation methods
     330             :   //===--------------------------------------------------------------------===//
     331             : 
     332             :   /// \brief Fetch the type representing a single bit
     333             :   IntegerType *getInt1Ty() {
     334       47606 :     return Type::getInt1Ty(Context);
     335             :   }
     336             : 
     337             :   /// \brief Fetch the type representing an 8-bit integer.
     338             :   IntegerType *getInt8Ty() {
     339       42910 :     return Type::getInt8Ty(Context);
     340             :   }
     341             : 
     342             :   /// \brief Fetch the type representing a 16-bit integer.
     343             :   IntegerType *getInt16Ty() {
     344        1039 :     return Type::getInt16Ty(Context);
     345             :   }
     346             : 
     347             :   /// \brief Fetch the type representing a 32-bit integer.
     348             :   IntegerType *getInt32Ty() {
     349      388901 :     return Type::getInt32Ty(Context);
     350             :   }
     351             : 
     352             :   /// \brief Fetch the type representing a 64-bit integer.
     353             :   IntegerType *getInt64Ty() {
     354      169506 :     return Type::getInt64Ty(Context);
     355             :   }
     356             : 
     357             :   /// \brief Fetch the type representing a 128-bit integer.
     358           1 :   IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
     359             : 
     360             :   /// \brief Fetch the type representing an N-bit integer.
     361             :   IntegerType *getIntNTy(unsigned N) {
     362       13391 :     return Type::getIntNTy(Context, N);
     363             :   }
     364             : 
     365             :   /// \brief Fetch the type representing a 16-bit floating point value.
     366             :   Type *getHalfTy() {
     367           6 :     return Type::getHalfTy(Context);
     368             :   }
     369             : 
     370             :   /// \brief Fetch the type representing a 32-bit floating point value.
     371             :   Type *getFloatTy() {
     372          12 :     return Type::getFloatTy(Context);
     373             :   }
     374             : 
     375             :   /// \brief Fetch the type representing a 64-bit floating point value.
     376             :   Type *getDoubleTy() {
     377          43 :     return Type::getDoubleTy(Context);
     378             :   }
     379             : 
     380             :   /// \brief Fetch the type representing void.
     381             :   Type *getVoidTy() {
     382       55055 :     return Type::getVoidTy(Context);
     383             :   }
     384             : 
     385             :   /// \brief Fetch the type representing a pointer to an 8-bit integer value.
     386             :   PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
     387      105677 :     return Type::getInt8PtrTy(Context, AddrSpace);
     388             :   }
     389             : 
     390             :   /// \brief Fetch the type representing a pointer to an integer value.
     391             :   IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
     392         765 :     return DL.getIntPtrType(Context, AddrSpace);
     393             :   }
     394             : 
     395             :   //===--------------------------------------------------------------------===//
     396             :   // Intrinsic creation methods
     397             :   //===--------------------------------------------------------------------===//
     398             : 
     399             :   /// \brief Create and insert a memset to the specified pointer and the
     400             :   /// specified value.
     401             :   ///
     402             :   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
     403             :   /// specified, it will be added to the instruction. Likewise with alias.scope
     404             :   /// and noalias tags.
     405          49 :   CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
     406             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     407             :                          MDNode *ScopeTag = nullptr,
     408             :                          MDNode *NoAliasTag = nullptr) {
     409       22459 :     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
     410       22459 :                         TBAATag, ScopeTag, NoAliasTag);
     411             :   }
     412             : 
     413             :   CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
     414             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     415             :                          MDNode *ScopeTag = nullptr,
     416             :                          MDNode *NoAliasTag = nullptr);
     417             : 
     418             :   /// \brief Create and insert a memcpy between the specified pointers.
     419             :   ///
     420             :   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
     421             :   /// specified, it will be added to the instruction. Likewise with alias.scope
     422             :   /// and noalias tags.
     423          23 :   CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
     424             :                          unsigned SrcAlign, uint64_t Size,
     425             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     426             :                          MDNode *TBAAStructTag = nullptr,
     427             :                          MDNode *ScopeTag = nullptr,
     428             :                          MDNode *NoAliasTag = nullptr) {
     429         242 :     return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
     430             :                         isVolatile, TBAATag, TBAAStructTag, ScopeTag,
     431         242 :                         NoAliasTag);
     432             :   }
     433             : 
     434             :   CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
     435             :                          unsigned SrcAlign, Value *Size,
     436             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     437             :                          MDNode *TBAAStructTag = nullptr,
     438             :                          MDNode *ScopeTag = nullptr,
     439             :                          MDNode *NoAliasTag = nullptr);
     440             : 
     441             :   // TODO: Old API. Remove this when no longer used.
     442           4 :   CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
     443             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     444             :                          MDNode *TBAAStructTag = nullptr,
     445             :                          MDNode *ScopeTag = nullptr,
     446             :                          MDNode *NoAliasTag = nullptr) {
     447           4 :     return CreateMemCpy(Dst, Align, Src, Align, getInt64(Size), isVolatile, TBAATag,
     448           4 :                         TBAAStructTag, ScopeTag, NoAliasTag);
     449             :   }
     450             :   // TODO: Old API. Remove this when no longer used.
     451             :   CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
     452             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     453             :                          MDNode *TBAAStructTag = nullptr,
     454             :                          MDNode *ScopeTag = nullptr,
     455             :                          MDNode *NoAliasTag = nullptr) {
     456             :     return CreateMemCpy(Dst, Align, Src, Align, Size, isVolatile, TBAATag,
     457         172 :                         TBAAStructTag, ScopeTag, NoAliasTag);
     458             :   }
     459             : 
     460             :   /// \brief Create and insert an element unordered-atomic memcpy between the
     461             :   /// specified pointers.
     462             :   ///
     463             :   /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
     464             :   ///
     465             :   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
     466             :   /// specified, it will be added to the instruction. Likewise with alias.scope
     467             :   /// and noalias tags.
     468             :   CallInst *CreateElementUnorderedAtomicMemCpy(
     469             :       Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
     470             :       uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
     471             :       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
     472             :       MDNode *NoAliasTag = nullptr) {
     473             :     return CreateElementUnorderedAtomicMemCpy(
     474             :         Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
     475             :         TBAAStructTag, ScopeTag, NoAliasTag);
     476             :   }
     477             : 
     478             :   CallInst *CreateElementUnorderedAtomicMemCpy(
     479             :       Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
     480             :       uint32_t ElementSize, MDNode *TBAATag = nullptr,
     481             :       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
     482             :       MDNode *NoAliasTag = nullptr);
     483             : 
     484             :   /// \brief Create and insert a memmove between the specified
     485             :   /// pointers.
     486             :   ///
     487             :   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
     488             :   /// specified, it will be added to the instruction. Likewise with alias.scope
     489             :   /// and noalias tags.
     490             :   CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
     491             :                           uint64_t Size, bool isVolatile = false,
     492             :                           MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
     493             :                           MDNode *NoAliasTag = nullptr) {
     494             :     return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), isVolatile,
     495             :                          TBAATag, ScopeTag, NoAliasTag);
     496             :   }
     497             : 
     498             :   CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
     499             :                           Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,
     500             :                           MDNode *ScopeTag = nullptr,
     501             :                           MDNode *NoAliasTag = nullptr);
     502             : 
     503             :   // TODO: Old API. Remove this when no longer used.
     504           3 :   CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
     505             :                           bool isVolatile = false, MDNode *TBAATag = nullptr,
     506             :                           MDNode *ScopeTag = nullptr,
     507             :                           MDNode *NoAliasTag = nullptr) {
     508           3 :     return CreateMemMove(Dst, Align, Src, Align, getInt64(Size), isVolatile,
     509           3 :                          TBAATag, ScopeTag, NoAliasTag);
     510             :   }
     511             :   // TODO: Old API. Remove this when no longer used.
     512             :   CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
     513             :                           bool isVolatile = false, MDNode *TBAATag = nullptr,
     514             :                           MDNode *ScopeTag = nullptr,
     515             :                           MDNode *NoAliasTag = nullptr) {
     516             :     return CreateMemMove(Dst, Align, Src, Align, Size, isVolatile, TBAATag,
     517           2 :                          ScopeTag, NoAliasTag);
     518             :   }
     519             : 
     520             :   /// \brief Create a vector fadd reduction intrinsic of the source vector.
     521             :   /// The first parameter is a scalar accumulator value for ordered reductions.
     522             :   CallInst *CreateFAddReduce(Value *Acc, Value *Src);
     523             : 
     524             :   /// \brief Create a vector fmul reduction intrinsic of the source vector.
     525             :   /// The first parameter is a scalar accumulator value for ordered reductions.
     526             :   CallInst *CreateFMulReduce(Value *Acc, Value *Src);
     527             : 
     528             :   /// \brief Create a vector int add reduction intrinsic of the source vector.
     529             :   CallInst *CreateAddReduce(Value *Src);
     530             : 
     531             :   /// \brief Create a vector int mul reduction intrinsic of the source vector.
     532             :   CallInst *CreateMulReduce(Value *Src);
     533             : 
     534             :   /// \brief Create a vector int AND reduction intrinsic of the source vector.
     535             :   CallInst *CreateAndReduce(Value *Src);
     536             : 
     537             :   /// \brief Create a vector int OR reduction intrinsic of the source vector.
     538             :   CallInst *CreateOrReduce(Value *Src);
     539             : 
     540             :   /// \brief Create a vector int XOR reduction intrinsic of the source vector.
     541             :   CallInst *CreateXorReduce(Value *Src);
     542             : 
     543             :   /// \brief Create a vector integer max reduction intrinsic of the source
     544             :   /// vector.
     545             :   CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
     546             : 
     547             :   /// \brief Create a vector integer min reduction intrinsic of the source
     548             :   /// vector.
     549             :   CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
     550             : 
     551             :   /// \brief Create a vector float max reduction intrinsic of the source
     552             :   /// vector.
     553             :   CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
     554             : 
     555             :   /// \brief Create a vector float min reduction intrinsic of the source
     556             :   /// vector.
     557             :   CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
     558             : 
     559             :   /// \brief Create a lifetime.start intrinsic.
     560             :   ///
     561             :   /// If the pointer isn't i8* it will be converted.
     562             :   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
     563             : 
     564             :   /// \brief Create a lifetime.end intrinsic.
     565             :   ///
     566             :   /// If the pointer isn't i8* it will be converted.
     567             :   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
     568             : 
     569             :   /// Create a call to invariant.start intrinsic.
     570             :   ///
     571             :   /// If the pointer isn't i8* it will be converted.
     572             :   CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
     573             : 
     574             :   /// \brief Create a call to Masked Load intrinsic
     575             :   CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
     576             :                              Value *PassThru = nullptr, const Twine &Name = "");
     577             : 
     578             :   /// \brief Create a call to Masked Store intrinsic
     579             :   CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
     580             :                               Value *Mask);
     581             : 
     582             :   /// \brief Create a call to Masked Gather intrinsic
     583             :   CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
     584             :                                Value *Mask = nullptr,
     585             :                                Value *PassThru = nullptr,
     586             :                                const Twine& Name = "");
     587             : 
     588             :   /// \brief Create a call to Masked Scatter intrinsic
     589             :   CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
     590             :                                 Value *Mask = nullptr);
     591             : 
     592             :   /// \brief Create an assume intrinsic call that allows the optimizer to
     593             :   /// assume that the provided condition will be true.
     594             :   CallInst *CreateAssumption(Value *Cond);
     595             : 
     596             :   /// \brief Create a call to the experimental.gc.statepoint intrinsic to
     597             :   /// start a new statepoint sequence.
     598             :   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
     599             :                                    Value *ActualCallee,
     600             :                                    ArrayRef<Value *> CallArgs,
     601             :                                    ArrayRef<Value *> DeoptArgs,
     602             :                                    ArrayRef<Value *> GCArgs,
     603             :                                    const Twine &Name = "");
     604             : 
     605             :   /// \brief Create a call to the experimental.gc.statepoint intrinsic to
     606             :   /// start a new statepoint sequence.
     607             :   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
     608             :                                    Value *ActualCallee, uint32_t Flags,
     609             :                                    ArrayRef<Use> CallArgs,
     610             :                                    ArrayRef<Use> TransitionArgs,
     611             :                                    ArrayRef<Use> DeoptArgs,
     612             :                                    ArrayRef<Value *> GCArgs,
     613             :                                    const Twine &Name = "");
     614             : 
     615             :   // \brief Conveninence function for the common case when CallArgs are filled
     616             :   // in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
     617             :   // .get()'ed to get the Value pointer.
     618             :   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
     619             :                                    Value *ActualCallee, ArrayRef<Use> CallArgs,
     620             :                                    ArrayRef<Value *> DeoptArgs,
     621             :                                    ArrayRef<Value *> GCArgs,
     622             :                                    const Twine &Name = "");
     623             : 
     624             :   /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
     625             :   /// start a new statepoint sequence.
     626             :   InvokeInst *
     627             :   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
     628             :                            Value *ActualInvokee, BasicBlock *NormalDest,
     629             :                            BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
     630             :                            ArrayRef<Value *> DeoptArgs,
     631             :                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
     632             : 
     633             :   /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
     634             :   /// start a new statepoint sequence.
     635             :   InvokeInst *CreateGCStatepointInvoke(
     636             :       uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
     637             :       BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
     638             :       ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
     639             :       ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
     640             :       const Twine &Name = "");
     641             : 
     642             :   // Conveninence function for the common case when CallArgs are filled in using
     643             :   // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
     644             :   // get the Value *.
     645             :   InvokeInst *
     646             :   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
     647             :                            Value *ActualInvokee, BasicBlock *NormalDest,
     648             :                            BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
     649             :                            ArrayRef<Value *> DeoptArgs,
     650             :                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
     651             : 
     652             :   /// \brief Create a call to the experimental.gc.result intrinsic to extract
     653             :   /// the result from a call wrapped in a statepoint.
     654             :   CallInst *CreateGCResult(Instruction *Statepoint,
     655             :                            Type *ResultType,
     656             :                            const Twine &Name = "");
     657             : 
     658             :   /// \brief Create a call to the experimental.gc.relocate intrinsics to
     659             :   /// project the relocated value of one pointer from the statepoint.
     660             :   CallInst *CreateGCRelocate(Instruction *Statepoint,
     661             :                              int BaseOffset,
     662             :                              int DerivedOffset,
     663             :                              Type *ResultType,
     664             :                              const Twine &Name = "");
     665             : 
     666             :   /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
     667             :   /// first type.
     668             :   CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID,
     669             :                                   Value *LHS, Value *RHS,
     670             :                                   const Twine &Name = "");
     671             : 
     672             :   /// Create call to the minnum intrinsic.
     673             :   CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
     674          12 :     return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, Name);
     675             :   }
     676             : 
     677             :   /// Create call to the maxnum intrinsic.
     678             :   CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
     679             :     return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, Name);
     680             :   }
     681             : 
     682             : private:
     683             :   /// \brief Create a call to a masked intrinsic with given Id.
     684             :   CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
     685             :                                   ArrayRef<Type *> OverloadedTypes,
     686             :                                   const Twine &Name = "");
     687             : 
     688             :   Value *getCastedInt8PtrValue(Value *Ptr);
     689             : };
     690             : 
     691             : /// \brief This provides a uniform API for creating instructions and inserting
     692             : /// them into a basic block: either at the end of a BasicBlock, or at a specific
     693             : /// iterator location in a block.
     694             : ///
     695             : /// Note that the builder does not expose the full generality of LLVM
     696             : /// instructions.  For access to extra instruction properties, use the mutators
     697             : /// (e.g. setVolatile) on the instructions after they have been
     698             : /// created. Convenience state exists to specify fast-math flags and fp-math
     699             : /// tags.
     700             : ///
     701             : /// The first template argument specifies a class to use for creating constants.
     702             : /// This defaults to creating minimally folded constants.  The second template
     703             : /// argument allows clients to specify custom insertion hooks that are called on
     704             : /// every newly created insertion.
     705             : template <typename T = ConstantFolder,
     706             :           typename Inserter = IRBuilderDefaultInserter>
     707     6022929 : class IRBuilder : public IRBuilderBase, public Inserter {
     708             :   T Folder;
     709             : 
     710             : public:
     711      481684 :   IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
     712             :             MDNode *FPMathTag = nullptr,
     713             :             ArrayRef<OperandBundleDef> OpBundles = None)
     714             :       : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
     715      397877 :         Folder(F) {}
     716             : 
     717       37711 :   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
     718             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     719             :       : IRBuilderBase(C, FPMathTag, OpBundles), Folder() {}
     720             : 
     721             :   explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
     722             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     723             :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
     724             :     SetInsertPoint(TheBB);
     725             :   }
     726             : 
     727     1662772 :   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
     728             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     729     1662772 :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() {
     730             :     SetInsertPoint(TheBB);
     731             :   }
     732             : 
     733    10109657 :   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
     734             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     735    10109657 :       : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles), Folder() {
     736    10109657 :     SetInsertPoint(IP);
     737    10109657 :   }
     738             : 
     739          76 :   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
     740             :             MDNode *FPMathTag = nullptr,
     741             :             ArrayRef<OperandBundleDef> OpBundles = None)
     742         152 :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
     743          76 :     SetInsertPoint(TheBB, IP);
     744          76 :   }
     745             : 
     746        1721 :   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
     747             :             MDNode *FPMathTag = nullptr,
     748             :             ArrayRef<OperandBundleDef> OpBundles = None)
     749        1721 :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() {
     750        1721 :     SetInsertPoint(TheBB, IP);
     751        1721 :   }
     752             : 
     753             :   /// \brief Get the constant folder being used.
     754             :   const T &getFolder() { return Folder; }
     755             : 
     756             :   /// \brief Insert and return the specified instruction.
     757             :   template<typename InstTy>
     758     3473082 :   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
     759     4423681 :     this->InsertHelper(I, Name, BB, InsertPt);
     760     4423681 :     this->SetInstDebugLocation(I);
     761     3473082 :     return I;
     762             :   }
     763             : 
     764             :   /// \brief No-op overload to handle constants.
     765             :   Constant *Insert(Constant *C, const Twine& = "") const {
     766             :     return C;
     767             :   }
     768             : 
     769             :   //===--------------------------------------------------------------------===//
     770             :   // Instruction creation methods: Terminators
     771             :   //===--------------------------------------------------------------------===//
     772             : 
     773             : private:
     774             :   /// \brief Helper to add branch weight and unpredictable metadata onto an
     775             :   /// instruction.
     776             :   /// \returns The annotated instruction.
     777             :   template <typename InstTy>
     778       71585 :   InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
     779       71585 :     if (Weights)
     780         950 :       I->setMetadata(LLVMContext::MD_prof, Weights);
     781       71585 :     if (Unpredictable)
     782           2 :       I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
     783       71585 :     return I;
     784             :   }
     785             : 
     786             : public:
     787             :   /// \brief Create a 'ret void' instruction.
     788       90420 :   ReturnInst *CreateRetVoid() {
     789      180840 :     return Insert(ReturnInst::Create(Context));
     790             :   }
     791             : 
     792             :   /// \brief Create a 'ret <val>' instruction.
     793       79016 :   ReturnInst *CreateRet(Value *V) {
     794       79413 :     return Insert(ReturnInst::Create(Context, V));
     795             :   }
     796             : 
     797             :   /// \brief Create a sequence of N insertvalue instructions,
     798             :   /// with one Value from the retVals array each, that build a aggregate
     799             :   /// return value one value at a time, and a ret instruction to return
     800             :   /// the resulting aggregate value.
     801             :   ///
     802             :   /// This is a convenience function for code that uses aggregate return values
     803             :   /// as a vehicle for having multiple return values.
     804           0 :   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
     805           0 :     Value *V = UndefValue::get(getCurrentFunctionReturnType());
     806           0 :     for (unsigned i = 0; i != N; ++i)
     807           0 :       V = CreateInsertValue(V, retVals[i], i, "mrv");
     808           0 :     return Insert(ReturnInst::Create(Context, V));
     809             :   }
     810             : 
     811             :   /// \brief Create an unconditional 'br label X' instruction.
     812      269886 :   BranchInst *CreateBr(BasicBlock *Dest) {
     813      269886 :     return Insert(BranchInst::Create(Dest));
     814             :   }
     815             : 
     816             :   /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
     817             :   /// instruction.
     818       68693 :   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
     819             :                            MDNode *BranchWeights = nullptr,
     820             :                            MDNode *Unpredictable = nullptr) {
     821      137386 :     return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
     822      133308 :                                     BranchWeights, Unpredictable));
     823             :   }
     824             : 
     825             :   /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
     826             :   /// instruction. Copy branch meta data if available.
     827         124 :   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
     828             :                            Instruction *MDSrc) {
     829             :     BranchInst *Br = BranchInst::Create(True, False, Cond);
     830         124 :     if (MDSrc) {
     831          56 :       unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
     832             :                         LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
     833          56 :       Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
     834             :     }
     835         124 :     return Insert(Br);
     836             :   }
     837             : 
     838             :   /// \brief Create a switch instruction with the specified value, default dest,
     839             :   /// and with a hint for the number of cases that will be added (for efficient
     840             :   /// allocation).
     841        1879 :   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
     842             :                            MDNode *BranchWeights = nullptr,
     843             :                            MDNode *Unpredictable = nullptr) {
     844        3758 :     return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
     845        3023 :                                     BranchWeights, Unpredictable));
     846             :   }
     847             : 
     848             :   /// \brief Create an indirect branch instruction with the specified address
     849             :   /// operand, with an optional hint for the number of destinations that will be
     850             :   /// added (for efficient allocation).
     851          31 :   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
     852          31 :     return Insert(IndirectBrInst::Create(Addr, NumDests));
     853             :   }
     854             : 
     855             :   /// \brief Create an invoke instruction.
     856           4 :   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
     857             :                            BasicBlock *UnwindDest,
     858             :                            ArrayRef<Value *> Args = None,
     859             :                            const Twine &Name = "") {
     860           4 :     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
     861           4 :                   Name);
     862             :   }
     863       48114 :   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
     864             :                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
     865             :                            ArrayRef<OperandBundleDef> OpBundles,
     866             :                            const Twine &Name = "") {
     867       48114 :     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
     868       96226 :                                      OpBundles), Name);
     869             :   }
     870             : 
     871        4687 :   ResumeInst *CreateResume(Value *Exn) {
     872        4687 :     return Insert(ResumeInst::Create(Exn));
     873             :   }
     874             : 
     875         114 :   CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
     876             :                                       BasicBlock *UnwindBB = nullptr) {
     877         114 :     return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
     878             :   }
     879             : 
     880          60 :   CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
     881             :                                      unsigned NumHandlers,
     882             :                                      const Twine &Name = "") {
     883          60 :     return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
     884         119 :                   Name);
     885             :   }
     886             : 
     887          63 :   CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
     888             :                                const Twine &Name = "") {
     889          64 :     return Insert(CatchPadInst::Create(ParentPad, Args), Name);
     890             :   }
     891             : 
     892         125 :   CleanupPadInst *CreateCleanupPad(Value *ParentPad,
     893             :                                    ArrayRef<Value *> Args = None,
     894             :                                    const Twine &Name = "") {
     895         125 :     return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
     896             :   }
     897             : 
     898          61 :   CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
     899          61 :     return Insert(CatchReturnInst::Create(CatchPad, BB));
     900             :   }
     901             : 
     902       13553 :   UnreachableInst *CreateUnreachable() {
     903       15820 :     return Insert(new UnreachableInst(Context));
     904             :   }
     905             : 
     906             :   //===--------------------------------------------------------------------===//
     907             :   // Instruction creation methods: Binary Operators
     908             :   //===--------------------------------------------------------------------===//
     909             : private:
     910      187525 :   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
     911             :                                           Value *LHS, Value *RHS,
     912             :                                           const Twine &Name,
     913             :                                           bool HasNUW, bool HasNSW) {
     914      297520 :     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
     915      187525 :     if (HasNUW) BO->setHasNoUnsignedWrap();
     916      187525 :     if (HasNSW) BO->setHasNoSignedWrap();
     917      187525 :     return BO;
     918             :   }
     919             : 
     920       19264 :   Instruction *AddFPMathAttributes(Instruction *I,
     921             :                                    MDNode *FPMathTag,
     922             :                                    FastMathFlags FMF) const {
     923       19264 :     if (!FPMathTag)
     924       19263 :       FPMathTag = DefaultFPMathTag;
     925       19264 :     if (FPMathTag)
     926           9 :       I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
     927       19264 :     I->setFastMathFlags(FMF);
     928       19264 :     return I;
     929             :   }
     930             : 
     931             : public:
     932      178951 :   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
     933             :                    bool HasNUW = false, bool HasNSW = false) {
     934             :     if (Constant *LC = dyn_cast<Constant>(LHS))
     935             :       if (Constant *RC = dyn_cast<Constant>(RHS))
     936       51021 :         return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
     937      153122 :     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
     938      153122 :                                    HasNUW, HasNSW);
     939             :   }
     940             :   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
     941       62280 :     return CreateAdd(LHS, RHS, Name, false, true);
     942             :   }
     943             :   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
     944         109 :     return CreateAdd(LHS, RHS, Name, true, false);
     945             :   }
     946        3871 :   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
     947             :                     MDNode *FPMathTag = nullptr) {
     948             :     if (Constant *LC = dyn_cast<Constant>(LHS))
     949             :       if (Constant *RC = dyn_cast<Constant>(RHS))
     950          31 :         return Insert(Folder.CreateFAdd(LC, RC), Name);
     951        7680 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
     952        3805 :                                       FPMathTag, FMF), Name);
     953             :   }
     954       69570 :   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
     955             :                    bool HasNUW = false, bool HasNSW = false) {
     956             :     if (Constant *LC = dyn_cast<Constant>(LHS))
     957             :       if (Constant *RC = dyn_cast<Constant>(RHS))
     958      108660 :         return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
     959       15220 :     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
     960       15220 :                                    HasNUW, HasNSW);
     961             :   }
     962             :   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
     963       61145 :     return CreateSub(LHS, RHS, Name, false, true);
     964             :   }
     965             :   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
     966          24 :     return CreateSub(LHS, RHS, Name, true, false);
     967             :   }
     968         947 :   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
     969             :                     MDNode *FPMathTag = nullptr) {
     970             :     if (Constant *LC = dyn_cast<Constant>(LHS))
     971             :       if (Constant *RC = dyn_cast<Constant>(RHS))
     972         118 :         return Insert(Folder.CreateFSub(LC, RC), Name);
     973        1658 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
     974         806 :                                       FPMathTag, FMF), Name);
     975             :   }
     976       23399 :   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
     977             :                    bool HasNUW = false, bool HasNSW = false) {
     978             :     if (Constant *LC = dyn_cast<Constant>(LHS))
     979             :       if (Constant *RC = dyn_cast<Constant>(RHS))
     980       13653 :         return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
     981       16509 :     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
     982       16509 :                                    HasNUW, HasNSW);
     983             :   }
     984             :   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
     985       14556 :     return CreateMul(LHS, RHS, Name, false, true);
     986             :   }
     987             :   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
     988        2121 :     return CreateMul(LHS, RHS, Name, true, false);
     989             :   }
     990        1975 :   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
     991             :                     MDNode *FPMathTag = nullptr) {
     992             :     if (Constant *LC = dyn_cast<Constant>(LHS))
     993             :       if (Constant *RC = dyn_cast<Constant>(RHS))
     994          23 :         return Insert(Folder.CreateFMul(LC, RC), Name);
     995        3904 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
     996        1738 :                                       FPMathTag, FMF), Name);
     997             :   }
     998        2062 :   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
     999             :                     bool isExact = false) {
    1000             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1001             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1002        2664 :         return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
    1003         730 :     if (!isExact)
    1004         696 :       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
    1005          35 :     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
    1006             :   }
    1007             :   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
    1008          33 :     return CreateUDiv(LHS, RHS, Name, true);
    1009             :   }
    1010       22581 :   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
    1011             :                     bool isExact = false) {
    1012             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1013             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1014       34812 :         return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
    1015        5175 :     if (!isExact)
    1016        3761 :       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
    1017        1414 :     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
    1018             :   }
    1019             :   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
    1020        1408 :     return CreateSDiv(LHS, RHS, Name, true);
    1021             :   }
    1022         391 :   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
    1023             :                     MDNode *FPMathTag = nullptr) {
    1024             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1025             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1026          10 :         return Insert(Folder.CreateFDiv(LC, RC), Name);
    1027         762 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
    1028         327 :                                       FPMathTag, FMF), Name);
    1029             :   }
    1030        1378 :   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
    1031             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1032             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1033         335 :         return Insert(Folder.CreateURem(LC, RC), Name);
    1034        1043 :     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
    1035             :   }
    1036         433 :   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
    1037             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1038             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1039           3 :         return Insert(Folder.CreateSRem(LC, RC), Name);
    1040         430 :     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
    1041             :   }
    1042          12 :   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
    1043             :                     MDNode *FPMathTag = nullptr) {
    1044             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1045             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1046           0 :         return Insert(Folder.CreateFRem(LC, RC), Name);
    1047          24 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
    1048          12 :                                       FPMathTag, FMF), Name);
    1049             :   }
    1050             : 
    1051        3924 :   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
    1052             :                    bool HasNUW = false, bool HasNSW = false) {
    1053             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1054             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1055        2478 :         return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
    1056        2674 :     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
    1057        2674 :                                    HasNUW, HasNSW);
    1058             :   }
    1059             :   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
    1060             :                    bool HasNUW = false, bool HasNSW = false) {
    1061             :     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
    1062             :                      HasNUW, HasNSW);
    1063             :   }
    1064        1508 :   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
    1065             :                    bool HasNUW = false, bool HasNSW = false) {
    1066        1508 :     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
    1067        1508 :                      HasNUW, HasNSW);
    1068             :   }
    1069             : 
    1070        2032 :   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
    1071             :                     bool isExact = false) {
    1072             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1073             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1074         200 :         return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
    1075        1924 :     if (!isExact)
    1076        1922 :       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
    1077           3 :     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
    1078             :   }
    1079             :   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
    1080             :                     bool isExact = false) {
    1081             :     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1082             :   }
    1083         975 :   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
    1084             :                     bool isExact = false) {
    1085         975 :     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1086             :   }
    1087             : 
    1088        1344 :   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
    1089             :                     bool isExact = false) {
    1090             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1091             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1092        1083 :         return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
    1093         793 :     if (!isExact)
    1094         783 :       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
    1095          10 :     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
    1096             :   }
    1097           7 :   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
    1098             :                     bool isExact = false) {
    1099           7 :     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1100             :   }
    1101         483 :   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
    1102             :                     bool isExact = false) {
    1103         483 :     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1104             :   }
    1105             : 
    1106       11797 :   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
    1107             :     if (Constant *RC = dyn_cast<Constant>(RHS)) {
    1108       16226 :       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
    1109             :         return LHS;  // LHS & -1 -> LHS
    1110             :       if (Constant *LC = dyn_cast<Constant>(LHS))
    1111        1535 :         return Insert(Folder.CreateAnd(LC, RC), Name);
    1112             :     }
    1113        9655 :     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
    1114             :   }
    1115        4269 :   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
    1116        4269 :     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1117             :   }
    1118         225 :   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
    1119         225 :     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1120             :   }
    1121             : 
    1122        6814 :   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
    1123             :     if (Constant *RC = dyn_cast<Constant>(RHS)) {
    1124        2913 :       if (RC->isNullValue())
    1125             :         return LHS;  // LHS | 0 -> LHS
    1126             :       if (Constant *LC = dyn_cast<Constant>(LHS))
    1127         760 :         return Insert(Folder.CreateOr(LC, RC), Name);
    1128             :     }
    1129        5204 :     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
    1130             :   }
    1131          19 :   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
    1132          19 :     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1133             :   }
    1134             :   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
    1135             :     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1136             :   }
    1137             : 
    1138        1742 :   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
    1139             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1140             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1141          20 :         return Insert(Folder.CreateXor(LC, RC), Name);
    1142        1722 :     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
    1143             :   }
    1144         128 :   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
    1145         128 :     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1146             :   }
    1147          11 :   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
    1148          11 :     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1149             :   }
    1150             : 
    1151       82376 :   Value *CreateBinOp(Instruction::BinaryOps Opc,
    1152             :                      Value *LHS, Value *RHS, const Twine &Name = "",
    1153             :                      MDNode *FPMathTag = nullptr) {
    1154             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1155             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1156        1823 :         return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
    1157       80553 :     Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
    1158       80553 :     if (isa<FPMathOperator>(BinOp))
    1159        1146 :       BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
    1160       80553 :     return Insert(BinOp, Name);
    1161             :   }
    1162             : 
    1163         528 :   Value *CreateNeg(Value *V, const Twine &Name = "",
    1164             :                    bool HasNUW = false, bool HasNSW = false) {
    1165             :     if (Constant *VC = dyn_cast<Constant>(V))
    1166         168 :       return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
    1167         651 :     BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
    1168         444 :     if (HasNUW) BO->setHasNoUnsignedWrap();
    1169         444 :     if (HasNSW) BO->setHasNoSignedWrap();
    1170             :     return BO;
    1171             :   }
    1172             :   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
    1173           0 :     return CreateNeg(V, Name, false, true);
    1174             :   }
    1175             :   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
    1176           0 :     return CreateNeg(V, Name, true, false);
    1177             :   }
    1178          49 :   Value *CreateFNeg(Value *V, const Twine &Name = "",
    1179             :                     MDNode *FPMathTag = nullptr) {
    1180             :     if (Constant *VC = dyn_cast<Constant>(V))
    1181          13 :       return Insert(Folder.CreateFNeg(VC), Name);
    1182          72 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
    1183          36 :                                       FPMathTag, FMF), Name);
    1184             :   }
    1185        9379 :   Value *CreateNot(Value *V, const Twine &Name = "") {
    1186             :     if (Constant *VC = dyn_cast<Constant>(V))
    1187         746 :       return Insert(Folder.CreateNot(VC), Name);
    1188        9030 :     return Insert(BinaryOperator::CreateNot(V), Name);
    1189             :   }
    1190             : 
    1191             :   //===--------------------------------------------------------------------===//
    1192             :   // Instruction creation methods: Memory Instructions
    1193             :   //===--------------------------------------------------------------------===//
    1194             : 
    1195             :   AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
    1196             :                            Value *ArraySize = nullptr, const Twine &Name = "") {
    1197             :     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
    1198             :   }
    1199             : 
    1200        2637 :   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
    1201             :                            const Twine &Name = "") {
    1202        2637 :     const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
    1203        3567 :     return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
    1204             :   }
    1205             :   // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
    1206             :   // converting the string to 'bool' for the isVolatile parameter.
    1207      122026 :   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
    1208      122026 :     return Insert(new LoadInst(Ptr), Name);
    1209             :   }
    1210      217266 :   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
    1211      217266 :     return Insert(new LoadInst(Ptr), Name);
    1212             :   }
    1213        1195 :   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
    1214        2390 :     return Insert(new LoadInst(Ty, Ptr), Name);
    1215             :   }
    1216      561949 :   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
    1217     1123898 :     return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
    1218             :   }
    1219     1159882 :   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
    1220     1380585 :     return Insert(new StoreInst(Val, Ptr, isVolatile));
    1221             :   }
    1222             :   // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
    1223             :   // correctly, instead of converting the string to 'bool' for the isVolatile
    1224             :   // parameter.
    1225             :   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
    1226      120778 :     LoadInst *LI = CreateLoad(Ptr, Name);
    1227      120778 :     LI->setAlignment(Align);
    1228             :     return LI;
    1229             :   }
    1230             :   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
    1231             :                               const Twine &Name = "") {
    1232       47033 :     LoadInst *LI = CreateLoad(Ptr, Name);
    1233       47033 :     LI->setAlignment(Align);
    1234             :     return LI;
    1235             :   }
    1236             :   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
    1237             :                               const Twine &Name = "") {
    1238      559432 :     LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
    1239      559432 :     LI->setAlignment(Align);
    1240             :     return LI;
    1241             :   }
    1242             :   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
    1243             :                                 bool isVolatile = false) {
    1244      938159 :     StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
    1245      938159 :     SI->setAlignment(Align);
    1246             :     return SI;
    1247             :   }
    1248         200 :   FenceInst *CreateFence(AtomicOrdering Ordering,
    1249             :                          SyncScope::ID SSID = SyncScope::System,
    1250             :                          const Twine &Name = "") {
    1251         364 :     return Insert(new FenceInst(Context, Ordering, SSID), Name);
    1252             :   }
    1253             :   AtomicCmpXchgInst *
    1254         812 :   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
    1255             :                       AtomicOrdering SuccessOrdering,
    1256             :                       AtomicOrdering FailureOrdering,
    1257             :                       SyncScope::ID SSID = SyncScope::System) {
    1258        1624 :     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
    1259        1192 :                                         FailureOrdering, SSID));
    1260             :   }
    1261         979 :   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
    1262             :                                  AtomicOrdering Ordering,
    1263             :                                  SyncScope::ID SSID = SyncScope::System) {
    1264        1026 :     return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID));
    1265             :   }
    1266             :   Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
    1267             :                    const Twine &Name = "") {
    1268          83 :     return CreateGEP(nullptr, Ptr, IdxList, Name);
    1269             :   }
    1270        9948 :   Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
    1271             :                    const Twine &Name = "") {
    1272             :     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
    1273             :       // Every index must be constant.
    1274             :       size_t i, e;
    1275        2059 :       for (i = 0, e = IdxList.size(); i != e; ++i)
    1276         985 :         if (!isa<Constant>(IdxList[i]))
    1277             :           break;
    1278         475 :       if (i == e)
    1279         282 :         return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
    1280             :     }
    1281       19254 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
    1282             :   }
    1283             :   Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
    1284             :                            const Twine &Name = "") {
    1285       53720 :     return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
    1286             :   }
    1287      254167 :   Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
    1288             :                            const Twine &Name = "") {
    1289             :     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
    1290             :       // Every index must be constant.
    1291             :       size_t i, e;
    1292      957649 :       for (i = 0, e = IdxList.size(); i != e; ++i)
    1293      396893 :         if (!isa<Constant>(IdxList[i]))
    1294             :           break;
    1295      198823 :       if (i == e)
    1296      156305 :         return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
    1297      181343 :                       Name);
    1298             :     }
    1299       72824 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
    1300             :   }
    1301             :   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
    1302        7466 :     return CreateGEP(nullptr, Ptr, Idx, Name);
    1303             :   }
    1304       28875 :   Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
    1305             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1306         774 :       if (Constant *IC = dyn_cast<Constant>(Idx))
    1307         374 :         return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
    1308       50648 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
    1309             :   }
    1310         254 :   Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
    1311             :                            const Twine &Name = "") {
    1312             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1313           2 :       if (Constant *IC = dyn_cast<Constant>(Idx))
    1314           0 :         return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
    1315         254 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
    1316             :   }
    1317             :   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
    1318        2008 :     return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
    1319             :   }
    1320        2121 :   Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
    1321             :                             const Twine &Name = "") {
    1322        2121 :     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
    1323             : 
    1324             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1325             :       return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
    1326             : 
    1327        2231 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
    1328             :   }
    1329         585 :   Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
    1330             :                                     const Twine &Name = "") {
    1331         585 :     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
    1332             : 
    1333             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1334             :       return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
    1335             : 
    1336         380 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
    1337             :   }
    1338         799 :   Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
    1339             :                             const Twine &Name = "") {
    1340        1598 :     Value *Idxs[] = {
    1341         799 :       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
    1342         799 :       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
    1343             :     };
    1344             : 
    1345             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1346             :       return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
    1347             : 
    1348        1371 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
    1349             :   }
    1350      187152 :   Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
    1351             :                                     unsigned Idx1, const Twine &Name = "") {
    1352      374304 :     Value *Idxs[] = {
    1353      187152 :       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
    1354      187152 :       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
    1355             :     };
    1356             : 
    1357             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1358             :       return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
    1359             : 
    1360      147814 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
    1361             :   }
    1362         753 :   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
    1363         753 :     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
    1364             : 
    1365             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1366             :       return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
    1367             : 
    1368         673 :     return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
    1369             :   }
    1370        2288 :   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
    1371             :                                     const Twine &Name = "") {
    1372        2288 :     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
    1373             : 
    1374             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1375             :       return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
    1376             : 
    1377        2272 :     return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
    1378             :   }
    1379       22278 :   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
    1380             :                     const Twine &Name = "") {
    1381       44556 :     Value *Idxs[] = {
    1382       22278 :       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
    1383       22278 :       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
    1384             :     };
    1385             : 
    1386             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1387             :       return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
    1388             : 
    1389           0 :     return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
    1390             :   }
    1391      175393 :   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
    1392             :                                     const Twine &Name = "") {
    1393      350786 :     Value *Idxs[] = {
    1394      175393 :       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
    1395      175393 :       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
    1396             :     };
    1397             : 
    1398             :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1399             :       return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
    1400             :                     Name);
    1401             : 
    1402           0 :     return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
    1403             :   }
    1404             :   Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
    1405             :                          const Twine &Name = "") {
    1406      153173 :     return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
    1407             :   }
    1408             : 
    1409             :   /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
    1410             :   /// instead of a pointer to array of i8.
    1411       22931 :   Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
    1412             :                                unsigned AddressSpace = 0) {
    1413       22931 :     GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
    1414       22931 :     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
    1415       22931 :     Value *Args[] = { zero, zero };
    1416       22931 :     return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
    1417             :   }
    1418             : 
    1419             :   //===--------------------------------------------------------------------===//
    1420             :   // Instruction creation methods: Cast/Conversion Operators
    1421             :   //===--------------------------------------------------------------------===//
    1422             : 
    1423             :   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
    1424        4511 :     return CreateCast(Instruction::Trunc, V, DestTy, Name);
    1425             :   }
    1426             :   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
    1427       15073 :     return CreateCast(Instruction::ZExt, V, DestTy, Name);
    1428             :   }
    1429             :   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
    1430        2737 :     return CreateCast(Instruction::SExt, V, DestTy, Name);
    1431             :   }
    1432             :   /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
    1433             :   /// the value untouched if the type of V is already DestTy.
    1434         563 :   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
    1435             :                            const Twine &Name = "") {
    1436             :     assert(V->getType()->isIntOrIntVectorTy() &&
    1437             :            DestTy->isIntOrIntVectorTy() &&
    1438             :            "Can only zero extend/truncate integers!");
    1439             :     Type *VTy = V->getType();
    1440         563 :     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
    1441         255 :       return CreateZExt(V, DestTy, Name);
    1442         308 :     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
    1443         154 :       return CreateTrunc(V, DestTy, Name);
    1444             :     return V;
    1445             :   }
    1446             :   /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
    1447             :   /// the value untouched if the type of V is already DestTy.
    1448        1486 :   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
    1449             :                            const Twine &Name = "") {
    1450             :     assert(V->getType()->isIntOrIntVectorTy() &&
    1451             :            DestTy->isIntOrIntVectorTy() &&
    1452             :            "Can only sign extend/truncate integers!");
    1453             :     Type *VTy = V->getType();
    1454        1486 :     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
    1455         431 :       return CreateSExt(V, DestTy, Name);
    1456        1055 :     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
    1457          67 :       return CreateTrunc(V, DestTy, Name);
    1458             :     return V;
    1459             :   }
    1460             :   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
    1461         117 :     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
    1462             :   }
    1463             :   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
    1464         975 :     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
    1465             :   }
    1466             :   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
    1467         207 :     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
    1468             :   }
    1469             :   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
    1470        5233 :     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
    1471             :   }
    1472             :   Value *CreateFPTrunc(Value *V, Type *DestTy,
    1473             :                        const Twine &Name = "") {
    1474        2162 :     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
    1475             :   }
    1476             :   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
    1477        3336 :     return CreateCast(Instruction::FPExt, V, DestTy, Name);
    1478             :   }
    1479             :   Value *CreatePtrToInt(Value *V, Type *DestTy,
    1480             :                         const Twine &Name = "") {
    1481        9150 :     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
    1482             :   }
    1483             :   Value *CreateIntToPtr(Value *V, Type *DestTy,
    1484             :                         const Twine &Name = "") {
    1485        6620 :     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
    1486             :   }
    1487             :   Value *CreateBitCast(Value *V, Type *DestTy,
    1488             :                        const Twine &Name = "") {
    1489      715540 :     return CreateCast(Instruction::BitCast, V, DestTy, Name);
    1490             :   }
    1491             :   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
    1492             :                              const Twine &Name = "") {
    1493          17 :     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
    1494             :   }
    1495        2174 :   Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
    1496             :                              const Twine &Name = "") {
    1497        2174 :     if (V->getType() == DestTy)
    1498             :       return V;
    1499             :     if (Constant *VC = dyn_cast<Constant>(V))
    1500          72 :       return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
    1501         742 :     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
    1502             :   }
    1503         457 :   Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
    1504             :                              const Twine &Name = "") {
    1505         457 :     if (V->getType() == DestTy)
    1506             :       return V;
    1507             :     if (Constant *VC = dyn_cast<Constant>(V))
    1508           0 :       return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
    1509         172 :     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
    1510             :   }
    1511        1011 :   Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
    1512             :                               const Twine &Name = "") {
    1513        1011 :     if (V->getType() == DestTy)
    1514             :       return V;
    1515             :     if (Constant *VC = dyn_cast<Constant>(V))
    1516          37 :       return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
    1517        1358 :     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
    1518             :   }
    1519      779492 :   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
    1520             :                     const Twine &Name = "") {
    1521      779492 :     if (V->getType() == DestTy)
    1522             :       return V;
    1523             :     if (Constant *VC = dyn_cast<Constant>(V))
    1524      176323 :       return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
    1525      317226 :     return Insert(CastInst::Create(Op, V, DestTy), Name);
    1526             :   }
    1527       34361 :   Value *CreatePointerCast(Value *V, Type *DestTy,
    1528             :                            const Twine &Name = "") {
    1529       34361 :     if (V->getType() == DestTy)
    1530             :       return V;
    1531             :     if (Constant *VC = dyn_cast<Constant>(V))
    1532        1930 :       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
    1533       61775 :     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
    1534             :   }
    1535             : 
    1536       24214 :   Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
    1537             :                                              const Twine &Name = "") {
    1538       24214 :     if (V->getType() == DestTy)
    1539             :       return V;
    1540             : 
    1541             :     if (Constant *VC = dyn_cast<Constant>(V)) {
    1542             :       return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
    1543         906 :                     Name);
    1544             :     }
    1545             : 
    1546       41274 :     return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
    1547       20637 :                   Name);
    1548             :   }
    1549             : 
    1550      119085 :   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
    1551             :                        const Twine &Name = "") {
    1552      119085 :     if (V->getType() == DestTy)
    1553             :       return V;
    1554             :     if (Constant *VC = dyn_cast<Constant>(V))
    1555      112239 :       return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
    1556       39380 :     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
    1557             :   }
    1558             : 
    1559        4679 :   Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
    1560             :                                 const Twine &Name = "") {
    1561        4679 :     if (V->getType() == DestTy)
    1562             :       return V;
    1563        3758 :     if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
    1564          77 :       return CreatePtrToInt(V, DestTy, Name);
    1565        2379 :     if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
    1566         358 :       return CreateIntToPtr(V, DestTy, Name);
    1567             : 
    1568        1645 :     return CreateBitCast(V, DestTy, Name);
    1569             :   }
    1570             : 
    1571             : public:
    1572           6 :   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
    1573           6 :     if (V->getType() == DestTy)
    1574             :       return V;
    1575             :     if (Constant *VC = dyn_cast<Constant>(V))
    1576           0 :       return Insert(Folder.CreateFPCast(VC, DestTy), Name);
    1577           6 :     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
    1578             :   }
    1579             : 
    1580             :   // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
    1581             :   // compile time error, instead of converting the string to bool for the
    1582             :   // isSigned parameter.
    1583             :   Value *CreateIntCast(Value *, Type *, const char *) = delete;
    1584             : 
    1585             :   //===--------------------------------------------------------------------===//
    1586             :   // Instruction creation methods: Compare Instructions
    1587             :   //===--------------------------------------------------------------------===//
    1588             : 
    1589             :   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
    1590       10618 :     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
    1591             :   }
    1592             :   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1593       21839 :     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
    1594             :   }
    1595             :   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1596         194 :     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
    1597             :   }
    1598             :   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1599         162 :     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
    1600             :   }
    1601             :   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1602        1707 :     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
    1603             :   }
    1604             :   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1605         241 :     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
    1606             :   }
    1607             :   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1608         313 :     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
    1609             :   }
    1610             :   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1611         287 :     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
    1612             :   }
    1613             :   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1614         288 :     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
    1615             :   }
    1616             :   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1617         103 :     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
    1618             :   }
    1619             : 
    1620             :   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
    1621             :                        MDNode *FPMathTag = nullptr) {
    1622          38 :     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
    1623             :   }
    1624             :   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
    1625             :                        MDNode *FPMathTag = nullptr) {
    1626          24 :     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
    1627             :   }
    1628             :   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
    1629             :                        MDNode *FPMathTag = nullptr) {
    1630          15 :     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
    1631             :   }
    1632             :   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
    1633             :                        MDNode *FPMathTag = nullptr) {
    1634          25 :     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
    1635             :   }
    1636             :   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
    1637             :                        MDNode *FPMathTag = nullptr) {
    1638          10 :     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
    1639             :   }
    1640             :   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
    1641             :                        MDNode *FPMathTag = nullptr) {
    1642           6 :     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
    1643             :   }
    1644             :   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
    1645             :                        MDNode *FPMathTag = nullptr) {
    1646          22 :     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
    1647             :   }
    1648             :   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
    1649             :                        MDNode *FPMathTag = nullptr) {
    1650         148 :     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
    1651             :   }
    1652             :   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
    1653             :                        MDNode *FPMathTag = nullptr) {
    1654             :     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
    1655             :   }
    1656             :   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
    1657             :                        MDNode *FPMathTag = nullptr) {
    1658           4 :     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
    1659             :   }
    1660             :   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
    1661             :                        MDNode *FPMathTag = nullptr) {
    1662           6 :     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
    1663             :   }
    1664             :   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
    1665             :                        MDNode *FPMathTag = nullptr) {
    1666           6 :     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
    1667             :   }
    1668             :   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
    1669             :                        MDNode *FPMathTag = nullptr) {
    1670           4 :     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
    1671             :   }
    1672             :   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
    1673             :                        MDNode *FPMathTag = nullptr) {
    1674          87 :     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
    1675             :   }
    1676             : 
    1677       77058 :   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
    1678             :                     const Twine &Name = "") {
    1679             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1680             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1681        3053 :         return Insert(Folder.CreateICmp(P, LC, RC), Name);
    1682       84035 :     return Insert(new ICmpInst(P, LHS, RHS), Name);
    1683             :   }
    1684        1799 :   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
    1685             :                     const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1686             :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1687             :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1688         101 :         return Insert(Folder.CreateFCmp(P, LC, RC), Name);
    1689        3460 :     return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS),
    1690        1375 :                                       FPMathTag, FMF), Name);
    1691             :   }
    1692             : 
    1693             :   //===--------------------------------------------------------------------===//
    1694             :   // Instruction creation methods: Other Instructions
    1695             :   //===--------------------------------------------------------------------===//
    1696             : 
    1697       18575 :   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
    1698             :                      const Twine &Name = "") {
    1699       18575 :     return Insert(PHINode::Create(Ty, NumReservedValues), Name);
    1700             :   }
    1701             : 
    1702             :   CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
    1703             :                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1704             :     PointerType *PTy = cast<PointerType>(Callee->getType());
    1705             :     FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
    1706      174634 :     return CreateCall(FTy, Callee, Args, Name, FPMathTag);
    1707             :   }
    1708             : 
    1709      293124 :   CallInst *CreateCall(FunctionType *FTy, Value *Callee,
    1710             :                        ArrayRef<Value *> Args, const Twine &Name = "",
    1711             :                        MDNode *FPMathTag = nullptr) {
    1712      293124 :     CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
    1713      293124 :     if (isa<FPMathOperator>(CI))
    1714        3374 :       CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF));
    1715      293124 :     return Insert(CI, Name);
    1716             :   }
    1717             : 
    1718      265010 :   CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
    1719             :                        ArrayRef<OperandBundleDef> OpBundles,
    1720             :                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1721      265010 :     CallInst *CI = CallInst::Create(Callee, Args, OpBundles);
    1722      265010 :     if (isa<FPMathOperator>(CI))
    1723        5964 :       CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF));
    1724      265010 :     return Insert(CI, Name);
    1725             :   }
    1726             : 
    1727             :   CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
    1728             :                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1729      118490 :     return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag);
    1730             :   }
    1731             : 
    1732       21298 :   Value *CreateSelect(Value *C, Value *True, Value *False,
    1733             :                       const Twine &Name = "", Instruction *MDFrom = nullptr) {
    1734             :     if (Constant *CC = dyn_cast<Constant>(C))
    1735             :       if (Constant *TC = dyn_cast<Constant>(True))
    1736             :         if (Constant *FC = dyn_cast<Constant>(False))
    1737         306 :           return Insert(Folder.CreateSelect(CC, TC, FC), Name);
    1738             : 
    1739       20998 :     SelectInst *Sel = SelectInst::Create(C, True, False);
    1740       20998 :     if (MDFrom) {
    1741             :       MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
    1742             :       MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
    1743        1013 :       Sel = addBranchMetadata(Sel, Prof, Unpred);
    1744             :     }
    1745       20998 :     return Insert(Sel, Name);
    1746             :   }
    1747             : 
    1748           9 :   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
    1749           9 :     return Insert(new VAArgInst(List, Ty), Name);
    1750             :   }
    1751             : 
    1752       11557 :   Value *CreateExtractElement(Value *Vec, Value *Idx,
    1753             :                               const Twine &Name = "") {
    1754             :     if (Constant *VC = dyn_cast<Constant>(Vec))
    1755             :       if (Constant *IC = dyn_cast<Constant>(Idx))
    1756         124 :         return Insert(Folder.CreateExtractElement(VC, IC), Name);
    1757       11433 :     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
    1758             :   }
    1759             : 
    1760             :   Value *CreateExtractElement(Value *Vec, uint64_t Idx,
    1761             :                               const Twine &Name = "") {
    1762         104 :     return CreateExtractElement(Vec, getInt64(Idx), Name);
    1763             :   }
    1764             : 
    1765      160395 :   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
    1766             :                              const Twine &Name = "") {
    1767             :     if (Constant *VC = dyn_cast<Constant>(Vec))
    1768             :       if (Constant *NC = dyn_cast<Constant>(NewElt))
    1769             :         if (Constant *IC = dyn_cast<Constant>(Idx))
    1770      151048 :           return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
    1771        9347 :     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
    1772             :   }
    1773             : 
    1774             :   Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
    1775             :                              const Twine &Name = "") {
    1776          90 :     return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
    1777             :   }
    1778             : 
    1779       11456 :   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
    1780             :                              const Twine &Name = "") {
    1781             :     if (Constant *V1C = dyn_cast<Constant>(V1))
    1782             :       if (Constant *V2C = dyn_cast<Constant>(V2))
    1783             :         if (Constant *MC = dyn_cast<Constant>(Mask))
    1784        2925 :           return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
    1785       13280 :     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
    1786             :   }
    1787             : 
    1788        3429 :   Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask,
    1789             :                              const Twine &Name = "") {
    1790        3429 :     Value *Mask = ConstantDataVector::get(Context, IntMask);
    1791        3429 :     return CreateShuffleVector(V1, V2, Mask, Name);
    1792             :   }
    1793             : 
    1794      219315 :   Value *CreateExtractValue(Value *Agg,
    1795             :                             ArrayRef<unsigned> Idxs,
    1796             :                             const Twine &Name = "") {
    1797             :     if (Constant *AggC = dyn_cast<Constant>(Agg))
    1798      156602 :       return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
    1799       64174 :     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
    1800             :   }
    1801             : 
    1802       11370 :   Value *CreateInsertValue(Value *Agg, Value *Val,
    1803             :                            ArrayRef<unsigned> Idxs,
    1804             :                            const Twine &Name = "") {
    1805             :     if (Constant *AggC = dyn_cast<Constant>(Agg))
    1806             :       if (Constant *ValC = dyn_cast<Constant>(Val))
    1807          46 :         return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
    1808       11533 :     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
    1809             :   }
    1810             : 
    1811       28508 :   LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
    1812             :                                    const Twine &Name = "") {
    1813       28510 :     return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
    1814             :   }
    1815             : 
    1816             :   //===--------------------------------------------------------------------===//
    1817             :   // Utility creation methods
    1818             :   //===--------------------------------------------------------------------===//
    1819             : 
    1820             :   /// \brief Return an i1 value testing if \p Arg is null.
    1821        1787 :   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
    1822        1787 :     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
    1823        1787 :                         Name);
    1824             :   }
    1825             : 
    1826             :   /// \brief Return an i1 value testing if \p Arg is not null.
    1827       17094 :   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
    1828       17094 :     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
    1829       17094 :                         Name);
    1830             :   }
    1831             : 
    1832             :   /// \brief Return the i64 difference between two pointer values, dividing out
    1833             :   /// the size of the pointed-to objects.
    1834             :   ///
    1835             :   /// This is intended to implement C-style pointer subtraction. As such, the
    1836             :   /// pointers must be appropriately aligned for their element types and
    1837             :   /// pointing into the same object.
    1838         120 :   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
    1839             :     assert(LHS->getType() == RHS->getType() &&
    1840             :            "Pointer subtraction operand types must match!");
    1841             :     PointerType *ArgType = cast<PointerType>(LHS->getType());
    1842         240 :     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
    1843         240 :     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
    1844         120 :     Value *Difference = CreateSub(LHS_int, RHS_int);
    1845             :     return CreateExactSDiv(Difference,
    1846         120 :                            ConstantExpr::getSizeOf(ArgType->getElementType()),
    1847         120 :                            Name);
    1848             :   }
    1849             : 
    1850             :   /// \brief Create an invariant.group.barrier intrinsic call, that stops
    1851             :   /// optimizer to propagate equality using invariant.group metadata.
    1852             :   /// If Ptr type is different from pointer to i8, it's casted to pointer to i8
    1853             :   /// in the same address space before call and casted back to Ptr type after
    1854             :   /// call.
    1855          20 :   Value *CreateInvariantGroupBarrier(Value *Ptr) {
    1856             :     assert(isa<PointerType>(Ptr->getType()) &&
    1857             :            "invariant.group.barrier only applies to pointers.");
    1858             :     auto *PtrType = Ptr->getType();
    1859             :     auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
    1860          20 :     if (PtrType != Int8PtrTy)
    1861          20 :       Ptr = CreateBitCast(Ptr, Int8PtrTy);
    1862          20 :     Module *M = BB->getParent()->getParent();
    1863          40 :     Function *FnInvariantGroupBarrier = Intrinsic::getDeclaration(
    1864             :         M, Intrinsic::invariant_group_barrier, {Int8PtrTy});
    1865             : 
    1866             :     assert(FnInvariantGroupBarrier->getReturnType() == Int8PtrTy &&
    1867             :            FnInvariantGroupBarrier->getFunctionType()->getParamType(0) ==
    1868             :                Int8PtrTy &&
    1869             :            "InvariantGroupBarrier should take and return the same type");
    1870             : 
    1871          40 :     CallInst *Fn = CreateCall(FnInvariantGroupBarrier, {Ptr});
    1872             : 
    1873          20 :     if (PtrType != Int8PtrTy)
    1874          20 :       return CreateBitCast(Fn, PtrType);
    1875             :     return Fn;
    1876             :   }
    1877             : 
    1878             :   /// \brief Return a vector value that contains \arg V broadcasted to \p
    1879             :   /// NumElts elements.
    1880        3918 :   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
    1881             :     assert(NumElts > 0 && "Cannot splat to an empty vector!");
    1882             : 
    1883             :     // First insert it into an undef vector so we can shuffle it.
    1884             :     Type *I32Ty = getInt32Ty();
    1885        3918 :     Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
    1886        3918 :     V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
    1887        3918 :                             Name + ".splatinsert");
    1888             : 
    1889             :     // Shuffle the value across the desired number of elements.
    1890        3918 :     Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
    1891        3918 :     return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
    1892             :   }
    1893             : 
    1894             :   /// \brief Return a value that has been extracted from a larger integer type.
    1895             :   Value *CreateExtractInteger(const DataLayout &DL, Value *From,
    1896             :                               IntegerType *ExtractedTy, uint64_t Offset,
    1897             :                               const Twine &Name) {
    1898             :     IntegerType *IntTy = cast<IntegerType>(From->getType());
    1899             :     assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
    1900             :                DL.getTypeStoreSize(IntTy) &&
    1901             :            "Element extends past full value");
    1902             :     uint64_t ShAmt = 8 * Offset;
    1903             :     Value *V = From;
    1904             :     if (DL.isBigEndian())
    1905             :       ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
    1906             :                    DL.getTypeStoreSize(ExtractedTy) - Offset);
    1907             :     if (ShAmt) {
    1908             :       V = CreateLShr(V, ShAmt, Name + ".shift");
    1909             :     }
    1910             :     assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
    1911             :            "Cannot extract to a larger integer!");
    1912             :     if (ExtractedTy != IntTy) {
    1913             :       V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
    1914             :     }
    1915             :     return V;
    1916             :   }
    1917             : 
    1918             : private:
    1919             :   /// \brief Helper function that creates an assume intrinsic call that
    1920             :   /// represents an alignment assumption on the provided Ptr, Mask, Type
    1921             :   /// and Offset.
    1922         327 :   CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
    1923             :                                             Value *PtrValue, Value *Mask,
    1924             :                                             Type *IntPtrTy,
    1925             :                                             Value *OffsetValue) {
    1926         327 :     Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
    1927             : 
    1928         327 :     if (OffsetValue) {
    1929             :       bool IsOffsetZero = false;
    1930             :       if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
    1931             :         IsOffsetZero = CI->isZero();
    1932             : 
    1933           4 :       if (!IsOffsetZero) {
    1934           2 :         if (OffsetValue->getType() != IntPtrTy)
    1935           1 :           OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
    1936             :                                       "offsetcast");
    1937           2 :         PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
    1938             :       }
    1939             :     }
    1940             : 
    1941         327 :     Value *Zero = ConstantInt::get(IntPtrTy, 0);
    1942         327 :     Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
    1943         327 :     Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
    1944         327 :     return CreateAssumption(InvCond);
    1945             :   }
    1946             : 
    1947             : public:
    1948             :   /// \brief Create an assume intrinsic call that represents an alignment
    1949             :   /// assumption on the provided pointer.
    1950             :   ///
    1951             :   /// An optional offset can be provided, and if it is provided, the offset
    1952             :   /// must be subtracted from the provided pointer to get the pointer with the
    1953             :   /// specified alignment.
    1954         321 :   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
    1955             :                                       unsigned Alignment,
    1956             :                                       Value *OffsetValue = nullptr) {
    1957             :     assert(isa<PointerType>(PtrValue->getType()) &&
    1958             :            "trying to create an alignment assumption on a non-pointer?");
    1959             :     PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
    1960             :     Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
    1961             : 
    1962         321 :     Value *Mask = ConstantInt::get(IntPtrTy, Alignment > 0 ? Alignment - 1 : 0);
    1963             :     return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
    1964         321 :                                            OffsetValue);
    1965             :   }
    1966             :   //
    1967             :   /// \brief Create an assume intrinsic call that represents an alignment
    1968             :   /// assumption on the provided pointer.
    1969             :   ///
    1970             :   /// An optional offset can be provided, and if it is provided, the offset
    1971             :   /// must be subtracted from the provided pointer to get the pointer with the
    1972             :   /// specified alignment.
    1973             :   ///
    1974             :   /// This overload handles the condition where the Alignment is dependent
    1975             :   /// on an existing value rather than a static value.
    1976           6 :   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
    1977             :                                       Value *Alignment,
    1978             :                                       Value *OffsetValue = nullptr) {
    1979             :     assert(isa<PointerType>(PtrValue->getType()) &&
    1980             :            "trying to create an alignment assumption on a non-pointer?");
    1981             :     PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
    1982             :     Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
    1983             : 
    1984           6 :     if (Alignment->getType() != IntPtrTy)
    1985           4 :       Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ true,
    1986             :                                 "alignmentcast");
    1987           6 :     Value *IsPositive =
    1988           6 :         CreateICmp(CmpInst::ICMP_SGT, Alignment,
    1989           6 :                    ConstantInt::get(Alignment->getType(), 0), "ispositive");
    1990           6 :     Value *PositiveMask =
    1991          12 :         CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "positivemask");
    1992           6 :     Value *Mask = CreateSelect(IsPositive, PositiveMask,
    1993           6 :                                ConstantInt::get(IntPtrTy, 0), "mask");
    1994             : 
    1995             :     return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
    1996           6 :                                            OffsetValue);
    1997             :   }
    1998             : };
    1999             : 
    2000             : // Create wrappers for C Binding types (see CBindingWrapping.h).
    2001             : DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
    2002             : 
    2003             : } // end namespace llvm
    2004             : 
    2005             : #endif // LLVM_IR_IRBUILDER_H

Generated by: LCOV version 1.13