blob: 19b01bd319667ebfe24264f825c7dbc3a858f9d1 [file] [log] [blame]
Nick Lewyckyea08c702014-02-26 03:10:45 +00001//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
Jim Laskey0cf8ed62006-03-23 18:05:12 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Jim Laskey0cf8ed62006-03-23 18:05:12 +00006//
7//===----------------------------------------------------------------------===//
Jim Laskey864e4442006-03-24 10:00:56 +00008//
9// This file implements methods that make it really easy to deal with intrinsic
Devang Patelbe94f232010-01-05 01:10:40 +000010// functions.
Jim Laskey864e4442006-03-24 10:00:56 +000011//
12// All intrinsic function calls are instances of the call instruction, so these
13// are all subclasses of the CallInst class. Note that none of these classes
14// has state or virtual methods, which is an important part of this gross/neat
15// hack working.
Wei Dinga131d3f2017-08-24 04:18:24 +000016//
Jim Laskey864e4442006-03-24 10:00:56 +000017// In some cases, arguments to intrinsics need to be generic and are defined as
18// type pointer to empty struct { }*. To access the real item of interest the
Wei Dinga131d3f2017-08-24 04:18:24 +000019// cast instruction needs to be stripped away.
Jim Laskey864e4442006-03-24 10:00:56 +000020//
21//===----------------------------------------------------------------------===//
Jim Laskey0cf8ed62006-03-23 18:05:12 +000022
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/IntrinsicInst.h"
Nikita Popov79dffc62019-04-16 18:55:16 +000024#include "llvm/IR/Operator.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//===----------------------------------------------------------------------===//
Hsiangkai Wangef72e482018-08-06 03:59:47 +000035/// DbgVariableIntrinsic - This is the common base class for debug info
36/// intrinsics for variables.
Jim Laskey0cf8ed62006-03-23 18:05:12 +000037///
38
Hsiangkai Wangef72e482018-08-06 03:59:47 +000039Value *DbgVariableIntrinsic::getVariableLocation(bool AllowNullOp) const {
Duncan P. N. Exon Smith40b44e12016-03-29 18:56:03 +000040 Value *Op = getArgOperand(0);
41 if (AllowNullOp && !Op)
42 return nullptr;
43
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000044 auto *MD = cast<MetadataAsValue>(Op)->getMetadata();
45 if (auto *V = dyn_cast<ValueAsMetadata>(MD))
46 return V->getValue();
47
48 // When the value goes to null, it gets replaced by an empty MDNode.
Hsiangkai Wangef72e482018-08-06 03:59:47 +000049 assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000050 return nullptr;
51}
52
Hsiangkai Wangef72e482018-08-06 03:59:47 +000053Optional<uint64_t> DbgVariableIntrinsic::getFragmentSizeInBits() const {
Bjorn Pettersson428caf92018-06-15 13:48:55 +000054 if (auto Fragment = getExpression()->getFragmentInfo())
55 return Fragment->SizeInBits;
56 return getVariable()->getSizeInBits();
57}
58
Reid Klecknerc2752da2016-01-26 22:33:19 +000059int llvm::Intrinsic::lookupLLVMIntrinsicByName(ArrayRef<const char *> NameTable,
60 StringRef Name) {
61 assert(Name.startswith("llvm."));
62
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.
Reid Klecknerc2752da2016-01-26 22:33:19 +000070 size_t CmpEnd = 4; // Skip the "llvm" component.
71 const char *const *Low = NameTable.begin();
72 const char *const *High = NameTable.end();
73 const char *const *LastLow = Low;
74 while (CmpEnd < Name.size() && High - Low > 0) {
Simon Pilgrimcc972982019-09-24 12:30:07 +000075 size_t CmpStart = CmpEnd;
Reid Klecknerc2752da2016-01-26 22:33:19 +000076 CmpEnd = Name.find('.', CmpStart + 1);
77 CmpEnd = CmpEnd == StringRef::npos ? Name.size() : CmpEnd;
78 auto Cmp = [CmpStart, CmpEnd](const char *LHS, const char *RHS) {
79 return strncmp(LHS + CmpStart, RHS + CmpStart, CmpEnd - CmpStart) < 0;
80 };
81 LastLow = Low;
82 std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
83 }
84 if (High - Low > 0)
85 LastLow = Low;
86
87 if (LastLow == NameTable.end())
88 return -1;
89 StringRef NameFound = *LastLow;
90 if (Name == NameFound ||
91 (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
92 return LastLow - NameTable.begin();
93 return -1;
94}
Xinliang David Lia754c472016-09-20 19:07:22 +000095
96Value *InstrProfIncrementInst::getStep() const {
97 if (InstrProfIncrementInstStep::classof(this)) {
98 return const_cast<Value *>(getArgOperand(4));
99 }
100 const Module *M = getModule();
101 LLVMContext &Context = M->getContext();
102 return ConstantInt::get(Type::getInt64Ty(Context), 1);
103}
Andrew Kaylora0a11642017-01-26 23:27:59 +0000104
Eric Christopher30e7ee32019-11-18 10:45:23 -0800105Optional<ConstrainedFPIntrinsic::RoundingMode>
106ConstrainedFPIntrinsic::getRoundingMode() const {
Andrew Kaylorf4660012017-05-25 21:31:00 +0000107 unsigned NumOperands = getNumArgOperands();
Wei Dinga131d3f2017-08-24 04:18:24 +0000108 Metadata *MD =
Simon Pilgrim06cdcb52019-09-24 11:40:45 +0000109 cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
Andrew Kaylora0a11642017-01-26 23:27:59 +0000110 if (!MD || !isa<MDString>(MD))
Kevin P. Neal472e5dd2019-07-08 16:18:18 +0000111 return None;
112 return StrToRoundingMode(cast<MDString>(MD)->getString());
113}
Andrew Kaylora0a11642017-01-26 23:27:59 +0000114
Eric Christopher30e7ee32019-11-18 10:45:23 -0800115Optional<ConstrainedFPIntrinsic::RoundingMode>
116ConstrainedFPIntrinsic::StrToRoundingMode(StringRef RoundingArg) {
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<Optional<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(None);
126}
127
128Optional<StringRef>
129ConstrainedFPIntrinsic::RoundingModeToStr(RoundingMode UseRounding) {
130 Optional<StringRef> RoundingStr = None;
131 switch (UseRounding) {
132 case ConstrainedFPIntrinsic::rmDynamic:
133 RoundingStr = "round.dynamic";
134 break;
135 case ConstrainedFPIntrinsic::rmToNearest:
136 RoundingStr = "round.tonearest";
137 break;
138 case ConstrainedFPIntrinsic::rmDownward:
139 RoundingStr = "round.downward";
140 break;
141 case ConstrainedFPIntrinsic::rmUpward:
142 RoundingStr = "round.upward";
143 break;
144 case ConstrainedFPIntrinsic::rmTowardZero:
145 RoundingStr = "round.towardzero";
146 break;
147 }
148 return RoundingStr;
149}
150
151Optional<ConstrainedFPIntrinsic::ExceptionBehavior>
Andrew Kaylora0a11642017-01-26 23:27:59 +0000152ConstrainedFPIntrinsic::getExceptionBehavior() const {
Andrew Kaylorf4660012017-05-25 21:31:00 +0000153 unsigned NumOperands = getNumArgOperands();
Wei Dinga131d3f2017-08-24 04:18:24 +0000154 Metadata *MD =
Simon Pilgrim06cdcb52019-09-24 11:40:45 +0000155 cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
Andrew Kaylora0a11642017-01-26 23:27:59 +0000156 if (!MD || !isa<MDString>(MD))
Kevin P. Neal472e5dd2019-07-08 16:18:18 +0000157 return None;
158 return StrToExceptionBehavior(cast<MDString>(MD)->getString());
159}
160
Eric Christopher30e7ee32019-11-18 10:45:23 -0800161Optional<ConstrainedFPIntrinsic::ExceptionBehavior>
162ConstrainedFPIntrinsic::StrToExceptionBehavior(StringRef ExceptionArg) {
163 return StringSwitch<Optional<ExceptionBehavior>>(ExceptionArg)
164 .Case("fpexcept.ignore", ebIgnore)
165 .Case("fpexcept.maytrap", ebMayTrap)
166 .Case("fpexcept.strict", ebStrict)
167 .Default(None);
168}
169
170Optional<StringRef>
171ConstrainedFPIntrinsic::ExceptionBehaviorToStr(ExceptionBehavior UseExcept) {
172 Optional<StringRef> ExceptStr = None;
173 switch (UseExcept) {
174 case ConstrainedFPIntrinsic::ebStrict:
175 ExceptStr = "fpexcept.strict";
176 break;
177 case ConstrainedFPIntrinsic::ebIgnore:
178 ExceptStr = "fpexcept.ignore";
179 break;
180 case ConstrainedFPIntrinsic::ebMayTrap:
181 ExceptStr = "fpexcept.maytrap";
182 break;
183 }
184 return ExceptStr;
185}
186
Andrew Kaylorf4660012017-05-25 21:31:00 +0000187bool ConstrainedFPIntrinsic::isUnaryOp() const {
188 switch (getIntrinsicID()) {
Wei Dinga131d3f2017-08-24 04:18:24 +0000189 default:
Andrew Kaylorf4660012017-05-25 21:31:00 +0000190 return false;
Serge Pavlov0c50c0b2019-11-05 20:42:16 +0700191#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
192 case Intrinsic::INTRINSIC: \
193 return NARG == 1;
194#include "llvm/IR/ConstrainedOps.def"
Andrew Kaylorf4660012017-05-25 21:31:00 +0000195 }
196}
Wei Dinga131d3f2017-08-24 04:18:24 +0000197
198bool ConstrainedFPIntrinsic::isTernaryOp() const {
199 switch (getIntrinsicID()) {
200 default:
201 return false;
Serge Pavlov0c50c0b2019-11-05 20:42:16 +0700202#define INSTRUCTION(NAME, NARG, ROUND_MODE, INTRINSIC, DAGN) \
203 case Intrinsic::INTRINSIC: \
204 return NARG == 3;
205#include "llvm/IR/ConstrainedOps.def"
206 }
207}
208
209bool ConstrainedFPIntrinsic::classof(const IntrinsicInst *I) {
210 switch (I->getIntrinsicID()) {
211#define INSTRUCTION(NAME, NARGS, ROUND_MODE, INTRINSIC, DAGN) \
212 case Intrinsic::INTRINSIC:
213#include "llvm/IR/ConstrainedOps.def"
214 return true;
215 default:
216 return false;
Wei Dinga131d3f2017-08-24 04:18:24 +0000217 }
218}
219
Nikita Popov2fb0a822019-05-28 18:08:06 +0000220Instruction::BinaryOps BinaryOpIntrinsic::getBinaryOp() const {
Nikita Popov79dffc62019-04-16 18:55:16 +0000221 switch (getIntrinsicID()) {
222 case Intrinsic::uadd_with_overflow:
223 case Intrinsic::sadd_with_overflow:
Nikita Popov2fb0a822019-05-28 18:08:06 +0000224 case Intrinsic::uadd_sat:
225 case Intrinsic::sadd_sat:
Nikita Popov79dffc62019-04-16 18:55:16 +0000226 return Instruction::Add;
227 case Intrinsic::usub_with_overflow:
228 case Intrinsic::ssub_with_overflow:
Nikita Popov2fb0a822019-05-28 18:08:06 +0000229 case Intrinsic::usub_sat:
230 case Intrinsic::ssub_sat:
Nikita Popov79dffc62019-04-16 18:55:16 +0000231 return Instruction::Sub;
232 case Intrinsic::umul_with_overflow:
233 case Intrinsic::smul_with_overflow:
234 return Instruction::Mul;
235 default:
236 llvm_unreachable("Invalid intrinsic");
237 }
238}
239
Nikita Popov2fb0a822019-05-28 18:08:06 +0000240bool BinaryOpIntrinsic::isSigned() const {
Nikita Popov79dffc62019-04-16 18:55:16 +0000241 switch (getIntrinsicID()) {
242 case Intrinsic::sadd_with_overflow:
243 case Intrinsic::ssub_with_overflow:
244 case Intrinsic::smul_with_overflow:
Nikita Popov2fb0a822019-05-28 18:08:06 +0000245 case Intrinsic::sadd_sat:
246 case Intrinsic::ssub_sat:
Nikita Popov79dffc62019-04-16 18:55:16 +0000247 return true;
248 default:
249 return false;
250 }
251}
252
Nikita Popov2fb0a822019-05-28 18:08:06 +0000253unsigned BinaryOpIntrinsic::getNoWrapKind() const {
Nikita Popov79dffc62019-04-16 18:55:16 +0000254 if (isSigned())
255 return OverflowingBinaryOperator::NoSignedWrap;
256 else
257 return OverflowingBinaryOperator::NoUnsignedWrap;
258}