blob: df3a38ac147feb08fbf98a9bb4d229ff2ebc0995 [file] [log] [blame]
Nick Lewycky5820a932014-02-26 03:10:45 +00001//===-- InstrinsicInst.cpp - Intrinsic Instruction Wrappers ---------------===//
Jim Laskey4556ce52006-03-23 18:05:12 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Laskey4556ce52006-03-23 18:05:12 +00007//
8//===----------------------------------------------------------------------===//
Jim Laskey98a69792006-03-24 10:00:56 +00009//
10// This file implements methods that make it really easy to deal with intrinsic
Devang Patel44a29e02010-01-05 01:10:40 +000011// functions.
Jim Laskey98a69792006-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 Ding75acc652017-08-24 04:18:24 +000017//
Jim Laskey98a69792006-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 Ding75acc652017-08-24 04:18:24 +000020// cast instruction needs to be stripped away.
Jim Laskey98a69792006-03-24 10:00:56 +000021//
22//===----------------------------------------------------------------------===//
Jim Laskey4556ce52006-03-23 18:05:12 +000023
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000024#include "llvm/IR/IntrinsicInst.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000025#include "llvm/ADT/StringSwitch.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Bjorn Pettersson0ac3f0a2018-06-15 13:48:55 +000027#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000028#include "llvm/IR/GlobalVariable.h"
29#include "llvm/IR/Metadata.h"
Xinliang David Li19dfeb02016-09-20 19:07:22 +000030#include "llvm/IR/Module.h"
Reid Klecknerdc8eb982016-01-26 22:33:19 +000031#include "llvm/Support/raw_ostream.h"
Jim Laskey4556ce52006-03-23 18:05:12 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
Hsiangkai Wangcb6242d2018-08-06 03:59:47 +000035/// DbgVariableIntrinsic - This is the common base class for debug info
36/// intrinsics for variables.
Jim Laskey4556ce52006-03-23 18:05:12 +000037///
38
Hsiangkai Wangcb6242d2018-08-06 03:59:47 +000039Value *DbgVariableIntrinsic::getVariableLocation(bool AllowNullOp) const {
Duncan P. N. Exon Smith0ee57882016-03-29 18:56:03 +000040 Value *Op = getArgOperand(0);
41 if (AllowNullOp && !Op)
42 return nullptr;
43
Duncan P. N. Exon Smithdad20b22014-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 Wangcb6242d2018-08-06 03:59:47 +000049 assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode");
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +000050 return nullptr;
51}
52
Hsiangkai Wangcb6242d2018-08-06 03:59:47 +000053Optional<uint64_t> DbgVariableIntrinsic::getFragmentSizeInBits() const {
Bjorn Pettersson0ac3f0a2018-06-15 13:48:55 +000054 if (auto Fragment = getExpression()->getFragmentInfo())
55 return Fragment->SizeInBits;
56 return getVariable()->getSizeInBits();
57}
58
Reid Klecknerdc8eb982016-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.
70 size_t CmpStart = 0;
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) {
76 CmpStart = CmpEnd;
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;
81 };
82 LastLow = Low;
83 std::tie(Low, High) = std::equal_range(Low, High, Name.data(), Cmp);
84 }
85 if (High - Low > 0)
86 LastLow = Low;
87
88 if (LastLow == NameTable.end())
89 return -1;
90 StringRef NameFound = *LastLow;
91 if (Name == NameFound ||
92 (Name.startswith(NameFound) && Name[NameFound.size()] == '.'))
93 return LastLow - NameTable.begin();
94 return -1;
95}
Xinliang David Li19dfeb02016-09-20 19:07:22 +000096
97Value *InstrProfIncrementInst::getStep() const {
98 if (InstrProfIncrementInstStep::classof(this)) {
99 return const_cast<Value *>(getArgOperand(4));
100 }
101 const Module *M = getModule();
102 LLVMContext &Context = M->getContext();
103 return ConstantInt::get(Type::getInt64Ty(Context), 1);
104}
Andrew Kaylore6f10d32017-01-26 23:27:59 +0000105
106ConstrainedFPIntrinsic::RoundingMode
107ConstrainedFPIntrinsic::getRoundingMode() const {
Andrew Kaylor325c6862017-05-25 21:31:00 +0000108 unsigned NumOperands = getNumArgOperands();
Wei Ding75acc652017-08-24 04:18:24 +0000109 Metadata *MD =
Andrew Kaylor325c6862017-05-25 21:31:00 +0000110 dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 2))->getMetadata();
Andrew Kaylore6f10d32017-01-26 23:27:59 +0000111 if (!MD || !isa<MDString>(MD))
112 return rmInvalid;
113 StringRef RoundingArg = cast<MDString>(MD)->getString();
114
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)
123 .Default(rmInvalid);
124}
125
126ConstrainedFPIntrinsic::ExceptionBehavior
127ConstrainedFPIntrinsic::getExceptionBehavior() const {
Andrew Kaylor325c6862017-05-25 21:31:00 +0000128 unsigned NumOperands = getNumArgOperands();
Wei Ding75acc652017-08-24 04:18:24 +0000129 Metadata *MD =
Andrew Kaylor325c6862017-05-25 21:31:00 +0000130 dyn_cast<MetadataAsValue>(getArgOperand(NumOperands - 1))->getMetadata();
Andrew Kaylore6f10d32017-01-26 23:27:59 +0000131 if (!MD || !isa<MDString>(MD))
132 return ebInvalid;
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)
138 .Default(ebInvalid);
139}
Andrew Kaylor325c6862017-05-25 21:31:00 +0000140
141bool ConstrainedFPIntrinsic::isUnaryOp() const {
142 switch (getIntrinsicID()) {
Wei Ding75acc652017-08-24 04:18:24 +0000143 default:
Andrew Kaylor325c6862017-05-25 21:31:00 +0000144 return false;
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:
Cameron McInally7c442632018-11-05 15:59:49 +0000155 case Intrinsic::experimental_constrained_ceil:
156 case Intrinsic::experimental_constrained_floor:
157 case Intrinsic::experimental_constrained_round:
158 case Intrinsic::experimental_constrained_trunc:
Andrew Kaylor325c6862017-05-25 21:31:00 +0000159 return true;
160 }
161}
Wei Ding75acc652017-08-24 04:18:24 +0000162
163bool ConstrainedFPIntrinsic::isTernaryOp() const {
164 switch (getIntrinsicID()) {
165 default:
166 return false;
167 case Intrinsic::experimental_constrained_fma:
168 return true;
169 }
170}
171