blob: 787889934d82cce68eefe6c5668bff35291e872b [file] [log] [blame]
Nick Lewyckyea08c702014-02-26 03:10:45 +00001//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
Jim Laskey0cf8ed62006-03-23 18:05:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Jim Laskey0cf8ed62006-03-23 18:05:12 +00007//
8//===----------------------------------------------------------------------===//
Jim Laskey864e4442006-03-24 10:00:56 +00009//
10// This file implements methods that make it really easy to deal with intrinsic
Devang Patelbe94f232010-01-05 01:10:40 +000011// functions.
Jim Laskey864e4442006-03-24 10:00:56 +000012//
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
16// hack working.
Wei Dinga131d3f2017-08-24 04:18:24 +000017//
Jim Laskey864e4442006-03-24 10:00:56 +000018// 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
Wei Dinga131d3f2017-08-24 04:18:24 +000020// cast instruction needs to be stripped away.
Jim Laskey864e4442006-03-24 10:00:56 +000021//
22//===----------------------------------------------------------------------===//
Jim Laskey0cf8ed62006-03-23 18:05:12 +000023
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "llvm/ADT/StringSwitch.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Bjorn Pettersson428caf92018-06-15 13:48:55 +000027#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Metadata.h"
Xinliang David Lia754c472016-09-20 19:07:22 +000030#include "llvm/IR/Module.h"
Reid Klecknerc2752da2016-01-26 22:33:19 +000031#include "llvm/Support/raw_ostream.h"
Jim Laskey0cf8ed62006-03-23 18:05:12 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35/// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
36///
37
Duncan P. N. Exon Smith40b44e12016-03-29 18:56:03 +000038Value *DbgInfoIntrinsic::getVariableLocation(bool AllowNullOp) const {
39 Value *Op = getArgOperand(0);
40 if (AllowNullOp && !Op)
41 return nullptr;
42
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000043 auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
44 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
45 return V->getValue();
46
47 // When the value goes to null, it gets replaced by an empty MDNode.
Shiva Chena0a52bf2018-07-03 07:56:04 +000048 assert((isa<DbgLabelInst>(this)
49 || !cast<MDNode>(MD)->getNumOperands())
50 && "DbgValueInst Expected an empty MDNode");
51
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000052 return nullptr;
53}
54
Bjorn Pettersson428caf92018-06-15 13:48:55 +000055Optional<uint64_t> DbgInfoIntrinsic::getFragmentSizeInBits() const {
56 if (auto Fragment = getExpression()->getFragmentInfo())
57 return Fragment->SizeInBits;
58 return getVariable()->getSizeInBits();
59}
60
Reid Klecknerc2752da2016-01-26 22:33:19 +000061int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
62 StringRef Name) {
63 assert(Name.startswith("llvm."));
64
65 // Do successive binary searches of the dotted name components. For
66 // "llvm.gc.experimental.statepoint.p1i8.p1i32", we will find the range of
67 // intrinsics starting with "llvm.gc", then "llvm.gc.experimental", then
68 // "llvm.gc.experimental.statepoint", and then we will stop as the range is
69 // size 1. During the search, we can skip the prefix that we already know is
70 // identical. By using strncmp we consider names with differing suffixes to
71 // be part of the equal range.
72 size_t CmpStart = 0;
73 size_t CmpEnd = 4; // Skip the "llvm" component.
74 const char *const *Low = NameTable.begin();
75 const char *const *High = NameTable.end();
76 const char *const *LastLow = Low;
77 while (CmpEnd < Name.size() && High - Low > 0) {
78 CmpStart = CmpEnd;
79 CmpEnd = Name.find('.', CmpStart + 1);
80 CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
81 auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
82 return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
83 };
84 LastLow = Low;
85 std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
86 }
87 if (High - Low > 0)
88 LastLow = Low;
89
90 if (LastLow == NameTable.end())
91 return -1;
92 StringRef NameFound = *LastLow;
93 if (Name == NameFound ||
94 (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
95 return LastLow - NameTable.begin();
96 return -1;
97}
Xinliang David Lia754c472016-09-20 19:07:22 +000098
99Value *InstrProfIncrementInst::getStep() const {
100 if (InstrProfIncrementInstStep::classof(this)) {
101 return const_cast<Value *>(getArgOperand(4));
102 }
103 const Module *M = getModule();
104 LLVMContext &Context = M->getContext();
105 return ConstantInt::get(Type::getInt64Ty(Context), 1);
106}
Andrew Kaylora0a11642017-01-26 23:27:59 +0000107
108ConstrainedFPIntrinsic::RoundingMode
109ConstrainedFPIntrinsic::getRoundingMode() const {
Andrew Kaylorf4660012017-05-25 21:31:00 +0000110 unsigned NumOperands = getNumArgOperands();
Wei Dinga131d3f2017-08-24 04:18:24 +0000111 Metadata *MD =
Andrew Kaylorf4660012017-05-25 21:31:00 +0000112 dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
Andrew Kaylora0a11642017-01-26 23:27:59 +0000113 if (!MD || !isa<MDString>(MD))
114 return rmInvalid;
115 StringRef RoundingArg = cast<MDString>(MD)->getString();
116
117 // For dynamic rounding mode, we use round to nearest but we will set the
118 // 'exact' SDNodeFlag so that the value will not be rounded.
119 return StringSwitch<RoundingMode>(RoundingArg)
120 .Case("round.dynamic", rmDynamic)
121 .Case("round.tonearest", rmToNearest)
122 .Case("round.downward", rmDownward)
123 .Case("round.upward", rmUpward)
124 .Case("round.towardzero", rmTowardZero)
125 .Default(rmInvalid);
126}
127
128ConstrainedFPIntrinsic::ExceptionBehavior
129ConstrainedFPIntrinsic::getExceptionBehavior() const {
Andrew Kaylorf4660012017-05-25 21:31:00 +0000130 unsigned NumOperands = getNumArgOperands();
Wei Dinga131d3f2017-08-24 04:18:24 +0000131 Metadata *MD =
Andrew Kaylorf4660012017-05-25 21:31:00 +0000132 dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
Andrew Kaylora0a11642017-01-26 23:27:59 +0000133 if (!MD || !isa<MDString>(MD))
134 return ebInvalid;
135 StringRef ExceptionArg = cast<MDString>(MD)->getString();
136 return StringSwitch<ExceptionBehavior>(ExceptionArg)
137 .Case("fpexcept.ignore", ebIgnore)
138 .Case("fpexcept.maytrap", ebMayTrap)
139 .Case("fpexcept.strict", ebStrict)
140 .Default(ebInvalid);
141}
Andrew Kaylorf4660012017-05-25 21:31:00 +0000142
143bool ConstrainedFPIntrinsic::isUnaryOp() const {
144 switch (getIntrinsicID()) {
Wei Dinga131d3f2017-08-24 04:18:24 +0000145 default:
Andrew Kaylorf4660012017-05-25 21:31:00 +0000146 return false;
147 case Intrinsic::experimental_constrained_sqrt:
148 case Intrinsic::experimental_constrained_sin:
149 case Intrinsic::experimental_constrained_cos:
150 case Intrinsic::experimental_constrained_exp:
151 case Intrinsic::experimental_constrained_exp2:
152 case Intrinsic::experimental_constrained_log:
153 case Intrinsic::experimental_constrained_log10:
154 case Intrinsic::experimental_constrained_log2:
155 case Intrinsic::experimental_constrained_rint:
156 case Intrinsic::experimental_constrained_nearbyint:
157 return true;
158 }
159}
Wei Dinga131d3f2017-08-24 04:18:24 +0000160
161bool ConstrainedFPIntrinsic::isTernaryOp() const {
162 switch (getIntrinsicID()) {
163 default:
164 return false;
165 case Intrinsic::experimental_constrained_fma:
166 return true;
167 }
168}
169