xref: /llvm-project/llvm/lib/TargetParser/SubtargetFeature.cpp (revision 4d6d56315d4ea2ae2b8059b99e45bdfee764861a)
18de9f2b5SJob Noorman //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
28de9f2b5SJob Noorman //
38de9f2b5SJob Noorman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48de9f2b5SJob Noorman // See https://llvm.org/LICENSE.txt for license information.
58de9f2b5SJob Noorman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68de9f2b5SJob Noorman //
78de9f2b5SJob Noorman //===----------------------------------------------------------------------===//
88de9f2b5SJob Noorman //
98de9f2b5SJob Noorman /// \file Implements the SubtargetFeature interface.
108de9f2b5SJob Noorman //
118de9f2b5SJob Noorman //===----------------------------------------------------------------------===//
128de9f2b5SJob Noorman 
138de9f2b5SJob Noorman #include "llvm/TargetParser/SubtargetFeature.h"
148de9f2b5SJob Noorman #include "llvm/ADT/SmallVector.h"
158de9f2b5SJob Noorman #include "llvm/ADT/StringExtras.h"
168de9f2b5SJob Noorman #include "llvm/ADT/StringRef.h"
178de9f2b5SJob Noorman #include "llvm/Config/llvm-config.h"
188de9f2b5SJob Noorman #include "llvm/Support/Compiler.h"
198de9f2b5SJob Noorman #include "llvm/Support/Debug.h"
208de9f2b5SJob Noorman #include "llvm/Support/raw_ostream.h"
218de9f2b5SJob Noorman #include "llvm/TargetParser/Triple.h"
228de9f2b5SJob Noorman #include <string>
238de9f2b5SJob Noorman #include <vector>
248de9f2b5SJob Noorman 
258de9f2b5SJob Noorman using namespace llvm;
268de9f2b5SJob Noorman 
278de9f2b5SJob Noorman /// Splits a string of comma separated items in to a vector of strings.
288de9f2b5SJob Noorman void SubtargetFeatures::Split(std::vector<std::string> &V, StringRef S) {
298de9f2b5SJob Noorman   SmallVector<StringRef, 3> Tmp;
308de9f2b5SJob Noorman   S.split(Tmp, ',', -1, false /* KeepEmpty */);
318de9f2b5SJob Noorman   V.reserve(Tmp.size());
328de9f2b5SJob Noorman   for (StringRef T : Tmp)
338de9f2b5SJob Noorman     V.push_back(std::string(T));
348de9f2b5SJob Noorman }
358de9f2b5SJob Noorman 
368de9f2b5SJob Noorman void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
378de9f2b5SJob Noorman   // Don't add empty features.
388de9f2b5SJob Noorman   if (!String.empty())
398de9f2b5SJob Noorman     // Convert to lowercase, prepend flag if we don't already have a flag.
408de9f2b5SJob Noorman     Features.push_back(hasFlag(String) ? String.lower()
418de9f2b5SJob Noorman                                        : (Enable ? "+" : "-") + String.lower());
428de9f2b5SJob Noorman }
438de9f2b5SJob Noorman 
448de9f2b5SJob Noorman void SubtargetFeatures::addFeaturesVector(
458de9f2b5SJob Noorman     const ArrayRef<std::string> OtherFeatures) {
468de9f2b5SJob Noorman   Features.insert(Features.cend(), OtherFeatures.begin(), OtherFeatures.end());
478de9f2b5SJob Noorman }
488de9f2b5SJob Noorman 
498de9f2b5SJob Noorman SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
508de9f2b5SJob Noorman   // Break up string into separate features
518de9f2b5SJob Noorman   Split(Features, Initial);
528de9f2b5SJob Noorman }
538de9f2b5SJob Noorman 
548de9f2b5SJob Noorman std::string SubtargetFeatures::getString() const {
558de9f2b5SJob Noorman   return join(Features.begin(), Features.end(), ",");
568de9f2b5SJob Noorman }
578de9f2b5SJob Noorman 
588de9f2b5SJob Noorman void SubtargetFeatures::print(raw_ostream &OS) const {
598de9f2b5SJob Noorman   for (const auto &F : Features)
608de9f2b5SJob Noorman     OS << F << " ";
618de9f2b5SJob Noorman   OS << "\n";
628de9f2b5SJob Noorman }
638de9f2b5SJob Noorman 
648de9f2b5SJob Noorman #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
658de9f2b5SJob Noorman LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
668de9f2b5SJob Noorman   print(dbgs());
678de9f2b5SJob Noorman }
688de9f2b5SJob Noorman #endif
698de9f2b5SJob Noorman 
708de9f2b5SJob Noorman void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
718de9f2b5SJob Noorman   // FIXME: This is an inelegant way of specifying the features of a
728de9f2b5SJob Noorman   // subtarget. It would be better if we could encode this information
73*6affdbadSJon Roelofs   // into the IR.
748de9f2b5SJob Noorman   if (Triple.getVendor() == Triple::Apple) {
758de9f2b5SJob Noorman     if (Triple.getArch() == Triple::ppc) {
768de9f2b5SJob Noorman       // powerpc-apple-*
778de9f2b5SJob Noorman       AddFeature("altivec");
788de9f2b5SJob Noorman     } else if (Triple.getArch() == Triple::ppc64) {
798de9f2b5SJob Noorman       // powerpc64-apple-*
808de9f2b5SJob Noorman       AddFeature("64bit");
818de9f2b5SJob Noorman       AddFeature("altivec");
828de9f2b5SJob Noorman     }
838de9f2b5SJob Noorman   }
848de9f2b5SJob Noorman }
85