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/TargetParser/SubtargetFeature.h"
14#include "llvm/ADT/SmallVector.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/ADT/StringRef.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 "llvm/TargetParser/Triple.h"
22#include <algorithm>
23#include <string>
24#include <vector>
25
26using namespace llvm;
27
28/// Splits a string of comma separated items in to a vector of strings.
29void SubtargetFeatures::Split(std::vector<std::string> &V, StringRef S) {
30 SmallVector<StringRef, 3> Tmp;
31 S.split(A&: Tmp, Separator: ',', MaxSplit: -1, KeepEmpty: false /* KeepEmpty */);
32 V.reserve(n: Tmp.size());
33 for (StringRef T : Tmp)
34 V.push_back(x: std::string(T));
35}
36
37void 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(x: hasFlag(Feature: String) ? String.lower()
42 : (Enable ? "+" : "-") + String.lower());
43}
44
45void SubtargetFeatures::addFeaturesVector(
46 const ArrayRef<std::string> OtherFeatures) {
47 Features.insert(position: Features.cend(), first: OtherFeatures.begin(), last: OtherFeatures.end());
48}
49
50SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
51 // Break up string into separate features
52 Split(V&: Features, S: Initial);
53}
54
55std::string SubtargetFeatures::getString() const {
56 return join(Begin: Features.begin(), End: Features.end(), Separator: ",");
57}
58
59void SubtargetFeatures::print(raw_ostream &OS) const {
60 for (const auto &F : Features)
61 OS << F << " ";
62 OS << "\n";
63}
64
65#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
66LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
67 print(dbgs());
68}
69#endif
70
71void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
72 // FIXME: This is an inelegant way of specifying the features of a
73 // subtarget. It would be better if we could encode this information
74 // into the IR.
75 if (Triple.getVendor() == Triple::Apple) {
76 if (Triple.getArch() == Triple::ppc) {
77 // powerpc-apple-*
78 AddFeature(String: "altivec");
79 } else if (Triple.getArch() == Triple::ppc64) {
80 // powerpc64-apple-*
81 AddFeature(String: "64bit");
82 AddFeature(String: "altivec");
83 }
84 }
85}
86