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/Instructions.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ConstantFolder.h"
26 /// IRBuilderDefaultInserter - This provides the default implementation of the
27 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
28 /// created by IRBuilder and needs to be inserted. By default, this inserts the
29 /// instruction at the insertion point.
30 template <bool preserveNames = true>
31 class IRBuilderDefaultInserter {
33 void InsertHelper(Instruction *I, const Twine &Name,
34 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
35 if (BB) BB->getInstList().insert(InsertPt, I);
41 /// IRBuilderBase - Common base class shared among various IRBuilders.
43 MDNode *CurDbgLocation;
46 BasicBlock::iterator InsertPt;
50 IRBuilderBase(LLVMContext &context)
51 : CurDbgLocation(0), Context(context) {
52 ClearInsertionPoint();
55 //===--------------------------------------------------------------------===//
56 // Builder configuration methods
57 //===--------------------------------------------------------------------===//
59 /// ClearInsertionPoint - Clear the insertion point: created instructions will
60 /// not be inserted into a block.
61 void ClearInsertionPoint() {
65 BasicBlock *GetInsertBlock() const { return BB; }
66 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
68 /// SetInsertPoint - This specifies that created instructions should be
69 /// appended to the end of the specified block.
70 void SetInsertPoint(BasicBlock *TheBB) {
75 /// SetInsertPoint - This specifies that created instructions should be
76 /// inserted at the specified point.
77 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
82 /// SetCurrentDebugLocation - Set location information used by debugging
84 void SetCurrentDebugLocation(MDNode *L) {
88 /// SetCurrentDebugLocation - Set location information used by debugging
90 MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
92 /// SetInstDebugLocation - If this builder has a current debug location, set
93 /// it on the specified instruction.
94 void SetInstDebugLocation(Instruction *I) const {
96 I->setDbgMetadata(CurDbgLocation);
99 //===--------------------------------------------------------------------===//
100 // Miscellaneous creation methods.
101 //===--------------------------------------------------------------------===//
103 /// CreateGlobalString - Make a new global variable with an initializer that
104 /// has array of i8 type filled in with the nul terminated string value
105 /// specified. If Name is specified, it is the name of the global variable
107 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
109 //===--------------------------------------------------------------------===//
110 // Type creation methods
111 //===--------------------------------------------------------------------===//
113 /// getInt1Ty - Fetch the type representing a single bit
114 const Type *getInt1Ty() {
115 return Type::getInt1Ty(Context);
118 /// getInt8Ty - Fetch the type representing an 8-bit integer.
119 const Type *getInt8Ty() {
120 return Type::getInt8Ty(Context);
123 /// getInt16Ty - Fetch the type representing a 16-bit integer.
124 const Type *getInt16Ty() {
125 return Type::getInt16Ty(Context);
128 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
129 const Type *getInt32Ty() {
130 return Type::getInt32Ty(Context);
133 /// getInt64Ty - Fetch the type representing a 64-bit integer.
134 const Type *getInt64Ty() {
135 return Type::getInt64Ty(Context);
138 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
139 const Type *getFloatTy() {
140 return Type::getFloatTy(Context);
143 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
144 const Type *getDoubleTy() {
145 return Type::getDoubleTy(Context);
148 /// getVoidTy - Fetch the type representing void.
149 const Type *getVoidTy() {
150 return Type::getVoidTy(Context);
153 const Type *getInt8PtrTy() {
154 return Type::getInt8PtrTy(Context);
157 /// getCurrentFunctionReturnType - Get the return type of the current function
158 /// that we're emitting into.
159 const Type *getCurrentFunctionReturnType() const;
162 /// IRBuilder - This provides a uniform API for creating instructions and
163 /// inserting them into a basic block: either at the end of a BasicBlock, or
164 /// at a specific iterator location in a block.
166 /// Note that the builder does not expose the full generality of LLVM
167 /// instructions. For access to extra instruction properties, use the mutators
168 /// (e.g. setVolatile) on the instructions after they have been created.
169 /// The first template argument handles whether or not to preserve names in the
170 /// final instruction output. This defaults to on. The second template argument
171 /// specifies a class to use for creating constants. This defaults to creating
172 /// minimally folded constants. The fourth template argument allows clients to
173 /// specify custom insertion hooks that are called on every newly created
175 template<bool preserveNames = true, typename T = ConstantFolder,
176 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
177 class IRBuilder : public IRBuilderBase, public Inserter {
180 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
181 : IRBuilderBase(C), Inserter(I), Folder(F) {
184 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
187 explicit IRBuilder(BasicBlock *TheBB, const T &F)
188 : IRBuilderBase(TheBB->getContext()), Folder(F) {
189 SetInsertPoint(TheBB);
192 explicit IRBuilder(BasicBlock *TheBB)
193 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
194 SetInsertPoint(TheBB);
197 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
198 : IRBuilderBase(TheBB->getContext()), Folder(F) {
199 SetInsertPoint(TheBB, IP);
202 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
203 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
204 SetInsertPoint(TheBB, IP);
207 /// getFolder - Get the constant folder being used.
208 const T &getFolder() { return Folder; }
210 /// isNamePreserving - Return true if this builder is configured to actually
211 /// add the requested names to IR created through it.
212 bool isNamePreserving() const { return preserveNames; }
214 /// Insert - Insert and return the specified instruction.
215 template<typename InstTy>
216 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
217 this->InsertHelper(I, Name, BB, InsertPt);
218 if (getCurrentDebugLocation() != 0)
219 this->SetInstDebugLocation(I);
223 //===--------------------------------------------------------------------===//
224 // Instruction creation methods: Terminators
225 //===--------------------------------------------------------------------===//
227 /// CreateRetVoid - Create a 'ret void' instruction.
228 ReturnInst *CreateRetVoid() {
229 return Insert(ReturnInst::Create(Context));
233 /// CreateRet - Create a 'ret <val>' instruction.
235 ReturnInst *CreateRet(Value *V) {
236 return Insert(ReturnInst::Create(Context, V));
239 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
240 /// with one Value from the retVals array each, that build a aggregate
241 /// return value one value at a time, and a ret instruction to return
242 /// the resulting aggregate value. This is a convenience function for
243 /// code that uses aggregate return values as a vehicle for having
244 /// multiple return values.
246 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
247 Value *V = UndefValue::get(getCurrentFunctionReturnType());
248 for (unsigned i = 0; i != N; ++i)
249 V = CreateInsertValue(V, retVals[i], i, "mrv");
250 return Insert(ReturnInst::Create(Context, V));
253 /// CreateBr - Create an unconditional 'br label X' instruction.
254 BranchInst *CreateBr(BasicBlock *Dest) {
255 return Insert(BranchInst::Create(Dest));
258 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
260 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
261 return Insert(BranchInst::Create(True, False, Cond));
264 /// CreateSwitch - Create a switch instruction with the specified value,
265 /// default dest, and with a hint for the number of cases that will be added
266 /// (for efficient allocation).
267 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
268 return Insert(SwitchInst::Create(V, Dest, NumCases));
271 /// CreateIndirectBr - Create an indirect branch instruction with the
272 /// specified address operand, with an optional hint for the number of
273 /// destinations that will be added (for efficient allocation).
274 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
275 return Insert(IndirectBrInst::Create(Addr, NumDests));
278 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
279 BasicBlock *UnwindDest, const Twine &Name = "") {
280 Value *Args[] = { 0 };
281 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
284 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
285 BasicBlock *UnwindDest, Value *Arg1,
286 const Twine &Name = "") {
287 Value *Args[] = { Arg1 };
288 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
291 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
292 BasicBlock *UnwindDest, Value *Arg1,
293 Value *Arg2, Value *Arg3,
294 const Twine &Name = "") {
295 Value *Args[] = { Arg1, Arg2, Arg3 };
296 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
299 /// CreateInvoke - Create an invoke instruction.
300 template<typename InputIterator>
301 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
302 BasicBlock *UnwindDest, InputIterator ArgBegin,
303 InputIterator ArgEnd, const Twine &Name = "") {
304 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
305 ArgBegin, ArgEnd), Name);
308 UnwindInst *CreateUnwind() {
309 return Insert(new UnwindInst(Context));
312 UnreachableInst *CreateUnreachable() {
313 return Insert(new UnreachableInst(Context));
316 //===--------------------------------------------------------------------===//
317 // Instruction creation methods: Binary Operators
318 //===--------------------------------------------------------------------===//
320 Value *CreateAdd(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.CreateAdd(LC, RC);
324 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
326 Value *CreateNSWAdd(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.CreateNSWAdd(LC, RC);
330 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
332 Value *CreateNUWAdd(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.CreateNUWAdd(LC, RC);
336 return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
338 Value *CreateFAdd(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.CreateFAdd(LC, RC);
342 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
344 Value *CreateSub(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.CreateSub(LC, RC);
348 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
350 Value *CreateNSWSub(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.CreateNSWSub(LC, RC);
354 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
356 Value *CreateNUWSub(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.CreateNUWSub(LC, RC);
360 return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
362 Value *CreateFSub(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.CreateFSub(LC, RC);
366 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
368 Value *CreateMul(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.CreateMul(LC, RC);
372 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
374 Value *CreateNSWMul(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.CreateNSWMul(LC, RC);
378 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
380 Value *CreateNUWMul(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.CreateNUWMul(LC, RC);
384 return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
386 Value *CreateFMul(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.CreateFMul(LC, RC);
390 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
392 Value *CreateUDiv(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.CreateUDiv(LC, RC);
396 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
398 Value *CreateSDiv(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.CreateSDiv(LC, RC);
402 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
404 Value *CreateExactSDiv(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.CreateExactSDiv(LC, RC);
408 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
410 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
411 if (Constant *LC = dyn_cast<Constant>(LHS))
412 if (Constant *RC = dyn_cast<Constant>(RHS))
413 return Folder.CreateFDiv(LC, RC);
414 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
416 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
417 if (Constant *LC = dyn_cast<Constant>(LHS))
418 if (Constant *RC = dyn_cast<Constant>(RHS))
419 return Folder.CreateURem(LC, RC);
420 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
422 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
423 if (Constant *LC = dyn_cast<Constant>(LHS))
424 if (Constant *RC = dyn_cast<Constant>(RHS))
425 return Folder.CreateSRem(LC, RC);
426 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
428 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
429 if (Constant *LC = dyn_cast<Constant>(LHS))
430 if (Constant *RC = dyn_cast<Constant>(RHS))
431 return Folder.CreateFRem(LC, RC);
432 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
434 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
435 if (Constant *LC = dyn_cast<Constant>(LHS))
436 if (Constant *RC = dyn_cast<Constant>(RHS))
437 return Folder.CreateShl(LC, RC);
438 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
440 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
441 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
442 if (Constant *LC = dyn_cast<Constant>(LHS))
443 return Folder.CreateShl(LC, RHSC);
444 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
447 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
448 if (Constant *LC = dyn_cast<Constant>(LHS))
449 if (Constant *RC = dyn_cast<Constant>(RHS))
450 return Folder.CreateLShr(LC, RC);
451 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
453 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
454 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
455 if (Constant *LC = dyn_cast<Constant>(LHS))
456 return Folder.CreateLShr(LC, RHSC);
457 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
460 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
461 if (Constant *LC = dyn_cast<Constant>(LHS))
462 if (Constant *RC = dyn_cast<Constant>(RHS))
463 return Folder.CreateAShr(LC, RC);
464 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
466 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
467 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
468 if (Constant *LC = dyn_cast<Constant>(LHS))
469 return Folder.CreateSShr(LC, RHSC);
470 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
473 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
474 if (Constant *RC = dyn_cast<Constant>(RHS)) {
475 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
476 return LHS; // LHS & -1 -> LHS
477 if (Constant *LC = dyn_cast<Constant>(LHS))
478 return Folder.CreateAnd(LC, RC);
480 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
482 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
483 if (Constant *RC = dyn_cast<Constant>(RHS)) {
484 if (RC->isNullValue())
485 return LHS; // LHS | 0 -> LHS
486 if (Constant *LC = dyn_cast<Constant>(LHS))
487 return Folder.CreateOr(LC, RC);
489 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
491 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
492 if (Constant *LC = dyn_cast<Constant>(LHS))
493 if (Constant *RC = dyn_cast<Constant>(RHS))
494 return Folder.CreateXor(LC, RC);
495 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
498 Value *CreateBinOp(Instruction::BinaryOps Opc,
499 Value *LHS, Value *RHS, const Twine &Name = "") {
500 if (Constant *LC = dyn_cast<Constant>(LHS))
501 if (Constant *RC = dyn_cast<Constant>(RHS))
502 return Folder.CreateBinOp(Opc, LC, RC);
503 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
506 Value *CreateNeg(Value *V, const Twine &Name = "") {
507 if (Constant *VC = dyn_cast<Constant>(V))
508 return Folder.CreateNeg(VC);
509 return Insert(BinaryOperator::CreateNeg(V), Name);
511 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
512 if (Constant *VC = dyn_cast<Constant>(V))
513 return Folder.CreateNSWNeg(VC);
514 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
516 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
517 if (Constant *VC = dyn_cast<Constant>(V))
518 return Folder.CreateNUWNeg(VC);
519 return Insert(BinaryOperator::CreateNUWNeg(V), Name);
521 Value *CreateFNeg(Value *V, const Twine &Name = "") {
522 if (Constant *VC = dyn_cast<Constant>(V))
523 return Folder.CreateFNeg(VC);
524 return Insert(BinaryOperator::CreateFNeg(V), Name);
526 Value *CreateNot(Value *V, const Twine &Name = "") {
527 if (Constant *VC = dyn_cast<Constant>(V))
528 return Folder.CreateNot(VC);
529 return Insert(BinaryOperator::CreateNot(V), Name);
532 //===--------------------------------------------------------------------===//
533 // Instruction creation methods: Memory Instructions
534 //===--------------------------------------------------------------------===//
536 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
537 const Twine &Name = "") {
538 return Insert(new AllocaInst(Ty, ArraySize), Name);
540 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
541 // converting the string to 'bool' for the isVolatile parameter.
542 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
543 return Insert(new LoadInst(Ptr), Name);
545 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
546 return Insert(new LoadInst(Ptr), Name);
548 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
549 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
551 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
552 return Insert(new StoreInst(Val, Ptr, isVolatile));
554 template<typename InputIterator>
555 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
556 const Twine &Name = "") {
557 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
558 // Every index must be constant.
560 for (i = IdxBegin; i < IdxEnd; ++i)
561 if (!isa<Constant>(*i))
564 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
566 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
568 template<typename InputIterator>
569 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
570 const Twine &Name = "") {
571 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
572 // Every index must be constant.
574 for (i = IdxBegin; i < IdxEnd; ++i)
575 if (!isa<Constant>(*i))
578 return Folder.CreateInBoundsGetElementPtr(PC,
582 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
585 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
586 if (Constant *PC = dyn_cast<Constant>(Ptr))
587 if (Constant *IC = dyn_cast<Constant>(Idx))
588 return Folder.CreateGetElementPtr(PC, &IC, 1);
589 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
591 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
592 if (Constant *PC = dyn_cast<Constant>(Ptr))
593 if (Constant *IC = dyn_cast<Constant>(Idx))
594 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
595 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
597 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
598 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
600 if (Constant *PC = dyn_cast<Constant>(Ptr))
601 return Folder.CreateGetElementPtr(PC, &Idx, 1);
603 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
605 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
606 const Twine &Name = "") {
607 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
609 if (Constant *PC = dyn_cast<Constant>(Ptr))
610 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
612 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
614 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
615 const Twine &Name = "") {
617 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
618 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
621 if (Constant *PC = dyn_cast<Constant>(Ptr))
622 return Folder.CreateGetElementPtr(PC, Idxs, 2);
624 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
626 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
627 const Twine &Name = "") {
629 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
630 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
633 if (Constant *PC = dyn_cast<Constant>(Ptr))
634 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
636 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
638 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
639 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
641 if (Constant *PC = dyn_cast<Constant>(Ptr))
642 return Folder.CreateGetElementPtr(PC, &Idx, 1);
644 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
646 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
647 const Twine &Name = "") {
648 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
650 if (Constant *PC = dyn_cast<Constant>(Ptr))
651 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
653 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
655 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
656 const Twine &Name = "") {
658 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
659 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
662 if (Constant *PC = dyn_cast<Constant>(Ptr))
663 return Folder.CreateGetElementPtr(PC, Idxs, 2);
665 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
667 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
668 const Twine &Name = "") {
670 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
671 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
674 if (Constant *PC = dyn_cast<Constant>(Ptr))
675 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
677 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
679 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
680 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
683 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
684 /// with "i8*" type instead of a pointer to array of i8.
685 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
686 Value *gv = CreateGlobalString(Str, Name);
687 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
688 Value *Args[] = { zero, zero };
689 return CreateInBoundsGEP(gv, Args, Args+2, Name);
692 //===--------------------------------------------------------------------===//
693 // Instruction creation methods: Cast/Conversion Operators
694 //===--------------------------------------------------------------------===//
696 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
697 return CreateCast(Instruction::Trunc, V, DestTy, Name);
699 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
700 return CreateCast(Instruction::ZExt, V, DestTy, Name);
702 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
703 return CreateCast(Instruction::SExt, V, DestTy, Name);
705 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
706 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
708 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
709 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
711 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
712 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
714 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
715 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
717 Value *CreateFPTrunc(Value *V, const Type *DestTy,
718 const Twine &Name = "") {
719 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
721 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
722 return CreateCast(Instruction::FPExt, V, DestTy, Name);
724 Value *CreatePtrToInt(Value *V, const Type *DestTy,
725 const Twine &Name = "") {
726 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
728 Value *CreateIntToPtr(Value *V, const Type *DestTy,
729 const Twine &Name = "") {
730 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
732 Value *CreateBitCast(Value *V, const Type *DestTy,
733 const Twine &Name = "") {
734 return CreateCast(Instruction::BitCast, V, DestTy, Name);
736 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
737 const Twine &Name = "") {
738 if (V->getType() == DestTy)
740 if (Constant *VC = dyn_cast<Constant>(V))
741 return Folder.CreateZExtOrBitCast(VC, DestTy);
742 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
744 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
745 const Twine &Name = "") {
746 if (V->getType() == DestTy)
748 if (Constant *VC = dyn_cast<Constant>(V))
749 return Folder.CreateSExtOrBitCast(VC, DestTy);
750 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
752 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
753 const Twine &Name = "") {
754 if (V->getType() == DestTy)
756 if (Constant *VC = dyn_cast<Constant>(V))
757 return Folder.CreateTruncOrBitCast(VC, DestTy);
758 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
760 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
761 const Twine &Name = "") {
762 if (V->getType() == DestTy)
764 if (Constant *VC = dyn_cast<Constant>(V))
765 return Folder.CreateCast(Op, VC, DestTy);
766 return Insert(CastInst::Create(Op, V, DestTy), Name);
768 Value *CreatePointerCast(Value *V, const Type *DestTy,
769 const Twine &Name = "") {
770 if (V->getType() == DestTy)
772 if (Constant *VC = dyn_cast<Constant>(V))
773 return Folder.CreatePointerCast(VC, DestTy);
774 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
776 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
777 const Twine &Name = "") {
778 if (V->getType() == DestTy)
780 if (Constant *VC = dyn_cast<Constant>(V))
781 return Folder.CreateIntCast(VC, DestTy, isSigned);
782 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
785 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
786 // error, instead of converting the string to bool for the isSigned parameter.
787 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
789 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
790 if (V->getType() == DestTy)
792 if (Constant *VC = dyn_cast<Constant>(V))
793 return Folder.CreateFPCast(VC, DestTy);
794 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
797 //===--------------------------------------------------------------------===//
798 // Instruction creation methods: Compare Instructions
799 //===--------------------------------------------------------------------===//
801 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
802 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
804 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
805 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
807 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
808 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
810 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
811 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
813 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
814 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
816 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
817 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
819 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
820 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
822 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
823 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
825 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
826 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
828 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
829 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
832 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
833 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
835 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
836 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
838 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
839 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
841 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
842 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
844 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
845 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
847 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
848 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
850 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
851 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
853 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
854 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
856 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
857 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
859 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
860 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
862 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
863 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
865 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
866 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
868 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
869 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
871 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
872 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
875 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
876 const Twine &Name = "") {
877 if (Constant *LC = dyn_cast<Constant>(LHS))
878 if (Constant *RC = dyn_cast<Constant>(RHS))
879 return Folder.CreateICmp(P, LC, RC);
880 return Insert(new ICmpInst(P, LHS, RHS), Name);
882 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
883 const Twine &Name = "") {
884 if (Constant *LC = dyn_cast<Constant>(LHS))
885 if (Constant *RC = dyn_cast<Constant>(RHS))
886 return Folder.CreateFCmp(P, LC, RC);
887 return Insert(new FCmpInst(P, LHS, RHS), Name);
890 //===--------------------------------------------------------------------===//
891 // Instruction creation methods: Other Instructions
892 //===--------------------------------------------------------------------===//
894 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
895 return Insert(PHINode::Create(Ty), Name);
898 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
899 return Insert(CallInst::Create(Callee), Name);
901 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
902 return Insert(CallInst::Create(Callee, Arg), Name);
904 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
905 const Twine &Name = "") {
906 Value *Args[] = { Arg1, Arg2 };
907 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
909 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
910 const Twine &Name = "") {
911 Value *Args[] = { Arg1, Arg2, Arg3 };
912 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
914 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
915 Value *Arg4, const Twine &Name = "") {
916 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
917 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
920 template<typename InputIterator>
921 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
922 InputIterator ArgEnd, const Twine &Name = "") {
923 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
926 Value *CreateSelect(Value *C, Value *True, Value *False,
927 const Twine &Name = "") {
928 if (Constant *CC = dyn_cast<Constant>(C))
929 if (Constant *TC = dyn_cast<Constant>(True))
930 if (Constant *FC = dyn_cast<Constant>(False))
931 return Folder.CreateSelect(CC, TC, FC);
932 return Insert(SelectInst::Create(C, True, False), Name);
935 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
936 return Insert(new VAArgInst(List, Ty), Name);
939 Value *CreateExtractElement(Value *Vec, Value *Idx,
940 const Twine &Name = "") {
941 if (Constant *VC = dyn_cast<Constant>(Vec))
942 if (Constant *IC = dyn_cast<Constant>(Idx))
943 return Folder.CreateExtractElement(VC, IC);
944 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
947 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
948 const Twine &Name = "") {
949 if (Constant *VC = dyn_cast<Constant>(Vec))
950 if (Constant *NC = dyn_cast<Constant>(NewElt))
951 if (Constant *IC = dyn_cast<Constant>(Idx))
952 return Folder.CreateInsertElement(VC, NC, IC);
953 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
956 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
957 const Twine &Name = "") {
958 if (Constant *V1C = dyn_cast<Constant>(V1))
959 if (Constant *V2C = dyn_cast<Constant>(V2))
960 if (Constant *MC = dyn_cast<Constant>(Mask))
961 return Folder.CreateShuffleVector(V1C, V2C, MC);
962 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
965 Value *CreateExtractValue(Value *Agg, unsigned Idx,
966 const Twine &Name = "") {
967 if (Constant *AggC = dyn_cast<Constant>(Agg))
968 return Folder.CreateExtractValue(AggC, &Idx, 1);
969 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
972 template<typename InputIterator>
973 Value *CreateExtractValue(Value *Agg,
974 InputIterator IdxBegin,
975 InputIterator IdxEnd,
976 const Twine &Name = "") {
977 if (Constant *AggC = dyn_cast<Constant>(Agg))
978 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
979 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
982 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
983 const Twine &Name = "") {
984 if (Constant *AggC = dyn_cast<Constant>(Agg))
985 if (Constant *ValC = dyn_cast<Constant>(Val))
986 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
987 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
990 template<typename InputIterator>
991 Value *CreateInsertValue(Value *Agg, Value *Val,
992 InputIterator IdxBegin,
993 InputIterator IdxEnd,
994 const Twine &Name = "") {
995 if (Constant *AggC = dyn_cast<Constant>(Agg))
996 if (Constant *ValC = dyn_cast<Constant>(Val))
997 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
998 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1001 //===--------------------------------------------------------------------===//
1002 // Utility creation methods
1003 //===--------------------------------------------------------------------===//
1005 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1006 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1007 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1011 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1012 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1013 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1017 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1018 /// dividing out the size of the pointed-to objects. This is intended to
1019 /// implement C-style pointer subtraction. As such, the pointers must be
1020 /// appropriately aligned for their element types and pointing into the
1022 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1023 assert(LHS->getType() == RHS->getType() &&
1024 "Pointer subtraction operand types must match!");
1025 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1026 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1027 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1028 Value *Difference = CreateSub(LHS_int, RHS_int);
1029 return CreateExactSDiv(Difference,
1030 ConstantExpr::getSizeOf(ArgType->getElementType()),