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 /// IRBuilderDefaultInserter - This provides the default implementation of the
30 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
31 /// created by IRBuilder and needs to be inserted. By default, this inserts the
32 /// instruction at the insertion point.
33 template <bool preserveNames = true>
34 class IRBuilderDefaultInserter {
36 void InsertHelper(Instruction *I, const Twine &Name,
37 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
38 if (BB) BB->getInstList().insert(InsertPt, I);
45 /// IRBuilder - This provides a uniform API for creating instructions and
46 /// inserting them into a basic block: either at the end of a BasicBlock, or
47 /// at a specific iterator location in a block.
49 /// Note that the builder does not expose the full generality of LLVM
50 /// instructions. For access to extra instruction properties, use the mutators
51 /// (e.g. setVolatile) on the instructions after they have been created.
52 /// The first template argument handles whether or not to preserve names in the
53 /// final instruction output. This defaults to on. The second template argument
54 /// specifies a class to use for creating constants. This defaults to creating
55 /// minimally folded constants. The fourth template argument allows clients to
56 /// specify custom insertion hooks that are called on every newly created
58 template<bool preserveNames = true, typename T = ConstantFolder,
59 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
60 class IRBuilder : public Inserter {
62 BasicBlock::iterator InsertPt;
66 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
67 : Inserter(I), Context(C), Folder(F) {
68 ClearInsertionPoint();
71 explicit IRBuilder(LLVMContext &C) : Context(C), Folder(C) {
72 ClearInsertionPoint();
75 explicit IRBuilder(BasicBlock *TheBB, const T &F)
76 : Context(TheBB->getContext()), Folder(F) {
77 SetInsertPoint(TheBB);
80 explicit IRBuilder(BasicBlock *TheBB)
81 : Context(TheBB->getContext()), Folder(Context) {
82 SetInsertPoint(TheBB);
85 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
86 : Context(TheBB->getContext()), Folder(F) {
87 SetInsertPoint(TheBB, IP);
90 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
91 : Context(TheBB->getContext()), Folder(Context) {
92 SetInsertPoint(TheBB, IP);
95 /// getFolder - Get the constant folder being used.
96 const T &getFolder() { return Folder; }
98 /// isNamePreserving - Return true if this builder is configured to actually
99 /// add the requested names to IR created through it.
100 bool isNamePreserving() const { return preserveNames; }
102 //===--------------------------------------------------------------------===//
103 // Builder configuration methods
104 //===--------------------------------------------------------------------===//
106 /// ClearInsertionPoint - Clear the insertion point: created instructions will
107 /// not be inserted into a block.
108 void ClearInsertionPoint() {
112 BasicBlock *GetInsertBlock() const { return BB; }
114 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
116 /// SetInsertPoint - This specifies that created instructions should be
117 /// appended to the end of the specified block.
118 void SetInsertPoint(BasicBlock *TheBB) {
120 InsertPt = BB->end();
123 /// SetInsertPoint - This specifies that created instructions should be
124 /// inserted at the specified point.
125 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
130 /// Insert - Insert and return the specified instruction.
131 template<typename InstTy>
132 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
133 InsertHelper(I, Name, BB, InsertPt);
137 //===--------------------------------------------------------------------===//
138 // Type creation methods
139 //===--------------------------------------------------------------------===//
141 /// getInt1Ty - Fetch the type representing a single bit
142 const Type *getInt1Ty() {
143 return Type::getInt1Ty(Context);
146 /// getInt8Ty - Fetch the type representing an 8-bit integer.
147 const Type *getInt8Ty() {
148 return Type::getInt8Ty(Context);
151 /// getInt16Ty - Fetch the type representing a 16-bit integer.
152 const Type *getInt16Ty() {
153 return Type::getInt16Ty(Context);
156 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
157 const Type *getInt32Ty() {
158 return Type::getInt32Ty(Context);
161 /// getInt64Ty - Fetch the type representing a 64-bit integer.
162 const Type *getInt64Ty() {
163 return Type::getInt64Ty(Context);
166 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
167 const Type *getFloatTy() {
168 return Type::getFloatTy(Context);
171 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
172 const Type *getDoubleTy() {
173 return Type::getDoubleTy(Context);
176 /// getVoidTy - Fetch the type representing void.
177 const Type *getVoidTy() {
178 return Type::getVoidTy(Context);
181 //===--------------------------------------------------------------------===//
182 // Instruction creation methods: Terminators
183 //===--------------------------------------------------------------------===//
185 /// CreateRetVoid - Create a 'ret void' instruction.
186 ReturnInst *CreateRetVoid() {
187 return Insert(ReturnInst::Create(Context));
191 /// CreateRet - Create a 'ret <val>' instruction.
193 ReturnInst *CreateRet(Value *V) {
194 return Insert(ReturnInst::Create(Context, V));
197 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
198 /// with one Value from the retVals array each, that build a aggregate
199 /// return value one value at a time, and a ret instruction to return
200 /// the resulting aggregate value. This is a convenience function for
201 /// code that uses aggregate return values as a vehicle for having
202 /// multiple return values.
204 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
205 const Type *RetType = BB->getParent()->getReturnType();
206 Value *V = UndefValue::get(RetType);
207 for (unsigned i = 0; i != N; ++i)
208 V = CreateInsertValue(V, retVals[i], i, "mrv");
209 return Insert(ReturnInst::Create(Context, V));
212 /// CreateBr - Create an unconditional 'br label X' instruction.
213 BranchInst *CreateBr(BasicBlock *Dest) {
214 return Insert(BranchInst::Create(Dest));
217 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
219 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
220 return Insert(BranchInst::Create(True, False, Cond));
223 /// CreateSwitch - Create a switch instruction with the specified value,
224 /// default dest, and with a hint for the number of cases that will be added
225 /// (for efficient allocation).
226 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
227 return Insert(SwitchInst::Create(V, Dest, NumCases));
230 /// CreateInvoke - Create an invoke instruction.
231 template<typename InputIterator>
232 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
233 BasicBlock *UnwindDest, InputIterator ArgBegin,
234 InputIterator ArgEnd, const Twine &Name = "") {
235 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
236 ArgBegin, ArgEnd), Name);
239 UnwindInst *CreateUnwind() {
240 return Insert(new UnwindInst(Context));
243 UnreachableInst *CreateUnreachable() {
244 return Insert(new UnreachableInst(Context));
247 //===--------------------------------------------------------------------===//
248 // Instruction creation methods: Binary Operators
249 //===--------------------------------------------------------------------===//
251 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
252 if (Constant *LC = dyn_cast<Constant>(LHS))
253 if (Constant *RC = dyn_cast<Constant>(RHS))
254 return Folder.CreateAdd(LC, RC);
255 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
257 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
258 if (Constant *LC = dyn_cast<Constant>(LHS))
259 if (Constant *RC = dyn_cast<Constant>(RHS))
260 return Folder.CreateNSWAdd(LC, RC);
261 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
263 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
264 if (Constant *LC = dyn_cast<Constant>(LHS))
265 if (Constant *RC = dyn_cast<Constant>(RHS))
266 return Folder.CreateFAdd(LC, RC);
267 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
269 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
270 if (Constant *LC = dyn_cast<Constant>(LHS))
271 if (Constant *RC = dyn_cast<Constant>(RHS))
272 return Folder.CreateSub(LC, RC);
273 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
275 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
276 if (Constant *LC = dyn_cast<Constant>(LHS))
277 if (Constant *RC = dyn_cast<Constant>(RHS))
278 return Folder.CreateFSub(LC, RC);
279 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
281 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
282 if (Constant *LC = dyn_cast<Constant>(LHS))
283 if (Constant *RC = dyn_cast<Constant>(RHS))
284 return Folder.CreateMul(LC, RC);
285 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
287 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
288 if (Constant *LC = dyn_cast<Constant>(LHS))
289 if (Constant *RC = dyn_cast<Constant>(RHS))
290 return Folder.CreateFMul(LC, RC);
291 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
293 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
294 if (Constant *LC = dyn_cast<Constant>(LHS))
295 if (Constant *RC = dyn_cast<Constant>(RHS))
296 return Folder.CreateUDiv(LC, RC);
297 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
299 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
300 if (Constant *LC = dyn_cast<Constant>(LHS))
301 if (Constant *RC = dyn_cast<Constant>(RHS))
302 return Folder.CreateSDiv(LC, RC);
303 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
305 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
306 if (Constant *LC = dyn_cast<Constant>(LHS))
307 if (Constant *RC = dyn_cast<Constant>(RHS))
308 return Folder.CreateExactSDiv(LC, RC);
309 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
311 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
312 if (Constant *LC = dyn_cast<Constant>(LHS))
313 if (Constant *RC = dyn_cast<Constant>(RHS))
314 return Folder.CreateFDiv(LC, RC);
315 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
317 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
318 if (Constant *LC = dyn_cast<Constant>(LHS))
319 if (Constant *RC = dyn_cast<Constant>(RHS))
320 return Folder.CreateURem(LC, RC);
321 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
323 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
324 if (Constant *LC = dyn_cast<Constant>(LHS))
325 if (Constant *RC = dyn_cast<Constant>(RHS))
326 return Folder.CreateSRem(LC, RC);
327 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
329 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
330 if (Constant *LC = dyn_cast<Constant>(LHS))
331 if (Constant *RC = dyn_cast<Constant>(RHS))
332 return Folder.CreateFRem(LC, RC);
333 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
335 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
336 if (Constant *LC = dyn_cast<Constant>(LHS))
337 if (Constant *RC = dyn_cast<Constant>(RHS))
338 return Folder.CreateShl(LC, RC);
339 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
341 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
342 if (Constant *LC = dyn_cast<Constant>(LHS))
343 if (Constant *RC = dyn_cast<Constant>(RHS))
344 return Folder.CreateLShr(LC, RC);
345 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
347 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
348 if (Constant *LC = dyn_cast<Constant>(LHS))
349 if (Constant *RC = dyn_cast<Constant>(RHS))
350 return Folder.CreateAShr(LC, RC);
351 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
353 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
354 if (Constant *LC = dyn_cast<Constant>(LHS))
355 if (Constant *RC = dyn_cast<Constant>(RHS))
356 return Folder.CreateAnd(LC, RC);
357 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
359 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
360 if (Constant *LC = dyn_cast<Constant>(LHS))
361 if (Constant *RC = dyn_cast<Constant>(RHS))
362 return Folder.CreateOr(LC, RC);
363 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
365 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
366 if (Constant *LC = dyn_cast<Constant>(LHS))
367 if (Constant *RC = dyn_cast<Constant>(RHS))
368 return Folder.CreateXor(LC, RC);
369 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
372 Value *CreateBinOp(Instruction::BinaryOps Opc,
373 Value *LHS, Value *RHS, const Twine &Name = "") {
374 if (Constant *LC = dyn_cast<Constant>(LHS))
375 if (Constant *RC = dyn_cast<Constant>(RHS))
376 return Folder.CreateBinOp(Opc, LC, RC);
377 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
380 Value *CreateNeg(Value *V, const Twine &Name = "") {
381 if (Constant *VC = dyn_cast<Constant>(V))
382 return Folder.CreateNeg(VC);
383 return Insert(BinaryOperator::CreateNeg(V), Name);
385 Value *CreateFNeg(Value *V, const Twine &Name = "") {
386 if (Constant *VC = dyn_cast<Constant>(V))
387 return Folder.CreateFNeg(VC);
388 return Insert(BinaryOperator::CreateFNeg(V), Name);
390 Value *CreateNot(Value *V, const Twine &Name = "") {
391 if (Constant *VC = dyn_cast<Constant>(V))
392 return Folder.CreateNot(VC);
393 return Insert(BinaryOperator::CreateNot(V), Name);
396 //===--------------------------------------------------------------------===//
397 // Instruction creation methods: Memory Instructions
398 //===--------------------------------------------------------------------===//
400 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
401 const Twine &Name = "") {
402 return Insert(new MallocInst(Ty, ArraySize), Name);
404 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
405 const Twine &Name = "") {
406 return Insert(new AllocaInst(Ty, ArraySize), Name);
408 FreeInst *CreateFree(Value *Ptr) {
409 return Insert(new FreeInst(Ptr));
411 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
412 // converting the string to 'bool' for the isVolatile parameter.
413 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
414 return Insert(new LoadInst(Ptr), Name);
416 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
417 return Insert(new LoadInst(Ptr), Name);
419 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
420 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
422 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
423 return Insert(new StoreInst(Val, Ptr, isVolatile));
425 template<typename InputIterator>
426 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
427 const Twine &Name = "") {
428 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
429 // Every index must be constant.
431 for (i = IdxBegin; i < IdxEnd; ++i)
432 if (!isa<Constant>(*i))
435 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
437 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
439 template<typename InputIterator>
440 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
441 const Twine &Name = "") {
442 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
443 // Every index must be constant.
445 for (i = IdxBegin; i < IdxEnd; ++i)
446 if (!isa<Constant>(*i))
449 return Folder.CreateInBoundsGetElementPtr(PC,
453 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
456 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
457 if (Constant *PC = dyn_cast<Constant>(Ptr))
458 if (Constant *IC = dyn_cast<Constant>(Idx))
459 return Folder.CreateGetElementPtr(PC, &IC, 1);
460 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
462 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
463 if (Constant *PC = dyn_cast<Constant>(Ptr))
464 if (Constant *IC = dyn_cast<Constant>(Idx))
465 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
466 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
468 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
469 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
471 if (Constant *PC = dyn_cast<Constant>(Ptr))
472 return Folder.CreateGetElementPtr(PC, &Idx, 1);
474 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
476 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
477 const Twine &Name = "") {
478 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
480 if (Constant *PC = dyn_cast<Constant>(Ptr))
481 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
483 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
485 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
486 const Twine &Name = "") {
488 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
489 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
492 if (Constant *PC = dyn_cast<Constant>(Ptr))
493 return Folder.CreateGetElementPtr(PC, Idxs, 2);
495 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
497 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
498 const Twine &Name = "") {
500 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
501 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
504 if (Constant *PC = dyn_cast<Constant>(Ptr))
505 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
507 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
509 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
510 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
512 if (Constant *PC = dyn_cast<Constant>(Ptr))
513 return Folder.CreateGetElementPtr(PC, &Idx, 1);
515 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
517 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
518 const Twine &Name = "") {
519 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
521 if (Constant *PC = dyn_cast<Constant>(Ptr))
522 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
524 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
526 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
527 const Twine &Name = "") {
529 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
530 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
533 if (Constant *PC = dyn_cast<Constant>(Ptr))
534 return Folder.CreateGetElementPtr(PC, Idxs, 2);
536 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
538 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
539 const Twine &Name = "") {
541 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
542 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
545 if (Constant *PC = dyn_cast<Constant>(Ptr))
546 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
548 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
550 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
551 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
553 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
554 Constant *StrConstant = ConstantArray::get(Context, Str, true);
555 Module &M = *BB->getParent()->getParent();
556 GlobalVariable *gv = new GlobalVariable(M,
557 StrConstant->getType(),
559 GlobalValue::InternalLinkage,
567 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
568 Value *gv = CreateGlobalString(Str, Name);
569 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
570 Value *Args[] = { zero, zero };
571 return CreateInBoundsGEP(gv, Args, Args+2, Name);
573 //===--------------------------------------------------------------------===//
574 // Instruction creation methods: Cast/Conversion Operators
575 //===--------------------------------------------------------------------===//
577 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
578 return CreateCast(Instruction::Trunc, V, DestTy, Name);
580 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
581 return CreateCast(Instruction::ZExt, V, DestTy, Name);
583 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
584 return CreateCast(Instruction::SExt, V, DestTy, Name);
586 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
587 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
589 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
590 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
592 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
593 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
595 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
596 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
598 Value *CreateFPTrunc(Value *V, const Type *DestTy,
599 const Twine &Name = "") {
600 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
602 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
603 return CreateCast(Instruction::FPExt, V, DestTy, Name);
605 Value *CreatePtrToInt(Value *V, const Type *DestTy,
606 const Twine &Name = "") {
607 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
609 Value *CreateIntToPtr(Value *V, const Type *DestTy,
610 const Twine &Name = "") {
611 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
613 Value *CreateBitCast(Value *V, const Type *DestTy,
614 const Twine &Name = "") {
615 return CreateCast(Instruction::BitCast, V, DestTy, Name);
617 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
618 const Twine &Name = "") {
619 if (V->getType() == DestTy)
621 if (Constant *VC = dyn_cast<Constant>(V))
622 return Folder.CreateZExtOrBitCast(VC, DestTy);
623 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
625 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
626 const Twine &Name = "") {
627 if (V->getType() == DestTy)
629 if (Constant *VC = dyn_cast<Constant>(V))
630 return Folder.CreateSExtOrBitCast(VC, DestTy);
631 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
633 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
634 const Twine &Name = "") {
635 if (V->getType() == DestTy)
637 if (Constant *VC = dyn_cast<Constant>(V))
638 return Folder.CreateTruncOrBitCast(VC, DestTy);
639 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
641 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
642 const Twine &Name = "") {
643 if (V->getType() == DestTy)
645 if (Constant *VC = dyn_cast<Constant>(V))
646 return Folder.CreateCast(Op, VC, DestTy);
647 return Insert(CastInst::Create(Op, V, DestTy), Name);
649 Value *CreatePointerCast(Value *V, const Type *DestTy,
650 const Twine &Name = "") {
651 if (V->getType() == DestTy)
653 if (Constant *VC = dyn_cast<Constant>(V))
654 return Folder.CreatePointerCast(VC, DestTy);
655 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
657 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
658 const Twine &Name = "") {
659 if (V->getType() == DestTy)
661 if (Constant *VC = dyn_cast<Constant>(V))
662 return Folder.CreateIntCast(VC, DestTy, isSigned);
663 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
665 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
666 if (V->getType() == DestTy)
668 if (Constant *VC = dyn_cast<Constant>(V))
669 return Folder.CreateFPCast(VC, DestTy);
670 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
673 //===--------------------------------------------------------------------===//
674 // Instruction creation methods: Compare Instructions
675 //===--------------------------------------------------------------------===//
677 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
678 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
680 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
681 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
683 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
684 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
686 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
687 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
689 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
690 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
692 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
693 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
695 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
696 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
698 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
699 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
701 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
702 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
704 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
705 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
708 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
709 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
711 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
712 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
714 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
715 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
717 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
718 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
720 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
721 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
723 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
724 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
726 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
727 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
729 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
730 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
732 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
733 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
735 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
736 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
738 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
739 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
741 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
742 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
744 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
745 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
747 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
748 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
751 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
752 const Twine &Name = "") {
753 if (Constant *LC = dyn_cast<Constant>(LHS))
754 if (Constant *RC = dyn_cast<Constant>(RHS))
755 return Folder.CreateICmp(P, LC, RC);
756 return Insert(new ICmpInst(P, LHS, RHS), Name);
758 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
759 const Twine &Name = "") {
760 if (Constant *LC = dyn_cast<Constant>(LHS))
761 if (Constant *RC = dyn_cast<Constant>(RHS))
762 return Folder.CreateFCmp(P, LC, RC);
763 return Insert(new FCmpInst(P, LHS, RHS), Name);
766 //===--------------------------------------------------------------------===//
767 // Instruction creation methods: Other Instructions
768 //===--------------------------------------------------------------------===//
770 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
771 return Insert(PHINode::Create(Ty), Name);
774 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
775 return Insert(CallInst::Create(Callee), Name);
777 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
778 return Insert(CallInst::Create(Callee, Arg), Name);
780 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
781 const Twine &Name = "") {
782 Value *Args[] = { Arg1, Arg2 };
783 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
785 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
786 const Twine &Name = "") {
787 Value *Args[] = { Arg1, Arg2, Arg3 };
788 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
790 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
791 Value *Arg4, const Twine &Name = "") {
792 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
793 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
796 template<typename InputIterator>
797 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
798 InputIterator ArgEnd, const Twine &Name = "") {
799 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
802 Value *CreateSelect(Value *C, Value *True, Value *False,
803 const Twine &Name = "") {
804 if (Constant *CC = dyn_cast<Constant>(C))
805 if (Constant *TC = dyn_cast<Constant>(True))
806 if (Constant *FC = dyn_cast<Constant>(False))
807 return Folder.CreateSelect(CC, TC, FC);
808 return Insert(SelectInst::Create(C, True, False), Name);
811 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
812 return Insert(new VAArgInst(List, Ty), Name);
815 Value *CreateExtractElement(Value *Vec, Value *Idx,
816 const Twine &Name = "") {
817 if (Constant *VC = dyn_cast<Constant>(Vec))
818 if (Constant *IC = dyn_cast<Constant>(Idx))
819 return Folder.CreateExtractElement(VC, IC);
820 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
823 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
824 const Twine &Name = "") {
825 if (Constant *VC = dyn_cast<Constant>(Vec))
826 if (Constant *NC = dyn_cast<Constant>(NewElt))
827 if (Constant *IC = dyn_cast<Constant>(Idx))
828 return Folder.CreateInsertElement(VC, NC, IC);
829 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
832 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
833 const Twine &Name = "") {
834 if (Constant *V1C = dyn_cast<Constant>(V1))
835 if (Constant *V2C = dyn_cast<Constant>(V2))
836 if (Constant *MC = dyn_cast<Constant>(Mask))
837 return Folder.CreateShuffleVector(V1C, V2C, MC);
838 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
841 Value *CreateExtractValue(Value *Agg, unsigned Idx,
842 const Twine &Name = "") {
843 if (Constant *AggC = dyn_cast<Constant>(Agg))
844 return Folder.CreateExtractValue(AggC, &Idx, 1);
845 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
848 template<typename InputIterator>
849 Value *CreateExtractValue(Value *Agg,
850 InputIterator IdxBegin,
851 InputIterator IdxEnd,
852 const Twine &Name = "") {
853 if (Constant *AggC = dyn_cast<Constant>(Agg))
854 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
855 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
858 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
859 const Twine &Name = "") {
860 if (Constant *AggC = dyn_cast<Constant>(Agg))
861 if (Constant *ValC = dyn_cast<Constant>(Val))
862 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
863 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
866 template<typename InputIterator>
867 Value *CreateInsertValue(Value *Agg, Value *Val,
868 InputIterator IdxBegin,
869 InputIterator IdxEnd,
870 const Twine &Name = "") {
871 if (Constant *AggC = dyn_cast<Constant>(Agg))
872 if (Constant *ValC = dyn_cast<Constant>(Val))
873 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
874 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
877 //===--------------------------------------------------------------------===//
878 // Utility creation methods
879 //===--------------------------------------------------------------------===//
881 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
882 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
883 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
887 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
888 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
889 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
893 /// CreatePtrDiff - Return the i64 difference between two pointer values,
894 /// dividing out the size of the pointed-to objects. This is intended to
895 /// implement C-style pointer subtraction. As such, the pointers must be
896 /// appropriately aligned for their element types and pointing into the
898 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
899 assert(LHS->getType() == RHS->getType() &&
900 "Pointer subtraction operand types must match!");
901 const PointerType *ArgType = cast<PointerType>(LHS->getType());
902 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
903 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
904 Value *Difference = CreateSub(LHS_int, RHS_int);
905 return CreateExactSDiv(Difference,
906 ConstantExpr::getSizeOf(ArgType->getElementType()),