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 *CreateFAdd(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.CreateFAdd(LC, RC);
206 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
208 Value *CreateSub(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.CreateSub(LC, RC);
212 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
214 Value *CreateFSub(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.CreateFSub(LC, RC);
218 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
220 Value *CreateMul(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.CreateMul(LC, RC);
224 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
226 Value *CreateFMul(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.CreateFMul(LC, RC);
230 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
232 Value *CreateUDiv(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.CreateUDiv(LC, RC);
236 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
238 Value *CreateSDiv(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.CreateSDiv(LC, RC);
242 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
244 Value *CreateExactSDiv(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.CreateExactSDiv(LC, RC);
248 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
250 Value *CreateFDiv(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.CreateFDiv(LC, RC);
254 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
256 Value *CreateURem(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.CreateURem(LC, RC);
260 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
262 Value *CreateSRem(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.CreateSRem(LC, RC);
266 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
268 Value *CreateFRem(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.CreateFRem(LC, RC);
272 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
274 Value *CreateShl(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.CreateShl(LC, RC);
278 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
280 Value *CreateLShr(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.CreateLShr(LC, RC);
284 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
286 Value *CreateAShr(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.CreateAShr(LC, RC);
290 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
292 Value *CreateAnd(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.CreateAnd(LC, RC);
296 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
298 Value *CreateOr(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.CreateOr(LC, RC);
302 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
304 Value *CreateXor(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.CreateXor(LC, RC);
308 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
311 Value *CreateBinOp(Instruction::BinaryOps Opc,
312 Value *LHS, Value *RHS, const char *Name = "") {
313 if (Constant *LC = dyn_cast<Constant>(LHS))
314 if (Constant *RC = dyn_cast<Constant>(RHS))
315 return Folder.CreateBinOp(Opc, LC, RC);
316 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
319 Value *CreateNeg(Value *V, const char *Name = "") {
320 if (Constant *VC = dyn_cast<Constant>(V))
321 return Folder.CreateNeg(VC);
322 return Insert(BinaryOperator::CreateNeg(Context, V), Name);
324 Value *CreateFNeg(Value *V, const char *Name = "") {
325 if (Constant *VC = dyn_cast<Constant>(V))
326 return Folder.CreateFNeg(VC);
327 return Insert(BinaryOperator::CreateFNeg(Context, V), Name);
329 Value *CreateNot(Value *V, const char *Name = "") {
330 if (Constant *VC = dyn_cast<Constant>(V))
331 return Folder.CreateNot(VC);
332 return Insert(BinaryOperator::CreateNot(Context, V), Name);
335 //===--------------------------------------------------------------------===//
336 // Instruction creation methods: Memory Instructions
337 //===--------------------------------------------------------------------===//
339 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
340 const char *Name = "") {
341 return Insert(new MallocInst(Ty, ArraySize), Name);
343 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
344 const char *Name = "") {
345 return Insert(new AllocaInst(Ty, ArraySize), Name);
347 FreeInst *CreateFree(Value *Ptr) {
348 return Insert(new FreeInst(Ptr));
350 LoadInst *CreateLoad(Value *Ptr, const char *Name = "") {
351 return Insert(new LoadInst(Ptr), Name);
353 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const char *Name = "") {
354 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
356 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
357 return Insert(new StoreInst(Val, Ptr, isVolatile));
359 template<typename InputIterator>
360 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
361 const char *Name = "") {
362 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
363 // Every index must be constant.
365 for (i = IdxBegin; i < IdxEnd; ++i) {
366 if (!dyn_cast<Constant>(*i))
370 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
372 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
374 Value *CreateGEP(Value *Ptr, Value *Idx, const char *Name = "") {
375 if (Constant *PC = dyn_cast<Constant>(Ptr))
376 if (Constant *IC = dyn_cast<Constant>(Idx))
377 return Folder.CreateGetElementPtr(PC, &IC, 1);
378 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
380 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
381 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
383 if (Constant *PC = dyn_cast<Constant>(Ptr))
384 return Folder.CreateGetElementPtr(PC, &Idx, 1);
386 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
388 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
389 const char *Name = "") {
391 ConstantInt::get(Type::Int32Ty, Idx0),
392 ConstantInt::get(Type::Int32Ty, Idx1)
395 if (Constant *PC = dyn_cast<Constant>(Ptr))
396 return Folder.CreateGetElementPtr(PC, Idxs, 2);
398 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
400 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
401 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
403 if (Constant *PC = dyn_cast<Constant>(Ptr))
404 return Folder.CreateGetElementPtr(PC, &Idx, 1);
406 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
408 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
409 const char *Name = "") {
411 ConstantInt::get(Type::Int64Ty, Idx0),
412 ConstantInt::get(Type::Int64Ty, Idx1)
415 if (Constant *PC = dyn_cast<Constant>(Ptr))
416 return Folder.CreateGetElementPtr(PC, Idxs, 2);
418 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
420 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const char *Name = "") {
421 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
423 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
424 Constant *StrConstant = ConstantArray::get(Str, true);
425 Module &M = *BB->getParent()->getParent();
426 GlobalVariable *gv = new GlobalVariable(M,
427 StrConstant->getType(),
429 GlobalValue::InternalLinkage,
437 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
438 Value *gv = CreateGlobalString(Str, Name);
439 Value *zero = ConstantInt::get(Type::Int32Ty, 0);
440 Value *Args[] = { zero, zero };
441 return CreateGEP(gv, Args, Args+2, Name);
443 //===--------------------------------------------------------------------===//
444 // Instruction creation methods: Cast/Conversion Operators
445 //===--------------------------------------------------------------------===//
447 Value *CreateTrunc(Value *V, const Type *DestTy, const char *Name = "") {
448 return CreateCast(Instruction::Trunc, V, DestTy, Name);
450 Value *CreateZExt(Value *V, const Type *DestTy, const char *Name = "") {
451 return CreateCast(Instruction::ZExt, V, DestTy, Name);
453 Value *CreateSExt(Value *V, const Type *DestTy, const char *Name = "") {
454 return CreateCast(Instruction::SExt, V, DestTy, Name);
456 Value *CreateFPToUI(Value *V, const Type *DestTy, const char *Name = ""){
457 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
459 Value *CreateFPToSI(Value *V, const Type *DestTy, const char *Name = ""){
460 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
462 Value *CreateUIToFP(Value *V, const Type *DestTy, const char *Name = ""){
463 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
465 Value *CreateSIToFP(Value *V, const Type *DestTy, const char *Name = ""){
466 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
468 Value *CreateFPTrunc(Value *V, const Type *DestTy,
469 const char *Name = "") {
470 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
472 Value *CreateFPExt(Value *V, const Type *DestTy, const char *Name = "") {
473 return CreateCast(Instruction::FPExt, V, DestTy, Name);
475 Value *CreatePtrToInt(Value *V, const Type *DestTy,
476 const char *Name = "") {
477 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
479 Value *CreateIntToPtr(Value *V, const Type *DestTy,
480 const char *Name = "") {
481 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
483 Value *CreateBitCast(Value *V, const Type *DestTy,
484 const char *Name = "") {
485 return CreateCast(Instruction::BitCast, V, DestTy, Name);
488 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
489 const char *Name = "") {
490 if (V->getType() == DestTy)
492 if (Constant *VC = dyn_cast<Constant>(V))
493 return Folder.CreateCast(Op, VC, DestTy);
494 return Insert(CastInst::Create(Op, V, DestTy), Name);
496 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
497 const char *Name = "") {
498 if (V->getType() == DestTy)
500 if (Constant *VC = dyn_cast<Constant>(V))
501 return Folder.CreateIntCast(VC, DestTy, isSigned);
502 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
505 //===--------------------------------------------------------------------===//
506 // Instruction creation methods: Compare Instructions
507 //===--------------------------------------------------------------------===//
509 Value *CreateICmpEQ(Value *LHS, Value *RHS, const char *Name = "") {
510 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
512 Value *CreateICmpNE(Value *LHS, Value *RHS, const char *Name = "") {
513 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
515 Value *CreateICmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
516 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
518 Value *CreateICmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
519 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
521 Value *CreateICmpULT(Value *LHS, Value *RHS, const char *Name = "") {
522 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
524 Value *CreateICmpULE(Value *LHS, Value *RHS, const char *Name = "") {
525 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
527 Value *CreateICmpSGT(Value *LHS, Value *RHS, const char *Name = "") {
528 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
530 Value *CreateICmpSGE(Value *LHS, Value *RHS, const char *Name = "") {
531 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
533 Value *CreateICmpSLT(Value *LHS, Value *RHS, const char *Name = "") {
534 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
536 Value *CreateICmpSLE(Value *LHS, Value *RHS, const char *Name = "") {
537 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
540 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const char *Name = "") {
541 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
543 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const char *Name = "") {
544 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
546 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const char *Name = "") {
547 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
549 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const char *Name = "") {
550 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
552 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const char *Name = "") {
553 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
555 Value *CreateFCmpONE(Value *LHS, Value *RHS, const char *Name = "") {
556 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
558 Value *CreateFCmpORD(Value *LHS, Value *RHS, const char *Name = "") {
559 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
561 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const char *Name = "") {
562 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
564 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const char *Name = "") {
565 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
567 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const char *Name = "") {
568 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
570 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const char *Name = "") {
571 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
573 Value *CreateFCmpULT(Value *LHS, Value *RHS, const char *Name = "") {
574 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
576 Value *CreateFCmpULE(Value *LHS, Value *RHS, const char *Name = "") {
577 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
579 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const char *Name = "") {
580 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
583 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
584 const char *Name = "") {
585 if (Constant *LC = dyn_cast<Constant>(LHS))
586 if (Constant *RC = dyn_cast<Constant>(RHS))
587 return Folder.CreateICmp(P, LC, RC);
588 return Insert(new ICmpInst(Context, P, LHS, RHS), Name);
590 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
591 const char *Name = "") {
592 if (Constant *LC = dyn_cast<Constant>(LHS))
593 if (Constant *RC = dyn_cast<Constant>(RHS))
594 return Folder.CreateFCmp(P, LC, RC);
595 return Insert(new FCmpInst(Context, P, LHS, RHS), Name);
598 //===--------------------------------------------------------------------===//
599 // Instruction creation methods: Other Instructions
600 //===--------------------------------------------------------------------===//
602 PHINode *CreatePHI(const Type *Ty, const char *Name = "") {
603 return Insert(PHINode::Create(Ty), Name);
606 CallInst *CreateCall(Value *Callee, const char *Name = "") {
607 return Insert(CallInst::Create(Callee), Name);
609 CallInst *CreateCall(Value *Callee, Value *Arg, const char *Name = "") {
610 return Insert(CallInst::Create(Callee, Arg), Name);
612 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
613 const char *Name = "") {
614 Value *Args[] = { Arg1, Arg2 };
615 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
617 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
618 const char *Name = "") {
619 Value *Args[] = { Arg1, Arg2, Arg3 };
620 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
622 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
623 Value *Arg4, const char *Name = "") {
624 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
625 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
628 template<typename InputIterator>
629 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
630 InputIterator ArgEnd, const char *Name = "") {
631 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
634 Value *CreateSelect(Value *C, Value *True, Value *False,
635 const char *Name = "") {
636 if (Constant *CC = dyn_cast<Constant>(C))
637 if (Constant *TC = dyn_cast<Constant>(True))
638 if (Constant *FC = dyn_cast<Constant>(False))
639 return Folder.CreateSelect(CC, TC, FC);
640 return Insert(SelectInst::Create(C, True, False), Name);
643 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const char *Name = "") {
644 return Insert(new VAArgInst(List, Ty), Name);
647 Value *CreateExtractElement(Value *Vec, Value *Idx,
648 const char *Name = "") {
649 if (Constant *VC = dyn_cast<Constant>(Vec))
650 if (Constant *IC = dyn_cast<Constant>(Idx))
651 return Folder.CreateExtractElement(VC, IC);
652 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
655 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
656 const char *Name = "") {
657 if (Constant *VC = dyn_cast<Constant>(Vec))
658 if (Constant *NC = dyn_cast<Constant>(NewElt))
659 if (Constant *IC = dyn_cast<Constant>(Idx))
660 return Folder.CreateInsertElement(VC, NC, IC);
661 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
664 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
665 const char *Name = "") {
666 if (Constant *V1C = dyn_cast<Constant>(V1))
667 if (Constant *V2C = dyn_cast<Constant>(V2))
668 if (Constant *MC = dyn_cast<Constant>(Mask))
669 return Folder.CreateShuffleVector(V1C, V2C, MC);
670 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
673 Value *CreateExtractValue(Value *Agg, unsigned Idx,
674 const char *Name = "") {
675 if (Constant *AggC = dyn_cast<Constant>(Agg))
676 return Folder.CreateExtractValue(AggC, &Idx, 1);
677 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
680 template<typename InputIterator>
681 Value *CreateExtractValue(Value *Agg,
682 InputIterator IdxBegin,
683 InputIterator IdxEnd,
684 const char *Name = "") {
685 if (Constant *AggC = dyn_cast<Constant>(Agg))
686 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
687 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
690 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
691 const char *Name = "") {
692 if (Constant *AggC = dyn_cast<Constant>(Agg))
693 if (Constant *ValC = dyn_cast<Constant>(Val))
694 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
695 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
698 template<typename InputIterator>
699 Value *CreateInsertValue(Value *Agg, Value *Val,
700 InputIterator IdxBegin,
701 InputIterator IdxEnd,
702 const char *Name = "") {
703 if (Constant *AggC = dyn_cast<Constant>(Agg))
704 if (Constant *ValC = dyn_cast<Constant>(Val))
705 return Folder.CreateInsertValue(AggC, ValC,
706 IdxBegin, IdxEnd - IdxBegin);
707 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
710 //===--------------------------------------------------------------------===//
711 // Utility creation methods
712 //===--------------------------------------------------------------------===//
714 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
715 Value *CreateIsNull(Value *Arg, const char *Name = "") {
716 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
720 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
721 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
722 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
726 /// CreatePtrDiff - Return the i64 difference between two pointer values,
727 /// dividing out the size of the pointed-to objects. This is intended to
728 /// implement C-style pointer subtraction. As such, the pointers must be
729 /// appropriately aligned for their element types and pointing into the
731 Value *CreatePtrDiff(Value *LHS, Value *RHS, const char *Name = "") {
732 assert(LHS->getType() == RHS->getType() &&
733 "Pointer subtraction operand types must match!");
734 const PointerType *ArgType = cast<PointerType>(LHS->getType());
735 Value *LHS_int = CreatePtrToInt(LHS, Type::Int64Ty);
736 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
737 Value *Difference = CreateSub(LHS_int, RHS_int);
738 return CreateExactSDiv(Difference,
739 ConstantExpr::getSizeOf(ArgType->getElementType()),