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 DebugLoc CurDbgLocation;
46 BasicBlock::iterator InsertPt;
50 IRBuilderBase(LLVMContext &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; }
67 LLVMContext &getContext() const { return Context; }
69 /// SetInsertPoint - This specifies that created instructions should be
70 /// appended to the end of the specified block.
71 void SetInsertPoint(BasicBlock *TheBB) {
76 /// SetInsertPoint - This specifies that created instructions should be
77 /// inserted at the specified point.
78 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
83 /// SetCurrentDebugLocation - Set location information used by debugging
85 void SetCurrentDebugLocation(const DebugLoc &L) {
89 /// getCurrentDebugLocation - Get location information used by debugging
91 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
93 /// SetInstDebugLocation - If this builder has a current debug location, set
94 /// it on the specified instruction.
95 void SetInstDebugLocation(Instruction *I) const {
96 if (!CurDbgLocation.isUnknown())
97 I->setDebugLoc(CurDbgLocation);
100 //===--------------------------------------------------------------------===//
101 // Miscellaneous creation methods.
102 //===--------------------------------------------------------------------===//
104 /// CreateGlobalString - Make a new global variable with an initializer that
105 /// has array of i8 type filled in with the nul terminated string value
106 /// specified. If Name is specified, it is the name of the global variable
108 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
110 //===--------------------------------------------------------------------===//
111 // Type creation methods
112 //===--------------------------------------------------------------------===//
114 /// getInt1Ty - Fetch the type representing a single bit
115 const Type *getInt1Ty() {
116 return Type::getInt1Ty(Context);
119 /// getInt8Ty - Fetch the type representing an 8-bit integer.
120 const Type *getInt8Ty() {
121 return Type::getInt8Ty(Context);
124 /// getInt16Ty - Fetch the type representing a 16-bit integer.
125 const Type *getInt16Ty() {
126 return Type::getInt16Ty(Context);
129 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
130 const Type *getInt32Ty() {
131 return Type::getInt32Ty(Context);
134 /// getInt64Ty - Fetch the type representing a 64-bit integer.
135 const Type *getInt64Ty() {
136 return Type::getInt64Ty(Context);
139 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
140 const Type *getFloatTy() {
141 return Type::getFloatTy(Context);
144 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
145 const Type *getDoubleTy() {
146 return Type::getDoubleTy(Context);
149 /// getVoidTy - Fetch the type representing void.
150 const Type *getVoidTy() {
151 return Type::getVoidTy(Context);
154 const Type *getInt8PtrTy() {
155 return Type::getInt8PtrTy(Context);
158 /// getCurrentFunctionReturnType - Get the return type of the current function
159 /// that we're emitting into.
160 const Type *getCurrentFunctionReturnType() const;
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);
219 if (!getCurrentDebugLocation().isUnknown())
220 this->SetInstDebugLocation(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 Value *V = UndefValue::get(getCurrentFunctionReturnType());
249 for (unsigned i = 0; i != N; ++i)
250 V = CreateInsertValue(V, retVals[i], i, "mrv");
251 return Insert(ReturnInst::Create(Context, V));
254 /// CreateBr - Create an unconditional 'br label X' instruction.
255 BranchInst *CreateBr(BasicBlock *Dest) {
256 return Insert(BranchInst::Create(Dest));
259 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
261 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
262 return Insert(BranchInst::Create(True, False, Cond));
265 /// CreateSwitch - Create a switch instruction with the specified value,
266 /// default dest, and with a hint for the number of cases that will be added
267 /// (for efficient allocation).
268 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
269 return Insert(SwitchInst::Create(V, Dest, NumCases));
272 /// CreateIndirectBr - Create an indirect branch instruction with the
273 /// specified address operand, with an optional hint for the number of
274 /// destinations that will be added (for efficient allocation).
275 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
276 return Insert(IndirectBrInst::Create(Addr, NumDests));
279 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
280 BasicBlock *UnwindDest, const Twine &Name = "") {
281 Value *Args[] = { 0 };
282 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
285 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
286 BasicBlock *UnwindDest, Value *Arg1,
287 const Twine &Name = "") {
288 Value *Args[] = { Arg1 };
289 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
292 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
293 BasicBlock *UnwindDest, Value *Arg1,
294 Value *Arg2, Value *Arg3,
295 const Twine &Name = "") {
296 Value *Args[] = { Arg1, Arg2, Arg3 };
297 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
300 /// CreateInvoke - Create an invoke instruction.
301 template<typename InputIterator>
302 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
303 BasicBlock *UnwindDest, InputIterator ArgBegin,
304 InputIterator ArgEnd, const Twine &Name = "") {
305 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
306 ArgBegin, ArgEnd), Name);
309 UnwindInst *CreateUnwind() {
310 return Insert(new UnwindInst(Context));
313 UnreachableInst *CreateUnreachable() {
314 return Insert(new UnreachableInst(Context));
317 //===--------------------------------------------------------------------===//
318 // Instruction creation methods: Binary Operators
319 //===--------------------------------------------------------------------===//
321 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
322 if (Constant *LC = dyn_cast<Constant>(LHS))
323 if (Constant *RC = dyn_cast<Constant>(RHS))
324 return Folder.CreateAdd(LC, RC);
325 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
327 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
328 if (Constant *LC = dyn_cast<Constant>(LHS))
329 if (Constant *RC = dyn_cast<Constant>(RHS))
330 return Folder.CreateNSWAdd(LC, RC);
331 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
333 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
334 if (Constant *LC = dyn_cast<Constant>(LHS))
335 if (Constant *RC = dyn_cast<Constant>(RHS))
336 return Folder.CreateNUWAdd(LC, RC);
337 return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
339 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
340 if (Constant *LC = dyn_cast<Constant>(LHS))
341 if (Constant *RC = dyn_cast<Constant>(RHS))
342 return Folder.CreateFAdd(LC, RC);
343 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
345 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
346 if (Constant *LC = dyn_cast<Constant>(LHS))
347 if (Constant *RC = dyn_cast<Constant>(RHS))
348 return Folder.CreateSub(LC, RC);
349 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
351 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
352 if (Constant *LC = dyn_cast<Constant>(LHS))
353 if (Constant *RC = dyn_cast<Constant>(RHS))
354 return Folder.CreateNSWSub(LC, RC);
355 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
357 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
358 if (Constant *LC = dyn_cast<Constant>(LHS))
359 if (Constant *RC = dyn_cast<Constant>(RHS))
360 return Folder.CreateNUWSub(LC, RC);
361 return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
363 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
364 if (Constant *LC = dyn_cast<Constant>(LHS))
365 if (Constant *RC = dyn_cast<Constant>(RHS))
366 return Folder.CreateFSub(LC, RC);
367 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
369 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
370 if (Constant *LC = dyn_cast<Constant>(LHS))
371 if (Constant *RC = dyn_cast<Constant>(RHS))
372 return Folder.CreateMul(LC, RC);
373 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
375 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
376 if (Constant *LC = dyn_cast<Constant>(LHS))
377 if (Constant *RC = dyn_cast<Constant>(RHS))
378 return Folder.CreateNSWMul(LC, RC);
379 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
381 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
382 if (Constant *LC = dyn_cast<Constant>(LHS))
383 if (Constant *RC = dyn_cast<Constant>(RHS))
384 return Folder.CreateNUWMul(LC, RC);
385 return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
387 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
388 if (Constant *LC = dyn_cast<Constant>(LHS))
389 if (Constant *RC = dyn_cast<Constant>(RHS))
390 return Folder.CreateFMul(LC, RC);
391 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
393 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
394 if (Constant *LC = dyn_cast<Constant>(LHS))
395 if (Constant *RC = dyn_cast<Constant>(RHS))
396 return Folder.CreateUDiv(LC, RC);
397 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
399 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
400 if (Constant *LC = dyn_cast<Constant>(LHS))
401 if (Constant *RC = dyn_cast<Constant>(RHS))
402 return Folder.CreateSDiv(LC, RC);
403 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
405 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
406 if (Constant *LC = dyn_cast<Constant>(LHS))
407 if (Constant *RC = dyn_cast<Constant>(RHS))
408 return Folder.CreateExactSDiv(LC, RC);
409 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
411 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
412 if (Constant *LC = dyn_cast<Constant>(LHS))
413 if (Constant *RC = dyn_cast<Constant>(RHS))
414 return Folder.CreateFDiv(LC, RC);
415 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
417 Value *CreateURem(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.CreateURem(LC, RC);
421 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
423 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
424 if (Constant *LC = dyn_cast<Constant>(LHS))
425 if (Constant *RC = dyn_cast<Constant>(RHS))
426 return Folder.CreateSRem(LC, RC);
427 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
429 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
430 if (Constant *LC = dyn_cast<Constant>(LHS))
431 if (Constant *RC = dyn_cast<Constant>(RHS))
432 return Folder.CreateFRem(LC, RC);
433 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
435 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
436 if (Constant *LC = dyn_cast<Constant>(LHS))
437 if (Constant *RC = dyn_cast<Constant>(RHS))
438 return Folder.CreateShl(LC, RC);
439 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
441 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
442 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
443 if (Constant *LC = dyn_cast<Constant>(LHS))
444 return Folder.CreateShl(LC, RHSC);
445 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
448 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
449 if (Constant *LC = dyn_cast<Constant>(LHS))
450 if (Constant *RC = dyn_cast<Constant>(RHS))
451 return Folder.CreateLShr(LC, RC);
452 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
454 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
455 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
456 if (Constant *LC = dyn_cast<Constant>(LHS))
457 return Folder.CreateLShr(LC, RHSC);
458 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
461 Value *CreateAShr(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.CreateAShr(LC, RC);
465 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
467 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
468 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
469 if (Constant *LC = dyn_cast<Constant>(LHS))
470 return Folder.CreateSShr(LC, RHSC);
471 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
474 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
475 if (Constant *RC = dyn_cast<Constant>(RHS)) {
476 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
477 return LHS; // LHS & -1 -> LHS
478 if (Constant *LC = dyn_cast<Constant>(LHS))
479 return Folder.CreateAnd(LC, RC);
481 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
483 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
484 if (Constant *RC = dyn_cast<Constant>(RHS)) {
485 if (RC->isNullValue())
486 return LHS; // LHS | 0 -> LHS
487 if (Constant *LC = dyn_cast<Constant>(LHS))
488 return Folder.CreateOr(LC, RC);
490 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
492 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
493 if (Constant *LC = dyn_cast<Constant>(LHS))
494 if (Constant *RC = dyn_cast<Constant>(RHS))
495 return Folder.CreateXor(LC, RC);
496 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
499 Value *CreateBinOp(Instruction::BinaryOps Opc,
500 Value *LHS, Value *RHS, const Twine &Name = "") {
501 if (Constant *LC = dyn_cast<Constant>(LHS))
502 if (Constant *RC = dyn_cast<Constant>(RHS))
503 return Folder.CreateBinOp(Opc, LC, RC);
504 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
507 Value *CreateNeg(Value *V, const Twine &Name = "") {
508 if (Constant *VC = dyn_cast<Constant>(V))
509 return Folder.CreateNeg(VC);
510 return Insert(BinaryOperator::CreateNeg(V), Name);
512 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
513 if (Constant *VC = dyn_cast<Constant>(V))
514 return Folder.CreateNSWNeg(VC);
515 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
517 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
518 if (Constant *VC = dyn_cast<Constant>(V))
519 return Folder.CreateNUWNeg(VC);
520 return Insert(BinaryOperator::CreateNUWNeg(V), Name);
522 Value *CreateFNeg(Value *V, const Twine &Name = "") {
523 if (Constant *VC = dyn_cast<Constant>(V))
524 return Folder.CreateFNeg(VC);
525 return Insert(BinaryOperator::CreateFNeg(V), Name);
527 Value *CreateNot(Value *V, const Twine &Name = "") {
528 if (Constant *VC = dyn_cast<Constant>(V))
529 return Folder.CreateNot(VC);
530 return Insert(BinaryOperator::CreateNot(V), Name);
533 //===--------------------------------------------------------------------===//
534 // Instruction creation methods: Memory Instructions
535 //===--------------------------------------------------------------------===//
537 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
538 const Twine &Name = "") {
539 return Insert(new AllocaInst(Ty, ArraySize), Name);
541 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
542 // converting the string to 'bool' for the isVolatile parameter.
543 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
544 return Insert(new LoadInst(Ptr), Name);
546 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
547 return Insert(new LoadInst(Ptr), Name);
549 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
550 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
552 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
553 return Insert(new StoreInst(Val, Ptr, isVolatile));
555 template<typename InputIterator>
556 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
557 const Twine &Name = "") {
558 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
559 // Every index must be constant.
561 for (i = IdxBegin; i < IdxEnd; ++i)
562 if (!isa<Constant>(*i))
565 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
567 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
569 template<typename InputIterator>
570 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
571 const Twine &Name = "") {
572 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
573 // Every index must be constant.
575 for (i = IdxBegin; i < IdxEnd; ++i)
576 if (!isa<Constant>(*i))
579 return Folder.CreateInBoundsGetElementPtr(PC,
583 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
586 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
587 if (Constant *PC = dyn_cast<Constant>(Ptr))
588 if (Constant *IC = dyn_cast<Constant>(Idx))
589 return Folder.CreateGetElementPtr(PC, &IC, 1);
590 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
592 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
593 if (Constant *PC = dyn_cast<Constant>(Ptr))
594 if (Constant *IC = dyn_cast<Constant>(Idx))
595 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
596 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
598 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
599 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
601 if (Constant *PC = dyn_cast<Constant>(Ptr))
602 return Folder.CreateGetElementPtr(PC, &Idx, 1);
604 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
606 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
607 const Twine &Name = "") {
608 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
610 if (Constant *PC = dyn_cast<Constant>(Ptr))
611 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
613 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
615 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
616 const Twine &Name = "") {
618 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
619 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
622 if (Constant *PC = dyn_cast<Constant>(Ptr))
623 return Folder.CreateGetElementPtr(PC, Idxs, 2);
625 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
627 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
628 const Twine &Name = "") {
630 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
631 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
634 if (Constant *PC = dyn_cast<Constant>(Ptr))
635 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
637 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
639 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
640 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
642 if (Constant *PC = dyn_cast<Constant>(Ptr))
643 return Folder.CreateGetElementPtr(PC, &Idx, 1);
645 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
647 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
648 const Twine &Name = "") {
649 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
651 if (Constant *PC = dyn_cast<Constant>(Ptr))
652 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
654 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
656 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
657 const Twine &Name = "") {
659 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
660 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
663 if (Constant *PC = dyn_cast<Constant>(Ptr))
664 return Folder.CreateGetElementPtr(PC, Idxs, 2);
666 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
668 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
669 const Twine &Name = "") {
671 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
672 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
675 if (Constant *PC = dyn_cast<Constant>(Ptr))
676 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
678 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
680 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
681 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
684 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
685 /// with "i8*" type instead of a pointer to array of i8.
686 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
687 Value *gv = CreateGlobalString(Str, Name);
688 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
689 Value *Args[] = { zero, zero };
690 return CreateInBoundsGEP(gv, Args, Args+2, Name);
693 //===--------------------------------------------------------------------===//
694 // Instruction creation methods: Cast/Conversion Operators
695 //===--------------------------------------------------------------------===//
697 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
698 return CreateCast(Instruction::Trunc, V, DestTy, Name);
700 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
701 return CreateCast(Instruction::ZExt, V, DestTy, Name);
703 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
704 return CreateCast(Instruction::SExt, V, DestTy, Name);
706 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
707 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
709 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
710 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
712 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
713 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
715 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
716 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
718 Value *CreateFPTrunc(Value *V, const Type *DestTy,
719 const Twine &Name = "") {
720 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
722 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
723 return CreateCast(Instruction::FPExt, V, DestTy, Name);
725 Value *CreatePtrToInt(Value *V, const Type *DestTy,
726 const Twine &Name = "") {
727 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
729 Value *CreateIntToPtr(Value *V, const Type *DestTy,
730 const Twine &Name = "") {
731 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
733 Value *CreateBitCast(Value *V, const Type *DestTy,
734 const Twine &Name = "") {
735 return CreateCast(Instruction::BitCast, V, DestTy, Name);
737 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
738 const Twine &Name = "") {
739 if (V->getType() == DestTy)
741 if (Constant *VC = dyn_cast<Constant>(V))
742 return Folder.CreateZExtOrBitCast(VC, DestTy);
743 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
745 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
746 const Twine &Name = "") {
747 if (V->getType() == DestTy)
749 if (Constant *VC = dyn_cast<Constant>(V))
750 return Folder.CreateSExtOrBitCast(VC, DestTy);
751 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
753 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
754 const Twine &Name = "") {
755 if (V->getType() == DestTy)
757 if (Constant *VC = dyn_cast<Constant>(V))
758 return Folder.CreateTruncOrBitCast(VC, DestTy);
759 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
761 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
762 const Twine &Name = "") {
763 if (V->getType() == DestTy)
765 if (Constant *VC = dyn_cast<Constant>(V))
766 return Folder.CreateCast(Op, VC, DestTy);
767 return Insert(CastInst::Create(Op, V, DestTy), Name);
769 Value *CreatePointerCast(Value *V, const Type *DestTy,
770 const Twine &Name = "") {
771 if (V->getType() == DestTy)
773 if (Constant *VC = dyn_cast<Constant>(V))
774 return Folder.CreatePointerCast(VC, DestTy);
775 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
777 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
778 const Twine &Name = "") {
779 if (V->getType() == DestTy)
781 if (Constant *VC = dyn_cast<Constant>(V))
782 return Folder.CreateIntCast(VC, DestTy, isSigned);
783 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
786 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
787 // error, instead of converting the string to bool for the isSigned parameter.
788 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
790 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
791 if (V->getType() == DestTy)
793 if (Constant *VC = dyn_cast<Constant>(V))
794 return Folder.CreateFPCast(VC, DestTy);
795 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
798 //===--------------------------------------------------------------------===//
799 // Instruction creation methods: Compare Instructions
800 //===--------------------------------------------------------------------===//
802 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
803 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
805 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
806 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
808 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
809 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
811 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
812 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
814 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
815 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
817 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
818 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
820 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
821 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
823 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
824 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
826 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
827 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
829 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
830 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
833 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
834 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
836 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
837 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
839 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
840 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
842 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
843 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
845 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
846 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
848 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
849 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
851 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
852 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
854 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
855 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
857 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
858 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
860 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
861 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
863 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
864 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
866 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
867 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
869 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
870 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
872 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
873 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
876 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
877 const Twine &Name = "") {
878 if (Constant *LC = dyn_cast<Constant>(LHS))
879 if (Constant *RC = dyn_cast<Constant>(RHS))
880 return Folder.CreateICmp(P, LC, RC);
881 return Insert(new ICmpInst(P, LHS, RHS), Name);
883 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
884 const Twine &Name = "") {
885 if (Constant *LC = dyn_cast<Constant>(LHS))
886 if (Constant *RC = dyn_cast<Constant>(RHS))
887 return Folder.CreateFCmp(P, LC, RC);
888 return Insert(new FCmpInst(P, LHS, RHS), Name);
891 //===--------------------------------------------------------------------===//
892 // Instruction creation methods: Other Instructions
893 //===--------------------------------------------------------------------===//
895 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
896 return Insert(PHINode::Create(Ty), Name);
899 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
900 return Insert(CallInst::Create(Callee), Name);
902 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
903 return Insert(CallInst::Create(Callee, Arg), Name);
905 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
906 const Twine &Name = "") {
907 Value *Args[] = { Arg1, Arg2 };
908 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
910 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
911 const Twine &Name = "") {
912 Value *Args[] = { Arg1, Arg2, Arg3 };
913 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
915 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
916 Value *Arg4, const Twine &Name = "") {
917 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
918 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
921 template<typename InputIterator>
922 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
923 InputIterator ArgEnd, const Twine &Name = "") {
924 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
927 Value *CreateSelect(Value *C, Value *True, Value *False,
928 const Twine &Name = "") {
929 if (Constant *CC = dyn_cast<Constant>(C))
930 if (Constant *TC = dyn_cast<Constant>(True))
931 if (Constant *FC = dyn_cast<Constant>(False))
932 return Folder.CreateSelect(CC, TC, FC);
933 return Insert(SelectInst::Create(C, True, False), Name);
936 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
937 return Insert(new VAArgInst(List, Ty), Name);
940 Value *CreateExtractElement(Value *Vec, Value *Idx,
941 const Twine &Name = "") {
942 if (Constant *VC = dyn_cast<Constant>(Vec))
943 if (Constant *IC = dyn_cast<Constant>(Idx))
944 return Folder.CreateExtractElement(VC, IC);
945 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
948 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
949 const Twine &Name = "") {
950 if (Constant *VC = dyn_cast<Constant>(Vec))
951 if (Constant *NC = dyn_cast<Constant>(NewElt))
952 if (Constant *IC = dyn_cast<Constant>(Idx))
953 return Folder.CreateInsertElement(VC, NC, IC);
954 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
957 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
958 const Twine &Name = "") {
959 if (Constant *V1C = dyn_cast<Constant>(V1))
960 if (Constant *V2C = dyn_cast<Constant>(V2))
961 if (Constant *MC = dyn_cast<Constant>(Mask))
962 return Folder.CreateShuffleVector(V1C, V2C, MC);
963 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
966 Value *CreateExtractValue(Value *Agg, unsigned Idx,
967 const Twine &Name = "") {
968 if (Constant *AggC = dyn_cast<Constant>(Agg))
969 return Folder.CreateExtractValue(AggC, &Idx, 1);
970 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
973 template<typename InputIterator>
974 Value *CreateExtractValue(Value *Agg,
975 InputIterator IdxBegin,
976 InputIterator IdxEnd,
977 const Twine &Name = "") {
978 if (Constant *AggC = dyn_cast<Constant>(Agg))
979 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
980 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
983 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
984 const Twine &Name = "") {
985 if (Constant *AggC = dyn_cast<Constant>(Agg))
986 if (Constant *ValC = dyn_cast<Constant>(Val))
987 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
988 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
991 template<typename InputIterator>
992 Value *CreateInsertValue(Value *Agg, Value *Val,
993 InputIterator IdxBegin,
994 InputIterator IdxEnd,
995 const Twine &Name = "") {
996 if (Constant *AggC = dyn_cast<Constant>(Agg))
997 if (Constant *ValC = dyn_cast<Constant>(Val))
998 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
999 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1002 //===--------------------------------------------------------------------===//
1003 // Utility creation methods
1004 //===--------------------------------------------------------------------===//
1006 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1007 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1008 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1012 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1013 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1014 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1018 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1019 /// dividing out the size of the pointed-to objects. This is intended to
1020 /// implement C-style pointer subtraction. As such, the pointers must be
1021 /// appropriately aligned for their element types and pointing into the
1023 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1024 assert(LHS->getType() == RHS->getType() &&
1025 "Pointer subtraction operand types must match!");
1026 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1027 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1028 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1029 Value *Difference = CreateSub(LHS_int, RHS_int);
1030 return CreateExactSDiv(Difference,
1031 ConstantExpr::getSizeOf(ArgType->getElementType()),