xref: /llvm-project/llvm/include/llvm/IR/IRBuilder.h (revision 04b002bbb838bc502bd6d5f602af95efd6cc96b3)
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.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_IRBUILDER_H
15 #define LLVM_IR_IRBUILDER_H
16 
17 #include "llvm-c/Types.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/STLExtras.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/FPEnv.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GlobalVariable.h"
32 #include "llvm/IR/InstrTypes.h"
33 #include "llvm/IR/Instruction.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/Intrinsics.h"
36 #include "llvm/IR/LLVMContext.h"
37 #include "llvm/IR/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 <cstdint>
46 #include <functional>
47 #include <optional>
48 #include <utility>
49 
50 namespace llvm {
51 
52 class APInt;
53 class Use;
54 
55 /// This provides the default implementation of the IRBuilder
56 /// 'InsertHelper' method that is called whenever an instruction is created by
57 /// IRBuilder and needs to be inserted.
58 ///
59 /// By default, this inserts the instruction at the insertion point.
60 class IRBuilderDefaultInserter {
61 public:
62   virtual ~IRBuilderDefaultInserter();
63 
64   virtual void InsertHelper(Instruction *I, const Twine &Name,
65                             BasicBlock::iterator InsertPt) const {
66     if (InsertPt.isValid())
67       I->insertInto(InsertPt.getNodeParent(), InsertPt);
68     I->setName(Name);
69   }
70 };
71 
72 /// Provides an 'InsertHelper' that calls a user-provided callback after
73 /// performing the default insertion.
74 class IRBuilderCallbackInserter : public IRBuilderDefaultInserter {
75   std::function<void(Instruction *)> Callback;
76 
77 public:
78   ~IRBuilderCallbackInserter() override;
79 
80   IRBuilderCallbackInserter(std::function<void(Instruction *)> Callback)
81       : Callback(std::move(Callback)) {}
82 
83   void InsertHelper(Instruction *I, const Twine &Name,
84                     BasicBlock::iterator InsertPt) const override {
85     IRBuilderDefaultInserter::InsertHelper(I, Name, InsertPt);
86     Callback(I);
87   }
88 };
89 
90 /// This provides a helper for copying FMF from an instruction or setting
91 /// specified flags.
92 class FMFSource {
93   std::optional<FastMathFlags> FMF;
94 
95 public:
96   FMFSource() = default;
97   FMFSource(Instruction *Source) {
98     if (Source)
99       FMF = Source->getFastMathFlags();
100   }
101   FMFSource(FastMathFlags FMF) : FMF(FMF) {}
102   FastMathFlags get(FastMathFlags Default) const {
103     return FMF.value_or(Default);
104   }
105   /// Intersect the FMF from two instructions.
106   static FMFSource intersect(Value *A, Value *B) {
107     return FMFSource(cast<FPMathOperator>(A)->getFastMathFlags() &
108                      cast<FPMathOperator>(B)->getFastMathFlags());
109   }
110 };
111 
112 /// Common base class shared among various IRBuilders.
113 class IRBuilderBase {
114   /// Pairs of (metadata kind, MDNode *) that should be added to all newly
115   /// created instructions, like !dbg metadata.
116   SmallVector<std::pair<unsigned, MDNode *>, 2> MetadataToCopy;
117 
118   /// Add or update the an entry (Kind, MD) to MetadataToCopy, if \p MD is not
119   /// null. If \p MD is null, remove the entry with \p Kind.
120   void AddOrRemoveMetadataToCopy(unsigned Kind, MDNode *MD) {
121     if (!MD) {
122       erase_if(MetadataToCopy, [Kind](const std::pair<unsigned, MDNode *> &KV) {
123         return KV.first == Kind;
124       });
125       return;
126     }
127 
128     for (auto &KV : MetadataToCopy)
129       if (KV.first == Kind) {
130         KV.second = MD;
131         return;
132       }
133 
134     MetadataToCopy.emplace_back(Kind, MD);
135   }
136 
137 protected:
138   BasicBlock *BB;
139   BasicBlock::iterator InsertPt;
140   LLVMContext &Context;
141   const IRBuilderFolder &Folder;
142   const IRBuilderDefaultInserter &Inserter;
143 
144   MDNode *DefaultFPMathTag;
145   FastMathFlags FMF;
146 
147   bool IsFPConstrained = false;
148   fp::ExceptionBehavior DefaultConstrainedExcept = fp::ebStrict;
149   RoundingMode DefaultConstrainedRounding = RoundingMode::Dynamic;
150 
151   ArrayRef<OperandBundleDef> DefaultOperandBundles;
152 
153 public:
154   IRBuilderBase(LLVMContext &context, const IRBuilderFolder &Folder,
155                 const IRBuilderDefaultInserter &Inserter, MDNode *FPMathTag,
156                 ArrayRef<OperandBundleDef> OpBundles)
157       : Context(context), Folder(Folder), Inserter(Inserter),
158         DefaultFPMathTag(FPMathTag), DefaultOperandBundles(OpBundles) {
159     ClearInsertionPoint();
160   }
161 
162   /// Insert and return the specified instruction.
163   template<typename InstTy>
164   InstTy *Insert(InstTy *I, const Twine &Name = "") const {
165     Inserter.InsertHelper(I, Name, InsertPt);
166     AddMetadataToInst(I);
167     return I;
168   }
169 
170   /// No-op overload to handle constants.
171   Constant *Insert(Constant *C, const Twine& = "") const {
172     return C;
173   }
174 
175   Value *Insert(Value *V, const Twine &Name = "") const {
176     if (Instruction *I = dyn_cast<Instruction>(V))
177       return Insert(I, Name);
178     assert(isa<Constant>(V));
179     return V;
180   }
181 
182   //===--------------------------------------------------------------------===//
183   // Builder configuration methods
184   //===--------------------------------------------------------------------===//
185 
186   /// Clear the insertion point: created instructions will not be
187   /// inserted into a block.
188   void ClearInsertionPoint() {
189     BB = nullptr;
190     InsertPt = BasicBlock::iterator();
191   }
192 
193   BasicBlock *GetInsertBlock() const { return BB; }
194   BasicBlock::iterator GetInsertPoint() const { return InsertPt; }
195   LLVMContext &getContext() const { return Context; }
196 
197   /// This specifies that created instructions should be appended to the
198   /// end of the specified block.
199   void SetInsertPoint(BasicBlock *TheBB) {
200     BB = TheBB;
201     InsertPt = BB->end();
202   }
203 
204   /// This specifies that created instructions should be inserted before
205   /// the specified instruction.
206   void SetInsertPoint(Instruction *I) {
207     BB = I->getParent();
208     InsertPt = I->getIterator();
209     assert(InsertPt != BB->end() && "Can't read debug loc from end()");
210     SetCurrentDebugLocation(I->getStableDebugLoc());
211   }
212 
213   /// This specifies that created instructions should be inserted at the
214   /// specified point.
215   void SetInsertPoint(BasicBlock *TheBB, BasicBlock::iterator IP) {
216     BB = TheBB;
217     InsertPt = IP;
218     if (IP != TheBB->end())
219       SetCurrentDebugLocation(IP->getStableDebugLoc());
220   }
221 
222   /// This specifies that created instructions should be inserted at
223   /// the specified point, but also requires that \p IP is dereferencable.
224   void SetInsertPoint(BasicBlock::iterator IP) {
225     BB = IP->getParent();
226     InsertPt = IP;
227     SetCurrentDebugLocation(IP->getStableDebugLoc());
228   }
229 
230   /// This specifies that created instructions should inserted at the beginning
231   /// end of the specified function, but after already existing static alloca
232   /// instructions that are at the start.
233   void SetInsertPointPastAllocas(Function *F) {
234     BB = &F->getEntryBlock();
235     InsertPt = BB->getFirstNonPHIOrDbgOrAlloca();
236   }
237 
238   /// Set location information used by debugging information.
239   void SetCurrentDebugLocation(DebugLoc L) {
240     AddOrRemoveMetadataToCopy(LLVMContext::MD_dbg, L.getAsMDNode());
241   }
242 
243   /// Set nosanitize metadata.
244   void SetNoSanitizeMetadata() {
245     AddOrRemoveMetadataToCopy(llvm::LLVMContext::MD_nosanitize,
246                               llvm::MDNode::get(getContext(), {}));
247   }
248 
249   /// Collect metadata with IDs \p MetadataKinds from \p Src which should be
250   /// added to all created instructions. Entries present in MedataDataToCopy but
251   /// not on \p Src will be dropped from MetadataToCopy.
252   void CollectMetadataToCopy(Instruction *Src,
253                              ArrayRef<unsigned> MetadataKinds) {
254     for (unsigned K : MetadataKinds)
255       AddOrRemoveMetadataToCopy(K, Src->getMetadata(K));
256   }
257 
258   /// Get location information used by debugging information.
259   DebugLoc getCurrentDebugLocation() const;
260 
261   /// If this builder has a current debug location, set it on the
262   /// specified instruction.
263   void SetInstDebugLocation(Instruction *I) const;
264 
265   /// Add all entries in MetadataToCopy to \p I.
266   void AddMetadataToInst(Instruction *I) const {
267     for (const auto &KV : MetadataToCopy)
268       I->setMetadata(KV.first, KV.second);
269   }
270 
271   /// Get the return type of the current function that we're emitting
272   /// into.
273   Type *getCurrentFunctionReturnType() const;
274 
275   /// InsertPoint - A saved insertion point.
276   class InsertPoint {
277     BasicBlock *Block = nullptr;
278     BasicBlock::iterator Point;
279 
280   public:
281     /// Creates a new insertion point which doesn't point to anything.
282     InsertPoint() = default;
283 
284     /// Creates a new insertion point at the given location.
285     InsertPoint(BasicBlock *InsertBlock, BasicBlock::iterator InsertPoint)
286         : Block(InsertBlock), Point(InsertPoint) {}
287 
288     /// Returns true if this insert point is set.
289     bool isSet() const { return (Block != nullptr); }
290 
291     BasicBlock *getBlock() const { return Block; }
292     BasicBlock::iterator getPoint() const { return Point; }
293   };
294 
295   /// Returns the current insert point.
296   InsertPoint saveIP() const {
297     return InsertPoint(GetInsertBlock(), GetInsertPoint());
298   }
299 
300   /// Returns the current insert point, clearing it in the process.
301   InsertPoint saveAndClearIP() {
302     InsertPoint IP(GetInsertBlock(), GetInsertPoint());
303     ClearInsertionPoint();
304     return IP;
305   }
306 
307   /// Sets the current insert point to a previously-saved location.
308   void restoreIP(InsertPoint IP) {
309     if (IP.isSet())
310       SetInsertPoint(IP.getBlock(), IP.getPoint());
311     else
312       ClearInsertionPoint();
313   }
314 
315   /// Get the floating point math metadata being used.
316   MDNode *getDefaultFPMathTag() const { return DefaultFPMathTag; }
317 
318   /// Get the flags to be applied to created floating point ops
319   FastMathFlags getFastMathFlags() const { return FMF; }
320 
321   FastMathFlags &getFastMathFlags() { return FMF; }
322 
323   /// Clear the fast-math flags.
324   void clearFastMathFlags() { FMF.clear(); }
325 
326   /// Set the floating point math metadata to be used.
327   void setDefaultFPMathTag(MDNode *FPMathTag) { DefaultFPMathTag = FPMathTag; }
328 
329   /// Set the fast-math flags to be used with generated fp-math operators
330   void setFastMathFlags(FastMathFlags NewFMF) { FMF = NewFMF; }
331 
332   /// Enable/Disable use of constrained floating point math. When
333   /// enabled the CreateF<op>() calls instead create constrained
334   /// floating point intrinsic calls. Fast math flags are unaffected
335   /// by this setting.
336   void setIsFPConstrained(bool IsCon) { IsFPConstrained = IsCon; }
337 
338   /// Query for the use of constrained floating point math
339   bool getIsFPConstrained() { return IsFPConstrained; }
340 
341   /// Set the exception handling to be used with constrained floating point
342   void setDefaultConstrainedExcept(fp::ExceptionBehavior NewExcept) {
343 #ifndef NDEBUG
344     std::optional<StringRef> ExceptStr =
345         convertExceptionBehaviorToStr(NewExcept);
346     assert(ExceptStr && "Garbage strict exception behavior!");
347 #endif
348     DefaultConstrainedExcept = NewExcept;
349   }
350 
351   /// Set the rounding mode handling to be used with constrained floating point
352   void setDefaultConstrainedRounding(RoundingMode NewRounding) {
353 #ifndef NDEBUG
354     std::optional<StringRef> RoundingStr =
355         convertRoundingModeToStr(NewRounding);
356     assert(RoundingStr && "Garbage strict rounding mode!");
357 #endif
358     DefaultConstrainedRounding = NewRounding;
359   }
360 
361   /// Get the exception handling used with constrained floating point
362   fp::ExceptionBehavior getDefaultConstrainedExcept() {
363     return DefaultConstrainedExcept;
364   }
365 
366   /// Get the rounding mode handling used with constrained floating point
367   RoundingMode getDefaultConstrainedRounding() {
368     return DefaultConstrainedRounding;
369   }
370 
371   void setConstrainedFPFunctionAttr() {
372     assert(BB && "Must have a basic block to set any function attributes!");
373 
374     Function *F = BB->getParent();
375     if (!F->hasFnAttribute(Attribute::StrictFP)) {
376       F->addFnAttr(Attribute::StrictFP);
377     }
378   }
379 
380   void setConstrainedFPCallAttr(CallBase *I) {
381     I->addFnAttr(Attribute::StrictFP);
382   }
383 
384   void setDefaultOperandBundles(ArrayRef<OperandBundleDef> OpBundles) {
385     DefaultOperandBundles = OpBundles;
386   }
387 
388   //===--------------------------------------------------------------------===//
389   // RAII helpers.
390   //===--------------------------------------------------------------------===//
391 
392   // RAII object that stores the current insertion point and restores it
393   // when the object is destroyed. This includes the debug location.
394   class InsertPointGuard {
395     IRBuilderBase &Builder;
396     AssertingVH<BasicBlock> Block;
397     BasicBlock::iterator Point;
398     DebugLoc DbgLoc;
399 
400   public:
401     InsertPointGuard(IRBuilderBase &B)
402         : Builder(B), Block(B.GetInsertBlock()), Point(B.GetInsertPoint()),
403           DbgLoc(B.getCurrentDebugLocation()) {}
404 
405     InsertPointGuard(const InsertPointGuard &) = delete;
406     InsertPointGuard &operator=(const InsertPointGuard &) = delete;
407 
408     ~InsertPointGuard() {
409       Builder.restoreIP(InsertPoint(Block, Point));
410       Builder.SetCurrentDebugLocation(DbgLoc);
411     }
412   };
413 
414   // RAII object that stores the current fast math settings and restores
415   // them when the object is destroyed.
416   class FastMathFlagGuard {
417     IRBuilderBase &Builder;
418     FastMathFlags FMF;
419     MDNode *FPMathTag;
420     bool IsFPConstrained;
421     fp::ExceptionBehavior DefaultConstrainedExcept;
422     RoundingMode DefaultConstrainedRounding;
423 
424   public:
425     FastMathFlagGuard(IRBuilderBase &B)
426         : Builder(B), FMF(B.FMF), FPMathTag(B.DefaultFPMathTag),
427           IsFPConstrained(B.IsFPConstrained),
428           DefaultConstrainedExcept(B.DefaultConstrainedExcept),
429           DefaultConstrainedRounding(B.DefaultConstrainedRounding) {}
430 
431     FastMathFlagGuard(const FastMathFlagGuard &) = delete;
432     FastMathFlagGuard &operator=(const FastMathFlagGuard &) = delete;
433 
434     ~FastMathFlagGuard() {
435       Builder.FMF = FMF;
436       Builder.DefaultFPMathTag = FPMathTag;
437       Builder.IsFPConstrained = IsFPConstrained;
438       Builder.DefaultConstrainedExcept = DefaultConstrainedExcept;
439       Builder.DefaultConstrainedRounding = DefaultConstrainedRounding;
440     }
441   };
442 
443   // RAII object that stores the current default operand bundles and restores
444   // them when the object is destroyed.
445   class OperandBundlesGuard {
446     IRBuilderBase &Builder;
447     ArrayRef<OperandBundleDef> DefaultOperandBundles;
448 
449   public:
450     OperandBundlesGuard(IRBuilderBase &B)
451         : Builder(B), DefaultOperandBundles(B.DefaultOperandBundles) {}
452 
453     OperandBundlesGuard(const OperandBundlesGuard &) = delete;
454     OperandBundlesGuard &operator=(const OperandBundlesGuard &) = delete;
455 
456     ~OperandBundlesGuard() {
457       Builder.DefaultOperandBundles = DefaultOperandBundles;
458     }
459   };
460 
461 
462   //===--------------------------------------------------------------------===//
463   // Miscellaneous creation methods.
464   //===--------------------------------------------------------------------===//
465 
466   /// Make a new global variable with initializer type i8*
467   ///
468   /// Make a new global variable with an initializer that has array of i8 type
469   /// filled in with the null terminated string value specified.  The new global
470   /// variable will be marked mergable with any others of the same contents.  If
471   /// Name is specified, it is the name of the global variable created.
472   ///
473   /// If no module is given via \p M, it is take from the insertion point basic
474   /// block.
475   GlobalVariable *CreateGlobalString(StringRef Str, const Twine &Name = "",
476                                      unsigned AddressSpace = 0,
477                                      Module *M = nullptr, bool AddNull = true);
478 
479   /// Get a constant value representing either true or false.
480   ConstantInt *getInt1(bool V) {
481     return ConstantInt::get(getInt1Ty(), V);
482   }
483 
484   /// Get the constant value for i1 true.
485   ConstantInt *getTrue() {
486     return ConstantInt::getTrue(Context);
487   }
488 
489   /// Get the constant value for i1 false.
490   ConstantInt *getFalse() {
491     return ConstantInt::getFalse(Context);
492   }
493 
494   /// Get a constant 8-bit value.
495   ConstantInt *getInt8(uint8_t C) {
496     return ConstantInt::get(getInt8Ty(), C);
497   }
498 
499   /// Get a constant 16-bit value.
500   ConstantInt *getInt16(uint16_t C) {
501     return ConstantInt::get(getInt16Ty(), C);
502   }
503 
504   /// Get a constant 32-bit value.
505   ConstantInt *getInt32(uint32_t C) {
506     return ConstantInt::get(getInt32Ty(), C);
507   }
508 
509   /// Get a constant 64-bit value.
510   ConstantInt *getInt64(uint64_t C) {
511     return ConstantInt::get(getInt64Ty(), C);
512   }
513 
514   /// Get a constant N-bit value, zero extended or truncated from
515   /// a 64-bit value.
516   ConstantInt *getIntN(unsigned N, uint64_t C) {
517     return ConstantInt::get(getIntNTy(N), C);
518   }
519 
520   /// Get a constant integer value.
521   ConstantInt *getInt(const APInt &AI) {
522     return ConstantInt::get(Context, AI);
523   }
524 
525   //===--------------------------------------------------------------------===//
526   // Type creation methods
527   //===--------------------------------------------------------------------===//
528 
529   /// Fetch the type representing a single bit
530   IntegerType *getInt1Ty() {
531     return Type::getInt1Ty(Context);
532   }
533 
534   /// Fetch the type representing an 8-bit integer.
535   IntegerType *getInt8Ty() {
536     return Type::getInt8Ty(Context);
537   }
538 
539   /// Fetch the type representing a 16-bit integer.
540   IntegerType *getInt16Ty() {
541     return Type::getInt16Ty(Context);
542   }
543 
544   /// Fetch the type representing a 32-bit integer.
545   IntegerType *getInt32Ty() {
546     return Type::getInt32Ty(Context);
547   }
548 
549   /// Fetch the type representing a 64-bit integer.
550   IntegerType *getInt64Ty() {
551     return Type::getInt64Ty(Context);
552   }
553 
554   /// Fetch the type representing a 128-bit integer.
555   IntegerType *getInt128Ty() { return Type::getInt128Ty(Context); }
556 
557   /// Fetch the type representing an N-bit integer.
558   IntegerType *getIntNTy(unsigned N) {
559     return Type::getIntNTy(Context, N);
560   }
561 
562   /// Fetch the type representing a 16-bit floating point value.
563   Type *getHalfTy() {
564     return Type::getHalfTy(Context);
565   }
566 
567   /// Fetch the type representing a 16-bit brain floating point value.
568   Type *getBFloatTy() {
569     return Type::getBFloatTy(Context);
570   }
571 
572   /// Fetch the type representing a 32-bit floating point value.
573   Type *getFloatTy() {
574     return Type::getFloatTy(Context);
575   }
576 
577   /// Fetch the type representing a 64-bit floating point value.
578   Type *getDoubleTy() {
579     return Type::getDoubleTy(Context);
580   }
581 
582   /// Fetch the type representing void.
583   Type *getVoidTy() {
584     return Type::getVoidTy(Context);
585   }
586 
587   /// Fetch the type representing a pointer.
588   PointerType *getPtrTy(unsigned AddrSpace = 0) {
589     return PointerType::get(Context, AddrSpace);
590   }
591 
592   /// Fetch the type of an integer with size at least as big as that of a
593   /// pointer in the given address space.
594   IntegerType *getIntPtrTy(const DataLayout &DL, unsigned AddrSpace = 0) {
595     return DL.getIntPtrType(Context, AddrSpace);
596   }
597 
598   /// Fetch the type of an integer that should be used to index GEP operations
599   /// within AddressSpace.
600   IntegerType *getIndexTy(const DataLayout &DL, unsigned AddrSpace) {
601     return DL.getIndexType(Context, AddrSpace);
602   }
603 
604   //===--------------------------------------------------------------------===//
605   // Intrinsic creation methods
606   //===--------------------------------------------------------------------===//
607 
608   /// Create and insert a memset to the specified pointer and the
609   /// specified value.
610   ///
611   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
612   /// specified, it will be added to the instruction. Likewise with alias.scope
613   /// and noalias tags.
614   CallInst *CreateMemSet(Value *Ptr, Value *Val, uint64_t Size,
615                          MaybeAlign Align, bool isVolatile = false,
616                          MDNode *TBAATag = nullptr, MDNode *ScopeTag = nullptr,
617                          MDNode *NoAliasTag = nullptr) {
618     return CreateMemSet(Ptr, Val, getInt64(Size), Align, isVolatile,
619                         TBAATag, ScopeTag, NoAliasTag);
620   }
621 
622   CallInst *CreateMemSet(Value *Ptr, Value *Val, Value *Size, MaybeAlign Align,
623                          bool isVolatile = false, MDNode *TBAATag = nullptr,
624                          MDNode *ScopeTag = nullptr,
625                          MDNode *NoAliasTag = nullptr);
626 
627   CallInst *CreateMemSetInline(Value *Dst, MaybeAlign DstAlign, Value *Val,
628                                Value *Size, bool IsVolatile = false,
629                                MDNode *TBAATag = nullptr,
630                                MDNode *ScopeTag = nullptr,
631                                MDNode *NoAliasTag = nullptr);
632 
633   /// Create and insert an element unordered-atomic memset of the region of
634   /// memory starting at the given pointer to the given value.
635   ///
636   /// If the pointer isn't an i8*, it will be converted. If a TBAA tag is
637   /// specified, it will be added to the instruction. Likewise with alias.scope
638   /// and noalias tags.
639   CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
640                                                uint64_t Size, Align Alignment,
641                                                uint32_t ElementSize,
642                                                MDNode *TBAATag = nullptr,
643                                                MDNode *ScopeTag = nullptr,
644                                                MDNode *NoAliasTag = nullptr) {
645     return CreateElementUnorderedAtomicMemSet(Ptr, Val, getInt64(Size),
646                                               Align(Alignment), ElementSize,
647                                               TBAATag, ScopeTag, NoAliasTag);
648   }
649 
650   CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
651                          Value *ArraySize, ArrayRef<OperandBundleDef> OpB,
652                          Function *MallocF = nullptr, const Twine &Name = "");
653 
654   /// CreateMalloc - Generate the IR for a call to malloc:
655   /// 1. Compute the malloc call's argument as the specified type's size,
656   ///    possibly multiplied by the array size if the array size is not
657   ///    constant 1.
658   /// 2. Call malloc with that argument.
659   CallInst *CreateMalloc(Type *IntPtrTy, Type *AllocTy, Value *AllocSize,
660                          Value *ArraySize, Function *MallocF = nullptr,
661                          const Twine &Name = "");
662   /// Generate the IR for a call to the builtin free function.
663   CallInst *CreateFree(Value *Source, ArrayRef<OperandBundleDef> Bundles = {});
664 
665   CallInst *CreateElementUnorderedAtomicMemSet(Value *Ptr, Value *Val,
666                                                Value *Size, Align Alignment,
667                                                uint32_t ElementSize,
668                                                MDNode *TBAATag = nullptr,
669                                                MDNode *ScopeTag = nullptr,
670                                                MDNode *NoAliasTag = nullptr);
671 
672   /// Create and insert a memcpy between the specified pointers.
673   ///
674   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
675   /// specified, it will be added to the instruction. Likewise with alias.scope
676   /// and noalias tags.
677   CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
678                          MaybeAlign SrcAlign, uint64_t Size,
679                          bool isVolatile = false, MDNode *TBAATag = nullptr,
680                          MDNode *TBAAStructTag = nullptr,
681                          MDNode *ScopeTag = nullptr,
682                          MDNode *NoAliasTag = nullptr) {
683     return CreateMemCpy(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
684                         isVolatile, TBAATag, TBAAStructTag, ScopeTag,
685                         NoAliasTag);
686   }
687 
688   CallInst *CreateMemTransferInst(
689       Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src,
690       MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
691       MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
692       MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr);
693 
694   CallInst *CreateMemCpy(Value *Dst, MaybeAlign DstAlign, Value *Src,
695                          MaybeAlign SrcAlign, Value *Size,
696                          bool isVolatile = false, MDNode *TBAATag = nullptr,
697                          MDNode *TBAAStructTag = nullptr,
698                          MDNode *ScopeTag = nullptr,
699                          MDNode *NoAliasTag = nullptr) {
700     return CreateMemTransferInst(Intrinsic::memcpy, Dst, DstAlign, Src,
701                                  SrcAlign, Size, isVolatile, TBAATag,
702                                  TBAAStructTag, ScopeTag, NoAliasTag);
703   }
704 
705   CallInst *
706   CreateMemCpyInline(Value *Dst, MaybeAlign DstAlign, Value *Src,
707                      MaybeAlign SrcAlign, Value *Size, bool isVolatile = false,
708                      MDNode *TBAATag = nullptr, MDNode *TBAAStructTag = nullptr,
709                      MDNode *ScopeTag = nullptr, MDNode *NoAliasTag = nullptr) {
710     return CreateMemTransferInst(Intrinsic::memcpy_inline, Dst, DstAlign, Src,
711                                  SrcAlign, Size, isVolatile, TBAATag,
712                                  TBAAStructTag, ScopeTag, NoAliasTag);
713   }
714 
715   /// Create and insert an element unordered-atomic memcpy between the
716   /// specified pointers.
717   ///
718   /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers, respectively.
719   ///
720   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
721   /// specified, it will be added to the instruction. Likewise with alias.scope
722   /// and noalias tags.
723   CallInst *CreateElementUnorderedAtomicMemCpy(
724       Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
725       uint32_t ElementSize, MDNode *TBAATag = nullptr,
726       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
727       MDNode *NoAliasTag = nullptr);
728 
729   CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
730                           MaybeAlign SrcAlign, uint64_t Size,
731                           bool isVolatile = false, MDNode *TBAATag = nullptr,
732                           MDNode *ScopeTag = nullptr,
733                           MDNode *NoAliasTag = nullptr) {
734     return CreateMemMove(Dst, DstAlign, Src, SrcAlign, getInt64(Size),
735                          isVolatile, TBAATag, ScopeTag, NoAliasTag);
736   }
737 
738   CallInst *CreateMemMove(Value *Dst, MaybeAlign DstAlign, Value *Src,
739                           MaybeAlign SrcAlign, Value *Size,
740                           bool isVolatile = false, MDNode *TBAATag = nullptr,
741                           MDNode *ScopeTag = nullptr,
742                           MDNode *NoAliasTag = nullptr) {
743     return CreateMemTransferInst(Intrinsic::memmove, Dst, DstAlign, Src,
744                                  SrcAlign, Size, isVolatile, TBAATag,
745                                  /*TBAAStructTag=*/nullptr, ScopeTag,
746                                  NoAliasTag);
747   }
748 
749   /// \brief Create and insert an element unordered-atomic memmove between the
750   /// specified pointers.
751   ///
752   /// DstAlign/SrcAlign are the alignments of the Dst/Src pointers,
753   /// respectively.
754   ///
755   /// If the pointers aren't i8*, they will be converted.  If a TBAA tag is
756   /// specified, it will be added to the instruction. Likewise with alias.scope
757   /// and noalias tags.
758   CallInst *CreateElementUnorderedAtomicMemMove(
759       Value *Dst, Align DstAlign, Value *Src, Align SrcAlign, Value *Size,
760       uint32_t ElementSize, MDNode *TBAATag = nullptr,
761       MDNode *TBAAStructTag = nullptr, MDNode *ScopeTag = nullptr,
762       MDNode *NoAliasTag = nullptr);
763 
764 private:
765   CallInst *getReductionIntrinsic(Intrinsic::ID ID, Value *Src);
766 
767 public:
768   /// Create a sequential vector fadd reduction intrinsic of the source vector.
769   /// The first parameter is a scalar accumulator value. An unordered reduction
770   /// can be created by adding the reassoc fast-math flag to the resulting
771   /// sequential reduction.
772   CallInst *CreateFAddReduce(Value *Acc, Value *Src);
773 
774   /// Create a sequential vector fmul reduction intrinsic of the source vector.
775   /// The first parameter is a scalar accumulator value. An unordered reduction
776   /// can be created by adding the reassoc fast-math flag to the resulting
777   /// sequential reduction.
778   CallInst *CreateFMulReduce(Value *Acc, Value *Src);
779 
780   /// Create a vector int add reduction intrinsic of the source vector.
781   CallInst *CreateAddReduce(Value *Src);
782 
783   /// Create a vector int mul reduction intrinsic of the source vector.
784   CallInst *CreateMulReduce(Value *Src);
785 
786   /// Create a vector int AND reduction intrinsic of the source vector.
787   CallInst *CreateAndReduce(Value *Src);
788 
789   /// Create a vector int OR reduction intrinsic of the source vector.
790   CallInst *CreateOrReduce(Value *Src);
791 
792   /// Create a vector int XOR reduction intrinsic of the source vector.
793   CallInst *CreateXorReduce(Value *Src);
794 
795   /// Create a vector integer max reduction intrinsic of the source
796   /// vector.
797   CallInst *CreateIntMaxReduce(Value *Src, bool IsSigned = false);
798 
799   /// Create a vector integer min reduction intrinsic of the source
800   /// vector.
801   CallInst *CreateIntMinReduce(Value *Src, bool IsSigned = false);
802 
803   /// Create a vector float max reduction intrinsic of the source
804   /// vector.
805   CallInst *CreateFPMaxReduce(Value *Src);
806 
807   /// Create a vector float min reduction intrinsic of the source
808   /// vector.
809   CallInst *CreateFPMinReduce(Value *Src);
810 
811   /// Create a vector float maximum reduction intrinsic of the source
812   /// vector. This variant follows the NaN and signed zero semantic of
813   /// llvm.maximum intrinsic.
814   CallInst *CreateFPMaximumReduce(Value *Src);
815 
816   /// Create a vector float minimum reduction intrinsic of the source
817   /// vector. This variant follows the NaN and signed zero semantic of
818   /// llvm.minimum intrinsic.
819   CallInst *CreateFPMinimumReduce(Value *Src);
820 
821   /// Create a lifetime.start intrinsic.
822   ///
823   /// If the pointer isn't i8* it will be converted.
824   CallInst *CreateLifetimeStart(Value *Ptr, ConstantInt *Size = nullptr);
825 
826   /// Create a lifetime.end intrinsic.
827   ///
828   /// If the pointer isn't i8* it will be converted.
829   CallInst *CreateLifetimeEnd(Value *Ptr, ConstantInt *Size = nullptr);
830 
831   /// Create a call to invariant.start intrinsic.
832   ///
833   /// If the pointer isn't i8* it will be converted.
834   CallInst *CreateInvariantStart(Value *Ptr, ConstantInt *Size = nullptr);
835 
836   /// Create a call to llvm.threadlocal.address intrinsic.
837   CallInst *CreateThreadLocalAddress(Value *Ptr);
838 
839   /// Create a call to Masked Load intrinsic
840   CallInst *CreateMaskedLoad(Type *Ty, Value *Ptr, Align Alignment, Value *Mask,
841                              Value *PassThru = nullptr, const Twine &Name = "");
842 
843   /// Create a call to Masked Store intrinsic
844   CallInst *CreateMaskedStore(Value *Val, Value *Ptr, Align Alignment,
845                               Value *Mask);
846 
847   /// Create a call to Masked Gather intrinsic
848   CallInst *CreateMaskedGather(Type *Ty, Value *Ptrs, Align Alignment,
849                                Value *Mask = nullptr, Value *PassThru = nullptr,
850                                const Twine &Name = "");
851 
852   /// Create a call to Masked Scatter intrinsic
853   CallInst *CreateMaskedScatter(Value *Val, Value *Ptrs, Align Alignment,
854                                 Value *Mask = nullptr);
855 
856   /// Create a call to Masked Expand Load intrinsic
857   CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
858                                    Value *Mask = nullptr,
859                                    Value *PassThru = nullptr,
860                                    const Twine &Name = "");
861 
862   /// Create a call to Masked Compress Store intrinsic
863   CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr, MaybeAlign Align,
864                                       Value *Mask = nullptr);
865 
866   /// Return an all true boolean vector (mask) with \p NumElts lanes.
867   Value *getAllOnesMask(ElementCount NumElts) {
868     VectorType *VTy = VectorType::get(Type::getInt1Ty(Context), NumElts);
869     return Constant::getAllOnesValue(VTy);
870   }
871 
872   /// Create an assume intrinsic call that allows the optimizer to
873   /// assume that the provided condition will be true.
874   ///
875   /// The optional argument \p OpBundles specifies operand bundles that are
876   /// added to the call instruction.
877   CallInst *CreateAssumption(Value *Cond,
878                              ArrayRef<OperandBundleDef> OpBundles = {});
879 
880   /// Create a llvm.experimental.noalias.scope.decl intrinsic call.
881   Instruction *CreateNoAliasScopeDeclaration(Value *Scope);
882   Instruction *CreateNoAliasScopeDeclaration(MDNode *ScopeTag) {
883     return CreateNoAliasScopeDeclaration(
884         MetadataAsValue::get(Context, ScopeTag));
885   }
886 
887   /// Create a call to the experimental.gc.statepoint intrinsic to
888   /// start a new statepoint sequence.
889   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
890                                    FunctionCallee ActualCallee,
891                                    ArrayRef<Value *> CallArgs,
892                                    std::optional<ArrayRef<Value *>> DeoptArgs,
893                                    ArrayRef<Value *> GCArgs,
894                                    const Twine &Name = "");
895 
896   /// Create a call to the experimental.gc.statepoint intrinsic to
897   /// start a new statepoint sequence.
898   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
899                                    FunctionCallee ActualCallee, uint32_t Flags,
900                                    ArrayRef<Value *> CallArgs,
901                                    std::optional<ArrayRef<Use>> TransitionArgs,
902                                    std::optional<ArrayRef<Use>> DeoptArgs,
903                                    ArrayRef<Value *> GCArgs,
904                                    const Twine &Name = "");
905 
906   /// Conveninence function for the common case when CallArgs are filled
907   /// in using ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be
908   /// .get()'ed to get the Value pointer.
909   CallInst *CreateGCStatepointCall(uint64_t ID, uint32_t NumPatchBytes,
910                                    FunctionCallee ActualCallee,
911                                    ArrayRef<Use> CallArgs,
912                                    std::optional<ArrayRef<Value *>> DeoptArgs,
913                                    ArrayRef<Value *> GCArgs,
914                                    const Twine &Name = "");
915 
916   /// Create an invoke to the experimental.gc.statepoint intrinsic to
917   /// start a new statepoint sequence.
918   InvokeInst *
919   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
920                            FunctionCallee ActualInvokee, BasicBlock *NormalDest,
921                            BasicBlock *UnwindDest, ArrayRef<Value *> InvokeArgs,
922                            std::optional<ArrayRef<Value *>> DeoptArgs,
923                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
924 
925   /// Create an invoke to the experimental.gc.statepoint intrinsic to
926   /// start a new statepoint sequence.
927   InvokeInst *CreateGCStatepointInvoke(
928       uint64_t ID, uint32_t NumPatchBytes, FunctionCallee ActualInvokee,
929       BasicBlock *NormalDest, BasicBlock *UnwindDest, uint32_t Flags,
930       ArrayRef<Value *> InvokeArgs, std::optional<ArrayRef<Use>> TransitionArgs,
931       std::optional<ArrayRef<Use>> DeoptArgs, ArrayRef<Value *> GCArgs,
932       const Twine &Name = "");
933 
934   // Convenience function for the common case when CallArgs are filled in using
935   // ArrayRef(CS.arg_begin(), CS.arg_end()); Use needs to be .get()'ed to
936   // get the Value *.
937   InvokeInst *
938   CreateGCStatepointInvoke(uint64_t ID, uint32_t NumPatchBytes,
939                            FunctionCallee ActualInvokee, BasicBlock *NormalDest,
940                            BasicBlock *UnwindDest, ArrayRef<Use> InvokeArgs,
941                            std::optional<ArrayRef<Value *>> DeoptArgs,
942                            ArrayRef<Value *> GCArgs, const Twine &Name = "");
943 
944   /// Create a call to the experimental.gc.result intrinsic to extract
945   /// the result from a call wrapped in a statepoint.
946   CallInst *CreateGCResult(Instruction *Statepoint,
947                            Type *ResultType,
948                            const Twine &Name = "");
949 
950   /// Create a call to the experimental.gc.relocate intrinsics to
951   /// project the relocated value of one pointer from the statepoint.
952   CallInst *CreateGCRelocate(Instruction *Statepoint,
953                              int BaseOffset,
954                              int DerivedOffset,
955                              Type *ResultType,
956                              const Twine &Name = "");
957 
958   /// Create a call to the experimental.gc.pointer.base intrinsic to get the
959   /// base pointer for the specified derived pointer.
960   CallInst *CreateGCGetPointerBase(Value *DerivedPtr, const Twine &Name = "");
961 
962   /// Create a call to the experimental.gc.get.pointer.offset intrinsic to get
963   /// the offset of the specified derived pointer from its base.
964   CallInst *CreateGCGetPointerOffset(Value *DerivedPtr, const Twine &Name = "");
965 
966   /// Create a call to llvm.vscale, multiplied by \p Scaling. The type of VScale
967   /// will be the same type as that of \p Scaling.
968   Value *CreateVScale(Constant *Scaling, const Twine &Name = "");
969 
970   /// Create an expression which evaluates to the number of elements in \p EC
971   /// at runtime.
972   Value *CreateElementCount(Type *DstType, ElementCount EC);
973 
974   /// Create an expression which evaluates to the number of units in \p Size
975   /// at runtime.  This works for both units of bits and bytes.
976   Value *CreateTypeSize(Type *DstType, TypeSize Size);
977 
978   /// Creates a vector of type \p DstType with the linear sequence <0, 1, ...>
979   Value *CreateStepVector(Type *DstType, const Twine &Name = "");
980 
981   /// Create a call to intrinsic \p ID with 1 operand which is mangled on its
982   /// type.
983   CallInst *CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V,
984                                  FMFSource FMFSource = {},
985                                  const Twine &Name = "");
986 
987   /// Create a call to intrinsic \p ID with 2 operands which is mangled on the
988   /// first type.
989   Value *CreateBinaryIntrinsic(Intrinsic::ID ID, Value *LHS, Value *RHS,
990                                FMFSource FMFSource = {},
991                                const Twine &Name = "");
992 
993   /// Create a call to intrinsic \p ID with \p Args, mangled using \p Types. If
994   /// \p FMFSource is provided, copy fast-math-flags from that instruction to
995   /// the intrinsic.
996   CallInst *CreateIntrinsic(Intrinsic::ID ID, ArrayRef<Type *> Types,
997                             ArrayRef<Value *> Args, FMFSource FMFSource = {},
998                             const Twine &Name = "");
999 
1000   /// Create a call to intrinsic \p ID with \p RetTy and \p Args. If
1001   /// \p FMFSource is provided, copy fast-math-flags from that instruction to
1002   /// the intrinsic.
1003   CallInst *CreateIntrinsic(Type *RetTy, Intrinsic::ID ID,
1004                             ArrayRef<Value *> Args, FMFSource FMFSource = {},
1005                             const Twine &Name = "");
1006 
1007   /// Create call to the minnum intrinsic.
1008   Value *CreateMinNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1009     if (IsFPConstrained) {
1010       return CreateConstrainedFPUnroundedBinOp(
1011           Intrinsic::experimental_constrained_minnum, LHS, RHS, nullptr, Name);
1012     }
1013 
1014     return CreateBinaryIntrinsic(Intrinsic::minnum, LHS, RHS, nullptr, Name);
1015   }
1016 
1017   /// Create call to the maxnum intrinsic.
1018   Value *CreateMaxNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1019     if (IsFPConstrained) {
1020       return CreateConstrainedFPUnroundedBinOp(
1021           Intrinsic::experimental_constrained_maxnum, LHS, RHS, nullptr, Name);
1022     }
1023 
1024     return CreateBinaryIntrinsic(Intrinsic::maxnum, LHS, RHS, nullptr, Name);
1025   }
1026 
1027   /// Create call to the minimum intrinsic.
1028   Value *CreateMinimum(Value *LHS, Value *RHS, const Twine &Name = "") {
1029     return CreateBinaryIntrinsic(Intrinsic::minimum, LHS, RHS, nullptr, Name);
1030   }
1031 
1032   /// Create call to the maximum intrinsic.
1033   Value *CreateMaximum(Value *LHS, Value *RHS, const Twine &Name = "") {
1034     return CreateBinaryIntrinsic(Intrinsic::maximum, LHS, RHS, nullptr, Name);
1035   }
1036 
1037   /// Create call to the minimumnum intrinsic.
1038   Value *CreateMinimumNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1039     return CreateBinaryIntrinsic(Intrinsic::minimumnum, LHS, RHS, nullptr,
1040                                  Name);
1041   }
1042 
1043   /// Create call to the maximum intrinsic.
1044   Value *CreateMaximumNum(Value *LHS, Value *RHS, const Twine &Name = "") {
1045     return CreateBinaryIntrinsic(Intrinsic::maximumnum, LHS, RHS, nullptr,
1046                                  Name);
1047   }
1048 
1049   /// Create call to the copysign intrinsic.
1050   Value *CreateCopySign(Value *LHS, Value *RHS, FMFSource FMFSource = {},
1051                         const Twine &Name = "") {
1052     return CreateBinaryIntrinsic(Intrinsic::copysign, LHS, RHS, FMFSource,
1053                                  Name);
1054   }
1055 
1056   /// Create call to the ldexp intrinsic.
1057   Value *CreateLdexp(Value *Src, Value *Exp, FMFSource FMFSource = {},
1058                      const Twine &Name = "") {
1059     assert(!IsFPConstrained && "TODO: Support strictfp");
1060     return CreateIntrinsic(Intrinsic::ldexp, {Src->getType(), Exp->getType()},
1061                            {Src, Exp}, FMFSource, Name);
1062   }
1063 
1064   /// Create a call to the arithmetic_fence intrinsic.
1065   CallInst *CreateArithmeticFence(Value *Val, Type *DstType,
1066                                   const Twine &Name = "") {
1067     return CreateIntrinsic(Intrinsic::arithmetic_fence, DstType, Val, nullptr,
1068                            Name);
1069   }
1070 
1071   /// Create a call to the vector.extract intrinsic.
1072   CallInst *CreateExtractVector(Type *DstType, Value *SrcVec, Value *Idx,
1073                                 const Twine &Name = "") {
1074     return CreateIntrinsic(Intrinsic::vector_extract,
1075                            {DstType, SrcVec->getType()}, {SrcVec, Idx}, nullptr,
1076                            Name);
1077   }
1078 
1079   /// Create a call to the vector.insert intrinsic.
1080   CallInst *CreateInsertVector(Type *DstType, Value *SrcVec, Value *SubVec,
1081                                Value *Idx, const Twine &Name = "") {
1082     return CreateIntrinsic(Intrinsic::vector_insert,
1083                            {DstType, SubVec->getType()}, {SrcVec, SubVec, Idx},
1084                            nullptr, Name);
1085   }
1086 
1087   /// Create a call to llvm.stacksave
1088   CallInst *CreateStackSave(const Twine &Name = "") {
1089     const DataLayout &DL = BB->getDataLayout();
1090     return CreateIntrinsic(Intrinsic::stacksave, {DL.getAllocaPtrType(Context)},
1091                            {}, nullptr, Name);
1092   }
1093 
1094   /// Create a call to llvm.stackrestore
1095   CallInst *CreateStackRestore(Value *Ptr, const Twine &Name = "") {
1096     return CreateIntrinsic(Intrinsic::stackrestore, {Ptr->getType()}, {Ptr},
1097                            nullptr, Name);
1098   }
1099 
1100   /// Create a call to llvm.experimental_cttz_elts
1101   Value *CreateCountTrailingZeroElems(Type *ResTy, Value *Mask,
1102                                       bool ZeroIsPoison = true,
1103                                       const Twine &Name = "") {
1104     return CreateIntrinsic(Intrinsic::experimental_cttz_elts,
1105                            {ResTy, Mask->getType()},
1106                            {Mask, getInt1(ZeroIsPoison)}, nullptr, Name);
1107   }
1108 
1109 private:
1110   /// Create a call to a masked intrinsic with given Id.
1111   CallInst *CreateMaskedIntrinsic(Intrinsic::ID Id, ArrayRef<Value *> Ops,
1112                                   ArrayRef<Type *> OverloadedTypes,
1113                                   const Twine &Name = "");
1114 
1115   //===--------------------------------------------------------------------===//
1116   // Instruction creation methods: Terminators
1117   //===--------------------------------------------------------------------===//
1118 
1119 private:
1120   /// Helper to add branch weight and unpredictable metadata onto an
1121   /// instruction.
1122   /// \returns The annotated instruction.
1123   template <typename InstTy>
1124   InstTy *addBranchMetadata(InstTy *I, MDNode *Weights, MDNode *Unpredictable) {
1125     if (Weights)
1126       I->setMetadata(LLVMContext::MD_prof, Weights);
1127     if (Unpredictable)
1128       I->setMetadata(LLVMContext::MD_unpredictable, Unpredictable);
1129     return I;
1130   }
1131 
1132 public:
1133   /// Create a 'ret void' instruction.
1134   ReturnInst *CreateRetVoid() {
1135     return Insert(ReturnInst::Create(Context));
1136   }
1137 
1138   /// Create a 'ret <val>' instruction.
1139   ReturnInst *CreateRet(Value *V) {
1140     return Insert(ReturnInst::Create(Context, V));
1141   }
1142 
1143   /// Create a sequence of N insertvalue instructions,
1144   /// with one Value from the retVals array each, that build a aggregate
1145   /// return value one value at a time, and a ret instruction to return
1146   /// the resulting aggregate value.
1147   ///
1148   /// This is a convenience function for code that uses aggregate return values
1149   /// as a vehicle for having multiple return values.
1150   ReturnInst *CreateAggregateRet(Value *const *retVals, unsigned N) {
1151     Value *V = PoisonValue::get(getCurrentFunctionReturnType());
1152     for (unsigned i = 0; i != N; ++i)
1153       V = CreateInsertValue(V, retVals[i], i, "mrv");
1154     return Insert(ReturnInst::Create(Context, V));
1155   }
1156 
1157   /// Create an unconditional 'br label X' instruction.
1158   BranchInst *CreateBr(BasicBlock *Dest) {
1159     return Insert(BranchInst::Create(Dest));
1160   }
1161 
1162   /// Create a conditional 'br Cond, TrueDest, FalseDest'
1163   /// instruction.
1164   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1165                            MDNode *BranchWeights = nullptr,
1166                            MDNode *Unpredictable = nullptr) {
1167     return Insert(addBranchMetadata(BranchInst::Create(True, False, Cond),
1168                                     BranchWeights, Unpredictable));
1169   }
1170 
1171   /// Create a conditional 'br Cond, TrueDest, FalseDest'
1172   /// instruction. Copy branch meta data if available.
1173   BranchInst *CreateCondBr(Value *Cond, BasicBlock *True, BasicBlock *False,
1174                            Instruction *MDSrc) {
1175     BranchInst *Br = BranchInst::Create(True, False, Cond);
1176     if (MDSrc) {
1177       unsigned WL[4] = {LLVMContext::MD_prof, LLVMContext::MD_unpredictable,
1178                         LLVMContext::MD_make_implicit, LLVMContext::MD_dbg};
1179       Br->copyMetadata(*MDSrc, WL);
1180     }
1181     return Insert(Br);
1182   }
1183 
1184   /// Create a switch instruction with the specified value, default dest,
1185   /// and with a hint for the number of cases that will be added (for efficient
1186   /// allocation).
1187   SwitchInst *CreateSwitch(Value *V, BasicBlock *Dest, unsigned NumCases = 10,
1188                            MDNode *BranchWeights = nullptr,
1189                            MDNode *Unpredictable = nullptr) {
1190     return Insert(addBranchMetadata(SwitchInst::Create(V, Dest, NumCases),
1191                                     BranchWeights, Unpredictable));
1192   }
1193 
1194   /// Create an indirect branch instruction with the specified address
1195   /// operand, with an optional hint for the number of destinations that will be
1196   /// added (for efficient allocation).
1197   IndirectBrInst *CreateIndirectBr(Value *Addr, unsigned NumDests = 10) {
1198     return Insert(IndirectBrInst::Create(Addr, NumDests));
1199   }
1200 
1201   /// Create an invoke instruction.
1202   InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1203                            BasicBlock *NormalDest, BasicBlock *UnwindDest,
1204                            ArrayRef<Value *> Args,
1205                            ArrayRef<OperandBundleDef> OpBundles,
1206                            const Twine &Name = "") {
1207     InvokeInst *II =
1208         InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args, OpBundles);
1209     if (IsFPConstrained)
1210       setConstrainedFPCallAttr(II);
1211     return Insert(II, Name);
1212   }
1213   InvokeInst *CreateInvoke(FunctionType *Ty, Value *Callee,
1214                            BasicBlock *NormalDest, BasicBlock *UnwindDest,
1215                            ArrayRef<Value *> Args = {},
1216                            const Twine &Name = "") {
1217     InvokeInst *II =
1218         InvokeInst::Create(Ty, Callee, NormalDest, UnwindDest, Args);
1219     if (IsFPConstrained)
1220       setConstrainedFPCallAttr(II);
1221     return Insert(II, Name);
1222   }
1223 
1224   InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1225                            BasicBlock *UnwindDest, ArrayRef<Value *> Args,
1226                            ArrayRef<OperandBundleDef> OpBundles,
1227                            const Twine &Name = "") {
1228     return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1229                         NormalDest, UnwindDest, Args, OpBundles, Name);
1230   }
1231 
1232   InvokeInst *CreateInvoke(FunctionCallee Callee, BasicBlock *NormalDest,
1233                            BasicBlock *UnwindDest, ArrayRef<Value *> Args = {},
1234                            const Twine &Name = "") {
1235     return CreateInvoke(Callee.getFunctionType(), Callee.getCallee(),
1236                         NormalDest, UnwindDest, Args, Name);
1237   }
1238 
1239   /// \brief Create a callbr instruction.
1240   CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1241                            BasicBlock *DefaultDest,
1242                            ArrayRef<BasicBlock *> IndirectDests,
1243                            ArrayRef<Value *> Args = {},
1244                            const Twine &Name = "") {
1245     return Insert(CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests,
1246                                      Args), Name);
1247   }
1248   CallBrInst *CreateCallBr(FunctionType *Ty, Value *Callee,
1249                            BasicBlock *DefaultDest,
1250                            ArrayRef<BasicBlock *> IndirectDests,
1251                            ArrayRef<Value *> Args,
1252                            ArrayRef<OperandBundleDef> OpBundles,
1253                            const Twine &Name = "") {
1254     return Insert(
1255         CallBrInst::Create(Ty, Callee, DefaultDest, IndirectDests, Args,
1256                            OpBundles), Name);
1257   }
1258 
1259   CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1260                            ArrayRef<BasicBlock *> IndirectDests,
1261                            ArrayRef<Value *> Args = {},
1262                            const Twine &Name = "") {
1263     return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1264                         DefaultDest, IndirectDests, Args, Name);
1265   }
1266   CallBrInst *CreateCallBr(FunctionCallee Callee, BasicBlock *DefaultDest,
1267                            ArrayRef<BasicBlock *> IndirectDests,
1268                            ArrayRef<Value *> Args,
1269                            ArrayRef<OperandBundleDef> OpBundles,
1270                            const Twine &Name = "") {
1271     return CreateCallBr(Callee.getFunctionType(), Callee.getCallee(),
1272                         DefaultDest, IndirectDests, Args, Name);
1273   }
1274 
1275   ResumeInst *CreateResume(Value *Exn) {
1276     return Insert(ResumeInst::Create(Exn));
1277   }
1278 
1279   CleanupReturnInst *CreateCleanupRet(CleanupPadInst *CleanupPad,
1280                                       BasicBlock *UnwindBB = nullptr) {
1281     return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
1282   }
1283 
1284   CatchSwitchInst *CreateCatchSwitch(Value *ParentPad, BasicBlock *UnwindBB,
1285                                      unsigned NumHandlers,
1286                                      const Twine &Name = "") {
1287     return Insert(CatchSwitchInst::Create(ParentPad, UnwindBB, NumHandlers),
1288                   Name);
1289   }
1290 
1291   CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef<Value *> Args,
1292                                const Twine &Name = "") {
1293     return Insert(CatchPadInst::Create(ParentPad, Args), Name);
1294   }
1295 
1296   CleanupPadInst *CreateCleanupPad(Value *ParentPad,
1297                                    ArrayRef<Value *> Args = {},
1298                                    const Twine &Name = "") {
1299     return Insert(CleanupPadInst::Create(ParentPad, Args), Name);
1300   }
1301 
1302   CatchReturnInst *CreateCatchRet(CatchPadInst *CatchPad, BasicBlock *BB) {
1303     return Insert(CatchReturnInst::Create(CatchPad, BB));
1304   }
1305 
1306   UnreachableInst *CreateUnreachable() {
1307     return Insert(new UnreachableInst(Context));
1308   }
1309 
1310   //===--------------------------------------------------------------------===//
1311   // Instruction creation methods: Binary Operators
1312   //===--------------------------------------------------------------------===//
1313 private:
1314   BinaryOperator *CreateInsertNUWNSWBinOp(BinaryOperator::BinaryOps Opc,
1315                                           Value *LHS, Value *RHS,
1316                                           const Twine &Name,
1317                                           bool HasNUW, bool HasNSW) {
1318     BinaryOperator *BO = Insert(BinaryOperator::Create(Opc, LHS, RHS), Name);
1319     if (HasNUW) BO->setHasNoUnsignedWrap();
1320     if (HasNSW) BO->setHasNoSignedWrap();
1321     return BO;
1322   }
1323 
1324   Instruction *setFPAttrs(Instruction *I, MDNode *FPMD,
1325                           FastMathFlags FMF) const {
1326     if (!FPMD)
1327       FPMD = DefaultFPMathTag;
1328     if (FPMD)
1329       I->setMetadata(LLVMContext::MD_fpmath, FPMD);
1330     I->setFastMathFlags(FMF);
1331     return I;
1332   }
1333 
1334   Value *getConstrainedFPRounding(std::optional<RoundingMode> Rounding) {
1335     RoundingMode UseRounding = DefaultConstrainedRounding;
1336 
1337     if (Rounding)
1338       UseRounding = *Rounding;
1339 
1340     std::optional<StringRef> RoundingStr =
1341         convertRoundingModeToStr(UseRounding);
1342     assert(RoundingStr && "Garbage strict rounding mode!");
1343     auto *RoundingMDS = MDString::get(Context, *RoundingStr);
1344 
1345     return MetadataAsValue::get(Context, RoundingMDS);
1346   }
1347 
1348   Value *getConstrainedFPExcept(std::optional<fp::ExceptionBehavior> Except) {
1349     std::optional<StringRef> ExceptStr = convertExceptionBehaviorToStr(
1350         Except.value_or(DefaultConstrainedExcept));
1351     assert(ExceptStr && "Garbage strict exception behavior!");
1352     auto *ExceptMDS = MDString::get(Context, *ExceptStr);
1353 
1354     return MetadataAsValue::get(Context, ExceptMDS);
1355   }
1356 
1357   Value *getConstrainedFPPredicate(CmpInst::Predicate Predicate) {
1358     assert(CmpInst::isFPPredicate(Predicate) &&
1359            Predicate != CmpInst::FCMP_FALSE &&
1360            Predicate != CmpInst::FCMP_TRUE &&
1361            "Invalid constrained FP comparison predicate!");
1362 
1363     StringRef PredicateStr = CmpInst::getPredicateName(Predicate);
1364     auto *PredicateMDS = MDString::get(Context, PredicateStr);
1365 
1366     return MetadataAsValue::get(Context, PredicateMDS);
1367   }
1368 
1369 public:
1370   Value *CreateAdd(Value *LHS, Value *RHS, const Twine &Name = "",
1371                    bool HasNUW = false, bool HasNSW = false) {
1372     if (Value *V =
1373             Folder.FoldNoWrapBinOp(Instruction::Add, LHS, RHS, HasNUW, HasNSW))
1374       return V;
1375     return CreateInsertNUWNSWBinOp(Instruction::Add, LHS, RHS, Name, HasNUW,
1376                                    HasNSW);
1377   }
1378 
1379   Value *CreateNSWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1380     return CreateAdd(LHS, RHS, Name, false, true);
1381   }
1382 
1383   Value *CreateNUWAdd(Value *LHS, Value *RHS, const Twine &Name = "") {
1384     return CreateAdd(LHS, RHS, Name, true, false);
1385   }
1386 
1387   Value *CreateSub(Value *LHS, Value *RHS, const Twine &Name = "",
1388                    bool HasNUW = false, bool HasNSW = false) {
1389     if (Value *V =
1390             Folder.FoldNoWrapBinOp(Instruction::Sub, LHS, RHS, HasNUW, HasNSW))
1391       return V;
1392     return CreateInsertNUWNSWBinOp(Instruction::Sub, LHS, RHS, Name, HasNUW,
1393                                    HasNSW);
1394   }
1395 
1396   Value *CreateNSWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1397     return CreateSub(LHS, RHS, Name, false, true);
1398   }
1399 
1400   Value *CreateNUWSub(Value *LHS, Value *RHS, const Twine &Name = "") {
1401     return CreateSub(LHS, RHS, Name, true, false);
1402   }
1403 
1404   Value *CreateMul(Value *LHS, Value *RHS, const Twine &Name = "",
1405                    bool HasNUW = false, bool HasNSW = false) {
1406     if (Value *V =
1407             Folder.FoldNoWrapBinOp(Instruction::Mul, LHS, RHS, HasNUW, HasNSW))
1408       return V;
1409     return CreateInsertNUWNSWBinOp(Instruction::Mul, LHS, RHS, Name, HasNUW,
1410                                    HasNSW);
1411   }
1412 
1413   Value *CreateNSWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1414     return CreateMul(LHS, RHS, Name, false, true);
1415   }
1416 
1417   Value *CreateNUWMul(Value *LHS, Value *RHS, const Twine &Name = "") {
1418     return CreateMul(LHS, RHS, Name, true, false);
1419   }
1420 
1421   Value *CreateUDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1422                     bool isExact = false) {
1423     if (Value *V = Folder.FoldExactBinOp(Instruction::UDiv, LHS, RHS, isExact))
1424       return V;
1425     if (!isExact)
1426       return Insert(BinaryOperator::CreateUDiv(LHS, RHS), Name);
1427     return Insert(BinaryOperator::CreateExactUDiv(LHS, RHS), Name);
1428   }
1429 
1430   Value *CreateExactUDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1431     return CreateUDiv(LHS, RHS, Name, true);
1432   }
1433 
1434   Value *CreateSDiv(Value *LHS, Value *RHS, const Twine &Name = "",
1435                     bool isExact = false) {
1436     if (Value *V = Folder.FoldExactBinOp(Instruction::SDiv, LHS, RHS, isExact))
1437       return V;
1438     if (!isExact)
1439       return Insert(BinaryOperator::CreateSDiv(LHS, RHS), Name);
1440     return Insert(BinaryOperator::CreateExactSDiv(LHS, RHS), Name);
1441   }
1442 
1443   Value *CreateExactSDiv(Value *LHS, Value *RHS, const Twine &Name = "") {
1444     return CreateSDiv(LHS, RHS, Name, true);
1445   }
1446 
1447   Value *CreateURem(Value *LHS, Value *RHS, const Twine &Name = "") {
1448     if (Value *V = Folder.FoldBinOp(Instruction::URem, LHS, RHS))
1449       return V;
1450     return Insert(BinaryOperator::CreateURem(LHS, RHS), Name);
1451   }
1452 
1453   Value *CreateSRem(Value *LHS, Value *RHS, const Twine &Name = "") {
1454     if (Value *V = Folder.FoldBinOp(Instruction::SRem, LHS, RHS))
1455       return V;
1456     return Insert(BinaryOperator::CreateSRem(LHS, RHS), Name);
1457   }
1458 
1459   Value *CreateShl(Value *LHS, Value *RHS, const Twine &Name = "",
1460                    bool HasNUW = false, bool HasNSW = false) {
1461     if (Value *V =
1462             Folder.FoldNoWrapBinOp(Instruction::Shl, LHS, RHS, HasNUW, HasNSW))
1463       return V;
1464     return CreateInsertNUWNSWBinOp(Instruction::Shl, LHS, RHS, Name,
1465                                    HasNUW, HasNSW);
1466   }
1467 
1468   Value *CreateShl(Value *LHS, const APInt &RHS, const Twine &Name = "",
1469                    bool HasNUW = false, bool HasNSW = false) {
1470     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1471                      HasNUW, HasNSW);
1472   }
1473 
1474   Value *CreateShl(Value *LHS, uint64_t RHS, const Twine &Name = "",
1475                    bool HasNUW = false, bool HasNSW = false) {
1476     return CreateShl(LHS, ConstantInt::get(LHS->getType(), RHS), Name,
1477                      HasNUW, HasNSW);
1478   }
1479 
1480   Value *CreateLShr(Value *LHS, Value *RHS, const Twine &Name = "",
1481                     bool isExact = false) {
1482     if (Value *V = Folder.FoldExactBinOp(Instruction::LShr, LHS, RHS, isExact))
1483       return V;
1484     if (!isExact)
1485       return Insert(BinaryOperator::CreateLShr(LHS, RHS), Name);
1486     return Insert(BinaryOperator::CreateExactLShr(LHS, RHS), Name);
1487   }
1488 
1489   Value *CreateLShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1490                     bool isExact = false) {
1491     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1492   }
1493 
1494   Value *CreateLShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1495                     bool isExact = false) {
1496     return CreateLShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1497   }
1498 
1499   Value *CreateAShr(Value *LHS, Value *RHS, const Twine &Name = "",
1500                     bool isExact = false) {
1501     if (Value *V = Folder.FoldExactBinOp(Instruction::AShr, LHS, RHS, isExact))
1502       return V;
1503     if (!isExact)
1504       return Insert(BinaryOperator::CreateAShr(LHS, RHS), Name);
1505     return Insert(BinaryOperator::CreateExactAShr(LHS, RHS), Name);
1506   }
1507 
1508   Value *CreateAShr(Value *LHS, const APInt &RHS, const Twine &Name = "",
1509                     bool isExact = false) {
1510     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1511   }
1512 
1513   Value *CreateAShr(Value *LHS, uint64_t RHS, const Twine &Name = "",
1514                     bool isExact = false) {
1515     return CreateAShr(LHS, ConstantInt::get(LHS->getType(), RHS), Name,isExact);
1516   }
1517 
1518   Value *CreateAnd(Value *LHS, Value *RHS, const Twine &Name = "") {
1519     if (auto *V = Folder.FoldBinOp(Instruction::And, LHS, RHS))
1520       return V;
1521     return Insert(BinaryOperator::CreateAnd(LHS, RHS), Name);
1522   }
1523 
1524   Value *CreateAnd(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1525     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1526   }
1527 
1528   Value *CreateAnd(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1529     return CreateAnd(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1530   }
1531 
1532   Value *CreateAnd(ArrayRef<Value*> Ops) {
1533     assert(!Ops.empty());
1534     Value *Accum = Ops[0];
1535     for (unsigned i = 1; i < Ops.size(); i++)
1536       Accum = CreateAnd(Accum, Ops[i]);
1537     return Accum;
1538   }
1539 
1540   Value *CreateOr(Value *LHS, Value *RHS, const Twine &Name = "") {
1541     if (auto *V = Folder.FoldBinOp(Instruction::Or, LHS, RHS))
1542       return V;
1543     return Insert(BinaryOperator::CreateOr(LHS, RHS), Name);
1544   }
1545 
1546   Value *CreateOr(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1547     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1548   }
1549 
1550   Value *CreateOr(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1551     return CreateOr(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1552   }
1553 
1554   Value *CreateOr(ArrayRef<Value*> Ops) {
1555     assert(!Ops.empty());
1556     Value *Accum = Ops[0];
1557     for (unsigned i = 1; i < Ops.size(); i++)
1558       Accum = CreateOr(Accum, Ops[i]);
1559     return Accum;
1560   }
1561 
1562   Value *CreateXor(Value *LHS, Value *RHS, const Twine &Name = "") {
1563     if (Value *V = Folder.FoldBinOp(Instruction::Xor, LHS, RHS))
1564       return V;
1565     return Insert(BinaryOperator::CreateXor(LHS, RHS), Name);
1566   }
1567 
1568   Value *CreateXor(Value *LHS, const APInt &RHS, const Twine &Name = "") {
1569     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1570   }
1571 
1572   Value *CreateXor(Value *LHS, uint64_t RHS, const Twine &Name = "") {
1573     return CreateXor(LHS, ConstantInt::get(LHS->getType(), RHS), Name);
1574   }
1575 
1576   Value *CreateFAdd(Value *L, Value *R, const Twine &Name = "",
1577                     MDNode *FPMD = nullptr) {
1578     return CreateFAddFMF(L, R, {}, Name, FPMD);
1579   }
1580 
1581   Value *CreateFAddFMF(Value *L, Value *R, FMFSource FMFSource,
1582                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1583     if (IsFPConstrained)
1584       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fadd,
1585                                       L, R, FMFSource, Name, FPMD);
1586 
1587     if (Value *V =
1588             Folder.FoldBinOpFMF(Instruction::FAdd, L, R, FMFSource.get(FMF)))
1589       return V;
1590     Instruction *I =
1591         setFPAttrs(BinaryOperator::CreateFAdd(L, R), FPMD, FMFSource.get(FMF));
1592     return Insert(I, Name);
1593   }
1594 
1595   Value *CreateFSub(Value *L, Value *R, const Twine &Name = "",
1596                     MDNode *FPMD = nullptr) {
1597     return CreateFSubFMF(L, R, {}, Name, FPMD);
1598   }
1599 
1600   Value *CreateFSubFMF(Value *L, Value *R, FMFSource FMFSource,
1601                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1602     if (IsFPConstrained)
1603       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fsub,
1604                                       L, R, FMFSource, Name, FPMD);
1605 
1606     if (Value *V =
1607             Folder.FoldBinOpFMF(Instruction::FSub, L, R, FMFSource.get(FMF)))
1608       return V;
1609     Instruction *I =
1610         setFPAttrs(BinaryOperator::CreateFSub(L, R), FPMD, FMFSource.get(FMF));
1611     return Insert(I, Name);
1612   }
1613 
1614   Value *CreateFMul(Value *L, Value *R, const Twine &Name = "",
1615                     MDNode *FPMD = nullptr) {
1616     return CreateFMulFMF(L, R, {}, Name, FPMD);
1617   }
1618 
1619   Value *CreateFMulFMF(Value *L, Value *R, FMFSource FMFSource,
1620                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1621     if (IsFPConstrained)
1622       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fmul,
1623                                       L, R, FMFSource, Name, FPMD);
1624 
1625     if (Value *V =
1626             Folder.FoldBinOpFMF(Instruction::FMul, L, R, FMFSource.get(FMF)))
1627       return V;
1628     Instruction *I =
1629         setFPAttrs(BinaryOperator::CreateFMul(L, R), FPMD, FMFSource.get(FMF));
1630     return Insert(I, Name);
1631   }
1632 
1633   Value *CreateFDiv(Value *L, Value *R, const Twine &Name = "",
1634                     MDNode *FPMD = nullptr) {
1635     return CreateFDivFMF(L, R, {}, Name, FPMD);
1636   }
1637 
1638   Value *CreateFDivFMF(Value *L, Value *R, FMFSource FMFSource,
1639                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1640     if (IsFPConstrained)
1641       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_fdiv,
1642                                       L, R, FMFSource, Name, FPMD);
1643 
1644     if (Value *V =
1645             Folder.FoldBinOpFMF(Instruction::FDiv, L, R, FMFSource.get(FMF)))
1646       return V;
1647     Instruction *I =
1648         setFPAttrs(BinaryOperator::CreateFDiv(L, R), FPMD, FMFSource.get(FMF));
1649     return Insert(I, Name);
1650   }
1651 
1652   Value *CreateFRem(Value *L, Value *R, const Twine &Name = "",
1653                     MDNode *FPMD = nullptr) {
1654     return CreateFRemFMF(L, R, {}, Name, FPMD);
1655   }
1656 
1657   Value *CreateFRemFMF(Value *L, Value *R, FMFSource FMFSource,
1658                        const Twine &Name = "", MDNode *FPMD = nullptr) {
1659     if (IsFPConstrained)
1660       return CreateConstrainedFPBinOp(Intrinsic::experimental_constrained_frem,
1661                                       L, R, FMFSource, Name, FPMD);
1662 
1663     if (Value *V =
1664             Folder.FoldBinOpFMF(Instruction::FRem, L, R, FMFSource.get(FMF)))
1665       return V;
1666     Instruction *I =
1667         setFPAttrs(BinaryOperator::CreateFRem(L, R), FPMD, FMFSource.get(FMF));
1668     return Insert(I, Name);
1669   }
1670 
1671   Value *CreateBinOp(Instruction::BinaryOps Opc,
1672                      Value *LHS, Value *RHS, const Twine &Name = "",
1673                      MDNode *FPMathTag = nullptr) {
1674     return CreateBinOpFMF(Opc, LHS, RHS, {}, Name, FPMathTag);
1675   }
1676 
1677   Value *CreateBinOpFMF(Instruction::BinaryOps Opc, Value *LHS, Value *RHS,
1678                         FMFSource FMFSource, const Twine &Name = "",
1679                         MDNode *FPMathTag = nullptr) {
1680     if (Value *V = Folder.FoldBinOp(Opc, LHS, RHS))
1681       return V;
1682     Instruction *BinOp = BinaryOperator::Create(Opc, LHS, RHS);
1683     if (isa<FPMathOperator>(BinOp))
1684       setFPAttrs(BinOp, FPMathTag, FMFSource.get(FMF));
1685     return Insert(BinOp, Name);
1686   }
1687 
1688   Value *CreateLogicalAnd(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1689     assert(Cond2->getType()->isIntOrIntVectorTy(1));
1690     return CreateSelect(Cond1, Cond2,
1691                         ConstantInt::getNullValue(Cond2->getType()), Name);
1692   }
1693 
1694   Value *CreateLogicalOr(Value *Cond1, Value *Cond2, const Twine &Name = "") {
1695     assert(Cond2->getType()->isIntOrIntVectorTy(1));
1696     return CreateSelect(Cond1, ConstantInt::getAllOnesValue(Cond2->getType()),
1697                         Cond2, Name);
1698   }
1699 
1700   Value *CreateLogicalOp(Instruction::BinaryOps Opc, Value *Cond1, Value *Cond2,
1701                          const Twine &Name = "") {
1702     switch (Opc) {
1703     case Instruction::And:
1704       return CreateLogicalAnd(Cond1, Cond2, Name);
1705     case Instruction::Or:
1706       return CreateLogicalOr(Cond1, Cond2, Name);
1707     default:
1708       break;
1709     }
1710     llvm_unreachable("Not a logical operation.");
1711   }
1712 
1713   // NOTE: this is sequential, non-commutative, ordered reduction!
1714   Value *CreateLogicalOr(ArrayRef<Value *> Ops) {
1715     assert(!Ops.empty());
1716     Value *Accum = Ops[0];
1717     for (unsigned i = 1; i < Ops.size(); i++)
1718       Accum = CreateLogicalOr(Accum, Ops[i]);
1719     return Accum;
1720   }
1721 
1722   CallInst *CreateConstrainedFPBinOp(
1723       Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource = {},
1724       const Twine &Name = "", MDNode *FPMathTag = nullptr,
1725       std::optional<RoundingMode> Rounding = std::nullopt,
1726       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1727 
1728   CallInst *CreateConstrainedFPUnroundedBinOp(
1729       Intrinsic::ID ID, Value *L, Value *R, FMFSource FMFSource = {},
1730       const Twine &Name = "", MDNode *FPMathTag = nullptr,
1731       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
1732 
1733   Value *CreateNeg(Value *V, const Twine &Name = "", bool HasNSW = false) {
1734     return CreateSub(Constant::getNullValue(V->getType()), V, Name,
1735                      /*HasNUW=*/0, HasNSW);
1736   }
1737 
1738   Value *CreateNSWNeg(Value *V, const Twine &Name = "") {
1739     return CreateNeg(V, Name, /*HasNSW=*/true);
1740   }
1741 
1742   Value *CreateFNeg(Value *V, const Twine &Name = "",
1743                     MDNode *FPMathTag = nullptr) {
1744     return CreateFNegFMF(V, {}, Name, FPMathTag);
1745   }
1746 
1747   Value *CreateFNegFMF(Value *V, FMFSource FMFSource, const Twine &Name = "",
1748                        MDNode *FPMathTag = nullptr) {
1749     if (Value *Res =
1750             Folder.FoldUnOpFMF(Instruction::FNeg, V, FMFSource.get(FMF)))
1751       return Res;
1752     return Insert(
1753         setFPAttrs(UnaryOperator::CreateFNeg(V), FPMathTag, FMFSource.get(FMF)),
1754         Name);
1755   }
1756 
1757   Value *CreateNot(Value *V, const Twine &Name = "") {
1758     return CreateXor(V, Constant::getAllOnesValue(V->getType()), Name);
1759   }
1760 
1761   Value *CreateUnOp(Instruction::UnaryOps Opc,
1762                     Value *V, const Twine &Name = "",
1763                     MDNode *FPMathTag = nullptr) {
1764     if (Value *Res = Folder.FoldUnOpFMF(Opc, V, FMF))
1765       return Res;
1766     Instruction *UnOp = UnaryOperator::Create(Opc, V);
1767     if (isa<FPMathOperator>(UnOp))
1768       setFPAttrs(UnOp, FPMathTag, FMF);
1769     return Insert(UnOp, Name);
1770   }
1771 
1772   /// Create either a UnaryOperator or BinaryOperator depending on \p Opc.
1773   /// Correct number of operands must be passed accordingly.
1774   Value *CreateNAryOp(unsigned Opc, ArrayRef<Value *> Ops,
1775                       const Twine &Name = "", MDNode *FPMathTag = nullptr);
1776 
1777   //===--------------------------------------------------------------------===//
1778   // Instruction creation methods: Memory Instructions
1779   //===--------------------------------------------------------------------===//
1780 
1781   AllocaInst *CreateAlloca(Type *Ty, unsigned AddrSpace,
1782                            Value *ArraySize = nullptr, const Twine &Name = "") {
1783     const DataLayout &DL = BB->getDataLayout();
1784     Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1785     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1786   }
1787 
1788   AllocaInst *CreateAlloca(Type *Ty, Value *ArraySize = nullptr,
1789                            const Twine &Name = "") {
1790     const DataLayout &DL = BB->getDataLayout();
1791     Align AllocaAlign = DL.getPrefTypeAlign(Ty);
1792     unsigned AddrSpace = DL.getAllocaAddrSpace();
1793     return Insert(new AllocaInst(Ty, AddrSpace, ArraySize, AllocaAlign), Name);
1794   }
1795 
1796   /// Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of
1797   /// converting the string to 'bool' for the isVolatile parameter.
1798   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const char *Name) {
1799     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1800   }
1801 
1802   LoadInst *CreateLoad(Type *Ty, Value *Ptr, const Twine &Name = "") {
1803     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), Name);
1804   }
1805 
1806   LoadInst *CreateLoad(Type *Ty, Value *Ptr, bool isVolatile,
1807                        const Twine &Name = "") {
1808     return CreateAlignedLoad(Ty, Ptr, MaybeAlign(), isVolatile, Name);
1809   }
1810 
1811   StoreInst *CreateStore(Value *Val, Value *Ptr, bool isVolatile = false) {
1812     return CreateAlignedStore(Val, Ptr, MaybeAlign(), isVolatile);
1813   }
1814 
1815   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1816                               const char *Name) {
1817     return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1818   }
1819 
1820   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1821                               const Twine &Name = "") {
1822     return CreateAlignedLoad(Ty, Ptr, Align, /*isVolatile*/false, Name);
1823   }
1824 
1825   LoadInst *CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
1826                               bool isVolatile, const Twine &Name = "") {
1827     if (!Align) {
1828       const DataLayout &DL = BB->getDataLayout();
1829       Align = DL.getABITypeAlign(Ty);
1830     }
1831     return Insert(new LoadInst(Ty, Ptr, Twine(), isVolatile, *Align), Name);
1832   }
1833 
1834   StoreInst *CreateAlignedStore(Value *Val, Value *Ptr, MaybeAlign Align,
1835                                 bool isVolatile = false) {
1836     if (!Align) {
1837       const DataLayout &DL = BB->getDataLayout();
1838       Align = DL.getABITypeAlign(Val->getType());
1839     }
1840     return Insert(new StoreInst(Val, Ptr, isVolatile, *Align));
1841   }
1842   FenceInst *CreateFence(AtomicOrdering Ordering,
1843                          SyncScope::ID SSID = SyncScope::System,
1844                          const Twine &Name = "") {
1845     return Insert(new FenceInst(Context, Ordering, SSID), Name);
1846   }
1847 
1848   AtomicCmpXchgInst *
1849   CreateAtomicCmpXchg(Value *Ptr, Value *Cmp, Value *New, MaybeAlign Align,
1850                       AtomicOrdering SuccessOrdering,
1851                       AtomicOrdering FailureOrdering,
1852                       SyncScope::ID SSID = SyncScope::System) {
1853     if (!Align) {
1854       const DataLayout &DL = BB->getDataLayout();
1855       Align = llvm::Align(DL.getTypeStoreSize(New->getType()));
1856     }
1857 
1858     return Insert(new AtomicCmpXchgInst(Ptr, Cmp, New, *Align, SuccessOrdering,
1859                                         FailureOrdering, SSID));
1860   }
1861 
1862   AtomicRMWInst *CreateAtomicRMW(AtomicRMWInst::BinOp Op, Value *Ptr,
1863                                  Value *Val, MaybeAlign Align,
1864                                  AtomicOrdering Ordering,
1865                                  SyncScope::ID SSID = SyncScope::System) {
1866     if (!Align) {
1867       const DataLayout &DL = BB->getDataLayout();
1868       Align = llvm::Align(DL.getTypeStoreSize(Val->getType()));
1869     }
1870 
1871     return Insert(new AtomicRMWInst(Op, Ptr, Val, *Align, Ordering, SSID));
1872   }
1873 
1874   Value *CreateGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1875                    const Twine &Name = "",
1876                    GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1877     if (auto *V = Folder.FoldGEP(Ty, Ptr, IdxList, NW))
1878       return V;
1879     return Insert(GetElementPtrInst::Create(Ty, Ptr, IdxList, NW), Name);
1880   }
1881 
1882   Value *CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
1883                            const Twine &Name = "") {
1884     return CreateGEP(Ty, Ptr, IdxList, Name, GEPNoWrapFlags::inBounds());
1885   }
1886 
1887   Value *CreateConstGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1888                             const Twine &Name = "") {
1889     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1890 
1891     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::none()))
1892       return V;
1893 
1894     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1895   }
1896 
1897   Value *CreateConstInBoundsGEP1_32(Type *Ty, Value *Ptr, unsigned Idx0,
1898                                     const Twine &Name = "") {
1899     Value *Idx = ConstantInt::get(Type::getInt32Ty(Context), Idx0);
1900 
1901     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::inBounds()))
1902       return V;
1903 
1904     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1905   }
1906 
1907   Value *CreateConstGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0, unsigned Idx1,
1908                             const Twine &Name = "",
1909                             GEPNoWrapFlags NWFlags = GEPNoWrapFlags::none()) {
1910     Value *Idxs[] = {
1911       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1912       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1913     };
1914 
1915     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, NWFlags))
1916       return V;
1917 
1918     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs, NWFlags), Name);
1919   }
1920 
1921   Value *CreateConstInBoundsGEP2_32(Type *Ty, Value *Ptr, unsigned Idx0,
1922                                     unsigned Idx1, const Twine &Name = "") {
1923     Value *Idxs[] = {
1924       ConstantInt::get(Type::getInt32Ty(Context), Idx0),
1925       ConstantInt::get(Type::getInt32Ty(Context), Idx1)
1926     };
1927 
1928     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::inBounds()))
1929       return V;
1930 
1931     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1932   }
1933 
1934   Value *CreateConstGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1935                             const Twine &Name = "") {
1936     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1937 
1938     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::none()))
1939       return V;
1940 
1941     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idx), Name);
1942   }
1943 
1944   Value *CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1945                                     const Twine &Name = "") {
1946     Value *Idx = ConstantInt::get(Type::getInt64Ty(Context), Idx0);
1947 
1948     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idx, GEPNoWrapFlags::inBounds()))
1949       return V;
1950 
1951     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idx), Name);
1952   }
1953 
1954   Value *CreateConstGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0, uint64_t Idx1,
1955                             const Twine &Name = "") {
1956     Value *Idxs[] = {
1957       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1958       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1959     };
1960 
1961     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::none()))
1962       return V;
1963 
1964     return Insert(GetElementPtrInst::Create(Ty, Ptr, Idxs), Name);
1965   }
1966 
1967   Value *CreateConstInBoundsGEP2_64(Type *Ty, Value *Ptr, uint64_t Idx0,
1968                                     uint64_t Idx1, const Twine &Name = "") {
1969     Value *Idxs[] = {
1970       ConstantInt::get(Type::getInt64Ty(Context), Idx0),
1971       ConstantInt::get(Type::getInt64Ty(Context), Idx1)
1972     };
1973 
1974     if (auto *V = Folder.FoldGEP(Ty, Ptr, Idxs, GEPNoWrapFlags::inBounds()))
1975       return V;
1976 
1977     return Insert(GetElementPtrInst::CreateInBounds(Ty, Ptr, Idxs), Name);
1978   }
1979 
1980   Value *CreateStructGEP(Type *Ty, Value *Ptr, unsigned Idx,
1981                          const Twine &Name = "") {
1982     GEPNoWrapFlags NWFlags =
1983         GEPNoWrapFlags::inBounds() | GEPNoWrapFlags::noUnsignedWrap();
1984     return CreateConstGEP2_32(Ty, Ptr, 0, Idx, Name, NWFlags);
1985   }
1986 
1987   Value *CreatePtrAdd(Value *Ptr, Value *Offset, const Twine &Name = "",
1988                       GEPNoWrapFlags NW = GEPNoWrapFlags::none()) {
1989     return CreateGEP(getInt8Ty(), Ptr, Offset, Name, NW);
1990   }
1991 
1992   Value *CreateInBoundsPtrAdd(Value *Ptr, Value *Offset,
1993                               const Twine &Name = "") {
1994     return CreateGEP(getInt8Ty(), Ptr, Offset, Name,
1995                      GEPNoWrapFlags::inBounds());
1996   }
1997 
1998   /// Same as CreateGlobalString, but return a pointer with "i8*" type
1999   /// instead of a pointer to array of i8.
2000   ///
2001   /// If no module is given via \p M, it is take from the insertion point basic
2002   /// block.
2003   LLVM_DEPRECATED("Use CreateGlobalString instead", "CreateGlobalString")
2004   Constant *CreateGlobalStringPtr(StringRef Str, const Twine &Name = "",
2005                                   unsigned AddressSpace = 0,
2006                                   Module *M = nullptr, bool AddNull = true) {
2007     GlobalVariable *GV =
2008         CreateGlobalString(Str, Name, AddressSpace, M, AddNull);
2009     Constant *Zero = ConstantInt::get(Type::getInt32Ty(Context), 0);
2010     Constant *Indices[] = {Zero, Zero};
2011     return ConstantExpr::getInBoundsGetElementPtr(GV->getValueType(), GV,
2012                                                   Indices);
2013   }
2014 
2015   //===--------------------------------------------------------------------===//
2016   // Instruction creation methods: Cast/Conversion Operators
2017   //===--------------------------------------------------------------------===//
2018 
2019   Value *CreateTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2020                      bool IsNUW = false, bool IsNSW = false) {
2021     if (V->getType() == DestTy)
2022       return V;
2023     if (Value *Folded = Folder.FoldCast(Instruction::Trunc, V, DestTy))
2024       return Folded;
2025     Instruction *I = CastInst::Create(Instruction::Trunc, V, DestTy);
2026     if (IsNUW)
2027       I->setHasNoUnsignedWrap();
2028     if (IsNSW)
2029       I->setHasNoSignedWrap();
2030     return Insert(I, Name);
2031   }
2032 
2033   Value *CreateZExt(Value *V, Type *DestTy, const Twine &Name = "",
2034                     bool IsNonNeg = false) {
2035     if (V->getType() == DestTy)
2036       return V;
2037     if (Value *Folded = Folder.FoldCast(Instruction::ZExt, V, DestTy))
2038       return Folded;
2039     Instruction *I = Insert(new ZExtInst(V, DestTy), Name);
2040     if (IsNonNeg)
2041       I->setNonNeg();
2042     return I;
2043   }
2044 
2045   Value *CreateSExt(Value *V, Type *DestTy, const Twine &Name = "") {
2046     return CreateCast(Instruction::SExt, V, DestTy, Name);
2047   }
2048 
2049   /// Create a ZExt or Trunc from the integer value V to DestTy. Return
2050   /// the value untouched if the type of V is already DestTy.
2051   Value *CreateZExtOrTrunc(Value *V, Type *DestTy,
2052                            const Twine &Name = "") {
2053     assert(V->getType()->isIntOrIntVectorTy() &&
2054            DestTy->isIntOrIntVectorTy() &&
2055            "Can only zero extend/truncate integers!");
2056     Type *VTy = V->getType();
2057     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2058       return CreateZExt(V, DestTy, Name);
2059     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2060       return CreateTrunc(V, DestTy, Name);
2061     return V;
2062   }
2063 
2064   /// Create a SExt or Trunc from the integer value V to DestTy. Return
2065   /// the value untouched if the type of V is already DestTy.
2066   Value *CreateSExtOrTrunc(Value *V, Type *DestTy,
2067                            const Twine &Name = "") {
2068     assert(V->getType()->isIntOrIntVectorTy() &&
2069            DestTy->isIntOrIntVectorTy() &&
2070            "Can only sign extend/truncate integers!");
2071     Type *VTy = V->getType();
2072     if (VTy->getScalarSizeInBits() < DestTy->getScalarSizeInBits())
2073       return CreateSExt(V, DestTy, Name);
2074     if (VTy->getScalarSizeInBits() > DestTy->getScalarSizeInBits())
2075       return CreateTrunc(V, DestTy, Name);
2076     return V;
2077   }
2078 
2079   Value *CreateFPToUI(Value *V, Type *DestTy, const Twine &Name = "") {
2080     if (IsFPConstrained)
2081       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptoui,
2082                                      V, DestTy, nullptr, Name);
2083     return CreateCast(Instruction::FPToUI, V, DestTy, Name);
2084   }
2085 
2086   Value *CreateFPToSI(Value *V, Type *DestTy, const Twine &Name = "") {
2087     if (IsFPConstrained)
2088       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fptosi,
2089                                      V, DestTy, nullptr, Name);
2090     return CreateCast(Instruction::FPToSI, V, DestTy, Name);
2091   }
2092 
2093   Value *CreateUIToFP(Value *V, Type *DestTy, const Twine &Name = "",
2094                       bool IsNonNeg = false) {
2095     if (IsFPConstrained)
2096       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_uitofp,
2097                                      V, DestTy, nullptr, Name);
2098     if (Value *Folded = Folder.FoldCast(Instruction::UIToFP, V, DestTy))
2099       return Folded;
2100     Instruction *I = Insert(new UIToFPInst(V, DestTy), Name);
2101     if (IsNonNeg)
2102       I->setNonNeg();
2103     return I;
2104   }
2105 
2106   Value *CreateSIToFP(Value *V, Type *DestTy, const Twine &Name = ""){
2107     if (IsFPConstrained)
2108       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_sitofp,
2109                                      V, DestTy, nullptr, Name);
2110     return CreateCast(Instruction::SIToFP, V, DestTy, Name);
2111   }
2112 
2113   Value *CreateFPTrunc(Value *V, Type *DestTy, const Twine &Name = "",
2114                        MDNode *FPMathTag = nullptr) {
2115     return CreateFPTruncFMF(V, DestTy, {}, Name, FPMathTag);
2116   }
2117 
2118   Value *CreateFPTruncFMF(Value *V, Type *DestTy, FMFSource FMFSource,
2119                           const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2120     if (IsFPConstrained)
2121       return CreateConstrainedFPCast(
2122           Intrinsic::experimental_constrained_fptrunc, V, DestTy, FMFSource,
2123           Name, FPMathTag);
2124     return CreateCast(Instruction::FPTrunc, V, DestTy, Name, FPMathTag,
2125                       FMFSource);
2126   }
2127 
2128   Value *CreateFPExt(Value *V, Type *DestTy, const Twine &Name = "",
2129                      MDNode *FPMathTag = nullptr) {
2130     return CreateFPExtFMF(V, DestTy, {}, Name, FPMathTag);
2131   }
2132 
2133   Value *CreateFPExtFMF(Value *V, Type *DestTy, FMFSource FMFSource,
2134                         const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2135     if (IsFPConstrained)
2136       return CreateConstrainedFPCast(Intrinsic::experimental_constrained_fpext,
2137                                      V, DestTy, FMFSource, Name, FPMathTag);
2138     return CreateCast(Instruction::FPExt, V, DestTy, Name, FPMathTag,
2139                       FMFSource);
2140   }
2141 
2142   Value *CreatePtrToInt(Value *V, Type *DestTy,
2143                         const Twine &Name = "") {
2144     return CreateCast(Instruction::PtrToInt, V, DestTy, Name);
2145   }
2146 
2147   Value *CreateIntToPtr(Value *V, Type *DestTy,
2148                         const Twine &Name = "") {
2149     return CreateCast(Instruction::IntToPtr, V, DestTy, Name);
2150   }
2151 
2152   Value *CreateBitCast(Value *V, Type *DestTy,
2153                        const Twine &Name = "") {
2154     return CreateCast(Instruction::BitCast, V, DestTy, Name);
2155   }
2156 
2157   Value *CreateAddrSpaceCast(Value *V, Type *DestTy,
2158                              const Twine &Name = "") {
2159     return CreateCast(Instruction::AddrSpaceCast, V, DestTy, Name);
2160   }
2161 
2162   Value *CreateZExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2163     Instruction::CastOps CastOp =
2164         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2165             ? Instruction::BitCast
2166             : Instruction::ZExt;
2167     return CreateCast(CastOp, V, DestTy, Name);
2168   }
2169 
2170   Value *CreateSExtOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2171     Instruction::CastOps CastOp =
2172         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2173             ? Instruction::BitCast
2174             : Instruction::SExt;
2175     return CreateCast(CastOp, V, DestTy, Name);
2176   }
2177 
2178   Value *CreateTruncOrBitCast(Value *V, Type *DestTy, const Twine &Name = "") {
2179     Instruction::CastOps CastOp =
2180         V->getType()->getScalarSizeInBits() == DestTy->getScalarSizeInBits()
2181             ? Instruction::BitCast
2182             : Instruction::Trunc;
2183     return CreateCast(CastOp, V, DestTy, Name);
2184   }
2185 
2186   Value *CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy,
2187                     const Twine &Name = "", MDNode *FPMathTag = nullptr,
2188                     FMFSource FMFSource = {}) {
2189     if (V->getType() == DestTy)
2190       return V;
2191     if (Value *Folded = Folder.FoldCast(Op, V, DestTy))
2192       return Folded;
2193     Instruction *Cast = CastInst::Create(Op, V, DestTy);
2194     if (isa<FPMathOperator>(Cast))
2195       setFPAttrs(Cast, FPMathTag, FMFSource.get(FMF));
2196     return Insert(Cast, Name);
2197   }
2198 
2199   Value *CreatePointerCast(Value *V, Type *DestTy,
2200                            const Twine &Name = "") {
2201     if (V->getType() == DestTy)
2202       return V;
2203     if (auto *VC = dyn_cast<Constant>(V))
2204       return Insert(Folder.CreatePointerCast(VC, DestTy), Name);
2205     return Insert(CastInst::CreatePointerCast(V, DestTy), Name);
2206   }
2207 
2208   // With opaque pointers enabled, this can be substituted with
2209   // CreateAddrSpaceCast.
2210   // TODO: Replace uses of this method and remove the method itself.
2211   Value *CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy,
2212                                              const Twine &Name = "") {
2213     if (V->getType() == DestTy)
2214       return V;
2215 
2216     if (auto *VC = dyn_cast<Constant>(V)) {
2217       return Insert(Folder.CreatePointerBitCastOrAddrSpaceCast(VC, DestTy),
2218                     Name);
2219     }
2220 
2221     return Insert(CastInst::CreatePointerBitCastOrAddrSpaceCast(V, DestTy),
2222                   Name);
2223   }
2224 
2225   Value *CreateIntCast(Value *V, Type *DestTy, bool isSigned,
2226                        const Twine &Name = "") {
2227     Instruction::CastOps CastOp =
2228         V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2229             ? Instruction::Trunc
2230             : (isSigned ? Instruction::SExt : Instruction::ZExt);
2231     return CreateCast(CastOp, V, DestTy, Name);
2232   }
2233 
2234   Value *CreateBitOrPointerCast(Value *V, Type *DestTy,
2235                                 const Twine &Name = "") {
2236     if (V->getType() == DestTy)
2237       return V;
2238     if (V->getType()->isPtrOrPtrVectorTy() && DestTy->isIntOrIntVectorTy())
2239       return CreatePtrToInt(V, DestTy, Name);
2240     if (V->getType()->isIntOrIntVectorTy() && DestTy->isPtrOrPtrVectorTy())
2241       return CreateIntToPtr(V, DestTy, Name);
2242 
2243     return CreateBitCast(V, DestTy, Name);
2244   }
2245 
2246   Value *CreateFPCast(Value *V, Type *DestTy, const Twine &Name = "",
2247                       MDNode *FPMathTag = nullptr) {
2248     Instruction::CastOps CastOp =
2249         V->getType()->getScalarSizeInBits() > DestTy->getScalarSizeInBits()
2250             ? Instruction::FPTrunc
2251             : Instruction::FPExt;
2252     return CreateCast(CastOp, V, DestTy, Name, FPMathTag);
2253   }
2254 
2255   CallInst *CreateConstrainedFPCast(
2256       Intrinsic::ID ID, Value *V, Type *DestTy, FMFSource FMFSource = {},
2257       const Twine &Name = "", MDNode *FPMathTag = nullptr,
2258       std::optional<RoundingMode> Rounding = std::nullopt,
2259       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2260 
2261   // Provided to resolve 'CreateIntCast(Ptr, Ptr, "...")', giving a
2262   // compile time error, instead of converting the string to bool for the
2263   // isSigned parameter.
2264   Value *CreateIntCast(Value *, Type *, const char *) = delete;
2265 
2266   //===--------------------------------------------------------------------===//
2267   // Instruction creation methods: Compare Instructions
2268   //===--------------------------------------------------------------------===//
2269 
2270   Value *CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name = "") {
2271     return CreateICmp(ICmpInst::ICMP_EQ, LHS, RHS, Name);
2272   }
2273 
2274   Value *CreateICmpNE(Value *LHS, Value *RHS, const Twine &Name = "") {
2275     return CreateICmp(ICmpInst::ICMP_NE, LHS, RHS, Name);
2276   }
2277 
2278   Value *CreateICmpUGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2279     return CreateICmp(ICmpInst::ICMP_UGT, LHS, RHS, Name);
2280   }
2281 
2282   Value *CreateICmpUGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2283     return CreateICmp(ICmpInst::ICMP_UGE, LHS, RHS, Name);
2284   }
2285 
2286   Value *CreateICmpULT(Value *LHS, Value *RHS, const Twine &Name = "") {
2287     return CreateICmp(ICmpInst::ICMP_ULT, LHS, RHS, Name);
2288   }
2289 
2290   Value *CreateICmpULE(Value *LHS, Value *RHS, const Twine &Name = "") {
2291     return CreateICmp(ICmpInst::ICMP_ULE, LHS, RHS, Name);
2292   }
2293 
2294   Value *CreateICmpSGT(Value *LHS, Value *RHS, const Twine &Name = "") {
2295     return CreateICmp(ICmpInst::ICMP_SGT, LHS, RHS, Name);
2296   }
2297 
2298   Value *CreateICmpSGE(Value *LHS, Value *RHS, const Twine &Name = "") {
2299     return CreateICmp(ICmpInst::ICMP_SGE, LHS, RHS, Name);
2300   }
2301 
2302   Value *CreateICmpSLT(Value *LHS, Value *RHS, const Twine &Name = "") {
2303     return CreateICmp(ICmpInst::ICMP_SLT, LHS, RHS, Name);
2304   }
2305 
2306   Value *CreateICmpSLE(Value *LHS, Value *RHS, const Twine &Name = "") {
2307     return CreateICmp(ICmpInst::ICMP_SLE, LHS, RHS, Name);
2308   }
2309 
2310   Value *CreateFCmpOEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2311                        MDNode *FPMathTag = nullptr) {
2312     return CreateFCmp(FCmpInst::FCMP_OEQ, LHS, RHS, Name, FPMathTag);
2313   }
2314 
2315   Value *CreateFCmpOGT(Value *LHS, Value *RHS, const Twine &Name = "",
2316                        MDNode *FPMathTag = nullptr) {
2317     return CreateFCmp(FCmpInst::FCMP_OGT, LHS, RHS, Name, FPMathTag);
2318   }
2319 
2320   Value *CreateFCmpOGE(Value *LHS, Value *RHS, const Twine &Name = "",
2321                        MDNode *FPMathTag = nullptr) {
2322     return CreateFCmp(FCmpInst::FCMP_OGE, LHS, RHS, Name, FPMathTag);
2323   }
2324 
2325   Value *CreateFCmpOLT(Value *LHS, Value *RHS, const Twine &Name = "",
2326                        MDNode *FPMathTag = nullptr) {
2327     return CreateFCmp(FCmpInst::FCMP_OLT, LHS, RHS, Name, FPMathTag);
2328   }
2329 
2330   Value *CreateFCmpOLE(Value *LHS, Value *RHS, const Twine &Name = "",
2331                        MDNode *FPMathTag = nullptr) {
2332     return CreateFCmp(FCmpInst::FCMP_OLE, LHS, RHS, Name, FPMathTag);
2333   }
2334 
2335   Value *CreateFCmpONE(Value *LHS, Value *RHS, const Twine &Name = "",
2336                        MDNode *FPMathTag = nullptr) {
2337     return CreateFCmp(FCmpInst::FCMP_ONE, LHS, RHS, Name, FPMathTag);
2338   }
2339 
2340   Value *CreateFCmpORD(Value *LHS, Value *RHS, const Twine &Name = "",
2341                        MDNode *FPMathTag = nullptr) {
2342     return CreateFCmp(FCmpInst::FCMP_ORD, LHS, RHS, Name, FPMathTag);
2343   }
2344 
2345   Value *CreateFCmpUNO(Value *LHS, Value *RHS, const Twine &Name = "",
2346                        MDNode *FPMathTag = nullptr) {
2347     return CreateFCmp(FCmpInst::FCMP_UNO, LHS, RHS, Name, FPMathTag);
2348   }
2349 
2350   Value *CreateFCmpUEQ(Value *LHS, Value *RHS, const Twine &Name = "",
2351                        MDNode *FPMathTag = nullptr) {
2352     return CreateFCmp(FCmpInst::FCMP_UEQ, LHS, RHS, Name, FPMathTag);
2353   }
2354 
2355   Value *CreateFCmpUGT(Value *LHS, Value *RHS, const Twine &Name = "",
2356                        MDNode *FPMathTag = nullptr) {
2357     return CreateFCmp(FCmpInst::FCMP_UGT, LHS, RHS, Name, FPMathTag);
2358   }
2359 
2360   Value *CreateFCmpUGE(Value *LHS, Value *RHS, const Twine &Name = "",
2361                        MDNode *FPMathTag = nullptr) {
2362     return CreateFCmp(FCmpInst::FCMP_UGE, LHS, RHS, Name, FPMathTag);
2363   }
2364 
2365   Value *CreateFCmpULT(Value *LHS, Value *RHS, const Twine &Name = "",
2366                        MDNode *FPMathTag = nullptr) {
2367     return CreateFCmp(FCmpInst::FCMP_ULT, LHS, RHS, Name, FPMathTag);
2368   }
2369 
2370   Value *CreateFCmpULE(Value *LHS, Value *RHS, const Twine &Name = "",
2371                        MDNode *FPMathTag = nullptr) {
2372     return CreateFCmp(FCmpInst::FCMP_ULE, LHS, RHS, Name, FPMathTag);
2373   }
2374 
2375   Value *CreateFCmpUNE(Value *LHS, Value *RHS, const Twine &Name = "",
2376                        MDNode *FPMathTag = nullptr) {
2377     return CreateFCmp(FCmpInst::FCMP_UNE, LHS, RHS, Name, FPMathTag);
2378   }
2379 
2380   Value *CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2381                     const Twine &Name = "") {
2382     if (auto *V = Folder.FoldCmp(P, LHS, RHS))
2383       return V;
2384     return Insert(new ICmpInst(P, LHS, RHS), Name);
2385   }
2386 
2387   // Create a quiet floating-point comparison (i.e. one that raises an FP
2388   // exception only in the case where an input is a signaling NaN).
2389   // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
2390   Value *CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS,
2391                     const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2392     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, {}, false);
2393   }
2394 
2395   // Create a quiet floating-point comparison (i.e. one that raises an FP
2396   // exception only in the case where an input is a signaling NaN).
2397   // Note that this differs from CreateFCmpS only if IsFPConstrained is true.
2398   Value *CreateFCmpFMF(CmpInst::Predicate P, Value *LHS, Value *RHS,
2399                        FMFSource FMFSource, const Twine &Name = "",
2400                        MDNode *FPMathTag = nullptr) {
2401     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, FMFSource, false);
2402   }
2403 
2404   Value *CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2405                    const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2406     return CmpInst::isFPPredicate(Pred)
2407                ? CreateFCmp(Pred, LHS, RHS, Name, FPMathTag)
2408                : CreateICmp(Pred, LHS, RHS, Name);
2409   }
2410 
2411   // Create a signaling floating-point comparison (i.e. one that raises an FP
2412   // exception whenever an input is any NaN, signaling or quiet).
2413   // Note that this differs from CreateFCmp only if IsFPConstrained is true.
2414   Value *CreateFCmpS(CmpInst::Predicate P, Value *LHS, Value *RHS,
2415                      const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2416     return CreateFCmpHelper(P, LHS, RHS, Name, FPMathTag, {}, true);
2417   }
2418 
2419 private:
2420   // Helper routine to create either a signaling or a quiet FP comparison.
2421   Value *CreateFCmpHelper(CmpInst::Predicate P, Value *LHS, Value *RHS,
2422                           const Twine &Name, MDNode *FPMathTag,
2423                           FMFSource FMFSource, bool IsSignaling);
2424 
2425 public:
2426   CallInst *CreateConstrainedFPCmp(
2427       Intrinsic::ID ID, CmpInst::Predicate P, Value *L, Value *R,
2428       const Twine &Name = "",
2429       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2430 
2431   //===--------------------------------------------------------------------===//
2432   // Instruction creation methods: Other Instructions
2433   //===--------------------------------------------------------------------===//
2434 
2435   PHINode *CreatePHI(Type *Ty, unsigned NumReservedValues,
2436                      const Twine &Name = "") {
2437     PHINode *Phi = PHINode::Create(Ty, NumReservedValues);
2438     if (isa<FPMathOperator>(Phi))
2439       setFPAttrs(Phi, nullptr /* MDNode* */, FMF);
2440     return Insert(Phi, Name);
2441   }
2442 
2443 private:
2444   CallInst *createCallHelper(Function *Callee, ArrayRef<Value *> Ops,
2445                              const Twine &Name = "", FMFSource FMFSource = {},
2446                              ArrayRef<OperandBundleDef> OpBundles = {});
2447 
2448 public:
2449   CallInst *CreateCall(FunctionType *FTy, Value *Callee,
2450                        ArrayRef<Value *> Args = {}, const Twine &Name = "",
2451                        MDNode *FPMathTag = nullptr) {
2452     CallInst *CI = CallInst::Create(FTy, Callee, Args, DefaultOperandBundles);
2453     if (IsFPConstrained)
2454       setConstrainedFPCallAttr(CI);
2455     if (isa<FPMathOperator>(CI))
2456       setFPAttrs(CI, FPMathTag, FMF);
2457     return Insert(CI, Name);
2458   }
2459 
2460   CallInst *CreateCall(FunctionType *FTy, Value *Callee, ArrayRef<Value *> Args,
2461                        ArrayRef<OperandBundleDef> OpBundles,
2462                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2463     CallInst *CI = CallInst::Create(FTy, Callee, Args, OpBundles);
2464     if (IsFPConstrained)
2465       setConstrainedFPCallAttr(CI);
2466     if (isa<FPMathOperator>(CI))
2467       setFPAttrs(CI, FPMathTag, FMF);
2468     return Insert(CI, Name);
2469   }
2470 
2471   CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args = {},
2472                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2473     return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args, Name,
2474                       FPMathTag);
2475   }
2476 
2477   CallInst *CreateCall(FunctionCallee Callee, ArrayRef<Value *> Args,
2478                        ArrayRef<OperandBundleDef> OpBundles,
2479                        const Twine &Name = "", MDNode *FPMathTag = nullptr) {
2480     return CreateCall(Callee.getFunctionType(), Callee.getCallee(), Args,
2481                       OpBundles, Name, FPMathTag);
2482   }
2483 
2484   CallInst *CreateConstrainedFPCall(
2485       Function *Callee, ArrayRef<Value *> Args, const Twine &Name = "",
2486       std::optional<RoundingMode> Rounding = std::nullopt,
2487       std::optional<fp::ExceptionBehavior> Except = std::nullopt);
2488 
2489   Value *CreateSelect(Value *C, Value *True, Value *False,
2490                       const Twine &Name = "", Instruction *MDFrom = nullptr);
2491   Value *CreateSelectFMF(Value *C, Value *True, Value *False,
2492                          FMFSource FMFSource, const Twine &Name = "",
2493                          Instruction *MDFrom = nullptr);
2494 
2495   VAArgInst *CreateVAArg(Value *List, Type *Ty, const Twine &Name = "") {
2496     return Insert(new VAArgInst(List, Ty), Name);
2497   }
2498 
2499   Value *CreateExtractElement(Value *Vec, Value *Idx,
2500                               const Twine &Name = "") {
2501     if (Value *V = Folder.FoldExtractElement(Vec, Idx))
2502       return V;
2503     return Insert(ExtractElementInst::Create(Vec, Idx), Name);
2504   }
2505 
2506   Value *CreateExtractElement(Value *Vec, uint64_t Idx,
2507                               const Twine &Name = "") {
2508     return CreateExtractElement(Vec, getInt64(Idx), Name);
2509   }
2510 
2511   Value *CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx,
2512                              const Twine &Name = "") {
2513     return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2514   }
2515 
2516   Value *CreateInsertElement(Type *VecTy, Value *NewElt, uint64_t Idx,
2517                              const Twine &Name = "") {
2518     return CreateInsertElement(PoisonValue::get(VecTy), NewElt, Idx, Name);
2519   }
2520 
2521   Value *CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx,
2522                              const Twine &Name = "") {
2523     if (Value *V = Folder.FoldInsertElement(Vec, NewElt, Idx))
2524       return V;
2525     return Insert(InsertElementInst::Create(Vec, NewElt, Idx), Name);
2526   }
2527 
2528   Value *CreateInsertElement(Value *Vec, Value *NewElt, uint64_t Idx,
2529                              const Twine &Name = "") {
2530     return CreateInsertElement(Vec, NewElt, getInt64(Idx), Name);
2531   }
2532 
2533   Value *CreateShuffleVector(Value *V1, Value *V2, Value *Mask,
2534                              const Twine &Name = "") {
2535     SmallVector<int, 16> IntMask;
2536     ShuffleVectorInst::getShuffleMask(cast<Constant>(Mask), IntMask);
2537     return CreateShuffleVector(V1, V2, IntMask, Name);
2538   }
2539 
2540   /// See class ShuffleVectorInst for a description of the mask representation.
2541   Value *CreateShuffleVector(Value *V1, Value *V2, ArrayRef<int> Mask,
2542                              const Twine &Name = "") {
2543     if (Value *V = Folder.FoldShuffleVector(V1, V2, Mask))
2544       return V;
2545     return Insert(new ShuffleVectorInst(V1, V2, Mask), Name);
2546   }
2547 
2548   /// Create a unary shuffle. The second vector operand of the IR instruction
2549   /// is poison.
2550   Value *CreateShuffleVector(Value *V, ArrayRef<int> Mask,
2551                              const Twine &Name = "") {
2552     return CreateShuffleVector(V, PoisonValue::get(V->getType()), Mask, Name);
2553   }
2554 
2555   Value *CreateExtractValue(Value *Agg, ArrayRef<unsigned> Idxs,
2556                             const Twine &Name = "") {
2557     if (auto *V = Folder.FoldExtractValue(Agg, Idxs))
2558       return V;
2559     return Insert(ExtractValueInst::Create(Agg, Idxs), Name);
2560   }
2561 
2562   Value *CreateInsertValue(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2563                            const Twine &Name = "") {
2564     if (auto *V = Folder.FoldInsertValue(Agg, Val, Idxs))
2565       return V;
2566     return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
2567   }
2568 
2569   LandingPadInst *CreateLandingPad(Type *Ty, unsigned NumClauses,
2570                                    const Twine &Name = "") {
2571     return Insert(LandingPadInst::Create(Ty, NumClauses), Name);
2572   }
2573 
2574   Value *CreateFreeze(Value *V, const Twine &Name = "") {
2575     return Insert(new FreezeInst(V), Name);
2576   }
2577 
2578   //===--------------------------------------------------------------------===//
2579   // Utility creation methods
2580   //===--------------------------------------------------------------------===//
2581 
2582   /// Return a boolean value testing if \p Arg == 0.
2583   Value *CreateIsNull(Value *Arg, const Twine &Name = "") {
2584     return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), Name);
2585   }
2586 
2587   /// Return a boolean value testing if \p Arg != 0.
2588   Value *CreateIsNotNull(Value *Arg, const Twine &Name = "") {
2589     return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), Name);
2590   }
2591 
2592   /// Return a boolean value testing if \p Arg < 0.
2593   Value *CreateIsNeg(Value *Arg, const Twine &Name = "") {
2594     return CreateICmpSLT(Arg, ConstantInt::getNullValue(Arg->getType()), Name);
2595   }
2596 
2597   /// Return a boolean value testing if \p Arg > -1.
2598   Value *CreateIsNotNeg(Value *Arg, const Twine &Name = "") {
2599     return CreateICmpSGT(Arg, ConstantInt::getAllOnesValue(Arg->getType()),
2600                          Name);
2601   }
2602 
2603   /// Return the i64 difference between two pointer values, dividing out
2604   /// the size of the pointed-to objects.
2605   ///
2606   /// This is intended to implement C-style pointer subtraction. As such, the
2607   /// pointers must be appropriately aligned for their element types and
2608   /// pointing into the same object.
2609   Value *CreatePtrDiff(Type *ElemTy, Value *LHS, Value *RHS,
2610                        const Twine &Name = "");
2611 
2612   /// Create a launder.invariant.group intrinsic call. If Ptr type is
2613   /// different from pointer to i8, it's casted to pointer to i8 in the same
2614   /// address space before call and casted back to Ptr type after call.
2615   Value *CreateLaunderInvariantGroup(Value *Ptr);
2616 
2617   /// \brief Create a strip.invariant.group intrinsic call. If Ptr type is
2618   /// different from pointer to i8, it's casted to pointer to i8 in the same
2619   /// address space before call and casted back to Ptr type after call.
2620   Value *CreateStripInvariantGroup(Value *Ptr);
2621 
2622   /// Return a vector value that contains the vector V reversed
2623   Value *CreateVectorReverse(Value *V, const Twine &Name = "");
2624 
2625   /// Return a vector splice intrinsic if using scalable vectors, otherwise
2626   /// return a shufflevector. If the immediate is positive, a vector is
2627   /// extracted from concat(V1, V2), starting at Imm. If the immediate
2628   /// is negative, we extract -Imm elements from V1 and the remaining
2629   /// elements from V2. Imm is a signed integer in the range
2630   /// -VL <= Imm < VL (where VL is the runtime vector length of the
2631   /// source/result vector)
2632   Value *CreateVectorSplice(Value *V1, Value *V2, int64_t Imm,
2633                             const Twine &Name = "");
2634 
2635   /// Return a vector value that contains \arg V broadcasted to \p
2636   /// NumElts elements.
2637   Value *CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name = "");
2638 
2639   /// Return a vector value that contains \arg V broadcasted to \p
2640   /// EC elements.
2641   Value *CreateVectorSplat(ElementCount EC, Value *V, const Twine &Name = "");
2642 
2643   Value *CreatePreserveArrayAccessIndex(Type *ElTy, Value *Base,
2644                                         unsigned Dimension, unsigned LastIndex,
2645                                         MDNode *DbgInfo);
2646 
2647   Value *CreatePreserveUnionAccessIndex(Value *Base, unsigned FieldIndex,
2648                                         MDNode *DbgInfo);
2649 
2650   Value *CreatePreserveStructAccessIndex(Type *ElTy, Value *Base,
2651                                          unsigned Index, unsigned FieldIndex,
2652                                          MDNode *DbgInfo);
2653 
2654   Value *createIsFPClass(Value *FPNum, unsigned Test);
2655 
2656 private:
2657   /// Helper function that creates an assume intrinsic call that
2658   /// represents an alignment assumption on the provided pointer \p PtrValue
2659   /// with offset \p OffsetValue and alignment value \p AlignValue.
2660   CallInst *CreateAlignmentAssumptionHelper(const DataLayout &DL,
2661                                             Value *PtrValue, Value *AlignValue,
2662                                             Value *OffsetValue);
2663 
2664 public:
2665   /// Create an assume intrinsic call that represents an alignment
2666   /// assumption on the provided pointer.
2667   ///
2668   /// An optional offset can be provided, and if it is provided, the offset
2669   /// must be subtracted from the provided pointer to get the pointer with the
2670   /// specified alignment.
2671   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2672                                       unsigned Alignment,
2673                                       Value *OffsetValue = nullptr);
2674 
2675   /// Create an assume intrinsic call that represents an alignment
2676   /// assumption on the provided pointer.
2677   ///
2678   /// An optional offset can be provided, and if it is provided, the offset
2679   /// must be subtracted from the provided pointer to get the pointer with the
2680   /// specified alignment.
2681   ///
2682   /// This overload handles the condition where the Alignment is dependent
2683   /// on an existing value rather than a static value.
2684   CallInst *CreateAlignmentAssumption(const DataLayout &DL, Value *PtrValue,
2685                                       Value *Alignment,
2686                                       Value *OffsetValue = nullptr);
2687 };
2688 
2689 /// This provides a uniform API for creating instructions and inserting
2690 /// them into a basic block: either at the end of a BasicBlock, or at a specific
2691 /// iterator location in a block.
2692 ///
2693 /// Note that the builder does not expose the full generality of LLVM
2694 /// instructions.  For access to extra instruction properties, use the mutators
2695 /// (e.g. setVolatile) on the instructions after they have been
2696 /// created. Convenience state exists to specify fast-math flags and fp-math
2697 /// tags.
2698 ///
2699 /// The first template argument specifies a class to use for creating constants.
2700 /// This defaults to creating minimally folded constants.  The second template
2701 /// argument allows clients to specify custom insertion hooks that are called on
2702 /// every newly created insertion.
2703 template <typename FolderTy = ConstantFolder,
2704           typename InserterTy = IRBuilderDefaultInserter>
2705 class IRBuilder : public IRBuilderBase {
2706 private:
2707   FolderTy Folder;
2708   InserterTy Inserter;
2709 
2710 public:
2711   IRBuilder(LLVMContext &C, FolderTy Folder, InserterTy Inserter = InserterTy(),
2712             MDNode *FPMathTag = nullptr,
2713             ArrayRef<OperandBundleDef> OpBundles = {})
2714       : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles),
2715         Folder(Folder), Inserter(Inserter) {}
2716 
2717   explicit IRBuilder(LLVMContext &C, MDNode *FPMathTag = nullptr,
2718                      ArrayRef<OperandBundleDef> OpBundles = {})
2719       : IRBuilderBase(C, this->Folder, this->Inserter, FPMathTag, OpBundles) {}
2720 
2721   explicit IRBuilder(BasicBlock *TheBB, FolderTy Folder,
2722                      MDNode *FPMathTag = nullptr,
2723                      ArrayRef<OperandBundleDef> OpBundles = {})
2724       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2725                       FPMathTag, OpBundles),
2726         Folder(Folder) {
2727     SetInsertPoint(TheBB);
2728   }
2729 
2730   explicit IRBuilder(BasicBlock *TheBB, MDNode *FPMathTag = nullptr,
2731                      ArrayRef<OperandBundleDef> OpBundles = {})
2732       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2733                       FPMathTag, OpBundles) {
2734     SetInsertPoint(TheBB);
2735   }
2736 
2737   explicit IRBuilder(Instruction *IP, MDNode *FPMathTag = nullptr,
2738                      ArrayRef<OperandBundleDef> OpBundles = {})
2739       : IRBuilderBase(IP->getContext(), this->Folder, this->Inserter, FPMathTag,
2740                       OpBundles) {
2741     SetInsertPoint(IP);
2742   }
2743 
2744   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, FolderTy Folder,
2745             MDNode *FPMathTag = nullptr,
2746             ArrayRef<OperandBundleDef> OpBundles = {})
2747       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2748                       FPMathTag, OpBundles),
2749         Folder(Folder) {
2750     SetInsertPoint(TheBB, IP);
2751   }
2752 
2753   IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP,
2754             MDNode *FPMathTag = nullptr,
2755             ArrayRef<OperandBundleDef> OpBundles = {})
2756       : IRBuilderBase(TheBB->getContext(), this->Folder, this->Inserter,
2757                       FPMathTag, OpBundles) {
2758     SetInsertPoint(TheBB, IP);
2759   }
2760 
2761   /// Avoid copying the full IRBuilder. Prefer using InsertPointGuard
2762   /// or FastMathFlagGuard instead.
2763   IRBuilder(const IRBuilder &) = delete;
2764 
2765   InserterTy &getInserter() { return Inserter; }
2766   const InserterTy &getInserter() const { return Inserter; }
2767 };
2768 
2769 template <typename FolderTy, typename InserterTy>
2770 IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *,
2771           ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy, InserterTy>;
2772 IRBuilder(LLVMContext &, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2773 template <typename FolderTy>
2774 IRBuilder(BasicBlock *, FolderTy, MDNode *, ArrayRef<OperandBundleDef>)
2775     -> IRBuilder<FolderTy>;
2776 IRBuilder(BasicBlock *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2777 IRBuilder(Instruction *, MDNode *, ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2778 template <typename FolderTy>
2779 IRBuilder(BasicBlock *, BasicBlock::iterator, FolderTy, MDNode *,
2780           ArrayRef<OperandBundleDef>) -> IRBuilder<FolderTy>;
2781 IRBuilder(BasicBlock *, BasicBlock::iterator, MDNode *,
2782           ArrayRef<OperandBundleDef>) -> IRBuilder<>;
2783 
2784 
2785 // Create wrappers for C Binding types (see CBindingWrapping.h).
2786 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(IRBuilder<>, LLVMBuilderRef)
2787 
2788 } // end namespace llvm
2789 
2790 #endif // LLVM_IR_IRBUILDER_H
2791