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/Function.h"
25 #include "llvm/IR/GlobalVariable.h"
26 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/LLVMContext.h"
28 #include "llvm/IR/Operator.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/CBindingWrapping.h"
35 /// \brief This provides the default implementation of the IRBuilder
36 /// 'InsertHelper' method that is called whenever an instruction is created by
37 /// IRBuilder and needs to be inserted.
39 /// By default, this inserts the instruction at the insertion point.
40 template <bool preserveNames = true>
41 class IRBuilderDefaultInserter {
43 void InsertHelper(Instruction *I, const Twine &Name,
44 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
45 if (BB) BB->getInstList().insert(InsertPt, I);
51 /// \brief Common base class shared among various IRBuilders.
53 DebugLoc CurDbgLocation;
56 BasicBlock::iterator InsertPt;
59 MDNode *DefaultFPMathTag;
63 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr)
64 : Context(context), DefaultFPMathTag(FPMathTag), FMF() {
65 ClearInsertionPoint();
68 //===--------------------------------------------------------------------===//
69 // Builder configuration methods
70 //===--------------------------------------------------------------------===//
72 /// \brief Clear the insertion point: created instructions will not be
73 /// inserted into a block.
74 void ClearInsertionPoint() {
79 BasicBlock *GetInsertBlock() const { return BB; }
80 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
81 LLVMContext &getContext() const { return Context; }
83 /// \brief This specifies that created instructions should be appended to the
84 /// end of the specified block.
85 void SetInsertPoint(BasicBlock *TheBB) {
90 /// \brief This specifies that created instructions should be inserted before
91 /// the specified instruction.
92 void SetInsertPoint(Instruction *I) {
95 assert(I != BB->end() && "Can't read debug loc from end()");
96 SetCurrentDebugLocation(I->getDebugLoc());
99 /// \brief This specifies that created instructions should be inserted at the
101 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
104 if (IP != TheBB->end())
105 SetCurrentDebugLocation(IP->getDebugLoc());
108 /// \brief Set location information used by debugging information.
109 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
111 /// \brief Get location information used by debugging information.
112 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
114 /// \brief If this builder has a current debug location, set it on the
115 /// specified instruction.
116 void SetInstDebugLocation(Instruction *I) const {
118 I->setDebugLoc(CurDbgLocation);
121 /// \brief Get the return type of the current function that we're emitting
123 Type *getCurrentFunctionReturnType() const;
125 /// InsertPoint - A saved insertion point.
128 BasicBlock::iterator Point;
131 /// \brief Creates a new insertion point which doesn't point to anything.
132 InsertPoint() : Block(nullptr) {}
134 /// \brief Creates a new insertion point at the given location.
135 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
136 : Block(InsertBlock), Point(InsertPoint) {}
138 /// \brief Returns true if this insert point is set.
139 bool isSet() const { return (Block != nullptr); }
141 llvm::BasicBlock *getBlock() const { return Block; }
142 llvm::BasicBlock::iterator getPoint() const { return Point; }
145 /// \brief Returns the current insert point.
146 InsertPoint saveIP() const {
147 return InsertPoint(GetInsertBlock(), GetInsertPoint());
150 /// \brief Returns the current insert point, clearing it in the process.
151 InsertPoint saveAndClearIP() {
152 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
153 ClearInsertionPoint();
157 /// \brief Sets the current insert point to a previously-saved location.
158 void restoreIP(InsertPoint IP) {
160 SetInsertPoint(IP.getBlock(), IP.getPoint());
162 ClearInsertionPoint();
165 /// \brief Get the floating point math metadata being used.
166 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
168 /// \brief Get the flags to be applied to created floating point ops
169 FastMathFlags getFastMathFlags() const { return FMF; }
171 /// \brief Clear the fast-math flags.
172 void clearFastMathFlags() { FMF.clear(); }
174 /// \brief Set the floating point math metadata to be used.
175 void SetDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
177 /// \brief Set the fast-math flags to be used with generated fp-math operators
178 void SetFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
180 //===--------------------------------------------------------------------===//
182 //===--------------------------------------------------------------------===//
184 // \brief RAII object that stores the current insertion point and restores it
185 // when the object is destroyed. This includes the debug location.
186 class InsertPointGuard {
187 IRBuilderBase &Builder;
188 AssertingVH<BasicBlock> Block;
189 BasicBlock::iterator Point;
192 InsertPointGuard(const InsertPointGuard &) = delete;
193 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
196 InsertPointGuard(IRBuilderBase &B)
197 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
198 DbgLoc(B.getCurrentDebugLocation()) {}
200 ~InsertPointGuard() {
201 Builder.restoreIP(InsertPoint(Block, Point));
202 Builder.SetCurrentDebugLocation(DbgLoc);
206 // \brief RAII object that stores the current fast math settings and restores
207 // them when the object is destroyed.
208 class FastMathFlagGuard {
209 IRBuilderBase &Builder;
213 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
214 FastMathFlagGuard &operator=(
215 const FastMathFlagGuard &) = delete;
218 FastMathFlagGuard(IRBuilderBase &B)
219 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
221 ~FastMathFlagGuard() {
223 Builder.DefaultFPMathTag = FPMathTag;
227 //===--------------------------------------------------------------------===//
228 // Miscellaneous creation methods.
229 //===--------------------------------------------------------------------===//
231 /// \brief Make a new global variable with initializer type i8*
233 /// Make a new global variable with an initializer that has array of i8 type
234 /// filled in with the null terminated string value specified. The new global
235 /// variable will be marked mergable with any others of the same contents. If
236 /// Name is specified, it is the name of the global variable created.
237 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
238 unsigned AddressSpace = 0);
240 /// \brief Get a constant value representing either true or false.
241 ConstantInt *getInt1(bool V) {
242 return ConstantInt::get(getInt1Ty(), V);
245 /// \brief Get the constant value for i1 true.
246 ConstantInt *getTrue() {
247 return ConstantInt::getTrue(Context);
250 /// \brief Get the constant value for i1 false.
251 ConstantInt *getFalse() {
252 return ConstantInt::getFalse(Context);
255 /// \brief Get a constant 8-bit value.
256 ConstantInt *getInt8(uint8_t C) {
257 return ConstantInt::get(getInt8Ty(), C);
260 /// \brief Get a constant 16-bit value.
261 ConstantInt *getInt16(uint16_t C) {
262 return ConstantInt::get(getInt16Ty(), C);
265 /// \brief Get a constant 32-bit value.
266 ConstantInt *getInt32(uint32_t C) {
267 return ConstantInt::get(getInt32Ty(), C);
270 /// \brief Get a constant 64-bit value.
271 ConstantInt *getInt64(uint64_t C) {
272 return ConstantInt::get(getInt64Ty(), C);
275 /// \brief Get a constant N-bit value, zero extended or truncated from
277 ConstantInt *getIntN(unsigned N, uint64_t C) {
278 return ConstantInt::get(getIntNTy(N), C);
281 /// \brief Get a constant integer value.
282 ConstantInt *getInt(const APInt &AI) {
283 return ConstantInt::get(Context, AI);
286 //===--------------------------------------------------------------------===//
287 // Type creation methods
288 //===--------------------------------------------------------------------===//
290 /// \brief Fetch the type representing a single bit
291 IntegerType *getInt1Ty() {
292 return Type::getInt1Ty(Context);
295 /// \brief Fetch the type representing an 8-bit integer.
296 IntegerType *getInt8Ty() {
297 return Type::getInt8Ty(Context);
300 /// \brief Fetch the type representing a 16-bit integer.
301 IntegerType *getInt16Ty() {
302 return Type::getInt16Ty(Context);
305 /// \brief Fetch the type representing a 32-bit integer.
306 IntegerType *getInt32Ty() {
307 return Type::getInt32Ty(Context);
310 /// \brief Fetch the type representing a 64-bit integer.
311 IntegerType *getInt64Ty() {
312 return Type::getInt64Ty(Context);
315 /// \brief Fetch the type representing a 128-bit integer.
316 IntegerType *getInt128Ty() {
317 return Type::getInt128Ty(Context);
320 /// \brief Fetch the type representing an N-bit integer.
321 IntegerType *getIntNTy(unsigned N) {
322 return Type::getIntNTy(Context, N);
325 /// \brief Fetch the type representing a 16-bit floating point value.
327 return Type::getHalfTy(Context);
330 /// \brief Fetch the type representing a 32-bit floating point value.
332 return Type::getFloatTy(Context);
335 /// \brief Fetch the type representing a 64-bit floating point value.
336 Type *getDoubleTy() {
337 return Type::getDoubleTy(Context);
340 /// \brief Fetch the type representing void.
342 return Type::getVoidTy(Context);
345 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
346 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
347 return Type::getInt8PtrTy(Context, AddrSpace);
350 /// \brief Fetch the type representing a pointer to an integer value.
351 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
352 return DL.getIntPtrType(Context, AddrSpace);
355 //===--------------------------------------------------------------------===//
356 // Intrinsic creation methods
357 //===--------------------------------------------------------------------===//
359 /// \brief Create and insert a memset to the specified pointer and the
362 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
363 /// specified, it will be added to the instruction. Likewise with alias.scope
364 /// and noalias tags.
365 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
366 bool isVolatile = false, MDNode *TBAATag = nullptr,
367 MDNode *ScopeTag = nullptr,
368 MDNode *NoAliasTag = nullptr) {
369 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
370 TBAATag, ScopeTag, NoAliasTag);
373 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
374 bool isVolatile = false, MDNode *TBAATag = nullptr,
375 MDNode *ScopeTag = nullptr,
376 MDNode *NoAliasTag = nullptr);
378 /// \brief Create and insert a memcpy between the specified pointers.
380 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
381 /// specified, it will be added to the instruction. Likewise with alias.scope
382 /// and noalias tags.
383 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
384 bool isVolatile = false, MDNode *TBAATag = nullptr,
385 MDNode *TBAAStructTag = nullptr,
386 MDNode *ScopeTag = nullptr,
387 MDNode *NoAliasTag = nullptr) {
388 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
389 TBAAStructTag, ScopeTag, NoAliasTag);
392 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
393 bool isVolatile = false, MDNode *TBAATag = nullptr,
394 MDNode *TBAAStructTag = nullptr,
395 MDNode *ScopeTag = nullptr,
396 MDNode *NoAliasTag = nullptr);
398 /// \brief Create and insert a memmove between the specified
401 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
402 /// specified, it will be added to the instruction. Likewise with alias.scope
403 /// and noalias tags.
404 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
405 bool isVolatile = false, MDNode *TBAATag = nullptr,
406 MDNode *ScopeTag = nullptr,
407 MDNode *NoAliasTag = nullptr) {
408 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
409 TBAATag, ScopeTag, NoAliasTag);
412 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
413 bool isVolatile = false, MDNode *TBAATag = nullptr,
414 MDNode *ScopeTag = nullptr,
415 MDNode *NoAliasTag = nullptr);
417 /// \brief Create a lifetime.start intrinsic.
419 /// If the pointer isn't i8* it will be converted.
420 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
422 /// \brief Create a lifetime.end intrinsic.
424 /// If the pointer isn't i8* it will be converted.
425 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
427 /// \brief Create a call to Masked Load intrinsic
428 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
429 Value *PassThru = 0, const Twine &Name = "");
431 /// \brief Create a call to Masked Store intrinsic
432 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
435 /// \brief Create an assume intrinsic call that allows the optimizer to
436 /// assume that the provided condition will be true.
437 CallInst *CreateAssumption(Value *Cond);
439 /// \brief Create a call to the experimental.gc.statepoint intrinsic to
440 /// start a new statepoint sequence.
441 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
443 ArrayRef<Value *> CallArgs,
444 ArrayRef<Value *> DeoptArgs,
445 ArrayRef<Value *> GCArgs,
446 const Twine &Name = "");
448 // \brief Conveninence function for the common case when CallArgs are filled
449 // in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
450 // .get()'ed to get the Value pointer.
451 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
452 Value *ActualCallee, ArrayRef<Use> CallArgs,
453 ArrayRef<Value *> DeoptArgs,
454 ArrayRef<Value *> GCArgs,
455 const Twine &Name = "");
457 /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
458 /// start a new statepoint sequence.
460 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
461 Value *ActualInvokee, BasicBlock *NormalDest,
462 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
463 ArrayRef<Value *> DeoptArgs,
464 ArrayRef<Value *> GCArgs, const Twine &Name = "");
466 // Conveninence function for the common case when CallArgs are filled in using
467 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
470 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
471 Value *ActualInvokee, BasicBlock *NormalDest,
472 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
473 ArrayRef<Value *> DeoptArgs,
474 ArrayRef<Value *> GCArgs, const Twine &Name = "");
476 /// \brief Create a call to the experimental.gc.result intrinsic to extract
477 /// the result from a call wrapped in a statepoint.
478 CallInst *CreateGCResult(Instruction *Statepoint,
480 const Twine &Name = "");
482 /// \brief Create a call to the experimental.gc.relocate intrinsics to
483 /// project the relocated value of one pointer from the statepoint.
484 CallInst *CreateGCRelocate(Instruction *Statepoint,
488 const Twine &Name = "");
491 /// \brief Create a call to a masked intrinsic with given Id.
492 /// Masked intrinsic has only one overloaded type - data type.
493 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
494 Type *DataTy, const Twine &Name = "");
496 Value *getCastedInt8PtrValue(Value *Ptr);
499 /// \brief This provides a uniform API for creating instructions and inserting
500 /// them into a basic block: either at the end of a BasicBlock, or at a specific
501 /// iterator location in a block.
503 /// Note that the builder does not expose the full generality of LLVM
504 /// instructions. For access to extra instruction properties, use the mutators
505 /// (e.g. setVolatile) on the instructions after they have been
506 /// created. Convenience state exists to specify fast-math flags and fp-math
509 /// The first template argument handles whether or not to preserve names in the
510 /// final instruction output. This defaults to on. The second template argument
511 /// specifies a class to use for creating constants. This defaults to creating
512 /// minimally folded constants. The third template argument allows clients to
513 /// specify custom insertion hooks that are called on every newly created
515 template<bool preserveNames = true, typename T = ConstantFolder,
516 typename Inserter = IRBuilderDefaultInserter<preserveNames> >
517 class IRBuilder : public IRBuilderBase, public Inserter {
520 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
521 MDNode *FPMathTag = nullptr)
522 : IRBuilderBase(C, FPMathTag), Inserter(std::move(I)), Folder(F) {}
524 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr)
525 : IRBuilderBase(C, FPMathTag), Folder() {
528 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr)
529 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
530 SetInsertPoint(TheBB);
533 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr)
534 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
535 SetInsertPoint(TheBB);
538 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr)
539 : IRBuilderBase(IP->getContext(), FPMathTag), Folder() {
543 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F,
544 MDNode *FPMathTag = nullptr)
545 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder(F) {
546 SetInsertPoint(TheBB, IP);
549 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
550 MDNode *FPMathTag = nullptr)
551 : IRBuilderBase(TheBB->getContext(), FPMathTag), Folder() {
552 SetInsertPoint(TheBB, IP);
555 /// \brief Get the constant folder being used.
556 const T &getFolder() { return Folder; }
558 /// \brief Return true if this builder is configured to actually add the
559 /// requested names to IR created through it.
560 bool isNamePreserving() const { return preserveNames; }
562 /// \brief Insert and return the specified instruction.
563 template<typename InstTy>
564 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
565 this->InsertHelper(I, Name, BB, InsertPt);
566 this->SetInstDebugLocation(I);
570 /// \brief No-op overload to handle constants.
571 Constant *Insert(Constant *C, const Twine& = "") const {
575 //===--------------------------------------------------------------------===//
576 // Instruction creation methods: Terminators
577 //===--------------------------------------------------------------------===//
580 /// \brief Helper to add branch weight and unpredictable metadata onto an
582 /// \returns The annotated instruction.
583 template <typename InstTy>
584 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
586 I->setMetadata(LLVMContext::MD_prof, Weights);
588 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
593 /// \brief Create a 'ret void' instruction.
594 ReturnInst *CreateRetVoid() {
595 return Insert(ReturnInst::Create(Context));
598 /// \brief Create a 'ret <val>' instruction.
599 ReturnInst *CreateRet(Value *V) {
600 return Insert(ReturnInst::Create(Context, V));
603 /// \brief Create a sequence of N insertvalue instructions,
604 /// with one Value from the retVals array each, that build a aggregate
605 /// return value one value at a time, and a ret instruction to return
606 /// the resulting aggregate value.
608 /// This is a convenience function for code that uses aggregate return values
609 /// as a vehicle for having multiple return values.
610 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
611 Value *V = UndefValue::get(getCurrentFunctionReturnType());
612 for (unsigned i = 0; i != N; ++i)
613 V = CreateInsertValue(V, retVals[i], i, "mrv");
614 return Insert(ReturnInst::Create(Context, V));
617 /// \brief Create an unconditional 'br label X' instruction.
618 BranchInst *CreateBr(BasicBlock *Dest) {
619 return Insert(BranchInst::Create(Dest));
622 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
624 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
625 MDNode *BranchWeights = nullptr,
626 MDNode *Unpredictable = nullptr) {
627 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
628 BranchWeights, Unpredictable));
631 /// \brief Create a switch instruction with the specified value, default dest,
632 /// and with a hint for the number of cases that will be added (for efficient
634 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
635 MDNode *BranchWeights = nullptr) {
636 // TODO: Add unpredictable metadata for a switch.
637 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
638 BranchWeights, nullptr));
641 /// \brief Create an indirect branch instruction with the specified address
642 /// operand, with an optional hint for the number of destinations that will be
643 /// added (for efficient allocation).
644 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
645 return Insert(IndirectBrInst::Create(Addr, NumDests));
648 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
649 BasicBlock *UnwindDest, const Twine &Name = "") {
650 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, None),
653 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
654 BasicBlock *UnwindDest, Value *Arg1,
655 const Twine &Name = "") {
656 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Arg1),
659 InvokeInst *CreateInvoke3(Value *Callee, BasicBlock *NormalDest,
660 BasicBlock *UnwindDest, Value *Arg1,
661 Value *Arg2, Value *Arg3,
662 const Twine &Name = "") {
663 Value *Args[] = { Arg1, Arg2, Arg3 };
664 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
667 /// \brief Create an invoke instruction.
668 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
669 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
670 const Twine &Name = "") {
671 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
675 ResumeInst *CreateResume(Value *Exn) {
676 return Insert(ResumeInst::Create(Exn));
679 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
680 BasicBlock *UnwindBB = nullptr) {
681 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
684 CatchEndPadInst *CreateCleanupEndPad(CleanupPadInst *CleanupPad,
685 BasicBlock *UnwindBB = nullptr) {
686 return Insert(CleanupEndPadInst::Create(CleanupPad, UnwindBB));
689 CatchPadInst *CreateCatchPad(BasicBlock *NormalDest, BasicBlock *UnwindDest,
690 ArrayRef<Value *> Args, const Twine &Name = "") {
691 return Insert(CatchPadInst::Create(NormalDest, UnwindDest, Args), Name);
694 CatchEndPadInst *CreateCatchEndPad(BasicBlock *UnwindBB = nullptr) {
695 return Insert(CatchEndPadInst::Create(Context, UnwindBB));
698 TerminatePadInst *CreateTerminatePad(BasicBlock *UnwindBB = nullptr,
699 ArrayRef<Value *> Args = {},
700 const Twine &Name = "") {
701 return Insert(TerminatePadInst::Create(Context, UnwindBB, Args), Name);
704 CleanupPadInst *CreateCleanupPad(ArrayRef<Value *> Args,
705 const Twine &Name = "") {
706 return Insert(CleanupPadInst::Create(Context, Args), Name);
709 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
710 return Insert(CatchReturnInst::Create(CatchPad, BB));
713 UnreachableInst *CreateUnreachable() {
714 return Insert(new UnreachableInst(Context));
717 //===--------------------------------------------------------------------===//
718 // Instruction creation methods: Binary Operators
719 //===--------------------------------------------------------------------===//
721 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
722 Value *LHS, Value *RHS,
724 bool HasNUW, bool HasNSW) {
725 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
726 if (HasNUW) BO->setHasNoUnsignedWrap();
727 if (HasNSW) BO->setHasNoSignedWrap();
731 Instruction *AddFPMathAttributes(Instruction *I,
733 FastMathFlags FMF) const {
735 FPMathTag = DefaultFPMathTag;
737 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
738 I->setFastMathFlags(FMF);
742 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
743 bool HasNUW = false, bool HasNSW = false) {
744 if (Constant *LC = dyn_cast<Constant>(LHS))
745 if (Constant *RC = dyn_cast<Constant>(RHS))
746 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
747 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
750 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
751 return CreateAdd(LHS, RHS, Name, false, true);
753 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
754 return CreateAdd(LHS, RHS, Name, true, false);
756 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
757 MDNode *FPMathTag = nullptr) {
758 if (Constant *LC = dyn_cast<Constant>(LHS))
759 if (Constant *RC = dyn_cast<Constant>(RHS))
760 return Insert(Folder.CreateFAdd(LC, RC), Name);
761 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
762 FPMathTag, FMF), Name);
764 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
765 bool HasNUW = false, bool HasNSW = false) {
766 if (Constant *LC = dyn_cast<Constant>(LHS))
767 if (Constant *RC = dyn_cast<Constant>(RHS))
768 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
769 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
772 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
773 return CreateSub(LHS, RHS, Name, false, true);
775 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
776 return CreateSub(LHS, RHS, Name, true, false);
778 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
779 MDNode *FPMathTag = nullptr) {
780 if (Constant *LC = dyn_cast<Constant>(LHS))
781 if (Constant *RC = dyn_cast<Constant>(RHS))
782 return Insert(Folder.CreateFSub(LC, RC), Name);
783 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
784 FPMathTag, FMF), Name);
786 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
787 bool HasNUW = false, bool HasNSW = false) {
788 if (Constant *LC = dyn_cast<Constant>(LHS))
789 if (Constant *RC = dyn_cast<Constant>(RHS))
790 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
791 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
794 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
795 return CreateMul(LHS, RHS, Name, false, true);
797 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
798 return CreateMul(LHS, RHS, Name, true, false);
800 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
801 MDNode *FPMathTag = nullptr) {
802 if (Constant *LC = dyn_cast<Constant>(LHS))
803 if (Constant *RC = dyn_cast<Constant>(RHS))
804 return Insert(Folder.CreateFMul(LC, RC), Name);
805 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
806 FPMathTag, FMF), Name);
808 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
809 bool isExact = false) {
810 if (Constant *LC = dyn_cast<Constant>(LHS))
811 if (Constant *RC = dyn_cast<Constant>(RHS))
812 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
814 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
815 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
817 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
818 return CreateUDiv(LHS, RHS, Name, true);
820 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
821 bool isExact = false) {
822 if (Constant *LC = dyn_cast<Constant>(LHS))
823 if (Constant *RC = dyn_cast<Constant>(RHS))
824 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
826 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
827 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
829 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
830 return CreateSDiv(LHS, RHS, Name, true);
832 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
833 MDNode *FPMathTag = nullptr) {
834 if (Constant *LC = dyn_cast<Constant>(LHS))
835 if (Constant *RC = dyn_cast<Constant>(RHS))
836 return Insert(Folder.CreateFDiv(LC, RC), Name);
837 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
838 FPMathTag, FMF), Name);
840 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
841 if (Constant *LC = dyn_cast<Constant>(LHS))
842 if (Constant *RC = dyn_cast<Constant>(RHS))
843 return Insert(Folder.CreateURem(LC, RC), Name);
844 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
846 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
847 if (Constant *LC = dyn_cast<Constant>(LHS))
848 if (Constant *RC = dyn_cast<Constant>(RHS))
849 return Insert(Folder.CreateSRem(LC, RC), Name);
850 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
852 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
853 MDNode *FPMathTag = nullptr) {
854 if (Constant *LC = dyn_cast<Constant>(LHS))
855 if (Constant *RC = dyn_cast<Constant>(RHS))
856 return Insert(Folder.CreateFRem(LC, RC), Name);
857 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
858 FPMathTag, FMF), Name);
861 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
862 bool HasNUW = false, bool HasNSW = false) {
863 if (Constant *LC = dyn_cast<Constant>(LHS))
864 if (Constant *RC = dyn_cast<Constant>(RHS))
865 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
866 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
869 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
870 bool HasNUW = false, bool HasNSW = false) {
871 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
874 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
875 bool HasNUW = false, bool HasNSW = false) {
876 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
880 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
881 bool isExact = false) {
882 if (Constant *LC = dyn_cast<Constant>(LHS))
883 if (Constant *RC = dyn_cast<Constant>(RHS))
884 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
886 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
887 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
889 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
890 bool isExact = false) {
891 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
893 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
894 bool isExact = false) {
895 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
898 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
899 bool isExact = false) {
900 if (Constant *LC = dyn_cast<Constant>(LHS))
901 if (Constant *RC = dyn_cast<Constant>(RHS))
902 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
904 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
905 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
907 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
908 bool isExact = false) {
909 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
911 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
912 bool isExact = false) {
913 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
916 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
917 if (Constant *RC = dyn_cast<Constant>(RHS)) {
918 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
919 return LHS; // LHS & -1 -> LHS
920 if (Constant *LC = dyn_cast<Constant>(LHS))
921 return Insert(Folder.CreateAnd(LC, RC), Name);
923 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
925 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
926 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
928 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
929 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
932 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
933 if (Constant *RC = dyn_cast<Constant>(RHS)) {
934 if (RC->isNullValue())
935 return LHS; // LHS | 0 -> LHS
936 if (Constant *LC = dyn_cast<Constant>(LHS))
937 return Insert(Folder.CreateOr(LC, RC), Name);
939 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
941 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
942 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
944 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
945 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
948 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
949 if (Constant *LC = dyn_cast<Constant>(LHS))
950 if (Constant *RC = dyn_cast<Constant>(RHS))
951 return Insert(Folder.CreateXor(LC, RC), Name);
952 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
954 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
955 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
957 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
958 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
961 Value *CreateBinOp(Instruction::BinaryOps Opc,
962 Value *LHS, Value *RHS, const Twine &Name = "",
963 MDNode *FPMathTag = nullptr) {
964 if (Constant *LC = dyn_cast<Constant>(LHS))
965 if (Constant *RC = dyn_cast<Constant>(RHS))
966 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
967 llvm::Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
968 if (isa<FPMathOperator>(BinOp))
969 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
970 return Insert(BinOp, Name);
973 Value *CreateNeg(Value *V, const Twine &Name = "",
974 bool HasNUW = false, bool HasNSW = false) {
975 if (Constant *VC = dyn_cast<Constant>(V))
976 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
977 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
978 if (HasNUW) BO->setHasNoUnsignedWrap();
979 if (HasNSW) BO->setHasNoSignedWrap();
982 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
983 return CreateNeg(V, Name, false, true);
985 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
986 return CreateNeg(V, Name, true, false);
988 Value *CreateFNeg(Value *V, const Twine &Name = "",
989 MDNode *FPMathTag = nullptr) {
990 if (Constant *VC = dyn_cast<Constant>(V))
991 return Insert(Folder.CreateFNeg(VC), Name);
992 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
993 FPMathTag, FMF), Name);
995 Value *CreateNot(Value *V, const Twine &Name = "") {
996 if (Constant *VC = dyn_cast<Constant>(V))
997 return Insert(Folder.CreateNot(VC), Name);
998 return Insert(BinaryOperator::CreateNot(V), Name);
1001 //===--------------------------------------------------------------------===//
1002 // Instruction creation methods: Memory Instructions
1003 //===--------------------------------------------------------------------===//
1005 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1006 const Twine &Name = "") {
1007 return Insert(new AllocaInst(Ty, ArraySize), Name);
1009 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
1010 // converting the string to 'bool' for the isVolatile parameter.
1011 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1012 return Insert(new LoadInst(Ptr), Name);
1014 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1015 return Insert(new LoadInst(Ptr), Name);
1017 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1018 return Insert(new LoadInst(Ty, Ptr), Name);
1020 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1021 return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
1023 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1024 return Insert(new StoreInst(Val, Ptr, isVolatile));
1026 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1027 // correctly, instead of converting the string to 'bool' for the isVolatile
1029 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1030 LoadInst *LI = CreateLoad(Ptr, Name);
1031 LI->setAlignment(Align);
1034 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1035 const Twine &Name = "") {
1036 LoadInst *LI = CreateLoad(Ptr, Name);
1037 LI->setAlignment(Align);
1040 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1041 const Twine &Name = "") {
1042 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
1043 LI->setAlignment(Align);
1046 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1047 bool isVolatile = false) {
1048 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1049 SI->setAlignment(Align);
1052 FenceInst *CreateFence(AtomicOrdering Ordering,
1053 SynchronizationScope SynchScope = CrossThread,
1054 const Twine &Name = "") {
1055 return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
1058 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1059 AtomicOrdering SuccessOrdering,
1060 AtomicOrdering FailureOrdering,
1061 SynchronizationScope SynchScope = CrossThread) {
1062 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1063 FailureOrdering, SynchScope));
1065 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1066 AtomicOrdering Ordering,
1067 SynchronizationScope SynchScope = CrossThread) {
1068 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
1070 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1071 const Twine &Name = "") {
1072 return CreateGEP(nullptr, Ptr, IdxList, Name);
1074 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1075 const Twine &Name = "") {
1076 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1077 // Every index must be constant.
1079 for (i = 0, e = IdxList.size(); i != e; ++i)
1080 if (!isa<Constant>(IdxList[i]))
1083 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1085 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1087 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1088 const Twine &Name = "") {
1089 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1091 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1092 const Twine &Name = "") {
1093 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1094 // Every index must be constant.
1096 for (i = 0, e = IdxList.size(); i != e; ++i)
1097 if (!isa<Constant>(IdxList[i]))
1100 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1103 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1105 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1106 return CreateGEP(nullptr, Ptr, Idx, Name);
1108 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1109 if (Constant *PC = dyn_cast<Constant>(Ptr))
1110 if (Constant *IC = dyn_cast<Constant>(Idx))
1111 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1112 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1114 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1115 const Twine &Name = "") {
1116 if (Constant *PC = dyn_cast<Constant>(Ptr))
1117 if (Constant *IC = dyn_cast<Constant>(Idx))
1118 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1119 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1121 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1122 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1124 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1125 const Twine &Name = "") {
1126 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1128 if (Constant *PC = dyn_cast<Constant>(Ptr))
1129 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1131 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1133 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1134 const Twine &Name = "") {
1135 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1137 if (Constant *PC = dyn_cast<Constant>(Ptr))
1138 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1140 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1142 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1143 const Twine &Name = "") {
1145 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1146 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1149 if (Constant *PC = dyn_cast<Constant>(Ptr))
1150 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1152 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1154 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1155 unsigned Idx1, const Twine &Name = "") {
1157 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1158 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1161 if (Constant *PC = dyn_cast<Constant>(Ptr))
1162 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1164 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1166 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1167 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1169 if (Constant *PC = dyn_cast<Constant>(Ptr))
1170 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1172 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1174 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1175 const Twine &Name = "") {
1176 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1178 if (Constant *PC = dyn_cast<Constant>(Ptr))
1179 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1181 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1183 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1184 const Twine &Name = "") {
1186 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1187 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1190 if (Constant *PC = dyn_cast<Constant>(Ptr))
1191 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1193 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1195 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1196 const Twine &Name = "") {
1198 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1199 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1202 if (Constant *PC = dyn_cast<Constant>(Ptr))
1203 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1206 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1208 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1209 const Twine &Name = "") {
1210 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1213 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1214 /// instead of a pointer to array of i8.
1215 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1216 unsigned AddressSpace = 0) {
1217 GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
1218 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1219 Value *Args[] = { zero, zero };
1220 return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
1223 //===--------------------------------------------------------------------===//
1224 // Instruction creation methods: Cast/Conversion Operators
1225 //===--------------------------------------------------------------------===//
1227 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1228 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1230 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1231 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1233 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1234 return CreateCast(Instruction::SExt, V, DestTy, Name);
1236 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1237 /// the value untouched if the type of V is already DestTy.
1238 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1239 const Twine &Name = "") {
1240 assert(V->getType()->isIntOrIntVectorTy() &&
1241 DestTy->isIntOrIntVectorTy() &&
1242 "Can only zero extend/truncate integers!");
1243 Type *VTy = V->getType();
1244 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1245 return CreateZExt(V, DestTy, Name);
1246 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1247 return CreateTrunc(V, DestTy, Name);
1250 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1251 /// the value untouched if the type of V is already DestTy.
1252 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1253 const Twine &Name = "") {
1254 assert(V->getType()->isIntOrIntVectorTy() &&
1255 DestTy->isIntOrIntVectorTy() &&
1256 "Can only sign extend/truncate integers!");
1257 Type *VTy = V->getType();
1258 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1259 return CreateSExt(V, DestTy, Name);
1260 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1261 return CreateTrunc(V, DestTy, Name);
1264 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1265 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1267 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1268 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1270 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1271 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1273 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1274 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1276 Value *CreateFPTrunc(Value *V, Type *DestTy,
1277 const Twine &Name = "") {
1278 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1280 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1281 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1283 Value *CreatePtrToInt(Value *V, Type *DestTy,
1284 const Twine &Name = "") {
1285 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1287 Value *CreateIntToPtr(Value *V, Type *DestTy,
1288 const Twine &Name = "") {
1289 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1291 Value *CreateBitCast(Value *V, Type *DestTy,
1292 const Twine &Name = "") {
1293 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1295 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1296 const Twine &Name = "") {
1297 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1299 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1300 const Twine &Name = "") {
1301 if (V->getType() == DestTy)
1303 if (Constant *VC = dyn_cast<Constant>(V))
1304 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1305 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1307 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1308 const Twine &Name = "") {
1309 if (V->getType() == DestTy)
1311 if (Constant *VC = dyn_cast<Constant>(V))
1312 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1313 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1315 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1316 const Twine &Name = "") {
1317 if (V->getType() == DestTy)
1319 if (Constant *VC = dyn_cast<Constant>(V))
1320 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1321 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1323 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1324 const Twine &Name = "") {
1325 if (V->getType() == DestTy)
1327 if (Constant *VC = dyn_cast<Constant>(V))
1328 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1329 return Insert(CastInst::Create(Op, V, DestTy), Name);
1331 Value *CreatePointerCast(Value *V, Type *DestTy,
1332 const Twine &Name = "") {
1333 if (V->getType() == DestTy)
1335 if (Constant *VC = dyn_cast<Constant>(V))
1336 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1337 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1340 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1341 const Twine &Name = "") {
1342 if (V->getType() == DestTy)
1345 if (Constant *VC = dyn_cast<Constant>(V)) {
1346 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1350 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1354 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1355 const Twine &Name = "") {
1356 if (V->getType() == DestTy)
1358 if (Constant *VC = dyn_cast<Constant>(V))
1359 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1360 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1363 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1364 const Twine &Name = "") {
1365 if (V->getType() == DestTy)
1367 if (V->getType()->isPointerTy() && DestTy->isIntegerTy())
1368 return CreatePtrToInt(V, DestTy, Name);
1369 if (V->getType()->isIntegerTy() && DestTy->isPointerTy())
1370 return CreateIntToPtr(V, DestTy, Name);
1372 return CreateBitCast(V, DestTy, Name);
1375 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1376 // compile time error, instead of converting the string to bool for the
1377 // isSigned parameter.
1378 Value *CreateIntCast(Value *, Type *, const char *) = delete;
1380 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1381 if (V->getType() == DestTy)
1383 if (Constant *VC = dyn_cast<Constant>(V))
1384 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1385 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1388 //===--------------------------------------------------------------------===//
1389 // Instruction creation methods: Compare Instructions
1390 //===--------------------------------------------------------------------===//
1392 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1393 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1395 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1396 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1398 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1399 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1401 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1402 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1404 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1405 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1407 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1408 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1410 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1411 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1413 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1414 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1416 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1417 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1419 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1420 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1423 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1424 MDNode *FPMathTag = nullptr) {
1425 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
1427 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
1428 MDNode *FPMathTag = nullptr) {
1429 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
1431 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
1432 MDNode *FPMathTag = nullptr) {
1433 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
1435 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
1436 MDNode *FPMathTag = nullptr) {
1437 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
1439 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
1440 MDNode *FPMathTag = nullptr) {
1441 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
1443 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
1444 MDNode *FPMathTag = nullptr) {
1445 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
1447 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
1448 MDNode *FPMathTag = nullptr) {
1449 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
1451 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
1452 MDNode *FPMathTag = nullptr) {
1453 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
1455 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1456 MDNode *FPMathTag = nullptr) {
1457 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
1459 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
1460 MDNode *FPMathTag = nullptr) {
1461 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
1463 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
1464 MDNode *FPMathTag = nullptr) {
1465 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
1467 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
1468 MDNode *FPMathTag = nullptr) {
1469 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
1471 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
1472 MDNode *FPMathTag = nullptr) {
1473 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
1475 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
1476 MDNode *FPMathTag = nullptr) {
1477 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
1480 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1481 const Twine &Name = "") {
1482 if (Constant *LC = dyn_cast<Constant>(LHS))
1483 if (Constant *RC = dyn_cast<Constant>(RHS))
1484 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1485 return Insert(new ICmpInst(P, LHS, RHS), Name);
1487 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1488 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1489 if (Constant *LC = dyn_cast<Constant>(LHS))
1490 if (Constant *RC = dyn_cast<Constant>(RHS))
1491 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1492 return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS),
1493 FPMathTag, FMF), Name);
1496 //===--------------------------------------------------------------------===//
1497 // Instruction creation methods: Other Instructions
1498 //===--------------------------------------------------------------------===//
1500 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1501 const Twine &Name = "") {
1502 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1505 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
1506 const Twine &Name = "") {
1507 return Insert(CallInst::Create(Callee, Args), Name);
1510 CallInst *CreateCall(llvm::FunctionType *FTy, Value *Callee,
1511 ArrayRef<Value *> Args, const Twine &Name = "") {
1512 return Insert(CallInst::Create(FTy, Callee, Args), Name);
1515 CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
1516 const Twine &Name = "") {
1517 return CreateCall(Callee->getFunctionType(), Callee, Args, Name);
1520 Value *CreateSelect(Value *C, Value *True, Value *False,
1521 const Twine &Name = "") {
1522 if (Constant *CC = dyn_cast<Constant>(C))
1523 if (Constant *TC = dyn_cast<Constant>(True))
1524 if (Constant *FC = dyn_cast<Constant>(False))
1525 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1526 return Insert(SelectInst::Create(C, True, False), Name);
1529 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1530 return Insert(new VAArgInst(List, Ty), Name);
1533 Value *CreateExtractElement(Value *Vec, Value *Idx,
1534 const Twine &Name = "") {
1535 if (Constant *VC = dyn_cast<Constant>(Vec))
1536 if (Constant *IC = dyn_cast<Constant>(Idx))
1537 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1538 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1541 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
1542 const Twine &Name = "") {
1543 return CreateExtractElement(Vec, getInt64(Idx), Name);
1546 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1547 const Twine &Name = "") {
1548 if (Constant *VC = dyn_cast<Constant>(Vec))
1549 if (Constant *NC = dyn_cast<Constant>(NewElt))
1550 if (Constant *IC = dyn_cast<Constant>(Idx))
1551 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1552 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1555 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
1556 const Twine &Name = "") {
1557 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
1560 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1561 const Twine &Name = "") {
1562 if (Constant *V1C = dyn_cast<Constant>(V1))
1563 if (Constant *V2C = dyn_cast<Constant>(V2))
1564 if (Constant *MC = dyn_cast<Constant>(Mask))
1565 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1566 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1569 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> IntMask,
1570 const Twine &Name = "") {
1571 size_t MaskSize = IntMask.size();
1572 SmallVector<Constant*, 8> MaskVec(MaskSize);
1573 for (size_t i = 0; i != MaskSize; ++i)
1574 MaskVec[i] = getInt32(IntMask[i]);
1575 Value *Mask = ConstantVector::get(MaskVec);
1576 return CreateShuffleVector(V1, V2, Mask, Name);
1579 Value *CreateExtractValue(Value *Agg,
1580 ArrayRef<unsigned> Idxs,
1581 const Twine &Name = "") {
1582 if (Constant *AggC = dyn_cast<Constant>(Agg))
1583 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1584 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1587 Value *CreateInsertValue(Value *Agg, Value *Val,
1588 ArrayRef<unsigned> Idxs,
1589 const Twine &Name = "") {
1590 if (Constant *AggC = dyn_cast<Constant>(Agg))
1591 if (Constant *ValC = dyn_cast<Constant>(Val))
1592 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1593 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1596 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
1597 const Twine &Name = "") {
1598 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
1601 //===--------------------------------------------------------------------===//
1602 // Utility creation methods
1603 //===--------------------------------------------------------------------===//
1605 /// \brief Return an i1 value testing if \p Arg is null.
1606 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1607 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1611 /// \brief Return an i1 value testing if \p Arg is not null.
1612 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1613 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1617 /// \brief Return the i64 difference between two pointer values, dividing out
1618 /// the size of the pointed-to objects.
1620 /// This is intended to implement C-style pointer subtraction. As such, the
1621 /// pointers must be appropriately aligned for their element types and
1622 /// pointing into the same object.
1623 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1624 assert(LHS->getType() == RHS->getType() &&
1625 "Pointer subtraction operand types must match!");
1626 PointerType *ArgType = cast<PointerType>(LHS->getType());
1627 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1628 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1629 Value *Difference = CreateSub(LHS_int, RHS_int);
1630 return CreateExactSDiv(Difference,
1631 ConstantExpr::getSizeOf(ArgType->getElementType()),
1635 /// \brief Return a vector value that contains \arg V broadcasted to \p
1636 /// NumElts elements.
1637 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1638 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1640 // First insert it into an undef vector so we can shuffle it.
1641 Type *I32Ty = getInt32Ty();
1642 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1643 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1644 Name + ".splatinsert");
1646 // Shuffle the value across the desired number of elements.
1647 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1648 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1651 /// \brief Return a value that has been extracted from a larger integer type.
1652 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1653 IntegerType *ExtractedTy, uint64_t Offset,
1654 const Twine &Name) {
1655 IntegerType *IntTy = cast<IntegerType>(From->getType());
1656 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1657 DL.getTypeStoreSize(IntTy) &&
1658 "Element extends past full value");
1659 uint64_t ShAmt = 8 * Offset;
1661 if (DL.isBigEndian())
1662 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1663 DL.getTypeStoreSize(ExtractedTy) - Offset);
1665 V = CreateLShr(V, ShAmt, Name + ".shift");
1667 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1668 "Cannot extract to a larger integer!");
1669 if (ExtractedTy != IntTy) {
1670 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1675 /// \brief Create an assume intrinsic call that represents an alignment
1676 /// assumption on the provided pointer.
1678 /// An optional offset can be provided, and if it is provided, the offset
1679 /// must be subtracted from the provided pointer to get the pointer with the
1680 /// specified alignment.
1681 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1683 Value *OffsetValue = nullptr) {
1684 assert(isa<PointerType>(PtrValue->getType()) &&
1685 "trying to create an alignment assumption on a non-pointer?");
1687 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1688 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
1689 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
1691 Value *Mask = ConstantInt::get(IntPtrTy,
1692 Alignment > 0 ? Alignment - 1 : 0);
1694 bool IsOffsetZero = false;
1695 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
1696 IsOffsetZero = CI->isZero();
1698 if (!IsOffsetZero) {
1699 if (OffsetValue->getType() != IntPtrTy)
1700 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
1702 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
1706 Value *Zero = ConstantInt::get(IntPtrTy, 0);
1707 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
1708 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
1710 return CreateAssumption(InvCond);
1714 // Create wrappers for C Binding types (see CBindingWrapping.h).
1715 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)