Add scoped-noalias metadata
[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 16-bit floating point value.
331   Type *getHalfTy() {
332     return Type::getHalfTy(Context);
333   }
334
335   /// \brief Fetch the type representing a 32-bit floating point value.
336   Type *getFloatTy() {
337     return Type::getFloatTy(Context);
338   }
339
340   /// \brief Fetch the type representing a 64-bit floating point value.
341   Type *getDoubleTy() {
342     return Type::getDoubleTy(Context);
343   }
344
345   /// \brief Fetch the type representing void.
346   Type *getVoidTy() {
347     return Type::getVoidTy(Context);
348   }
349
350   /// \brief Fetch the type representing a pointer to an 8-bit integer value.
351   PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
352     return Type::getInt8PtrTy(Context, AddrSpace);
353   }
354
355   /// \brief Fetch the type representing a pointer to an integer value.
356   IntegerType* getIntPtrTy(const DataLayout *DL, unsigned AddrSpace = 0) {
357     return DL->getIntPtrType(Context, AddrSpace);
358   }
359
360   //===--------------------------------------------------------------------===//
361   // Intrinsic creation methods
362   //===--------------------------------------------------------------------===//
363
364   /// \brief Create and insert a memset to the specified pointer and the
365   /// specified value.
366   ///
367   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
368   /// specified, it will be added to the instruction. Likewise with alias.scope
369   /// and noalias tags.
370   CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
371                          bool isVolatile = false, MDNode *TBAATag = nullptr,
372                          MDNode *ScopeTag = nullptr,
373                          MDNode *NoAliasTag = nullptr) {
374     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
375                         TBAATag, ScopeTag, NoAliasTag);
376   }
377
378   CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
379                          bool isVolatile = false, MDNode *TBAATag = nullptr,
380                          MDNode *ScopeTag = nullptr,
381                          MDNode *NoAliasTag = nullptr);
382
383   /// \brief Create and insert a memcpy between the specified pointers.
384   ///
385   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
386   /// specified, it will be added to the instruction. Likewise with alias.scope
387   /// and noalias tags.
388   CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
389                          bool isVolatile = false, MDNode *TBAATag = nullptr,
390                          MDNode *TBAAStructTag = nullptr,
391                          MDNode *ScopeTag = nullptr,
392                          MDNode *NoAliasTag = nullptr) {
393     return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
394                         TBAAStructTag, ScopeTag, NoAliasTag);
395   }
396
397   CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
398                          bool isVolatile = false, MDNode *TBAATag = nullptr,
399                          MDNode *TBAAStructTag = nullptr,
400                          MDNode *ScopeTag = nullptr,
401                          MDNode *NoAliasTag = nullptr);
402
403   /// \brief Create and insert a memmove between the specified
404   /// pointers.
405   ///
406   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
407   /// specified, it will be added to the instruction. Likewise with alias.scope
408   /// and noalias tags.
409   CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
410                           bool isVolatile = false, MDNode *TBAATag = nullptr,
411                           MDNode *ScopeTag = nullptr,
412                           MDNode *NoAliasTag = nullptr) {
413     return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
414                          TBAATag, ScopeTag, NoAliasTag);
415   }
416
417   CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
418                           bool isVolatile = false, MDNode *TBAATag = nullptr,
419                           MDNode *ScopeTag = nullptr,
420                           MDNode *NoAliasTag = nullptr);
421
422   /// \brief Create a lifetime.start intrinsic.
423   ///
424   /// If the pointer isn't i8* it will be converted.
425   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
426
427   /// \brief Create a lifetime.end intrinsic.
428   ///
429   /// If the pointer isn't i8* it will be converted.
430   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
431
432 private:
433   Value *getCastedInt8PtrValue(Value *Ptr);
434 };
435
436 /// \brief This provides a uniform API for creating instructions and inserting
437 /// them into a basic block: either at the end of a BasicBlock, or at a specific
438 /// iterator location in a block.
439 ///
440 /// Note that the builder does not expose the full generality of LLVM
441 /// instructions.  For access to extra instruction properties, use the mutators
442 /// (e.g. setVolatile) on the instructions after they have been
443 /// created. Convenience state exists to specify fast-math flags and fp-math
444 /// tags.
445 ///
446 /// The first template argument handles whether or not to preserve names in the
447 /// final instruction output. This defaults to on.  The second template argument
448 /// specifies a class to use for creating constants.  This defaults to creating
449 /// minimally folded constants.  The fourth template argument allows clients to
450 /// specify custom insertion hooks that are called on every newly created
451 /// insertion.
452 template<bool preserveNames = true, typename T = ConstantFolder,
453          typename Inserter = IRBuilderDefaultInserter<preserveNames> >
454 class IRBuilder : public IRBuilderBase, public Inserter {
455   T Folder;
456 public:
457   IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
458             MDNode *FPMathTag = nullptr)
459     : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
460   }
461
462   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
463     : IRBuilderBase(C, FPMathTag), Folder() {
464   }
465
466   explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
467     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
468     SetInsertPoint(TheBB);
469   }
470
471   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
472     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
473     SetInsertPoint(TheBB);
474   }
475
476   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
477     : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
478     SetInsertPoint(IP);
479     SetCurrentDebugLocation(IP->getDebugLoc());
480   }
481
482   explicit IRBuilder(Use &U, MDNode *FPMathTag = nullptr)
483     : IRBuilderBase(U->getContext(), FPMathTag), Folder() {
484     SetInsertPoint(U);
485     SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
486   }
487
488   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
489             MDNode *FPMathTag = nullptr)
490     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
491     SetInsertPoint(TheBB, IP);
492   }
493
494   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
495             MDNode *FPMathTag = nullptr)
496     : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
497     SetInsertPoint(TheBB, IP);
498   }
499
500   /// \brief Get the constant folder being used.
501   const T &getFolder() { return Folder; }
502
503   /// \brief Return true if this builder is configured to actually add the
504   /// requested names to IR created through it.
505   bool isNamePreserving() const { return preserveNames; }
506
507   /// \brief Insert and return the specified instruction.
508   template<typename InstTy>
509   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
510     this->InsertHelper(I, Name, BB, InsertPt);
511     this->SetInstDebugLocation(I);
512     return I;
513   }
514
515   /// \brief No-op overload to handle constants.
516   Constant *Insert(Constant *C, const Twine& = "") const {
517     return C;
518   }
519
520   //===--------------------------------------------------------------------===//
521   // Instruction creation methods: Terminators
522   //===--------------------------------------------------------------------===//
523
524 private:
525   /// \brief Helper to add branch weight metadata onto an instruction.
526   /// \returns The annotated instruction.
527   template <typename InstTy>
528   InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
529     if (Weights)
530       I->setMetadata(LLVMContext::MD_prof, Weights);
531     return I;
532   }
533
534 public:
535   /// \brief Create a 'ret void' instruction.
536   ReturnInst *CreateRetVoid() {
537     return Insert(ReturnInst::Create(Context));
538   }
539
540   /// \brief Create a 'ret <val>' instruction.
541   ReturnInst *CreateRet(Value *V) {
542     return Insert(ReturnInst::Create(Context, V));
543   }
544
545   /// \brief Create a sequence of N insertvalue instructions,
546   /// with one Value from the retVals array each, that build a aggregate
547   /// return value one value at a time, and a ret instruction to return
548   /// the resulting aggregate value.
549   ///
550   /// This is a convenience function for code that uses aggregate return values
551   /// as a vehicle for having multiple return values.
552   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
553     Value *V = UndefValue::get(getCurrentFunctionReturnType());
554     for (unsigned i = 0; i != N; ++i)
555       V = CreateInsertValue(V, retVals[i], i, "mrv");
556     return Insert(ReturnInst::Create(Context, V));
557   }
558
559   /// \brief Create an unconditional 'br label X' instruction.
560   BranchInst *CreateBr(BasicBlock *Dest) {
561     return Insert(BranchInst::Create(Dest));
562   }
563
564   /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
565   /// instruction.
566   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
567                            MDNode *BranchWeights = nullptr) {
568     return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
569                                    BranchWeights));
570   }
571
572   /// \brief Create a switch instruction with the specified value, default dest,
573   /// and with a hint for the number of cases that will be added (for efficient
574   /// allocation).
575   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
576                            MDNode *BranchWeights = nullptr) {
577     return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
578                                    BranchWeights));
579   }
580
581   /// \brief Create an indirect branch instruction with the specified address
582   /// operand, with an optional hint for the number of destinations that will be
583   /// added (for efficient allocation).
584   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
585     return Insert(IndirectBrInst::Create(Addr, NumDests));
586   }
587
588   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
589                            BasicBlock *UnwindDest, const Twine &Name = "") {
590     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
591                                      ArrayRef<Value *>()),
592                   Name);
593   }
594   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
595                            BasicBlock *UnwindDest, Value *Arg1,
596                            const Twine &Name = "") {
597     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
598                   Name);
599   }
600   InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
601                             BasicBlock *UnwindDest, Value *Arg1,
602                             Value *Arg2, Value *Arg3,
603                             const Twine &Name = "") {
604     Value *Args[] = { Arg1, Arg2, Arg3 };
605     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
606                   Name);
607   }
608   /// \brief Create an invoke instruction.
609   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
610                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
611                            const Twine &Name = "") {
612     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
613                   Name);
614   }
615
616   ResumeInst *CreateResume(Value *Exn) {
617     return Insert(ResumeInst::Create(Exn));
618   }
619
620   UnreachableInst *CreateUnreachable() {
621     return Insert(new UnreachableInst(Context));
622   }
623
624   //===--------------------------------------------------------------------===//
625   // Instruction creation methods: Binary Operators
626   //===--------------------------------------------------------------------===//
627 private:
628   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
629                                           Value *LHS, Value *RHS,
630                                           const Twine &Name,
631                                           bool HasNUW, bool HasNSW) {
632     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
633     if (HasNUW) BO->setHasNoUnsignedWrap();
634     if (HasNSW) BO->setHasNoSignedWrap();
635     return BO;
636   }
637
638   Instruction *AddFPMathAttributes(Instruction *I,
639                                    MDNode *FPMathTag,
640                                    FastMathFlags FMF) const {
641     if (!FPMathTag)
642       FPMathTag = DefaultFPMathTag;
643     if (FPMathTag)
644       I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
645     I->setFastMathFlags(FMF);
646     return I;
647   }
648 public:
649   Value *CreateAdd(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.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
654     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
655                                    HasNUW, HasNSW);
656   }
657   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
658     return CreateAdd(LHS, RHS, Name, false, true);
659   }
660   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
661     return CreateAdd(LHS, RHS, Name, true, false);
662   }
663   Value *CreateFAdd(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.CreateFAdd(LC, RC), Name);
668     return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
669                                       FPMathTag, FMF), Name);
670   }
671   Value *CreateSub(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.CreateSub(LC, RC, HasNUW, HasNSW), Name);
676     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
677                                    HasNUW, HasNSW);
678   }
679   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
680     return CreateSub(LHS, RHS, Name, false, true);
681   }
682   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
683     return CreateSub(LHS, RHS, Name, true, false);
684   }
685   Value *CreateFSub(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.CreateFSub(LC, RC), Name);
690     return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
691                                       FPMathTag, FMF), Name);
692   }
693   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
694                    bool HasNUW = false, bool HasNSW = false) {
695     if (Constant *LC = dyn_cast<Constant>(LHS))
696       if (Constant *RC = dyn_cast<Constant>(RHS))
697         return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
698     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
699                                    HasNUW, HasNSW);
700   }
701   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
702     return CreateMul(LHS, RHS, Name, false, true);
703   }
704   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
705     return CreateMul(LHS, RHS, Name, true, false);
706   }
707   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
708                     MDNode *FPMathTag = nullptr) {
709     if (Constant *LC = dyn_cast<Constant>(LHS))
710       if (Constant *RC = dyn_cast<Constant>(RHS))
711         return Insert(Folder.CreateFMul(LC, RC), Name);
712     return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
713                                       FPMathTag, FMF), Name);
714   }
715   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
716                     bool isExact = false) {
717     if (Constant *LC = dyn_cast<Constant>(LHS))
718       if (Constant *RC = dyn_cast<Constant>(RHS))
719         return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
720     if (!isExact)
721       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
722     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
723   }
724   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
725     return CreateUDiv(LHS, RHS, Name, true);
726   }
727   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
728                     bool isExact = false) {
729     if (Constant *LC = dyn_cast<Constant>(LHS))
730       if (Constant *RC = dyn_cast<Constant>(RHS))
731         return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
732     if (!isExact)
733       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
734     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
735   }
736   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
737     return CreateSDiv(LHS, RHS, Name, true);
738   }
739   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
740                     MDNode *FPMathTag = nullptr) {
741     if (Constant *LC = dyn_cast<Constant>(LHS))
742       if (Constant *RC = dyn_cast<Constant>(RHS))
743         return Insert(Folder.CreateFDiv(LC, RC), Name);
744     return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
745                                       FPMathTag, FMF), Name);
746   }
747   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
748     if (Constant *LC = dyn_cast<Constant>(LHS))
749       if (Constant *RC = dyn_cast<Constant>(RHS))
750         return Insert(Folder.CreateURem(LC, RC), Name);
751     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
752   }
753   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
754     if (Constant *LC = dyn_cast<Constant>(LHS))
755       if (Constant *RC = dyn_cast<Constant>(RHS))
756         return Insert(Folder.CreateSRem(LC, RC), Name);
757     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
758   }
759   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
760                     MDNode *FPMathTag = nullptr) {
761     if (Constant *LC = dyn_cast<Constant>(LHS))
762       if (Constant *RC = dyn_cast<Constant>(RHS))
763         return Insert(Folder.CreateFRem(LC, RC), Name);
764     return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
765                                       FPMathTag, FMF), Name);
766   }
767
768   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
769                    bool HasNUW = false, bool HasNSW = false) {
770     if (Constant *LC = dyn_cast<Constant>(LHS))
771       if (Constant *RC = dyn_cast<Constant>(RHS))
772         return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
773     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
774                                    HasNUW, HasNSW);
775   }
776   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
777                    bool HasNUW = false, bool HasNSW = false) {
778     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
779                      HasNUW, HasNSW);
780   }
781   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
782                    bool HasNUW = false, bool HasNSW = false) {
783     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
784                      HasNUW, HasNSW);
785   }
786
787   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
788                     bool isExact = false) {
789     if (Constant *LC = dyn_cast<Constant>(LHS))
790       if (Constant *RC = dyn_cast<Constant>(RHS))
791         return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
792     if (!isExact)
793       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
794     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
795   }
796   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
797                     bool isExact = false) {
798     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
799   }
800   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
801                     bool isExact = false) {
802     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
803   }
804
805   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
806                     bool isExact = false) {
807     if (Constant *LC = dyn_cast<Constant>(LHS))
808       if (Constant *RC = dyn_cast<Constant>(RHS))
809         return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
810     if (!isExact)
811       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
812     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
813   }
814   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
815                     bool isExact = false) {
816     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
817   }
818   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
819                     bool isExact = false) {
820     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
821   }
822
823   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
824     if (Constant *RC = dyn_cast<Constant>(RHS)) {
825       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
826         return LHS;  // LHS & -1 -> LHS
827       if (Constant *LC = dyn_cast<Constant>(LHS))
828         return Insert(Folder.CreateAnd(LC, RC), Name);
829     }
830     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
831   }
832   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
833     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
834   }
835   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
836     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
837   }
838
839   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
840     if (Constant *RC = dyn_cast<Constant>(RHS)) {
841       if (RC->isNullValue())
842         return LHS;  // LHS | 0 -> LHS
843       if (Constant *LC = dyn_cast<Constant>(LHS))
844         return Insert(Folder.CreateOr(LC, RC), Name);
845     }
846     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
847   }
848   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
849     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
850   }
851   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
852     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
853   }
854
855   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
856     if (Constant *LC = dyn_cast<Constant>(LHS))
857       if (Constant *RC = dyn_cast<Constant>(RHS))
858         return Insert(Folder.CreateXor(LC, RC), Name);
859     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
860   }
861   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
862     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
863   }
864   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
865     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
866   }
867
868   Value *CreateBinOp(Instruction::BinaryOps Opc,
869                      Value *LHS, Value *RHS, const Twine &Name = "",
870                      MDNode *FPMathTag = nullptr) {
871     if (Constant *LC = dyn_cast<Constant>(LHS))
872       if (Constant *RC = dyn_cast<Constant>(RHS))
873         return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
874     llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
875     if (isa<FPMathOperator>(BinOp))
876       BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
877     return Insert(BinOp, Name);
878   }
879
880   Value *CreateNeg(Value *V, const Twine &Name = "",
881                    bool HasNUW = false, bool HasNSW = false) {
882     if (Constant *VC = dyn_cast<Constant>(V))
883       return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
884     BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
885     if (HasNUW) BO->setHasNoUnsignedWrap();
886     if (HasNSW) BO->setHasNoSignedWrap();
887     return BO;
888   }
889   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
890     return CreateNeg(V, Name, false, true);
891   }
892   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
893     return CreateNeg(V, Name, true, false);
894   }
895   Value *CreateFNeg(Value *V, const Twine &Name = "",
896                     MDNode *FPMathTag = nullptr) {
897     if (Constant *VC = dyn_cast<Constant>(V))
898       return Insert(Folder.CreateFNeg(VC), Name);
899     return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
900                                       FPMathTag, FMF), Name);
901   }
902   Value *CreateNot(Value *V, const Twine &Name = "") {
903     if (Constant *VC = dyn_cast<Constant>(V))
904       return Insert(Folder.CreateNot(VC), Name);
905     return Insert(BinaryOperator::CreateNot(V), Name);
906   }
907
908   //===--------------------------------------------------------------------===//
909   // Instruction creation methods: Memory Instructions
910   //===--------------------------------------------------------------------===//
911
912   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
913                            const Twine &Name = "") {
914     return Insert(new AllocaInst(Ty, ArraySize), Name);
915   }
916   // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
917   // converting the string to 'bool' for the isVolatile parameter.
918   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
919     return Insert(new LoadInst(Ptr), Name);
920   }
921   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
922     return Insert(new LoadInst(Ptr), Name);
923   }
924   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
925     return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
926   }
927   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
928     return Insert(new StoreInst(Val, Ptr, isVolatile));
929   }
930   // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
931   // correctly, instead of converting the string to 'bool' for the isVolatile
932   // parameter.
933   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
934     LoadInst *LI = CreateLoad(Ptr, Name);
935     LI->setAlignment(Align);
936     return LI;
937   }
938   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
939                               const Twine &Name = "") {
940     LoadInst *LI = CreateLoad(Ptr, Name);
941     LI->setAlignment(Align);
942     return LI;
943   }
944   LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
945                               const Twine &Name = "") {
946     LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
947     LI->setAlignment(Align);
948     return LI;
949   }
950   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
951                                 bool isVolatile = false) {
952     StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
953     SI->setAlignment(Align);
954     return SI;
955   }
956   FenceInst *CreateFence(AtomicOrdering Ordering,
957                          SynchronizationScope SynchScope = CrossThread,
958                          const Twine &Name = "") {
959     return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
960   }
961   AtomicCmpXchgInst *
962   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
963                       AtomicOrdering SuccessOrdering,
964                       AtomicOrdering FailureOrdering,
965                       SynchronizationScope SynchScope = CrossThread) {
966     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
967                                         FailureOrdering, SynchScope));
968   }
969   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
970                                  AtomicOrdering Ordering,
971                                SynchronizationScope SynchScope = CrossThread) {
972     return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
973   }
974   Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
975                    const Twine &Name = "") {
976     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
977       // Every index must be constant.
978       size_t i, e;
979       for (i = 0, e = IdxList.size(); i != e; ++i)
980         if (!isa<Constant>(IdxList[i]))
981           break;
982       if (i == e)
983         return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
984     }
985     return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
986   }
987   Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
988                            const Twine &Name = "") {
989     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
990       // Every index must be constant.
991       size_t i, e;
992       for (i = 0, e = IdxList.size(); i != e; ++i)
993         if (!isa<Constant>(IdxList[i]))
994           break;
995       if (i == e)
996         return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
997     }
998     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
999   }
1000   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1001     if (Constant *PC = dyn_cast<Constant>(Ptr))
1002       if (Constant *IC = dyn_cast<Constant>(Idx))
1003         return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
1004     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1005   }
1006   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1007     if (Constant *PC = dyn_cast<Constant>(Ptr))
1008       if (Constant *IC = dyn_cast<Constant>(Idx))
1009         return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
1010     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1011   }
1012   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1013     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1014
1015     if (Constant *PC = dyn_cast<Constant>(Ptr))
1016       return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
1017
1018     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1019   }
1020   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
1021                                     const Twine &Name = "") {
1022     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1023
1024     if (Constant *PC = dyn_cast<Constant>(Ptr))
1025       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1026
1027     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1028   }
1029   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1030                     const Twine &Name = "") {
1031     Value *Idxs[] = {
1032       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1033       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1034     };
1035
1036     if (Constant *PC = dyn_cast<Constant>(Ptr))
1037       return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1038
1039     return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1040   }
1041   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1042                                     const Twine &Name = "") {
1043     Value *Idxs[] = {
1044       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1045       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1046     };
1047
1048     if (Constant *PC = dyn_cast<Constant>(Ptr))
1049       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1050
1051     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1052   }
1053   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1054     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1055
1056     if (Constant *PC = dyn_cast<Constant>(Ptr))
1057       return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
1058
1059     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
1060   }
1061   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1062                                     const Twine &Name = "") {
1063     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1064
1065     if (Constant *PC = dyn_cast<Constant>(Ptr))
1066       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
1067
1068     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
1069   }
1070   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1071                     const Twine &Name = "") {
1072     Value *Idxs[] = {
1073       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1074       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1075     };
1076
1077     if (Constant *PC = dyn_cast<Constant>(Ptr))
1078       return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
1079
1080     return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
1081   }
1082   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1083                                     const Twine &Name = "") {
1084     Value *Idxs[] = {
1085       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1086       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1087     };
1088
1089     if (Constant *PC = dyn_cast<Constant>(Ptr))
1090       return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
1091
1092     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
1093   }
1094   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1095     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1096   }
1097
1098   /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1099   /// instead of a pointer to array of i8.
1100   Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1101     Value *gv = CreateGlobalString(Str, Name);
1102     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1103     Value *Args[] = { zero, zero };
1104     return CreateInBoundsGEP(gv, Args, Name);
1105   }
1106
1107   //===--------------------------------------------------------------------===//
1108   // Instruction creation methods: Cast/Conversion Operators
1109   //===--------------------------------------------------------------------===//
1110
1111   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1112     return CreateCast(Instruction::Trunc, V, DestTy, Name);
1113   }
1114   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1115     return CreateCast(Instruction::ZExt, V, DestTy, Name);
1116   }
1117   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1118     return CreateCast(Instruction::SExt, V, DestTy, Name);
1119   }
1120   /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1121   /// the value untouched if the type of V is already DestTy.
1122   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1123                            const Twine &Name = "") {
1124     assert(V->getType()->isIntOrIntVectorTy() &&
1125            DestTy->isIntOrIntVectorTy() &&
1126            "Can only zero extend/truncate integers!");
1127     Type *VTy = V->getType();
1128     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1129       return CreateZExt(V, DestTy, Name);
1130     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1131       return CreateTrunc(V, DestTy, Name);
1132     return V;
1133   }
1134   /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1135   /// the value untouched if the type of V is already DestTy.
1136   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1137                            const Twine &Name = "") {
1138     assert(V->getType()->isIntOrIntVectorTy() &&
1139            DestTy->isIntOrIntVectorTy() &&
1140            "Can only sign extend/truncate integers!");
1141     Type *VTy = V->getType();
1142     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1143       return CreateSExt(V, DestTy, Name);
1144     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1145       return CreateTrunc(V, DestTy, Name);
1146     return V;
1147   }
1148   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1149     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1150   }
1151   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1152     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1153   }
1154   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1155     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1156   }
1157   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1158     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1159   }
1160   Value *CreateFPTrunc(Value *V, Type *DestTy,
1161                        const Twine &Name = "") {
1162     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1163   }
1164   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1165     return CreateCast(Instruction::FPExt, V, DestTy, Name);
1166   }
1167   Value *CreatePtrToInt(Value *V, Type *DestTy,
1168                         const Twine &Name = "") {
1169     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1170   }
1171   Value *CreateIntToPtr(Value *V, Type *DestTy,
1172                         const Twine &Name = "") {
1173     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1174   }
1175   Value *CreateBitCast(Value *V, Type *DestTy,
1176                        const Twine &Name = "") {
1177     return CreateCast(Instruction::BitCast, V, DestTy, Name);
1178   }
1179   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1180                              const Twine &Name = "") {
1181     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1182   }
1183   Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1184                              const Twine &Name = "") {
1185     if (V->getType() == DestTy)
1186       return V;
1187     if (Constant *VC = dyn_cast<Constant>(V))
1188       return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1189     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1190   }
1191   Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1192                              const Twine &Name = "") {
1193     if (V->getType() == DestTy)
1194       return V;
1195     if (Constant *VC = dyn_cast<Constant>(V))
1196       return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1197     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1198   }
1199   Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1200                               const Twine &Name = "") {
1201     if (V->getType() == DestTy)
1202       return V;
1203     if (Constant *VC = dyn_cast<Constant>(V))
1204       return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1205     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1206   }
1207   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1208                     const Twine &Name = "") {
1209     if (V->getType() == DestTy)
1210       return V;
1211     if (Constant *VC = dyn_cast<Constant>(V))
1212       return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1213     return Insert(CastInst::Create(Op, V, DestTy), Name);
1214   }
1215   Value *CreatePointerCast(Value *V, Type *DestTy,
1216                            const Twine &Name = "") {
1217     if (V->getType() == DestTy)
1218       return V;
1219     if (Constant *VC = dyn_cast<Constant>(V))
1220       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1221     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1222   }
1223
1224   Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1225                                              const Twine &Name = "") {
1226     if (V->getType() == DestTy)
1227       return V;
1228
1229     if (Constant *VC = dyn_cast<Constant>(V)) {
1230       return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1231                     Name);
1232     }
1233
1234     return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1235                   Name);
1236   }
1237
1238   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1239                        const Twine &Name = "") {
1240     if (V->getType() == DestTy)
1241       return V;
1242     if (Constant *VC = dyn_cast<Constant>(V))
1243       return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1244     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1245   }
1246 private:
1247   // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1248   // compile time error, instead of converting the string to bool for the
1249   // isSigned parameter.
1250   Value *CreateIntCast(Value *, Type *, const char *) LLVM_DELETED_FUNCTION;
1251 public:
1252   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1253     if (V->getType() == DestTy)
1254       return V;
1255     if (Constant *VC = dyn_cast<Constant>(V))
1256       return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1257     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1258   }
1259
1260   //===--------------------------------------------------------------------===//
1261   // Instruction creation methods: Compare Instructions
1262   //===--------------------------------------------------------------------===//
1263
1264   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1265     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1266   }
1267   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1268     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1269   }
1270   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1271     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1272   }
1273   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1274     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1275   }
1276   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1277     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1278   }
1279   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1280     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1281   }
1282   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1283     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1284   }
1285   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1286     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1287   }
1288   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1289     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1290   }
1291   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1292     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1293   }
1294
1295   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1296     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1297   }
1298   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1299     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1300   }
1301   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1302     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1303   }
1304   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1305     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1306   }
1307   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1308     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1309   }
1310   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1311     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1312   }
1313   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1314     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1315   }
1316   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1317     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1318   }
1319   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1320     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1321   }
1322   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1323     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1324   }
1325   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1326     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1327   }
1328   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1329     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1330   }
1331   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1332     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1333   }
1334   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1335     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1336   }
1337
1338   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1339                     const Twine &Name = "") {
1340     if (Constant *LC = dyn_cast<Constant>(LHS))
1341       if (Constant *RC = dyn_cast<Constant>(RHS))
1342         return Insert(Folder.CreateICmp(P, LC, RC), Name);
1343     return Insert(new ICmpInst(P, LHS, RHS), Name);
1344   }
1345   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1346                     const Twine &Name = "") {
1347     if (Constant *LC = dyn_cast<Constant>(LHS))
1348       if (Constant *RC = dyn_cast<Constant>(RHS))
1349         return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1350     return Insert(new FCmpInst(P, LHS, RHS), Name);
1351   }
1352
1353   //===--------------------------------------------------------------------===//
1354   // Instruction creation methods: Other Instructions
1355   //===--------------------------------------------------------------------===//
1356
1357   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1358                      const Twine &Name = "") {
1359     return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1360   }
1361
1362   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1363     return Insert(CallInst::Create(Callee), Name);
1364   }
1365   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1366     return Insert(CallInst::Create(Callee, Arg), Name);
1367   }
1368   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1369                         const Twine &Name = "") {
1370     Value *Args[] = { Arg1, Arg2 };
1371     return Insert(CallInst::Create(Callee, Args), Name);
1372   }
1373   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1374                         const Twine &Name = "") {
1375     Value *Args[] = { Arg1, Arg2, Arg3 };
1376     return Insert(CallInst::Create(Callee, Args), Name);
1377   }
1378   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1379                         Value *Arg4, const Twine &Name = "") {
1380     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1381     return Insert(CallInst::Create(Callee, Args), Name);
1382   }
1383   CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1384                         Value *Arg4, Value *Arg5, const Twine &Name = "") {
1385     Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1386     return Insert(CallInst::Create(Callee, Args), Name);
1387   }
1388
1389   CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1390                        const Twine &Name = "") {
1391     return Insert(CallInst::Create(Callee, Args), Name);
1392   }
1393
1394   Value *CreateSelect(Value *C, Value *True, Value *False,
1395                       const Twine &Name = "") {
1396     if (Constant *CC = dyn_cast<Constant>(C))
1397       if (Constant *TC = dyn_cast<Constant>(True))
1398         if (Constant *FC = dyn_cast<Constant>(False))
1399           return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1400     return Insert(SelectInst::Create(C, True, False), Name);
1401   }
1402
1403   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1404     return Insert(new VAArgInst(List, Ty), Name);
1405   }
1406
1407   Value *CreateExtractElement(Value *Vec, Value *Idx,
1408                               const Twine &Name = "") {
1409     if (Constant *VC = dyn_cast<Constant>(Vec))
1410       if (Constant *IC = dyn_cast<Constant>(Idx))
1411         return Insert(Folder.CreateExtractElement(VC, IC), Name);
1412     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1413   }
1414
1415   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1416                              const Twine &Name = "") {
1417     if (Constant *VC = dyn_cast<Constant>(Vec))
1418       if (Constant *NC = dyn_cast<Constant>(NewElt))
1419         if (Constant *IC = dyn_cast<Constant>(Idx))
1420           return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1421     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1422   }
1423
1424   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1425                              const Twine &Name = "") {
1426     if (Constant *V1C = dyn_cast<Constant>(V1))
1427       if (Constant *V2C = dyn_cast<Constant>(V2))
1428         if (Constant *MC = dyn_cast<Constant>(Mask))
1429           return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1430     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1431   }
1432
1433   Value *CreateExtractValue(Value *Agg,
1434                             ArrayRef<unsigned> Idxs,
1435                             const Twine &Name = "") {
1436     if (Constant *AggC = dyn_cast<Constant>(Agg))
1437       return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1438     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1439   }
1440
1441   Value *CreateInsertValue(Value *Agg, Value *Val,
1442                            ArrayRef<unsigned> Idxs,
1443                            const Twine &Name = "") {
1444     if (Constant *AggC = dyn_cast<Constant>(Agg))
1445       if (Constant *ValC = dyn_cast<Constant>(Val))
1446         return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1447     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1448   }
1449
1450   LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1451                                    const Twine &Name = "") {
1452     return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1453   }
1454
1455   //===--------------------------------------------------------------------===//
1456   // Utility creation methods
1457   //===--------------------------------------------------------------------===//
1458
1459   /// \brief Return an i1 value testing if \p Arg is null.
1460   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1461     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1462                         Name);
1463   }
1464
1465   /// \brief Return an i1 value testing if \p Arg is not null.
1466   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1467     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1468                         Name);
1469   }
1470
1471   /// \brief Return the i64 difference between two pointer values, dividing out
1472   /// the size of the pointed-to objects.
1473   ///
1474   /// This is intended to implement C-style pointer subtraction. As such, the
1475   /// pointers must be appropriately aligned for their element types and
1476   /// pointing into the same object.
1477   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1478     assert(LHS->getType() == RHS->getType() &&
1479            "Pointer subtraction operand types must match!");
1480     PointerType *ArgType = cast<PointerType>(LHS->getType());
1481     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1482     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1483     Value *Difference = CreateSub(LHS_int, RHS_int);
1484     return CreateExactSDiv(Difference,
1485                            ConstantExpr::getSizeOf(ArgType->getElementType()),
1486                            Name);
1487   }
1488
1489   /// \brief Return a vector value that contains \arg V broadcasted to \p
1490   /// NumElts elements.
1491   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1492     assert(NumElts > 0 && "Cannot splat to an empty vector!");
1493
1494     // First insert it into an undef vector so we can shuffle it.
1495     Type *I32Ty = getInt32Ty();
1496     Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1497     V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1498                             Name + ".splatinsert");
1499
1500     // Shuffle the value across the desired number of elements.
1501     Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1502     return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1503   }
1504
1505   /// \brief Return a value that has been extracted from a larger integer type.
1506   Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1507                               IntegerType *ExtractedTy, uint64_t Offset,
1508                               const Twine &Name) {
1509     IntegerType *IntTy = cast<IntegerType>(From->getType());
1510     assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1511                DL.getTypeStoreSize(IntTy) &&
1512            "Element extends past full value");
1513     uint64_t ShAmt = 8 * Offset;
1514     Value *V = From;
1515     if (DL.isBigEndian())
1516       ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1517                    DL.getTypeStoreSize(ExtractedTy) - Offset);
1518     if (ShAmt) {
1519       V = CreateLShr(V, ShAmt, Name + ".shift");
1520     }
1521     assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1522            "Cannot extract to a larger integer!");
1523     if (ExtractedTy != IntTy) {
1524       V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1525     }
1526     return V;
1527   }
1528 };
1529
1530 // Create wrappers for C Binding types (see CBindingWrapping.h).
1531 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
1532
1533 }
1534
1535 #endif