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());
93 /// CreateRet - Create a 'ret <val>' instruction.
95 ReturnInst *CreateRet(Value *V) {
96 return Insert(new ReturnInst(V));
99 /// CreateBr - Create an unconditional 'br label X' instruction.
100 BranchInst *CreateBr(BasicBlock *Dest) {
101 return Insert(new BranchInst(Dest));
104 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
106 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
107 return Insert(new BranchInst(True, False, Cond));
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));
117 /// CreateInvoke - Create an invoke instruction.
118 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
119 BasicBlock *UnwindDest,
120 Value *const* Args, unsigned NumArgs,
121 const char *Name = "") {
122 return Insert(new InvokeInst(Callee, NormalDest, UnwindDest, Args, NumArgs,
126 UnwindInst *CreateUnwind() {
127 return Insert(new UnwindInst());
130 UnreachableInst *CreateUnreachable() {
131 return Insert(new UnreachableInst());
134 //===--------------------------------------------------------------------===//
135 // Instruction creation methods: Binary Operators
136 //===--------------------------------------------------------------------===//
138 BinaryOperator *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
139 return Insert(BinaryOperator::createAdd(LHS, RHS, Name));
141 BinaryOperator *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
142 return Insert(BinaryOperator::createSub(LHS, RHS, Name));
144 BinaryOperator *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
145 return Insert(BinaryOperator::createMul(LHS, RHS, Name));
147 BinaryOperator *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
148 return Insert(BinaryOperator::createUDiv(LHS, RHS, Name));
150 BinaryOperator *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
151 return Insert(BinaryOperator::createSDiv(LHS, RHS, Name));
153 BinaryOperator *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
154 return Insert(BinaryOperator::createFDiv(LHS, RHS, Name));
156 BinaryOperator *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
157 return Insert(BinaryOperator::createURem(LHS, RHS, Name));
159 BinaryOperator *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
160 return Insert(BinaryOperator::createSRem(LHS, RHS, Name));
162 BinaryOperator *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
163 return Insert(BinaryOperator::createFRem(LHS, RHS, Name));
165 BinaryOperator *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
166 return Insert(BinaryOperator::createShl(LHS, RHS, Name));
168 BinaryOperator *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
169 return Insert(BinaryOperator::createLShr(LHS, RHS, Name));
171 BinaryOperator *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
172 return Insert(BinaryOperator::createAShr(LHS, RHS, Name));
174 BinaryOperator *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
175 return Insert(BinaryOperator::createAnd(LHS, RHS, Name));
177 BinaryOperator *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
178 return Insert(BinaryOperator::createOr(LHS, RHS, Name));
180 BinaryOperator *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
181 return Insert(BinaryOperator::createXor(LHS, RHS, Name));
184 BinaryOperator *CreateBinOp(Instruction::BinaryOps Opc,
185 Value *LHS, Value *RHS, const char *Name = "") {
186 return Insert(BinaryOperator::create(Opc, LHS, RHS, Name));
189 BinaryOperator *CreateNeg(Value *V, const char *Name = "") {
190 return Insert(BinaryOperator::createNeg(V, Name));
192 BinaryOperator *CreateNot(Value *V, const char *Name = "") {
193 return Insert(BinaryOperator::createNot(V, Name));
196 //===--------------------------------------------------------------------===//
197 // Instruction creation methods: Memory Instructions
198 //===--------------------------------------------------------------------===//
200 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
201 const char *Name = "") {
202 return Insert(new MallocInst(Ty, ArraySize, Name));
204 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
205 const char *Name = "") {
206 return Insert(new AllocaInst(Ty, ArraySize, Name));
208 FreeInst *CreateFree(Value *Ptr) {
209 return Insert(new FreeInst(Ptr));
211 LoadInst *CreateLoad(Value *Ptr, const char *Name = 0) {
212 return Insert(new LoadInst(Ptr, Name));
214 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = 0) {
215 return Insert(new LoadInst(Ptr, Name, isVolatile));
217 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
218 return Insert(new StoreInst(Val, Ptr, isVolatile));
220 GetElementPtrInst *CreateGEP(Value *Ptr, Value* const *Idx, unsigned NumIdx,
221 const char *Name = "") {
222 return Insert(new GetElementPtrInst(Ptr, Idx, NumIdx, Name));
224 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
225 return Insert(new GetElementPtrInst(Ptr, &Idx, 1, Name));
227 GetElementPtrInst *CreateGEP(Value *Ptr, Value *Idx0, Value *Idx1,
228 const char *Name = "") {
229 return Insert(new GetElementPtrInst(Ptr, Idx0, Idx1, Name));
232 //===--------------------------------------------------------------------===//
233 // Instruction creation methods: Cast/Conversion Operators
234 //===--------------------------------------------------------------------===//
236 TruncInst *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
237 return Insert(new TruncInst(V, DestTy, Name));
239 ZExtInst *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
240 return Insert(new ZExtInst(V, DestTy, Name));
242 SExtInst *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
243 return Insert(new SExtInst(V, DestTy, Name));
245 FPToUIInst *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
246 return Insert(new FPToUIInst(V, DestTy, Name));
248 FPToSIInst *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
249 return Insert(new FPToSIInst(V, DestTy, Name));
251 UIToFPInst *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
252 return Insert(new UIToFPInst(V, DestTy, Name));
254 SIToFPInst *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
255 return Insert(new SIToFPInst(V, DestTy, Name));
257 FPTruncInst *CreateFPTrunc(Value *V, const Type *DestTy,
258 const char *Name = "") {
259 return Insert(new FPTruncInst(V, DestTy, Name));
261 FPExtInst *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
262 return Insert(new FPExtInst(V, DestTy, Name));
264 PtrToIntInst *CreatePtrToInt(Value *V, const Type *DestTy,
265 const char *Name = "") {
266 return Insert(new PtrToIntInst(V, DestTy, Name));
268 IntToPtrInst *CreateIntToPtr(Value *V, const Type *DestTy,
269 const char *Name = "") {
270 return Insert(new IntToPtrInst(V, DestTy, Name));
272 BitCastInst *CreateBitCast(Value *V, const Type *DestTy,
273 const char *Name = "") {
274 return Insert(new BitCastInst(V, DestTy, Name));
277 CastInst *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
278 const char *Name = "") {
279 return Insert(CastInst::create(Op, V, DestTy, Name));
281 CastInst *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
282 const char *Name = "") {
283 return Insert(CastInst::createIntegerCast(V, DestTy, isSigned, Name));
288 //===--------------------------------------------------------------------===//
289 // Instruction creation methods: Compare Instructions
290 //===--------------------------------------------------------------------===//
292 ICmpInst *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
293 return Insert(new ICmpInst(ICmpInst::ICMP_EQ, LHS, RHS, Name));
295 ICmpInst *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
296 return Insert(new ICmpInst(ICmpInst::ICMP_NE, LHS, RHS, Name));
298 ICmpInst *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
299 return Insert(new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, Name));
301 ICmpInst *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
302 return Insert(new ICmpInst(ICmpInst::ICMP_UGE, LHS, RHS, Name));
304 ICmpInst *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
305 return Insert(new ICmpInst(ICmpInst::ICMP_ULT, LHS, RHS, Name));
307 ICmpInst *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
308 return Insert(new ICmpInst(ICmpInst::ICMP_ULE, LHS, RHS, Name));
310 ICmpInst *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
311 return Insert(new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, Name));
313 ICmpInst *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
314 return Insert(new ICmpInst(ICmpInst::ICMP_SGE, LHS, RHS, Name));
316 ICmpInst *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
317 return Insert(new ICmpInst(ICmpInst::ICMP_SLT, LHS, RHS, Name));
319 ICmpInst *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
320 return Insert(new ICmpInst(ICmpInst::ICMP_SLE, LHS, RHS, Name));
323 FCmpInst *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
324 return Insert(new FCmpInst(FCmpInst::FCMP_OEQ, LHS, RHS, Name));
326 FCmpInst *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
327 return Insert(new FCmpInst(FCmpInst::FCMP_OGT, LHS, RHS, Name));
329 FCmpInst *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
330 return Insert(new FCmpInst(FCmpInst::FCMP_OGE, LHS, RHS, Name));
332 FCmpInst *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
333 return Insert(new FCmpInst(FCmpInst::FCMP_OLT, LHS, RHS, Name));
335 FCmpInst *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
336 return Insert(new FCmpInst(FCmpInst::FCMP_OLE, LHS, RHS, Name));
338 FCmpInst *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
339 return Insert(new FCmpInst(FCmpInst::FCMP_ONE, LHS, RHS, Name));
341 FCmpInst *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
342 return Insert(new FCmpInst(FCmpInst::FCMP_ORD, LHS, RHS, Name));
344 FCmpInst *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
345 return Insert(new FCmpInst(FCmpInst::FCMP_UNO, LHS, RHS, Name));
347 FCmpInst *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
348 return Insert(new FCmpInst(FCmpInst::FCMP_UEQ, LHS, RHS, Name));
350 FCmpInst *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
351 return Insert(new FCmpInst(FCmpInst::FCMP_UGT, LHS, RHS, Name));
353 FCmpInst *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
354 return Insert(new FCmpInst(FCmpInst::FCMP_UGE, LHS, RHS, Name));
356 FCmpInst *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
357 return Insert(new FCmpInst(FCmpInst::FCMP_ULT, LHS, RHS, Name));
359 FCmpInst *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
360 return Insert(new FCmpInst(FCmpInst::FCMP_ULE, LHS, RHS, Name));
362 FCmpInst *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
363 return Insert(new FCmpInst(FCmpInst::FCMP_UNE, LHS, RHS, Name));
367 ICmpInst *CreateICmp(ICmpInst::Predicate P, Value *LHS, Value *RHS,
368 const char *Name = "") {
369 return Insert(new ICmpInst(P, LHS, RHS, Name));
371 FCmpInst *CreateFCmp(FCmpInst::Predicate P, Value *LHS, Value *RHS,
372 const char *Name = "") {
373 return Insert(new FCmpInst(P, LHS, RHS, Name));
376 //===--------------------------------------------------------------------===//
377 // Instruction creation methods: Other Instructions
378 //===--------------------------------------------------------------------===//
380 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
381 return Insert(new PHINode(Ty, Name));
384 CallInst *CreateCall(Value *Callee, const char *Name = "") {
385 return Insert(new CallInst(Callee, Name));
387 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
388 return Insert(new CallInst(Callee, Arg, Name));
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)));
397 SelectInst *CreateSelect(Value *C, Value *True, Value *False,
398 const char *Name = "") {
399 return Insert(new SelectInst(C, True, False, Name));
402 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
403 return Insert(new VAArgInst(List, Ty, Name));
406 ExtractElementInst *CreateExtractElement(Value *Vec, Value *Idx,
407 const char *Name = "") {
408 return Insert(new ExtractElementInst(Vec, Idx, Name));
411 InsertElementInst *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
412 const char *Name = "") {
413 return Insert(new InsertElementInst(Vec, NewElt, Idx, Name));
416 ShuffleVectorInst *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
417 const char *Name = "") {
418 return Insert(new ShuffleVectorInst(V1, V2, Mask, Name));
422 // TODO: A version of LLVMBuilder that constant folds operands as they come in.
423 //class LLVMFoldingBuilder {