Add support for stpncpy_chk.
[oota-llvm.git] / include / llvm / Support / IRBuilder.h
1 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- 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_SUPPORT_IRBUILDER_H
16 #define LLVM_SUPPORT_IRBUILDER_H
17
18 #include "llvm/Instructions.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ConstantFolder.h"
22
23 namespace llvm {
24   class MDNode;
25
26 /// IRBuilderDefaultInserter - This provides the default implementation of the
27 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
28 /// created by IRBuilder and needs to be inserted.  By default, this inserts the
29 /// instruction at the insertion point.
30 template <bool preserveNames = true>
31 class IRBuilderDefaultInserter {
32 protected:
33   void InsertHelper(Instruction *I, const Twine &Name,
34                     BasicBlock *BB, BasicBlock::iterator InsertPt) const {
35     if (BB) BB->getInstList().insert(InsertPt, I);
36     if (preserveNames)
37       I->setName(Name);
38   }
39 };
40
41 /// IRBuilderBase - Common base class shared among various IRBuilders.
42 class IRBuilderBase {
43   DebugLoc CurDbgLocation;
44 protected:
45   BasicBlock *BB;
46   BasicBlock::iterator InsertPt;
47   LLVMContext &Context;
48 public:
49   
50   IRBuilderBase(LLVMContext &context)
51     : Context(context) {
52     ClearInsertionPoint();
53   }
54   
55   //===--------------------------------------------------------------------===//
56   // Builder configuration methods
57   //===--------------------------------------------------------------------===//
58   
59   /// ClearInsertionPoint - Clear the insertion point: created instructions will
60   /// not be inserted into a block.
61   void ClearInsertionPoint() {
62     BB = 0;
63   }
64   
65   BasicBlock *GetInsertBlock() const { return BB; }
66   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
67   LLVMContext &getContext() const { return Context; }
68   
69   /// SetInsertPoint - This specifies that created instructions should be
70   /// appended to the end of the specified block.
71   void SetInsertPoint(BasicBlock *TheBB) {
72     BB = TheBB;
73     InsertPt = BB->end();
74   }
75   
76   /// SetInsertPoint - This specifies that created instructions should be
77   /// inserted at the specified point.
78   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
79     BB = TheBB;
80     InsertPt = IP;
81   }
82   
83   /// SetCurrentDebugLocation - Set location information used by debugging
84   /// information.
85   void SetCurrentDebugLocation(const DebugLoc &L) {
86     CurDbgLocation = L;
87   }
88   
89   /// getCurrentDebugLocation - Get location information used by debugging
90   /// information.
91   const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
92   
93   /// SetInstDebugLocation - If this builder has a current debug location, set
94   /// it on the specified instruction.
95   void SetInstDebugLocation(Instruction *I) const {
96     if (!CurDbgLocation.isUnknown())
97       I->setDebugLoc(CurDbgLocation);
98   }
99
100   //===--------------------------------------------------------------------===//
101   // Miscellaneous creation methods.
102   //===--------------------------------------------------------------------===//
103   
104   /// CreateGlobalString - Make a new global variable with an initializer that
105   /// has array of i8 type filled in with the nul terminated string value
106   /// specified.  If Name is specified, it is the name of the global variable
107   /// created.
108   Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
109   
110   //===--------------------------------------------------------------------===//
111   // Type creation methods
112   //===--------------------------------------------------------------------===//
113   
114   /// getInt1Ty - Fetch the type representing a single bit
115   const Type *getInt1Ty() {
116     return Type::getInt1Ty(Context);
117   }
118   
119   /// getInt8Ty - Fetch the type representing an 8-bit integer.
120   const Type *getInt8Ty() {
121     return Type::getInt8Ty(Context);
122   }
123   
124   /// getInt16Ty - Fetch the type representing a 16-bit integer.
125   const Type *getInt16Ty() {
126     return Type::getInt16Ty(Context);
127   }
128   
129   /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
130   const Type *getInt32Ty() {
131     return Type::getInt32Ty(Context);
132   }
133   
134   /// getInt64Ty - Fetch the type representing a 64-bit integer.
135   const Type *getInt64Ty() {
136     return Type::getInt64Ty(Context);
137   }
138   
139   /// getFloatTy - Fetch the type representing a 32-bit floating point value.
140   const Type *getFloatTy() {
141     return Type::getFloatTy(Context);
142   }
143   
144   /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
145   const Type *getDoubleTy() {
146     return Type::getDoubleTy(Context);
147   }
148   
149   /// getVoidTy - Fetch the type representing void.
150   const Type *getVoidTy() {
151     return Type::getVoidTy(Context);
152   }
153   
154   const Type *getInt8PtrTy() {
155     return Type::getInt8PtrTy(Context);
156   }
157   
158   /// getCurrentFunctionReturnType - Get the return type of the current function
159   /// that we're emitting into.
160   const Type *getCurrentFunctionReturnType() const;
161 };
162   
163 /// IRBuilder - This provides a uniform API for creating instructions and
164 /// inserting them into a basic block: either at the end of a BasicBlock, or
165 /// at a specific iterator location in a block.
166 ///
167 /// Note that the builder does not expose the full generality of LLVM
168 /// instructions.  For access to extra instruction properties, use the mutators
169 /// (e.g. setVolatile) on the instructions after they have been created.
170 /// The first template argument handles whether or not to preserve names in the
171 /// final instruction output. This defaults to on.  The second template argument
172 /// specifies a class to use for creating constants.  This defaults to creating
173 /// minimally folded constants.  The fourth template argument allows clients to
174 /// specify custom insertion hooks that are called on every newly created
175 /// insertion.
176 template<bool preserveNames = true, typename T = ConstantFolder,
177          typename Inserter = IRBuilderDefaultInserter<preserveNames> >
178 class IRBuilder : public IRBuilderBase, public Inserter {
179   T Folder;
180 public:
181   IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
182     : IRBuilderBase(C), Inserter(I), Folder(F) {
183   }
184   
185   explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
186   }
187   
188   explicit IRBuilder(BasicBlock *TheBB, const T &F)
189     : IRBuilderBase(TheBB->getContext()), Folder(F) {
190     SetInsertPoint(TheBB);
191   }
192   
193   explicit IRBuilder(BasicBlock *TheBB)
194     : IRBuilderBase(TheBB->getContext()), Folder(Context) {
195     SetInsertPoint(TheBB);
196   }
197   
198   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
199     : IRBuilderBase(TheBB->getContext()), Folder(F) {
200     SetInsertPoint(TheBB, IP);
201   }
202   
203   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
204     : IRBuilderBase(TheBB->getContext()), Folder(Context) {
205     SetInsertPoint(TheBB, IP);
206   }
207
208   /// getFolder - Get the constant folder being used.
209   const T &getFolder() { return Folder; }
210
211   /// isNamePreserving - Return true if this builder is configured to actually
212   /// add the requested names to IR created through it.
213   bool isNamePreserving() const { return preserveNames; }
214   
215   /// Insert - Insert and return the specified instruction.
216   template<typename InstTy>
217   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
218     this->InsertHelper(I, Name, BB, InsertPt);
219     if (!getCurrentDebugLocation().isUnknown())
220       this->SetInstDebugLocation(I);
221     return I;
222   }
223
224   //===--------------------------------------------------------------------===//
225   // Instruction creation methods: Terminators
226   //===--------------------------------------------------------------------===//
227
228   /// CreateRetVoid - Create a 'ret void' instruction.
229   ReturnInst *CreateRetVoid() {
230     return Insert(ReturnInst::Create(Context));
231   }
232
233   /// @verbatim
234   /// CreateRet - Create a 'ret <val>' instruction.
235   /// @endverbatim
236   ReturnInst *CreateRet(Value *V) {
237     return Insert(ReturnInst::Create(Context, V));
238   }
239   
240   /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
241   /// with one Value from the retVals array each, that build a aggregate
242   /// return value one value at a time, and a ret instruction to return
243   /// the resulting aggregate value. This is a convenience function for
244   /// code that uses aggregate return values as a vehicle for having
245   /// multiple return values.
246   ///
247   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
248     Value *V = UndefValue::get(getCurrentFunctionReturnType());
249     for (unsigned i = 0; i != N; ++i)
250       V = CreateInsertValue(V, retVals[i], i, "mrv");
251     return Insert(ReturnInst::Create(Context, V));
252   }
253
254   /// CreateBr - Create an unconditional 'br label X' instruction.
255   BranchInst *CreateBr(BasicBlock *Dest) {
256     return Insert(BranchInst::Create(Dest));
257   }
258
259   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
260   /// instruction.
261   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
262     return Insert(BranchInst::Create(True, False, Cond));
263   }
264
265   /// CreateSwitch - Create a switch instruction with the specified value,
266   /// default dest, and with a hint for the number of cases that will be added
267   /// (for efficient allocation).
268   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
269     return Insert(SwitchInst::Create(V, Dest, NumCases));
270   }
271
272   /// CreateIndirectBr - Create an indirect branch instruction with the
273   /// specified address operand, with an optional hint for the number of
274   /// destinations that will be added (for efficient allocation).
275   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
276     return Insert(IndirectBrInst::Create(Addr, NumDests));
277   }
278
279   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
280                            BasicBlock *UnwindDest, const Twine &Name = "") {
281     Value *Args[] = { 0 };
282     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
283                                      Args), Name);
284   }
285   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
286                            BasicBlock *UnwindDest, Value *Arg1,
287                            const Twine &Name = "") {
288     Value *Args[] = { Arg1 };
289     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
290                                      Args+1), Name);
291   }
292   InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
293                             BasicBlock *UnwindDest, Value *Arg1,
294                             Value *Arg2, Value *Arg3,
295                             const Twine &Name = "") {
296     Value *Args[] = { Arg1, Arg2, Arg3 };
297     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
298                                      Args+3), Name);
299   }
300   /// CreateInvoke - Create an invoke instruction.
301   template<typename InputIterator>
302   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
303                            BasicBlock *UnwindDest, InputIterator ArgBegin,
304                            InputIterator ArgEnd, const Twine &Name = "") {
305     return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
306                                      ArgBegin, ArgEnd), Name);
307   }
308
309   UnwindInst *CreateUnwind() {
310     return Insert(new UnwindInst(Context));
311   }
312
313   UnreachableInst *CreateUnreachable() {
314     return Insert(new UnreachableInst(Context));
315   }
316
317   //===--------------------------------------------------------------------===//
318   // Instruction creation methods: Binary Operators
319   //===--------------------------------------------------------------------===//
320
321   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
322     if (Constant *LC = dyn_cast<Constant>(LHS))
323       if (Constant *RC = dyn_cast<Constant>(RHS))
324         return Folder.CreateAdd(LC, RC);
325     return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
326   }
327   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
328     if (Constant *LC = dyn_cast<Constant>(LHS))
329       if (Constant *RC = dyn_cast<Constant>(RHS))
330         return Folder.CreateNSWAdd(LC, RC);
331     return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
332   }
333   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
334     if (Constant *LC = dyn_cast<Constant>(LHS))
335       if (Constant *RC = dyn_cast<Constant>(RHS))
336         return Folder.CreateNUWAdd(LC, RC);
337     return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
338   }
339   Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
340     if (Constant *LC = dyn_cast<Constant>(LHS))
341       if (Constant *RC = dyn_cast<Constant>(RHS))
342         return Folder.CreateFAdd(LC, RC);
343     return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
344   }
345   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
346     if (Constant *LC = dyn_cast<Constant>(LHS))
347       if (Constant *RC = dyn_cast<Constant>(RHS))
348         return Folder.CreateSub(LC, RC);
349     return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
350   }
351   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
352     if (Constant *LC = dyn_cast<Constant>(LHS))
353       if (Constant *RC = dyn_cast<Constant>(RHS))
354         return Folder.CreateNSWSub(LC, RC);
355     return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
356   }
357   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
358     if (Constant *LC = dyn_cast<Constant>(LHS))
359       if (Constant *RC = dyn_cast<Constant>(RHS))
360         return Folder.CreateNUWSub(LC, RC);
361     return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
362   }
363   Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
364     if (Constant *LC = dyn_cast<Constant>(LHS))
365       if (Constant *RC = dyn_cast<Constant>(RHS))
366         return Folder.CreateFSub(LC, RC);
367     return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
368   }
369   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
370     if (Constant *LC = dyn_cast<Constant>(LHS))
371       if (Constant *RC = dyn_cast<Constant>(RHS))
372         return Folder.CreateMul(LC, RC);
373     return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
374   }
375   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
376     if (Constant *LC = dyn_cast<Constant>(LHS))
377       if (Constant *RC = dyn_cast<Constant>(RHS))
378         return Folder.CreateNSWMul(LC, RC);
379     return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
380   }
381   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
382     if (Constant *LC = dyn_cast<Constant>(LHS))
383       if (Constant *RC = dyn_cast<Constant>(RHS))
384         return Folder.CreateNUWMul(LC, RC);
385     return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
386   }
387   Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
388     if (Constant *LC = dyn_cast<Constant>(LHS))
389       if (Constant *RC = dyn_cast<Constant>(RHS))
390         return Folder.CreateFMul(LC, RC);
391     return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
392   }
393   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
394     if (Constant *LC = dyn_cast<Constant>(LHS))
395       if (Constant *RC = dyn_cast<Constant>(RHS))
396         return Folder.CreateUDiv(LC, RC);
397     return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
398   }
399   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
400     if (Constant *LC = dyn_cast<Constant>(LHS))
401       if (Constant *RC = dyn_cast<Constant>(RHS))
402         return Folder.CreateSDiv(LC, RC);
403     return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
404   }
405   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
406     if (Constant *LC = dyn_cast<Constant>(LHS))
407       if (Constant *RC = dyn_cast<Constant>(RHS))
408         return Folder.CreateExactSDiv(LC, RC);
409     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
410   }
411   Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
412     if (Constant *LC = dyn_cast<Constant>(LHS))
413       if (Constant *RC = dyn_cast<Constant>(RHS))
414         return Folder.CreateFDiv(LC, RC);
415     return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
416   }
417   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
418     if (Constant *LC = dyn_cast<Constant>(LHS))
419       if (Constant *RC = dyn_cast<Constant>(RHS))
420         return Folder.CreateURem(LC, RC);
421     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
422   }
423   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
424     if (Constant *LC = dyn_cast<Constant>(LHS))
425       if (Constant *RC = dyn_cast<Constant>(RHS))
426         return Folder.CreateSRem(LC, RC);
427     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
428   }
429   Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
430     if (Constant *LC = dyn_cast<Constant>(LHS))
431       if (Constant *RC = dyn_cast<Constant>(RHS))
432         return Folder.CreateFRem(LC, RC);
433     return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
434   }
435   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
436     if (Constant *LC = dyn_cast<Constant>(LHS))
437       if (Constant *RC = dyn_cast<Constant>(RHS))
438         return Folder.CreateShl(LC, RC);
439     return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
440   }
441   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
442     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
443     if (Constant *LC = dyn_cast<Constant>(LHS))
444       return Folder.CreateShl(LC, RHSC);
445     return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
446   }
447
448   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
449     if (Constant *LC = dyn_cast<Constant>(LHS))
450       if (Constant *RC = dyn_cast<Constant>(RHS))
451         return Folder.CreateLShr(LC, RC);
452     return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
453   }
454   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
455     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
456     if (Constant *LC = dyn_cast<Constant>(LHS))
457       return Folder.CreateLShr(LC, RHSC);
458     return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
459   }
460   
461   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
462     if (Constant *LC = dyn_cast<Constant>(LHS))
463       if (Constant *RC = dyn_cast<Constant>(RHS))
464         return Folder.CreateAShr(LC, RC);
465     return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
466   }
467   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
468     Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
469     if (Constant *LC = dyn_cast<Constant>(LHS))
470       return Folder.CreateSShr(LC, RHSC);
471     return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
472   }
473
474   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
475     if (Constant *RC = dyn_cast<Constant>(RHS)) {
476       if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
477         return LHS;  // LHS & -1 -> LHS
478       if (Constant *LC = dyn_cast<Constant>(LHS))
479         return Folder.CreateAnd(LC, RC);
480     }
481     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
482   }
483   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
484     if (Constant *RC = dyn_cast<Constant>(RHS)) {
485       if (RC->isNullValue())
486         return LHS;  // LHS | 0 -> LHS
487       if (Constant *LC = dyn_cast<Constant>(LHS))
488         return Folder.CreateOr(LC, RC);
489     }
490     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
491   }
492   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
493     if (Constant *LC = dyn_cast<Constant>(LHS))
494       if (Constant *RC = dyn_cast<Constant>(RHS))
495         return Folder.CreateXor(LC, RC);
496     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
497   }
498
499   Value *CreateBinOp(Instruction::BinaryOps Opc,
500                      Value *LHS, Value *RHS, const Twine &Name = "") {
501     if (Constant *LC = dyn_cast<Constant>(LHS))
502       if (Constant *RC = dyn_cast<Constant>(RHS))
503         return Folder.CreateBinOp(Opc, LC, RC);
504     return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
505   }
506
507   Value *CreateNeg(Value *V, const Twine &Name = "") {
508     if (Constant *VC = dyn_cast<Constant>(V))
509       return Folder.CreateNeg(VC);
510     return Insert(BinaryOperator::CreateNeg(V), Name);
511   }
512   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
513     if (Constant *VC = dyn_cast<Constant>(V))
514       return Folder.CreateNSWNeg(VC);
515     return Insert(BinaryOperator::CreateNSWNeg(V), Name);
516   }
517   Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
518     if (Constant *VC = dyn_cast<Constant>(V))
519       return Folder.CreateNUWNeg(VC);
520     return Insert(BinaryOperator::CreateNUWNeg(V), Name);
521   }
522   Value *CreateFNeg(Value *V, const Twine &Name = "") {
523     if (Constant *VC = dyn_cast<Constant>(V))
524       return Folder.CreateFNeg(VC);
525     return Insert(BinaryOperator::CreateFNeg(V), Name);
526   }
527   Value *CreateNot(Value *V, const Twine &Name = "") {
528     if (Constant *VC = dyn_cast<Constant>(V))
529       return Folder.CreateNot(VC);
530     return Insert(BinaryOperator::CreateNot(V), Name);
531   }
532
533   //===--------------------------------------------------------------------===//
534   // Instruction creation methods: Memory Instructions
535   //===--------------------------------------------------------------------===//
536
537   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
538                            const Twine &Name = "") {
539     return Insert(new AllocaInst(Ty, ArraySize), Name);
540   }
541   // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
542   // converting the string to 'bool' for the isVolatile parameter.
543   LoadInst *CreateLoad(Value *Ptr, const char *Name) {
544     return Insert(new LoadInst(Ptr), Name);
545   }
546   LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
547     return Insert(new LoadInst(Ptr), Name);
548   }
549   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
550     return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
551   }
552   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
553     return Insert(new StoreInst(Val, Ptr, isVolatile));
554   }
555   template<typename InputIterator>
556   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
557                    const Twine &Name = "") {
558     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
559       // Every index must be constant.
560       InputIterator i;
561       for (i = IdxBegin; i < IdxEnd; ++i)
562         if (!isa<Constant>(*i))
563           break;
564       if (i == IdxEnd)
565         return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
566     }
567     return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
568   }
569   template<typename InputIterator>
570   Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
571                            const Twine &Name = "") {
572     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
573       // Every index must be constant.
574       InputIterator i;
575       for (i = IdxBegin; i < IdxEnd; ++i)
576         if (!isa<Constant>(*i))
577           break;
578       if (i == IdxEnd)
579         return Folder.CreateInBoundsGetElementPtr(PC,
580                                                   &IdxBegin[0],
581                                                   IdxEnd - IdxBegin);
582     }
583     return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
584                   Name);
585   }
586   Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
587     if (Constant *PC = dyn_cast<Constant>(Ptr))
588       if (Constant *IC = dyn_cast<Constant>(Idx))
589         return Folder.CreateGetElementPtr(PC, &IC, 1);
590     return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
591   }
592   Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
593     if (Constant *PC = dyn_cast<Constant>(Ptr))
594       if (Constant *IC = dyn_cast<Constant>(Idx))
595         return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
596     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
597   }
598   Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
599     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
600
601     if (Constant *PC = dyn_cast<Constant>(Ptr))
602       return Folder.CreateGetElementPtr(PC, &Idx, 1);
603
604     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
605   }
606   Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
607                                     const Twine &Name = "") {
608     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
609
610     if (Constant *PC = dyn_cast<Constant>(Ptr))
611       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
612
613     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
614   }
615   Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, 
616                     const Twine &Name = "") {
617     Value *Idxs[] = {
618       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
619       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
620     };
621
622     if (Constant *PC = dyn_cast<Constant>(Ptr))
623       return Folder.CreateGetElementPtr(PC, Idxs, 2);
624
625     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
626   }
627   Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
628                                     const Twine &Name = "") {
629     Value *Idxs[] = {
630       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
631       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
632     };
633
634     if (Constant *PC = dyn_cast<Constant>(Ptr))
635       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
636
637     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
638   }
639   Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
640     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
641
642     if (Constant *PC = dyn_cast<Constant>(Ptr))
643       return Folder.CreateGetElementPtr(PC, &Idx, 1);
644
645     return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);    
646   }
647   Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
648                                     const Twine &Name = "") {
649     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
650
651     if (Constant *PC = dyn_cast<Constant>(Ptr))
652       return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
653
654     return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
655   }
656   Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
657                     const Twine &Name = "") {
658     Value *Idxs[] = {
659       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
660       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
661     };
662
663     if (Constant *PC = dyn_cast<Constant>(Ptr))
664       return Folder.CreateGetElementPtr(PC, Idxs, 2);
665
666     return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);    
667   }
668   Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
669                                     const Twine &Name = "") {
670     Value *Idxs[] = {
671       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
672       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
673     };
674
675     if (Constant *PC = dyn_cast<Constant>(Ptr))
676       return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
677
678     return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
679   }
680   Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
681     return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
682   }
683   
684   /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
685   /// with "i8*" type instead of a pointer to array of i8.
686   Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
687     Value *gv = CreateGlobalString(Str, Name);
688     Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
689     Value *Args[] = { zero, zero };
690     return CreateInBoundsGEP(gv, Args, Args+2, Name);
691   }
692   
693   //===--------------------------------------------------------------------===//
694   // Instruction creation methods: Cast/Conversion Operators
695   //===--------------------------------------------------------------------===//
696
697   Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
698     return CreateCast(Instruction::Trunc, V, DestTy, Name);
699   }
700   Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
701     return CreateCast(Instruction::ZExt, V, DestTy, Name);
702   }
703   Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
704     return CreateCast(Instruction::SExt, V, DestTy, Name);
705   }
706   Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
707     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
708   }
709   Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
710     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
711   }
712   Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
713     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
714   }
715   Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
716     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
717   }
718   Value *CreateFPTrunc(Value *V, const Type *DestTy,
719                        const Twine &Name = "") {
720     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
721   }
722   Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
723     return CreateCast(Instruction::FPExt, V, DestTy, Name);
724   }
725   Value *CreatePtrToInt(Value *V, const Type *DestTy,
726                         const Twine &Name = "") {
727     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
728   }
729   Value *CreateIntToPtr(Value *V, const Type *DestTy,
730                         const Twine &Name = "") {
731     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
732   }
733   Value *CreateBitCast(Value *V, const Type *DestTy,
734                        const Twine &Name = "") {
735     return CreateCast(Instruction::BitCast, V, DestTy, Name);
736   }
737   Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
738                              const Twine &Name = "") {
739     if (V->getType() == DestTy)
740       return V;
741     if (Constant *VC = dyn_cast<Constant>(V))
742       return Folder.CreateZExtOrBitCast(VC, DestTy);
743     return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
744   }
745   Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
746                              const Twine &Name = "") {
747     if (V->getType() == DestTy)
748       return V;
749     if (Constant *VC = dyn_cast<Constant>(V))
750       return Folder.CreateSExtOrBitCast(VC, DestTy);
751     return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
752   }
753   Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
754                               const Twine &Name = "") {
755     if (V->getType() == DestTy)
756       return V;
757     if (Constant *VC = dyn_cast<Constant>(V))
758       return Folder.CreateTruncOrBitCast(VC, DestTy);
759     return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
760   }
761   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
762                     const Twine &Name = "") {
763     if (V->getType() == DestTy)
764       return V;
765     if (Constant *VC = dyn_cast<Constant>(V))
766       return Folder.CreateCast(Op, VC, DestTy);
767     return Insert(CastInst::Create(Op, V, DestTy), Name);
768   }
769   Value *CreatePointerCast(Value *V, const Type *DestTy,
770                            const Twine &Name = "") {
771     if (V->getType() == DestTy)
772       return V;
773     if (Constant *VC = dyn_cast<Constant>(V))
774       return Folder.CreatePointerCast(VC, DestTy);
775     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
776   }
777   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
778                        const Twine &Name = "") {
779     if (V->getType() == DestTy)
780       return V;
781     if (Constant *VC = dyn_cast<Constant>(V))
782       return Folder.CreateIntCast(VC, DestTy, isSigned);
783     return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
784   }
785 private:
786   // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
787   // error, instead of converting the string to bool for the isSigned parameter.
788   Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
789 public:
790   Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
791     if (V->getType() == DestTy)
792       return V;
793     if (Constant *VC = dyn_cast<Constant>(V))
794       return Folder.CreateFPCast(VC, DestTy);
795     return Insert(CastInst::CreateFPCast(V, DestTy), Name);
796   }
797
798   //===--------------------------------------------------------------------===//
799   // Instruction creation methods: Compare Instructions
800   //===--------------------------------------------------------------------===//
801
802   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
803     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
804   }
805   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
806     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
807   }
808   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
809     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
810   }
811   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
812     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
813   }
814   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
815     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
816   }
817   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
818     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
819   }
820   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
821     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
822   }
823   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
824     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
825   }
826   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
827     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
828   }
829   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
830     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
831   }
832
833   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
834     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
835   }
836   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
837     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
838   }
839   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
840     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
841   }
842   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
843     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
844   }
845   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
846     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
847   }
848   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
849     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
850   }
851   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
852     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
853   }
854   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
855     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
856   }
857   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
858     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
859   }
860   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
861     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
862   }
863   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
864     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
865   }
866   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
867     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
868   }
869   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
870     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
871   }
872   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
873     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
874   }
875
876   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
877                     const Twine &Name = "") {
878     if (Constant *LC = dyn_cast<Constant>(LHS))
879       if (Constant *RC = dyn_cast<Constant>(RHS))
880         return Folder.CreateICmp(P, LC, RC);
881     return Insert(new ICmpInst(P, LHS, RHS), Name);
882   }
883   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
884                     const Twine &Name = "") {
885     if (Constant *LC = dyn_cast<Constant>(LHS))
886       if (Constant *RC = dyn_cast<Constant>(RHS))
887         return Folder.CreateFCmp(P, LC, RC);
888     return Insert(new FCmpInst(P, LHS, RHS), Name);
889   }
890
891   //===--------------------------------------------------------------------===//
892   // Instruction creation methods: Other Instructions
893   //===--------------------------------------------------------------------===//
894
895   PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
896     return Insert(PHINode::Create(Ty), Name);
897   }
898
899   CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
900     return Insert(CallInst::Create(Callee), Name);
901   }
902   CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
903     return Insert(CallInst::Create(Callee, Arg), Name);
904   }
905   CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
906                         const Twine &Name = "") {
907     Value *Args[] = { Arg1, Arg2 };
908     return Insert(CallInst::Create(Callee, Args, Args+2), Name);
909   }
910   CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
911                         const Twine &Name = "") {
912     Value *Args[] = { Arg1, Arg2, Arg3 };
913     return Insert(CallInst::Create(Callee, Args, Args+3), Name);
914   }
915   CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
916                         Value *Arg4, const Twine &Name = "") {
917     Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
918     return Insert(CallInst::Create(Callee, Args, Args+4), Name);
919   }
920   CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
921                         Value *Arg4, Value *Arg5, const Twine &Name = "") {
922     Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
923     return Insert(CallInst::Create(Callee, Args, Args+5), Name);
924   }
925
926   template<typename InputIterator>
927   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
928                        InputIterator ArgEnd, const Twine &Name = "") {
929     return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
930   }
931
932   Value *CreateSelect(Value *C, Value *True, Value *False,
933                       const Twine &Name = "") {
934     if (Constant *CC = dyn_cast<Constant>(C))
935       if (Constant *TC = dyn_cast<Constant>(True))
936         if (Constant *FC = dyn_cast<Constant>(False))
937           return Folder.CreateSelect(CC, TC, FC);
938     return Insert(SelectInst::Create(C, True, False), Name);
939   }
940
941   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
942     return Insert(new VAArgInst(List, Ty), Name);
943   }
944
945   Value *CreateExtractElement(Value *Vec, Value *Idx,
946                               const Twine &Name = "") {
947     if (Constant *VC = dyn_cast<Constant>(Vec))
948       if (Constant *IC = dyn_cast<Constant>(Idx))
949         return Folder.CreateExtractElement(VC, IC);
950     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
951   }
952
953   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
954                              const Twine &Name = "") {
955     if (Constant *VC = dyn_cast<Constant>(Vec))
956       if (Constant *NC = dyn_cast<Constant>(NewElt))
957         if (Constant *IC = dyn_cast<Constant>(Idx))
958           return Folder.CreateInsertElement(VC, NC, IC);
959     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
960   }
961
962   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
963                              const Twine &Name = "") {
964     if (Constant *V1C = dyn_cast<Constant>(V1))
965       if (Constant *V2C = dyn_cast<Constant>(V2))
966         if (Constant *MC = dyn_cast<Constant>(Mask))
967           return Folder.CreateShuffleVector(V1C, V2C, MC);
968     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
969   }
970
971   Value *CreateExtractValue(Value *Agg, unsigned Idx,
972                             const Twine &Name = "") {
973     if (Constant *AggC = dyn_cast<Constant>(Agg))
974       return Folder.CreateExtractValue(AggC, &Idx, 1);
975     return Insert(ExtractValueInst::Create(Agg, Idx), Name);
976   }
977
978   template<typename InputIterator>
979   Value *CreateExtractValue(Value *Agg,
980                             InputIterator IdxBegin,
981                             InputIterator IdxEnd,
982                             const Twine &Name = "") {
983     if (Constant *AggC = dyn_cast<Constant>(Agg))
984       return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
985     return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
986   }
987
988   Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
989                            const Twine &Name = "") {
990     if (Constant *AggC = dyn_cast<Constant>(Agg))
991       if (Constant *ValC = dyn_cast<Constant>(Val))
992         return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
993     return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
994   }
995
996   template<typename InputIterator>
997   Value *CreateInsertValue(Value *Agg, Value *Val,
998                            InputIterator IdxBegin,
999                            InputIterator IdxEnd,
1000                            const Twine &Name = "") {
1001     if (Constant *AggC = dyn_cast<Constant>(Agg))
1002       if (Constant *ValC = dyn_cast<Constant>(Val))
1003         return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
1004     return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1005   }
1006
1007   //===--------------------------------------------------------------------===//
1008   // Utility creation methods
1009   //===--------------------------------------------------------------------===//
1010
1011   /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1012   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1013     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1014                         Name);
1015   }
1016
1017   /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1018   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1019     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1020                         Name);
1021   }
1022
1023   /// CreatePtrDiff - Return the i64 difference between two pointer values,
1024   /// dividing out the size of the pointed-to objects.  This is intended to
1025   /// implement C-style pointer subtraction. As such, the pointers must be
1026   /// appropriately aligned for their element types and pointing into the
1027   /// same object.
1028   Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1029     assert(LHS->getType() == RHS->getType() &&
1030            "Pointer subtraction operand types must match!");
1031     const PointerType *ArgType = cast<PointerType>(LHS->getType());
1032     Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1033     Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1034     Value *Difference = CreateSub(LHS_int, RHS_int);
1035     return CreateExactSDiv(Difference,
1036                            ConstantExpr::getSizeOf(ArgType->getElementType()),
1037                            Name);
1038   }
1039 };
1040
1041 }
1042
1043 #endif