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 /// \brief Declares a llvm.vp.* intrinsic in \p M that matches the parameters
393 /// \p Params.
394 static Function *getDeclarationForParams(Module *M, Intrinsic::ID,
395 ArrayRef<Value *> Params);
396
397 static Optional<unsigned> getMaskParamPos(Intrinsic::ID IntrinsicID);
398 static Optional<unsigned> getVectorLengthParamPos(Intrinsic::ID IntrinsicID);
399
400 /// The llvm.vp.* intrinsics for this instruction Opcode
401 static Intrinsic::ID getForOpcode(unsigned OC);
402
403 // Whether \p ID is a VP intrinsic ID.
404 static bool isVPIntrinsic(Intrinsic::ID);
405
406 /// \return The mask parameter or nullptr.
407 Value *getMaskParam() const;
408 void setMaskParam(Value *);
409
410 /// \return The vector length parameter or nullptr.
411 Value *getVectorLengthParam() const;
412 void setVectorLengthParam(Value *);
413
414 /// \return Whether the vector length param can be ignored.
415 bool canIgnoreVectorLengthParam() const;
416
417 /// \return The static element count (vector number of elements) the vector
418 /// length parameter applies to.
419 ElementCount getStaticVectorLength() const;
420
421 /// \return The alignment of the pointer used by this load/store/gather or
422 /// scatter.
423 MaybeAlign getPointerAlignment() const;
424 // MaybeAlign setPointerAlignment(Align NewAlign); // TODO
425
426 /// \return The pointer operand of this load,store, gather or scatter.
427 Value *getMemoryPointerParam() const;
428 static Optional<unsigned> getMemoryPointerParamPos(Intrinsic::ID);
429
430 /// \return The data (payload) operand of this store or scatter.
431 Value *getMemoryDataParam() const;
432 static Optional<unsigned> getMemoryDataParamPos(Intrinsic::ID);
433
434 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)435 static bool classof(const IntrinsicInst *I) {
436 return isVPIntrinsic(I->getIntrinsicID());
437 }
classof(const Value * V)438 static bool classof(const Value *V) {
439 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
440 }
441
442 // Equivalent non-predicated opcode
getFunctionalOpcode()443 Optional<unsigned> getFunctionalOpcode() const {
444 return getFunctionalOpcodeForVP(getIntrinsicID());
445 }
446
447 // Equivalent non-predicated opcode
448 static Optional<unsigned> getFunctionalOpcodeForVP(Intrinsic::ID ID);
449 };
450
451 /// This is the common base class for constrained floating point intrinsics.
452 class ConstrainedFPIntrinsic : public IntrinsicInst {
453 public:
454 bool isUnaryOp() const;
455 bool isTernaryOp() const;
456 Optional<RoundingMode> getRoundingMode() const;
457 Optional<fp::ExceptionBehavior> getExceptionBehavior() const;
458 bool isDefaultFPEnvironment() const;
459
460 // Methods for support type inquiry through isa, cast, and dyn_cast:
461 static bool classof(const IntrinsicInst *I);
classof(const Value * V)462 static bool classof(const Value *V) {
463 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
464 }
465 };
466
467 /// Constrained floating point compare intrinsics.
468 class ConstrainedFPCmpIntrinsic : public ConstrainedFPIntrinsic {
469 public:
470 FCmpInst::Predicate getPredicate() const;
471
472 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)473 static bool classof(const IntrinsicInst *I) {
474 switch (I->getIntrinsicID()) {
475 case Intrinsic::experimental_constrained_fcmp:
476 case Intrinsic::experimental_constrained_fcmps:
477 return true;
478 default:
479 return false;
480 }
481 }
classof(const Value * V)482 static bool classof(const Value *V) {
483 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
484 }
485 };
486
487 /// This class represents min/max intrinsics.
488 class MinMaxIntrinsic : public IntrinsicInst {
489 public:
classof(const IntrinsicInst * I)490 static bool classof(const IntrinsicInst *I) {
491 switch (I->getIntrinsicID()) {
492 case Intrinsic::umin:
493 case Intrinsic::umax:
494 case Intrinsic::smin:
495 case Intrinsic::smax:
496 return true;
497 default:
498 return false;
499 }
500 }
classof(const Value * V)501 static bool classof(const Value *V) {
502 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
503 }
504
getLHS()505 Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
getRHS()506 Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
507
508 /// Returns the comparison predicate underlying the intrinsic.
getPredicate()509 ICmpInst::Predicate getPredicate() const {
510 switch (getIntrinsicID()) {
511 case Intrinsic::umin:
512 return ICmpInst::Predicate::ICMP_ULT;
513 case Intrinsic::umax:
514 return ICmpInst::Predicate::ICMP_UGT;
515 case Intrinsic::smin:
516 return ICmpInst::Predicate::ICMP_SLT;
517 case Intrinsic::smax:
518 return ICmpInst::Predicate::ICMP_SGT;
519 default:
520 llvm_unreachable("Invalid intrinsic");
521 }
522 }
523
524 /// Whether the intrinsic is signed or unsigned.
isSigned()525 bool isSigned() const { return ICmpInst::isSigned(getPredicate()); };
526 };
527
528 /// This class represents an intrinsic that is based on a binary operation.
529 /// This includes op.with.overflow and saturating add/sub intrinsics.
530 class BinaryOpIntrinsic : public IntrinsicInst {
531 public:
classof(const IntrinsicInst * I)532 static bool classof(const IntrinsicInst *I) {
533 switch (I->getIntrinsicID()) {
534 case Intrinsic::uadd_with_overflow:
535 case Intrinsic::sadd_with_overflow:
536 case Intrinsic::usub_with_overflow:
537 case Intrinsic::ssub_with_overflow:
538 case Intrinsic::umul_with_overflow:
539 case Intrinsic::smul_with_overflow:
540 case Intrinsic::uadd_sat:
541 case Intrinsic::sadd_sat:
542 case Intrinsic::usub_sat:
543 case Intrinsic::ssub_sat:
544 return true;
545 default:
546 return false;
547 }
548 }
classof(const Value * V)549 static bool classof(const Value *V) {
550 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
551 }
552
getLHS()553 Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
getRHS()554 Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
555
556 /// Returns the binary operation underlying the intrinsic.
557 Instruction::BinaryOps getBinaryOp() const;
558
559 /// Whether the intrinsic is signed or unsigned.
560 bool isSigned() const;
561
562 /// Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
563 unsigned getNoWrapKind() const;
564 };
565
566 /// Represents an op.with.overflow intrinsic.
567 class WithOverflowInst : public BinaryOpIntrinsic {
568 public:
classof(const IntrinsicInst * I)569 static bool classof(const IntrinsicInst *I) {
570 switch (I->getIntrinsicID()) {
571 case Intrinsic::uadd_with_overflow:
572 case Intrinsic::sadd_with_overflow:
573 case Intrinsic::usub_with_overflow:
574 case Intrinsic::ssub_with_overflow:
575 case Intrinsic::umul_with_overflow:
576 case Intrinsic::smul_with_overflow:
577 return true;
578 default:
579 return false;
580 }
581 }
classof(const Value * V)582 static bool classof(const Value *V) {
583 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
584 }
585 };
586
587 /// Represents a saturating add/sub intrinsic.
588 class SaturatingInst : public BinaryOpIntrinsic {
589 public:
classof(const IntrinsicInst * I)590 static bool classof(const IntrinsicInst *I) {
591 switch (I->getIntrinsicID()) {
592 case Intrinsic::uadd_sat:
593 case Intrinsic::sadd_sat:
594 case Intrinsic::usub_sat:
595 case Intrinsic::ssub_sat:
596 return true;
597 default:
598 return false;
599 }
600 }
classof(const Value * V)601 static bool classof(const Value *V) {
602 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
603 }
604 };
605
606 /// Common base class for all memory intrinsics. Simply provides
607 /// common methods.
608 /// Written as CRTP to avoid a common base class amongst the
609 /// three atomicity hierarchies.
610 template <typename Derived> class MemIntrinsicBase : public IntrinsicInst {
611 private:
612 enum { ARG_DEST = 0, ARG_LENGTH = 2 };
613
614 public:
getRawDest()615 Value *getRawDest() const {
616 return const_cast<Value *>(getArgOperand(ARG_DEST));
617 }
getRawDestUse()618 const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); }
getRawDestUse()619 Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); }
620
getLength()621 Value *getLength() const {
622 return const_cast<Value *>(getArgOperand(ARG_LENGTH));
623 }
getLengthUse()624 const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); }
getLengthUse()625 Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); }
626
627 /// This is just like getRawDest, but it strips off any cast
628 /// instructions (including addrspacecast) that feed it, giving the
629 /// original input. The returned value is guaranteed to be a pointer.
getDest()630 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
631
getDestAddressSpace()632 unsigned getDestAddressSpace() const {
633 return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
634 }
635
636 /// FIXME: Remove this function once transition to Align is over.
637 /// Use getDestAlign() instead.
getDestAlignment()638 unsigned getDestAlignment() const {
639 if (auto MA = getParamAlign(ARG_DEST))
640 return MA->value();
641 return 0;
642 }
getDestAlign()643 MaybeAlign getDestAlign() const { return getParamAlign(ARG_DEST); }
644
645 /// Set the specified arguments of the instruction.
setDest(Value * Ptr)646 void setDest(Value *Ptr) {
647 assert(getRawDest()->getType() == Ptr->getType() &&
648 "setDest called with pointer of wrong type!");
649 setArgOperand(ARG_DEST, Ptr);
650 }
651
652 /// FIXME: Remove this function once transition to Align is over.
653 /// Use the version that takes MaybeAlign instead of this one.
setDestAlignment(unsigned Alignment)654 void setDestAlignment(unsigned Alignment) {
655 setDestAlignment(MaybeAlign(Alignment));
656 }
setDestAlignment(MaybeAlign Alignment)657 void setDestAlignment(MaybeAlign Alignment) {
658 removeParamAttr(ARG_DEST, Attribute::Alignment);
659 if (Alignment)
660 addParamAttr(ARG_DEST,
661 Attribute::getWithAlignment(getContext(), *Alignment));
662 }
setDestAlignment(Align Alignment)663 void setDestAlignment(Align Alignment) {
664 removeParamAttr(ARG_DEST, Attribute::Alignment);
665 addParamAttr(ARG_DEST,
666 Attribute::getWithAlignment(getContext(), Alignment));
667 }
668
setLength(Value * L)669 void setLength(Value *L) {
670 assert(getLength()->getType() == L->getType() &&
671 "setLength called with value of wrong type!");
672 setArgOperand(ARG_LENGTH, L);
673 }
674 };
675
676 /// Common base class for all memory transfer intrinsics. Simply provides
677 /// common methods.
678 template <class BaseCL> class MemTransferBase : public BaseCL {
679 private:
680 enum { ARG_SOURCE = 1 };
681
682 public:
683 /// Return the arguments to the instruction.
getRawSource()684 Value *getRawSource() const {
685 return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE));
686 }
getRawSourceUse()687 const Use &getRawSourceUse() const {
688 return BaseCL::getArgOperandUse(ARG_SOURCE);
689 }
getRawSourceUse()690 Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); }
691
692 /// This is just like getRawSource, but it strips off any cast
693 /// instructions that feed it, giving the original input. The returned
694 /// value is guaranteed to be a pointer.
getSource()695 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
696
getSourceAddressSpace()697 unsigned getSourceAddressSpace() const {
698 return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
699 }
700
701 /// FIXME: Remove this function once transition to Align is over.
702 /// Use getSourceAlign() instead.
getSourceAlignment()703 unsigned getSourceAlignment() const {
704 if (auto MA = BaseCL::getParamAlign(ARG_SOURCE))
705 return MA->value();
706 return 0;
707 }
708
getSourceAlign()709 MaybeAlign getSourceAlign() const {
710 return BaseCL::getParamAlign(ARG_SOURCE);
711 }
712
setSource(Value * Ptr)713 void setSource(Value *Ptr) {
714 assert(getRawSource()->getType() == Ptr->getType() &&
715 "setSource called with pointer of wrong type!");
716 BaseCL::setArgOperand(ARG_SOURCE, Ptr);
717 }
718
719 /// FIXME: Remove this function once transition to Align is over.
720 /// Use the version that takes MaybeAlign instead of this one.
setSourceAlignment(unsigned Alignment)721 void setSourceAlignment(unsigned Alignment) {
722 setSourceAlignment(MaybeAlign(Alignment));
723 }
setSourceAlignment(MaybeAlign Alignment)724 void setSourceAlignment(MaybeAlign Alignment) {
725 BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
726 if (Alignment)
727 BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
728 BaseCL::getContext(), *Alignment));
729 }
setSourceAlignment(Align Alignment)730 void setSourceAlignment(Align Alignment) {
731 BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
732 BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
733 BaseCL::getContext(), Alignment));
734 }
735 };
736
737 /// Common base class for all memset intrinsics. Simply provides
738 /// common methods.
739 template <class BaseCL> class MemSetBase : public BaseCL {
740 private:
741 enum { ARG_VALUE = 1 };
742
743 public:
getValue()744 Value *getValue() const {
745 return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE));
746 }
getValueUse()747 const Use &getValueUse() const { return BaseCL::getArgOperandUse(ARG_VALUE); }
getValueUse()748 Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); }
749
setValue(Value * Val)750 void setValue(Value *Val) {
751 assert(getValue()->getType() == Val->getType() &&
752 "setValue called with value of wrong type!");
753 BaseCL::setArgOperand(ARG_VALUE, Val);
754 }
755 };
756
757 // The common base class for the atomic memset/memmove/memcpy intrinsics
758 // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
759 class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> {
760 private:
761 enum { ARG_ELEMENTSIZE = 3 };
762
763 public:
getRawElementSizeInBytes()764 Value *getRawElementSizeInBytes() const {
765 return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE));
766 }
767
getElementSizeInBytesCst()768 ConstantInt *getElementSizeInBytesCst() const {
769 return cast<ConstantInt>(getRawElementSizeInBytes());
770 }
771
getElementSizeInBytes()772 uint32_t getElementSizeInBytes() const {
773 return getElementSizeInBytesCst()->getZExtValue();
774 }
775
setElementSizeInBytes(Constant * V)776 void setElementSizeInBytes(Constant *V) {
777 assert(V->getType() == Type::getInt8Ty(getContext()) &&
778 "setElementSizeInBytes called with value of wrong type!");
779 setArgOperand(ARG_ELEMENTSIZE, V);
780 }
781
classof(const IntrinsicInst * I)782 static bool classof(const IntrinsicInst *I) {
783 switch (I->getIntrinsicID()) {
784 case Intrinsic::memcpy_element_unordered_atomic:
785 case Intrinsic::memmove_element_unordered_atomic:
786 case Intrinsic::memset_element_unordered_atomic:
787 return true;
788 default:
789 return false;
790 }
791 }
classof(const Value * V)792 static bool classof(const Value *V) {
793 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
794 }
795 };
796
797 /// This class represents atomic memset intrinsic
798 // i.e. llvm.element.unordered.atomic.memset
799 class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> {
800 public:
classof(const IntrinsicInst * I)801 static bool classof(const IntrinsicInst *I) {
802 return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic;
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 wraps the atomic memcpy/memmove intrinsics
810 // i.e. llvm.element.unordered.atomic.memcpy/memmove
811 class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> {
812 public:
classof(const IntrinsicInst * I)813 static bool classof(const IntrinsicInst *I) {
814 switch (I->getIntrinsicID()) {
815 case Intrinsic::memcpy_element_unordered_atomic:
816 case Intrinsic::memmove_element_unordered_atomic:
817 return true;
818 default:
819 return false;
820 }
821 }
classof(const Value * V)822 static bool classof(const Value *V) {
823 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
824 }
825 };
826
827 /// This class represents the atomic memcpy intrinsic
828 /// i.e. llvm.element.unordered.atomic.memcpy
829 class AtomicMemCpyInst : public AtomicMemTransferInst {
830 public:
classof(const IntrinsicInst * I)831 static bool classof(const IntrinsicInst *I) {
832 return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic;
833 }
classof(const Value * V)834 static bool classof(const Value *V) {
835 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
836 }
837 };
838
839 /// This class represents the atomic memmove intrinsic
840 /// i.e. llvm.element.unordered.atomic.memmove
841 class AtomicMemMoveInst : public AtomicMemTransferInst {
842 public:
classof(const IntrinsicInst * I)843 static bool classof(const IntrinsicInst *I) {
844 return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic;
845 }
classof(const Value * V)846 static bool classof(const Value *V) {
847 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
848 }
849 };
850
851 /// This is the common base class for memset/memcpy/memmove.
852 class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> {
853 private:
854 enum { ARG_VOLATILE = 3 };
855
856 public:
getVolatileCst()857 ConstantInt *getVolatileCst() const {
858 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(ARG_VOLATILE)));
859 }
860
isVolatile()861 bool isVolatile() const { return !getVolatileCst()->isZero(); }
862
setVolatile(Constant * V)863 void setVolatile(Constant *V) { setArgOperand(ARG_VOLATILE, V); }
864
865 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)866 static bool classof(const IntrinsicInst *I) {
867 switch (I->getIntrinsicID()) {
868 case Intrinsic::memcpy:
869 case Intrinsic::memmove:
870 case Intrinsic::memset:
871 case Intrinsic::memcpy_inline:
872 return true;
873 default:
874 return false;
875 }
876 }
classof(const Value * V)877 static bool classof(const Value *V) {
878 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
879 }
880 };
881
882 /// This class wraps the llvm.memset intrinsic.
883 class MemSetInst : public MemSetBase<MemIntrinsic> {
884 public:
885 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)886 static bool classof(const IntrinsicInst *I) {
887 return I->getIntrinsicID() == Intrinsic::memset;
888 }
classof(const Value * V)889 static bool classof(const Value *V) {
890 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
891 }
892 };
893
894 /// This class wraps the llvm.memcpy/memmove intrinsics.
895 class MemTransferInst : public MemTransferBase<MemIntrinsic> {
896 public:
897 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)898 static bool classof(const IntrinsicInst *I) {
899 switch (I->getIntrinsicID()) {
900 case Intrinsic::memcpy:
901 case Intrinsic::memmove:
902 case Intrinsic::memcpy_inline:
903 return true;
904 default:
905 return false;
906 }
907 }
classof(const Value * V)908 static bool classof(const Value *V) {
909 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
910 }
911 };
912
913 /// This class wraps the llvm.memcpy intrinsic.
914 class MemCpyInst : public MemTransferInst {
915 public:
916 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)917 static bool classof(const IntrinsicInst *I) {
918 return I->getIntrinsicID() == Intrinsic::memcpy ||
919 I->getIntrinsicID() == Intrinsic::memcpy_inline;
920 }
classof(const Value * V)921 static bool classof(const Value *V) {
922 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
923 }
924 };
925
926 /// This class wraps the llvm.memmove intrinsic.
927 class MemMoveInst : public MemTransferInst {
928 public:
929 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)930 static bool classof(const IntrinsicInst *I) {
931 return I->getIntrinsicID() == Intrinsic::memmove;
932 }
classof(const Value * V)933 static bool classof(const Value *V) {
934 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
935 }
936 };
937
938 /// This class wraps the llvm.memcpy.inline intrinsic.
939 class MemCpyInlineInst : public MemCpyInst {
940 public:
getLength()941 ConstantInt *getLength() const {
942 return cast<ConstantInt>(MemCpyInst::getLength());
943 }
944 // Methods for support type inquiry through isa, cast, and dyn_cast:
classof(const IntrinsicInst * I)945 static bool classof(const IntrinsicInst *I) {
946 return I->getIntrinsicID() == Intrinsic::memcpy_inline;
947 }
classof(const Value * V)948 static bool classof(const Value *V) {
949 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
950 }
951 };
952
953 // The common base class for any memset/memmove/memcpy intrinsics;
954 // whether they be atomic or non-atomic.
955 // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
956 // and llvm.memset/memcpy/memmove
957 class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> {
958 public:
isVolatile()959 bool isVolatile() const {
960 // Only the non-atomic intrinsics can be volatile
961 if (auto *MI = dyn_cast<MemIntrinsic>(this))
962 return MI->isVolatile();
963 return false;
964 }
965
classof(const IntrinsicInst * I)966 static bool classof(const IntrinsicInst *I) {
967 switch (I->getIntrinsicID()) {
968 case Intrinsic::memcpy:
969 case Intrinsic::memcpy_inline:
970 case Intrinsic::memmove:
971 case Intrinsic::memset:
972 case Intrinsic::memcpy_element_unordered_atomic:
973 case Intrinsic::memmove_element_unordered_atomic:
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 represents any memset intrinsic
986 // i.e. llvm.element.unordered.atomic.memset
987 // and llvm.memset
988 class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> {
989 public:
classof(const IntrinsicInst * I)990 static bool classof(const IntrinsicInst *I) {
991 switch (I->getIntrinsicID()) {
992 case Intrinsic::memset:
993 case Intrinsic::memset_element_unordered_atomic:
994 return true;
995 default:
996 return false;
997 }
998 }
classof(const Value * V)999 static bool classof(const Value *V) {
1000 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1001 }
1002 };
1003
1004 // This class wraps any memcpy/memmove intrinsics
1005 // i.e. llvm.element.unordered.atomic.memcpy/memmove
1006 // and llvm.memcpy/memmove
1007 class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> {
1008 public:
classof(const IntrinsicInst * I)1009 static bool classof(const IntrinsicInst *I) {
1010 switch (I->getIntrinsicID()) {
1011 case Intrinsic::memcpy:
1012 case Intrinsic::memcpy_inline:
1013 case Intrinsic::memmove:
1014 case Intrinsic::memcpy_element_unordered_atomic:
1015 case Intrinsic::memmove_element_unordered_atomic:
1016 return true;
1017 default:
1018 return false;
1019 }
1020 }
classof(const Value * V)1021 static bool classof(const Value *V) {
1022 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1023 }
1024 };
1025
1026 /// This class represents any memcpy intrinsic
1027 /// i.e. llvm.element.unordered.atomic.memcpy
1028 /// and llvm.memcpy
1029 class AnyMemCpyInst : public AnyMemTransferInst {
1030 public:
classof(const IntrinsicInst * I)1031 static bool classof(const IntrinsicInst *I) {
1032 switch (I->getIntrinsicID()) {
1033 case Intrinsic::memcpy:
1034 case Intrinsic::memcpy_inline:
1035 case Intrinsic::memcpy_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 class represents any memmove intrinsic
1047 /// i.e. llvm.element.unordered.atomic.memmove
1048 /// and llvm.memmove
1049 class AnyMemMoveInst : public AnyMemTransferInst {
1050 public:
classof(const IntrinsicInst * I)1051 static bool classof(const IntrinsicInst *I) {
1052 switch (I->getIntrinsicID()) {
1053 case Intrinsic::memmove:
1054 case Intrinsic::memmove_element_unordered_atomic:
1055 return true;
1056 default:
1057 return false;
1058 }
1059 }
classof(const Value * V)1060 static bool classof(const Value *V) {
1061 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1062 }
1063 };
1064
1065 /// This represents the llvm.va_start intrinsic.
1066 class VAStartInst : public IntrinsicInst {
1067 public:
classof(const IntrinsicInst * I)1068 static bool classof(const IntrinsicInst *I) {
1069 return I->getIntrinsicID() == Intrinsic::vastart;
1070 }
classof(const Value * V)1071 static bool classof(const Value *V) {
1072 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1073 }
1074
getArgList()1075 Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
1076 };
1077
1078 /// This represents the llvm.va_end intrinsic.
1079 class VAEndInst : public IntrinsicInst {
1080 public:
classof(const IntrinsicInst * I)1081 static bool classof(const IntrinsicInst *I) {
1082 return I->getIntrinsicID() == Intrinsic::vaend;
1083 }
classof(const Value * V)1084 static bool classof(const Value *V) {
1085 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1086 }
1087
getArgList()1088 Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
1089 };
1090
1091 /// This represents the llvm.va_copy intrinsic.
1092 class VACopyInst : public IntrinsicInst {
1093 public:
classof(const IntrinsicInst * I)1094 static bool classof(const IntrinsicInst *I) {
1095 return I->getIntrinsicID() == Intrinsic::vacopy;
1096 }
classof(const Value * V)1097 static bool classof(const Value *V) {
1098 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1099 }
1100
getDest()1101 Value *getDest() const { return const_cast<Value *>(getArgOperand(0)); }
getSrc()1102 Value *getSrc() const { return const_cast<Value *>(getArgOperand(1)); }
1103 };
1104
1105 /// This represents the llvm.instrprof_increment intrinsic.
1106 class InstrProfIncrementInst : public IntrinsicInst {
1107 public:
classof(const IntrinsicInst * I)1108 static bool classof(const IntrinsicInst *I) {
1109 return I->getIntrinsicID() == Intrinsic::instrprof_increment;
1110 }
classof(const Value * V)1111 static bool classof(const Value *V) {
1112 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1113 }
1114
getName()1115 GlobalVariable *getName() const {
1116 return cast<GlobalVariable>(
1117 const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
1118 }
1119
getHash()1120 ConstantInt *getHash() const {
1121 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
1122 }
1123
getNumCounters()1124 ConstantInt *getNumCounters() const {
1125 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
1126 }
1127
getIndex()1128 ConstantInt *getIndex() const {
1129 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
1130 }
1131
1132 Value *getStep() const;
1133 };
1134
1135 class InstrProfIncrementInstStep : public InstrProfIncrementInst {
1136 public:
classof(const IntrinsicInst * I)1137 static bool classof(const IntrinsicInst *I) {
1138 return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
1139 }
classof(const Value * V)1140 static bool classof(const Value *V) {
1141 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1142 }
1143 };
1144
1145 /// This represents the llvm.instrprof_value_profile intrinsic.
1146 class InstrProfValueProfileInst : public IntrinsicInst {
1147 public:
classof(const IntrinsicInst * I)1148 static bool classof(const IntrinsicInst *I) {
1149 return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
1150 }
classof(const Value * V)1151 static bool classof(const Value *V) {
1152 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1153 }
1154
getName()1155 GlobalVariable *getName() const {
1156 return cast<GlobalVariable>(
1157 const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
1158 }
1159
getHash()1160 ConstantInt *getHash() const {
1161 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
1162 }
1163
getTargetValue()1164 Value *getTargetValue() const {
1165 return cast<Value>(const_cast<Value *>(getArgOperand(2)));
1166 }
1167
getValueKind()1168 ConstantInt *getValueKind() const {
1169 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
1170 }
1171
1172 // Returns the value site index.
getIndex()1173 ConstantInt *getIndex() const {
1174 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
1175 }
1176 };
1177
1178 class PseudoProbeInst : public IntrinsicInst {
1179 public:
classof(const IntrinsicInst * I)1180 static bool classof(const IntrinsicInst *I) {
1181 return I->getIntrinsicID() == Intrinsic::pseudoprobe;
1182 }
1183
classof(const Value * V)1184 static bool classof(const Value *V) {
1185 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1186 }
1187
getFuncGuid()1188 ConstantInt *getFuncGuid() const {
1189 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(0)));
1190 }
1191
getIndex()1192 ConstantInt *getIndex() const {
1193 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
1194 }
1195
getAttributes()1196 ConstantInt *getAttributes() const {
1197 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
1198 }
1199
getFactor()1200 ConstantInt *getFactor() const {
1201 return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
1202 }
1203 };
1204
1205 class NoAliasScopeDeclInst : public IntrinsicInst {
1206 public:
classof(const IntrinsicInst * I)1207 static bool classof(const IntrinsicInst *I) {
1208 return I->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl;
1209 }
1210
classof(const Value * V)1211 static bool classof(const Value *V) {
1212 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1213 }
1214
getScopeList()1215 MDNode *getScopeList() const {
1216 auto *MV =
1217 cast<MetadataAsValue>(getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
1218 return cast<MDNode>(MV->getMetadata());
1219 }
1220
setScopeList(MDNode * ScopeList)1221 void setScopeList(MDNode *ScopeList) {
1222 setOperand(Intrinsic::NoAliasScopeDeclScopeArg,
1223 MetadataAsValue::get(getContext(), ScopeList));
1224 }
1225 };
1226
1227 // Defined in Statepoint.h -- NOT a subclass of IntrinsicInst
1228 class GCStatepointInst;
1229
1230 /// Common base class for representing values projected from a statepoint.
1231 /// Currently, the only projections available are gc.result and gc.relocate.
1232 class GCProjectionInst : public IntrinsicInst {
1233 public:
classof(const IntrinsicInst * I)1234 static bool classof(const IntrinsicInst *I) {
1235 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
1236 I->getIntrinsicID() == Intrinsic::experimental_gc_result;
1237 }
1238
classof(const Value * V)1239 static bool classof(const Value *V) {
1240 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1241 }
1242
1243 /// Return true if this relocate is tied to the invoke statepoint.
1244 /// This includes relocates which are on the unwinding path.
isTiedToInvoke()1245 bool isTiedToInvoke() const {
1246 const Value *Token = getArgOperand(0);
1247
1248 return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
1249 }
1250
1251 /// The statepoint with which this gc.relocate is associated.
1252 const GCStatepointInst *getStatepoint() const;
1253 };
1254
1255 /// Represents calls to the gc.relocate intrinsic.
1256 class GCRelocateInst : public GCProjectionInst {
1257 public:
classof(const IntrinsicInst * I)1258 static bool classof(const IntrinsicInst *I) {
1259 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
1260 }
1261
classof(const Value * V)1262 static bool classof(const Value *V) {
1263 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1264 }
1265
1266 /// The index into the associate statepoint's argument list
1267 /// which contains the base pointer of the pointer whose
1268 /// relocation this gc.relocate describes.
getBasePtrIndex()1269 unsigned getBasePtrIndex() const {
1270 return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
1271 }
1272
1273 /// The index into the associate statepoint's argument list which
1274 /// contains the pointer whose relocation this gc.relocate describes.
getDerivedPtrIndex()1275 unsigned getDerivedPtrIndex() const {
1276 return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
1277 }
1278
1279 Value *getBasePtr() const;
1280 Value *getDerivedPtr() const;
1281 };
1282
1283 /// Represents calls to the gc.result intrinsic.
1284 class GCResultInst : public GCProjectionInst {
1285 public:
classof(const IntrinsicInst * I)1286 static bool classof(const IntrinsicInst *I) {
1287 return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
1288 }
1289
classof(const Value * V)1290 static bool classof(const Value *V) {
1291 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1292 }
1293 };
1294
1295
1296 /// This represents the llvm.assume intrinsic.
1297 class AssumeInst : public IntrinsicInst {
1298 public:
classof(const IntrinsicInst * I)1299 static bool classof(const IntrinsicInst *I) {
1300 return I->getIntrinsicID() == Intrinsic::assume;
1301 }
classof(const Value * V)1302 static bool classof(const Value *V) {
1303 return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
1304 }
1305 };
1306
1307 } // end namespace llvm
1308
1309 #endif // LLVM_IR_INTRINSICINST_H
1310