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