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/Constants.h"
19 #include "llvm/Instructions.h"
20 #include "llvm/GlobalAlias.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 /// isNamePreserving - Return true if this builder is configured to actually
56 /// add the requested names to IR created through it.
57 bool isNamePreserving() const { return preserveNames; }
59 //===--------------------------------------------------------------------===//
60 // Builder configuration methods
61 //===--------------------------------------------------------------------===//
63 /// ClearInsertionPoint - Clear the insertion point: created instructions will
64 /// not be inserted into a block.
65 void ClearInsertionPoint() {
69 BasicBlock *GetInsertBlock() const { return BB; }
71 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
73 /// SetInsertPoint - This specifies that created instructions should be
74 /// appended to the end of the specified block.
75 void SetInsertPoint(BasicBlock *TheBB) {
80 /// SetInsertPoint - This specifies that created instructions should be
81 /// inserted at the specified point.
82 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
87 /// Insert - Insert and return the specified instruction.
88 template<typename InstTy>
89 InstTy *Insert(InstTy *I, const char *Name = "") const {
90 InsertHelper(I, Name);
94 /// InsertHelper - Insert the specified instruction at the specified insertion
95 /// point. This is split out of Insert so that it isn't duplicated for every
96 /// template instantiation.
97 void InsertHelper(Instruction *I, const char *Name) const {
98 if (BB) BB->getInstList().insert(InsertPt, I);
99 if (preserveNames && Name[0])
103 //===--------------------------------------------------------------------===//
104 // Instruction creation methods: Terminators
105 //===--------------------------------------------------------------------===//
107 /// CreateRetVoid - Create a 'ret void' instruction.
108 ReturnInst *CreateRetVoid() {
109 return Insert(ReturnInst::Create());
113 /// CreateRet - Create a 'ret <val>' instruction.
115 ReturnInst *CreateRet(Value *V) {
116 return Insert(ReturnInst::Create(V));
119 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
120 /// with one Value from the retVals array each, that build a aggregate
121 /// return value one value at a time, and a ret instruction to return
122 /// the resulting aggregate value. This is a convenience function for
123 /// code that uses aggregate return values as a vehicle for having
124 /// multiple return values.
126 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
127 const Type *RetType = BB->getParent()->getReturnType();
128 Value *V = UndefValue::get(RetType);
129 for (unsigned i = 0; i != N; ++i)
130 V = CreateInsertValue(V, retVals[i], i, "mrv");
131 return Insert(ReturnInst::Create(V));
134 /// CreateBr - Create an unconditional 'br label X' instruction.
135 BranchInst *CreateBr(BasicBlock *Dest) {
136 return Insert(BranchInst::Create(Dest));
139 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
141 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
142 return Insert(BranchInst::Create(True, False, Cond));
145 /// CreateSwitch - Create a switch instruction with the specified value,
146 /// default dest, and with a hint for the number of cases that will be added
147 /// (for efficient allocation).
148 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
149 return Insert(SwitchInst::Create(V, Dest, NumCases));
152 /// CreateInvoke - Create an invoke instruction.
153 template<typename InputIterator>
154 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
155 BasicBlock *UnwindDest, InputIterator ArgBegin,
156 InputIterator ArgEnd, const char *Name = "") {
157 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
158 ArgBegin, ArgEnd), Name);
161 UnwindInst *CreateUnwind() {
162 return Insert(new UnwindInst());
165 UnreachableInst *CreateUnreachable() {
166 return Insert(new UnreachableInst());
169 //===--------------------------------------------------------------------===//
170 // Instruction creation methods: Binary Operators
171 //===--------------------------------------------------------------------===//
173 Value *CreateAdd(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.CreateAdd(LC, RC);
177 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
179 Value *CreateFAdd(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.CreateFAdd(LC, RC);
183 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
185 Value *CreateSub(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.CreateSub(LC, RC);
189 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
191 Value *CreateFSub(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.CreateFSub(LC, RC);
195 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
197 Value *CreateMul(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.CreateMul(LC, RC);
201 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
203 Value *CreateFMul(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.CreateFMul(LC, RC);
207 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
209 Value *CreateUDiv(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.CreateUDiv(LC, RC);
213 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
215 Value *CreateSDiv(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.CreateSDiv(LC, RC);
219 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
221 Value *CreateFDiv(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.CreateFDiv(LC, RC);
225 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
227 Value *CreateURem(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.CreateURem(LC, RC);
231 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
233 Value *CreateSRem(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.CreateSRem(LC, RC);
237 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
239 Value *CreateFRem(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.CreateFRem(LC, RC);
243 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
245 Value *CreateShl(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.CreateShl(LC, RC);
249 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
251 Value *CreateLShr(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.CreateLShr(LC, RC);
255 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
257 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
258 if (Constant *LC = dyn_cast<Constant>(LHS))
259 if (Constant *RC = dyn_cast<Constant>(RHS))
260 return Folder.CreateAShr(LC, RC);
261 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
263 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
264 if (Constant *LC = dyn_cast<Constant>(LHS))
265 if (Constant *RC = dyn_cast<Constant>(RHS))
266 return Folder.CreateAnd(LC, RC);
267 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
269 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
270 if (Constant *LC = dyn_cast<Constant>(LHS))
271 if (Constant *RC = dyn_cast<Constant>(RHS))
272 return Folder.CreateOr(LC, RC);
273 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
275 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
276 if (Constant *LC = dyn_cast<Constant>(LHS))
277 if (Constant *RC = dyn_cast<Constant>(RHS))
278 return Folder.CreateXor(LC, RC);
279 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
282 Value *CreateBinOp(Instruction::BinaryOps Opc,
283 Value *LHS, Value *RHS, const char *Name = "") {
284 if (Constant *LC = dyn_cast<Constant>(LHS))
285 if (Constant *RC = dyn_cast<Constant>(RHS))
286 return Folder.CreateBinOp(Opc, LC, RC);
287 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
290 Value *CreateNeg(Value *V, const char *Name = "") {
291 if (Constant *VC = dyn_cast<Constant>(V))
292 return Folder.CreateNeg(VC);
293 return Insert(BinaryOperator::CreateNeg(V), Name);
295 Value *CreateFNeg(Value *V, const char *Name = "") {
296 if (Constant *VC = dyn_cast<Constant>(V))
297 return Folder.CreateFNeg(VC);
298 return Insert(BinaryOperator::CreateFNeg(V), Name);
300 Value *CreateNot(Value *V, const char *Name = "") {
301 if (Constant *VC = dyn_cast<Constant>(V))
302 return Folder.CreateNot(VC);
303 return Insert(BinaryOperator::CreateNot(V), Name);
306 //===--------------------------------------------------------------------===//
307 // Instruction creation methods: Memory Instructions
308 //===--------------------------------------------------------------------===//
310 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
311 const char *Name = "") {
312 return Insert(new MallocInst(Ty, ArraySize), Name);
314 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
315 const char *Name = "") {
316 return Insert(new AllocaInst(Ty, ArraySize), Name);
318 FreeInst *CreateFree(Value *Ptr) {
319 return Insert(new FreeInst(Ptr));
321 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
322 return Insert(new LoadInst(Ptr), Name);
324 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
325 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
327 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
328 return Insert(new StoreInst(Val, Ptr, isVolatile));
330 template<typename InputIterator>
331 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
332 const char *Name = "") {
333 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
334 // Every index must be constant.
336 for (i = IdxBegin; i < IdxEnd; ++i) {
337 if (!dyn_cast<Constant>(*i))
341 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
343 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
345 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
346 if (Constant *PC = dyn_cast<Constant>(Ptr))
347 if (Constant *IC = dyn_cast<Constant>(Idx))
348 return Folder.CreateGetElementPtr(PC, &IC, 1);
349 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
351 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
352 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
354 if (Constant *PC = dyn_cast<Constant>(Ptr))
355 return Folder.CreateGetElementPtr(PC, &Idx, 1);
357 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
359 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
360 const char *Name = "") {
362 ConstantInt::get(Type::Int32Ty, Idx0),
363 ConstantInt::get(Type::Int32Ty, Idx1)
366 if (Constant *PC = dyn_cast<Constant>(Ptr))
367 return Folder.CreateGetElementPtr(PC, Idxs, 2);
369 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
371 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
372 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
374 if (Constant *PC = dyn_cast<Constant>(Ptr))
375 return Folder.CreateGetElementPtr(PC, &Idx, 1);
377 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
379 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
380 const char *Name = "") {
382 ConstantInt::get(Type::Int64Ty, Idx0),
383 ConstantInt::get(Type::Int64Ty, Idx1)
386 if (Constant *PC = dyn_cast<Constant>(Ptr))
387 return Folder.CreateGetElementPtr(PC, Idxs, 2);
389 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
391 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
392 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
394 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
395 Constant *StrConstant = ConstantArray::get(Str, true);
396 GlobalVariable *gv = new GlobalVariable(StrConstant->getType(),
398 GlobalValue::InternalLinkage,
401 BB->getParent()->getParent(),
406 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
407 Value *gv = CreateGlobalString(Str, Name);
408 Value *zero = ConstantInt::get(Type::Int32Ty, 0);
409 Value *Args[] = { zero, zero };
410 return CreateGEP(gv, Args, Args+2, Name);
412 //===--------------------------------------------------------------------===//
413 // Instruction creation methods: Cast/Conversion Operators
414 //===--------------------------------------------------------------------===//
416 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
417 return CreateCast(Instruction::Trunc, V, DestTy, Name);
419 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
420 return CreateCast(Instruction::ZExt, V, DestTy, Name);
422 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
423 return CreateCast(Instruction::SExt, V, DestTy, Name);
425 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
426 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
428 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
429 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
431 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
432 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
434 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
435 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
437 Value *CreateFPTrunc(Value *V, const Type *DestTy,
438 const char *Name = "") {
439 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
441 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
442 return CreateCast(Instruction::FPExt, V, DestTy, Name);
444 Value *CreatePtrToInt(Value *V, const Type *DestTy,
445 const char *Name = "") {
446 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
448 Value *CreateIntToPtr(Value *V, const Type *DestTy,
449 const char *Name = "") {
450 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
452 Value *CreateBitCast(Value *V, const Type *DestTy,
453 const char *Name = "") {
454 return CreateCast(Instruction::BitCast, V, DestTy, Name);
457 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
458 const char *Name = "") {
459 if (V->getType() == DestTy)
461 if (Constant *VC = dyn_cast<Constant>(V))
462 return Folder.CreateCast(Op, VC, DestTy);
463 return Insert(CastInst::Create(Op, V, DestTy), Name);
465 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
466 const char *Name = "") {
467 if (V->getType() == DestTy)
469 if (Constant *VC = dyn_cast<Constant>(V))
470 return Folder.CreateIntCast(VC, DestTy, isSigned);
471 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
474 //===--------------------------------------------------------------------===//
475 // Instruction creation methods: Compare Instructions
476 //===--------------------------------------------------------------------===//
478 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
479 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
481 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
482 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
484 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
485 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
487 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
488 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
490 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
491 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
493 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
494 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
496 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
497 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
499 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
500 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
502 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
503 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
505 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
506 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
509 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
510 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
512 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
513 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
515 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
516 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
518 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
519 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
521 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
522 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
524 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
525 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
527 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
528 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
530 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
531 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
533 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
534 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
536 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
537 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
539 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
540 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
542 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
543 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
545 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
546 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
548 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
549 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
552 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
553 const char *Name = "") {
554 if (Constant *LC = dyn_cast<Constant>(LHS))
555 if (Constant *RC = dyn_cast<Constant>(RHS))
556 return Folder.CreateICmp(P, LC, RC);
557 return Insert(new ICmpInst(P, LHS, RHS), Name);
559 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
560 const char *Name = "") {
561 if (Constant *LC = dyn_cast<Constant>(LHS))
562 if (Constant *RC = dyn_cast<Constant>(RHS))
563 return Folder.CreateFCmp(P, LC, RC);
564 return Insert(new FCmpInst(P, LHS, RHS), Name);
567 Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
568 const char *Name = "") {
569 if (Constant *LC = dyn_cast<Constant>(LHS))
570 if (Constant *RC = dyn_cast<Constant>(RHS))
571 return Folder.CreateVICmp(P, LC, RC);
572 return Insert(new VICmpInst(P, LHS, RHS), Name);
574 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
575 const char *Name = "") {
576 if (Constant *LC = dyn_cast<Constant>(LHS))
577 if (Constant *RC = dyn_cast<Constant>(RHS))
578 return Folder.CreateVFCmp(P, LC, RC);
579 return Insert(new VFCmpInst(P, LHS, RHS), Name);
582 //===--------------------------------------------------------------------===//
583 // Instruction creation methods: Other Instructions
584 //===--------------------------------------------------------------------===//
586 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
587 return Insert(PHINode::Create(Ty), Name);
590 CallInst *TransferAttributes(CallInst *CI, const Value* Callee) const {
591 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Callee))
592 Callee = GA->getAliasedGlobal();
594 if (const Function *F = dyn_cast<Function>(Callee)) {
595 CI->setCallingConv(F->getCallingConv());
596 CI->setAttributes(F->getAttributes());
602 CallInst *CreateCall(Value *Callee, const char *Name = "") {
603 return Insert(TransferAttributes(CallInst::Create(Callee), Callee), Name);
605 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
606 return Insert(TransferAttributes(CallInst::Create(Callee, Arg),
609 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
610 const char *Name = "") {
611 Value *Args[] = { Arg1, Arg2 };
612 return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+2),
615 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
616 const char *Name = "") {
617 Value *Args[] = { Arg1, Arg2, Arg3 };
618 return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+3),
621 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
622 Value *Arg4, const char *Name = "") {
623 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
624 return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+4),
628 template<typename InputIterator>
629 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
630 InputIterator ArgEnd, const char *Name = "") {
631 return Insert(TransferAttributes(CallInst::Create(Callee, ArgBegin, ArgEnd),
635 Value *CreateSelect(Value *C, Value *True, Value *False,
636 const char *Name = "") {
637 if (Constant *CC = dyn_cast<Constant>(C))
638 if (Constant *TC = dyn_cast<Constant>(True))
639 if (Constant *FC = dyn_cast<Constant>(False))
640 return Folder.CreateSelect(CC, TC, FC);
641 return Insert(SelectInst::Create(C, True, False), Name);
644 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
645 return Insert(new VAArgInst(List, Ty), Name);
648 Value *CreateExtractElement(Value *Vec, Value *Idx,
649 const char *Name = "") {
650 if (Constant *VC = dyn_cast<Constant>(Vec))
651 if (Constant *IC = dyn_cast<Constant>(Idx))
652 return Folder.CreateExtractElement(VC, IC);
653 return Insert(new ExtractElementInst(Vec, Idx), Name);
656 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
657 const char *Name = "") {
658 if (Constant *VC = dyn_cast<Constant>(Vec))
659 if (Constant *NC = dyn_cast<Constant>(NewElt))
660 if (Constant *IC = dyn_cast<Constant>(Idx))
661 return Folder.CreateInsertElement(VC, NC, IC);
662 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
665 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
666 const char *Name = "") {
667 if (Constant *V1C = dyn_cast<Constant>(V1))
668 if (Constant *V2C = dyn_cast<Constant>(V2))
669 if (Constant *MC = dyn_cast<Constant>(Mask))
670 return Folder.CreateShuffleVector(V1C, V2C, MC);
671 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
674 Value *CreateExtractValue(Value *Agg, unsigned Idx,
675 const char *Name = "") {
676 if (Constant *AggC = dyn_cast<Constant>(Agg))
677 return Folder.CreateExtractValue(AggC, &Idx, 1);
678 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
681 template<typename InputIterator>
682 Value *CreateExtractValue(Value *Agg,
683 InputIterator IdxBegin,
684 InputIterator IdxEnd,
685 const char *Name = "") {
686 if (Constant *AggC = dyn_cast<Constant>(Agg))
687 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
688 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
691 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
692 const char *Name = "") {
693 if (Constant *AggC = dyn_cast<Constant>(Agg))
694 if (Constant *ValC = dyn_cast<Constant>(Val))
695 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
696 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
699 template<typename InputIterator>
700 Value *CreateInsertValue(Value *Agg, Value *Val,
701 InputIterator IdxBegin,
702 InputIterator IdxEnd,
703 const char *Name = "") {
704 if (Constant *AggC = dyn_cast<Constant>(Agg))
705 if (Constant *ValC = dyn_cast<Constant>(Val))
706 return Folder.CreateInsertValue(AggC, ValC,
707 IdxBegin, IdxEnd - IdxBegin);
708 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
711 //===--------------------------------------------------------------------===//
712 // Utility creation methods
713 //===--------------------------------------------------------------------===//
715 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
716 Value *CreateIsNull(Value *Arg, const char *Name = "") {
717 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
721 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
722 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
723 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
727 /// CreatePtrDiff - Return the i64 difference between two pointer values,
728 /// dividing out the size of the pointed-to objects. This is intended to
729 /// implement C-style pointer subtraction.
730 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
731 assert(LHS->getType() == RHS->getType() &&
732 "Pointer subtraction operand types must match!");
733 const PointerType *ArgType = cast<PointerType>(LHS->getType());
734 Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
735 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
736 Value *Difference = CreateSub(LHS_int, RHS_int);
737 return CreateSDiv(Difference,
738 ConstantExpr::getSizeOf(ArgType->getElementType()),