1 //===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements methods that make it really easy to deal with intrinsic
13 // All intrinsic function calls are instances of the call instruction, so these
14 // are all subclasses of the CallInst class. Note that none of these classes
15 // has state or virtual methods, which is an important part of this gross/neat
18 // In some cases, arguments to intrinsics need to be generic and are defined as
19 // type pointer to empty struct { }*. To access the real item of interest the
20 // cast instruction needs to be stripped away.
22 //===----------------------------------------------------------------------===//
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/ADT/StringSwitch.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DebugInfoMetadata.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Metadata.h"
30 #include "llvm/IR/Module.h"
31 #include "llvm/Support/raw_ostream.h"
34 //===----------------------------------------------------------------------===//
35 /// DbgVariableIntrinsic - This is the common base class for debug info
36 /// intrinsics for variables.
39 Value
*DbgVariableIntrinsic::getVariableLocation(bool AllowNullOp
) const {
40 Value
*Op
= getArgOperand(0);
41 if (AllowNullOp
&& !Op
)
44 auto *MD
= cast
<MetadataAsValue
>(Op
)->getMetadata();
45 if (auto *V
= dyn_cast
<ValueAsMetadata
>(MD
))
48 // When the value goes to null, it gets replaced by an empty MDNode.
49 assert(!cast
<MDNode
>(MD
)->getNumOperands() && "Expected an empty MDNode");
53 Optional
<uint64_t> DbgVariableIntrinsic::getFragmentSizeInBits() const {
54 if (auto Fragment
= getExpression()->getFragmentInfo())
55 return Fragment
->SizeInBits
;
56 return getVariable()->getSizeInBits();
59 int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef
<const char *> NameTable
,
61 assert(Name
.startswith("llvm."));
63 // Do successive binary searches of the dotted name components. For
64 // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
65 // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
66 // "llvm.gc.experimental.statepoint", and then we will stop as the range is
67 // size 1. During the search, we can skip the prefix that we already know is
68 // identical. By using strncmp we consider names with differing suffixes to
69 // be part of the equal range.
71 size_t CmpEnd
= 4; // Skip the "llvm" component.
72 const char *const *Low
= NameTable
.begin();
73 const char *const *High
= NameTable
.end();
74 const char *const *LastLow
= Low
;
75 while (CmpEnd
< Name
.size() && High
- Low
> 0) {
77 CmpEnd
= Name
.find('.', CmpStart
+ 1);
78 CmpEnd
= CmpEnd
== StringRef::npos
? Name
.size() : CmpEnd
;
79 auto Cmp
= [CmpStart
, CmpEnd
](const char *LHS
, const char *RHS
) {
80 return strncmp(LHS
+ CmpStart
, RHS
+ CmpStart
, CmpEnd
- CmpStart
) < 0;
83 std::tie(Low
, High
) = std::equal_range(Low
, High
, Name
.data(), Cmp
);
88 if (LastLow
== NameTable
.end())
90 StringRef NameFound
= *LastLow
;
91 if (Name
== NameFound
||
92 (Name
.startswith(NameFound
) && Name
[NameFound
.size()] == '.'))
93 return LastLow
- NameTable
.begin();
97 Value
*InstrProfIncrementInst::getStep() const {
98 if (InstrProfIncrementInstStep::classof(this)) {
99 return const_cast<Value
*>(getArgOperand(4));
101 const Module
*M
= getModule();
102 LLVMContext
&Context
= M
->getContext();
103 return ConstantInt::get(Type::getInt64Ty(Context
), 1);
106 ConstrainedFPIntrinsic::RoundingMode
107 ConstrainedFPIntrinsic::getRoundingMode() const {
108 unsigned NumOperands
= getNumArgOperands();
110 dyn_cast
<MetadataAsValue
>(getArgOperand(NumOperands
- 2))->getMetadata();
111 if (!MD
|| !isa
<MDString
>(MD
))
113 StringRef RoundingArg
= cast
<MDString
>(MD
)->getString();
115 // For dynamic rounding mode, we use round to nearest but we will set the
116 // 'exact' SDNodeFlag so that the value will not be rounded.
117 return StringSwitch
<RoundingMode
>(RoundingArg
)
118 .Case("round.dynamic", rmDynamic
)
119 .Case("round.tonearest", rmToNearest
)
120 .Case("round.downward", rmDownward
)
121 .Case("round.upward", rmUpward
)
122 .Case("round.towardzero", rmTowardZero
)
126 ConstrainedFPIntrinsic::ExceptionBehavior
127 ConstrainedFPIntrinsic::getExceptionBehavior() const {
128 unsigned NumOperands
= getNumArgOperands();
130 dyn_cast
<MetadataAsValue
>(getArgOperand(NumOperands
- 1))->getMetadata();
131 if (!MD
|| !isa
<MDString
>(MD
))
133 StringRef ExceptionArg
= cast
<MDString
>(MD
)->getString();
134 return StringSwitch
<ExceptionBehavior
>(ExceptionArg
)
135 .Case("fpexcept.ignore", ebIgnore
)
136 .Case("fpexcept.maytrap", ebMayTrap
)
137 .Case("fpexcept.strict", ebStrict
)
141 bool ConstrainedFPIntrinsic::isUnaryOp() const {
142 switch (getIntrinsicID()) {
145 case Intrinsic::experimental_constrained_sqrt
:
146 case Intrinsic::experimental_constrained_sin
:
147 case Intrinsic::experimental_constrained_cos
:
148 case Intrinsic::experimental_constrained_exp
:
149 case Intrinsic::experimental_constrained_exp2
:
150 case Intrinsic::experimental_constrained_log
:
151 case Intrinsic::experimental_constrained_log10
:
152 case Intrinsic::experimental_constrained_log2
:
153 case Intrinsic::experimental_constrained_rint
:
154 case Intrinsic::experimental_constrained_nearbyint
:
159 bool ConstrainedFPIntrinsic::isTernaryOp() const {
160 switch (getIntrinsicID()) {
163 case Intrinsic::experimental_constrained_fma
: