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