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,
303 InputIterator IdxEnd, const char *Name = "") {
305 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
306 // Every index must be constant.
308 for (i = IdxBegin; i < IdxEnd; ++i) {
309 if (!dyn_cast<Constant>(*i))
313 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
315 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
317 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
318 if (Constant *PC = dyn_cast<Constant>(Ptr))
319 if (Constant *IC = dyn_cast<Constant>(Idx))
320 return Folder.CreateGetElementPtr(PC, &IC, 1);
321 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
323 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
324 llvm::Value *Idxs[] = {
325 ConstantInt::get(llvm::Type::Int32Ty, 0),
326 ConstantInt::get(llvm::Type::Int32Ty, Idx)
329 if (Constant *PC = dyn_cast<Constant>(Ptr))
330 return Folder.CreateGetElementPtr(PC, Idxs, 2);
332 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
334 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
335 Constant *StrConstant = ConstantArray::get(Str, true);
336 GlobalVariable *gv = new llvm::GlobalVariable(StrConstant->getType(),
338 GlobalValue::InternalLinkage,
341 BB->getParent()->getParent(),
346 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
347 Value *gv = CreateGlobalString(Str, Name);
348 Value *zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0);
349 Value *Args[] = { zero, zero };
350 return CreateGEP(gv, Args, Args+2, Name);
352 //===--------------------------------------------------------------------===//
353 // Instruction creation methods: Cast/Conversion Operators
354 //===--------------------------------------------------------------------===//
356 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
357 return CreateCast(Instruction::Trunc, V, DestTy, Name);
359 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
360 return CreateCast(Instruction::ZExt, V, DestTy, Name);
362 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
363 return CreateCast(Instruction::SExt, V, DestTy, Name);
365 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
366 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
368 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
369 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
371 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
372 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
374 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
375 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
377 Value *CreateFPTrunc(Value *V, const Type *DestTy,
378 const char *Name = "") {
379 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
381 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
382 return CreateCast(Instruction::FPExt, V, DestTy, Name);
384 Value *CreatePtrToInt(Value *V, const Type *DestTy,
385 const char *Name = "") {
386 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
388 Value *CreateIntToPtr(Value *V, const Type *DestTy,
389 const char *Name = "") {
390 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
392 Value *CreateBitCast(Value *V, const Type *DestTy,
393 const char *Name = "") {
394 return CreateCast(Instruction::BitCast, V, DestTy, Name);
397 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
398 const char *Name = "") {
399 if (V->getType() == DestTy)
401 if (Constant *VC = dyn_cast<Constant>(V))
402 return Folder.CreateCast(Op, VC, DestTy);
403 return Insert(CastInst::Create(Op, V, DestTy), Name);
405 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
406 const char *Name = "") {
407 if (V->getType() == DestTy)
409 if (Constant *VC = dyn_cast<Constant>(V))
410 return Folder.CreateIntCast(VC, DestTy, isSigned);
411 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
414 //===--------------------------------------------------------------------===//
415 // Instruction creation methods: Compare Instructions
416 //===--------------------------------------------------------------------===//
418 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
419 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
421 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
422 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
424 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
425 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
427 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
428 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
430 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
431 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
433 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
434 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
436 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
437 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
439 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
440 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
442 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
443 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
445 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
446 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
449 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
450 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
452 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
453 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
455 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
456 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
458 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
459 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
461 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
462 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
464 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
465 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
467 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
468 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
470 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
471 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
473 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
474 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
476 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
477 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
479 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
480 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
482 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
483 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
485 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
486 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
488 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
489 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
492 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
493 const char *Name = "") {
494 if (Constant *LC = dyn_cast<Constant>(LHS))
495 if (Constant *RC = dyn_cast<Constant>(RHS))
496 return Folder.CreateCompare(P, LC, RC);
497 return Insert(new ICmpInst(P, LHS, RHS), Name);
499 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
500 const char *Name = "") {
501 if (Constant *LC = dyn_cast<Constant>(LHS))
502 if (Constant *RC = dyn_cast<Constant>(RHS))
503 return Folder.CreateCompare(P, LC, RC);
504 return Insert(new FCmpInst(P, LHS, RHS), Name);
507 Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
508 const char *Name = "") {
509 if (Constant *LC = dyn_cast<Constant>(LHS))
510 if (Constant *RC = dyn_cast<Constant>(RHS))
511 return Folder.CreateCompare(P, LC, RC);
512 return Insert(new VICmpInst(P, LHS, RHS), Name);
514 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
515 const char *Name = "") {
516 if (Constant *LC = dyn_cast<Constant>(LHS))
517 if (Constant *RC = dyn_cast<Constant>(RHS))
518 return Folder.CreateCompare(P, LC, RC);
519 return Insert(new VFCmpInst(P, LHS, RHS), Name);
522 //===--------------------------------------------------------------------===//
523 // Instruction creation methods: Other Instructions
524 //===--------------------------------------------------------------------===//
526 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
527 return Insert(PHINode::Create(Ty), Name);
530 CallInst *CreateCall(Value *Callee, const char *Name = "") {
531 return Insert(CallInst::Create(Callee), Name);
533 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
534 return Insert(CallInst::Create(Callee, Arg), Name);
536 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
537 const char *Name = "") {
538 Value *Args[] = { Arg1, Arg2 };
539 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
541 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
542 const char *Name = "") {
543 Value *Args[] = { Arg1, Arg2, Arg3 };
544 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
546 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
547 Value *Arg4, const char *Name = "") {
548 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
549 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
552 template<typename InputIterator>
553 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
554 InputIterator ArgEnd, const char *Name = "") {
555 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
558 Value *CreateSelect(Value *C, Value *True, Value *False,
559 const char *Name = "") {
560 if (Constant *CC = dyn_cast<Constant>(C))
561 if (Constant *TC = dyn_cast<Constant>(True))
562 if (Constant *FC = dyn_cast<Constant>(False))
563 return Folder.CreateSelect(CC, TC, FC);
564 return Insert(SelectInst::Create(C, True, False), Name);
567 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
568 return Insert(new VAArgInst(List, Ty), Name);
571 Value *CreateExtractElement(Value *Vec, Value *Idx,
572 const char *Name = "") {
573 if (Constant *VC = dyn_cast<Constant>(Vec))
574 if (Constant *IC = dyn_cast<Constant>(Idx))
575 return Folder.CreateExtractElement(VC, IC);
576 return Insert(new ExtractElementInst(Vec, Idx), Name);
579 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
580 const char *Name = "") {
581 if (Constant *VC = dyn_cast<Constant>(Vec))
582 if (Constant *NC = dyn_cast<Constant>(NewElt))
583 if (Constant *IC = dyn_cast<Constant>(Idx))
584 return Folder.CreateInsertElement(VC, NC, IC);
585 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
588 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
589 const char *Name = "") {
590 if (Constant *V1C = dyn_cast<Constant>(V1))
591 if (Constant *V2C = dyn_cast<Constant>(V2))
592 if (Constant *MC = dyn_cast<Constant>(Mask))
593 return Folder.CreateShuffleVector(V1C, V2C, MC);
594 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
597 Value *CreateExtractValue(Value *Agg, unsigned Idx,
598 const char *Name = "") {
599 if (Constant *AggC = dyn_cast<Constant>(Agg))
600 return Folder.CreateExtractValue(AggC, &Idx, 1);
601 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
604 template<typename InputIterator>
605 Value *CreateExtractValue(Value *Agg,
606 InputIterator IdxBegin,
607 InputIterator IdxEnd,
608 const char *Name = "") {
609 if (Constant *AggC = dyn_cast<Constant>(Agg))
610 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
611 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
614 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
615 const char *Name = "") {
616 if (Constant *AggC = dyn_cast<Constant>(Agg))
617 if (Constant *ValC = dyn_cast<Constant>(Val))
618 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
619 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
622 template<typename InputIterator>
623 Value *CreateInsertValue(Value *Agg, Value *Val,
624 InputIterator IdxBegin,
625 InputIterator IdxEnd,
626 const char *Name = "") {
627 if (Constant *AggC = dyn_cast<Constant>(Agg))
628 if (Constant *ValC = dyn_cast<Constant>(Val))
629 return Folder.CreateInsertValue(AggC, ValC,
630 IdxBegin, IdxEnd - IdxBegin);
631 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);