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(TransferAttributes(InvokeInst::Create(Callee,
158 NormalDest, UnwindDest,
163 UnwindInst *CreateUnwind() {
164 return Insert(new UnwindInst());
167 UnreachableInst *CreateUnreachable() {
168 return Insert(new UnreachableInst());
171 //===--------------------------------------------------------------------===//
172 // Instruction creation methods: Binary Operators
173 //===--------------------------------------------------------------------===//
175 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
176 if (Constant *LC = dyn_cast<Constant>(LHS))
177 if (Constant *RC = dyn_cast<Constant>(RHS))
178 return Folder.CreateAdd(LC, RC);
179 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
181 Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
182 if (Constant *LC = dyn_cast<Constant>(LHS))
183 if (Constant *RC = dyn_cast<Constant>(RHS))
184 return Folder.CreateFAdd(LC, RC);
185 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
187 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
188 if (Constant *LC = dyn_cast<Constant>(LHS))
189 if (Constant *RC = dyn_cast<Constant>(RHS))
190 return Folder.CreateSub(LC, RC);
191 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
193 Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") {
194 if (Constant *LC = dyn_cast<Constant>(LHS))
195 if (Constant *RC = dyn_cast<Constant>(RHS))
196 return Folder.CreateFSub(LC, RC);
197 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
199 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
200 if (Constant *LC = dyn_cast<Constant>(LHS))
201 if (Constant *RC = dyn_cast<Constant>(RHS))
202 return Folder.CreateMul(LC, RC);
203 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
205 Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") {
206 if (Constant *LC = dyn_cast<Constant>(LHS))
207 if (Constant *RC = dyn_cast<Constant>(RHS))
208 return Folder.CreateFMul(LC, RC);
209 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
211 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
212 if (Constant *LC = dyn_cast<Constant>(LHS))
213 if (Constant *RC = dyn_cast<Constant>(RHS))
214 return Folder.CreateUDiv(LC, RC);
215 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
217 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
218 if (Constant *LC = dyn_cast<Constant>(LHS))
219 if (Constant *RC = dyn_cast<Constant>(RHS))
220 return Folder.CreateSDiv(LC, RC);
221 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
223 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
224 if (Constant *LC = dyn_cast<Constant>(LHS))
225 if (Constant *RC = dyn_cast<Constant>(RHS))
226 return Folder.CreateFDiv(LC, RC);
227 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
229 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
230 if (Constant *LC = dyn_cast<Constant>(LHS))
231 if (Constant *RC = dyn_cast<Constant>(RHS))
232 return Folder.CreateURem(LC, RC);
233 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
235 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
236 if (Constant *LC = dyn_cast<Constant>(LHS))
237 if (Constant *RC = dyn_cast<Constant>(RHS))
238 return Folder.CreateSRem(LC, RC);
239 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
241 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
242 if (Constant *LC = dyn_cast<Constant>(LHS))
243 if (Constant *RC = dyn_cast<Constant>(RHS))
244 return Folder.CreateFRem(LC, RC);
245 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
247 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
248 if (Constant *LC = dyn_cast<Constant>(LHS))
249 if (Constant *RC = dyn_cast<Constant>(RHS))
250 return Folder.CreateShl(LC, RC);
251 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
253 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
254 if (Constant *LC = dyn_cast<Constant>(LHS))
255 if (Constant *RC = dyn_cast<Constant>(RHS))
256 return Folder.CreateLShr(LC, RC);
257 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
259 Value *CreateAShr(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.CreateAShr(LC, RC);
263 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
265 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
266 if (Constant *LC = dyn_cast<Constant>(LHS))
267 if (Constant *RC = dyn_cast<Constant>(RHS))
268 return Folder.CreateAnd(LC, RC);
269 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
271 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
272 if (Constant *LC = dyn_cast<Constant>(LHS))
273 if (Constant *RC = dyn_cast<Constant>(RHS))
274 return Folder.CreateOr(LC, RC);
275 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
277 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
278 if (Constant *LC = dyn_cast<Constant>(LHS))
279 if (Constant *RC = dyn_cast<Constant>(RHS))
280 return Folder.CreateXor(LC, RC);
281 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
284 Value *CreateBinOp(Instruction::BinaryOps Opc,
285 Value *LHS, Value *RHS, const char *Name = "") {
286 if (Constant *LC = dyn_cast<Constant>(LHS))
287 if (Constant *RC = dyn_cast<Constant>(RHS))
288 return Folder.CreateBinOp(Opc, LC, RC);
289 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
292 Value *CreateNeg(Value *V, const char *Name = "") {
293 if (Constant *VC = dyn_cast<Constant>(V))
294 return Folder.CreateNeg(VC);
295 return Insert(BinaryOperator::CreateNeg(V), Name);
297 Value *CreateFNeg(Value *V, const char *Name = "") {
298 if (Constant *VC = dyn_cast<Constant>(V))
299 return Folder.CreateFNeg(VC);
300 return Insert(BinaryOperator::CreateFNeg(V), Name);
302 Value *CreateNot(Value *V, const char *Name = "") {
303 if (Constant *VC = dyn_cast<Constant>(V))
304 return Folder.CreateNot(VC);
305 return Insert(BinaryOperator::CreateNot(V), Name);
308 //===--------------------------------------------------------------------===//
309 // Instruction creation methods: Memory Instructions
310 //===--------------------------------------------------------------------===//
312 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
313 const char *Name = "") {
314 return Insert(new MallocInst(Ty, ArraySize), Name);
316 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
317 const char *Name = "") {
318 return Insert(new AllocaInst(Ty, ArraySize), Name);
320 FreeInst *CreateFree(Value *Ptr) {
321 return Insert(new FreeInst(Ptr));
323 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
324 return Insert(new LoadInst(Ptr), Name);
326 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
327 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
329 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
330 return Insert(new StoreInst(Val, Ptr, isVolatile));
332 template<typename InputIterator>
333 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
334 const char *Name = "") {
335 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
336 // Every index must be constant.
338 for (i = IdxBegin; i < IdxEnd; ++i) {
339 if (!dyn_cast<Constant>(*i))
343 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
345 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
347 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
348 if (Constant *PC = dyn_cast<Constant>(Ptr))
349 if (Constant *IC = dyn_cast<Constant>(Idx))
350 return Folder.CreateGetElementPtr(PC, &IC, 1);
351 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
353 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
354 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
356 if (Constant *PC = dyn_cast<Constant>(Ptr))
357 return Folder.CreateGetElementPtr(PC, &Idx, 1);
359 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
361 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
362 const char *Name = "") {
364 ConstantInt::get(Type::Int32Ty, Idx0),
365 ConstantInt::get(Type::Int32Ty, Idx1)
368 if (Constant *PC = dyn_cast<Constant>(Ptr))
369 return Folder.CreateGetElementPtr(PC, Idxs, 2);
371 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
373 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
374 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
376 if (Constant *PC = dyn_cast<Constant>(Ptr))
377 return Folder.CreateGetElementPtr(PC, &Idx, 1);
379 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
381 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
382 const char *Name = "") {
384 ConstantInt::get(Type::Int64Ty, Idx0),
385 ConstantInt::get(Type::Int64Ty, Idx1)
388 if (Constant *PC = dyn_cast<Constant>(Ptr))
389 return Folder.CreateGetElementPtr(PC, Idxs, 2);
391 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
393 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
394 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
396 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
397 Constant *StrConstant = ConstantArray::get(Str, true);
398 GlobalVariable *gv = new GlobalVariable(StrConstant->getType(),
400 GlobalValue::InternalLinkage,
403 BB->getParent()->getParent(),
408 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
409 Value *gv = CreateGlobalString(Str, Name);
410 Value *zero = ConstantInt::get(Type::Int32Ty, 0);
411 Value *Args[] = { zero, zero };
412 return CreateGEP(gv, Args, Args+2, Name);
414 //===--------------------------------------------------------------------===//
415 // Instruction creation methods: Cast/Conversion Operators
416 //===--------------------------------------------------------------------===//
418 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
419 return CreateCast(Instruction::Trunc, V, DestTy, Name);
421 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
422 return CreateCast(Instruction::ZExt, V, DestTy, Name);
424 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
425 return CreateCast(Instruction::SExt, V, DestTy, Name);
427 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
428 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
430 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
431 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
433 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
434 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
436 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
437 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
439 Value *CreateFPTrunc(Value *V, const Type *DestTy,
440 const char *Name = "") {
441 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
443 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
444 return CreateCast(Instruction::FPExt, V, DestTy, Name);
446 Value *CreatePtrToInt(Value *V, const Type *DestTy,
447 const char *Name = "") {
448 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
450 Value *CreateIntToPtr(Value *V, const Type *DestTy,
451 const char *Name = "") {
452 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
454 Value *CreateBitCast(Value *V, const Type *DestTy,
455 const char *Name = "") {
456 return CreateCast(Instruction::BitCast, V, DestTy, Name);
459 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
460 const char *Name = "") {
461 if (V->getType() == DestTy)
463 if (Constant *VC = dyn_cast<Constant>(V))
464 return Folder.CreateCast(Op, VC, DestTy);
465 return Insert(CastInst::Create(Op, V, DestTy), Name);
467 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
468 const char *Name = "") {
469 if (V->getType() == DestTy)
471 if (Constant *VC = dyn_cast<Constant>(V))
472 return Folder.CreateIntCast(VC, DestTy, isSigned);
473 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
476 //===--------------------------------------------------------------------===//
477 // Instruction creation methods: Compare Instructions
478 //===--------------------------------------------------------------------===//
480 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
481 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
483 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
484 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
486 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
487 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
489 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
490 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
492 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
493 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
495 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
496 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
498 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
499 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
501 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
502 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
504 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
505 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
507 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
508 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
511 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
512 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
514 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
515 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
517 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
518 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
520 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
521 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
523 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
524 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
526 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
527 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
529 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
530 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
532 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
533 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
535 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
536 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
538 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
539 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
541 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
542 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
544 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
545 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
547 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
548 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
550 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
551 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
554 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
555 const char *Name = "") {
556 if (Constant *LC = dyn_cast<Constant>(LHS))
557 if (Constant *RC = dyn_cast<Constant>(RHS))
558 return Folder.CreateICmp(P, LC, RC);
559 return Insert(new ICmpInst(P, LHS, RHS), Name);
561 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
562 const char *Name = "") {
563 if (Constant *LC = dyn_cast<Constant>(LHS))
564 if (Constant *RC = dyn_cast<Constant>(RHS))
565 return Folder.CreateFCmp(P, LC, RC);
566 return Insert(new FCmpInst(P, LHS, RHS), Name);
569 Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
570 const char *Name = "") {
571 if (Constant *LC = dyn_cast<Constant>(LHS))
572 if (Constant *RC = dyn_cast<Constant>(RHS))
573 return Folder.CreateVICmp(P, LC, RC);
574 return Insert(new VICmpInst(P, LHS, RHS), Name);
576 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
577 const char *Name = "") {
578 if (Constant *LC = dyn_cast<Constant>(LHS))
579 if (Constant *RC = dyn_cast<Constant>(RHS))
580 return Folder.CreateVFCmp(P, LC, RC);
581 return Insert(new VFCmpInst(P, LHS, RHS), Name);
584 //===--------------------------------------------------------------------===//
585 // Instruction creation methods: Other Instructions
586 //===--------------------------------------------------------------------===//
588 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
589 return Insert(PHINode::Create(Ty), Name);
592 CallInst *TransferAttributes(CallInst *CI, const Value* Callee) const {
593 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(Callee))
594 Callee = GA->getAliasedGlobal();
596 if (const Function *F = dyn_cast<Function>(Callee)) {
597 CI->setCallingConv(F->getCallingConv());
598 CI->setAttributes(F->getAttributes());
604 CallInst *CreateCall(Value *Callee, const char *Name = "") {
605 return Insert(TransferAttributes(CallInst::Create(Callee), Callee), Name);
607 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
608 return Insert(TransferAttributes(CallInst::Create(Callee, Arg),
611 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
612 const char *Name = "") {
613 Value *Args[] = { Arg1, Arg2 };
614 return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+2),
617 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
618 const char *Name = "") {
619 Value *Args[] = { Arg1, Arg2, Arg3 };
620 return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+3),
623 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
624 Value *Arg4, const char *Name = "") {
625 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
626 return Insert(TransferAttributes(CallInst::Create(Callee, Args, Args+4),
630 template<typename InputIterator>
631 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
632 InputIterator ArgEnd, const char *Name = "") {
633 return Insert(TransferAttributes(CallInst::Create(Callee, ArgBegin, ArgEnd),
637 Value *CreateSelect(Value *C, Value *True, Value *False,
638 const char *Name = "") {
639 if (Constant *CC = dyn_cast<Constant>(C))
640 if (Constant *TC = dyn_cast<Constant>(True))
641 if (Constant *FC = dyn_cast<Constant>(False))
642 return Folder.CreateSelect(CC, TC, FC);
643 return Insert(SelectInst::Create(C, True, False), Name);
646 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
647 return Insert(new VAArgInst(List, Ty), Name);
650 Value *CreateExtractElement(Value *Vec, Value *Idx,
651 const char *Name = "") {
652 if (Constant *VC = dyn_cast<Constant>(Vec))
653 if (Constant *IC = dyn_cast<Constant>(Idx))
654 return Folder.CreateExtractElement(VC, IC);
655 return Insert(new ExtractElementInst(Vec, Idx), Name);
658 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
659 const char *Name = "") {
660 if (Constant *VC = dyn_cast<Constant>(Vec))
661 if (Constant *NC = dyn_cast<Constant>(NewElt))
662 if (Constant *IC = dyn_cast<Constant>(Idx))
663 return Folder.CreateInsertElement(VC, NC, IC);
664 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
667 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
668 const char *Name = "") {
669 if (Constant *V1C = dyn_cast<Constant>(V1))
670 if (Constant *V2C = dyn_cast<Constant>(V2))
671 if (Constant *MC = dyn_cast<Constant>(Mask))
672 return Folder.CreateShuffleVector(V1C, V2C, MC);
673 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
676 Value *CreateExtractValue(Value *Agg, unsigned Idx,
677 const char *Name = "") {
678 if (Constant *AggC = dyn_cast<Constant>(Agg))
679 return Folder.CreateExtractValue(AggC, &Idx, 1);
680 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
683 template<typename InputIterator>
684 Value *CreateExtractValue(Value *Agg,
685 InputIterator IdxBegin,
686 InputIterator IdxEnd,
687 const char *Name = "") {
688 if (Constant *AggC = dyn_cast<Constant>(Agg))
689 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
690 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
693 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
694 const char *Name = "") {
695 if (Constant *AggC = dyn_cast<Constant>(Agg))
696 if (Constant *ValC = dyn_cast<Constant>(Val))
697 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
698 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
701 template<typename InputIterator>
702 Value *CreateInsertValue(Value *Agg, Value *Val,
703 InputIterator IdxBegin,
704 InputIterator IdxEnd,
705 const char *Name = "") {
706 if (Constant *AggC = dyn_cast<Constant>(Agg))
707 if (Constant *ValC = dyn_cast<Constant>(Val))
708 return Folder.CreateInsertValue(AggC, ValC,
709 IdxBegin, IdxEnd - IdxBegin);
710 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
713 //===--------------------------------------------------------------------===//
714 // Utility creation methods
715 //===--------------------------------------------------------------------===//
717 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
718 Value *CreateIsNull(Value *Arg, const char *Name = "") {
719 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
723 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
724 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
725 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
729 /// CreatePtrDiff - Return the i64 difference between two pointer values,
730 /// dividing out the size of the pointed-to objects. This is intended to
731 /// implement C-style pointer subtraction.
732 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
733 assert(LHS->getType() == RHS->getType() &&
734 "Pointer subtraction operand types must match!");
735 const PointerType *ArgType = cast<PointerType>(LHS->getType());
736 Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
737 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
738 Value *Difference = CreateSub(LHS_int, RHS_int);
739 return CreateSDiv(Difference,
740 ConstantExpr::getSizeOf(ArgType->getElementType()),