[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / include / llvm / IR / IRBuilder.h
blobe5dcea1d4c0e9e30acf16f71c8f935c05904e5c2
1 //===- llvm/IRBuilder.h - Builder for LLVM Instructions ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the IRBuilder class, which is used as a convenient way
10 // to create LLVM instructions with a consistent and simplified interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_IRBUILDER_H
15 #define LLVM_IR_IRBUILDER_H
17 #include "llvm-c/Types.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constant.h"
24 #include "llvm/IR/ConstantFolder.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DataLayout.h"
27 #include "llvm/IR/DebugLoc.h"
28 #include "llvm/IR/DerivedTypes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/IR/GlobalVariable.h"
31 #include "llvm/IR/InstrTypes.h"
32 #include "llvm/IR/Instruction.h"
33 #include "llvm/IR/Instructions.h"
34 #include "llvm/IR/IntrinsicInst.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/Operator.h"
38 #include "llvm/IR/Type.h"
39 #include "llvm/IR/Value.h"
40 #include "llvm/IR/ValueHandle.h"
41 #include "llvm/Support/AtomicOrdering.h"
42 #include "llvm/Support/CBindingWrapping.h"
43 #include "llvm/Support/Casting.h"
44 #include <cassert>
45 #include <cstddef>
46 #include <cstdint>
47 #include <functional>
48 #include <utility>
50 namespace llvm {
52 class APInt;
53 class MDNode;
54 class Use;
56 /// This provides the default implementation of the IRBuilder
57 /// 'InsertHelper' method that is called whenever an instruction is created by
58 /// IRBuilder and needs to be inserted.
59 ///
60 /// By default, this inserts the instruction at the insertion point.
61 class IRBuilderDefaultInserter {
62 protected:
63 void InsertHelper(Instruction *I, const Twine &Name,
64 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
65 if (BB) BB->getInstList().insert(InsertPt, I);
66 I->setName(Name);
70 /// Provides an 'InsertHelper' that calls a user-provided callback after
71 /// performing the default insertion.
72 class IRBuilderCallbackInserter : IRBuilderDefaultInserter {
73 std::function<void(Instruction *)> Callback;
75 public:
76 IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
77 : Callback(std::move(Callback)) {}
79 protected:
80 void InsertHelper(Instruction *I, const Twine &Name,
81 BasicBlock *BB, BasicBlock::iterator InsertPt) const {
82 IRBuilderDefaultInserter::InsertHelper(I, Name, BB, InsertPt);
83 Callback(I);
87 /// Common base class shared among various IRBuilders.
88 class IRBuilderBase {
89 DebugLoc CurDbgLocation;
91 protected:
92 BasicBlock *BB;
93 BasicBlock::iterator InsertPt;
94 LLVMContext &Context;
96 MDNode *DefaultFPMathTag;
97 FastMathFlags FMF;
99 bool IsFPConstrained;
100 ConstrainedFPIntrinsic::ExceptionBehavior DefaultConstrainedExcept;
101 ConstrainedFPIntrinsic::RoundingMode DefaultConstrainedRounding;
103 ArrayRef<OperandBundleDef> DefaultOperandBundles;
105 public:
106 IRBuilderBase(LLVMContext &context, MDNode *FPMathTag = nullptr,
107 ArrayRef<OperandBundleDef> OpBundles = None)
108 : Context(context), DefaultFPMathTag(FPMathTag), IsFPConstrained(false),
109 DefaultConstrainedExcept(ConstrainedFPIntrinsic::ebStrict),
110 DefaultConstrainedRounding(ConstrainedFPIntrinsic::rmDynamic),
111 DefaultOperandBundles(OpBundles) {
112 ClearInsertionPoint();
115 //===--------------------------------------------------------------------===//
116 // Builder configuration methods
117 //===--------------------------------------------------------------------===//
119 /// Clear the insertion point: created instructions will not be
120 /// inserted into a block.
121 void ClearInsertionPoint() {
122 BB = nullptr;
123 InsertPt = BasicBlock::iterator();
126 BasicBlock *GetInsertBlock() const { return BB; }
127 BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
128 LLVMContext &getContext() const { return Context; }
130 /// This specifies that created instructions should be appended to the
131 /// end of the specified block.
132 void SetInsertPoint(BasicBlock *TheBB) {
133 BB = TheBB;
134 InsertPt = BB->end();
137 /// This specifies that created instructions should be inserted before
138 /// the specified instruction.
139 void SetInsertPoint(Instruction *I) {
140 BB = I->getParent();
141 InsertPt = I->getIterator();
142 assert(InsertPt != BB->end() && "Can't read debug loc from end()");
143 SetCurrentDebugLocation(I->getDebugLoc());
146 /// This specifies that created instructions should be inserted at the
147 /// specified point.
148 void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
149 BB = TheBB;
150 InsertPt = IP;
151 if (IP != TheBB->end())
152 SetCurrentDebugLocation(IP->getDebugLoc());
155 /// Set location information used by debugging information.
156 void SetCurrentDebugLocation(DebugLoc L) { CurDbgLocation = std::move(L); }
158 /// Get location information used by debugging information.
159 const DebugLoc &getCurrentDebugLocation() const { return CurDbgLocation; }
161 /// If this builder has a current debug location, set it on the
162 /// specified instruction.
163 void SetInstDebugLocation(Instruction *I) const {
164 if (CurDbgLocation)
165 I->setDebugLoc(CurDbgLocation);
168 /// Get the return type of the current function that we're emitting
169 /// into.
170 Type *getCurrentFunctionReturnType() const;
172 /// InsertPoint - A saved insertion point.
173 class InsertPoint {
174 BasicBlock *Block = nullptr;
175 BasicBlock::iterator Point;
177 public:
178 /// Creates a new insertion point which doesn't point to anything.
179 InsertPoint() = default;
181 /// Creates a new insertion point at the given location.
182 InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
183 : Block(InsertBlock), Point(InsertPoint) {}
185 /// Returns true if this insert point is set.
186 bool isSet() const { return (Block != nullptr); }
188 BasicBlock *getBlock() const { return Block; }
189 BasicBlock::iterator getPoint() const { return Point; }
192 /// Returns the current insert point.
193 InsertPoint saveIP() const {
194 return InsertPoint(GetInsertBlock(), GetInsertPoint());
197 /// Returns the current insert point, clearing it in the process.
198 InsertPoint saveAndClearIP() {
199 InsertPoint IP(GetInsertBlock(), GetInsertPoint());
200 ClearInsertionPoint();
201 return IP;
204 /// Sets the current insert point to a previously-saved location.
205 void restoreIP(InsertPoint IP) {
206 if (IP.isSet())
207 SetInsertPoint(IP.getBlock(), IP.getPoint());
208 else
209 ClearInsertionPoint();
212 /// Get the floating point math metadata being used.
213 MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
215 /// Get the flags to be applied to created floating point ops
216 FastMathFlags getFastMathFlags() const { return FMF; }
218 /// Clear the fast-math flags.
219 void clearFastMathFlags() { FMF.clear(); }
221 /// Set the floating point math metadata to be used.
222 void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
224 /// Set the fast-math flags to be used with generated fp-math operators
225 void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
227 /// Enable/Disable use of constrained floating point math. When
228 /// enabled the CreateF<op>() calls instead create constrained
229 /// floating point intrinsic calls. Fast math flags are unaffected
230 /// by this setting.
231 void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
233 /// Query for the use of constrained floating point math
234 bool getIsFPConstrained() { return IsFPConstrained; }
236 /// Set the exception handling to be used with constrained floating point
237 void setDefaultConstrainedExcept(
238 ConstrainedFPIntrinsic::ExceptionBehavior NewExcept) {
239 DefaultConstrainedExcept = NewExcept;
242 /// Set the rounding mode handling to be used with constrained floating point
243 void setDefaultConstrainedRounding(
244 ConstrainedFPIntrinsic::RoundingMode NewRounding) {
245 DefaultConstrainedRounding = NewRounding;
248 /// Get the exception handling used with constrained floating point
249 ConstrainedFPIntrinsic::ExceptionBehavior getDefaultConstrainedExcept() {
250 return DefaultConstrainedExcept;
253 /// Get the rounding mode handling used with constrained floating point
254 ConstrainedFPIntrinsic::RoundingMode getDefaultConstrainedRounding() {
255 return DefaultConstrainedRounding;
258 //===--------------------------------------------------------------------===//
259 // RAII helpers.
260 //===--------------------------------------------------------------------===//
262 // RAII object that stores the current insertion point and restores it
263 // when the object is destroyed. This includes the debug location.
264 class InsertPointGuard {
265 IRBuilderBase &Builder;
266 AssertingVH<BasicBlock> Block;
267 BasicBlock::iterator Point;
268 DebugLoc DbgLoc;
270 public:
271 InsertPointGuard(IRBuilderBase &B)
272 : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
273 DbgLoc(B.getCurrentDebugLocation()) {}
275 InsertPointGuard(const InsertPointGuard &) = delete;
276 InsertPointGuard &operator=(const InsertPointGuard &) = delete;
278 ~InsertPointGuard() {
279 Builder.restoreIP(InsertPoint(Block, Point));
280 Builder.SetCurrentDebugLocation(DbgLoc);
284 // RAII object that stores the current fast math settings and restores
285 // them when the object is destroyed.
286 class FastMathFlagGuard {
287 IRBuilderBase &Builder;
288 FastMathFlags FMF;
289 MDNode *FPMathTag;
291 public:
292 FastMathFlagGuard(IRBuilderBase &B)
293 : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag) {}
295 FastMathFlagGuard(const FastMathFlagGuard &) = delete;
296 FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
298 ~FastMathFlagGuard() {
299 Builder.FMF = FMF;
300 Builder.DefaultFPMathTag = FPMathTag;
304 //===--------------------------------------------------------------------===//
305 // Miscellaneous creation methods.
306 //===--------------------------------------------------------------------===//
308 /// Make a new global variable with initializer type i8*
310 /// Make a new global variable with an initializer that has array of i8 type
311 /// filled in with the null terminated string value specified. The new global
312 /// variable will be marked mergable with any others of the same contents. If
313 /// Name is specified, it is the name of the global variable created.
314 GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
315 unsigned AddressSpace = 0);
317 /// Get a constant value representing either true or false.
318 ConstantInt *getInt1(bool V) {
319 return ConstantInt::get(getInt1Ty(), V);
322 /// Get the constant value for i1 true.
323 ConstantInt *getTrue() {
324 return ConstantInt::getTrue(Context);
327 /// Get the constant value for i1 false.
328 ConstantInt *getFalse() {
329 return ConstantInt::getFalse(Context);
332 /// Get a constant 8-bit value.
333 ConstantInt *getInt8(uint8_t C) {
334 return ConstantInt::get(getInt8Ty(), C);
337 /// Get a constant 16-bit value.
338 ConstantInt *getInt16(uint16_t C) {
339 return ConstantInt::get(getInt16Ty(), C);
342 /// Get a constant 32-bit value.
343 ConstantInt *getInt32(uint32_t C) {
344 return ConstantInt::get(getInt32Ty(), C);
347 /// Get a constant 64-bit value.
348 ConstantInt *getInt64(uint64_t C) {
349 return ConstantInt::get(getInt64Ty(), C);
352 /// Get a constant N-bit value, zero extended or truncated from
353 /// a 64-bit value.
354 ConstantInt *getIntN(unsigned N, uint64_t C) {
355 return ConstantInt::get(getIntNTy(N), C);
358 /// Get a constant integer value.
359 ConstantInt *getInt(const APInt &AI) {
360 return ConstantInt::get(Context, AI);
363 //===--------------------------------------------------------------------===//
364 // Type creation methods
365 //===--------------------------------------------------------------------===//
367 /// Fetch the type representing a single bit
368 IntegerType *getInt1Ty() {
369 return Type::getInt1Ty(Context);
372 /// Fetch the type representing an 8-bit integer.
373 IntegerType *getInt8Ty() {
374 return Type::getInt8Ty(Context);
377 /// Fetch the type representing a 16-bit integer.
378 IntegerType *getInt16Ty() {
379 return Type::getInt16Ty(Context);
382 /// Fetch the type representing a 32-bit integer.
383 IntegerType *getInt32Ty() {
384 return Type::getInt32Ty(Context);
387 /// Fetch the type representing a 64-bit integer.
388 IntegerType *getInt64Ty() {
389 return Type::getInt64Ty(Context);
392 /// Fetch the type representing a 128-bit integer.
393 IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
395 /// Fetch the type representing an N-bit integer.
396 IntegerType *getIntNTy(unsigned N) {
397 return Type::getIntNTy(Context, N);
400 /// Fetch the type representing a 16-bit floating point value.
401 Type *getHalfTy() {
402 return Type::getHalfTy(Context);
405 /// Fetch the type representing a 32-bit floating point value.
406 Type *getFloatTy() {
407 return Type::getFloatTy(Context);
410 /// Fetch the type representing a 64-bit floating point value.
411 Type *getDoubleTy() {
412 return Type::getDoubleTy(Context);
415 /// Fetch the type representing void.
416 Type *getVoidTy() {
417 return Type::getVoidTy(Context);
420 /// Fetch the type representing a pointer to an 8-bit integer value.
421 PointerType *getInt8PtrTy(unsigned AddrSpace = 0) {
422 return Type::getInt8PtrTy(Context, AddrSpace);
425 /// Fetch the type representing a pointer to an integer value.
426 IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
427 return DL.getIntPtrType(Context, AddrSpace);
430 //===--------------------------------------------------------------------===//
431 // Intrinsic creation methods
432 //===--------------------------------------------------------------------===//
434 /// Create and insert a memset to the specified pointer and the
435 /// specified value.
437 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
438 /// specified, it will be added to the instruction. Likewise with alias.scope
439 /// and noalias tags.
440 CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, unsigned Align,
441 bool isVolatile = false, MDNode *TBAATag = nullptr,
442 MDNode *ScopeTag = nullptr,
443 MDNode *NoAliasTag = nullptr) {
444 return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
445 TBAATag, ScopeTag, NoAliasTag);
448 CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, unsigned Align,
449 bool isVolatile = false, MDNode *TBAATag = nullptr,
450 MDNode *ScopeTag = nullptr,
451 MDNode *NoAliasTag = nullptr);
453 /// Create and insert an element unordered-atomic memset of the region of
454 /// memory starting at the given pointer to the given value.
456 /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
457 /// specified, it will be added to the instruction. Likewise with alias.scope
458 /// and noalias tags.
459 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
460 uint64_t Size, unsigned Align,
461 uint32_t ElementSize,
462 MDNode *TBAATag = nullptr,
463 MDNode *ScopeTag = nullptr,
464 MDNode *NoAliasTag = nullptr) {
465 return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size), Align,
466 ElementSize, TBAATag, ScopeTag,
467 NoAliasTag);
470 CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
471 Value *Size, unsigned Align,
472 uint32_t ElementSize,
473 MDNode *TBAATag = nullptr,
474 MDNode *ScopeTag = nullptr,
475 MDNode *NoAliasTag = nullptr);
477 /// Create and insert a memcpy between the specified pointers.
479 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
480 /// specified, it will be added to the instruction. Likewise with alias.scope
481 /// and noalias tags.
482 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
483 unsigned SrcAlign, uint64_t Size,
484 bool isVolatile = false, MDNode *TBAATag = nullptr,
485 MDNode *TBAAStructTag = nullptr,
486 MDNode *ScopeTag = nullptr,
487 MDNode *NoAliasTag = nullptr) {
488 return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
489 isVolatile, TBAATag, TBAAStructTag, ScopeTag,
490 NoAliasTag);
493 CallInst *CreateMemCpy(Value *Dst, unsigned DstAlign, Value *Src,
494 unsigned SrcAlign, Value *Size,
495 bool isVolatile = false, MDNode *TBAATag = nullptr,
496 MDNode *TBAAStructTag = nullptr,
497 MDNode *ScopeTag = nullptr,
498 MDNode *NoAliasTag = nullptr);
500 /// Create and insert an element unordered-atomic memcpy between the
501 /// specified pointers.
503 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
505 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
506 /// specified, it will be added to the instruction. Likewise with alias.scope
507 /// and noalias tags.
508 CallInst *CreateElementUnorderedAtomicMemCpy(
509 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
510 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
511 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
512 MDNode *NoAliasTag = nullptr) {
513 return CreateElementUnorderedAtomicMemCpy(
514 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
515 TBAAStructTag, ScopeTag, NoAliasTag);
518 CallInst *CreateElementUnorderedAtomicMemCpy(
519 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
520 uint32_t ElementSize, MDNode *TBAATag = nullptr,
521 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
522 MDNode *NoAliasTag = nullptr);
524 /// Create and insert a memmove between the specified
525 /// pointers.
527 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
528 /// specified, it will be added to the instruction. Likewise with alias.scope
529 /// and noalias tags.
530 CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
531 uint64_t Size, bool isVolatile = false,
532 MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
533 MDNode *NoAliasTag = nullptr) {
534 return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size), isVolatile,
535 TBAATag, ScopeTag, NoAliasTag);
538 CallInst *CreateMemMove(Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
539 Value *Size, bool isVolatile = false, MDNode *TBAATag = nullptr,
540 MDNode *ScopeTag = nullptr,
541 MDNode *NoAliasTag = nullptr);
543 /// \brief Create and insert an element unordered-atomic memmove between the
544 /// specified pointers.
546 /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
547 /// respectively.
549 /// If the pointers aren't i8*, they will be converted. If a TBAA tag is
550 /// specified, it will be added to the instruction. Likewise with alias.scope
551 /// and noalias tags.
552 CallInst *CreateElementUnorderedAtomicMemMove(
553 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign,
554 uint64_t Size, uint32_t ElementSize, MDNode *TBAATag = nullptr,
555 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
556 MDNode *NoAliasTag = nullptr) {
557 return CreateElementUnorderedAtomicMemMove(
558 Dst, DstAlign, Src, SrcAlign, getInt64(Size), ElementSize, TBAATag,
559 TBAAStructTag, ScopeTag, NoAliasTag);
562 CallInst *CreateElementUnorderedAtomicMemMove(
563 Value *Dst, unsigned DstAlign, Value *Src, unsigned SrcAlign, Value *Size,
564 uint32_t ElementSize, MDNode *TBAATag = nullptr,
565 MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
566 MDNode *NoAliasTag = nullptr);
568 /// Create a vector fadd reduction intrinsic of the source vector.
569 /// The first parameter is a scalar accumulator value for ordered reductions.
570 CallInst *CreateFAddReduce(Value *Acc, Value *Src);
572 /// Create a vector fmul reduction intrinsic of the source vector.
573 /// The first parameter is a scalar accumulator value for ordered reductions.
574 CallInst *CreateFMulReduce(Value *Acc, Value *Src);
576 /// Create a vector int add reduction intrinsic of the source vector.
577 CallInst *CreateAddReduce(Value *Src);
579 /// Create a vector int mul reduction intrinsic of the source vector.
580 CallInst *CreateMulReduce(Value *Src);
582 /// Create a vector int AND reduction intrinsic of the source vector.
583 CallInst *CreateAndReduce(Value *Src);
585 /// Create a vector int OR reduction intrinsic of the source vector.
586 CallInst *CreateOrReduce(Value *Src);
588 /// Create a vector int XOR reduction intrinsic of the source vector.
589 CallInst *CreateXorReduce(Value *Src);
591 /// Create a vector integer max reduction intrinsic of the source
592 /// vector.
593 CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
595 /// Create a vector integer min reduction intrinsic of the source
596 /// vector.
597 CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
599 /// Create a vector float max reduction intrinsic of the source
600 /// vector.
601 CallInst *CreateFPMaxReduce(Value *Src, bool NoNaN = false);
603 /// Create a vector float min reduction intrinsic of the source
604 /// vector.
605 CallInst *CreateFPMinReduce(Value *Src, bool NoNaN = false);
607 /// Create a lifetime.start intrinsic.
609 /// If the pointer isn't i8* it will be converted.
610 CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
612 /// Create a lifetime.end intrinsic.
614 /// If the pointer isn't i8* it will be converted.
615 CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
617 /// Create a call to invariant.start intrinsic.
619 /// If the pointer isn't i8* it will be converted.
620 CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
622 /// Create a call to Masked Load intrinsic
623 CallInst *CreateMaskedLoad(Value *Ptr, unsigned Align, Value *Mask,
624 Value *PassThru = nullptr, const Twine &Name = "");
626 /// Create a call to Masked Store intrinsic
627 CallInst *CreateMaskedStore(Value *Val, Value *Ptr, unsigned Align,
628 Value *Mask);
630 /// Create a call to Masked Gather intrinsic
631 CallInst *CreateMaskedGather(Value *Ptrs, unsigned Align,
632 Value *Mask = nullptr,
633 Value *PassThru = nullptr,
634 const Twine& Name = "");
636 /// Create a call to Masked Scatter intrinsic
637 CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, unsigned Align,
638 Value *Mask = nullptr);
640 /// Create an assume intrinsic call that allows the optimizer to
641 /// assume that the provided condition will be true.
642 CallInst *CreateAssumption(Value *Cond);
644 /// Create a call to the experimental.gc.statepoint intrinsic to
645 /// start a new statepoint sequence.
646 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
647 Value *ActualCallee,
648 ArrayRef<Value *> CallArgs,
649 ArrayRef<Value *> DeoptArgs,
650 ArrayRef<Value *> GCArgs,
651 const Twine &Name = "");
653 /// Create a call to the experimental.gc.statepoint intrinsic to
654 /// start a new statepoint sequence.
655 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
656 Value *ActualCallee, uint32_t Flags,
657 ArrayRef<Use> CallArgs,
658 ArrayRef<Use> TransitionArgs,
659 ArrayRef<Use> DeoptArgs,
660 ArrayRef<Value *> GCArgs,
661 const Twine &Name = "");
663 /// Conveninence function for the common case when CallArgs are filled
664 /// in using makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
665 /// .get()'ed to get the Value pointer.
666 CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
667 Value *ActualCallee, ArrayRef<Use> CallArgs,
668 ArrayRef<Value *> DeoptArgs,
669 ArrayRef<Value *> GCArgs,
670 const Twine &Name = "");
672 /// Create an invoke to the experimental.gc.statepoint intrinsic to
673 /// start a new statepoint sequence.
674 InvokeInst *
675 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
676 Value *ActualInvokee, BasicBlock *NormalDest,
677 BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
678 ArrayRef<Value *> DeoptArgs,
679 ArrayRef<Value *> GCArgs, const Twine &Name = "");
681 /// Create an invoke to the experimental.gc.statepoint intrinsic to
682 /// start a new statepoint sequence.
683 InvokeInst *CreateGCStatepointInvoke(
684 uint64_t ID, uint32_t NumPatchBytes, Value *ActualInvokee,
685 BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
686 ArrayRef<Use> InvokeArgs, ArrayRef<Use> TransitionArgs,
687 ArrayRef<Use> DeoptArgs, ArrayRef<Value *> GCArgs,
688 const Twine &Name = "");
690 // Convenience function for the common case when CallArgs are filled in using
691 // makeArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
692 // get the Value *.
693 InvokeInst *
694 CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
695 Value *ActualInvokee, BasicBlock *NormalDest,
696 BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
697 ArrayRef<Value *> DeoptArgs,
698 ArrayRef<Value *> GCArgs, const Twine &Name = "");
700 /// Create a call to the experimental.gc.result intrinsic to extract
701 /// the result from a call wrapped in a statepoint.
702 CallInst *CreateGCResult(Instruction *Statepoint,
703 Type *ResultType,
704 const Twine &Name = "");
706 /// Create a call to the experimental.gc.relocate intrinsics to
707 /// project the relocated value of one pointer from the statepoint.
708 CallInst *CreateGCRelocate(Instruction *Statepoint,
709 int BaseOffset,
710 int DerivedOffset,
711 Type *ResultType,
712 const Twine &Name = "");
714 /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
715 /// type.
716 CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
717 Instruction *FMFSource = nullptr,
718 const Twine &Name = "");
720 /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
721 /// first type.
722 CallInst *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
723 Instruction *FMFSource = nullptr,
724 const Twine &Name = "");
726 /// Create a call to intrinsic \p ID with \p args, mangled using \p Types. If
727 /// \p FMFSource is provided, copy fast-math-flags from that instruction to
728 /// the intrinsic.
729 CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
730 ArrayRef<Value *> Args,
731 Instruction *FMFSource = nullptr,
732 const Twine &Name = "");
734 /// Create call to the minnum intrinsic.
735 CallInst *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
736 return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
739 /// Create call to the maxnum intrinsic.
740 CallInst *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
741 return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
744 /// Create call to the minimum intrinsic.
745 CallInst *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
746 return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
749 /// Create call to the maximum intrinsic.
750 CallInst *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
751 return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
754 private:
755 /// Create a call to a masked intrinsic with given Id.
756 CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
757 ArrayRef<Type *> OverloadedTypes,
758 const Twine &Name = "");
760 Value *getCastedInt8PtrValue(Value *Ptr);
763 /// This provides a uniform API for creating instructions and inserting
764 /// them into a basic block: either at the end of a BasicBlock, or at a specific
765 /// iterator location in a block.
767 /// Note that the builder does not expose the full generality of LLVM
768 /// instructions. For access to extra instruction properties, use the mutators
769 /// (e.g. setVolatile) on the instructions after they have been
770 /// created. Convenience state exists to specify fast-math flags and fp-math
771 /// tags.
773 /// The first template argument specifies a class to use for creating constants.
774 /// This defaults to creating minimally folded constants. The second template
775 /// argument allows clients to specify custom insertion hooks that are called on
776 /// every newly created insertion.
777 template <typename T = ConstantFolder,
778 typename Inserter = IRBuilderDefaultInserter>
779 class IRBuilder : public IRBuilderBase, public Inserter {
780 T Folder;
782 public:
783 IRBuilder(LLVMContext &C, const T &F, Inserter I = Inserter(),
784 MDNode *FPMathTag = nullptr,
785 ArrayRef<OperandBundleDef> OpBundles = None)
786 : IRBuilderBase(C, FPMathTag, OpBundles), Inserter(std::move(I)),
787 Folder(F) {}
789 explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
790 ArrayRef<OperandBundleDef> OpBundles = None)
791 : IRBuilderBase(C, FPMathTag, OpBundles) {}
793 explicit IRBuilder(BasicBlock *TheBB, const T &F, MDNode *FPMathTag = nullptr,
794 ArrayRef<OperandBundleDef> OpBundles = None)
795 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
796 SetInsertPoint(TheBB);
799 explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
800 ArrayRef<OperandBundleDef> OpBundles = None)
801 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
802 SetInsertPoint(TheBB);
805 explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
806 ArrayRef<OperandBundleDef> OpBundles = None)
807 : IRBuilderBase(IP->getContext(), FPMathTag, OpBundles) {
808 SetInsertPoint(IP);
811 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T &F,
812 MDNode *FPMathTag = nullptr,
813 ArrayRef<OperandBundleDef> OpBundles = None)
814 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles), Folder(F) {
815 SetInsertPoint(TheBB, IP);
818 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
819 MDNode *FPMathTag = nullptr,
820 ArrayRef<OperandBundleDef> OpBundles = None)
821 : IRBuilderBase(TheBB->getContext(), FPMathTag, OpBundles) {
822 SetInsertPoint(TheBB, IP);
825 /// Get the constant folder being used.
826 const T &getFolder() { return Folder; }
828 /// Insert and return the specified instruction.
829 template<typename InstTy>
830 InstTy *Insert(InstTy *I, const Twine &Name = "") const {
831 this->InsertHelper(I, Name, BB, InsertPt);
832 this->SetInstDebugLocation(I);
833 return I;
836 /// No-op overload to handle constants.
837 Constant *Insert(Constant *C, const Twine& = "") const {
838 return C;
841 //===--------------------------------------------------------------------===//
842 // Instruction creation methods: Terminators
843 //===--------------------------------------------------------------------===//
845 private:
846 /// Helper to add branch weight and unpredictable metadata onto an
847 /// instruction.
848 /// \returns The annotated instruction.
849 template <typename InstTy>
850 InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
851 if (Weights)
852 I->setMetadata(LLVMContext::MD_prof, Weights);
853 if (Unpredictable)
854 I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
855 return I;
858 public:
859 /// Create a 'ret void' instruction.
860 ReturnInst *CreateRetVoid() {
861 return Insert(ReturnInst::Create(Context));
864 /// Create a 'ret <val>' instruction.
865 ReturnInst *CreateRet(Value *V) {
866 return Insert(ReturnInst::Create(Context, V));
869 /// Create a sequence of N insertvalue instructions,
870 /// with one Value from the retVals array each, that build a aggregate
871 /// return value one value at a time, and a ret instruction to return
872 /// the resulting aggregate value.
874 /// This is a convenience function for code that uses aggregate return values
875 /// as a vehicle for having multiple return values.
876 ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
877 Value *V = UndefValue::get(getCurrentFunctionReturnType());
878 for (unsigned i = 0; i != N; ++i)
879 V = CreateInsertValue(V, retVals[i], i, "mrv");
880 return Insert(ReturnInst::Create(Context, V));
883 /// Create an unconditional 'br label X' instruction.
884 BranchInst *CreateBr(BasicBlock *Dest) {
885 return Insert(BranchInst::Create(Dest));
888 /// Create a conditional 'br Cond, TrueDest, FalseDest'
889 /// instruction.
890 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
891 MDNode *BranchWeights = nullptr,
892 MDNode *Unpredictable = nullptr) {
893 return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
894 BranchWeights, Unpredictable));
897 /// Create a conditional 'br Cond, TrueDest, FalseDest'
898 /// instruction. Copy branch meta data if available.
899 BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
900 Instruction *MDSrc) {
901 BranchInst *Br = BranchInst::Create(True, False, Cond);
902 if (MDSrc) {
903 unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
904 LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
905 Br->copyMetadata(*MDSrc, makeArrayRef(&WL[0], 4));
907 return Insert(Br);
910 /// Create a switch instruction with the specified value, default dest,
911 /// and with a hint for the number of cases that will be added (for efficient
912 /// allocation).
913 SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
914 MDNode *BranchWeights = nullptr,
915 MDNode *Unpredictable = nullptr) {
916 return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
917 BranchWeights, Unpredictable));
920 /// Create an indirect branch instruction with the specified address
921 /// operand, with an optional hint for the number of destinations that will be
922 /// added (for efficient allocation).
923 IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
924 return Insert(IndirectBrInst::Create(Addr, NumDests));
927 /// Create an invoke instruction.
928 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
929 BasicBlock *NormalDest, BasicBlock *UnwindDest,
930 ArrayRef<Value *> Args,
931 ArrayRef<OperandBundleDef> OpBundles,
932 const Twine &Name = "") {
933 return Insert(
934 InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles),
935 Name);
937 InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
938 BasicBlock *NormalDest, BasicBlock *UnwindDest,
939 ArrayRef<Value *> Args = None,
940 const Twine &Name = "") {
941 return Insert(InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args),
942 Name);
945 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
946 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
947 ArrayRef<OperandBundleDef> OpBundles,
948 const Twine &Name = "") {
949 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
950 NormalDest, UnwindDest, Args, OpBundles, Name);
953 InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
954 BasicBlock *UnwindDest,
955 ArrayRef<Value *> Args = None,
956 const Twine &Name = "") {
957 return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
958 NormalDest, UnwindDest, Args, Name);
961 // Deprecated [opaque pointer types]
962 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
963 BasicBlock *UnwindDest, ArrayRef<Value *> Args,
964 ArrayRef<OperandBundleDef> OpBundles,
965 const Twine &Name = "") {
966 return CreateInvoke(
967 cast<FunctionType>(
968 cast<PointerType>(Callee->getType())->getElementType()),
969 Callee, NormalDest, UnwindDest, Args, OpBundles, Name);
972 // Deprecated [opaque pointer types]
973 InvokeInst *CreateInvoke(Value *Callee, BasicBlock *NormalDest,
974 BasicBlock *UnwindDest,
975 ArrayRef<Value *> Args = None,
976 const Twine &Name = "") {
977 return CreateInvoke(
978 cast<FunctionType>(
979 cast<PointerType>(Callee->getType())->getElementType()),
980 Callee, NormalDest, UnwindDest, Args, Name);
983 /// \brief Create a callbr instruction.
984 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
985 BasicBlock *DefaultDest,
986 ArrayRef<BasicBlock *> IndirectDests,
987 ArrayRef<Value *> Args = None,
988 const Twine &Name = "") {
989 return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
990 Args), Name);
992 CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
993 BasicBlock *DefaultDest,
994 ArrayRef<BasicBlock *> IndirectDests,
995 ArrayRef<Value *> Args,
996 ArrayRef<OperandBundleDef> OpBundles,
997 const Twine &Name = "") {
998 return Insert(
999 CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1000 OpBundles), Name);
1003 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1004 ArrayRef<BasicBlock *> IndirectDests,
1005 ArrayRef<Value *> Args = None,
1006 const Twine &Name = "") {
1007 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1008 DefaultDest, IndirectDests, Args, Name);
1010 CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1011 ArrayRef<BasicBlock *> IndirectDests,
1012 ArrayRef<Value *> Args,
1013 ArrayRef<OperandBundleDef> OpBundles,
1014 const Twine &Name = "") {
1015 return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1016 DefaultDest, IndirectDests, Args, Name);
1019 ResumeInst *CreateResume(Value *Exn) {
1020 return Insert(ResumeInst::Create(Exn));
1023 CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1024 BasicBlock *UnwindBB = nullptr) {
1025 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1028 CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1029 unsigned NumHandlers,
1030 const Twine &Name = "") {
1031 return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1032 Name);
1035 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1036 const Twine &Name = "") {
1037 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1040 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1041 ArrayRef<Value *> Args = None,
1042 const Twine &Name = "") {
1043 return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1046 CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1047 return Insert(CatchReturnInst::Create(CatchPad, BB));
1050 UnreachableInst *CreateUnreachable() {
1051 return Insert(new UnreachableInst(Context));
1054 //===--------------------------------------------------------------------===//
1055 // Instruction creation methods: Binary Operators
1056 //===--------------------------------------------------------------------===//
1057 private:
1058 BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1059 Value *LHS, Value *RHS,
1060 const Twine &Name,
1061 bool HasNUW, bool HasNSW) {
1062 BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1063 if (HasNUW) BO->setHasNoUnsignedWrap();
1064 if (HasNSW) BO->setHasNoSignedWrap();
1065 return BO;
1068 Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1069 FastMathFlags FMF) const {
1070 if (!FPMD)
1071 FPMD = DefaultFPMathTag;
1072 if (FPMD)
1073 I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1074 I->setFastMathFlags(FMF);
1075 return I;
1078 Value *foldConstant(Instruction::BinaryOps Opc, Value *L,
1079 Value *R, const Twine &Name) const {
1080 auto *LC = dyn_cast<Constant>(L);
1081 auto *RC = dyn_cast<Constant>(R);
1082 return (LC && RC) ? Insert(Folder.CreateBinOp(Opc, LC, RC), Name) : nullptr;
1085 Value *getConstrainedFPRounding(
1086 Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding) {
1087 ConstrainedFPIntrinsic::RoundingMode UseRounding =
1088 DefaultConstrainedRounding;
1090 if (Rounding.hasValue())
1091 UseRounding = Rounding.getValue();
1093 Optional<StringRef> RoundingStr =
1094 ConstrainedFPIntrinsic::RoundingModeToStr(UseRounding);
1095 assert(RoundingStr.hasValue() && "Garbage strict rounding mode!");
1096 auto *RoundingMDS = MDString::get(Context, RoundingStr.getValue());
1098 return MetadataAsValue::get(Context, RoundingMDS);
1101 Value *getConstrainedFPExcept(
1102 Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except) {
1103 ConstrainedFPIntrinsic::ExceptionBehavior UseExcept =
1104 DefaultConstrainedExcept;
1106 if (Except.hasValue())
1107 UseExcept = Except.getValue();
1109 Optional<StringRef> ExceptStr =
1110 ConstrainedFPIntrinsic::ExceptionBehaviorToStr(UseExcept);
1111 assert(ExceptStr.hasValue() && "Garbage strict exception behavior!");
1112 auto *ExceptMDS = MDString::get(Context, ExceptStr.getValue());
1114 return MetadataAsValue::get(Context, ExceptMDS);
1117 public:
1118 Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1119 bool HasNUW = false, bool HasNSW = false) {
1120 if (auto *LC = dyn_cast<Constant>(LHS))
1121 if (auto *RC = dyn_cast<Constant>(RHS))
1122 return Insert(Folder.CreateAdd(LC, RC, HasNUW, HasNSW), Name);
1123 return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name,
1124 HasNUW, HasNSW);
1127 Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1128 return CreateAdd(LHS, RHS, Name, false, true);
1131 Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1132 return CreateAdd(LHS, RHS, Name, true, false);
1135 Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1136 bool HasNUW = false, bool HasNSW = false) {
1137 if (auto *LC = dyn_cast<Constant>(LHS))
1138 if (auto *RC = dyn_cast<Constant>(RHS))
1139 return Insert(Folder.CreateSub(LC, RC, HasNUW, HasNSW), Name);
1140 return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name,
1141 HasNUW, HasNSW);
1144 Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1145 return CreateSub(LHS, RHS, Name, false, true);
1148 Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1149 return CreateSub(LHS, RHS, Name, true, false);
1152 Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1153 bool HasNUW = false, bool HasNSW = false) {
1154 if (auto *LC = dyn_cast<Constant>(LHS))
1155 if (auto *RC = dyn_cast<Constant>(RHS))
1156 return Insert(Folder.CreateMul(LC, RC, HasNUW, HasNSW), Name);
1157 return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name,
1158 HasNUW, HasNSW);
1161 Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1162 return CreateMul(LHS, RHS, Name, false, true);
1165 Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1166 return CreateMul(LHS, RHS, Name, true, false);
1169 Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1170 bool isExact = false) {
1171 if (auto *LC = dyn_cast<Constant>(LHS))
1172 if (auto *RC = dyn_cast<Constant>(RHS))
1173 return Insert(Folder.CreateUDiv(LC, RC, isExact), Name);
1174 if (!isExact)
1175 return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1176 return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1179 Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1180 return CreateUDiv(LHS, RHS, Name, true);
1183 Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1184 bool isExact = false) {
1185 if (auto *LC = dyn_cast<Constant>(LHS))
1186 if (auto *RC = dyn_cast<Constant>(RHS))
1187 return Insert(Folder.CreateSDiv(LC, RC, isExact), Name);
1188 if (!isExact)
1189 return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1190 return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1193 Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1194 return CreateSDiv(LHS, RHS, Name, true);
1197 Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1198 if (Value *V = foldConstant(Instruction::URem, LHS, RHS, Name)) return V;
1199 return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1202 Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1203 if (Value *V = foldConstant(Instruction::SRem, LHS, RHS, Name)) return V;
1204 return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1207 Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1208 bool HasNUW = false, bool HasNSW = false) {
1209 if (auto *LC = dyn_cast<Constant>(LHS))
1210 if (auto *RC = dyn_cast<Constant>(RHS))
1211 return Insert(Folder.CreateShl(LC, RC, HasNUW, HasNSW), Name);
1212 return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1213 HasNUW, HasNSW);
1216 Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1217 bool HasNUW = false, bool HasNSW = false) {
1218 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1219 HasNUW, HasNSW);
1222 Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1223 bool HasNUW = false, bool HasNSW = false) {
1224 return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1225 HasNUW, HasNSW);
1228 Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1229 bool isExact = false) {
1230 if (auto *LC = dyn_cast<Constant>(LHS))
1231 if (auto *RC = dyn_cast<Constant>(RHS))
1232 return Insert(Folder.CreateLShr(LC, RC, isExact), Name);
1233 if (!isExact)
1234 return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1235 return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1238 Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1239 bool isExact = false) {
1240 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1243 Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1244 bool isExact = false) {
1245 return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1248 Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1249 bool isExact = false) {
1250 if (auto *LC = dyn_cast<Constant>(LHS))
1251 if (auto *RC = dyn_cast<Constant>(RHS))
1252 return Insert(Folder.CreateAShr(LC, RC, isExact), Name);
1253 if (!isExact)
1254 return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1255 return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1258 Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1259 bool isExact = false) {
1260 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1263 Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1264 bool isExact = false) {
1265 return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1268 Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1269 if (auto *RC = dyn_cast<Constant>(RHS)) {
1270 if (isa<ConstantInt>(RC) && cast<ConstantInt>(RC)->isMinusOne())
1271 return LHS; // LHS & -1 -> LHS
1272 if (auto *LC = dyn_cast<Constant>(LHS))
1273 return Insert(Folder.CreateAnd(LC, RC), Name);
1275 return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1278 Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1279 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1282 Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1283 return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1286 Value *CreateAnd(ArrayRef<Value*> Ops) {
1287 assert(!Ops.empty());
1288 Value *Accum = Ops[0];
1289 for (unsigned i = 1; i < Ops.size(); i++)
1290 Accum = CreateAnd(Accum, Ops[i]);
1291 return Accum;
1294 Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1295 if (auto *RC = dyn_cast<Constant>(RHS)) {
1296 if (RC->isNullValue())
1297 return LHS; // LHS | 0 -> LHS
1298 if (auto *LC = dyn_cast<Constant>(LHS))
1299 return Insert(Folder.CreateOr(LC, RC), Name);
1301 return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1304 Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1305 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1308 Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1309 return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1312 Value *CreateOr(ArrayRef<Value*> Ops) {
1313 assert(!Ops.empty());
1314 Value *Accum = Ops[0];
1315 for (unsigned i = 1; i < Ops.size(); i++)
1316 Accum = CreateOr(Accum, Ops[i]);
1317 return Accum;
1320 Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1321 if (Value *V = foldConstant(Instruction::Xor, LHS, RHS, Name)) return V;
1322 return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1325 Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1326 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1329 Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1330 return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1333 Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1334 MDNode *FPMD = nullptr) {
1335 if (IsFPConstrained)
1336 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1337 L, R, nullptr, Name, FPMD);
1339 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1340 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMF);
1341 return Insert(I, Name);
1344 /// Copy fast-math-flags from an instruction rather than using the builder's
1345 /// default FMF.
1346 Value *CreateFAddFMF(Value *L, Value *R, Instruction *FMFSource,
1347 const Twine &Name = "") {
1348 if (IsFPConstrained)
1349 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1350 L, R, FMFSource, Name);
1352 if (Value *V = foldConstant(Instruction::FAdd, L, R, Name)) return V;
1353 Instruction *I = setFPAttrs(BinaryOperator::CreateFAdd(L, R), nullptr,
1354 FMFSource->getFastMathFlags());
1355 return Insert(I, Name);
1358 Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1359 MDNode *FPMD = nullptr) {
1360 if (IsFPConstrained)
1361 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1362 L, R, nullptr, Name, FPMD);
1364 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1365 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMF);
1366 return Insert(I, Name);
1369 /// Copy fast-math-flags from an instruction rather than using the builder's
1370 /// default FMF.
1371 Value *CreateFSubFMF(Value *L, Value *R, Instruction *FMFSource,
1372 const Twine &Name = "") {
1373 if (IsFPConstrained)
1374 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1375 L, R, FMFSource, Name);
1377 if (Value *V = foldConstant(Instruction::FSub, L, R, Name)) return V;
1378 Instruction *I = setFPAttrs(BinaryOperator::CreateFSub(L, R), nullptr,
1379 FMFSource->getFastMathFlags());
1380 return Insert(I, Name);
1383 Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1384 MDNode *FPMD = nullptr) {
1385 if (IsFPConstrained)
1386 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1387 L, R, nullptr, Name, FPMD);
1389 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1390 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMF);
1391 return Insert(I, Name);
1394 /// Copy fast-math-flags from an instruction rather than using the builder's
1395 /// default FMF.
1396 Value *CreateFMulFMF(Value *L, Value *R, Instruction *FMFSource,
1397 const Twine &Name = "") {
1398 if (IsFPConstrained)
1399 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1400 L, R, FMFSource, Name);
1402 if (Value *V = foldConstant(Instruction::FMul, L, R, Name)) return V;
1403 Instruction *I = setFPAttrs(BinaryOperator::CreateFMul(L, R), nullptr,
1404 FMFSource->getFastMathFlags());
1405 return Insert(I, Name);
1408 Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1409 MDNode *FPMD = nullptr) {
1410 if (IsFPConstrained)
1411 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1412 L, R, nullptr, Name, FPMD);
1414 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1415 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMF);
1416 return Insert(I, Name);
1419 /// Copy fast-math-flags from an instruction rather than using the builder's
1420 /// default FMF.
1421 Value *CreateFDivFMF(Value *L, Value *R, Instruction *FMFSource,
1422 const Twine &Name = "") {
1423 if (IsFPConstrained)
1424 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1425 L, R, FMFSource, Name);
1427 if (Value *V = foldConstant(Instruction::FDiv, L, R, Name)) return V;
1428 Instruction *I = setFPAttrs(BinaryOperator::CreateFDiv(L, R), nullptr,
1429 FMFSource->getFastMathFlags());
1430 return Insert(I, Name);
1433 Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1434 MDNode *FPMD = nullptr) {
1435 if (IsFPConstrained)
1436 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1437 L, R, nullptr, Name, FPMD);
1439 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1440 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMF);
1441 return Insert(I, Name);
1444 /// Copy fast-math-flags from an instruction rather than using the builder's
1445 /// default FMF.
1446 Value *CreateFRemFMF(Value *L, Value *R, Instruction *FMFSource,
1447 const Twine &Name = "") {
1448 if (IsFPConstrained)
1449 return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1450 L, R, FMFSource, Name);
1452 if (Value *V = foldConstant(Instruction::FRem, L, R, Name)) return V;
1453 Instruction *I = setFPAttrs(BinaryOperator::CreateFRem(L, R), nullptr,
1454 FMFSource->getFastMathFlags());
1455 return Insert(I, Name);
1458 Value *CreateBinOp(Instruction::BinaryOps Opc,
1459 Value *LHS, Value *RHS, const Twine &Name = "",
1460 MDNode *FPMathTag = nullptr) {
1461 if (Value *V = foldConstant(Opc, LHS, RHS, Name)) return V;
1462 Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1463 if (isa<FPMathOperator>(BinOp))
1464 BinOp = setFPAttrs(BinOp, FPMathTag, FMF);
1465 return Insert(BinOp, Name);
1468 CallInst *CreateConstrainedFPBinOp(
1469 Intrinsic::ID ID, Value *L, Value *R, Instruction *FMFSource = nullptr,
1470 const Twine &Name = "", MDNode *FPMathTag = nullptr,
1471 Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding = None,
1472 Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except = None) {
1473 Value *RoundingV = getConstrainedFPRounding(Rounding);
1474 Value *ExceptV = getConstrainedFPExcept(Except);
1476 FastMathFlags UseFMF = FMF;
1477 if (FMFSource)
1478 UseFMF = FMFSource->getFastMathFlags();
1480 CallInst *C = CreateIntrinsic(ID, {L->getType()},
1481 {L, R, RoundingV, ExceptV}, nullptr, Name);
1482 return cast<CallInst>(setFPAttrs(C, FPMathTag, UseFMF));
1485 Value *CreateNeg(Value *V, const Twine &Name = "",
1486 bool HasNUW = false, bool HasNSW = false) {
1487 if (auto *VC = dyn_cast<Constant>(V))
1488 return Insert(Folder.CreateNeg(VC, HasNUW, HasNSW), Name);
1489 BinaryOperator *BO = Insert(BinaryOperator::CreateNeg(V), Name);
1490 if (HasNUW) BO->setHasNoUnsignedWrap();
1491 if (HasNSW) BO->setHasNoSignedWrap();
1492 return BO;
1495 Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1496 return CreateNeg(V, Name, false, true);
1499 Value *CreateNUWNeg(Value *V, const Twine &Name = "") {
1500 return CreateNeg(V, Name, true, false);
1503 Value *CreateFNeg(Value *V, const Twine &Name = "",
1504 MDNode *FPMathTag = nullptr) {
1505 if (auto *VC = dyn_cast<Constant>(V))
1506 return Insert(Folder.CreateFNeg(VC), Name);
1507 return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V), FPMathTag, FMF),
1508 Name);
1511 /// Copy fast-math-flags from an instruction rather than using the builder's
1512 /// default FMF.
1513 Value *CreateFNegFMF(Value *V, Instruction *FMFSource,
1514 const Twine &Name = "") {
1515 if (auto *VC = dyn_cast<Constant>(V))
1516 return Insert(Folder.CreateFNeg(VC), Name);
1517 // TODO: This should return UnaryOperator::CreateFNeg(...) once we are
1518 // confident that they are optimized sufficiently.
1519 return Insert(setFPAttrs(BinaryOperator::CreateFNeg(V), nullptr,
1520 FMFSource->getFastMathFlags()),
1521 Name);
1524 Value *CreateNot(Value *V, const Twine &Name = "") {
1525 if (auto *VC = dyn_cast<Constant>(V))
1526 return Insert(Folder.CreateNot(VC), Name);
1527 return Insert(BinaryOperator::CreateNot(V), Name);
1530 Value *CreateUnOp(Instruction::UnaryOps Opc,
1531 Value *V, const Twine &Name = "",
1532 MDNode *FPMathTag = nullptr) {
1533 if (auto *VC = dyn_cast<Constant>(V))
1534 return Insert(Folder.CreateUnOp(Opc, VC), Name);
1535 Instruction *UnOp = UnaryOperator::Create(Opc, V);
1536 if (isa<FPMathOperator>(UnOp))
1537 UnOp = setFPAttrs(UnOp, FPMathTag, FMF);
1538 return Insert(UnOp, Name);
1541 /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1542 /// Correct number of operands must be passed accordingly.
1543 Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1544 const Twine &Name = "",
1545 MDNode *FPMathTag = nullptr) {
1546 if (Instruction::isBinaryOp(Opc)) {
1547 assert(Ops.size() == 2 && "Invalid number of operands!");
1548 return CreateBinOp(static_cast<Instruction::BinaryOps>(Opc),
1549 Ops[0], Ops[1], Name, FPMathTag);
1551 if (Instruction::isUnaryOp(Opc)) {
1552 assert(Ops.size() == 1 && "Invalid number of operands!");
1553 return CreateUnOp(static_cast<Instruction::UnaryOps>(Opc),
1554 Ops[0], Name, FPMathTag);
1556 llvm_unreachable("Unexpected opcode!");
1559 //===--------------------------------------------------------------------===//
1560 // Instruction creation methods: Memory Instructions
1561 //===--------------------------------------------------------------------===//
1563 AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1564 Value *ArraySize = nullptr, const Twine &Name = "") {
1565 return Insert(new AllocaInst(Ty, AddrSpace, ArraySize), Name);
1568 AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1569 const Twine &Name = "") {
1570 const DataLayout &DL = BB->getParent()->getParent()->getDataLayout();
1571 return Insert(new AllocaInst(Ty, DL.getAllocaAddrSpace(), ArraySize), Name);
1574 /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1575 /// converting the string to 'bool' for the isVolatile parameter.
1576 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1577 return Insert(new LoadInst(Ty, Ptr), Name);
1580 LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1581 return Insert(new LoadInst(Ty, Ptr), Name);
1584 LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1585 const Twine &Name = "") {
1586 return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile), Name);
1589 // Deprecated [opaque pointer types]
1590 LoadInst *CreateLoad(Value *Ptr, const char *Name) {
1591 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1594 // Deprecated [opaque pointer types]
1595 LoadInst *CreateLoad(Value *Ptr, const Twine &Name = "") {
1596 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, Name);
1599 // Deprecated [opaque pointer types]
1600 LoadInst *CreateLoad(Value *Ptr, bool isVolatile, const Twine &Name = "") {
1601 return CreateLoad(Ptr->getType()->getPointerElementType(), Ptr, isVolatile,
1602 Name);
1605 StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1606 return Insert(new StoreInst(Val, Ptr, isVolatile));
1609 /// Provided to resolve 'CreateAlignedLoad(Ptr, Align, "...")'
1610 /// correctly, instead of converting the string to 'bool' for the isVolatile
1611 /// parameter.
1612 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1613 const char *Name) {
1614 LoadInst *LI = CreateLoad(Ty, Ptr, Name);
1615 LI->setAlignment(Align);
1616 return LI;
1618 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1619 const Twine &Name = "") {
1620 LoadInst *LI = CreateLoad(Ty, Ptr, Name);
1621 LI->setAlignment(Align);
1622 return LI;
1624 LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, unsigned Align,
1625 bool isVolatile, const Twine &Name = "") {
1626 LoadInst *LI = CreateLoad(Ty, Ptr, isVolatile, Name);
1627 LI->setAlignment(Align);
1628 return LI;
1631 // Deprecated [opaque pointer types]
1632 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, const char *Name) {
1633 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1634 Align, Name);
1636 // Deprecated [opaque pointer types]
1637 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align,
1638 const Twine &Name = "") {
1639 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1640 Align, Name);
1642 // Deprecated [opaque pointer types]
1643 LoadInst *CreateAlignedLoad(Value *Ptr, unsigned Align, bool isVolatile,
1644 const Twine &Name = "") {
1645 return CreateAlignedLoad(Ptr->getType()->getPointerElementType(), Ptr,
1646 Align, isVolatile, Name);
1649 StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, unsigned Align,
1650 bool isVolatile = false) {
1651 StoreInst *SI = CreateStore(Val, Ptr, isVolatile);
1652 SI->setAlignment(Align);
1653 return SI;
1656 FenceInst *CreateFence(AtomicOrdering Ordering,
1657 SyncScope::ID SSID = SyncScope::System,
1658 const Twine &Name = "") {
1659 return Insert(new FenceInst(Context, Ordering, SSID), Name);
1662 AtomicCmpXchgInst *
1663 CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New,
1664 AtomicOrdering SuccessOrdering,
1665 AtomicOrdering FailureOrdering,
1666 SyncScope::ID SSID = SyncScope::System) {
1667 return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, SuccessOrdering,
1668 FailureOrdering, SSID));
1671 AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr, Value *Val,
1672 AtomicOrdering Ordering,
1673 SyncScope::ID SSID = SyncScope::System) {
1674 return Insert(new AtomicRMWInst(Op, Ptr, Val, Ordering, SSID));
1677 Value *CreateGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1678 const Twine &Name = "") {
1679 return CreateGEP(nullptr, Ptr, IdxList, Name);
1682 Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1683 const Twine &Name = "") {
1684 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1685 // Every index must be constant.
1686 size_t i, e;
1687 for (i = 0, e = IdxList.size(); i != e; ++i)
1688 if (!isa<Constant>(IdxList[i]))
1689 break;
1690 if (i == e)
1691 return Insert(Folder.CreateGetElementPtr(Ty, PC, IdxList), Name);
1693 return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList), Name);
1696 Value *CreateInBoundsGEP(Value *Ptr, ArrayRef<Value *> IdxList,
1697 const Twine &Name = "") {
1698 return CreateInBoundsGEP(nullptr, Ptr, IdxList, Name);
1701 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1702 const Twine &Name = "") {
1703 if (auto *PC = dyn_cast<Constant>(Ptr)) {
1704 // Every index must be constant.
1705 size_t i, e;
1706 for (i = 0, e = IdxList.size(); i != e; ++i)
1707 if (!isa<Constant>(IdxList[i]))
1708 break;
1709 if (i == e)
1710 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IdxList),
1711 Name);
1713 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, IdxList), Name);
1716 Value *CreateGEP(Value *Ptr, Value *Idx, const Twine &Name = "") {
1717 return CreateGEP(nullptr, Ptr, Idx, Name);
1720 Value *CreateGEP(Type *Ty, Value *Ptr, Value *Idx, const Twine &Name = "") {
1721 if (auto *PC = dyn_cast<Constant>(Ptr))
1722 if (auto *IC = dyn_cast<Constant>(Idx))
1723 return Insert(Folder.CreateGetElementPtr(Ty, PC, IC), Name);
1724 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1727 Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, Value *Idx,
1728 const Twine &Name = "") {
1729 if (auto *PC = dyn_cast<Constant>(Ptr))
1730 if (auto *IC = dyn_cast<Constant>(Idx))
1731 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, IC), Name);
1732 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1735 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const Twine &Name = "") {
1736 return CreateConstGEP1_32(nullptr, Ptr, Idx0, Name);
1739 Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1740 const Twine &Name = "") {
1741 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1743 if (auto *PC = dyn_cast<Constant>(Ptr))
1744 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1746 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1749 Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1750 const Twine &Name = "") {
1751 Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1753 if (auto *PC = dyn_cast<Constant>(Ptr))
1754 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1756 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1759 Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1760 const Twine &Name = "") {
1761 Value *Idxs[] = {
1762 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1763 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1766 if (auto *PC = dyn_cast<Constant>(Ptr))
1767 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1769 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1772 Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1773 unsigned Idx1, const Twine &Name = "") {
1774 Value *Idxs[] = {
1775 ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1776 ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1779 if (auto *PC = dyn_cast<Constant>(Ptr))
1780 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1782 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1785 Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1786 const Twine &Name = "") {
1787 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1789 if (auto *PC = dyn_cast<Constant>(Ptr))
1790 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idx), Name);
1792 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1795 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const Twine &Name = "") {
1796 return CreateConstGEP1_64(nullptr, Ptr, Idx0, Name);
1799 Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1800 const Twine &Name = "") {
1801 Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1803 if (auto *PC = dyn_cast<Constant>(Ptr))
1804 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idx), Name);
1806 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1809 Value *CreateConstInBoundsGEP1_64(Value *Ptr, uint64_t Idx0,
1810 const Twine &Name = "") {
1811 return CreateConstInBoundsGEP1_64(nullptr, Ptr, Idx0, Name);
1814 Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1815 const Twine &Name = "") {
1816 Value *Idxs[] = {
1817 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1818 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1821 if (auto *PC = dyn_cast<Constant>(Ptr))
1822 return Insert(Folder.CreateGetElementPtr(Ty, PC, Idxs), Name);
1824 return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1827 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1828 const Twine &Name = "") {
1829 return CreateConstGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1832 Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1833 uint64_t Idx1, const Twine &Name = "") {
1834 Value *Idxs[] = {
1835 ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1836 ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1839 if (auto *PC = dyn_cast<Constant>(Ptr))
1840 return Insert(Folder.CreateInBoundsGetElementPtr(Ty, PC, Idxs), Name);
1842 return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1845 Value *CreateConstInBoundsGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1846 const Twine &Name = "") {
1847 return CreateConstInBoundsGEP2_64(nullptr, Ptr, Idx0, Idx1, Name);
1850 Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1851 const Twine &Name = "") {
1852 return CreateConstInBoundsGEP2_32(Ty, Ptr, 0, Idx, Name);
1855 Value *CreateStructGEP(Value *Ptr, unsigned Idx, const Twine &Name = "") {
1856 return CreateConstInBoundsGEP2_32(nullptr, Ptr, 0, Idx, Name);
1859 /// Same as CreateGlobalString, but return a pointer with "i8*" type
1860 /// instead of a pointer to array of i8.
1861 Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
1862 unsigned AddressSpace = 0) {
1863 GlobalVariable *GV = CreateGlobalString(Str, Name, AddressSpace);
1864 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
1865 Constant *Indices[] = {Zero, Zero};
1866 return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
1867 Indices);
1870 //===--------------------------------------------------------------------===//
1871 // Instruction creation methods: Cast/Conversion Operators
1872 //===--------------------------------------------------------------------===//
1874 Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "") {
1875 return CreateCast(Instruction::Trunc, V, DestTy, Name);
1878 Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "") {
1879 return CreateCast(Instruction::ZExt, V, DestTy, Name);
1882 Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
1883 return CreateCast(Instruction::SExt, V, DestTy, Name);
1886 /// Create a ZExt or Trunc from the integer value V to DestTy. Return
1887 /// the value untouched if the type of V is already DestTy.
1888 Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
1889 const Twine &Name = "") {
1890 assert(V->getType()->isIntOrIntVectorTy() &&
1891 DestTy->isIntOrIntVectorTy() &&
1892 "Can only zero extend/truncate integers!");
1893 Type *VTy = V->getType();
1894 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1895 return CreateZExt(V, DestTy, Name);
1896 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1897 return CreateTrunc(V, DestTy, Name);
1898 return V;
1901 /// Create a SExt or Trunc from the integer value V to DestTy. Return
1902 /// the value untouched if the type of V is already DestTy.
1903 Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
1904 const Twine &Name = "") {
1905 assert(V->getType()->isIntOrIntVectorTy() &&
1906 DestTy->isIntOrIntVectorTy() &&
1907 "Can only sign extend/truncate integers!");
1908 Type *VTy = V->getType();
1909 if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
1910 return CreateSExt(V, DestTy, Name);
1911 if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
1912 return CreateTrunc(V, DestTy, Name);
1913 return V;
1916 Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
1917 if (IsFPConstrained)
1918 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
1919 V, DestTy, nullptr, Name);
1920 return CreateCast(Instruction::FPToUI, V, DestTy, Name);
1923 Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
1924 if (IsFPConstrained)
1925 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
1926 V, DestTy, nullptr, Name);
1927 return CreateCast(Instruction::FPToSI, V, DestTy, Name);
1930 Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1931 return CreateCast(Instruction::UIToFP, V, DestTy, Name);
1934 Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
1935 return CreateCast(Instruction::SIToFP, V, DestTy, Name);
1938 Value *CreateFPTrunc(Value *V, Type *DestTy,
1939 const Twine &Name = "") {
1940 if (IsFPConstrained)
1941 return CreateConstrainedFPCast(
1942 Intrinsic::experimental_constrained_fptrunc, V, DestTy, nullptr,
1943 Name);
1944 return CreateCast(Instruction::FPTrunc, V, DestTy, Name);
1947 Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "") {
1948 if (IsFPConstrained)
1949 return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
1950 V, DestTy, nullptr, Name);
1951 return CreateCast(Instruction::FPExt, V, DestTy, Name);
1954 Value *CreatePtrToInt(Value *V, Type *DestTy,
1955 const Twine &Name = "") {
1956 return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
1959 Value *CreateIntToPtr(Value *V, Type *DestTy,
1960 const Twine &Name = "") {
1961 return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
1964 Value *CreateBitCast(Value *V, Type *DestTy,
1965 const Twine &Name = "") {
1966 return CreateCast(Instruction::BitCast, V, DestTy, Name);
1969 Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
1970 const Twine &Name = "") {
1971 return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
1974 Value *CreateZExtOrBitCast(Value *V, Type *DestTy,
1975 const Twine &Name = "") {
1976 if (V->getType() == DestTy)
1977 return V;
1978 if (auto *VC = dyn_cast<Constant>(V))
1979 return Insert(Folder.CreateZExtOrBitCast(VC, DestTy), Name);
1980 return Insert(CastInst::CreateZExtOrBitCast(V, DestTy), Name);
1983 Value *CreateSExtOrBitCast(Value *V, Type *DestTy,
1984 const Twine &Name = "") {
1985 if (V->getType() == DestTy)
1986 return V;
1987 if (auto *VC = dyn_cast<Constant>(V))
1988 return Insert(Folder.CreateSExtOrBitCast(VC, DestTy), Name);
1989 return Insert(CastInst::CreateSExtOrBitCast(V, DestTy), Name);
1992 Value *CreateTruncOrBitCast(Value *V, Type *DestTy,
1993 const Twine &Name = "") {
1994 if (V->getType() == DestTy)
1995 return V;
1996 if (auto *VC = dyn_cast<Constant>(V))
1997 return Insert(Folder.CreateTruncOrBitCast(VC, DestTy), Name);
1998 return Insert(CastInst::CreateTruncOrBitCast(V, DestTy), Name);
2001 Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2002 const Twine &Name = "") {
2003 if (V->getType() == DestTy)
2004 return V;
2005 if (auto *VC = dyn_cast<Constant>(V))
2006 return Insert(Folder.CreateCast(Op, VC, DestTy), Name);
2007 return Insert(CastInst::Create(Op, V, DestTy), Name);
2010 Value *CreatePointerCast(Value *V, Type *DestTy,
2011 const Twine &Name = "") {
2012 if (V->getType() == DestTy)
2013 return V;
2014 if (auto *VC = dyn_cast<Constant>(V))
2015 return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2016 return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2019 Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2020 const Twine &Name = "") {
2021 if (V->getType() == DestTy)
2022 return V;
2024 if (auto *VC = dyn_cast<Constant>(V)) {
2025 return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2026 Name);
2029 return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2030 Name);
2033 Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2034 const Twine &Name = "") {
2035 if (V->getType() == DestTy)
2036 return V;
2037 if (auto *VC = dyn_cast<Constant>(V))
2038 return Insert(Folder.CreateIntCast(VC, DestTy, isSigned), Name);
2039 return Insert(CastInst::CreateIntegerCast(V, DestTy, isSigned), Name);
2042 Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2043 const Twine &Name = "") {
2044 if (V->getType() == DestTy)
2045 return V;
2046 if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2047 return CreatePtrToInt(V, DestTy, Name);
2048 if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2049 return CreateIntToPtr(V, DestTy, Name);
2051 return CreateBitCast(V, DestTy, Name);
2054 Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "") {
2055 if (V->getType() == DestTy)
2056 return V;
2057 if (auto *VC = dyn_cast<Constant>(V))
2058 return Insert(Folder.CreateFPCast(VC, DestTy), Name);
2059 return Insert(CastInst::CreateFPCast(V, DestTy), Name);
2062 CallInst *CreateConstrainedFPCast(
2063 Intrinsic::ID ID, Value *V, Type *DestTy,
2064 Instruction *FMFSource = nullptr, const Twine &Name = "",
2065 MDNode *FPMathTag = nullptr,
2066 Optional<ConstrainedFPIntrinsic::RoundingMode> Rounding = None,
2067 Optional<ConstrainedFPIntrinsic::ExceptionBehavior> Except = None) {
2068 Value *ExceptV = getConstrainedFPExcept(Except);
2070 FastMathFlags UseFMF = FMF;
2071 if (FMFSource)
2072 UseFMF = FMFSource->getFastMathFlags();
2074 CallInst *C;
2075 switch (ID) {
2076 default: {
2077 Value *RoundingV = getConstrainedFPRounding(Rounding);
2078 C = CreateIntrinsic(ID, {DestTy, V->getType()}, {V, RoundingV, ExceptV},
2079 nullptr, Name);
2080 } break;
2081 case Intrinsic::experimental_constrained_fpext:
2082 case Intrinsic::experimental_constrained_fptoui:
2083 case Intrinsic::experimental_constrained_fptosi:
2084 C = CreateIntrinsic(ID, {DestTy, V->getType()}, {V, ExceptV}, nullptr,
2085 Name);
2086 break;
2088 if (isa<FPMathOperator>(C))
2089 C = cast<CallInst>(setFPAttrs(C, FPMathTag, UseFMF));
2090 return C;
2093 // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2094 // compile time error, instead of converting the string to bool for the
2095 // isSigned parameter.
2096 Value *CreateIntCast(Value *, Type *, const char *) = delete;
2098 //===--------------------------------------------------------------------===//
2099 // Instruction creation methods: Compare Instructions
2100 //===--------------------------------------------------------------------===//
2102 Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2103 return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2106 Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2107 return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2110 Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2111 return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2114 Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2115 return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2118 Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2119 return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2122 Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2123 return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2126 Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2127 return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2130 Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2131 return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
2134 Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2135 return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
2138 Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2139 return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
2142 Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2143 MDNode *FPMathTag = nullptr) {
2144 return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2147 Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2148 MDNode *FPMathTag = nullptr) {
2149 return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2152 Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2153 MDNode *FPMathTag = nullptr) {
2154 return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2157 Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2158 MDNode *FPMathTag = nullptr) {
2159 return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2162 Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2163 MDNode *FPMathTag = nullptr) {
2164 return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2167 Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2168 MDNode *FPMathTag = nullptr) {
2169 return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2172 Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2173 MDNode *FPMathTag = nullptr) {
2174 return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2177 Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2178 MDNode *FPMathTag = nullptr) {
2179 return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2182 Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2183 MDNode *FPMathTag = nullptr) {
2184 return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2187 Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2188 MDNode *FPMathTag = nullptr) {
2189 return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2192 Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2193 MDNode *FPMathTag = nullptr) {
2194 return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2197 Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2198 MDNode *FPMathTag = nullptr) {
2199 return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2202 Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2203 MDNode *FPMathTag = nullptr) {
2204 return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2207 Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2208 MDNode *FPMathTag = nullptr) {
2209 return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2212 Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2213 const Twine &Name = "") {
2214 if (auto *LC = dyn_cast<Constant>(LHS))
2215 if (auto *RC = dyn_cast<Constant>(RHS))
2216 return Insert(Folder.CreateICmp(P, LC, RC), Name);
2217 return Insert(new ICmpInst(P, LHS, RHS), Name);
2220 Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2221 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2222 if (auto *LC = dyn_cast<Constant>(LHS))
2223 if (auto *RC = dyn_cast<Constant>(RHS))
2224 return Insert(Folder.CreateFCmp(P, LC, RC), Name);
2225 return Insert(setFPAttrs(new FCmpInst(P, LHS, RHS), FPMathTag, FMF), Name);
2228 //===--------------------------------------------------------------------===//
2229 // Instruction creation methods: Other Instructions
2230 //===--------------------------------------------------------------------===//
2232 PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2233 const Twine &Name = "") {
2234 return Insert(PHINode::Create(Ty, NumReservedValues), Name);
2237 CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2238 ArrayRef<Value *> Args = None, const Twine &Name = "",
2239 MDNode *FPMathTag = nullptr) {
2240 CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2241 if (isa<FPMathOperator>(CI))
2242 CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2243 return Insert(CI, Name);
2246 CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2247 ArrayRef<OperandBundleDef> OpBundles,
2248 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2249 CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2250 if (isa<FPMathOperator>(CI))
2251 CI = cast<CallInst>(setFPAttrs(CI, FPMathTag, FMF));
2252 return Insert(CI, Name);
2255 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = None,
2256 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2257 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2258 FPMathTag);
2261 CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2262 ArrayRef<OperandBundleDef> OpBundles,
2263 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2264 return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2265 OpBundles, Name, FPMathTag);
2268 // Deprecated [opaque pointer types]
2269 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args = None,
2270 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2271 return CreateCall(
2272 cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2273 Args, Name, FPMathTag);
2276 // Deprecated [opaque pointer types]
2277 CallInst *CreateCall(Value *Callee, ArrayRef<Value *> Args,
2278 ArrayRef<OperandBundleDef> OpBundles,
2279 const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2280 return CreateCall(
2281 cast<FunctionType>(Callee->getType()->getPointerElementType()), Callee,
2282 Args, OpBundles, Name, FPMathTag);
2285 Value *CreateSelect(Value *C, Value *True, Value *False,
2286 const Twine &Name = "", Instruction *MDFrom = nullptr) {
2287 if (auto *CC = dyn_cast<Constant>(C))
2288 if (auto *TC = dyn_cast<Constant>(True))
2289 if (auto *FC = dyn_cast<Constant>(False))
2290 return Insert(Folder.CreateSelect(CC, TC, FC), Name);
2292 SelectInst *Sel = SelectInst::Create(C, True, False);
2293 if (MDFrom) {
2294 MDNode *Prof = MDFrom->getMetadata(LLVMContext::MD_prof);
2295 MDNode *Unpred = MDFrom->getMetadata(LLVMContext::MD_unpredictable);
2296 Sel = addBranchMetadata(Sel, Prof, Unpred);
2298 if (isa<FPMathOperator>(Sel))
2299 Sel = cast<SelectInst>(setFPAttrs(Sel, nullptr /* MDNode* */, FMF));
2300 return Insert(Sel, Name);
2303 VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2304 return Insert(new VAArgInst(List, Ty), Name);
2307 Value *CreateExtractElement(Value *Vec, Value *Idx,
2308 const Twine &Name = "") {
2309 if (auto *VC = dyn_cast<Constant>(Vec))
2310 if (auto *IC = dyn_cast<Constant>(Idx))
2311 return Insert(Folder.CreateExtractElement(VC, IC), Name);
2312 return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2315 Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2316 const Twine &Name = "") {
2317 return CreateExtractElement(Vec, getInt64(Idx), Name);
2320 Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2321 const Twine &Name = "") {
2322 if (auto *VC = dyn_cast<Constant>(Vec))
2323 if (auto *NC = dyn_cast<Constant>(NewElt))
2324 if (auto *IC = dyn_cast<Constant>(Idx))
2325 return Insert(Folder.CreateInsertElement(VC, NC, IC), Name);
2326 return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2329 Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2330 const Twine &Name = "") {
2331 return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2334 Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2335 const Twine &Name = "") {
2336 if (auto *V1C = dyn_cast<Constant>(V1))
2337 if (auto *V2C = dyn_cast<Constant>(V2))
2338 if (auto *MC = dyn_cast<Constant>(Mask))
2339 return Insert(Folder.CreateShuffleVector(V1C, V2C, MC), Name);
2340 return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2343 Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<uint32_t> IntMask,
2344 const Twine &Name = "") {
2345 Value *Mask = ConstantDataVector::get(Context, IntMask);
2346 return CreateShuffleVector(V1, V2, Mask, Name);
2349 Value *CreateExtractValue(Value *Agg,
2350 ArrayRef<unsigned> Idxs,
2351 const Twine &Name = "") {
2352 if (auto *AggC = dyn_cast<Constant>(Agg))
2353 return Insert(Folder.CreateExtractValue(AggC, Idxs), Name);
2354 return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2357 Value *CreateInsertValue(Value *Agg, Value *Val,
2358 ArrayRef<unsigned> Idxs,
2359 const Twine &Name = "") {
2360 if (auto *AggC = dyn_cast<Constant>(Agg))
2361 if (auto *ValC = dyn_cast<Constant>(Val))
2362 return Insert(Folder.CreateInsertValue(AggC, ValC, Idxs), Name);
2363 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2366 LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2367 const Twine &Name = "") {
2368 return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2371 //===--------------------------------------------------------------------===//
2372 // Utility creation methods
2373 //===--------------------------------------------------------------------===//
2375 /// Return an i1 value testing if \p Arg is null.
2376 Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2377 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
2378 Name);
2381 /// Return an i1 value testing if \p Arg is not null.
2382 Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2383 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
2384 Name);
2387 /// Return the i64 difference between two pointer values, dividing out
2388 /// the size of the pointed-to objects.
2390 /// This is intended to implement C-style pointer subtraction. As such, the
2391 /// pointers must be appropriately aligned for their element types and
2392 /// pointing into the same object.
2393 Value *CreatePtrDiff(Value *LHS, Value *RHS, const Twine &Name = "") {
2394 assert(LHS->getType() == RHS->getType() &&
2395 "Pointer subtraction operand types must match!");
2396 auto *ArgType = cast<PointerType>(LHS->getType());
2397 Value *LHS_int = CreatePtrToInt(LHS, Type::getInt64Ty(Context));
2398 Value *RHS_int = CreatePtrToInt(RHS, Type::getInt64Ty(Context));
2399 Value *Difference = CreateSub(LHS_int, RHS_int);
2400 return CreateExactSDiv(Difference,
2401 ConstantExpr::getSizeOf(ArgType->getElementType()),
2402 Name);
2405 /// Create a launder.invariant.group intrinsic call. If Ptr type is
2406 /// different from pointer to i8, it's casted to pointer to i8 in the same
2407 /// address space before call and casted back to Ptr type after call.
2408 Value *CreateLaunderInvariantGroup(Value *Ptr) {
2409 assert(isa<PointerType>(Ptr->getType()) &&
2410 "launder.invariant.group only applies to pointers.");
2411 // FIXME: we could potentially avoid casts to/from i8*.
2412 auto *PtrType = Ptr->getType();
2413 auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2414 if (PtrType != Int8PtrTy)
2415 Ptr = CreateBitCast(Ptr, Int8PtrTy);
2416 Module *M = BB->getParent()->getParent();
2417 Function *FnLaunderInvariantGroup = Intrinsic::getDeclaration(
2418 M, Intrinsic::launder_invariant_group, {Int8PtrTy});
2420 assert(FnLaunderInvariantGroup->getReturnType() == Int8PtrTy &&
2421 FnLaunderInvariantGroup->getFunctionType()->getParamType(0) ==
2422 Int8PtrTy &&
2423 "LaunderInvariantGroup should take and return the same type");
2425 CallInst *Fn = CreateCall(FnLaunderInvariantGroup, {Ptr});
2427 if (PtrType != Int8PtrTy)
2428 return CreateBitCast(Fn, PtrType);
2429 return Fn;
2432 /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2433 /// different from pointer to i8, it's casted to pointer to i8 in the same
2434 /// address space before call and casted back to Ptr type after call.
2435 Value *CreateStripInvariantGroup(Value *Ptr) {
2436 assert(isa<PointerType>(Ptr->getType()) &&
2437 "strip.invariant.group only applies to pointers.");
2439 // FIXME: we could potentially avoid casts to/from i8*.
2440 auto *PtrType = Ptr->getType();
2441 auto *Int8PtrTy = getInt8PtrTy(PtrType->getPointerAddressSpace());
2442 if (PtrType != Int8PtrTy)
2443 Ptr = CreateBitCast(Ptr, Int8PtrTy);
2444 Module *M = BB->getParent()->getParent();
2445 Function *FnStripInvariantGroup = Intrinsic::getDeclaration(
2446 M, Intrinsic::strip_invariant_group, {Int8PtrTy});
2448 assert(FnStripInvariantGroup->getReturnType() == Int8PtrTy &&
2449 FnStripInvariantGroup->getFunctionType()->getParamType(0) ==
2450 Int8PtrTy &&
2451 "StripInvariantGroup should take and return the same type");
2453 CallInst *Fn = CreateCall(FnStripInvariantGroup, {Ptr});
2455 if (PtrType != Int8PtrTy)
2456 return CreateBitCast(Fn, PtrType);
2457 return Fn;
2460 /// Return a vector value that contains \arg V broadcasted to \p
2461 /// NumElts elements.
2462 Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "") {
2463 assert(NumElts > 0 && "Cannot splat to an empty vector!");
2465 // First insert it into an undef vector so we can shuffle it.
2466 Type *I32Ty = getInt32Ty();
2467 Value *Undef = UndefValue::get(VectorType::get(V->getType(), NumElts));
2468 V = CreateInsertElement(Undef, V, ConstantInt::get(I32Ty, 0),
2469 Name + ".splatinsert");
2471 // Shuffle the value across the desired number of elements.
2472 Value *Zeros = ConstantAggregateZero::get(VectorType::get(I32Ty, NumElts));
2473 return CreateShuffleVector(V, Undef, Zeros, Name + ".splat");
2476 /// Return a value that has been extracted from a larger integer type.
2477 Value *CreateExtractInteger(const DataLayout &DL, Value *From,
2478 IntegerType *ExtractedTy, uint64_t Offset,
2479 const Twine &Name) {
2480 auto *IntTy = cast<IntegerType>(From->getType());
2481 assert(DL.getTypeStoreSize(ExtractedTy) + Offset <=
2482 DL.getTypeStoreSize(IntTy) &&
2483 "Element extends past full value");
2484 uint64_t ShAmt = 8 * Offset;
2485 Value *V = From;
2486 if (DL.isBigEndian())
2487 ShAmt = 8 * (DL.getTypeStoreSize(IntTy) -
2488 DL.getTypeStoreSize(ExtractedTy) - Offset);
2489 if (ShAmt) {
2490 V = CreateLShr(V, ShAmt, Name + ".shift");
2492 assert(ExtractedTy->getBitWidth() <= IntTy->getBitWidth() &&
2493 "Cannot extract to a larger integer!");
2494 if (ExtractedTy != IntTy) {
2495 V = CreateTrunc(V, ExtractedTy, Name + ".trunc");
2497 return V;
2500 Value *CreatePreserveArrayAccessIndex(Value *Base, unsigned Dimension,
2501 unsigned LastIndex, MDNode *DbgInfo) {
2502 assert(isa<PointerType>(Base->getType()) &&
2503 "Invalid Base ptr type for preserve.array.access.index.");
2504 auto *BaseType = Base->getType();
2506 Value *LastIndexV = getInt32(LastIndex);
2507 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
2508 SmallVector<Value *, 4> IdxList;
2509 for (unsigned I = 0; I < Dimension; ++I)
2510 IdxList.push_back(Zero);
2511 IdxList.push_back(LastIndexV);
2513 Type *ResultType =
2514 GetElementPtrInst::getGEPReturnType(Base, IdxList);
2516 Module *M = BB->getParent()->getParent();
2517 Function *FnPreserveArrayAccessIndex = Intrinsic::getDeclaration(
2518 M, Intrinsic::preserve_array_access_index, {ResultType, BaseType});
2520 Value *DimV = getInt32(Dimension);
2521 CallInst *Fn =
2522 CreateCall(FnPreserveArrayAccessIndex, {Base, DimV, LastIndexV});
2523 if (DbgInfo)
2524 Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
2526 return Fn;
2529 Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2530 MDNode *DbgInfo) {
2531 assert(isa<PointerType>(Base->getType()) &&
2532 "Invalid Base ptr type for preserve.union.access.index.");
2533 auto *BaseType = Base->getType();
2535 Module *M = BB->getParent()->getParent();
2536 Function *FnPreserveUnionAccessIndex = Intrinsic::getDeclaration(
2537 M, Intrinsic::preserve_union_access_index, {BaseType, BaseType});
2539 Value *DIIndex = getInt32(FieldIndex);
2540 CallInst *Fn =
2541 CreateCall(FnPreserveUnionAccessIndex, {Base, DIIndex});
2542 if (DbgInfo)
2543 Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
2545 return Fn;
2548 Value *CreatePreserveStructAccessIndex(Value *Base, unsigned Index,
2549 unsigned FieldIndex, MDNode *DbgInfo) {
2550 assert(isa<PointerType>(Base->getType()) &&
2551 "Invalid Base ptr type for preserve.struct.access.index.");
2552 auto *BaseType = Base->getType();
2554 Value *GEPIndex = getInt32(Index);
2555 Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
2556 Type *ResultType =
2557 GetElementPtrInst::getGEPReturnType(Base, {Zero, GEPIndex});
2559 Module *M = BB->getParent()->getParent();
2560 Function *FnPreserveStructAccessIndex = Intrinsic::getDeclaration(
2561 M, Intrinsic::preserve_struct_access_index, {ResultType, BaseType});
2563 Value *DIIndex = getInt32(FieldIndex);
2564 CallInst *Fn = CreateCall(FnPreserveStructAccessIndex,
2565 {Base, GEPIndex, DIIndex});
2566 if (DbgInfo)
2567 Fn->setMetadata(LLVMContext::MD_preserve_access_index, DbgInfo);
2569 return Fn;
2572 private:
2573 /// Helper function that creates an assume intrinsic call that
2574 /// represents an alignment assumption on the provided Ptr, Mask, Type
2575 /// and Offset. It may be sometimes useful to do some other logic
2576 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2577 CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2578 Value *PtrValue, Value *Mask,
2579 Type *IntPtrTy, Value *OffsetValue,
2580 Value **TheCheck) {
2581 Value *PtrIntValue = CreatePtrToInt(PtrValue, IntPtrTy, "ptrint");
2583 if (OffsetValue) {
2584 bool IsOffsetZero = false;
2585 if (const auto *CI = dyn_cast<ConstantInt>(OffsetValue))
2586 IsOffsetZero = CI->isZero();
2588 if (!IsOffsetZero) {
2589 if (OffsetValue->getType() != IntPtrTy)
2590 OffsetValue = CreateIntCast(OffsetValue, IntPtrTy, /*isSigned*/ true,
2591 "offsetcast");
2592 PtrIntValue = CreateSub(PtrIntValue, OffsetValue, "offsetptr");
2596 Value *Zero = ConstantInt::get(IntPtrTy, 0);
2597 Value *MaskedPtr = CreateAnd(PtrIntValue, Mask, "maskedptr");
2598 Value *InvCond = CreateICmpEQ(MaskedPtr, Zero, "maskcond");
2599 if (TheCheck)
2600 *TheCheck = InvCond;
2602 return CreateAssumption(InvCond);
2605 public:
2606 /// Create an assume intrinsic call that represents an alignment
2607 /// assumption on the provided pointer.
2609 /// An optional offset can be provided, and if it is provided, the offset
2610 /// must be subtracted from the provided pointer to get the pointer with the
2611 /// specified alignment.
2613 /// It may be sometimes useful to do some other logic
2614 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2615 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2616 unsigned Alignment,
2617 Value *OffsetValue = nullptr,
2618 Value **TheCheck = nullptr) {
2619 assert(isa<PointerType>(PtrValue->getType()) &&
2620 "trying to create an alignment assumption on a non-pointer?");
2621 assert(Alignment != 0 && "Invalid Alignment");
2622 auto *PtrTy = cast<PointerType>(PtrValue->getType());
2623 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2625 Value *Mask = ConstantInt::get(IntPtrTy, Alignment - 1);
2626 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2627 OffsetValue, TheCheck);
2630 /// Create an assume intrinsic call that represents an alignment
2631 /// assumption on the provided pointer.
2633 /// An optional offset can be provided, and if it is provided, the offset
2634 /// must be subtracted from the provided pointer to get the pointer with the
2635 /// specified alignment.
2637 /// It may be sometimes useful to do some other logic
2638 /// based on this alignment check, thus it can be stored into 'TheCheck'.
2640 /// This overload handles the condition where the Alignment is dependent
2641 /// on an existing value rather than a static value.
2642 CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2643 Value *Alignment,
2644 Value *OffsetValue = nullptr,
2645 Value **TheCheck = nullptr) {
2646 assert(isa<PointerType>(PtrValue->getType()) &&
2647 "trying to create an alignment assumption on a non-pointer?");
2648 auto *PtrTy = cast<PointerType>(PtrValue->getType());
2649 Type *IntPtrTy = getIntPtrTy(DL, PtrTy->getAddressSpace());
2651 if (Alignment->getType() != IntPtrTy)
2652 Alignment = CreateIntCast(Alignment, IntPtrTy, /*isSigned*/ false,
2653 "alignmentcast");
2655 Value *Mask = CreateSub(Alignment, ConstantInt::get(IntPtrTy, 1), "mask");
2657 return CreateAlignmentAssumptionHelper(DL, PtrValue, Mask, IntPtrTy,
2658 OffsetValue, TheCheck);
2662 // Create wrappers for C Binding types (see CBindingWrapping.h).
2663 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2665 } // end namespace llvm
2667 #endif // LLVM_IR_IRBUILDER_H