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().getMDKindID("dbg");
144 MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
146 /// SetDebugLocation - Set location information for the given instruction.
147 void SetDebugLocation(Instruction *I) {
149 Context.getMetadata().addMD(MDKind, CurDbgLocation, I);
152 /// SetDebugLocation - Set location information for the given instruction.
153 void SetDebugLocation(Instruction *I, MDNode *Loc) {
155 MDKind = Context.getMetadata().getMDKindID("dbg");
156 Context.getMetadata().addMD(MDKind, Loc, I);
159 /// Insert - Insert and return the specified instruction.
160 template<typename InstTy>
161 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
162 this->InsertHelper(I, Name, BB, InsertPt);
164 Context.getMetadata().addMD(MDKind, CurDbgLocation, I);
168 //===--------------------------------------------------------------------===//
169 // Type creation methods
170 //===--------------------------------------------------------------------===//
172 /// getInt1Ty - Fetch the type representing a single bit
173 const Type *getInt1Ty() {
174 return Type::getInt1Ty(Context);
177 /// getInt8Ty - Fetch the type representing an 8-bit integer.
178 const Type *getInt8Ty() {
179 return Type::getInt8Ty(Context);
182 /// getInt16Ty - Fetch the type representing a 16-bit integer.
183 const Type *getInt16Ty() {
184 return Type::getInt16Ty(Context);
187 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
188 const Type *getInt32Ty() {
189 return Type::getInt32Ty(Context);
192 /// getInt64Ty - Fetch the type representing a 64-bit integer.
193 const Type *getInt64Ty() {
194 return Type::getInt64Ty(Context);
197 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
198 const Type *getFloatTy() {
199 return Type::getFloatTy(Context);
202 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
203 const Type *getDoubleTy() {
204 return Type::getDoubleTy(Context);
207 /// getVoidTy - Fetch the type representing void.
208 const Type *getVoidTy() {
209 return Type::getVoidTy(Context);
212 //===--------------------------------------------------------------------===//
213 // Instruction creation methods: Terminators
214 //===--------------------------------------------------------------------===//
216 /// CreateRetVoid - Create a 'ret void' instruction.
217 ReturnInst *CreateRetVoid() {
218 return Insert(ReturnInst::Create(Context));
222 /// CreateRet - Create a 'ret <val>' instruction.
224 ReturnInst *CreateRet(Value *V) {
225 return Insert(ReturnInst::Create(Context, V));
228 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
229 /// with one Value from the retVals array each, that build a aggregate
230 /// return value one value at a time, and a ret instruction to return
231 /// the resulting aggregate value. This is a convenience function for
232 /// code that uses aggregate return values as a vehicle for having
233 /// multiple return values.
235 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
236 const Type *RetType = BB->getParent()->getReturnType();
237 Value *V = UndefValue::get(RetType);
238 for (unsigned i = 0; i != N; ++i)
239 V = CreateInsertValue(V, retVals[i], i, "mrv");
240 return Insert(ReturnInst::Create(Context, V));
243 /// CreateBr - Create an unconditional 'br label X' instruction.
244 BranchInst *CreateBr(BasicBlock *Dest) {
245 return Insert(BranchInst::Create(Dest));
248 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
250 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
251 return Insert(BranchInst::Create(True, False, Cond));
254 /// CreateSwitch - Create a switch instruction with the specified value,
255 /// default dest, and with a hint for the number of cases that will be added
256 /// (for efficient allocation).
257 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
258 return Insert(SwitchInst::Create(V, Dest, NumCases));
261 /// CreateIndirectBr - Create an indirect branch instruction with the
262 /// specified address operand, with an optional hint for the number of
263 /// destinations that will be added (for efficient allocation).
264 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
265 return Insert(IndirectBrInst::Create(Addr, NumDests));
268 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
269 BasicBlock *UnwindDest, const Twine &Name = "") {
270 Value *Args[] = { 0 };
271 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
274 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
275 BasicBlock *UnwindDest, Value *Arg1,
276 const Twine &Name = "") {
277 Value *Args[] = { Arg1 };
278 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
281 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
282 BasicBlock *UnwindDest, Value *Arg1,
283 Value *Arg2, Value *Arg3,
284 const Twine &Name = "") {
285 Value *Args[] = { Arg1, Arg2, Arg3 };
286 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
289 /// CreateInvoke - Create an invoke instruction.
290 template<typename InputIterator>
291 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
292 BasicBlock *UnwindDest, InputIterator ArgBegin,
293 InputIterator ArgEnd, const Twine &Name = "") {
294 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
295 ArgBegin, ArgEnd), Name);
298 UnwindInst *CreateUnwind() {
299 return Insert(new UnwindInst(Context));
302 UnreachableInst *CreateUnreachable() {
303 return Insert(new UnreachableInst(Context));
306 //===--------------------------------------------------------------------===//
307 // Instruction creation methods: Binary Operators
308 //===--------------------------------------------------------------------===//
310 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
311 if (Constant *LC = dyn_cast<Constant>(LHS))
312 if (Constant *RC = dyn_cast<Constant>(RHS))
313 return Folder.CreateAdd(LC, RC);
314 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
316 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
317 if (Constant *LC = dyn_cast<Constant>(LHS))
318 if (Constant *RC = dyn_cast<Constant>(RHS))
319 return Folder.CreateNSWAdd(LC, RC);
320 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
322 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
323 if (Constant *LC = dyn_cast<Constant>(LHS))
324 if (Constant *RC = dyn_cast<Constant>(RHS))
325 return Folder.CreateFAdd(LC, RC);
326 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
328 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
329 if (Constant *LC = dyn_cast<Constant>(LHS))
330 if (Constant *RC = dyn_cast<Constant>(RHS))
331 return Folder.CreateSub(LC, RC);
332 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
334 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
335 if (Constant *LC = dyn_cast<Constant>(LHS))
336 if (Constant *RC = dyn_cast<Constant>(RHS))
337 return Folder.CreateNSWSub(LC, RC);
338 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
340 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
341 if (Constant *LC = dyn_cast<Constant>(LHS))
342 if (Constant *RC = dyn_cast<Constant>(RHS))
343 return Folder.CreateFSub(LC, RC);
344 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
346 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
347 if (Constant *LC = dyn_cast<Constant>(LHS))
348 if (Constant *RC = dyn_cast<Constant>(RHS))
349 return Folder.CreateMul(LC, RC);
350 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
352 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
353 if (Constant *LC = dyn_cast<Constant>(LHS))
354 if (Constant *RC = dyn_cast<Constant>(RHS))
355 return Folder.CreateNSWMul(LC, RC);
356 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
358 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
359 if (Constant *LC = dyn_cast<Constant>(LHS))
360 if (Constant *RC = dyn_cast<Constant>(RHS))
361 return Folder.CreateFMul(LC, RC);
362 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
364 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
365 if (Constant *LC = dyn_cast<Constant>(LHS))
366 if (Constant *RC = dyn_cast<Constant>(RHS))
367 return Folder.CreateUDiv(LC, RC);
368 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
370 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
371 if (Constant *LC = dyn_cast<Constant>(LHS))
372 if (Constant *RC = dyn_cast<Constant>(RHS))
373 return Folder.CreateSDiv(LC, RC);
374 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
376 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
377 if (Constant *LC = dyn_cast<Constant>(LHS))
378 if (Constant *RC = dyn_cast<Constant>(RHS))
379 return Folder.CreateExactSDiv(LC, RC);
380 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
382 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
383 if (Constant *LC = dyn_cast<Constant>(LHS))
384 if (Constant *RC = dyn_cast<Constant>(RHS))
385 return Folder.CreateFDiv(LC, RC);
386 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
388 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
389 if (Constant *LC = dyn_cast<Constant>(LHS))
390 if (Constant *RC = dyn_cast<Constant>(RHS))
391 return Folder.CreateURem(LC, RC);
392 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
394 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
395 if (Constant *LC = dyn_cast<Constant>(LHS))
396 if (Constant *RC = dyn_cast<Constant>(RHS))
397 return Folder.CreateSRem(LC, RC);
398 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
400 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
401 if (Constant *LC = dyn_cast<Constant>(LHS))
402 if (Constant *RC = dyn_cast<Constant>(RHS))
403 return Folder.CreateFRem(LC, RC);
404 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
406 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
407 if (Constant *LC = dyn_cast<Constant>(LHS))
408 if (Constant *RC = dyn_cast<Constant>(RHS))
409 return Folder.CreateShl(LC, RC);
410 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
412 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
413 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
414 if (Constant *LC = dyn_cast<Constant>(LHS))
415 return Folder.CreateShl(LC, RHSC);
416 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
419 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
420 if (Constant *LC = dyn_cast<Constant>(LHS))
421 if (Constant *RC = dyn_cast<Constant>(RHS))
422 return Folder.CreateLShr(LC, RC);
423 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
425 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
426 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
427 if (Constant *LC = dyn_cast<Constant>(LHS))
428 return Folder.CreateLShr(LC, RHSC);
429 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
432 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
433 if (Constant *LC = dyn_cast<Constant>(LHS))
434 if (Constant *RC = dyn_cast<Constant>(RHS))
435 return Folder.CreateAShr(LC, RC);
436 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
438 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
439 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
440 if (Constant *LC = dyn_cast<Constant>(LHS))
441 return Folder.CreateSShr(LC, RHSC);
442 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
445 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
446 if (Constant *RC = dyn_cast<Constant>(RHS)) {
447 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
448 return LHS; // LHS & -1 -> LHS
449 if (Constant *LC = dyn_cast<Constant>(LHS))
450 return Folder.CreateAnd(LC, RC);
452 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
454 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
455 if (Constant *RC = dyn_cast<Constant>(RHS)) {
456 if (RC->isNullValue())
457 return LHS; // LHS | 0 -> LHS
458 if (Constant *LC = dyn_cast<Constant>(LHS))
459 return Folder.CreateOr(LC, RC);
461 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
463 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
464 if (Constant *LC = dyn_cast<Constant>(LHS))
465 if (Constant *RC = dyn_cast<Constant>(RHS))
466 return Folder.CreateXor(LC, RC);
467 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
470 Value *CreateBinOp(Instruction::BinaryOps Opc,
471 Value *LHS, Value *RHS, const Twine &Name = "") {
472 if (Constant *LC = dyn_cast<Constant>(LHS))
473 if (Constant *RC = dyn_cast<Constant>(RHS))
474 return Folder.CreateBinOp(Opc, LC, RC);
475 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
478 Value *CreateNeg(Value *V, const Twine &Name = "") {
479 if (Constant *VC = dyn_cast<Constant>(V))
480 return Folder.CreateNeg(VC);
481 return Insert(BinaryOperator::CreateNeg(V), Name);
483 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
484 if (Constant *VC = dyn_cast<Constant>(V))
485 return Folder.CreateNSWNeg(VC);
486 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
488 Value *CreateFNeg(Value *V, const Twine &Name = "") {
489 if (Constant *VC = dyn_cast<Constant>(V))
490 return Folder.CreateFNeg(VC);
491 return Insert(BinaryOperator::CreateFNeg(V), Name);
493 Value *CreateNot(Value *V, const Twine &Name = "") {
494 if (Constant *VC = dyn_cast<Constant>(V))
495 return Folder.CreateNot(VC);
496 return Insert(BinaryOperator::CreateNot(V), Name);
499 //===--------------------------------------------------------------------===//
500 // Instruction creation methods: Memory Instructions
501 //===--------------------------------------------------------------------===//
503 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
504 const Twine &Name = "") {
505 return Insert(new AllocaInst(Ty, ArraySize), Name);
507 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
508 // converting the string to 'bool' for the isVolatile parameter.
509 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
510 return Insert(new LoadInst(Ptr), Name);
512 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
513 return Insert(new LoadInst(Ptr), Name);
515 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
516 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
518 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
519 return Insert(new StoreInst(Val, Ptr, isVolatile));
521 template<typename InputIterator>
522 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
523 const Twine &Name = "") {
524 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
525 // Every index must be constant.
527 for (i = IdxBegin; i < IdxEnd; ++i)
528 if (!isa<Constant>(*i))
531 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
533 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
535 template<typename InputIterator>
536 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
537 const Twine &Name = "") {
538 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
539 // Every index must be constant.
541 for (i = IdxBegin; i < IdxEnd; ++i)
542 if (!isa<Constant>(*i))
545 return Folder.CreateInBoundsGetElementPtr(PC,
549 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
552 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
553 if (Constant *PC = dyn_cast<Constant>(Ptr))
554 if (Constant *IC = dyn_cast<Constant>(Idx))
555 return Folder.CreateGetElementPtr(PC, &IC, 1);
556 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
558 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
559 if (Constant *PC = dyn_cast<Constant>(Ptr))
560 if (Constant *IC = dyn_cast<Constant>(Idx))
561 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
562 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
564 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
565 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
567 if (Constant *PC = dyn_cast<Constant>(Ptr))
568 return Folder.CreateGetElementPtr(PC, &Idx, 1);
570 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
572 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
573 const Twine &Name = "") {
574 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
576 if (Constant *PC = dyn_cast<Constant>(Ptr))
577 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
579 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
581 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
582 const Twine &Name = "") {
584 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
585 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
588 if (Constant *PC = dyn_cast<Constant>(Ptr))
589 return Folder.CreateGetElementPtr(PC, Idxs, 2);
591 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
593 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
594 const Twine &Name = "") {
596 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
597 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
600 if (Constant *PC = dyn_cast<Constant>(Ptr))
601 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
603 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
605 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
606 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
608 if (Constant *PC = dyn_cast<Constant>(Ptr))
609 return Folder.CreateGetElementPtr(PC, &Idx, 1);
611 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
613 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
614 const Twine &Name = "") {
615 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
617 if (Constant *PC = dyn_cast<Constant>(Ptr))
618 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
620 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
622 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
623 const Twine &Name = "") {
625 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
626 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
629 if (Constant *PC = dyn_cast<Constant>(Ptr))
630 return Folder.CreateGetElementPtr(PC, Idxs, 2);
632 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
634 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
635 const Twine &Name = "") {
637 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
638 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
641 if (Constant *PC = dyn_cast<Constant>(Ptr))
642 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
644 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
646 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
647 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
649 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
650 Constant *StrConstant = ConstantArray::get(Context, Str, true);
651 Module &M = *BB->getParent()->getParent();
652 GlobalVariable *gv = new GlobalVariable(M,
653 StrConstant->getType(),
655 GlobalValue::InternalLinkage,
663 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
664 Value *gv = CreateGlobalString(Str, Name);
665 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
666 Value *Args[] = { zero, zero };
667 return CreateInBoundsGEP(gv, Args, Args+2, Name);
669 //===--------------------------------------------------------------------===//
670 // Instruction creation methods: Cast/Conversion Operators
671 //===--------------------------------------------------------------------===//
673 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
674 return CreateCast(Instruction::Trunc, V, DestTy, Name);
676 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
677 return CreateCast(Instruction::ZExt, V, DestTy, Name);
679 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
680 return CreateCast(Instruction::SExt, V, DestTy, Name);
682 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
683 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
685 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
686 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
688 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
689 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
691 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
692 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
694 Value *CreateFPTrunc(Value *V, const Type *DestTy,
695 const Twine &Name = "") {
696 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
698 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
699 return CreateCast(Instruction::FPExt, V, DestTy, Name);
701 Value *CreatePtrToInt(Value *V, const Type *DestTy,
702 const Twine &Name = "") {
703 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
705 Value *CreateIntToPtr(Value *V, const Type *DestTy,
706 const Twine &Name = "") {
707 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
709 Value *CreateBitCast(Value *V, const Type *DestTy,
710 const Twine &Name = "") {
711 return CreateCast(Instruction::BitCast, V, DestTy, Name);
713 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
714 const Twine &Name = "") {
715 if (V->getType() == DestTy)
717 if (Constant *VC = dyn_cast<Constant>(V))
718 return Folder.CreateZExtOrBitCast(VC, DestTy);
719 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
721 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
722 const Twine &Name = "") {
723 if (V->getType() == DestTy)
725 if (Constant *VC = dyn_cast<Constant>(V))
726 return Folder.CreateSExtOrBitCast(VC, DestTy);
727 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
729 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
730 const Twine &Name = "") {
731 if (V->getType() == DestTy)
733 if (Constant *VC = dyn_cast<Constant>(V))
734 return Folder.CreateTruncOrBitCast(VC, DestTy);
735 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
737 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
738 const Twine &Name = "") {
739 if (V->getType() == DestTy)
741 if (Constant *VC = dyn_cast<Constant>(V))
742 return Folder.CreateCast(Op, VC, DestTy);
743 return Insert(CastInst::Create(Op, V, DestTy), Name);
745 Value *CreatePointerCast(Value *V, const Type *DestTy,
746 const Twine &Name = "") {
747 if (V->getType() == DestTy)
749 if (Constant *VC = dyn_cast<Constant>(V))
750 return Folder.CreatePointerCast(VC, DestTy);
751 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
753 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
754 const Twine &Name = "") {
755 if (V->getType() == DestTy)
757 if (Constant *VC = dyn_cast<Constant>(V))
758 return Folder.CreateIntCast(VC, DestTy, isSigned);
759 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
762 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
763 // error, instead of converting the string to bool for the isSigned parameter.
764 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
766 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
767 if (V->getType() == DestTy)
769 if (Constant *VC = dyn_cast<Constant>(V))
770 return Folder.CreateFPCast(VC, DestTy);
771 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
774 //===--------------------------------------------------------------------===//
775 // Instruction creation methods: Compare Instructions
776 //===--------------------------------------------------------------------===//
778 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
779 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
781 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
782 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
784 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
785 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
787 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
788 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
790 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
791 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
793 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
794 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
796 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
797 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
799 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
800 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
802 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
803 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
805 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
806 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
809 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
810 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
812 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
813 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
815 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
816 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
818 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
819 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
821 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
822 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
824 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
825 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
827 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
828 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
830 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
831 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
833 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
834 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
836 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
837 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
839 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
840 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
842 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
843 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
845 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
846 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
848 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
849 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
852 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
853 const Twine &Name = "") {
854 if (Constant *LC = dyn_cast<Constant>(LHS))
855 if (Constant *RC = dyn_cast<Constant>(RHS))
856 return Folder.CreateICmp(P, LC, RC);
857 return Insert(new ICmpInst(P, LHS, RHS), Name);
859 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
860 const Twine &Name = "") {
861 if (Constant *LC = dyn_cast<Constant>(LHS))
862 if (Constant *RC = dyn_cast<Constant>(RHS))
863 return Folder.CreateFCmp(P, LC, RC);
864 return Insert(new FCmpInst(P, LHS, RHS), Name);
867 //===--------------------------------------------------------------------===//
868 // Instruction creation methods: Other Instructions
869 //===--------------------------------------------------------------------===//
871 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
872 return Insert(PHINode::Create(Ty), Name);
875 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
876 return Insert(CallInst::Create(Callee), Name);
878 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
879 return Insert(CallInst::Create(Callee, Arg), Name);
881 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
882 const Twine &Name = "") {
883 Value *Args[] = { Arg1, Arg2 };
884 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
886 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
887 const Twine &Name = "") {
888 Value *Args[] = { Arg1, Arg2, Arg3 };
889 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
891 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
892 Value *Arg4, const Twine &Name = "") {
893 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
894 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
897 template<typename InputIterator>
898 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
899 InputIterator ArgEnd, const Twine &Name = "") {
900 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
903 Value *CreateSelect(Value *C, Value *True, Value *False,
904 const Twine &Name = "") {
905 if (Constant *CC = dyn_cast<Constant>(C))
906 if (Constant *TC = dyn_cast<Constant>(True))
907 if (Constant *FC = dyn_cast<Constant>(False))
908 return Folder.CreateSelect(CC, TC, FC);
909 return Insert(SelectInst::Create(C, True, False), Name);
912 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
913 return Insert(new VAArgInst(List, Ty), Name);
916 Value *CreateExtractElement(Value *Vec, Value *Idx,
917 const Twine &Name = "") {
918 if (Constant *VC = dyn_cast<Constant>(Vec))
919 if (Constant *IC = dyn_cast<Constant>(Idx))
920 return Folder.CreateExtractElement(VC, IC);
921 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
924 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
925 const Twine &Name = "") {
926 if (Constant *VC = dyn_cast<Constant>(Vec))
927 if (Constant *NC = dyn_cast<Constant>(NewElt))
928 if (Constant *IC = dyn_cast<Constant>(Idx))
929 return Folder.CreateInsertElement(VC, NC, IC);
930 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
933 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
934 const Twine &Name = "") {
935 if (Constant *V1C = dyn_cast<Constant>(V1))
936 if (Constant *V2C = dyn_cast<Constant>(V2))
937 if (Constant *MC = dyn_cast<Constant>(Mask))
938 return Folder.CreateShuffleVector(V1C, V2C, MC);
939 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
942 Value *CreateExtractValue(Value *Agg, unsigned Idx,
943 const Twine &Name = "") {
944 if (Constant *AggC = dyn_cast<Constant>(Agg))
945 return Folder.CreateExtractValue(AggC, &Idx, 1);
946 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
949 template<typename InputIterator>
950 Value *CreateExtractValue(Value *Agg,
951 InputIterator IdxBegin,
952 InputIterator IdxEnd,
953 const Twine &Name = "") {
954 if (Constant *AggC = dyn_cast<Constant>(Agg))
955 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
956 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
959 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
960 const Twine &Name = "") {
961 if (Constant *AggC = dyn_cast<Constant>(Agg))
962 if (Constant *ValC = dyn_cast<Constant>(Val))
963 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
964 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
967 template<typename InputIterator>
968 Value *CreateInsertValue(Value *Agg, Value *Val,
969 InputIterator IdxBegin,
970 InputIterator IdxEnd,
971 const Twine &Name = "") {
972 if (Constant *AggC = dyn_cast<Constant>(Agg))
973 if (Constant *ValC = dyn_cast<Constant>(Val))
974 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
975 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
978 //===--------------------------------------------------------------------===//
979 // Utility creation methods
980 //===--------------------------------------------------------------------===//
982 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
983 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
984 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
988 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
989 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
990 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
994 /// CreatePtrDiff - Return the i64 difference between two pointer values,
995 /// dividing out the size of the pointed-to objects. This is intended to
996 /// implement C-style pointer subtraction. As such, the pointers must be
997 /// appropriately aligned for their element types and pointing into the
999 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1000 assert(LHS->getType() == RHS->getType() &&
1001 "Pointer subtraction operand types must match!");
1002 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1003 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1004 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1005 Value *Difference = CreateSub(LHS_int, RHS_int);
1006 return CreateExactSDiv(Difference,
1007 ConstantExpr::getSizeOf(ArgType->getElementType()),