1 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_IRBUILDER_H
16 #define LLVM_SUPPORT_IRBUILDER_H
18 #include "llvm/BasicBlock.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Function.h"
23 #include "llvm/Support/ConstantFolder.h"
27 /// IRBuilder - This provides a uniform API for creating instructions and
28 /// inserting them into a basic block: either at the end of a BasicBlock, or
29 /// at a specific iterator location in a block.
31 /// Note that the builder does not expose the full generality of LLVM
32 /// instructions. For example, it cannot be used to create instructions with
33 /// arbitrary names (specifically, names with nul characters in them) - It only
34 /// supports nul-terminated C strings. For fully generic names, use
35 /// I->setName(). For access to extra instruction properties, use the mutators
36 /// (e.g. setVolatile) on the instructions after they have been created.
37 /// The first template argument handles whether or not to preserve names in the
38 /// final instruction output. This defaults to on. The second template argument
39 /// specifies a class to use for creating constants. This defaults to creating
40 /// minimally folded constants.
41 template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
43 BasicBlock::iterator InsertPt;
46 IRBuilder(const T& F = T()) : Folder(F) { ClearInsertionPoint(); }
47 explicit IRBuilder(BasicBlock *TheBB, const T& F = T())
48 : Folder(F) { SetInsertPoint(TheBB); }
49 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T())
50 : Folder(F) { SetInsertPoint(TheBB, IP); }
52 /// getFolder - Get the constant folder being used.
53 const T& getFolder() { return Folder; }
55 //===--------------------------------------------------------------------===//
56 // Builder configuration methods
57 //===--------------------------------------------------------------------===//
59 /// ClearInsertionPoint - Clear the insertion point: created instructions will
60 /// not be inserted into a block.
61 void ClearInsertionPoint() {
65 BasicBlock *GetInsertBlock() const { return BB; }
67 /// SetInsertPoint - This specifies that created instructions should be
68 /// appended to the end of the specified block.
69 void SetInsertPoint(BasicBlock *TheBB) {
74 /// SetInsertPoint - This specifies that created instructions should be
75 /// inserted at the specified point.
76 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
81 /// Insert - Insert and return the specified instruction.
82 template<typename InstTy>
83 InstTy *Insert(InstTy *I, const char *Name = "") const {
84 InsertHelper(I, Name);
88 /// InsertHelper - Insert the specified instruction at the specified insertion
89 /// point. This is split out of Insert so that it isn't duplicated for every
90 /// template instantiation.
91 void InsertHelper(Instruction *I, const char *Name) const {
92 if (BB) BB->getInstList().insert(InsertPt, I);
93 if (preserveNames && Name[0])
97 //===--------------------------------------------------------------------===//
98 // Instruction creation methods: Terminators
99 //===--------------------------------------------------------------------===//
101 /// CreateRetVoid - Create a 'ret void' instruction.
102 ReturnInst *CreateRetVoid() {
103 return Insert(ReturnInst::Create());
107 /// CreateRet - Create a 'ret <val>' instruction.
109 ReturnInst *CreateRet(Value *V) {
110 return Insert(ReturnInst::Create(V));
113 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
114 /// with one Value from the retVals array each, that build a aggregate
115 /// return value one value at a time, and a ret instruction to return
116 /// the resulting aggregate value. This is a convenience function for
117 /// code that uses aggregate return values as a vehicle for having
118 /// multiple return values.
120 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
121 const Type *RetType = BB->getParent()->getReturnType();
122 Value *V = UndefValue::get(RetType);
123 for (unsigned i = 0; i != N; ++i)
124 V = CreateInsertValue(V, retVals[i], i, "mrv");
125 return Insert(ReturnInst::Create(V));
128 /// CreateBr - Create an unconditional 'br label X' instruction.
129 BranchInst *CreateBr(BasicBlock *Dest) {
130 return Insert(BranchInst::Create(Dest));
133 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
135 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
136 return Insert(BranchInst::Create(True, False, Cond));
139 /// CreateSwitch - Create a switch instruction with the specified value,
140 /// default dest, and with a hint for the number of cases that will be added
141 /// (for efficient allocation).
142 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
143 return Insert(SwitchInst::Create(V, Dest, NumCases));
146 /// CreateInvoke - Create an invoke instruction.
147 template<typename InputIterator>
148 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
149 BasicBlock *UnwindDest, InputIterator ArgBegin,
150 InputIterator ArgEnd, const char *Name = "") {
151 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
152 ArgBegin, ArgEnd), Name);
155 UnwindInst *CreateUnwind() {
156 return Insert(new UnwindInst());
159 UnreachableInst *CreateUnreachable() {
160 return Insert(new UnreachableInst());
163 //===--------------------------------------------------------------------===//
164 // Instruction creation methods: Binary Operators
165 //===--------------------------------------------------------------------===//
167 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
168 if (Constant *LC = dyn_cast<Constant>(LHS))
169 if (Constant *RC = dyn_cast<Constant>(RHS))
170 return Folder.CreateAdd(LC, RC);
171 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
173 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
174 if (Constant *LC = dyn_cast<Constant>(LHS))
175 if (Constant *RC = dyn_cast<Constant>(RHS))
176 return Folder.CreateSub(LC, RC);
177 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
179 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
180 if (Constant *LC = dyn_cast<Constant>(LHS))
181 if (Constant *RC = dyn_cast<Constant>(RHS))
182 return Folder.CreateMul(LC, RC);
183 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
185 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
186 if (Constant *LC = dyn_cast<Constant>(LHS))
187 if (Constant *RC = dyn_cast<Constant>(RHS))
188 return Folder.CreateUDiv(LC, RC);
189 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
191 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
192 if (Constant *LC = dyn_cast<Constant>(LHS))
193 if (Constant *RC = dyn_cast<Constant>(RHS))
194 return Folder.CreateSDiv(LC, RC);
195 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
197 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
198 if (Constant *LC = dyn_cast<Constant>(LHS))
199 if (Constant *RC = dyn_cast<Constant>(RHS))
200 return Folder.CreateFDiv(LC, RC);
201 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
203 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
204 if (Constant *LC = dyn_cast<Constant>(LHS))
205 if (Constant *RC = dyn_cast<Constant>(RHS))
206 return Folder.CreateURem(LC, RC);
207 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
209 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
210 if (Constant *LC = dyn_cast<Constant>(LHS))
211 if (Constant *RC = dyn_cast<Constant>(RHS))
212 return Folder.CreateSRem(LC, RC);
213 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
215 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
216 if (Constant *LC = dyn_cast<Constant>(LHS))
217 if (Constant *RC = dyn_cast<Constant>(RHS))
218 return Folder.CreateFRem(LC, RC);
219 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
221 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
222 if (Constant *LC = dyn_cast<Constant>(LHS))
223 if (Constant *RC = dyn_cast<Constant>(RHS))
224 return Folder.CreateShl(LC, RC);
225 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
227 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
228 if (Constant *LC = dyn_cast<Constant>(LHS))
229 if (Constant *RC = dyn_cast<Constant>(RHS))
230 return Folder.CreateLShr(LC, RC);
231 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
233 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
234 if (Constant *LC = dyn_cast<Constant>(LHS))
235 if (Constant *RC = dyn_cast<Constant>(RHS))
236 return Folder.CreateAShr(LC, RC);
237 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
239 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
240 if (Constant *LC = dyn_cast<Constant>(LHS))
241 if (Constant *RC = dyn_cast<Constant>(RHS))
242 return Folder.CreateAnd(LC, RC);
243 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
245 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
246 if (Constant *LC = dyn_cast<Constant>(LHS))
247 if (Constant *RC = dyn_cast<Constant>(RHS))
248 return Folder.CreateOr(LC, RC);
249 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
251 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
252 if (Constant *LC = dyn_cast<Constant>(LHS))
253 if (Constant *RC = dyn_cast<Constant>(RHS))
254 return Folder.CreateXor(LC, RC);
255 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
258 Value *CreateBinOp(Instruction::BinaryOps Opc,
259 Value *LHS, Value *RHS, const char *Name = "") {
260 if (Constant *LC = dyn_cast<Constant>(LHS))
261 if (Constant *RC = dyn_cast<Constant>(RHS))
262 return Folder.CreateBinOp(Opc, LC, RC);
263 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
266 Value *CreateNeg(Value *V, const char *Name = "") {
267 if (Constant *VC = dyn_cast<Constant>(V))
268 return Folder.CreateNeg(VC);
269 return Insert(BinaryOperator::CreateNeg(V), Name);
271 Value *CreateNot(Value *V, const char *Name = "") {
272 if (Constant *VC = dyn_cast<Constant>(V))
273 return Folder.CreateNot(VC);
274 return Insert(BinaryOperator::CreateNot(V), Name);
277 //===--------------------------------------------------------------------===//
278 // Instruction creation methods: Memory Instructions
279 //===--------------------------------------------------------------------===//
281 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
282 const char *Name = "") {
283 return Insert(new MallocInst(Ty, ArraySize), Name);
285 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
286 const char *Name = "") {
287 return Insert(new AllocaInst(Ty, ArraySize), Name);
289 FreeInst *CreateFree(Value *Ptr) {
290 return Insert(new FreeInst(Ptr));
292 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
293 return Insert(new LoadInst(Ptr), Name);
295 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
296 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
298 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
299 return Insert(new StoreInst(Val, Ptr, isVolatile));
301 template<typename InputIterator>
302 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
303 const char *Name = "") {
304 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
305 // Every index must be constant.
307 for (i = IdxBegin; i < IdxEnd; ++i) {
308 if (!dyn_cast<Constant>(*i))
312 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
314 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
316 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
317 if (Constant *PC = dyn_cast<Constant>(Ptr))
318 if (Constant *IC = dyn_cast<Constant>(Idx))
319 return Folder.CreateGetElementPtr(PC, &IC, 1);
320 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
322 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
323 llvm::Value *Idxs[] = {
324 ConstantInt::get(llvm::Type::Int32Ty, 0),
325 ConstantInt::get(llvm::Type::Int32Ty, Idx)
328 if (Constant *PC = dyn_cast<Constant>(Ptr))
329 return Folder.CreateGetElementPtr(PC, Idxs, 2);
331 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
333 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
334 Constant *StrConstant = ConstantArray::get(Str, true);
335 GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
337 GlobalValue::InternalLinkage,
340 BB->getParent()->getParent(),
345 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
346 Value *gv = CreateGlobalString(Str, Name);
347 Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
348 Value *Args[] = { zero, zero };
349 return CreateGEP(gv, Args, Args+2, Name);
351 //===--------------------------------------------------------------------===//
352 // Instruction creation methods: Cast/Conversion Operators
353 //===--------------------------------------------------------------------===//
355 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
356 return CreateCast(Instruction::Trunc, V, DestTy, Name);
358 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
359 return CreateCast(Instruction::ZExt, V, DestTy, Name);
361 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
362 return CreateCast(Instruction::SExt, V, DestTy, Name);
364 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
365 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
367 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
368 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
370 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
371 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
373 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
374 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
376 Value *CreateFPTrunc(Value *V, const Type *DestTy,
377 const char *Name = "") {
378 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
380 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
381 return CreateCast(Instruction::FPExt, V, DestTy, Name);
383 Value *CreatePtrToInt(Value *V, const Type *DestTy,
384 const char *Name = "") {
385 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
387 Value *CreateIntToPtr(Value *V, const Type *DestTy,
388 const char *Name = "") {
389 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
391 Value *CreateBitCast(Value *V, const Type *DestTy,
392 const char *Name = "") {
393 return CreateCast(Instruction::BitCast, V, DestTy, Name);
396 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
397 const char *Name = "") {
398 if (V->getType() == DestTy)
400 if (Constant *VC = dyn_cast<Constant>(V))
401 return Folder.CreateCast(Op, VC, DestTy);
402 return Insert(CastInst::Create(Op, V, DestTy), Name);
404 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
405 const char *Name = "") {
406 if (V->getType() == DestTy)
408 if (Constant *VC = dyn_cast<Constant>(V))
409 return Folder.CreateIntCast(VC, DestTy, isSigned);
410 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
413 //===--------------------------------------------------------------------===//
414 // Instruction creation methods: Compare Instructions
415 //===--------------------------------------------------------------------===//
417 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
418 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
420 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
421 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
423 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
424 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
426 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
427 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
429 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
430 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
432 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
433 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
435 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
436 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
438 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
439 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
441 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
442 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
444 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
445 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
448 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
449 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
451 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
452 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
454 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
455 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
457 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
458 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
460 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
461 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
463 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
464 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
466 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
467 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
469 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
470 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
472 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
473 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
475 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
476 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
478 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
479 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
481 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
482 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
484 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
485 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
487 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
488 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
491 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
492 const char *Name = "") {
493 if (Constant *LC = dyn_cast<Constant>(LHS))
494 if (Constant *RC = dyn_cast<Constant>(RHS))
495 return Folder.CreateICmp(P, LC, RC);
496 return Insert(new ICmpInst(P, LHS, RHS), Name);
498 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
499 const char *Name = "") {
500 if (Constant *LC = dyn_cast<Constant>(LHS))
501 if (Constant *RC = dyn_cast<Constant>(RHS))
502 return Folder.CreateFCmp(P, LC, RC);
503 return Insert(new FCmpInst(P, LHS, RHS), Name);
506 Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
507 const char *Name = "") {
508 if (Constant *LC = dyn_cast<Constant>(LHS))
509 if (Constant *RC = dyn_cast<Constant>(RHS))
510 return Folder.CreateVICmp(P, LC, RC);
511 return Insert(new VICmpInst(P, LHS, RHS), Name);
513 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
514 const char *Name = "") {
515 if (Constant *LC = dyn_cast<Constant>(LHS))
516 if (Constant *RC = dyn_cast<Constant>(RHS))
517 return Folder.CreateVFCmp(P, LC, RC);
518 return Insert(new VFCmpInst(P, LHS, RHS), Name);
521 //===--------------------------------------------------------------------===//
522 // Instruction creation methods: Other Instructions
523 //===--------------------------------------------------------------------===//
525 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
526 return Insert(PHINode::Create(Ty), Name);
529 CallInst *CreateCall(Value *Callee, const char *Name = "") {
530 return Insert(CallInst::Create(Callee), Name);
532 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
533 return Insert(CallInst::Create(Callee, Arg), Name);
535 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
536 const char *Name = "") {
537 Value *Args[] = { Arg1, Arg2 };
538 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
540 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
541 const char *Name = "") {
542 Value *Args[] = { Arg1, Arg2, Arg3 };
543 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
545 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
546 Value *Arg4, const char *Name = "") {
547 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
548 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
551 template<typename InputIterator>
552 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
553 InputIterator ArgEnd, const char *Name = "") {
554 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
557 Value *CreateSelect(Value *C, Value *True, Value *False,
558 const char *Name = "") {
559 if (Constant *CC = dyn_cast<Constant>(C))
560 if (Constant *TC = dyn_cast<Constant>(True))
561 if (Constant *FC = dyn_cast<Constant>(False))
562 return Folder.CreateSelect(CC, TC, FC);
563 return Insert(SelectInst::Create(C, True, False), Name);
566 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
567 return Insert(new VAArgInst(List, Ty), Name);
570 Value *CreateExtractElement(Value *Vec, Value *Idx,
571 const char *Name = "") {
572 if (Constant *VC = dyn_cast<Constant>(Vec))
573 if (Constant *IC = dyn_cast<Constant>(Idx))
574 return Folder.CreateExtractElement(VC, IC);
575 return Insert(new ExtractElementInst(Vec, Idx), Name);
578 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
579 const char *Name = "") {
580 if (Constant *VC = dyn_cast<Constant>(Vec))
581 if (Constant *NC = dyn_cast<Constant>(NewElt))
582 if (Constant *IC = dyn_cast<Constant>(Idx))
583 return Folder.CreateInsertElement(VC, NC, IC);
584 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
587 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
588 const char *Name = "") {
589 if (Constant *V1C = dyn_cast<Constant>(V1))
590 if (Constant *V2C = dyn_cast<Constant>(V2))
591 if (Constant *MC = dyn_cast<Constant>(Mask))
592 return Folder.CreateShuffleVector(V1C, V2C, MC);
593 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
596 Value *CreateExtractValue(Value *Agg, unsigned Idx,
597 const char *Name = "") {
598 if (Constant *AggC = dyn_cast<Constant>(Agg))
599 return Folder.CreateExtractValue(AggC, &Idx, 1);
600 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
603 template<typename InputIterator>
604 Value *CreateExtractValue(Value *Agg,
605 InputIterator IdxBegin,
606 InputIterator IdxEnd,
607 const char *Name = "") {
608 if (Constant *AggC = dyn_cast<Constant>(Agg))
609 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
610 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
613 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
614 const char *Name = "") {
615 if (Constant *AggC = dyn_cast<Constant>(Agg))
616 if (Constant *ValC = dyn_cast<Constant>(Val))
617 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
618 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
621 template<typename InputIterator>
622 Value *CreateInsertValue(Value *Agg, Value *Val,
623 InputIterator IdxBegin,
624 InputIterator IdxEnd,
625 const char *Name = "") {
626 if (Constant *AggC = dyn_cast<Constant>(Agg))
627 if (Constant *ValC = dyn_cast<Constant>(Val))
628 return Folder.CreateInsertValue(AggC, ValC,
629 IdxBegin, IdxEnd - IdxBegin);
630 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
633 //===--------------------------------------------------------------------===//
634 // Utility creation methods
635 //===--------------------------------------------------------------------===//
637 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
638 Value *CreateIsNull(Value *Arg, const char *Name = "") {
639 return CreateICmpEQ(Arg, llvm::Constant::getNullValue(Arg->getType()),
643 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
644 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
645 return CreateICmpNE(Arg, llvm::Constant::getNullValue(Arg->getType()),