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