TargetRecip.cpp 7.2 KB

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