1*9880d681SAndroid Build Coastguard Worker //===-------------------------- TargetRecip.cpp ---------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This class is used to customize machine-specific reciprocal estimate code
11*9880d681SAndroid Build Coastguard Worker // generation in a target-independent way.
12*9880d681SAndroid Build Coastguard Worker // If a target does not support operations in this specification, then code
13*9880d681SAndroid Build Coastguard Worker // generation will default to using supported operations.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRecip.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringRef.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker using namespace llvm;
23*9880d681SAndroid Build Coastguard Worker
24*9880d681SAndroid Build Coastguard Worker // These are the names of the individual reciprocal operations. These are
25*9880d681SAndroid Build Coastguard Worker // the key strings for queries and command-line inputs.
26*9880d681SAndroid Build Coastguard Worker // In addition, the command-line interface recognizes the global parameters
27*9880d681SAndroid Build Coastguard Worker // "all", "none", and "default".
28*9880d681SAndroid Build Coastguard Worker static const char *const RecipOps[] = {
29*9880d681SAndroid Build Coastguard Worker "divd",
30*9880d681SAndroid Build Coastguard Worker "divf",
31*9880d681SAndroid Build Coastguard Worker "vec-divd",
32*9880d681SAndroid Build Coastguard Worker "vec-divf",
33*9880d681SAndroid Build Coastguard Worker "sqrtd",
34*9880d681SAndroid Build Coastguard Worker "sqrtf",
35*9880d681SAndroid Build Coastguard Worker "vec-sqrtd",
36*9880d681SAndroid Build Coastguard Worker "vec-sqrtf",
37*9880d681SAndroid Build Coastguard Worker };
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker // The uninitialized state is needed for the enabled settings and refinement
40*9880d681SAndroid Build Coastguard Worker // steps because custom settings may arrive via the command-line before target
41*9880d681SAndroid Build Coastguard Worker // defaults are set.
TargetRecip()42*9880d681SAndroid Build Coastguard Worker TargetRecip::TargetRecip() {
43*9880d681SAndroid Build Coastguard Worker unsigned NumStrings = llvm::array_lengthof(RecipOps);
44*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i < NumStrings; ++i)
45*9880d681SAndroid Build Coastguard Worker RecipMap.insert(std::make_pair(RecipOps[i], RecipParams()));
46*9880d681SAndroid Build Coastguard Worker }
47*9880d681SAndroid Build Coastguard Worker
parseRefinementStep(StringRef In,size_t & Position,uint8_t & Value)48*9880d681SAndroid Build Coastguard Worker static bool parseRefinementStep(StringRef In, size_t &Position,
49*9880d681SAndroid Build Coastguard Worker uint8_t &Value) {
50*9880d681SAndroid Build Coastguard Worker const char RefStepToken = ':';
51*9880d681SAndroid Build Coastguard Worker Position = In.find(RefStepToken);
52*9880d681SAndroid Build Coastguard Worker if (Position == StringRef::npos)
53*9880d681SAndroid Build Coastguard Worker return false;
54*9880d681SAndroid Build Coastguard Worker
55*9880d681SAndroid Build Coastguard Worker StringRef RefStepString = In.substr(Position + 1);
56*9880d681SAndroid Build Coastguard Worker // Allow exactly one numeric character for the additional refinement
57*9880d681SAndroid Build Coastguard Worker // step parameter.
58*9880d681SAndroid Build Coastguard Worker if (RefStepString.size() == 1) {
59*9880d681SAndroid Build Coastguard Worker char RefStepChar = RefStepString[0];
60*9880d681SAndroid Build Coastguard Worker if (RefStepChar >= '0' && RefStepChar <= '9') {
61*9880d681SAndroid Build Coastguard Worker Value = RefStepChar - '0';
62*9880d681SAndroid Build Coastguard Worker return true;
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker report_fatal_error("Invalid refinement step for -recip.");
66*9880d681SAndroid Build Coastguard Worker }
67*9880d681SAndroid Build Coastguard Worker
parseGlobalParams(const std::string & Arg)68*9880d681SAndroid Build Coastguard Worker bool TargetRecip::parseGlobalParams(const std::string &Arg) {
69*9880d681SAndroid Build Coastguard Worker StringRef ArgSub = Arg;
70*9880d681SAndroid Build Coastguard Worker
71*9880d681SAndroid Build Coastguard Worker // Look for an optional setting of the number of refinement steps needed
72*9880d681SAndroid Build Coastguard Worker // for this type of reciprocal operation.
73*9880d681SAndroid Build Coastguard Worker size_t RefPos;
74*9880d681SAndroid Build Coastguard Worker uint8_t RefSteps;
75*9880d681SAndroid Build Coastguard Worker StringRef RefStepString;
76*9880d681SAndroid Build Coastguard Worker if (parseRefinementStep(ArgSub, RefPos, RefSteps)) {
77*9880d681SAndroid Build Coastguard Worker // Split the string for further processing.
78*9880d681SAndroid Build Coastguard Worker RefStepString = ArgSub.substr(RefPos + 1);
79*9880d681SAndroid Build Coastguard Worker ArgSub = ArgSub.substr(0, RefPos);
80*9880d681SAndroid Build Coastguard Worker }
81*9880d681SAndroid Build Coastguard Worker bool Enable;
82*9880d681SAndroid Build Coastguard Worker bool UseDefaults;
83*9880d681SAndroid Build Coastguard Worker if (ArgSub == "all") {
84*9880d681SAndroid Build Coastguard Worker UseDefaults = false;
85*9880d681SAndroid Build Coastguard Worker Enable = true;
86*9880d681SAndroid Build Coastguard Worker } else if (ArgSub == "none") {
87*9880d681SAndroid Build Coastguard Worker UseDefaults = false;
88*9880d681SAndroid Build Coastguard Worker Enable = false;
89*9880d681SAndroid Build Coastguard Worker } else if (ArgSub == "default") {
90*9880d681SAndroid Build Coastguard Worker UseDefaults = true;
91*9880d681SAndroid Build Coastguard Worker } else {
92*9880d681SAndroid Build Coastguard Worker // Any other string is invalid or an individual setting.
93*9880d681SAndroid Build Coastguard Worker return false;
94*9880d681SAndroid Build Coastguard Worker }
95*9880d681SAndroid Build Coastguard Worker
96*9880d681SAndroid Build Coastguard Worker // All enable values will be initialized to target defaults if 'default' was
97*9880d681SAndroid Build Coastguard Worker // specified.
98*9880d681SAndroid Build Coastguard Worker if (!UseDefaults)
99*9880d681SAndroid Build Coastguard Worker for (auto &KV : RecipMap)
100*9880d681SAndroid Build Coastguard Worker KV.second.Enabled = Enable;
101*9880d681SAndroid Build Coastguard Worker
102*9880d681SAndroid Build Coastguard Worker // Custom refinement count was specified with all, none, or default.
103*9880d681SAndroid Build Coastguard Worker if (!RefStepString.empty())
104*9880d681SAndroid Build Coastguard Worker for (auto &KV : RecipMap)
105*9880d681SAndroid Build Coastguard Worker KV.second.RefinementSteps = RefSteps;
106*9880d681SAndroid Build Coastguard Worker
107*9880d681SAndroid Build Coastguard Worker return true;
108*9880d681SAndroid Build Coastguard Worker }
109*9880d681SAndroid Build Coastguard Worker
parseIndividualParams(const std::vector<std::string> & Args)110*9880d681SAndroid Build Coastguard Worker void TargetRecip::parseIndividualParams(const std::vector<std::string> &Args) {
111*9880d681SAndroid Build Coastguard Worker static const char DisabledPrefix = '!';
112*9880d681SAndroid Build Coastguard Worker unsigned NumArgs = Args.size();
113*9880d681SAndroid Build Coastguard Worker
114*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0; i != NumArgs; ++i) {
115*9880d681SAndroid Build Coastguard Worker StringRef Val = Args[i];
116*9880d681SAndroid Build Coastguard Worker
117*9880d681SAndroid Build Coastguard Worker bool IsDisabled = Val[0] == DisabledPrefix;
118*9880d681SAndroid Build Coastguard Worker // Ignore the disablement token for string matching.
119*9880d681SAndroid Build Coastguard Worker if (IsDisabled)
120*9880d681SAndroid Build Coastguard Worker Val = Val.substr(1);
121*9880d681SAndroid Build Coastguard Worker
122*9880d681SAndroid Build Coastguard Worker size_t RefPos;
123*9880d681SAndroid Build Coastguard Worker uint8_t RefSteps;
124*9880d681SAndroid Build Coastguard Worker StringRef RefStepString;
125*9880d681SAndroid Build Coastguard Worker if (parseRefinementStep(Val, RefPos, RefSteps)) {
126*9880d681SAndroid Build Coastguard Worker // Split the string for further processing.
127*9880d681SAndroid Build Coastguard Worker RefStepString = Val.substr(RefPos + 1);
128*9880d681SAndroid Build Coastguard Worker Val = Val.substr(0, RefPos);
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker RecipIter Iter = RecipMap.find(Val);
132*9880d681SAndroid Build Coastguard Worker if (Iter == RecipMap.end()) {
133*9880d681SAndroid Build Coastguard Worker // Try again specifying float suffix.
134*9880d681SAndroid Build Coastguard Worker Iter = RecipMap.find(Val.str() + 'f');
135*9880d681SAndroid Build Coastguard Worker if (Iter == RecipMap.end()) {
136*9880d681SAndroid Build Coastguard Worker Iter = RecipMap.find(Val.str() + 'd');
137*9880d681SAndroid Build Coastguard Worker assert(Iter == RecipMap.end() && "Float entry missing from map");
138*9880d681SAndroid Build Coastguard Worker report_fatal_error("Invalid option for -recip.");
139*9880d681SAndroid Build Coastguard Worker }
140*9880d681SAndroid Build Coastguard Worker
141*9880d681SAndroid Build Coastguard Worker // The option was specified without a float or double suffix.
142*9880d681SAndroid Build Coastguard Worker if (RecipMap[Val.str() + 'd'].Enabled != Uninitialized) {
143*9880d681SAndroid Build Coastguard Worker // Make sure that the double entry was not already specified.
144*9880d681SAndroid Build Coastguard Worker // The float entry will be checked below.
145*9880d681SAndroid Build Coastguard Worker report_fatal_error("Duplicate option for -recip.");
146*9880d681SAndroid Build Coastguard Worker }
147*9880d681SAndroid Build Coastguard Worker }
148*9880d681SAndroid Build Coastguard Worker
149*9880d681SAndroid Build Coastguard Worker if (Iter->second.Enabled != Uninitialized)
150*9880d681SAndroid Build Coastguard Worker report_fatal_error("Duplicate option for -recip.");
151*9880d681SAndroid Build Coastguard Worker
152*9880d681SAndroid Build Coastguard Worker // Mark the matched option as found. Do not allow duplicate specifiers.
153*9880d681SAndroid Build Coastguard Worker Iter->second.Enabled = !IsDisabled;
154*9880d681SAndroid Build Coastguard Worker if (!RefStepString.empty())
155*9880d681SAndroid Build Coastguard Worker Iter->second.RefinementSteps = RefSteps;
156*9880d681SAndroid Build Coastguard Worker
157*9880d681SAndroid Build Coastguard Worker // If the precision was not specified, the double entry is also initialized.
158*9880d681SAndroid Build Coastguard Worker if (Val.back() != 'f' && Val.back() != 'd') {
159*9880d681SAndroid Build Coastguard Worker RecipParams &Params = RecipMap[Val.str() + 'd'];
160*9880d681SAndroid Build Coastguard Worker Params.Enabled = !IsDisabled;
161*9880d681SAndroid Build Coastguard Worker if (!RefStepString.empty())
162*9880d681SAndroid Build Coastguard Worker Params.RefinementSteps = RefSteps;
163*9880d681SAndroid Build Coastguard Worker }
164*9880d681SAndroid Build Coastguard Worker }
165*9880d681SAndroid Build Coastguard Worker }
166*9880d681SAndroid Build Coastguard Worker
TargetRecip(const std::vector<std::string> & Args)167*9880d681SAndroid Build Coastguard Worker TargetRecip::TargetRecip(const std::vector<std::string> &Args) :
168*9880d681SAndroid Build Coastguard Worker TargetRecip() {
169*9880d681SAndroid Build Coastguard Worker unsigned NumArgs = Args.size();
170*9880d681SAndroid Build Coastguard Worker
171*9880d681SAndroid Build Coastguard Worker // Check if "all", "default", or "none" was specified.
172*9880d681SAndroid Build Coastguard Worker if (NumArgs == 1 && parseGlobalParams(Args[0]))
173*9880d681SAndroid Build Coastguard Worker return;
174*9880d681SAndroid Build Coastguard Worker
175*9880d681SAndroid Build Coastguard Worker parseIndividualParams(Args);
176*9880d681SAndroid Build Coastguard Worker }
177*9880d681SAndroid Build Coastguard Worker
isEnabled(StringRef Key) const178*9880d681SAndroid Build Coastguard Worker bool TargetRecip::isEnabled(StringRef Key) const {
179*9880d681SAndroid Build Coastguard Worker ConstRecipIter Iter = RecipMap.find(Key);
180*9880d681SAndroid Build Coastguard Worker assert(Iter != RecipMap.end() && "Unknown name for reciprocal map");
181*9880d681SAndroid Build Coastguard Worker assert(Iter->second.Enabled != Uninitialized &&
182*9880d681SAndroid Build Coastguard Worker "Enablement setting was not initialized");
183*9880d681SAndroid Build Coastguard Worker return Iter->second.Enabled;
184*9880d681SAndroid Build Coastguard Worker }
185*9880d681SAndroid Build Coastguard Worker
getRefinementSteps(StringRef Key) const186*9880d681SAndroid Build Coastguard Worker unsigned TargetRecip::getRefinementSteps(StringRef Key) const {
187*9880d681SAndroid Build Coastguard Worker ConstRecipIter Iter = RecipMap.find(Key);
188*9880d681SAndroid Build Coastguard Worker assert(Iter != RecipMap.end() && "Unknown name for reciprocal map");
189*9880d681SAndroid Build Coastguard Worker assert(Iter->second.RefinementSteps != Uninitialized &&
190*9880d681SAndroid Build Coastguard Worker "Refinement step setting was not initialized");
191*9880d681SAndroid Build Coastguard Worker return Iter->second.RefinementSteps;
192*9880d681SAndroid Build Coastguard Worker }
193*9880d681SAndroid Build Coastguard Worker
194*9880d681SAndroid Build Coastguard Worker /// Custom settings (previously initialized values) override target defaults.
setDefaults(StringRef Key,bool Enable,unsigned RefSteps)195*9880d681SAndroid Build Coastguard Worker void TargetRecip::setDefaults(StringRef Key, bool Enable,
196*9880d681SAndroid Build Coastguard Worker unsigned RefSteps) {
197*9880d681SAndroid Build Coastguard Worker if (Key == "all") {
198*9880d681SAndroid Build Coastguard Worker for (auto &KV : RecipMap) {
199*9880d681SAndroid Build Coastguard Worker RecipParams &RP = KV.second;
200*9880d681SAndroid Build Coastguard Worker if (RP.Enabled == Uninitialized)
201*9880d681SAndroid Build Coastguard Worker RP.Enabled = Enable;
202*9880d681SAndroid Build Coastguard Worker if (RP.RefinementSteps == Uninitialized)
203*9880d681SAndroid Build Coastguard Worker RP.RefinementSteps = RefSteps;
204*9880d681SAndroid Build Coastguard Worker }
205*9880d681SAndroid Build Coastguard Worker } else {
206*9880d681SAndroid Build Coastguard Worker RecipParams &RP = RecipMap[Key];
207*9880d681SAndroid Build Coastguard Worker if (RP.Enabled == Uninitialized)
208*9880d681SAndroid Build Coastguard Worker RP.Enabled = Enable;
209*9880d681SAndroid Build Coastguard Worker if (RP.RefinementSteps == Uninitialized)
210*9880d681SAndroid Build Coastguard Worker RP.RefinementSteps = RefSteps;
211*9880d681SAndroid Build Coastguard Worker }
212*9880d681SAndroid Build Coastguard Worker }
213*9880d681SAndroid Build Coastguard Worker
operator ==(const TargetRecip & Other) const214*9880d681SAndroid Build Coastguard Worker bool TargetRecip::operator==(const TargetRecip &Other) const {
215*9880d681SAndroid Build Coastguard Worker for (const auto &KV : RecipMap) {
216*9880d681SAndroid Build Coastguard Worker StringRef Op = KV.first;
217*9880d681SAndroid Build Coastguard Worker const RecipParams &RP = KV.second;
218*9880d681SAndroid Build Coastguard Worker const RecipParams &OtherRP = Other.RecipMap.find(Op)->second;
219*9880d681SAndroid Build Coastguard Worker if (RP.RefinementSteps != OtherRP.RefinementSteps)
220*9880d681SAndroid Build Coastguard Worker return false;
221*9880d681SAndroid Build Coastguard Worker if (RP.Enabled != OtherRP.Enabled)
222*9880d681SAndroid Build Coastguard Worker return false;
223*9880d681SAndroid Build Coastguard Worker }
224*9880d681SAndroid Build Coastguard Worker return true;
225*9880d681SAndroid Build Coastguard Worker }
226