1 //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- 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_IRBUILDER_H
16 #define LLVM_IRBUILDER_H
18 #include "llvm/Instructions.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/LLVMContext.h"
21 #include "llvm/ADT/ArrayRef.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Support/ConstantFolder.h"
29 /// IRBuilderDefaultInserter - This provides the default implementation of the
30 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
31 /// created by IRBuilder and needs to be inserted. By default, this inserts the
32 /// instruction at the insertion point.
33 template <bool preserveNames = true>
34 class IRBuilderDefaultInserter {
36 void InsertHelper(Instruction *I, const Twine &Name,
37 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
38 if (BB) BB->getInstList().insert(InsertPt, I);
44 /// IRBuilderBase - Common base class shared among various IRBuilders.
46 DebugLoc CurDbgLocation;
49 BasicBlock::iterator InsertPt;
53 IRBuilderBase(LLVMContext &context)
55 ClearInsertionPoint();
58 //===--------------------------------------------------------------------===//
59 // Builder configuration methods
60 //===--------------------------------------------------------------------===//
62 /// ClearInsertionPoint - Clear the insertion point: created instructions will
63 /// not be inserted into a block.
64 void ClearInsertionPoint() {
68 BasicBlock *GetInsertBlock() const { return BB; }
69 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
70 LLVMContext &getContext() const { return Context; }
72 /// SetInsertPoint - This specifies that created instructions should be
73 /// appended to the end of the specified block.
74 void SetInsertPoint(BasicBlock *TheBB) {
79 /// SetInsertPoint - This specifies that created instructions should be
80 /// inserted before the specified instruction.
81 void SetInsertPoint(Instruction *I) {
84 SetCurrentDebugLocation(I->getDebugLoc());
87 /// SetInsertPoint - This specifies that created instructions should be
88 /// inserted at the specified point.
89 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
94 /// SetInsertPoint(Use) - Find the nearest point that dominates this use, and
95 /// specify that created instructions should be inserted at this point.
96 void SetInsertPoint(Use &U) {
97 Instruction *UseInst = cast<Instruction>(U.getUser());
98 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
99 BasicBlock *PredBB = Phi->getIncomingBlock(U);
100 assert(U != PredBB->getTerminator() && "critical edge not split");
101 SetInsertPoint(PredBB, PredBB->getTerminator());
104 SetInsertPoint(UseInst);
107 /// SetCurrentDebugLocation - Set location information used by debugging
109 void SetCurrentDebugLocation(const DebugLoc &L) {
113 /// getCurrentDebugLocation - Get location information used by debugging
115 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
117 /// SetInstDebugLocation - If this builder has a current debug location, set
118 /// it on the specified instruction.
119 void SetInstDebugLocation(Instruction *I) const {
120 if (!CurDbgLocation.isUnknown())
121 I->setDebugLoc(CurDbgLocation);
124 /// getCurrentFunctionReturnType - Get the return type of the current function
125 /// that we're emitting into.
126 Type *getCurrentFunctionReturnType() const;
128 /// InsertPoint - A saved insertion point.
131 BasicBlock::iterator Point;
134 /// Creates a new insertion point which doesn't point to anything.
135 InsertPoint() : Block(0) {}
137 /// Creates a new insertion point at the given location.
138 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
139 : Block(InsertBlock), Point(InsertPoint) {}
141 /// isSet - Returns true if this insert point is set.
142 bool isSet() const { return (Block != 0); }
144 llvm::BasicBlock *getBlock() const { return Block; }
145 llvm::BasicBlock::iterator getPoint() const { return Point; }
148 /// saveIP - Returns the current insert point.
149 InsertPoint saveIP() const {
150 return InsertPoint(GetInsertBlock(), GetInsertPoint());
153 /// saveAndClearIP - Returns the current insert point, clearing it
155 InsertPoint saveAndClearIP() {
156 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
157 ClearInsertionPoint();
161 /// restoreIP - Sets the current insert point to a previously-saved
163 void restoreIP(InsertPoint IP) {
165 SetInsertPoint(IP.getBlock(), IP.getPoint());
167 ClearInsertionPoint();
170 //===--------------------------------------------------------------------===//
171 // Miscellaneous creation methods.
172 //===--------------------------------------------------------------------===//
174 /// CreateGlobalString - Make a new global variable with an initializer that
175 /// has array of i8 type filled in with the nul terminated string value
176 /// specified. The new global variable will be marked mergable with any
177 /// others of the same contents. If Name is specified, it is the name of the
178 /// global variable created.
179 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
181 /// getInt1 - Get a constant value representing either true or false.
182 ConstantInt *getInt1(bool V) {
183 return ConstantInt::get(getInt1Ty(), V);
186 /// getTrue - Get the constant value for i1 true.
187 ConstantInt *getTrue() {
188 return ConstantInt::getTrue(Context);
191 /// getFalse - Get the constant value for i1 false.
192 ConstantInt *getFalse() {
193 return ConstantInt::getFalse(Context);
196 /// getInt8 - Get a constant 8-bit value.
197 ConstantInt *getInt8(uint8_t C) {
198 return ConstantInt::get(getInt8Ty(), C);
201 /// getInt16 - Get a constant 16-bit value.
202 ConstantInt *getInt16(uint16_t C) {
203 return ConstantInt::get(getInt16Ty(), C);
206 /// getInt32 - Get a constant 32-bit value.
207 ConstantInt *getInt32(uint32_t C) {
208 return ConstantInt::get(getInt32Ty(), C);
211 /// getInt64 - Get a constant 64-bit value.
212 ConstantInt *getInt64(uint64_t C) {
213 return ConstantInt::get(getInt64Ty(), C);
216 /// getInt - Get a constant integer value.
217 ConstantInt *getInt(const APInt &AI) {
218 return ConstantInt::get(Context, AI);
221 //===--------------------------------------------------------------------===//
222 // Type creation methods
223 //===--------------------------------------------------------------------===//
225 /// getInt1Ty - Fetch the type representing a single bit
226 IntegerType *getInt1Ty() {
227 return Type::getInt1Ty(Context);
230 /// getInt8Ty - Fetch the type representing an 8-bit integer.
231 IntegerType *getInt8Ty() {
232 return Type::getInt8Ty(Context);
235 /// getInt16Ty - Fetch the type representing a 16-bit integer.
236 IntegerType *getInt16Ty() {
237 return Type::getInt16Ty(Context);
240 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
241 IntegerType *getInt32Ty() {
242 return Type::getInt32Ty(Context);
245 /// getInt64Ty - Fetch the type representing a 64-bit integer.
246 IntegerType *getInt64Ty() {
247 return Type::getInt64Ty(Context);
250 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
252 return Type::getFloatTy(Context);
255 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
256 Type *getDoubleTy() {
257 return Type::getDoubleTy(Context);
260 /// getVoidTy - Fetch the type representing void.
262 return Type::getVoidTy(Context);
265 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
266 return Type::getInt8PtrTy(Context, AddrSpace);
269 //===--------------------------------------------------------------------===//
270 // Intrinsic creation methods
271 //===--------------------------------------------------------------------===//
273 /// CreateMemSet - Create and insert a memset to the specified pointer and the
274 /// specified value. If the pointer isn't an i8*, it will be converted. If a
275 /// TBAA tag is specified, it will be added to the instruction.
276 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
277 bool isVolatile = false, MDNode *TBAATag = 0) {
278 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
281 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
282 bool isVolatile = false, MDNode *TBAATag = 0);
284 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
285 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
286 /// specified, it will be added to the instruction.
287 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
288 bool isVolatile = false, MDNode *TBAATag = 0) {
289 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
292 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
293 bool isVolatile = false, MDNode *TBAATag = 0);
295 /// CreateMemMove - Create and insert a memmove between the specified
296 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
297 /// tag is specified, it will be added to the instruction.
298 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
299 bool isVolatile = false, MDNode *TBAATag = 0) {
300 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
303 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
304 bool isVolatile = false, MDNode *TBAATag = 0);
306 /// CreateLifetimeStart - Create a lifetime.start intrinsic. If the pointer
307 /// isn't i8* it will be converted.
308 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
310 /// CreateLifetimeEnd - Create a lifetime.end intrinsic. If the pointer isn't
311 /// i8* it will be converted.
312 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
315 Value *getCastedInt8PtrValue(Value *Ptr);
318 /// IRBuilder - This provides a uniform API for creating instructions and
319 /// inserting them into a basic block: either at the end of a BasicBlock, or
320 /// at a specific iterator location in a block.
322 /// Note that the builder does not expose the full generality of LLVM
323 /// instructions. For access to extra instruction properties, use the mutators
324 /// (e.g. setVolatile) on the instructions after they have been created.
325 /// The first template argument handles whether or not to preserve names in the
326 /// final instruction output. This defaults to on. The second template argument
327 /// specifies a class to use for creating constants. This defaults to creating
328 /// minimally folded constants. The fourth template argument allows clients to
329 /// specify custom insertion hooks that are called on every newly created
331 template<bool preserveNames = true, typename T = ConstantFolder,
332 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
333 class IRBuilder : public IRBuilderBase, public Inserter {
335 MDNode *DefaultFPMathTag;
337 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
338 MDNode *FPMathTag = 0)
339 : IRBuilderBase(C), Inserter(I), Folder(F), DefaultFPMathTag(FPMathTag) {
342 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = 0) : IRBuilderBase(C),
343 Folder(), DefaultFPMathTag(FPMathTag) {
346 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = 0)
347 : IRBuilderBase(TheBB->getContext()), Folder(F),
348 DefaultFPMathTag(FPMathTag) {
349 SetInsertPoint(TheBB);
352 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = 0)
353 : IRBuilderBase(TheBB->getContext()), Folder(),
354 DefaultFPMathTag(FPMathTag) {
355 SetInsertPoint(TheBB);
358 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = 0)
359 : IRBuilderBase(IP->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
361 SetCurrentDebugLocation(IP->getDebugLoc());
364 explicit IRBuilder(Use &U, MDNode *FPMathTag = 0)
365 : IRBuilderBase(U->getContext()), Folder(), DefaultFPMathTag(FPMathTag) {
367 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
370 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
371 MDNode *FPMathTag = 0)
372 : IRBuilderBase(TheBB->getContext()), Folder(F),
373 DefaultFPMathTag(FPMathTag) {
374 SetInsertPoint(TheBB, IP);
377 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, MDNode *FPMathTag = 0)
378 : IRBuilderBase(TheBB->getContext()), Folder(),
379 DefaultFPMathTag(FPMathTag) {
380 SetInsertPoint(TheBB, IP);
383 /// getFolder - Get the constant folder being used.
384 const T &getFolder() { return Folder; }
386 /// getDefaultFPMathTag - Get the floating point math metadata being used.
387 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
389 /// SetDefaultFPMathTag - Set the floating point math metadata to be used.
390 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
392 /// isNamePreserving - Return true if this builder is configured to actually
393 /// add the requested names to IR created through it.
394 bool isNamePreserving() const { return preserveNames; }
396 /// Insert - Insert and return the specified instruction.
397 template<typename InstTy>
398 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
399 this->InsertHelper(I, Name, BB, InsertPt);
400 if (!getCurrentDebugLocation().isUnknown())
401 this->SetInstDebugLocation(I);
405 /// Insert - No-op overload to handle constants.
406 Constant *Insert(Constant *C, const Twine& = "") const {
410 //===--------------------------------------------------------------------===//
411 // Instruction creation methods: Terminators
412 //===--------------------------------------------------------------------===//
415 /// \brief Helper to add branch weight metadata onto an instruction.
416 /// \returns The annotated instruction.
417 template <typename InstTy>
418 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
420 I->setMetadata(LLVMContext::MD_prof, Weights);
425 /// CreateRetVoid - Create a 'ret void' instruction.
426 ReturnInst *CreateRetVoid() {
427 return Insert(ReturnInst::Create(Context));
431 /// CreateRet - Create a 'ret <val>' instruction.
433 ReturnInst *CreateRet(Value *V) {
434 return Insert(ReturnInst::Create(Context, V));
437 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
438 /// with one Value from the retVals array each, that build a aggregate
439 /// return value one value at a time, and a ret instruction to return
440 /// the resulting aggregate value. This is a convenience function for
441 /// code that uses aggregate return values as a vehicle for having
442 /// multiple return values.
444 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
445 Value *V = UndefValue::get(getCurrentFunctionReturnType());
446 for (unsigned i = 0; i != N; ++i)
447 V = CreateInsertValue(V, retVals[i], i, "mrv");
448 return Insert(ReturnInst::Create(Context, V));
451 /// CreateBr - Create an unconditional 'br label X' instruction.
452 BranchInst *CreateBr(BasicBlock *Dest) {
453 return Insert(BranchInst::Create(Dest));
456 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
458 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
459 MDNode *BranchWeights = 0) {
460 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
464 /// CreateSwitch - Create a switch instruction with the specified value,
465 /// default dest, and with a hint for the number of cases that will be added
466 /// (for efficient allocation).
467 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
468 MDNode *BranchWeights = 0) {
469 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
473 /// CreateIndirectBr - Create an indirect branch instruction with the
474 /// specified address operand, with an optional hint for the number of
475 /// destinations that will be added (for efficient allocation).
476 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
477 return Insert(IndirectBrInst::Create(Addr, NumDests));
480 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
481 BasicBlock *UnwindDest, const Twine &Name = "") {
482 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
483 ArrayRef<Value *>()),
486 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
487 BasicBlock *UnwindDest, Value *Arg1,
488 const Twine &Name = "") {
489 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
492 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
493 BasicBlock *UnwindDest, Value *Arg1,
494 Value *Arg2, Value *Arg3,
495 const Twine &Name = "") {
496 Value *Args[] = { Arg1, Arg2, Arg3 };
497 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
500 /// CreateInvoke - Create an invoke instruction.
501 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
502 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
503 const Twine &Name = "") {
504 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
508 ResumeInst *CreateResume(Value *Exn) {
509 return Insert(ResumeInst::Create(Exn));
512 UnreachableInst *CreateUnreachable() {
513 return Insert(new UnreachableInst(Context));
516 //===--------------------------------------------------------------------===//
517 // Instruction creation methods: Binary Operators
518 //===--------------------------------------------------------------------===//
520 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
521 Value *LHS, Value *RHS,
523 bool HasNUW, bool HasNSW) {
524 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
525 if (HasNUW) BO->setHasNoUnsignedWrap();
526 if (HasNSW) BO->setHasNoSignedWrap();
530 Instruction *AddFPMathTag(Instruction *I, MDNode *FPMathTag) const {
532 FPMathTag = DefaultFPMathTag;
534 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
538 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
539 bool HasNUW = false, bool HasNSW = false) {
540 if (Constant *LC = dyn_cast<Constant>(LHS))
541 if (Constant *RC = dyn_cast<Constant>(RHS))
542 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
543 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
546 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
547 return CreateAdd(LHS, RHS, Name, false, true);
549 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
550 return CreateAdd(LHS, RHS, Name, true, false);
552 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
553 MDNode *FPMathTag = 0) {
554 if (Constant *LC = dyn_cast<Constant>(LHS))
555 if (Constant *RC = dyn_cast<Constant>(RHS))
556 return Insert(Folder.CreateFAdd(LC, RC), Name);
557 return Insert(AddFPMathTag(BinaryOperator::CreateFAdd(LHS, RHS),
560 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
561 bool HasNUW = false, bool HasNSW = false) {
562 if (Constant *LC = dyn_cast<Constant>(LHS))
563 if (Constant *RC = dyn_cast<Constant>(RHS))
564 return Insert(Folder.CreateSub(LC, RC), Name);
565 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
568 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
569 return CreateSub(LHS, RHS, Name, false, true);
571 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
572 return CreateSub(LHS, RHS, Name, true, false);
574 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
575 MDNode *FPMathTag = 0) {
576 if (Constant *LC = dyn_cast<Constant>(LHS))
577 if (Constant *RC = dyn_cast<Constant>(RHS))
578 return Insert(Folder.CreateFSub(LC, RC), Name);
579 return Insert(AddFPMathTag(BinaryOperator::CreateFSub(LHS, RHS),
582 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
583 bool HasNUW = false, bool HasNSW = false) {
584 if (Constant *LC = dyn_cast<Constant>(LHS))
585 if (Constant *RC = dyn_cast<Constant>(RHS))
586 return Insert(Folder.CreateMul(LC, RC), Name);
587 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
590 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
591 return CreateMul(LHS, RHS, Name, false, true);
593 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
594 return CreateMul(LHS, RHS, Name, true, false);
596 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
597 MDNode *FPMathTag = 0) {
598 if (Constant *LC = dyn_cast<Constant>(LHS))
599 if (Constant *RC = dyn_cast<Constant>(RHS))
600 return Insert(Folder.CreateFMul(LC, RC), Name);
601 return Insert(AddFPMathTag(BinaryOperator::CreateFMul(LHS, RHS),
604 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
605 bool isExact = false) {
606 if (Constant *LC = dyn_cast<Constant>(LHS))
607 if (Constant *RC = dyn_cast<Constant>(RHS))
608 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
610 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
611 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
613 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
614 return CreateUDiv(LHS, RHS, Name, true);
616 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
617 bool isExact = false) {
618 if (Constant *LC = dyn_cast<Constant>(LHS))
619 if (Constant *RC = dyn_cast<Constant>(RHS))
620 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
622 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
623 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
625 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
626 return CreateSDiv(LHS, RHS, Name, true);
628 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
629 MDNode *FPMathTag = 0) {
630 if (Constant *LC = dyn_cast<Constant>(LHS))
631 if (Constant *RC = dyn_cast<Constant>(RHS))
632 return Insert(Folder.CreateFDiv(LC, RC), Name);
633 return Insert(AddFPMathTag(BinaryOperator::CreateFDiv(LHS, RHS),
636 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
637 if (Constant *LC = dyn_cast<Constant>(LHS))
638 if (Constant *RC = dyn_cast<Constant>(RHS))
639 return Insert(Folder.CreateURem(LC, RC), Name);
640 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
642 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
643 if (Constant *LC = dyn_cast<Constant>(LHS))
644 if (Constant *RC = dyn_cast<Constant>(RHS))
645 return Insert(Folder.CreateSRem(LC, RC), Name);
646 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
648 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
649 MDNode *FPMathTag = 0) {
650 if (Constant *LC = dyn_cast<Constant>(LHS))
651 if (Constant *RC = dyn_cast<Constant>(RHS))
652 return Insert(Folder.CreateFRem(LC, RC), Name);
653 return Insert(AddFPMathTag(BinaryOperator::CreateFRem(LHS, RHS),
657 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
658 bool HasNUW = false, bool HasNSW = false) {
659 if (Constant *LC = dyn_cast<Constant>(LHS))
660 if (Constant *RC = dyn_cast<Constant>(RHS))
661 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
662 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
665 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
666 bool HasNUW = false, bool HasNSW = false) {
667 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
670 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
671 bool HasNUW = false, bool HasNSW = false) {
672 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
676 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
677 bool isExact = false) {
678 if (Constant *LC = dyn_cast<Constant>(LHS))
679 if (Constant *RC = dyn_cast<Constant>(RHS))
680 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
682 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
683 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
685 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
686 bool isExact = false) {
687 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
689 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
690 bool isExact = false) {
691 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
694 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
695 bool isExact = false) {
696 if (Constant *LC = dyn_cast<Constant>(LHS))
697 if (Constant *RC = dyn_cast<Constant>(RHS))
698 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
700 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
701 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
703 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
704 bool isExact = false) {
705 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
707 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
708 bool isExact = false) {
709 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
712 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
713 if (Constant *RC = dyn_cast<Constant>(RHS)) {
714 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
715 return LHS; // LHS & -1 -> LHS
716 if (Constant *LC = dyn_cast<Constant>(LHS))
717 return Insert(Folder.CreateAnd(LC, RC), Name);
719 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
721 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
722 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
724 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
725 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
728 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
729 if (Constant *RC = dyn_cast<Constant>(RHS)) {
730 if (RC->isNullValue())
731 return LHS; // LHS | 0 -> LHS
732 if (Constant *LC = dyn_cast<Constant>(LHS))
733 return Insert(Folder.CreateOr(LC, RC), Name);
735 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
737 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
738 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
740 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
741 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
744 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
745 if (Constant *LC = dyn_cast<Constant>(LHS))
746 if (Constant *RC = dyn_cast<Constant>(RHS))
747 return Insert(Folder.CreateXor(LC, RC), Name);
748 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
750 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
751 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
753 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
754 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
757 Value *CreateBinOp(Instruction::BinaryOps Opc,
758 Value *LHS, Value *RHS, const Twine &Name = "") {
759 if (Constant *LC = dyn_cast<Constant>(LHS))
760 if (Constant *RC = dyn_cast<Constant>(RHS))
761 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
762 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
765 Value *CreateNeg(Value *V, const Twine &Name = "",
766 bool HasNUW = false, bool HasNSW = false) {
767 if (Constant *VC = dyn_cast<Constant>(V))
768 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
769 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
770 if (HasNUW) BO->setHasNoUnsignedWrap();
771 if (HasNSW) BO->setHasNoSignedWrap();
774 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
775 return CreateNeg(V, Name, false, true);
777 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
778 return CreateNeg(V, Name, true, false);
780 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
781 if (Constant *VC = dyn_cast<Constant>(V))
782 return Insert(Folder.CreateFNeg(VC), Name);
783 return Insert(AddFPMathTag(BinaryOperator::CreateFNeg(V), FPMathTag), Name);
785 Value *CreateNot(Value *V, const Twine &Name = "") {
786 if (Constant *VC = dyn_cast<Constant>(V))
787 return Insert(Folder.CreateNot(VC), Name);
788 return Insert(BinaryOperator::CreateNot(V), Name);
791 //===--------------------------------------------------------------------===//
792 // Instruction creation methods: Memory Instructions
793 //===--------------------------------------------------------------------===//
795 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
796 const Twine &Name = "") {
797 return Insert(new AllocaInst(Ty, ArraySize), Name);
799 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
800 // converting the string to 'bool' for the isVolatile parameter.
801 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
802 return Insert(new LoadInst(Ptr), Name);
804 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
805 return Insert(new LoadInst(Ptr), Name);
807 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
808 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
810 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
811 return Insert(new StoreInst(Val, Ptr, isVolatile));
813 FenceInst *CreateFence(AtomicOrdering Ordering,
814 SynchronizationScope SynchScope = CrossThread) {
815 return Insert(new FenceInst(Context, Ordering, SynchScope));
817 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
818 AtomicOrdering Ordering,
819 SynchronizationScope SynchScope = CrossThread) {
820 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
822 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
823 AtomicOrdering Ordering,
824 SynchronizationScope SynchScope = CrossThread) {
825 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
827 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
828 const Twine &Name = "") {
829 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
830 // Every index must be constant.
832 for (i = 0, e = IdxList.size(); i != e; ++i)
833 if (!isa<Constant>(IdxList[i]))
836 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
838 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
840 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
841 const Twine &Name = "") {
842 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
843 // Every index must be constant.
845 for (i = 0, e = IdxList.size(); i != e; ++i)
846 if (!isa<Constant>(IdxList[i]))
849 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
851 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
853 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
854 if (Constant *PC = dyn_cast<Constant>(Ptr))
855 if (Constant *IC = dyn_cast<Constant>(Idx))
856 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
857 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
859 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
860 if (Constant *PC = dyn_cast<Constant>(Ptr))
861 if (Constant *IC = dyn_cast<Constant>(Idx))
862 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
863 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
865 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
866 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
868 if (Constant *PC = dyn_cast<Constant>(Ptr))
869 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
871 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
873 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
874 const Twine &Name = "") {
875 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
877 if (Constant *PC = dyn_cast<Constant>(Ptr))
878 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
880 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
882 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
883 const Twine &Name = "") {
885 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
886 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
889 if (Constant *PC = dyn_cast<Constant>(Ptr))
890 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
892 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
894 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
895 const Twine &Name = "") {
897 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
898 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
901 if (Constant *PC = dyn_cast<Constant>(Ptr))
902 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
904 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
906 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
907 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
909 if (Constant *PC = dyn_cast<Constant>(Ptr))
910 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
912 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
914 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
915 const Twine &Name = "") {
916 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
918 if (Constant *PC = dyn_cast<Constant>(Ptr))
919 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
921 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
923 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
924 const Twine &Name = "") {
926 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
927 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
930 if (Constant *PC = dyn_cast<Constant>(Ptr))
931 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
933 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
935 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
936 const Twine &Name = "") {
938 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
939 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
942 if (Constant *PC = dyn_cast<Constant>(Ptr))
943 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
945 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
947 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
948 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
951 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
952 /// with "i8*" type instead of a pointer to array of i8.
953 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
954 Value *gv = CreateGlobalString(Str, Name);
955 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
956 Value *Args[] = { zero, zero };
957 return CreateInBoundsGEP(gv, Args, Name);
960 //===--------------------------------------------------------------------===//
961 // Instruction creation methods: Cast/Conversion Operators
962 //===--------------------------------------------------------------------===//
964 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
965 return CreateCast(Instruction::Trunc, V, DestTy, Name);
967 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
968 return CreateCast(Instruction::ZExt, V, DestTy, Name);
970 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
971 return CreateCast(Instruction::SExt, V, DestTy, Name);
973 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
974 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
976 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
977 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
979 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
980 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
982 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
983 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
985 Value *CreateFPTrunc(Value *V, Type *DestTy,
986 const Twine &Name = "") {
987 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
989 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
990 return CreateCast(Instruction::FPExt, V, DestTy, Name);
992 Value *CreatePtrToInt(Value *V, Type *DestTy,
993 const Twine &Name = "") {
994 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
996 Value *CreateIntToPtr(Value *V, Type *DestTy,
997 const Twine &Name = "") {
998 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1000 Value *CreateBitCast(Value *V, Type *DestTy,
1001 const Twine &Name = "") {
1002 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1004 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1005 const Twine &Name = "") {
1006 if (V->getType() == DestTy)
1008 if (Constant *VC = dyn_cast<Constant>(V))
1009 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1010 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1012 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1013 const Twine &Name = "") {
1014 if (V->getType() == DestTy)
1016 if (Constant *VC = dyn_cast<Constant>(V))
1017 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1018 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1020 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1021 const Twine &Name = "") {
1022 if (V->getType() == DestTy)
1024 if (Constant *VC = dyn_cast<Constant>(V))
1025 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1026 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1028 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1029 const Twine &Name = "") {
1030 if (V->getType() == DestTy)
1032 if (Constant *VC = dyn_cast<Constant>(V))
1033 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1034 return Insert(CastInst::Create(Op, V, DestTy), Name);
1036 Value *CreatePointerCast(Value *V, Type *DestTy,
1037 const Twine &Name = "") {
1038 if (V->getType() == DestTy)
1040 if (Constant *VC = dyn_cast<Constant>(V))
1041 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1042 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1044 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1045 const Twine &Name = "") {
1046 if (V->getType() == DestTy)
1048 if (Constant *VC = dyn_cast<Constant>(V))
1049 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1050 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1053 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
1054 // error, instead of converting the string to bool for the isSigned parameter.
1055 Value *CreateIntCast(Value *, Type *, const char *); // DO NOT IMPLEMENT
1057 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1058 if (V->getType() == DestTy)
1060 if (Constant *VC = dyn_cast<Constant>(V))
1061 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1062 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1065 //===--------------------------------------------------------------------===//
1066 // Instruction creation methods: Compare Instructions
1067 //===--------------------------------------------------------------------===//
1069 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1070 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1072 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1073 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1075 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1076 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1078 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1079 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1081 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1082 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1084 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1085 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1087 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1088 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1090 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1091 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1093 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1094 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1096 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1097 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1100 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1101 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1103 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1104 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1106 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1107 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1109 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1110 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1112 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1113 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1115 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1116 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1118 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1119 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1121 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1122 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1124 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1125 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1127 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1128 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1130 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1131 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1133 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1134 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1136 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1137 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1139 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1140 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1143 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1144 const Twine &Name = "") {
1145 if (Constant *LC = dyn_cast<Constant>(LHS))
1146 if (Constant *RC = dyn_cast<Constant>(RHS))
1147 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1148 return Insert(new ICmpInst(P, LHS, RHS), Name);
1150 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1151 const Twine &Name = "") {
1152 if (Constant *LC = dyn_cast<Constant>(LHS))
1153 if (Constant *RC = dyn_cast<Constant>(RHS))
1154 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1155 return Insert(new FCmpInst(P, LHS, RHS), Name);
1158 //===--------------------------------------------------------------------===//
1159 // Instruction creation methods: Other Instructions
1160 //===--------------------------------------------------------------------===//
1162 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1163 const Twine &Name = "") {
1164 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1167 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1168 return Insert(CallInst::Create(Callee), Name);
1170 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1171 return Insert(CallInst::Create(Callee, Arg), Name);
1173 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1174 const Twine &Name = "") {
1175 Value *Args[] = { Arg1, Arg2 };
1176 return Insert(CallInst::Create(Callee, Args), Name);
1178 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1179 const Twine &Name = "") {
1180 Value *Args[] = { Arg1, Arg2, Arg3 };
1181 return Insert(CallInst::Create(Callee, Args), Name);
1183 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1184 Value *Arg4, const Twine &Name = "") {
1185 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1186 return Insert(CallInst::Create(Callee, Args), Name);
1188 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1189 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1190 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1191 return Insert(CallInst::Create(Callee, Args), Name);
1194 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1195 const Twine &Name = "") {
1196 return Insert(CallInst::Create(Callee, Args), Name);
1199 Value *CreateSelect(Value *C, Value *True, Value *False,
1200 const Twine &Name = "") {
1201 if (Constant *CC = dyn_cast<Constant>(C))
1202 if (Constant *TC = dyn_cast<Constant>(True))
1203 if (Constant *FC = dyn_cast<Constant>(False))
1204 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1205 return Insert(SelectInst::Create(C, True, False), Name);
1208 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1209 return Insert(new VAArgInst(List, Ty), Name);
1212 Value *CreateExtractElement(Value *Vec, Value *Idx,
1213 const Twine &Name = "") {
1214 if (Constant *VC = dyn_cast<Constant>(Vec))
1215 if (Constant *IC = dyn_cast<Constant>(Idx))
1216 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1217 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1220 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1221 const Twine &Name = "") {
1222 if (Constant *VC = dyn_cast<Constant>(Vec))
1223 if (Constant *NC = dyn_cast<Constant>(NewElt))
1224 if (Constant *IC = dyn_cast<Constant>(Idx))
1225 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1226 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1229 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1230 const Twine &Name = "") {
1231 if (Constant *V1C = dyn_cast<Constant>(V1))
1232 if (Constant *V2C = dyn_cast<Constant>(V2))
1233 if (Constant *MC = dyn_cast<Constant>(Mask))
1234 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1235 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1238 Value *CreateExtractValue(Value *Agg,
1239 ArrayRef<unsigned> Idxs,
1240 const Twine &Name = "") {
1241 if (Constant *AggC = dyn_cast<Constant>(Agg))
1242 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1243 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1246 Value *CreateInsertValue(Value *Agg, Value *Val,
1247 ArrayRef<unsigned> Idxs,
1248 const Twine &Name = "") {
1249 if (Constant *AggC = dyn_cast<Constant>(Agg))
1250 if (Constant *ValC = dyn_cast<Constant>(Val))
1251 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1252 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1255 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1256 const Twine &Name = "") {
1257 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses, Name));
1260 //===--------------------------------------------------------------------===//
1261 // Utility creation methods
1262 //===--------------------------------------------------------------------===//
1264 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1265 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1266 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1270 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1271 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1272 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1276 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1277 /// dividing out the size of the pointed-to objects. This is intended to
1278 /// implement C-style pointer subtraction. As such, the pointers must be
1279 /// appropriately aligned for their element types and pointing into the
1281 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1282 assert(LHS->getType() == RHS->getType() &&
1283 "Pointer subtraction operand types must match!");
1284 PointerType *ArgType = cast<PointerType>(LHS->getType());
1285 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1286 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1287 Value *Difference = CreateSub(LHS_int, RHS_int);
1288 return CreateExactSDiv(Difference,
1289 ConstantExpr::getSizeOf(ArgType->getElementType()),