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 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1073 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1074 const Twine &Name = "") {
1075 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1077 if (Constant *PC = dyn_cast<Constant>(Ptr))
1078 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1080 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1082 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1083 const Twine &Name = "") {
1084 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1086 if (Constant *PC = dyn_cast<Constant>(Ptr))
1087 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1089 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1091 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1092 const Twine &Name = "") {
1094 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1095 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1098 if (Constant *PC = dyn_cast<Constant>(Ptr))
1099 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1101 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1103 Value *CreateConstInBoundsGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
1104 const Twine &Name = "") {
1105 return CreateConstInBoundsGEP2_32(nullptr, Ptr, Idx0, Idx1, Name);
1107 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1108 unsigned Idx1, const Twine &Name = "") {
1110 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1111 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1114 if (Constant *PC = dyn_cast<Constant>(Ptr))
1115 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1117 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1119 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1120 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1122 if (Constant *PC = dyn_cast<Constant>(Ptr))
1123 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1125 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1127 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1128 const Twine &Name = "") {
1129 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1131 if (Constant *PC = dyn_cast<Constant>(Ptr))
1132 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1134 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1136 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1137 const Twine &Name = "") {
1139 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1140 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1143 if (Constant *PC = dyn_cast<Constant>(Ptr))
1144 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1146 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1148 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1149 const Twine &Name = "") {
1151 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1152 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1155 if (Constant *PC = dyn_cast<Constant>(Ptr))
1156 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1159 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1161 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1162 return CreateStructGEP(nullptr, Ptr, Idx, Name);
1164 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1165 const Twine &Name = "") {
1166 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1169 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1170 /// instead of a pointer to array of i8.
1171 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "") {
1172 GlobalVariable *gv = CreateGlobalString(Str, Name);
1173 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1174 Value *Args[] = { zero, zero };
1175 return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
1178 //===--------------------------------------------------------------------===//
1179 // Instruction creation methods: Cast/Conversion Operators
1180 //===--------------------------------------------------------------------===//
1182 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1183 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1185 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1186 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1188 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1189 return CreateCast(Instruction::SExt, V, DestTy, Name);
1191 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1192 /// the value untouched if the type of V is already DestTy.
1193 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1194 const Twine &Name = "") {
1195 assert(V->getType()->isIntOrIntVectorTy() &&
1196 DestTy->isIntOrIntVectorTy() &&
1197 "Can only zero extend/truncate integers!");
1198 Type *VTy = V->getType();
1199 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1200 return CreateZExt(V, DestTy, Name);
1201 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1202 return CreateTrunc(V, DestTy, Name);
1205 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1206 /// the value untouched if the type of V is already DestTy.
1207 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1208 const Twine &Name = "") {
1209 assert(V->getType()->isIntOrIntVectorTy() &&
1210 DestTy->isIntOrIntVectorTy() &&
1211 "Can only sign extend/truncate integers!");
1212 Type *VTy = V->getType();
1213 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1214 return CreateSExt(V, DestTy, Name);
1215 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1216 return CreateTrunc(V, DestTy, Name);
1219 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1220 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1222 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1223 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1225 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1226 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1228 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1229 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1231 Value *CreateFPTrunc(Value *V, Type *DestTy,
1232 const Twine &Name = "") {
1233 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1235 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1236 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1238 Value *CreatePtrToInt(Value *V, Type *DestTy,
1239 const Twine &Name = "") {
1240 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1242 Value *CreateIntToPtr(Value *V, Type *DestTy,
1243 const Twine &Name = "") {
1244 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1246 Value *CreateBitCast(Value *V, Type *DestTy,
1247 const Twine &Name = "") {
1248 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1250 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1251 const Twine &Name = "") {
1252 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1254 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1255 const Twine &Name = "") {
1256 if (V->getType() == DestTy)
1258 if (Constant *VC = dyn_cast<Constant>(V))
1259 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1260 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1262 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1263 const Twine &Name = "") {
1264 if (V->getType() == DestTy)
1266 if (Constant *VC = dyn_cast<Constant>(V))
1267 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1268 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1270 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1271 const Twine &Name = "") {
1272 if (V->getType() == DestTy)
1274 if (Constant *VC = dyn_cast<Constant>(V))
1275 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1276 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1278 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1279 const Twine &Name = "") {
1280 if (V->getType() == DestTy)
1282 if (Constant *VC = dyn_cast<Constant>(V))
1283 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1284 return Insert(CastInst::Create(Op, V, DestTy), Name);
1286 Value *CreatePointerCast(Value *V, Type *DestTy,
1287 const Twine &Name = "") {
1288 if (V->getType() == DestTy)
1290 if (Constant *VC = dyn_cast<Constant>(V))
1291 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1292 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1295 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1296 const Twine &Name = "") {
1297 if (V->getType() == DestTy)
1300 if (Constant *VC = dyn_cast<Constant>(V)) {
1301 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1305 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1309 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1310 const Twine &Name = "") {
1311 if (V->getType() == DestTy)
1313 if (Constant *VC = dyn_cast<Constant>(V))
1314 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1315 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1318 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1319 const Twine &Name = "") {
1320 if (V->getType() == DestTy)
1322 if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
1323 return CreatePtrToInt(V, DestTy, Name);
1324 if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
1325 return CreateIntToPtr(V, DestTy, Name);
1327 return CreateBitCast(V, DestTy, Name);
1330 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1331 // compile time error, instead of converting the string to bool for the
1332 // isSigned parameter.
1333 Value *CreateIntCast(Value *, Type *, const char *) = delete;
1335 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1336 if (V->getType() == DestTy)
1338 if (Constant *VC = dyn_cast<Constant>(V))
1339 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1340 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1343 //===--------------------------------------------------------------------===//
1344 // Instruction creation methods: Compare Instructions
1345 //===--------------------------------------------------------------------===//
1347 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1348 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1350 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1351 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1353 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1354 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1356 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1357 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1359 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1360 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1362 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1363 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1365 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1366 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1368 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1369 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1371 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1372 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1374 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1375 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1378 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1379 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name);
1381 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1382 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name);
1384 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1385 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name);
1387 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1388 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name);
1390 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1391 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name);
1393 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "") {
1394 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name);
1396 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "") {
1397 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name);
1399 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "") {
1400 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name);
1402 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1403 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name);
1405 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1406 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name);
1408 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1409 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name);
1411 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1412 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name);
1414 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1415 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name);
1417 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1418 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name);
1421 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1422 const Twine &Name = "") {
1423 if (Constant *LC = dyn_cast<Constant>(LHS))
1424 if (Constant *RC = dyn_cast<Constant>(RHS))
1425 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1426 return Insert(new ICmpInst(P, LHS, RHS), Name);
1428 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1429 const Twine &Name = "") {
1430 if (Constant *LC = dyn_cast<Constant>(LHS))
1431 if (Constant *RC = dyn_cast<Constant>(RHS))
1432 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1433 return Insert(new FCmpInst(P, LHS, RHS), Name);
1436 //===--------------------------------------------------------------------===//
1437 // Instruction creation methods: Other Instructions
1438 //===--------------------------------------------------------------------===//
1440 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1441 const Twine &Name = "") {
1442 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1445 CallInst *CreateCall(Value *Callee, const Twine &Name = "") {
1446 return Insert(CallInst::Create(Callee), Name);
1448 CallInst *CreateCall(Value *Callee, Value *Arg, const Twine &Name = "") {
1449 return Insert(CallInst::Create(Callee, Arg), Name);
1451 CallInst *CreateCall2(Value *Callee, Value *Arg1, Value *Arg2,
1452 const Twine &Name = "") {
1453 Value *Args[] = { Arg1, Arg2 };
1454 return Insert(CallInst::Create(Callee, Args), Name);
1456 CallInst *CreateCall3(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1457 const Twine &Name = "") {
1458 Value *Args[] = { Arg1, Arg2, Arg3 };
1459 return Insert(CallInst::Create(Callee, Args), Name);
1461 CallInst *CreateCall4(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1462 Value *Arg4, const Twine &Name = "") {
1463 Value *Args[] = { Arg1, Arg2, Arg3, Arg4 };
1464 return Insert(CallInst::Create(Callee, Args), Name);
1466 CallInst *CreateCall5(Value *Callee, Value *Arg1, Value *Arg2, Value *Arg3,
1467 Value *Arg4, Value *Arg5, const Twine &Name = "") {
1468 Value *Args[] = { Arg1, Arg2, Arg3, Arg4, Arg5 };
1469 return Insert(CallInst::Create(Callee, Args), Name);
1472 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1473 const Twine &Name = "") {
1474 return Insert(CallInst::Create(Callee, Args), Name);
1477 Value *CreateSelect(Value *C, Value *True, Value *False,
1478 const Twine &Name = "") {
1479 if (Constant *CC = dyn_cast<Constant>(C))
1480 if (Constant *TC = dyn_cast<Constant>(True))
1481 if (Constant *FC = dyn_cast<Constant>(False))
1482 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1483 return Insert(SelectInst::Create(C, True, False), Name);
1486 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1487 return Insert(new VAArgInst(List, Ty), Name);
1490 Value *CreateExtractElement(Value *Vec, Value *Idx,
1491 const Twine &Name = "") {
1492 if (Constant *VC = dyn_cast<Constant>(Vec))
1493 if (Constant *IC = dyn_cast<Constant>(Idx))
1494 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1495 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1498 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1499 const Twine &Name = "") {
1500 if (Constant *VC = dyn_cast<Constant>(Vec))
1501 if (Constant *NC = dyn_cast<Constant>(NewElt))
1502 if (Constant *IC = dyn_cast<Constant>(Idx))
1503 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1504 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1507 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1508 const Twine &Name = "") {
1509 if (Constant *V1C = dyn_cast<Constant>(V1))
1510 if (Constant *V2C = dyn_cast<Constant>(V2))
1511 if (Constant *MC = dyn_cast<Constant>(Mask))
1512 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1513 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1516 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> IntMask,
1517 const Twine &Name = "") {
1518 size_t MaskSize = IntMask.size();
1519 SmallVector<Constant*, 8> MaskVec(MaskSize);
1520 for (size_t i = 0; i != MaskSize; ++i)
1521 MaskVec[i] = getInt32(IntMask[i]);
1522 Value *Mask = ConstantVector::get(MaskVec);
1523 return CreateShuffleVector(V1, V2, Mask, Name);
1526 Value *CreateExtractValue(Value *Agg,
1527 ArrayRef<unsigned> Idxs,
1528 const Twine &Name = "") {
1529 if (Constant *AggC = dyn_cast<Constant>(Agg))
1530 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1531 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1534 Value *CreateInsertValue(Value *Agg, Value *Val,
1535 ArrayRef<unsigned> Idxs,
1536 const Twine &Name = "") {
1537 if (Constant *AggC = dyn_cast<Constant>(Agg))
1538 if (Constant *ValC = dyn_cast<Constant>(Val))
1539 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1540 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1543 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1544 const Twine &Name = "") {
1545 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses), Name);
1548 //===--------------------------------------------------------------------===//
1549 // Utility creation methods
1550 //===--------------------------------------------------------------------===//
1552 /// \brief Return an i1 value testing if \p Arg is null.
1553 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1554 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1558 /// \brief Return an i1 value testing if \p Arg is not null.
1559 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1560 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1564 /// \brief Return the i64 difference between two pointer values, dividing out
1565 /// the size of the pointed-to objects.
1567 /// This is intended to implement C-style pointer subtraction. As such, the
1568 /// pointers must be appropriately aligned for their element types and
1569 /// pointing into the same object.
1570 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1571 assert(LHS->getType() == RHS->getType() &&
1572 "Pointer subtraction operand types must match!");
1573 PointerType *ArgType = cast<PointerType>(LHS->getType());
1574 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1575 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1576 Value *Difference = CreateSub(LHS_int, RHS_int);
1577 return CreateExactSDiv(Difference,
1578 ConstantExpr::getSizeOf(ArgType->getElementType()),
1582 /// \brief Return a vector value that contains \arg V broadcasted to \p
1583 /// NumElts elements.
1584 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1585 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1587 // First insert it into an undef vector so we can shuffle it.
1588 Type *I32Ty = getInt32Ty();
1589 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1590 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1591 Name + ".splatinsert");
1593 // Shuffle the value across the desired number of elements.
1594 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1595 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1598 /// \brief Return a value that has been extracted from a larger integer type.
1599 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1600 IntegerType *ExtractedTy, uint64_t Offset,
1601 const Twine &Name) {
1602 IntegerType *IntTy = cast<IntegerType>(From->getType());
1603 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1604 DL.getTypeStoreSize(IntTy) &&
1605 "Element extends past full value");
1606 uint64_t ShAmt = 8 * Offset;
1608 if (DL.isBigEndian())
1609 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1610 DL.getTypeStoreSize(ExtractedTy) - Offset);
1612 V = CreateLShr(V, ShAmt, Name + ".shift");
1614 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1615 "Cannot extract to a larger integer!");
1616 if (ExtractedTy != IntTy) {
1617 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1622 /// \brief Create an assume intrinsic call that represents an alignment
1623 /// assumption on the provided pointer.
1625 /// An optional offset can be provided, and if it is provided, the offset
1626 /// must be subtracted from the provided pointer to get the pointer with the
1627 /// specified alignment.
1628 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1630 Value *OffsetValue = nullptr) {
1631 assert(isa<PointerType>(PtrValue->getType()) &&
1632 "trying to create an alignment assumption on a non-pointer?");
1634 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1635 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
1636 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
1638 Value *Mask = ConstantInt::get(IntPtrTy,
1639 Alignment > 0 ? Alignment - 1 : 0);
1641 bool IsOffsetZero = false;
1642 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
1643 IsOffsetZero = CI->isZero();
1645 if (!IsOffsetZero) {
1646 if (OffsetValue->getType() != IntPtrTy)
1647 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
1649 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
1653 Value *Zero = ConstantInt::get(IntPtrTy, 0);
1654 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
1655 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
1657 return CreateAssumption(InvCond);
1661 // Create wrappers for C Binding types (see CBindingWrapping.h).
1662 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)