SubtargetFeature.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <cassert>
  23. #include <cstddef>
  24. #include <cstring>
  25. #include <iterator>
  26. #include <string>
  27. #include <vector>
  28. using namespace llvm;
  29. /// Splits a string of comma separated items in to a vector of strings.
  30. void SubtargetFeatures::Split(std::vector<std::string> &V, StringRef S) {
  31. SmallVector<StringRef, 3> Tmp;
  32. S.split(Tmp, ',', -1, false /* KeepEmpty */);
  33. V.reserve(Tmp.size());
  34. for (StringRef T : Tmp)
  35. V.push_back(std::string(T));
  36. }
  37. void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
  38. // Don't add empty features.
  39. if (!String.empty())
  40. // Convert to lowercase, prepend flag if we don't already have a flag.
  41. Features.push_back(hasFlag(String) ? String.lower()
  42. : (Enable ? "+" : "-") + String.lower());
  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 (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. }