18264e272SEvan Cheng //===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//
28264e272SEvan Cheng //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68264e272SEvan Cheng //
78264e272SEvan Cheng //===----------------------------------------------------------------------===//
88264e272SEvan Cheng //
905e5fd6bSMatthias Braun /// \file Implements the SubtargetFeature interface.
108264e272SEvan Cheng //
118264e272SEvan Cheng //===----------------------------------------------------------------------===//
128264e272SEvan Cheng 
136bda14b3SChandler Carruth #include "llvm/MC/SubtargetFeature.h"
1444d95122SEugene Zelenko #include "llvm/ADT/SmallVector.h"
155377ad62SBenjamin Kramer #include "llvm/ADT/StringExtras.h"
1644d95122SEugene Zelenko #include "llvm/ADT/StringRef.h"
1744d95122SEugene Zelenko #include "llvm/ADT/Triple.h"
18432a3883SNico Weber #include "llvm/Config/llvm-config.h"
1944d95122SEugene Zelenko #include "llvm/Support/Compiler.h"
208264e272SEvan Cheng #include "llvm/Support/Debug.h"
218264e272SEvan Cheng #include "llvm/Support/raw_ostream.h"
228264e272SEvan Cheng #include <algorithm>
2344d95122SEugene Zelenko #include <string>
2444d95122SEugene Zelenko #include <vector>
2544d95122SEugene Zelenko 
268264e272SEvan Cheng using namespace llvm;
278264e272SEvan Cheng 
2805e5fd6bSMatthias Braun /// Splits a string of comma separated items in to a vector of strings.
Split(std::vector<std::string> & V,StringRef S)2916fc15abSCraig Topper void SubtargetFeatures::Split(std::vector<std::string> &V, StringRef S) {
304fa2fd12SHans Wennborg   SmallVector<StringRef, 3> Tmp;
31e4405e94SChandler Carruth   S.split(Tmp, ',', -1, false /* KeepEmpty */);
32*adcd0268SBenjamin Kramer   V.reserve(Tmp.size());
33*adcd0268SBenjamin Kramer   for (StringRef T : Tmp)
34*adcd0268SBenjamin Kramer     V.push_back(std::string(T));
358264e272SEvan Cheng }
368264e272SEvan Cheng 
AddFeature(StringRef String,bool Enable)37f8cfe1deSCraig Topper void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {
3828f550b4SCraig Topper   // Don't add empty features.
397eba3f90SEric Christopher   if (!String.empty())
407eba3f90SEric Christopher     // Convert to lowercase, prepend flag if we don't already have a flag.
41f8cfe1deSCraig Topper     Features.push_back(hasFlag(String) ? String.lower()
42f8cfe1deSCraig Topper                                        : (Enable ? "+" : "-") + String.lower());
438264e272SEvan Cheng }
448264e272SEvan Cheng 
SubtargetFeatures(StringRef Initial)456dc4a8bcSCraig Topper SubtargetFeatures::SubtargetFeatures(StringRef Initial) {
468264e272SEvan Cheng   // Break up string into separate features
478264e272SEvan Cheng   Split(Features, Initial);
488264e272SEvan Cheng }
498264e272SEvan Cheng 
getString() const50968af4feSRafael Espindola std::string SubtargetFeatures::getString() const {
515377ad62SBenjamin Kramer   return join(Features.begin(), Features.end(), ",");
528264e272SEvan Cheng }
538264e272SEvan Cheng 
print(raw_ostream & OS) const548264e272SEvan Cheng void SubtargetFeatures::print(raw_ostream &OS) const {
559c928478SEric Christopher   for (auto &F : Features)
569c928478SEric Christopher     OS << F << " ";
578264e272SEvan Cheng   OS << "\n";
588264e272SEvan Cheng }
598264e272SEvan Cheng 
60615eb470SAaron Ballman #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const61eb2a2546SYaron Keren LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {
628264e272SEvan Cheng   print(dbgs());
638264e272SEvan Cheng }
648c209aa8SMatthias Braun #endif
658264e272SEvan Cheng 
getDefaultSubtargetFeatures(const Triple & Triple)66fe6e405eSEvan Cheng void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {
6705e5fd6bSMatthias Braun   // FIXME: This is an inelegant way of specifying the features of a
6805e5fd6bSMatthias Braun   // subtarget. It would be better if we could encode this information
6905e5fd6bSMatthias Braun   // into the IR. See <rdar://5972456>.
708264e272SEvan Cheng   if (Triple.getVendor() == Triple::Apple) {
718264e272SEvan Cheng     if (Triple.getArch() == Triple::ppc) {
728264e272SEvan Cheng       // powerpc-apple-*
738264e272SEvan Cheng       AddFeature("altivec");
748264e272SEvan Cheng     } else if (Triple.getArch() == Triple::ppc64) {
758264e272SEvan Cheng       // powerpc64-apple-*
768264e272SEvan Cheng       AddFeature("64bit");
778264e272SEvan Cheng       AddFeature("altivec");
788264e272SEvan Cheng     }
798264e272SEvan Cheng   }
808264e272SEvan Cheng }
81