Don't attribute in file headers anymore. See llvmdev for the
[oota-llvm.git] / include / llvm / Support / LLVMBuilder.h
1 //===-- llvm/Support/LLVMBuilder.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 LLVMBuilder 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_LLVMBUILDER_H
16 #define LLVM_SUPPORT_LLVMBUILDER_H
17
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/Constants.h"
21
22 namespace llvm {
23
24 /// LLVMBuilder - This provides a uniform API for creating instructions and
25 /// inserting them into a basic block: either at the end of a BasicBlock, or 
26 /// at a specific iterator location in a block.
27 ///
28 /// Note that the builder does not expose the full generality of LLVM
29 /// instructions.  For example, it cannot be used to create instructions with
30 /// arbitrary names (specifically, names with nul characters in them) - It only
31 /// supports nul-terminated C strings.  For fully generic names, use
32 /// I->setName().  For access to extra instruction properties, use the mutators
33 /// (e.g. setVolatile) on the instructions after they have been created.
34 class LLVMBuilder {
35   BasicBlock *BB;
36   BasicBlock::iterator InsertPt;
37 public:
38   LLVMBuilder() { ClearInsertionPoint(); }
39   explicit LLVMBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
40   LLVMBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
41     SetInsertPoint(TheBB, IP);
42   }
43
44   //===--------------------------------------------------------------------===//
45   // Builder configuration methods
46   //===--------------------------------------------------------------------===//
47
48   /// ClearInsertionPoint - Clear the insertion point: created instructions will
49   /// not be inserted into a block.
50   void ClearInsertionPoint() {
51     BB = 0;
52   }
53   
54   BasicBlock *GetInsertBlock() const { return BB; }
55   
56   /// SetInsertPoint - This specifies that created instructions should be
57   /// appended to the end of the specified block.
58   void SetInsertPoint(BasicBlock *TheBB) {
59     BB = TheBB;
60     InsertPt = BB->end();
61   }
62   
63   /// SetInsertPoint - This specifies that created instructions should be
64   /// inserted at the specified point.
65   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
66     BB = TheBB;
67     InsertPt = IP;
68   }
69   
70   /// Insert - Insert and return the specified instruction.
71   template<typename InstTy>
72   InstTy *Insert(InstTy *I) const {
73     InsertHelper(I);
74     return I;
75   }
76   
77   /// InsertHelper - Insert the specified instruction at the specified insertion
78   /// point.  This is split out of Insert so that it isn't duplicated for every
79   /// template instantiation.
80   void InsertHelper(Instruction *I) const {
81     if (BB) BB->getInstList().insert(InsertPt, I);
82   }
83   
84   //===--------------------------------------------------------------------===//
85   // Instruction creation methods: Terminators
86   //===--------------------------------------------------------------------===//
87
88   /// CreateRetVoid - Create a 'ret void' instruction.
89   ReturnInst *CreateRetVoid() {
90     return Insert(new ReturnInst());
91   }
92
93   /// @verbatim 
94   /// CreateRet - Create a 'ret <val>' instruction. 
95   /// @endverbatim
96   ReturnInst *CreateRet(Value *V) {
97     return Insert(new ReturnInst(V));
98   }
99   
100   /// CreateBr - Create an unconditional 'br label X' instruction.
101   BranchInst *CreateBr(BasicBlock *Dest) {
102     return Insert(new BranchInst(Dest));
103   }
104
105   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
106   /// instruction.
107   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
108     return Insert(new BranchInst(True, False, Cond));
109   }
110   
111   /// CreateSwitch - Create a switch instruction with the specified value,
112   /// default dest, and with a hint for the number of cases that will be added
113   /// (for efficient allocation).
114   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
115     return Insert(new SwitchInst(V, Dest, NumCases));
116   }
117   
118   /// CreateInvoke - Create an invoke instruction.
119   template<typename InputIterator>
120   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 
121                            BasicBlock *UnwindDest, InputIterator ArgBegin, 
122                            InputIterator ArgEnd, const char *Name = "") {
123     return(Insert(new InvokeInst(Callee, NormalDest, UnwindDest,
124                                  ArgBegin, ArgEnd, Name)));
125   }
126   
127   UnwindInst *CreateUnwind() {
128     return Insert(new UnwindInst());
129   }
130
131   UnreachableInst *CreateUnreachable() {
132     return Insert(new UnreachableInst());
133   }
134   
135   //===--------------------------------------------------------------------===//
136   // Instruction creation methods: Binary Operators
137   //===--------------------------------------------------------------------===//
138
139   BinaryOperator *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
140     return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
141   }
142   BinaryOperator *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
143     return Insert(BinaryOperator::createSub(LHS, RHS, Name));
144   }
145   BinaryOperator *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
146     return Insert(BinaryOperator::createMul(LHS, RHS, Name));
147   }
148   BinaryOperator *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
149     return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
150   }
151   BinaryOperator *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
152     return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
153   }
154   BinaryOperator *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
155     return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
156   }
157   BinaryOperator *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
158     return Insert(BinaryOperator::createURem(LHS, RHS, Name));
159   }
160   BinaryOperator *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
161     return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
162   }
163   BinaryOperator *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
164     return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
165   }
166   BinaryOperator *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
167     return Insert(BinaryOperator::createShl(LHS, RHS, Name));
168   }
169   BinaryOperator *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
170     return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
171   }
172   BinaryOperator *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
173     return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
174   }
175   BinaryOperator *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
176     return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
177   }
178   BinaryOperator *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
179     return Insert(BinaryOperator::createOr(LHS, RHS, Name));
180   }
181   BinaryOperator *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
182     return Insert(BinaryOperator::createXor(LHS, RHS, Name));
183   }
184
185   BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
186                               Value *LHS, Value *RHS, const char *Name = "") {
187     return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
188   }
189   
190   BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
191     return Insert(BinaryOperator::createNeg(V, Name));
192   }
193   BinaryOperator *CreateNot(Value *V, const char *Name = "") {
194     return Insert(BinaryOperator::createNot(V, Name));
195   }
196   
197   //===--------------------------------------------------------------------===//
198   // Instruction creation methods: Memory Instructions
199   //===--------------------------------------------------------------------===//
200   
201   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
202                            const char *Name = "") {
203     return Insert(new MallocInst(Ty, ArraySize, Name));
204   }
205   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
206                            const char *Name = "") {
207     return Insert(new AllocaInst(Ty, ArraySize, Name));
208   }
209   FreeInst *CreateFree(Value *Ptr) {
210     return Insert(new FreeInst(Ptr));
211   }
212   LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
213     return Insert(new LoadInst(Ptr, Name));
214   }
215   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
216     return Insert(new LoadInst(Ptr, Name, isVolatile));
217   }
218   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
219     return Insert(new StoreInst(Val, Ptr, isVolatile));
220   }
221   template<typename InputIterator>
222   GetElementPtrInst *CreateGEP(Value *Ptr, InputIterator IdxBegin, 
223                                InputIterator IdxEnd, const char *Name = "") {
224     return(Insert(new GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Name)));
225   }
226   GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
227     return Insert(new GetElementPtrInst(Ptr, Idx, Name));
228   }
229   
230   //===--------------------------------------------------------------------===//
231   // Instruction creation methods: Cast/Conversion Operators
232   //===--------------------------------------------------------------------===//
233   
234   TruncInst *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
235     return Insert(new TruncInst(V, DestTy, Name));
236   }
237   ZExtInst *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
238     return Insert(new ZExtInst(V, DestTy, Name));
239   }
240   SExtInst *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
241     return Insert(new SExtInst(V, DestTy, Name));
242   }
243   FPToUIInst *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
244     return Insert(new FPToUIInst(V, DestTy, Name));
245   }
246   FPToSIInst *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
247     return Insert(new FPToSIInst(V, DestTy, Name));
248   }
249   UIToFPInst *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
250     return Insert(new UIToFPInst(V, DestTy, Name));
251   }
252   SIToFPInst *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
253     return Insert(new SIToFPInst(V, DestTy, Name));
254   }
255   FPTruncInst *CreateFPTrunc(Value *V, const Type *DestTy,
256                              const char *Name = "") {
257     return Insert(new FPTruncInst(V, DestTy, Name));
258   }
259   FPExtInst *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
260     return Insert(new FPExtInst(V, DestTy, Name));
261   }
262   PtrToIntInst *CreatePtrToInt(Value *V, const Type *DestTy,
263                                const char *Name = "") {
264     return Insert(new PtrToIntInst(V, DestTy, Name));
265   }
266   IntToPtrInst *CreateIntToPtr(Value *V, const Type *DestTy,
267                                const char *Name = "") {
268     return Insert(new IntToPtrInst(V, DestTy, Name));
269   }
270   BitCastInst *CreateBitCast(Value *V, const Type *DestTy,
271                              const char *Name = "") {
272     return Insert(new BitCastInst(V, DestTy, Name));
273   }
274   
275   CastInst *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
276                        const char *Name = "") {
277     return Insert(CastInst::create(Op, V, DestTy, Name));
278   }
279   CastInst *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
280                           const char *Name = "") {
281     return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
282   }
283   
284   
285   
286   //===--------------------------------------------------------------------===//
287   // Instruction creation methods: Compare Instructions
288   //===--------------------------------------------------------------------===//
289   
290   ICmpInst *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
291     return Insert(new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS, Name));
292   }
293   ICmpInst *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
294     return Insert(new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS, Name));
295   }
296   ICmpInst *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
297     return Insert(new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, Name));
298   }
299   ICmpInst *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
300     return Insert(new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS, Name));
301   }
302   ICmpInst *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
303     return Insert(new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS, Name));
304   }
305   ICmpInst *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
306     return Insert(new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS, Name));
307   }
308   ICmpInst *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
309     return Insert(new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, Name));
310   }
311   ICmpInst *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
312     return Insert(new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS, Name));
313   }
314   ICmpInst *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
315     return Insert(new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS, Name));
316   }
317   ICmpInst *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
318     return Insert(new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS, Name));
319   }
320   
321   FCmpInst *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
322     return Insert(new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS, Name));
323   }
324   FCmpInst *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
325     return Insert(new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS, Name));
326   }
327   FCmpInst *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
328     return Insert(new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS, Name));
329   }
330   FCmpInst *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
331     return Insert(new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS, Name));
332   }
333   FCmpInst *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
334     return Insert(new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS, Name));
335   }
336   FCmpInst *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
337     return Insert(new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS, Name));
338   }
339   FCmpInst *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
340     return Insert(new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS, Name));
341   }
342   FCmpInst *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
343     return Insert(new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS, Name));
344   }
345   FCmpInst *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
346     return Insert(new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS, Name));
347   }
348   FCmpInst *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
349     return Insert(new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS, Name));
350   }
351   FCmpInst *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
352     return Insert(new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS, Name));
353   }
354   FCmpInst *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
355     return Insert(new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS, Name));
356   }
357   FCmpInst *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
358     return Insert(new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS, Name));
359   }
360   FCmpInst *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
361     return Insert(new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS, Name));
362   }
363   
364   
365   ICmpInst *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS, 
366                        const char *Name = "") {
367     return Insert(new ICmpInst(P, LHS, RHS, Name));
368   }
369   FCmpInst *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS, 
370                        const char *Name = "") {
371     return Insert(new FCmpInst(P, LHS, RHS, Name));
372   }
373   
374   //===--------------------------------------------------------------------===//
375   // Instruction creation methods: Other Instructions
376   //===--------------------------------------------------------------------===//
377   
378   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
379     return Insert(new PHINode(Ty, Name));
380   }
381
382   CallInst *CreateCall(Value *Callee, const char *Name = "") {
383     return Insert(new CallInst(Callee, Name));
384   }
385   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
386     return Insert(new CallInst(Callee, Arg, Name));
387   }
388
389   template<typename InputIterator>
390   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin, 
391                        InputIterator ArgEnd, const char *Name = "") {
392     return(Insert(new CallInst(Callee, ArgBegin, ArgEnd, Name)));
393   }
394   
395   SelectInst *CreateSelect(Value *C, Value *True, Value *False,
396                            const char *Name = "") {
397     return Insert(new SelectInst(C, True, False, Name));
398   }
399   
400   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
401     return Insert(new VAArgInst(List, Ty, Name));
402   }
403   
404   ExtractElementInst *CreateExtractElement(Value *Vec, Value *Idx,
405                                            const char *Name = "") {
406     return Insert(new ExtractElementInst(Vec, Idx, Name));
407   }
408   
409   InsertElementInst *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
410                                          const char *Name = "") {
411     return Insert(new InsertElementInst(Vec, NewElt, Idx, Name));
412   }
413   
414   ShuffleVectorInst *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
415                                          const char *Name = "") {
416     return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
417   }
418 };
419
420 /// LLVMFoldingBuilder - A version of LLVMBuilder that constant folds operands 
421 /// as they come in.
422 class LLVMFoldingBuilder : public LLVMBuilder {
423     
424 public:
425   LLVMFoldingBuilder() {}
426   explicit LLVMFoldingBuilder(BasicBlock *TheBB) 
427     : LLVMBuilder(TheBB) {}
428   LLVMFoldingBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) 
429     : LLVMBuilder(TheBB, IP) {}
430
431   //===--------------------------------------------------------------------===//
432   // Instruction creation methods: Binary Operators
433   //===--------------------------------------------------------------------===//
434
435   Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
436     if (Constant *LC = dyn_cast<Constant>(LHS))
437       if (Constant *RC = dyn_cast<Constant>(RHS))
438         return ConstantExpr::getAdd(LC, RC);
439     return LLVMBuilder::CreateAdd(LHS, RHS, Name);
440   }
441
442   Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
443     if (Constant *LC = dyn_cast<Constant>(LHS))
444       if (Constant *RC = dyn_cast<Constant>(RHS))
445         return ConstantExpr::getSub(LC, RC);
446     return LLVMBuilder::CreateSub(LHS, RHS, Name);
447   }
448
449   Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
450     if (Constant *LC = dyn_cast<Constant>(LHS))
451       if (Constant *RC = dyn_cast<Constant>(RHS))
452         return ConstantExpr::getMul(LC, RC);
453     return LLVMBuilder::CreateMul(LHS, RHS, Name);
454   }
455
456   Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
457     if (Constant *LC = dyn_cast<Constant>(LHS))
458       if (Constant *RC = dyn_cast<Constant>(RHS))
459         return ConstantExpr::getUDiv(LC, RC);
460     return LLVMBuilder::CreateUDiv(LHS, RHS, Name);
461   }
462
463   Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
464     if (Constant *LC = dyn_cast<Constant>(LHS))
465       if (Constant *RC = dyn_cast<Constant>(RHS))
466         return ConstantExpr::getSDiv(LC, RC);
467     return LLVMBuilder::CreateSDiv(LHS, RHS, Name);
468   }
469
470   Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
471     if (Constant *LC = dyn_cast<Constant>(LHS))
472       if (Constant *RC = dyn_cast<Constant>(RHS))
473         return ConstantExpr::getFDiv(LC, RC);
474     return LLVMBuilder::CreateFDiv(LHS, RHS, Name);
475   }
476
477   Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
478     if (Constant *LC = dyn_cast<Constant>(LHS))
479       if (Constant *RC = dyn_cast<Constant>(RHS))
480         return ConstantExpr::getURem(LC, RC);
481     return LLVMBuilder::CreateURem(LHS, RHS, Name);
482   }
483
484   Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
485     if (Constant *LC = dyn_cast<Constant>(LHS))
486       if (Constant *RC = dyn_cast<Constant>(RHS))
487         return ConstantExpr::getSRem(LC, RC);
488     return LLVMBuilder::CreateSRem(LHS, RHS, Name);
489   }
490
491   Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
492     if (Constant *LC = dyn_cast<Constant>(LHS))
493       if (Constant *RC = dyn_cast<Constant>(RHS))
494         return ConstantExpr::getFRem(LC, RC);
495     return LLVMBuilder::CreateFRem(LHS, RHS, Name);
496   }
497
498   Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
499     if (Constant *LC = dyn_cast<Constant>(LHS))
500       if (Constant *RC = dyn_cast<Constant>(RHS))
501         return ConstantExpr::getAnd(LC, RC);
502     return LLVMBuilder::CreateAnd(LHS, RHS, Name);
503   }
504
505   Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
506     if (Constant *LC = dyn_cast<Constant>(LHS))
507       if (Constant *RC = dyn_cast<Constant>(RHS))
508         return ConstantExpr::getOr(LC, RC);
509     return LLVMBuilder::CreateOr(LHS, RHS, Name);
510   }
511
512   Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
513     if (Constant *LC = dyn_cast<Constant>(LHS))
514       if (Constant *RC = dyn_cast<Constant>(RHS))
515         return ConstantExpr::getXor(LC, RC);
516     return LLVMBuilder::CreateXor(LHS, RHS, Name);
517   }
518
519   Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
520     if (Constant *LC = dyn_cast<Constant>(LHS))
521       if (Constant *RC = dyn_cast<Constant>(RHS))
522         return ConstantExpr::getShl(LC, RC);
523     return LLVMBuilder::CreateShl(LHS, RHS, Name);
524   }
525
526   Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
527     if (Constant *LC = dyn_cast<Constant>(LHS))
528       if (Constant *RC = dyn_cast<Constant>(RHS))
529         return ConstantExpr::getLShr(LC, RC);
530     return LLVMBuilder::CreateLShr(LHS, RHS, Name);
531   }
532
533   Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
534     if (Constant *LC = dyn_cast<Constant>(LHS))
535       if (Constant *RC = dyn_cast<Constant>(RHS))
536         return ConstantExpr::getAShr(LC, RC);
537     return LLVMBuilder::CreateAShr(LHS, RHS, Name);
538   }
539
540   //===--------------------------------------------------------------------===//
541   // Instruction creation methods: Memory Instructions
542   //===--------------------------------------------------------------------===//
543
544   template<typename InputIterator>
545   Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, 
546                    InputIterator IdxEnd, const char *Name = "") {
547     
548     if (Constant *PC = dyn_cast<Constant>(Ptr)) {
549       // Every index must be constant.
550       InputIterator i;
551       for (i = IdxBegin; i < IdxEnd; ++i) 
552         if (!dyn_cast<Constant>(*i))
553           break;
554       if (i == IdxEnd)
555         return ConstantExpr::getGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
556     }
557     return LLVMBuilder::CreateGEP(Ptr, IdxBegin, IdxEnd, Name);
558   }
559   Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
560     if (Constant *PC = dyn_cast<Constant>(Ptr))
561       if (Constant *IC = dyn_cast<Constant>(Idx))
562         return ConstantExpr::getGetElementPtr(PC, &IC, 1);
563     return LLVMBuilder::CreateGEP(Ptr, Idx, Name);
564   }
565   
566   //===--------------------------------------------------------------------===//
567   // Instruction creation methods: Cast/Conversion Operators
568   //===--------------------------------------------------------------------===//
569   
570   Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
571     return CreateCast(Instruction::Trunc, V, DestTy, Name);
572   }
573   Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
574     return CreateCast(Instruction::ZExt, V, DestTy, Name);
575   }
576   Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
577     return CreateCast(Instruction::SExt, V, DestTy, Name);
578   }
579   Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
580     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
581   }
582   Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
583     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
584   }
585   Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
586     return CreateCast(Instruction::UIToFP, V, DestTy, Name);
587   }
588   Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
589     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
590   }
591   Value *CreateFPTrunc(Value *V, const Type *DestTy,
592                        const char *Name = "") {
593     return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
594   }
595   Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
596     return CreateCast(Instruction::FPExt, V, DestTy, Name);
597   }
598   Value *CreatePtrToInt(Value *V, const Type *DestTy,
599                         const char *Name = "") {
600     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
601   }
602   Value *CreateIntToPtr(Value *V, const Type *DestTy,
603                         const char *Name = "") {
604     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
605   }
606   Value *CreateBitCast(Value *V, const Type *DestTy,
607                        const char *Name = "") {
608     return CreateCast(Instruction::BitCast, V, DestTy, Name);
609   }
610   
611   Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
612                     const char *Name = "") {
613     if (Constant *VC = dyn_cast<Constant>(V))
614       return ConstantExpr::getCast(Op, VC, DestTy);
615     return LLVMBuilder::CreateCast(Op, V, DestTy, Name);
616   }
617   Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
618                        const char *Name = "") {
619     if (Constant *VC = dyn_cast<Constant>(V))
620       return ConstantExpr::getIntegerCast(VC, DestTy, isSigned);
621     return LLVMBuilder::CreateIntCast(V, DestTy, isSigned, Name);
622   }
623   
624   //===--------------------------------------------------------------------===//
625   // Instruction creation methods: Compare Instructions
626   //===--------------------------------------------------------------------===//
627   
628   Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
629     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
630   }
631   Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
632     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
633   }
634   Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
635     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
636   }
637   Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
638     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
639   }
640   Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
641     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
642   }
643   Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
644     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
645   }
646   Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
647     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
648   }
649   Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
650     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
651   }
652   Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
653     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
654   }
655   Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
656     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
657   }
658
659   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
660     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
661   }
662   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
663     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
664   }
665   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
666     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
667   }
668   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
669     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
670   }
671   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
672     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
673   }
674   Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
675     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
676   }
677   Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
678     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
679   }
680   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
681     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
682   }
683   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
684     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
685   }
686   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
687     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
688   }
689   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
690     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
691   }
692   Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
693     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
694   }
695   Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
696     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
697   }
698   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
699     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
700   }
701   
702   Value *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS, 
703                     const char *Name = "") {
704     if (Constant *LC = dyn_cast<Constant>(LHS))
705       if (Constant *RC = dyn_cast<Constant>(RHS))
706         return ConstantExpr::getCompare(P, LC, RC);
707     return LLVMBuilder::CreateICmp(P, LHS, RHS, Name);
708   }
709
710   Value *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS, 
711                     const char *Name = "") {
712     if (Constant *LC = dyn_cast<Constant>(LHS))
713       if (Constant *RC = dyn_cast<Constant>(RHS))
714         return ConstantExpr::getCompare(P, LC, RC);
715     return LLVMBuilder::CreateFCmp(P, LHS, RHS, Name);
716   }
717
718   //===--------------------------------------------------------------------===//
719   // Instruction creation methods: Other Instructions
720   //===--------------------------------------------------------------------===//
721   
722   Value *CreateSelect(Value *C, Value *True, Value *False,
723                       const char *Name = "") {
724     if (Constant *CC = dyn_cast<Constant>(C))
725       if (Constant *TC = dyn_cast<Constant>(True))
726         if (Constant *FC = dyn_cast<Constant>(False))
727           return ConstantExpr::getSelect(CC, TC, FC);
728     return LLVMBuilder::CreateSelect(C, True, False, Name); 
729   }
730   
731   Value *CreateExtractElement(Value *Vec, Value *Idx,
732                               const char *Name = "") {
733     if (Constant *VC = dyn_cast<Constant>(Vec))
734       if (Constant *IC = dyn_cast<Constant>(Idx))
735         return ConstantExpr::getExtractElement(VC, IC);
736     return LLVMBuilder::CreateExtractElement(Vec, Idx, Name); 
737   }
738   
739   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
740                              const char *Name = "") {
741     if (Constant *VC = dyn_cast<Constant>(Vec))
742       if (Constant *NC = dyn_cast<Constant>(NewElt))
743         if (Constant *IC = dyn_cast<Constant>(Idx))
744           return ConstantExpr::getInsertElement(VC, NC, IC);
745     return LLVMBuilder::CreateInsertElement(Vec, NewElt, Idx, Name); 
746   }
747   
748   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
749                              const char *Name = "") {
750     if (Constant *V1C = dyn_cast<Constant>(V1))
751       if (Constant *V2C = dyn_cast<Constant>(V2))
752         if (Constant *MC = dyn_cast<Constant>(Mask))
753           return ConstantExpr::getShuffleVector(V1C, V2C, MC);
754     return LLVMBuilder::CreateShuffleVector(V1, V2, Mask, Name); 
755   }
756 };
757   
758 }
759
760 #endif