Update InvokeInst to work like CallInst
[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 was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source 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
21 namespace llvm {
22
23 /// LLVMBuilder - This provides a uniform API for creating instructions and
24 /// inserting them into a basic block: either at the end of a BasicBlock, or 
25 /// at a specific iterator location in a block.
26 ///
27 /// Note that the builder does not expose the full generality of LLVM
28 /// instructions.  For example, it cannot be used to create instructions with
29 /// arbitrary names (specifically, names with nul characters in them) - It only
30 /// supports nul-terminated C strings.  For fully generic names, use
31 /// I->setName().  For access to extra instruction properties, use the mutators
32 /// (e.g. setVolatile) on the instructions after they have been created.
33 class LLVMBuilder {
34   BasicBlock *BB;
35   BasicBlock::iterator InsertPt;
36 public:
37   LLVMBuilder() { ClearInsertionPoint(); }
38   explicit LLVMBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
39   LLVMBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
40     SetInsertPoint(TheBB, IP);
41   }
42
43   //===--------------------------------------------------------------------===//
44   // Builder configuration methods
45   //===--------------------------------------------------------------------===//
46
47   /// ClearInsertionPoint - Clear the insertion point: created instructions will
48   /// not be inserted into a block.
49   void ClearInsertionPoint() {
50     BB = 0;
51   }
52   
53   BasicBlock *GetInsertBlock() const { return BB; }
54   
55   /// SetInsertPoint - This specifies that created instructions should be
56   /// appended to the end of the specified block.
57   void SetInsertPoint(BasicBlock *TheBB) {
58     BB = TheBB;
59     InsertPt = BB->end();
60   }
61   
62   /// SetInsertPoint - This specifies that created instructions should be
63   /// inserted at the specified point.
64   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
65     BB = TheBB;
66     InsertPt = IP;
67   }
68   
69   /// Insert - Insert and return the specified instruction.
70   template<typename InstTy>
71   InstTy *Insert(InstTy *I) const {
72     InsertHelper(I);
73     return I;
74   }
75   
76   /// InsertHelper - Insert the specified instruction at the specified insertion
77   /// point.  This is split out of Insert so that it isn't duplicated for every
78   /// template instantiation.
79   void InsertHelper(Instruction *I) const {
80     if (BB) BB->getInstList().insert(InsertPt, I);
81   }
82   
83   //===--------------------------------------------------------------------===//
84   // Instruction creation methods: Terminators
85   //===--------------------------------------------------------------------===//
86
87   /// CreateRetVoid - Create a 'ret void' instruction.
88   ReturnInst *CreateRetVoid() {
89     return Insert(new ReturnInst());
90   }
91
92   /// @verbatim 
93   /// CreateRet - Create a 'ret <val>' instruction. 
94   /// @endverbatim
95   ReturnInst *CreateRet(Value *V) {
96     return Insert(new ReturnInst(V));
97   }
98   
99   /// CreateBr - Create an unconditional 'br label X' instruction.
100   BranchInst *CreateBr(BasicBlock *Dest) {
101     return Insert(new BranchInst(Dest));
102   }
103
104   /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
105   /// instruction.
106   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
107     return Insert(new BranchInst(True, False, Cond));
108   }
109   
110   /// CreateSwitch - Create a switch instruction with the specified value,
111   /// default dest, and with a hint for the number of cases that will be added
112   /// (for efficient allocation).
113   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
114     return Insert(new SwitchInst(V, Dest, NumCases));
115   }
116   
117   /// CreateInvoke - Create an invoke instruction.
118   template<typename InputIterator>
119   InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest, 
120                            BasicBlock *UnwindDest, InputIterator ArgBegin, 
121                            InputIterator ArgEnd, const char *Name = "") {
122     return(Insert(new InvokeInst(Callee, NormalDest, UnwindDest,
123                                  ArgBegin, ArgEnd, Name)));
124   }
125   
126   UnwindInst *CreateUnwind() {
127     return Insert(new UnwindInst());
128   }
129
130   UnreachableInst *CreateUnreachable() {
131     return Insert(new UnreachableInst());
132   }
133   
134   //===--------------------------------------------------------------------===//
135   // Instruction creation methods: Binary Operators
136   //===--------------------------------------------------------------------===//
137
138   BinaryOperator *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
139     return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
140   }
141   BinaryOperator *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
142     return Insert(BinaryOperator::createSub(LHS, RHS, Name));
143   }
144   BinaryOperator *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
145     return Insert(BinaryOperator::createMul(LHS, RHS, Name));
146   }
147   BinaryOperator *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
148     return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
149   }
150   BinaryOperator *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
151     return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
152   }
153   BinaryOperator *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
154     return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
155   }
156   BinaryOperator *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
157     return Insert(BinaryOperator::createURem(LHS, RHS, Name));
158   }
159   BinaryOperator *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
160     return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
161   }
162   BinaryOperator *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
163     return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
164   }
165   BinaryOperator *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
166     return Insert(BinaryOperator::createShl(LHS, RHS, Name));
167   }
168   BinaryOperator *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
169     return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
170   }
171   BinaryOperator *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
172     return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
173   }
174   BinaryOperator *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
175     return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
176   }
177   BinaryOperator *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
178     return Insert(BinaryOperator::createOr(LHS, RHS, Name));
179   }
180   BinaryOperator *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
181     return Insert(BinaryOperator::createXor(LHS, RHS, Name));
182   }
183
184   BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
185                               Value *LHS, Value *RHS, const char *Name = "") {
186     return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
187   }
188   
189   BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
190     return Insert(BinaryOperator::createNeg(V, Name));
191   }
192   BinaryOperator *CreateNot(Value *V, const char *Name = "") {
193     return Insert(BinaryOperator::createNot(V, Name));
194   }
195   
196   //===--------------------------------------------------------------------===//
197   // Instruction creation methods: Memory Instructions
198   //===--------------------------------------------------------------------===//
199   
200   MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
201                            const char *Name = "") {
202     return Insert(new MallocInst(Ty, ArraySize, Name));
203   }
204   AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
205                            const char *Name = "") {
206     return Insert(new AllocaInst(Ty, ArraySize, Name));
207   }
208   FreeInst *CreateFree(Value *Ptr) {
209     return Insert(new FreeInst(Ptr));
210   }
211   LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
212     return Insert(new LoadInst(Ptr, Name));
213   }
214   LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
215     return Insert(new LoadInst(Ptr, Name, isVolatile));
216   }
217   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
218     return Insert(new StoreInst(Val, Ptr, isVolatile));
219   }
220   GetElementPtrInst *CreateGEP(Value *Ptr, Value* const *Idx, unsigned NumIdx,
221                                const char *Name = "") {
222     return Insert(new GetElementPtrInst(Ptr, Idx, NumIdx, Name));
223   }
224   GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
225     return Insert(new GetElementPtrInst(Ptr, &Idx, 1, Name));
226   }
227   GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx0, Value *Idx1,
228                                const char *Name = "") {
229     return Insert(new GetElementPtrInst(Ptr, Idx0, Idx1, Name));
230   }
231   
232   //===--------------------------------------------------------------------===//
233   // Instruction creation methods: Cast/Conversion Operators
234   //===--------------------------------------------------------------------===//
235   
236   TruncInst *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
237     return Insert(new TruncInst(V, DestTy, Name));
238   }
239   ZExtInst *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
240     return Insert(new ZExtInst(V, DestTy, Name));
241   }
242   SExtInst *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
243     return Insert(new SExtInst(V, DestTy, Name));
244   }
245   FPToUIInst *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
246     return Insert(new FPToUIInst(V, DestTy, Name));
247   }
248   FPToSIInst *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
249     return Insert(new FPToSIInst(V, DestTy, Name));
250   }
251   UIToFPInst *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
252     return Insert(new UIToFPInst(V, DestTy, Name));
253   }
254   SIToFPInst *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
255     return Insert(new SIToFPInst(V, DestTy, Name));
256   }
257   FPTruncInst *CreateFPTrunc(Value *V, const Type *DestTy,
258                              const char *Name = "") {
259     return Insert(new FPTruncInst(V, DestTy, Name));
260   }
261   FPExtInst *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
262     return Insert(new FPExtInst(V, DestTy, Name));
263   }
264   PtrToIntInst *CreatePtrToInt(Value *V, const Type *DestTy,
265                                const char *Name = "") {
266     return Insert(new PtrToIntInst(V, DestTy, Name));
267   }
268   IntToPtrInst *CreateIntToPtr(Value *V, const Type *DestTy,
269                                const char *Name = "") {
270     return Insert(new IntToPtrInst(V, DestTy, Name));
271   }
272   BitCastInst *CreateBitCast(Value *V, const Type *DestTy,
273                              const char *Name = "") {
274     return Insert(new BitCastInst(V, DestTy, Name));
275   }
276   
277   CastInst *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
278                        const char *Name = "") {
279     return Insert(CastInst::create(Op, V, DestTy, Name));
280   }
281   CastInst *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
282                           const char *Name = "") {
283     return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
284   }
285   
286   
287   
288   //===--------------------------------------------------------------------===//
289   // Instruction creation methods: Compare Instructions
290   //===--------------------------------------------------------------------===//
291   
292   ICmpInst *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
293     return Insert(new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS, Name));
294   }
295   ICmpInst *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
296     return Insert(new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS, Name));
297   }
298   ICmpInst *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
299     return Insert(new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, Name));
300   }
301   ICmpInst *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
302     return Insert(new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS, Name));
303   }
304   ICmpInst *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
305     return Insert(new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS, Name));
306   }
307   ICmpInst *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
308     return Insert(new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS, Name));
309   }
310   ICmpInst *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
311     return Insert(new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, Name));
312   }
313   ICmpInst *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
314     return Insert(new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS, Name));
315   }
316   ICmpInst *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
317     return Insert(new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS, Name));
318   }
319   ICmpInst *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
320     return Insert(new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS, Name));
321   }
322   
323   FCmpInst *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
324     return Insert(new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS, Name));
325   }
326   FCmpInst *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
327     return Insert(new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS, Name));
328   }
329   FCmpInst *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
330     return Insert(new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS, Name));
331   }
332   FCmpInst *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
333     return Insert(new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS, Name));
334   }
335   FCmpInst *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
336     return Insert(new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS, Name));
337   }
338   FCmpInst *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
339     return Insert(new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS, Name));
340   }
341   FCmpInst *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
342     return Insert(new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS, Name));
343   }
344   FCmpInst *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
345     return Insert(new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS, Name));
346   }
347   FCmpInst *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
348     return Insert(new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS, Name));
349   }
350   FCmpInst *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
351     return Insert(new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS, Name));
352   }
353   FCmpInst *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
354     return Insert(new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS, Name));
355   }
356   FCmpInst *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
357     return Insert(new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS, Name));
358   }
359   FCmpInst *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
360     return Insert(new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS, Name));
361   }
362   FCmpInst *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
363     return Insert(new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS, Name));
364   }
365   
366   
367   ICmpInst *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS, 
368                        const char *Name = "") {
369     return Insert(new ICmpInst(P, LHS, RHS, Name));
370   }
371   FCmpInst *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS, 
372                        const char *Name = "") {
373     return Insert(new FCmpInst(P, LHS, RHS, Name));
374   }
375   
376   //===--------------------------------------------------------------------===//
377   // Instruction creation methods: Other Instructions
378   //===--------------------------------------------------------------------===//
379   
380   PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
381     return Insert(new PHINode(Ty, Name));
382   }
383
384   CallInst *CreateCall(Value *Callee, const char *Name = "") {
385     return Insert(new CallInst(Callee, Name));
386   }
387   CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
388     return Insert(new CallInst(Callee, Arg, Name));
389   }
390
391   template<typename InputIterator>
392   CallInst *CreateCall(Value *Callee, InputIterator ArgBegin, 
393                        InputIterator ArgEnd, const char *Name = "") {
394     return(Insert(new CallInst(Callee, ArgBegin, ArgEnd, Name)));
395   }
396   
397   SelectInst *CreateSelect(Value *C, Value *True, Value *False,
398                            const char *Name = "") {
399     return Insert(new SelectInst(C, True, False, Name));
400   }
401   
402   VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
403     return Insert(new VAArgInst(List, Ty, Name));
404   }
405   
406   ExtractElementInst *CreateExtractElement(Value *Vec, Value *Idx,
407                                            const char *Name = "") {
408     return Insert(new ExtractElementInst(Vec, Idx, Name));
409   }
410   
411   InsertElementInst *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
412                                          const char *Name = "") {
413     return Insert(new InsertElementInst(Vec, NewElt, Idx, Name));
414   }
415   
416   ShuffleVectorInst *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
417                                          const char *Name = "") {
418     return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
419   }
420 };
421
422 // TODO: A version of LLVMBuilder that constant folds operands as they come in.
423 //class LLVMFoldingBuilder {
424 //};
425   
426 }
427
428 #endif