SubtargetFeature.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
  2. //
  3. // 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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file Implements the SubtargetFeature interface.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/MC/SubtargetFeature.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/ADT/Triple.h"
  17. #include "llvm/Config/llvm-config.h"
  18. #include "llvm/Support/Compiler.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <algorithm>
  22. #include <string>
  23. #include <vector>
  24. using namespace llvm;
  25. /// Splits a string of comma separated items in to a vector of strings.
  26. void SubtargetFeatures::Split(std::vector<std::string> &V, StringRef S) {
  27. SmallVector<StringRef, 3> Tmp;
  28. S.split(Tmp, ',', -1, false /* KeepEmpty */);
  29. V.reserve(Tmp.size());
  30. for (StringRef T : Tmp)
  31. V.push_back(std::string(T));
  32. }
  33. void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
  34. // Don't add empty features.
  35. if (!String.empty())
  36. // Convert to lowercase, prepend flag if we don't already have a flag.
  37. Features.push_back(hasFlag(String) ? String.lower()
  38. : (Enable ? "+" : "-") + String.lower());
  39. }
  40. void SubtargetFeatures::addFeaturesVector(
  41. const ArrayRef<std::string> OtherFeatures) {
  42. Features.insert(Features.cend(), OtherFeatures.begin(), OtherFeatures.end());
  43. }
  44. SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
  45. // Break up string into separate features
  46. Split(Features, Initial);
  47. }
  48. std::string SubtargetFeatures::getString() const {
  49. return join(Features.begin(), Features.end(), ",");
  50. }
  51. void SubtargetFeatures::print(raw_ostream &OS) const {
  52. for (const auto &F : Features)
  53. OS << F << " ";
  54. OS << "\n";
  55. }
  56. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  57. LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
  58. print(dbgs());
  59. }
  60. #endif
  61. void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
  62. // FIXME: This is an inelegant way of specifying the features of a
  63. // subtarget. It would be better if we could encode this information
  64. // into the IR. See <rdar://5972456>.
  65. if (Triple.getVendor() == Triple::Apple) {
  66. if (Triple.getArch() == Triple::ppc) {
  67. // powerpc-apple-*
  68. AddFeature("altivec");
  69. } else if (Triple.getArch() == Triple::ppc64) {
  70. // powerpc64-apple-*
  71. AddFeature("64bit");
  72. AddFeature("altivec");
  73. }
  74. }
  75. }