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(const T& F = T(), LLVMContext &C = getGlobalContext()) :
49 Folder(F), Context(C) { ClearInsertionPoint(); }
50 explicit IRBuilder(BasicBlock *TheBB, const T& F = T(),
51 LLVMContext &C = getGlobalContext()) :
52 Folder(F), Context(C) { SetInsertPoint(TheBB); }
53 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T(),
54 LLVMContext &C = getGlobalContext())
55 : Folder(F), Context(C) { SetInsertPoint(TheBB, IP); }
57 /// getFolder - Get the constant folder being used.
58 const T& getFolder() { return Folder; }
60 /// isNamePreserving - Return true if this builder is configured to actually
61 /// add the requested names to IR created through it.
62 bool isNamePreserving() const { return preserveNames; }
64 //===--------------------------------------------------------------------===//
65 // Builder configuration methods
66 //===--------------------------------------------------------------------===//
68 /// ClearInsertionPoint - Clear the insertion point: created instructions will
69 /// not be inserted into a block.
70 void ClearInsertionPoint() {
74 BasicBlock *GetInsertBlock() const { return BB; }
76 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
78 /// SetInsertPoint - This specifies that created instructions should be
79 /// appended to the end of the specified block.
80 void SetInsertPoint(BasicBlock *TheBB) {
85 /// SetInsertPoint - This specifies that created instructions should be
86 /// inserted at the specified point.
87 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
92 /// Insert - Insert and return the specified instruction.
93 template<typename InstTy>
94 InstTy *Insert(InstTy *I, const char *Name = "") const {
95 InsertHelper(I, Name);
99 /// InsertHelper - Insert the specified instruction at the specified insertion
100 /// point. This is split out of Insert so that it isn't duplicated for every
101 /// template instantiation.
102 void InsertHelper(Instruction *I, const char *Name) const {
103 if (BB) BB->getInstList().insert(InsertPt, I);
104 if (preserveNames && Name[0])
108 //===--------------------------------------------------------------------===//
109 // Instruction creation methods: Terminators
110 //===--------------------------------------------------------------------===//
112 /// CreateRetVoid - Create a 'ret void' instruction.
113 ReturnInst *CreateRetVoid() {
114 return Insert(ReturnInst::Create());
118 /// CreateRet - Create a 'ret <val>' instruction.
120 ReturnInst *CreateRet(Value *V) {
121 return Insert(ReturnInst::Create(V));
124 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
125 /// with one Value from the retVals array each, that build a aggregate
126 /// return value one value at a time, and a ret instruction to return
127 /// the resulting aggregate value. This is a convenience function for
128 /// code that uses aggregate return values as a vehicle for having
129 /// multiple return values.
131 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
132 const Type *RetType = BB->getParent()->getReturnType();
133 Value *V = Context.getUndef(RetType);
134 for (unsigned i = 0; i != N; ++i)
135 V = CreateInsertValue(V, retVals[i], i, "mrv");
136 return Insert(ReturnInst::Create(V));
139 /// CreateBr - Create an unconditional 'br label X' instruction.
140 BranchInst *CreateBr(BasicBlock *Dest) {
141 return Insert(BranchInst::Create(Dest));
144 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
146 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
147 return Insert(BranchInst::Create(True, False, Cond));
150 /// CreateSwitch - Create a switch instruction with the specified value,
151 /// default dest, and with a hint for the number of cases that will be added
152 /// (for efficient allocation).
153 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
154 return Insert(SwitchInst::Create(V, Dest, NumCases));
157 /// CreateInvoke - Create an invoke instruction.
158 template<typename InputIterator>
159 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
160 BasicBlock *UnwindDest, InputIterator ArgBegin,
161 InputIterator ArgEnd, const char *Name = "") {
162 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
163 ArgBegin, ArgEnd), Name);
166 UnwindInst *CreateUnwind() {
167 return Insert(new UnwindInst());
170 UnreachableInst *CreateUnreachable() {
171 return Insert(new UnreachableInst());
174 //===--------------------------------------------------------------------===//
175 // Instruction creation methods: Binary Operators
176 //===--------------------------------------------------------------------===//
178 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
179 if (Constant *LC = dyn_cast<Constant>(LHS))
180 if (Constant *RC = dyn_cast<Constant>(RHS))
181 return Folder.CreateAdd(LC, RC);
182 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
184 Value *CreateFAdd(Value *LHS, Value *RHS, const char *Name = "") {
185 if (Constant *LC = dyn_cast<Constant>(LHS))
186 if (Constant *RC = dyn_cast<Constant>(RHS))
187 return Folder.CreateFAdd(LC, RC);
188 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
190 Value *CreateSub(Value *LHS, Value *RHS, const char *Name = "") {
191 if (Constant *LC = dyn_cast<Constant>(LHS))
192 if (Constant *RC = dyn_cast<Constant>(RHS))
193 return Folder.CreateSub(LC, RC);
194 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
196 Value *CreateFSub(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.CreateFSub(LC, RC);
200 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
202 Value *CreateMul(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.CreateMul(LC, RC);
206 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
208 Value *CreateFMul(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.CreateFMul(LC, RC);
212 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
214 Value *CreateUDiv(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.CreateUDiv(LC, RC);
218 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
220 Value *CreateSDiv(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.CreateSDiv(LC, RC);
224 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
226 Value *CreateFDiv(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.CreateFDiv(LC, RC);
230 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
232 Value *CreateURem(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.CreateURem(LC, RC);
236 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
238 Value *CreateSRem(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.CreateSRem(LC, RC);
242 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
244 Value *CreateFRem(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.CreateFRem(LC, RC);
248 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
250 Value *CreateShl(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.CreateShl(LC, RC);
254 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
256 Value *CreateLShr(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.CreateLShr(LC, RC);
260 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
262 Value *CreateAShr(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.CreateAShr(LC, RC);
266 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
268 Value *CreateAnd(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.CreateAnd(LC, RC);
272 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
274 Value *CreateOr(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.CreateOr(LC, RC);
278 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
280 Value *CreateXor(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.CreateXor(LC, RC);
284 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
287 Value *CreateBinOp(Instruction::BinaryOps Opc,
288 Value *LHS, Value *RHS, const char *Name = "") {
289 if (Constant *LC = dyn_cast<Constant>(LHS))
290 if (Constant *RC = dyn_cast<Constant>(RHS))
291 return Folder.CreateBinOp(Opc, LC, RC);
292 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
295 Value *CreateNeg(Value *V, const char *Name = "") {
296 if (Constant *VC = dyn_cast<Constant>(V))
297 return Folder.CreateNeg(VC);
298 return Insert(BinaryOperator::CreateNeg(V), Name);
300 Value *CreateFNeg(Value *V, const char *Name = "") {
301 if (Constant *VC = dyn_cast<Constant>(V))
302 return Folder.CreateFNeg(VC);
303 return Insert(BinaryOperator::CreateFNeg(V), Name);
305 Value *CreateNot(Value *V, const char *Name = "") {
306 if (Constant *VC = dyn_cast<Constant>(V))
307 return Folder.CreateNot(VC);
308 return Insert(BinaryOperator::CreateNot(V), Name);
311 //===--------------------------------------------------------------------===//
312 // Instruction creation methods: Memory Instructions
313 //===--------------------------------------------------------------------===//
315 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
316 const char *Name = "") {
317 return Insert(new MallocInst(Ty, ArraySize), Name);
319 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
320 const char *Name = "") {
321 return Insert(new AllocaInst(Ty, ArraySize), Name);
323 FreeInst *CreateFree(Value *Ptr) {
324 return Insert(new FreeInst(Ptr));
326 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
327 return Insert(new LoadInst(Ptr), Name);
329 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
330 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
332 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
333 return Insert(new StoreInst(Val, Ptr, isVolatile));
335 template<typename InputIterator>
336 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
337 const char *Name = "") {
338 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
339 // Every index must be constant.
341 for (i = IdxBegin; i < IdxEnd; ++i) {
342 if (!dyn_cast<Constant>(*i))
346 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
348 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
350 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
351 if (Constant *PC = dyn_cast<Constant>(Ptr))
352 if (Constant *IC = dyn_cast<Constant>(Idx))
353 return Folder.CreateGetElementPtr(PC, &IC, 1);
354 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
356 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
357 Value *Idx = Context.getConstantInt(Type::Int32Ty, Idx0);
359 if (Constant *PC = dyn_cast<Constant>(Ptr))
360 return Folder.CreateGetElementPtr(PC, &Idx, 1);
362 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
364 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
365 const char *Name = "") {
367 Context.getConstantInt(Type::Int32Ty, Idx0),
368 Context.getConstantInt(Type::Int32Ty, Idx1)
371 if (Constant *PC = dyn_cast<Constant>(Ptr))
372 return Folder.CreateGetElementPtr(PC, Idxs, 2);
374 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
376 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
377 Value *Idx = Context.getConstantInt(Type::Int64Ty, Idx0);
379 if (Constant *PC = dyn_cast<Constant>(Ptr))
380 return Folder.CreateGetElementPtr(PC, &Idx, 1);
382 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
384 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
385 const char *Name = "") {
387 Context.getConstantInt(Type::Int64Ty, Idx0),
388 Context.getConstantInt(Type::Int64Ty, Idx1)
391 if (Constant *PC = dyn_cast<Constant>(Ptr))
392 return Folder.CreateGetElementPtr(PC, Idxs, 2);
394 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
396 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
397 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
399 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
400 Constant *StrConstant = Context.getConstantArray(Str, true);
401 GlobalVariable *gv = new GlobalVariable(Context,
402 StrConstant->getType(),
404 GlobalValue::InternalLinkage,
407 BB->getParent()->getParent(),
412 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
413 Value *gv = CreateGlobalString(Str, Name);
414 Value *zero = Context.getConstantInt(Type::Int32Ty, 0);
415 Value *Args[] = { zero, zero };
416 return CreateGEP(gv, Args, Args+2, Name);
418 //===--------------------------------------------------------------------===//
419 // Instruction creation methods: Cast/Conversion Operators
420 //===--------------------------------------------------------------------===//
422 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
423 return CreateCast(Instruction::Trunc, V, DestTy, Name);
425 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
426 return CreateCast(Instruction::ZExt, V, DestTy, Name);
428 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
429 return CreateCast(Instruction::SExt, V, DestTy, Name);
431 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
432 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
434 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
435 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
437 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
438 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
440 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
441 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
443 Value *CreateFPTrunc(Value *V, const Type *DestTy,
444 const char *Name = "") {
445 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
447 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
448 return CreateCast(Instruction::FPExt, V, DestTy, Name);
450 Value *CreatePtrToInt(Value *V, const Type *DestTy,
451 const char *Name = "") {
452 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
454 Value *CreateIntToPtr(Value *V, const Type *DestTy,
455 const char *Name = "") {
456 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
458 Value *CreateBitCast(Value *V, const Type *DestTy,
459 const char *Name = "") {
460 return CreateCast(Instruction::BitCast, V, DestTy, Name);
463 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
464 const char *Name = "") {
465 if (V->getType() == DestTy)
467 if (Constant *VC = dyn_cast<Constant>(V))
468 return Folder.CreateCast(Op, VC, DestTy);
469 return Insert(CastInst::Create(Op, V, DestTy), Name);
471 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
472 const char *Name = "") {
473 if (V->getType() == DestTy)
475 if (Constant *VC = dyn_cast<Constant>(V))
476 return Folder.CreateIntCast(VC, DestTy, isSigned);
477 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
480 //===--------------------------------------------------------------------===//
481 // Instruction creation methods: Compare Instructions
482 //===--------------------------------------------------------------------===//
484 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
485 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
487 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
488 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
490 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
491 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
493 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
494 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
496 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
497 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
499 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
500 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
502 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
503 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
505 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
506 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
508 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
509 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
511 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
512 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
515 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
516 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
518 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
519 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
521 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
522 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
524 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
525 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
527 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
528 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
530 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
531 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
533 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
534 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
536 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
537 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
539 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
540 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
542 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
543 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
545 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
546 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
548 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
549 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
551 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
552 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
554 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
555 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
558 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
559 const char *Name = "") {
560 if (Constant *LC = dyn_cast<Constant>(LHS))
561 if (Constant *RC = dyn_cast<Constant>(RHS))
562 return Folder.CreateICmp(P, LC, RC);
563 return Insert(new ICmpInst(P, LHS, RHS), Name);
565 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
566 const char *Name = "") {
567 if (Constant *LC = dyn_cast<Constant>(LHS))
568 if (Constant *RC = dyn_cast<Constant>(RHS))
569 return Folder.CreateFCmp(P, LC, RC);
570 return Insert(new FCmpInst(P, LHS, RHS), Name);
573 //===--------------------------------------------------------------------===//
574 // Instruction creation methods: Other Instructions
575 //===--------------------------------------------------------------------===//
577 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
578 return Insert(PHINode::Create(Ty), Name);
581 CallInst *CreateCall(Value *Callee, const char *Name = "") {
582 return Insert(CallInst::Create(Callee), Name);
584 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
585 return Insert(CallInst::Create(Callee, Arg), Name);
587 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
588 const char *Name = "") {
589 Value *Args[] = { Arg1, Arg2 };
590 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
592 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
593 const char *Name = "") {
594 Value *Args[] = { Arg1, Arg2, Arg3 };
595 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
597 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
598 Value *Arg4, const char *Name = "") {
599 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
600 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
603 template<typename InputIterator>
604 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
605 InputIterator ArgEnd, const char *Name = "") {
606 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
609 Value *CreateSelect(Value *C, Value *True, Value *False,
610 const char *Name = "") {
611 if (Constant *CC = dyn_cast<Constant>(C))
612 if (Constant *TC = dyn_cast<Constant>(True))
613 if (Constant *FC = dyn_cast<Constant>(False))
614 return Folder.CreateSelect(CC, TC, FC);
615 return Insert(SelectInst::Create(C, True, False), Name);
618 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
619 return Insert(new VAArgInst(List, Ty), Name);
622 Value *CreateExtractElement(Value *Vec, Value *Idx,
623 const char *Name = "") {
624 if (Constant *VC = dyn_cast<Constant>(Vec))
625 if (Constant *IC = dyn_cast<Constant>(Idx))
626 return Folder.CreateExtractElement(VC, IC);
627 return Insert(new ExtractElementInst(Vec, Idx), Name);
630 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
631 const char *Name = "") {
632 if (Constant *VC = dyn_cast<Constant>(Vec))
633 if (Constant *NC = dyn_cast<Constant>(NewElt))
634 if (Constant *IC = dyn_cast<Constant>(Idx))
635 return Folder.CreateInsertElement(VC, NC, IC);
636 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
639 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
640 const char *Name = "") {
641 if (Constant *V1C = dyn_cast<Constant>(V1))
642 if (Constant *V2C = dyn_cast<Constant>(V2))
643 if (Constant *MC = dyn_cast<Constant>(Mask))
644 return Folder.CreateShuffleVector(V1C, V2C, MC);
645 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
648 Value *CreateExtractValue(Value *Agg, unsigned Idx,
649 const char *Name = "") {
650 if (Constant *AggC = dyn_cast<Constant>(Agg))
651 return Folder.CreateExtractValue(AggC, &Idx, 1);
652 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
655 template<typename InputIterator>
656 Value *CreateExtractValue(Value *Agg,
657 InputIterator IdxBegin,
658 InputIterator IdxEnd,
659 const char *Name = "") {
660 if (Constant *AggC = dyn_cast<Constant>(Agg))
661 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
662 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
665 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
666 const char *Name = "") {
667 if (Constant *AggC = dyn_cast<Constant>(Agg))
668 if (Constant *ValC = dyn_cast<Constant>(Val))
669 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
670 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
673 template<typename InputIterator>
674 Value *CreateInsertValue(Value *Agg, Value *Val,
675 InputIterator IdxBegin,
676 InputIterator IdxEnd,
677 const char *Name = "") {
678 if (Constant *AggC = dyn_cast<Constant>(Agg))
679 if (Constant *ValC = dyn_cast<Constant>(Val))
680 return Folder.CreateInsertValue(AggC, ValC,
681 IdxBegin, IdxEnd - IdxBegin);
682 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
685 //===--------------------------------------------------------------------===//
686 // Utility creation methods
687 //===--------------------------------------------------------------------===//
689 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
690 Value *CreateIsNull(Value *Arg, const char *Name = "") {
691 return CreateICmpEQ(Arg, Context.getNullValue(Arg->getType()),
695 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
696 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
697 return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()),
701 /// CreatePtrDiff - Return the i64 difference between two pointer values,
702 /// dividing out the size of the pointed-to objects. This is intended to
703 /// implement C-style pointer subtraction.
704 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
705 assert(LHS->getType() == RHS->getType() &&
706 "Pointer subtraction operand types must match!");
707 const PointerType *ArgType = cast<PointerType>(LHS->getType());
708 Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
709 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
710 Value *Difference = CreateSub(LHS_int, RHS_int);
711 return CreateSDiv(Difference,
712 Context.getConstantExprSizeOf(ArgType->getElementType()),