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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

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