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