cedb87cdb7ccf819a3bf8790b37b0d4e26916ca8
[oota-llvm.git] / include / llvm / IR / IRBuilder.h
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/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/ConstantFolder.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Operator.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/CBindingWrapping.h"
29
30 namespace llvm {
31   class MDNode;
32
33 /// \brief This provides the default implementation of the IRBuilder
34 /// 'InsertHelper' method that is called whenever an instruction is created by
35 /// IRBuilder and needs to be inserted.
36 ///
37 /// By default, this inserts the instruction at the insertion point.
38 template <bool preserveNames = true>
39 class IRBuilderDefaultInserter {
40 protected:
41   void InsertHelper(Instruction *I, const Twine &Name,
42                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
43     if (BB) BB->getInstList().insert(InsertPt, I);
44     if (preserveNames)
45       I->setName(Name);
46   }
47 };
48
49 /// \brief Common base class shared among various IRBuilders.
50 class IRBuilderBase {
51   DebugLoc CurDbgLocation;
52 protected:
53   BasicBlock *BB;
54   BasicBlock::iterator InsertPt;
55   LLVMContext &Context;
56
57   MDNode *DefaultFPMathTag;
58   FastMathFlags FMF;
59 public:
60
61   IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr)
62     : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
63     ClearInsertionPoint();
64   }
65
66   //===--------------------------------------------------------------------===//
67   // Builder configuration methods
68   //===--------------------------------------------------------------------===//
69
70   /// \brief Clear the insertion point: created instructions will not be
71   /// inserted into a block.
72   void ClearInsertionPoint() {
73     BB = nullptr;
74     InsertPt = nullptr;
75   }
76
77   BasicBlock *GetInsertBlock() const { return BB; }
78   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
79   LLVMContext &getContext() const { return Context; }
80
81   /// \brief This specifies that created instructions should be appended to the
82   /// end of the specified block.
83   void SetInsertPoint(BasicBlock *TheBB) {
84     BB = TheBB;
85     InsertPt = BB->end();
86   }
87
88   /// \brief This specifies that created instructions should be inserted before
89   /// the specified instruction.
90   void SetInsertPoint(Instruction *I) {
91     BB = I->getParent();
92     InsertPt = I;
93     assert(I != BB->end() && "Can't read debug loc from end()");
94     SetCurrentDebugLocation(I->getDebugLoc());
95   }
96
97   /// \brief This specifies that created instructions should be inserted at the
98   /// specified point.
99   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
100     BB = TheBB;
101     InsertPt = IP;
102   }
103
104   /// \brief Find the nearest point that dominates this use, and specify that
105   /// created instructions should be inserted at this point.
106   void SetInsertPoint(Use &U) {
107     Instruction *UseInst = cast<Instruction>(U.getUser());
108     if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
109       BasicBlock *PredBB = Phi->getIncomingBlock(U);
110       assert(U != PredBB->getTerminator() && "critical edge not split");
111       SetInsertPoint(PredBB, PredBB->getTerminator());
112       return;
113     }
114     SetInsertPoint(UseInst);
115   }
116
117   /// \brief Set location information used by debugging information.
118   void SetCurrentDebugLocation(const DebugLoc &L) {
119     CurDbgLocation = L;
120   }
121
122   /// \brief Get location information used by debugging information.
123   DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
124
125   /// \brief If this builder has a current debug location, set it on the
126   /// specified instruction.
127   void SetInstDebugLocation(Instruction *I) const {
128     if (!CurDbgLocation.isUnknown())
129       I->setDebugLoc(CurDbgLocation);
130   }
131
132   /// \brief Get the return type of the current function that we're emitting
133   /// into.
134   Type *getCurrentFunctionReturnType() const;
135
136   /// InsertPoint - A saved insertion point.
137   class InsertPoint {
138     BasicBlock *Block;
139     BasicBlock::iterator Point;
140
141   public:
142     /// \brief Creates a new insertion point which doesn't point to anything.
143     InsertPoint() : Block(nullptr) {}
144
145     /// \brief Creates a new insertion point at the given location.
146     InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
147       : Block(InsertBlock), Point(InsertPoint) {}
148
149     /// \brief Returns true if this insert point is set.
150     bool isSet() const { return (Block != nullptr); }
151
152     llvm::BasicBlock *getBlock() const { return Block; }
153     llvm::BasicBlock::iterator getPoint() const { return Point; }
154   };
155
156   /// \brief Returns the current insert point.
157   InsertPoint saveIP() const {
158     return InsertPoint(GetInsertBlock(), GetInsertPoint());
159   }
160
161   /// \brief Returns the current insert point, clearing it in the process.
162   InsertPoint saveAndClearIP() {
163     InsertPoint IP(GetInsertBlock(), GetInsertPoint());
164     ClearInsertionPoint();
165     return IP;
166   }
167
168   /// \brief Sets the current insert point to a previously-saved location.
169   void restoreIP(InsertPoint IP) {
170     if (IP.isSet())
171       SetInsertPoint(IP.getBlock(), IP.getPoint());
172     else
173       ClearInsertionPoint();
174   }
175
176   /// \brief Get the floating point math metadata being used.
177   MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
178
179   /// \brief Get the flags to be applied to created floating point ops
180   FastMathFlags getFastMathFlags() const { return FMF; }
181
182   /// \brief Clear the fast-math flags.
183   void clearFastMathFlags() { FMF.clear(); }
184
185   /// \brief Set the floating point math metadata to be used.
186   void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
187
188   /// \brief Set the fast-math flags to be used with generated fp-math operators
189   void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
190
191   //===--------------------------------------------------------------------===//
192   // RAII helpers.
193   //===--------------------------------------------------------------------===//
194
195   // \brief RAII object that stores the current insertion point and restores it
196   // when the object is destroyed. This includes the debug location.
197   class InsertPointGuard {
198     IRBuilderBase &Builder;
199     AssertingVH<BasicBlock> Block;
200     BasicBlock::iterator Point;
201     DebugLoc DbgLoc;
202
203     InsertPointGuard(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
204     InsertPointGuard &operator=(const InsertPointGuard &) LLVM_DELETED_FUNCTION;
205
206   public:
207     InsertPointGuard(IRBuilderBase &B)
208         : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
209           DbgLoc(B.getCurrentDebugLocation()) {}
210
211     ~InsertPointGuard() {
212       Builder.restoreIP(InsertPoint(Block, Point));
213       Builder.SetCurrentDebugLocation(DbgLoc);
214     }
215   };
216
217   // \brief RAII object that stores the current fast math settings and restores
218   // them when the object is destroyed.
219   class FastMathFlagGuard {
220     IRBuilderBase &Builder;
221     FastMathFlags FMF;
222     MDNode *FPMathTag;
223
224     FastMathFlagGuard(const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
225     FastMathFlagGuard &operator=(
226         const FastMathFlagGuard &) LLVM_DELETED_FUNCTION;
227
228   public:
229     FastMathFlagGuard(IRBuilderBase &B)
230         : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
231
232     ~FastMathFlagGuard() {
233       Builder.FMF = FMF;
234       Builder.DefaultFPMathTag = FPMathTag;
235     }
236   };
237
238   //===--------------------------------------------------------------------===//
239   // Miscellaneous creation methods.
240   //===--------------------------------------------------------------------===//
241
242   /// \brief Make a new global variable with initializer type i8*
243   ///
244   /// Make a new global variable with an initializer that has array of i8 type
245   /// filled in with the null terminated string value specified.  The new global
246   /// variable will be marked mergable with any others of the same contents.  If
247   /// Name is specified, it is the name of the global variable created.
248   Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
249
250   /// \brief Get a constant value representing either true or false.
251   ConstantInt *getInt1(bool V) {
252     return ConstantInt::get(getInt1Ty(), V);
253   }
254
255   /// \brief Get the constant value for i1 true.
256   ConstantInt *getTrue() {
257     return ConstantInt::getTrue(Context);
258   }
259
260   /// \brief Get the constant value for i1 false.
261   ConstantInt *getFalse() {
262     return ConstantInt::getFalse(Context);
263   }
264
265   /// \brief Get a constant 8-bit value.
266   ConstantInt *getInt8(uint8_t C) {
267     return ConstantInt::get(getInt8Ty(), C);
268   }
269
270   /// \brief Get a constant 16-bit value.
271   ConstantInt *getInt16(uint16_t C) {
272     return ConstantInt::get(getInt16Ty(), C);
273   }
274
275   /// \brief Get a constant 32-bit value.
276   ConstantInt *getInt32(uint32_t C) {
277     return ConstantInt::get(getInt32Ty(), C);
278   }
279
280   /// \brief Get a constant 64-bit value.
281   ConstantInt *getInt64(uint64_t C) {
282     return ConstantInt::get(getInt64Ty(), C);
283   }
284
285   /// \brief Get a constant N-bit value, zero extended or truncated from
286   /// a 64-bit value.
287   ConstantInt *getIntN(unsigned N, uint64_t C) {
288     return ConstantInt::get(getIntNTy(N), C);
289   }
290
291   /// \brief Get a constant integer value.
292   ConstantInt *getInt(const APInt &AI) {
293     return ConstantInt::get(Context, AI);
294   }
295
296   //===--------------------------------------------------------------------===//
297   // Type creation methods
298   //===--------------------------------------------------------------------===//
299
300   /// \brief Fetch the type representing a single bit
301   IntegerType *getInt1Ty() {
302     return Type::getInt1Ty(Context);
303   }
304
305   /// \brief Fetch the type representing an 8-bit integer.
306   IntegerType *getInt8Ty() {
307     return Type::getInt8Ty(Context);
308   }
309
310   /// \brief Fetch the type representing a 16-bit integer.
311   IntegerType *getInt16Ty() {
312     return Type::getInt16Ty(Context);
313   }
314
315   /// \brief Fetch the type representing a 32-bit integer.
316   IntegerType *getInt32Ty() {
317     return Type::getInt32Ty(Context);
318   }
319
320   /// \brief Fetch the type representing a 64-bit integer.
321   IntegerType *getInt64Ty() {
322     return Type::getInt64Ty(Context);
323   }
324
325   /// \brief Fetch the type representing an N-bit integer.
326   IntegerType *getIntNTy(unsigned N) {
327     return Type::getIntNTy(Context, N);
328   }
329
330   /// \brief Fetch the type representing a 32-bit floating point value.
331   Type *getFloatTy() {
332     return Type::getFloatTy(Context);
333   }
334
335   /// \brief Fetch the type representing a 64-bit floating point value.
336   Type *getDoubleTy() {
337     return Type::getDoubleTy(Context);
338   }
339
340   /// \brief Fetch the type representing void.
341   Type *getVoidTy() {
342     return Type::getVoidTy(Context);
343   }
344
345   /// \brief Fetch the type representing a pointer to an 8-bit integer value.
346   PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
347     return Type::getInt8PtrTy(Context, AddrSpace);
348   }
349
350   /// \brief Fetch the type representing a pointer to an integer value.
351   IntegerType* getIntPtrTy(const DataLayout *DL, unsigned AddrSpace = 0) {
352     return DL->getIntPtrType(Context, AddrSpace);
353   }
354
355   //===--------------------------------------------------------------------===//
356   // Intrinsic creation methods
357   //===--------------------------------------------------------------------===//
358
359   /// \brief Create and insert a memset to the specified pointer and the
360   /// specified value.
361   ///
362   /// If the pointer isn't an i8*, it will be converted.  If a TBAA tag is
363   /// specified, it will be added to the instruction.
364   CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
365                          bool isVolatile = false, MDNode *TBAATag = nullptr) {
366     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
367   }
368
369   CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
370                          bool isVolatile = false, MDNode *TBAATag = nullptr);
371
372   /// \brief Create and insert a memcpy between the specified pointers.
373   ///
374   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
375   /// specified, it will be added to the instruction.
376   CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
377                          bool isVolatile = false, MDNode *TBAATag = nullptr,
378                          MDNode *TBAAStructTag = nullptr) {
379     return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
380                         TBAAStructTag);
381   }
382
383   CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
384                          bool isVolatile = false, MDNode *TBAATag = nullptr,
385                          MDNode *TBAAStructTag = nullptr);
386
387   /// \brief Create and insert a memmove between the specified
388   /// pointers.
389   ///
390   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
391   /// specified, it will be added to the instruction.
392   CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
393                           bool isVolatile = false, MDNode *TBAATag = nullptr) {
394     return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
395   }
396
397   CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
398                           bool isVolatile = false, MDNode *TBAATag = nullptr);
399
400   /// \brief Create a lifetime.start intrinsic.
401   ///
402   /// If the pointer isn't i8* it will be converted.
403   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
404
405   /// \brief Create a lifetime.end intrinsic.
406   ///
407   /// If the pointer isn't i8* it will be converted.
408   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
409
410 private:
411   Value *getCastedInt8PtrValue(Value *Ptr);
412 };
413
414 /// \brief This provides a uniform API for creating instructions and inserting
415 /// them into a basic block: either at the end of a BasicBlock, or at a specific
416 /// iterator location in a block.
417 ///
418 /// Note that the builder does not expose the full generality of LLVM
419 /// instructions.  For access to extra instruction properties, use the mutators
420 /// (e.g. setVolatile) on the instructions after they have been
421 /// created. Convenience state exists to specify fast-math flags and fp-math
422 /// tags.
423 ///
424 /// The first template argument handles whether or not to preserve names in the
425 /// final instruction output. This defaults to on.  The second template argument
426 /// specifies a class to use for creating constants.  This defaults to creating
427 /// minimally folded constants.  The fourth template argument allows clients to
428 /// specify custom insertion hooks that are called on every newly created
429 /// insertion.
430 template<bool preserveNames = true, typename T = ConstantFolder,
431          typename Inserter = IRBuilderDefaultInserter<preserveNames> >
432 class IRBuilder : public IRBuilderBase, public Inserter {
433   T Folder;
434 public:
435   IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
436             MDNode *FPMathTag = nullptr)
437     : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
438   }
439
440   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
441     : IRBuilderBase(C, FPMathTag), Folder() {
442   }
443
444   explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
445     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
446     SetInsertPoint(TheBB);
447   }
448
449   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
450     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
451     SetInsertPoint(TheBB);
452   }
453
454   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
455     : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
456     SetInsertPoint(IP);
457     SetCurrentDebugLocation(IP->getDebugLoc());
458   }
459
460   explicit IRBuilder(Use &U, MDNode *FPMathTag = nullptr)
461     : IRBuilderBase(U->getContext(), FPMathTag), Folder() {
462     SetInsertPoint(U);
463     SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
464   }
465
466   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
467             MDNode *FPMathTag = nullptr)
468     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
469     SetInsertPoint(TheBB, IP);
470   }
471
472   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
473             MDNode *FPMathTag = nullptr)
474     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
475     SetInsertPoint(TheBB, IP);
476   }
477
478   /// \brief Get the constant folder being used.
479   const T &getFolder() { return Folder; }
480
481   /// \brief Return true if this builder is configured to actually add the
482   /// requested names to IR created through it.
483   bool isNamePreserving() const { return preserveNames; }
484
485   /// \brief Insert and return the specified instruction.
486   template<typename InstTy>
487   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
488     this->InsertHelper(I, Name, BB, InsertPt);
489     this->SetInstDebugLocation(I);
490     return I;
491   }
492
493   /// \brief No-op overload to handle constants.
494   Constant *Insert(Constant *C, const Twine& = "") const {
495     return C;
496   }
497
498   //===--------------------------------------------------------------------===//
499   // Instruction creation methods: Terminators
500   //===--------------------------------------------------------------------===//
501
502 private:
503   /// \brief Helper to add branch weight metadata onto an instruction.
504   /// \returns The annotated instruction.
505   template <typename InstTy>
506   InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
507     if (Weights)
508       I->setMetadata(LLVMContext::MD_prof, Weights);
509     return I;
510   }
511
512 public:
513   /// \brief Create a 'ret void' instruction.
514   ReturnInst *CreateRetVoid() {
515     return Insert(ReturnInst::Create(Context));
516   }
517
518   /// \brief Create a 'ret <val>' instruction.
519   ReturnInst *CreateRet(Value *V) {
520     return Insert(ReturnInst::Create(Context, V));
521   }
522
523   /// \brief Create a sequence of N insertvalue instructions,
524   /// with one Value from the retVals array each, that build a aggregate
525   /// return value one value at a time, and a ret instruction to return
526   /// the resulting aggregate value.
527   ///
528   /// This is a convenience function for code that uses aggregate return values
529   /// as a vehicle for having multiple return values.
530   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
531     Value *V = UndefValue::get(getCurrentFunctionReturnType());
532     for (unsigned i = 0; i != N; ++i)
533       V = CreateInsertValue(V, retVals[i], i, "mrv");
534     return Insert(ReturnInst::Create(Context, V));
535   }
536
537   /// \brief Create an unconditional 'br label X' instruction.
538   BranchInst *CreateBr(BasicBlock *Dest) {
539     return Insert(BranchInst::Create(Dest));
540   }
541
542   /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
543   /// instruction.
544   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
545                            MDNode *BranchWeights = nullptr) {
546     return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
547                                    BranchWeights));
548   }
549
550   /// \brief Create a switch instruction with the specified value, default dest,
551   /// and with a hint for the number of cases that will be added (for efficient
552   /// allocation).
553   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
554                            MDNode *BranchWeights = nullptr) {
555     return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
556                                    BranchWeights));
557   }
558
559   /// \brief Create an indirect branch instruction with the specified address
560   /// operand, with an optional hint for the number of destinations that will be
561   /// added (for efficient allocation).
562   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
563     return Insert(IndirectBrInst::Create(Addr, NumDests));
564   }
565
566   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
567                            BasicBlock *UnwindDest, const Twine &Name = "") {
568     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
569                                      ArrayRef<Value *>()),
570                   Name);
571   }
572   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
573                            BasicBlock *UnwindDest, Value *Arg1,
574                            const Twine &Name = "") {
575     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
576                   Name);
577   }
578   InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
579                             BasicBlock *UnwindDest, Value *Arg1,
580                             Value *Arg2, Value *Arg3,
581                             const Twine &Name = "") {
582     Value *Args[] = { Arg1, Arg2, Arg3 };
583     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
584                   Name);
585   }
586   /// \brief Create an invoke instruction.
587   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
588                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
589                            const Twine &Name = "") {
590     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
591                   Name);
592   }
593
594   ResumeInst *CreateResume(Value *Exn) {
595     return Insert(ResumeInst::Create(Exn));
596   }
597
598   UnreachableInst *CreateUnreachable() {
599     return Insert(new UnreachableInst(Context));
600   }
601
602   //===--------------------------------------------------------------------===//
603   // Instruction creation methods: Binary Operators
604   //===--------------------------------------------------------------------===//
605 private:
606   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
607                                           Value *LHS, Value *RHS,
608                                           const Twine &Name,
609                                           bool HasNUW, bool HasNSW) {
610     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
611     if (HasNUW) BO->setHasNoUnsignedWrap();
612     if (HasNSW) BO->setHasNoSignedWrap();
613     return BO;
614   }
615
616   Instruction *AddFPMathAttributes(Instruction *I,
617                                    MDNode *FPMathTag,
618                                    FastMathFlags FMF) const {
619     if (!FPMathTag)
620       FPMathTag = DefaultFPMathTag;
621     if (FPMathTag)
622       I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
623     I->setFastMathFlags(FMF);
624     return I;
625   }
626 public:
627   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
628                    bool HasNUW = false, bool HasNSW = false) {
629     if (Constant *LC = dyn_cast<Constant>(LHS))
630       if (Constant *RC = dyn_cast<Constant>(RHS))
631         return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
632     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
633                                    HasNUW, HasNSW);
634   }
635   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
636     return CreateAdd(LHS, RHS, Name, false, true);
637   }
638   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
639     return CreateAdd(LHS, RHS, Name, true, false);
640   }
641   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
642                     MDNode *FPMathTag = nullptr) {
643     if (Constant *LC = dyn_cast<Constant>(LHS))
644       if (Constant *RC = dyn_cast<Constant>(RHS))
645         return Insert(Folder.CreateFAdd(LC, RC), Name);
646     return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
647                                       FPMathTag, FMF), Name);
648   }
649   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
650                    bool HasNUW = false, bool HasNSW = false) {
651     if (Constant *LC = dyn_cast<Constant>(LHS))
652       if (Constant *RC = dyn_cast<Constant>(RHS))
653         return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
654     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
655                                    HasNUW, HasNSW);
656   }
657   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
658     return CreateSub(LHS, RHS, Name, false, true);
659   }
660   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
661     return CreateSub(LHS, RHS, Name, true, false);
662   }
663   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
664                     MDNode *FPMathTag = nullptr) {
665     if (Constant *LC = dyn_cast<Constant>(LHS))
666       if (Constant *RC = dyn_cast<Constant>(RHS))
667         return Insert(Folder.CreateFSub(LC, RC), Name);
668     return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
669                                       FPMathTag, FMF), Name);
670   }
671   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
672                    bool HasNUW = false, bool HasNSW = false) {
673     if (Constant *LC = dyn_cast<Constant>(LHS))
674       if (Constant *RC = dyn_cast<Constant>(RHS))
675         return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
676     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
677                                    HasNUW, HasNSW);
678   }
679   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
680     return CreateMul(LHS, RHS, Name, false, true);
681   }
682   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
683     return CreateMul(LHS, RHS, Name, true, false);
684   }
685   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
686                     MDNode *FPMathTag = nullptr) {
687     if (Constant *LC = dyn_cast<Constant>(LHS))
688       if (Constant *RC = dyn_cast<Constant>(RHS))
689         return Insert(Folder.CreateFMul(LC, RC), Name);
690     return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
691                                       FPMathTag, FMF), Name);
692   }
693   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
694                     bool isExact = false) {
695     if (Constant *LC = dyn_cast<Constant>(LHS))
696       if (Constant *RC = dyn_cast<Constant>(RHS))
697         return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
698     if (!isExact)
699       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
700     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
701   }
702   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
703     return CreateUDiv(LHS, RHS, Name, true);
704   }
705   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
706                     bool isExact = false) {
707     if (Constant *LC = dyn_cast<Constant>(LHS))
708       if (Constant *RC = dyn_cast<Constant>(RHS))
709         return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
710     if (!isExact)
711       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
712     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
713   }
714   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
715     return CreateSDiv(LHS, RHS, Name, true);
716   }
717   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
718                     MDNode *FPMathTag = nullptr) {
719     if (Constant *LC = dyn_cast<Constant>(LHS))
720       if (Constant *RC = dyn_cast<Constant>(RHS))
721         return Insert(Folder.CreateFDiv(LC, RC), Name);
722     return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
723                                       FPMathTag, FMF), Name);
724   }
725   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
726     if (Constant *LC = dyn_cast<Constant>(LHS))
727       if (Constant *RC = dyn_cast<Constant>(RHS))
728         return Insert(Folder.CreateURem(LC, RC), Name);
729     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
730   }
731   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
732     if (Constant *LC = dyn_cast<Constant>(LHS))
733       if (Constant *RC = dyn_cast<Constant>(RHS))
734         return Insert(Folder.CreateSRem(LC, RC), Name);
735     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
736   }
737   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
738                     MDNode *FPMathTag = nullptr) {
739     if (Constant *LC = dyn_cast<Constant>(LHS))
740       if (Constant *RC = dyn_cast<Constant>(RHS))
741         return Insert(Folder.CreateFRem(LC, RC), Name);
742     return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
743                                       FPMathTag, FMF), Name);
744   }
745
746   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
747                    bool HasNUW = false, bool HasNSW = false) {
748     if (Constant *LC = dyn_cast<Constant>(LHS))
749       if (Constant *RC = dyn_cast<Constant>(RHS))
750         return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
751     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
752                                    HasNUW, HasNSW);
753   }
754   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
755                    bool HasNUW = false, bool HasNSW = false) {
756     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
757                      HasNUW, HasNSW);
758   }
759   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
760                    bool HasNUW = false, bool HasNSW = false) {
761     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
762                      HasNUW, HasNSW);
763   }
764
765   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
766                     bool isExact = false) {
767     if (Constant *LC = dyn_cast<Constant>(LHS))
768       if (Constant *RC = dyn_cast<Constant>(RHS))
769         return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
770     if (!isExact)
771       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
772     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
773   }
774   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
775                     bool isExact = false) {
776     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
777   }
778   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
779                     bool isExact = false) {
780     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
781   }
782
783   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
784                     bool isExact = false) {
785     if (Constant *LC = dyn_cast<Constant>(LHS))
786       if (Constant *RC = dyn_cast<Constant>(RHS))
787         return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
788     if (!isExact)
789       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
790     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
791   }
792   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
793                     bool isExact = false) {
794     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
795   }
796   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
797                     bool isExact = false) {
798     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
799   }
800
801   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
802     if (Constant *RC = dyn_cast<Constant>(RHS)) {
803       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
804         return LHS;  // LHS & -1 -> LHS
805       if (Constant *LC = dyn_cast<Constant>(LHS))
806         return Insert(Folder.CreateAnd(LC, RC), Name);
807     }
808     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
809   }
810   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
811     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
812   }
813   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
814     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
815   }
816
817   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
818     if (Constant *RC = dyn_cast<Constant>(RHS)) {
819       if (RC->isNullValue())
820         return LHS;  // LHS | 0 -> LHS
821       if (Constant *LC = dyn_cast<Constant>(LHS))
822         return Insert(Folder.CreateOr(LC, RC), Name);
823     }
824     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
825   }
826   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
827     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
828   }
829   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
830     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
831   }
832
833   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
834     if (Constant *LC = dyn_cast<Constant>(LHS))
835       if (Constant *RC = dyn_cast<Constant>(RHS))
836         return Insert(Folder.CreateXor(LC, RC), Name);
837     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
838   }
839   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
840     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
841   }
842   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
843     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
844   }
845
846   Value *CreateBinOp(Instruction::BinaryOps Opc,
847                      Value *LHS, Value *RHS, const Twine &Name = "",
848                      MDNode *FPMathTag = nullptr) {
849     if (Constant *LC = dyn_cast<Constant>(LHS))
850       if (Constant *RC = dyn_cast<Constant>(RHS))
851         return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
852     llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
853     if (isa<FPMathOperator>(BinOp))
854       BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
855     return Insert(BinOp, Name);
856   }
857
858   Value *CreateNeg(Value *V, const Twine &Name = "",
859                    bool HasNUW = false, bool HasNSW = false) {
860     if (Constant *VC = dyn_cast<Constant>(V))
861       return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
862     BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
863     if (HasNUW) BO->setHasNoUnsignedWrap();
864     if (HasNSW) BO->setHasNoSignedWrap();
865     return BO;
866   }
867   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
868     return CreateNeg(V, Name, false, true);
869   }
870   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
871     return CreateNeg(V, Name, true, false);
872   }
873   Value *CreateFNeg(Value *V, const Twine &Name = "",
874                     MDNode *FPMathTag = nullptr) {
875     if (Constant *VC = dyn_cast<Constant>(V))
876       return Insert(Folder.CreateFNeg(VC), Name);
877     return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
878                                       FPMathTag, FMF), Name);
879   }
880   Value *CreateNot(Value *V, const Twine &Name = "") {
881     if (Constant *VC = dyn_cast<Constant>(V))
882       return Insert(Folder.CreateNot(VC), Name);
883     return Insert(BinaryOperator::CreateNot(V), Name);
884   }
885
886   //===--------------------------------------------------------------------===//
887   // Instruction creation methods: Memory Instructions
888   //===--------------------------------------------------------------------===//
889
890   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
891                            const Twine &Name = "") {
892     return Insert(new AllocaInst(Ty, ArraySize), Name);
893   }
894   // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
895   // converting the string to 'bool' for the isVolatile parameter.
896   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
897     return Insert(new LoadInst(Ptr), Name);
898   }
899   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
900     return Insert(new LoadInst(Ptr), Name);
901   }
902   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
903     return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
904   }
905   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
906     return Insert(new StoreInst(Val, Ptr, isVolatile));
907   }
908   // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
909   // correctly, instead of converting the string to 'bool' for the isVolatile
910   // parameter.
911   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
912     LoadInst *LI = CreateLoad(Ptr, Name);
913     LI->setAlignment(Align);
914     return LI;
915   }
916   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
917                               const Twine &Name = "") {
918     LoadInst *LI = CreateLoad(Ptr, Name);
919     LI->setAlignment(Align);
920     return LI;
921   }
922   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
923                               const Twine &Name = "") {
924     LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
925     LI->setAlignment(Align);
926     return LI;
927   }
928   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
929                                 bool isVolatile = false) {
930     StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
931     SI->setAlignment(Align);
932     return SI;
933   }
934   FenceInst *CreateFence(AtomicOrdering Ordering,
935                          SynchronizationScope SynchScope = CrossThread,
936                          const Twine &Name = "") {
937     return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
938   }
939   AtomicCmpXchgInst *
940   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
941                       AtomicOrdering SuccessOrdering,
942                       AtomicOrdering FailureOrdering,
943                       SynchronizationScope SynchScope = CrossThread) {
944     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
945                                         FailureOrdering, SynchScope));
946   }
947   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
948                                  AtomicOrdering Ordering,
949                                SynchronizationScope SynchScope = CrossThread) {
950     return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
951   }
952   Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
953                    const Twine &Name = "") {
954     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
955       // Every index must be constant.
956       size_t i, e;
957       for (i = 0, e = IdxList.size(); i != e; ++i)
958         if (!isa<Constant>(IdxList[i]))
959           break;
960       if (i == e)
961         return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
962     }
963     return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
964   }
965   Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
966                            const Twine &Name = "") {
967     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
968       // Every index must be constant.
969       size_t i, e;
970       for (i = 0, e = IdxList.size(); i != e; ++i)
971         if (!isa<Constant>(IdxList[i]))
972           break;
973       if (i == e)
974         return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
975     }
976     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
977   }
978   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
979     if (Constant *PC = dyn_cast<Constant>(Ptr))
980       if (Constant *IC = dyn_cast<Constant>(Idx))
981         return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
982     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
983   }
984   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
985     if (Constant *PC = dyn_cast<Constant>(Ptr))
986       if (Constant *IC = dyn_cast<Constant>(Idx))
987         return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
988     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
989   }
990   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
991     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
992
993     if (Constant *PC = dyn_cast<Constant>(Ptr))
994       return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
995
996     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
997   }
998   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
999                                     const Twine &Name = "") {
1000     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1001
1002     if (Constant *PC = dyn_cast<Constant>(Ptr))
1003       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1004
1005     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1006   }
1007   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1008                     const Twine &Name = "") {
1009     Value *Idxs[] = {
1010       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1011       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1012     };
1013
1014     if (Constant *PC = dyn_cast<Constant>(Ptr))
1015       return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1016
1017     return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1018   }
1019   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1020                                     const Twine &Name = "") {
1021     Value *Idxs[] = {
1022       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1023       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1024     };
1025
1026     if (Constant *PC = dyn_cast<Constant>(Ptr))
1027       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1028
1029     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1030   }
1031   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1032     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1033
1034     if (Constant *PC = dyn_cast<Constant>(Ptr))
1035       return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
1036
1037     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1038   }
1039   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1040                                     const Twine &Name = "") {
1041     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1042
1043     if (Constant *PC = dyn_cast<Constant>(Ptr))
1044       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1045
1046     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1047   }
1048   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1049                     const Twine &Name = "") {
1050     Value *Idxs[] = {
1051       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1052       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1053     };
1054
1055     if (Constant *PC = dyn_cast<Constant>(Ptr))
1056       return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1057
1058     return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1059   }
1060   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1061                                     const Twine &Name = "") {
1062     Value *Idxs[] = {
1063       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1064       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1065     };
1066
1067     if (Constant *PC = dyn_cast<Constant>(Ptr))
1068       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1069
1070     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1071   }
1072   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1073     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1074   }
1075
1076   /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1077   /// instead of a pointer to array of i8.
1078   Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1079     Value *gv = CreateGlobalString(Str, Name);
1080     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1081     Value *Args[] = { zero, zero };
1082     return CreateInBoundsGEP(gv, Args, Name);
1083   }
1084
1085   //===--------------------------------------------------------------------===//
1086   // Instruction creation methods: Cast/Conversion Operators
1087   //===--------------------------------------------------------------------===//
1088
1089   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1090     return CreateCast(Instruction::Trunc, V, DestTy, Name);
1091   }
1092   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1093     return CreateCast(Instruction::ZExt, V, DestTy, Name);
1094   }
1095   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1096     return CreateCast(Instruction::SExt, V, DestTy, Name);
1097   }
1098   /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1099   /// the value untouched if the type of V is already DestTy.
1100   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1101                            const Twine &Name = "") {
1102     assert(V->getType()->isIntOrIntVectorTy() &&
1103            DestTy->isIntOrIntVectorTy() &&
1104            "Can only zero extend/truncate integers!");
1105     Type *VTy = V->getType();
1106     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1107       return CreateZExt(V, DestTy, Name);
1108     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1109       return CreateTrunc(V, DestTy, Name);
1110     return V;
1111   }
1112   /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1113   /// the value untouched if the type of V is already DestTy.
1114   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1115                            const Twine &Name = "") {
1116     assert(V->getType()->isIntOrIntVectorTy() &&
1117            DestTy->isIntOrIntVectorTy() &&
1118            "Can only sign extend/truncate integers!");
1119     Type *VTy = V->getType();
1120     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1121       return CreateSExt(V, DestTy, Name);
1122     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1123       return CreateTrunc(V, DestTy, Name);
1124     return V;
1125   }
1126   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1127     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1128   }
1129   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1130     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1131   }
1132   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1133     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1134   }
1135   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1136     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1137   }
1138   Value *CreateFPTrunc(Value *V, Type *DestTy,
1139                        const Twine &Name = "") {
1140     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1141   }
1142   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1143     return CreateCast(Instruction::FPExt, V, DestTy, Name);
1144   }
1145   Value *CreatePtrToInt(Value *V, Type *DestTy,
1146                         const Twine &Name = "") {
1147     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1148   }
1149   Value *CreateIntToPtr(Value *V, Type *DestTy,
1150                         const Twine &Name = "") {
1151     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1152   }
1153   Value *CreateBitCast(Value *V, Type *DestTy,
1154                        const Twine &Name = "") {
1155     return CreateCast(Instruction::BitCast, V, DestTy, Name);
1156   }
1157   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1158                              const Twine &Name = "") {
1159     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1160   }
1161   Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1162                              const Twine &Name = "") {
1163     if (V->getType() == DestTy)
1164       return V;
1165     if (Constant *VC = dyn_cast<Constant>(V))
1166       return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1167     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1168   }
1169   Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1170                              const Twine &Name = "") {
1171     if (V->getType() == DestTy)
1172       return V;
1173     if (Constant *VC = dyn_cast<Constant>(V))
1174       return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1175     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1176   }
1177   Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1178                               const Twine &Name = "") {
1179     if (V->getType() == DestTy)
1180       return V;
1181     if (Constant *VC = dyn_cast<Constant>(V))
1182       return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1183     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1184   }
1185   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1186                     const Twine &Name = "") {
1187     if (V->getType() == DestTy)
1188       return V;
1189     if (Constant *VC = dyn_cast<Constant>(V))
1190       return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1191     return Insert(CastInst::Create(Op, V, DestTy), Name);
1192   }
1193   Value *CreatePointerCast(Value *V, Type *DestTy,
1194                            const Twine &Name = "") {
1195     if (V->getType() == DestTy)
1196       return V;
1197     if (Constant *VC = dyn_cast<Constant>(V))
1198       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1199     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1200   }
1201   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1202                        const Twine &Name = "") {
1203     if (V->getType() == DestTy)
1204       return V;
1205     if (Constant *VC = dyn_cast<Constant>(V))
1206       return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1207     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1208   }
1209 private:
1210   // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1211   // compile time error, instead of converting the string to bool for the
1212   // isSigned parameter.
1213   Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1214 public:
1215   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1216     if (V->getType() == DestTy)
1217       return V;
1218     if (Constant *VC = dyn_cast<Constant>(V))
1219       return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1220     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1221   }
1222
1223   //===--------------------------------------------------------------------===//
1224   // Instruction creation methods: Compare Instructions
1225   //===--------------------------------------------------------------------===//
1226
1227   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1228     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1229   }
1230   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1231     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1232   }
1233   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1234     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1235   }
1236   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1237     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1238   }
1239   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1240     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1241   }
1242   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1243     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1244   }
1245   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1246     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1247   }
1248   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1249     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1250   }
1251   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1252     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1253   }
1254   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1255     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1256   }
1257
1258   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1259     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1260   }
1261   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1262     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1263   }
1264   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1265     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1266   }
1267   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1268     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1269   }
1270   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1271     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1272   }
1273   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1274     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1275   }
1276   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1277     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1278   }
1279   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1280     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1281   }
1282   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1283     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1284   }
1285   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1286     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1287   }
1288   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1289     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1290   }
1291   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1292     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1293   }
1294   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1295     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1296   }
1297   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1298     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1299   }
1300
1301   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1302                     const Twine &Name = "") {
1303     if (Constant *LC = dyn_cast<Constant>(LHS))
1304       if (Constant *RC = dyn_cast<Constant>(RHS))
1305         return Insert(Folder.CreateICmp(P, LC, RC), Name);
1306     return Insert(new ICmpInst(P, LHS, RHS), Name);
1307   }
1308   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1309                     const Twine &Name = "") {
1310     if (Constant *LC = dyn_cast<Constant>(LHS))
1311       if (Constant *RC = dyn_cast<Constant>(RHS))
1312         return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1313     return Insert(new FCmpInst(P, LHS, RHS), Name);
1314   }
1315
1316   //===--------------------------------------------------------------------===//
1317   // Instruction creation methods: Other Instructions
1318   //===--------------------------------------------------------------------===//
1319
1320   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1321                      const Twine &Name = "") {
1322     return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1323   }
1324
1325   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1326     return Insert(CallInst::Create(Callee), Name);
1327   }
1328   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1329     return Insert(CallInst::Create(Callee, Arg), Name);
1330   }
1331   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1332                         const Twine &Name = "") {
1333     Value *Args[] = { Arg1, Arg2 };
1334     return Insert(CallInst::Create(Callee, Args), Name);
1335   }
1336   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1337                         const Twine &Name = "") {
1338     Value *Args[] = { Arg1, Arg2, Arg3 };
1339     return Insert(CallInst::Create(Callee, Args), Name);
1340   }
1341   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1342                         Value *Arg4, const Twine &Name = "") {
1343     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1344     return Insert(CallInst::Create(Callee, Args), Name);
1345   }
1346   CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1347                         Value *Arg4, Value *Arg5, const Twine &Name = "") {
1348     Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1349     return Insert(CallInst::Create(Callee, Args), Name);
1350   }
1351
1352   CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1353                        const Twine &Name = "") {
1354     return Insert(CallInst::Create(Callee, Args), Name);
1355   }
1356
1357   Value *CreateSelect(Value *C, Value *True, Value *False,
1358                       const Twine &Name = "") {
1359     if (Constant *CC = dyn_cast<Constant>(C))
1360       if (Constant *TC = dyn_cast<Constant>(True))
1361         if (Constant *FC = dyn_cast<Constant>(False))
1362           return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1363     return Insert(SelectInst::Create(C, True, False), Name);
1364   }
1365
1366   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1367     return Insert(new VAArgInst(List, Ty), Name);
1368   }
1369
1370   Value *CreateExtractElement(Value *Vec, Value *Idx,
1371                               const Twine &Name = "") {
1372     if (Constant *VC = dyn_cast<Constant>(Vec))
1373       if (Constant *IC = dyn_cast<Constant>(Idx))
1374         return Insert(Folder.CreateExtractElement(VC, IC), Name);
1375     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1376   }
1377
1378   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1379                              const Twine &Name = "") {
1380     if (Constant *VC = dyn_cast<Constant>(Vec))
1381       if (Constant *NC = dyn_cast<Constant>(NewElt))
1382         if (Constant *IC = dyn_cast<Constant>(Idx))
1383           return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1384     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1385   }
1386
1387   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1388                              const Twine &Name = "") {
1389     if (Constant *V1C = dyn_cast<Constant>(V1))
1390       if (Constant *V2C = dyn_cast<Constant>(V2))
1391         if (Constant *MC = dyn_cast<Constant>(Mask))
1392           return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1393     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1394   }
1395
1396   Value *CreateExtractValue(Value *Agg,
1397                             ArrayRef<unsigned> Idxs,
1398                             const Twine &Name = "") {
1399     if (Constant *AggC = dyn_cast<Constant>(Agg))
1400       return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1401     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1402   }
1403
1404   Value *CreateInsertValue(Value *Agg, Value *Val,
1405                            ArrayRef<unsigned> Idxs,
1406                            const Twine &Name = "") {
1407     if (Constant *AggC = dyn_cast<Constant>(Agg))
1408       if (Constant *ValC = dyn_cast<Constant>(Val))
1409         return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1410     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1411   }
1412
1413   LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1414                                    const Twine &Name = "") {
1415     return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1416   }
1417
1418   //===--------------------------------------------------------------------===//
1419   // Utility creation methods
1420   //===--------------------------------------------------------------------===//
1421
1422   /// \brief Return an i1 value testing if \p Arg is null.
1423   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1424     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1425                         Name);
1426   }
1427
1428   /// \brief Return an i1 value testing if \p Arg is not null.
1429   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1430     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1431                         Name);
1432   }
1433
1434   /// \brief Return the i64 difference between two pointer values, dividing out
1435   /// the size of the pointed-to objects.
1436   ///
1437   /// This is intended to implement C-style pointer subtraction. As such, the
1438   /// pointers must be appropriately aligned for their element types and
1439   /// pointing into the same object.
1440   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1441     assert(LHS->getType() == RHS->getType() &&
1442            "Pointer subtraction operand types must match!");
1443     PointerType *ArgType = cast<PointerType>(LHS->getType());
1444     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1445     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1446     Value *Difference = CreateSub(LHS_int, RHS_int);
1447     return CreateExactSDiv(Difference,
1448                            ConstantExpr::getSizeOf(ArgType->getElementType()),
1449                            Name);
1450   }
1451
1452   /// \brief Return a vector value that contains \arg V broadcasted to \p
1453   /// NumElts elements.
1454   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1455     assert(NumElts > 0 && "Cannot splat to an empty vector!");
1456
1457     // First insert it into an undef vector so we can shuffle it.
1458     Type *I32Ty = getInt32Ty();
1459     Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1460     V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1461                             Name + ".splatinsert");
1462
1463     // Shuffle the value across the desired number of elements.
1464     Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1465     return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1466   }
1467
1468   /// \brief Return a value that has been extracted from a larger integer type.
1469   Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1470                               IntegerType *ExtractedTy, uint64_t Offset,
1471                               const Twine &Name) {
1472     IntegerType *IntTy = cast<IntegerType>(From->getType());
1473     assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1474                DL.getTypeStoreSize(IntTy) &&
1475            "Element extends past full value");
1476     uint64_t ShAmt = 8 * Offset;
1477     Value *V = From;
1478     if (DL.isBigEndian())
1479       ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1480                    DL.getTypeStoreSize(ExtractedTy) - Offset);
1481     if (ShAmt) {
1482       V = CreateLShr(V, ShAmt, Name + ".shift");
1483     }
1484     assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1485            "Cannot extract to a larger integer!");
1486     if (ExtractedTy != IntTy) {
1487       V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1488     }
1489     return V;
1490   }
1491 };
1492
1493 // Create wrappers for C Binding types (see CBindingWrapping.h).
1494 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
1495
1496 }
1497
1498 #endif