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/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 //===--------------------------------------------------------------------===//
414 /// CreateRetVoid - Create a 'ret void' instruction.
415 ReturnInst *CreateRetVoid() {
416 return Insert(ReturnInst::Create(Context));
420 /// CreateRet - Create a 'ret <val>' instruction.
422 ReturnInst *CreateRet(Value *V) {
423 return Insert(ReturnInst::Create(Context, V));
426 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
427 /// with one Value from the retVals array each, that build a aggregate
428 /// return value one value at a time, and a ret instruction to return
429 /// the resulting aggregate value. This is a convenience function for
430 /// code that uses aggregate return values as a vehicle for having
431 /// multiple return values.
433 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
434 Value *V = UndefValue::get(getCurrentFunctionReturnType());
435 for (unsigned i = 0; i != N; ++i)
436 V = CreateInsertValue(V, retVals[i], i, "mrv");
437 return Insert(ReturnInst::Create(Context, V));
440 /// CreateBr - Create an unconditional 'br label X' instruction.
441 BranchInst *CreateBr(BasicBlock *Dest) {
442 return Insert(BranchInst::Create(Dest));
445 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
447 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
448 return Insert(BranchInst::Create(True, False, Cond));
451 /// CreateSwitch - Create a switch instruction with the specified value,
452 /// default dest, and with a hint for the number of cases that will be added
453 /// (for efficient allocation).
454 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
455 return Insert(SwitchInst::Create(V, Dest, NumCases));
458 /// CreateIndirectBr - Create an indirect branch instruction with the
459 /// specified address operand, with an optional hint for the number of
460 /// destinations that will be added (for efficient allocation).
461 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
462 return Insert(IndirectBrInst::Create(Addr, NumDests));
465 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
466 BasicBlock *UnwindDest, const Twine &Name = "") {
467 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
468 ArrayRef<Value *>()),
471 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
472 BasicBlock *UnwindDest, Value *Arg1,
473 const Twine &Name = "") {
474 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
477 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
478 BasicBlock *UnwindDest, Value *Arg1,
479 Value *Arg2, Value *Arg3,
480 const Twine &Name = "") {
481 Value *Args[] = { Arg1, Arg2, Arg3 };
482 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
485 /// CreateInvoke - Create an invoke instruction.
486 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
487 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
488 const Twine &Name = "") {
489 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
493 ResumeInst *CreateResume(Value *Exn) {
494 return Insert(ResumeInst::Create(Exn));
497 UnreachableInst *CreateUnreachable() {
498 return Insert(new UnreachableInst(Context));
501 //===--------------------------------------------------------------------===//
502 // Instruction creation methods: Binary Operators
503 //===--------------------------------------------------------------------===//
505 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
506 Value *LHS, Value *RHS,
508 bool HasNUW, bool HasNSW) {
509 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
510 if (HasNUW) BO->setHasNoUnsignedWrap();
511 if (HasNSW) BO->setHasNoSignedWrap();
515 Instruction *AddFPMathTag(Instruction *I, MDNode *FPMathTag) const {
517 FPMathTag = DefaultFPMathTag;
519 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
523 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
524 bool HasNUW = false, bool HasNSW = false) {
525 if (Constant *LC = dyn_cast<Constant>(LHS))
526 if (Constant *RC = dyn_cast<Constant>(RHS))
527 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
528 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
531 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
532 return CreateAdd(LHS, RHS, Name, false, true);
534 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
535 return CreateAdd(LHS, RHS, Name, true, false);
537 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
538 MDNode *FPMathTag = 0) {
539 if (Constant *LC = dyn_cast<Constant>(LHS))
540 if (Constant *RC = dyn_cast<Constant>(RHS))
541 return Insert(Folder.CreateFAdd(LC, RC), Name);
542 return Insert(AddFPMathTag(BinaryOperator::CreateFAdd(LHS, RHS),
545 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
546 bool HasNUW = false, bool HasNSW = false) {
547 if (Constant *LC = dyn_cast<Constant>(LHS))
548 if (Constant *RC = dyn_cast<Constant>(RHS))
549 return Insert(Folder.CreateSub(LC, RC), Name);
550 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
553 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
554 return CreateSub(LHS, RHS, Name, false, true);
556 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
557 return CreateSub(LHS, RHS, Name, true, false);
559 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
560 MDNode *FPMathTag = 0) {
561 if (Constant *LC = dyn_cast<Constant>(LHS))
562 if (Constant *RC = dyn_cast<Constant>(RHS))
563 return Insert(Folder.CreateFSub(LC, RC), Name);
564 return Insert(AddFPMathTag(BinaryOperator::CreateFSub(LHS, RHS),
567 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
568 bool HasNUW = false, bool HasNSW = false) {
569 if (Constant *LC = dyn_cast<Constant>(LHS))
570 if (Constant *RC = dyn_cast<Constant>(RHS))
571 return Insert(Folder.CreateMul(LC, RC), Name);
572 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
575 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
576 return CreateMul(LHS, RHS, Name, false, true);
578 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
579 return CreateMul(LHS, RHS, Name, true, false);
581 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
582 MDNode *FPMathTag = 0) {
583 if (Constant *LC = dyn_cast<Constant>(LHS))
584 if (Constant *RC = dyn_cast<Constant>(RHS))
585 return Insert(Folder.CreateFMul(LC, RC), Name);
586 return Insert(AddFPMathTag(BinaryOperator::CreateFMul(LHS, RHS),
589 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
590 bool isExact = false) {
591 if (Constant *LC = dyn_cast<Constant>(LHS))
592 if (Constant *RC = dyn_cast<Constant>(RHS))
593 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
595 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
596 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
598 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
599 return CreateUDiv(LHS, RHS, Name, true);
601 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
602 bool isExact = false) {
603 if (Constant *LC = dyn_cast<Constant>(LHS))
604 if (Constant *RC = dyn_cast<Constant>(RHS))
605 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
607 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
608 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
610 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
611 return CreateSDiv(LHS, RHS, Name, true);
613 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
614 MDNode *FPMathTag = 0) {
615 if (Constant *LC = dyn_cast<Constant>(LHS))
616 if (Constant *RC = dyn_cast<Constant>(RHS))
617 return Insert(Folder.CreateFDiv(LC, RC), Name);
618 return Insert(AddFPMathTag(BinaryOperator::CreateFDiv(LHS, RHS),
621 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
622 if (Constant *LC = dyn_cast<Constant>(LHS))
623 if (Constant *RC = dyn_cast<Constant>(RHS))
624 return Insert(Folder.CreateURem(LC, RC), Name);
625 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
627 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
628 if (Constant *LC = dyn_cast<Constant>(LHS))
629 if (Constant *RC = dyn_cast<Constant>(RHS))
630 return Insert(Folder.CreateSRem(LC, RC), Name);
631 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
633 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
634 MDNode *FPMathTag = 0) {
635 if (Constant *LC = dyn_cast<Constant>(LHS))
636 if (Constant *RC = dyn_cast<Constant>(RHS))
637 return Insert(Folder.CreateFRem(LC, RC), Name);
638 return Insert(AddFPMathTag(BinaryOperator::CreateFRem(LHS, RHS),
642 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
643 bool HasNUW = false, bool HasNSW = false) {
644 if (Constant *LC = dyn_cast<Constant>(LHS))
645 if (Constant *RC = dyn_cast<Constant>(RHS))
646 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
647 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
650 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
651 bool HasNUW = false, bool HasNSW = false) {
652 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
655 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
656 bool HasNUW = false, bool HasNSW = false) {
657 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
661 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
662 bool isExact = false) {
663 if (Constant *LC = dyn_cast<Constant>(LHS))
664 if (Constant *RC = dyn_cast<Constant>(RHS))
665 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
667 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
668 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
670 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
671 bool isExact = false) {
672 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
674 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
675 bool isExact = false) {
676 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
679 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
680 bool isExact = false) {
681 if (Constant *LC = dyn_cast<Constant>(LHS))
682 if (Constant *RC = dyn_cast<Constant>(RHS))
683 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
685 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
686 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
688 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
689 bool isExact = false) {
690 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
692 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
693 bool isExact = false) {
694 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
697 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
698 if (Constant *RC = dyn_cast<Constant>(RHS)) {
699 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
700 return LHS; // LHS & -1 -> LHS
701 if (Constant *LC = dyn_cast<Constant>(LHS))
702 return Insert(Folder.CreateAnd(LC, RC), Name);
704 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
706 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
707 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
709 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
710 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
713 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
714 if (Constant *RC = dyn_cast<Constant>(RHS)) {
715 if (RC->isNullValue())
716 return LHS; // LHS | 0 -> LHS
717 if (Constant *LC = dyn_cast<Constant>(LHS))
718 return Insert(Folder.CreateOr(LC, RC), Name);
720 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
722 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
723 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
725 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
726 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
729 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
730 if (Constant *LC = dyn_cast<Constant>(LHS))
731 if (Constant *RC = dyn_cast<Constant>(RHS))
732 return Insert(Folder.CreateXor(LC, RC), Name);
733 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
735 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
736 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
738 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
739 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
742 Value *CreateBinOp(Instruction::BinaryOps Opc,
743 Value *LHS, Value *RHS, const Twine &Name = "") {
744 if (Constant *LC = dyn_cast<Constant>(LHS))
745 if (Constant *RC = dyn_cast<Constant>(RHS))
746 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
747 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
750 Value *CreateNeg(Value *V, const Twine &Name = "",
751 bool HasNUW = false, bool HasNSW = false) {
752 if (Constant *VC = dyn_cast<Constant>(V))
753 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
754 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
755 if (HasNUW) BO->setHasNoUnsignedWrap();
756 if (HasNSW) BO->setHasNoSignedWrap();
759 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
760 return CreateNeg(V, Name, false, true);
762 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
763 return CreateNeg(V, Name, true, false);
765 Value *CreateFNeg(Value *V, const Twine &Name = "", MDNode *FPMathTag = 0) {
766 if (Constant *VC = dyn_cast<Constant>(V))
767 return Insert(Folder.CreateFNeg(VC), Name);
768 return Insert(AddFPMathTag(BinaryOperator::CreateFNeg(V), FPMathTag), Name);
770 Value *CreateNot(Value *V, const Twine &Name = "") {
771 if (Constant *VC = dyn_cast<Constant>(V))
772 return Insert(Folder.CreateNot(VC), Name);
773 return Insert(BinaryOperator::CreateNot(V), Name);
776 //===--------------------------------------------------------------------===//
777 // Instruction creation methods: Memory Instructions
778 //===--------------------------------------------------------------------===//
780 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
781 const Twine &Name = "") {
782 return Insert(new AllocaInst(Ty, ArraySize), Name);
784 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
785 // converting the string to 'bool' for the isVolatile parameter.
786 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
787 return Insert(new LoadInst(Ptr), Name);
789 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
790 return Insert(new LoadInst(Ptr), Name);
792 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
793 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
795 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
796 return Insert(new StoreInst(Val, Ptr, isVolatile));
798 FenceInst *CreateFence(AtomicOrdering Ordering,
799 SynchronizationScope SynchScope = CrossThread) {
800 return Insert(new FenceInst(Context, Ordering, SynchScope));
802 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
803 AtomicOrdering Ordering,
804 SynchronizationScope SynchScope = CrossThread) {
805 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
807 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
808 AtomicOrdering Ordering,
809 SynchronizationScope SynchScope = CrossThread) {
810 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
812 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
813 const Twine &Name = "") {
814 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
815 // Every index must be constant.
817 for (i = 0, e = IdxList.size(); i != e; ++i)
818 if (!isa<Constant>(IdxList[i]))
821 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
823 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
825 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
826 const Twine &Name = "") {
827 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
828 // Every index must be constant.
830 for (i = 0, e = IdxList.size(); i != e; ++i)
831 if (!isa<Constant>(IdxList[i]))
834 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
836 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
838 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
839 if (Constant *PC = dyn_cast<Constant>(Ptr))
840 if (Constant *IC = dyn_cast<Constant>(Idx))
841 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
842 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
844 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
845 if (Constant *PC = dyn_cast<Constant>(Ptr))
846 if (Constant *IC = dyn_cast<Constant>(Idx))
847 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
848 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
850 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
851 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
853 if (Constant *PC = dyn_cast<Constant>(Ptr))
854 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
856 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
858 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
859 const Twine &Name = "") {
860 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
862 if (Constant *PC = dyn_cast<Constant>(Ptr))
863 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
865 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
867 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
868 const Twine &Name = "") {
870 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
871 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
874 if (Constant *PC = dyn_cast<Constant>(Ptr))
875 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
877 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
879 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
880 const Twine &Name = "") {
882 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
883 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
886 if (Constant *PC = dyn_cast<Constant>(Ptr))
887 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
889 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
891 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
892 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
894 if (Constant *PC = dyn_cast<Constant>(Ptr))
895 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
897 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
899 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
900 const Twine &Name = "") {
901 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
903 if (Constant *PC = dyn_cast<Constant>(Ptr))
904 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
906 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
908 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
909 const Twine &Name = "") {
911 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
912 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
915 if (Constant *PC = dyn_cast<Constant>(Ptr))
916 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
918 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
920 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
921 const Twine &Name = "") {
923 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
924 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
927 if (Constant *PC = dyn_cast<Constant>(Ptr))
928 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
930 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
932 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
933 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
936 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
937 /// with "i8*" type instead of a pointer to array of i8.
938 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
939 Value *gv = CreateGlobalString(Str, Name);
940 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
941 Value *Args[] = { zero, zero };
942 return CreateInBoundsGEP(gv, Args, Name);
945 //===--------------------------------------------------------------------===//
946 // Instruction creation methods: Cast/Conversion Operators
947 //===--------------------------------------------------------------------===//
949 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
950 return CreateCast(Instruction::Trunc, V, DestTy, Name);
952 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
953 return CreateCast(Instruction::ZExt, V, DestTy, Name);
955 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
956 return CreateCast(Instruction::SExt, V, DestTy, Name);
958 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
959 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
961 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
962 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
964 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
965 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
967 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
968 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
970 Value *CreateFPTrunc(Value *V, Type *DestTy,
971 const Twine &Name = "") {
972 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
974 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
975 return CreateCast(Instruction::FPExt, V, DestTy, Name);
977 Value *CreatePtrToInt(Value *V, Type *DestTy,
978 const Twine &Name = "") {
979 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
981 Value *CreateIntToPtr(Value *V, Type *DestTy,
982 const Twine &Name = "") {
983 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
985 Value *CreateBitCast(Value *V, Type *DestTy,
986 const Twine &Name = "") {
987 return CreateCast(Instruction::BitCast, V, DestTy, Name);
989 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
990 const Twine &Name = "") {
991 if (V->getType() == DestTy)
993 if (Constant *VC = dyn_cast<Constant>(V))
994 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
995 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
997 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
998 const Twine &Name = "") {
999 if (V->getType() == DestTy)
1001 if (Constant *VC = dyn_cast<Constant>(V))
1002 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1003 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1005 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1006 const Twine &Name = "") {
1007 if (V->getType() == DestTy)
1009 if (Constant *VC = dyn_cast<Constant>(V))
1010 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1011 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1013 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1014 const Twine &Name = "") {
1015 if (V->getType() == DestTy)
1017 if (Constant *VC = dyn_cast<Constant>(V))
1018 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1019 return Insert(CastInst::Create(Op, V, DestTy), Name);
1021 Value *CreatePointerCast(Value *V, Type *DestTy,
1022 const Twine &Name = "") {
1023 if (V->getType() == DestTy)
1025 if (Constant *VC = dyn_cast<Constant>(V))
1026 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1027 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1029 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1030 const Twine &Name = "") {
1031 if (V->getType() == DestTy)
1033 if (Constant *VC = dyn_cast<Constant>(V))
1034 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1035 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1038 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
1039 // error, instead of converting the string to bool for the isSigned parameter.
1040 Value *CreateIntCast(Value *, Type *, const char *); // DO NOT IMPLEMENT
1042 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1043 if (V->getType() == DestTy)
1045 if (Constant *VC = dyn_cast<Constant>(V))
1046 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1047 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1050 //===--------------------------------------------------------------------===//
1051 // Instruction creation methods: Compare Instructions
1052 //===--------------------------------------------------------------------===//
1054 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1055 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1057 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1058 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1060 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1061 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1063 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1064 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1066 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1067 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1069 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1070 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1072 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1073 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1075 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1076 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1078 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1079 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1081 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1082 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1085 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1086 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1088 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1089 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1091 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1092 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1094 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1095 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1097 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1098 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1100 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1101 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1103 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1104 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1106 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1107 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1109 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1110 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1112 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1113 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1115 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1116 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1118 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1119 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1121 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1122 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1124 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1125 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1128 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1129 const Twine &Name = "") {
1130 if (Constant *LC = dyn_cast<Constant>(LHS))
1131 if (Constant *RC = dyn_cast<Constant>(RHS))
1132 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1133 return Insert(new ICmpInst(P, LHS, RHS), Name);
1135 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1136 const Twine &Name = "") {
1137 if (Constant *LC = dyn_cast<Constant>(LHS))
1138 if (Constant *RC = dyn_cast<Constant>(RHS))
1139 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1140 return Insert(new FCmpInst(P, LHS, RHS), Name);
1143 //===--------------------------------------------------------------------===//
1144 // Instruction creation methods: Other Instructions
1145 //===--------------------------------------------------------------------===//
1147 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1148 const Twine &Name = "") {
1149 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1152 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1153 return Insert(CallInst::Create(Callee), Name);
1155 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1156 return Insert(CallInst::Create(Callee, Arg), Name);
1158 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1159 const Twine &Name = "") {
1160 Value *Args[] = { Arg1, Arg2 };
1161 return Insert(CallInst::Create(Callee, Args), Name);
1163 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1164 const Twine &Name = "") {
1165 Value *Args[] = { Arg1, Arg2, Arg3 };
1166 return Insert(CallInst::Create(Callee, Args), Name);
1168 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1169 Value *Arg4, const Twine &Name = "") {
1170 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1171 return Insert(CallInst::Create(Callee, Args), Name);
1173 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1174 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1175 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1176 return Insert(CallInst::Create(Callee, Args), Name);
1179 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1180 const Twine &Name = "") {
1181 return Insert(CallInst::Create(Callee, Args), Name);
1184 Value *CreateSelect(Value *C, Value *True, Value *False,
1185 const Twine &Name = "") {
1186 if (Constant *CC = dyn_cast<Constant>(C))
1187 if (Constant *TC = dyn_cast<Constant>(True))
1188 if (Constant *FC = dyn_cast<Constant>(False))
1189 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1190 return Insert(SelectInst::Create(C, True, False), Name);
1193 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1194 return Insert(new VAArgInst(List, Ty), Name);
1197 Value *CreateExtractElement(Value *Vec, Value *Idx,
1198 const Twine &Name = "") {
1199 if (Constant *VC = dyn_cast<Constant>(Vec))
1200 if (Constant *IC = dyn_cast<Constant>(Idx))
1201 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1202 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1205 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1206 const Twine &Name = "") {
1207 if (Constant *VC = dyn_cast<Constant>(Vec))
1208 if (Constant *NC = dyn_cast<Constant>(NewElt))
1209 if (Constant *IC = dyn_cast<Constant>(Idx))
1210 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1211 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1214 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1215 const Twine &Name = "") {
1216 if (Constant *V1C = dyn_cast<Constant>(V1))
1217 if (Constant *V2C = dyn_cast<Constant>(V2))
1218 if (Constant *MC = dyn_cast<Constant>(Mask))
1219 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1220 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1223 Value *CreateExtractValue(Value *Agg,
1224 ArrayRef<unsigned> Idxs,
1225 const Twine &Name = "") {
1226 if (Constant *AggC = dyn_cast<Constant>(Agg))
1227 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1228 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1231 Value *CreateInsertValue(Value *Agg, Value *Val,
1232 ArrayRef<unsigned> Idxs,
1233 const Twine &Name = "") {
1234 if (Constant *AggC = dyn_cast<Constant>(Agg))
1235 if (Constant *ValC = dyn_cast<Constant>(Val))
1236 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1237 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1240 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1241 const Twine &Name = "") {
1242 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses, Name));
1245 //===--------------------------------------------------------------------===//
1246 // Utility creation methods
1247 //===--------------------------------------------------------------------===//
1249 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1250 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1251 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1255 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1256 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1257 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1261 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1262 /// dividing out the size of the pointed-to objects. This is intended to
1263 /// implement C-style pointer subtraction. As such, the pointers must be
1264 /// appropriately aligned for their element types and pointing into the
1266 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1267 assert(LHS->getType() == RHS->getType() &&
1268 "Pointer subtraction operand types must match!");
1269 PointerType *ArgType = cast<PointerType>(LHS->getType());
1270 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1271 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1272 Value *Difference = CreateSub(LHS_int, RHS_int);
1273 return CreateExactSDiv(Difference,
1274 ConstantExpr::getSizeOf(ArgType->getElementType()),