1 //===---- llvm/IRBuilder.h - Builder for LLVM Instructions ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the IRBuilder class, which is used as a convenient way
11 // to create LLVM instructions with a consistent and simplified interface.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_IR_IRBUILDER_H
16 #define LLVM_IR_IRBUILDER_H
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/ConstantFolder.h"
23 #include "llvm/IR/DataLayout.h"
24 #include "llvm/IR/GlobalVariable.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Operator.h"
28 #include "llvm/IR/ValueHandle.h"
29 #include "llvm/Support/CBindingWrapping.h"
34 /// \brief This provides the default implementation of the IRBuilder
35 /// 'InsertHelper' method that is called whenever an instruction is created by
36 /// IRBuilder and needs to be inserted.
38 /// By default, this inserts the instruction at the insertion point.
39 template <bool preserveNames = true>
40 class IRBuilderDefaultInserter {
42 void InsertHelper(Instruction *I, const Twine &Name,
43 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
44 if (BB) BB->getInstList().insert(InsertPt, I);
50 /// \brief Common base class shared among various IRBuilders.
52 DebugLoc CurDbgLocation;
55 BasicBlock::iterator InsertPt;
58 MDNode *DefaultFPMathTag;
62 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr)
63 : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
64 ClearInsertionPoint();
67 //===--------------------------------------------------------------------===//
68 // Builder configuration methods
69 //===--------------------------------------------------------------------===//
71 /// \brief Clear the insertion point: created instructions will not be
72 /// inserted into a block.
73 void ClearInsertionPoint() {
78 BasicBlock *GetInsertBlock() const { return BB; }
79 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
80 LLVMContext &getContext() const { return Context; }
82 /// \brief This specifies that created instructions should be appended to the
83 /// end of the specified block.
84 void SetInsertPoint(BasicBlock *TheBB) {
89 /// \brief This specifies that created instructions should be inserted before
90 /// the specified instruction.
91 void SetInsertPoint(Instruction *I) {
94 assert(I != BB->end() && "Can't read debug loc from end()");
95 SetCurrentDebugLocation(I->getDebugLoc());
98 /// \brief This specifies that created instructions should be inserted at the
100 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
105 /// \brief Find the nearest point that dominates this use, and specify that
106 /// created instructions should be inserted at this point.
107 void SetInsertPoint(Use &U) {
108 Instruction *UseInst = cast<Instruction>(U.getUser());
109 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
110 BasicBlock *PredBB = Phi->getIncomingBlock(U);
111 assert(U != PredBB->getTerminator() && "critical edge not split");
112 SetInsertPoint(PredBB, PredBB->getTerminator());
115 SetInsertPoint(UseInst);
118 /// \brief Set location information used by debugging information.
119 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
121 /// \brief Get location information used by debugging information.
122 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
124 /// \brief If this builder has a current debug location, set it on the
125 /// specified instruction.
126 void SetInstDebugLocation(Instruction *I) const {
128 I->setDebugLoc(CurDbgLocation);
131 /// \brief Get the return type of the current function that we're emitting
133 Type *getCurrentFunctionReturnType() const;
135 /// InsertPoint - A saved insertion point.
138 BasicBlock::iterator Point;
141 /// \brief Creates a new insertion point which doesn't point to anything.
142 InsertPoint() : Block(nullptr) {}
144 /// \brief Creates a new insertion point at the given location.
145 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
146 : Block(InsertBlock), Point(InsertPoint) {}
148 /// \brief Returns true if this insert point is set.
149 bool isSet() const { return (Block != nullptr); }
151 llvm::BasicBlock *getBlock() const { return Block; }
152 llvm::BasicBlock::iterator getPoint() const { return Point; }
155 /// \brief Returns the current insert point.
156 InsertPoint saveIP() const {
157 return InsertPoint(GetInsertBlock(), GetInsertPoint());
160 /// \brief Returns the current insert point, clearing it in the process.
161 InsertPoint saveAndClearIP() {
162 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
163 ClearInsertionPoint();
167 /// \brief Sets the current insert point to a previously-saved location.
168 void restoreIP(InsertPoint IP) {
170 SetInsertPoint(IP.getBlock(), IP.getPoint());
172 ClearInsertionPoint();
175 /// \brief Get the floating point math metadata being used.
176 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
178 /// \brief Get the flags to be applied to created floating point ops
179 FastMathFlags getFastMathFlags() const { return FMF; }
181 /// \brief Clear the fast-math flags.
182 void clearFastMathFlags() { FMF.clear(); }
184 /// \brief Set the floating point math metadata to be used.
185 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
187 /// \brief Set the fast-math flags to be used with generated fp-math operators
188 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
190 //===--------------------------------------------------------------------===//
192 //===--------------------------------------------------------------------===//
194 // \brief RAII object that stores the current insertion point and restores it
195 // when the object is destroyed. This includes the debug location.
196 class InsertPointGuard {
197 IRBuilderBase &Builder;
198 AssertingVH<BasicBlock> Block;
199 BasicBlock::iterator Point;
202 InsertPointGuard(const InsertPointGuard &) = delete;
203 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
206 InsertPointGuard(IRBuilderBase &B)
207 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
208 DbgLoc(B.getCurrentDebugLocation()) {}
210 ~InsertPointGuard() {
211 Builder.restoreIP(InsertPoint(Block, Point));
212 Builder.SetCurrentDebugLocation(DbgLoc);
216 // \brief RAII object that stores the current fast math settings and restores
217 // them when the object is destroyed.
218 class FastMathFlagGuard {
219 IRBuilderBase &Builder;
223 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
224 FastMathFlagGuard &operator=(
225 const FastMathFlagGuard &) = delete;
228 FastMathFlagGuard(IRBuilderBase &B)
229 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
231 ~FastMathFlagGuard() {
233 Builder.DefaultFPMathTag = FPMathTag;
237 //===--------------------------------------------------------------------===//
238 // Miscellaneous creation methods.
239 //===--------------------------------------------------------------------===//
241 /// \brief Make a new global variable with initializer type i8*
243 /// Make a new global variable with an initializer that has array of i8 type
244 /// filled in with the null terminated string value specified. The new global
245 /// variable will be marked mergable with any others of the same contents. If
246 /// Name is specified, it is the name of the global variable created.
247 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "");
249 /// \brief Get a constant value representing either true or false.
250 ConstantInt *getInt1(bool V) {
251 return ConstantInt::get(getInt1Ty(), V);
254 /// \brief Get the constant value for i1 true.
255 ConstantInt *getTrue() {
256 return ConstantInt::getTrue(Context);
259 /// \brief Get the constant value for i1 false.
260 ConstantInt *getFalse() {
261 return ConstantInt::getFalse(Context);
264 /// \brief Get a constant 8-bit value.
265 ConstantInt *getInt8(uint8_t C) {
266 return ConstantInt::get(getInt8Ty(), C);
269 /// \brief Get a constant 16-bit value.
270 ConstantInt *getInt16(uint16_t C) {
271 return ConstantInt::get(getInt16Ty(), C);
274 /// \brief Get a constant 32-bit value.
275 ConstantInt *getInt32(uint32_t C) {
276 return ConstantInt::get(getInt32Ty(), C);
279 /// \brief Get a constant 64-bit value.
280 ConstantInt *getInt64(uint64_t C) {
281 return ConstantInt::get(getInt64Ty(), C);
284 /// \brief Get a constant N-bit value, zero extended or truncated from
286 ConstantInt *getIntN(unsigned N, uint64_t C) {
287 return ConstantInt::get(getIntNTy(N), C);
290 /// \brief Get a constant integer value.
291 ConstantInt *getInt(const APInt &AI) {
292 return ConstantInt::get(Context, AI);
295 //===--------------------------------------------------------------------===//
296 // Type creation methods
297 //===--------------------------------------------------------------------===//
299 /// \brief Fetch the type representing a single bit
300 IntegerType *getInt1Ty() {
301 return Type::getInt1Ty(Context);
304 /// \brief Fetch the type representing an 8-bit integer.
305 IntegerType *getInt8Ty() {
306 return Type::getInt8Ty(Context);
309 /// \brief Fetch the type representing a 16-bit integer.
310 IntegerType *getInt16Ty() {
311 return Type::getInt16Ty(Context);
314 /// \brief Fetch the type representing a 32-bit integer.
315 IntegerType *getInt32Ty() {
316 return Type::getInt32Ty(Context);
319 /// \brief Fetch the type representing a 64-bit integer.
320 IntegerType *getInt64Ty() {
321 return Type::getInt64Ty(Context);
324 /// \brief Fetch the type representing an N-bit integer.
325 IntegerType *getIntNTy(unsigned N) {
326 return Type::getIntNTy(Context, N);
329 /// \brief Fetch the type representing a 16-bit floating point value.
331 return Type::getHalfTy(Context);
334 /// \brief Fetch the type representing a 32-bit floating point value.
336 return Type::getFloatTy(Context);
339 /// \brief Fetch the type representing a 64-bit floating point value.
340 Type *getDoubleTy() {
341 return Type::getDoubleTy(Context);
344 /// \brief Fetch the type representing void.
346 return Type::getVoidTy(Context);
349 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
350 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
351 return Type::getInt8PtrTy(Context, AddrSpace);
354 /// \brief Fetch the type representing a pointer to an integer value.
355 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
356 return DL.getIntPtrType(Context, AddrSpace);
359 //===--------------------------------------------------------------------===//
360 // Intrinsic creation methods
361 //===--------------------------------------------------------------------===//
363 /// \brief Create and insert a memset to the specified pointer and the
366 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
367 /// specified, it will be added to the instruction. Likewise with alias.scope
368 /// and noalias tags.
369 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
370 bool isVolatile = false, MDNode *TBAATag = nullptr,
371 MDNode *ScopeTag = nullptr,
372 MDNode *NoAliasTag = nullptr) {
373 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
374 TBAATag, ScopeTag, NoAliasTag);
377 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
378 bool isVolatile = false, MDNode *TBAATag = nullptr,
379 MDNode *ScopeTag = nullptr,
380 MDNode *NoAliasTag = nullptr);
382 /// \brief Create and insert a memcpy between the specified pointers.
384 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
385 /// specified, it will be added to the instruction. Likewise with alias.scope
386 /// and noalias tags.
387 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
388 bool isVolatile = false, MDNode *TBAATag = nullptr,
389 MDNode *TBAAStructTag = nullptr,
390 MDNode *ScopeTag = nullptr,
391 MDNode *NoAliasTag = nullptr) {
392 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
393 TBAAStructTag, ScopeTag, NoAliasTag);
396 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
397 bool isVolatile = false, MDNode *TBAATag = nullptr,
398 MDNode *TBAAStructTag = nullptr,
399 MDNode *ScopeTag = nullptr,
400 MDNode *NoAliasTag = nullptr);
402 /// \brief Create and insert a memmove between the specified
405 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
406 /// specified, it will be added to the instruction. Likewise with alias.scope
407 /// and noalias tags.
408 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
409 bool isVolatile = false, MDNode *TBAATag = nullptr,
410 MDNode *ScopeTag = nullptr,
411 MDNode *NoAliasTag = nullptr) {
412 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
413 TBAATag, ScopeTag, NoAliasTag);
416 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
417 bool isVolatile = false, MDNode *TBAATag = nullptr,
418 MDNode *ScopeTag = nullptr,
419 MDNode *NoAliasTag = nullptr);
421 /// \brief Create a lifetime.start intrinsic.
423 /// If the pointer isn't i8* it will be converted.
424 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
426 /// \brief Create a lifetime.end intrinsic.
428 /// If the pointer isn't i8* it will be converted.
429 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
431 /// \brief Create a call to Masked Load intrinsic
432 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
433 Value *PassThru = 0, const Twine &Name = "");
435 /// \brief Create a call to Masked Store intrinsic
436 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
439 /// \brief Create an assume intrinsic call that allows the optimizer to
440 /// assume that the provided condition will be true.
441 CallInst *CreateAssumption(Value *Cond);
443 /// \brief Create a call to the experimental.gc.statepoint intrinsic to
444 /// start a new statepoint sequence.
445 CallInst *CreateGCStatepoint(Value *ActualCallee,
446 ArrayRef<Value *> CallArgs,
447 ArrayRef<Value *> DeoptArgs,
448 ArrayRef<Value *> GCArgs,
449 const Twine &Name = "");
451 // Conveninence function for the common case when CallArgs are filled in using
452 // makeArrayRef(CS.arg_begin(), .arg_end()); Use needs to be .get()'ed to get
454 CallInst *CreateGCStatepoint(Value *ActualCallee, ArrayRef<Use> CallArgs,
455 ArrayRef<Value *> DeoptArgs,
456 ArrayRef<Value *> GCArgs,
457 const Twine &Name = "");
459 /// \brief Create a call to the experimental.gc.result intrinsic to extract
460 /// the result from a call wrapped in a statepoint.
461 CallInst *CreateGCResult(Instruction *Statepoint,
463 const Twine &Name = "");
465 /// \brief Create a call to the experimental.gc.relocate intrinsics to
466 /// project the relocated value of one pointer from the statepoint.
467 CallInst *CreateGCRelocate(Instruction *Statepoint,
471 const Twine &Name = "");
474 /// \brief Create a call to a masked intrinsic with given Id.
475 /// Masked intrinsic has only one overloaded type - data type.
476 CallInst *CreateMaskedIntrinsic(unsigned Id, ArrayRef<Value *> Ops,
477 Type *DataTy, const Twine &Name = "");
479 Value *getCastedInt8PtrValue(Value *Ptr);
482 /// \brief This provides a uniform API for creating instructions and inserting
483 /// them into a basic block: either at the end of a BasicBlock, or at a specific
484 /// iterator location in a block.
486 /// Note that the builder does not expose the full generality of LLVM
487 /// instructions. For access to extra instruction properties, use the mutators
488 /// (e.g. setVolatile) on the instructions after they have been
489 /// created. Convenience state exists to specify fast-math flags and fp-math
492 /// The first template argument handles whether or not to preserve names in the
493 /// final instruction output. This defaults to on. The second template argument
494 /// specifies a class to use for creating constants. This defaults to creating
495 /// minimally folded constants. The third template argument allows clients to
496 /// specify custom insertion hooks that are called on every newly created
498 template<bool preserveNames = true, typename T = ConstantFolder,
499 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
500 class IRBuilder : public IRBuilderBase, public Inserter {
503 IRBuilder(LLVMContext &C, const T &F, const Inserter &I = Inserter(),
504 MDNode *FPMathTag = nullptr)
505 : IRBuilderBase(C, FPMathTag), Inserter(I), Folder(F) {
508 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
509 : IRBuilderBase(C, FPMathTag), Folder() {
512 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
513 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
514 SetInsertPoint(TheBB);
517 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
518 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
519 SetInsertPoint(TheBB);
522 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
523 : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
525 SetCurrentDebugLocation(IP->getDebugLoc());
528 explicit IRBuilder(Use &U, MDNode *FPMathTag = nullptr)
529 : IRBuilderBase(U->getContext(), FPMathTag), Folder() {
531 SetCurrentDebugLocation(cast<Instruction>(U.getUser())->getDebugLoc());
534 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
535 MDNode *FPMathTag = nullptr)
536 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
537 SetInsertPoint(TheBB, IP);
540 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
541 MDNode *FPMathTag = nullptr)
542 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
543 SetInsertPoint(TheBB, IP);
546 /// \brief Get the constant folder being used.
547 const T &getFolder() { return Folder; }
549 /// \brief Return true if this builder is configured to actually add the
550 /// requested names to IR created through it.
551 bool isNamePreserving() const { return preserveNames; }
553 /// \brief Insert and return the specified instruction.
554 template<typename InstTy>
555 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
556 this->InsertHelper(I, Name, BB, InsertPt);
557 this->SetInstDebugLocation(I);
561 /// \brief No-op overload to handle constants.
562 Constant *Insert(Constant *C, const Twine& = "") const {
566 //===--------------------------------------------------------------------===//
567 // Instruction creation methods: Terminators
568 //===--------------------------------------------------------------------===//
571 /// \brief Helper to add branch weight metadata onto an instruction.
572 /// \returns The annotated instruction.
573 template <typename InstTy>
574 InstTy *addBranchWeights(InstTy *I, MDNode *Weights) {
576 I->setMetadata(LLVMContext::MD_prof, Weights);
581 /// \brief Create a 'ret void' instruction.
582 ReturnInst *CreateRetVoid() {
583 return Insert(ReturnInst::Create(Context));
586 /// \brief Create a 'ret <val>' instruction.
587 ReturnInst *CreateRet(Value *V) {
588 return Insert(ReturnInst::Create(Context, V));
591 /// \brief Create a sequence of N insertvalue instructions,
592 /// with one Value from the retVals array each, that build a aggregate
593 /// return value one value at a time, and a ret instruction to return
594 /// the resulting aggregate value.
596 /// This is a convenience function for code that uses aggregate return values
597 /// as a vehicle for having multiple return values.
598 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
599 Value *V = UndefValue::get(getCurrentFunctionReturnType());
600 for (unsigned i = 0; i != N; ++i)
601 V = CreateInsertValue(V, retVals[i], i, "mrv");
602 return Insert(ReturnInst::Create(Context, V));
605 /// \brief Create an unconditional 'br label X' instruction.
606 BranchInst *CreateBr(BasicBlock *Dest) {
607 return Insert(BranchInst::Create(Dest));
610 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
612 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
613 MDNode *BranchWeights = nullptr) {
614 return Insert(addBranchWeights(BranchInst::Create(True, False, Cond),
618 /// \brief Create a switch instruction with the specified value, default dest,
619 /// and with a hint for the number of cases that will be added (for efficient
621 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
622 MDNode *BranchWeights = nullptr) {
623 return Insert(addBranchWeights(SwitchInst::Create(V, Dest, NumCases),
627 /// \brief Create an indirect branch instruction with the specified address
628 /// operand, with an optional hint for the number of destinations that will be
629 /// added (for efficient allocation).
630 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
631 return Insert(IndirectBrInst::Create(Addr, NumDests));
634 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
635 BasicBlock *UnwindDest, const Twine &Name = "") {
636 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, None),
639 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
640 BasicBlock *UnwindDest, Value *Arg1,
641 const Twine &Name = "") {
642 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
645 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
646 BasicBlock *UnwindDest, Value *Arg1,
647 Value *Arg2, Value *Arg3,
648 const Twine &Name = "") {
649 Value *Args[] = { Arg1, Arg2, Arg3 };
650 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
653 /// \brief Create an invoke instruction.
654 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
655 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
656 const Twine &Name = "") {
657 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
661 ResumeInst *CreateResume(Value *Exn) {
662 return Insert(ResumeInst::Create(Exn));
665 UnreachableInst *CreateUnreachable() {
666 return Insert(new UnreachableInst(Context));
669 //===--------------------------------------------------------------------===//
670 // Instruction creation methods: Binary Operators
671 //===--------------------------------------------------------------------===//
673 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
674 Value *LHS, Value *RHS,
676 bool HasNUW, bool HasNSW) {
677 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
678 if (HasNUW) BO->setHasNoUnsignedWrap();
679 if (HasNSW) BO->setHasNoSignedWrap();
683 Instruction *AddFPMathAttributes(Instruction *I,
685 FastMathFlags FMF) const {
687 FPMathTag = DefaultFPMathTag;
689 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
690 I->setFastMathFlags(FMF);
694 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
695 bool HasNUW = false, bool HasNSW = false) {
696 if (Constant *LC = dyn_cast<Constant>(LHS))
697 if (Constant *RC = dyn_cast<Constant>(RHS))
698 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
699 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
702 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
703 return CreateAdd(LHS, RHS, Name, false, true);
705 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
706 return CreateAdd(LHS, RHS, Name, true, false);
708 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
709 MDNode *FPMathTag = nullptr) {
710 if (Constant *LC = dyn_cast<Constant>(LHS))
711 if (Constant *RC = dyn_cast<Constant>(RHS))
712 return Insert(Folder.CreateFAdd(LC, RC), Name);
713 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
714 FPMathTag, FMF), Name);
716 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
717 bool HasNUW = false, bool HasNSW = false) {
718 if (Constant *LC = dyn_cast<Constant>(LHS))
719 if (Constant *RC = dyn_cast<Constant>(RHS))
720 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
721 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
724 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
725 return CreateSub(LHS, RHS, Name, false, true);
727 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
728 return CreateSub(LHS, RHS, Name, true, false);
730 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
731 MDNode *FPMathTag = nullptr) {
732 if (Constant *LC = dyn_cast<Constant>(LHS))
733 if (Constant *RC = dyn_cast<Constant>(RHS))
734 return Insert(Folder.CreateFSub(LC, RC), Name);
735 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
736 FPMathTag, FMF), Name);
738 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
739 bool HasNUW = false, bool HasNSW = false) {
740 if (Constant *LC = dyn_cast<Constant>(LHS))
741 if (Constant *RC = dyn_cast<Constant>(RHS))
742 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
743 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
746 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
747 return CreateMul(LHS, RHS, Name, false, true);
749 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
750 return CreateMul(LHS, RHS, Name, true, false);
752 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
753 MDNode *FPMathTag = nullptr) {
754 if (Constant *LC = dyn_cast<Constant>(LHS))
755 if (Constant *RC = dyn_cast<Constant>(RHS))
756 return Insert(Folder.CreateFMul(LC, RC), Name);
757 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
758 FPMathTag, FMF), Name);
760 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
761 bool isExact = false) {
762 if (Constant *LC = dyn_cast<Constant>(LHS))
763 if (Constant *RC = dyn_cast<Constant>(RHS))
764 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
766 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
767 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
769 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
770 return CreateUDiv(LHS, RHS, Name, true);
772 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
773 bool isExact = false) {
774 if (Constant *LC = dyn_cast<Constant>(LHS))
775 if (Constant *RC = dyn_cast<Constant>(RHS))
776 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
778 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
779 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
781 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
782 return CreateSDiv(LHS, RHS, Name, true);
784 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
785 MDNode *FPMathTag = nullptr) {
786 if (Constant *LC = dyn_cast<Constant>(LHS))
787 if (Constant *RC = dyn_cast<Constant>(RHS))
788 return Insert(Folder.CreateFDiv(LC, RC), Name);
789 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
790 FPMathTag, FMF), Name);
792 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
793 if (Constant *LC = dyn_cast<Constant>(LHS))
794 if (Constant *RC = dyn_cast<Constant>(RHS))
795 return Insert(Folder.CreateURem(LC, RC), Name);
796 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
798 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
799 if (Constant *LC = dyn_cast<Constant>(LHS))
800 if (Constant *RC = dyn_cast<Constant>(RHS))
801 return Insert(Folder.CreateSRem(LC, RC), Name);
802 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
804 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
805 MDNode *FPMathTag = nullptr) {
806 if (Constant *LC = dyn_cast<Constant>(LHS))
807 if (Constant *RC = dyn_cast<Constant>(RHS))
808 return Insert(Folder.CreateFRem(LC, RC), Name);
809 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
810 FPMathTag, FMF), Name);
813 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
814 bool HasNUW = false, bool HasNSW = false) {
815 if (Constant *LC = dyn_cast<Constant>(LHS))
816 if (Constant *RC = dyn_cast<Constant>(RHS))
817 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
818 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
821 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
822 bool HasNUW = false, bool HasNSW = false) {
823 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
826 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
827 bool HasNUW = false, bool HasNSW = false) {
828 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
832 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
833 bool isExact = false) {
834 if (Constant *LC = dyn_cast<Constant>(LHS))
835 if (Constant *RC = dyn_cast<Constant>(RHS))
836 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
838 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
839 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
841 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
842 bool isExact = false) {
843 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
845 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
846 bool isExact = false) {
847 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
850 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
851 bool isExact = false) {
852 if (Constant *LC = dyn_cast<Constant>(LHS))
853 if (Constant *RC = dyn_cast<Constant>(RHS))
854 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
856 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
857 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
859 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
860 bool isExact = false) {
861 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
863 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
864 bool isExact = false) {
865 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
868 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
869 if (Constant *RC = dyn_cast<Constant>(RHS)) {
870 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
871 return LHS; // LHS & -1 -> LHS
872 if (Constant *LC = dyn_cast<Constant>(LHS))
873 return Insert(Folder.CreateAnd(LC, RC), Name);
875 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
877 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
878 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
880 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
881 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
884 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
885 if (Constant *RC = dyn_cast<Constant>(RHS)) {
886 if (RC->isNullValue())
887 return LHS; // LHS | 0 -> LHS
888 if (Constant *LC = dyn_cast<Constant>(LHS))
889 return Insert(Folder.CreateOr(LC, RC), Name);
891 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
893 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
894 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
896 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
897 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
900 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
901 if (Constant *LC = dyn_cast<Constant>(LHS))
902 if (Constant *RC = dyn_cast<Constant>(RHS))
903 return Insert(Folder.CreateXor(LC, RC), Name);
904 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
906 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
907 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
909 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
910 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
913 Value *CreateBinOp(Instruction::BinaryOps Opc,
914 Value *LHS, Value *RHS, const Twine &Name = "",
915 MDNode *FPMathTag = nullptr) {
916 if (Constant *LC = dyn_cast<Constant>(LHS))
917 if (Constant *RC = dyn_cast<Constant>(RHS))
918 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
919 llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
920 if (isa<FPMathOperator>(BinOp))
921 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
922 return Insert(BinOp, Name);
925 Value *CreateNeg(Value *V, const Twine &Name = "",
926 bool HasNUW = false, bool HasNSW = false) {
927 if (Constant *VC = dyn_cast<Constant>(V))
928 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
929 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
930 if (HasNUW) BO->setHasNoUnsignedWrap();
931 if (HasNSW) BO->setHasNoSignedWrap();
934 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
935 return CreateNeg(V, Name, false, true);
937 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
938 return CreateNeg(V, Name, true, false);
940 Value *CreateFNeg(Value *V, const Twine &Name = "",
941 MDNode *FPMathTag = nullptr) {
942 if (Constant *VC = dyn_cast<Constant>(V))
943 return Insert(Folder.CreateFNeg(VC), Name);
944 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
945 FPMathTag, FMF), Name);
947 Value *CreateNot(Value *V, const Twine &Name = "") {
948 if (Constant *VC = dyn_cast<Constant>(V))
949 return Insert(Folder.CreateNot(VC), Name);
950 return Insert(BinaryOperator::CreateNot(V), Name);
953 //===--------------------------------------------------------------------===//
954 // Instruction creation methods: Memory Instructions
955 //===--------------------------------------------------------------------===//
957 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
958 const Twine &Name = "") {
959 return Insert(new AllocaInst(Ty, ArraySize), Name);
961 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
962 // converting the string to 'bool' for the isVolatile parameter.
963 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
964 return Insert(new LoadInst(Ptr), Name);
966 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
967 return Insert(new LoadInst(Ptr), Name);
969 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
970 return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
972 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
973 return Insert(new StoreInst(Val, Ptr, isVolatile));
975 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
976 // correctly, instead of converting the string to 'bool' for the isVolatile
978 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
979 LoadInst *LI = CreateLoad(Ptr, Name);
980 LI->setAlignment(Align);
983 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
984 const Twine &Name = "") {
985 LoadInst *LI = CreateLoad(Ptr, Name);
986 LI->setAlignment(Align);
989 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
990 const Twine &Name = "") {
991 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
992 LI->setAlignment(Align);
995 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
996 bool isVolatile = false) {
997 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
998 SI->setAlignment(Align);
1001 FenceInst *CreateFence(AtomicOrdering Ordering,
1002 SynchronizationScope SynchScope = CrossThread,
1003 const Twine &Name = "") {
1004 return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
1007 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1008 AtomicOrdering SuccessOrdering,
1009 AtomicOrdering FailureOrdering,
1010 SynchronizationScope SynchScope = CrossThread) {
1011 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1012 FailureOrdering, SynchScope));
1014 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1015 AtomicOrdering Ordering,
1016 SynchronizationScope SynchScope = CrossThread) {
1017 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
1019 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1020 const Twine &Name = "") {
1021 return CreateGEP(nullptr, Ptr, IdxList, Name);
1023 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1024 const Twine &Name = "") {
1025 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1026 // Every index must be constant.
1028 for (i = 0, e = IdxList.size(); i != e; ++i)
1029 if (!isa<Constant>(IdxList[i]))
1032 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1034 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1036 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1037 const Twine &Name = "") {
1038 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1040 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1041 const Twine &Name = "") {
1042 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1043 // Every index must be constant.
1045 for (i = 0, e = IdxList.size(); i != e; ++i)
1046 if (!isa<Constant>(IdxList[i]))
1049 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1052 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1054 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1055 return CreateGEP(nullptr, Ptr, Idx, Name);
1057 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1058 if (Constant *PC = dyn_cast<Constant>(Ptr))
1059 if (Constant *IC = dyn_cast<Constant>(Idx))
1060 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1061 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1063 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1064 const Twine &Name = "") {
1065 if (Constant *PC = dyn_cast<Constant>(Ptr))
1066 if (Constant *IC = dyn_cast<Constant>(Idx))
1067 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1068 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1070 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1071 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1073 if (Constant *PC = dyn_cast<Constant>(Ptr))
1074 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1076 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1078 Value *CreateConstInBoundsGEP1_32(Value *Ptr, unsigned Idx0,
1079 const Twine &Name = "") {
1080 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1082 if (Constant *PC = dyn_cast<Constant>(Ptr))
1083 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1085 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1087 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1088 const Twine &Name = "") {
1090 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1091 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1094 if (Constant *PC = dyn_cast<Constant>(Ptr))
1095 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1097 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1099 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1100 const Twine &Name = "") {
1102 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1103 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1106 if (Constant *PC = dyn_cast<Constant>(Ptr))
1107 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1110 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1112 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1113 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1115 if (Constant *PC = dyn_cast<Constant>(Ptr))
1116 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1118 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1120 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1121 const Twine &Name = "") {
1122 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1124 if (Constant *PC = dyn_cast<Constant>(Ptr))
1125 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1127 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1129 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1130 const Twine &Name = "") {
1132 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1133 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1136 if (Constant *PC = dyn_cast<Constant>(Ptr))
1137 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1139 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1141 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1142 const Twine &Name = "") {
1144 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1145 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1148 if (Constant *PC = dyn_cast<Constant>(Ptr))
1149 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1152 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1154 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1155 return CreateConstInBoundsGEP2_32(Ptr, 0, Idx, Name);
1158 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1159 /// instead of a pointer to array of i8.
1160 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1161 GlobalVariable *gv = CreateGlobalString(Str, Name);
1162 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1163 Value *Args[] = { zero, zero };
1164 return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
1167 //===--------------------------------------------------------------------===//
1168 // Instruction creation methods: Cast/Conversion Operators
1169 //===--------------------------------------------------------------------===//
1171 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1172 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1174 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1175 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1177 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1178 return CreateCast(Instruction::SExt, V, DestTy, Name);
1180 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1181 /// the value untouched if the type of V is already DestTy.
1182 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1183 const Twine &Name = "") {
1184 assert(V->getType()->isIntOrIntVectorTy() &&
1185 DestTy->isIntOrIntVectorTy() &&
1186 "Can only zero extend/truncate integers!");
1187 Type *VTy = V->getType();
1188 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1189 return CreateZExt(V, DestTy, Name);
1190 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1191 return CreateTrunc(V, DestTy, Name);
1194 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1195 /// the value untouched if the type of V is already DestTy.
1196 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1197 const Twine &Name = "") {
1198 assert(V->getType()->isIntOrIntVectorTy() &&
1199 DestTy->isIntOrIntVectorTy() &&
1200 "Can only sign extend/truncate integers!");
1201 Type *VTy = V->getType();
1202 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1203 return CreateSExt(V, DestTy, Name);
1204 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1205 return CreateTrunc(V, DestTy, Name);
1208 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1209 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1211 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1212 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1214 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1215 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1217 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1218 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1220 Value *CreateFPTrunc(Value *V, Type *DestTy,
1221 const Twine &Name = "") {
1222 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1224 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1225 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1227 Value *CreatePtrToInt(Value *V, Type *DestTy,
1228 const Twine &Name = "") {
1229 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1231 Value *CreateIntToPtr(Value *V, Type *DestTy,
1232 const Twine &Name = "") {
1233 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1235 Value *CreateBitCast(Value *V, Type *DestTy,
1236 const Twine &Name = "") {
1237 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1239 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1240 const Twine &Name = "") {
1241 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1243 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1244 const Twine &Name = "") {
1245 if (V->getType() == DestTy)
1247 if (Constant *VC = dyn_cast<Constant>(V))
1248 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1249 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1251 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1252 const Twine &Name = "") {
1253 if (V->getType() == DestTy)
1255 if (Constant *VC = dyn_cast<Constant>(V))
1256 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1257 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1259 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1260 const Twine &Name = "") {
1261 if (V->getType() == DestTy)
1263 if (Constant *VC = dyn_cast<Constant>(V))
1264 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1265 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1267 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1268 const Twine &Name = "") {
1269 if (V->getType() == DestTy)
1271 if (Constant *VC = dyn_cast<Constant>(V))
1272 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1273 return Insert(CastInst::Create(Op, V, DestTy), Name);
1275 Value *CreatePointerCast(Value *V, Type *DestTy,
1276 const Twine &Name = "") {
1277 if (V->getType() == DestTy)
1279 if (Constant *VC = dyn_cast<Constant>(V))
1280 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1281 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1284 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1285 const Twine &Name = "") {
1286 if (V->getType() == DestTy)
1289 if (Constant *VC = dyn_cast<Constant>(V)) {
1290 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1294 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1298 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1299 const Twine &Name = "") {
1300 if (V->getType() == DestTy)
1302 if (Constant *VC = dyn_cast<Constant>(V))
1303 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1304 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1307 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1308 const Twine &Name = "") {
1309 if (V->getType() == DestTy)
1311 if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
1312 return CreatePtrToInt(V, DestTy, Name);
1313 if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
1314 return CreateIntToPtr(V, DestTy, Name);
1316 return CreateBitCast(V, DestTy, Name);
1319 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1320 // compile time error, instead of converting the string to bool for the
1321 // isSigned parameter.
1322 Value *CreateIntCast(Value *, Type *, const char *) = delete;
1324 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1325 if (V->getType() == DestTy)
1327 if (Constant *VC = dyn_cast<Constant>(V))
1328 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1329 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1332 //===--------------------------------------------------------------------===//
1333 // Instruction creation methods: Compare Instructions
1334 //===--------------------------------------------------------------------===//
1336 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1337 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1339 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1340 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1342 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1343 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1345 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1346 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1348 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1349 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1351 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1352 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1354 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1355 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1357 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1358 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1360 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1361 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1363 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1364 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1367 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1368 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1370 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1371 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1373 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1374 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1376 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1377 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1379 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1380 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1382 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1383 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1385 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1386 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1388 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1389 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1391 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1392 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1394 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1395 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1397 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1398 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1400 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1401 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1403 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1404 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1406 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1407 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1410 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1411 const Twine &Name = "") {
1412 if (Constant *LC = dyn_cast<Constant>(LHS))
1413 if (Constant *RC = dyn_cast<Constant>(RHS))
1414 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1415 return Insert(new ICmpInst(P, LHS, RHS), Name);
1417 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1418 const Twine &Name = "") {
1419 if (Constant *LC = dyn_cast<Constant>(LHS))
1420 if (Constant *RC = dyn_cast<Constant>(RHS))
1421 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1422 return Insert(new FCmpInst(P, LHS, RHS), Name);
1425 //===--------------------------------------------------------------------===//
1426 // Instruction creation methods: Other Instructions
1427 //===--------------------------------------------------------------------===//
1429 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1430 const Twine &Name = "") {
1431 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1434 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1435 return Insert(CallInst::Create(Callee), Name);
1437 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1438 return Insert(CallInst::Create(Callee, Arg), Name);
1440 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1441 const Twine &Name = "") {
1442 Value *Args[] = { Arg1, Arg2 };
1443 return Insert(CallInst::Create(Callee, Args), Name);
1445 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1446 const Twine &Name = "") {
1447 Value *Args[] = { Arg1, Arg2, Arg3 };
1448 return Insert(CallInst::Create(Callee, Args), Name);
1450 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1451 Value *Arg4, const Twine &Name = "") {
1452 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1453 return Insert(CallInst::Create(Callee, Args), Name);
1455 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1456 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1457 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1458 return Insert(CallInst::Create(Callee, Args), Name);
1461 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1462 const Twine &Name = "") {
1463 return Insert(CallInst::Create(Callee, Args), Name);
1466 Value *CreateSelect(Value *C, Value *True, Value *False,
1467 const Twine &Name = "") {
1468 if (Constant *CC = dyn_cast<Constant>(C))
1469 if (Constant *TC = dyn_cast<Constant>(True))
1470 if (Constant *FC = dyn_cast<Constant>(False))
1471 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1472 return Insert(SelectInst::Create(C, True, False), Name);
1475 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1476 return Insert(new VAArgInst(List, Ty), Name);
1479 Value *CreateExtractElement(Value *Vec, Value *Idx,
1480 const Twine &Name = "") {
1481 if (Constant *VC = dyn_cast<Constant>(Vec))
1482 if (Constant *IC = dyn_cast<Constant>(Idx))
1483 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1484 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1487 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1488 const Twine &Name = "") {
1489 if (Constant *VC = dyn_cast<Constant>(Vec))
1490 if (Constant *NC = dyn_cast<Constant>(NewElt))
1491 if (Constant *IC = dyn_cast<Constant>(Idx))
1492 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1493 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1496 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1497 const Twine &Name = "") {
1498 if (Constant *V1C = dyn_cast<Constant>(V1))
1499 if (Constant *V2C = dyn_cast<Constant>(V2))
1500 if (Constant *MC = dyn_cast<Constant>(Mask))
1501 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1502 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1505 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> IntMask,
1506 const Twine &Name = "") {
1507 size_t MaskSize = IntMask.size();
1508 SmallVector<Constant*, 8> MaskVec(MaskSize);
1509 for (size_t i = 0; i != MaskSize; ++i)
1510 MaskVec[i] = getInt32(IntMask[i]);
1511 Value *Mask = ConstantVector::get(MaskVec);
1512 return CreateShuffleVector(V1, V2, Mask, Name);
1515 Value *CreateExtractValue(Value *Agg,
1516 ArrayRef<unsigned> Idxs,
1517 const Twine &Name = "") {
1518 if (Constant *AggC = dyn_cast<Constant>(Agg))
1519 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1520 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1523 Value *CreateInsertValue(Value *Agg, Value *Val,
1524 ArrayRef<unsigned> Idxs,
1525 const Twine &Name = "") {
1526 if (Constant *AggC = dyn_cast<Constant>(Agg))
1527 if (Constant *ValC = dyn_cast<Constant>(Val))
1528 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1529 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1532 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1533 const Twine &Name = "") {
1534 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1537 //===--------------------------------------------------------------------===//
1538 // Utility creation methods
1539 //===--------------------------------------------------------------------===//
1541 /// \brief Return an i1 value testing if \p Arg is null.
1542 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1543 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1547 /// \brief Return an i1 value testing if \p Arg is not null.
1548 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1549 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1553 /// \brief Return the i64 difference between two pointer values, dividing out
1554 /// the size of the pointed-to objects.
1556 /// This is intended to implement C-style pointer subtraction. As such, the
1557 /// pointers must be appropriately aligned for their element types and
1558 /// pointing into the same object.
1559 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1560 assert(LHS->getType() == RHS->getType() &&
1561 "Pointer subtraction operand types must match!");
1562 PointerType *ArgType = cast<PointerType>(LHS->getType());
1563 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1564 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1565 Value *Difference = CreateSub(LHS_int, RHS_int);
1566 return CreateExactSDiv(Difference,
1567 ConstantExpr::getSizeOf(ArgType->getElementType()),
1571 /// \brief Return a vector value that contains \arg V broadcasted to \p
1572 /// NumElts elements.
1573 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1574 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1576 // First insert it into an undef vector so we can shuffle it.
1577 Type *I32Ty = getInt32Ty();
1578 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1579 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1580 Name + ".splatinsert");
1582 // Shuffle the value across the desired number of elements.
1583 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1584 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1587 /// \brief Return a value that has been extracted from a larger integer type.
1588 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1589 IntegerType *ExtractedTy, uint64_t Offset,
1590 const Twine &Name) {
1591 IntegerType *IntTy = cast<IntegerType>(From->getType());
1592 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1593 DL.getTypeStoreSize(IntTy) &&
1594 "Element extends past full value");
1595 uint64_t ShAmt = 8 * Offset;
1597 if (DL.isBigEndian())
1598 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1599 DL.getTypeStoreSize(ExtractedTy) - Offset);
1601 V = CreateLShr(V, ShAmt, Name + ".shift");
1603 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1604 "Cannot extract to a larger integer!");
1605 if (ExtractedTy != IntTy) {
1606 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1611 /// \brief Create an assume intrinsic call that represents an alignment
1612 /// assumption on the provided pointer.
1614 /// An optional offset can be provided, and if it is provided, the offset
1615 /// must be subtracted from the provided pointer to get the pointer with the
1616 /// specified alignment.
1617 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1619 Value *OffsetValue = nullptr) {
1620 assert(isa<PointerType>(PtrValue->getType()) &&
1621 "trying to create an alignment assumption on a non-pointer?");
1623 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1624 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
1625 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
1627 Value *Mask = ConstantInt::get(IntPtrTy,
1628 Alignment > 0 ? Alignment - 1 : 0);
1630 bool IsOffsetZero = false;
1631 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
1632 IsOffsetZero = CI->isZero();
1634 if (!IsOffsetZero) {
1635 if (OffsetValue->getType() != IntPtrTy)
1636 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
1638 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
1642 Value *Zero = ConstantInt::get(IntPtrTy, 0);
1643 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
1644 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
1646 return CreateAssumption(InvCond);
1650 // Create wrappers for C Binding types (see CBindingWrapping.h).
1651 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)