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