1 //===-- llvm/Support/LLVMBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_LLVMBUILDER_H
16 #define LLVM_SUPPORT_LLVMBUILDER_H
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Instructions.h"
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.
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.
35 BasicBlock::iterator InsertPt;
37 LLVMBuilder() { ClearInsertionPoint(); }
38 explicit LLVMBuilder(BasicBlock *TheBB) { SetInsertPoint(TheBB); }
39 LLVMBuilder(BasicBlock *TheBB, BasicBlock::iterator IP) {
40 SetInsertPoint(TheBB, IP);
43 //===--------------------------------------------------------------------===//
44 // Builder configuration methods
45 //===--------------------------------------------------------------------===//
47 /// ClearInsertionPoint - Clear the insertion point: created instructions will
48 /// not be inserted into a block.
49 void ClearInsertionPoint() {
53 BasicBlock *GetInsertBlock() const { return BB; }
55 /// SetInsertPoint - This specifies that created instructions should be
56 /// appended to the end of the specified block.
57 void SetInsertPoint(BasicBlock *TheBB) {
62 /// SetInsertPoint - This specifies that created instructions should be
63 /// inserted at the specified point.
64 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
69 /// Insert - Insert and return the specified instruction.
70 template<typename InstTy>
71 InstTy *Insert(InstTy *I) const {
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);
83 //===--------------------------------------------------------------------===//
84 // Instruction creation methods: Terminators
85 //===--------------------------------------------------------------------===//
87 /// CreateRetVoid - Create a 'ret void' instruction.
88 ReturnInst *CreateRetVoid() {
89 return Insert(new ReturnInst());
92 /// @verbatim CreateRet - Create a 'ret <val>' instruction. @endverbatim
93 ReturnInst *CreateRet(Value *V) {
94 return Insert(new ReturnInst(V));
97 /// CreateBr - Create an unconditional 'br label X' instruction.
98 BranchInst *CreateBr(BasicBlock *Dest) {
99 return Insert(new BranchInst(Dest));
102 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
104 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
105 return Insert(new BranchInst(True, False, Cond));
108 /// CreateSwitch - Create a switch instruction with the specified value,
109 /// default dest, and with a hint for the number of cases that will be added
110 /// (for efficient allocation).
111 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
112 return Insert(new SwitchInst(V, Dest, NumCases));
115 /// CreateInvoke - Create an invoke instruction.
116 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
117 BasicBlock *UnwindDest,
118 Value *const* Args, unsigned NumArgs,
119 const char *Name = "") {
120 return Insert(new InvokeInst(Callee, NormalDest, UnwindDest, Args, NumArgs,
124 UnwindInst *CreateUnwind() {
125 return Insert(new UnwindInst());
128 UnreachableInst *CreateUnreachable() {
129 return Insert(new UnreachableInst());
132 //===--------------------------------------------------------------------===//
133 // Instruction creation methods: Binary Operators
134 //===--------------------------------------------------------------------===//
136 BinaryOperator *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
137 return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
139 BinaryOperator *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
140 return Insert(BinaryOperator::createSub(LHS, RHS, Name));
142 BinaryOperator *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
143 return Insert(BinaryOperator::createMul(LHS, RHS, Name));
145 BinaryOperator *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
146 return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
148 BinaryOperator *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
149 return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
151 BinaryOperator *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
152 return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
154 BinaryOperator *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
155 return Insert(BinaryOperator::createURem(LHS, RHS, Name));
157 BinaryOperator *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
158 return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
160 BinaryOperator *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
161 return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
163 BinaryOperator *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
164 return Insert(BinaryOperator::createShl(LHS, RHS, Name));
166 BinaryOperator *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
167 return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
169 BinaryOperator *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
170 return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
172 BinaryOperator *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
173 return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
175 BinaryOperator *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
176 return Insert(BinaryOperator::createOr(LHS, RHS, Name));
178 BinaryOperator *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
179 return Insert(BinaryOperator::createXor(LHS, RHS, Name));
182 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
183 Value *LHS, Value *RHS, const char *Name = "") {
184 return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
187 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
188 return Insert(BinaryOperator::createNeg(V, Name));
190 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
191 return Insert(BinaryOperator::createNot(V, Name));
194 //===--------------------------------------------------------------------===//
195 // Instruction creation methods: Memory Instructions
196 //===--------------------------------------------------------------------===//
198 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
199 const char *Name = "") {
200 return Insert(new MallocInst(Ty, ArraySize, Name));
202 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
203 const char *Name = "") {
204 return Insert(new AllocaInst(Ty, ArraySize, Name));
206 FreeInst *CreateFree(Value *Ptr) {
207 return Insert(new FreeInst(Ptr));
209 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
210 return Insert(new LoadInst(Ptr, Name));
212 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
213 return Insert(new LoadInst(Ptr, Name, isVolatile));
215 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
216 return Insert(new StoreInst(Val, Ptr, isVolatile));
218 GetElementPtrInst *CreateGEP(Value *Ptr, Value* const *Idx, unsigned NumIdx,
219 const char *Name = "") {
220 return Insert(new GetElementPtrInst(Ptr, Idx, NumIdx, Name));
222 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
223 return Insert(new GetElementPtrInst(Ptr, &Idx, 1, Name));
225 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx0, Value *Idx1,
226 const char *Name = "") {
227 return Insert(new GetElementPtrInst(Ptr, Idx0, Idx1, Name));
230 //===--------------------------------------------------------------------===//
231 // Instruction creation methods: Cast/Conversion Operators
232 //===--------------------------------------------------------------------===//
234 TruncInst *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
235 return Insert(new TruncInst(V, DestTy, Name));
237 ZExtInst *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
238 return Insert(new ZExtInst(V, DestTy, Name));
240 SExtInst *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
241 return Insert(new SExtInst(V, DestTy, Name));
243 FPToUIInst *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
244 return Insert(new FPToUIInst(V, DestTy, Name));
246 FPToSIInst *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
247 return Insert(new FPToSIInst(V, DestTy, Name));
249 UIToFPInst *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
250 return Insert(new UIToFPInst(V, DestTy, Name));
252 SIToFPInst *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
253 return Insert(new SIToFPInst(V, DestTy, Name));
255 FPTruncInst *CreateFPTrunc(Value *V, const Type *DestTy,
256 const char *Name = "") {
257 return Insert(new FPTruncInst(V, DestTy, Name));
259 FPExtInst *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
260 return Insert(new FPExtInst(V, DestTy, Name));
262 PtrToIntInst *CreatePtrToInt(Value *V, const Type *DestTy,
263 const char *Name = "") {
264 return Insert(new PtrToIntInst(V, DestTy, Name));
266 IntToPtrInst *CreateIntToPtr(Value *V, const Type *DestTy,
267 const char *Name = "") {
268 return Insert(new IntToPtrInst(V, DestTy, Name));
270 BitCastInst *CreateBitCast(Value *V, const Type *DestTy,
271 const char *Name = "") {
272 return Insert(new BitCastInst(V, DestTy, Name));
275 CastInst *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
276 const char *Name = "") {
277 return Insert(CastInst::create(Op, V, DestTy, Name));
279 CastInst *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
280 const char *Name = "") {
281 return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
286 //===--------------------------------------------------------------------===//
287 // Instruction creation methods: Compare Instructions
288 //===--------------------------------------------------------------------===//
290 ICmpInst *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
291 return Insert(new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS, Name));
293 ICmpInst *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
294 return Insert(new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS, Name));
296 ICmpInst *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
297 return Insert(new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, Name));
299 ICmpInst *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
300 return Insert(new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS, Name));
302 ICmpInst *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
303 return Insert(new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS, Name));
305 ICmpInst *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
306 return Insert(new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS, Name));
308 ICmpInst *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
309 return Insert(new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, Name));
311 ICmpInst *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
312 return Insert(new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS, Name));
314 ICmpInst *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
315 return Insert(new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS, Name));
317 ICmpInst *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
318 return Insert(new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS, Name));
321 FCmpInst *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
322 return Insert(new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS, Name));
324 FCmpInst *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
325 return Insert(new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS, Name));
327 FCmpInst *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
328 return Insert(new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS, Name));
330 FCmpInst *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
331 return Insert(new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS, Name));
333 FCmpInst *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
334 return Insert(new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS, Name));
336 FCmpInst *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
337 return Insert(new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS, Name));
339 FCmpInst *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
340 return Insert(new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS, Name));
342 FCmpInst *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
343 return Insert(new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS, Name));
345 FCmpInst *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
346 return Insert(new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS, Name));
348 FCmpInst *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
349 return Insert(new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS, Name));
351 FCmpInst *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
352 return Insert(new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS, Name));
354 FCmpInst *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
355 return Insert(new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS, Name));
357 FCmpInst *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
358 return Insert(new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS, Name));
360 FCmpInst *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
361 return Insert(new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS, Name));
365 ICmpInst *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS,
366 const char *Name = "") {
367 return Insert(new ICmpInst(P, LHS, RHS, Name));
369 FCmpInst *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS,
370 const char *Name = "") {
371 return Insert(new FCmpInst(P, LHS, RHS, Name));
374 //===--------------------------------------------------------------------===//
375 // Instruction creation methods: Other Instructions
376 //===--------------------------------------------------------------------===//
378 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
379 return Insert(new PHINode(Ty, Name));
382 CallInst *CreateCall(Value *Callee, const char *Name = "") {
383 return Insert(new CallInst(Callee, Name));
385 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
386 return Insert(new CallInst(Callee, Arg, Name));
389 template<typename InputIterator>
390 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin, InputIterator ArgEnd,
391 const char *Name = "") {
392 return(Insert(new CallInst(Callee, ArgBegin, ArgEnd, Name)));
396 CallInst *CreateCall(Value *Callee, Value *Arg0, Value *Arg1,
397 const char *Name = "") {
398 Value *Args[] = { Arg0, Arg1 };
399 return Insert(new CallInst(Callee, Args, Args+2, Name));
402 // Leave this here for llvm-gcc
403 CallInst *CreateCall(Value *Callee, Value* const *Args, unsigned NumArgs,
404 const char *Name = "") {
405 return Insert(new CallInst(Callee, Args, Args+NumArgs, Name));
409 SelectInst *CreateSelect(Value *C, Value *True, Value *False,
410 const char *Name = "") {
411 return Insert(new SelectInst(C, True, False, Name));
414 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
415 return Insert(new VAArgInst(List, Ty, Name));
418 ExtractElementInst *CreateExtractElement(Value *Vec, Value *Idx,
419 const char *Name = "") {
420 return Insert(new ExtractElementInst(Vec, Idx, Name));
423 InsertElementInst *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
424 const char *Name = "") {
425 return Insert(new InsertElementInst(Vec, NewElt, Idx, Name));
428 ShuffleVectorInst *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
429 const char *Name = "") {
430 return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
434 // TODO: A version of LLVMBuilder that constant folds operands as they come in.
435 //class LLVMFoldingBuilder {