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/GlobalVariable.h"
21 #include "llvm/Function.h"
22 #include "llvm/Support/ConstantFolder.h"
26 /// IRBuilder - This provides a uniform API for creating instructions and
27 /// inserting them into a basic block: either at the end of a BasicBlock, or
28 /// at a specific iterator location in a block.
30 /// Note that the builder does not expose the full generality of LLVM
31 /// instructions. For example, it cannot be used to create instructions with
32 /// arbitrary names (specifically, names with nul characters in them) - It only
33 /// supports nul-terminated C strings. For fully generic names, use
34 /// I->setName(). For access to extra instruction properties, use the mutators
35 /// (e.g. setVolatile) on the instructions after they have been created.
36 /// The first template argument handles whether or not to preserve names in the
37 /// final instruction output. This defaults to on. The second template argument
38 /// specifies a class to use for creating constants. This defaults to creating
39 /// minimally folded constants.
40 template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
42 BasicBlock::iterator InsertPt;
45 IRBuilder(const T& F = T()) : Folder(F) { ClearInsertionPoint(); }
46 explicit IRBuilder(BasicBlock *TheBB, const T& F = T())
47 : Folder(F) { SetInsertPoint(TheBB); }
48 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T())
49 : Folder(F) { SetInsertPoint(TheBB, IP); }
51 /// getFolder - Get the constant folder being used.
52 const T& getFolder() { return Folder; }
54 /// isNamePreserving - Return true if this builder is configured to actually
55 /// add the requested names to IR created through it.
56 bool isNamePreserving() const { return preserveNames; }
58 //===--------------------------------------------------------------------===//
59 // Builder configuration methods
60 //===--------------------------------------------------------------------===//
62 /// ClearInsertionPoint - Clear the insertion point: created instructions will
63 /// not be inserted into a block.
64 void ClearInsertionPoint() {
68 BasicBlock *GetInsertBlock() const { return BB; }
70 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
72 /// SetInsertPoint - This specifies that created instructions should be
73 /// appended to the end of the specified block.
74 void SetInsertPoint(BasicBlock *TheBB) {
79 /// SetInsertPoint - This specifies that created instructions should be
80 /// inserted at the specified point.
81 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
86 /// Insert - Insert and return the specified instruction.
87 template<typename InstTy>
88 InstTy *Insert(InstTy *I, const char *Name = "") const {
89 InsertHelper(I, Name);
93 /// InsertHelper - Insert the specified instruction at the specified insertion
94 /// point. This is split out of Insert so that it isn't duplicated for every
95 /// template instantiation.
96 void InsertHelper(Instruction *I, const char *Name) const {
97 if (BB) BB->getInstList().insert(InsertPt, I);
98 if (preserveNames && Name[0])
102 //===--------------------------------------------------------------------===//
103 // Instruction creation methods: Terminators
104 //===--------------------------------------------------------------------===//
106 /// CreateRetVoid - Create a 'ret void' instruction.
107 ReturnInst *CreateRetVoid() {
108 return Insert(ReturnInst::Create());
112 /// CreateRet - Create a 'ret <val>' instruction.
114 ReturnInst *CreateRet(Value *V) {
115 return Insert(ReturnInst::Create(V));
118 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
119 /// with one Value from the retVals array each, that build a aggregate
120 /// return value one value at a time, and a ret instruction to return
121 /// the resulting aggregate value. This is a convenience function for
122 /// code that uses aggregate return values as a vehicle for having
123 /// multiple return values.
125 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
126 const Type *RetType = BB->getParent()->getReturnType();
127 Value *V = UndefValue::get(RetType);
128 for (unsigned i = 0; i != N; ++i)
129 V = CreateInsertValue(V, retVals[i], i, "mrv");
130 return Insert(ReturnInst::Create(V));
133 /// CreateBr - Create an unconditional 'br label X' instruction.
134 BranchInst *CreateBr(BasicBlock *Dest) {
135 return Insert(BranchInst::Create(Dest));
138 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
140 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
141 return Insert(BranchInst::Create(True, False, Cond));
144 /// CreateSwitch - Create a switch instruction with the specified value,
145 /// default dest, and with a hint for the number of cases that will be added
146 /// (for efficient allocation).
147 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
148 return Insert(SwitchInst::Create(V, Dest, NumCases));
151 /// CreateInvoke - Create an invoke instruction.
152 template<typename InputIterator>
153 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
154 BasicBlock *UnwindDest, InputIterator ArgBegin,
155 InputIterator ArgEnd, const char *Name = "") {
156 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
157 ArgBegin, ArgEnd), Name);
160 UnwindInst *CreateUnwind() {
161 return Insert(new UnwindInst());
164 UnreachableInst *CreateUnreachable() {
165 return Insert(new UnreachableInst());
168 //===--------------------------------------------------------------------===//
169 // Instruction creation methods: Binary Operators
170 //===--------------------------------------------------------------------===//
172 Value *CreateAdd(Value *LHS, Value *RHS, const char *Name = "") {
173 if (Constant *LC = dyn_cast<Constant>(LHS))
174 if (Constant *RC = dyn_cast<Constant>(RHS))
175 return Folder.CreateAdd(LC, RC);
176 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
178 Value *CreateFAdd(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.CreateFAdd(LC, RC);
182 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
184 Value *CreateSub(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.CreateSub(LC, RC);
188 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
190 Value *CreateFSub(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.CreateFSub(LC, RC);
194 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
196 Value *CreateMul(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.CreateMul(LC, RC);
200 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
202 Value *CreateFMul(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.CreateFMul(LC, RC);
206 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
208 Value *CreateUDiv(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.CreateUDiv(LC, RC);
212 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
214 Value *CreateSDiv(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.CreateSDiv(LC, RC);
218 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
220 Value *CreateFDiv(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.CreateFDiv(LC, RC);
224 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
226 Value *CreateURem(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.CreateURem(LC, RC);
230 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
232 Value *CreateSRem(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.CreateSRem(LC, RC);
236 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
238 Value *CreateFRem(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.CreateFRem(LC, RC);
242 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
244 Value *CreateShl(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.CreateShl(LC, RC);
248 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
250 Value *CreateLShr(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.CreateLShr(LC, RC);
254 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
256 Value *CreateAShr(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.CreateAShr(LC, RC);
260 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
262 Value *CreateAnd(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.CreateAnd(LC, RC);
266 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
268 Value *CreateOr(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.CreateOr(LC, RC);
272 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
274 Value *CreateXor(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.CreateXor(LC, RC);
278 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
281 Value *CreateBinOp(Instruction::BinaryOps Opc,
282 Value *LHS, Value *RHS, const char *Name = "") {
283 if (Constant *LC = dyn_cast<Constant>(LHS))
284 if (Constant *RC = dyn_cast<Constant>(RHS))
285 return Folder.CreateBinOp(Opc, LC, RC);
286 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
289 Value *CreateNeg(Value *V, const char *Name = "") {
290 if (Constant *VC = dyn_cast<Constant>(V))
291 return Folder.CreateNeg(VC);
292 return Insert(BinaryOperator::CreateNeg(V), Name);
294 Value *CreateFNeg(Value *V, const char *Name = "") {
295 if (Constant *VC = dyn_cast<Constant>(V))
296 return Folder.CreateFNeg(VC);
297 return Insert(BinaryOperator::CreateFNeg(V), Name);
299 Value *CreateNot(Value *V, const char *Name = "") {
300 if (Constant *VC = dyn_cast<Constant>(V))
301 return Folder.CreateNot(VC);
302 return Insert(BinaryOperator::CreateNot(V), Name);
305 //===--------------------------------------------------------------------===//
306 // Instruction creation methods: Memory Instructions
307 //===--------------------------------------------------------------------===//
309 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
310 const char *Name = "") {
311 return Insert(new MallocInst(Ty, ArraySize), Name);
313 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
314 const char *Name = "") {
315 return Insert(new AllocaInst(Ty, ArraySize), Name);
317 FreeInst *CreateFree(Value *Ptr) {
318 return Insert(new FreeInst(Ptr));
320 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
321 return Insert(new LoadInst(Ptr), Name);
323 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
324 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
326 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
327 return Insert(new StoreInst(Val, Ptr, isVolatile));
329 template<typename InputIterator>
330 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
331 const char *Name = "") {
332 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
333 // Every index must be constant.
335 for (i = IdxBegin; i < IdxEnd; ++i) {
336 if (!dyn_cast<Constant>(*i))
340 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
342 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
344 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
345 if (Constant *PC = dyn_cast<Constant>(Ptr))
346 if (Constant *IC = dyn_cast<Constant>(Idx))
347 return Folder.CreateGetElementPtr(PC, &IC, 1);
348 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
350 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
351 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
353 if (Constant *PC = dyn_cast<Constant>(Ptr))
354 return Folder.CreateGetElementPtr(PC, &Idx, 1);
356 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
358 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
359 const char *Name = "") {
361 ConstantInt::get(Type::Int32Ty, Idx0),
362 ConstantInt::get(Type::Int32Ty, Idx1)
365 if (Constant *PC = dyn_cast<Constant>(Ptr))
366 return Folder.CreateGetElementPtr(PC, Idxs, 2);
368 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
370 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
371 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
373 if (Constant *PC = dyn_cast<Constant>(Ptr))
374 return Folder.CreateGetElementPtr(PC, &Idx, 1);
376 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
378 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
379 const char *Name = "") {
381 ConstantInt::get(Type::Int64Ty, Idx0),
382 ConstantInt::get(Type::Int64Ty, Idx1)
385 if (Constant *PC = dyn_cast<Constant>(Ptr))
386 return Folder.CreateGetElementPtr(PC, Idxs, 2);
388 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
390 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
391 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
393 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
394 Constant *StrConstant = ConstantArray::get(Str, true);
395 GlobalVariable *gv = new GlobalVariable(StrConstant->getType(),
397 GlobalValue::InternalLinkage,
400 BB->getParent()->getParent(),
405 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
406 Value *gv = CreateGlobalString(Str, Name);
407 Value *zero = ConstantInt::get(Type::Int32Ty, 0);
408 Value *Args[] = { zero, zero };
409 return CreateGEP(gv, Args, Args+2, Name);
411 //===--------------------------------------------------------------------===//
412 // Instruction creation methods: Cast/Conversion Operators
413 //===--------------------------------------------------------------------===//
415 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
416 return CreateCast(Instruction::Trunc, V, DestTy, Name);
418 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
419 return CreateCast(Instruction::ZExt, V, DestTy, Name);
421 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
422 return CreateCast(Instruction::SExt, V, DestTy, Name);
424 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
425 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
427 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
428 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
430 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
431 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
433 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
434 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
436 Value *CreateFPTrunc(Value *V, const Type *DestTy,
437 const char *Name = "") {
438 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
440 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
441 return CreateCast(Instruction::FPExt, V, DestTy, Name);
443 Value *CreatePtrToInt(Value *V, const Type *DestTy,
444 const char *Name = "") {
445 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
447 Value *CreateIntToPtr(Value *V, const Type *DestTy,
448 const char *Name = "") {
449 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
451 Value *CreateBitCast(Value *V, const Type *DestTy,
452 const char *Name = "") {
453 return CreateCast(Instruction::BitCast, V, DestTy, Name);
456 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
457 const char *Name = "") {
458 if (V->getType() == DestTy)
460 if (Constant *VC = dyn_cast<Constant>(V))
461 return Folder.CreateCast(Op, VC, DestTy);
462 return Insert(CastInst::Create(Op, V, DestTy), Name);
464 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
465 const char *Name = "") {
466 if (V->getType() == DestTy)
468 if (Constant *VC = dyn_cast<Constant>(V))
469 return Folder.CreateIntCast(VC, DestTy, isSigned);
470 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
473 //===--------------------------------------------------------------------===//
474 // Instruction creation methods: Compare Instructions
475 //===--------------------------------------------------------------------===//
477 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
478 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
480 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
481 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
483 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
484 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
486 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
487 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
489 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
490 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
492 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
493 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
495 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
496 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
498 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
499 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
501 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
502 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
504 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
505 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
508 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
509 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
511 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
512 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
514 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
515 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
517 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
518 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
520 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
521 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
523 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
524 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
526 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
527 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
529 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
530 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
532 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
533 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
535 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
536 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
538 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
539 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
541 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
542 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
544 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
545 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
547 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
548 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
551 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
552 const char *Name = "") {
553 if (Constant *LC = dyn_cast<Constant>(LHS))
554 if (Constant *RC = dyn_cast<Constant>(RHS))
555 return Folder.CreateICmp(P, LC, RC);
556 return Insert(new ICmpInst(P, LHS, RHS), Name);
558 Value *CreateFCmp(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.CreateFCmp(P, LC, RC);
563 return Insert(new FCmpInst(P, LHS, RHS), Name);
566 Value *CreateVICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
567 const char *Name = "") {
568 if (Constant *LC = dyn_cast<Constant>(LHS))
569 if (Constant *RC = dyn_cast<Constant>(RHS))
570 return Folder.CreateVICmp(P, LC, RC);
571 return Insert(new VICmpInst(P, LHS, RHS), Name);
573 Value *CreateVFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
574 const char *Name = "") {
575 if (Constant *LC = dyn_cast<Constant>(LHS))
576 if (Constant *RC = dyn_cast<Constant>(RHS))
577 return Folder.CreateVFCmp(P, LC, RC);
578 return Insert(new VFCmpInst(P, LHS, RHS), Name);
581 //===--------------------------------------------------------------------===//
582 // Instruction creation methods: Other Instructions
583 //===--------------------------------------------------------------------===//
585 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
586 return Insert(PHINode::Create(Ty), Name);
589 CallInst *CreateCall(Value *Callee, const char *Name = "") {
590 return Insert(CallInst::Create(Callee), Name);
592 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
593 return Insert(CallInst::Create(Callee, Arg), Name);
595 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
596 const char *Name = "") {
597 Value *Args[] = { Arg1, Arg2 };
598 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
600 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
601 const char *Name = "") {
602 Value *Args[] = { Arg1, Arg2, Arg3 };
603 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
605 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
606 Value *Arg4, const char *Name = "") {
607 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
608 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
611 template<typename InputIterator>
612 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
613 InputIterator ArgEnd, const char *Name = "") {
614 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
617 Value *CreateSelect(Value *C, Value *True, Value *False,
618 const char *Name = "") {
619 if (Constant *CC = dyn_cast<Constant>(C))
620 if (Constant *TC = dyn_cast<Constant>(True))
621 if (Constant *FC = dyn_cast<Constant>(False))
622 return Folder.CreateSelect(CC, TC, FC);
623 return Insert(SelectInst::Create(C, True, False), Name);
626 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
627 return Insert(new VAArgInst(List, Ty), Name);
630 Value *CreateExtractElement(Value *Vec, Value *Idx,
631 const char *Name = "") {
632 if (Constant *VC = dyn_cast<Constant>(Vec))
633 if (Constant *IC = dyn_cast<Constant>(Idx))
634 return Folder.CreateExtractElement(VC, IC);
635 return Insert(new ExtractElementInst(Vec, Idx), Name);
638 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
639 const char *Name = "") {
640 if (Constant *VC = dyn_cast<Constant>(Vec))
641 if (Constant *NC = dyn_cast<Constant>(NewElt))
642 if (Constant *IC = dyn_cast<Constant>(Idx))
643 return Folder.CreateInsertElement(VC, NC, IC);
644 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
647 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
648 const char *Name = "") {
649 if (Constant *V1C = dyn_cast<Constant>(V1))
650 if (Constant *V2C = dyn_cast<Constant>(V2))
651 if (Constant *MC = dyn_cast<Constant>(Mask))
652 return Folder.CreateShuffleVector(V1C, V2C, MC);
653 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
656 Value *CreateExtractValue(Value *Agg, unsigned Idx,
657 const char *Name = "") {
658 if (Constant *AggC = dyn_cast<Constant>(Agg))
659 return Folder.CreateExtractValue(AggC, &Idx, 1);
660 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
663 template<typename InputIterator>
664 Value *CreateExtractValue(Value *Agg,
665 InputIterator IdxBegin,
666 InputIterator IdxEnd,
667 const char *Name = "") {
668 if (Constant *AggC = dyn_cast<Constant>(Agg))
669 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
670 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
673 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
674 const char *Name = "") {
675 if (Constant *AggC = dyn_cast<Constant>(Agg))
676 if (Constant *ValC = dyn_cast<Constant>(Val))
677 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
678 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
681 template<typename InputIterator>
682 Value *CreateInsertValue(Value *Agg, Value *Val,
683 InputIterator IdxBegin,
684 InputIterator IdxEnd,
685 const char *Name = "") {
686 if (Constant *AggC = dyn_cast<Constant>(Agg))
687 if (Constant *ValC = dyn_cast<Constant>(Val))
688 return Folder.CreateInsertValue(AggC, ValC,
689 IdxBegin, IdxEnd - IdxBegin);
690 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
693 //===--------------------------------------------------------------------===//
694 // Utility creation methods
695 //===--------------------------------------------------------------------===//
697 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
698 Value *CreateIsNull(Value *Arg, const char *Name = "") {
699 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
703 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
704 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
705 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
709 /// CreatePtrDiff - Return the i64 difference between two pointer values,
710 /// dividing out the size of the pointed-to objects. This is intended to
711 /// implement C-style pointer subtraction.
712 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
713 assert(LHS->getType() == RHS->getType() &&
714 "Pointer subtraction operand types must match!");
715 const PointerType *ArgType = cast<PointerType>(LHS->getType());
716 Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
717 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
718 Value *Difference = CreateSub(LHS_int, RHS_int);
719 return CreateSDiv(Difference,
720 ConstantExpr::getSizeOf(ArgType->getElementType()),