LCOV - code coverage report
Current view: top level - include/llvm/IR - IRBuilder.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 658 677 97.2 %
Date: 2017-09-14 15:23:50 Functions: 651 803 81.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     3097513 :   void InsertHelper(Instruction *I, const Twine &Name,
      66             :                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
      67     3097513 :     if (BB) BB->getInstList().insert(InsertPt, I);
      68     3097513 :     I->setName(Name);
      69     3097513 :   }
      70             : };
      71             : 
      72             : /// Provides an 'InsertHelper' that calls a user-provided callback after
      73             : /// performing the default insertion.
      74     1916550 : class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
      75             :   std::function<void(Instruction *)> Callback;
      76             : 
      77             : public:
      78             :   IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
      79      421058 :       : Callback(std::move(Callback)) {}
      80             : 
      81             : protected:
      82      183987 :   void InsertHelper(Instruction *I, const Twine &Name,
      83             :                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
      84      183987 :     IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
      85      367974 :     Callback(I);
      86      183987 :   }
      87             : };
      88             : 
      89             : /// \brief Common base class shared among various IRBuilders.
      90    23104912 : 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    11224564 :       : Context(context), DefaultFPMathTag(FPMathTag),
     107    33779413 :         DefaultOperandBundles(OpBundles) {
     108    11225136 :     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    10028614 :     BB = nullptr;
     119    11639254 :     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     1964543 :     BB = TheBB;
     130     3929086 :     InsertPt = BB->end();
     131             :   }
     132             : 
     133             :   /// \brief This specifies that created instructions should be inserted before
     134             :   /// the specified instruction.
     135    30457285 :   void SetInsertPoint(Instruction *I) {
     136    30457285 :     BB = I->getParent();
     137    60914570 :     InsertPt = I->getIterator();
     138             :     assert(InsertPt != BB->end() && "Can't read debug loc from end()");
     139   121829140 :     SetCurrentDebugLocation(I->getDebugLoc());
     140    30457285 :   }
     141             : 
     142             :   /// \brief This specifies that created instructions should be inserted at the
     143             :   /// specified point.
     144      558383 :   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
     145      558383 :     BB = TheBB;
     146      558383 :     InsertPt = IP;
     147      558383 :     if (IP != TheBB->end())
     148     1994580 :       SetCurrentDebugLocation(IP->getDebugLoc());
     149      558383 :   }
     150             : 
     151             :   /// \brief Set location information used by debugging information.
     152   105101456 :   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     3191427 :   void SetInstDebugLocation(Instruction *I) const {
     160     6382854 :     if (CurDbgLocation)
     161     6395664 :       I->setDebugLoc(CurDbgLocation);
     162     3191427 :   }
     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       56077 :       : 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        5927 :     return InsertPoint(GetInsertBlock(), GetInsertPoint());
     191             :   }
     192             : 
     193             :   /// \brief Returns the current insert point, clearing it in the process.
     194             :   InsertPoint saveAndClearIP() {
     195      112154 :     InsertPoint IP(GetInsertBlock(), GetInsertPoint());
     196       56077 :     ClearInsertionPoint();
     197             :     return IP;
     198             :   }
     199             : 
     200             :   /// \brief Sets the current insert point to a previously-saved location.
     201             :   void restoreIP(InsertPoint IP) {
     202      343902 :     if (IP.isSet())
     203      340534 :       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          10 :   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      105057 :   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      212311 :     InsertPointGuard(IRBuilderBase &B)
     237      212311 :         : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
     238      636933 :           DbgLoc(B.getCurrentDebugLocation()) {}
     239             : 
     240             :     InsertPointGuard(const InsertPointGuard &) = delete;
     241             :     InsertPointGuard &operator=(const InsertPointGuard &) = delete;
     242             : 
     243      636933 :     ~InsertPointGuard() {
     244      636933 :       Builder.restoreIP(InsertPoint(Block, Point));
     245      849244 :       Builder.SetCurrentDebugLocation(DbgLoc);
     246      212311 :     }
     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         463 :         : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
     259             : 
     260             :     FastMathFlagGuard(const FastMathFlagGuard &) = delete;
     261             :     FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
     262             : 
     263         463 :     ~FastMathFlagGuard() {
     264         463 :       Builder.FMF = FMF;
     265         463 :       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       31708 :   ConstantInt *getInt1(bool V) {
     284       63416 :     return ConstantInt::get(getInt1Ty(), V);
     285             :   }
     286             : 
     287             :   /// \brief Get the constant value for i1 true.
     288             :   ConstantInt *getTrue() {
     289        1289 :     return ConstantInt::getTrue(Context);
     290             :   }
     291             : 
     292             :   /// \brief Get the constant value for i1 false.
     293             :   ConstantInt *getFalse() {
     294        1608 :     return ConstantInt::getFalse(Context);
     295             :   }
     296             : 
     297             :   /// \brief Get a constant 8-bit value.
     298       22188 :   ConstantInt *getInt8(uint8_t C) {
     299       44376 :     return ConstantInt::get(getInt8Ty(), C);
     300             :   }
     301             : 
     302             :   /// \brief Get a constant 16-bit value.
     303         678 :   ConstantInt *getInt16(uint16_t C) {
     304        1356 :     return ConstantInt::get(getInt16Ty(), C);
     305             :   }
     306             : 
     307             :   /// \brief Get a constant 32-bit value.
     308      362001 :   ConstantInt *getInt32(uint32_t C) {
     309      724002 :     return ConstantInt::get(getInt32Ty(), C);
     310             :   }
     311             : 
     312             :   /// \brief Get a constant 64-bit value.
     313      157647 :   ConstantInt *getInt64(uint64_t C) {
     314      315294 :     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        3030 :   ConstantInt *getIntN(unsigned N, uint64_t C) {
     320        6060 :     return ConstantInt::get(getIntNTy(N), C);
     321             :   }
     322             : 
     323             :   /// \brief Get a constant integer value.
     324             :   ConstantInt *getInt(const APInt &AI) {
     325      113538 :     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       37974 :     return Type::getInt1Ty(Context);
     335             :   }
     336             : 
     337             :   /// \brief Fetch the type representing an 8-bit integer.
     338             :   IntegerType *getInt8Ty() {
     339       39438 :     return Type::getInt8Ty(Context);
     340             :   }
     341             : 
     342             :   /// \brief Fetch the type representing a 16-bit integer.
     343             :   IntegerType *getInt16Ty() {
     344         770 :     return Type::getInt16Ty(Context);
     345             :   }
     346             : 
     347             :   /// \brief Fetch the type representing a 32-bit integer.
     348             :   IntegerType *getInt32Ty() {
     349      375363 :     return Type::getInt32Ty(Context);
     350             :   }
     351             : 
     352             :   /// \brief Fetch the type representing a 64-bit integer.
     353             :   IntegerType *getInt64Ty() {
     354      158262 :     return Type::getInt64Ty(Context);
     355             :   }
     356             : 
     357             :   /// \brief Fetch the type representing a 128-bit integer.
     358             :   IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
     359             : 
     360             :   /// \brief Fetch the type representing an N-bit integer.
     361             :   IntegerType *getIntNTy(unsigned N) {
     362        4995 :     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       48318 :     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      105759 :     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         431 :     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          43 :   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       20971 :     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
     410       20971 :                         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          17 :   CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
     424             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     425             :                          MDNode *TBAAStructTag = nullptr,
     426             :                          MDNode *ScopeTag = nullptr,
     427             :                          MDNode *NoAliasTag = nullptr) {
     428         174 :     return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
     429         174 :                         TBAAStructTag, ScopeTag, NoAliasTag);
     430             :   }
     431             : 
     432             :   CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
     433             :                          bool isVolatile = false, MDNode *TBAATag = nullptr,
     434             :                          MDNode *TBAAStructTag = nullptr,
     435             :                          MDNode *ScopeTag = nullptr,
     436             :                          MDNode *NoAliasTag = nullptr);
     437             : 
     438             :   /// \brief Create and insert an element unordered-atomic memcpy between the
     439             :   /// specified pointers.
     440             :   ///
     441             :   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
     442             :   /// specified, it will be added to the instruction. Likewise with alias.scope
     443             :   /// and noalias tags.
     444             :   CallInst *CreateElementUnorderedAtomicMemCpy(
     445             :       Value *Dst, Value *Src, uint64_t Size, uint32_t ElementSize,
     446             :       MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
     447             :       MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
     448             :     return CreateElementUnorderedAtomicMemCpy(
     449             :         Dst, Src, getInt64(Size), ElementSize, TBAATag, TBAAStructTag, ScopeTag,
     450             :         NoAliasTag);
     451             :   }
     452             : 
     453             :   CallInst *CreateElementUnorderedAtomicMemCpy(
     454             :       Value *Dst, Value *Src, Value *Size, uint32_t ElementSize,
     455             :       MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
     456             :       MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
     457             : 
     458             :   /// \brief Create and insert a memmove between the specified
     459             :   /// pointers.
     460             :   ///
     461             :   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
     462             :   /// specified, it will be added to the instruction. Likewise with alias.scope
     463             :   /// and noalias tags.
     464             :   CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
     465             :                           bool isVolatile = false, MDNode *TBAATag = nullptr,
     466             :                           MDNode *ScopeTag = nullptr,
     467             :                           MDNode *NoAliasTag = nullptr) {
     468           3 :     return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
     469           3 :                          TBAATag, ScopeTag, NoAliasTag);
     470             :   }
     471             : 
     472             :   CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
     473             :                           bool isVolatile = false, MDNode *TBAATag = nullptr,
     474             :                           MDNode *ScopeTag = nullptr,
     475             :                           MDNode *NoAliasTag = nullptr);
     476             : 
     477             :   /// \brief Create a vector fadd reduction intrinsic of the source vector.
     478             :   /// The first parameter is a scalar accumulator value for ordered reductions.
     479             :   CallInst *CreateFAddReduce(Value *Acc, Value *Src);
     480             : 
     481             :   /// \brief Create a vector fmul reduction intrinsic of the source vector.
     482             :   /// The first parameter is a scalar accumulator value for ordered reductions.
     483             :   CallInst *CreateFMulReduce(Value *Acc, Value *Src);
     484             : 
     485             :   /// \brief Create a vector int add reduction intrinsic of the source vector.
     486             :   CallInst *CreateAddReduce(Value *Src);
     487             : 
     488             :   /// \brief Create a vector int mul reduction intrinsic of the source vector.
     489             :   CallInst *CreateMulReduce(Value *Src);
     490             : 
     491             :   /// \brief Create a vector int AND reduction intrinsic of the source vector.
     492             :   CallInst *CreateAndReduce(Value *Src);
     493             : 
     494             :   /// \brief Create a vector int OR reduction intrinsic of the source vector.
     495             :   CallInst *CreateOrReduce(Value *Src);
     496             : 
     497             :   /// \brief Create a vector int XOR reduction intrinsic of the source vector.
     498             :   CallInst *CreateXorReduce(Value *Src);
     499             : 
     500             :   /// \brief Create a vector integer max reduction intrinsic of the source
     501             :   /// vector.
     502             :   CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
     503             : 
     504             :   /// \brief Create a vector integer min reduction intrinsic of the source
     505             :   /// vector.
     506             :   CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
     507             : 
     508             :   /// \brief Create a vector float max reduction intrinsic of the source
     509             :   /// vector.
     510             :   CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
     511             : 
     512             :   /// \brief Create a vector float min reduction intrinsic of the source
     513             :   /// vector.
     514             :   CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
     515             : 
     516             :   /// \brief Create a lifetime.start intrinsic.
     517             :   ///
     518             :   /// If the pointer isn't i8* it will be converted.
     519             :   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
     520             : 
     521             :   /// \brief Create a lifetime.end intrinsic.
     522             :   ///
     523             :   /// If the pointer isn't i8* it will be converted.
     524             :   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
     525             : 
     526             :   /// Create a call to invariant.start intrinsic.
     527             :   ///
     528             :   /// If the pointer isn't i8* it will be converted.
     529             :   CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
     530             : 
     531             :   /// \brief Create a call to Masked Load intrinsic
     532             :   CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
     533             :                              Value *PassThru = nullptr, const Twine &Name = "");
     534             : 
     535             :   /// \brief Create a call to Masked Store intrinsic
     536             :   CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
     537             :                               Value *Mask);
     538             : 
     539             :   /// \brief Create a call to Masked Gather intrinsic
     540             :   CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
     541             :                                Value *Mask = nullptr,
     542             :                                Value *PassThru = nullptr,
     543             :                                const Twine& Name = "");
     544             : 
     545             :   /// \brief Create a call to Masked Scatter intrinsic
     546             :   CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
     547             :                                 Value *Mask = nullptr);
     548             : 
     549             :   /// \brief Create an assume intrinsic call that allows the optimizer to
     550             :   /// assume that the provided condition will be true.
     551             :   CallInst *CreateAssumption(Value *Cond);
     552             : 
     553             :   /// \brief Create a call to the experimental.gc.statepoint intrinsic to
     554             :   /// start a new statepoint sequence.
     555             :   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
     556             :                                    Value *ActualCallee,
     557             :                                    ArrayRef<Value *> CallArgs,
     558             :                                    ArrayRef<Value *> DeoptArgs,
     559             :                                    ArrayRef<Value *> GCArgs,
     560             :                                    const Twine &Name = "");
     561             : 
     562             :   /// \brief Create a call to the experimental.gc.statepoint intrinsic to
     563             :   /// start a new statepoint sequence.
     564             :   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
     565             :                                    Value *ActualCallee, uint32_t Flags,
     566             :                                    ArrayRef<Use> CallArgs,
     567             :                                    ArrayRef<Use> TransitionArgs,
     568             :                                    ArrayRef<Use> DeoptArgs,
     569             :                                    ArrayRef<Value *> GCArgs,
     570             :                                    const Twine &Name = "");
     571             : 
     572             :   // \brief Conveninence function for the common case when CallArgs are filled
     573             :   // in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
     574             :   // .get()'ed to get the Value pointer.
     575             :   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
     576             :                                    Value *ActualCallee, ArrayRef<Use> CallArgs,
     577             :                                    ArrayRef<Value *> DeoptArgs,
     578             :                                    ArrayRef<Value *> GCArgs,
     579             :                                    const Twine &Name = "");
     580             : 
     581             :   /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
     582             :   /// start a new statepoint sequence.
     583             :   InvokeInst *
     584             :   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
     585             :                            Value *ActualInvokee, BasicBlock *NormalDest,
     586             :                            BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
     587             :                            ArrayRef<Value *> DeoptArgs,
     588             :                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
     589             : 
     590             :   /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
     591             :   /// start a new statepoint sequence.
     592             :   InvokeInst *CreateGCStatepointInvoke(
     593             :       uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
     594             :       BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
     595             :       ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
     596             :       ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
     597             :       const Twine &Name = "");
     598             : 
     599             :   // Conveninence function for the common case when CallArgs are filled in using
     600             :   // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
     601             :   // get the Value *.
     602             :   InvokeInst *
     603             :   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
     604             :                            Value *ActualInvokee, BasicBlock *NormalDest,
     605             :                            BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
     606             :                            ArrayRef<Value *> DeoptArgs,
     607             :                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
     608             : 
     609             :   /// \brief Create a call to the experimental.gc.result intrinsic to extract
     610             :   /// the result from a call wrapped in a statepoint.
     611             :   CallInst *CreateGCResult(Instruction *Statepoint,
     612             :                            Type *ResultType,
     613             :                            const Twine &Name = "");
     614             : 
     615             :   /// \brief Create a call to the experimental.gc.relocate intrinsics to
     616             :   /// project the relocated value of one pointer from the statepoint.
     617             :   CallInst *CreateGCRelocate(Instruction *Statepoint,
     618             :                              int BaseOffset,
     619             :                              int DerivedOffset,
     620             :                              Type *ResultType,
     621             :                              const Twine &Name = "");
     622             : 
     623             :   /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
     624             :   /// first type.
     625             :   CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID,
     626             :                                   Value *LHS, Value *RHS,
     627             :                                   const Twine &Name = "");
     628             : 
     629             :   /// Create call to the minnum intrinsic.
     630             :   CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
     631          12 :     return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, Name);
     632             :   }
     633             : 
     634             :   /// Create call to the maxnum intrinsic.
     635             :   CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
     636             :     return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, Name);
     637             :   }
     638             : 
     639             : private:
     640             :   /// \brief Create a call to a masked intrinsic with given Id.
     641             :   CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
     642             :                                   ArrayRef<Type *> OverloadedTypes,
     643             :                                   const Twine &Name = "");
     644             : 
     645             :   Value *getCastedInt8PtrValue(Value *Ptr);
     646             : };
     647             : 
     648             : /// \brief This provides a uniform API for creating instructions and inserting
     649             : /// them into a basic block: either at the end of a BasicBlock, or at a specific
     650             : /// iterator location in a block.
     651             : ///
     652             : /// Note that the builder does not expose the full generality of LLVM
     653             : /// instructions.  For access to extra instruction properties, use the mutators
     654             : /// (e.g. setVolatile) on the instructions after they have been
     655             : /// created. Convenience state exists to specify fast-math flags and fp-math
     656             : /// tags.
     657             : ///
     658             : /// The first template argument specifies a class to use for creating constants.
     659             : /// This defaults to creating minimally folded constants.  The second template
     660             : /// argument allows clients to specify custom insertion hooks that are called on
     661             : /// every newly created insertion.
     662             : template <typename T = ConstantFolder,
     663             :           typename Inserter = IRBuilderDefaultInserter>
     664    23565895 : class IRBuilder : public IRBuilderBase, public Inserter {
     665             :   T Folder;
     666             : 
     667             : public:
     668      419265 :   IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
     669             :             MDNode *FPMathTag = nullptr,
     670             :             ArrayRef<OperandBundleDef> OpBundles = None)
     671      419837 :       : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
     672     1259511 :         Folder(F) {}
     673             : 
     674       39840 :   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
     675             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     676       79808 :       : IRBuilderBase(C, FPMathTag, OpBundles), Folder() {}
     677             : 
     678             :   explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
     679             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     680             :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
     681             :     SetInsertPoint(TheBB);
     682             :   }
     683             : 
     684     1610610 :   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
     685             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     686     3222231 :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() {
     687     3221220 :     SetInsertPoint(TheBB);
     688             :   }
     689             : 
     690     9152223 :   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
     691             :                      ArrayRef<OperandBundleDef> OpBundles = None)
     692    18305272 :       : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles), Folder() {
     693     9152223 :     SetInsertPoint(IP);
     694     9152223 :   }
     695             : 
     696             :   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
     697             :             MDNode *FPMathTag = nullptr,
     698             :             ArrayRef<OperandBundleDef> OpBundles = None)
     699             :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
     700             :     SetInsertPoint(TheBB, IP);
     701             :   }
     702             : 
     703        2626 :   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
     704             :             MDNode *FPMathTag = nullptr,
     705             :             ArrayRef<OperandBundleDef> OpBundles = None)
     706        5252 :       : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() {
     707        2626 :     SetInsertPoint(TheBB, IP);
     708        2626 :   }
     709             : 
     710             :   /// \brief Get the constant folder being used.
     711             :   const T &getFolder() { return Folder; }
     712             : 
     713             :   /// \brief Insert and return the specified instruction.
     714             :   template<typename InstTy>
     715     2281936 :   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
     716     3374829 :     this->InsertHelper(I, Name, BB, InsertPt);
     717     3097513 :     this->SetInstDebugLocation(I);
     718     2281936 :     return I;
     719             :   }
     720             : 
     721             :   /// \brief No-op overload to handle constants.
     722             :   Constant *Insert(Constant *C, const Twine& = "") const {
     723             :     return C;
     724             :   }
     725             : 
     726             :   //===--------------------------------------------------------------------===//
     727             :   // Instruction creation methods: Terminators
     728             :   //===--------------------------------------------------------------------===//
     729             : 
     730             : private:
     731             :   /// \brief Helper to add branch weight and unpredictable metadata onto an
     732             :   /// instruction.
     733             :   /// \returns The annotated instruction.
     734             :   template <typename InstTy>
     735       37391 :   InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
     736       37391 :     if (Weights)
     737         909 :       I->setMetadata(LLVMContext::MD_prof, Weights);
     738       37391 :     if (Unpredictable)
     739           2 :       I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
     740       37391 :     return I;
     741             :   }
     742             : 
     743             : public:
     744             :   /// \brief Create a 'ret void' instruction.
     745       49523 :   ReturnInst *CreateRetVoid() {
     746       99469 :     return Insert(ReturnInst::Create(Context));
     747             :   }
     748             : 
     749             :   /// \brief Create a 'ret <val>' instruction.
     750       54729 :   ReturnInst *CreateRet(Value *V) {
     751       55059 :     return Insert(ReturnInst::Create(Context, V));
     752             :   }
     753             : 
     754             :   /// \brief Create a sequence of N insertvalue instructions,
     755             :   /// with one Value from the retVals array each, that build a aggregate
     756             :   /// return value one value at a time, and a ret instruction to return
     757             :   /// the resulting aggregate value.
     758             :   ///
     759             :   /// This is a convenience function for code that uses aggregate return values
     760             :   /// as a vehicle for having multiple return values.
     761           0 :   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
     762           0 :     Value *V = UndefValue::get(getCurrentFunctionReturnType());
     763           0 :     for (unsigned i = 0; i != N; ++i)
     764           0 :       V = CreateInsertValue(V, retVals[i], i, "mrv");
     765           0 :     return Insert(ReturnInst::Create(Context, V));
     766             :   }
     767             : 
     768             :   /// \brief Create an unconditional 'br label X' instruction.
     769      168325 :   BranchInst *CreateBr(BasicBlock *Dest) {
     770      343769 :     return Insert(BranchInst::Create(Dest));
     771             :   }
     772             : 
     773             :   /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
     774             :   /// instruction.
     775       35318 :   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
     776             :                            MDNode *BranchWeights = nullptr,
     777             :                            MDNode *Unpredictable = nullptr) {
     778      105954 :     return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
     779       70636 :                                     BranchWeights, Unpredictable));
     780             :   }
     781             : 
     782             :   /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
     783             :   /// instruction. Copy branch meta data if available.
     784         124 :   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
     785             :                            Instruction *MDSrc) {
     786         124 :     BranchInst *Br = BranchInst::Create(True, False, Cond);
     787         124 :     if (MDSrc) {
     788          56 :       unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
     789             :                         LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
     790         112 :       Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
     791             :     }
     792         248 :     return Insert(Br);
     793             :   }
     794             : 
     795             :   /// \brief Create a switch instruction with the specified value, default dest,
     796             :   /// and with a hint for the number of cases that will be added (for efficient
     797             :   /// allocation).
     798        1230 :   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
     799             :                            MDNode *BranchWeights = nullptr,
     800             :                            MDNode *Unpredictable = nullptr) {
     801        3690 :     return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
     802        2460 :                                     BranchWeights, Unpredictable));
     803             :   }
     804             : 
     805             :   /// \brief Create an indirect branch instruction with the specified address
     806             :   /// operand, with an optional hint for the number of destinations that will be
     807             :   /// added (for efficient allocation).
     808          25 :   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
     809          50 :     return Insert(IndirectBrInst::Create(Addr, NumDests));
     810             :   }
     811             : 
     812             :   /// \brief Create an invoke instruction.
     813           3 :   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
     814             :                            BasicBlock *UnwindDest,
     815             :                            ArrayRef<Value *> Args = None,
     816             :                            const Twine &Name = "") {
     817           9 :     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
     818           6 :                   Name);
     819             :   }
     820       32860 :   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
     821             :                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
     822             :                            ArrayRef<OperandBundleDef> OpBundles,
     823             :                            const Twine &Name = "") {
     824       98580 :     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
     825       65720 :                                      OpBundles), Name);
     826             :   }
     827             : 
     828        3330 :   ResumeInst *CreateResume(Value *Exn) {
     829        6661 :     return Insert(ResumeInst::Create(Exn));
     830             :   }
     831             : 
     832          96 :   CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
     833             :                                       BasicBlock *UnwindBB = nullptr) {
     834          96 :     return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
     835             :   }
     836             : 
     837          55 :   CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
     838             :                                      unsigned NumHandlers,
     839             :                                      const Twine &Name = "") {
     840         165 :     return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
     841         110 :                   Name);
     842             :   }
     843             : 
     844          58 :   CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
     845             :                                const Twine &Name = "") {
     846          58 :     return Insert(CatchPadInst::Create(ParentPad, Args), Name);
     847             :   }
     848             : 
     849         103 :   CleanupPadInst *CreateCleanupPad(Value *ParentPad,
     850             :                                    ArrayRef<Value *> Args = None,
     851             :                                    const Twine &Name = "") {
     852         103 :     return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
     853             :   }
     854             : 
     855          56 :   CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
     856         112 :     return Insert(CatchReturnInst::Create(CatchPad, BB));
     857             :   }
     858             : 
     859        8537 :   UnreachableInst *CreateUnreachable() {
     860       19160 :     return Insert(new UnreachableInst(Context));
     861             :   }
     862             : 
     863             :   //===--------------------------------------------------------------------===//
     864             :   // Instruction creation methods: Binary Operators
     865             :   //===--------------------------------------------------------------------===//
     866             : private:
     867      131683 :   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
     868             :                                           Value *LHS, Value *RHS,
     869             :                                           const Twine &Name,
     870             :                                           bool HasNUW, bool HasNSW) {
     871      235670 :     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
     872      131683 :     if (HasNUW) BO->setHasNoUnsignedWrap();
     873      131683 :     if (HasNSW) BO->setHasNoSignedWrap();
     874      131683 :     return BO;
     875             :   }
     876             : 
     877       15945 :   Instruction *AddFPMathAttributes(Instruction *I,
     878             :                                    MDNode *FPMathTag,
     879             :                                    FastMathFlags FMF) const {
     880       15945 :     if (!FPMathTag)
     881       15944 :       FPMathTag = DefaultFPMathTag;
     882       15945 :     if (FPMathTag)
     883           9 :       I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
     884       15945 :     I->setFastMathFlags(FMF);
     885       15945 :     return I;
     886             :   }
     887             : 
     888             : public:
     889      121921 :   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
     890             :                    bool HasNUW = false, bool HasNSW = false) {
     891        6568 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     892        5004 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     893        9434 :         return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
     894      116917 :     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
     895      116917 :                                    HasNUW, HasNSW);
     896             :   }
     897             :   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
     898       13195 :     return CreateAdd(LHS, RHS, Name, false, true);
     899             :   }
     900             :   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
     901          85 :     return CreateAdd(LHS, RHS, Name, true, false);
     902             :   }
     903        1859 :   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
     904             :                     MDNode *FPMathTag = nullptr) {
     905          38 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     906          25 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     907          49 :         return Insert(Folder.CreateFAdd(LC, RC), Name);
     908        5502 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
     909        1834 :                                       FPMathTag, FMF), Name);
     910             :   }
     911       15081 :   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
     912             :                    bool HasNUW = false, bool HasNSW = false) {
     913        9623 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     914        8742 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     915       17464 :         return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
     916        6339 :     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
     917        6339 :                                    HasNUW, HasNSW);
     918             :   }
     919             :   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
     920        9711 :     return CreateSub(LHS, RHS, Name, false, true);
     921             :   }
     922             :   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
     923          12 :     return CreateSub(LHS, RHS, Name, true, false);
     924             :   }
     925         830 :   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
     926             :                     MDNode *FPMathTag = nullptr) {
     927         411 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     928          85 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     929         168 :         return Insert(Folder.CreateFSub(LC, RC), Name);
     930        2235 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
     931         745 :                                       FPMathTag, FMF), Name);
     932             :   }
     933        8507 :   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
     934             :                    bool HasNUW = false, bool HasNSW = false) {
     935        3661 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     936        2336 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     937        4554 :         return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
     938        6171 :     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
     939        6171 :                                    HasNUW, HasNSW);
     940             :   }
     941             :   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
     942        2873 :     return CreateMul(LHS, RHS, Name, false, true);
     943             :   }
     944             :   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
     945         851 :     return CreateMul(LHS, RHS, Name, true, false);
     946             :   }
     947        1333 :   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
     948             :                     MDNode *FPMathTag = nullptr) {
     949          69 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     950          23 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     951          45 :         return Insert(Folder.CreateFMul(LC, RC), Name);
     952        3930 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
     953        1310 :                                       FPMathTag, FMF), Name);
     954             :   }
     955        1081 :   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
     956             :                     bool isExact = false) {
     957         605 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     958         574 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     959        1148 :         return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
     960         507 :     if (!isExact)
     961        1031 :       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
     962          24 :     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
     963             :   }
     964             :   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
     965          22 :     return CreateUDiv(LHS, RHS, Name, true);
     966             :   }
     967        3207 :   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
     968             :                     bool isExact = false) {
     969        1909 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     970        1903 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     971        3806 :         return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
     972        1304 :     if (!isExact)
     973        1598 :       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
     974         520 :     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
     975             :   }
     976             :   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
     977         463 :     return CreateSDiv(LHS, RHS, Name, true);
     978             :   }
     979         313 :   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
     980             :                     MDNode *FPMathTag = nullptr) {
     981          39 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     982          10 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     983          20 :         return Insert(Folder.CreateFDiv(LC, RC), Name);
     984         909 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
     985         303 :                                       FPMathTag, FMF), Name);
     986             :   }
     987        1128 :   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
     988         326 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     989         322 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     990         644 :         return Insert(Folder.CreateURem(LC, RC), Name);
     991        2036 :     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
     992             :   }
     993         203 :   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
     994           7 :     if (Constant *LC = dyn_cast<Constant>(LHS))
     995           3 :       if (Constant *RC = dyn_cast<Constant>(RHS))
     996           6 :         return Insert(Folder.CreateSRem(LC, RC), Name);
     997         430 :     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
     998             :   }
     999           6 :   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
    1000             :                     MDNode *FPMathTag = nullptr) {
    1001           0 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1002           0 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1003           0 :         return Insert(Folder.CreateFRem(LC, RC), Name);
    1004          18 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
    1005           6 :                                       FPMathTag, FMF), Name);
    1006             :   }
    1007             : 
    1008        3442 :   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
    1009             :                    bool HasNUW = false, bool HasNSW = false) {
    1010        1299 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1011        1186 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1012        2346 :         return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
    1013        2256 :     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
    1014        2256 :                                    HasNUW, HasNSW);
    1015             :   }
    1016             :   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
    1017             :                    bool HasNUW = false, bool HasNSW = false) {
    1018             :     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
    1019             :                      HasNUW, HasNSW);
    1020             :   }
    1021        1245 :   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
    1022             :                    bool HasNUW = false, bool HasNSW = false) {
    1023        1245 :     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
    1024        1245 :                      HasNUW, HasNSW);
    1025             :   }
    1026             : 
    1027        1687 :   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
    1028             :                     bool isExact = false) {
    1029          94 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1030          83 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1031         150 :         return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
    1032        1604 :     if (!isExact)
    1033        3803 :       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
    1034           3 :     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
    1035             :   }
    1036             :   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
    1037             :                     bool isExact = false) {
    1038             :     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1039             :   }
    1040         797 :   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
    1041             :                     bool isExact = false) {
    1042         797 :     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1043             :   }
    1044             : 
    1045        1162 :   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
    1046             :                     bool isExact = false) {
    1047         544 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1048         544 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1049        1069 :         return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
    1050         618 :     if (!isExact)
    1051        1241 :       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
    1052          10 :     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
    1053             :   }
    1054             :   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
    1055             :                     bool isExact = false) {
    1056             :     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1057             :   }
    1058         330 :   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
    1059             :                     bool isExact = false) {
    1060         330 :     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
    1061             :   }
    1062             : 
    1063       10450 :   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
    1064        7231 :     if (Constant *RC = dyn_cast<Constant>(RHS)) {
    1065       28780 :       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
    1066             :         return LHS;  // LHS & -1 -> LHS
    1067        1422 :       if (Constant *LC = dyn_cast<Constant>(LHS))
    1068        2844 :         return Insert(Folder.CreateAnd(LC, RC), Name);
    1069             :     }
    1070       18336 :     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
    1071             :   }
    1072        3879 :   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
    1073        3879 :     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1074             :   }
    1075         185 :   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
    1076         185 :     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1077             :   }
    1078             : 
    1079        6147 :   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
    1080        2476 :     if (Constant *RC = dyn_cast<Constant>(RHS)) {
    1081        2476 :       if (RC->isNullValue())
    1082             :         return LHS;  // LHS | 0 -> LHS
    1083         443 :       if (Constant *LC = dyn_cast<Constant>(LHS))
    1084         845 :         return Insert(Folder.CreateOr(LC, RC), Name);
    1085             :     }
    1086       10906 :     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
    1087             :   }
    1088          19 :   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
    1089          19 :     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1090             :   }
    1091             :   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
    1092             :     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1093             :   }
    1094             : 
    1095        1553 :   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
    1096          60 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1097          28 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1098          56 :         return Insert(Folder.CreateXor(LC, RC), Name);
    1099        3641 :     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
    1100             :   }
    1101         119 :   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
    1102         119 :     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1103             :   }
    1104          11 :   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
    1105          11 :     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
    1106             :   }
    1107             : 
    1108       80450 :   Value *CreateBinOp(Instruction::BinaryOps Opc,
    1109             :                      Value *LHS, Value *RHS, const Twine &Name = "",
    1110             :                      MDNode *FPMathTag = nullptr) {
    1111       69769 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1112        1830 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1113        3648 :         return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
    1114       78620 :     Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
    1115      157240 :     if (isa<FPMathOperator>(BinOp))
    1116        1149 :       BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
    1117      156216 :     return Insert(BinOp, Name);
    1118             :   }
    1119             : 
    1120         442 :   Value *CreateNeg(Value *V, const Twine &Name = "",
    1121             :                    bool HasNUW = false, bool HasNSW = false) {
    1122          65 :     if (Constant *VC = dyn_cast<Constant>(V))
    1123         130 :       return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
    1124         554 :     BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
    1125         377 :     if (HasNUW) BO->setHasNoUnsignedWrap();
    1126         377 :     if (HasNSW) BO->setHasNoSignedWrap();
    1127             :     return BO;
    1128             :   }
    1129             :   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
    1130           0 :     return CreateNeg(V, Name, false, true);
    1131             :   }
    1132             :   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
    1133           0 :     return CreateNeg(V, Name, true, false);
    1134             :   }
    1135          42 :   Value *CreateFNeg(Value *V, const Twine &Name = "",
    1136             :                     MDNode *FPMathTag = nullptr) {
    1137          13 :     if (Constant *VC = dyn_cast<Constant>(V))
    1138          25 :       return Insert(Folder.CreateFNeg(VC), Name);
    1139          58 :     return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
    1140          29 :                                       FPMathTag, FMF), Name);
    1141             :   }
    1142        8849 :   Value *CreateNot(Value *V, const Twine &Name = "") {
    1143         685 :     if (Constant *VC = dyn_cast<Constant>(V))
    1144        1305 :       return Insert(Folder.CreateNot(VC), Name);
    1145        8547 :     return Insert(BinaryOperator::CreateNot(V), Name);
    1146             :   }
    1147             : 
    1148             :   //===--------------------------------------------------------------------===//
    1149             :   // Instruction creation methods: Memory Instructions
    1150             :   //===--------------------------------------------------------------------===//
    1151             : 
    1152             :   AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
    1153             :                            Value *ArraySize = nullptr, const Twine &Name = "") {
    1154             :     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
    1155             :   }
    1156             : 
    1157        1377 :   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
    1158             :                            const Twine &Name = "") {
    1159        1377 :     const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
    1160        4947 :     return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
    1161             :   }
    1162             :   // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
    1163             :   // converting the string to 'bool' for the isVolatile parameter.
    1164       94138 :   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
    1165      284552 :     return Insert(new LoadInst(Ptr), Name);
    1166             :   }
    1167      210907 :   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
    1168      585891 :     return Insert(new LoadInst(Ptr), Name);
    1169             :   }
    1170         957 :   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
    1171        1914 :     return Insert(new LoadInst(Ty, Ptr), Name);
    1172             :   }
    1173      320196 :   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
    1174      641936 :     return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
    1175             :   }
    1176      873654 :   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
    1177     1960392 :     return Insert(new StoreInst(Val, Ptr, isVolatile));
    1178             :   }
    1179             :   // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
    1180             :   // correctly, instead of converting the string to 'bool' for the isVolatile
    1181             :   // parameter.
    1182             :   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
    1183       92977 :     LoadInst *LI = CreateLoad(Ptr, Name);
    1184       92977 :     LI->setAlignment(Align);
    1185             :     return LI;
    1186             :   }
    1187             :   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
    1188             :                               const Twine &Name = "") {
    1189       46221 :     LoadInst *LI = CreateLoad(Ptr, Name);
    1190       46221 :     LI->setAlignment(Align);
    1191             :     return LI;
    1192             :   }
    1193             :   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
    1194             :                               const Twine &Name = "") {
    1195      317761 :     LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
    1196      317761 :     LI->setAlignment(Align);
    1197             :     return LI;
    1198             :   }
    1199             :   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
    1200             :                                 bool isVolatile = false) {
    1201      659645 :     StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
    1202      659645 :     SI->setAlignment(Align);
    1203             :     return SI;
    1204             :   }
    1205         201 :   FenceInst *CreateFence(AtomicOrdering Ordering,
    1206             :                          SyncScope::ID SSID = SyncScope::System,
    1207             :                          const Twine &Name = "") {
    1208         366 :     return Insert(new FenceInst(Context, Ordering, SSID), Name);
    1209             :   }
    1210             :   AtomicCmpXchgInst *
    1211         801 :   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
    1212             :                       AtomicOrdering SuccessOrdering,
    1213             :                       AtomicOrdering FailureOrdering,
    1214             :                       SyncScope::ID SSID = SyncScope::System) {
    1215        2403 :     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
    1216        1602 :                                         FailureOrdering, SSID));
    1217             :   }
    1218         669 :   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
    1219             :                                  AtomicOrdering Ordering,
    1220             :                                  SyncScope::ID SSID = SyncScope::System) {
    1221        1385 :     return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID));
    1222             :   }
    1223             :   Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
    1224             :                    const Twine &Name = "") {
    1225          82 :     return CreateGEP(nullptr, Ptr, IdxList, Name);
    1226             :   }
    1227        7277 :   Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
    1228             :                    const Twine &Name = "") {
    1229         306 :     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
    1230             :       // Every index must be constant.
    1231             :       size_t i, e;
    1232        1167 :       for (i = 0, e = IdxList.size(); i != e; ++i)
    1233        1550 :         if (!isa<Constant>(IdxList[i]))
    1234             :           break;
    1235         381 :       if (i == e)
    1236         283 :         return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
    1237             :     }
    1238       14063 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
    1239             :   }
    1240             :   Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
    1241             :                            const Twine &Name = "") {
    1242       26532 :     return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
    1243             :   }
    1244      219171 :   Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
    1245             :                            const Twine &Name = "") {
    1246             :     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
    1247             :       // Every index must be constant.
    1248             :       size_t i, e;
    1249      915505 :       for (i = 0, e = IdxList.size(); i != e; ++i)
    1250      759140 :         if (!isa<Constant>(IdxList[i]))
    1251             :           break;
    1252      189883 :       if (i == e)
    1253      195322 :         return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
    1254      173124 :                       Name);
    1255             :     }
    1256      105683 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
    1257             :   }
    1258             :   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
    1259        4703 :     return CreateGEP(nullptr, Ptr, Idx, Name);
    1260             :   }
    1261       23488 :   Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
    1262         754 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1263        1114 :       if (Constant *IC = dyn_cast<Constant>(Idx))
    1264         720 :         return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
    1265       65718 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
    1266             :   }
    1267         319 :   Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
    1268             :                            const Twine &Name = "") {
    1269           2 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1270           2 :       if (Constant *IC = dyn_cast<Constant>(Idx))
    1271           0 :         return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
    1272         730 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
    1273             :   }
    1274             :   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
    1275         781 :     return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
    1276             :   }
    1277         894 :   Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
    1278             :                             const Twine &Name = "") {
    1279         894 :     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
    1280             : 
    1281          16 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1282          32 :       return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
    1283             : 
    1284        1882 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
    1285             :   }
    1286         566 :   Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
    1287             :                                     const Twine &Name = "") {
    1288         566 :     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
    1289             : 
    1290         205 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1291         410 :       return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
    1292             : 
    1293         725 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
    1294             :   }
    1295         658 :   Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
    1296             :                             const Twine &Name = "") {
    1297        1316 :     Value *Idxs[] = {
    1298         658 :       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
    1299         658 :       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
    1300             :     };
    1301             : 
    1302           0 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1303           0 :       return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
    1304             : 
    1305        1840 :     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
    1306             :   }
    1307      100631 :   Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
    1308             :                                     unsigned Idx1, const Twine &Name = "") {
    1309      201262 :     Value *Idxs[] = {
    1310      100631 :       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
    1311      100631 :       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
    1312             :     };
    1313             : 
    1314       24174 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1315       48348 :       return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
    1316             : 
    1317      153463 :     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
    1318             :   }
    1319         667 :   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
    1320         667 :     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
    1321             : 
    1322          80 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1323         160 :       return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
    1324             : 
    1325        1174 :     return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
    1326             :   }
    1327        1906 :   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
    1328             :                                     const Twine &Name = "") {
    1329        1906 :     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
    1330             : 
    1331          16 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1332          32 :       return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
    1333             : 
    1334        3862 :     return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
    1335             :   }
    1336       20821 :   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
    1337             :                     const Twine &Name = "") {
    1338       41642 :     Value *Idxs[] = {
    1339       20821 :       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
    1340       20821 :       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
    1341             :     };
    1342             : 
    1343       20821 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1344       41642 :       return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
    1345             : 
    1346           0 :     return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
    1347             :   }
    1348      170054 :   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
    1349             :                                     const Twine &Name = "") {
    1350      340108 :     Value *Idxs[] = {
    1351      170054 :       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
    1352      170054 :       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
    1353             :     };
    1354             : 
    1355      170054 :     if (Constant *PC = dyn_cast<Constant>(Ptr))
    1356      340108 :       return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
    1357      170054 :                     Name);
    1358             : 
    1359           0 :     return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
    1360             :   }
    1361             :   Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
    1362             :                          const Twine &Name = "") {
    1363       85790 :     return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
    1364             :   }
    1365             : 
    1366             :   /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
    1367             :   /// instead of a pointer to array of i8.
    1368       21456 :   Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
    1369             :                                unsigned AddressSpace = 0) {
    1370       21456 :     GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
    1371       21456 :     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
    1372       21456 :     Value *Args[] = { zero, zero };
    1373       21456 :     return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
    1374             :   }
    1375             : 
    1376             :   //===--------------------------------------------------------------------===//
    1377             :   // Instruction creation methods: Cast/Conversion Operators
    1378             :   //===--------------------------------------------------------------------===//
    1379             : 
    1380             :   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
    1381        3406 :     return CreateCast(Instruction::Trunc, V, DestTy, Name);
    1382             :   }
    1383             :   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
    1384       12848 :     return CreateCast(Instruction::ZExt, V, DestTy, Name);
    1385             :   }
    1386             :   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
    1387        2636 :     return CreateCast(Instruction::SExt, V, DestTy, Name);
    1388             :   }
    1389             :   /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
    1390             :   /// the value untouched if the type of V is already DestTy.
    1391         531 :   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
    1392             :                            const Twine &Name = "") {
    1393             :     assert(V->getType()->isIntOrIntVectorTy() &&
    1394             :            DestTy->isIntOrIntVectorTy() &&
    1395             :            "Can only zero extend/truncate integers!");
    1396         531 :     Type *VTy = V->getType();
    1397         531 :     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
    1398         239 :       return CreateZExt(V, DestTy, Name);
    1399         292 :     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
    1400         147 :       return CreateTrunc(V, DestTy, Name);
    1401             :     return V;
    1402             :   }
    1403             :   /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
    1404             :   /// the value untouched if the type of V is already DestTy.
    1405        1247 :   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
    1406             :                            const Twine &Name = "") {
    1407             :     assert(V->getType()->isIntOrIntVectorTy() &&
    1408             :            DestTy->isIntOrIntVectorTy() &&
    1409             :            "Can only sign extend/truncate integers!");
    1410        1247 :     Type *VTy = V->getType();
    1411        1247 :     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
    1412         425 :       return CreateSExt(V, DestTy, Name);
    1413         822 :     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
    1414          65 :       return CreateTrunc(V, DestTy, Name);
    1415             :     return V;
    1416             :   }
    1417             :   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
    1418         103 :     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
    1419             :   }
    1420             :   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
    1421         496 :     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
    1422             :   }
    1423             :   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
    1424         171 :     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
    1425             :   }
    1426             :   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
    1427        2236 :     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
    1428             :   }
    1429             :   Value *CreateFPTrunc(Value *V, Type *DestTy,
    1430             :                        const Twine &Name = "") {
    1431         837 :     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
    1432             :   }
    1433             :   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
    1434         887 :     return CreateCast(Instruction::FPExt, V, DestTy, Name);
    1435             :   }
    1436             :   Value *CreatePtrToInt(Value *V, Type *DestTy,
    1437             :                         const Twine &Name = "") {
    1438        5934 :     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
    1439             :   }
    1440             :   Value *CreateIntToPtr(Value *V, Type *DestTy,
    1441             :                         const Twine &Name = "") {
    1442        5926 :     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
    1443             :   }
    1444             :   Value *CreateBitCast(Value *V, Type *DestTy,
    1445             :                        const Twine &Name = "") {
    1446      564608 :     return CreateCast(Instruction::BitCast, V, DestTy, Name);
    1447             :   }
    1448             :   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
    1449             :                              const Twine &Name = "") {
    1450          16 :     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
    1451             :   }
    1452        1673 :   Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
    1453             :                              const Twine &Name = "") {
    1454        1673 :     if (V->getType() == DestTy)
    1455             :       return V;
    1456          75 :     if (Constant *VC = dyn_cast<Constant>(V))
    1457         150 :       return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
    1458         651 :     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
    1459             :   }
    1460         481 :   Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
    1461             :                              const Twine &Name = "") {
    1462         481 :     if (V->getType() == DestTy)
    1463             :       return V;
    1464           0 :     if (Constant *VC = dyn_cast<Constant>(V))
    1465           0 :       return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
    1466         190 :     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
    1467             :   }
    1468        1052 :   Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
    1469             :                               const Twine &Name = "") {
    1470        1052 :     if (V->getType() == DestTy)
    1471             :       return V;
    1472          36 :     if (Constant *VC = dyn_cast<Constant>(V))
    1473          72 :       return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
    1474        1406 :     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
    1475             :   }
    1476      613722 :   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
    1477             :                     const Twine &Name = "") {
    1478      613722 :     if (V->getType() == DestTy)
    1479             :       return V;
    1480      163039 :     if (Constant *VC = dyn_cast<Constant>(V))
    1481      313310 :       return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
    1482      256994 :     return Insert(CastInst::Create(Op, V, DestTy), Name);
    1483             :   }
    1484       31307 :   Value *CreatePointerCast(Value *V, Type *DestTy,
    1485             :                            const Twine &Name = "") {
    1486       31307 :     if (V->getType() == DestTy)
    1487             :       return V;
    1488        1842 :     if (Constant *VC = dyn_cast<Constant>(V))
    1489        3684 :       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
    1490       56429 :     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
    1491             :   }
    1492             : 
    1493       10900 :   Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
    1494             :                                              const Twine &Name = "") {
    1495       10900 :     if (V->getType() == DestTy)
    1496             :       return V;
    1497             : 
    1498         381 :     if (Constant *VC = dyn_cast<Constant>(V)) {
    1499         762 :       return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
    1500         381 :                     Name);
    1501             :     }
    1502             : 
    1503       16612 :     return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
    1504        8306 :                   Name);
    1505             :   }
    1506             : 
    1507       81435 :   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
    1508             :                        const Twine &Name = "") {
    1509       81435 :     if (V->getType() == DestTy)
    1510             :       return V;
    1511       52965 :     if (Constant *VC = dyn_cast<Constant>(V))
    1512       77457 :       return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
    1513       24001 :     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
    1514             :   }
    1515             : 
    1516        4099 :   Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
    1517             :                                 const Twine &Name = "") {
    1518        4099 :     if (V->getType() == DestTy)
    1519             :       return V;
    1520        4496 :     if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
    1521          58 :       return CreatePtrToInt(V, DestTy, Name);
    1522        3465 :     if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
    1523         329 :       return CreateIntToPtr(V, DestTy, Name);
    1524             : 
    1525        1232 :     return CreateBitCast(V, DestTy, Name);
    1526             :   }
    1527             : 
    1528             : public:
    1529           5 :   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
    1530           5 :     if (V->getType() == DestTy)
    1531             :       return V;
    1532           0 :     if (Constant *VC = dyn_cast<Constant>(V))
    1533           0 :       return Insert(Folder.CreateFPCast(VC, DestTy), Name);
    1534           5 :     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
    1535             :   }
    1536             : 
    1537             :   // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
    1538             :   // compile time error, instead of converting the string to bool for the
    1539             :   // isSigned parameter.
    1540             :   Value *CreateIntCast(Value *, Type *, const char *) = delete;
    1541             : 
    1542             :   //===--------------------------------------------------------------------===//
    1543             :   // Instruction creation methods: Compare Instructions
    1544             :   //===--------------------------------------------------------------------===//
    1545             : 
    1546             :   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
    1547        6282 :     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
    1548             :   }
    1549             :   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1550       13023 :     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
    1551             :   }
    1552             :   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1553         150 :     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
    1554             :   }
    1555             :   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1556         155 :     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
    1557             :   }
    1558             :   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1559        1459 :     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
    1560             :   }
    1561             :   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1562         209 :     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
    1563             :   }
    1564             :   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1565         306 :     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
    1566             :   }
    1567             :   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1568         246 :     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
    1569             :   }
    1570             :   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
    1571         248 :     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
    1572             :   }
    1573             :   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
    1574         103 :     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
    1575             :   }
    1576             : 
    1577             :   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
    1578             :                        MDNode *FPMathTag = nullptr) {
    1579          57 :     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
    1580             :   }
    1581             :   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
    1582             :                        MDNode *FPMathTag = nullptr) {
    1583          24 :     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
    1584             :   }
    1585             :   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
    1586             :                        MDNode *FPMathTag = nullptr) {
    1587          10 :     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
    1588             :   }
    1589             :   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
    1590             :                        MDNode *FPMathTag = nullptr) {
    1591          25 :     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
    1592             :   }
    1593             :   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
    1594             :                        MDNode *FPMathTag = nullptr) {
    1595          10 :     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
    1596             :   }
    1597             :   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
    1598             :                        MDNode *FPMathTag = nullptr) {
    1599           6 :     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
    1600             :   }
    1601             :   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
    1602             :                        MDNode *FPMathTag = nullptr) {
    1603             :     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
    1604             :   }
    1605             :   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
    1606             :                        MDNode *FPMathTag = nullptr) {
    1607         124 :     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
    1608             :   }
    1609             :   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
    1610             :                        MDNode *FPMathTag = nullptr) {
    1611             :     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
    1612             :   }
    1613             :   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
    1614             :                        MDNode *FPMathTag = nullptr) {
    1615           4 :     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
    1616             :   }
    1617             :   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
    1618             :                        MDNode *FPMathTag = nullptr) {
    1619           6 :     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
    1620             :   }
    1621             :   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
    1622             :                        MDNode *FPMathTag = nullptr) {
    1623           6 :     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
    1624             :   }
    1625             :   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
    1626             :                        MDNode *FPMathTag = nullptr) {
    1627           4 :     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
    1628             :   }
    1629             :   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
    1630             :                        MDNode *FPMathTag = nullptr) {
    1631          77 :     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
    1632             :   }
    1633             : 
    1634       42755 :   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
    1635             :                     const Twine &Name = "") {
    1636        3086 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1637        2247 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1638        4353 :         return Insert(Folder.CreateICmp(P, LC, RC), Name);
    1639       89719 :     return Insert(new ICmpInst(P, LHS, RHS), Name);
    1640             :   }
    1641        1645 :   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
    1642             :                     const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1643         105 :     if (Constant *LC = dyn_cast<Constant>(LHS))
    1644          69 :       if (Constant *RC = dyn_cast<Constant>(RHS))
    1645         138 :         return Insert(Folder.CreateFCmp(P, LC, RC), Name);
    1646        4728 :     return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS),
    1647        1576 :                                       FPMathTag, FMF), Name);
    1648             :   }
    1649             : 
    1650             :   //===--------------------------------------------------------------------===//
    1651             :   // Instruction creation methods: Other Instructions
    1652             :   //===--------------------------------------------------------------------===//
    1653             : 
    1654       10120 :   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
    1655             :                      const Twine &Name = "") {
    1656       25753 :     return Insert(PHINode::Create(Ty, NumReservedValues), Name);
    1657             :   }
    1658             : 
    1659             :   CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
    1660             :                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1661      332006 :     PointerType *PTy = cast<PointerType>(Callee->getType());
    1662      332006 :     FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
    1663      166003 :     return CreateCall(FTy, Callee, Args, Name, FPMathTag);
    1664             :   }
    1665             : 
    1666      192219 :   CallInst *CreateCall(FunctionType *FTy, Value *Callee,
    1667             :                        ArrayRef<Value *> Args, const Twine &Name = "",
    1668             :                        MDNode *FPMathTag = nullptr) {
    1669      192219 :     CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
    1670      384438 :     if (isa<FPMathOperator>(CI))
    1671        7366 :       CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF));
    1672      285851 :     return Insert(CI, Name);
    1673             :   }
    1674             : 
    1675      154434 :   CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
    1676             :                        ArrayRef<OperandBundleDef> OpBundles,
    1677             :                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1678      308868 :     CallInst *CI = CallInst::Create(Callee, Args, OpBundles);
    1679      308868 :     if (isa<FPMathOperator>(CI))
    1680       10620 :       CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF));
    1681      155107 :     return Insert(CI, Name);
    1682             :   }
    1683             : 
    1684             :   CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
    1685             :                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
    1686       26216 :     return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag);
    1687             :   }
    1688             : 
    1689       20680 :   Value *CreateSelect(Value *C, Value *True, Value *False,
    1690             :                       const Twine &Name = "", Instruction *MDFrom = nullptr) {
    1691         701 :     if (Constant *CC = dyn_cast<Constant>(C))
    1692         296 :       if (Constant *TC = dyn_cast<Constant>(True))
    1693         285 :         if (Constant *FC = dyn_cast<Constant>(False))
    1694         508 :           return Insert(Folder.CreateSelect(CC, TC, FC), Name);
    1695             : 
    1696       40790 :     SelectInst *Sel = SelectInst::Create(C, True, False);
    1697       20395 :     if (MDFrom) {
    1698         843 :       MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
    1699         843 :       MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
    1700         843 :       Sel = addBranchMetadata(Sel, Prof, Unpred);
    1701             :     }
    1702       20395 :     return Insert(Sel, Name);
    1703             :   }
    1704             : 
    1705           9 :   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
    1706          27 :     return Insert(new VAArgInst(List, Ty), Name);
    1707             :   }
    1708             : 
    1709       11414 :   Value *CreateExtractElement(Value *Vec, Value *Idx,
    1710             :                               const Twine &Name = "") {
    1711         131 :     if (Constant *VC = dyn_cast<Constant>(Vec))
    1712         131 :       if (Constant *IC = dyn_cast<Constant>(Idx))
    1713         234 :         return Insert(Folder.CreateExtractElement(VC, IC), Name);
    1714       31875 :     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
    1715             :   }
    1716             : 
    1717             :   Value *CreateExtractElement(Value *Vec, uint64_t Idx,
    1718             :                               const Twine &Name = "") {
    1719         168 :     return CreateExtractElement(Vec, getInt64(Idx), Name);
    1720             :   }
    1721             : 
    1722      156863 :   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
    1723             :                              const Twine &Name = "") {
    1724      150430 :     if (Constant *VC = dyn_cast<Constant>(Vec))
    1725      148029 :       if (Constant *NC = dyn_cast<Constant>(NewElt))
    1726      148029 :         if (Constant *IC = dyn_cast<Constant>(Idx))
    1727      295541 :           return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
    1728       20706 :     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
    1729             :   }
    1730             : 
    1731             :   Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
    1732             :                              const Twine &Name = "") {
    1733         106 :     return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
    1734             :   }
    1735             : 
    1736       10500 :   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
    1737             :                              const Twine &Name = "") {
    1738        2856 :     if (Constant *V1C = dyn_cast<Constant>(V1))
    1739        2827 :       if (Constant *V2C = dyn_cast<Constant>(V2))
    1740        2827 :         if (Constant *MC = dyn_cast<Constant>(Mask))
    1741        5514 :           return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
    1742       19249 :     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
    1743             :   }
    1744             : 
    1745        2841 :   Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask,
    1746             :                              const Twine &Name = "") {
    1747        2841 :     Value *Mask = ConstantDataVector::get(Context, IntMask);
    1748        2841 :     return CreateShuffleVector(V1, V2, Mask, Name);
    1749             :   }
    1750             : 
    1751      203210 :   Value *CreateExtractValue(Value *Agg,
    1752             :                             ArrayRef<unsigned> Idxs,
    1753             :                             const Twine &Name = "") {
    1754      151221 :     if (Constant *AggC = dyn_cast<Constant>(Agg))
    1755      151431 :       return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
    1756       53414 :     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
    1757             :   }
    1758             : 
    1759        8520 :   Value *CreateInsertValue(Value *Agg, Value *Val,
    1760             :                            ArrayRef<unsigned> Idxs,
    1761             :                            const Twine &Name = "") {
    1762        4086 :     if (Constant *AggC = dyn_cast<Constant>(Agg))
    1763          46 :       if (Constant *ValC = dyn_cast<Constant>(Val))
    1764          92 :         return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
    1765        8653 :     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
    1766             :   }
    1767             : 
    1768       23037 :   LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
    1769             :                                    const Twine &Name = "") {
    1770       23039 :     return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
    1771             :   }
    1772             : 
    1773             :   //===--------------------------------------------------------------------===//
    1774             :   // Utility creation methods
    1775             :   //===--------------------------------------------------------------------===//
    1776             : 
    1777             :   /// \brief Return an i1 value testing if \p Arg is null.
    1778        1415 :   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
    1779        1415 :     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
    1780        1415 :                         Name);
    1781             :   }
    1782             : 
    1783             :   /// \brief Return an i1 value testing if \p Arg is not null.
    1784       10570 :   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
    1785       10570 :     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
    1786       10570 :                         Name);
    1787             :   }
    1788             : 
    1789             :   /// \brief Return the i64 difference between two pointer values, dividing out
    1790             :   /// the size of the pointed-to objects.
    1791             :   ///
    1792             :   /// This is intended to implement C-style pointer subtraction. As such, the
    1793             :   /// pointers must be appropriately aligned for their element types and
    1794             :   /// pointing into the same object.
    1795          46 :   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
    1796             :     assert(LHS->getType() == RHS->getType() &&
    1797             :            "Pointer subtraction operand types must match!");
    1798          92 :     PointerType *ArgType = cast<PointerType>(LHS->getType());
    1799          92 :     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
    1800          92 :     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
    1801          46 :     Value *Difference = CreateSub(LHS_int, RHS_int);
    1802             :     return CreateExactSDiv(Difference,
    1803          46 :                            ConstantExpr::getSizeOf(ArgType->getElementType()),
    1804          46 :                            Name);
    1805             :   }
    1806             : 
    1807             :   /// \brief Create an invariant.group.barrier intrinsic call, that stops
    1808             :   /// optimizer to propagate equality using invariant.group metadata.
    1809             :   /// If Ptr type is different from i8*, it's casted to i8* before call
    1810             :   /// and casted back to Ptr type after call.
    1811          20 :   Value *CreateInvariantGroupBarrier(Value *Ptr) {
    1812          20 :     Module *M = BB->getParent()->getParent();
    1813          20 :     Function *FnInvariantGroupBarrier = Intrinsic::getDeclaration(M,
    1814             :             Intrinsic::invariant_group_barrier);
    1815             : 
    1816          20 :     Type *ArgumentAndReturnType = FnInvariantGroupBarrier->getReturnType();
    1817             :     assert(ArgumentAndReturnType ==
    1818             :         FnInvariantGroupBarrier->getFunctionType()->getParamType(0) &&
    1819             :         "InvariantGroupBarrier should take and return the same type");
    1820          20 :     Type *PtrType = Ptr->getType();
    1821             : 
    1822          20 :     bool PtrTypeConversionNeeded = PtrType != ArgumentAndReturnType;
    1823          20 :     if (PtrTypeConversionNeeded)
    1824          40 :       Ptr = CreateBitCast(Ptr, ArgumentAndReturnType);
    1825             : 
    1826          60 :     CallInst *Fn = CreateCall(FnInvariantGroupBarrier, {Ptr});
    1827             : 
    1828          20 :     if (PtrTypeConversionNeeded)
    1829          40 :       return CreateBitCast(Fn, PtrType);
    1830             :     return Fn;
    1831             :   }
    1832             : 
    1833             :   /// \brief Return a vector value that contains \arg V broadcasted to \p
    1834             :   /// NumElts elements.
    1835        3708 :   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
    1836             :     assert(NumElts > 0 && "Cannot splat to an empty vector!");
    1837             : 
    1838             :     // First insert it into an undef vector so we can shuffle it.
    1839        7416 :     Type *I32Ty = getInt32Ty();
    1840        3708 :     Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
    1841        3708 :     V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
    1842       11124 :                             Name + ".splatinsert");
    1843             : 
    1844             :     // Shuffle the value across the desired number of elements.
    1845        3708 :     Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
    1846        7416 :     return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
    1847             :   }
    1848             : 
    1849             :   /// \brief Return a value that has been extracted from a larger integer type.
    1850             :   Value *CreateExtractInteger(const DataLayout &DL, Value *From,
    1851             :                               IntegerType *ExtractedTy, uint64_t Offset,
    1852             :                               const Twine &Name) {
    1853             :     IntegerType *IntTy = cast<IntegerType>(From->getType());
    1854             :     assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
    1855             :                DL.getTypeStoreSize(IntTy) &&
    1856             :            "Element extends past full value");
    1857             :     uint64_t ShAmt = 8 * Offset;
    1858             :     Value *V = From;
    1859             :     if (DL.isBigEndian())
    1860             :       ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
    1861             :                    DL.getTypeStoreSize(ExtractedTy) - Offset);
    1862             :     if (ShAmt) {
    1863             :       V = CreateLShr(V, ShAmt, Name + ".shift");
    1864             :     }
    1865             :     assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
    1866             :            "Cannot extract to a larger integer!");
    1867             :     if (ExtractedTy != IntTy) {
    1868             :       V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
    1869             :     }
    1870             :     return V;
    1871             :   }
    1872             : 
    1873             : private:
    1874             :   /// \brief Helper function that creates an assume intrinsic call that
    1875             :   /// represents an alignment assumption on the provided Ptr, Mask, Type
    1876             :   /// and Offset.
    1877         117 :   CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
    1878             :                                             Value *PtrValue, Value *Mask,
    1879             :                                             Type *IntPtrTy,
    1880             :                                             Value *OffsetValue) {
    1881         234 :     Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
    1882             : 
    1883         117 :     if (OffsetValue) {
    1884           4 :       bool IsOffsetZero = false;
    1885           3 :       if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
    1886             :         IsOffsetZero = CI->isZero();
    1887             : 
    1888           4 :       if (!IsOffsetZero) {
    1889           2 :         if (OffsetValue->getType() != IntPtrTy)
    1890           1 :           OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
    1891             :                                       "offsetcast");
    1892           2 :         PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
    1893             :       }
    1894             :     }
    1895             : 
    1896         117 :     Value *Zero = ConstantInt::get(IntPtrTy, 0);
    1897         117 :     Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
    1898         234 :     Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
    1899         117 :     return CreateAssumption(InvCond);
    1900             :   }
    1901             : 
    1902             : public:
    1903             :   /// \brief Create an assume intrinsic call that represents an alignment
    1904             :   /// assumption on the provided pointer.
    1905             :   ///
    1906             :   /// An optional offset can be provided, and if it is provided, the offset
    1907             :   /// must be subtracted from the provided pointer to get the pointer with the
    1908             :   /// specified alignment.
    1909         111 :   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
    1910             :                                       unsigned Alignment,
    1911             :                                       Value *OffsetValue = nullptr) {
    1912             :     assert(isa<PointerType>(PtrValue->getType()) &&
    1913             :            "trying to create an alignment assumption on a non-pointer?");
    1914         222 :     PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
    1915         333 :     Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
    1916             : 
    1917         111 :     Value *Mask = ConstantInt::get(IntPtrTy, Alignment > 0 ? Alignment - 1 : 0);
    1918             :     return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
    1919         111 :                                            OffsetValue);
    1920             :   }
    1921             :   //
    1922             :   /// \brief Create an assume intrinsic call that represents an alignment
    1923             :   /// assumption on the provided pointer.
    1924             :   ///
    1925             :   /// An optional offset can be provided, and if it is provided, the offset
    1926             :   /// must be subtracted from the provided pointer to get the pointer with the
    1927             :   /// specified alignment.
    1928             :   ///
    1929             :   /// This overload handles the condition where the Alignment is dependent
    1930             :   /// on an existing value rather than a static value.
    1931           6 :   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
    1932             :                                       Value *Alignment,
    1933             :                                       Value *OffsetValue = nullptr) {
    1934             :     assert(isa<PointerType>(PtrValue->getType()) &&
    1935             :            "trying to create an alignment assumption on a non-pointer?");
    1936          12 :     PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
    1937          18 :     Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
    1938             : 
    1939           6 :     if (Alignment->getType() != IntPtrTy)
    1940           4 :       Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ true,
    1941             :                                 "alignmentcast");
    1942           6 :     Value *IsPositive =
    1943          12 :         CreateICmp(CmpInst::ICMP_SGT, Alignment,
    1944           6 :                    ConstantInt::get(Alignment->getType(), 0), "ispositive");
    1945           6 :     Value *PositiveMask =
    1946          12 :         CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "positivemask");
    1947          12 :     Value *Mask = CreateSelect(IsPositive, PositiveMask,
    1948           6 :                                ConstantInt::get(IntPtrTy, 0), "mask");
    1949             : 
    1950             :     return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
    1951           6 :                                            OffsetValue);
    1952             :   }
    1953             : };
    1954             : 
    1955             : // Create wrappers for C Binding types (see CBindingWrapping.h).
    1956             : DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
    1957             : 
    1958             : } // end namespace llvm
    1959             : 
    1960             : #endif // LLVM_IR_IRBUILDER_H

Generated by: LCOV version 1.13