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