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