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 explicit IRBuilder(LLVMContext &C) : Context(C), Folder(C) {
52 ClearInsertionPoint();
55 explicit IRBuilder(BasicBlock *TheBB, const T& F)
56 : Context(TheBB->getContext()), Folder(F) {
57 SetInsertPoint(TheBB);
60 explicit IRBuilder(BasicBlock *TheBB)
61 : Context(TheBB->getContext()), Folder(Context) {
62 SetInsertPoint(TheBB);
65 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
66 : Context(TheBB->getContext()), Folder(F) {
67 SetInsertPoint(TheBB, IP);
70 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
71 : Context(TheBB->getContext()), Folder(Context) {
72 SetInsertPoint(TheBB, IP);
75 /// getFolder - Get the constant folder being used.
76 const T& getFolder() { return Folder; }
78 /// isNamePreserving - Return true if this builder is configured to actually
79 /// add the requested names to IR created through it.
80 bool isNamePreserving() const { return preserveNames; }
82 //===--------------------------------------------------------------------===//
83 // Builder configuration methods
84 //===--------------------------------------------------------------------===//
86 /// ClearInsertionPoint - Clear the insertion point: created instructions will
87 /// not be inserted into a block.
88 void ClearInsertionPoint() {
92 BasicBlock *GetInsertBlock() const { return BB; }
94 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
96 /// SetInsertPoint - This specifies that created instructions should be
97 /// appended to the end of the specified block.
98 void SetInsertPoint(BasicBlock *TheBB) {
100 InsertPt = BB->end();
103 /// SetInsertPoint - This specifies that created instructions should be
104 /// inserted at the specified point.
105 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
110 /// Insert - Insert and return the specified instruction.
111 template<typename InstTy>
112 InstTy *Insert(InstTy *I, const char *Name = "") const {
113 InsertHelper(I, Name);
117 /// InsertHelper - Insert the specified instruction at the specified insertion
118 /// point. This is split out of Insert so that it isn't duplicated for every
119 /// template instantiation.
120 void InsertHelper(Instruction *I, const char *Name) const {
121 if (BB) BB->getInstList().insert(InsertPt, I);
122 if (preserveNames && Name[0])
126 //===--------------------------------------------------------------------===//
127 // Instruction creation methods: Terminators
128 //===--------------------------------------------------------------------===//
130 /// CreateRetVoid - Create a 'ret void' instruction.
131 ReturnInst *CreateRetVoid() {
132 return Insert(ReturnInst::Create());
136 /// CreateRet - Create a 'ret <val>' instruction.
138 ReturnInst *CreateRet(Value *V) {
139 return Insert(ReturnInst::Create(V));
142 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
143 /// with one Value from the retVals array each, that build a aggregate
144 /// return value one value at a time, and a ret instruction to return
145 /// the resulting aggregate value. This is a convenience function for
146 /// code that uses aggregate return values as a vehicle for having
147 /// multiple return values.
149 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
150 const Type *RetType = BB->getParent()->getReturnType();
151 Value *V = UndefValue::get(RetType);
152 for (unsigned i = 0; i != N; ++i)
153 V = CreateInsertValue(V, retVals[i], i, "mrv");
154 return Insert(ReturnInst::Create(V));
157 /// CreateBr - Create an unconditional 'br label X' instruction.
158 BranchInst *CreateBr(BasicBlock *Dest) {
159 return Insert(BranchInst::Create(Dest));
162 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
164 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
165 return Insert(BranchInst::Create(True, False, Cond));
168 /// CreateSwitch - Create a switch instruction with the specified value,
169 /// default dest, and with a hint for the number of cases that will be added
170 /// (for efficient allocation).
171 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
172 return Insert(SwitchInst::Create(V, Dest, NumCases));
175 /// CreateInvoke - Create an invoke instruction.
176 template<typename InputIterator>
177 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
178 BasicBlock *UnwindDest, InputIterator ArgBegin,
179 InputIterator ArgEnd, const char *Name = "") {
180 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
181 ArgBegin, ArgEnd), Name);
184 UnwindInst *CreateUnwind() {
185 return Insert(new UnwindInst());
188 UnreachableInst *CreateUnreachable() {
189 return Insert(new UnreachableInst());
192 //===--------------------------------------------------------------------===//
193 // Instruction creation methods: Binary Operators
194 //===--------------------------------------------------------------------===//
196 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
197 if (Constant *LC = dyn_cast<Constant>(LHS))
198 if (Constant *RC = dyn_cast<Constant>(RHS))
199 return Folder.CreateAdd(LC, RC);
200 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
202 Value *CreateNSWAdd(Value *LHS, Value *RHS, const char *Name = "") {
203 if (Constant *LC = dyn_cast<Constant>(LHS))
204 if (Constant *RC = dyn_cast<Constant>(RHS))
205 return Folder.CreateNSWAdd(LC, RC);
206 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
208 Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
209 if (Constant *LC = dyn_cast<Constant>(LHS))
210 if (Constant *RC = dyn_cast<Constant>(RHS))
211 return Folder.CreateFAdd(LC, RC);
212 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
214 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
215 if (Constant *LC = dyn_cast<Constant>(LHS))
216 if (Constant *RC = dyn_cast<Constant>(RHS))
217 return Folder.CreateSub(LC, RC);
218 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
220 Value *CreateFSub(Value *LHS, Value *RHS, const char *Name = "") {
221 if (Constant *LC = dyn_cast<Constant>(LHS))
222 if (Constant *RC = dyn_cast<Constant>(RHS))
223 return Folder.CreateFSub(LC, RC);
224 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
226 Value *CreateMul(Value *LHS, Value *RHS, const char *Name = "") {
227 if (Constant *LC = dyn_cast<Constant>(LHS))
228 if (Constant *RC = dyn_cast<Constant>(RHS))
229 return Folder.CreateMul(LC, RC);
230 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
232 Value *CreateFMul(Value *LHS, Value *RHS, const char *Name = "") {
233 if (Constant *LC = dyn_cast<Constant>(LHS))
234 if (Constant *RC = dyn_cast<Constant>(RHS))
235 return Folder.CreateFMul(LC, RC);
236 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
238 Value *CreateUDiv(Value *LHS, Value *RHS, const char *Name = "") {
239 if (Constant *LC = dyn_cast<Constant>(LHS))
240 if (Constant *RC = dyn_cast<Constant>(RHS))
241 return Folder.CreateUDiv(LC, RC);
242 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
244 Value *CreateSDiv(Value *LHS, Value *RHS, const char *Name = "") {
245 if (Constant *LC = dyn_cast<Constant>(LHS))
246 if (Constant *RC = dyn_cast<Constant>(RHS))
247 return Folder.CreateSDiv(LC, RC);
248 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
250 Value *CreateExactSDiv(Value *LHS, Value *RHS, const char *Name = "") {
251 if (Constant *LC = dyn_cast<Constant>(LHS))
252 if (Constant *RC = dyn_cast<Constant>(RHS))
253 return Folder.CreateExactSDiv(LC, RC);
254 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
256 Value *CreateFDiv(Value *LHS, Value *RHS, const char *Name = "") {
257 if (Constant *LC = dyn_cast<Constant>(LHS))
258 if (Constant *RC = dyn_cast<Constant>(RHS))
259 return Folder.CreateFDiv(LC, RC);
260 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
262 Value *CreateURem(Value *LHS, Value *RHS, const char *Name = "") {
263 if (Constant *LC = dyn_cast<Constant>(LHS))
264 if (Constant *RC = dyn_cast<Constant>(RHS))
265 return Folder.CreateURem(LC, RC);
266 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
268 Value *CreateSRem(Value *LHS, Value *RHS, const char *Name = "") {
269 if (Constant *LC = dyn_cast<Constant>(LHS))
270 if (Constant *RC = dyn_cast<Constant>(RHS))
271 return Folder.CreateSRem(LC, RC);
272 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
274 Value *CreateFRem(Value *LHS, Value *RHS, const char *Name = "") {
275 if (Constant *LC = dyn_cast<Constant>(LHS))
276 if (Constant *RC = dyn_cast<Constant>(RHS))
277 return Folder.CreateFRem(LC, RC);
278 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
280 Value *CreateShl(Value *LHS, Value *RHS, const char *Name = "") {
281 if (Constant *LC = dyn_cast<Constant>(LHS))
282 if (Constant *RC = dyn_cast<Constant>(RHS))
283 return Folder.CreateShl(LC, RC);
284 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
286 Value *CreateLShr(Value *LHS, Value *RHS, const char *Name = "") {
287 if (Constant *LC = dyn_cast<Constant>(LHS))
288 if (Constant *RC = dyn_cast<Constant>(RHS))
289 return Folder.CreateLShr(LC, RC);
290 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
292 Value *CreateAShr(Value *LHS, Value *RHS, const char *Name = "") {
293 if (Constant *LC = dyn_cast<Constant>(LHS))
294 if (Constant *RC = dyn_cast<Constant>(RHS))
295 return Folder.CreateAShr(LC, RC);
296 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
298 Value *CreateAnd(Value *LHS, Value *RHS, const char *Name = "") {
299 if (Constant *LC = dyn_cast<Constant>(LHS))
300 if (Constant *RC = dyn_cast<Constant>(RHS))
301 return Folder.CreateAnd(LC, RC);
302 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
304 Value *CreateOr(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.CreateOr(LC, RC);
308 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
310 Value *CreateXor(Value *LHS, Value *RHS, const char *Name = "") {
311 if (Constant *LC = dyn_cast<Constant>(LHS))
312 if (Constant *RC = dyn_cast<Constant>(RHS))
313 return Folder.CreateXor(LC, RC);
314 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
317 Value *CreateBinOp(Instruction::BinaryOps Opc,
318 Value *LHS, Value *RHS, const char *Name = "") {
319 if (Constant *LC = dyn_cast<Constant>(LHS))
320 if (Constant *RC = dyn_cast<Constant>(RHS))
321 return Folder.CreateBinOp(Opc, LC, RC);
322 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
325 Value *CreateNeg(Value *V, const char *Name = "") {
326 if (Constant *VC = dyn_cast<Constant>(V))
327 return Folder.CreateNeg(VC);
328 return Insert(BinaryOperator::CreateNeg(V), Name);
330 Value *CreateFNeg(Value *V, const char *Name = "") {
331 if (Constant *VC = dyn_cast<Constant>(V))
332 return Folder.CreateFNeg(VC);
333 return Insert(BinaryOperator::CreateFNeg(V), Name);
335 Value *CreateNot(Value *V, const char *Name = "") {
336 if (Constant *VC = dyn_cast<Constant>(V))
337 return Folder.CreateNot(VC);
338 return Insert(BinaryOperator::CreateNot(V), Name);
341 //===--------------------------------------------------------------------===//
342 // Instruction creation methods: Memory Instructions
343 //===--------------------------------------------------------------------===//
345 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
346 const char *Name = "") {
347 return Insert(new MallocInst(Ty, ArraySize), Name);
349 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
350 const char *Name = "") {
351 return Insert(new AllocaInst(Ty, ArraySize), Name);
353 FreeInst *CreateFree(Value *Ptr) {
354 return Insert(new FreeInst(Ptr));
356 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
357 return Insert(new LoadInst(Ptr), Name);
359 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
360 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
362 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
363 return Insert(new StoreInst(Val, Ptr, isVolatile));
365 template<typename InputIterator>
366 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
367 const char *Name = "") {
368 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
369 // Every index must be constant.
371 for (i = IdxBegin; i < IdxEnd; ++i)
372 if (!isa<Constant>(*i))
375 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
377 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
379 template<typename InputIterator>
380 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
381 const char *Name = "") {
382 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
383 // Every index must be constant.
385 for (i = IdxBegin; i < IdxEnd; ++i)
386 if (!isa<Constant>(*i))
389 return Folder.CreateInBoundsGetElementPtr(PC,
393 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
396 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
397 if (Constant *PC = dyn_cast<Constant>(Ptr))
398 if (Constant *IC = dyn_cast<Constant>(Idx))
399 return Folder.CreateGetElementPtr(PC, &IC, 1);
400 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
402 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const char *Name = "") {
403 if (Constant *PC = dyn_cast<Constant>(Ptr))
404 if (Constant *IC = dyn_cast<Constant>(Idx))
405 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
406 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
408 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
409 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
411 if (Constant *PC = dyn_cast<Constant>(Ptr))
412 return Folder.CreateGetElementPtr(PC, &Idx, 1);
414 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
416 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
417 const char *Name = "") {
418 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
420 if (Constant *PC = dyn_cast<Constant>(Ptr))
421 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
423 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
425 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
426 const char *Name = "") {
428 ConstantInt::get(Type::Int32Ty, Idx0),
429 ConstantInt::get(Type::Int32Ty, Idx1)
432 if (Constant *PC = dyn_cast<Constant>(Ptr))
433 return Folder.CreateGetElementPtr(PC, Idxs, 2);
435 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
437 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
438 const char *Name = "") {
440 ConstantInt::get(Type::Int32Ty, Idx0),
441 ConstantInt::get(Type::Int32Ty, Idx1)
444 if (Constant *PC = dyn_cast<Constant>(Ptr))
445 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
447 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
449 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
450 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
452 if (Constant *PC = dyn_cast<Constant>(Ptr))
453 return Folder.CreateGetElementPtr(PC, &Idx, 1);
455 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
457 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
458 const char *Name = "") {
459 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
461 if (Constant *PC = dyn_cast<Constant>(Ptr))
462 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
464 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
466 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
467 const char *Name = "") {
469 ConstantInt::get(Type::Int64Ty, Idx0),
470 ConstantInt::get(Type::Int64Ty, Idx1)
473 if (Constant *PC = dyn_cast<Constant>(Ptr))
474 return Folder.CreateGetElementPtr(PC, Idxs, 2);
476 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
478 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
479 const char *Name = "") {
481 ConstantInt::get(Type::Int64Ty, Idx0),
482 ConstantInt::get(Type::Int64Ty, Idx1)
485 if (Constant *PC = dyn_cast<Constant>(Ptr))
486 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
488 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
490 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
491 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
493 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
494 Constant *StrConstant = ConstantArray::get(Str, true);
495 Module &M = *BB->getParent()->getParent();
496 GlobalVariable *gv = new GlobalVariable(M,
497 StrConstant->getType(),
499 GlobalValue::InternalLinkage,
507 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
508 Value *gv = CreateGlobalString(Str, Name);
509 Value *zero = ConstantInt::get(Type::Int32Ty, 0);
510 Value *Args[] = { zero, zero };
511 return CreateInBoundsGEP(gv, Args, Args+2, Name);
513 //===--------------------------------------------------------------------===//
514 // Instruction creation methods: Cast/Conversion Operators
515 //===--------------------------------------------------------------------===//
517 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
518 return CreateCast(Instruction::Trunc, V, DestTy, Name);
520 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
521 return CreateCast(Instruction::ZExt, V, DestTy, Name);
523 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
524 return CreateCast(Instruction::SExt, V, DestTy, Name);
526 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
527 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
529 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
530 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
532 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
533 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
535 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
536 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
538 Value *CreateFPTrunc(Value *V, const Type *DestTy,
539 const char *Name = "") {
540 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
542 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
543 return CreateCast(Instruction::FPExt, V, DestTy, Name);
545 Value *CreatePtrToInt(Value *V, const Type *DestTy,
546 const char *Name = "") {
547 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
549 Value *CreateIntToPtr(Value *V, const Type *DestTy,
550 const char *Name = "") {
551 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
553 Value *CreateBitCast(Value *V, const Type *DestTy,
554 const char *Name = "") {
555 return CreateCast(Instruction::BitCast, V, DestTy, Name);
558 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
559 const char *Name = "") {
560 if (V->getType() == DestTy)
562 if (Constant *VC = dyn_cast<Constant>(V))
563 return Folder.CreateCast(Op, VC, DestTy);
564 return Insert(CastInst::Create(Op, V, DestTy), Name);
566 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
567 const char *Name = "") {
568 if (V->getType() == DestTy)
570 if (Constant *VC = dyn_cast<Constant>(V))
571 return Folder.CreateIntCast(VC, DestTy, isSigned);
572 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
575 //===--------------------------------------------------------------------===//
576 // Instruction creation methods: Compare Instructions
577 //===--------------------------------------------------------------------===//
579 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
580 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
582 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
583 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
585 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
586 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
588 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
589 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
591 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
592 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
594 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
595 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
597 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
598 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
600 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
601 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
603 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
604 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
606 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
607 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
610 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
611 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
613 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
614 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
616 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
617 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
619 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
620 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
622 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
623 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
625 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
626 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
628 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
629 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
631 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
632 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
634 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
635 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
637 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
638 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
640 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
641 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
643 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
644 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
646 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
647 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
649 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
650 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
653 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
654 const char *Name = "") {
655 if (Constant *LC = dyn_cast<Constant>(LHS))
656 if (Constant *RC = dyn_cast<Constant>(RHS))
657 return Folder.CreateICmp(P, LC, RC);
658 return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
660 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
661 const char *Name = "") {
662 if (Constant *LC = dyn_cast<Constant>(LHS))
663 if (Constant *RC = dyn_cast<Constant>(RHS))
664 return Folder.CreateFCmp(P, LC, RC);
665 return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
668 //===--------------------------------------------------------------------===//
669 // Instruction creation methods: Other Instructions
670 //===--------------------------------------------------------------------===//
672 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
673 return Insert(PHINode::Create(Ty), Name);
676 CallInst *CreateCall(Value *Callee, const char *Name = "") {
677 return Insert(CallInst::Create(Callee), Name);
679 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
680 return Insert(CallInst::Create(Callee, Arg), Name);
682 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
683 const char *Name = "") {
684 Value *Args[] = { Arg1, Arg2 };
685 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
687 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
688 const char *Name = "") {
689 Value *Args[] = { Arg1, Arg2, Arg3 };
690 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
692 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
693 Value *Arg4, const char *Name = "") {
694 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
695 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
698 template<typename InputIterator>
699 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
700 InputIterator ArgEnd, const char *Name = "") {
701 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
704 Value *CreateSelect(Value *C, Value *True, Value *False,
705 const char *Name = "") {
706 if (Constant *CC = dyn_cast<Constant>(C))
707 if (Constant *TC = dyn_cast<Constant>(True))
708 if (Constant *FC = dyn_cast<Constant>(False))
709 return Folder.CreateSelect(CC, TC, FC);
710 return Insert(SelectInst::Create(C, True, False), Name);
713 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
714 return Insert(new VAArgInst(List, Ty), Name);
717 Value *CreateExtractElement(Value *Vec, Value *Idx,
718 const char *Name = "") {
719 if (Constant *VC = dyn_cast<Constant>(Vec))
720 if (Constant *IC = dyn_cast<Constant>(Idx))
721 return Folder.CreateExtractElement(VC, IC);
722 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
725 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
726 const char *Name = "") {
727 if (Constant *VC = dyn_cast<Constant>(Vec))
728 if (Constant *NC = dyn_cast<Constant>(NewElt))
729 if (Constant *IC = dyn_cast<Constant>(Idx))
730 return Folder.CreateInsertElement(VC, NC, IC);
731 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
734 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
735 const char *Name = "") {
736 if (Constant *V1C = dyn_cast<Constant>(V1))
737 if (Constant *V2C = dyn_cast<Constant>(V2))
738 if (Constant *MC = dyn_cast<Constant>(Mask))
739 return Folder.CreateShuffleVector(V1C, V2C, MC);
740 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
743 Value *CreateExtractValue(Value *Agg, unsigned Idx,
744 const char *Name = "") {
745 if (Constant *AggC = dyn_cast<Constant>(Agg))
746 return Folder.CreateExtractValue(AggC, &Idx, 1);
747 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
750 template<typename InputIterator>
751 Value *CreateExtractValue(Value *Agg,
752 InputIterator IdxBegin,
753 InputIterator IdxEnd,
754 const char *Name = "") {
755 if (Constant *AggC = dyn_cast<Constant>(Agg))
756 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
757 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
760 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
761 const char *Name = "") {
762 if (Constant *AggC = dyn_cast<Constant>(Agg))
763 if (Constant *ValC = dyn_cast<Constant>(Val))
764 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
765 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
768 template<typename InputIterator>
769 Value *CreateInsertValue(Value *Agg, Value *Val,
770 InputIterator IdxBegin,
771 InputIterator IdxEnd,
772 const char *Name = "") {
773 if (Constant *AggC = dyn_cast<Constant>(Agg))
774 if (Constant *ValC = dyn_cast<Constant>(Val))
775 return Folder.CreateInsertValue(AggC, ValC,
776 IdxBegin, IdxEnd - IdxBegin);
777 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
780 //===--------------------------------------------------------------------===//
781 // Utility creation methods
782 //===--------------------------------------------------------------------===//
784 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
785 Value *CreateIsNull(Value *Arg, const char *Name = "") {
786 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
790 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
791 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
792 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
796 /// CreatePtrDiff - Return the i64 difference between two pointer values,
797 /// dividing out the size of the pointed-to objects. This is intended to
798 /// implement C-style pointer subtraction. As such, the pointers must be
799 /// appropriately aligned for their element types and pointing into the
801 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
802 assert(LHS->getType() == RHS->getType() &&
803 "Pointer subtraction operand types must match!");
804 const PointerType *ArgType = cast<PointerType>(LHS->getType());
805 Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
806 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
807 Value *Difference = CreateSub(LHS_int, RHS_int);
808 return CreateExactSDiv(Difference,
809 ConstantExpr::getSizeOf(ArgType->getElementType()),