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/Function.h"
21 #include "llvm/Metadata.h"
22 #include "llvm/LLVMContext.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Support/ConstantFolder.h"
28 /// IRBuilderDefaultInserter - This provides the default implementation of the
29 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
30 /// created by IRBuilder and needs to be inserted. By default, this inserts the
31 /// instruction at the insertion point.
32 template <bool preserveNames = true>
33 class IRBuilderDefaultInserter {
35 void InsertHelper(Instruction *I, const Twine &Name,
36 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
37 if (BB) BB->getInstList().insert(InsertPt, I);
43 /// IRBuilderBase - Common base class shared among various IRBuilders.
47 BasicBlock::iterator InsertPt;
49 MDNode *CurDbgLocation;
53 IRBuilderBase(LLVMContext &context)
54 : DbgMDKind(0), CurDbgLocation(0), Context(context) {
55 ClearInsertionPoint();
58 //===--------------------------------------------------------------------===//
59 // Builder configuration methods
60 //===--------------------------------------------------------------------===//
62 /// ClearInsertionPoint - Clear the insertion point: created instructions will
63 /// not be inserted into a block.
64 void ClearInsertionPoint() {
68 BasicBlock *GetInsertBlock() const { return BB; }
69 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
71 /// SetInsertPoint - This specifies that created instructions should be
72 /// appended to the end of the specified block.
73 void SetInsertPoint(BasicBlock *TheBB) {
78 /// SetInsertPoint - This specifies that created instructions should be
79 /// inserted at the specified point.
80 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
85 /// SetCurrentDebugLocation - Set location information used by debugging
87 void SetCurrentDebugLocation(MDNode *L) {
89 DbgMDKind = Context.getMetadata().getMDKindID("dbg");
93 MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
95 /// SetDebugLocation - Set location information for the given instruction.
96 void SetDebugLocation(Instruction *I) {
98 Context.getMetadata().addMD(DbgMDKind, CurDbgLocation, I);
101 /// SetDebugLocation - Set location information for the given instruction.
102 void SetDebugLocation(Instruction *I, MDNode *Loc) {
104 DbgMDKind = Context.getMetadata().getMDKindID("dbg");
105 Context.getMetadata().addMD(DbgMDKind, Loc, I);
108 //===--------------------------------------------------------------------===//
109 // Miscellaneous creation methods.
110 //===--------------------------------------------------------------------===//
112 /// CreateGlobalString - Make a new global variable with an initializer that
113 /// has array of i8 type filled in the the nul terminated string value
114 /// specified. If Name is specified, it is the name of the global variable
116 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
118 //===--------------------------------------------------------------------===//
119 // Type creation methods
120 //===--------------------------------------------------------------------===//
122 /// getInt1Ty - Fetch the type representing a single bit
123 const Type *getInt1Ty() {
124 return Type::getInt1Ty(Context);
127 /// getInt8Ty - Fetch the type representing an 8-bit integer.
128 const Type *getInt8Ty() {
129 return Type::getInt8Ty(Context);
132 /// getInt16Ty - Fetch the type representing a 16-bit integer.
133 const Type *getInt16Ty() {
134 return Type::getInt16Ty(Context);
137 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
138 const Type *getInt32Ty() {
139 return Type::getInt32Ty(Context);
142 /// getInt64Ty - Fetch the type representing a 64-bit integer.
143 const Type *getInt64Ty() {
144 return Type::getInt64Ty(Context);
147 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
148 const Type *getFloatTy() {
149 return Type::getFloatTy(Context);
152 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
153 const Type *getDoubleTy() {
154 return Type::getDoubleTy(Context);
157 /// getVoidTy - Fetch the type representing void.
158 const Type *getVoidTy() {
159 return Type::getVoidTy(Context);
163 /// IRBuilder - This provides a uniform API for creating instructions and
164 /// inserting them into a basic block: either at the end of a BasicBlock, or
165 /// at a specific iterator location in a block.
167 /// Note that the builder does not expose the full generality of LLVM
168 /// instructions. For access to extra instruction properties, use the mutators
169 /// (e.g. setVolatile) on the instructions after they have been created.
170 /// The first template argument handles whether or not to preserve names in the
171 /// final instruction output. This defaults to on. The second template argument
172 /// specifies a class to use for creating constants. This defaults to creating
173 /// minimally folded constants. The fourth template argument allows clients to
174 /// specify custom insertion hooks that are called on every newly created
176 template<bool preserveNames = true, typename T = ConstantFolder,
177 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
178 class IRBuilder : public IRBuilderBase, public Inserter {
181 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
182 : IRBuilderBase(C), Inserter(I), Folder(F) {
185 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
188 explicit IRBuilder(BasicBlock *TheBB, const T &F)
189 : IRBuilderBase(TheBB->getContext()), Folder(F) {
190 SetInsertPoint(TheBB);
193 explicit IRBuilder(BasicBlock *TheBB)
194 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
195 SetInsertPoint(TheBB);
198 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
199 : IRBuilderBase(TheBB->getContext()), Folder(F) {
200 SetInsertPoint(TheBB, IP);
203 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
204 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
205 SetInsertPoint(TheBB, IP);
208 /// getFolder - Get the constant folder being used.
209 const T &getFolder() { return Folder; }
211 /// isNamePreserving - Return true if this builder is configured to actually
212 /// add the requested names to IR created through it.
213 bool isNamePreserving() const { return preserveNames; }
215 /// Insert - Insert and return the specified instruction.
216 template<typename InstTy>
217 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
218 this->InsertHelper(I, Name, BB, InsertPt);
220 Context.getMetadata().addMD(DbgMDKind, CurDbgLocation, I);
224 //===--------------------------------------------------------------------===//
225 // Instruction creation methods: Terminators
226 //===--------------------------------------------------------------------===//
228 /// CreateRetVoid - Create a 'ret void' instruction.
229 ReturnInst *CreateRetVoid() {
230 return Insert(ReturnInst::Create(Context));
234 /// CreateRet - Create a 'ret <val>' instruction.
236 ReturnInst *CreateRet(Value *V) {
237 return Insert(ReturnInst::Create(Context, V));
240 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
241 /// with one Value from the retVals array each, that build a aggregate
242 /// return value one value at a time, and a ret instruction to return
243 /// the resulting aggregate value. This is a convenience function for
244 /// code that uses aggregate return values as a vehicle for having
245 /// multiple return values.
247 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
248 const Type *RetType = BB->getParent()->getReturnType();
249 Value *V = UndefValue::get(RetType);
250 for (unsigned i = 0; i != N; ++i)
251 V = CreateInsertValue(V, retVals[i], i, "mrv");
252 return Insert(ReturnInst::Create(Context, V));
255 /// CreateBr - Create an unconditional 'br label X' instruction.
256 BranchInst *CreateBr(BasicBlock *Dest) {
257 return Insert(BranchInst::Create(Dest));
260 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
262 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
263 return Insert(BranchInst::Create(True, False, Cond));
266 /// CreateSwitch - Create a switch instruction with the specified value,
267 /// default dest, and with a hint for the number of cases that will be added
268 /// (for efficient allocation).
269 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
270 return Insert(SwitchInst::Create(V, Dest, NumCases));
273 /// CreateIndirectBr - Create an indirect branch instruction with the
274 /// specified address operand, with an optional hint for the number of
275 /// destinations that will be added (for efficient allocation).
276 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
277 return Insert(IndirectBrInst::Create(Addr, NumDests));
280 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
281 BasicBlock *UnwindDest, const Twine &Name = "") {
282 Value *Args[] = { 0 };
283 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
286 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
287 BasicBlock *UnwindDest, Value *Arg1,
288 const Twine &Name = "") {
289 Value *Args[] = { Arg1 };
290 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
293 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
294 BasicBlock *UnwindDest, Value *Arg1,
295 Value *Arg2, Value *Arg3,
296 const Twine &Name = "") {
297 Value *Args[] = { Arg1, Arg2, Arg3 };
298 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
301 /// CreateInvoke - Create an invoke instruction.
302 template<typename InputIterator>
303 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
304 BasicBlock *UnwindDest, InputIterator ArgBegin,
305 InputIterator ArgEnd, const Twine &Name = "") {
306 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
307 ArgBegin, ArgEnd), Name);
310 UnwindInst *CreateUnwind() {
311 return Insert(new UnwindInst(Context));
314 UnreachableInst *CreateUnreachable() {
315 return Insert(new UnreachableInst(Context));
318 //===--------------------------------------------------------------------===//
319 // Instruction creation methods: Binary Operators
320 //===--------------------------------------------------------------------===//
322 Value *CreateAdd(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.CreateAdd(LC, RC);
326 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
328 Value *CreateNSWAdd(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.CreateNSWAdd(LC, RC);
332 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
334 Value *CreateFAdd(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.CreateFAdd(LC, RC);
338 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
340 Value *CreateSub(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.CreateSub(LC, RC);
344 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
346 Value *CreateNSWSub(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.CreateNSWSub(LC, RC);
350 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
352 Value *CreateFSub(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.CreateFSub(LC, RC);
356 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
358 Value *CreateMul(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.CreateMul(LC, RC);
362 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
364 Value *CreateNSWMul(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.CreateNSWMul(LC, RC);
368 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
370 Value *CreateFMul(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.CreateFMul(LC, RC);
374 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
376 Value *CreateUDiv(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.CreateUDiv(LC, RC);
380 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
382 Value *CreateSDiv(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.CreateSDiv(LC, RC);
386 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
388 Value *CreateExactSDiv(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.CreateExactSDiv(LC, RC);
392 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
394 Value *CreateFDiv(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.CreateFDiv(LC, RC);
398 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
400 Value *CreateURem(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.CreateURem(LC, RC);
404 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
406 Value *CreateSRem(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.CreateSRem(LC, RC);
410 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
412 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
413 if (Constant *LC = dyn_cast<Constant>(LHS))
414 if (Constant *RC = dyn_cast<Constant>(RHS))
415 return Folder.CreateFRem(LC, RC);
416 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
418 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
419 if (Constant *LC = dyn_cast<Constant>(LHS))
420 if (Constant *RC = dyn_cast<Constant>(RHS))
421 return Folder.CreateShl(LC, RC);
422 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
424 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
425 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
426 if (Constant *LC = dyn_cast<Constant>(LHS))
427 return Folder.CreateShl(LC, RHSC);
428 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
431 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
432 if (Constant *LC = dyn_cast<Constant>(LHS))
433 if (Constant *RC = dyn_cast<Constant>(RHS))
434 return Folder.CreateLShr(LC, RC);
435 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
437 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
438 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
439 if (Constant *LC = dyn_cast<Constant>(LHS))
440 return Folder.CreateLShr(LC, RHSC);
441 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
444 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
445 if (Constant *LC = dyn_cast<Constant>(LHS))
446 if (Constant *RC = dyn_cast<Constant>(RHS))
447 return Folder.CreateAShr(LC, RC);
448 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
450 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
451 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
452 if (Constant *LC = dyn_cast<Constant>(LHS))
453 return Folder.CreateSShr(LC, RHSC);
454 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
457 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
458 if (Constant *RC = dyn_cast<Constant>(RHS)) {
459 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
460 return LHS; // LHS & -1 -> LHS
461 if (Constant *LC = dyn_cast<Constant>(LHS))
462 return Folder.CreateAnd(LC, RC);
464 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
466 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
467 if (Constant *RC = dyn_cast<Constant>(RHS)) {
468 if (RC->isNullValue())
469 return LHS; // LHS | 0 -> LHS
470 if (Constant *LC = dyn_cast<Constant>(LHS))
471 return Folder.CreateOr(LC, RC);
473 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
475 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
476 if (Constant *LC = dyn_cast<Constant>(LHS))
477 if (Constant *RC = dyn_cast<Constant>(RHS))
478 return Folder.CreateXor(LC, RC);
479 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
482 Value *CreateBinOp(Instruction::BinaryOps Opc,
483 Value *LHS, Value *RHS, const Twine &Name = "") {
484 if (Constant *LC = dyn_cast<Constant>(LHS))
485 if (Constant *RC = dyn_cast<Constant>(RHS))
486 return Folder.CreateBinOp(Opc, LC, RC);
487 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
490 Value *CreateNeg(Value *V, const Twine &Name = "") {
491 if (Constant *VC = dyn_cast<Constant>(V))
492 return Folder.CreateNeg(VC);
493 return Insert(BinaryOperator::CreateNeg(V), Name);
495 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
496 if (Constant *VC = dyn_cast<Constant>(V))
497 return Folder.CreateNSWNeg(VC);
498 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
500 Value *CreateFNeg(Value *V, const Twine &Name = "") {
501 if (Constant *VC = dyn_cast<Constant>(V))
502 return Folder.CreateFNeg(VC);
503 return Insert(BinaryOperator::CreateFNeg(V), Name);
505 Value *CreateNot(Value *V, const Twine &Name = "") {
506 if (Constant *VC = dyn_cast<Constant>(V))
507 return Folder.CreateNot(VC);
508 return Insert(BinaryOperator::CreateNot(V), Name);
511 //===--------------------------------------------------------------------===//
512 // Instruction creation methods: Memory Instructions
513 //===--------------------------------------------------------------------===//
515 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
516 const Twine &Name = "") {
517 return Insert(new AllocaInst(Ty, ArraySize), Name);
519 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
520 // converting the string to 'bool' for the isVolatile parameter.
521 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
522 return Insert(new LoadInst(Ptr), Name);
524 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
525 return Insert(new LoadInst(Ptr), Name);
527 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
528 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
530 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
531 return Insert(new StoreInst(Val, Ptr, isVolatile));
533 template<typename InputIterator>
534 Value *CreateGEP(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.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
545 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
547 template<typename InputIterator>
548 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
549 const Twine &Name = "") {
550 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
551 // Every index must be constant.
553 for (i = IdxBegin; i < IdxEnd; ++i)
554 if (!isa<Constant>(*i))
557 return Folder.CreateInBoundsGetElementPtr(PC,
561 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
564 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
565 if (Constant *PC = dyn_cast<Constant>(Ptr))
566 if (Constant *IC = dyn_cast<Constant>(Idx))
567 return Folder.CreateGetElementPtr(PC, &IC, 1);
568 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
570 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
571 if (Constant *PC = dyn_cast<Constant>(Ptr))
572 if (Constant *IC = dyn_cast<Constant>(Idx))
573 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
574 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
576 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
577 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
579 if (Constant *PC = dyn_cast<Constant>(Ptr))
580 return Folder.CreateGetElementPtr(PC, &Idx, 1);
582 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
584 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
585 const Twine &Name = "") {
586 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
588 if (Constant *PC = dyn_cast<Constant>(Ptr))
589 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
591 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
593 Value *CreateConstGEP2_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.CreateGetElementPtr(PC, Idxs, 2);
603 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
605 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
606 const Twine &Name = "") {
608 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
609 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
612 if (Constant *PC = dyn_cast<Constant>(Ptr))
613 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
615 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
617 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
618 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
620 if (Constant *PC = dyn_cast<Constant>(Ptr))
621 return Folder.CreateGetElementPtr(PC, &Idx, 1);
623 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
625 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
626 const Twine &Name = "") {
627 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
629 if (Constant *PC = dyn_cast<Constant>(Ptr))
630 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
632 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
634 Value *CreateConstGEP2_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.CreateGetElementPtr(PC, Idxs, 2);
644 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
646 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
647 const Twine &Name = "") {
649 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
650 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
653 if (Constant *PC = dyn_cast<Constant>(Ptr))
654 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
656 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
658 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
659 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
662 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
663 /// with "i8*" type instead of a pointer to array of i8.
664 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
665 Value *gv = CreateGlobalString(Str, Name);
666 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
667 Value *Args[] = { zero, zero };
668 return CreateInBoundsGEP(gv, Args, Args+2, Name);
671 //===--------------------------------------------------------------------===//
672 // Instruction creation methods: Cast/Conversion Operators
673 //===--------------------------------------------------------------------===//
675 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
676 return CreateCast(Instruction::Trunc, V, DestTy, Name);
678 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
679 return CreateCast(Instruction::ZExt, V, DestTy, Name);
681 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
682 return CreateCast(Instruction::SExt, V, DestTy, Name);
684 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
685 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
687 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
688 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
690 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
691 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
693 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
694 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
696 Value *CreateFPTrunc(Value *V, const Type *DestTy,
697 const Twine &Name = "") {
698 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
700 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
701 return CreateCast(Instruction::FPExt, V, DestTy, Name);
703 Value *CreatePtrToInt(Value *V, const Type *DestTy,
704 const Twine &Name = "") {
705 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
707 Value *CreateIntToPtr(Value *V, const Type *DestTy,
708 const Twine &Name = "") {
709 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
711 Value *CreateBitCast(Value *V, const Type *DestTy,
712 const Twine &Name = "") {
713 return CreateCast(Instruction::BitCast, V, DestTy, Name);
715 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
716 const Twine &Name = "") {
717 if (V->getType() == DestTy)
719 if (Constant *VC = dyn_cast<Constant>(V))
720 return Folder.CreateZExtOrBitCast(VC, DestTy);
721 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
723 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
724 const Twine &Name = "") {
725 if (V->getType() == DestTy)
727 if (Constant *VC = dyn_cast<Constant>(V))
728 return Folder.CreateSExtOrBitCast(VC, DestTy);
729 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
731 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
732 const Twine &Name = "") {
733 if (V->getType() == DestTy)
735 if (Constant *VC = dyn_cast<Constant>(V))
736 return Folder.CreateTruncOrBitCast(VC, DestTy);
737 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
739 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
740 const Twine &Name = "") {
741 if (V->getType() == DestTy)
743 if (Constant *VC = dyn_cast<Constant>(V))
744 return Folder.CreateCast(Op, VC, DestTy);
745 return Insert(CastInst::Create(Op, V, DestTy), Name);
747 Value *CreatePointerCast(Value *V, const Type *DestTy,
748 const Twine &Name = "") {
749 if (V->getType() == DestTy)
751 if (Constant *VC = dyn_cast<Constant>(V))
752 return Folder.CreatePointerCast(VC, DestTy);
753 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
755 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
756 const Twine &Name = "") {
757 if (V->getType() == DestTy)
759 if (Constant *VC = dyn_cast<Constant>(V))
760 return Folder.CreateIntCast(VC, DestTy, isSigned);
761 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
764 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
765 // error, instead of converting the string to bool for the isSigned parameter.
766 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
768 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
769 if (V->getType() == DestTy)
771 if (Constant *VC = dyn_cast<Constant>(V))
772 return Folder.CreateFPCast(VC, DestTy);
773 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
776 //===--------------------------------------------------------------------===//
777 // Instruction creation methods: Compare Instructions
778 //===--------------------------------------------------------------------===//
780 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
781 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
783 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
784 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
786 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
787 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
789 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
790 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
792 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
793 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
795 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
796 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
798 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
799 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
801 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
802 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
804 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
805 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
807 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
808 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
811 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
812 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
814 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
815 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
817 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
818 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
820 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
821 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
823 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
824 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
826 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
827 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
829 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
830 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
832 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
833 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
835 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
836 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
838 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
839 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
841 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
842 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
844 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
845 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
847 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
848 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
850 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
851 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
854 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
855 const Twine &Name = "") {
856 if (Constant *LC = dyn_cast<Constant>(LHS))
857 if (Constant *RC = dyn_cast<Constant>(RHS))
858 return Folder.CreateICmp(P, LC, RC);
859 return Insert(new ICmpInst(P, LHS, RHS), Name);
861 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
862 const Twine &Name = "") {
863 if (Constant *LC = dyn_cast<Constant>(LHS))
864 if (Constant *RC = dyn_cast<Constant>(RHS))
865 return Folder.CreateFCmp(P, LC, RC);
866 return Insert(new FCmpInst(P, LHS, RHS), Name);
869 //===--------------------------------------------------------------------===//
870 // Instruction creation methods: Other Instructions
871 //===--------------------------------------------------------------------===//
873 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
874 return Insert(PHINode::Create(Ty), Name);
877 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
878 return Insert(CallInst::Create(Callee), Name);
880 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
881 return Insert(CallInst::Create(Callee, Arg), Name);
883 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
884 const Twine &Name = "") {
885 Value *Args[] = { Arg1, Arg2 };
886 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
888 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
889 const Twine &Name = "") {
890 Value *Args[] = { Arg1, Arg2, Arg3 };
891 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
893 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
894 Value *Arg4, const Twine &Name = "") {
895 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
896 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
899 template<typename InputIterator>
900 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
901 InputIterator ArgEnd, const Twine &Name = "") {
902 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
905 Value *CreateSelect(Value *C, Value *True, Value *False,
906 const Twine &Name = "") {
907 if (Constant *CC = dyn_cast<Constant>(C))
908 if (Constant *TC = dyn_cast<Constant>(True))
909 if (Constant *FC = dyn_cast<Constant>(False))
910 return Folder.CreateSelect(CC, TC, FC);
911 return Insert(SelectInst::Create(C, True, False), Name);
914 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
915 return Insert(new VAArgInst(List, Ty), Name);
918 Value *CreateExtractElement(Value *Vec, Value *Idx,
919 const Twine &Name = "") {
920 if (Constant *VC = dyn_cast<Constant>(Vec))
921 if (Constant *IC = dyn_cast<Constant>(Idx))
922 return Folder.CreateExtractElement(VC, IC);
923 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
926 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
927 const Twine &Name = "") {
928 if (Constant *VC = dyn_cast<Constant>(Vec))
929 if (Constant *NC = dyn_cast<Constant>(NewElt))
930 if (Constant *IC = dyn_cast<Constant>(Idx))
931 return Folder.CreateInsertElement(VC, NC, IC);
932 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
935 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
936 const Twine &Name = "") {
937 if (Constant *V1C = dyn_cast<Constant>(V1))
938 if (Constant *V2C = dyn_cast<Constant>(V2))
939 if (Constant *MC = dyn_cast<Constant>(Mask))
940 return Folder.CreateShuffleVector(V1C, V2C, MC);
941 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
944 Value *CreateExtractValue(Value *Agg, unsigned Idx,
945 const Twine &Name = "") {
946 if (Constant *AggC = dyn_cast<Constant>(Agg))
947 return Folder.CreateExtractValue(AggC, &Idx, 1);
948 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
951 template<typename InputIterator>
952 Value *CreateExtractValue(Value *Agg,
953 InputIterator IdxBegin,
954 InputIterator IdxEnd,
955 const Twine &Name = "") {
956 if (Constant *AggC = dyn_cast<Constant>(Agg))
957 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
958 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
961 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
962 const Twine &Name = "") {
963 if (Constant *AggC = dyn_cast<Constant>(Agg))
964 if (Constant *ValC = dyn_cast<Constant>(Val))
965 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
966 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
969 template<typename InputIterator>
970 Value *CreateInsertValue(Value *Agg, Value *Val,
971 InputIterator IdxBegin,
972 InputIterator IdxEnd,
973 const Twine &Name = "") {
974 if (Constant *AggC = dyn_cast<Constant>(Agg))
975 if (Constant *ValC = dyn_cast<Constant>(Val))
976 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
977 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
980 //===--------------------------------------------------------------------===//
981 // Utility creation methods
982 //===--------------------------------------------------------------------===//
984 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
985 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
986 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
990 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
991 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
992 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
996 /// CreatePtrDiff - Return the i64 difference between two pointer values,
997 /// dividing out the size of the pointed-to objects. This is intended to
998 /// implement C-style pointer subtraction. As such, the pointers must be
999 /// appropriately aligned for their element types and pointing into the
1001 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1002 assert(LHS->getType() == RHS->getType() &&
1003 "Pointer subtraction operand types must match!");
1004 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1005 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1006 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1007 Value *Difference = CreateSub(LHS_int, RHS_int);
1008 return CreateExactSDiv(Difference,
1009 ConstantExpr::getSizeOf(ArgType->getElementType()),