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/ADT/Twine.h"
25 #include "llvm/Support/ConstantFolder.h"
29 /// IRBuilder - This provides a uniform API for creating instructions and
30 /// inserting them into a basic block: either at the end of a BasicBlock, or
31 /// at a specific iterator location in a block.
33 /// Note that the builder does not expose the full generality of LLVM
34 /// instructions. For example, it cannot be used to create instructions with
35 /// arbitrary names (specifically, names with nul characters in them) - It only
36 /// supports nul-terminated C strings. For fully generic names, use
37 /// I->setName(). For access to extra instruction properties, use the mutators
38 /// (e.g. setVolatile) on the instructions after they have been created.
39 /// The first template argument handles whether or not to preserve names in the
40 /// final instruction output. This defaults to on. The second template argument
41 /// specifies a class to use for creating constants. This defaults to creating
42 /// minimally folded constants.
43 template <bool preserveNames=true, typename T = ConstantFolder> class IRBuilder{
45 BasicBlock::iterator InsertPt;
49 IRBuilder(LLVMContext &C, const T& F) :
50 Context(C), Folder(F) { ClearInsertionPoint(); }
52 explicit IRBuilder(LLVMContext &C) : Context(C), Folder(C) {
53 ClearInsertionPoint();
56 explicit IRBuilder(BasicBlock *TheBB, const T& F)
57 : Context(TheBB->getContext()), Folder(F) {
58 SetInsertPoint(TheBB);
61 explicit IRBuilder(BasicBlock *TheBB)
62 : Context(TheBB->getContext()), Folder(Context) {
63 SetInsertPoint(TheBB);
66 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
67 : Context(TheBB->getContext()), Folder(F) {
68 SetInsertPoint(TheBB, IP);
71 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
72 : Context(TheBB->getContext()), Folder(Context) {
73 SetInsertPoint(TheBB, IP);
76 /// getFolder - Get the constant folder being used.
77 const T& getFolder() { return Folder; }
79 /// isNamePreserving - Return true if this builder is configured to actually
80 /// add the requested names to IR created through it.
81 bool isNamePreserving() const { return preserveNames; }
83 //===--------------------------------------------------------------------===//
84 // Builder configuration methods
85 //===--------------------------------------------------------------------===//
87 /// ClearInsertionPoint - Clear the insertion point: created instructions will
88 /// not be inserted into a block.
89 void ClearInsertionPoint() {
93 BasicBlock *GetInsertBlock() const { return BB; }
95 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
97 /// SetInsertPoint - This specifies that created instructions should be
98 /// appended to the end of the specified block.
99 void SetInsertPoint(BasicBlock *TheBB) {
101 InsertPt = BB->end();
104 /// SetInsertPoint - This specifies that created instructions should be
105 /// inserted at the specified point.
106 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
111 /// Insert - Insert and return the specified instruction.
112 template<typename InstTy>
113 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
114 InsertHelper(I, Name);
118 /// InsertHelper - Insert the specified instruction at the specified insertion
119 /// point. This is split out of Insert so that it isn't duplicated for every
120 /// template instantiation.
121 void InsertHelper(Instruction *I, const Twine &Name) const {
122 if (BB) BB->getInstList().insert(InsertPt, I);
127 //===--------------------------------------------------------------------===//
128 // Type creation methods
129 //===--------------------------------------------------------------------===//
131 /// getInt1Ty - Fetch the type representing a single bit
132 const Type *getInt1Ty() {
133 return Type::getInt1Ty(Context);
136 /// getInt8Ty - Fetch the type representing an 8-bit integer.
137 const Type *getInt8Ty() {
138 return Type::getInt8Ty(Context);
141 /// getInt16Ty - Fetch the type representing a 16-bit integer.
142 const Type *getInt16Ty() {
143 return Type::getInt16Ty(Context);
146 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
147 const Type *getInt32Ty() {
148 return Type::getInt32Ty(Context);
151 /// getInt64Ty - Fetch the type representing a 64-bit integer.
152 const Type *getInt64Ty() {
153 return Type::getInt64Ty(Context);
156 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
157 const Type *getFloatTy() {
158 return Type::getFloatTy(Context);
161 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
162 const Type *getDoubleTy() {
163 return Type::getDoubleTy(Context);
166 /// getVoidTy - Fetch the type representing void.
167 const Type *getVoidTy() {
168 return Type::getVoidTy(Context);
171 //===--------------------------------------------------------------------===//
172 // Instruction creation methods: Terminators
173 //===--------------------------------------------------------------------===//
175 /// CreateRetVoid - Create a 'ret void' instruction.
176 ReturnInst *CreateRetVoid() {
177 return Insert(ReturnInst::Create(Context));
181 /// CreateRet - Create a 'ret <val>' instruction.
183 ReturnInst *CreateRet(Value *V) {
184 return Insert(ReturnInst::Create(Context, V));
187 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
188 /// with one Value from the retVals array each, that build a aggregate
189 /// return value one value at a time, and a ret instruction to return
190 /// the resulting aggregate value. This is a convenience function for
191 /// code that uses aggregate return values as a vehicle for having
192 /// multiple return values.
194 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
195 const Type *RetType = BB->getParent()->getReturnType();
196 Value *V = UndefValue::get(RetType);
197 for (unsigned i = 0; i != N; ++i)
198 V = CreateInsertValue(V, retVals[i], i, "mrv");
199 return Insert(ReturnInst::Create(Context, V));
202 /// CreateBr - Create an unconditional 'br label X' instruction.
203 BranchInst *CreateBr(BasicBlock *Dest) {
204 return Insert(BranchInst::Create(Dest));
207 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
209 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
210 return Insert(BranchInst::Create(True, False, Cond));
213 /// CreateSwitch - Create a switch instruction with the specified value,
214 /// default dest, and with a hint for the number of cases that will be added
215 /// (for efficient allocation).
216 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
217 return Insert(SwitchInst::Create(V, Dest, NumCases));
220 /// CreateInvoke - Create an invoke instruction.
221 template<typename InputIterator>
222 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
223 BasicBlock *UnwindDest, InputIterator ArgBegin,
224 InputIterator ArgEnd, const Twine &Name = "") {
225 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
226 ArgBegin, ArgEnd), Name);
229 UnwindInst *CreateUnwind() {
230 return Insert(new UnwindInst(Context));
233 UnreachableInst *CreateUnreachable() {
234 return Insert(new UnreachableInst(Context));
237 //===--------------------------------------------------------------------===//
238 // Instruction creation methods: Binary Operators
239 //===--------------------------------------------------------------------===//
241 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
242 if (Constant *LC = dyn_cast<Constant>(LHS))
243 if (Constant *RC = dyn_cast<Constant>(RHS))
244 return Folder.CreateAdd(LC, RC);
245 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
247 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
248 if (Constant *LC = dyn_cast<Constant>(LHS))
249 if (Constant *RC = dyn_cast<Constant>(RHS))
250 return Folder.CreateNSWAdd(LC, RC);
251 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
253 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
254 if (Constant *LC = dyn_cast<Constant>(LHS))
255 if (Constant *RC = dyn_cast<Constant>(RHS))
256 return Folder.CreateFAdd(LC, RC);
257 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
259 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
260 if (Constant *LC = dyn_cast<Constant>(LHS))
261 if (Constant *RC = dyn_cast<Constant>(RHS))
262 return Folder.CreateSub(LC, RC);
263 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
265 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
266 if (Constant *LC = dyn_cast<Constant>(LHS))
267 if (Constant *RC = dyn_cast<Constant>(RHS))
268 return Folder.CreateFSub(LC, RC);
269 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
271 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
272 if (Constant *LC = dyn_cast<Constant>(LHS))
273 if (Constant *RC = dyn_cast<Constant>(RHS))
274 return Folder.CreateMul(LC, RC);
275 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
277 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
278 if (Constant *LC = dyn_cast<Constant>(LHS))
279 if (Constant *RC = dyn_cast<Constant>(RHS))
280 return Folder.CreateFMul(LC, RC);
281 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
283 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
284 if (Constant *LC = dyn_cast<Constant>(LHS))
285 if (Constant *RC = dyn_cast<Constant>(RHS))
286 return Folder.CreateUDiv(LC, RC);
287 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
289 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
290 if (Constant *LC = dyn_cast<Constant>(LHS))
291 if (Constant *RC = dyn_cast<Constant>(RHS))
292 return Folder.CreateSDiv(LC, RC);
293 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
295 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
296 if (Constant *LC = dyn_cast<Constant>(LHS))
297 if (Constant *RC = dyn_cast<Constant>(RHS))
298 return Folder.CreateExactSDiv(LC, RC);
299 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
301 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
302 if (Constant *LC = dyn_cast<Constant>(LHS))
303 if (Constant *RC = dyn_cast<Constant>(RHS))
304 return Folder.CreateFDiv(LC, RC);
305 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
307 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
308 if (Constant *LC = dyn_cast<Constant>(LHS))
309 if (Constant *RC = dyn_cast<Constant>(RHS))
310 return Folder.CreateURem(LC, RC);
311 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
313 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
314 if (Constant *LC = dyn_cast<Constant>(LHS))
315 if (Constant *RC = dyn_cast<Constant>(RHS))
316 return Folder.CreateSRem(LC, RC);
317 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
319 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
320 if (Constant *LC = dyn_cast<Constant>(LHS))
321 if (Constant *RC = dyn_cast<Constant>(RHS))
322 return Folder.CreateFRem(LC, RC);
323 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
325 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
326 if (Constant *LC = dyn_cast<Constant>(LHS))
327 if (Constant *RC = dyn_cast<Constant>(RHS))
328 return Folder.CreateShl(LC, RC);
329 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
331 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
332 if (Constant *LC = dyn_cast<Constant>(LHS))
333 if (Constant *RC = dyn_cast<Constant>(RHS))
334 return Folder.CreateLShr(LC, RC);
335 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
337 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
338 if (Constant *LC = dyn_cast<Constant>(LHS))
339 if (Constant *RC = dyn_cast<Constant>(RHS))
340 return Folder.CreateAShr(LC, RC);
341 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
343 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
344 if (Constant *LC = dyn_cast<Constant>(LHS))
345 if (Constant *RC = dyn_cast<Constant>(RHS))
346 return Folder.CreateAnd(LC, RC);
347 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
349 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
350 if (Constant *LC = dyn_cast<Constant>(LHS))
351 if (Constant *RC = dyn_cast<Constant>(RHS))
352 return Folder.CreateOr(LC, RC);
353 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
355 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
356 if (Constant *LC = dyn_cast<Constant>(LHS))
357 if (Constant *RC = dyn_cast<Constant>(RHS))
358 return Folder.CreateXor(LC, RC);
359 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
362 Value *CreateBinOp(Instruction::BinaryOps Opc,
363 Value *LHS, Value *RHS, const Twine &Name = "") {
364 if (Constant *LC = dyn_cast<Constant>(LHS))
365 if (Constant *RC = dyn_cast<Constant>(RHS))
366 return Folder.CreateBinOp(Opc, LC, RC);
367 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
370 Value *CreateNeg(Value *V, const Twine &Name = "") {
371 if (Constant *VC = dyn_cast<Constant>(V))
372 return Folder.CreateNeg(VC);
373 return Insert(BinaryOperator::CreateNeg(V), Name);
375 Value *CreateFNeg(Value *V, const Twine &Name = "") {
376 if (Constant *VC = dyn_cast<Constant>(V))
377 return Folder.CreateFNeg(VC);
378 return Insert(BinaryOperator::CreateFNeg(V), Name);
380 Value *CreateNot(Value *V, const Twine &Name = "") {
381 if (Constant *VC = dyn_cast<Constant>(V))
382 return Folder.CreateNot(VC);
383 return Insert(BinaryOperator::CreateNot(V), Name);
386 //===--------------------------------------------------------------------===//
387 // Instruction creation methods: Memory Instructions
388 //===--------------------------------------------------------------------===//
390 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
391 const Twine &Name = "") {
392 return Insert(new MallocInst(Ty, ArraySize), Name);
394 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
395 const Twine &Name = "") {
396 return Insert(new AllocaInst(Ty, ArraySize), Name);
398 FreeInst *CreateFree(Value *Ptr) {
399 return Insert(new FreeInst(Ptr));
401 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
402 // converting the string to 'bool' for the isVolatile parameter.
403 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
404 return Insert(new LoadInst(Ptr), Name);
406 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
407 return Insert(new LoadInst(Ptr), Name);
409 LoadInst *CreateLoad(Value *Ptr, bool isVolatile,
410 const Twine &Name = "") {
411 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
413 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
414 return Insert(new StoreInst(Val, Ptr, isVolatile));
416 template<typename InputIterator>
417 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
418 const Twine &Name = "") {
419 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
420 // Every index must be constant.
422 for (i = IdxBegin; i < IdxEnd; ++i)
423 if (!isa<Constant>(*i))
426 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
428 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
430 template<typename InputIterator>
431 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
432 const Twine &Name = "") {
433 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
434 // Every index must be constant.
436 for (i = IdxBegin; i < IdxEnd; ++i)
437 if (!isa<Constant>(*i))
440 return Folder.CreateInBoundsGetElementPtr(PC,
444 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
447 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
448 if (Constant *PC = dyn_cast<Constant>(Ptr))
449 if (Constant *IC = dyn_cast<Constant>(Idx))
450 return Folder.CreateGetElementPtr(PC, &IC, 1);
451 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
453 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
454 if (Constant *PC = dyn_cast<Constant>(Ptr))
455 if (Constant *IC = dyn_cast<Constant>(Idx))
456 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
457 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
459 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
460 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
462 if (Constant *PC = dyn_cast<Constant>(Ptr))
463 return Folder.CreateGetElementPtr(PC, &Idx, 1);
465 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
467 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
468 const Twine &Name = "") {
469 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
471 if (Constant *PC = dyn_cast<Constant>(Ptr))
472 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
474 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
476 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
477 const Twine &Name = "") {
479 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
480 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
483 if (Constant *PC = dyn_cast<Constant>(Ptr))
484 return Folder.CreateGetElementPtr(PC, Idxs, 2);
486 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
488 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
489 const Twine &Name = "") {
491 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
492 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
495 if (Constant *PC = dyn_cast<Constant>(Ptr))
496 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
498 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
500 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
501 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
503 if (Constant *PC = dyn_cast<Constant>(Ptr))
504 return Folder.CreateGetElementPtr(PC, &Idx, 1);
506 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
508 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
509 const Twine &Name = "") {
510 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
512 if (Constant *PC = dyn_cast<Constant>(Ptr))
513 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
515 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
517 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
518 const Twine &Name = "") {
520 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
521 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
524 if (Constant *PC = dyn_cast<Constant>(Ptr))
525 return Folder.CreateGetElementPtr(PC, Idxs, 2);
527 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
529 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
530 const Twine &Name = "") {
532 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
533 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
536 if (Constant *PC = dyn_cast<Constant>(Ptr))
537 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
539 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
541 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
542 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
544 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
545 Constant *StrConstant = ConstantArray::get(Context, Str, true);
546 Module &M = *BB->getParent()->getParent();
547 GlobalVariable *gv = new GlobalVariable(M,
548 StrConstant->getType(),
550 GlobalValue::InternalLinkage,
558 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
559 Value *gv = CreateGlobalString(Str, Name);
560 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
561 Value *Args[] = { zero, zero };
562 return CreateInBoundsGEP(gv, Args, Args+2, Name);
564 //===--------------------------------------------------------------------===//
565 // Instruction creation methods: Cast/Conversion Operators
566 //===--------------------------------------------------------------------===//
568 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
569 return CreateCast(Instruction::Trunc, V, DestTy, Name);
571 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
572 return CreateCast(Instruction::ZExt, V, DestTy, Name);
574 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
575 return CreateCast(Instruction::SExt, V, DestTy, Name);
577 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
578 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
580 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
581 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
583 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
584 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
586 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
587 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
589 Value *CreateFPTrunc(Value *V, const Type *DestTy,
590 const Twine &Name = "") {
591 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
593 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
594 return CreateCast(Instruction::FPExt, V, DestTy, Name);
596 Value *CreatePtrToInt(Value *V, const Type *DestTy,
597 const Twine &Name = "") {
598 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
600 Value *CreateIntToPtr(Value *V, const Type *DestTy,
601 const Twine &Name = "") {
602 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
604 Value *CreateBitCast(Value *V, const Type *DestTy,
605 const Twine &Name = "") {
606 return CreateCast(Instruction::BitCast, V, DestTy, Name);
608 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
609 const Twine &Name = "") {
610 if (V->getType() == DestTy)
612 if (Constant *VC = dyn_cast<Constant>(V))
613 return Folder.CreateZExtOrBitCast(VC, DestTy);
614 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
616 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
617 const Twine &Name = "") {
618 if (V->getType() == DestTy)
620 if (Constant *VC = dyn_cast<Constant>(V))
621 return Folder.CreateSExtOrBitCast(VC, DestTy);
622 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
624 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
625 const Twine &Name = "") {
626 if (V->getType() == DestTy)
628 if (Constant *VC = dyn_cast<Constant>(V))
629 return Folder.CreateTruncOrBitCast(VC, DestTy);
630 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
632 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
633 const Twine &Name = "") {
634 if (V->getType() == DestTy)
636 if (Constant *VC = dyn_cast<Constant>(V))
637 return Folder.CreateCast(Op, VC, DestTy);
638 return Insert(CastInst::Create(Op, V, DestTy), Name);
640 Value *CreatePointerCast(Value *V, const Type *DestTy,
641 const Twine &Name = "") {
642 if (V->getType() == DestTy)
644 if (Constant *VC = dyn_cast<Constant>(V))
645 return Folder.CreatePointerCast(VC, DestTy);
646 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
648 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
649 const Twine &Name = "") {
650 if (V->getType() == DestTy)
652 if (Constant *VC = dyn_cast<Constant>(V))
653 return Folder.CreateIntCast(VC, DestTy, isSigned);
654 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
656 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
657 if (V->getType() == DestTy)
659 if (Constant *VC = dyn_cast<Constant>(V))
660 return Folder.CreateFPCast(VC, DestTy);
661 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
664 //===--------------------------------------------------------------------===//
665 // Instruction creation methods: Compare Instructions
666 //===--------------------------------------------------------------------===//
668 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
669 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
671 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
672 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
674 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
675 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
677 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
678 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
680 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
681 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
683 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
684 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
686 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
687 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
689 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
690 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
692 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
693 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
695 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
696 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
699 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
700 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
702 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
703 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
705 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
706 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
708 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
709 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
711 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
712 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
714 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
715 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
717 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
718 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
720 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
721 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
723 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
724 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
726 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
727 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
729 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
730 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
732 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
733 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
735 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
736 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
738 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
739 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
742 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
743 const Twine &Name = "") {
744 if (Constant *LC = dyn_cast<Constant>(LHS))
745 if (Constant *RC = dyn_cast<Constant>(RHS))
746 return Folder.CreateICmp(P, LC, RC);
747 return Insert(new ICmpInst(P, LHS, RHS), Name);
749 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
750 const Twine &Name = "") {
751 if (Constant *LC = dyn_cast<Constant>(LHS))
752 if (Constant *RC = dyn_cast<Constant>(RHS))
753 return Folder.CreateFCmp(P, LC, RC);
754 return Insert(new FCmpInst(P, LHS, RHS), Name);
757 //===--------------------------------------------------------------------===//
758 // Instruction creation methods: Other Instructions
759 //===--------------------------------------------------------------------===//
761 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
762 return Insert(PHINode::Create(Ty), Name);
765 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
766 return Insert(CallInst::Create(Callee), Name);
768 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
769 return Insert(CallInst::Create(Callee, Arg), Name);
771 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
772 const Twine &Name = "") {
773 Value *Args[] = { Arg1, Arg2 };
774 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
776 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
777 const Twine &Name = "") {
778 Value *Args[] = { Arg1, Arg2, Arg3 };
779 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
781 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
782 Value *Arg4, const Twine &Name = "") {
783 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
784 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
787 template<typename InputIterator>
788 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
789 InputIterator ArgEnd, const Twine &Name = "") {
790 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
793 Value *CreateSelect(Value *C, Value *True, Value *False,
794 const Twine &Name = "") {
795 if (Constant *CC = dyn_cast<Constant>(C))
796 if (Constant *TC = dyn_cast<Constant>(True))
797 if (Constant *FC = dyn_cast<Constant>(False))
798 return Folder.CreateSelect(CC, TC, FC);
799 return Insert(SelectInst::Create(C, True, False), Name);
802 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
803 return Insert(new VAArgInst(List, Ty), Name);
806 Value *CreateExtractElement(Value *Vec, Value *Idx,
807 const Twine &Name = "") {
808 if (Constant *VC = dyn_cast<Constant>(Vec))
809 if (Constant *IC = dyn_cast<Constant>(Idx))
810 return Folder.CreateExtractElement(VC, IC);
811 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
814 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
815 const Twine &Name = "") {
816 if (Constant *VC = dyn_cast<Constant>(Vec))
817 if (Constant *NC = dyn_cast<Constant>(NewElt))
818 if (Constant *IC = dyn_cast<Constant>(Idx))
819 return Folder.CreateInsertElement(VC, NC, IC);
820 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
823 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
824 const Twine &Name = "") {
825 if (Constant *V1C = dyn_cast<Constant>(V1))
826 if (Constant *V2C = dyn_cast<Constant>(V2))
827 if (Constant *MC = dyn_cast<Constant>(Mask))
828 return Folder.CreateShuffleVector(V1C, V2C, MC);
829 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
832 Value *CreateExtractValue(Value *Agg, unsigned Idx,
833 const Twine &Name = "") {
834 if (Constant *AggC = dyn_cast<Constant>(Agg))
835 return Folder.CreateExtractValue(AggC, &Idx, 1);
836 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
839 template<typename InputIterator>
840 Value *CreateExtractValue(Value *Agg,
841 InputIterator IdxBegin,
842 InputIterator IdxEnd,
843 const Twine &Name = "") {
844 if (Constant *AggC = dyn_cast<Constant>(Agg))
845 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
846 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
849 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
850 const Twine &Name = "") {
851 if (Constant *AggC = dyn_cast<Constant>(Agg))
852 if (Constant *ValC = dyn_cast<Constant>(Val))
853 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
854 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
857 template<typename InputIterator>
858 Value *CreateInsertValue(Value *Agg, Value *Val,
859 InputIterator IdxBegin,
860 InputIterator IdxEnd,
861 const Twine &Name = "") {
862 if (Constant *AggC = dyn_cast<Constant>(Agg))
863 if (Constant *ValC = dyn_cast<Constant>(Val))
864 return Folder.CreateInsertValue(AggC, ValC,
865 IdxBegin, IdxEnd - IdxBegin);
866 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
869 //===--------------------------------------------------------------------===//
870 // Utility creation methods
871 //===--------------------------------------------------------------------===//
873 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
874 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
875 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
879 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
880 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
881 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
885 /// CreatePtrDiff - Return the i64 difference between two pointer values,
886 /// dividing out the size of the pointed-to objects. This is intended to
887 /// implement C-style pointer subtraction. As such, the pointers must be
888 /// appropriately aligned for their element types and pointing into the
890 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
891 assert(LHS->getType() == RHS->getType() &&
892 "Pointer subtraction operand types must match!");
893 const PointerType *ArgType = cast<PointerType>(LHS->getType());
894 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
895 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
896 Value *Difference = CreateSub(LHS_int, RHS_int);
897 return CreateExactSDiv(Difference,
898 ConstantExpr::getSizeOf(ArgType->getElementType()),