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