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/LLVMContext.h"
24 #include "llvm/Support/ConstantFolder.h"
28 /// IRBuilder - This provides a uniform API for creating instructions and
29 /// inserting them into a basic block: either at the end of a BasicBlock, or
30 /// at a specific iterator location in a block.
32 /// Note that the builder does not expose the full generality of LLVM
33 /// instructions. For example, it cannot be used to create instructions with
34 /// arbitrary names (specifically, names with nul characters in them) - It only
35 /// supports nul-terminated C strings. For fully generic names, use
36 /// I->setName(). For access to extra instruction properties, use the mutators
37 /// (e.g. setVolatile) on the instructions after they have been created.
38 /// The first template argument handles whether or not to preserve names in the
39 /// final instruction output. This defaults to on. The second template argument
40 /// specifies a class to use for creating constants. This defaults to creating
41 /// minimally folded constants.
42 template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
44 BasicBlock::iterator InsertPt;
48 IRBuilder(LLVMContext &C, const T& F) :
49 Context(C), Folder(F) { ClearInsertionPoint(); }
51 IRBuilder(LLVMContext &C) : Context(C), Folder(C) { ClearInsertionPoint(); }
53 explicit IRBuilder(BasicBlock *TheBB, const T& F)
54 : Context(*TheBB->getParent()->getContext()), Folder(F) {
55 SetInsertPoint(TheBB);
58 explicit IRBuilder(BasicBlock *TheBB)
59 : Context(*TheBB->getParent()->getContext()), Folder(Context) {
60 SetInsertPoint(TheBB);
63 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
64 : Context(*TheBB->getParent()->getContext()), Folder(F) {
65 SetInsertPoint(TheBB, IP);
68 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
69 : Context(*TheBB->getParent()->getContext()), Folder(Context) {
70 SetInsertPoint(TheBB, IP);
73 /// getFolder - Get the constant folder being used.
74 const T& getFolder() { return Folder; }
76 /// isNamePreserving - Return true if this builder is configured to actually
77 /// add the requested names to IR created through it.
78 bool isNamePreserving() const { return preserveNames; }
80 //===--------------------------------------------------------------------===//
81 // Builder configuration methods
82 //===--------------------------------------------------------------------===//
84 /// ClearInsertionPoint - Clear the insertion point: created instructions will
85 /// not be inserted into a block.
86 void ClearInsertionPoint() {
90 BasicBlock *GetInsertBlock() const { return BB; }
92 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
94 /// SetInsertPoint - This specifies that created instructions should be
95 /// appended to the end of the specified block.
96 void SetInsertPoint(BasicBlock *TheBB) {
101 /// SetInsertPoint - This specifies that created instructions should be
102 /// inserted at the specified point.
103 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
108 /// Insert - Insert and return the specified instruction.
109 template<typename InstTy>
110 InstTy *Insert(InstTy *I, const char *Name = "") const {
111 InsertHelper(I, Name);
115 /// InsertHelper - Insert the specified instruction at the specified insertion
116 /// point. This is split out of Insert so that it isn't duplicated for every
117 /// template instantiation.
118 void InsertHelper(Instruction *I, const char *Name) const {
119 if (BB) BB->getInstList().insert(InsertPt, I);
120 if (preserveNames && Name[0])
124 //===--------------------------------------------------------------------===//
125 // Instruction creation methods: Terminators
126 //===--------------------------------------------------------------------===//
128 /// CreateRetVoid - Create a 'ret void' instruction.
129 ReturnInst *CreateRetVoid() {
130 return Insert(ReturnInst::Create());
134 /// CreateRet - Create a 'ret <val>' instruction.
136 ReturnInst *CreateRet(Value *V) {
137 return Insert(ReturnInst::Create(V));
140 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
141 /// with one Value from the retVals array each, that build a aggregate
142 /// return value one value at a time, and a ret instruction to return
143 /// the resulting aggregate value. This is a convenience function for
144 /// code that uses aggregate return values as a vehicle for having
145 /// multiple return values.
147 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
148 const Type *RetType = BB->getParent()->getReturnType();
149 Value *V = Context.getUndef(RetType);
150 for (unsigned i = 0; i != N; ++i)
151 V = CreateInsertValue(V, retVals[i], i, "mrv");
152 return Insert(ReturnInst::Create(V));
155 /// CreateBr - Create an unconditional 'br label X' instruction.
156 BranchInst *CreateBr(BasicBlock *Dest) {
157 return Insert(BranchInst::Create(Dest));
160 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
162 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
163 return Insert(BranchInst::Create(True, False, Cond));
166 /// CreateSwitch - Create a switch instruction with the specified value,
167 /// default dest, and with a hint for the number of cases that will be added
168 /// (for efficient allocation).
169 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
170 return Insert(SwitchInst::Create(V, Dest, NumCases));
173 /// CreateInvoke - Create an invoke instruction.
174 template<typename InputIterator>
175 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
176 BasicBlock *UnwindDest, InputIterator ArgBegin,
177 InputIterator ArgEnd, const char *Name = "") {
178 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
179 ArgBegin, ArgEnd), Name);
182 UnwindInst *CreateUnwind() {
183 return Insert(new UnwindInst());
186 UnreachableInst *CreateUnreachable() {
187 return Insert(new UnreachableInst());
190 //===--------------------------------------------------------------------===//
191 // Instruction creation methods: Binary Operators
192 //===--------------------------------------------------------------------===//
194 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
195 if (Constant *LC = dyn_cast<Constant>(LHS))
196 if (Constant *RC = dyn_cast<Constant>(RHS))
197 return Folder.CreateAdd(LC, RC);
198 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
200 Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
201 if (Constant *LC = dyn_cast<Constant>(LHS))
202 if (Constant *RC = dyn_cast<Constant>(RHS))
203 return Folder.CreateFAdd(LC, RC);
204 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
206 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
207 if (Constant *LC = dyn_cast<Constant>(LHS))
208 if (Constant *RC = dyn_cast<Constant>(RHS))
209 return Folder.CreateSub(LC, RC);
210 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
212 Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") {
213 if (Constant *LC = dyn_cast<Constant>(LHS))
214 if (Constant *RC = dyn_cast<Constant>(RHS))
215 return Folder.CreateFSub(LC, RC);
216 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
218 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
219 if (Constant *LC = dyn_cast<Constant>(LHS))
220 if (Constant *RC = dyn_cast<Constant>(RHS))
221 return Folder.CreateMul(LC, RC);
222 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
224 Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") {
225 if (Constant *LC = dyn_cast<Constant>(LHS))
226 if (Constant *RC = dyn_cast<Constant>(RHS))
227 return Folder.CreateFMul(LC, RC);
228 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
230 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
231 if (Constant *LC = dyn_cast<Constant>(LHS))
232 if (Constant *RC = dyn_cast<Constant>(RHS))
233 return Folder.CreateUDiv(LC, RC);
234 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
236 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
237 if (Constant *LC = dyn_cast<Constant>(LHS))
238 if (Constant *RC = dyn_cast<Constant>(RHS))
239 return Folder.CreateSDiv(LC, RC);
240 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
242 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
243 if (Constant *LC = dyn_cast<Constant>(LHS))
244 if (Constant *RC = dyn_cast<Constant>(RHS))
245 return Folder.CreateFDiv(LC, RC);
246 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
248 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
249 if (Constant *LC = dyn_cast<Constant>(LHS))
250 if (Constant *RC = dyn_cast<Constant>(RHS))
251 return Folder.CreateURem(LC, RC);
252 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
254 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
255 if (Constant *LC = dyn_cast<Constant>(LHS))
256 if (Constant *RC = dyn_cast<Constant>(RHS))
257 return Folder.CreateSRem(LC, RC);
258 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
260 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
261 if (Constant *LC = dyn_cast<Constant>(LHS))
262 if (Constant *RC = dyn_cast<Constant>(RHS))
263 return Folder.CreateFRem(LC, RC);
264 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
266 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
267 if (Constant *LC = dyn_cast<Constant>(LHS))
268 if (Constant *RC = dyn_cast<Constant>(RHS))
269 return Folder.CreateShl(LC, RC);
270 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
272 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
273 if (Constant *LC = dyn_cast<Constant>(LHS))
274 if (Constant *RC = dyn_cast<Constant>(RHS))
275 return Folder.CreateLShr(LC, RC);
276 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
278 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
279 if (Constant *LC = dyn_cast<Constant>(LHS))
280 if (Constant *RC = dyn_cast<Constant>(RHS))
281 return Folder.CreateAShr(LC, RC);
282 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
284 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
285 if (Constant *LC = dyn_cast<Constant>(LHS))
286 if (Constant *RC = dyn_cast<Constant>(RHS))
287 return Folder.CreateAnd(LC, RC);
288 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
290 Value *CreateOr(Value *LHS, Value *RHS, const char *Name = "") {
291 if (Constant *LC = dyn_cast<Constant>(LHS))
292 if (Constant *RC = dyn_cast<Constant>(RHS))
293 return Folder.CreateOr(LC, RC);
294 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
296 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
297 if (Constant *LC = dyn_cast<Constant>(LHS))
298 if (Constant *RC = dyn_cast<Constant>(RHS))
299 return Folder.CreateXor(LC, RC);
300 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
303 Value *CreateBinOp(Instruction::BinaryOps Opc,
304 Value *LHS, Value *RHS, const char *Name = "") {
305 if (Constant *LC = dyn_cast<Constant>(LHS))
306 if (Constant *RC = dyn_cast<Constant>(RHS))
307 return Folder.CreateBinOp(Opc, LC, RC);
308 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
311 Value *CreateNeg(Value *V, const char *Name = "") {
312 if (Constant *VC = dyn_cast<Constant>(V))
313 return Folder.CreateNeg(VC);
314 return Insert(BinaryOperator::CreateNeg(Context, V), Name);
316 Value *CreateFNeg(Value *V, const char *Name = "") {
317 if (Constant *VC = dyn_cast<Constant>(V))
318 return Folder.CreateFNeg(VC);
319 return Insert(BinaryOperator::CreateFNeg(Context, V), Name);
321 Value *CreateNot(Value *V, const char *Name = "") {
322 if (Constant *VC = dyn_cast<Constant>(V))
323 return Folder.CreateNot(VC);
324 return Insert(BinaryOperator::CreateNot(Context, V), Name);
327 //===--------------------------------------------------------------------===//
328 // Instruction creation methods: Memory Instructions
329 //===--------------------------------------------------------------------===//
331 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
332 const char *Name = "") {
333 return Insert(new MallocInst(Context, Ty, ArraySize), Name);
335 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
336 const char *Name = "") {
337 return Insert(new AllocaInst(Context, Ty, ArraySize), Name);
339 FreeInst *CreateFree(Value *Ptr) {
340 return Insert(new FreeInst(Ptr));
342 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
343 return Insert(new LoadInst(Ptr), Name);
345 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
346 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
348 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
349 return Insert(new StoreInst(Val, Ptr, isVolatile));
351 template<typename InputIterator>
352 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
353 const char *Name = "") {
354 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
355 // Every index must be constant.
357 for (i = IdxBegin; i < IdxEnd; ++i) {
358 if (!dyn_cast<Constant>(*i))
362 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
364 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
366 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
367 if (Constant *PC = dyn_cast<Constant>(Ptr))
368 if (Constant *IC = dyn_cast<Constant>(Idx))
369 return Folder.CreateGetElementPtr(PC, &IC, 1);
370 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
372 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
373 Value *Idx = Context.getConstantInt(Type::Int32Ty, Idx0);
375 if (Constant *PC = dyn_cast<Constant>(Ptr))
376 return Folder.CreateGetElementPtr(PC, &Idx, 1);
378 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
380 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
381 const char *Name = "") {
383 Context.getConstantInt(Type::Int32Ty, Idx0),
384 Context.getConstantInt(Type::Int32Ty, Idx1)
387 if (Constant *PC = dyn_cast<Constant>(Ptr))
388 return Folder.CreateGetElementPtr(PC, Idxs, 2);
390 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
392 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
393 Value *Idx = Context.getConstantInt(Type::Int64Ty, Idx0);
395 if (Constant *PC = dyn_cast<Constant>(Ptr))
396 return Folder.CreateGetElementPtr(PC, &Idx, 1);
398 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
400 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
401 const char *Name = "") {
403 Context.getConstantInt(Type::Int64Ty, Idx0),
404 Context.getConstantInt(Type::Int64Ty, Idx1)
407 if (Constant *PC = dyn_cast<Constant>(Ptr))
408 return Folder.CreateGetElementPtr(PC, Idxs, 2);
410 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
412 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
413 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
415 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
416 Constant *StrConstant = Context.getConstantArray(Str, true);
417 Module &M = *BB->getParent()->getParent();
418 GlobalVariable *gv = new GlobalVariable(M,
419 StrConstant->getType(),
421 GlobalValue::InternalLinkage,
429 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
430 Value *gv = CreateGlobalString(Str, Name);
431 Value *zero = Context.getConstantInt(Type::Int32Ty, 0);
432 Value *Args[] = { zero, zero };
433 return CreateGEP(gv, Args, Args+2, Name);
435 //===--------------------------------------------------------------------===//
436 // Instruction creation methods: Cast/Conversion Operators
437 //===--------------------------------------------------------------------===//
439 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
440 return CreateCast(Instruction::Trunc, V, DestTy, Name);
442 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
443 return CreateCast(Instruction::ZExt, V, DestTy, Name);
445 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
446 return CreateCast(Instruction::SExt, V, DestTy, Name);
448 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
449 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
451 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
452 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
454 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
455 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
457 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
458 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
460 Value *CreateFPTrunc(Value *V, const Type *DestTy,
461 const char *Name = "") {
462 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
464 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
465 return CreateCast(Instruction::FPExt, V, DestTy, Name);
467 Value *CreatePtrToInt(Value *V, const Type *DestTy,
468 const char *Name = "") {
469 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
471 Value *CreateIntToPtr(Value *V, const Type *DestTy,
472 const char *Name = "") {
473 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
475 Value *CreateBitCast(Value *V, const Type *DestTy,
476 const char *Name = "") {
477 return CreateCast(Instruction::BitCast, V, DestTy, Name);
480 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
481 const char *Name = "") {
482 if (V->getType() == DestTy)
484 if (Constant *VC = dyn_cast<Constant>(V))
485 return Folder.CreateCast(Op, VC, DestTy);
486 return Insert(CastInst::Create(Op, V, DestTy), Name);
488 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
489 const char *Name = "") {
490 if (V->getType() == DestTy)
492 if (Constant *VC = dyn_cast<Constant>(V))
493 return Folder.CreateIntCast(VC, DestTy, isSigned);
494 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
497 //===--------------------------------------------------------------------===//
498 // Instruction creation methods: Compare Instructions
499 //===--------------------------------------------------------------------===//
501 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
502 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
504 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
505 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
507 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
508 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
510 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
511 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
513 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
514 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
516 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
517 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
519 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
520 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
522 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
523 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
525 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
526 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
528 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
529 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
532 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
533 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
535 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
536 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
538 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
539 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
541 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
542 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
544 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
545 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
547 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
548 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
550 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
551 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
553 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
554 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
556 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
557 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
559 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
560 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
562 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
563 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
565 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
566 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
568 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
569 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
571 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
572 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
575 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
576 const char *Name = "") {
577 if (Constant *LC = dyn_cast<Constant>(LHS))
578 if (Constant *RC = dyn_cast<Constant>(RHS))
579 return Folder.CreateICmp(P, LC, RC);
580 return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
582 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
583 const char *Name = "") {
584 if (Constant *LC = dyn_cast<Constant>(LHS))
585 if (Constant *RC = dyn_cast<Constant>(RHS))
586 return Folder.CreateFCmp(P, LC, RC);
587 return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
590 //===--------------------------------------------------------------------===//
591 // Instruction creation methods: Other Instructions
592 //===--------------------------------------------------------------------===//
594 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
595 return Insert(PHINode::Create(Ty), Name);
598 CallInst *CreateCall(Value *Callee, const char *Name = "") {
599 return Insert(CallInst::Create(Callee), Name);
601 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
602 return Insert(CallInst::Create(Callee, Arg), Name);
604 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
605 const char *Name = "") {
606 Value *Args[] = { Arg1, Arg2 };
607 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
609 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
610 const char *Name = "") {
611 Value *Args[] = { Arg1, Arg2, Arg3 };
612 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
614 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
615 Value *Arg4, const char *Name = "") {
616 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
617 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
620 template<typename InputIterator>
621 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
622 InputIterator ArgEnd, const char *Name = "") {
623 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
626 Value *CreateSelect(Value *C, Value *True, Value *False,
627 const char *Name = "") {
628 if (Constant *CC = dyn_cast<Constant>(C))
629 if (Constant *TC = dyn_cast<Constant>(True))
630 if (Constant *FC = dyn_cast<Constant>(False))
631 return Folder.CreateSelect(CC, TC, FC);
632 return Insert(SelectInst::Create(C, True, False), Name);
635 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
636 return Insert(new VAArgInst(List, Ty), Name);
639 Value *CreateExtractElement(Value *Vec, Value *Idx,
640 const char *Name = "") {
641 if (Constant *VC = dyn_cast<Constant>(Vec))
642 if (Constant *IC = dyn_cast<Constant>(Idx))
643 return Folder.CreateExtractElement(VC, IC);
644 return Insert(new ExtractElementInst(Vec, Idx), Name);
647 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
648 const char *Name = "") {
649 if (Constant *VC = dyn_cast<Constant>(Vec))
650 if (Constant *NC = dyn_cast<Constant>(NewElt))
651 if (Constant *IC = dyn_cast<Constant>(Idx))
652 return Folder.CreateInsertElement(VC, NC, IC);
653 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
656 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
657 const char *Name = "") {
658 if (Constant *V1C = dyn_cast<Constant>(V1))
659 if (Constant *V2C = dyn_cast<Constant>(V2))
660 if (Constant *MC = dyn_cast<Constant>(Mask))
661 return Folder.CreateShuffleVector(V1C, V2C, MC);
662 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
665 Value *CreateExtractValue(Value *Agg, unsigned Idx,
666 const char *Name = "") {
667 if (Constant *AggC = dyn_cast<Constant>(Agg))
668 return Folder.CreateExtractValue(AggC, &Idx, 1);
669 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
672 template<typename InputIterator>
673 Value *CreateExtractValue(Value *Agg,
674 InputIterator IdxBegin,
675 InputIterator IdxEnd,
676 const char *Name = "") {
677 if (Constant *AggC = dyn_cast<Constant>(Agg))
678 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
679 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
682 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
683 const char *Name = "") {
684 if (Constant *AggC = dyn_cast<Constant>(Agg))
685 if (Constant *ValC = dyn_cast<Constant>(Val))
686 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
687 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
690 template<typename InputIterator>
691 Value *CreateInsertValue(Value *Agg, Value *Val,
692 InputIterator IdxBegin,
693 InputIterator IdxEnd,
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,
698 IdxBegin, IdxEnd - IdxBegin);
699 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
702 //===--------------------------------------------------------------------===//
703 // Utility creation methods
704 //===--------------------------------------------------------------------===//
706 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
707 Value *CreateIsNull(Value *Arg, const char *Name = "") {
708 return CreateICmpEQ(Arg, Context.getNullValue(Arg->getType()),
712 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
713 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
714 return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()),
718 /// CreatePtrDiff - Return the i64 difference between two pointer values,
719 /// dividing out the size of the pointed-to objects. This is intended to
720 /// implement C-style pointer subtraction.
721 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
722 assert(LHS->getType() == RHS->getType() &&
723 "Pointer subtraction operand types must match!");
724 const PointerType *ArgType = cast<PointerType>(LHS->getType());
725 Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
726 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
727 Value *Difference = CreateSub(LHS_int, RHS_int);
728 return CreateSDiv(Difference,
729 Context.getConstantExprSizeOf(ArgType->getElementType()),