1//===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines classes that make it really easy to deal with intrinsic
10// functions with the isa/dyncast family of functions. In particular, this
11// allows you to do things like:
12//
13// if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
14// ... MCI->getDest() ... MCI->getSource() ...
15//
16// All intrinsic function calls are instances of the call instruction, so these
17// are all subclasses of the CallInst class. Note that none of these classes
18// has state or virtual methods, which is an important part of this gross/neat
19// hack working.
20//
21//===----------------------------------------------------------------------===//
22
23#ifndef LLVM_IR_INTRINSICINST_H
24#define LLVM_IR_INTRINSICINST_H
25
26#include "llvm/IR/Constants.h"
27#include "llvm/IR/DebugInfoMetadata.h"
28#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/FPEnv.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/GlobalVariable.h"
32#include "llvm/IR/Instructions.h"
33#include "llvm/IR/Intrinsics.h"
34#include "llvm/IR/Value.h"
35#include "llvm/Support/Casting.h"
36#include <cassert>
37#include <cstdint>
38#include <optional>
39
40namespace llvm {
41
42class Metadata;
43
44/// A wrapper class for inspecting calls to intrinsic functions.
45/// This allows the standard isa/dyncast/cast functionality to work with calls
46/// to intrinsic functions.
47class IntrinsicInst : public CallInst {
48public:
49 IntrinsicInst() = delete;
50 IntrinsicInst(const IntrinsicInst &) = delete;
51 IntrinsicInst &operator=(const IntrinsicInst &) = delete;
52
53 /// Return the intrinsic ID of this intrinsic.
54 Intrinsic::ID getIntrinsicID() const {
55 return getCalledFunction()->getIntrinsicID();
56 }
57
58 bool isAssociative() const {
59 switch (getIntrinsicID()) {
60 case Intrinsic::smax:
61 case Intrinsic::smin:
62 case Intrinsic::umax:
63 case Intrinsic::umin:
64 return true;
65 default:
66 return false;
67 }
68 }
69
70 /// Return true if swapping the first two arguments to the intrinsic produces
71 /// the same result.
72 bool isCommutative() const {
73 switch (getIntrinsicID()) {
74 case Intrinsic::maxnum:
75 case Intrinsic::minnum:
76 case Intrinsic::maximum:
77 case Intrinsic::minimum:
78 case Intrinsic::smax:
79 case Intrinsic::smin:
80 case Intrinsic::umax:
81 case Intrinsic::umin:
82 case Intrinsic::sadd_sat:
83 case Intrinsic::uadd_sat:
84 case Intrinsic::sadd_with_overflow:
85 case Intrinsic::uadd_with_overflow:
86 case Intrinsic::smul_with_overflow:
87 case Intrinsic::umul_with_overflow:
88 case Intrinsic::smul_fix:
89 case Intrinsic::umul_fix:
90 case Intrinsic::smul_fix_sat:
91 case Intrinsic::umul_fix_sat:
92 case Intrinsic::fma:
93 case Intrinsic::fmuladd:
94 return true;
95 default:
96 return false;
97 }
98 }
99
100 /// Checks if the intrinsic is an annotation.
101 bool isAssumeLikeIntrinsic() const {
102 switch (getIntrinsicID()) {
103 default: break;
104 case Intrinsic::assume:
105 case Intrinsic::sideeffect:
106 case Intrinsic::pseudoprobe:
107 case Intrinsic::dbg_assign:
108 case Intrinsic::dbg_declare:
109 case Intrinsic::dbg_value:
110 case Intrinsic::dbg_label:
111 case Intrinsic::invariant_start:
112 case Intrinsic::invariant_end:
113 case Intrinsic::lifetime_start:
114 case Intrinsic::lifetime_end:
115 case Intrinsic::experimental_noalias_scope_decl:
116 case Intrinsic::objectsize:
117 case Intrinsic::ptr_annotation:
118 case Intrinsic::var_annotation:
119 return true;
120 }
121 return false;
122 }
123
124 /// Check if the intrinsic might lower into a regular function call in the
125 /// course of IR transformations
126 static bool mayLowerToFunctionCall(Intrinsic::ID IID);
127
128 /// Methods for support type inquiry through isa, cast, and dyn_cast:
129 static bool classof(const CallInst *I) {
130 if (const Function *CF = I->getCalledFunction())
131 return CF->isIntrinsic();
132 return false;
133 }
134 static bool classof(const Value *V) {
135 return isa<CallInst>(Val: V) && classof(I: cast<CallInst>(Val: V));
136 }
137};
138
139/// Check if \p ID corresponds to a lifetime intrinsic.
140static inline bool isLifetimeIntrinsic(Intrinsic::ID ID) {
141 switch (ID) {
142 case Intrinsic::lifetime_start:
143 case Intrinsic::lifetime_end:
144 return true;
145 default:
146 return false;
147 }
148}
149
150/// This is the common base class for lifetime intrinsics.
151class LifetimeIntrinsic : public IntrinsicInst {
152public:
153 /// \name Casting methods
154 /// @{
155 static bool classof(const IntrinsicInst *I) {
156 return isLifetimeIntrinsic(ID: I->getIntrinsicID());
157 }
158 static bool classof(const Value *V) {
159 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
160 }
161 /// @}
162};
163
164/// Check if \p ID corresponds to a debug info intrinsic.
165static inline bool isDbgInfoIntrinsic(Intrinsic::ID ID) {
166 switch (ID) {
167 case Intrinsic::dbg_declare:
168 case Intrinsic::dbg_value:
169 case Intrinsic::dbg_label:
170 case Intrinsic::dbg_assign:
171 return true;
172 default:
173 return false;
174 }
175}
176
177/// This is the common base class for debug info intrinsics.
178class DbgInfoIntrinsic : public IntrinsicInst {
179public:
180 /// \name Casting methods
181 /// @{
182 static bool classof(const IntrinsicInst *I) {
183 return isDbgInfoIntrinsic(ID: I->getIntrinsicID());
184 }
185 static bool classof(const Value *V) {
186 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
187 }
188 /// @}
189};
190
191// Iterator for ValueAsMetadata that internally uses direct pointer iteration
192// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
193// ValueAsMetadata .
194class location_op_iterator
195 : public iterator_facade_base<location_op_iterator,
196 std::bidirectional_iterator_tag, Value *> {
197 PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I;
198
199public:
200 location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {}
201 location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {}
202
203 location_op_iterator(const location_op_iterator &R) : I(R.I) {}
204 location_op_iterator &operator=(const location_op_iterator &R) {
205 I = R.I;
206 return *this;
207 }
208 bool operator==(const location_op_iterator &RHS) const { return I == RHS.I; }
209 const Value *operator*() const {
210 ValueAsMetadata *VAM = isa<ValueAsMetadata *>(Val: I)
211 ? cast<ValueAsMetadata *>(Val: I)
212 : *cast<ValueAsMetadata **>(Val: I);
213 return VAM->getValue();
214 };
215 Value *operator*() {
216 ValueAsMetadata *VAM = isa<ValueAsMetadata *>(Val: I)
217 ? cast<ValueAsMetadata *>(Val&: I)
218 : *cast<ValueAsMetadata **>(Val&: I);
219 return VAM->getValue();
220 }
221 location_op_iterator &operator++() {
222 if (isa<ValueAsMetadata *>(Val: I))
223 I = cast<ValueAsMetadata *>(Val&: I) + 1;
224 else
225 I = cast<ValueAsMetadata **>(Val&: I) + 1;
226 return *this;
227 }
228 location_op_iterator &operator--() {
229 if (isa<ValueAsMetadata *>(Val: I))
230 I = cast<ValueAsMetadata *>(Val&: I) - 1;
231 else
232 I = cast<ValueAsMetadata **>(Val&: I) - 1;
233 return *this;
234 }
235};
236
237/// Lightweight class that wraps the location operand metadata of a debug
238/// intrinsic. The raw location may be a ValueAsMetadata, an empty MDTuple,
239/// or a DIArgList.
240class RawLocationWrapper {
241 Metadata *RawLocation = nullptr;
242
243public:
244 RawLocationWrapper() = default;
245 explicit RawLocationWrapper(Metadata *RawLocation)
246 : RawLocation(RawLocation) {
247 // Allow ValueAsMetadata, empty MDTuple, DIArgList.
248 assert(RawLocation && "unexpected null RawLocation");
249 assert(isa<ValueAsMetadata>(RawLocation) || isa<DIArgList>(RawLocation) ||
250 (isa<MDNode>(RawLocation) &&
251 !cast<MDNode>(RawLocation)->getNumOperands()));
252 }
253 Metadata *getRawLocation() const { return RawLocation; }
254 /// Get the locations corresponding to the variable referenced by the debug
255 /// info intrinsic. Depending on the intrinsic, this could be the
256 /// variable's value or its address.
257 iterator_range<location_op_iterator> location_ops() const;
258 Value *getVariableLocationOp(unsigned OpIdx) const;
259 unsigned getNumVariableLocationOps() const {
260 if (hasArgList())
261 return cast<DIArgList>(Val: getRawLocation())->getArgs().size();
262 return 1;
263 }
264 bool hasArgList() const { return isa<DIArgList>(Val: getRawLocation()); }
265 bool isKillLocation(const DIExpression *Expression) const {
266 // Check for "kill" sentinel values.
267 // Non-variadic: empty metadata.
268 if (!hasArgList() && isa<MDNode>(Val: getRawLocation()))
269 return true;
270 // Variadic: empty DIArgList with empty expression.
271 if (getNumVariableLocationOps() == 0 && !Expression->isComplex())
272 return true;
273 // Variadic and non-variadic: Interpret expressions using undef or poison
274 // values as kills.
275 return any_of(Range: location_ops(), P: [](Value *V) { return isa<UndefValue>(Val: V); });
276 }
277
278 friend bool operator==(const RawLocationWrapper &A,
279 const RawLocationWrapper &B) {
280 return A.RawLocation == B.RawLocation;
281 }
282 friend bool operator!=(const RawLocationWrapper &A,
283 const RawLocationWrapper &B) {
284 return !(A == B);
285 }
286 friend bool operator>(const RawLocationWrapper &A,
287 const RawLocationWrapper &B) {
288 return A.RawLocation > B.RawLocation;
289 }
290 friend bool operator>=(const RawLocationWrapper &A,
291 const RawLocationWrapper &B) {
292 return A.RawLocation >= B.RawLocation;
293 }
294 friend bool operator<(const RawLocationWrapper &A,
295 const RawLocationWrapper &B) {
296 return A.RawLocation < B.RawLocation;
297 }
298 friend bool operator<=(const RawLocationWrapper &A,
299 const RawLocationWrapper &B) {
300 return A.RawLocation <= B.RawLocation;
301 }
302};
303
304/// This is the common base class for debug info intrinsics for variables.
305class DbgVariableIntrinsic : public DbgInfoIntrinsic {
306public:
307 /// Get the locations corresponding to the variable referenced by the debug
308 /// info intrinsic. Depending on the intrinsic, this could be the
309 /// variable's value or its address.
310 iterator_range<location_op_iterator> location_ops() const;
311
312 Value *getVariableLocationOp(unsigned OpIdx) const;
313
314 void replaceVariableLocationOp(Value *OldValue, Value *NewValue);
315 void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue);
316 /// Adding a new location operand will always result in this intrinsic using
317 /// an ArgList, and must always be accompanied by a new expression that uses
318 /// the new operand.
319 void addVariableLocationOps(ArrayRef<Value *> NewValues,
320 DIExpression *NewExpr);
321
322 void setVariable(DILocalVariable *NewVar) {
323 setArgOperand(i: 1, v: MetadataAsValue::get(Context&: NewVar->getContext(), MD: NewVar));
324 }
325
326 void setExpression(DIExpression *NewExpr) {
327 setArgOperand(i: 2, v: MetadataAsValue::get(Context&: NewExpr->getContext(), MD: NewExpr));
328 }
329
330 unsigned getNumVariableLocationOps() const {
331 return getWrappedLocation().getNumVariableLocationOps();
332 }
333
334 bool hasArgList() const { return getWrappedLocation().hasArgList(); }
335
336 /// Does this describe the address of a local variable. True for dbg.declare,
337 /// but not dbg.value, which describes its value, or dbg.assign, which
338 /// describes a combination of the variable's value and address.
339 bool isAddressOfVariable() const {
340 return getIntrinsicID() == Intrinsic::dbg_declare;
341 }
342
343 void setKillLocation() {
344 // TODO: When/if we remove duplicate values from DIArgLists, we don't need
345 // this set anymore.
346 SmallPtrSet<Value *, 4> RemovedValues;
347 for (Value *OldValue : location_ops()) {
348 if (!RemovedValues.insert(Ptr: OldValue).second)
349 continue;
350 Value *Poison = PoisonValue::get(T: OldValue->getType());
351 replaceVariableLocationOp(OldValue, NewValue: Poison);
352 }
353 }
354
355 bool isKillLocation() const {
356 return getWrappedLocation().isKillLocation(Expression: getExpression());
357 }
358
359 DILocalVariable *getVariable() const {
360 return cast<DILocalVariable>(Val: getRawVariable());
361 }
362
363 DIExpression *getExpression() const {
364 return cast<DIExpression>(Val: getRawExpression());
365 }
366
367 Metadata *getRawLocation() const {
368 return cast<MetadataAsValue>(Val: getArgOperand(i: 0))->getMetadata();
369 }
370
371 RawLocationWrapper getWrappedLocation() const {
372 return RawLocationWrapper(getRawLocation());
373 }
374
375 Metadata *getRawVariable() const {
376 return cast<MetadataAsValue>(Val: getArgOperand(i: 1))->getMetadata();
377 }
378
379 Metadata *getRawExpression() const {
380 return cast<MetadataAsValue>(Val: getArgOperand(i: 2))->getMetadata();
381 }
382
383 /// Use of this should generally be avoided; instead,
384 /// replaceVariableLocationOp and addVariableLocationOps should be used where
385 /// possible to avoid creating invalid state.
386 void setRawLocation(Metadata *Location) {
387 return setArgOperand(i: 0, v: MetadataAsValue::get(Context&: getContext(), MD: Location));
388 }
389
390 /// Get the size (in bits) of the variable, or fragment of the variable that
391 /// is described.
392 std::optional<uint64_t> getFragmentSizeInBits() const;
393
394 /// Get the FragmentInfo for the variable.
395 std::optional<DIExpression::FragmentInfo> getFragment() const {
396 return getExpression()->getFragmentInfo();
397 }
398
399 /// Get the FragmentInfo for the variable if it exists, otherwise return a
400 /// FragmentInfo that covers the entire variable if the variable size is
401 /// known, otherwise return a zero-sized fragment.
402 DIExpression::FragmentInfo getFragmentOrEntireVariable() const {
403 DIExpression::FragmentInfo VariableSlice(0, 0);
404 // Get the fragment or variable size, or zero.
405 if (auto Sz = getFragmentSizeInBits())
406 VariableSlice.SizeInBits = *Sz;
407 if (auto Frag = getExpression()->getFragmentInfo())
408 VariableSlice.OffsetInBits = Frag->OffsetInBits;
409 return VariableSlice;
410 }
411
412 /// \name Casting methods
413 /// @{
414 static bool classof(const IntrinsicInst *I) {
415 switch (I->getIntrinsicID()) {
416 case Intrinsic::dbg_declare:
417 case Intrinsic::dbg_value:
418 case Intrinsic::dbg_assign:
419 return true;
420 default:
421 return false;
422 }
423 }
424 static bool classof(const Value *V) {
425 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
426 }
427 /// @}
428protected:
429 void setArgOperand(unsigned i, Value *v) {
430 DbgInfoIntrinsic::setArgOperand(i, v);
431 }
432 void setOperand(unsigned i, Value *v) { DbgInfoIntrinsic::setOperand(i_nocapture: i, Val_nocapture: v); }
433};
434
435/// This represents the llvm.dbg.declare instruction.
436class DbgDeclareInst : public DbgVariableIntrinsic {
437public:
438 Value *getAddress() const {
439 assert(getNumVariableLocationOps() == 1 &&
440 "dbg.declare must have exactly 1 location operand.");
441 return getVariableLocationOp(OpIdx: 0);
442 }
443
444 /// \name Casting methods
445 /// @{
446 static bool classof(const IntrinsicInst *I) {
447 return I->getIntrinsicID() == Intrinsic::dbg_declare;
448 }
449 static bool classof(const Value *V) {
450 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
451 }
452 /// @}
453};
454
455/// This represents the llvm.dbg.value instruction.
456class DbgValueInst : public DbgVariableIntrinsic {
457public:
458 // The default argument should only be used in ISel, and the default option
459 // should be removed once ISel support for multiple location ops is complete.
460 Value *getValue(unsigned OpIdx = 0) const {
461 return getVariableLocationOp(OpIdx);
462 }
463 iterator_range<location_op_iterator> getValues() const {
464 return location_ops();
465 }
466
467 /// \name Casting methods
468 /// @{
469 static bool classof(const IntrinsicInst *I) {
470 return I->getIntrinsicID() == Intrinsic::dbg_value ||
471 I->getIntrinsicID() == Intrinsic::dbg_assign;
472 }
473 static bool classof(const Value *V) {
474 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
475 }
476 /// @}
477};
478
479/// This represents the llvm.dbg.assign instruction.
480class DbgAssignIntrinsic : public DbgValueInst {
481 enum Operands {
482 OpValue,
483 OpVar,
484 OpExpr,
485 OpAssignID,
486 OpAddress,
487 OpAddressExpr,
488 };
489
490public:
491 Value *getAddress() const;
492 Metadata *getRawAddress() const {
493 return cast<MetadataAsValue>(Val: getArgOperand(i: OpAddress))->getMetadata();
494 }
495 Metadata *getRawAssignID() const {
496 return cast<MetadataAsValue>(Val: getArgOperand(i: OpAssignID))->getMetadata();
497 }
498 DIAssignID *getAssignID() const { return cast<DIAssignID>(Val: getRawAssignID()); }
499 Metadata *getRawAddressExpression() const {
500 return cast<MetadataAsValue>(Val: getArgOperand(i: OpAddressExpr))->getMetadata();
501 }
502 DIExpression *getAddressExpression() const {
503 return cast<DIExpression>(Val: getRawAddressExpression());
504 }
505 void setAddressExpression(DIExpression *NewExpr) {
506 setArgOperand(i: OpAddressExpr,
507 v: MetadataAsValue::get(Context&: NewExpr->getContext(), MD: NewExpr));
508 }
509 void setAssignId(DIAssignID *New);
510 void setAddress(Value *V);
511 /// Kill the address component.
512 void setKillAddress();
513 /// Check whether this kills the address component. This doesn't take into
514 /// account the position of the intrinsic, therefore a returned value of false
515 /// does not guarentee the address is a valid location for the variable at the
516 /// intrinsic's position in IR.
517 bool isKillAddress() const;
518 void setValue(Value *V);
519 /// \name Casting methods
520 /// @{
521 static bool classof(const IntrinsicInst *I) {
522 return I->getIntrinsicID() == Intrinsic::dbg_assign;
523 }
524 static bool classof(const Value *V) {
525 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
526 }
527 /// @}
528};
529
530/// This represents the llvm.dbg.label instruction.
531class DbgLabelInst : public DbgInfoIntrinsic {
532public:
533 DILabel *getLabel() const { return cast<DILabel>(Val: getRawLabel()); }
534 void setLabel(DILabel *NewLabel) {
535 setArgOperand(i: 0, v: MetadataAsValue::get(Context&: getContext(), MD: NewLabel));
536 }
537
538 Metadata *getRawLabel() const {
539 return cast<MetadataAsValue>(Val: getArgOperand(i: 0))->getMetadata();
540 }
541
542 /// Methods for support type inquiry through isa, cast, and dyn_cast:
543 /// @{
544 static bool classof(const IntrinsicInst *I) {
545 return I->getIntrinsicID() == Intrinsic::dbg_label;
546 }
547 static bool classof(const Value *V) {
548 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
549 }
550 /// @}
551};
552
553/// This is the common base class for vector predication intrinsics.
554class VPIntrinsic : public IntrinsicInst {
555public:
556 /// \brief Declares a llvm.vp.* intrinsic in \p M that matches the parameters
557 /// \p Params. Additionally, the load and gather intrinsics require
558 /// \p ReturnType to be specified.
559 static Function *getDeclarationForParams(Module *M, Intrinsic::ID,
560 Type *ReturnType,
561 ArrayRef<Value *> Params);
562
563 static std::optional<unsigned> getMaskParamPos(Intrinsic::ID IntrinsicID);
564 static std::optional<unsigned> getVectorLengthParamPos(
565 Intrinsic::ID IntrinsicID);
566
567 /// The llvm.vp.* intrinsics for this instruction Opcode
568 static Intrinsic::ID getForOpcode(unsigned OC);
569
570 // Whether \p ID is a VP intrinsic ID.
571 static bool isVPIntrinsic(Intrinsic::ID);
572
573 /// \return The mask parameter or nullptr.
574 Value *getMaskParam() const;
575 void setMaskParam(Value *);
576
577 /// \return The vector length parameter or nullptr.
578 Value *getVectorLengthParam() const;
579 void setVectorLengthParam(Value *);
580
581 /// \return Whether the vector length param can be ignored.
582 bool canIgnoreVectorLengthParam() const;
583
584 /// \return The static element count (vector number of elements) the vector
585 /// length parameter applies to.
586 ElementCount getStaticVectorLength() const;
587
588 /// \return The alignment of the pointer used by this load/store/gather or
589 /// scatter.
590 MaybeAlign getPointerAlignment() const;
591 // MaybeAlign setPointerAlignment(Align NewAlign); // TODO
592
593 /// \return The pointer operand of this load,store, gather or scatter.
594 Value *getMemoryPointerParam() const;
595 static std::optional<unsigned> getMemoryPointerParamPos(Intrinsic::ID);
596
597 /// \return The data (payload) operand of this store or scatter.
598 Value *getMemoryDataParam() const;
599 static std::optional<unsigned> getMemoryDataParamPos(Intrinsic::ID);
600
601 // Methods for support type inquiry through isa, cast, and dyn_cast:
602 static bool classof(const IntrinsicInst *I) {
603 return isVPIntrinsic(I->getIntrinsicID());
604 }
605 static bool classof(const Value *V) {
606 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
607 }
608
609 // Equivalent non-predicated opcode
610 std::optional<unsigned> getFunctionalOpcode() const {
611 return getFunctionalOpcodeForVP(ID: getIntrinsicID());
612 }
613
614 // Equivalent non-predicated intrinsic ID
615 std::optional<unsigned> getFunctionalIntrinsicID() const {
616 return getFunctionalIntrinsicIDForVP(ID: getIntrinsicID());
617 }
618
619 // Equivalent non-predicated constrained ID
620 std::optional<unsigned> getConstrainedIntrinsicID() const {
621 return getConstrainedIntrinsicIDForVP(ID: getIntrinsicID());
622 }
623
624 // Equivalent non-predicated opcode
625 static std::optional<unsigned> getFunctionalOpcodeForVP(Intrinsic::ID ID);
626
627 // Equivalent non-predicated intrinsic ID
628 static std::optional<Intrinsic::ID>
629 getFunctionalIntrinsicIDForVP(Intrinsic::ID ID);
630
631 // Equivalent non-predicated constrained ID
632 static std::optional<Intrinsic::ID>
633 getConstrainedIntrinsicIDForVP(Intrinsic::ID ID);
634};
635
636/// This represents vector predication reduction intrinsics.
637class VPReductionIntrinsic : public VPIntrinsic {
638public:
639 static bool isVPReduction(Intrinsic::ID ID);
640
641 unsigned getStartParamPos() const;
642 unsigned getVectorParamPos() const;
643
644 static std::optional<unsigned> getStartParamPos(Intrinsic::ID ID);
645 static std::optional<unsigned> getVectorParamPos(Intrinsic::ID ID);
646
647 /// Methods for support type inquiry through isa, cast, and dyn_cast:
648 /// @{
649 static bool classof(const IntrinsicInst *I) {
650 return VPReductionIntrinsic::isVPReduction(ID: I->getIntrinsicID());
651 }
652 static bool classof(const Value *V) {
653 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
654 }
655 /// @}
656};
657
658class VPCastIntrinsic : public VPIntrinsic {
659public:
660 static bool isVPCast(Intrinsic::ID ID);
661
662 /// Methods for support type inquiry through isa, cast, and dyn_cast:
663 /// @{
664 static bool classof(const IntrinsicInst *I) {
665 return VPCastIntrinsic::isVPCast(ID: I->getIntrinsicID());
666 }
667 static bool classof(const Value *V) {
668 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
669 }
670 /// @}
671};
672
673class VPCmpIntrinsic : public VPIntrinsic {
674public:
675 static bool isVPCmp(Intrinsic::ID ID);
676
677 CmpInst::Predicate getPredicate() const;
678
679 /// Methods for support type inquiry through isa, cast, and dyn_cast:
680 /// @{
681 static bool classof(const IntrinsicInst *I) {
682 return VPCmpIntrinsic::isVPCmp(ID: I->getIntrinsicID());
683 }
684 static bool classof(const Value *V) {
685 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
686 }
687 /// @}
688};
689
690class VPBinOpIntrinsic : public VPIntrinsic {
691public:
692 static bool isVPBinOp(Intrinsic::ID ID);
693
694 /// Methods for support type inquiry through isa, cast, and dyn_cast:
695 /// @{
696 static bool classof(const IntrinsicInst *I) {
697 return VPBinOpIntrinsic::isVPBinOp(ID: I->getIntrinsicID());
698 }
699 static bool classof(const Value *V) {
700 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
701 }
702 /// @}
703};
704
705
706/// This is the common base class for constrained floating point intrinsics.
707class ConstrainedFPIntrinsic : public IntrinsicInst {
708public:
709 bool isUnaryOp() const;
710 bool isTernaryOp() const;
711 std::optional<RoundingMode> getRoundingMode() const;
712 std::optional<fp::ExceptionBehavior> getExceptionBehavior() const;
713 bool isDefaultFPEnvironment() const;
714
715 // Methods for support type inquiry through isa, cast, and dyn_cast:
716 static bool classof(const IntrinsicInst *I);
717 static bool classof(const Value *V) {
718 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
719 }
720};
721
722/// Constrained floating point compare intrinsics.
723class ConstrainedFPCmpIntrinsic : public ConstrainedFPIntrinsic {
724public:
725 FCmpInst::Predicate getPredicate() const;
726 bool isSignaling() const {
727 return getIntrinsicID() == Intrinsic::experimental_constrained_fcmps;
728 }
729
730 // Methods for support type inquiry through isa, cast, and dyn_cast:
731 static bool classof(const IntrinsicInst *I) {
732 switch (I->getIntrinsicID()) {
733 case Intrinsic::experimental_constrained_fcmp:
734 case Intrinsic::experimental_constrained_fcmps:
735 return true;
736 default:
737 return false;
738 }
739 }
740 static bool classof(const Value *V) {
741 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
742 }
743};
744
745/// This class represents min/max intrinsics.
746class MinMaxIntrinsic : public IntrinsicInst {
747public:
748 static bool classof(const IntrinsicInst *I) {
749 switch (I->getIntrinsicID()) {
750 case Intrinsic::umin:
751 case Intrinsic::umax:
752 case Intrinsic::smin:
753 case Intrinsic::smax:
754 return true;
755 default:
756 return false;
757 }
758 }
759 static bool classof(const Value *V) {
760 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
761 }
762
763 Value *getLHS() const { return const_cast<Value *>(getArgOperand(i: 0)); }
764 Value *getRHS() const { return const_cast<Value *>(getArgOperand(i: 1)); }
765
766 /// Returns the comparison predicate underlying the intrinsic.
767 static ICmpInst::Predicate getPredicate(Intrinsic::ID ID) {
768 switch (ID) {
769 case Intrinsic::umin:
770 return ICmpInst::Predicate::ICMP_ULT;
771 case Intrinsic::umax:
772 return ICmpInst::Predicate::ICMP_UGT;
773 case Intrinsic::smin:
774 return ICmpInst::Predicate::ICMP_SLT;
775 case Intrinsic::smax:
776 return ICmpInst::Predicate::ICMP_SGT;
777 default:
778 llvm_unreachable("Invalid intrinsic");
779 }
780 }
781
782 /// Returns the comparison predicate underlying the intrinsic.
783 ICmpInst::Predicate getPredicate() const {
784 return getPredicate(ID: getIntrinsicID());
785 }
786
787 /// Whether the intrinsic is signed or unsigned.
788 static bool isSigned(Intrinsic::ID ID) {
789 return ICmpInst::isSigned(predicate: getPredicate(ID));
790 };
791
792 /// Whether the intrinsic is signed or unsigned.
793 bool isSigned() const { return isSigned(ID: getIntrinsicID()); };
794
795 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
796 /// so there is a certain threshold value, upon reaching which,
797 /// their value can no longer change. Return said threshold.
798 static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits) {
799 switch (ID) {
800 case Intrinsic::umin:
801 return APInt::getMinValue(numBits);
802 case Intrinsic::umax:
803 return APInt::getMaxValue(numBits);
804 case Intrinsic::smin:
805 return APInt::getSignedMinValue(numBits);
806 case Intrinsic::smax:
807 return APInt::getSignedMaxValue(numBits);
808 default:
809 llvm_unreachable("Invalid intrinsic");
810 }
811 }
812
813 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
814 /// so there is a certain threshold value, upon reaching which,
815 /// their value can no longer change. Return said threshold.
816 APInt getSaturationPoint(unsigned numBits) const {
817 return getSaturationPoint(ID: getIntrinsicID(), numBits);
818 }
819
820 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
821 /// so there is a certain threshold value, upon reaching which,
822 /// their value can no longer change. Return said threshold.
823 static Constant *getSaturationPoint(Intrinsic::ID ID, Type *Ty) {
824 return Constant::getIntegerValue(
825 Ty, V: getSaturationPoint(ID, numBits: Ty->getScalarSizeInBits()));
826 }
827
828 /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
829 /// so there is a certain threshold value, upon reaching which,
830 /// their value can no longer change. Return said threshold.
831 Constant *getSaturationPoint(Type *Ty) const {
832 return getSaturationPoint(ID: getIntrinsicID(), Ty);
833 }
834};
835
836/// This class represents an intrinsic that is based on a binary operation.
837/// This includes op.with.overflow and saturating add/sub intrinsics.
838class BinaryOpIntrinsic : public IntrinsicInst {
839public:
840 static bool classof(const IntrinsicInst *I) {
841 switch (I->getIntrinsicID()) {
842 case Intrinsic::uadd_with_overflow:
843 case Intrinsic::sadd_with_overflow:
844 case Intrinsic::usub_with_overflow:
845 case Intrinsic::ssub_with_overflow:
846 case Intrinsic::umul_with_overflow:
847 case Intrinsic::smul_with_overflow:
848 case Intrinsic::uadd_sat:
849 case Intrinsic::sadd_sat:
850 case Intrinsic::usub_sat:
851 case Intrinsic::ssub_sat:
852 return true;
853 default:
854 return false;
855 }
856 }
857 static bool classof(const Value *V) {
858 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
859 }
860
861 Value *getLHS() const { return const_cast<Value *>(getArgOperand(i: 0)); }
862 Value *getRHS() const { return const_cast<Value *>(getArgOperand(i: 1)); }
863
864 /// Returns the binary operation underlying the intrinsic.
865 Instruction::BinaryOps getBinaryOp() const;
866
867 /// Whether the intrinsic is signed or unsigned.
868 bool isSigned() const;
869
870 /// Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
871 unsigned getNoWrapKind() const;
872};
873
874/// Represents an op.with.overflow intrinsic.
875class WithOverflowInst : public BinaryOpIntrinsic {
876public:
877 static bool classof(const IntrinsicInst *I) {
878 switch (I->getIntrinsicID()) {
879 case Intrinsic::uadd_with_overflow:
880 case Intrinsic::sadd_with_overflow:
881 case Intrinsic::usub_with_overflow:
882 case Intrinsic::ssub_with_overflow:
883 case Intrinsic::umul_with_overflow:
884 case Intrinsic::smul_with_overflow:
885 return true;
886 default:
887 return false;
888 }
889 }
890 static bool classof(const Value *V) {
891 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
892 }
893};
894
895/// Represents a saturating add/sub intrinsic.
896class SaturatingInst : public BinaryOpIntrinsic {
897public:
898 static bool classof(const IntrinsicInst *I) {
899 switch (I->getIntrinsicID()) {
900 case Intrinsic::uadd_sat:
901 case Intrinsic::sadd_sat:
902 case Intrinsic::usub_sat:
903 case Intrinsic::ssub_sat:
904 return true;
905 default:
906 return false;
907 }
908 }
909 static bool classof(const Value *V) {
910 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
911 }
912};
913
914/// Common base class for all memory intrinsics. Simply provides
915/// common methods.
916/// Written as CRTP to avoid a common base class amongst the
917/// three atomicity hierarchies.
918template <typename Derived> class MemIntrinsicBase : public IntrinsicInst {
919private:
920 enum { ARG_DEST = 0, ARG_LENGTH = 2 };
921
922public:
923 Value *getRawDest() const {
924 return const_cast<Value *>(getArgOperand(i: ARG_DEST));
925 }
926 const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); }
927 Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); }
928
929 Value *getLength() const {
930 return const_cast<Value *>(getArgOperand(i: ARG_LENGTH));
931 }
932 const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); }
933 Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); }
934
935 /// This is just like getRawDest, but it strips off any cast
936 /// instructions (including addrspacecast) that feed it, giving the
937 /// original input. The returned value is guaranteed to be a pointer.
938 Value *getDest() const { return getRawDest()->stripPointerCasts(); }
939
940 unsigned getDestAddressSpace() const {
941 return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
942 }
943
944 /// FIXME: Remove this function once transition to Align is over.
945 /// Use getDestAlign() instead.
946 LLVM_DEPRECATED("Use getDestAlign() instead", "getDestAlign")
947 unsigned getDestAlignment() const {
948 if (auto MA = getParamAlign(ArgNo: ARG_DEST))
949 return MA->value();
950 return 0;
951 }
952 MaybeAlign getDestAlign() const { return getParamAlign(ArgNo: ARG_DEST); }
953
954 /// Set the specified arguments of the instruction.
955 void setDest(Value *Ptr) {
956 assert(getRawDest()->getType() == Ptr->getType() &&
957 "setDest called with pointer of wrong type!");
958 setArgOperand(i: ARG_DEST, v: Ptr);
959 }
960
961 void setDestAlignment(MaybeAlign Alignment) {
962 removeParamAttr(ARG_DEST, Attribute::Alignment);
963 if (Alignment)
964 addParamAttr(ARG_DEST,
965 Attribute::getWithAlignment(Context&: getContext(), Alignment: *Alignment));
966 }
967 void setDestAlignment(Align Alignment) {
968 removeParamAttr(ARG_DEST, Attribute::Alignment);
969 addParamAttr(ARG_DEST,
970 Attribute::getWithAlignment(Context&: getContext(), Alignment));
971 }
972
973 void setLength(Value *L) {
974 assert(getLength()->getType() == L->getType() &&
975 "setLength called with value of wrong type!");
976 setArgOperand(i: ARG_LENGTH, v: L);
977 }
978};
979
980/// Common base class for all memory transfer intrinsics. Simply provides
981/// common methods.
982template <class BaseCL> class MemTransferBase : public BaseCL {
983private:
984 enum { ARG_SOURCE = 1 };
985
986public:
987 /// Return the arguments to the instruction.
988 Value *getRawSource() const {
989 return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE));
990 }
991 const Use &getRawSourceUse() const {
992 return BaseCL::getArgOperandUse(ARG_SOURCE);
993 }
994 Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); }
995
996 /// This is just like getRawSource, but it strips off any cast
997 /// instructions that feed it, giving the original input. The returned
998 /// value is guaranteed to be a pointer.
999 Value *getSource() const { return getRawSource()->stripPointerCasts(); }
1000
1001 unsigned getSourceAddressSpace() const {
1002 return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
1003 }
1004
1005 /// FIXME: Remove this function once transition to Align is over.
1006 /// Use getSourceAlign() instead.
1007 LLVM_DEPRECATED("Use getSourceAlign() instead", "getSourceAlign")
1008 unsigned getSourceAlignment() const {
1009 if (auto MA = BaseCL::getParamAlign(ARG_SOURCE))
1010 return MA->value();
1011 return 0;
1012 }
1013
1014 MaybeAlign getSourceAlign() const {
1015 return BaseCL::getParamAlign(ARG_SOURCE);
1016 }
1017
1018 void setSource(Value *Ptr) {
1019 assert(getRawSource()->getType() == Ptr->getType() &&
1020 "setSource called with pointer of wrong type!");
1021 BaseCL::setArgOperand(ARG_SOURCE, Ptr);
1022 }
1023
1024 void setSourceAlignment(MaybeAlign Alignment) {
1025 BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
1026 if (Alignment)
1027 BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
1028 Context&: BaseCL::getContext(), Alignment: *Alignment));
1029 }
1030
1031 void setSourceAlignment(Align Alignment) {
1032 BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
1033 BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
1034 Context&: BaseCL::getContext(), Alignment));
1035 }
1036};
1037
1038/// Common base class for all memset intrinsics. Simply provides
1039/// common methods.
1040template <class BaseCL> class MemSetBase : public BaseCL {
1041private:
1042 enum { ARG_VALUE = 1 };
1043
1044public:
1045 Value *getValue() const {
1046 return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE));
1047 }
1048 const Use &getValueUse() const { return BaseCL::getArgOperandUse(ARG_VALUE); }
1049 Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); }
1050
1051 void setValue(Value *Val) {
1052 assert(getValue()->getType() == Val->getType() &&
1053 "setValue called with value of wrong type!");
1054 BaseCL::setArgOperand(ARG_VALUE, Val);
1055 }
1056};
1057
1058// The common base class for the atomic memset/memmove/memcpy intrinsics
1059// i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
1060class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> {
1061private:
1062 enum { ARG_ELEMENTSIZE = 3 };
1063
1064public:
1065 Value *getRawElementSizeInBytes() const {
1066 return const_cast<Value *>(getArgOperand(i: ARG_ELEMENTSIZE));
1067 }
1068
1069 ConstantInt *getElementSizeInBytesCst() const {
1070 return cast<ConstantInt>(Val: getRawElementSizeInBytes());
1071 }
1072
1073 uint32_t getElementSizeInBytes() const {
1074 return getElementSizeInBytesCst()->getZExtValue();
1075 }
1076
1077 void setElementSizeInBytes(Constant *V) {
1078 assert(V->getType() == Type::getInt8Ty(getContext()) &&
1079 "setElementSizeInBytes called with value of wrong type!");
1080 setArgOperand(i: ARG_ELEMENTSIZE, v: V);
1081 }
1082
1083 static bool classof(const IntrinsicInst *I) {
1084 switch (I->getIntrinsicID()) {
1085 case Intrinsic::memcpy_element_unordered_atomic:
1086 case Intrinsic::memmove_element_unordered_atomic:
1087 case Intrinsic::memset_element_unordered_atomic:
1088 return true;
1089 default:
1090 return false;
1091 }
1092 }
1093 static bool classof(const Value *V) {
1094 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1095 }
1096};
1097
1098/// This class represents atomic memset intrinsic
1099// i.e. llvm.element.unordered.atomic.memset
1100class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> {
1101public:
1102 static bool classof(const IntrinsicInst *I) {
1103 return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic;
1104 }
1105 static bool classof(const Value *V) {
1106 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1107 }
1108};
1109
1110// This class wraps the atomic memcpy/memmove intrinsics
1111// i.e. llvm.element.unordered.atomic.memcpy/memmove
1112class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> {
1113public:
1114 static bool classof(const IntrinsicInst *I) {
1115 switch (I->getIntrinsicID()) {
1116 case Intrinsic::memcpy_element_unordered_atomic:
1117 case Intrinsic::memmove_element_unordered_atomic:
1118 return true;
1119 default:
1120 return false;
1121 }
1122 }
1123 static bool classof(const Value *V) {
1124 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1125 }
1126};
1127
1128/// This class represents the atomic memcpy intrinsic
1129/// i.e. llvm.element.unordered.atomic.memcpy
1130class AtomicMemCpyInst : public AtomicMemTransferInst {
1131public:
1132 static bool classof(const IntrinsicInst *I) {
1133 return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic;
1134 }
1135 static bool classof(const Value *V) {
1136 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1137 }
1138};
1139
1140/// This class represents the atomic memmove intrinsic
1141/// i.e. llvm.element.unordered.atomic.memmove
1142class AtomicMemMoveInst : public AtomicMemTransferInst {
1143public:
1144 static bool classof(const IntrinsicInst *I) {
1145 return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic;
1146 }
1147 static bool classof(const Value *V) {
1148 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1149 }
1150};
1151
1152/// This is the common base class for memset/memcpy/memmove.
1153class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> {
1154private:
1155 enum { ARG_VOLATILE = 3 };
1156
1157public:
1158 ConstantInt *getVolatileCst() const {
1159 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: ARG_VOLATILE)));
1160 }
1161
1162 bool isVolatile() const { return !getVolatileCst()->isZero(); }
1163
1164 void setVolatile(Constant *V) { setArgOperand(i: ARG_VOLATILE, v: V); }
1165
1166 // Methods for support type inquiry through isa, cast, and dyn_cast:
1167 static bool classof(const IntrinsicInst *I) {
1168 switch (I->getIntrinsicID()) {
1169 case Intrinsic::memcpy:
1170 case Intrinsic::memmove:
1171 case Intrinsic::memset:
1172 case Intrinsic::memset_inline:
1173 case Intrinsic::memcpy_inline:
1174 return true;
1175 default:
1176 return false;
1177 }
1178 }
1179 static bool classof(const Value *V) {
1180 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1181 }
1182};
1183
1184/// This class wraps the llvm.memset and llvm.memset.inline intrinsics.
1185class MemSetInst : public MemSetBase<MemIntrinsic> {
1186public:
1187 // Methods for support type inquiry through isa, cast, and dyn_cast:
1188 static bool classof(const IntrinsicInst *I) {
1189 switch (I->getIntrinsicID()) {
1190 case Intrinsic::memset:
1191 case Intrinsic::memset_inline:
1192 return true;
1193 default:
1194 return false;
1195 }
1196 }
1197 static bool classof(const Value *V) {
1198 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1199 }
1200};
1201
1202/// This class wraps the llvm.memset.inline intrinsic.
1203class MemSetInlineInst : public MemSetInst {
1204public:
1205 ConstantInt *getLength() const {
1206 return cast<ConstantInt>(Val: MemSetInst::getLength());
1207 }
1208 // Methods for support type inquiry through isa, cast, and dyn_cast:
1209 static bool classof(const IntrinsicInst *I) {
1210 return I->getIntrinsicID() == Intrinsic::memset_inline;
1211 }
1212 static bool classof(const Value *V) {
1213 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1214 }
1215};
1216
1217/// This class wraps the llvm.memcpy/memmove intrinsics.
1218class MemTransferInst : public MemTransferBase<MemIntrinsic> {
1219public:
1220 // Methods for support type inquiry through isa, cast, and dyn_cast:
1221 static bool classof(const IntrinsicInst *I) {
1222 switch (I->getIntrinsicID()) {
1223 case Intrinsic::memcpy:
1224 case Intrinsic::memmove:
1225 case Intrinsic::memcpy_inline:
1226 return true;
1227 default:
1228 return false;
1229 }
1230 }
1231 static bool classof(const Value *V) {
1232 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1233 }
1234};
1235
1236/// This class wraps the llvm.memcpy intrinsic.
1237class MemCpyInst : public MemTransferInst {
1238public:
1239 // Methods for support type inquiry through isa, cast, and dyn_cast:
1240 static bool classof(const IntrinsicInst *I) {
1241 return I->getIntrinsicID() == Intrinsic::memcpy ||
1242 I->getIntrinsicID() == Intrinsic::memcpy_inline;
1243 }
1244 static bool classof(const Value *V) {
1245 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1246 }
1247};
1248
1249/// This class wraps the llvm.memmove intrinsic.
1250class MemMoveInst : public MemTransferInst {
1251public:
1252 // Methods for support type inquiry through isa, cast, and dyn_cast:
1253 static bool classof(const IntrinsicInst *I) {
1254 return I->getIntrinsicID() == Intrinsic::memmove;
1255 }
1256 static bool classof(const Value *V) {
1257 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1258 }
1259};
1260
1261/// This class wraps the llvm.memcpy.inline intrinsic.
1262class MemCpyInlineInst : public MemCpyInst {
1263public:
1264 ConstantInt *getLength() const {
1265 return cast<ConstantInt>(Val: MemCpyInst::getLength());
1266 }
1267 // Methods for support type inquiry through isa, cast, and dyn_cast:
1268 static bool classof(const IntrinsicInst *I) {
1269 return I->getIntrinsicID() == Intrinsic::memcpy_inline;
1270 }
1271 static bool classof(const Value *V) {
1272 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1273 }
1274};
1275
1276// The common base class for any memset/memmove/memcpy intrinsics;
1277// whether they be atomic or non-atomic.
1278// i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
1279// and llvm.memset/memcpy/memmove
1280class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> {
1281public:
1282 bool isVolatile() const {
1283 // Only the non-atomic intrinsics can be volatile
1284 if (auto *MI = dyn_cast<MemIntrinsic>(Val: this))
1285 return MI->isVolatile();
1286 return false;
1287 }
1288
1289 static bool classof(const IntrinsicInst *I) {
1290 switch (I->getIntrinsicID()) {
1291 case Intrinsic::memcpy:
1292 case Intrinsic::memcpy_inline:
1293 case Intrinsic::memmove:
1294 case Intrinsic::memset:
1295 case Intrinsic::memset_inline:
1296 case Intrinsic::memcpy_element_unordered_atomic:
1297 case Intrinsic::memmove_element_unordered_atomic:
1298 case Intrinsic::memset_element_unordered_atomic:
1299 return true;
1300 default:
1301 return false;
1302 }
1303 }
1304 static bool classof(const Value *V) {
1305 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1306 }
1307};
1308
1309/// This class represents any memset intrinsic
1310// i.e. llvm.element.unordered.atomic.memset
1311// and llvm.memset
1312class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> {
1313public:
1314 static bool classof(const IntrinsicInst *I) {
1315 switch (I->getIntrinsicID()) {
1316 case Intrinsic::memset:
1317 case Intrinsic::memset_inline:
1318 case Intrinsic::memset_element_unordered_atomic:
1319 return true;
1320 default:
1321 return false;
1322 }
1323 }
1324 static bool classof(const Value *V) {
1325 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1326 }
1327};
1328
1329// This class wraps any memcpy/memmove intrinsics
1330// i.e. llvm.element.unordered.atomic.memcpy/memmove
1331// and llvm.memcpy/memmove
1332class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> {
1333public:
1334 static bool classof(const IntrinsicInst *I) {
1335 switch (I->getIntrinsicID()) {
1336 case Intrinsic::memcpy:
1337 case Intrinsic::memcpy_inline:
1338 case Intrinsic::memmove:
1339 case Intrinsic::memcpy_element_unordered_atomic:
1340 case Intrinsic::memmove_element_unordered_atomic:
1341 return true;
1342 default:
1343 return false;
1344 }
1345 }
1346 static bool classof(const Value *V) {
1347 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1348 }
1349};
1350
1351/// This class represents any memcpy intrinsic
1352/// i.e. llvm.element.unordered.atomic.memcpy
1353/// and llvm.memcpy
1354class AnyMemCpyInst : public AnyMemTransferInst {
1355public:
1356 static bool classof(const IntrinsicInst *I) {
1357 switch (I->getIntrinsicID()) {
1358 case Intrinsic::memcpy:
1359 case Intrinsic::memcpy_inline:
1360 case Intrinsic::memcpy_element_unordered_atomic:
1361 return true;
1362 default:
1363 return false;
1364 }
1365 }
1366 static bool classof(const Value *V) {
1367 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1368 }
1369};
1370
1371/// This class represents any memmove intrinsic
1372/// i.e. llvm.element.unordered.atomic.memmove
1373/// and llvm.memmove
1374class AnyMemMoveInst : public AnyMemTransferInst {
1375public:
1376 static bool classof(const IntrinsicInst *I) {
1377 switch (I->getIntrinsicID()) {
1378 case Intrinsic::memmove:
1379 case Intrinsic::memmove_element_unordered_atomic:
1380 return true;
1381 default:
1382 return false;
1383 }
1384 }
1385 static bool classof(const Value *V) {
1386 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1387 }
1388};
1389
1390/// This represents the llvm.va_start intrinsic.
1391class VAStartInst : public IntrinsicInst {
1392public:
1393 static bool classof(const IntrinsicInst *I) {
1394 return I->getIntrinsicID() == Intrinsic::vastart;
1395 }
1396 static bool classof(const Value *V) {
1397 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1398 }
1399
1400 Value *getArgList() const { return const_cast<Value *>(getArgOperand(i: 0)); }
1401};
1402
1403/// This represents the llvm.va_end intrinsic.
1404class VAEndInst : public IntrinsicInst {
1405public:
1406 static bool classof(const IntrinsicInst *I) {
1407 return I->getIntrinsicID() == Intrinsic::vaend;
1408 }
1409 static bool classof(const Value *V) {
1410 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1411 }
1412
1413 Value *getArgList() const { return const_cast<Value *>(getArgOperand(i: 0)); }
1414};
1415
1416/// This represents the llvm.va_copy intrinsic.
1417class VACopyInst : public IntrinsicInst {
1418public:
1419 static bool classof(const IntrinsicInst *I) {
1420 return I->getIntrinsicID() == Intrinsic::vacopy;
1421 }
1422 static bool classof(const Value *V) {
1423 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1424 }
1425
1426 Value *getDest() const { return const_cast<Value *>(getArgOperand(i: 0)); }
1427 Value *getSrc() const { return const_cast<Value *>(getArgOperand(i: 1)); }
1428};
1429
1430/// A base class for all instrprof intrinsics.
1431class InstrProfInstBase : public IntrinsicInst {
1432protected:
1433 static bool isCounterBase(const IntrinsicInst &I) {
1434 switch (I.getIntrinsicID()) {
1435 case Intrinsic::instrprof_cover:
1436 case Intrinsic::instrprof_increment:
1437 case Intrinsic::instrprof_increment_step:
1438 case Intrinsic::instrprof_timestamp:
1439 case Intrinsic::instrprof_value_profile:
1440 return true;
1441 }
1442 return false;
1443 }
1444 static bool isMCDCBitmapBase(const IntrinsicInst &I) {
1445 switch (I.getIntrinsicID()) {
1446 case Intrinsic::instrprof_mcdc_parameters:
1447 case Intrinsic::instrprof_mcdc_tvbitmap_update:
1448 return true;
1449 }
1450 return false;
1451 }
1452
1453public:
1454 static bool classof(const Value *V) {
1455 if (const auto *Instr = dyn_cast<IntrinsicInst>(V))
1456 return isCounterBase(*Instr) || isMCDCBitmapBase(*Instr) ||
1457 Instr->getIntrinsicID() ==
1458 Intrinsic::instrprof_mcdc_condbitmap_update;
1459 return false;
1460 }
1461 // The name of the instrumented function.
1462 GlobalVariable *getName() const {
1463 return cast<GlobalVariable>(
1464 Val: const_cast<Value *>(getArgOperand(i: 0))->stripPointerCasts());
1465 }
1466 // The hash of the CFG for the instrumented function.
1467 ConstantInt *getHash() const {
1468 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 1)));
1469 }
1470};
1471
1472/// A base class for all instrprof counter intrinsics.
1473class InstrProfCntrInstBase : public InstrProfInstBase {
1474public:
1475 static bool classof(const Value *V) {
1476 if (const auto *Instr = dyn_cast<IntrinsicInst>(Val: V))
1477 return InstrProfInstBase::isCounterBase(I: *Instr);
1478 return false;
1479 }
1480
1481 // The number of counters for the instrumented function.
1482 ConstantInt *getNumCounters() const;
1483 // The index of the counter that this instruction acts on.
1484 ConstantInt *getIndex() const;
1485};
1486
1487/// This represents the llvm.instrprof.cover intrinsic.
1488class InstrProfCoverInst : public InstrProfCntrInstBase {
1489public:
1490 static bool classof(const IntrinsicInst *I) {
1491 return I->getIntrinsicID() == Intrinsic::instrprof_cover;
1492 }
1493 static bool classof(const Value *V) {
1494 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1495 }
1496};
1497
1498/// This represents the llvm.instrprof.increment intrinsic.
1499class InstrProfIncrementInst : public InstrProfCntrInstBase {
1500public:
1501 static bool classof(const IntrinsicInst *I) {
1502 return I->getIntrinsicID() == Intrinsic::instrprof_increment ||
1503 I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
1504 }
1505 static bool classof(const Value *V) {
1506 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1507 }
1508 Value *getStep() const;
1509};
1510
1511/// This represents the llvm.instrprof.increment.step intrinsic.
1512class InstrProfIncrementInstStep : public InstrProfIncrementInst {
1513public:
1514 static bool classof(const IntrinsicInst *I) {
1515 return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
1516 }
1517 static bool classof(const Value *V) {
1518 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1519 }
1520};
1521
1522/// This represents the llvm.instrprof.timestamp intrinsic.
1523class InstrProfTimestampInst : public InstrProfCntrInstBase {
1524public:
1525 static bool classof(const IntrinsicInst *I) {
1526 return I->getIntrinsicID() == Intrinsic::instrprof_timestamp;
1527 }
1528 static bool classof(const Value *V) {
1529 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1530 }
1531};
1532
1533/// This represents the llvm.instrprof.value.profile intrinsic.
1534class InstrProfValueProfileInst : public InstrProfCntrInstBase {
1535public:
1536 static bool classof(const IntrinsicInst *I) {
1537 return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
1538 }
1539 static bool classof(const Value *V) {
1540 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1541 }
1542
1543 Value *getTargetValue() const {
1544 return cast<Value>(Val: const_cast<Value *>(getArgOperand(i: 2)));
1545 }
1546
1547 ConstantInt *getValueKind() const {
1548 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 3)));
1549 }
1550
1551 // Returns the value site index.
1552 ConstantInt *getIndex() const {
1553 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 4)));
1554 }
1555};
1556
1557/// A base class for instrprof mcdc intrinsics that require global bitmap bytes.
1558class InstrProfMCDCBitmapInstBase : public InstrProfInstBase {
1559public:
1560 static bool classof(const IntrinsicInst *I) {
1561 return InstrProfInstBase::isMCDCBitmapBase(I: *I);
1562 }
1563 static bool classof(const Value *V) {
1564 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1565 }
1566
1567 /// \return The number of bytes used for the MCDC bitmaps for the instrumented
1568 /// function.
1569 ConstantInt *getNumBitmapBytes() const {
1570 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 2)));
1571 }
1572};
1573
1574/// This represents the llvm.instrprof.mcdc.parameters intrinsic.
1575class InstrProfMCDCBitmapParameters : public InstrProfMCDCBitmapInstBase {
1576public:
1577 static bool classof(const IntrinsicInst *I) {
1578 return I->getIntrinsicID() == Intrinsic::instrprof_mcdc_parameters;
1579 }
1580 static bool classof(const Value *V) {
1581 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1582 }
1583};
1584
1585/// This represents the llvm.instrprof.mcdc.tvbitmap.update intrinsic.
1586class InstrProfMCDCTVBitmapUpdate : public InstrProfMCDCBitmapInstBase {
1587public:
1588 static bool classof(const IntrinsicInst *I) {
1589 return I->getIntrinsicID() == Intrinsic::instrprof_mcdc_tvbitmap_update;
1590 }
1591 static bool classof(const Value *V) {
1592 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1593 }
1594
1595 /// \return The index of the TestVector Bitmap upon which this intrinsic
1596 /// acts.
1597 ConstantInt *getBitmapIndex() const {
1598 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 3)));
1599 }
1600
1601 /// \return The address of the corresponding condition bitmap containing
1602 /// the index of the TestVector to update within the TestVector Bitmap.
1603 Value *getMCDCCondBitmapAddr() const {
1604 return cast<Value>(Val: const_cast<Value *>(getArgOperand(i: 4)));
1605 }
1606};
1607
1608/// This represents the llvm.instrprof.mcdc.condbitmap.update intrinsic.
1609/// It does not pertain to global bitmap updates or parameters and so doesn't
1610/// inherit from InstrProfMCDCBitmapInstBase.
1611class InstrProfMCDCCondBitmapUpdate : public InstrProfInstBase {
1612public:
1613 static bool classof(const IntrinsicInst *I) {
1614 return I->getIntrinsicID() == Intrinsic::instrprof_mcdc_condbitmap_update;
1615 }
1616 static bool classof(const Value *V) {
1617 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1618 }
1619
1620 /// \return The ID of the condition to update.
1621 ConstantInt *getCondID() const {
1622 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 2)));
1623 }
1624
1625 /// \return The address of the corresponding condition bitmap.
1626 Value *getMCDCCondBitmapAddr() const {
1627 return cast<Value>(Val: const_cast<Value *>(getArgOperand(i: 3)));
1628 }
1629
1630 /// \return The boolean value to set in the condition bitmap for the
1631 /// corresponding condition ID. This represents how the condition evaluated.
1632 Value *getCondBool() const {
1633 return cast<Value>(Val: const_cast<Value *>(getArgOperand(i: 4)));
1634 }
1635};
1636
1637class PseudoProbeInst : public IntrinsicInst {
1638public:
1639 static bool classof(const IntrinsicInst *I) {
1640 return I->getIntrinsicID() == Intrinsic::pseudoprobe;
1641 }
1642
1643 static bool classof(const Value *V) {
1644 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1645 }
1646
1647 ConstantInt *getFuncGuid() const {
1648 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 0)));
1649 }
1650
1651 ConstantInt *getIndex() const {
1652 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 1)));
1653 }
1654
1655 ConstantInt *getAttributes() const {
1656 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 2)));
1657 }
1658
1659 ConstantInt *getFactor() const {
1660 return cast<ConstantInt>(Val: const_cast<Value *>(getArgOperand(i: 3)));
1661 }
1662};
1663
1664class NoAliasScopeDeclInst : public IntrinsicInst {
1665public:
1666 static bool classof(const IntrinsicInst *I) {
1667 return I->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl;
1668 }
1669
1670 static bool classof(const Value *V) {
1671 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1672 }
1673
1674 MDNode *getScopeList() const {
1675 auto *MV =
1676 cast<MetadataAsValue>(Val: getOperand(i_nocapture: Intrinsic::NoAliasScopeDeclScopeArg));
1677 return cast<MDNode>(Val: MV->getMetadata());
1678 }
1679
1680 void setScopeList(MDNode *ScopeList) {
1681 setOperand(i_nocapture: Intrinsic::NoAliasScopeDeclScopeArg,
1682 Val_nocapture: MetadataAsValue::get(Context&: getContext(), MD: ScopeList));
1683 }
1684};
1685
1686/// Common base class for representing values projected from a statepoint.
1687/// Currently, the only projections available are gc.result and gc.relocate.
1688class GCProjectionInst : public IntrinsicInst {
1689public:
1690 static bool classof(const IntrinsicInst *I) {
1691 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
1692 I->getIntrinsicID() == Intrinsic::experimental_gc_result;
1693 }
1694
1695 static bool classof(const Value *V) {
1696 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1697 }
1698
1699 /// Return true if this relocate is tied to the invoke statepoint.
1700 /// This includes relocates which are on the unwinding path.
1701 bool isTiedToInvoke() const {
1702 const Value *Token = getArgOperand(i: 0);
1703
1704 return isa<LandingPadInst>(Val: Token) || isa<InvokeInst>(Val: Token);
1705 }
1706
1707 /// The statepoint with which this gc.relocate is associated.
1708 const Value *getStatepoint() const;
1709};
1710
1711/// Represents calls to the gc.relocate intrinsic.
1712class GCRelocateInst : public GCProjectionInst {
1713public:
1714 static bool classof(const IntrinsicInst *I) {
1715 return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
1716 }
1717
1718 static bool classof(const Value *V) {
1719 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1720 }
1721
1722 /// The index into the associate statepoint's argument list
1723 /// which contains the base pointer of the pointer whose
1724 /// relocation this gc.relocate describes.
1725 unsigned getBasePtrIndex() const {
1726 return cast<ConstantInt>(Val: getArgOperand(i: 1))->getZExtValue();
1727 }
1728
1729 /// The index into the associate statepoint's argument list which
1730 /// contains the pointer whose relocation this gc.relocate describes.
1731 unsigned getDerivedPtrIndex() const {
1732 return cast<ConstantInt>(Val: getArgOperand(i: 2))->getZExtValue();
1733 }
1734
1735 Value *getBasePtr() const;
1736 Value *getDerivedPtr() const;
1737};
1738
1739/// Represents calls to the gc.result intrinsic.
1740class GCResultInst : public GCProjectionInst {
1741public:
1742 static bool classof(const IntrinsicInst *I) {
1743 return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
1744 }
1745
1746 static bool classof(const Value *V) {
1747 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1748 }
1749};
1750
1751
1752/// This represents the llvm.assume intrinsic.
1753class AssumeInst : public IntrinsicInst {
1754public:
1755 static bool classof(const IntrinsicInst *I) {
1756 return I->getIntrinsicID() == Intrinsic::assume;
1757 }
1758 static bool classof(const Value *V) {
1759 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1760 }
1761};
1762
1763/// Check if \p ID corresponds to a convergence control intrinsic.
1764static inline bool isConvergenceControlIntrinsic(unsigned IntrinsicID) {
1765 switch (IntrinsicID) {
1766 default:
1767 return false;
1768 case Intrinsic::experimental_convergence_anchor:
1769 case Intrinsic::experimental_convergence_entry:
1770 case Intrinsic::experimental_convergence_loop:
1771 return true;
1772 }
1773}
1774
1775/// Represents calls to the llvm.experimintal.convergence.* intrinsics.
1776class ConvergenceControlInst : public IntrinsicInst {
1777public:
1778 static bool classof(const IntrinsicInst *I) {
1779 return isConvergenceControlIntrinsic(IntrinsicID: I->getIntrinsicID());
1780 }
1781
1782 static bool classof(const Value *V) {
1783 return isa<IntrinsicInst>(Val: V) && classof(I: cast<IntrinsicInst>(Val: V));
1784 }
1785
1786 // Returns the convergence intrinsic referenced by |I|'s convergencectrl
1787 // attribute if any.
1788 static IntrinsicInst *getParentConvergenceToken(Instruction *I) {
1789 auto *CI = dyn_cast<llvm::CallInst>(Val: I);
1790 if (!CI)
1791 return nullptr;
1792
1793 auto Bundle = CI->getOperandBundle(ID: llvm::LLVMContext::OB_convergencectrl);
1794 assert(Bundle->Inputs.size() == 1 &&
1795 Bundle->Inputs[0]->getType()->isTokenTy());
1796 return dyn_cast<llvm::IntrinsicInst>(Val: Bundle->Inputs[0].get());
1797 }
1798};
1799
1800} // end namespace llvm
1801
1802#endif // LLVM_IR_INTRINSICINST_H
1803

source code of llvm/include/llvm/IR/IntrinsicInst.h