1 //===---- llvm/Support/IRBuilder.h - Builder for LLVM Instrs ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the IRBuilder class, which is used as a convenient way
11 // to create LLVM instructions with a consistent and simplified interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_IRBUILDER_H
16 #define LLVM_SUPPORT_IRBUILDER_H
18 #include "llvm/Instructions.h"
19 #include "llvm/BasicBlock.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ConstantFolder.h"
28 /// IRBuilderDefaultInserter - This provides the default implementation of the
29 /// IRBuilder 'InsertHelper' method that is called whenever an instruction is
30 /// created by IRBuilder and needs to be inserted. By default, this inserts the
31 /// instruction at the insertion point.
32 template <bool preserveNames = true>
33 class IRBuilderDefaultInserter {
35 void InsertHelper(Instruction *I, const Twine &Name,
36 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
37 if (BB) BB->getInstList().insert(InsertPt, I);
43 /// IRBuilderBase - Common base class shared among various IRBuilders.
45 DebugLoc CurDbgLocation;
48 BasicBlock::iterator InsertPt;
52 IRBuilderBase(LLVMContext &context)
54 ClearInsertionPoint();
57 //===--------------------------------------------------------------------===//
58 // Builder configuration methods
59 //===--------------------------------------------------------------------===//
61 /// ClearInsertionPoint - Clear the insertion point: created instructions will
62 /// not be inserted into a block.
63 void ClearInsertionPoint() {
67 BasicBlock *GetInsertBlock() const { return BB; }
68 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
69 LLVMContext &getContext() const { return Context; }
71 /// SetInsertPoint - This specifies that created instructions should be
72 /// appended to the end of the specified block.
73 void SetInsertPoint(BasicBlock *TheBB) {
78 /// SetInsertPoint - This specifies that created instructions should be
79 /// inserted before the specified instruction.
80 void SetInsertPoint(Instruction *I) {
83 SetCurrentDebugLocation(I->getDebugLoc());
86 /// SetInsertPoint - This specifies that created instructions should be
87 /// inserted at the specified point.
88 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
93 /// SetInsertPoint(Use) - Find the nearest point that dominates this use, and
94 /// specify that created instructions should be inserted at this point.
95 void SetInsertPoint(Use &U) {
96 Instruction *UseInst = cast<Instruction>(U.getUser());
97 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
98 BasicBlock *PredBB = Phi->getIncomingBlock(U);
99 assert(U != PredBB->getTerminator() && "critical edge not split");
100 SetInsertPoint(PredBB, PredBB->getTerminator());
103 SetInsertPoint(UseInst);
106 /// SetCurrentDebugLocation - Set location information used by debugging
108 void SetCurrentDebugLocation(const DebugLoc &L) {
112 /// getCurrentDebugLocation - Get location information used by debugging
114 DebugLoc getCurrentDebugLocation() const { return CurDbgLocation; }
116 /// SetInstDebugLocation - If this builder has a current debug location, set
117 /// it on the specified instruction.
118 void SetInstDebugLocation(Instruction *I) const {
119 if (!CurDbgLocation.isUnknown())
120 I->setDebugLoc(CurDbgLocation);
123 /// getCurrentFunctionReturnType - Get the return type of the current function
124 /// that we're emitting into.
125 Type *getCurrentFunctionReturnType() const;
127 /// InsertPoint - A saved insertion point.
130 BasicBlock::iterator Point;
133 /// Creates a new insertion point which doesn't point to anything.
134 InsertPoint() : Block(0) {}
136 /// Creates a new insertion point at the given location.
137 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
138 : Block(InsertBlock), Point(InsertPoint) {}
140 /// isSet - Returns true if this insert point is set.
141 bool isSet() const { return (Block != 0); }
143 llvm::BasicBlock *getBlock() const { return Block; }
144 llvm::BasicBlock::iterator getPoint() const { return Point; }
147 /// saveIP - Returns the current insert point.
148 InsertPoint saveIP() const {
149 return InsertPoint(GetInsertBlock(), GetInsertPoint());
152 /// saveAndClearIP - Returns the current insert point, clearing it
154 InsertPoint saveAndClearIP() {
155 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
156 ClearInsertionPoint();
160 /// restoreIP - Sets the current insert point to a previously-saved
162 void restoreIP(InsertPoint IP) {
164 SetInsertPoint(IP.getBlock(), IP.getPoint());
166 ClearInsertionPoint();
169 //===--------------------------------------------------------------------===//
170 // Miscellaneous creation methods.
171 //===--------------------------------------------------------------------===//
173 /// CreateGlobalString - Make a new global variable with an initializer that
174 /// has array of i8 type filled in with the nul terminated string value
175 /// specified. The new global variable will be marked mergable with any
176 /// others of the same contents. If Name is specified, it is the name of the
177 /// global variable created.
178 Value *CreateGlobalString(StringRef Str, const Twine &Name = "");
180 /// getInt1 - Get a constant value representing either true or false.
181 ConstantInt *getInt1(bool V) {
182 return ConstantInt::get(getInt1Ty(), V);
185 /// getTrue - Get the constant value for i1 true.
186 ConstantInt *getTrue() {
187 return ConstantInt::getTrue(Context);
190 /// getFalse - Get the constant value for i1 false.
191 ConstantInt *getFalse() {
192 return ConstantInt::getFalse(Context);
195 /// getInt8 - Get a constant 8-bit value.
196 ConstantInt *getInt8(uint8_t C) {
197 return ConstantInt::get(getInt8Ty(), C);
200 /// getInt16 - Get a constant 16-bit value.
201 ConstantInt *getInt16(uint16_t C) {
202 return ConstantInt::get(getInt16Ty(), C);
205 /// getInt32 - Get a constant 32-bit value.
206 ConstantInt *getInt32(uint32_t C) {
207 return ConstantInt::get(getInt32Ty(), C);
210 /// getInt64 - Get a constant 64-bit value.
211 ConstantInt *getInt64(uint64_t C) {
212 return ConstantInt::get(getInt64Ty(), C);
215 /// getInt - Get a constant integer value.
216 ConstantInt *getInt(const APInt &AI) {
217 return ConstantInt::get(Context, AI);
220 //===--------------------------------------------------------------------===//
221 // Type creation methods
222 //===--------------------------------------------------------------------===//
224 /// getInt1Ty - Fetch the type representing a single bit
225 IntegerType *getInt1Ty() {
226 return Type::getInt1Ty(Context);
229 /// getInt8Ty - Fetch the type representing an 8-bit integer.
230 IntegerType *getInt8Ty() {
231 return Type::getInt8Ty(Context);
234 /// getInt16Ty - Fetch the type representing a 16-bit integer.
235 IntegerType *getInt16Ty() {
236 return Type::getInt16Ty(Context);
239 /// getInt32Ty - Fetch the type resepresenting a 32-bit integer.
240 IntegerType *getInt32Ty() {
241 return Type::getInt32Ty(Context);
244 /// getInt64Ty - Fetch the type representing a 64-bit integer.
245 IntegerType *getInt64Ty() {
246 return Type::getInt64Ty(Context);
249 /// getFloatTy - Fetch the type representing a 32-bit floating point value.
251 return Type::getFloatTy(Context);
254 /// getDoubleTy - Fetch the type representing a 64-bit floating point value.
255 Type *getDoubleTy() {
256 return Type::getDoubleTy(Context);
259 /// getVoidTy - Fetch the type representing void.
261 return Type::getVoidTy(Context);
264 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
265 return Type::getInt8PtrTy(Context, AddrSpace);
268 //===--------------------------------------------------------------------===//
269 // Intrinsic creation methods
270 //===--------------------------------------------------------------------===//
272 /// CreateMemSet - Create and insert a memset to the specified pointer and the
273 /// specified value. If the pointer isn't an i8*, it will be converted. If a
274 /// TBAA tag is specified, it will be added to the instruction.
275 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
276 bool isVolatile = false, MDNode *TBAATag = 0) {
277 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile, TBAATag);
280 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
281 bool isVolatile = false, MDNode *TBAATag = 0);
283 /// CreateMemCpy - Create and insert a memcpy between the specified pointers.
284 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
285 /// specified, it will be added to the instruction.
286 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
287 bool isVolatile = false, MDNode *TBAATag = 0) {
288 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
291 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
292 bool isVolatile = false, MDNode *TBAATag = 0);
294 /// CreateMemMove - Create and insert a memmove between the specified
295 /// pointers. If the pointers aren't i8*, they will be converted. If a TBAA
296 /// tag is specified, it will be added to the instruction.
297 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
298 bool isVolatile = false, MDNode *TBAATag = 0) {
299 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag);
302 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
303 bool isVolatile = false, MDNode *TBAATag = 0);
305 /// CreateLifetimeStart - Create a lifetime.start intrinsic. If the pointer
306 /// isn't i8* it will be converted.
307 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = 0);
309 /// CreateLifetimeEnd - Create a lifetime.end intrinsic. If the pointer isn't
310 /// i8* it will be converted.
311 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = 0);
314 Value *getCastedInt8PtrValue(Value *Ptr);
317 /// IRBuilder - This provides a uniform API for creating instructions and
318 /// inserting them into a basic block: either at the end of a BasicBlock, or
319 /// at a specific iterator location in a block.
321 /// Note that the builder does not expose the full generality of LLVM
322 /// instructions. For access to extra instruction properties, use the mutators
323 /// (e.g. setVolatile) on the instructions after they have been created.
324 /// The first template argument handles whether or not to preserve names in the
325 /// final instruction output. This defaults to on. The second template argument
326 /// specifies a class to use for creating constants. This defaults to creating
327 /// minimally folded constants. The fourth template argument allows clients to
328 /// specify custom insertion hooks that are called on every newly created
330 template<bool preserveNames = true, typename T = ConstantFolder,
331 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
332 class IRBuilder : public IRBuilderBase, public Inserter {
335 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter())
336 : IRBuilderBase(C), Inserter(I), Folder(F) {
339 explicit IRBuilder(LLVMContext &C) : IRBuilderBase(C), Folder() {
342 explicit IRBuilder(BasicBlock *TheBB, const T &F)
343 : IRBuilderBase(TheBB->getContext()), Folder(F) {
344 SetInsertPoint(TheBB);
347 explicit IRBuilder(BasicBlock *TheBB)
348 : IRBuilderBase(TheBB->getContext()), Folder() {
349 SetInsertPoint(TheBB);
352 explicit IRBuilder(Instruction *IP)
353 : IRBuilderBase(IP->getContext()), Folder() {
355 SetCurrentDebugLocation(IP->getDebugLoc());
358 explicit IRBuilder(Use &U)
359 : IRBuilderBase(U->getContext()), Folder() {
361 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
364 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F)
365 : IRBuilderBase(TheBB->getContext()), Folder(F) {
366 SetInsertPoint(TheBB, IP);
369 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP)
370 : IRBuilderBase(TheBB->getContext()), Folder() {
371 SetInsertPoint(TheBB, IP);
374 /// getFolder - Get the constant folder being used.
375 const T &getFolder() { return Folder; }
377 /// isNamePreserving - Return true if this builder is configured to actually
378 /// add the requested names to IR created through it.
379 bool isNamePreserving() const { return preserveNames; }
381 /// Insert - Insert and return the specified instruction.
382 template<typename InstTy>
383 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
384 this->InsertHelper(I, Name, BB, InsertPt);
385 if (!getCurrentDebugLocation().isUnknown())
386 this->SetInstDebugLocation(I);
390 /// Insert - No-op overload to handle constants.
391 Constant *Insert(Constant *C, const Twine& = "") const {
395 //===--------------------------------------------------------------------===//
396 // Instruction creation methods: Terminators
397 //===--------------------------------------------------------------------===//
399 /// CreateRetVoid - Create a 'ret void' instruction.
400 ReturnInst *CreateRetVoid() {
401 return Insert(ReturnInst::Create(Context));
405 /// CreateRet - Create a 'ret <val>' instruction.
407 ReturnInst *CreateRet(Value *V) {
408 return Insert(ReturnInst::Create(Context, V));
411 /// CreateAggregateRet - Create a sequence of N insertvalue instructions,
412 /// with one Value from the retVals array each, that build a aggregate
413 /// return value one value at a time, and a ret instruction to return
414 /// the resulting aggregate value. This is a convenience function for
415 /// code that uses aggregate return values as a vehicle for having
416 /// multiple return values.
418 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
419 Value *V = UndefValue::get(getCurrentFunctionReturnType());
420 for (unsigned i = 0; i != N; ++i)
421 V = CreateInsertValue(V, retVals[i], i, "mrv");
422 return Insert(ReturnInst::Create(Context, V));
425 /// CreateBr - Create an unconditional 'br label X' instruction.
426 BranchInst *CreateBr(BasicBlock *Dest) {
427 return Insert(BranchInst::Create(Dest));
430 /// CreateCondBr - Create a conditional 'br Cond, TrueDest, FalseDest'
432 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False) {
433 return Insert(BranchInst::Create(True, False, Cond));
436 /// CreateSwitch - Create a switch instruction with the specified value,
437 /// default dest, and with a hint for the number of cases that will be added
438 /// (for efficient allocation).
439 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10) {
440 return Insert(SwitchInst::Create(V, Dest, NumCases));
443 /// CreateIndirectBr - Create an indirect branch instruction with the
444 /// specified address operand, with an optional hint for the number of
445 /// destinations that will be added (for efficient allocation).
446 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
447 return Insert(IndirectBrInst::Create(Addr, NumDests));
450 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
451 BasicBlock *UnwindDest, const Twine &Name = "") {
452 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest,
453 ArrayRef<Value *>()),
456 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
457 BasicBlock *UnwindDest, Value *Arg1,
458 const Twine &Name = "") {
459 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
462 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
463 BasicBlock *UnwindDest, Value *Arg1,
464 Value *Arg2, Value *Arg3,
465 const Twine &Name = "") {
466 Value *Args[] = { Arg1, Arg2, Arg3 };
467 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
470 /// CreateInvoke - Create an invoke instruction.
471 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
472 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
473 const Twine &Name = "") {
474 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
478 UnwindInst *CreateUnwind() {
479 return Insert(new UnwindInst(Context));
482 ResumeInst *CreateResume(Value *Exn) {
483 return Insert(ResumeInst::Create(Exn));
486 UnreachableInst *CreateUnreachable() {
487 return Insert(new UnreachableInst(Context));
490 //===--------------------------------------------------------------------===//
491 // Instruction creation methods: Binary Operators
492 //===--------------------------------------------------------------------===//
494 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
495 Value *LHS, Value *RHS,
497 bool HasNUW, bool HasNSW) {
498 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
499 if (HasNUW) BO->setHasNoUnsignedWrap();
500 if (HasNSW) BO->setHasNoSignedWrap();
504 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
505 bool HasNUW = false, bool HasNSW = false) {
506 if (Constant *LC = dyn_cast<Constant>(LHS))
507 if (Constant *RC = dyn_cast<Constant>(RHS))
508 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
509 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
512 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
513 return CreateAdd(LHS, RHS, Name, false, true);
515 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
516 return CreateAdd(LHS, RHS, Name, true, false);
518 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
519 if (Constant *LC = dyn_cast<Constant>(LHS))
520 if (Constant *RC = dyn_cast<Constant>(RHS))
521 return Insert(Folder.CreateFAdd(LC, RC), Name);
522 return Insert(BinaryOperator::CreateFAdd(LHS, RHS), Name);
524 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
525 bool HasNUW = false, bool HasNSW = false) {
526 if (Constant *LC = dyn_cast<Constant>(LHS))
527 if (Constant *RC = dyn_cast<Constant>(RHS))
528 return Insert(Folder.CreateSub(LC, RC), Name);
529 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
532 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
533 return CreateSub(LHS, RHS, Name, false, true);
535 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
536 return CreateSub(LHS, RHS, Name, true, false);
538 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "") {
539 if (Constant *LC = dyn_cast<Constant>(LHS))
540 if (Constant *RC = dyn_cast<Constant>(RHS))
541 return Insert(Folder.CreateFSub(LC, RC), Name);
542 return Insert(BinaryOperator::CreateFSub(LHS, RHS), Name);
544 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
545 bool HasNUW = false, bool HasNSW = false) {
546 if (Constant *LC = dyn_cast<Constant>(LHS))
547 if (Constant *RC = dyn_cast<Constant>(RHS))
548 return Insert(Folder.CreateMul(LC, RC), Name);
549 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
552 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
553 return CreateMul(LHS, RHS, Name, false, true);
555 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
556 return CreateMul(LHS, RHS, Name, true, false);
558 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "") {
559 if (Constant *LC = dyn_cast<Constant>(LHS))
560 if (Constant *RC = dyn_cast<Constant>(RHS))
561 return Insert(Folder.CreateFMul(LC, RC), Name);
562 return Insert(BinaryOperator::CreateFMul(LHS, RHS), Name);
564 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
565 bool isExact = false) {
566 if (Constant *LC = dyn_cast<Constant>(LHS))
567 if (Constant *RC = dyn_cast<Constant>(RHS))
568 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
570 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
571 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
573 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
574 return CreateUDiv(LHS, RHS, Name, true);
576 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
577 bool isExact = false) {
578 if (Constant *LC = dyn_cast<Constant>(LHS))
579 if (Constant *RC = dyn_cast<Constant>(RHS))
580 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
582 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
583 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
585 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
586 return CreateSDiv(LHS, RHS, Name, true);
588 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
589 if (Constant *LC = dyn_cast<Constant>(LHS))
590 if (Constant *RC = dyn_cast<Constant>(RHS))
591 return Insert(Folder.CreateFDiv(LC, RC), Name);
592 return Insert(BinaryOperator::CreateFDiv(LHS, RHS), Name);
594 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
595 if (Constant *LC = dyn_cast<Constant>(LHS))
596 if (Constant *RC = dyn_cast<Constant>(RHS))
597 return Insert(Folder.CreateURem(LC, RC), Name);
598 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
600 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
601 if (Constant *LC = dyn_cast<Constant>(LHS))
602 if (Constant *RC = dyn_cast<Constant>(RHS))
603 return Insert(Folder.CreateSRem(LC, RC), Name);
604 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
606 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "") {
607 if (Constant *LC = dyn_cast<Constant>(LHS))
608 if (Constant *RC = dyn_cast<Constant>(RHS))
609 return Insert(Folder.CreateFRem(LC, RC), Name);
610 return Insert(BinaryOperator::CreateFRem(LHS, RHS), Name);
613 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
614 bool HasNUW = false, bool HasNSW = false) {
615 if (Constant *LC = dyn_cast<Constant>(LHS))
616 if (Constant *RC = dyn_cast<Constant>(RHS))
617 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
618 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
621 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
622 bool HasNUW = false, bool HasNSW = false) {
623 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
626 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
627 bool HasNUW = false, bool HasNSW = false) {
628 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
632 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
633 bool isExact = false) {
634 if (Constant *LC = dyn_cast<Constant>(LHS))
635 if (Constant *RC = dyn_cast<Constant>(RHS))
636 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
638 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
639 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
641 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
642 bool isExact = false) {
643 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
645 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
646 bool isExact = false) {
647 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
650 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
651 bool isExact = false) {
652 if (Constant *LC = dyn_cast<Constant>(LHS))
653 if (Constant *RC = dyn_cast<Constant>(RHS))
654 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
656 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
657 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
659 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
660 bool isExact = false) {
661 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
663 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
664 bool isExact = false) {
665 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
668 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
669 if (Constant *RC = dyn_cast<Constant>(RHS)) {
670 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
671 return LHS; // LHS & -1 -> LHS
672 if (Constant *LC = dyn_cast<Constant>(LHS))
673 return Insert(Folder.CreateAnd(LC, RC), Name);
675 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
677 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
678 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
680 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
681 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
684 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
685 if (Constant *RC = dyn_cast<Constant>(RHS)) {
686 if (RC->isNullValue())
687 return LHS; // LHS | 0 -> LHS
688 if (Constant *LC = dyn_cast<Constant>(LHS))
689 return Insert(Folder.CreateOr(LC, RC), Name);
691 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
693 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
694 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
696 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
697 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
700 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
701 if (Constant *LC = dyn_cast<Constant>(LHS))
702 if (Constant *RC = dyn_cast<Constant>(RHS))
703 return Insert(Folder.CreateXor(LC, RC), Name);
704 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
706 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
707 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
709 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
710 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
713 Value *CreateBinOp(Instruction::BinaryOps Opc,
714 Value *LHS, Value *RHS, const Twine &Name = "") {
715 if (Constant *LC = dyn_cast<Constant>(LHS))
716 if (Constant *RC = dyn_cast<Constant>(RHS))
717 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
718 return Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
721 Value *CreateNeg(Value *V, const Twine &Name = "",
722 bool HasNUW = false, bool HasNSW = false) {
723 if (Constant *VC = dyn_cast<Constant>(V))
724 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
725 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
726 if (HasNUW) BO->setHasNoUnsignedWrap();
727 if (HasNSW) BO->setHasNoSignedWrap();
730 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
731 return CreateNeg(V, Name, false, true);
733 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
734 return CreateNeg(V, Name, true, false);
736 Value *CreateFNeg(Value *V, const Twine &Name = "") {
737 if (Constant *VC = dyn_cast<Constant>(V))
738 return Insert(Folder.CreateFNeg(VC), Name);
739 return Insert(BinaryOperator::CreateFNeg(V), Name);
741 Value *CreateNot(Value *V, const Twine &Name = "") {
742 if (Constant *VC = dyn_cast<Constant>(V))
743 return Insert(Folder.CreateNot(VC), Name);
744 return Insert(BinaryOperator::CreateNot(V), Name);
747 //===--------------------------------------------------------------------===//
748 // Instruction creation methods: Memory Instructions
749 //===--------------------------------------------------------------------===//
751 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = 0,
752 const Twine &Name = "") {
753 return Insert(new AllocaInst(Ty, ArraySize), Name);
755 // Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
756 // converting the string to 'bool' for the isVolatile parameter.
757 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
758 return Insert(new LoadInst(Ptr), Name);
760 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
761 return Insert(new LoadInst(Ptr), Name);
763 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
764 return Insert(new LoadInst(Ptr, 0, isVolatile), Name);
766 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
767 return Insert(new StoreInst(Val, Ptr, isVolatile));
769 FenceInst *CreateFence(AtomicOrdering Ordering,
770 SynchronizationScope SynchScope = CrossThread) {
771 return Insert(new FenceInst(Context, Ordering, SynchScope));
773 AtomicCmpXchgInst *CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
774 AtomicOrdering Ordering,
775 SynchronizationScope SynchScope = CrossThread) {
776 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, Ordering, SynchScope));
778 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
779 AtomicOrdering Ordering,
780 SynchronizationScope SynchScope = CrossThread) {
781 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
783 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
784 const Twine &Name = "") {
785 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
786 // Every index must be constant.
788 for (i = 0, e = IdxList.size(); i != e; ++i)
789 if (!isa<Constant>(IdxList[i]))
792 return Insert(Folder.CreateGetElementPtr(PC, IdxList), Name);
794 return Insert(GetElementPtrInst::Create(Ptr, IdxList), Name);
796 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
797 const Twine &Name = "") {
798 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
799 // Every index must be constant.
801 for (i = 0, e = IdxList.size(); i != e; ++i)
802 if (!isa<Constant>(IdxList[i]))
805 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IdxList), Name);
807 return Insert(GetElementPtrInst::CreateInBounds(Ptr, IdxList), Name);
809 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
810 if (Constant *PC = dyn_cast<Constant>(Ptr))
811 if (Constant *IC = dyn_cast<Constant>(Idx))
812 return Insert(Folder.CreateGetElementPtr(PC, IC), Name);
813 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
815 Value *CreateInBoundsGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
816 if (Constant *PC = dyn_cast<Constant>(Ptr))
817 if (Constant *IC = dyn_cast<Constant>(Idx))
818 return Insert(Folder.CreateInBoundsGetElementPtr(PC, IC), Name);
819 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
821 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
822 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
824 if (Constant *PC = dyn_cast<Constant>(Ptr))
825 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
827 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
829 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
830 const Twine &Name = "") {
831 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
833 if (Constant *PC = dyn_cast<Constant>(Ptr))
834 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
836 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
838 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
839 const Twine &Name = "") {
841 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
842 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
845 if (Constant *PC = dyn_cast<Constant>(Ptr))
846 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
848 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
850 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
851 const Twine &Name = "") {
853 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
854 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
857 if (Constant *PC = dyn_cast<Constant>(Ptr))
858 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
860 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
862 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
863 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
865 if (Constant *PC = dyn_cast<Constant>(Ptr))
866 return Insert(Folder.CreateGetElementPtr(PC, Idx), Name);
868 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
870 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
871 const Twine &Name = "") {
872 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
874 if (Constant *PC = dyn_cast<Constant>(Ptr))
875 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idx), Name);
877 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idx), Name);
879 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
880 const Twine &Name = "") {
882 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
883 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
886 if (Constant *PC = dyn_cast<Constant>(Ptr))
887 return Insert(Folder.CreateGetElementPtr(PC, Idxs), Name);
889 return Insert(GetElementPtrInst::Create(Ptr, Idxs), Name);
891 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
892 const Twine &Name = "") {
894 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
895 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
898 if (Constant *PC = dyn_cast<Constant>(Ptr))
899 return Insert(Folder.CreateInBoundsGetElementPtr(PC, Idxs), Name);
901 return Insert(GetElementPtrInst::CreateInBounds(Ptr, Idxs), Name);
903 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
904 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
907 /// CreateGlobalStringPtr - Same as CreateGlobalString, but return a pointer
908 /// with "i8*" type instead of a pointer to array of i8.
909 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
910 Value *gv = CreateGlobalString(Str, Name);
911 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
912 Value *Args[] = { zero, zero };
913 return CreateInBoundsGEP(gv, Args, Name);
916 //===--------------------------------------------------------------------===//
917 // Instruction creation methods: Cast/Conversion Operators
918 //===--------------------------------------------------------------------===//
920 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
921 return CreateCast(Instruction::Trunc, V, DestTy, Name);
923 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
924 return CreateCast(Instruction::ZExt, V, DestTy, Name);
926 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
927 return CreateCast(Instruction::SExt, V, DestTy, Name);
929 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
930 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
932 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
933 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
935 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
936 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
938 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
939 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
941 Value *CreateFPTrunc(Value *V, Type *DestTy,
942 const Twine &Name = "") {
943 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
945 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
946 return CreateCast(Instruction::FPExt, V, DestTy, Name);
948 Value *CreatePtrToInt(Value *V, Type *DestTy,
949 const Twine &Name = "") {
950 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
952 Value *CreateIntToPtr(Value *V, Type *DestTy,
953 const Twine &Name = "") {
954 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
956 Value *CreateBitCast(Value *V, Type *DestTy,
957 const Twine &Name = "") {
958 return CreateCast(Instruction::BitCast, V, DestTy, Name);
960 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
961 const Twine &Name = "") {
962 if (V->getType() == DestTy)
964 if (Constant *VC = dyn_cast<Constant>(V))
965 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
966 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
968 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
969 const Twine &Name = "") {
970 if (V->getType() == DestTy)
972 if (Constant *VC = dyn_cast<Constant>(V))
973 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
974 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
976 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
977 const Twine &Name = "") {
978 if (V->getType() == DestTy)
980 if (Constant *VC = dyn_cast<Constant>(V))
981 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
982 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
984 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
985 const Twine &Name = "") {
986 if (V->getType() == DestTy)
988 if (Constant *VC = dyn_cast<Constant>(V))
989 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
990 return Insert(CastInst::Create(Op, V, DestTy), Name);
992 Value *CreatePointerCast(Value *V, Type *DestTy,
993 const Twine &Name = "") {
994 if (V->getType() == DestTy)
996 if (Constant *VC = dyn_cast<Constant>(V))
997 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
998 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1000 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1001 const Twine &Name = "") {
1002 if (V->getType() == DestTy)
1004 if (Constant *VC = dyn_cast<Constant>(V))
1005 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1006 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1009 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a compile time
1010 // error, instead of converting the string to bool for the isSigned parameter.
1011 Value *CreateIntCast(Value *, Type *, const char *); // DO NOT IMPLEMENT
1013 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1014 if (V->getType() == DestTy)
1016 if (Constant *VC = dyn_cast<Constant>(V))
1017 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1018 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1021 //===--------------------------------------------------------------------===//
1022 // Instruction creation methods: Compare Instructions
1023 //===--------------------------------------------------------------------===//
1025 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1026 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1028 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1029 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1031 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1032 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1034 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1035 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1037 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1038 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1040 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1041 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1043 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1044 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1046 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1047 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1049 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1050 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1052 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1053 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1056 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1057 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1059 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1060 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1062 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1063 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1065 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1066 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1068 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1069 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1071 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1072 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1074 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1075 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1077 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1078 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1080 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1081 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1083 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1084 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1086 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1087 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1089 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1090 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1092 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1093 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1095 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1096 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1099 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1100 const Twine &Name = "") {
1101 if (Constant *LC = dyn_cast<Constant>(LHS))
1102 if (Constant *RC = dyn_cast<Constant>(RHS))
1103 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1104 return Insert(new ICmpInst(P, LHS, RHS), Name);
1106 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1107 const Twine &Name = "") {
1108 if (Constant *LC = dyn_cast<Constant>(LHS))
1109 if (Constant *RC = dyn_cast<Constant>(RHS))
1110 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1111 return Insert(new FCmpInst(P, LHS, RHS), Name);
1114 //===--------------------------------------------------------------------===//
1115 // Instruction creation methods: Other Instructions
1116 //===--------------------------------------------------------------------===//
1118 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1119 const Twine &Name = "") {
1120 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1123 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1124 return Insert(CallInst::Create(Callee), Name);
1126 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1127 return Insert(CallInst::Create(Callee, Arg), Name);
1129 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1130 const Twine &Name = "") {
1131 Value *Args[] = { Arg1, Arg2 };
1132 return Insert(CallInst::Create(Callee, Args), Name);
1134 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1135 const Twine &Name = "") {
1136 Value *Args[] = { Arg1, Arg2, Arg3 };
1137 return Insert(CallInst::Create(Callee, Args), Name);
1139 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1140 Value *Arg4, const Twine &Name = "") {
1141 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1142 return Insert(CallInst::Create(Callee, Args), Name);
1144 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1145 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1146 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1147 return Insert(CallInst::Create(Callee, Args), Name);
1150 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1151 const Twine &Name = "") {
1152 return Insert(CallInst::Create(Callee, Args, Name));
1155 Value *CreateSelect(Value *C, Value *True, Value *False,
1156 const Twine &Name = "") {
1157 if (Constant *CC = dyn_cast<Constant>(C))
1158 if (Constant *TC = dyn_cast<Constant>(True))
1159 if (Constant *FC = dyn_cast<Constant>(False))
1160 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1161 return Insert(SelectInst::Create(C, True, False), Name);
1164 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1165 return Insert(new VAArgInst(List, Ty), Name);
1168 Value *CreateExtractElement(Value *Vec, Value *Idx,
1169 const Twine &Name = "") {
1170 if (Constant *VC = dyn_cast<Constant>(Vec))
1171 if (Constant *IC = dyn_cast<Constant>(Idx))
1172 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1173 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1176 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1177 const Twine &Name = "") {
1178 if (Constant *VC = dyn_cast<Constant>(Vec))
1179 if (Constant *NC = dyn_cast<Constant>(NewElt))
1180 if (Constant *IC = dyn_cast<Constant>(Idx))
1181 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1182 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1185 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1186 const Twine &Name = "") {
1187 if (Constant *V1C = dyn_cast<Constant>(V1))
1188 if (Constant *V2C = dyn_cast<Constant>(V2))
1189 if (Constant *MC = dyn_cast<Constant>(Mask))
1190 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1191 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1194 Value *CreateExtractValue(Value *Agg,
1195 ArrayRef<unsigned> Idxs,
1196 const Twine &Name = "") {
1197 if (Constant *AggC = dyn_cast<Constant>(Agg))
1198 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1199 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1202 Value *CreateInsertValue(Value *Agg, Value *Val,
1203 ArrayRef<unsigned> Idxs,
1204 const Twine &Name = "") {
1205 if (Constant *AggC = dyn_cast<Constant>(Agg))
1206 if (Constant *ValC = dyn_cast<Constant>(Val))
1207 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1208 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1211 //===--------------------------------------------------------------------===//
1212 // Utility creation methods
1213 //===--------------------------------------------------------------------===//
1215 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
1216 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1217 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1221 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
1222 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1223 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1227 /// CreatePtrDiff - Return the i64 difference between two pointer values,
1228 /// dividing out the size of the pointed-to objects. This is intended to
1229 /// implement C-style pointer subtraction. As such, the pointers must be
1230 /// appropriately aligned for their element types and pointing into the
1232 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1233 assert(LHS->getType() == RHS->getType() &&
1234 "Pointer subtraction operand types must match!");
1235 PointerType *ArgType = cast<PointerType>(LHS->getType());
1236 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1237 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1238 Value *Difference = CreateSub(LHS_int, RHS_int);
1239 return CreateExactSDiv(Difference,
1240 ConstantExpr::getSizeOf(ArgType->getElementType()),