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-c/Types.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/ConstantFolder.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DataLayout.h"
28 #include "llvm/IR/DebugLoc.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/Module.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/IR/Type.h"
40 #include "llvm/IR/Value.h"
41 #include "llvm/IR/ValueHandle.h"
42 #include "llvm/Support/AtomicOrdering.h"
43 #include "llvm/Support/CBindingWrapping.h"
44 #include "llvm/Support/Casting.h"
58 /// \brief This provides the default implementation of the IRBuilder
59 /// 'InsertHelper' method that is called whenever an instruction is created by
60 /// IRBuilder and needs to be inserted.
62 /// By default, this inserts the instruction at the insertion point.
63 class IRBuilderDefaultInserter {
65 void InsertHelper(Instruction *I, const Twine &Name,
66 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
67 if (BB) BB->getInstList().insert(InsertPt, I);
72 /// Provides an 'InsertHelper' that calls a user-provided callback after
73 /// performing the default insertion.
74 class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
75 std::function<void(Instruction *)> Callback;
78 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
79 : Callback(std::move(Callback)) {}
82 void InsertHelper(Instruction *I, const Twine &Name,
83 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
84 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
89 /// \brief Common base class shared among various IRBuilders.
91 DebugLoc CurDbgLocation;
95 BasicBlock::iterator InsertPt;
98 MDNode *DefaultFPMathTag;
101 ArrayRef<OperandBundleDef> DefaultOperandBundles;
104 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
105 ArrayRef<OperandBundleDef> OpBundles = None)
106 : Context(context), DefaultFPMathTag(FPMathTag),
107 DefaultOperandBundles(OpBundles) {
108 ClearInsertionPoint();
111 //===--------------------------------------------------------------------===//
112 // Builder configuration methods
113 //===--------------------------------------------------------------------===//
115 /// \brief Clear the insertion point: created instructions will not be
116 /// inserted into a block.
117 void ClearInsertionPoint() {
119 InsertPt = BasicBlock::iterator();
122 BasicBlock *GetInsertBlock() const { return BB; }
123 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
124 LLVMContext &getContext() const { return Context; }
126 /// \brief This specifies that created instructions should be appended to the
127 /// end of the specified block.
128 void SetInsertPoint(BasicBlock *TheBB) {
130 InsertPt = BB->end();
133 /// \brief This specifies that created instructions should be inserted before
134 /// the specified instruction.
135 void SetInsertPoint(Instruction *I) {
137 InsertPt = I->getIterator();
138 assert(InsertPt != BB->end() && "Can't read debug loc from end()");
139 SetCurrentDebugLocation(I->getDebugLoc());
142 /// \brief This specifies that created instructions should be inserted at the
144 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
147 if (IP != TheBB->end())
148 SetCurrentDebugLocation(IP->getDebugLoc());
151 /// \brief Set location information used by debugging information.
152 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
154 /// \brief Get location information used by debugging information.
155 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
157 /// \brief If this builder has a current debug location, set it on the
158 /// specified instruction.
159 void SetInstDebugLocation(Instruction *I) const {
161 I->setDebugLoc(CurDbgLocation);
164 /// \brief Get the return type of the current function that we're emitting
166 Type *getCurrentFunctionReturnType() const;
168 /// InsertPoint - A saved insertion point.
170 BasicBlock *Block = nullptr;
171 BasicBlock::iterator Point;
174 /// \brief Creates a new insertion point which doesn't point to anything.
175 InsertPoint() = default;
177 /// \brief Creates a new insertion point at the given location.
178 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
179 : Block(InsertBlock), Point(InsertPoint) {}
181 /// \brief Returns true if this insert point is set.
182 bool isSet() const { return (Block != nullptr); }
184 BasicBlock *getBlock() const { return Block; }
185 BasicBlock::iterator getPoint() const { return Point; }
188 /// \brief Returns the current insert point.
189 InsertPoint saveIP() const {
190 return InsertPoint(GetInsertBlock(), GetInsertPoint());
193 /// \brief Returns the current insert point, clearing it in the process.
194 InsertPoint saveAndClearIP() {
195 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
196 ClearInsertionPoint();
200 /// \brief Sets the current insert point to a previously-saved location.
201 void restoreIP(InsertPoint IP) {
203 SetInsertPoint(IP.getBlock(), IP.getPoint());
205 ClearInsertionPoint();
208 /// \brief Get the floating point math metadata being used.
209 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
211 /// \brief Get the flags to be applied to created floating point ops
212 FastMathFlags getFastMathFlags() const { return FMF; }
214 /// \brief Clear the fast-math flags.
215 void clearFastMathFlags() { FMF.clear(); }
217 /// \brief Set the floating point math metadata to be used.
218 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
220 /// \brief Set the fast-math flags to be used with generated fp-math operators
221 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
223 //===--------------------------------------------------------------------===//
225 //===--------------------------------------------------------------------===//
227 // \brief RAII object that stores the current insertion point and restores it
228 // when the object is destroyed. This includes the debug location.
229 class InsertPointGuard {
230 IRBuilderBase &Builder;
231 AssertingVH<BasicBlock> Block;
232 BasicBlock::iterator Point;
236 InsertPointGuard(IRBuilderBase &B)
237 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
238 DbgLoc(B.getCurrentDebugLocation()) {}
240 InsertPointGuard(const InsertPointGuard &) = delete;
241 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
243 ~InsertPointGuard() {
244 Builder.restoreIP(InsertPoint(Block, Point));
245 Builder.SetCurrentDebugLocation(DbgLoc);
249 // \brief RAII object that stores the current fast math settings and restores
250 // them when the object is destroyed.
251 class FastMathFlagGuard {
252 IRBuilderBase &Builder;
257 FastMathFlagGuard(IRBuilderBase &B)
258 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
260 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
261 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
263 ~FastMathFlagGuard() {
265 Builder.DefaultFPMathTag = FPMathTag;
269 //===--------------------------------------------------------------------===//
270 // Miscellaneous creation methods.
271 //===--------------------------------------------------------------------===//
273 /// \brief Make a new global variable with initializer type i8*
275 /// Make a new global variable with an initializer that has array of i8 type
276 /// filled in with the null terminated string value specified. The new global
277 /// variable will be marked mergable with any others of the same contents. If
278 /// Name is specified, it is the name of the global variable created.
279 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
280 unsigned AddressSpace = 0);
282 /// \brief Get a constant value representing either true or false.
283 ConstantInt *getInt1(bool V) {
284 return ConstantInt::get(getInt1Ty(), V);
287 /// \brief Get the constant value for i1 true.
288 ConstantInt *getTrue() {
289 return ConstantInt::getTrue(Context);
292 /// \brief Get the constant value for i1 false.
293 ConstantInt *getFalse() {
294 return ConstantInt::getFalse(Context);
297 /// \brief Get a constant 8-bit value.
298 ConstantInt *getInt8(uint8_t C) {
299 return ConstantInt::get(getInt8Ty(), C);
302 /// \brief Get a constant 16-bit value.
303 ConstantInt *getInt16(uint16_t C) {
304 return ConstantInt::get(getInt16Ty(), C);
307 /// \brief Get a constant 32-bit value.
308 ConstantInt *getInt32(uint32_t C) {
309 return ConstantInt::get(getInt32Ty(), C);
312 /// \brief Get a constant 64-bit value.
313 ConstantInt *getInt64(uint64_t C) {
314 return ConstantInt::get(getInt64Ty(), C);
317 /// \brief Get a constant N-bit value, zero extended or truncated from
319 ConstantInt *getIntN(unsigned N, uint64_t C) {
320 return ConstantInt::get(getIntNTy(N), C);
323 /// \brief Get a constant integer value.
324 ConstantInt *getInt(const APInt &AI) {
325 return ConstantInt::get(Context, AI);
328 //===--------------------------------------------------------------------===//
329 // Type creation methods
330 //===--------------------------------------------------------------------===//
332 /// \brief Fetch the type representing a single bit
333 IntegerType *getInt1Ty() {
334 return Type::getInt1Ty(Context);
337 /// \brief Fetch the type representing an 8-bit integer.
338 IntegerType *getInt8Ty() {
339 return Type::getInt8Ty(Context);
342 /// \brief Fetch the type representing a 16-bit integer.
343 IntegerType *getInt16Ty() {
344 return Type::getInt16Ty(Context);
347 /// \brief Fetch the type representing a 32-bit integer.
348 IntegerType *getInt32Ty() {
349 return Type::getInt32Ty(Context);
352 /// \brief Fetch the type representing a 64-bit integer.
353 IntegerType *getInt64Ty() {
354 return Type::getInt64Ty(Context);
357 /// \brief Fetch the type representing a 128-bit integer.
358 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
360 /// \brief Fetch the type representing an N-bit integer.
361 IntegerType *getIntNTy(unsigned N) {
362 return Type::getIntNTy(Context, N);
365 /// \brief Fetch the type representing a 16-bit floating point value.
367 return Type::getHalfTy(Context);
370 /// \brief Fetch the type representing a 32-bit floating point value.
372 return Type::getFloatTy(Context);
375 /// \brief Fetch the type representing a 64-bit floating point value.
376 Type *getDoubleTy() {
377 return Type::getDoubleTy(Context);
380 /// \brief Fetch the type representing void.
382 return Type::getVoidTy(Context);
385 /// \brief Fetch the type representing a pointer to an 8-bit integer value.
386 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
387 return Type::getInt8PtrTy(Context, AddrSpace);
390 /// \brief Fetch the type representing a pointer to an integer value.
391 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
392 return DL.getIntPtrType(Context, AddrSpace);
395 //===--------------------------------------------------------------------===//
396 // Intrinsic creation methods
397 //===--------------------------------------------------------------------===//
399 /// \brief Create and insert a memset to the specified pointer and the
402 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
403 /// specified, it will be added to the instruction. Likewise with alias.scope
404 /// and noalias tags.
405 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
406 bool isVolatile = false, MDNode *TBAATag = nullptr,
407 MDNode *ScopeTag = nullptr,
408 MDNode *NoAliasTag = nullptr) {
409 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
410 TBAATag, ScopeTag, NoAliasTag);
413 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
414 bool isVolatile = false, MDNode *TBAATag = nullptr,
415 MDNode *ScopeTag = nullptr,
416 MDNode *NoAliasTag = nullptr);
418 /// \brief Create and insert a memcpy between the specified pointers.
420 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
421 /// specified, it will be added to the instruction. Likewise with alias.scope
422 /// and noalias tags.
423 CallInst *CreateMemCpy(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
424 bool isVolatile = false, MDNode *TBAATag = nullptr,
425 MDNode *TBAAStructTag = nullptr,
426 MDNode *ScopeTag = nullptr,
427 MDNode *NoAliasTag = nullptr) {
428 return CreateMemCpy(Dst, Src, getInt64(Size), Align, isVolatile, TBAATag,
429 TBAAStructTag, ScopeTag, NoAliasTag);
432 CallInst *CreateMemCpy(Value *Dst, Value *Src, Value *Size, unsigned Align,
433 bool isVolatile = false, MDNode *TBAATag = nullptr,
434 MDNode *TBAAStructTag = nullptr,
435 MDNode *ScopeTag = nullptr,
436 MDNode *NoAliasTag = nullptr);
438 /// \brief Create and insert an element unordered-atomic memcpy between the
439 /// specified pointers.
441 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
442 /// specified, it will be added to the instruction. Likewise with alias.scope
443 /// and noalias tags.
444 CallInst *CreateElementUnorderedAtomicMemCpy(
445 Value *Dst, Value *Src, uint64_t Size, uint32_t ElementSize,
446 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
447 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
448 return CreateElementUnorderedAtomicMemCpy(
449 Dst, Src, getInt64(Size), ElementSize, TBAATag, TBAAStructTag, ScopeTag,
453 CallInst *CreateElementUnorderedAtomicMemCpy(
454 Value *Dst, Value *Src, Value *Size, uint32_t ElementSize,
455 MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
456 MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
458 /// \brief Create and insert a memmove between the specified
461 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
462 /// specified, it will be added to the instruction. Likewise with alias.scope
463 /// and noalias tags.
464 CallInst *CreateMemMove(Value *Dst, Value *Src, uint64_t Size, unsigned Align,
465 bool isVolatile = false, MDNode *TBAATag = nullptr,
466 MDNode *ScopeTag = nullptr,
467 MDNode *NoAliasTag = nullptr) {
468 return CreateMemMove(Dst, Src, getInt64(Size), Align, isVolatile,
469 TBAATag, ScopeTag, NoAliasTag);
472 CallInst *CreateMemMove(Value *Dst, Value *Src, Value *Size, unsigned Align,
473 bool isVolatile = false, MDNode *TBAATag = nullptr,
474 MDNode *ScopeTag = nullptr,
475 MDNode *NoAliasTag = nullptr);
477 /// \brief Create a vector fadd reduction intrinsic of the source vector.
478 /// The first parameter is a scalar accumulator value for ordered reductions.
479 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
481 /// \brief Create a vector fmul reduction intrinsic of the source vector.
482 /// The first parameter is a scalar accumulator value for ordered reductions.
483 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
485 /// \brief Create a vector int add reduction intrinsic of the source vector.
486 CallInst *CreateAddReduce(Value *Src);
488 /// \brief Create a vector int mul reduction intrinsic of the source vector.
489 CallInst *CreateMulReduce(Value *Src);
491 /// \brief Create a vector int AND reduction intrinsic of the source vector.
492 CallInst *CreateAndReduce(Value *Src);
494 /// \brief Create a vector int OR reduction intrinsic of the source vector.
495 CallInst *CreateOrReduce(Value *Src);
497 /// \brief Create a vector int XOR reduction intrinsic of the source vector.
498 CallInst *CreateXorReduce(Value *Src);
500 /// \brief Create a vector integer max reduction intrinsic of the source
502 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
504 /// \brief Create a vector integer min reduction intrinsic of the source
506 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
508 /// \brief Create a vector float max reduction intrinsic of the source
510 CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
512 /// \brief Create a vector float min reduction intrinsic of the source
514 CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
516 /// \brief Create a lifetime.start intrinsic.
518 /// If the pointer isn't i8* it will be converted.
519 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
521 /// \brief Create a lifetime.end intrinsic.
523 /// If the pointer isn't i8* it will be converted.
524 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
526 /// Create a call to invariant.start intrinsic.
528 /// If the pointer isn't i8* it will be converted.
529 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
531 /// \brief Create a call to Masked Load intrinsic
532 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
533 Value *PassThru = nullptr, const Twine &Name = "");
535 /// \brief Create a call to Masked Store intrinsic
536 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
539 /// \brief Create a call to Masked Gather intrinsic
540 CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
541 Value *Mask = nullptr,
542 Value *PassThru = nullptr,
543 const Twine& Name = "");
545 /// \brief Create a call to Masked Scatter intrinsic
546 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
547 Value *Mask = nullptr);
549 /// \brief Create an assume intrinsic call that allows the optimizer to
550 /// assume that the provided condition will be true.
551 CallInst *CreateAssumption(Value *Cond);
553 /// \brief Create a call to the experimental.gc.statepoint intrinsic to
554 /// start a new statepoint sequence.
555 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
557 ArrayRef<Value *> CallArgs,
558 ArrayRef<Value *> DeoptArgs,
559 ArrayRef<Value *> GCArgs,
560 const Twine &Name = "");
562 /// \brief Create a call to the experimental.gc.statepoint intrinsic to
563 /// start a new statepoint sequence.
564 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
565 Value *ActualCallee, uint32_t Flags,
566 ArrayRef<Use> CallArgs,
567 ArrayRef<Use> TransitionArgs,
568 ArrayRef<Use> DeoptArgs,
569 ArrayRef<Value *> GCArgs,
570 const Twine &Name = "");
572 // \brief Conveninence function for the common case when CallArgs are filled
573 // in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
574 // .get()'ed to get the Value pointer.
575 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
576 Value *ActualCallee, ArrayRef<Use> CallArgs,
577 ArrayRef<Value *> DeoptArgs,
578 ArrayRef<Value *> GCArgs,
579 const Twine &Name = "");
581 /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
582 /// start a new statepoint sequence.
584 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
585 Value *ActualInvokee, BasicBlock *NormalDest,
586 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
587 ArrayRef<Value *> DeoptArgs,
588 ArrayRef<Value *> GCArgs, const Twine &Name = "");
590 /// brief Create an invoke to the experimental.gc.statepoint intrinsic to
591 /// start a new statepoint sequence.
592 InvokeInst *CreateGCStatepointInvoke(
593 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
594 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
595 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
596 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
597 const Twine &Name = "");
599 // Conveninence function for the common case when CallArgs are filled in using
600 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
603 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
604 Value *ActualInvokee, BasicBlock *NormalDest,
605 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
606 ArrayRef<Value *> DeoptArgs,
607 ArrayRef<Value *> GCArgs, const Twine &Name = "");
609 /// \brief Create a call to the experimental.gc.result intrinsic to extract
610 /// the result from a call wrapped in a statepoint.
611 CallInst *CreateGCResult(Instruction *Statepoint,
613 const Twine &Name = "");
615 /// \brief Create a call to the experimental.gc.relocate intrinsics to
616 /// project the relocated value of one pointer from the statepoint.
617 CallInst *CreateGCRelocate(Instruction *Statepoint,
621 const Twine &Name = "");
623 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
625 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID,
626 Value *LHS, Value *RHS,
627 const Twine &Name = "");
629 /// Create call to the minnum intrinsic.
630 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
631 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, Name);
634 /// Create call to the maxnum intrinsic.
635 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
636 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, Name);
640 /// \brief Create a call to a masked intrinsic with given Id.
641 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
642 ArrayRef<Type *> OverloadedTypes,
643 const Twine &Name = "");
645 Value *getCastedInt8PtrValue(Value *Ptr);
648 /// \brief This provides a uniform API for creating instructions and inserting
649 /// them into a basic block: either at the end of a BasicBlock, or at a specific
650 /// iterator location in a block.
652 /// Note that the builder does not expose the full generality of LLVM
653 /// instructions. For access to extra instruction properties, use the mutators
654 /// (e.g. setVolatile) on the instructions after they have been
655 /// created. Convenience state exists to specify fast-math flags and fp-math
658 /// The first template argument specifies a class to use for creating constants.
659 /// This defaults to creating minimally folded constants. The second template
660 /// argument allows clients to specify custom insertion hooks that are called on
661 /// every newly created insertion.
662 template <typename T = ConstantFolder,
663 typename Inserter = IRBuilderDefaultInserter>
664 class IRBuilder : public IRBuilderBase, public Inserter {
668 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
669 MDNode *FPMathTag = nullptr,
670 ArrayRef<OperandBundleDef> OpBundles = None)
671 : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
674 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
675 ArrayRef<OperandBundleDef> OpBundles = None)
676 : IRBuilderBase(C, FPMathTag, OpBundles), Folder() {}
678 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
679 ArrayRef<OperandBundleDef> OpBundles = None)
680 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
681 SetInsertPoint(TheBB);
684 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
685 ArrayRef<OperandBundleDef> OpBundles = None)
686 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() {
687 SetInsertPoint(TheBB);
690 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
691 ArrayRef<OperandBundleDef> OpBundles = None)
692 : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles), Folder() {
696 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
697 MDNode *FPMathTag = nullptr,
698 ArrayRef<OperandBundleDef> OpBundles = None)
699 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
700 SetInsertPoint(TheBB, IP);
703 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
704 MDNode *FPMathTag = nullptr,
705 ArrayRef<OperandBundleDef> OpBundles = None)
706 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder() {
707 SetInsertPoint(TheBB, IP);
710 /// \brief Get the constant folder being used.
711 const T &getFolder() { return Folder; }
713 /// \brief Insert and return the specified instruction.
714 template<typename InstTy>
715 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
716 this->InsertHelper(I, Name, BB, InsertPt);
717 this->SetInstDebugLocation(I);
721 /// \brief No-op overload to handle constants.
722 Constant *Insert(Constant *C, const Twine& = "") const {
726 //===--------------------------------------------------------------------===//
727 // Instruction creation methods: Terminators
728 //===--------------------------------------------------------------------===//
731 /// \brief Helper to add branch weight and unpredictable metadata onto an
733 /// \returns The annotated instruction.
734 template <typename InstTy>
735 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
737 I->setMetadata(LLVMContext::MD_prof, Weights);
739 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
744 /// \brief Create a 'ret void' instruction.
745 ReturnInst *CreateRetVoid() {
746 return Insert(ReturnInst::Create(Context));
749 /// \brief Create a 'ret <val>' instruction.
750 ReturnInst *CreateRet(Value *V) {
751 return Insert(ReturnInst::Create(Context, V));
754 /// \brief Create a sequence of N insertvalue instructions,
755 /// with one Value from the retVals array each, that build a aggregate
756 /// return value one value at a time, and a ret instruction to return
757 /// the resulting aggregate value.
759 /// This is a convenience function for code that uses aggregate return values
760 /// as a vehicle for having multiple return values.
761 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
762 Value *V = UndefValue::get(getCurrentFunctionReturnType());
763 for (unsigned i = 0; i != N; ++i)
764 V = CreateInsertValue(V, retVals[i], i, "mrv");
765 return Insert(ReturnInst::Create(Context, V));
768 /// \brief Create an unconditional 'br label X' instruction.
769 BranchInst *CreateBr(BasicBlock *Dest) {
770 return Insert(BranchInst::Create(Dest));
773 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
775 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
776 MDNode *BranchWeights = nullptr,
777 MDNode *Unpredictable = nullptr) {
778 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
779 BranchWeights, Unpredictable));
782 /// \brief Create a conditional 'br Cond, TrueDest, FalseDest'
783 /// instruction. Copy branch meta data if available.
784 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
785 Instruction *MDSrc) {
786 BranchInst *Br = BranchInst::Create(True, False, Cond);
788 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
789 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
790 Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
795 /// \brief Create a switch instruction with the specified value, default dest,
796 /// and with a hint for the number of cases that will be added (for efficient
798 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
799 MDNode *BranchWeights = nullptr,
800 MDNode *Unpredictable = nullptr) {
801 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
802 BranchWeights, Unpredictable));
805 /// \brief Create an indirect branch instruction with the specified address
806 /// operand, with an optional hint for the number of destinations that will be
807 /// added (for efficient allocation).
808 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
809 return Insert(IndirectBrInst::Create(Addr, NumDests));
812 /// \brief Create an invoke instruction.
813 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
814 BasicBlock *UnwindDest,
815 ArrayRef<Value *> Args = None,
816 const Twine &Name = "") {
817 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args),
820 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
821 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
822 ArrayRef<OperandBundleDef> OpBundles,
823 const Twine &Name = "") {
824 return Insert(InvokeInst::Create(Callee, NormalDest, UnwindDest, Args,
828 ResumeInst *CreateResume(Value *Exn) {
829 return Insert(ResumeInst::Create(Exn));
832 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
833 BasicBlock *UnwindBB = nullptr) {
834 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
837 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
838 unsigned NumHandlers,
839 const Twine &Name = "") {
840 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
844 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
845 const Twine &Name = "") {
846 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
849 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
850 ArrayRef<Value *> Args = None,
851 const Twine &Name = "") {
852 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
855 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
856 return Insert(CatchReturnInst::Create(CatchPad, BB));
859 UnreachableInst *CreateUnreachable() {
860 return Insert(new UnreachableInst(Context));
863 //===--------------------------------------------------------------------===//
864 // Instruction creation methods: Binary Operators
865 //===--------------------------------------------------------------------===//
867 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
868 Value *LHS, Value *RHS,
870 bool HasNUW, bool HasNSW) {
871 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
872 if (HasNUW) BO->setHasNoUnsignedWrap();
873 if (HasNSW) BO->setHasNoSignedWrap();
877 Instruction *AddFPMathAttributes(Instruction *I,
879 FastMathFlags FMF) const {
881 FPMathTag = DefaultFPMathTag;
883 I->setMetadata(LLVMContext::MD_fpmath, FPMathTag);
884 I->setFastMathFlags(FMF);
889 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
890 bool HasNUW = false, bool HasNSW = false) {
891 if (Constant *LC = dyn_cast<Constant>(LHS))
892 if (Constant *RC = dyn_cast<Constant>(RHS))
893 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
894 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
897 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
898 return CreateAdd(LHS, RHS, Name, false, true);
900 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
901 return CreateAdd(LHS, RHS, Name, true, false);
903 Value *CreateFAdd(Value *LHS, Value *RHS, const Twine &Name = "",
904 MDNode *FPMathTag = nullptr) {
905 if (Constant *LC = dyn_cast<Constant>(LHS))
906 if (Constant *RC = dyn_cast<Constant>(RHS))
907 return Insert(Folder.CreateFAdd(LC, RC), Name);
908 return Insert(AddFPMathAttributes(BinaryOperator::CreateFAdd(LHS, RHS),
909 FPMathTag, FMF), Name);
911 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
912 bool HasNUW = false, bool HasNSW = false) {
913 if (Constant *LC = dyn_cast<Constant>(LHS))
914 if (Constant *RC = dyn_cast<Constant>(RHS))
915 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
916 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
919 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
920 return CreateSub(LHS, RHS, Name, false, true);
922 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
923 return CreateSub(LHS, RHS, Name, true, false);
925 Value *CreateFSub(Value *LHS, Value *RHS, const Twine &Name = "",
926 MDNode *FPMathTag = nullptr) {
927 if (Constant *LC = dyn_cast<Constant>(LHS))
928 if (Constant *RC = dyn_cast<Constant>(RHS))
929 return Insert(Folder.CreateFSub(LC, RC), Name);
930 return Insert(AddFPMathAttributes(BinaryOperator::CreateFSub(LHS, RHS),
931 FPMathTag, FMF), Name);
933 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
934 bool HasNUW = false, bool HasNSW = false) {
935 if (Constant *LC = dyn_cast<Constant>(LHS))
936 if (Constant *RC = dyn_cast<Constant>(RHS))
937 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
938 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
941 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
942 return CreateMul(LHS, RHS, Name, false, true);
944 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
945 return CreateMul(LHS, RHS, Name, true, false);
947 Value *CreateFMul(Value *LHS, Value *RHS, const Twine &Name = "",
948 MDNode *FPMathTag = nullptr) {
949 if (Constant *LC = dyn_cast<Constant>(LHS))
950 if (Constant *RC = dyn_cast<Constant>(RHS))
951 return Insert(Folder.CreateFMul(LC, RC), Name);
952 return Insert(AddFPMathAttributes(BinaryOperator::CreateFMul(LHS, RHS),
953 FPMathTag, FMF), Name);
955 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
956 bool isExact = false) {
957 if (Constant *LC = dyn_cast<Constant>(LHS))
958 if (Constant *RC = dyn_cast<Constant>(RHS))
959 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
961 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
962 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
964 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
965 return CreateUDiv(LHS, RHS, Name, true);
967 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
968 bool isExact = false) {
969 if (Constant *LC = dyn_cast<Constant>(LHS))
970 if (Constant *RC = dyn_cast<Constant>(RHS))
971 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
973 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
974 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
976 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
977 return CreateSDiv(LHS, RHS, Name, true);
979 Value *CreateFDiv(Value *LHS, Value *RHS, const Twine &Name = "",
980 MDNode *FPMathTag = nullptr) {
981 if (Constant *LC = dyn_cast<Constant>(LHS))
982 if (Constant *RC = dyn_cast<Constant>(RHS))
983 return Insert(Folder.CreateFDiv(LC, RC), Name);
984 return Insert(AddFPMathAttributes(BinaryOperator::CreateFDiv(LHS, RHS),
985 FPMathTag, FMF), Name);
987 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
988 if (Constant *LC = dyn_cast<Constant>(LHS))
989 if (Constant *RC = dyn_cast<Constant>(RHS))
990 return Insert(Folder.CreateURem(LC, RC), Name);
991 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
993 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
994 if (Constant *LC = dyn_cast<Constant>(LHS))
995 if (Constant *RC = dyn_cast<Constant>(RHS))
996 return Insert(Folder.CreateSRem(LC, RC), Name);
997 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
999 Value *CreateFRem(Value *LHS, Value *RHS, const Twine &Name = "",
1000 MDNode *FPMathTag = nullptr) {
1001 if (Constant *LC = dyn_cast<Constant>(LHS))
1002 if (Constant *RC = dyn_cast<Constant>(RHS))
1003 return Insert(Folder.CreateFRem(LC, RC), Name);
1004 return Insert(AddFPMathAttributes(BinaryOperator::CreateFRem(LHS, RHS),
1005 FPMathTag, FMF), Name);
1008 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1009 bool HasNUW = false, bool HasNSW = false) {
1010 if (Constant *LC = dyn_cast<Constant>(LHS))
1011 if (Constant *RC = dyn_cast<Constant>(RHS))
1012 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1013 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1016 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1017 bool HasNUW = false, bool HasNSW = false) {
1018 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1021 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1022 bool HasNUW = false, bool HasNSW = false) {
1023 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1027 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1028 bool isExact = false) {
1029 if (Constant *LC = dyn_cast<Constant>(LHS))
1030 if (Constant *RC = dyn_cast<Constant>(RHS))
1031 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1033 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1034 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1036 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1037 bool isExact = false) {
1038 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1040 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1041 bool isExact = false) {
1042 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1045 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1046 bool isExact = false) {
1047 if (Constant *LC = dyn_cast<Constant>(LHS))
1048 if (Constant *RC = dyn_cast<Constant>(RHS))
1049 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1051 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1052 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1054 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1055 bool isExact = false) {
1056 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1058 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1059 bool isExact = false) {
1060 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1063 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1064 if (Constant *RC = dyn_cast<Constant>(RHS)) {
1065 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isAllOnesValue())
1066 return LHS; // LHS & -1 -> LHS
1067 if (Constant *LC = dyn_cast<Constant>(LHS))
1068 return Insert(Folder.CreateAnd(LC, RC), Name);
1070 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1072 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1073 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1075 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1076 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1079 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1080 if (Constant *RC = dyn_cast<Constant>(RHS)) {
1081 if (RC->isNullValue())
1082 return LHS; // LHS | 0 -> LHS
1083 if (Constant *LC = dyn_cast<Constant>(LHS))
1084 return Insert(Folder.CreateOr(LC, RC), Name);
1086 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1088 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1089 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1091 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1092 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1095 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1096 if (Constant *LC = dyn_cast<Constant>(LHS))
1097 if (Constant *RC = dyn_cast<Constant>(RHS))
1098 return Insert(Folder.CreateXor(LC, RC), Name);
1099 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1101 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1102 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1104 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1105 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1108 Value *CreateBinOp(Instruction::BinaryOps Opc,
1109 Value *LHS, Value *RHS, const Twine &Name = "",
1110 MDNode *FPMathTag = nullptr) {
1111 if (Constant *LC = dyn_cast<Constant>(LHS))
1112 if (Constant *RC = dyn_cast<Constant>(RHS))
1113 return Insert(Folder.CreateBinOp(Opc, LC, RC), Name);
1114 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1115 if (isa<FPMathOperator>(BinOp))
1116 BinOp = AddFPMathAttributes(BinOp, FPMathTag, FMF);
1117 return Insert(BinOp, Name);
1120 Value *CreateNeg(Value *V, const Twine &Name = "",
1121 bool HasNUW = false, bool HasNSW = false) {
1122 if (Constant *VC = dyn_cast<Constant>(V))
1123 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
1124 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
1125 if (HasNUW) BO->setHasNoUnsignedWrap();
1126 if (HasNSW) BO->setHasNoSignedWrap();
1129 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1130 return CreateNeg(V, Name, false, true);
1132 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1133 return CreateNeg(V, Name, true, false);
1135 Value *CreateFNeg(Value *V, const Twine &Name = "",
1136 MDNode *FPMathTag = nullptr) {
1137 if (Constant *VC = dyn_cast<Constant>(V))
1138 return Insert(Folder.CreateFNeg(VC), Name);
1139 return Insert(AddFPMathAttributes(BinaryOperator::CreateFNeg(V),
1140 FPMathTag, FMF), Name);
1142 Value *CreateNot(Value *V, const Twine &Name = "") {
1143 if (Constant *VC = dyn_cast<Constant>(V))
1144 return Insert(Folder.CreateNot(VC), Name);
1145 return Insert(BinaryOperator::CreateNot(V), Name);
1148 //===--------------------------------------------------------------------===//
1149 // Instruction creation methods: Memory Instructions
1150 //===--------------------------------------------------------------------===//
1152 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1153 Value *ArraySize = nullptr, const Twine &Name = "") {
1154 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
1157 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1158 const Twine &Name = "") {
1159 const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
1160 return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
1162 // \brief Provided to resolve 'CreateLoad(Ptr, "...")' correctly, instead of
1163 // converting the string to 'bool' for the isVolatile parameter.
1164 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1165 return Insert(new LoadInst(Ptr), Name);
1167 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1168 return Insert(new LoadInst(Ptr), Name);
1170 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1171 return Insert(new LoadInst(Ty, Ptr), Name);
1173 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1174 return Insert(new LoadInst(Ptr, nullptr, isVolatile), Name);
1176 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1177 return Insert(new StoreInst(Val, Ptr, isVolatile));
1179 // \brief Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1180 // correctly, instead of converting the string to 'bool' for the isVolatile
1182 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1183 LoadInst *LI = CreateLoad(Ptr, Name);
1184 LI->setAlignment(Align);
1187 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1188 const Twine &Name = "") {
1189 LoadInst *LI = CreateLoad(Ptr, Name);
1190 LI->setAlignment(Align);
1193 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1194 const Twine &Name = "") {
1195 LoadInst *LI = CreateLoad(Ptr, isVolatile, Name);
1196 LI->setAlignment(Align);
1199 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1200 bool isVolatile = false) {
1201 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1202 SI->setAlignment(Align);
1205 FenceInst *CreateFence(AtomicOrdering Ordering,
1206 SynchronizationScope SynchScope = CrossThread,
1207 const Twine &Name = "") {
1208 return Insert(new FenceInst(Context, Ordering, SynchScope), Name);
1211 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1212 AtomicOrdering SuccessOrdering,
1213 AtomicOrdering FailureOrdering,
1214 SynchronizationScope SynchScope = CrossThread) {
1215 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1216 FailureOrdering, SynchScope));
1218 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1219 AtomicOrdering Ordering,
1220 SynchronizationScope SynchScope = CrossThread) {
1221 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SynchScope));
1223 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1224 const Twine &Name = "") {
1225 return CreateGEP(nullptr, Ptr, IdxList, Name);
1227 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1228 const Twine &Name = "") {
1229 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1230 // Every index must be constant.
1232 for (i = 0, e = IdxList.size(); i != e; ++i)
1233 if (!isa<Constant>(IdxList[i]))
1236 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1238 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1240 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1241 const Twine &Name = "") {
1242 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1244 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1245 const Twine &Name = "") {
1246 if (Constant *PC = dyn_cast<Constant>(Ptr)) {
1247 // Every index must be constant.
1249 for (i = 0, e = IdxList.size(); i != e; ++i)
1250 if (!isa<Constant>(IdxList[i]))
1253 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1256 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1258 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1259 return CreateGEP(nullptr, Ptr, Idx, Name);
1261 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1262 if (Constant *PC = dyn_cast<Constant>(Ptr))
1263 if (Constant *IC = dyn_cast<Constant>(Idx))
1264 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1265 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1267 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1268 const Twine &Name = "") {
1269 if (Constant *PC = dyn_cast<Constant>(Ptr))
1270 if (Constant *IC = dyn_cast<Constant>(Idx))
1271 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1272 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1274 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1275 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1277 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1278 const Twine &Name = "") {
1279 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1281 if (Constant *PC = dyn_cast<Constant>(Ptr))
1282 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1284 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1286 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1287 const Twine &Name = "") {
1288 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1290 if (Constant *PC = dyn_cast<Constant>(Ptr))
1291 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1293 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1295 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1296 const Twine &Name = "") {
1298 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1299 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1302 if (Constant *PC = dyn_cast<Constant>(Ptr))
1303 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1305 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1307 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1308 unsigned Idx1, const Twine &Name = "") {
1310 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1311 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1314 if (Constant *PC = dyn_cast<Constant>(Ptr))
1315 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1317 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1319 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1320 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1322 if (Constant *PC = dyn_cast<Constant>(Ptr))
1323 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idx), Name);
1325 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idx), Name);
1327 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1328 const Twine &Name = "") {
1329 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1331 if (Constant *PC = dyn_cast<Constant>(Ptr))
1332 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idx), Name);
1334 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idx), Name);
1336 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1337 const Twine &Name = "") {
1339 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1340 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1343 if (Constant *PC = dyn_cast<Constant>(Ptr))
1344 return Insert(Folder.CreateGetElementPtr(nullptr, PC, Idxs), Name);
1346 return Insert(GetElementPtrInst::Create(nullptr, Ptr, Idxs), Name);
1348 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1349 const Twine &Name = "") {
1351 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1352 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1355 if (Constant *PC = dyn_cast<Constant>(Ptr))
1356 return Insert(Folder.CreateInBoundsGetElementPtr(nullptr, PC, Idxs),
1359 return Insert(GetElementPtrInst::CreateInBounds(nullptr, Ptr, Idxs), Name);
1361 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1362 const Twine &Name = "") {
1363 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1366 /// \brief Same as CreateGlobalString, but return a pointer with "i8*" type
1367 /// instead of a pointer to array of i8.
1368 Value *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1369 unsigned AddressSpace = 0) {
1370 GlobalVariable *gv = CreateGlobalString(Str, Name, AddressSpace);
1371 Value *zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1372 Value *Args[] = { zero, zero };
1373 return CreateInBoundsGEP(gv->getValueType(), gv, Args, Name);
1376 //===--------------------------------------------------------------------===//
1377 // Instruction creation methods: Cast/Conversion Operators
1378 //===--------------------------------------------------------------------===//
1380 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1381 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1383 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1384 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1386 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1387 return CreateCast(Instruction::SExt, V, DestTy, Name);
1389 /// \brief Create a ZExt or Trunc from the integer value V to DestTy. Return
1390 /// the value untouched if the type of V is already DestTy.
1391 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1392 const Twine &Name = "") {
1393 assert(V->getType()->isIntOrIntVectorTy() &&
1394 DestTy->isIntOrIntVectorTy() &&
1395 "Can only zero extend/truncate integers!");
1396 Type *VTy = V->getType();
1397 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1398 return CreateZExt(V, DestTy, Name);
1399 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1400 return CreateTrunc(V, DestTy, Name);
1403 /// \brief Create a SExt or Trunc from the integer value V to DestTy. Return
1404 /// the value untouched if the type of V is already DestTy.
1405 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1406 const Twine &Name = "") {
1407 assert(V->getType()->isIntOrIntVectorTy() &&
1408 DestTy->isIntOrIntVectorTy() &&
1409 "Can only sign extend/truncate integers!");
1410 Type *VTy = V->getType();
1411 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1412 return CreateSExt(V, DestTy, Name);
1413 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1414 return CreateTrunc(V, DestTy, Name);
1417 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = ""){
1418 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1420 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = ""){
1421 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1423 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1424 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1426 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1427 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1429 Value *CreateFPTrunc(Value *V, Type *DestTy,
1430 const Twine &Name = "") {
1431 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1433 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1434 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1436 Value *CreatePtrToInt(Value *V, Type *DestTy,
1437 const Twine &Name = "") {
1438 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1440 Value *CreateIntToPtr(Value *V, Type *DestTy,
1441 const Twine &Name = "") {
1442 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1444 Value *CreateBitCast(Value *V, Type *DestTy,
1445 const Twine &Name = "") {
1446 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1448 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1449 const Twine &Name = "") {
1450 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1452 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1453 const Twine &Name = "") {
1454 if (V->getType() == DestTy)
1456 if (Constant *VC = dyn_cast<Constant>(V))
1457 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1458 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1460 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1461 const Twine &Name = "") {
1462 if (V->getType() == DestTy)
1464 if (Constant *VC = dyn_cast<Constant>(V))
1465 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1466 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1468 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1469 const Twine &Name = "") {
1470 if (V->getType() == DestTy)
1472 if (Constant *VC = dyn_cast<Constant>(V))
1473 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1474 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
1476 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
1477 const Twine &Name = "") {
1478 if (V->getType() == DestTy)
1480 if (Constant *VC = dyn_cast<Constant>(V))
1481 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
1482 return Insert(CastInst::Create(Op, V, DestTy), Name);
1484 Value *CreatePointerCast(Value *V, Type *DestTy,
1485 const Twine &Name = "") {
1486 if (V->getType() == DestTy)
1488 if (Constant *VC = dyn_cast<Constant>(V))
1489 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
1490 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
1493 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
1494 const Twine &Name = "") {
1495 if (V->getType() == DestTy)
1498 if (Constant *VC = dyn_cast<Constant>(V)) {
1499 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
1503 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
1507 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
1508 const Twine &Name = "") {
1509 if (V->getType() == DestTy)
1511 if (Constant *VC = dyn_cast<Constant>(V))
1512 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
1513 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
1516 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
1517 const Twine &Name = "") {
1518 if (V->getType() == DestTy)
1520 if (V->getType()->getScalarType()->isPointerTy() &&
1521 DestTy->getScalarType()->isIntegerTy())
1522 return CreatePtrToInt(V, DestTy, Name);
1523 if (V->getType()->getScalarType()->isIntegerTy() &&
1524 DestTy->getScalarType()->isPointerTy())
1525 return CreateIntToPtr(V, DestTy, Name);
1527 return CreateBitCast(V, DestTy, Name);
1531 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
1532 if (V->getType() == DestTy)
1534 if (Constant *VC = dyn_cast<Constant>(V))
1535 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
1536 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
1539 // \brief Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
1540 // compile time error, instead of converting the string to bool for the
1541 // isSigned parameter.
1542 Value *CreateIntCast(Value *, Type *, const char *) = delete;
1544 //===--------------------------------------------------------------------===//
1545 // Instruction creation methods: Compare Instructions
1546 //===--------------------------------------------------------------------===//
1548 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
1549 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
1551 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
1552 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
1554 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1555 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
1557 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1558 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
1560 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
1561 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
1563 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
1564 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
1566 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
1567 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
1569 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
1570 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
1572 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
1573 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
1575 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
1576 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
1579 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1580 MDNode *FPMathTag = nullptr) {
1581 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
1583 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
1584 MDNode *FPMathTag = nullptr) {
1585 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
1587 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
1588 MDNode *FPMathTag = nullptr) {
1589 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
1591 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
1592 MDNode *FPMathTag = nullptr) {
1593 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
1595 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
1596 MDNode *FPMathTag = nullptr) {
1597 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
1599 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
1600 MDNode *FPMathTag = nullptr) {
1601 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
1603 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
1604 MDNode *FPMathTag = nullptr) {
1605 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
1607 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
1608 MDNode *FPMathTag = nullptr) {
1609 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
1611 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
1612 MDNode *FPMathTag = nullptr) {
1613 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
1615 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
1616 MDNode *FPMathTag = nullptr) {
1617 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
1619 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
1620 MDNode *FPMathTag = nullptr) {
1621 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
1623 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
1624 MDNode *FPMathTag = nullptr) {
1625 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
1627 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
1628 MDNode *FPMathTag = nullptr) {
1629 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
1631 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
1632 MDNode *FPMathTag = nullptr) {
1633 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
1636 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1637 const Twine &Name = "") {
1638 if (Constant *LC = dyn_cast<Constant>(LHS))
1639 if (Constant *RC = dyn_cast<Constant>(RHS))
1640 return Insert(Folder.CreateICmp(P, LC, RC), Name);
1641 return Insert(new ICmpInst(P, LHS, RHS), Name);
1643 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
1644 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1645 if (Constant *LC = dyn_cast<Constant>(LHS))
1646 if (Constant *RC = dyn_cast<Constant>(RHS))
1647 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
1648 return Insert(AddFPMathAttributes(new FCmpInst(P, LHS, RHS),
1649 FPMathTag, FMF), Name);
1652 //===--------------------------------------------------------------------===//
1653 // Instruction creation methods: Other Instructions
1654 //===--------------------------------------------------------------------===//
1656 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
1657 const Twine &Name = "") {
1658 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
1661 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
1662 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1663 PointerType *PTy = cast<PointerType>(Callee->getType());
1664 FunctionType *FTy = cast<FunctionType>(PTy->getElementType());
1665 return CreateCall(FTy, Callee, Args, Name, FPMathTag);
1668 CallInst *CreateCall(FunctionType *FTy, Value *Callee,
1669 ArrayRef<Value *> Args, const Twine &Name = "",
1670 MDNode *FPMathTag = nullptr) {
1671 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
1672 if (isa<FPMathOperator>(CI))
1673 CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF));
1674 return Insert(CI, Name);
1677 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
1678 ArrayRef<OperandBundleDef> OpBundles,
1679 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1680 CallInst *CI = CallInst::Create(Callee, Args, OpBundles);
1681 if (isa<FPMathOperator>(CI))
1682 CI = cast<CallInst>(AddFPMathAttributes(CI, FPMathTag, FMF));
1683 return Insert(CI, Name);
1686 CallInst *CreateCall(Function *Callee, ArrayRef<Value *> Args,
1687 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
1688 return CreateCall(Callee->getFunctionType(), Callee, Args, Name, FPMathTag);
1691 Value *CreateSelect(Value *C, Value *True, Value *False,
1692 const Twine &Name = "", Instruction *MDFrom = nullptr) {
1693 if (Constant *CC = dyn_cast<Constant>(C))
1694 if (Constant *TC = dyn_cast<Constant>(True))
1695 if (Constant *FC = dyn_cast<Constant>(False))
1696 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
1698 SelectInst *Sel = SelectInst::Create(C, True, False);
1700 MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
1701 MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
1702 Sel = addBranchMetadata(Sel, Prof, Unpred);
1704 return Insert(Sel, Name);
1707 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
1708 return Insert(new VAArgInst(List, Ty), Name);
1711 Value *CreateExtractElement(Value *Vec, Value *Idx,
1712 const Twine &Name = "") {
1713 if (Constant *VC = dyn_cast<Constant>(Vec))
1714 if (Constant *IC = dyn_cast<Constant>(Idx))
1715 return Insert(Folder.CreateExtractElement(VC, IC), Name);
1716 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
1719 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
1720 const Twine &Name = "") {
1721 return CreateExtractElement(Vec, getInt64(Idx), Name);
1724 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
1725 const Twine &Name = "") {
1726 if (Constant *VC = dyn_cast<Constant>(Vec))
1727 if (Constant *NC = dyn_cast<Constant>(NewElt))
1728 if (Constant *IC = dyn_cast<Constant>(Idx))
1729 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
1730 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
1733 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
1734 const Twine &Name = "") {
1735 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
1738 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
1739 const Twine &Name = "") {
1740 if (Constant *V1C = dyn_cast<Constant>(V1))
1741 if (Constant *V2C = dyn_cast<Constant>(V2))
1742 if (Constant *MC = dyn_cast<Constant>(Mask))
1743 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
1744 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
1747 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask,
1748 const Twine &Name = "") {
1749 Value *Mask = ConstantDataVector::get(Context, IntMask);
1750 return CreateShuffleVector(V1, V2, Mask, Name);
1753 Value *CreateExtractValue(Value *Agg,
1754 ArrayRef<unsigned> Idxs,
1755 const Twine &Name = "") {
1756 if (Constant *AggC = dyn_cast<Constant>(Agg))
1757 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
1758 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
1761 Value *CreateInsertValue(Value *Agg, Value *Val,
1762 ArrayRef<unsigned> Idxs,
1763 const Twine &Name = "") {
1764 if (Constant *AggC = dyn_cast<Constant>(Agg))
1765 if (Constant *ValC = dyn_cast<Constant>(Val))
1766 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
1767 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
1770 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
1771 const Twine &Name = "") {
1772 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
1775 //===--------------------------------------------------------------------===//
1776 // Utility creation methods
1777 //===--------------------------------------------------------------------===//
1779 /// \brief Return an i1 value testing if \p Arg is null.
1780 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
1781 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
1785 /// \brief Return an i1 value testing if \p Arg is not null.
1786 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
1787 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
1791 /// \brief Return the i64 difference between two pointer values, dividing out
1792 /// the size of the pointed-to objects.
1794 /// This is intended to implement C-style pointer subtraction. As such, the
1795 /// pointers must be appropriately aligned for their element types and
1796 /// pointing into the same object.
1797 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
1798 assert(LHS->getType() == RHS->getType() &&
1799 "Pointer subtraction operand types must match!");
1800 PointerType *ArgType = cast<PointerType>(LHS->getType());
1801 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
1802 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
1803 Value *Difference = CreateSub(LHS_int, RHS_int);
1804 return CreateExactSDiv(Difference,
1805 ConstantExpr::getSizeOf(ArgType->getElementType()),
1809 /// \brief Create an invariant.group.barrier intrinsic call, that stops
1810 /// optimizer to propagate equality using invariant.group metadata.
1811 /// If Ptr type is different from i8*, it's casted to i8* before call
1812 /// and casted back to Ptr type after call.
1813 Value *CreateInvariantGroupBarrier(Value *Ptr) {
1814 Module *M = BB->getParent()->getParent();
1815 Function *FnInvariantGroupBarrier = Intrinsic::getDeclaration(M,
1816 Intrinsic::invariant_group_barrier);
1818 Type *ArgumentAndReturnType = FnInvariantGroupBarrier->getReturnType();
1819 assert(ArgumentAndReturnType ==
1820 FnInvariantGroupBarrier->getFunctionType()->getParamType(0) &&
1821 "InvariantGroupBarrier should take and return the same type");
1822 Type *PtrType = Ptr->getType();
1824 bool PtrTypeConversionNeeded = PtrType != ArgumentAndReturnType;
1825 if (PtrTypeConversionNeeded)
1826 Ptr = CreateBitCast(Ptr, ArgumentAndReturnType);
1828 CallInst *Fn = CreateCall(FnInvariantGroupBarrier, {Ptr});
1830 if (PtrTypeConversionNeeded)
1831 return CreateBitCast(Fn, PtrType);
1835 /// \brief Return a vector value that contains \arg V broadcasted to \p
1836 /// NumElts elements.
1837 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
1838 assert(NumElts > 0 && "Cannot splat to an empty vector!");
1840 // First insert it into an undef vector so we can shuffle it.
1841 Type *I32Ty = getInt32Ty();
1842 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
1843 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
1844 Name + ".splatinsert");
1846 // Shuffle the value across the desired number of elements.
1847 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
1848 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
1851 /// \brief Return a value that has been extracted from a larger integer type.
1852 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
1853 IntegerType *ExtractedTy, uint64_t Offset,
1854 const Twine &Name) {
1855 IntegerType *IntTy = cast<IntegerType>(From->getType());
1856 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
1857 DL.getTypeStoreSize(IntTy) &&
1858 "Element extends past full value");
1859 uint64_t ShAmt = 8 * Offset;
1861 if (DL.isBigEndian())
1862 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
1863 DL.getTypeStoreSize(ExtractedTy) - Offset);
1865 V = CreateLShr(V, ShAmt, Name + ".shift");
1867 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
1868 "Cannot extract to a larger integer!");
1869 if (ExtractedTy != IntTy) {
1870 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
1876 /// \brief Helper function that creates an assume intrinsic call that
1877 /// represents an alignment assumption on the provided Ptr, Mask, Type
1879 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
1880 Value *PtrValue, Value *Mask,
1882 Value *OffsetValue) {
1883 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
1886 bool IsOffsetZero = false;
1887 if (ConstantInt *CI = dyn_cast<ConstantInt>(OffsetValue))
1888 IsOffsetZero = CI->isZero();
1890 if (!IsOffsetZero) {
1891 if (OffsetValue->getType() != IntPtrTy)
1892 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
1894 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
1898 Value *Zero = ConstantInt::get(IntPtrTy, 0);
1899 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
1900 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
1901 return CreateAssumption(InvCond);
1905 /// \brief Create an assume intrinsic call that represents an alignment
1906 /// assumption on the provided pointer.
1908 /// An optional offset can be provided, and if it is provided, the offset
1909 /// must be subtracted from the provided pointer to get the pointer with the
1910 /// specified alignment.
1911 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1913 Value *OffsetValue = nullptr) {
1914 assert(isa<PointerType>(PtrValue->getType()) &&
1915 "trying to create an alignment assumption on a non-pointer?");
1916 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1917 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
1919 Value *Mask = ConstantInt::get(IntPtrTy, Alignment > 0 ? Alignment - 1 : 0);
1920 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
1924 /// \brief Create an assume intrinsic call that represents an alignment
1925 /// assumption on the provided pointer.
1927 /// An optional offset can be provided, and if it is provided, the offset
1928 /// must be subtracted from the provided pointer to get the pointer with the
1929 /// specified alignment.
1931 /// This overload handles the condition where the Alignment is dependent
1932 /// on an existing value rather than a static value.
1933 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
1935 Value *OffsetValue = nullptr) {
1936 assert(isa<PointerType>(PtrValue->getType()) &&
1937 "trying to create an alignment assumption on a non-pointer?");
1938 PointerType *PtrTy = cast<PointerType>(PtrValue->getType());
1939 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
1941 if (Alignment->getType() != IntPtrTy)
1942 Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ true,
1945 CreateICmp(CmpInst::ICMP_SGT, Alignment,
1946 ConstantInt::get(Alignment->getType(), 0), "ispositive");
1947 Value *PositiveMask =
1948 CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "positivemask");
1949 Value *Mask = CreateSelect(IsPositive, PositiveMask,
1950 ConstantInt::get(IntPtrTy, 0), "mask");
1952 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
1957 // Create wrappers for C Binding types (see CBindingWrapping.h).
1958 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
1960 } // end namespace llvm
1962 #endif // LLVM_IR_IRBUILDER_H