LLVM API Documentation

IRBuilder.h
Go to the documentation of this file.
00001 //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the IRBuilder class, which is used as a convenient way
00011 // to create LLVM instructions with a consistent and simplified interface.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_IR_IRBUILDER_H
00016 #define LLVM_IR_IRBUILDER_H
00017 
00018 #include "llvm/ADT/ArrayRef.h"
00019 #include "llvm/ADT/StringRef.h"
00020 #include "llvm/ADT/Twine.h"
00021 #include "llvm/IR/BasicBlock.h"
00022 #include "llvm/IR/ConstantFolder.h"
00023 #include "llvm/IR/DataLayout.h"
00024 #include "llvm/IR/Instructions.h"
00025 #include "llvm/IR/LLVMContext.h"
00026 #include "llvm/IR/Operator.h"
00027 #include "llvm/IR/ValueHandle.h"
00028 #include "llvm/Support/CBindingWrapping.h"
00029 
00030 namespace llvm {
00031   class MDNode;
00032 
00033 /// \brief This provides the default implementation of the IRBuilder
00034 /// 'InsertHelper' method that is called whenever an instruction is created by
00035 /// IRBuilder and needs to be inserted.
00036 ///
00037 /// By default, this inserts the instruction at the insertion point.
00038 template <bool preserveNames = true>
00039 class IRBuilderDefaultInserter {
00040 protected:
00041   void InsertHelper(Instruction *I, const Twine &Name,
00042                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
00043     if (BB) BB->getInstList().insert(InsertPt, I);
00044     if (preserveNames)
00045       I->setName(Name);
00046   }
00047 };
00048 
00049 /// \brief Common base class shared among various IRBuilders.
00050 class IRBuilderBase {
00051   DebugLoc CurDbgLocation;
00052 protected:
00053   BasicBlock *BB;
00054   BasicBlock::iterator InsertPt;
00055   LLVMContext &Context;
00056 
00057   MDNode *DefaultFPMathTag;
00058   FastMathFlags FMF;
00059 public:
00060 
00061   IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr)
00062     : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
00063     ClearInsertionPoint();
00064   }
00065 
00066   //===--------------------------------------------------------------------===//
00067   // Builder configuration methods
00068   //===--------------------------------------------------------------------===//
00069 
00070   /// \brief Clear the insertion point: created instructions will not be
00071   /// inserted into a block.
00072   void ClearInsertionPoint() {
00073     BB = nullptr;
00074     InsertPt = nullptr;
00075   }
00076 
00077   BasicBlock *GetInsertBlock() const { return BB; }
00078   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
00079   LLVMContext &getContext() const { return Context; }
00080 
00081   /// \brief This specifies that created instructions should be appended to the
00082   /// end of the specified block.
00083   void SetInsertPoint(BasicBlock *TheBB) {
00084     BB = TheBB;
00085     InsertPt = BB->end();
00086   }
00087 
00088   /// \brief This specifies that created instructions should be inserted before
00089   /// the specified instruction.
00090   void SetInsertPoint(Instruction *I) {
00091     BB = I->getParent();
00092     InsertPt = I;
00093     assert(I != BB->end() && "Can't read debug loc from end()");
00094     SetCurrentDebugLocation(I->getDebugLoc());
00095   }
00096 
00097   /// \brief This specifies that created instructions should be inserted at the
00098   /// specified point.
00099   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
00100     BB = TheBB;
00101     InsertPt = IP;
00102   }
00103 
00104   /// \brief Find the nearest point that dominates this use, and specify that
00105   /// created instructions should be inserted at this point.
00106   void SetInsertPoint(Use &U) {
00107     Instruction *UseInst = cast<Instruction>(U.getUser());
00108     if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
00109       BasicBlock *PredBB = Phi->getIncomingBlock(U);
00110       assert(U != PredBB->getTerminator() && "critical edge not split");
00111       SetInsertPoint(PredBB, PredBB->getTerminator());
00112       return;
00113     }
00114     SetInsertPoint(UseInst);
00115   }
00116 
00117   /// \brief Set location information used by debugging information.
00118   void SetCurrentDebugLocation(const DebugLoc &L) {
00119     CurDbgLocation = L;
00120   }
00121 
00122   /// \brief Get location information used by debugging information.
00123   DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
00124 
00125   /// \brief If this builder has a current debug location, set it on the
00126   /// specified instruction.
00127   void SetInstDebugLocation(Instruction *I) const {
00128     if (!CurDbgLocation.isUnknown())
00129       I->setDebugLoc(CurDbgLocation);
00130   }
00131 
00132   /// \brief Get the return type of the current function that we're emitting
00133   /// into.
00134   Type *getCurrentFunctionReturnType() const;
00135 
00136   /// InsertPoint - A saved insertion point.
00137   class InsertPoint {
00138     BasicBlock *Block;
00139     BasicBlock::iterator Point;
00140 
00141   public:
00142     /// \brief Creates a new insertion point which doesn't point to anything.
00143     InsertPoint() : Block(nullptr) {}
00144 
00145     /// \brief Creates a new insertion point at the given location.
00146     InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
00147       : Block(InsertBlock), Point(InsertPoint) {}
00148 
00149     /// \brief Returns true if this insert point is set.
00150     bool isSet() const { return (Block != nullptr); }
00151 
00152     llvm::BasicBlock *getBlock() const { return Block; }
00153     llvm::BasicBlock::iterator getPoint() const { return Point; }
00154   };
00155 
00156   /// \brief Returns the current insert point.
00157   InsertPoint saveIP() const {
00158     return InsertPoint(GetInsertBlock(), GetInsertPoint());
00159   }
00160 
00161   /// \brief Returns the current insert point, clearing it in the process.
00162   InsertPoint saveAndClearIP() {
00163     InsertPoint IP(GetInsertBlock(), GetInsertPoint());
00164     ClearInsertionPoint();
00165     return IP;
00166   }
00167 
00168   /// \brief Sets the current insert point to a previously-saved location.
00169   void restoreIP(InsertPoint IP) {
00170     if (IP.isSet())
00171       SetInsertPoint(IP.getBlock(), IP.getPoint());
00172     else
00173       ClearInsertionPoint();
00174   }
00175 
00176   /// \brief Get the floating point math metadata being used.
00177   MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
00178 
00179   /// \brief Get the flags to be applied to created floating point ops
00180   FastMathFlags getFastMathFlags() const { return FMF; }
00181 
00182   /// \brief Clear the fast-math flags.
00183   void clearFastMathFlags() { FMF.clear(); }
00184 
00185   /// \brief Set the floating point math metadata to be used.
00186   void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
00187 
00188   /// \brief Set the fast-math flags to be used with generated fp-math operators
00189   void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
00190 
00191   //===--------------------------------------------------------------------===//
00192   // RAII helpers.
00193   //===--------------------------------------------------------------------===//
00194 
00195   // \brief RAII object that stores the current insertion point and restores it
00196   // when the object is destroyed. This includes the debug location.
00197   class InsertPointGuard {
00198     IRBuilderBase &Builder;
00199     AssertingVH<BasicBlock> Block;
00200     BasicBlock::iterator Point;
00201     DebugLoc DbgLoc;
00202 
00203     InsertPointGuard(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
00204     InsertPointGuard &operator=(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
00205 
00206   public:
00207     InsertPointGuard(IRBuilderBase &B)
00208         : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
00209           DbgLoc(B.getCurrentDebugLocation()) {}
00210 
00211     ~InsertPointGuard() {
00212       Builder.restoreIP(InsertPoint(Block, Point));
00213       Builder.SetCurrentDebugLocation(DbgLoc);
00214     }
00215   };
00216 
00217   // \brief RAII object that stores the current fast math settings and restores
00218   // them when the object is destroyed.
00219   class FastMathFlagGuard {
00220     IRBuilderBase &Builder;
00221     FastMathFlags FMF;
00222     MDNode *FPMathTag;
00223 
00224     FastMathFlagGuard(const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
00225     FastMathFlagGuard &operator=(
00226         const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
00227 
00228   public:
00229     FastMathFlagGuard(IRBuilderBase &B)
00230         : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
00231 
00232     ~FastMathFlagGuard() {
00233       Builder.FMF = FMF;
00234       Builder.DefaultFPMathTag = FPMathTag;
00235     }
00236   };
00237 
00238   //===--------------------------------------------------------------------===//
00239   // Miscellaneous creation methods.
00240   //===--------------------------------------------------------------------===//
00241 
00242   /// \brief Make a new global variable with initializer type i8*
00243   ///
00244   /// Make a new global variable with an initializer that has array of i8 type
00245   /// filled in with the null terminated string value specified.  The new global
00246   /// variable will be marked mergable with any others of the same contents.  If
00247   /// Name is specified, it is the name of the global variable created.
00248   Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
00249 
00250   /// \brief Get a constant value representing either true or false.
00251   ConstantInt *getInt1(bool V) {
00252     return ConstantInt::get(getInt1Ty(), V);
00253   }
00254 
00255   /// \brief Get the constant value for i1 true.
00256   ConstantInt *getTrue() {
00257     return ConstantInt::getTrue(Context);
00258   }
00259 
00260   /// \brief Get the constant value for i1 false.
00261   ConstantInt *getFalse() {
00262     return ConstantInt::getFalse(Context);
00263   }
00264 
00265   /// \brief Get a constant 8-bit value.
00266   ConstantInt *getInt8(uint8_t C) {
00267     return ConstantInt::get(getInt8Ty(), C);
00268   }
00269 
00270   /// \brief Get a constant 16-bit value.
00271   ConstantInt *getInt16(uint16_t C) {
00272     return ConstantInt::get(getInt16Ty(), C);
00273   }
00274 
00275   /// \brief Get a constant 32-bit value.
00276   ConstantInt *getInt32(uint32_t C) {
00277     return ConstantInt::get(getInt32Ty(), C);
00278   }
00279 
00280   /// \brief Get a constant 64-bit value.
00281   ConstantInt *getInt64(uint64_t C) {
00282     return ConstantInt::get(getInt64Ty(), C);
00283   }
00284 
00285   /// \brief Get a constant N-bit value, zero extended or truncated from
00286   /// a 64-bit value.
00287   ConstantInt *getIntN(unsigned N, uint64_t C) {
00288     return ConstantInt::get(getIntNTy(N), C);
00289   }
00290 
00291   /// \brief Get a constant integer value.
00292   ConstantInt *getInt(const APInt &AI) {
00293     return ConstantInt::get(Context, AI);
00294   }
00295 
00296   //===--------------------------------------------------------------------===//
00297   // Type creation methods
00298   //===--------------------------------------------------------------------===//
00299 
00300   /// \brief Fetch the type representing a single bit
00301   IntegerType *getInt1Ty() {
00302     return Type::getInt1Ty(Context);
00303   }
00304 
00305   /// \brief Fetch the type representing an 8-bit integer.
00306   IntegerType *getInt8Ty() {
00307     return Type::getInt8Ty(Context);
00308   }
00309 
00310   /// \brief Fetch the type representing a 16-bit integer.
00311   IntegerType *getInt16Ty() {
00312     return Type::getInt16Ty(Context);
00313   }
00314 
00315   /// \brief Fetch the type representing a 32-bit integer.
00316   IntegerType *getInt32Ty() {
00317     return Type::getInt32Ty(Context);
00318   }
00319 
00320   /// \brief Fetch the type representing a 64-bit integer.
00321   IntegerType *getInt64Ty() {
00322     return Type::getInt64Ty(Context);
00323   }
00324 
00325   /// \brief Fetch the type representing an N-bit integer.
00326   IntegerType *getIntNTy(unsigned N) {
00327     return Type::getIntNTy(Context, N);
00328   }
00329 
00330   /// \brief Fetch the type representing a 32-bit floating point value.
00331   Type *getFloatTy() {
00332     return Type::getFloatTy(Context);
00333   }
00334 
00335   /// \brief Fetch the type representing a 64-bit floating point value.
00336   Type *getDoubleTy() {
00337     return Type::getDoubleTy(Context);
00338   }
00339 
00340   /// \brief Fetch the type representing void.
00341   Type *getVoidTy() {
00342     return Type::getVoidTy(Context);
00343   }
00344 
00345   /// \brief Fetch the type representing a pointer to an 8-bit integer value.
00346   PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
00347     return Type::getInt8PtrTy(Context, AddrSpace);
00348   }
00349 
00350   /// \brief Fetch the type representing a pointer to an integer value.
00351   IntegerType* getIntPtrTy(const DataLayout *DL, unsigned AddrSpace = 0) {
00352     return DL->getIntPtrType(Context, AddrSpace);
00353   }
00354 
00355   //===--------------------------------------------------------------------===//
00356   // Intrinsic creation methods
00357   //===--------------------------------------------------------------------===//
00358 
00359   /// \brief Create and insert a memset to the specified pointer and the
00360   /// specified value.
00361   ///
00362   /// If the pointer isn't an i8*, it will be converted.  If a TBAA tag is
00363   /// specified, it will be added to the instruction.
00364   CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
00365                          bool isVolatile = false, MDNode *TBAATag = nullptr) {
00366     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
00367   }
00368 
00369   CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
00370                          bool isVolatile = false, MDNode *TBAATag = nullptr);
00371 
00372   /// \brief Create and insert a memcpy between the specified pointers.
00373   ///
00374   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
00375   /// specified, it will be added to the instruction.
00376   CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
00377                          bool isVolatile = false, MDNode *TBAATag = nullptr,
00378                          MDNode *TBAAStructTag = nullptr) {
00379     return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
00380                         TBAAStructTag);
00381   }
00382 
00383   CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
00384                          bool isVolatile = false, MDNode *TBAATag = nullptr,
00385                          MDNode *TBAAStructTag = nullptr);
00386 
00387   /// \brief Create and insert a memmove between the specified
00388   /// pointers.
00389   ///
00390   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
00391   /// specified, it will be added to the instruction.
00392   CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
00393                           bool isVolatile = false, MDNode *TBAATag = nullptr) {
00394     return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
00395   }
00396 
00397   CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
00398                           bool isVolatile = false, MDNode *TBAATag = nullptr);
00399 
00400   /// \brief Create a lifetime.start intrinsic.
00401   ///
00402   /// If the pointer isn't i8* it will be converted.
00403   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
00404 
00405   /// \brief Create a lifetime.end intrinsic.
00406   ///
00407   /// If the pointer isn't i8* it will be converted.
00408   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
00409 
00410 private:
00411   Value *getCastedInt8PtrValue(Value *Ptr);
00412 };
00413 
00414 /// \brief This provides a uniform API for creating instructions and inserting
00415 /// them into a basic block: either at the end of a BasicBlock, or at a specific
00416 /// iterator location in a block.
00417 ///
00418 /// Note that the builder does not expose the full generality of LLVM
00419 /// instructions.  For access to extra instruction properties, use the mutators
00420 /// (e.g. setVolatile) on the instructions after they have been
00421 /// created. Convenience state exists to specify fast-math flags and fp-math
00422 /// tags.
00423 ///
00424 /// The first template argument handles whether or not to preserve names in the
00425 /// final instruction output. This defaults to on.  The second template argument
00426 /// specifies a class to use for creating constants.  This defaults to creating
00427 /// minimally folded constants.  The fourth template argument allows clients to
00428 /// specify custom insertion hooks that are called on every newly created
00429 /// insertion.
00430 template<bool preserveNames = true, typename T = ConstantFolder,
00431          typename Inserter = IRBuilderDefaultInserter<preserveNames> >
00432 class IRBuilder : public IRBuilderBase, public Inserter {
00433   T Folder;
00434 public:
00435   IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
00436             MDNode *FPMathTag = nullptr)
00437     : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
00438   }
00439 
00440   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
00441     : IRBuilderBase(C, FPMathTag), Folder() {
00442   }
00443 
00444   explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
00445     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
00446     SetInsertPoint(TheBB);
00447   }
00448 
00449   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
00450     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
00451     SetInsertPoint(TheBB);
00452   }
00453 
00454   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
00455     : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
00456     SetInsertPoint(IP);
00457     SetCurrentDebugLocation(IP->getDebugLoc());
00458   }
00459 
00460   explicit IRBuilder(Use &U, MDNode *FPMathTag = nullptr)
00461     : IRBuilderBase(U->getContext(), FPMathTag), Folder() {
00462     SetInsertPoint(U);
00463     SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
00464   }
00465 
00466   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
00467             MDNode *FPMathTag = nullptr)
00468     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
00469     SetInsertPoint(TheBB, IP);
00470   }
00471 
00472   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
00473             MDNode *FPMathTag = nullptr)
00474     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
00475     SetInsertPoint(TheBB, IP);
00476   }
00477 
00478   /// \brief Get the constant folder being used.
00479   const T &getFolder() { return Folder; }
00480 
00481   /// \brief Return true if this builder is configured to actually add the
00482   /// requested names to IR created through it.
00483   bool isNamePreserving() const { return preserveNames; }
00484 
00485   /// \brief Insert and return the specified instruction.
00486   template<typename InstTy>
00487   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
00488     this->InsertHelper(I, Name, BB, InsertPt);
00489     this->SetInstDebugLocation(I);
00490     return I;
00491   }
00492 
00493   /// \brief No-op overload to handle constants.
00494   Constant *Insert(Constant *C, const Twine& = "") const {
00495     return C;
00496   }
00497 
00498   //===--------------------------------------------------------------------===//
00499   // Instruction creation methods: Terminators
00500   //===--------------------------------------------------------------------===//
00501 
00502 private:
00503   /// \brief Helper to add branch weight metadata onto an instruction.
00504   /// \returns The annotated instruction.
00505   template <typename InstTy>
00506   InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
00507     if (Weights)
00508       I->setMetadata(LLVMContext::MD_prof, Weights);
00509     return I;
00510   }
00511 
00512 public:
00513   /// \brief Create a 'ret void' instruction.
00514   ReturnInst *CreateRetVoid() {
00515     return Insert(ReturnInst::Create(Context));
00516   }
00517 
00518   /// \brief Create a 'ret <val>' instruction.
00519   ReturnInst *CreateRet(Value *V) {
00520     return Insert(ReturnInst::Create(Context, V));
00521   }
00522 
00523   /// \brief Create a sequence of N insertvalue instructions,
00524   /// with one Value from the retVals array each, that build a aggregate
00525   /// return value one value at a time, and a ret instruction to return
00526   /// the resulting aggregate value.
00527   ///
00528   /// This is a convenience function for code that uses aggregate return values
00529   /// as a vehicle for having multiple return values.
00530   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
00531     Value *V = UndefValue::get(getCurrentFunctionReturnType());
00532     for (unsigned i = 0; i != N; ++i)
00533       V = CreateInsertValue(V, retVals[i], i, "mrv");
00534     return Insert(ReturnInst::Create(Context, V));
00535   }
00536 
00537   /// \brief Create an unconditional 'br label X' instruction.
00538   BranchInst *CreateBr(BasicBlock *Dest) {
00539     return Insert(BranchInst::Create(Dest));
00540   }
00541 
00542   /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
00543   /// instruction.
00544   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
00545                            MDNode *BranchWeights = nullptr) {
00546     return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
00547                                    BranchWeights));
00548   }
00549 
00550   /// \brief Create a switch instruction with the specified value, default dest,
00551   /// and with a hint for the number of cases that will be added (for efficient
00552   /// allocation).
00553   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
00554                            MDNode *BranchWeights = nullptr) {
00555     return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
00556                                    BranchWeights));
00557   }
00558 
00559   /// \brief Create an indirect branch instruction with the specified address
00560   /// operand, with an optional hint for the number of destinations that will be
00561   /// added (for efficient allocation).
00562   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
00563     return Insert(IndirectBrInst::Create(Addr, NumDests));
00564   }
00565 
00566   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
00567                            BasicBlock *UnwindDest, const Twine &Name = "") {
00568     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
00569                                      ArrayRef<Value *>()),
00570                   Name);
00571   }
00572   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
00573                            BasicBlock *UnwindDest, Value *Arg1,
00574                            const Twine &Name = "") {
00575     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
00576                   Name);
00577   }
00578   InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
00579                             BasicBlock *UnwindDest, Value *Arg1,
00580                             Value *Arg2, Value *Arg3,
00581                             const Twine &Name = "") {
00582     Value *Args[] = { Arg1, Arg2, Arg3 };
00583     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
00584                   Name);
00585   }
00586   /// \brief Create an invoke instruction.
00587   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
00588                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
00589                            const Twine &Name = "") {
00590     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
00591                   Name);
00592   }
00593 
00594   ResumeInst *CreateResume(Value *Exn) {
00595     return Insert(ResumeInst::Create(Exn));
00596   }
00597 
00598   UnreachableInst *CreateUnreachable() {
00599     return Insert(new UnreachableInst(Context));
00600   }
00601 
00602   //===--------------------------------------------------------------------===//
00603   // Instruction creation methods: Binary Operators
00604   //===--------------------------------------------------------------------===//
00605 private:
00606   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
00607                                           Value *LHS, Value *RHS,
00608                                           const Twine &Name,
00609                                           bool HasNUW, bool HasNSW) {
00610     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
00611     if (HasNUW) BO->setHasNoUnsignedWrap();
00612     if (HasNSW) BO->setHasNoSignedWrap();
00613     return BO;
00614   }
00615 
00616   Instruction *AddFPMathAttributes(Instruction *I,
00617                                    MDNode *FPMathTag,
00618                                    FastMathFlags FMF) const {
00619     if (!FPMathTag)
00620       FPMathTag = DefaultFPMathTag;
00621     if (FPMathTag)
00622       I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
00623     I->setFastMathFlags(FMF);
00624     return I;
00625   }
00626 public:
00627   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
00628                    bool HasNUW = false, bool HasNSW = false) {
00629     if (Constant *LC = dyn_cast<Constant>(LHS))
00630       if (Constant *RC = dyn_cast<Constant>(RHS))
00631         return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
00632     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
00633                                    HasNUW, HasNSW);
00634   }
00635   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
00636     return CreateAdd(LHS, RHS, Name, false, true);
00637   }
00638   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
00639     return CreateAdd(LHS, RHS, Name, true, false);
00640   }
00641   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
00642                     MDNode *FPMathTag = nullptr) {
00643     if (Constant *LC = dyn_cast<Constant>(LHS))
00644       if (Constant *RC = dyn_cast<Constant>(RHS))
00645         return Insert(Folder.CreateFAdd(LC, RC), Name);
00646     return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
00647                                       FPMathTag, FMF), Name);
00648   }
00649   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
00650                    bool HasNUW = false, bool HasNSW = false) {
00651     if (Constant *LC = dyn_cast<Constant>(LHS))
00652       if (Constant *RC = dyn_cast<Constant>(RHS))
00653         return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
00654     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
00655                                    HasNUW, HasNSW);
00656   }
00657   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
00658     return CreateSub(LHS, RHS, Name, false, true);
00659   }
00660   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
00661     return CreateSub(LHS, RHS, Name, true, false);
00662   }
00663   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
00664                     MDNode *FPMathTag = nullptr) {
00665     if (Constant *LC = dyn_cast<Constant>(LHS))
00666       if (Constant *RC = dyn_cast<Constant>(RHS))
00667         return Insert(Folder.CreateFSub(LC, RC), Name);
00668     return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
00669                                       FPMathTag, FMF), Name);
00670   }
00671   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
00672                    bool HasNUW = false, bool HasNSW = false) {
00673     if (Constant *LC = dyn_cast<Constant>(LHS))
00674       if (Constant *RC = dyn_cast<Constant>(RHS))
00675         return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
00676     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
00677                                    HasNUW, HasNSW);
00678   }
00679   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
00680     return CreateMul(LHS, RHS, Name, false, true);
00681   }
00682   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
00683     return CreateMul(LHS, RHS, Name, true, false);
00684   }
00685   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
00686                     MDNode *FPMathTag = nullptr) {
00687     if (Constant *LC = dyn_cast<Constant>(LHS))
00688       if (Constant *RC = dyn_cast<Constant>(RHS))
00689         return Insert(Folder.CreateFMul(LC, RC), Name);
00690     return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
00691                                       FPMathTag, FMF), Name);
00692   }
00693   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
00694                     bool isExact = false) {
00695     if (Constant *LC = dyn_cast<Constant>(LHS))
00696       if (Constant *RC = dyn_cast<Constant>(RHS))
00697         return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
00698     if (!isExact)
00699       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
00700     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
00701   }
00702   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
00703     return CreateUDiv(LHS, RHS, Name, true);
00704   }
00705   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
00706                     bool isExact = false) {
00707     if (Constant *LC = dyn_cast<Constant>(LHS))
00708       if (Constant *RC = dyn_cast<Constant>(RHS))
00709         return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
00710     if (!isExact)
00711       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
00712     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
00713   }
00714   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
00715     return CreateSDiv(LHS, RHS, Name, true);
00716   }
00717   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
00718                     MDNode *FPMathTag = nullptr) {
00719     if (Constant *LC = dyn_cast<Constant>(LHS))
00720       if (Constant *RC = dyn_cast<Constant>(RHS))
00721         return Insert(Folder.CreateFDiv(LC, RC), Name);
00722     return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
00723                                       FPMathTag, FMF), Name);
00724   }
00725   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
00726     if (Constant *LC = dyn_cast<Constant>(LHS))
00727       if (Constant *RC = dyn_cast<Constant>(RHS))
00728         return Insert(Folder.CreateURem(LC, RC), Name);
00729     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
00730   }
00731   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
00732     if (Constant *LC = dyn_cast<Constant>(LHS))
00733       if (Constant *RC = dyn_cast<Constant>(RHS))
00734         return Insert(Folder.CreateSRem(LC, RC), Name);
00735     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
00736   }
00737   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
00738                     MDNode *FPMathTag = nullptr) {
00739     if (Constant *LC = dyn_cast<Constant>(LHS))
00740       if (Constant *RC = dyn_cast<Constant>(RHS))
00741         return Insert(Folder.CreateFRem(LC, RC), Name);
00742     return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
00743                                       FPMathTag, FMF), Name);
00744   }
00745 
00746   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
00747                    bool HasNUW = false, bool HasNSW = false) {
00748     if (Constant *LC = dyn_cast<Constant>(LHS))
00749       if (Constant *RC = dyn_cast<Constant>(RHS))
00750         return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
00751     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
00752                                    HasNUW, HasNSW);
00753   }
00754   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
00755                    bool HasNUW = false, bool HasNSW = false) {
00756     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
00757                      HasNUW, HasNSW);
00758   }
00759   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
00760                    bool HasNUW = false, bool HasNSW = false) {
00761     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
00762                      HasNUW, HasNSW);
00763   }
00764 
00765   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
00766                     bool isExact = false) {
00767     if (Constant *LC = dyn_cast<Constant>(LHS))
00768       if (Constant *RC = dyn_cast<Constant>(RHS))
00769         return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
00770     if (!isExact)
00771       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
00772     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
00773   }
00774   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
00775                     bool isExact = false) {
00776     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
00777   }
00778   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
00779                     bool isExact = false) {
00780     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
00781   }
00782 
00783   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
00784                     bool isExact = false) {
00785     if (Constant *LC = dyn_cast<Constant>(LHS))
00786       if (Constant *RC = dyn_cast<Constant>(RHS))
00787         return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
00788     if (!isExact)
00789       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
00790     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
00791   }
00792   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
00793                     bool isExact = false) {
00794     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
00795   }
00796   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
00797                     bool isExact = false) {
00798     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
00799   }
00800 
00801   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
00802     if (Constant *RC = dyn_cast<Constant>(RHS)) {
00803       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
00804         return LHS;  // LHS & -1 -> LHS
00805       if (Constant *LC = dyn_cast<Constant>(LHS))
00806         return Insert(Folder.CreateAnd(LC, RC), Name);
00807     }
00808     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
00809   }
00810   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
00811     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
00812   }
00813   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
00814     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
00815   }
00816 
00817   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
00818     if (Constant *RC = dyn_cast<Constant>(RHS)) {
00819       if (RC->isNullValue())
00820         return LHS;  // LHS | 0 -> LHS
00821       if (Constant *LC = dyn_cast<Constant>(LHS))
00822         return Insert(Folder.CreateOr(LC, RC), Name);
00823     }
00824     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
00825   }
00826   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
00827     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
00828   }
00829   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
00830     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
00831   }
00832 
00833   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
00834     if (Constant *LC = dyn_cast<Constant>(LHS))
00835       if (Constant *RC = dyn_cast<Constant>(RHS))
00836         return Insert(Folder.CreateXor(LC, RC), Name);
00837     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
00838   }
00839   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
00840     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
00841   }
00842   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
00843     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
00844   }
00845 
00846   Value *CreateBinOp(Instruction::BinaryOps Opc,
00847                      Value *LHS, Value *RHS, const Twine &Name = "",
00848                      MDNode *FPMathTag = nullptr) {
00849     if (Constant *LC = dyn_cast<Constant>(LHS))
00850       if (Constant *RC = dyn_cast<Constant>(RHS))
00851         return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
00852     llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
00853     if (isa<FPMathOperator>(BinOp))
00854       BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
00855     return Insert(BinOp, Name);
00856   }
00857 
00858   Value *CreateNeg(Value *V, const Twine &Name = "",
00859                    bool HasNUW = false, bool HasNSW = false) {
00860     if (Constant *VC = dyn_cast<Constant>(V))
00861       return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
00862     BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
00863     if (HasNUW) BO->setHasNoUnsignedWrap();
00864     if (HasNSW) BO->setHasNoSignedWrap();
00865     return BO;
00866   }
00867   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
00868     return CreateNeg(V, Name, false, true);
00869   }
00870   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
00871     return CreateNeg(V, Name, true, false);
00872   }
00873   Value *CreateFNeg(Value *V, const Twine &Name = "",
00874                     MDNode *FPMathTag = nullptr) {
00875     if (Constant *VC = dyn_cast<Constant>(V))
00876       return Insert(Folder.CreateFNeg(VC), Name);
00877     return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
00878                                       FPMathTag, FMF), Name);
00879   }
00880   Value *CreateNot(Value *V, const Twine &Name = "") {
00881     if (Constant *VC = dyn_cast<Constant>(V))
00882       return Insert(Folder.CreateNot(VC), Name);
00883     return Insert(BinaryOperator::CreateNot(V), Name);
00884   }
00885 
00886   //===--------------------------------------------------------------------===//
00887   // Instruction creation methods: Memory Instructions
00888   //===--------------------------------------------------------------------===//
00889 
00890   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
00891                            const Twine &Name = "") {
00892     return Insert(new AllocaInst(Ty, ArraySize), Name);
00893   }
00894   // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
00895   // converting the string to 'bool' for the isVolatile parameter.
00896   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
00897     return Insert(new LoadInst(Ptr), Name);
00898   }
00899   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
00900     return Insert(new LoadInst(Ptr), Name);
00901   }
00902   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
00903     return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
00904   }
00905   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
00906     return Insert(new StoreInst(Val, Ptr, isVolatile));
00907   }
00908   // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
00909   // correctly, instead of converting the string to 'bool' for the isVolatile
00910   // parameter.
00911   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
00912     LoadInst *LI = CreateLoad(Ptr, Name);
00913     LI->setAlignment(Align);
00914     return LI;
00915   }
00916   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
00917                               const Twine &Name = "") {
00918     LoadInst *LI = CreateLoad(Ptr, Name);
00919     LI->setAlignment(Align);
00920     return LI;
00921   }
00922   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
00923                               const Twine &Name = "") {
00924     LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
00925     LI->setAlignment(Align);
00926     return LI;
00927   }
00928   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
00929                                 bool isVolatile = false) {
00930     StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
00931     SI->setAlignment(Align);
00932     return SI;
00933   }
00934   FenceInst *CreateFence(AtomicOrdering Ordering,
00935                          SynchronizationScope SynchScope = CrossThread,
00936                          const Twine &Name = "") {
00937     return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
00938   }
00939   AtomicCmpXchgInst *
00940   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
00941                       AtomicOrdering SuccessOrdering,
00942                       AtomicOrdering FailureOrdering,
00943                       SynchronizationScope SynchScope = CrossThread) {
00944     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
00945                                         FailureOrdering, SynchScope));
00946   }
00947   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
00948                                  AtomicOrdering Ordering,
00949                                SynchronizationScope SynchScope = CrossThread) {
00950     return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
00951   }
00952   Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
00953                    const Twine &Name = "") {
00954     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
00955       // Every index must be constant.
00956       size_t i, e;
00957       for (i = 0, e = IdxList.size(); i != e; ++i)
00958         if (!isa<Constant>(IdxList[i]))
00959           break;
00960       if (i == e)
00961         return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
00962     }
00963     return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
00964   }
00965   Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
00966                            const Twine &Name = "") {
00967     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
00968       // Every index must be constant.
00969       size_t i, e;
00970       for (i = 0, e = IdxList.size(); i != e; ++i)
00971         if (!isa<Constant>(IdxList[i]))
00972           break;
00973       if (i == e)
00974         return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
00975     }
00976     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
00977   }
00978   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
00979     if (Constant *PC = dyn_cast<Constant>(Ptr))
00980       if (Constant *IC = dyn_cast<Constant>(Idx))
00981         return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
00982     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
00983   }
00984   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
00985     if (Constant *PC = dyn_cast<Constant>(Ptr))
00986       if (Constant *IC = dyn_cast<Constant>(Idx))
00987         return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
00988     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
00989   }
00990   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
00991     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
00992 
00993     if (Constant *PC = dyn_cast<Constant>(Ptr))
00994       return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
00995 
00996     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
00997   }
00998   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
00999                                     const Twine &Name = "") {
01000     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
01001 
01002     if (Constant *PC = dyn_cast<Constant>(Ptr))
01003       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
01004 
01005     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
01006   }
01007   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
01008                     const Twine &Name = "") {
01009     Value *Idxs[] = {
01010       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
01011       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
01012     };
01013 
01014     if (Constant *PC = dyn_cast<Constant>(Ptr))
01015       return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
01016 
01017     return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
01018   }
01019   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
01020                                     const Twine &Name = "") {
01021     Value *Idxs[] = {
01022       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
01023       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
01024     };
01025 
01026     if (Constant *PC = dyn_cast<Constant>(Ptr))
01027       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
01028 
01029     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
01030   }
01031   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
01032     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
01033 
01034     if (Constant *PC = dyn_cast<Constant>(Ptr))
01035       return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
01036 
01037     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
01038   }
01039   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
01040                                     const Twine &Name = "") {
01041     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
01042 
01043     if (Constant *PC = dyn_cast<Constant>(Ptr))
01044       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
01045 
01046     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
01047   }
01048   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
01049                     const Twine &Name = "") {
01050     Value *Idxs[] = {
01051       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
01052       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
01053     };
01054 
01055     if (Constant *PC = dyn_cast<Constant>(Ptr))
01056       return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
01057 
01058     return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
01059   }
01060   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
01061                                     const Twine &Name = "") {
01062     Value *Idxs[] = {
01063       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
01064       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
01065     };
01066 
01067     if (Constant *PC = dyn_cast<Constant>(Ptr))
01068       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
01069 
01070     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
01071   }
01072   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
01073     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
01074   }
01075 
01076   /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
01077   /// instead of a pointer to array of i8.
01078   Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
01079     Value *gv = CreateGlobalString(Str, Name);
01080     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
01081     Value *Args[] = { zero, zero };
01082     return CreateInBoundsGEP(gv, Args, Name);
01083   }
01084 
01085   //===--------------------------------------------------------------------===//
01086   // Instruction creation methods: Cast/Conversion Operators
01087   //===--------------------------------------------------------------------===//
01088 
01089   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
01090     return CreateCast(Instruction::Trunc, V, DestTy, Name);
01091   }
01092   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
01093     return CreateCast(Instruction::ZExt, V, DestTy, Name);
01094   }
01095   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
01096     return CreateCast(Instruction::SExt, V, DestTy, Name);
01097   }
01098   /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
01099   /// the value untouched if the type of V is already DestTy.
01100   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
01101                            const Twine &Name = "") {
01102     assert(V->getType()->isIntOrIntVectorTy() &&
01103            DestTy->isIntOrIntVectorTy() &&
01104            "Can only zero extend/truncate integers!");
01105     Type *VTy = V->getType();
01106     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
01107       return CreateZExt(V, DestTy, Name);
01108     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
01109       return CreateTrunc(V, DestTy, Name);
01110     return V;
01111   }
01112   /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
01113   /// the value untouched if the type of V is already DestTy.
01114   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
01115                            const Twine &Name = "") {
01116     assert(V->getType()->isIntOrIntVectorTy() &&
01117            DestTy->isIntOrIntVectorTy() &&
01118            "Can only sign extend/truncate integers!");
01119     Type *VTy = V->getType();
01120     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
01121       return CreateSExt(V, DestTy, Name);
01122     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
01123       return CreateTrunc(V, DestTy, Name);
01124     return V;
01125   }
01126   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
01127     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
01128   }
01129   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
01130     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
01131   }
01132   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
01133     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
01134   }
01135   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
01136     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
01137   }
01138   Value *CreateFPTrunc(Value *V, Type *DestTy,
01139                        const Twine &Name = "") {
01140     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
01141   }
01142   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
01143     return CreateCast(Instruction::FPExt, V, DestTy, Name);
01144   }
01145   Value *CreatePtrToInt(Value *V, Type *DestTy,
01146                         const Twine &Name = "") {
01147     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
01148   }
01149   Value *CreateIntToPtr(Value *V, Type *DestTy,
01150                         const Twine &Name = "") {
01151     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
01152   }
01153   Value *CreateBitCast(Value *V, Type *DestTy,
01154                        const Twine &Name = "") {
01155     return CreateCast(Instruction::BitCast, V, DestTy, Name);
01156   }
01157   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
01158                              const Twine &Name = "") {
01159     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
01160   }
01161   Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
01162                              const Twine &Name = "") {
01163     if (V->getType() == DestTy)
01164       return V;
01165     if (Constant *VC = dyn_cast<Constant>(V))
01166       return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
01167     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
01168   }
01169   Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
01170                              const Twine &Name = "") {
01171     if (V->getType() == DestTy)
01172       return V;
01173     if (Constant *VC = dyn_cast<Constant>(V))
01174       return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
01175     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
01176   }
01177   Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
01178                               const Twine &Name = "") {
01179     if (V->getType() == DestTy)
01180       return V;
01181     if (Constant *VC = dyn_cast<Constant>(V))
01182       return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
01183     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
01184   }
01185   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
01186                     const Twine &Name = "") {
01187     if (V->getType() == DestTy)
01188       return V;
01189     if (Constant *VC = dyn_cast<Constant>(V))
01190       return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
01191     return Insert(CastInst::Create(Op, V, DestTy), Name);
01192   }
01193   Value *CreatePointerCast(Value *V, Type *DestTy,
01194                            const Twine &Name = "") {
01195     if (V->getType() == DestTy)
01196       return V;
01197     if (Constant *VC = dyn_cast<Constant>(V))
01198       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
01199     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
01200   }
01201   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
01202                        const Twine &Name = "") {
01203     if (V->getType() == DestTy)
01204       return V;
01205     if (Constant *VC = dyn_cast<Constant>(V))
01206       return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
01207     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
01208   }
01209 private:
01210   // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
01211   // compile time error, instead of converting the string to bool for the
01212   // isSigned parameter.
01213   Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
01214 public:
01215   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
01216     if (V->getType() == DestTy)
01217       return V;
01218     if (Constant *VC = dyn_cast<Constant>(V))
01219       return Insert(Folder.CreateFPCast(VC, DestTy), Name);
01220     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
01221   }
01222 
01223   //===--------------------------------------------------------------------===//
01224   // Instruction creation methods: Compare Instructions
01225   //===--------------------------------------------------------------------===//
01226 
01227   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
01228     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
01229   }
01230   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
01231     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
01232   }
01233   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
01234     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
01235   }
01236   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
01237     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
01238   }
01239   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
01240     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
01241   }
01242   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
01243     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
01244   }
01245   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
01246     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
01247   }
01248   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
01249     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
01250   }
01251   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
01252     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
01253   }
01254   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
01255     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
01256   }
01257 
01258   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
01259     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
01260   }
01261   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
01262     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
01263   }
01264   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
01265     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
01266   }
01267   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
01268     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
01269   }
01270   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
01271     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
01272   }
01273   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
01274     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
01275   }
01276   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
01277     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
01278   }
01279   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
01280     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
01281   }
01282   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
01283     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
01284   }
01285   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
01286     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
01287   }
01288   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
01289     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
01290   }
01291   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
01292     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
01293   }
01294   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
01295     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
01296   }
01297   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
01298     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
01299   }
01300 
01301   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
01302                     const Twine &Name = "") {
01303     if (Constant *LC = dyn_cast<Constant>(LHS))
01304       if (Constant *RC = dyn_cast<Constant>(RHS))
01305         return Insert(Folder.CreateICmp(P, LC, RC), Name);
01306     return Insert(new ICmpInst(P, LHS, RHS), Name);
01307   }
01308   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
01309                     const Twine &Name = "") {
01310     if (Constant *LC = dyn_cast<Constant>(LHS))
01311       if (Constant *RC = dyn_cast<Constant>(RHS))
01312         return Insert(Folder.CreateFCmp(P, LC, RC), Name);
01313     return Insert(new FCmpInst(P, LHS, RHS), Name);
01314   }
01315 
01316   //===--------------------------------------------------------------------===//
01317   // Instruction creation methods: Other Instructions
01318   //===--------------------------------------------------------------------===//
01319 
01320   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
01321                      const Twine &Name = "") {
01322     return Insert(PHINode::Create(Ty, NumReservedValues), Name);
01323   }
01324 
01325   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
01326     return Insert(CallInst::Create(Callee), Name);
01327   }
01328   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
01329     return Insert(CallInst::Create(Callee, Arg), Name);
01330   }
01331   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
01332                         const Twine &Name = "") {
01333     Value *Args[] = { Arg1, Arg2 };
01334     return Insert(CallInst::Create(Callee, Args), Name);
01335   }
01336   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
01337                         const Twine &Name = "") {
01338     Value *Args[] = { Arg1, Arg2, Arg3 };
01339     return Insert(CallInst::Create(Callee, Args), Name);
01340   }
01341   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
01342                         Value *Arg4, const Twine &Name = "") {
01343     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
01344     return Insert(CallInst::Create(Callee, Args), Name);
01345   }
01346   CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
01347                         Value *Arg4, Value *Arg5, const Twine &Name = "") {
01348     Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
01349     return Insert(CallInst::Create(Callee, Args), Name);
01350   }
01351 
01352   CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
01353                        const Twine &Name = "") {
01354     return Insert(CallInst::Create(Callee, Args), Name);
01355   }
01356 
01357   Value *CreateSelect(Value *C, Value *True, Value *False,
01358                       const Twine &Name = "") {
01359     if (Constant *CC = dyn_cast<Constant>(C))
01360       if (Constant *TC = dyn_cast<Constant>(True))
01361         if (Constant *FC = dyn_cast<Constant>(False))
01362           return Insert(Folder.CreateSelect(CC, TC, FC), Name);
01363     return Insert(SelectInst::Create(C, True, False), Name);
01364   }
01365 
01366   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
01367     return Insert(new VAArgInst(List, Ty), Name);
01368   }
01369 
01370   Value *CreateExtractElement(Value *Vec, Value *Idx,
01371                               const Twine &Name = "") {
01372     if (Constant *VC = dyn_cast<Constant>(Vec))
01373       if (Constant *IC = dyn_cast<Constant>(Idx))
01374         return Insert(Folder.CreateExtractElement(VC, IC), Name);
01375     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
01376   }
01377 
01378   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
01379                              const Twine &Name = "") {
01380     if (Constant *VC = dyn_cast<Constant>(Vec))
01381       if (Constant *NC = dyn_cast<Constant>(NewElt))
01382         if (Constant *IC = dyn_cast<Constant>(Idx))
01383           return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
01384     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
01385   }
01386 
01387   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
01388                              const Twine &Name = "") {
01389     if (Constant *V1C = dyn_cast<Constant>(V1))
01390       if (Constant *V2C = dyn_cast<Constant>(V2))
01391         if (Constant *MC = dyn_cast<Constant>(Mask))
01392           return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
01393     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
01394   }
01395 
01396   Value *CreateExtractValue(Value *Agg,
01397                             ArrayRef<unsigned> Idxs,
01398                             const Twine &Name = "") {
01399     if (Constant *AggC = dyn_cast<Constant>(Agg))
01400       return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
01401     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
01402   }
01403 
01404   Value *CreateInsertValue(Value *Agg, Value *Val,
01405                            ArrayRef<unsigned> Idxs,
01406                            const Twine &Name = "") {
01407     if (Constant *AggC = dyn_cast<Constant>(Agg))
01408       if (Constant *ValC = dyn_cast<Constant>(Val))
01409         return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
01410     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
01411   }
01412 
01413   LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
01414                                    const Twine &Name = "") {
01415     return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
01416   }
01417 
01418   //===--------------------------------------------------------------------===//
01419   // Utility creation methods
01420   //===--------------------------------------------------------------------===//
01421 
01422   /// \brief Return an i1 value testing if \p Arg is null.
01423   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
01424     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
01425                         Name);
01426   }
01427 
01428   /// \brief Return an i1 value testing if \p Arg is not null.
01429   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
01430     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
01431                         Name);
01432   }
01433 
01434   /// \brief Return the i64 difference between two pointer values, dividing out
01435   /// the size of the pointed-to objects.
01436   ///
01437   /// This is intended to implement C-style pointer subtraction. As such, the
01438   /// pointers must be appropriately aligned for their element types and
01439   /// pointing into the same object.
01440   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
01441     assert(LHS->getType() == RHS->getType() &&
01442            "Pointer subtraction operand types must match!");
01443     PointerType *ArgType = cast<PointerType>(LHS->getType());
01444     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
01445     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
01446     Value *Difference = CreateSub(LHS_int, RHS_int);
01447     return CreateExactSDiv(Difference,
01448                            ConstantExpr::getSizeOf(ArgType->getElementType()),
01449                            Name);
01450   }
01451 
01452   /// \brief Return a vector value that contains \arg V broadcasted to \p
01453   /// NumElts elements.
01454   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
01455     assert(NumElts > 0 && "Cannot splat to an empty vector!");
01456 
01457     // First insert it into an undef vector so we can shuffle it.
01458     Type *I32Ty = getInt32Ty();
01459     Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
01460     V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
01461                             Name + ".splatinsert");
01462 
01463     // Shuffle the value across the desired number of elements.
01464     Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
01465     return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
01466   }
01467 };
01468 
01469 // Create wrappers for C Binding types (see CBindingWrapping.h).
01470 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
01471 
01472 }
01473 
01474 #endif