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().addMD(MDKind, CurDbgLocation, I);
154 /// SetDebugLocation - Set location information for the given instruction.
155 void SetDebugLocation(Instruction *I, MDNode *Loc) {
157 MDKind = Context.getMetadata().getMDKind("dbg");
159 MDKind = Context.getMetadata().registerMDKind("dbg");
160 Context.getMetadata().addMD(MDKind, Loc, I);
163 /// Insert - Insert and return the specified instruction.
164 template<typename InstTy>
165 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
166 this->InsertHelper(I, Name, BB, InsertPt);
168 Context.getMetadata().addMD(MDKind, CurDbgLocation, I);
172 //===--------------------------------------------------------------------===//
173 // Type creation methods
174 //===--------------------------------------------------------------------===//
176 /// getInt1Ty - Fetch the type representing a single bit
177 const Type *getInt1Ty() {
178 return Type::getInt1Ty(Context);
181 /// getInt8Ty - Fetch the type representing an 8-bit integer.
182 const Type *getInt8Ty() {
183 return Type::getInt8Ty(Context);
186 /// getInt16Ty - Fetch the type representing a 16-bit integer.
187 const Type *getInt16Ty() {
188 return Type::getInt16Ty(Context);
191 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
192 const Type *getInt32Ty() {
193 return Type::getInt32Ty(Context);
196 /// getInt64Ty - Fetch the type representing a 64-bit integer.
197 const Type *getInt64Ty() {
198 return Type::getInt64Ty(Context);
201 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
202 const Type *getFloatTy() {
203 return Type::getFloatTy(Context);
206 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
207 const Type *getDoubleTy() {
208 return Type::getDoubleTy(Context);
211 /// getVoidTy - Fetch the type representing void.
212 const Type *getVoidTy() {
213 return Type::getVoidTy(Context);
216 //===--------------------------------------------------------------------===//
217 // Instruction creation methods: Terminators
218 //===--------------------------------------------------------------------===//
220 /// CreateRetVoid - Create a 'ret void' instruction.
221 ReturnInst *CreateRetVoid() {
222 return Insert(ReturnInst::Create(Context));
226 /// CreateRet - Create a 'ret <val>' instruction.
228 ReturnInst *CreateRet(Value *V) {
229 return Insert(ReturnInst::Create(Context, V));
232 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
233 /// with one Value from the retVals array each, that build a aggregate
234 /// return value one value at a time, and a ret instruction to return
235 /// the resulting aggregate value. This is a convenience function for
236 /// code that uses aggregate return values as a vehicle for having
237 /// multiple return values.
239 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
240 const Type *RetType = BB->getParent()->getReturnType();
241 Value *V = UndefValue::get(RetType);
242 for (unsigned i = 0; i != N; ++i)
243 V = CreateInsertValue(V, retVals[i], i, "mrv");
244 return Insert(ReturnInst::Create(Context, V));
247 /// CreateBr - Create an unconditional 'br label X' instruction.
248 BranchInst *CreateBr(BasicBlock *Dest) {
249 return Insert(BranchInst::Create(Dest));
252 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
254 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
255 return Insert(BranchInst::Create(True, False, Cond));
258 /// CreateSwitch - Create a switch instruction with the specified value,
259 /// default dest, and with a hint for the number of cases that will be added
260 /// (for efficient allocation).
261 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
262 return Insert(SwitchInst::Create(V, Dest, NumCases));
265 /// CreateIndirectBr - Create an indirect branch instruction with the
266 /// specified address operand, with an optional hint for the number of
267 /// destinations that will be added (for efficient allocation).
268 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
269 return Insert(IndirectBrInst::Create(Addr, NumDests));
272 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
273 BasicBlock *UnwindDest, const Twine &Name = "") {
274 Value *Args[] = { 0 };
275 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
278 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
279 BasicBlock *UnwindDest, Value *Arg1,
280 const Twine &Name = "") {
281 Value *Args[] = { Arg1 };
282 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
285 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
286 BasicBlock *UnwindDest, Value *Arg1,
287 Value *Arg2, Value *Arg3,
288 const Twine &Name = "") {
289 Value *Args[] = { Arg1, Arg2, Arg3 };
290 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
293 /// CreateInvoke - Create an invoke instruction.
294 template<typename InputIterator>
295 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
296 BasicBlock *UnwindDest, InputIterator ArgBegin,
297 InputIterator ArgEnd, const Twine &Name = "") {
298 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
299 ArgBegin, ArgEnd), Name);
302 UnwindInst *CreateUnwind() {
303 return Insert(new UnwindInst(Context));
306 UnreachableInst *CreateUnreachable() {
307 return Insert(new UnreachableInst(Context));
310 //===--------------------------------------------------------------------===//
311 // Instruction creation methods: Binary Operators
312 //===--------------------------------------------------------------------===//
314 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
315 if (Constant *LC = dyn_cast<Constant>(LHS))
316 if (Constant *RC = dyn_cast<Constant>(RHS))
317 return Folder.CreateAdd(LC, RC);
318 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
320 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
321 if (Constant *LC = dyn_cast<Constant>(LHS))
322 if (Constant *RC = dyn_cast<Constant>(RHS))
323 return Folder.CreateNSWAdd(LC, RC);
324 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
326 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
327 if (Constant *LC = dyn_cast<Constant>(LHS))
328 if (Constant *RC = dyn_cast<Constant>(RHS))
329 return Folder.CreateFAdd(LC, RC);
330 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
332 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
333 if (Constant *LC = dyn_cast<Constant>(LHS))
334 if (Constant *RC = dyn_cast<Constant>(RHS))
335 return Folder.CreateSub(LC, RC);
336 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
338 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
339 if (Constant *LC = dyn_cast<Constant>(LHS))
340 if (Constant *RC = dyn_cast<Constant>(RHS))
341 return Folder.CreateNSWSub(LC, RC);
342 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
344 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
345 if (Constant *LC = dyn_cast<Constant>(LHS))
346 if (Constant *RC = dyn_cast<Constant>(RHS))
347 return Folder.CreateFSub(LC, RC);
348 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
350 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
351 if (Constant *LC = dyn_cast<Constant>(LHS))
352 if (Constant *RC = dyn_cast<Constant>(RHS))
353 return Folder.CreateMul(LC, RC);
354 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
356 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
357 if (Constant *LC = dyn_cast<Constant>(LHS))
358 if (Constant *RC = dyn_cast<Constant>(RHS))
359 return Folder.CreateFMul(LC, RC);
360 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
362 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
363 if (Constant *LC = dyn_cast<Constant>(LHS))
364 if (Constant *RC = dyn_cast<Constant>(RHS))
365 return Folder.CreateUDiv(LC, RC);
366 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
368 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
369 if (Constant *LC = dyn_cast<Constant>(LHS))
370 if (Constant *RC = dyn_cast<Constant>(RHS))
371 return Folder.CreateSDiv(LC, RC);
372 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
374 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
375 if (Constant *LC = dyn_cast<Constant>(LHS))
376 if (Constant *RC = dyn_cast<Constant>(RHS))
377 return Folder.CreateExactSDiv(LC, RC);
378 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
380 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
381 if (Constant *LC = dyn_cast<Constant>(LHS))
382 if (Constant *RC = dyn_cast<Constant>(RHS))
383 return Folder.CreateFDiv(LC, RC);
384 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
386 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
387 if (Constant *LC = dyn_cast<Constant>(LHS))
388 if (Constant *RC = dyn_cast<Constant>(RHS))
389 return Folder.CreateURem(LC, RC);
390 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
392 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
393 if (Constant *LC = dyn_cast<Constant>(LHS))
394 if (Constant *RC = dyn_cast<Constant>(RHS))
395 return Folder.CreateSRem(LC, RC);
396 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
398 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
399 if (Constant *LC = dyn_cast<Constant>(LHS))
400 if (Constant *RC = dyn_cast<Constant>(RHS))
401 return Folder.CreateFRem(LC, RC);
402 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
404 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
405 if (Constant *LC = dyn_cast<Constant>(LHS))
406 if (Constant *RC = dyn_cast<Constant>(RHS))
407 return Folder.CreateShl(LC, RC);
408 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
410 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
411 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
412 if (Constant *LC = dyn_cast<Constant>(LHS))
413 return Folder.CreateShl(LC, RHSC);
414 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
417 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
418 if (Constant *LC = dyn_cast<Constant>(LHS))
419 if (Constant *RC = dyn_cast<Constant>(RHS))
420 return Folder.CreateLShr(LC, RC);
421 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
423 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
424 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
425 if (Constant *LC = dyn_cast<Constant>(LHS))
426 return Folder.CreateLShr(LC, RHSC);
427 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
430 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
431 if (Constant *LC = dyn_cast<Constant>(LHS))
432 if (Constant *RC = dyn_cast<Constant>(RHS))
433 return Folder.CreateAShr(LC, RC);
434 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
436 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
437 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
438 if (Constant *LC = dyn_cast<Constant>(LHS))
439 return Folder.CreateSShr(LC, RHSC);
440 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
443 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
444 if (Constant *RC = dyn_cast<Constant>(RHS)) {
445 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
446 return LHS; // LHS & -1 -> LHS
447 if (Constant *LC = dyn_cast<Constant>(LHS))
448 return Folder.CreateAnd(LC, RC);
450 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
452 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
453 if (Constant *RC = dyn_cast<Constant>(RHS)) {
454 if (RC->isNullValue())
455 return LHS; // LHS | 0 -> LHS
456 if (Constant *LC = dyn_cast<Constant>(LHS))
457 return Folder.CreateOr(LC, RC);
459 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
461 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
462 if (Constant *LC = dyn_cast<Constant>(LHS))
463 if (Constant *RC = dyn_cast<Constant>(RHS))
464 return Folder.CreateXor(LC, RC);
465 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
468 Value *CreateBinOp(Instruction::BinaryOps Opc,
469 Value *LHS, Value *RHS, const Twine &Name = "") {
470 if (Constant *LC = dyn_cast<Constant>(LHS))
471 if (Constant *RC = dyn_cast<Constant>(RHS))
472 return Folder.CreateBinOp(Opc, LC, RC);
473 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
476 Value *CreateNeg(Value *V, const Twine &Name = "") {
477 if (Constant *VC = dyn_cast<Constant>(V))
478 return Folder.CreateNeg(VC);
479 return Insert(BinaryOperator::CreateNeg(V), Name);
481 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
482 if (Constant *VC = dyn_cast<Constant>(V))
483 return Folder.CreateNSWNeg(VC);
484 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
486 Value *CreateFNeg(Value *V, const Twine &Name = "") {
487 if (Constant *VC = dyn_cast<Constant>(V))
488 return Folder.CreateFNeg(VC);
489 return Insert(BinaryOperator::CreateFNeg(V), Name);
491 Value *CreateNot(Value *V, const Twine &Name = "") {
492 if (Constant *VC = dyn_cast<Constant>(V))
493 return Folder.CreateNot(VC);
494 return Insert(BinaryOperator::CreateNot(V), Name);
497 //===--------------------------------------------------------------------===//
498 // Instruction creation methods: Memory Instructions
499 //===--------------------------------------------------------------------===//
501 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
502 const Twine &Name = "") {
503 return Insert(new AllocaInst(Ty, ArraySize), Name);
505 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
506 // converting the string to 'bool' for the isVolatile parameter.
507 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
508 return Insert(new LoadInst(Ptr), Name);
510 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
511 return Insert(new LoadInst(Ptr), Name);
513 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
514 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
516 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
517 return Insert(new StoreInst(Val, Ptr, isVolatile));
519 template<typename InputIterator>
520 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
521 const Twine &Name = "") {
522 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
523 // Every index must be constant.
525 for (i = IdxBegin; i < IdxEnd; ++i)
526 if (!isa<Constant>(*i))
529 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
531 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
533 template<typename InputIterator>
534 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
535 const Twine &Name = "") {
536 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
537 // Every index must be constant.
539 for (i = IdxBegin; i < IdxEnd; ++i)
540 if (!isa<Constant>(*i))
543 return Folder.CreateInBoundsGetElementPtr(PC,
547 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
550 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
551 if (Constant *PC = dyn_cast<Constant>(Ptr))
552 if (Constant *IC = dyn_cast<Constant>(Idx))
553 return Folder.CreateGetElementPtr(PC, &IC, 1);
554 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
556 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
557 if (Constant *PC = dyn_cast<Constant>(Ptr))
558 if (Constant *IC = dyn_cast<Constant>(Idx))
559 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
560 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
562 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
563 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
565 if (Constant *PC = dyn_cast<Constant>(Ptr))
566 return Folder.CreateGetElementPtr(PC, &Idx, 1);
568 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
570 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
571 const Twine &Name = "") {
572 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
574 if (Constant *PC = dyn_cast<Constant>(Ptr))
575 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
577 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
579 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
580 const Twine &Name = "") {
582 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
583 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
586 if (Constant *PC = dyn_cast<Constant>(Ptr))
587 return Folder.CreateGetElementPtr(PC, Idxs, 2);
589 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
591 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
592 const Twine &Name = "") {
594 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
595 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
598 if (Constant *PC = dyn_cast<Constant>(Ptr))
599 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
601 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
603 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
604 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
606 if (Constant *PC = dyn_cast<Constant>(Ptr))
607 return Folder.CreateGetElementPtr(PC, &Idx, 1);
609 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
611 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
612 const Twine &Name = "") {
613 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
615 if (Constant *PC = dyn_cast<Constant>(Ptr))
616 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
618 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
620 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
621 const Twine &Name = "") {
623 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
624 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
627 if (Constant *PC = dyn_cast<Constant>(Ptr))
628 return Folder.CreateGetElementPtr(PC, Idxs, 2);
630 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
632 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
633 const Twine &Name = "") {
635 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
636 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
639 if (Constant *PC = dyn_cast<Constant>(Ptr))
640 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
642 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
644 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
645 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
647 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "") {
648 Constant *StrConstant = ConstantArray::get(Context, Str, true);
649 Module &M = *BB->getParent()->getParent();
650 GlobalVariable *gv = new GlobalVariable(M,
651 StrConstant->getType(),
653 GlobalValue::InternalLinkage,
661 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
662 Value *gv = CreateGlobalString(Str, Name);
663 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
664 Value *Args[] = { zero, zero };
665 return CreateInBoundsGEP(gv, Args, Args+2, Name);
667 //===--------------------------------------------------------------------===//
668 // Instruction creation methods: Cast/Conversion Operators
669 //===--------------------------------------------------------------------===//
671 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
672 return CreateCast(Instruction::Trunc, V, DestTy, Name);
674 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
675 return CreateCast(Instruction::ZExt, V, DestTy, Name);
677 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
678 return CreateCast(Instruction::SExt, V, DestTy, Name);
680 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
681 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
683 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
684 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
686 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
687 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
689 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
690 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
692 Value *CreateFPTrunc(Value *V, const Type *DestTy,
693 const Twine &Name = "") {
694 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
696 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
697 return CreateCast(Instruction::FPExt, V, DestTy, Name);
699 Value *CreatePtrToInt(Value *V, const Type *DestTy,
700 const Twine &Name = "") {
701 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
703 Value *CreateIntToPtr(Value *V, const Type *DestTy,
704 const Twine &Name = "") {
705 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
707 Value *CreateBitCast(Value *V, const Type *DestTy,
708 const Twine &Name = "") {
709 return CreateCast(Instruction::BitCast, V, DestTy, Name);
711 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
712 const Twine &Name = "") {
713 if (V->getType() == DestTy)
715 if (Constant *VC = dyn_cast<Constant>(V))
716 return Folder.CreateZExtOrBitCast(VC, DestTy);
717 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
719 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
720 const Twine &Name = "") {
721 if (V->getType() == DestTy)
723 if (Constant *VC = dyn_cast<Constant>(V))
724 return Folder.CreateSExtOrBitCast(VC, DestTy);
725 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
727 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
728 const Twine &Name = "") {
729 if (V->getType() == DestTy)
731 if (Constant *VC = dyn_cast<Constant>(V))
732 return Folder.CreateTruncOrBitCast(VC, DestTy);
733 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
735 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
736 const Twine &Name = "") {
737 if (V->getType() == DestTy)
739 if (Constant *VC = dyn_cast<Constant>(V))
740 return Folder.CreateCast(Op, VC, DestTy);
741 return Insert(CastInst::Create(Op, V, DestTy), Name);
743 Value *CreatePointerCast(Value *V, const Type *DestTy,
744 const Twine &Name = "") {
745 if (V->getType() == DestTy)
747 if (Constant *VC = dyn_cast<Constant>(V))
748 return Folder.CreatePointerCast(VC, DestTy);
749 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
751 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
752 const Twine &Name = "") {
753 if (V->getType() == DestTy)
755 if (Constant *VC = dyn_cast<Constant>(V))
756 return Folder.CreateIntCast(VC, DestTy, isSigned);
757 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
760 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
761 // error, instead of converting the string to bool for the isSigned parameter.
762 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
764 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
765 if (V->getType() == DestTy)
767 if (Constant *VC = dyn_cast<Constant>(V))
768 return Folder.CreateFPCast(VC, DestTy);
769 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
772 //===--------------------------------------------------------------------===//
773 // Instruction creation methods: Compare Instructions
774 //===--------------------------------------------------------------------===//
776 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
777 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
779 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
780 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
782 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
783 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
785 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
786 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
788 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
789 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
791 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
792 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
794 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
795 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
797 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
798 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
800 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
801 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
803 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
804 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
807 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
808 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
810 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
811 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
813 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
814 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
816 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
817 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
819 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
820 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
822 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
823 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
825 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
826 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
828 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
829 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
831 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
832 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
834 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
835 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
837 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
838 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
840 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
841 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
843 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
844 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
846 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
847 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
850 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
851 const Twine &Name = "") {
852 if (Constant *LC = dyn_cast<Constant>(LHS))
853 if (Constant *RC = dyn_cast<Constant>(RHS))
854 return Folder.CreateICmp(P, LC, RC);
855 return Insert(new ICmpInst(P, LHS, RHS), Name);
857 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
858 const Twine &Name = "") {
859 if (Constant *LC = dyn_cast<Constant>(LHS))
860 if (Constant *RC = dyn_cast<Constant>(RHS))
861 return Folder.CreateFCmp(P, LC, RC);
862 return Insert(new FCmpInst(P, LHS, RHS), Name);
865 //===--------------------------------------------------------------------===//
866 // Instruction creation methods: Other Instructions
867 //===--------------------------------------------------------------------===//
869 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
870 return Insert(PHINode::Create(Ty), Name);
873 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
874 return Insert(CallInst::Create(Callee), Name);
876 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
877 return Insert(CallInst::Create(Callee, Arg), Name);
879 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
880 const Twine &Name = "") {
881 Value *Args[] = { Arg1, Arg2 };
882 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
884 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
885 const Twine &Name = "") {
886 Value *Args[] = { Arg1, Arg2, Arg3 };
887 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
889 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
890 Value *Arg4, const Twine &Name = "") {
891 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
892 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
895 template<typename InputIterator>
896 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
897 InputIterator ArgEnd, const Twine &Name = "") {
898 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
901 Value *CreateSelect(Value *C, Value *True, Value *False,
902 const Twine &Name = "") {
903 if (Constant *CC = dyn_cast<Constant>(C))
904 if (Constant *TC = dyn_cast<Constant>(True))
905 if (Constant *FC = dyn_cast<Constant>(False))
906 return Folder.CreateSelect(CC, TC, FC);
907 return Insert(SelectInst::Create(C, True, False), Name);
910 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
911 return Insert(new VAArgInst(List, Ty), Name);
914 Value *CreateExtractElement(Value *Vec, Value *Idx,
915 const Twine &Name = "") {
916 if (Constant *VC = dyn_cast<Constant>(Vec))
917 if (Constant *IC = dyn_cast<Constant>(Idx))
918 return Folder.CreateExtractElement(VC, IC);
919 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
922 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
923 const Twine &Name = "") {
924 if (Constant *VC = dyn_cast<Constant>(Vec))
925 if (Constant *NC = dyn_cast<Constant>(NewElt))
926 if (Constant *IC = dyn_cast<Constant>(Idx))
927 return Folder.CreateInsertElement(VC, NC, IC);
928 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
931 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
932 const Twine &Name = "") {
933 if (Constant *V1C = dyn_cast<Constant>(V1))
934 if (Constant *V2C = dyn_cast<Constant>(V2))
935 if (Constant *MC = dyn_cast<Constant>(Mask))
936 return Folder.CreateShuffleVector(V1C, V2C, MC);
937 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
940 Value *CreateExtractValue(Value *Agg, unsigned Idx,
941 const Twine &Name = "") {
942 if (Constant *AggC = dyn_cast<Constant>(Agg))
943 return Folder.CreateExtractValue(AggC, &Idx, 1);
944 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
947 template<typename InputIterator>
948 Value *CreateExtractValue(Value *Agg,
949 InputIterator IdxBegin,
950 InputIterator IdxEnd,
951 const Twine &Name = "") {
952 if (Constant *AggC = dyn_cast<Constant>(Agg))
953 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
954 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
957 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
958 const Twine &Name = "") {
959 if (Constant *AggC = dyn_cast<Constant>(Agg))
960 if (Constant *ValC = dyn_cast<Constant>(Val))
961 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
962 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
965 template<typename InputIterator>
966 Value *CreateInsertValue(Value *Agg, Value *Val,
967 InputIterator IdxBegin,
968 InputIterator IdxEnd,
969 const Twine &Name = "") {
970 if (Constant *AggC = dyn_cast<Constant>(Agg))
971 if (Constant *ValC = dyn_cast<Constant>(Val))
972 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
973 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
976 //===--------------------------------------------------------------------===//
977 // Utility creation methods
978 //===--------------------------------------------------------------------===//
980 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
981 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
982 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
986 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
987 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
988 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
992 /// CreatePtrDiff - Return the i64 difference between two pointer values,
993 /// dividing out the size of the pointed-to objects. This is intended to
994 /// implement C-style pointer subtraction. As such, the pointers must be
995 /// appropriately aligned for their element types and pointing into the
997 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
998 assert(LHS->getType() == RHS->getType() &&
999 "Pointer subtraction operand types must match!");
1000 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1001 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1002 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1003 Value *Difference = CreateSub(LHS_int, RHS_int);
1004 return CreateExactSDiv(Difference,
1005 ConstantExpr::getSizeOf(ArgType->getElementType()),