xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/IntrinsicInst.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1 //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- 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 classes that make it really easy to deal with intrinsic
10 // functions with the isa/dyncast family of functions.  In particular, this
11 // allows you to do things like:
12 //
13 //     if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
14 //        ... MCI->getDest() ... MCI->getSource() ...
15 //
16 // All intrinsic function calls are instances of the call instruction, so these
17 // are all subclasses of the CallInst class.  Note that none of these classes
18 // has state or virtual methods, which is an important part of this gross/neat
19 // hack working.
20 //
21 //===----------------------------------------------------------------------===//
22 
23 #ifndef LLVM_IR_INTRINSICINST_H
24 #define LLVM_IR_INTRINSICINST_H
25 
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DebugInfoMetadata.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/Instructions.h"
33 #include "llvm/IR/Intrinsics.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
36 #include <cassert>
37 #include <cstdint>
38 #include <optional>
39 
40 namespace llvm {
41 
42 class Metadata;
43 
44 /// A wrapper class for inspecting calls to intrinsic functions.
45 /// This allows the standard isa/dyncast/cast functionality to work with calls
46 /// to intrinsic functions.
47 class IntrinsicInst : public CallInst {
48 public:
49   IntrinsicInst() = delete;
50   IntrinsicInst(const IntrinsicInst &) = delete;
51   IntrinsicInst &operator=(const IntrinsicInst &) = delete;
52 
53   /// Return the intrinsic ID of this intrinsic.
54   Intrinsic::ID getIntrinsicID() const {
55     return getCalledFunction()->getIntrinsicID();
56   }
57 
58   bool isAssociative() const {
59     switch (getIntrinsicID()) {
60     case Intrinsic::smax:
61     case Intrinsic::smin:
62     case Intrinsic::umax:
63     case Intrinsic::umin:
64       return true;
65     default:
66       return false;
67     }
68   }
69 
70   /// Return true if swapping the first two arguments to the intrinsic produces
71   /// the same result.
72   bool isCommutative() const {
73     switch (getIntrinsicID()) {
74     case Intrinsic::maxnum:
75     case Intrinsic::minnum:
76     case Intrinsic::maximum:
77     case Intrinsic::minimum:
78     case Intrinsic::smax:
79     case Intrinsic::smin:
80     case Intrinsic::umax:
81     case Intrinsic::umin:
82     case Intrinsic::sadd_sat:
83     case Intrinsic::uadd_sat:
84     case Intrinsic::sadd_with_overflow:
85     case Intrinsic::uadd_with_overflow:
86     case Intrinsic::smul_with_overflow:
87     case Intrinsic::umul_with_overflow:
88     case Intrinsic::smul_fix:
89     case Intrinsic::umul_fix:
90     case Intrinsic::smul_fix_sat:
91     case Intrinsic::umul_fix_sat:
92     case Intrinsic::fma:
93     case Intrinsic::fmuladd:
94       return true;
95     default:
96       return false;
97     }
98   }
99 
100   /// Checks if the intrinsic is an annotation.
101   bool isAssumeLikeIntrinsic() const {
102     switch (getIntrinsicID()) {
103     default: break;
104     case Intrinsic::assume:
105     case Intrinsic::sideeffect:
106     case Intrinsic::pseudoprobe:
107     case Intrinsic::dbg_assign:
108     case Intrinsic::dbg_declare:
109     case Intrinsic::dbg_value:
110     case Intrinsic::dbg_label:
111     case Intrinsic::invariant_start:
112     case Intrinsic::invariant_end:
113     case Intrinsic::lifetime_start:
114     case Intrinsic::lifetime_end:
115     case Intrinsic::experimental_noalias_scope_decl:
116     case Intrinsic::objectsize:
117     case Intrinsic::ptr_annotation:
118     case Intrinsic::var_annotation:
119       return true;
120     }
121     return false;
122   }
123 
124   /// Check if the intrinsic might lower into a regular function call in the
125   /// course of IR transformations
126   static bool mayLowerToFunctionCall(Intrinsic::ID IID);
127 
128   /// Methods for support type inquiry through isa, cast, and dyn_cast:
129   static bool classof(const CallInst *I) {
130     if (const Function *CF = I->getCalledFunction())
131       return CF->isIntrinsic();
132     return false;
133   }
134   static bool classof(const Value *V) {
135     return isa<CallInst>(V) && classof(cast<CallInst>(V));
136   }
137 };
138 
139 /// Check if \p ID corresponds to a lifetime intrinsic.
140 static inline bool isLifetimeIntrinsic(Intrinsic::ID ID) {
141   switch (ID) {
142   case Intrinsic::lifetime_start:
143   case Intrinsic::lifetime_end:
144     return true;
145   default:
146     return false;
147   }
148 }
149 
150 /// This is the common base class for lifetime intrinsics.
151 class LifetimeIntrinsic : public IntrinsicInst {
152 public:
153   /// \name Casting methods
154   /// @{
155   static bool classof(const IntrinsicInst *I) {
156     return isLifetimeIntrinsic(I->getIntrinsicID());
157   }
158   static bool classof(const Value *V) {
159     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
160   }
161   /// @}
162 };
163 
164 /// Check if \p ID corresponds to a debug info intrinsic.
165 static inline bool isDbgInfoIntrinsic(Intrinsic::ID ID) {
166   switch (ID) {
167   case Intrinsic::dbg_declare:
168   case Intrinsic::dbg_value:
169   case Intrinsic::dbg_label:
170   case Intrinsic::dbg_assign:
171     return true;
172   default:
173     return false;
174   }
175 }
176 
177 /// This is the common base class for debug info intrinsics.
178 class DbgInfoIntrinsic : public IntrinsicInst {
179 public:
180   /// \name Casting methods
181   /// @{
182   static bool classof(const IntrinsicInst *I) {
183     return isDbgInfoIntrinsic(I->getIntrinsicID());
184   }
185   static bool classof(const Value *V) {
186     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
187   }
188   /// @}
189 };
190 
191 // Iterator for ValueAsMetadata that internally uses direct pointer iteration
192 // over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
193 // ValueAsMetadata .
194 class location_op_iterator
195     : public iterator_facade_base<location_op_iterator,
196                                   std::bidirectional_iterator_tag, Value *> {
197   PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I;
198 
199 public:
200   location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {}
201   location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {}
202 
203   location_op_iterator(const location_op_iterator &R) : I(R.I) {}
204   location_op_iterator &operator=(const location_op_iterator &R) {
205     I = R.I;
206     return *this;
207   }
208   bool operator==(const location_op_iterator &RHS) const { return I == RHS.I; }
209   const Value *operator*() const {
210     ValueAsMetadata *VAM = isa<ValueAsMetadata *>(I)
211                                ? cast<ValueAsMetadata *>(I)
212                                : *cast<ValueAsMetadata **>(I);
213     return VAM->getValue();
214   };
215   Value *operator*() {
216     ValueAsMetadata *VAM = isa<ValueAsMetadata *>(I)
217                                ? cast<ValueAsMetadata *>(I)
218                                : *cast<ValueAsMetadata **>(I);
219     return VAM->getValue();
220   }
221   location_op_iterator &operator++() {
222     if (isa<ValueAsMetadata *>(I))
223       I = cast<ValueAsMetadata *>(I) + 1;
224     else
225       I = cast<ValueAsMetadata **>(I) + 1;
226     return *this;
227   }
228   location_op_iterator &operator--() {
229     if (isa<ValueAsMetadata *>(I))
230       I = cast<ValueAsMetadata *>(I) - 1;
231     else
232       I = cast<ValueAsMetadata **>(I) - 1;
233     return *this;
234   }
235 };
236 
237 /// Lightweight class that wraps the location operand metadata of a debug
238 /// intrinsic. The raw location may be a ValueAsMetadata, an empty MDTuple,
239 /// or a DIArgList.
240 class RawLocationWrapper {
241   Metadata *RawLocation = nullptr;
242 
243 public:
244   RawLocationWrapper() = default;
245   explicit RawLocationWrapper(Metadata *RawLocation)
246       : RawLocation(RawLocation) {
247     // Allow ValueAsMetadata, empty MDTuple, DIArgList.
248     assert(RawLocation && "unexpected null RawLocation");
249     assert(isa<ValueAsMetadata>(RawLocation) || isa<DIArgList>(RawLocation) ||
250            (isa<MDNode>(RawLocation) &&
251             !cast<MDNode>(RawLocation)->getNumOperands()));
252   }
253   Metadata *getRawLocation() const { return RawLocation; }
254   /// Get the locations corresponding to the variable referenced by the debug
255   /// info intrinsic.  Depending on the intrinsic, this could be the
256   /// variable's value or its address.
257   iterator_range<location_op_iterator> location_ops() const;
258   Value *getVariableLocationOp(unsigned OpIdx) const;
259   unsigned getNumVariableLocationOps() const {
260     if (hasArgList())
261       return cast<DIArgList>(getRawLocation())->getArgs().size();
262     return 1;
263   }
264   bool hasArgList() const { return isa<DIArgList>(getRawLocation()); }
265   bool isKillLocation(const DIExpression *Expression) const {
266     // Check for "kill" sentinel values.
267     // Non-variadic: empty metadata.
268     if (!hasArgList() && isa<MDNode>(getRawLocation()))
269       return true;
270     // Variadic: empty DIArgList with empty expression.
271     if (getNumVariableLocationOps() == 0 && !Expression->isComplex())
272       return true;
273     // Variadic and non-variadic: Interpret expressions using undef or poison
274     // values as kills.
275     return any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); });
276   }
277 
278   friend bool operator==(const RawLocationWrapper &A,
279                          const RawLocationWrapper &B) {
280     return A.RawLocation == B.RawLocation;
281   }
282   friend bool operator!=(const RawLocationWrapper &A,
283                          const RawLocationWrapper &B) {
284     return !(A == B);
285   }
286   friend bool operator>(const RawLocationWrapper &A,
287                         const RawLocationWrapper &B) {
288     return A.RawLocation > B.RawLocation;
289   }
290   friend bool operator>=(const RawLocationWrapper &A,
291                          const RawLocationWrapper &B) {
292     return A.RawLocation >= B.RawLocation;
293   }
294   friend bool operator<(const RawLocationWrapper &A,
295                         const RawLocationWrapper &B) {
296     return A.RawLocation < B.RawLocation;
297   }
298   friend bool operator<=(const RawLocationWrapper &A,
299                          const RawLocationWrapper &B) {
300     return A.RawLocation <= B.RawLocation;
301   }
302 };
303 
304 /// This is the common base class for debug info intrinsics for variables.
305 class DbgVariableIntrinsic : public DbgInfoIntrinsic {
306 public:
307   /// Get the locations corresponding to the variable referenced by the debug
308   /// info intrinsic.  Depending on the intrinsic, this could be the
309   /// variable's value or its address.
310   iterator_range<location_op_iterator> location_ops() const;
311 
312   Value *getVariableLocationOp(unsigned OpIdx) const;
313 
314   void replaceVariableLocationOp(Value *OldValue, Value *NewValue);
315   void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue);
316   /// Adding a new location operand will always result in this intrinsic using
317   /// an ArgList, and must always be accompanied by a new expression that uses
318   /// the new operand.
319   void addVariableLocationOps(ArrayRef<Value *> NewValues,
320                               DIExpression *NewExpr);
321 
322   void setVariable(DILocalVariable *NewVar) {
323     setArgOperand(1, MetadataAsValue::get(NewVar->getContext(), NewVar));
324   }
325 
326   void setExpression(DIExpression *NewExpr) {
327     setArgOperand(2, MetadataAsValue::get(NewExpr->getContext(), NewExpr));
328   }
329 
330   unsigned getNumVariableLocationOps() const {
331     return getWrappedLocation().getNumVariableLocationOps();
332   }
333 
334   bool hasArgList() const { return getWrappedLocation().hasArgList(); }
335 
336   /// Does this describe the address of a local variable. True for dbg.declare,
337   /// but not dbg.value, which describes its value, or dbg.assign, which
338   /// describes a combination of the variable's value and address.
339   bool isAddressOfVariable() const {
340     return getIntrinsicID() == Intrinsic::dbg_declare;
341   }
342 
343   void setKillLocation() {
344     // TODO: When/if we remove duplicate values from DIArgLists, we don't need
345     // this set anymore.
346     SmallPtrSet<Value *, 4> RemovedValues;
347     for (Value *OldValue : location_ops()) {
348       if (!RemovedValues.insert(OldValue).second)
349         continue;
350       Value *Poison = PoisonValue::get(OldValue->getType());
351       replaceVariableLocationOp(OldValue, Poison);
352     }
353   }
354 
355   bool isKillLocation() const {
356     return getWrappedLocation().isKillLocation(getExpression());
357   }
358 
359   DILocalVariable *getVariable() const {
360     return cast<DILocalVariable>(getRawVariable());
361   }
362 
363   DIExpression *getExpression() const {
364     return cast<DIExpression>(getRawExpression());
365   }
366 
367   Metadata *getRawLocation() const {
368     return cast<MetadataAsValue>(getArgOperand(0))->getMetadata();
369   }
370 
371   RawLocationWrapper getWrappedLocation() const {
372     return RawLocationWrapper(getRawLocation());
373   }
374 
375   Metadata *getRawVariable() const {
376     return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
377   }
378 
379   Metadata *getRawExpression() const {
380     return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
381   }
382 
383   /// Use of this should generally be avoided; instead,
384   /// replaceVariableLocationOp and addVariableLocationOps should be used where
385   /// possible to avoid creating invalid state.
386   void setRawLocation(Metadata *Location) {
387     return setArgOperand(0, MetadataAsValue::get(getContext(), Location));
388   }
389 
390   /// Get the size (in bits) of the variable, or fragment of the variable that
391   /// is described.
392   std::optional<uint64_t> getFragmentSizeInBits() const;
393 
394   /// Get the FragmentInfo for the variable.
395   std::optional<DIExpression::FragmentInfo> getFragment() const {
396     return getExpression()->getFragmentInfo();
397   }
398 
399   /// Get the FragmentInfo for the variable if it exists, otherwise return a
400   /// FragmentInfo that covers the entire variable if the variable size is
401   /// known, otherwise return a zero-sized fragment.
402   DIExpression::FragmentInfo getFragmentOrEntireVariable() const {
403     DIExpression::FragmentInfo VariableSlice(0, 0);
404     // Get the fragment or variable size, or zero.
405     if (auto Sz = getFragmentSizeInBits())
406       VariableSlice.SizeInBits = *Sz;
407     if (auto Frag = getExpression()->getFragmentInfo())
408       VariableSlice.OffsetInBits = Frag->OffsetInBits;
409     return VariableSlice;
410   }
411 
412   /// \name Casting methods
413   /// @{
414   static bool classof(const IntrinsicInst *I) {
415     switch (I->getIntrinsicID()) {
416     case Intrinsic::dbg_declare:
417     case Intrinsic::dbg_value:
418     case Intrinsic::dbg_assign:
419       return true;
420     default:
421       return false;
422     }
423   }
424   static bool classof(const Value *V) {
425     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
426   }
427   /// @}
428 protected:
429   void setArgOperand(unsigned i, Value *v) {
430     DbgInfoIntrinsic::setArgOperand(i, v);
431   }
432   void setOperand(unsigned i, Value *v) { DbgInfoIntrinsic::setOperand(i, v); }
433 };
434 
435 /// This represents the llvm.dbg.declare instruction.
436 class DbgDeclareInst : public DbgVariableIntrinsic {
437 public:
438   Value *getAddress() const {
439     assert(getNumVariableLocationOps() == 1 &&
440            "dbg.declare must have exactly 1 location operand.");
441     return getVariableLocationOp(0);
442   }
443 
444   /// \name Casting methods
445   /// @{
446   static bool classof(const IntrinsicInst *I) {
447     return I->getIntrinsicID() == Intrinsic::dbg_declare;
448   }
449   static bool classof(const Value *V) {
450     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
451   }
452   /// @}
453 };
454 
455 /// This represents the llvm.dbg.value instruction.
456 class DbgValueInst : public DbgVariableIntrinsic {
457 public:
458   // The default argument should only be used in ISel, and the default option
459   // should be removed once ISel support for multiple location ops is complete.
460   Value *getValue(unsigned OpIdx = 0) const {
461     return getVariableLocationOp(OpIdx);
462   }
463   iterator_range<location_op_iterator> getValues() const {
464     return location_ops();
465   }
466 
467   /// \name Casting methods
468   /// @{
469   static bool classof(const IntrinsicInst *I) {
470     return I->getIntrinsicID() == Intrinsic::dbg_value ||
471            I->getIntrinsicID() == Intrinsic::dbg_assign;
472   }
473   static bool classof(const Value *V) {
474     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
475   }
476   /// @}
477 };
478 
479 /// This represents the llvm.dbg.assign instruction.
480 class DbgAssignIntrinsic : public DbgValueInst {
481   enum Operands {
482     OpValue,
483     OpVar,
484     OpExpr,
485     OpAssignID,
486     OpAddress,
487     OpAddressExpr,
488   };
489 
490 public:
491   Value *getAddress() const;
492   Metadata *getRawAddress() const {
493     return cast<MetadataAsValue>(getArgOperand(OpAddress))->getMetadata();
494   }
495   Metadata *getRawAssignID() const {
496     return cast<MetadataAsValue>(getArgOperand(OpAssignID))->getMetadata();
497   }
498   DIAssignID *getAssignID() const { return cast<DIAssignID>(getRawAssignID()); }
499   Metadata *getRawAddressExpression() const {
500     return cast<MetadataAsValue>(getArgOperand(OpAddressExpr))->getMetadata();
501   }
502   DIExpression *getAddressExpression() const {
503     return cast<DIExpression>(getRawAddressExpression());
504   }
505   void setAddressExpression(DIExpression *NewExpr) {
506     setArgOperand(OpAddressExpr,
507                   MetadataAsValue::get(NewExpr->getContext(), NewExpr));
508   }
509   void setAssignId(DIAssignID *New);
510   void setAddress(Value *V);
511   /// Kill the address component.
512   void setKillAddress();
513   /// Check whether this kills the address component. This doesn't take into
514   /// account the position of the intrinsic, therefore a returned value of false
515   /// does not guarentee the address is a valid location for the variable at the
516   /// intrinsic's position in IR.
517   bool isKillAddress() const;
518   void setValue(Value *V);
519   /// \name Casting methods
520   /// @{
521   static bool classof(const IntrinsicInst *I) {
522     return I->getIntrinsicID() == Intrinsic::dbg_assign;
523   }
524   static bool classof(const Value *V) {
525     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
526   }
527   /// @}
528 };
529 
530 /// This represents the llvm.dbg.label instruction.
531 class DbgLabelInst : public DbgInfoIntrinsic {
532 public:
533   DILabel *getLabel() const { return cast<DILabel>(getRawLabel()); }
534 
535   Metadata *getRawLabel() const {
536     return cast<MetadataAsValue>(getArgOperand(0))->getMetadata();
537   }
538 
539   /// Methods for support type inquiry through isa, cast, and dyn_cast:
540   /// @{
541   static bool classof(const IntrinsicInst *I) {
542     return I->getIntrinsicID() == Intrinsic::dbg_label;
543   }
544   static bool classof(const Value *V) {
545     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
546   }
547   /// @}
548 };
549 
550 /// This is the common base class for vector predication intrinsics.
551 class VPIntrinsic : public IntrinsicInst {
552 public:
553   /// \brief Declares a llvm.vp.* intrinsic in \p M that matches the parameters
554   /// \p Params. Additionally, the load and gather intrinsics require
555   /// \p ReturnType to be specified.
556   static Function *getDeclarationForParams(Module *M, Intrinsic::ID,
557                                            Type *ReturnType,
558                                            ArrayRef<Value *> Params);
559 
560   static std::optional<unsigned> getMaskParamPos(Intrinsic::ID IntrinsicID);
561   static std::optional<unsigned> getVectorLengthParamPos(
562       Intrinsic::ID IntrinsicID);
563 
564   /// The llvm.vp.* intrinsics for this instruction Opcode
565   static Intrinsic::ID getForOpcode(unsigned OC);
566 
567   // Whether \p ID is a VP intrinsic ID.
568   static bool isVPIntrinsic(Intrinsic::ID);
569 
570   /// \return The mask parameter or nullptr.
571   Value *getMaskParam() const;
572   void setMaskParam(Value *);
573 
574   /// \return The vector length parameter or nullptr.
575   Value *getVectorLengthParam() const;
576   void setVectorLengthParam(Value *);
577 
578   /// \return Whether the vector length param can be ignored.
579   bool canIgnoreVectorLengthParam() const;
580 
581   /// \return The static element count (vector number of elements) the vector
582   /// length parameter applies to.
583   ElementCount getStaticVectorLength() const;
584 
585   /// \return The alignment of the pointer used by this load/store/gather or
586   /// scatter.
587   MaybeAlign getPointerAlignment() const;
588   // MaybeAlign setPointerAlignment(Align NewAlign); // TODO
589 
590   /// \return The pointer operand of this load,store, gather or scatter.
591   Value *getMemoryPointerParam() const;
592   static std::optional<unsigned> getMemoryPointerParamPos(Intrinsic::ID);
593 
594   /// \return The data (payload) operand of this store or scatter.
595   Value *getMemoryDataParam() const;
596   static std::optional<unsigned> getMemoryDataParamPos(Intrinsic::ID);
597 
598   // Methods for support type inquiry through isa, cast, and dyn_cast:
599   static bool classof(const IntrinsicInst *I) {
600     return isVPIntrinsic(I->getIntrinsicID());
601   }
602   static bool classof(const Value *V) {
603     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
604   }
605 
606   // Equivalent non-predicated opcode
607   std::optional<unsigned> getFunctionalOpcode() const {
608     return getFunctionalOpcodeForVP(getIntrinsicID());
609   }
610 
611   // Equivalent non-predicated intrinsic ID
612   std::optional<unsigned> getFunctionalIntrinsicID() const {
613     return getFunctionalIntrinsicIDForVP(getIntrinsicID());
614   }
615 
616   // Equivalent non-predicated constrained ID
617   std::optional<unsigned> getConstrainedIntrinsicID() const {
618     return getConstrainedIntrinsicIDForVP(getIntrinsicID());
619   }
620 
621   // Equivalent non-predicated opcode
622   static std::optional<unsigned> getFunctionalOpcodeForVP(Intrinsic::ID ID);
623 
624   // Equivalent non-predicated intrinsic ID
625   static std::optional<Intrinsic::ID>
626   getFunctionalIntrinsicIDForVP(Intrinsic::ID ID);
627 
628   // Equivalent non-predicated constrained ID
629   static std::optional<Intrinsic::ID>
630   getConstrainedIntrinsicIDForVP(Intrinsic::ID ID);
631 };
632 
633 /// This represents vector predication reduction intrinsics.
634 class VPReductionIntrinsic : public VPIntrinsic {
635 public:
636   static bool isVPReduction(Intrinsic::ID ID);
637 
638   unsigned getStartParamPos() const;
639   unsigned getVectorParamPos() const;
640 
641   static std::optional<unsigned> getStartParamPos(Intrinsic::ID ID);
642   static std::optional<unsigned> getVectorParamPos(Intrinsic::ID ID);
643 
644   /// Methods for support type inquiry through isa, cast, and dyn_cast:
645   /// @{
646   static bool classof(const IntrinsicInst *I) {
647     return VPReductionIntrinsic::isVPReduction(I->getIntrinsicID());
648   }
649   static bool classof(const Value *V) {
650     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
651   }
652   /// @}
653 };
654 
655 class VPCastIntrinsic : public VPIntrinsic {
656 public:
657   static bool isVPCast(Intrinsic::ID ID);
658 
659   /// Methods for support type inquiry through isa, cast, and dyn_cast:
660   /// @{
661   static bool classof(const IntrinsicInst *I) {
662     return VPCastIntrinsic::isVPCast(I->getIntrinsicID());
663   }
664   static bool classof(const Value *V) {
665     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
666   }
667   /// @}
668 };
669 
670 class VPCmpIntrinsic : public VPIntrinsic {
671 public:
672   static bool isVPCmp(Intrinsic::ID ID);
673 
674   CmpInst::Predicate getPredicate() const;
675 
676   /// Methods for support type inquiry through isa, cast, and dyn_cast:
677   /// @{
678   static bool classof(const IntrinsicInst *I) {
679     return VPCmpIntrinsic::isVPCmp(I->getIntrinsicID());
680   }
681   static bool classof(const Value *V) {
682     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
683   }
684   /// @}
685 };
686 
687 class VPBinOpIntrinsic : public VPIntrinsic {
688 public:
689   static bool isVPBinOp(Intrinsic::ID ID);
690 
691   /// Methods for support type inquiry through isa, cast, and dyn_cast:
692   /// @{
693   static bool classof(const IntrinsicInst *I) {
694     return VPBinOpIntrinsic::isVPBinOp(I->getIntrinsicID());
695   }
696   static bool classof(const Value *V) {
697     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
698   }
699   /// @}
700 };
701 
702 
703 /// This is the common base class for constrained floating point intrinsics.
704 class ConstrainedFPIntrinsic : public IntrinsicInst {
705 public:
706   bool isUnaryOp() const;
707   bool isTernaryOp() const;
708   std::optional<RoundingMode> getRoundingMode() const;
709   std::optional<fp::ExceptionBehavior> getExceptionBehavior() const;
710   bool isDefaultFPEnvironment() const;
711 
712   // Methods for support type inquiry through isa, cast, and dyn_cast:
713   static bool classof(const IntrinsicInst *I);
714   static bool classof(const Value *V) {
715     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
716   }
717 };
718 
719 /// Constrained floating point compare intrinsics.
720 class ConstrainedFPCmpIntrinsic : public ConstrainedFPIntrinsic {
721 public:
722   FCmpInst::Predicate getPredicate() const;
723   bool isSignaling() const {
724     return getIntrinsicID() == Intrinsic::experimental_constrained_fcmps;
725   }
726 
727   // Methods for support type inquiry through isa, cast, and dyn_cast:
728   static bool classof(const IntrinsicInst *I) {
729     switch (I->getIntrinsicID()) {
730     case Intrinsic::experimental_constrained_fcmp:
731     case Intrinsic::experimental_constrained_fcmps:
732       return true;
733     default:
734       return false;
735     }
736   }
737   static bool classof(const Value *V) {
738     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
739   }
740 };
741 
742 /// This class represents min/max intrinsics.
743 class MinMaxIntrinsic : public IntrinsicInst {
744 public:
745   static bool classof(const IntrinsicInst *I) {
746     switch (I->getIntrinsicID()) {
747     case Intrinsic::umin:
748     case Intrinsic::umax:
749     case Intrinsic::smin:
750     case Intrinsic::smax:
751       return true;
752     default:
753       return false;
754     }
755   }
756   static bool classof(const Value *V) {
757     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
758   }
759 
760   Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
761   Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
762 
763   /// Returns the comparison predicate underlying the intrinsic.
764   static ICmpInst::Predicate getPredicate(Intrinsic::ID ID) {
765     switch (ID) {
766     case Intrinsic::umin:
767       return ICmpInst::Predicate::ICMP_ULT;
768     case Intrinsic::umax:
769       return ICmpInst::Predicate::ICMP_UGT;
770     case Intrinsic::smin:
771       return ICmpInst::Predicate::ICMP_SLT;
772     case Intrinsic::smax:
773       return ICmpInst::Predicate::ICMP_SGT;
774     default:
775       llvm_unreachable("Invalid intrinsic");
776     }
777   }
778 
779   /// Returns the comparison predicate underlying the intrinsic.
780   ICmpInst::Predicate getPredicate() const {
781     return getPredicate(getIntrinsicID());
782   }
783 
784   /// Whether the intrinsic is signed or unsigned.
785   static bool isSigned(Intrinsic::ID ID) {
786     return ICmpInst::isSigned(getPredicate(ID));
787   };
788 
789   /// Whether the intrinsic is signed or unsigned.
790   bool isSigned() const { return isSigned(getIntrinsicID()); };
791 
792   /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
793   /// so there is a certain threshold value, upon reaching which,
794   /// their value can no longer change. Return said threshold.
795   static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits) {
796     switch (ID) {
797     case Intrinsic::umin:
798       return APInt::getMinValue(numBits);
799     case Intrinsic::umax:
800       return APInt::getMaxValue(numBits);
801     case Intrinsic::smin:
802       return APInt::getSignedMinValue(numBits);
803     case Intrinsic::smax:
804       return APInt::getSignedMaxValue(numBits);
805     default:
806       llvm_unreachable("Invalid intrinsic");
807     }
808   }
809 
810   /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
811   /// so there is a certain threshold value, upon reaching which,
812   /// their value can no longer change. Return said threshold.
813   APInt getSaturationPoint(unsigned numBits) const {
814     return getSaturationPoint(getIntrinsicID(), numBits);
815   }
816 
817   /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
818   /// so there is a certain threshold value, upon reaching which,
819   /// their value can no longer change. Return said threshold.
820   static Constant *getSaturationPoint(Intrinsic::ID ID, Type *Ty) {
821     return Constant::getIntegerValue(
822         Ty, getSaturationPoint(ID, Ty->getScalarSizeInBits()));
823   }
824 
825   /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
826   /// so there is a certain threshold value, upon reaching which,
827   /// their value can no longer change. Return said threshold.
828   Constant *getSaturationPoint(Type *Ty) const {
829     return getSaturationPoint(getIntrinsicID(), Ty);
830   }
831 };
832 
833 /// This class represents an intrinsic that is based on a binary operation.
834 /// This includes op.with.overflow and saturating add/sub intrinsics.
835 class BinaryOpIntrinsic : public IntrinsicInst {
836 public:
837   static bool classof(const IntrinsicInst *I) {
838     switch (I->getIntrinsicID()) {
839     case Intrinsic::uadd_with_overflow:
840     case Intrinsic::sadd_with_overflow:
841     case Intrinsic::usub_with_overflow:
842     case Intrinsic::ssub_with_overflow:
843     case Intrinsic::umul_with_overflow:
844     case Intrinsic::smul_with_overflow:
845     case Intrinsic::uadd_sat:
846     case Intrinsic::sadd_sat:
847     case Intrinsic::usub_sat:
848     case Intrinsic::ssub_sat:
849       return true;
850     default:
851       return false;
852     }
853   }
854   static bool classof(const Value *V) {
855     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
856   }
857 
858   Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
859   Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
860 
861   /// Returns the binary operation underlying the intrinsic.
862   Instruction::BinaryOps getBinaryOp() const;
863 
864   /// Whether the intrinsic is signed or unsigned.
865   bool isSigned() const;
866 
867   /// Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
868   unsigned getNoWrapKind() const;
869 };
870 
871 /// Represents an op.with.overflow intrinsic.
872 class WithOverflowInst : public BinaryOpIntrinsic {
873 public:
874   static bool classof(const IntrinsicInst *I) {
875     switch (I->getIntrinsicID()) {
876     case Intrinsic::uadd_with_overflow:
877     case Intrinsic::sadd_with_overflow:
878     case Intrinsic::usub_with_overflow:
879     case Intrinsic::ssub_with_overflow:
880     case Intrinsic::umul_with_overflow:
881     case Intrinsic::smul_with_overflow:
882       return true;
883     default:
884       return false;
885     }
886   }
887   static bool classof(const Value *V) {
888     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
889   }
890 };
891 
892 /// Represents a saturating add/sub intrinsic.
893 class SaturatingInst : public BinaryOpIntrinsic {
894 public:
895   static bool classof(const IntrinsicInst *I) {
896     switch (I->getIntrinsicID()) {
897     case Intrinsic::uadd_sat:
898     case Intrinsic::sadd_sat:
899     case Intrinsic::usub_sat:
900     case Intrinsic::ssub_sat:
901       return true;
902     default:
903       return false;
904     }
905   }
906   static bool classof(const Value *V) {
907     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
908   }
909 };
910 
911 /// Common base class for all memory intrinsics. Simply provides
912 /// common methods.
913 /// Written as CRTP to avoid a common base class amongst the
914 /// three atomicity hierarchies.
915 template <typename Derived> class MemIntrinsicBase : public IntrinsicInst {
916 private:
917   enum { ARG_DEST = 0, ARG_LENGTH = 2 };
918 
919 public:
920   Value *getRawDest() const {
921     return const_cast<Value *>(getArgOperand(ARG_DEST));
922   }
923   const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); }
924   Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); }
925 
926   Value *getLength() const {
927     return const_cast<Value *>(getArgOperand(ARG_LENGTH));
928   }
929   const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); }
930   Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); }
931 
932   /// This is just like getRawDest, but it strips off any cast
933   /// instructions (including addrspacecast) that feed it, giving the
934   /// original input.  The returned value is guaranteed to be a pointer.
935   Value *getDest() const { return getRawDest()->stripPointerCasts(); }
936 
937   unsigned getDestAddressSpace() const {
938     return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
939   }
940 
941   /// FIXME: Remove this function once transition to Align is over.
942   /// Use getDestAlign() instead.
943   LLVM_DEPRECATED("Use getDestAlign() instead", "getDestAlign")
944   unsigned getDestAlignment() const {
945     if (auto MA = getParamAlign(ARG_DEST))
946       return MA->value();
947     return 0;
948   }
949   MaybeAlign getDestAlign() const { return getParamAlign(ARG_DEST); }
950 
951   /// Set the specified arguments of the instruction.
952   void setDest(Value *Ptr) {
953     assert(getRawDest()->getType() == Ptr->getType() &&
954            "setDest called with pointer of wrong type!");
955     setArgOperand(ARG_DEST, Ptr);
956   }
957 
958   void setDestAlignment(MaybeAlign Alignment) {
959     removeParamAttr(ARG_DEST, Attribute::Alignment);
960     if (Alignment)
961       addParamAttr(ARG_DEST,
962                    Attribute::getWithAlignment(getContext(), *Alignment));
963   }
964   void setDestAlignment(Align Alignment) {
965     removeParamAttr(ARG_DEST, Attribute::Alignment);
966     addParamAttr(ARG_DEST,
967                  Attribute::getWithAlignment(getContext(), Alignment));
968   }
969 
970   void setLength(Value *L) {
971     assert(getLength()->getType() == L->getType() &&
972            "setLength called with value of wrong type!");
973     setArgOperand(ARG_LENGTH, L);
974   }
975 };
976 
977 /// Common base class for all memory transfer intrinsics. Simply provides
978 /// common methods.
979 template <class BaseCL> class MemTransferBase : public BaseCL {
980 private:
981   enum { ARG_SOURCE = 1 };
982 
983 public:
984   /// Return the arguments to the instruction.
985   Value *getRawSource() const {
986     return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE));
987   }
988   const Use &getRawSourceUse() const {
989     return BaseCL::getArgOperandUse(ARG_SOURCE);
990   }
991   Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); }
992 
993   /// This is just like getRawSource, but it strips off any cast
994   /// instructions that feed it, giving the original input.  The returned
995   /// value is guaranteed to be a pointer.
996   Value *getSource() const { return getRawSource()->stripPointerCasts(); }
997 
998   unsigned getSourceAddressSpace() const {
999     return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
1000   }
1001 
1002   /// FIXME: Remove this function once transition to Align is over.
1003   /// Use getSourceAlign() instead.
1004   LLVM_DEPRECATED("Use getSourceAlign() instead", "getSourceAlign")
1005   unsigned getSourceAlignment() const {
1006     if (auto MA = BaseCL::getParamAlign(ARG_SOURCE))
1007       return MA->value();
1008     return 0;
1009   }
1010 
1011   MaybeAlign getSourceAlign() const {
1012     return BaseCL::getParamAlign(ARG_SOURCE);
1013   }
1014 
1015   void setSource(Value *Ptr) {
1016     assert(getRawSource()->getType() == Ptr->getType() &&
1017            "setSource called with pointer of wrong type!");
1018     BaseCL::setArgOperand(ARG_SOURCE, Ptr);
1019   }
1020 
1021   void setSourceAlignment(MaybeAlign Alignment) {
1022     BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
1023     if (Alignment)
1024       BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
1025                                            BaseCL::getContext(), *Alignment));
1026   }
1027 
1028   void setSourceAlignment(Align Alignment) {
1029     BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
1030     BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
1031                                          BaseCL::getContext(), Alignment));
1032   }
1033 };
1034 
1035 /// Common base class for all memset intrinsics. Simply provides
1036 /// common methods.
1037 template <class BaseCL> class MemSetBase : public BaseCL {
1038 private:
1039   enum { ARG_VALUE = 1 };
1040 
1041 public:
1042   Value *getValue() const {
1043     return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE));
1044   }
1045   const Use &getValueUse() const { return BaseCL::getArgOperandUse(ARG_VALUE); }
1046   Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); }
1047 
1048   void setValue(Value *Val) {
1049     assert(getValue()->getType() == Val->getType() &&
1050            "setValue called with value of wrong type!");
1051     BaseCL::setArgOperand(ARG_VALUE, Val);
1052   }
1053 };
1054 
1055 // The common base class for the atomic memset/memmove/memcpy intrinsics
1056 // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
1057 class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> {
1058 private:
1059   enum { ARG_ELEMENTSIZE = 3 };
1060 
1061 public:
1062   Value *getRawElementSizeInBytes() const {
1063     return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE));
1064   }
1065 
1066   ConstantInt *getElementSizeInBytesCst() const {
1067     return cast<ConstantInt>(getRawElementSizeInBytes());
1068   }
1069 
1070   uint32_t getElementSizeInBytes() const {
1071     return getElementSizeInBytesCst()->getZExtValue();
1072   }
1073 
1074   void setElementSizeInBytes(Constant *V) {
1075     assert(V->getType() == Type::getInt8Ty(getContext()) &&
1076            "setElementSizeInBytes called with value of wrong type!");
1077     setArgOperand(ARG_ELEMENTSIZE, V);
1078   }
1079 
1080   static bool classof(const IntrinsicInst *I) {
1081     switch (I->getIntrinsicID()) {
1082     case Intrinsic::memcpy_element_unordered_atomic:
1083     case Intrinsic::memmove_element_unordered_atomic:
1084     case Intrinsic::memset_element_unordered_atomic:
1085       return true;
1086     default:
1087       return false;
1088     }
1089   }
1090   static bool classof(const Value *V) {
1091     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1092   }
1093 };
1094 
1095 /// This class represents atomic memset intrinsic
1096 // i.e. llvm.element.unordered.atomic.memset
1097 class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> {
1098 public:
1099   static bool classof(const IntrinsicInst *I) {
1100     return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic;
1101   }
1102   static bool classof(const Value *V) {
1103     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1104   }
1105 };
1106 
1107 // This class wraps the atomic memcpy/memmove intrinsics
1108 // i.e. llvm.element.unordered.atomic.memcpy/memmove
1109 class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> {
1110 public:
1111   static bool classof(const IntrinsicInst *I) {
1112     switch (I->getIntrinsicID()) {
1113     case Intrinsic::memcpy_element_unordered_atomic:
1114     case Intrinsic::memmove_element_unordered_atomic:
1115       return true;
1116     default:
1117       return false;
1118     }
1119   }
1120   static bool classof(const Value *V) {
1121     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1122   }
1123 };
1124 
1125 /// This class represents the atomic memcpy intrinsic
1126 /// i.e. llvm.element.unordered.atomic.memcpy
1127 class AtomicMemCpyInst : public AtomicMemTransferInst {
1128 public:
1129   static bool classof(const IntrinsicInst *I) {
1130     return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic;
1131   }
1132   static bool classof(const Value *V) {
1133     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1134   }
1135 };
1136 
1137 /// This class represents the atomic memmove intrinsic
1138 /// i.e. llvm.element.unordered.atomic.memmove
1139 class AtomicMemMoveInst : public AtomicMemTransferInst {
1140 public:
1141   static bool classof(const IntrinsicInst *I) {
1142     return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic;
1143   }
1144   static bool classof(const Value *V) {
1145     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1146   }
1147 };
1148 
1149 /// This is the common base class for memset/memcpy/memmove.
1150 class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> {
1151 private:
1152   enum { ARG_VOLATILE = 3 };
1153 
1154 public:
1155   ConstantInt *getVolatileCst() const {
1156     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(ARG_VOLATILE)));
1157   }
1158 
1159   bool isVolatile() const { return !getVolatileCst()->isZero(); }
1160 
1161   void setVolatile(Constant *V) { setArgOperand(ARG_VOLATILE, V); }
1162 
1163   // Methods for support type inquiry through isa, cast, and dyn_cast:
1164   static bool classof(const IntrinsicInst *I) {
1165     switch (I->getIntrinsicID()) {
1166     case Intrinsic::memcpy:
1167     case Intrinsic::memmove:
1168     case Intrinsic::memset:
1169     case Intrinsic::memset_inline:
1170     case Intrinsic::memcpy_inline:
1171       return true;
1172     default:
1173       return false;
1174     }
1175   }
1176   static bool classof(const Value *V) {
1177     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1178   }
1179 };
1180 
1181 /// This class wraps the llvm.memset and llvm.memset.inline intrinsics.
1182 class MemSetInst : public MemSetBase<MemIntrinsic> {
1183 public:
1184   // Methods for support type inquiry through isa, cast, and dyn_cast:
1185   static bool classof(const IntrinsicInst *I) {
1186     switch (I->getIntrinsicID()) {
1187     case Intrinsic::memset:
1188     case Intrinsic::memset_inline:
1189       return true;
1190     default:
1191       return false;
1192     }
1193   }
1194   static bool classof(const Value *V) {
1195     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1196   }
1197 };
1198 
1199 /// This class wraps the llvm.memset.inline intrinsic.
1200 class MemSetInlineInst : public MemSetInst {
1201 public:
1202   ConstantInt *getLength() const {
1203     return cast<ConstantInt>(MemSetInst::getLength());
1204   }
1205   // Methods for support type inquiry through isa, cast, and dyn_cast:
1206   static bool classof(const IntrinsicInst *I) {
1207     return I->getIntrinsicID() == Intrinsic::memset_inline;
1208   }
1209   static bool classof(const Value *V) {
1210     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1211   }
1212 };
1213 
1214 /// This class wraps the llvm.memcpy/memmove intrinsics.
1215 class MemTransferInst : public MemTransferBase<MemIntrinsic> {
1216 public:
1217   // Methods for support type inquiry through isa, cast, and dyn_cast:
1218   static bool classof(const IntrinsicInst *I) {
1219     switch (I->getIntrinsicID()) {
1220     case Intrinsic::memcpy:
1221     case Intrinsic::memmove:
1222     case Intrinsic::memcpy_inline:
1223       return true;
1224     default:
1225       return false;
1226     }
1227   }
1228   static bool classof(const Value *V) {
1229     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1230   }
1231 };
1232 
1233 /// This class wraps the llvm.memcpy intrinsic.
1234 class MemCpyInst : public MemTransferInst {
1235 public:
1236   // Methods for support type inquiry through isa, cast, and dyn_cast:
1237   static bool classof(const IntrinsicInst *I) {
1238     return I->getIntrinsicID() == Intrinsic::memcpy ||
1239            I->getIntrinsicID() == Intrinsic::memcpy_inline;
1240   }
1241   static bool classof(const Value *V) {
1242     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1243   }
1244 };
1245 
1246 /// This class wraps the llvm.memmove intrinsic.
1247 class MemMoveInst : public MemTransferInst {
1248 public:
1249   // Methods for support type inquiry through isa, cast, and dyn_cast:
1250   static bool classof(const IntrinsicInst *I) {
1251     return I->getIntrinsicID() == Intrinsic::memmove;
1252   }
1253   static bool classof(const Value *V) {
1254     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1255   }
1256 };
1257 
1258 /// This class wraps the llvm.memcpy.inline intrinsic.
1259 class MemCpyInlineInst : public MemCpyInst {
1260 public:
1261   ConstantInt *getLength() const {
1262     return cast<ConstantInt>(MemCpyInst::getLength());
1263   }
1264   // Methods for support type inquiry through isa, cast, and dyn_cast:
1265   static bool classof(const IntrinsicInst *I) {
1266     return I->getIntrinsicID() == Intrinsic::memcpy_inline;
1267   }
1268   static bool classof(const Value *V) {
1269     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1270   }
1271 };
1272 
1273 // The common base class for any memset/memmove/memcpy intrinsics;
1274 // whether they be atomic or non-atomic.
1275 // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
1276 //  and llvm.memset/memcpy/memmove
1277 class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> {
1278 public:
1279   bool isVolatile() const {
1280     // Only the non-atomic intrinsics can be volatile
1281     if (auto *MI = dyn_cast<MemIntrinsic>(this))
1282       return MI->isVolatile();
1283     return false;
1284   }
1285 
1286   static bool classof(const IntrinsicInst *I) {
1287     switch (I->getIntrinsicID()) {
1288     case Intrinsic::memcpy:
1289     case Intrinsic::memcpy_inline:
1290     case Intrinsic::memmove:
1291     case Intrinsic::memset:
1292     case Intrinsic::memset_inline:
1293     case Intrinsic::memcpy_element_unordered_atomic:
1294     case Intrinsic::memmove_element_unordered_atomic:
1295     case Intrinsic::memset_element_unordered_atomic:
1296       return true;
1297     default:
1298       return false;
1299     }
1300   }
1301   static bool classof(const Value *V) {
1302     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1303   }
1304 };
1305 
1306 /// This class represents any memset intrinsic
1307 // i.e. llvm.element.unordered.atomic.memset
1308 // and  llvm.memset
1309 class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> {
1310 public:
1311   static bool classof(const IntrinsicInst *I) {
1312     switch (I->getIntrinsicID()) {
1313     case Intrinsic::memset:
1314     case Intrinsic::memset_inline:
1315     case Intrinsic::memset_element_unordered_atomic:
1316       return true;
1317     default:
1318       return false;
1319     }
1320   }
1321   static bool classof(const Value *V) {
1322     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1323   }
1324 };
1325 
1326 // This class wraps any memcpy/memmove intrinsics
1327 // i.e. llvm.element.unordered.atomic.memcpy/memmove
1328 // and  llvm.memcpy/memmove
1329 class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> {
1330 public:
1331   static bool classof(const IntrinsicInst *I) {
1332     switch (I->getIntrinsicID()) {
1333     case Intrinsic::memcpy:
1334     case Intrinsic::memcpy_inline:
1335     case Intrinsic::memmove:
1336     case Intrinsic::memcpy_element_unordered_atomic:
1337     case Intrinsic::memmove_element_unordered_atomic:
1338       return true;
1339     default:
1340       return false;
1341     }
1342   }
1343   static bool classof(const Value *V) {
1344     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1345   }
1346 };
1347 
1348 /// This class represents any memcpy intrinsic
1349 /// i.e. llvm.element.unordered.atomic.memcpy
1350 ///  and llvm.memcpy
1351 class AnyMemCpyInst : public AnyMemTransferInst {
1352 public:
1353   static bool classof(const IntrinsicInst *I) {
1354     switch (I->getIntrinsicID()) {
1355     case Intrinsic::memcpy:
1356     case Intrinsic::memcpy_inline:
1357     case Intrinsic::memcpy_element_unordered_atomic:
1358       return true;
1359     default:
1360       return false;
1361     }
1362   }
1363   static bool classof(const Value *V) {
1364     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1365   }
1366 };
1367 
1368 /// This class represents any memmove intrinsic
1369 /// i.e. llvm.element.unordered.atomic.memmove
1370 ///  and llvm.memmove
1371 class AnyMemMoveInst : public AnyMemTransferInst {
1372 public:
1373   static bool classof(const IntrinsicInst *I) {
1374     switch (I->getIntrinsicID()) {
1375     case Intrinsic::memmove:
1376     case Intrinsic::memmove_element_unordered_atomic:
1377       return true;
1378     default:
1379       return false;
1380     }
1381   }
1382   static bool classof(const Value *V) {
1383     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1384   }
1385 };
1386 
1387 /// This represents the llvm.va_start intrinsic.
1388 class VAStartInst : public IntrinsicInst {
1389 public:
1390   static bool classof(const IntrinsicInst *I) {
1391     return I->getIntrinsicID() == Intrinsic::vastart;
1392   }
1393   static bool classof(const Value *V) {
1394     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1395   }
1396 
1397   Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
1398 };
1399 
1400 /// This represents the llvm.va_end intrinsic.
1401 class VAEndInst : public IntrinsicInst {
1402 public:
1403   static bool classof(const IntrinsicInst *I) {
1404     return I->getIntrinsicID() == Intrinsic::vaend;
1405   }
1406   static bool classof(const Value *V) {
1407     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1408   }
1409 
1410   Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
1411 };
1412 
1413 /// This represents the llvm.va_copy intrinsic.
1414 class VACopyInst : public IntrinsicInst {
1415 public:
1416   static bool classof(const IntrinsicInst *I) {
1417     return I->getIntrinsicID() == Intrinsic::vacopy;
1418   }
1419   static bool classof(const Value *V) {
1420     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1421   }
1422 
1423   Value *getDest() const { return const_cast<Value *>(getArgOperand(0)); }
1424   Value *getSrc() const { return const_cast<Value *>(getArgOperand(1)); }
1425 };
1426 
1427 /// A base class for all instrprof intrinsics.
1428 class InstrProfInstBase : public IntrinsicInst {
1429 public:
1430   // The name of the instrumented function.
1431   GlobalVariable *getName() const {
1432     return cast<GlobalVariable>(
1433         const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
1434   }
1435   // The hash of the CFG for the instrumented function.
1436   ConstantInt *getHash() const {
1437     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
1438   }
1439 };
1440 
1441 /// A base class for all instrprof counter intrinsics.
1442 class InstrProfCntrInstBase : public InstrProfInstBase {
1443 public:
1444   // The number of counters for the instrumented function.
1445   ConstantInt *getNumCounters() const;
1446   // The index of the counter that this instruction acts on.
1447   ConstantInt *getIndex() const;
1448 };
1449 
1450 /// This represents the llvm.instrprof.cover intrinsic.
1451 class InstrProfCoverInst : public InstrProfCntrInstBase {
1452 public:
1453   static bool classof(const IntrinsicInst *I) {
1454     return I->getIntrinsicID() == Intrinsic::instrprof_cover;
1455   }
1456   static bool classof(const Value *V) {
1457     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1458   }
1459 };
1460 
1461 /// This represents the llvm.instrprof.increment intrinsic.
1462 class InstrProfIncrementInst : public InstrProfCntrInstBase {
1463 public:
1464   static bool classof(const IntrinsicInst *I) {
1465     return I->getIntrinsicID() == Intrinsic::instrprof_increment ||
1466            I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
1467   }
1468   static bool classof(const Value *V) {
1469     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1470   }
1471   Value *getStep() const;
1472 };
1473 
1474 /// This represents the llvm.instrprof.increment.step intrinsic.
1475 class InstrProfIncrementInstStep : public InstrProfIncrementInst {
1476 public:
1477   static bool classof(const IntrinsicInst *I) {
1478     return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
1479   }
1480   static bool classof(const Value *V) {
1481     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1482   }
1483 };
1484 
1485 /// This represents the llvm.instrprof.timestamp intrinsic.
1486 class InstrProfTimestampInst : public InstrProfCntrInstBase {
1487 public:
1488   static bool classof(const IntrinsicInst *I) {
1489     return I->getIntrinsicID() == Intrinsic::instrprof_timestamp;
1490   }
1491   static bool classof(const Value *V) {
1492     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1493   }
1494 };
1495 
1496 /// This represents the llvm.instrprof.value.profile intrinsic.
1497 class InstrProfValueProfileInst : public InstrProfCntrInstBase {
1498 public:
1499   static bool classof(const IntrinsicInst *I) {
1500     return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
1501   }
1502   static bool classof(const Value *V) {
1503     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1504   }
1505 
1506   Value *getTargetValue() const {
1507     return cast<Value>(const_cast<Value *>(getArgOperand(2)));
1508   }
1509 
1510   ConstantInt *getValueKind() const {
1511     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
1512   }
1513 
1514   // Returns the value site index.
1515   ConstantInt *getIndex() const {
1516     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
1517   }
1518 };
1519 
1520 /// A base class for instrprof mcdc intrinsics that require global bitmap bytes.
1521 class InstrProfMCDCBitmapInstBase : public InstrProfInstBase {
1522 public:
1523   static bool classof(const IntrinsicInst *I) {
1524     return I->getIntrinsicID() == Intrinsic::instrprof_mcdc_parameters ||
1525            I->getIntrinsicID() == Intrinsic::instrprof_mcdc_tvbitmap_update;
1526   }
1527   static bool classof(const Value *V) {
1528     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1529   }
1530 
1531   /// \return The number of bytes used for the MCDC bitmaps for the instrumented
1532   /// function.
1533   ConstantInt *getNumBitmapBytes() const {
1534     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
1535   }
1536 };
1537 
1538 /// This represents the llvm.instrprof.mcdc.parameters intrinsic.
1539 class InstrProfMCDCBitmapParameters : public InstrProfMCDCBitmapInstBase {
1540 public:
1541   static bool classof(const IntrinsicInst *I) {
1542     return I->getIntrinsicID() == Intrinsic::instrprof_mcdc_parameters;
1543   }
1544   static bool classof(const Value *V) {
1545     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1546   }
1547 };
1548 
1549 /// This represents the llvm.instrprof.mcdc.tvbitmap.update intrinsic.
1550 class InstrProfMCDCTVBitmapUpdate : public InstrProfMCDCBitmapInstBase {
1551 public:
1552   static bool classof(const IntrinsicInst *I) {
1553     return I->getIntrinsicID() == Intrinsic::instrprof_mcdc_tvbitmap_update;
1554   }
1555   static bool classof(const Value *V) {
1556     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1557   }
1558 
1559   /// \return The index of the TestVector Bitmap upon which this intrinsic
1560   /// acts.
1561   ConstantInt *getBitmapIndex() const {
1562     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
1563   }
1564 
1565   /// \return The address of the corresponding condition bitmap containing
1566   /// the index of the TestVector to update within the TestVector Bitmap.
1567   Value *getMCDCCondBitmapAddr() const {
1568     return cast<Value>(const_cast<Value *>(getArgOperand(4)));
1569   }
1570 };
1571 
1572 /// This represents the llvm.instrprof.mcdc.condbitmap.update intrinsic.
1573 /// It does not pertain to global bitmap updates or parameters and so doesn't
1574 /// inherit from InstrProfMCDCBitmapInstBase.
1575 class InstrProfMCDCCondBitmapUpdate : public InstrProfInstBase {
1576 public:
1577   static bool classof(const IntrinsicInst *I) {
1578     return I->getIntrinsicID() == Intrinsic::instrprof_mcdc_condbitmap_update;
1579   }
1580   static bool classof(const Value *V) {
1581     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1582   }
1583 
1584   /// \return The ID of the condition to update.
1585   ConstantInt *getCondID() const {
1586     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
1587   }
1588 
1589   /// \return The address of the corresponding condition bitmap.
1590   Value *getMCDCCondBitmapAddr() const {
1591     return cast<Value>(const_cast<Value *>(getArgOperand(3)));
1592   }
1593 
1594   /// \return The boolean value to set in the condition bitmap for the
1595   /// corresponding condition ID. This represents how the condition evaluated.
1596   Value *getCondBool() const {
1597     return cast<Value>(const_cast<Value *>(getArgOperand(4)));
1598   }
1599 };
1600 
1601 class PseudoProbeInst : public IntrinsicInst {
1602 public:
1603   static bool classof(const IntrinsicInst *I) {
1604     return I->getIntrinsicID() == Intrinsic::pseudoprobe;
1605   }
1606 
1607   static bool classof(const Value *V) {
1608     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1609   }
1610 
1611   ConstantInt *getFuncGuid() const {
1612     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(0)));
1613   }
1614 
1615   ConstantInt *getIndex() const {
1616     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
1617   }
1618 
1619   ConstantInt *getAttributes() const {
1620     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
1621   }
1622 
1623   ConstantInt *getFactor() const {
1624     return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
1625   }
1626 };
1627 
1628 class NoAliasScopeDeclInst : public IntrinsicInst {
1629 public:
1630   static bool classof(const IntrinsicInst *I) {
1631     return I->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl;
1632   }
1633 
1634   static bool classof(const Value *V) {
1635     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1636   }
1637 
1638   MDNode *getScopeList() const {
1639     auto *MV =
1640         cast<MetadataAsValue>(getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
1641     return cast<MDNode>(MV->getMetadata());
1642   }
1643 
1644   void setScopeList(MDNode *ScopeList) {
1645     setOperand(Intrinsic::NoAliasScopeDeclScopeArg,
1646                MetadataAsValue::get(getContext(), ScopeList));
1647   }
1648 };
1649 
1650 /// Common base class for representing values projected from a statepoint.
1651 /// Currently, the only projections available are gc.result and gc.relocate.
1652 class GCProjectionInst : public IntrinsicInst {
1653 public:
1654   static bool classof(const IntrinsicInst *I) {
1655     return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
1656       I->getIntrinsicID() == Intrinsic::experimental_gc_result;
1657   }
1658 
1659   static bool classof(const Value *V) {
1660     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1661   }
1662 
1663   /// Return true if this relocate is tied to the invoke statepoint.
1664   /// This includes relocates which are on the unwinding path.
1665   bool isTiedToInvoke() const {
1666     const Value *Token = getArgOperand(0);
1667 
1668     return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
1669   }
1670 
1671   /// The statepoint with which this gc.relocate is associated.
1672   const Value *getStatepoint() const;
1673 };
1674 
1675 /// Represents calls to the gc.relocate intrinsic.
1676 class GCRelocateInst : public GCProjectionInst {
1677 public:
1678   static bool classof(const IntrinsicInst *I) {
1679     return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
1680   }
1681 
1682   static bool classof(const Value *V) {
1683     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1684   }
1685 
1686   /// The index into the associate statepoint's argument list
1687   /// which contains the base pointer of the pointer whose
1688   /// relocation this gc.relocate describes.
1689   unsigned getBasePtrIndex() const {
1690     return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
1691   }
1692 
1693   /// The index into the associate statepoint's argument list which
1694   /// contains the pointer whose relocation this gc.relocate describes.
1695   unsigned getDerivedPtrIndex() const {
1696     return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
1697   }
1698 
1699   Value *getBasePtr() const;
1700   Value *getDerivedPtr() const;
1701 };
1702 
1703 /// Represents calls to the gc.result intrinsic.
1704 class GCResultInst : public GCProjectionInst {
1705 public:
1706   static bool classof(const IntrinsicInst *I) {
1707     return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
1708   }
1709 
1710   static bool classof(const Value *V) {
1711     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1712   }
1713 };
1714 
1715 
1716 /// This represents the llvm.assume intrinsic.
1717 class AssumeInst : public IntrinsicInst {
1718 public:
1719   static bool classof(const IntrinsicInst *I) {
1720     return I->getIntrinsicID() == Intrinsic::assume;
1721   }
1722   static bool classof(const Value *V) {
1723     return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1724   }
1725 };
1726 
1727 } // end namespace llvm
1728 
1729 #endif // LLVM_IR_INTRINSICINST_H
1730