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 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 *CreateNUWAdd(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.CreateNUWAdd(LC, RC);
325 return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
327 Value *CreateFAdd(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.CreateFAdd(LC, RC);
331 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
333 Value *CreateSub(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.CreateSub(LC, RC);
337 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
339 Value *CreateNSWSub(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.CreateNSWSub(LC, RC);
343 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
345 Value *CreateNUWSub(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.CreateNUWSub(LC, RC);
349 return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
351 Value *CreateFSub(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.CreateFSub(LC, RC);
355 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
357 Value *CreateMul(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.CreateMul(LC, RC);
361 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
363 Value *CreateNSWMul(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.CreateNSWMul(LC, RC);
367 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
369 Value *CreateNUWMul(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.CreateNUWMul(LC, RC);
373 return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
375 Value *CreateFMul(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.CreateFMul(LC, RC);
379 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
381 Value *CreateUDiv(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.CreateUDiv(LC, RC);
385 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
387 Value *CreateSDiv(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.CreateSDiv(LC, RC);
391 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
393 Value *CreateExactSDiv(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.CreateExactSDiv(LC, RC);
397 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
399 Value *CreateFDiv(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.CreateFDiv(LC, RC);
403 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
405 Value *CreateURem(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.CreateURem(LC, RC);
409 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
411 Value *CreateSRem(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.CreateSRem(LC, RC);
415 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
417 Value *CreateFRem(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.CreateFRem(LC, RC);
421 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
423 Value *CreateShl(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.CreateShl(LC, RC);
427 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
429 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
430 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
431 if (Constant *LC = dyn_cast<Constant>(LHS))
432 return Folder.CreateShl(LC, RHSC);
433 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
436 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
437 if (Constant *LC = dyn_cast<Constant>(LHS))
438 if (Constant *RC = dyn_cast<Constant>(RHS))
439 return Folder.CreateLShr(LC, RC);
440 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
442 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
443 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
444 if (Constant *LC = dyn_cast<Constant>(LHS))
445 return Folder.CreateLShr(LC, RHSC);
446 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
449 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
450 if (Constant *LC = dyn_cast<Constant>(LHS))
451 if (Constant *RC = dyn_cast<Constant>(RHS))
452 return Folder.CreateAShr(LC, RC);
453 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
455 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
456 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
457 if (Constant *LC = dyn_cast<Constant>(LHS))
458 return Folder.CreateSShr(LC, RHSC);
459 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
462 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
463 if (Constant *RC = dyn_cast<Constant>(RHS)) {
464 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
465 return LHS; // LHS & -1 -> LHS
466 if (Constant *LC = dyn_cast<Constant>(LHS))
467 return Folder.CreateAnd(LC, RC);
469 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
471 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
472 if (Constant *RC = dyn_cast<Constant>(RHS)) {
473 if (RC->isNullValue())
474 return LHS; // LHS | 0 -> LHS
475 if (Constant *LC = dyn_cast<Constant>(LHS))
476 return Folder.CreateOr(LC, RC);
478 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
480 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
481 if (Constant *LC = dyn_cast<Constant>(LHS))
482 if (Constant *RC = dyn_cast<Constant>(RHS))
483 return Folder.CreateXor(LC, RC);
484 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
487 Value *CreateBinOp(Instruction::BinaryOps Opc,
488 Value *LHS, Value *RHS, const Twine &Name = "") {
489 if (Constant *LC = dyn_cast<Constant>(LHS))
490 if (Constant *RC = dyn_cast<Constant>(RHS))
491 return Folder.CreateBinOp(Opc, LC, RC);
492 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
495 Value *CreateNeg(Value *V, const Twine &Name = "") {
496 if (Constant *VC = dyn_cast<Constant>(V))
497 return Folder.CreateNeg(VC);
498 return Insert(BinaryOperator::CreateNeg(V), Name);
500 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
501 if (Constant *VC = dyn_cast<Constant>(V))
502 return Folder.CreateNSWNeg(VC);
503 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
505 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
506 if (Constant *VC = dyn_cast<Constant>(V))
507 return Folder.CreateNUWNeg(VC);
508 return Insert(BinaryOperator::CreateNUWNeg(V), Name);
510 Value *CreateFNeg(Value *V, const Twine &Name = "") {
511 if (Constant *VC = dyn_cast<Constant>(V))
512 return Folder.CreateFNeg(VC);
513 return Insert(BinaryOperator::CreateFNeg(V), Name);
515 Value *CreateNot(Value *V, const Twine &Name = "") {
516 if (Constant *VC = dyn_cast<Constant>(V))
517 return Folder.CreateNot(VC);
518 return Insert(BinaryOperator::CreateNot(V), Name);
521 //===--------------------------------------------------------------------===//
522 // Instruction creation methods: Memory Instructions
523 //===--------------------------------------------------------------------===//
525 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
526 const Twine &Name = "") {
527 return Insert(new AllocaInst(Ty, ArraySize), Name);
529 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
530 // converting the string to 'bool' for the isVolatile parameter.
531 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
532 return Insert(new LoadInst(Ptr), Name);
534 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
535 return Insert(new LoadInst(Ptr), Name);
537 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
538 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
540 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
541 return Insert(new StoreInst(Val, Ptr, isVolatile));
543 template<typename InputIterator>
544 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
545 const Twine &Name = "") {
546 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
547 // Every index must be constant.
549 for (i = IdxBegin; i < IdxEnd; ++i)
550 if (!isa<Constant>(*i))
553 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
555 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
557 template<typename InputIterator>
558 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
559 const Twine &Name = "") {
560 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
561 // Every index must be constant.
563 for (i = IdxBegin; i < IdxEnd; ++i)
564 if (!isa<Constant>(*i))
567 return Folder.CreateInBoundsGetElementPtr(PC,
571 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
574 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
575 if (Constant *PC = dyn_cast<Constant>(Ptr))
576 if (Constant *IC = dyn_cast<Constant>(Idx))
577 return Folder.CreateGetElementPtr(PC, &IC, 1);
578 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
580 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
581 if (Constant *PC = dyn_cast<Constant>(Ptr))
582 if (Constant *IC = dyn_cast<Constant>(Idx))
583 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
584 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
586 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
587 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
589 if (Constant *PC = dyn_cast<Constant>(Ptr))
590 return Folder.CreateGetElementPtr(PC, &Idx, 1);
592 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
594 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
595 const Twine &Name = "") {
596 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
598 if (Constant *PC = dyn_cast<Constant>(Ptr))
599 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
601 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
603 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
604 const Twine &Name = "") {
606 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
607 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
610 if (Constant *PC = dyn_cast<Constant>(Ptr))
611 return Folder.CreateGetElementPtr(PC, Idxs, 2);
613 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
615 Value *CreateConstInBoundsGEP2_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.CreateInBoundsGetElementPtr(PC, Idxs, 2);
625 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
627 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
628 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
630 if (Constant *PC = dyn_cast<Constant>(Ptr))
631 return Folder.CreateGetElementPtr(PC, &Idx, 1);
633 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
635 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
636 const Twine &Name = "") {
637 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
639 if (Constant *PC = dyn_cast<Constant>(Ptr))
640 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
642 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
644 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
645 const Twine &Name = "") {
647 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
648 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
651 if (Constant *PC = dyn_cast<Constant>(Ptr))
652 return Folder.CreateGetElementPtr(PC, Idxs, 2);
654 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
656 Value *CreateConstInBoundsGEP2_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.CreateInBoundsGetElementPtr(PC, Idxs, 2);
666 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
668 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
669 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
672 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
673 /// with "i8*" type instead of a pointer to array of i8.
674 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
675 Value *gv = CreateGlobalString(Str, Name);
676 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
677 Value *Args[] = { zero, zero };
678 return CreateInBoundsGEP(gv, Args, Args+2, Name);
681 //===--------------------------------------------------------------------===//
682 // Instruction creation methods: Cast/Conversion Operators
683 //===--------------------------------------------------------------------===//
685 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
686 return CreateCast(Instruction::Trunc, V, DestTy, Name);
688 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
689 return CreateCast(Instruction::ZExt, V, DestTy, Name);
691 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
692 return CreateCast(Instruction::SExt, V, DestTy, Name);
694 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
695 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
697 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
698 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
700 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
701 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
703 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
704 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
706 Value *CreateFPTrunc(Value *V, const Type *DestTy,
707 const Twine &Name = "") {
708 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
710 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
711 return CreateCast(Instruction::FPExt, V, DestTy, Name);
713 Value *CreatePtrToInt(Value *V, const Type *DestTy,
714 const Twine &Name = "") {
715 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
717 Value *CreateIntToPtr(Value *V, const Type *DestTy,
718 const Twine &Name = "") {
719 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
721 Value *CreateBitCast(Value *V, const Type *DestTy,
722 const Twine &Name = "") {
723 return CreateCast(Instruction::BitCast, V, DestTy, Name);
725 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
726 const Twine &Name = "") {
727 if (V->getType() == DestTy)
729 if (Constant *VC = dyn_cast<Constant>(V))
730 return Folder.CreateZExtOrBitCast(VC, DestTy);
731 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
733 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
734 const Twine &Name = "") {
735 if (V->getType() == DestTy)
737 if (Constant *VC = dyn_cast<Constant>(V))
738 return Folder.CreateSExtOrBitCast(VC, DestTy);
739 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
741 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
742 const Twine &Name = "") {
743 if (V->getType() == DestTy)
745 if (Constant *VC = dyn_cast<Constant>(V))
746 return Folder.CreateTruncOrBitCast(VC, DestTy);
747 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
749 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
750 const Twine &Name = "") {
751 if (V->getType() == DestTy)
753 if (Constant *VC = dyn_cast<Constant>(V))
754 return Folder.CreateCast(Op, VC, DestTy);
755 return Insert(CastInst::Create(Op, V, DestTy), Name);
757 Value *CreatePointerCast(Value *V, const Type *DestTy,
758 const Twine &Name = "") {
759 if (V->getType() == DestTy)
761 if (Constant *VC = dyn_cast<Constant>(V))
762 return Folder.CreatePointerCast(VC, DestTy);
763 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
765 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
766 const Twine &Name = "") {
767 if (V->getType() == DestTy)
769 if (Constant *VC = dyn_cast<Constant>(V))
770 return Folder.CreateIntCast(VC, DestTy, isSigned);
771 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
774 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
775 // error, instead of converting the string to bool for the isSigned parameter.
776 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
778 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
779 if (V->getType() == DestTy)
781 if (Constant *VC = dyn_cast<Constant>(V))
782 return Folder.CreateFPCast(VC, DestTy);
783 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
786 //===--------------------------------------------------------------------===//
787 // Instruction creation methods: Compare Instructions
788 //===--------------------------------------------------------------------===//
790 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
791 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
793 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
794 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
796 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
797 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
799 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
800 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
802 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
803 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
805 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
806 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
808 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
809 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
811 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
812 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
814 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
815 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
817 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
818 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
821 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
822 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
824 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
825 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
827 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
828 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
830 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
831 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
833 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
834 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
836 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
837 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
839 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
840 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
842 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
843 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
845 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
846 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
848 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
849 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
851 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
852 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
854 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
855 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
857 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
858 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
860 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
861 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
864 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
865 const Twine &Name = "") {
866 if (Constant *LC = dyn_cast<Constant>(LHS))
867 if (Constant *RC = dyn_cast<Constant>(RHS))
868 return Folder.CreateICmp(P, LC, RC);
869 return Insert(new ICmpInst(P, LHS, RHS), Name);
871 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
872 const Twine &Name = "") {
873 if (Constant *LC = dyn_cast<Constant>(LHS))
874 if (Constant *RC = dyn_cast<Constant>(RHS))
875 return Folder.CreateFCmp(P, LC, RC);
876 return Insert(new FCmpInst(P, LHS, RHS), Name);
879 //===--------------------------------------------------------------------===//
880 // Instruction creation methods: Other Instructions
881 //===--------------------------------------------------------------------===//
883 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
884 return Insert(PHINode::Create(Ty), Name);
887 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
888 return Insert(CallInst::Create(Callee), Name);
890 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
891 return Insert(CallInst::Create(Callee, Arg), Name);
893 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
894 const Twine &Name = "") {
895 Value *Args[] = { Arg1, Arg2 };
896 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
898 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
899 const Twine &Name = "") {
900 Value *Args[] = { Arg1, Arg2, Arg3 };
901 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
903 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
904 Value *Arg4, const Twine &Name = "") {
905 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
906 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
909 template<typename InputIterator>
910 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
911 InputIterator ArgEnd, const Twine &Name = "") {
912 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
915 Value *CreateSelect(Value *C, Value *True, Value *False,
916 const Twine &Name = "") {
917 if (Constant *CC = dyn_cast<Constant>(C))
918 if (Constant *TC = dyn_cast<Constant>(True))
919 if (Constant *FC = dyn_cast<Constant>(False))
920 return Folder.CreateSelect(CC, TC, FC);
921 return Insert(SelectInst::Create(C, True, False), Name);
924 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
925 return Insert(new VAArgInst(List, Ty), Name);
928 Value *CreateExtractElement(Value *Vec, Value *Idx,
929 const Twine &Name = "") {
930 if (Constant *VC = dyn_cast<Constant>(Vec))
931 if (Constant *IC = dyn_cast<Constant>(Idx))
932 return Folder.CreateExtractElement(VC, IC);
933 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
936 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
937 const Twine &Name = "") {
938 if (Constant *VC = dyn_cast<Constant>(Vec))
939 if (Constant *NC = dyn_cast<Constant>(NewElt))
940 if (Constant *IC = dyn_cast<Constant>(Idx))
941 return Folder.CreateInsertElement(VC, NC, IC);
942 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
945 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
946 const Twine &Name = "") {
947 if (Constant *V1C = dyn_cast<Constant>(V1))
948 if (Constant *V2C = dyn_cast<Constant>(V2))
949 if (Constant *MC = dyn_cast<Constant>(Mask))
950 return Folder.CreateShuffleVector(V1C, V2C, MC);
951 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
954 Value *CreateExtractValue(Value *Agg, unsigned Idx,
955 const Twine &Name = "") {
956 if (Constant *AggC = dyn_cast<Constant>(Agg))
957 return Folder.CreateExtractValue(AggC, &Idx, 1);
958 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
961 template<typename InputIterator>
962 Value *CreateExtractValue(Value *Agg,
963 InputIterator IdxBegin,
964 InputIterator IdxEnd,
965 const Twine &Name = "") {
966 if (Constant *AggC = dyn_cast<Constant>(Agg))
967 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
968 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
971 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
972 const Twine &Name = "") {
973 if (Constant *AggC = dyn_cast<Constant>(Agg))
974 if (Constant *ValC = dyn_cast<Constant>(Val))
975 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
976 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
979 template<typename InputIterator>
980 Value *CreateInsertValue(Value *Agg, Value *Val,
981 InputIterator IdxBegin,
982 InputIterator IdxEnd,
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, IdxBegin, IdxEnd-IdxBegin);
987 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
990 //===--------------------------------------------------------------------===//
991 // Utility creation methods
992 //===--------------------------------------------------------------------===//
994 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
995 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
996 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1000 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1001 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1002 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1006 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1007 /// dividing out the size of the pointed-to objects. This is intended to
1008 /// implement C-style pointer subtraction. As such, the pointers must be
1009 /// appropriately aligned for their element types and pointing into the
1011 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1012 assert(LHS->getType() == RHS->getType() &&
1013 "Pointer subtraction operand types must match!");
1014 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1015 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1016 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1017 Value *Difference = CreateSub(LHS_int, RHS_int);
1018 return CreateExactSDiv(Difference,
1019 ConstantExpr::getSizeOf(ArgType->getElementType()),