1 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the IRBuilder class, which is used as a convenient way
11 // to create LLVM instructions with a consistent and simplified interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_IRBUILDER_H
16 #define LLVM_SUPPORT_IRBUILDER_H
18 #include "llvm/Instructions.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Support/ConstantFolder.h"
26 /// IRBuilderDefaultInserter - This provides the default implementation of the
27 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
28 /// created by IRBuilder and needs to be inserted. By default, this inserts the
29 /// instruction at the insertion point.
30 template <bool preserveNames = true>
31 class IRBuilderDefaultInserter {
33 void InsertHelper(Instruction *I, const Twine &Name,
34 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
35 if (BB) BB->getInstList().insert(InsertPt, I);
41 /// IRBuilderBase - Common base class shared among various IRBuilders.
43 DebugLoc CurDbgLocation;
46 BasicBlock::iterator InsertPt;
50 IRBuilderBase(LLVMContext &context)
52 ClearInsertionPoint();
55 //===--------------------------------------------------------------------===//
56 // Builder configuration methods
57 //===--------------------------------------------------------------------===//
59 /// ClearInsertionPoint - Clear the insertion point: created instructions will
60 /// not be inserted into a block.
61 void ClearInsertionPoint() {
65 BasicBlock *GetInsertBlock() const { return BB; }
66 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
67 LLVMContext &getContext() const { return Context; }
69 /// SetInsertPoint - This specifies that created instructions should be
70 /// appended to the end of the specified block.
71 void SetInsertPoint(BasicBlock *TheBB) {
76 /// SetInsertPoint - This specifies that created instructions should be
77 /// inserted at the specified point.
78 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
83 /// SetCurrentDebugLocation - Set location information used by debugging
85 void SetCurrentDebugLocation(const DebugLoc &L) {
89 /// getCurrentDebugLocation - Get location information used by debugging
91 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
93 /// SetInstDebugLocation - If this builder has a current debug location, set
94 /// it on the specified instruction.
95 void SetInstDebugLocation(Instruction *I) const {
96 if (!CurDbgLocation.isUnknown())
97 I->setDebugLoc(CurDbgLocation);
100 /// InsertPoint - A saved insertion point.
103 BasicBlock::iterator Point;
106 /// Creates a new insertion point which doesn't point to anything.
107 InsertPoint() : Block(0) {}
109 /// Creates a new insertion point at the given location.
110 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
111 : Block(InsertBlock), Point(InsertPoint) {}
113 /// isSet - Returns true if this insert point is set.
114 bool isSet() const { return (Block != 0); }
116 llvm::BasicBlock *getBlock() const { return Block; }
117 llvm::BasicBlock::iterator getPoint() const { return Point; }
120 /// saveIP - Returns the current insert point.
121 InsertPoint saveIP() const {
122 return InsertPoint(GetInsertBlock(), GetInsertPoint());
125 /// saveAndClearIP - Returns the current insert point, clearing it
127 InsertPoint saveAndClearIP() {
128 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
129 ClearInsertionPoint();
133 /// restoreIP - Sets the current insert point to a previously-saved
135 void restoreIP(InsertPoint IP) {
137 SetInsertPoint(IP.getBlock(), IP.getPoint());
139 ClearInsertionPoint();
142 //===--------------------------------------------------------------------===//
143 // Miscellaneous creation methods.
144 //===--------------------------------------------------------------------===//
146 /// CreateGlobalString - Make a new global variable with an initializer that
147 /// has array of i8 type filled in with the nul terminated string value
148 /// specified. If Name is specified, it is the name of the global variable
150 Value *CreateGlobalString(const char *Str = "", const Twine &Name = "");
152 /// getInt1 - Get a constant value representing either true or false.
153 ConstantInt *getInt1(bool V) {
154 return ConstantInt::get(getInt1Ty(), V);
157 /// getTrue - Get the constant value for i1 true.
158 ConstantInt *getTrue() {
159 return ConstantInt::getTrue(Context);
162 /// getFalse - Get the constant value for i1 false.
163 ConstantInt *getFalse() {
164 return ConstantInt::getFalse(Context);
167 /// getInt8 - Get a constant 8-bit value.
168 ConstantInt *getInt8(uint8_t C) {
169 return ConstantInt::get(getInt8Ty(), C);
172 /// getInt16 - Get a constant 16-bit value.
173 ConstantInt *getInt16(uint16_t C) {
174 return ConstantInt::get(getInt16Ty(), C);
177 /// getInt32 - Get a constant 32-bit value.
178 ConstantInt *getInt32(uint32_t C) {
179 return ConstantInt::get(getInt32Ty(), C);
182 /// getInt64 - Get a constant 64-bit value.
183 ConstantInt *getInt64(uint64_t C) {
184 return ConstantInt::get(getInt64Ty(), C);
187 //===--------------------------------------------------------------------===//
188 // Type creation methods
189 //===--------------------------------------------------------------------===//
191 /// getInt1Ty - Fetch the type representing a single bit
192 const IntegerType *getInt1Ty() {
193 return Type::getInt1Ty(Context);
196 /// getInt8Ty - Fetch the type representing an 8-bit integer.
197 const IntegerType *getInt8Ty() {
198 return Type::getInt8Ty(Context);
201 /// getInt16Ty - Fetch the type representing a 16-bit integer.
202 const IntegerType *getInt16Ty() {
203 return Type::getInt16Ty(Context);
206 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
207 const IntegerType *getInt32Ty() {
208 return Type::getInt32Ty(Context);
211 /// getInt64Ty - Fetch the type representing a 64-bit integer.
212 const IntegerType *getInt64Ty() {
213 return Type::getInt64Ty(Context);
216 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
217 const Type *getFloatTy() {
218 return Type::getFloatTy(Context);
221 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
222 const Type *getDoubleTy() {
223 return Type::getDoubleTy(Context);
226 /// getVoidTy - Fetch the type representing void.
227 const Type *getVoidTy() {
228 return Type::getVoidTy(Context);
231 const PointerType *getInt8PtrTy() {
232 return Type::getInt8PtrTy(Context);
235 /// getCurrentFunctionReturnType - Get the return type of the current function
236 /// that we're emitting into.
237 const Type *getCurrentFunctionReturnType() const;
240 /// IRBuilder - This provides a uniform API for creating instructions and
241 /// inserting them into a basic block: either at the end of a BasicBlock, or
242 /// at a specific iterator location in a block.
244 /// Note that the builder does not expose the full generality of LLVM
245 /// instructions. For access to extra instruction properties, use the mutators
246 /// (e.g. setVolatile) on the instructions after they have been created.
247 /// The first template argument handles whether or not to preserve names in the
248 /// final instruction output. This defaults to on. The second template argument
249 /// specifies a class to use for creating constants. This defaults to creating
250 /// minimally folded constants. The fourth template argument allows clients to
251 /// specify custom insertion hooks that are called on every newly created
253 template<bool preserveNames = true, typename T = ConstantFolder,
254 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
255 class IRBuilder : public IRBuilderBase, public Inserter {
258 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
259 : IRBuilderBase(C), Inserter(I), Folder(F) {
262 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder(C) {
265 explicit IRBuilder(BasicBlock *TheBB, const T &F)
266 : IRBuilderBase(TheBB->getContext()), Folder(F) {
267 SetInsertPoint(TheBB);
270 explicit IRBuilder(BasicBlock *TheBB)
271 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
272 SetInsertPoint(TheBB);
275 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
276 : IRBuilderBase(TheBB->getContext()), Folder(F) {
277 SetInsertPoint(TheBB, IP);
280 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
281 : IRBuilderBase(TheBB->getContext()), Folder(Context) {
282 SetInsertPoint(TheBB, IP);
285 /// getFolder - Get the constant folder being used.
286 const T &getFolder() { return Folder; }
288 /// isNamePreserving - Return true if this builder is configured to actually
289 /// add the requested names to IR created through it.
290 bool isNamePreserving() const { return preserveNames; }
292 /// Insert - Insert and return the specified instruction.
293 template<typename InstTy>
294 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
295 this->InsertHelper(I, Name, BB, InsertPt);
296 if (!getCurrentDebugLocation().isUnknown())
297 this->SetInstDebugLocation(I);
301 //===--------------------------------------------------------------------===//
302 // Instruction creation methods: Terminators
303 //===--------------------------------------------------------------------===//
305 /// CreateRetVoid - Create a 'ret void' instruction.
306 ReturnInst *CreateRetVoid() {
307 return Insert(ReturnInst::Create(Context));
311 /// CreateRet - Create a 'ret <val>' instruction.
313 ReturnInst *CreateRet(Value *V) {
314 return Insert(ReturnInst::Create(Context, V));
317 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
318 /// with one Value from the retVals array each, that build a aggregate
319 /// return value one value at a time, and a ret instruction to return
320 /// the resulting aggregate value. This is a convenience function for
321 /// code that uses aggregate return values as a vehicle for having
322 /// multiple return values.
324 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
325 Value *V = UndefValue::get(getCurrentFunctionReturnType());
326 for (unsigned i = 0; i != N; ++i)
327 V = CreateInsertValue(V, retVals[i], i, "mrv");
328 return Insert(ReturnInst::Create(Context, V));
331 /// CreateBr - Create an unconditional 'br label X' instruction.
332 BranchInst *CreateBr(BasicBlock *Dest) {
333 return Insert(BranchInst::Create(Dest));
336 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
338 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
339 return Insert(BranchInst::Create(True, False, Cond));
342 /// CreateSwitch - Create a switch instruction with the specified value,
343 /// default dest, and with a hint for the number of cases that will be added
344 /// (for efficient allocation).
345 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
346 return Insert(SwitchInst::Create(V, Dest, NumCases));
349 /// CreateIndirectBr - Create an indirect branch instruction with the
350 /// specified address operand, with an optional hint for the number of
351 /// destinations that will be added (for efficient allocation).
352 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
353 return Insert(IndirectBrInst::Create(Addr, NumDests));
356 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
357 BasicBlock *UnwindDest, const Twine &Name = "") {
358 Value *Args[] = { 0 };
359 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
362 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
363 BasicBlock *UnwindDest, Value *Arg1,
364 const Twine &Name = "") {
365 Value *Args[] = { Arg1 };
366 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
369 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
370 BasicBlock *UnwindDest, Value *Arg1,
371 Value *Arg2, Value *Arg3,
372 const Twine &Name = "") {
373 Value *Args[] = { Arg1, Arg2, Arg3 };
374 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
377 /// CreateInvoke - Create an invoke instruction.
378 template<typename InputIterator>
379 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
380 BasicBlock *UnwindDest, InputIterator ArgBegin,
381 InputIterator ArgEnd, const Twine &Name = "") {
382 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
383 ArgBegin, ArgEnd), Name);
386 UnwindInst *CreateUnwind() {
387 return Insert(new UnwindInst(Context));
390 UnreachableInst *CreateUnreachable() {
391 return Insert(new UnreachableInst(Context));
394 //===--------------------------------------------------------------------===//
395 // Instruction creation methods: Binary Operators
396 //===--------------------------------------------------------------------===//
398 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
399 if (Constant *LC = dyn_cast<Constant>(LHS))
400 if (Constant *RC = dyn_cast<Constant>(RHS))
401 return Folder.CreateAdd(LC, RC);
402 return Insert(BinaryOperator::CreateAdd(LHS, RHS), Name);
404 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
405 if (Constant *LC = dyn_cast<Constant>(LHS))
406 if (Constant *RC = dyn_cast<Constant>(RHS))
407 return Folder.CreateNSWAdd(LC, RC);
408 return Insert(BinaryOperator::CreateNSWAdd(LHS, RHS), Name);
410 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
411 if (Constant *LC = dyn_cast<Constant>(LHS))
412 if (Constant *RC = dyn_cast<Constant>(RHS))
413 return Folder.CreateNUWAdd(LC, RC);
414 return Insert(BinaryOperator::CreateNUWAdd(LHS, RHS), Name);
416 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
417 if (Constant *LC = dyn_cast<Constant>(LHS))
418 if (Constant *RC = dyn_cast<Constant>(RHS))
419 return Folder.CreateFAdd(LC, RC);
420 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
422 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "") {
423 if (Constant *LC = dyn_cast<Constant>(LHS))
424 if (Constant *RC = dyn_cast<Constant>(RHS))
425 return Folder.CreateSub(LC, RC);
426 return Insert(BinaryOperator::CreateSub(LHS, RHS), Name);
428 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
429 if (Constant *LC = dyn_cast<Constant>(LHS))
430 if (Constant *RC = dyn_cast<Constant>(RHS))
431 return Folder.CreateNSWSub(LC, RC);
432 return Insert(BinaryOperator::CreateNSWSub(LHS, RHS), Name);
434 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
435 if (Constant *LC = dyn_cast<Constant>(LHS))
436 if (Constant *RC = dyn_cast<Constant>(RHS))
437 return Folder.CreateNUWSub(LC, RC);
438 return Insert(BinaryOperator::CreateNUWSub(LHS, RHS), Name);
440 Value *CreateFSub(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.CreateFSub(LC, RC);
444 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
446 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "") {
447 if (Constant *LC = dyn_cast<Constant>(LHS))
448 if (Constant *RC = dyn_cast<Constant>(RHS))
449 return Folder.CreateMul(LC, RC);
450 return Insert(BinaryOperator::CreateMul(LHS, RHS), Name);
452 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
453 if (Constant *LC = dyn_cast<Constant>(LHS))
454 if (Constant *RC = dyn_cast<Constant>(RHS))
455 return Folder.CreateNSWMul(LC, RC);
456 return Insert(BinaryOperator::CreateNSWMul(LHS, RHS), Name);
458 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
459 if (Constant *LC = dyn_cast<Constant>(LHS))
460 if (Constant *RC = dyn_cast<Constant>(RHS))
461 return Folder.CreateNUWMul(LC, RC);
462 return Insert(BinaryOperator::CreateNUWMul(LHS, RHS), Name);
464 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
465 if (Constant *LC = dyn_cast<Constant>(LHS))
466 if (Constant *RC = dyn_cast<Constant>(RHS))
467 return Folder.CreateFMul(LC, RC);
468 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
470 Value *CreateUDiv(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.CreateUDiv(LC, RC);
474 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
476 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
477 if (Constant *LC = dyn_cast<Constant>(LHS))
478 if (Constant *RC = dyn_cast<Constant>(RHS))
479 return Folder.CreateSDiv(LC, RC);
480 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
482 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
483 if (Constant *LC = dyn_cast<Constant>(LHS))
484 if (Constant *RC = dyn_cast<Constant>(RHS))
485 return Folder.CreateExactSDiv(LC, RC);
486 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
488 Value *CreateFDiv(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.CreateFDiv(LC, RC);
492 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
494 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
495 if (Constant *LC = dyn_cast<Constant>(LHS))
496 if (Constant *RC = dyn_cast<Constant>(RHS))
497 return Folder.CreateURem(LC, RC);
498 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
500 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
501 if (Constant *LC = dyn_cast<Constant>(LHS))
502 if (Constant *RC = dyn_cast<Constant>(RHS))
503 return Folder.CreateSRem(LC, RC);
504 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
506 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
507 if (Constant *LC = dyn_cast<Constant>(LHS))
508 if (Constant *RC = dyn_cast<Constant>(RHS))
509 return Folder.CreateFRem(LC, RC);
510 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
513 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "") {
514 if (Constant *LC = dyn_cast<Constant>(LHS))
515 if (Constant *RC = dyn_cast<Constant>(RHS))
516 return Folder.CreateShl(LC, RC);
517 return Insert(BinaryOperator::CreateShl(LHS, RHS), Name);
519 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "") {
520 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
521 if (Constant *LC = dyn_cast<Constant>(LHS))
522 return Folder.CreateShl(LC, RHSC);
523 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
525 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "") {
526 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
527 if (Constant *LC = dyn_cast<Constant>(LHS))
528 return Folder.CreateShl(LC, RHSC);
529 return Insert(BinaryOperator::CreateShl(LHS, RHSC), Name);
532 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "") {
533 if (Constant *LC = dyn_cast<Constant>(LHS))
534 if (Constant *RC = dyn_cast<Constant>(RHS))
535 return Folder.CreateLShr(LC, RC);
536 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
538 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
539 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
540 if (Constant *LC = dyn_cast<Constant>(LHS))
541 return Folder.CreateLShr(LC, RHSC);
542 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
544 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
545 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
546 if (Constant *LC = dyn_cast<Constant>(LHS))
547 return Folder.CreateLShr(LC, RHSC);
548 return Insert(BinaryOperator::CreateLShr(LHS, RHSC), Name);
551 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "") {
552 if (Constant *LC = dyn_cast<Constant>(LHS))
553 if (Constant *RC = dyn_cast<Constant>(RHS))
554 return Folder.CreateAShr(LC, RC);
555 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
557 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
558 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
559 if (Constant *LC = dyn_cast<Constant>(LHS))
560 return Folder.CreateAShr(LC, RHSC);
561 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
563 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
564 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
565 if (Constant *LC = dyn_cast<Constant>(LHS))
566 return Folder.CreateAShr(LC, RHSC);
567 return Insert(BinaryOperator::CreateAShr(LHS, RHSC), Name);
570 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
571 if (Constant *RC = dyn_cast<Constant>(RHS)) {
572 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
573 return LHS; // LHS & -1 -> LHS
574 if (Constant *LC = dyn_cast<Constant>(LHS))
575 return Folder.CreateAnd(LC, RC);
577 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
579 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
580 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
581 if (Constant *LC = dyn_cast<Constant>(LHS))
582 return Folder.CreateAnd(LC, RHSC);
583 return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
585 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
586 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
587 if (Constant *LC = dyn_cast<Constant>(LHS))
588 return Folder.CreateAnd(LC, RHSC);
589 return Insert(BinaryOperator::CreateAnd(LHS, RHSC), Name);
592 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
593 if (Constant *RC = dyn_cast<Constant>(RHS)) {
594 if (RC->isNullValue())
595 return LHS; // LHS | 0 -> LHS
596 if (Constant *LC = dyn_cast<Constant>(LHS))
597 return Folder.CreateOr(LC, RC);
599 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
601 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
602 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
603 if (Constant *LC = dyn_cast<Constant>(LHS))
604 return Folder.CreateOr(LC, RHSC);
605 return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
607 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
608 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
609 if (Constant *LC = dyn_cast<Constant>(LHS))
610 return Folder.CreateOr(LC, RHSC);
611 return Insert(BinaryOperator::CreateOr(LHS, RHSC), Name);
614 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
615 if (Constant *LC = dyn_cast<Constant>(LHS))
616 if (Constant *RC = dyn_cast<Constant>(RHS))
617 return Folder.CreateXor(LC, RC);
618 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
620 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
621 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
622 if (Constant *LC = dyn_cast<Constant>(LHS))
623 return Folder.CreateXor(LC, RHSC);
624 return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
626 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
627 Constant *RHSC = ConstantInt::get(LHS->getType(), RHS);
628 if (Constant *LC = dyn_cast<Constant>(LHS))
629 return Folder.CreateXor(LC, RHSC);
630 return Insert(BinaryOperator::CreateXor(LHS, RHSC), Name);
633 Value *CreateBinOp(Instruction::BinaryOps Opc,
634 Value *LHS, Value *RHS, const Twine &Name = "") {
635 if (Constant *LC = dyn_cast<Constant>(LHS))
636 if (Constant *RC = dyn_cast<Constant>(RHS))
637 return Folder.CreateBinOp(Opc, LC, RC);
638 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
641 Value *CreateNeg(Value *V, const Twine &Name = "") {
642 if (Constant *VC = dyn_cast<Constant>(V))
643 return Folder.CreateNeg(VC);
644 return Insert(BinaryOperator::CreateNeg(V), Name);
646 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
647 if (Constant *VC = dyn_cast<Constant>(V))
648 return Folder.CreateNSWNeg(VC);
649 return Insert(BinaryOperator::CreateNSWNeg(V), Name);
651 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
652 if (Constant *VC = dyn_cast<Constant>(V))
653 return Folder.CreateNUWNeg(VC);
654 return Insert(BinaryOperator::CreateNUWNeg(V), Name);
656 Value *CreateFNeg(Value *V, const Twine &Name = "") {
657 if (Constant *VC = dyn_cast<Constant>(V))
658 return Folder.CreateFNeg(VC);
659 return Insert(BinaryOperator::CreateFNeg(V), Name);
661 Value *CreateNot(Value *V, const Twine &Name = "") {
662 if (Constant *VC = dyn_cast<Constant>(V))
663 return Folder.CreateNot(VC);
664 return Insert(BinaryOperator::CreateNot(V), Name);
667 //===--------------------------------------------------------------------===//
668 // Instruction creation methods: Memory Instructions
669 //===--------------------------------------------------------------------===//
671 AllocaInst *CreateAlloca(const Type *Ty, Value *ArraySize = 0,
672 const Twine &Name = "") {
673 return Insert(new AllocaInst(Ty, ArraySize), Name);
675 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
676 // converting the string to 'bool' for the isVolatile parameter.
677 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
678 return Insert(new LoadInst(Ptr), Name);
680 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
681 return Insert(new LoadInst(Ptr), Name);
683 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
684 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
686 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
687 return Insert(new StoreInst(Val, Ptr, isVolatile));
689 template<typename InputIterator>
690 Value *CreateGEP(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
691 const Twine &Name = "") {
692 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
693 // Every index must be constant.
695 for (i = IdxBegin; i < IdxEnd; ++i)
696 if (!isa<Constant>(*i))
699 return Folder.CreateGetElementPtr(PC, &IdxBegin[0], IdxEnd - IdxBegin);
701 return Insert(GetElementPtrInst::Create(Ptr, IdxBegin, IdxEnd), Name);
703 template<typename InputIterator>
704 Value *CreateInBoundsGEP(Value *Ptr, InputIterator IdxBegin,
705 InputIterator IdxEnd, const Twine &Name = "") {
706 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
707 // Every index must be constant.
709 for (i = IdxBegin; i < IdxEnd; ++i)
710 if (!isa<Constant>(*i))
713 return Folder.CreateInBoundsGetElementPtr(PC,
717 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxBegin, IdxEnd),
720 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
721 if (Constant *PC = dyn_cast<Constant>(Ptr))
722 if (Constant *IC = dyn_cast<Constant>(Idx))
723 return Folder.CreateGetElementPtr(PC, &IC, 1);
724 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
726 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
727 if (Constant *PC = dyn_cast<Constant>(Ptr))
728 if (Constant *IC = dyn_cast<Constant>(Idx))
729 return Folder.CreateInBoundsGetElementPtr(PC, &IC, 1);
730 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
732 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
733 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
735 if (Constant *PC = dyn_cast<Constant>(Ptr))
736 return Folder.CreateGetElementPtr(PC, &Idx, 1);
738 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
740 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
741 const Twine &Name = "") {
742 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
744 if (Constant *PC = dyn_cast<Constant>(Ptr))
745 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
747 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
749 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
750 const Twine &Name = "") {
752 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
753 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
756 if (Constant *PC = dyn_cast<Constant>(Ptr))
757 return Folder.CreateGetElementPtr(PC, Idxs, 2);
759 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
761 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
762 const Twine &Name = "") {
764 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
765 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
768 if (Constant *PC = dyn_cast<Constant>(Ptr))
769 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
771 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
773 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
774 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
776 if (Constant *PC = dyn_cast<Constant>(Ptr))
777 return Folder.CreateGetElementPtr(PC, &Idx, 1);
779 return Insert(GetElementPtrInst::Create(Ptr, &Idx, &Idx+1), Name);
781 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
782 const Twine &Name = "") {
783 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
785 if (Constant *PC = dyn_cast<Constant>(Ptr))
786 return Folder.CreateInBoundsGetElementPtr(PC, &Idx, 1);
788 return Insert(GetElementPtrInst::CreateInBounds(Ptr, &Idx, &Idx+1), Name);
790 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
791 const Twine &Name = "") {
793 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
794 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
797 if (Constant *PC = dyn_cast<Constant>(Ptr))
798 return Folder.CreateGetElementPtr(PC, Idxs, 2);
800 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
802 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
803 const Twine &Name = "") {
805 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
806 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
809 if (Constant *PC = dyn_cast<Constant>(Ptr))
810 return Folder.CreateInBoundsGetElementPtr(PC, Idxs, 2);
812 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs, Idxs+2), Name);
814 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
815 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
818 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
819 /// with "i8*" type instead of a pointer to array of i8.
820 Value *CreateGlobalStringPtr(const char *Str = "", const Twine &Name = "") {
821 Value *gv = CreateGlobalString(Str, Name);
822 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
823 Value *Args[] = { zero, zero };
824 return CreateInBoundsGEP(gv, Args, Args+2, Name);
827 //===--------------------------------------------------------------------===//
828 // Instruction creation methods: Cast/Conversion Operators
829 //===--------------------------------------------------------------------===//
831 Value *CreateTrunc(Value *V, const Type *DestTy, const Twine &Name = "") {
832 return CreateCast(Instruction::Trunc, V, DestTy, Name);
834 Value *CreateZExt(Value *V, const Type *DestTy, const Twine &Name = "") {
835 return CreateCast(Instruction::ZExt, V, DestTy, Name);
837 Value *CreateSExt(Value *V, const Type *DestTy, const Twine &Name = "") {
838 return CreateCast(Instruction::SExt, V, DestTy, Name);
840 Value *CreateFPToUI(Value *V, const Type *DestTy, const Twine &Name = ""){
841 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
843 Value *CreateFPToSI(Value *V, const Type *DestTy, const Twine &Name = ""){
844 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
846 Value *CreateUIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
847 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
849 Value *CreateSIToFP(Value *V, const Type *DestTy, const Twine &Name = ""){
850 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
852 Value *CreateFPTrunc(Value *V, const Type *DestTy,
853 const Twine &Name = "") {
854 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
856 Value *CreateFPExt(Value *V, const Type *DestTy, const Twine &Name = "") {
857 return CreateCast(Instruction::FPExt, V, DestTy, Name);
859 Value *CreatePtrToInt(Value *V, const Type *DestTy,
860 const Twine &Name = "") {
861 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
863 Value *CreateIntToPtr(Value *V, const Type *DestTy,
864 const Twine &Name = "") {
865 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
867 Value *CreateBitCast(Value *V, const Type *DestTy,
868 const Twine &Name = "") {
869 return CreateCast(Instruction::BitCast, V, DestTy, Name);
871 Value *CreateZExtOrBitCast(Value *V, const Type *DestTy,
872 const Twine &Name = "") {
873 if (V->getType() == DestTy)
875 if (Constant *VC = dyn_cast<Constant>(V))
876 return Folder.CreateZExtOrBitCast(VC, DestTy);
877 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
879 Value *CreateSExtOrBitCast(Value *V, const Type *DestTy,
880 const Twine &Name = "") {
881 if (V->getType() == DestTy)
883 if (Constant *VC = dyn_cast<Constant>(V))
884 return Folder.CreateSExtOrBitCast(VC, DestTy);
885 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
887 Value *CreateTruncOrBitCast(Value *V, const Type *DestTy,
888 const Twine &Name = "") {
889 if (V->getType() == DestTy)
891 if (Constant *VC = dyn_cast<Constant>(V))
892 return Folder.CreateTruncOrBitCast(VC, DestTy);
893 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
895 Value *CreateCast(Instruction::CastOps Op, Value *V, const Type *DestTy,
896 const Twine &Name = "") {
897 if (V->getType() == DestTy)
899 if (Constant *VC = dyn_cast<Constant>(V))
900 return Folder.CreateCast(Op, VC, DestTy);
901 return Insert(CastInst::Create(Op, V, DestTy), Name);
903 Value *CreatePointerCast(Value *V, const Type *DestTy,
904 const Twine &Name = "") {
905 if (V->getType() == DestTy)
907 if (Constant *VC = dyn_cast<Constant>(V))
908 return Folder.CreatePointerCast(VC, DestTy);
909 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
911 Value *CreateIntCast(Value *V, const Type *DestTy, bool isSigned,
912 const Twine &Name = "") {
913 if (V->getType() == DestTy)
915 if (Constant *VC = dyn_cast<Constant>(V))
916 return Folder.CreateIntCast(VC, DestTy, isSigned);
917 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
920 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
921 // error, instead of converting the string to bool for the isSigned parameter.
922 Value *CreateIntCast(Value *, const Type *, const char *); // DO NOT IMPLEMENT
924 Value *CreateFPCast(Value *V, const Type *DestTy, const Twine &Name = "") {
925 if (V->getType() == DestTy)
927 if (Constant *VC = dyn_cast<Constant>(V))
928 return Folder.CreateFPCast(VC, DestTy);
929 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
932 //===--------------------------------------------------------------------===//
933 // Instruction creation methods: Compare Instructions
934 //===--------------------------------------------------------------------===//
936 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
937 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
939 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
940 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
942 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
943 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
945 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
946 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
948 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
949 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
951 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
952 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
954 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
955 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
957 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
958 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
960 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
961 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
963 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
964 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
967 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
968 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
970 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
971 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
973 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
974 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
976 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
977 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
979 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
980 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
982 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
983 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
985 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
986 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
988 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
989 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
991 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
992 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
994 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
995 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
997 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
998 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1000 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1001 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1003 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1004 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1006 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1007 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1010 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1011 const Twine &Name = "") {
1012 if (Constant *LC = dyn_cast<Constant>(LHS))
1013 if (Constant *RC = dyn_cast<Constant>(RHS))
1014 return Folder.CreateICmp(P, LC, RC);
1015 return Insert(new ICmpInst(P, LHS, RHS), Name);
1017 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1018 const Twine &Name = "") {
1019 if (Constant *LC = dyn_cast<Constant>(LHS))
1020 if (Constant *RC = dyn_cast<Constant>(RHS))
1021 return Folder.CreateFCmp(P, LC, RC);
1022 return Insert(new FCmpInst(P, LHS, RHS), Name);
1025 //===--------------------------------------------------------------------===//
1026 // Instruction creation methods: Other Instructions
1027 //===--------------------------------------------------------------------===//
1029 PHINode *CreatePHI(const Type *Ty, const Twine &Name = "") {
1030 return Insert(PHINode::Create(Ty), Name);
1033 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1034 return Insert(CallInst::Create(Callee), Name);
1036 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1037 return Insert(CallInst::Create(Callee, Arg), Name);
1039 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1040 const Twine &Name = "") {
1041 Value *Args[] = { Arg1, Arg2 };
1042 return Insert(CallInst::Create(Callee, Args, Args+2), Name);
1044 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1045 const Twine &Name = "") {
1046 Value *Args[] = { Arg1, Arg2, Arg3 };
1047 return Insert(CallInst::Create(Callee, Args, Args+3), Name);
1049 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1050 Value *Arg4, const Twine &Name = "") {
1051 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1052 return Insert(CallInst::Create(Callee, Args, Args+4), Name);
1054 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1055 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1056 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1057 return Insert(CallInst::Create(Callee, Args, Args+5), Name);
1060 template<typename InputIterator>
1061 CallInst *CreateCall(Value *Callee, InputIterator ArgBegin,
1062 InputIterator ArgEnd, const Twine &Name = "") {
1063 return Insert(CallInst::Create(Callee, ArgBegin, ArgEnd), Name);
1066 Value *CreateSelect(Value *C, Value *True, Value *False,
1067 const Twine &Name = "") {
1068 if (Constant *CC = dyn_cast<Constant>(C))
1069 if (Constant *TC = dyn_cast<Constant>(True))
1070 if (Constant *FC = dyn_cast<Constant>(False))
1071 return Folder.CreateSelect(CC, TC, FC);
1072 return Insert(SelectInst::Create(C, True, False), Name);
1075 VAArgInst *CreateVAArg(Value *List, const Type *Ty, const Twine &Name = "") {
1076 return Insert(new VAArgInst(List, Ty), Name);
1079 Value *CreateExtractElement(Value *Vec, Value *Idx,
1080 const Twine &Name = "") {
1081 if (Constant *VC = dyn_cast<Constant>(Vec))
1082 if (Constant *IC = dyn_cast<Constant>(Idx))
1083 return Folder.CreateExtractElement(VC, IC);
1084 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1087 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1088 const Twine &Name = "") {
1089 if (Constant *VC = dyn_cast<Constant>(Vec))
1090 if (Constant *NC = dyn_cast<Constant>(NewElt))
1091 if (Constant *IC = dyn_cast<Constant>(Idx))
1092 return Folder.CreateInsertElement(VC, NC, IC);
1093 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1096 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1097 const Twine &Name = "") {
1098 if (Constant *V1C = dyn_cast<Constant>(V1))
1099 if (Constant *V2C = dyn_cast<Constant>(V2))
1100 if (Constant *MC = dyn_cast<Constant>(Mask))
1101 return Folder.CreateShuffleVector(V1C, V2C, MC);
1102 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1105 Value *CreateExtractValue(Value *Agg, unsigned Idx,
1106 const Twine &Name = "") {
1107 if (Constant *AggC = dyn_cast<Constant>(Agg))
1108 return Folder.CreateExtractValue(AggC, &Idx, 1);
1109 return Insert(ExtractValueInst::Create(Agg, Idx), Name);
1112 template<typename InputIterator>
1113 Value *CreateExtractValue(Value *Agg,
1114 InputIterator IdxBegin,
1115 InputIterator IdxEnd,
1116 const Twine &Name = "") {
1117 if (Constant *AggC = dyn_cast<Constant>(Agg))
1118 return Folder.CreateExtractValue(AggC, IdxBegin, IdxEnd - IdxBegin);
1119 return Insert(ExtractValueInst::Create(Agg, IdxBegin, IdxEnd), Name);
1122 Value *CreateInsertValue(Value *Agg, Value *Val, unsigned Idx,
1123 const Twine &Name = "") {
1124 if (Constant *AggC = dyn_cast<Constant>(Agg))
1125 if (Constant *ValC = dyn_cast<Constant>(Val))
1126 return Folder.CreateInsertValue(AggC, ValC, &Idx, 1);
1127 return Insert(InsertValueInst::Create(Agg, Val, Idx), Name);
1130 template<typename InputIterator>
1131 Value *CreateInsertValue(Value *Agg, Value *Val,
1132 InputIterator IdxBegin,
1133 InputIterator IdxEnd,
1134 const Twine &Name = "") {
1135 if (Constant *AggC = dyn_cast<Constant>(Agg))
1136 if (Constant *ValC = dyn_cast<Constant>(Val))
1137 return Folder.CreateInsertValue(AggC, ValC, IdxBegin, IdxEnd-IdxBegin);
1138 return Insert(InsertValueInst::Create(Agg, Val, IdxBegin, IdxEnd), Name);
1141 //===--------------------------------------------------------------------===//
1142 // Utility creation methods
1143 //===--------------------------------------------------------------------===//
1145 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1146 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1147 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1151 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1152 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1153 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1157 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1158 /// dividing out the size of the pointed-to objects. This is intended to
1159 /// implement C-style pointer subtraction. As such, the pointers must be
1160 /// appropriately aligned for their element types and pointing into the
1162 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1163 assert(LHS->getType() == RHS->getType() &&
1164 "Pointer subtraction operand types must match!");
1165 const PointerType *ArgType = cast<PointerType>(LHS->getType());
1166 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1167 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1168 Value *Difference = CreateSub(LHS_int, RHS_int);
1169 return CreateExactSDiv(Difference,
1170 ConstantExpr::getSizeOf(ArgType->getElementType()),