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.
44 MDNode *CurDbgLocation;
47 BasicBlock::iterator InsertPt;
51 IRBuilderBase(LLVMContext &context)
52 : DbgMDKind(0), CurDbgLocation(0), Context(context) {
53 ClearInsertionPoint();
56 //===--------------------------------------------------------------------===//
57 // Builder configuration methods
58 //===--------------------------------------------------------------------===//
60 /// ClearInsertionPoint - Clear the insertion point: created instructions will
61 /// not be inserted into a block.
62 void ClearInsertionPoint() {
66 BasicBlock *GetInsertBlock() const { return BB; }
67 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
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(MDNode *L);
86 MDNode *getCurrentDebugLocation() const { return CurDbgLocation; }
88 /// SetInstDebugLocation - If this builder has a current debug location, set
89 /// it on the specified instruction.
90 void SetInstDebugLocation(Instruction *I) const;
92 //===--------------------------------------------------------------------===//
93 // Miscellaneous creation methods.
94 //===--------------------------------------------------------------------===//
96 /// CreateGlobalString - Make a new global variable with an initializer that
97 /// has array of i8 type filled in the the nul terminated string value
98 /// specified. If Name is specified, it is the name of the global variable
100 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
102 //===--------------------------------------------------------------------===//
103 // Type creation methods
104 //===--------------------------------------------------------------------===//
106 /// getInt1Ty - Fetch the type representing a single bit
107 const Type *getInt1Ty() {
108 return Type::getInt1Ty(Context);
111 /// getInt8Ty - Fetch the type representing an 8-bit integer.
112 const Type *getInt8Ty() {
113 return Type::getInt8Ty(Context);
116 /// getInt16Ty - Fetch the type representing a 16-bit integer.
117 const Type *getInt16Ty() {
118 return Type::getInt16Ty(Context);
121 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
122 const Type *getInt32Ty() {
123 return Type::getInt32Ty(Context);
126 /// getInt64Ty - Fetch the type representing a 64-bit integer.
127 const Type *getInt64Ty() {
128 return Type::getInt64Ty(Context);
131 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
132 const Type *getFloatTy() {
133 return Type::getFloatTy(Context);
136 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
137 const Type *getDoubleTy() {
138 return Type::getDoubleTy(Context);
141 /// getVoidTy - Fetch the type representing void.
142 const Type *getVoidTy() {
143 return Type::getVoidTy(Context);
146 /// getCurrentFunctionReturnType - Get the return type of the current function
147 /// that we're emitting into.
148 const Type *getCurrentFunctionReturnType() const;
151 /// IRBuilder - This provides a uniform API for creating instructions and
152 /// inserting them into a basic block: either at the end of a BasicBlock, or
153 /// at a specific iterator location in a block.
155 /// Note that the builder does not expose the full generality of LLVM
156 /// instructions. For access to extra instruction properties, use the mutators
157 /// (e.g. setVolatile) on the instructions after they have been created.
158 /// The first template argument handles whether or not to preserve names in the
159 /// final instruction output. This defaults to on. The second template argument
160 /// specifies a class to use for creating constants. This defaults to creating
161 /// minimally folded constants. The fourth template argument allows clients to
162 /// specify custom insertion hooks that are called on every newly created
164 template<bool preserveNames = true, typename T = ConstantFolder,
165 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
166 class IRBuilder : public IRBuilderBase, public Inserter {
169 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
170 : IRBuilderBase(C), Inserter(I), Folder(F) {
173 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
176 explicit IRBuilder(BasicBlock *TheBB, const T &F)
177 : IRBuilderBase(TheBB->getContext()), Folder(F) {
178 SetInsertPoint(TheBB);
181 explicit IRBuilder(BasicBlock *TheBB)
182 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
183 SetInsertPoint(TheBB);
186 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
187 : IRBuilderBase(TheBB->getContext()), Folder(F) {
188 SetInsertPoint(TheBB, IP);
191 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
192 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
193 SetInsertPoint(TheBB, IP);
196 /// getFolder - Get the constant folder being used.
197 const T &getFolder() { return Folder; }
199 /// isNamePreserving - Return true if this builder is configured to actually
200 /// add the requested names to IR created through it.
201 bool isNamePreserving() const { return preserveNames; }
203 /// Insert - Insert and return the specified instruction.
204 template<typename InstTy>
205 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
206 this->InsertHelper(I, Name, BB, InsertPt);
207 if (getCurrentDebugLocation() != 0)
208 this->SetInstDebugLocation(I);
212 //===--------------------------------------------------------------------===//
213 // Instruction creation methods: Terminators
214 //===--------------------------------------------------------------------===//
216 /// CreateRetVoid - Create a 'ret void' instruction.
217 ReturnInst *CreateRetVoid() {
218 return Insert(ReturnInst::Create(Context));
222 /// CreateRet - Create a 'ret <val>' instruction.
224 ReturnInst *CreateRet(Value *V) {
225 return Insert(ReturnInst::Create(Context, V));
228 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
229 /// with one Value from the retVals array each, that build a aggregate
230 /// return value one value at a time, and a ret instruction to return
231 /// the resulting aggregate value. This is a convenience function for
232 /// code that uses aggregate return values as a vehicle for having
233 /// multiple return values.
235 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
236 Value *V = UndefValue::get(getCurrentFunctionReturnType());
237 for (unsigned i = 0; i != N; ++i)
238 V = CreateInsertValue(V, retVals[i], i, "mrv");
239 return Insert(ReturnInst::Create(Context, V));
242 /// CreateBr - Create an unconditional 'br label X' instruction.
243 BranchInst *CreateBr(BasicBlock *Dest) {
244 return Insert(BranchInst::Create(Dest));
247 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
249 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
250 return Insert(BranchInst::Create(True, False, Cond));
253 /// CreateSwitch - Create a switch instruction with the specified value,
254 /// default dest, and with a hint for the number of cases that will be added
255 /// (for efficient allocation).
256 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
257 return Insert(SwitchInst::Create(V, Dest, NumCases));
260 /// CreateIndirectBr - Create an indirect branch instruction with the
261 /// specified address operand, with an optional hint for the number of
262 /// destinations that will be added (for efficient allocation).
263 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
264 return Insert(IndirectBrInst::Create(Addr, NumDests));
267 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
268 BasicBlock *UnwindDest, const Twine &Name = "") {
269 Value *Args[] = { 0 };
270 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
273 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
274 BasicBlock *UnwindDest, Value *Arg1,
275 const Twine &Name = "") {
276 Value *Args[] = { Arg1 };
277 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
280 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
281 BasicBlock *UnwindDest, Value *Arg1,
282 Value *Arg2, Value *Arg3,
283 const Twine &Name = "") {
284 Value *Args[] = { Arg1, Arg2, Arg3 };
285 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
288 /// CreateInvoke - Create an invoke instruction.
289 template<typename InputIterator>
290 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
291 BasicBlock *UnwindDest, InputIterator ArgBegin,
292 InputIterator ArgEnd, const Twine &Name = "") {
293 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
294 ArgBegin, ArgEnd), Name);
297 UnwindInst *CreateUnwind() {
298 return Insert(new UnwindInst(Context));
301 UnreachableInst *CreateUnreachable() {
302 return Insert(new UnreachableInst(Context));
305 //===--------------------------------------------------------------------===//
306 // Instruction creation methods: Binary Operators
307 //===--------------------------------------------------------------------===//
309 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
310 if (Constant *LC = dyn_cast<Constant>(LHS))
311 if (Constant *RC = dyn_cast<Constant>(RHS))
312 return Folder.CreateAdd(LC, RC);
313 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
315 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
316 if (Constant *LC = dyn_cast<Constant>(LHS))
317 if (Constant *RC = dyn_cast<Constant>(RHS))
318 return Folder.CreateNSWAdd(LC, RC);
319 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
321 Value *CreateFAdd(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.CreateFAdd(LC, RC);
325 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
327 Value *CreateSub(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.CreateSub(LC, RC);
331 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
333 Value *CreateNSWSub(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.CreateNSWSub(LC, RC);
337 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
339 Value *CreateFSub(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.CreateFSub(LC, RC);
343 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
345 Value *CreateMul(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.CreateMul(LC, RC);
349 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
351 Value *CreateNSWMul(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.CreateNSWMul(LC, RC);
355 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
357 Value *CreateFMul(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.CreateFMul(LC, RC);
361 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
363 Value *CreateUDiv(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.CreateUDiv(LC, RC);
367 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
369 Value *CreateSDiv(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.CreateSDiv(LC, RC);
373 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
375 Value *CreateExactSDiv(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.CreateExactSDiv(LC, RC);
379 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
381 Value *CreateFDiv(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.CreateFDiv(LC, RC);
385 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
387 Value *CreateURem(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.CreateURem(LC, RC);
391 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
393 Value *CreateSRem(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.CreateSRem(LC, RC);
397 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
399 Value *CreateFRem(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.CreateFRem(LC, RC);
403 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
405 Value *CreateShl(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.CreateShl(LC, RC);
409 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
411 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
412 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
413 if (Constant *LC = dyn_cast<Constant>(LHS))
414 return Folder.CreateShl(LC, RHSC);
415 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
418 Value *CreateLShr(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.CreateLShr(LC, RC);
422 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
424 Value *CreateLShr(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.CreateLShr(LC, RHSC);
428 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
431 Value *CreateAShr(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.CreateAShr(LC, RC);
435 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
437 Value *CreateAShr(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.CreateSShr(LC, RHSC);
441 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
444 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
445 if (Constant *RC = dyn_cast<Constant>(RHS)) {
446 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
447 return LHS; // LHS & -1 -> LHS
448 if (Constant *LC = dyn_cast<Constant>(LHS))
449 return Folder.CreateAnd(LC, RC);
451 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
453 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
454 if (Constant *RC = dyn_cast<Constant>(RHS)) {
455 if (RC->isNullValue())
456 return LHS; // LHS | 0 -> LHS
457 if (Constant *LC = dyn_cast<Constant>(LHS))
458 return Folder.CreateOr(LC, RC);
460 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
462 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
463 if (Constant *LC = dyn_cast<Constant>(LHS))
464 if (Constant *RC = dyn_cast<Constant>(RHS))
465 return Folder.CreateXor(LC, RC);
466 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
469 Value *CreateBinOp(Instruction::BinaryOps Opc,
470 Value *LHS, Value *RHS, const Twine &Name = "") {
471 if (Constant *LC = dyn_cast<Constant>(LHS))
472 if (Constant *RC = dyn_cast<Constant>(RHS))
473 return Folder.CreateBinOp(Opc, LC, RC);
474 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
477 Value *CreateNeg(Value *V, const Twine &Name = "") {
478 if (Constant *VC = dyn_cast<Constant>(V))
479 return Folder.CreateNeg(VC);
480 return Insert(BinaryOperator::CreateNeg(V), Name);
482 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
483 if (Constant *VC = dyn_cast<Constant>(V))
484 return Folder.CreateNSWNeg(VC);
485 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
487 Value *CreateFNeg(Value *V, const Twine &Name = "") {
488 if (Constant *VC = dyn_cast<Constant>(V))
489 return Folder.CreateFNeg(VC);
490 return Insert(BinaryOperator::CreateFNeg(V), Name);
492 Value *CreateNot(Value *V, const Twine &Name = "") {
493 if (Constant *VC = dyn_cast<Constant>(V))
494 return Folder.CreateNot(VC);
495 return Insert(BinaryOperator::CreateNot(V), Name);
498 //===--------------------------------------------------------------------===//
499 // Instruction creation methods: Memory Instructions
500 //===--------------------------------------------------------------------===//
502 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
503 const Twine &Name = "") {
504 return Insert(new AllocaInst(Ty, ArraySize), Name);
506 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
507 // converting the string to 'bool' for the isVolatile parameter.
508 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
509 return Insert(new LoadInst(Ptr), Name);
511 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
512 return Insert(new LoadInst(Ptr), Name);
514 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
515 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
517 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
518 return Insert(new StoreInst(Val, Ptr, isVolatile));
520 template<typename InputIterator>
521 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
522 const Twine &Name = "") {
523 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
524 // Every index must be constant.
526 for (i = IdxBegin; i < IdxEnd; ++i)
527 if (!isa<Constant>(*i))
530 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
532 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
534 template<typename InputIterator>
535 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
536 const Twine &Name = "") {
537 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
538 // Every index must be constant.
540 for (i = IdxBegin; i < IdxEnd; ++i)
541 if (!isa<Constant>(*i))
544 return Folder.CreateInBoundsGetElementPtr(PC,
548 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
551 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
552 if (Constant *PC = dyn_cast<Constant>(Ptr))
553 if (Constant *IC = dyn_cast<Constant>(Idx))
554 return Folder.CreateGetElementPtr(PC, &IC, 1);
555 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
557 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
558 if (Constant *PC = dyn_cast<Constant>(Ptr))
559 if (Constant *IC = dyn_cast<Constant>(Idx))
560 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
561 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
563 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
564 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
566 if (Constant *PC = dyn_cast<Constant>(Ptr))
567 return Folder.CreateGetElementPtr(PC, &Idx, 1);
569 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
571 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
572 const Twine &Name = "") {
573 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
575 if (Constant *PC = dyn_cast<Constant>(Ptr))
576 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
578 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
580 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
581 const Twine &Name = "") {
583 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
584 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
587 if (Constant *PC = dyn_cast<Constant>(Ptr))
588 return Folder.CreateGetElementPtr(PC, Idxs, 2);
590 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
592 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
593 const Twine &Name = "") {
595 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
596 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
599 if (Constant *PC = dyn_cast<Constant>(Ptr))
600 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
602 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
604 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
605 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
607 if (Constant *PC = dyn_cast<Constant>(Ptr))
608 return Folder.CreateGetElementPtr(PC, &Idx, 1);
610 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
612 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
613 const Twine &Name = "") {
614 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
616 if (Constant *PC = dyn_cast<Constant>(Ptr))
617 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
619 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
621 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
622 const Twine &Name = "") {
624 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
625 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
628 if (Constant *PC = dyn_cast<Constant>(Ptr))
629 return Folder.CreateGetElementPtr(PC, Idxs, 2);
631 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
633 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
634 const Twine &Name = "") {
636 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
637 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
640 if (Constant *PC = dyn_cast<Constant>(Ptr))
641 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
643 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
645 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
646 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
649 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
650 /// with "i8*" type instead of a pointer to array of i8.
651 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
652 Value *gv = CreateGlobalString(Str, Name);
653 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
654 Value *Args[] = { zero, zero };
655 return CreateInBoundsGEP(gv, Args, Args+2, Name);
658 //===--------------------------------------------------------------------===//
659 // Instruction creation methods: Cast/Conversion Operators
660 //===--------------------------------------------------------------------===//
662 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
663 return CreateCast(Instruction::Trunc, V, DestTy, Name);
665 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
666 return CreateCast(Instruction::ZExt, V, DestTy, Name);
668 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
669 return CreateCast(Instruction::SExt, V, DestTy, Name);
671 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
672 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
674 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
675 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
677 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
678 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
680 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
681 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
683 Value *CreateFPTrunc(Value *V, const Type *DestTy,
684 const Twine &Name = "") {
685 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
687 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
688 return CreateCast(Instruction::FPExt, V, DestTy, Name);
690 Value *CreatePtrToInt(Value *V, const Type *DestTy,
691 const Twine &Name = "") {
692 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
694 Value *CreateIntToPtr(Value *V, const Type *DestTy,
695 const Twine &Name = "") {
696 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
698 Value *CreateBitCast(Value *V, const Type *DestTy,
699 const Twine &Name = "") {
700 return CreateCast(Instruction::BitCast, V, DestTy, Name);
702 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
703 const Twine &Name = "") {
704 if (V->getType() == DestTy)
706 if (Constant *VC = dyn_cast<Constant>(V))
707 return Folder.CreateZExtOrBitCast(VC, DestTy);
708 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
710 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
711 const Twine &Name = "") {
712 if (V->getType() == DestTy)
714 if (Constant *VC = dyn_cast<Constant>(V))
715 return Folder.CreateSExtOrBitCast(VC, DestTy);
716 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
718 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
719 const Twine &Name = "") {
720 if (V->getType() == DestTy)
722 if (Constant *VC = dyn_cast<Constant>(V))
723 return Folder.CreateTruncOrBitCast(VC, DestTy);
724 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
726 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
727 const Twine &Name = "") {
728 if (V->getType() == DestTy)
730 if (Constant *VC = dyn_cast<Constant>(V))
731 return Folder.CreateCast(Op, VC, DestTy);
732 return Insert(CastInst::Create(Op, V, DestTy), Name);
734 Value *CreatePointerCast(Value *V, const Type *DestTy,
735 const Twine &Name = "") {
736 if (V->getType() == DestTy)
738 if (Constant *VC = dyn_cast<Constant>(V))
739 return Folder.CreatePointerCast(VC, DestTy);
740 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
742 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
743 const Twine &Name = "") {
744 if (V->getType() == DestTy)
746 if (Constant *VC = dyn_cast<Constant>(V))
747 return Folder.CreateIntCast(VC, DestTy, isSigned);
748 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
751 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
752 // error, instead of converting the string to bool for the isSigned parameter.
753 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
755 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
756 if (V->getType() == DestTy)
758 if (Constant *VC = dyn_cast<Constant>(V))
759 return Folder.CreateFPCast(VC, DestTy);
760 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
763 //===--------------------------------------------------------------------===//
764 // Instruction creation methods: Compare Instructions
765 //===--------------------------------------------------------------------===//
767 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
768 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
770 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
771 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
773 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
774 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
776 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
777 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
779 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
780 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
782 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
783 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
785 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
786 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
788 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
789 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
791 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
792 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
794 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
795 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
798 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
799 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
801 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
802 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
804 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
805 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
807 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
808 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
810 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
811 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
813 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
814 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
816 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
817 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
819 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
820 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
822 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
823 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
825 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
826 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
828 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
829 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
831 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
832 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
834 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
835 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
837 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
838 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
841 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
842 const Twine &Name = "") {
843 if (Constant *LC = dyn_cast<Constant>(LHS))
844 if (Constant *RC = dyn_cast<Constant>(RHS))
845 return Folder.CreateICmp(P, LC, RC);
846 return Insert(new ICmpInst(P, LHS, RHS), Name);
848 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
849 const Twine &Name = "") {
850 if (Constant *LC = dyn_cast<Constant>(LHS))
851 if (Constant *RC = dyn_cast<Constant>(RHS))
852 return Folder.CreateFCmp(P, LC, RC);
853 return Insert(new FCmpInst(P, LHS, RHS), Name);
856 //===--------------------------------------------------------------------===//
857 // Instruction creation methods: Other Instructions
858 //===--------------------------------------------------------------------===//
860 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
861 return Insert(PHINode::Create(Ty), Name);
864 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
865 return Insert(CallInst::Create(Callee), Name);
867 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
868 return Insert(CallInst::Create(Callee, Arg), Name);
870 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
871 const Twine &Name = "") {
872 Value *Args[] = { Arg1, Arg2 };
873 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
875 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
876 const Twine &Name = "") {
877 Value *Args[] = { Arg1, Arg2, Arg3 };
878 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
880 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
881 Value *Arg4, const Twine &Name = "") {
882 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
883 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
886 template<typename InputIterator>
887 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
888 InputIterator ArgEnd, const Twine &Name = "") {
889 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
892 Value *CreateSelect(Value *C, Value *True, Value *False,
893 const Twine &Name = "") {
894 if (Constant *CC = dyn_cast<Constant>(C))
895 if (Constant *TC = dyn_cast<Constant>(True))
896 if (Constant *FC = dyn_cast<Constant>(False))
897 return Folder.CreateSelect(CC, TC, FC);
898 return Insert(SelectInst::Create(C, True, False), Name);
901 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
902 return Insert(new VAArgInst(List, Ty), Name);
905 Value *CreateExtractElement(Value *Vec, Value *Idx,
906 const Twine &Name = "") {
907 if (Constant *VC = dyn_cast<Constant>(Vec))
908 if (Constant *IC = dyn_cast<Constant>(Idx))
909 return Folder.CreateExtractElement(VC, IC);
910 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
913 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
914 const Twine &Name = "") {
915 if (Constant *VC = dyn_cast<Constant>(Vec))
916 if (Constant *NC = dyn_cast<Constant>(NewElt))
917 if (Constant *IC = dyn_cast<Constant>(Idx))
918 return Folder.CreateInsertElement(VC, NC, IC);
919 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
922 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
923 const Twine &Name = "") {
924 if (Constant *V1C = dyn_cast<Constant>(V1))
925 if (Constant *V2C = dyn_cast<Constant>(V2))
926 if (Constant *MC = dyn_cast<Constant>(Mask))
927 return Folder.CreateShuffleVector(V1C, V2C, MC);
928 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
931 Value *CreateExtractValue(Value *Agg, unsigned Idx,
932 const Twine &Name = "") {
933 if (Constant *AggC = dyn_cast<Constant>(Agg))
934 return Folder.CreateExtractValue(AggC, &Idx, 1);
935 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
938 template<typename InputIterator>
939 Value *CreateExtractValue(Value *Agg,
940 InputIterator IdxBegin,
941 InputIterator IdxEnd,
942 const Twine &Name = "") {
943 if (Constant *AggC = dyn_cast<Constant>(Agg))
944 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
945 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
948 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
949 const Twine &Name = "") {
950 if (Constant *AggC = dyn_cast<Constant>(Agg))
951 if (Constant *ValC = dyn_cast<Constant>(Val))
952 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
953 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
956 template<typename InputIterator>
957 Value *CreateInsertValue(Value *Agg, Value *Val,
958 InputIterator IdxBegin,
959 InputIterator IdxEnd,
960 const Twine &Name = "") {
961 if (Constant *AggC = dyn_cast<Constant>(Agg))
962 if (Constant *ValC = dyn_cast<Constant>(Val))
963 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
964 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
967 //===--------------------------------------------------------------------===//
968 // Utility creation methods
969 //===--------------------------------------------------------------------===//
971 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
972 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
973 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
977 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
978 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
979 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
983 /// CreatePtrDiff - Return the i64 difference between two pointer values,
984 /// dividing out the size of the pointed-to objects. This is intended to
985 /// implement C-style pointer subtraction. As such, the pointers must be
986 /// appropriately aligned for their element types and pointing into the
988 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
989 assert(LHS->getType() == RHS->getType() &&
990 "Pointer subtraction operand types must match!");
991 const PointerType *ArgType = cast<PointerType>(LHS->getType());
992 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
993 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
994 Value *Difference = CreateSub(LHS_int, RHS_int);
995 return CreateExactSDiv(Difference,
996 ConstantExpr::getSizeOf(ArgType->getElementType()),