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 with 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 const Type *getInt8PtrTy() {
147 return Type::getInt8PtrTy(Context);
150 /// getCurrentFunctionReturnType - Get the return type of the current function
151 /// that we're emitting into.
152 const Type *getCurrentFunctionReturnType() const;
155 /// IRBuilder - This provides a uniform API for creating instructions and
156 /// inserting them into a basic block: either at the end of a BasicBlock, or
157 /// at a specific iterator location in a block.
159 /// Note that the builder does not expose the full generality of LLVM
160 /// instructions. For access to extra instruction properties, use the mutators
161 /// (e.g. setVolatile) on the instructions after they have been created.
162 /// The first template argument handles whether or not to preserve names in the
163 /// final instruction output. This defaults to on. The second template argument
164 /// specifies a class to use for creating constants. This defaults to creating
165 /// minimally folded constants. The fourth template argument allows clients to
166 /// specify custom insertion hooks that are called on every newly created
168 template<bool preserveNames = true, typename T = ConstantFolder,
169 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
170 class IRBuilder : public IRBuilderBase, public Inserter {
173 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
174 : IRBuilderBase(C), Inserter(I), Folder(F) {
177 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
180 explicit IRBuilder(BasicBlock *TheBB, const T &F)
181 : IRBuilderBase(TheBB->getContext()), Folder(F) {
182 SetInsertPoint(TheBB);
185 explicit IRBuilder(BasicBlock *TheBB)
186 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
187 SetInsertPoint(TheBB);
190 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
191 : IRBuilderBase(TheBB->getContext()), Folder(F) {
192 SetInsertPoint(TheBB, IP);
195 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
196 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
197 SetInsertPoint(TheBB, IP);
200 /// getFolder - Get the constant folder being used.
201 const T &getFolder() { return Folder; }
203 /// isNamePreserving - Return true if this builder is configured to actually
204 /// add the requested names to IR created through it.
205 bool isNamePreserving() const { return preserveNames; }
207 /// Insert - Insert and return the specified instruction.
208 template<typename InstTy>
209 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
210 this->InsertHelper(I, Name, BB, InsertPt);
211 if (getCurrentDebugLocation() != 0)
212 this->SetInstDebugLocation(I);
216 //===--------------------------------------------------------------------===//
217 // Instruction creation methods: Terminators
218 //===--------------------------------------------------------------------===//
220 /// CreateRetVoid - Create a 'ret void' instruction.
221 ReturnInst *CreateRetVoid() {
222 return Insert(ReturnInst::Create(Context));
226 /// CreateRet - Create a 'ret <val>' instruction.
228 ReturnInst *CreateRet(Value *V) {
229 return Insert(ReturnInst::Create(Context, V));
232 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
233 /// with one Value from the retVals array each, that build a aggregate
234 /// return value one value at a time, and a ret instruction to return
235 /// the resulting aggregate value. This is a convenience function for
236 /// code that uses aggregate return values as a vehicle for having
237 /// multiple return values.
239 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
240 Value *V = UndefValue::get(getCurrentFunctionReturnType());
241 for (unsigned i = 0; i != N; ++i)
242 V = CreateInsertValue(V, retVals[i], i, "mrv");
243 return Insert(ReturnInst::Create(Context, V));
246 /// CreateBr - Create an unconditional 'br label X' instruction.
247 BranchInst *CreateBr(BasicBlock *Dest) {
248 return Insert(BranchInst::Create(Dest));
251 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
253 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
254 return Insert(BranchInst::Create(True, False, Cond));
257 /// CreateSwitch - Create a switch instruction with the specified value,
258 /// default dest, and with a hint for the number of cases that will be added
259 /// (for efficient allocation).
260 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
261 return Insert(SwitchInst::Create(V, Dest, NumCases));
264 /// CreateIndirectBr - Create an indirect branch instruction with the
265 /// specified address operand, with an optional hint for the number of
266 /// destinations that will be added (for efficient allocation).
267 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
268 return Insert(IndirectBrInst::Create(Addr, NumDests));
271 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
272 BasicBlock *UnwindDest, const Twine &Name = "") {
273 Value *Args[] = { 0 };
274 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
277 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
278 BasicBlock *UnwindDest, Value *Arg1,
279 const Twine &Name = "") {
280 Value *Args[] = { Arg1 };
281 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
284 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
285 BasicBlock *UnwindDest, Value *Arg1,
286 Value *Arg2, Value *Arg3,
287 const Twine &Name = "") {
288 Value *Args[] = { Arg1, Arg2, Arg3 };
289 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
292 /// CreateInvoke - Create an invoke instruction.
293 template<typename InputIterator>
294 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
295 BasicBlock *UnwindDest, InputIterator ArgBegin,
296 InputIterator ArgEnd, const Twine &Name = "") {
297 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
298 ArgBegin, ArgEnd), Name);
301 UnwindInst *CreateUnwind() {
302 return Insert(new UnwindInst(Context));
305 UnreachableInst *CreateUnreachable() {
306 return Insert(new UnreachableInst(Context));
309 //===--------------------------------------------------------------------===//
310 // Instruction creation methods: Binary Operators
311 //===--------------------------------------------------------------------===//
313 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
314 if (Constant *LC = dyn_cast<Constant>(LHS))
315 if (Constant *RC = dyn_cast<Constant>(RHS))
316 return Folder.CreateAdd(LC, RC);
317 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
319 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
320 if (Constant *LC = dyn_cast<Constant>(LHS))
321 if (Constant *RC = dyn_cast<Constant>(RHS))
322 return Folder.CreateNSWAdd(LC, RC);
323 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
325 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
326 if (Constant *LC = dyn_cast<Constant>(LHS))
327 if (Constant *RC = dyn_cast<Constant>(RHS))
328 return Folder.CreateNUWAdd(LC, RC);
329 return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
331 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
332 if (Constant *LC = dyn_cast<Constant>(LHS))
333 if (Constant *RC = dyn_cast<Constant>(RHS))
334 return Folder.CreateFAdd(LC, RC);
335 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
337 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
338 if (Constant *LC = dyn_cast<Constant>(LHS))
339 if (Constant *RC = dyn_cast<Constant>(RHS))
340 return Folder.CreateSub(LC, RC);
341 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
343 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
344 if (Constant *LC = dyn_cast<Constant>(LHS))
345 if (Constant *RC = dyn_cast<Constant>(RHS))
346 return Folder.CreateNSWSub(LC, RC);
347 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
349 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
350 if (Constant *LC = dyn_cast<Constant>(LHS))
351 if (Constant *RC = dyn_cast<Constant>(RHS))
352 return Folder.CreateNUWSub(LC, RC);
353 return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
355 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
356 if (Constant *LC = dyn_cast<Constant>(LHS))
357 if (Constant *RC = dyn_cast<Constant>(RHS))
358 return Folder.CreateFSub(LC, RC);
359 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
361 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
362 if (Constant *LC = dyn_cast<Constant>(LHS))
363 if (Constant *RC = dyn_cast<Constant>(RHS))
364 return Folder.CreateMul(LC, RC);
365 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
367 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
368 if (Constant *LC = dyn_cast<Constant>(LHS))
369 if (Constant *RC = dyn_cast<Constant>(RHS))
370 return Folder.CreateNSWMul(LC, RC);
371 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
373 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
374 if (Constant *LC = dyn_cast<Constant>(LHS))
375 if (Constant *RC = dyn_cast<Constant>(RHS))
376 return Folder.CreateNUWMul(LC, RC);
377 return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
379 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
380 if (Constant *LC = dyn_cast<Constant>(LHS))
381 if (Constant *RC = dyn_cast<Constant>(RHS))
382 return Folder.CreateFMul(LC, RC);
383 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
385 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
386 if (Constant *LC = dyn_cast<Constant>(LHS))
387 if (Constant *RC = dyn_cast<Constant>(RHS))
388 return Folder.CreateUDiv(LC, RC);
389 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
391 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
392 if (Constant *LC = dyn_cast<Constant>(LHS))
393 if (Constant *RC = dyn_cast<Constant>(RHS))
394 return Folder.CreateSDiv(LC, RC);
395 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
397 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
398 if (Constant *LC = dyn_cast<Constant>(LHS))
399 if (Constant *RC = dyn_cast<Constant>(RHS))
400 return Folder.CreateExactSDiv(LC, RC);
401 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
403 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
404 if (Constant *LC = dyn_cast<Constant>(LHS))
405 if (Constant *RC = dyn_cast<Constant>(RHS))
406 return Folder.CreateFDiv(LC, RC);
407 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
409 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
410 if (Constant *LC = dyn_cast<Constant>(LHS))
411 if (Constant *RC = dyn_cast<Constant>(RHS))
412 return Folder.CreateURem(LC, RC);
413 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
415 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
416 if (Constant *LC = dyn_cast<Constant>(LHS))
417 if (Constant *RC = dyn_cast<Constant>(RHS))
418 return Folder.CreateSRem(LC, RC);
419 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
421 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
422 if (Constant *LC = dyn_cast<Constant>(LHS))
423 if (Constant *RC = dyn_cast<Constant>(RHS))
424 return Folder.CreateFRem(LC, RC);
425 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
427 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
428 if (Constant *LC = dyn_cast<Constant>(LHS))
429 if (Constant *RC = dyn_cast<Constant>(RHS))
430 return Folder.CreateShl(LC, RC);
431 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
433 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
434 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
435 if (Constant *LC = dyn_cast<Constant>(LHS))
436 return Folder.CreateShl(LC, RHSC);
437 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
440 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
441 if (Constant *LC = dyn_cast<Constant>(LHS))
442 if (Constant *RC = dyn_cast<Constant>(RHS))
443 return Folder.CreateLShr(LC, RC);
444 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
446 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
447 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
448 if (Constant *LC = dyn_cast<Constant>(LHS))
449 return Folder.CreateLShr(LC, RHSC);
450 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
453 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
454 if (Constant *LC = dyn_cast<Constant>(LHS))
455 if (Constant *RC = dyn_cast<Constant>(RHS))
456 return Folder.CreateAShr(LC, RC);
457 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
459 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
460 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
461 if (Constant *LC = dyn_cast<Constant>(LHS))
462 return Folder.CreateSShr(LC, RHSC);
463 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
466 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
467 if (Constant *RC = dyn_cast<Constant>(RHS)) {
468 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
469 return LHS; // LHS & -1 -> LHS
470 if (Constant *LC = dyn_cast<Constant>(LHS))
471 return Folder.CreateAnd(LC, RC);
473 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
475 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
476 if (Constant *RC = dyn_cast<Constant>(RHS)) {
477 if (RC->isNullValue())
478 return LHS; // LHS | 0 -> LHS
479 if (Constant *LC = dyn_cast<Constant>(LHS))
480 return Folder.CreateOr(LC, RC);
482 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
484 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
485 if (Constant *LC = dyn_cast<Constant>(LHS))
486 if (Constant *RC = dyn_cast<Constant>(RHS))
487 return Folder.CreateXor(LC, RC);
488 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
491 Value *CreateBinOp(Instruction::BinaryOps Opc,
492 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.CreateBinOp(Opc, LC, RC);
496 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
499 Value *CreateNeg(Value *V, const Twine &Name = "") {
500 if (Constant *VC = dyn_cast<Constant>(V))
501 return Folder.CreateNeg(VC);
502 return Insert(BinaryOperator::CreateNeg(V), Name);
504 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
505 if (Constant *VC = dyn_cast<Constant>(V))
506 return Folder.CreateNSWNeg(VC);
507 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
509 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
510 if (Constant *VC = dyn_cast<Constant>(V))
511 return Folder.CreateNUWNeg(VC);
512 return Insert(BinaryOperator::CreateNUWNeg(V), Name);
514 Value *CreateFNeg(Value *V, const Twine &Name = "") {
515 if (Constant *VC = dyn_cast<Constant>(V))
516 return Folder.CreateFNeg(VC);
517 return Insert(BinaryOperator::CreateFNeg(V), Name);
519 Value *CreateNot(Value *V, const Twine &Name = "") {
520 if (Constant *VC = dyn_cast<Constant>(V))
521 return Folder.CreateNot(VC);
522 return Insert(BinaryOperator::CreateNot(V), Name);
525 //===--------------------------------------------------------------------===//
526 // Instruction creation methods: Memory Instructions
527 //===--------------------------------------------------------------------===//
529 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
530 const Twine &Name = "") {
531 return Insert(new AllocaInst(Ty, ArraySize), Name);
533 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
534 // converting the string to 'bool' for the isVolatile parameter.
535 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
536 return Insert(new LoadInst(Ptr), Name);
538 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
539 return Insert(new LoadInst(Ptr), Name);
541 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
542 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
544 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
545 return Insert(new StoreInst(Val, Ptr, isVolatile));
547 template<typename InputIterator>
548 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
549 const Twine &Name = "") {
550 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
551 // Every index must be constant.
553 for (i = IdxBegin; i < IdxEnd; ++i)
554 if (!isa<Constant>(*i))
557 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
559 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
561 template<typename InputIterator>
562 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
563 const Twine &Name = "") {
564 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
565 // Every index must be constant.
567 for (i = IdxBegin; i < IdxEnd; ++i)
568 if (!isa<Constant>(*i))
571 return Folder.CreateInBoundsGetElementPtr(PC,
575 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
578 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
579 if (Constant *PC = dyn_cast<Constant>(Ptr))
580 if (Constant *IC = dyn_cast<Constant>(Idx))
581 return Folder.CreateGetElementPtr(PC, &IC, 1);
582 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
584 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
585 if (Constant *PC = dyn_cast<Constant>(Ptr))
586 if (Constant *IC = dyn_cast<Constant>(Idx))
587 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
588 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
590 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
591 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
593 if (Constant *PC = dyn_cast<Constant>(Ptr))
594 return Folder.CreateGetElementPtr(PC, &Idx, 1);
596 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
598 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
599 const Twine &Name = "") {
600 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
602 if (Constant *PC = dyn_cast<Constant>(Ptr))
603 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
605 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
607 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
608 const Twine &Name = "") {
610 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
611 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
614 if (Constant *PC = dyn_cast<Constant>(Ptr))
615 return Folder.CreateGetElementPtr(PC, Idxs, 2);
617 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
619 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
620 const Twine &Name = "") {
622 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
623 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
626 if (Constant *PC = dyn_cast<Constant>(Ptr))
627 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
629 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
631 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
632 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
634 if (Constant *PC = dyn_cast<Constant>(Ptr))
635 return Folder.CreateGetElementPtr(PC, &Idx, 1);
637 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
639 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
640 const Twine &Name = "") {
641 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
643 if (Constant *PC = dyn_cast<Constant>(Ptr))
644 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
646 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
648 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
649 const Twine &Name = "") {
651 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
652 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
655 if (Constant *PC = dyn_cast<Constant>(Ptr))
656 return Folder.CreateGetElementPtr(PC, Idxs, 2);
658 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
660 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
661 const Twine &Name = "") {
663 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
664 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
667 if (Constant *PC = dyn_cast<Constant>(Ptr))
668 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
670 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
672 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
673 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
676 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
677 /// with "i8*" type instead of a pointer to array of i8.
678 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
679 Value *gv = CreateGlobalString(Str, Name);
680 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
681 Value *Args[] = { zero, zero };
682 return CreateInBoundsGEP(gv, Args, Args+2, Name);
685 //===--------------------------------------------------------------------===//
686 // Instruction creation methods: Cast/Conversion Operators
687 //===--------------------------------------------------------------------===//
689 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
690 return CreateCast(Instruction::Trunc, V, DestTy, Name);
692 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
693 return CreateCast(Instruction::ZExt, V, DestTy, Name);
695 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
696 return CreateCast(Instruction::SExt, V, DestTy, Name);
698 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
699 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
701 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
702 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
704 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
705 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
707 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
708 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
710 Value *CreateFPTrunc(Value *V, const Type *DestTy,
711 const Twine &Name = "") {
712 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
714 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
715 return CreateCast(Instruction::FPExt, V, DestTy, Name);
717 Value *CreatePtrToInt(Value *V, const Type *DestTy,
718 const Twine &Name = "") {
719 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
721 Value *CreateIntToPtr(Value *V, const Type *DestTy,
722 const Twine &Name = "") {
723 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
725 Value *CreateBitCast(Value *V, const Type *DestTy,
726 const Twine &Name = "") {
727 return CreateCast(Instruction::BitCast, V, DestTy, Name);
729 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
730 const Twine &Name = "") {
731 if (V->getType() == DestTy)
733 if (Constant *VC = dyn_cast<Constant>(V))
734 return Folder.CreateZExtOrBitCast(VC, DestTy);
735 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
737 Value *CreateSExtOrBitCast(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.CreateSExtOrBitCast(VC, DestTy);
743 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
745 Value *CreateTruncOrBitCast(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.CreateTruncOrBitCast(VC, DestTy);
751 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
753 Value *CreateCast(Instruction::CastOps Op, 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.CreateCast(Op, VC, DestTy);
759 return Insert(CastInst::Create(Op, V, DestTy), Name);
761 Value *CreatePointerCast(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.CreatePointerCast(VC, DestTy);
767 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
769 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
770 const Twine &Name = "") {
771 if (V->getType() == DestTy)
773 if (Constant *VC = dyn_cast<Constant>(V))
774 return Folder.CreateIntCast(VC, DestTy, isSigned);
775 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
778 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
779 // error, instead of converting the string to bool for the isSigned parameter.
780 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
782 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
783 if (V->getType() == DestTy)
785 if (Constant *VC = dyn_cast<Constant>(V))
786 return Folder.CreateFPCast(VC, DestTy);
787 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
790 //===--------------------------------------------------------------------===//
791 // Instruction creation methods: Compare Instructions
792 //===--------------------------------------------------------------------===//
794 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
795 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
797 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
798 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
800 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
801 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
803 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
804 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
806 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
807 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
809 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
810 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
812 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
813 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
815 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
816 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
818 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
819 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
821 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
822 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
825 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
826 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
828 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
829 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
831 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
832 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
834 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
835 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
837 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
838 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
840 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
841 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
843 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
844 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
846 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
847 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
849 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
850 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
852 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
853 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
855 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
856 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
858 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
859 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
861 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
862 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
864 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
865 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
868 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
869 const Twine &Name = "") {
870 if (Constant *LC = dyn_cast<Constant>(LHS))
871 if (Constant *RC = dyn_cast<Constant>(RHS))
872 return Folder.CreateICmp(P, LC, RC);
873 return Insert(new ICmpInst(P, LHS, RHS), Name);
875 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
876 const Twine &Name = "") {
877 if (Constant *LC = dyn_cast<Constant>(LHS))
878 if (Constant *RC = dyn_cast<Constant>(RHS))
879 return Folder.CreateFCmp(P, LC, RC);
880 return Insert(new FCmpInst(P, LHS, RHS), Name);
883 //===--------------------------------------------------------------------===//
884 // Instruction creation methods: Other Instructions
885 //===--------------------------------------------------------------------===//
887 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
888 return Insert(PHINode::Create(Ty), Name);
891 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
892 return Insert(CallInst::Create(Callee), Name);
894 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
895 return Insert(CallInst::Create(Callee, Arg), Name);
897 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
898 const Twine &Name = "") {
899 Value *Args[] = { Arg1, Arg2 };
900 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
902 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
903 const Twine &Name = "") {
904 Value *Args[] = { Arg1, Arg2, Arg3 };
905 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
907 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
908 Value *Arg4, const Twine &Name = "") {
909 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
910 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
913 template<typename InputIterator>
914 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
915 InputIterator ArgEnd, const Twine &Name = "") {
916 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
919 Value *CreateSelect(Value *C, Value *True, Value *False,
920 const Twine &Name = "") {
921 if (Constant *CC = dyn_cast<Constant>(C))
922 if (Constant *TC = dyn_cast<Constant>(True))
923 if (Constant *FC = dyn_cast<Constant>(False))
924 return Folder.CreateSelect(CC, TC, FC);
925 return Insert(SelectInst::Create(C, True, False), Name);
928 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
929 return Insert(new VAArgInst(List, Ty), Name);
932 Value *CreateExtractElement(Value *Vec, Value *Idx,
933 const Twine &Name = "") {
934 if (Constant *VC = dyn_cast<Constant>(Vec))
935 if (Constant *IC = dyn_cast<Constant>(Idx))
936 return Folder.CreateExtractElement(VC, IC);
937 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
940 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
941 const Twine &Name = "") {
942 if (Constant *VC = dyn_cast<Constant>(Vec))
943 if (Constant *NC = dyn_cast<Constant>(NewElt))
944 if (Constant *IC = dyn_cast<Constant>(Idx))
945 return Folder.CreateInsertElement(VC, NC, IC);
946 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
949 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
950 const Twine &Name = "") {
951 if (Constant *V1C = dyn_cast<Constant>(V1))
952 if (Constant *V2C = dyn_cast<Constant>(V2))
953 if (Constant *MC = dyn_cast<Constant>(Mask))
954 return Folder.CreateShuffleVector(V1C, V2C, MC);
955 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
958 Value *CreateExtractValue(Value *Agg, unsigned Idx,
959 const Twine &Name = "") {
960 if (Constant *AggC = dyn_cast<Constant>(Agg))
961 return Folder.CreateExtractValue(AggC, &Idx, 1);
962 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
965 template<typename InputIterator>
966 Value *CreateExtractValue(Value *Agg,
967 InputIterator IdxBegin,
968 InputIterator IdxEnd,
969 const Twine &Name = "") {
970 if (Constant *AggC = dyn_cast<Constant>(Agg))
971 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
972 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
975 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
976 const Twine &Name = "") {
977 if (Constant *AggC = dyn_cast<Constant>(Agg))
978 if (Constant *ValC = dyn_cast<Constant>(Val))
979 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
980 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
983 template<typename InputIterator>
984 Value *CreateInsertValue(Value *Agg, Value *Val,
985 InputIterator IdxBegin,
986 InputIterator IdxEnd,
987 const Twine &Name = "") {
988 if (Constant *AggC = dyn_cast<Constant>(Agg))
989 if (Constant *ValC = dyn_cast<Constant>(Val))
990 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
991 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
994 //===--------------------------------------------------------------------===//
995 // Utility creation methods
996 //===--------------------------------------------------------------------===//
998 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
999 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1000 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1004 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1005 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1006 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1010 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1011 /// dividing out the size of the pointed-to objects. This is intended to
1012 /// implement C-style pointer subtraction. As such, the pointers must be
1013 /// appropriately aligned for their element types and pointing into the
1015 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1016 assert(LHS->getType() == RHS->getType() &&
1017 "Pointer subtraction operand types must match!");
1018 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1019 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1020 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1021 Value *Difference = CreateSub(LHS_int, RHS_int);
1022 return CreateExactSDiv(Difference,
1023 ConstantExpr::getSizeOf(ArgType->getElementType()),