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/Metadata.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/ADT/Twine.h"
26 #include "llvm/Support/ConstantFolder.h"
30 /// IRBuilderDefaultInserter - This provides the default implementation of the
31 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
32 /// created by IRBuilder and needs to be inserted. By default, this inserts the
33 /// instruction at the insertion point.
34 template <bool preserveNames = true>
35 class IRBuilderDefaultInserter {
37 void InsertHelper(Instruction *I, const Twine &Name,
38 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
39 if (BB) BB->getInstList().insert(InsertPt, I);
46 /// IRBuilder - This provides a uniform API for creating instructions and
47 /// inserting them into a basic block: either at the end of a BasicBlock, or
48 /// at a specific iterator location in a block.
50 /// Note that the builder does not expose the full generality of LLVM
51 /// instructions. For access to extra instruction properties, use the mutators
52 /// (e.g. setVolatile) on the instructions after they have been created.
53 /// The first template argument handles whether or not to preserve names in the
54 /// final instruction output. This defaults to on. The second template argument
55 /// specifies a class to use for creating constants. This defaults to creating
56 /// minimally folded constants. The fourth template argument allows clients to
57 /// specify custom insertion hooks that are called on every newly created
59 template<bool preserveNames = true, typename T = ConstantFolder,
60 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
61 class IRBuilder : public Inserter {
63 BasicBlock::iterator InsertPt;
65 MDNode *CurDbgLocation;
69 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
70 : Inserter(I), MDKind(0), CurDbgLocation(0), Context(C), Folder(F) {
71 ClearInsertionPoint();
74 explicit IRBuilder(LLVMContext &C)
75 : MDKind(0), CurDbgLocation(0), Context(C), Folder(C) {
76 ClearInsertionPoint();
79 explicit IRBuilder(BasicBlock *TheBB, const T &F)
80 : MDKind(0), CurDbgLocation(0), Context(TheBB->getContext()), Folder(F) {
81 SetInsertPoint(TheBB);
84 explicit IRBuilder(BasicBlock *TheBB)
85 : MDKind(0), CurDbgLocation(0), Context(TheBB->getContext()),
87 SetInsertPoint(TheBB);
90 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
91 : MDKind(0), CurDbgLocation(0), Context(TheBB->getContext()), Folder(F) {
92 SetInsertPoint(TheBB, IP);
95 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
96 : MDKind(0), CurDbgLocation(0), Context(TheBB->getContext()),
98 SetInsertPoint(TheBB, IP);
101 /// getFolder - Get the constant folder being used.
102 const T &getFolder() { return Folder; }
104 /// isNamePreserving - Return true if this builder is configured to actually
105 /// add the requested names to IR created through it.
106 bool isNamePreserving() const { return preserveNames; }
108 //===--------------------------------------------------------------------===//
109 // Builder configuration methods
110 //===--------------------------------------------------------------------===//
112 /// ClearInsertionPoint - Clear the insertion point: created instructions will
113 /// not be inserted into a block.
114 void ClearInsertionPoint() {
118 BasicBlock *GetInsertBlock() const { return BB; }
120 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
122 /// SetInsertPoint - This specifies that created instructions should be
123 /// appended to the end of the specified block.
124 void SetInsertPoint(BasicBlock *TheBB) {
126 InsertPt = BB->end();
129 /// SetInsertPoint - This specifies that created instructions should be
130 /// inserted at the specified point.
131 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
136 /// SetCurrentDebugLocation - Set location information used by debugging
138 void SetCurrentDebugLocation(MDNode *L) {
140 MDKind = Context.getMetadata().getMDKind("dbg");
142 MDKind = Context.getMetadata().RegisterMDKind("dbg");
146 MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
148 /// SetDebugLocation - Set location information for the given instruction.
149 void SetDebugLocation(Instruction *I) {
151 Context.getMetadata().setMD(MDKind, CurDbgLocation, I);
154 /// Insert - Insert and return the specified instruction.
155 template<typename InstTy>
156 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
157 this->InsertHelper(I, Name, BB, InsertPt);
159 Context.getMetadata().setMD(MDKind, CurDbgLocation, I);
163 //===--------------------------------------------------------------------===//
164 // Type creation methods
165 //===--------------------------------------------------------------------===//
167 /// getInt1Ty - Fetch the type representing a single bit
168 const Type *getInt1Ty() {
169 return Type::getInt1Ty(Context);
172 /// getInt8Ty - Fetch the type representing an 8-bit integer.
173 const Type *getInt8Ty() {
174 return Type::getInt8Ty(Context);
177 /// getInt16Ty - Fetch the type representing a 16-bit integer.
178 const Type *getInt16Ty() {
179 return Type::getInt16Ty(Context);
182 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
183 const Type *getInt32Ty() {
184 return Type::getInt32Ty(Context);
187 /// getInt64Ty - Fetch the type representing a 64-bit integer.
188 const Type *getInt64Ty() {
189 return Type::getInt64Ty(Context);
192 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
193 const Type *getFloatTy() {
194 return Type::getFloatTy(Context);
197 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
198 const Type *getDoubleTy() {
199 return Type::getDoubleTy(Context);
202 /// getVoidTy - Fetch the type representing void.
203 const Type *getVoidTy() {
204 return Type::getVoidTy(Context);
207 //===--------------------------------------------------------------------===//
208 // Instruction creation methods: Terminators
209 //===--------------------------------------------------------------------===//
211 /// CreateRetVoid - Create a 'ret void' instruction.
212 ReturnInst *CreateRetVoid() {
213 return Insert(ReturnInst::Create(Context));
217 /// CreateRet - Create a 'ret <val>' instruction.
219 ReturnInst *CreateRet(Value *V) {
220 return Insert(ReturnInst::Create(Context, V));
223 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
224 /// with one Value from the retVals array each, that build a aggregate
225 /// return value one value at a time, and a ret instruction to return
226 /// the resulting aggregate value. This is a convenience function for
227 /// code that uses aggregate return values as a vehicle for having
228 /// multiple return values.
230 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
231 const Type *RetType = BB->getParent()->getReturnType();
232 Value *V = UndefValue::get(RetType);
233 for (unsigned i = 0; i != N; ++i)
234 V = CreateInsertValue(V, retVals[i], i, "mrv");
235 return Insert(ReturnInst::Create(Context, V));
238 /// CreateBr - Create an unconditional 'br label X' instruction.
239 BranchInst *CreateBr(BasicBlock *Dest) {
240 return Insert(BranchInst::Create(Dest));
243 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
245 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
246 return Insert(BranchInst::Create(True, False, Cond));
249 /// CreateSwitch - Create a switch instruction with the specified value,
250 /// default dest, and with a hint for the number of cases that will be added
251 /// (for efficient allocation).
252 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
253 return Insert(SwitchInst::Create(V, Dest, NumCases));
256 /// CreateInvoke - Create an invoke instruction.
257 template<typename InputIterator>
258 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
259 BasicBlock *UnwindDest, InputIterator ArgBegin,
260 InputIterator ArgEnd, const Twine &Name = "") {
261 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
262 ArgBegin, ArgEnd), Name);
265 UnwindInst *CreateUnwind() {
266 return Insert(new UnwindInst(Context));
269 UnreachableInst *CreateUnreachable() {
270 return Insert(new UnreachableInst(Context));
273 //===--------------------------------------------------------------------===//
274 // Instruction creation methods: Binary Operators
275 //===--------------------------------------------------------------------===//
277 Value *CreateAdd(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.CreateAdd(LC, RC);
281 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
283 Value *CreateNSWAdd(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.CreateNSWAdd(LC, RC);
287 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
289 Value *CreateFAdd(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.CreateFAdd(LC, RC);
293 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
295 Value *CreateSub(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.CreateSub(LC, RC);
299 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
301 Value *CreateFSub(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.CreateFSub(LC, RC);
305 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
307 Value *CreateMul(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.CreateMul(LC, RC);
311 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
313 Value *CreateFMul(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.CreateFMul(LC, RC);
317 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
319 Value *CreateUDiv(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.CreateUDiv(LC, RC);
323 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
325 Value *CreateSDiv(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.CreateSDiv(LC, RC);
329 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
331 Value *CreateExactSDiv(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.CreateExactSDiv(LC, RC);
335 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
337 Value *CreateFDiv(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.CreateFDiv(LC, RC);
341 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
343 Value *CreateURem(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.CreateURem(LC, RC);
347 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
349 Value *CreateSRem(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.CreateSRem(LC, RC);
353 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
355 Value *CreateFRem(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.CreateFRem(LC, RC);
359 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
361 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
362 if (Constant *LC = dyn_cast<Constant>(LHS))
363 if (Constant *RC = dyn_cast<Constant>(RHS))
364 return Folder.CreateShl(LC, RC);
365 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
367 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
368 if (Constant *LC = dyn_cast<Constant>(LHS))
369 if (Constant *RC = dyn_cast<Constant>(RHS))
370 return Folder.CreateLShr(LC, RC);
371 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
373 Value *CreateAShr(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.CreateAShr(LC, RC);
377 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
379 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
380 if (Constant *LC = dyn_cast<Constant>(LHS))
381 if (Constant *RC = dyn_cast<Constant>(RHS))
382 return Folder.CreateAnd(LC, RC);
383 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
385 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
386 if (Constant *LC = dyn_cast<Constant>(LHS))
387 if (Constant *RC = dyn_cast<Constant>(RHS))
388 return Folder.CreateOr(LC, RC);
389 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
391 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
392 if (Constant *LC = dyn_cast<Constant>(LHS))
393 if (Constant *RC = dyn_cast<Constant>(RHS))
394 return Folder.CreateXor(LC, RC);
395 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
398 Value *CreateBinOp(Instruction::BinaryOps Opc,
399 Value *LHS, Value *RHS, const Twine &Name = "") {
400 if (Constant *LC = dyn_cast<Constant>(LHS))
401 if (Constant *RC = dyn_cast<Constant>(RHS))
402 return Folder.CreateBinOp(Opc, LC, RC);
403 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
406 Value *CreateNeg(Value *V, const Twine &Name = "") {
407 if (Constant *VC = dyn_cast<Constant>(V))
408 return Folder.CreateNeg(VC);
409 return Insert(BinaryOperator::CreateNeg(V), Name);
411 Value *CreateFNeg(Value *V, const Twine &Name = "") {
412 if (Constant *VC = dyn_cast<Constant>(V))
413 return Folder.CreateFNeg(VC);
414 return Insert(BinaryOperator::CreateFNeg(V), Name);
416 Value *CreateNot(Value *V, const Twine &Name = "") {
417 if (Constant *VC = dyn_cast<Constant>(V))
418 return Folder.CreateNot(VC);
419 return Insert(BinaryOperator::CreateNot(V), Name);
422 //===--------------------------------------------------------------------===//
423 // Instruction creation methods: Memory Instructions
424 //===--------------------------------------------------------------------===//
426 MallocInst *CreateMalloc(const Type *Ty, Value *ArraySize = 0,
427 const Twine &Name = "") {
428 return Insert(new MallocInst(Ty, ArraySize), Name);
430 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
431 const Twine &Name = "") {
432 return Insert(new AllocaInst(Ty, ArraySize), Name);
434 FreeInst *CreateFree(Value *Ptr) {
435 return Insert(new FreeInst(Ptr));
437 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
438 // converting the string to 'bool' for the isVolatile parameter.
439 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
440 return Insert(new LoadInst(Ptr), Name);
442 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
443 return Insert(new LoadInst(Ptr), Name);
445 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
446 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
448 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
449 return Insert(new StoreInst(Val, Ptr, isVolatile));
451 template<typename InputIterator>
452 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
453 const Twine &Name = "") {
454 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
455 // Every index must be constant.
457 for (i = IdxBegin; i < IdxEnd; ++i)
458 if (!isa<Constant>(*i))
461 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
463 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
465 template<typename InputIterator>
466 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
467 const Twine &Name = "") {
468 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
469 // Every index must be constant.
471 for (i = IdxBegin; i < IdxEnd; ++i)
472 if (!isa<Constant>(*i))
475 return Folder.CreateInBoundsGetElementPtr(PC,
479 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
482 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
483 if (Constant *PC = dyn_cast<Constant>(Ptr))
484 if (Constant *IC = dyn_cast<Constant>(Idx))
485 return Folder.CreateGetElementPtr(PC, &IC, 1);
486 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
488 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
489 if (Constant *PC = dyn_cast<Constant>(Ptr))
490 if (Constant *IC = dyn_cast<Constant>(Idx))
491 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
492 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
494 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
495 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
497 if (Constant *PC = dyn_cast<Constant>(Ptr))
498 return Folder.CreateGetElementPtr(PC, &Idx, 1);
500 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
502 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
503 const Twine &Name = "") {
504 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
506 if (Constant *PC = dyn_cast<Constant>(Ptr))
507 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
509 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
511 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
512 const Twine &Name = "") {
514 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
515 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
518 if (Constant *PC = dyn_cast<Constant>(Ptr))
519 return Folder.CreateGetElementPtr(PC, Idxs, 2);
521 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
523 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
524 const Twine &Name = "") {
526 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
527 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
530 if (Constant *PC = dyn_cast<Constant>(Ptr))
531 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
533 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
535 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
536 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
538 if (Constant *PC = dyn_cast<Constant>(Ptr))
539 return Folder.CreateGetElementPtr(PC, &Idx, 1);
541 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
543 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
544 const Twine &Name = "") {
545 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
547 if (Constant *PC = dyn_cast<Constant>(Ptr))
548 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
550 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
552 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
553 const Twine &Name = "") {
555 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
556 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
559 if (Constant *PC = dyn_cast<Constant>(Ptr))
560 return Folder.CreateGetElementPtr(PC, Idxs, 2);
562 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
564 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
565 const Twine &Name = "") {
567 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
568 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
571 if (Constant *PC = dyn_cast<Constant>(Ptr))
572 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
574 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
576 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
577 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
579 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
580 Constant *StrConstant = ConstantArray::get(Context, Str, true);
581 Module &M = *BB->getParent()->getParent();
582 GlobalVariable *gv = new GlobalVariable(M,
583 StrConstant->getType(),
585 GlobalValue::InternalLinkage,
593 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
594 Value *gv = CreateGlobalString(Str, Name);
595 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
596 Value *Args[] = { zero, zero };
597 return CreateInBoundsGEP(gv, Args, Args+2, Name);
599 //===--------------------------------------------------------------------===//
600 // Instruction creation methods: Cast/Conversion Operators
601 //===--------------------------------------------------------------------===//
603 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
604 return CreateCast(Instruction::Trunc, V, DestTy, Name);
606 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
607 return CreateCast(Instruction::ZExt, V, DestTy, Name);
609 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
610 return CreateCast(Instruction::SExt, V, DestTy, Name);
612 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
613 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
615 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
616 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
618 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
619 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
621 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
622 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
624 Value *CreateFPTrunc(Value *V, const Type *DestTy,
625 const Twine &Name = "") {
626 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
628 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
629 return CreateCast(Instruction::FPExt, V, DestTy, Name);
631 Value *CreatePtrToInt(Value *V, const Type *DestTy,
632 const Twine &Name = "") {
633 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
635 Value *CreateIntToPtr(Value *V, const Type *DestTy,
636 const Twine &Name = "") {
637 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
639 Value *CreateBitCast(Value *V, const Type *DestTy,
640 const Twine &Name = "") {
641 return CreateCast(Instruction::BitCast, V, DestTy, Name);
643 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
644 const Twine &Name = "") {
645 if (V->getType() == DestTy)
647 if (Constant *VC = dyn_cast<Constant>(V))
648 return Folder.CreateZExtOrBitCast(VC, DestTy);
649 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
651 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
652 const Twine &Name = "") {
653 if (V->getType() == DestTy)
655 if (Constant *VC = dyn_cast<Constant>(V))
656 return Folder.CreateSExtOrBitCast(VC, DestTy);
657 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
659 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
660 const Twine &Name = "") {
661 if (V->getType() == DestTy)
663 if (Constant *VC = dyn_cast<Constant>(V))
664 return Folder.CreateTruncOrBitCast(VC, DestTy);
665 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
667 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
668 const Twine &Name = "") {
669 if (V->getType() == DestTy)
671 if (Constant *VC = dyn_cast<Constant>(V))
672 return Folder.CreateCast(Op, VC, DestTy);
673 return Insert(CastInst::Create(Op, V, DestTy), Name);
675 Value *CreatePointerCast(Value *V, const Type *DestTy,
676 const Twine &Name = "") {
677 if (V->getType() == DestTy)
679 if (Constant *VC = dyn_cast<Constant>(V))
680 return Folder.CreatePointerCast(VC, DestTy);
681 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
683 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
684 const Twine &Name = "") {
685 if (V->getType() == DestTy)
687 if (Constant *VC = dyn_cast<Constant>(V))
688 return Folder.CreateIntCast(VC, DestTy, isSigned);
689 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
691 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
692 if (V->getType() == DestTy)
694 if (Constant *VC = dyn_cast<Constant>(V))
695 return Folder.CreateFPCast(VC, DestTy);
696 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
699 //===--------------------------------------------------------------------===//
700 // Instruction creation methods: Compare Instructions
701 //===--------------------------------------------------------------------===//
703 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
704 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
706 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
707 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
709 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
710 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
712 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
713 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
715 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
716 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
718 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
719 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
721 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
722 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
724 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
725 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
727 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
728 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
730 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
731 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
734 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
735 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
737 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
738 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
740 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
741 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
743 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
744 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
746 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
747 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
749 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
750 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
752 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
753 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
755 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
756 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
758 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
759 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
761 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
762 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
764 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
765 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
767 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
768 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
770 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
771 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
773 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
774 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
777 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
778 const Twine &Name = "") {
779 if (Constant *LC = dyn_cast<Constant>(LHS))
780 if (Constant *RC = dyn_cast<Constant>(RHS))
781 return Folder.CreateICmp(P, LC, RC);
782 return Insert(new ICmpInst(P, LHS, RHS), Name);
784 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
785 const Twine &Name = "") {
786 if (Constant *LC = dyn_cast<Constant>(LHS))
787 if (Constant *RC = dyn_cast<Constant>(RHS))
788 return Folder.CreateFCmp(P, LC, RC);
789 return Insert(new FCmpInst(P, LHS, RHS), Name);
792 //===--------------------------------------------------------------------===//
793 // Instruction creation methods: Other Instructions
794 //===--------------------------------------------------------------------===//
796 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
797 return Insert(PHINode::Create(Ty), Name);
800 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
801 return Insert(CallInst::Create(Callee), Name);
803 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
804 return Insert(CallInst::Create(Callee, Arg), Name);
806 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
807 const Twine &Name = "") {
808 Value *Args[] = { Arg1, Arg2 };
809 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
811 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
812 const Twine &Name = "") {
813 Value *Args[] = { Arg1, Arg2, Arg3 };
814 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
816 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
817 Value *Arg4, const Twine &Name = "") {
818 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
819 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
822 template<typename InputIterator>
823 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
824 InputIterator ArgEnd, const Twine &Name = "") {
825 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
828 Value *CreateSelect(Value *C, Value *True, Value *False,
829 const Twine &Name = "") {
830 if (Constant *CC = dyn_cast<Constant>(C))
831 if (Constant *TC = dyn_cast<Constant>(True))
832 if (Constant *FC = dyn_cast<Constant>(False))
833 return Folder.CreateSelect(CC, TC, FC);
834 return Insert(SelectInst::Create(C, True, False), Name);
837 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
838 return Insert(new VAArgInst(List, Ty), Name);
841 Value *CreateExtractElement(Value *Vec, Value *Idx,
842 const Twine &Name = "") {
843 if (Constant *VC = dyn_cast<Constant>(Vec))
844 if (Constant *IC = dyn_cast<Constant>(Idx))
845 return Folder.CreateExtractElement(VC, IC);
846 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
849 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
850 const Twine &Name = "") {
851 if (Constant *VC = dyn_cast<Constant>(Vec))
852 if (Constant *NC = dyn_cast<Constant>(NewElt))
853 if (Constant *IC = dyn_cast<Constant>(Idx))
854 return Folder.CreateInsertElement(VC, NC, IC);
855 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
858 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
859 const Twine &Name = "") {
860 if (Constant *V1C = dyn_cast<Constant>(V1))
861 if (Constant *V2C = dyn_cast<Constant>(V2))
862 if (Constant *MC = dyn_cast<Constant>(Mask))
863 return Folder.CreateShuffleVector(V1C, V2C, MC);
864 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
867 Value *CreateExtractValue(Value *Agg, unsigned Idx,
868 const Twine &Name = "") {
869 if (Constant *AggC = dyn_cast<Constant>(Agg))
870 return Folder.CreateExtractValue(AggC, &Idx, 1);
871 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
874 template<typename InputIterator>
875 Value *CreateExtractValue(Value *Agg,
876 InputIterator IdxBegin,
877 InputIterator IdxEnd,
878 const Twine &Name = "") {
879 if (Constant *AggC = dyn_cast<Constant>(Agg))
880 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
881 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
884 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
885 const Twine &Name = "") {
886 if (Constant *AggC = dyn_cast<Constant>(Agg))
887 if (Constant *ValC = dyn_cast<Constant>(Val))
888 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
889 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
892 template<typename InputIterator>
893 Value *CreateInsertValue(Value *Agg, Value *Val,
894 InputIterator IdxBegin,
895 InputIterator IdxEnd,
896 const Twine &Name = "") {
897 if (Constant *AggC = dyn_cast<Constant>(Agg))
898 if (Constant *ValC = dyn_cast<Constant>(Val))
899 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
900 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
903 //===--------------------------------------------------------------------===//
904 // Utility creation methods
905 //===--------------------------------------------------------------------===//
907 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
908 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
909 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
913 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
914 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
915 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
919 /// CreatePtrDiff - Return the i64 difference between two pointer values,
920 /// dividing out the size of the pointed-to objects. This is intended to
921 /// implement C-style pointer subtraction. As such, the pointers must be
922 /// appropriately aligned for their element types and pointing into the
924 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
925 assert(LHS->getType() == RHS->getType() &&
926 "Pointer subtraction operand types must match!");
927 const PointerType *ArgType = cast<PointerType>(LHS->getType());
928 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
929 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
930 Value *Difference = CreateSub(LHS_int, RHS_int);
931 return CreateExactSDiv(Difference,
932 ConstantExpr::getSizeOf(ArgType->getElementType()),