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 
13 #include "llvm/MC/SubtargetFeature.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Config/llvm-config.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <algorithm>
23 #include <string>
24 #include <vector>
25 
26 using namespace llvm;
27 
28 /// Splits a string of comma separated items in to a vector of strings.
Split(std::vector<std::string> & V,StringRef S)29 void SubtargetFeatures::Split(std::vector<std::string> &V, StringRef S) {
30   SmallVector<StringRef, 3> Tmp;
31   S.split(Tmp, ',', -1, false /* KeepEmpty */);
32   V.reserve(Tmp.size());
33   for (StringRef T : Tmp)
34     V.push_back(std::string(T));
35 }
36 
AddFeature(StringRef String,bool Enable)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(StringRef Initial)45 SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
46   // Break up string into separate features
47   Split(Features, Initial);
48 }
49 
getString() const50 std::string SubtargetFeatures::getString() const {
51   return join(Features.begin(), Features.end(), ",");
52 }
53 
print(raw_ostream & OS) const54 void SubtargetFeatures::print(raw_ostream &OS) const {
55   for (auto &F : Features)
56     OS << F << " ";
57   OS << "\n";
58 }
59 
60 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const61 LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
62   print(dbgs());
63 }
64 #endif
65 
getDefaultSubtargetFeatures(const Triple & Triple)66 void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
67   // FIXME: This is an inelegant way of specifying the features of a
68   // subtarget. It would be better if we could encode this information
69   // into the IR. See <rdar://5972456>.
70   if (Triple.getVendor() == Triple::Apple) {
71     if (Triple.getArch() == Triple::ppc) {
72       // powerpc-apple-*
73       AddFeature("altivec");
74     } else if (Triple.getArch() == Triple::ppc64) {
75       // powerpc64-apple-*
76       AddFeature("64bit");
77       AddFeature("altivec");
78     }
79   }
80 }
81