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