Path: blob/main/contrib/llvm-project/llvm/lib/TargetParser/SubtargetFeature.cpp
35233 views
//===- SubtargetFeature.cpp - CPU characteristics Implementation ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8/// \file Implements the SubtargetFeature interface.9//10//===----------------------------------------------------------------------===//1112#include "llvm/TargetParser/SubtargetFeature.h"13#include "llvm/ADT/SmallVector.h"14#include "llvm/ADT/StringExtras.h"15#include "llvm/ADT/StringRef.h"16#include "llvm/Config/llvm-config.h"17#include "llvm/Support/Compiler.h"18#include "llvm/Support/Debug.h"19#include "llvm/Support/raw_ostream.h"20#include "llvm/TargetParser/Triple.h"21#include <algorithm>22#include <string>23#include <vector>2425using namespace llvm;2627/// Splits a string of comma separated items in to a vector of strings.28void SubtargetFeatures::Split(std::vector<std::string> &V, StringRef S) {29SmallVector<StringRef, 3> Tmp;30S.split(Tmp, ',', -1, false /* KeepEmpty */);31V.reserve(Tmp.size());32for (StringRef T : Tmp)33V.push_back(std::string(T));34}3536void SubtargetFeatures::AddFeature(StringRef String, bool Enable) {37// Don't add empty features.38if (!String.empty())39// Convert to lowercase, prepend flag if we don't already have a flag.40Features.push_back(hasFlag(String) ? String.lower()41: (Enable ? "+" : "-") + String.lower());42}4344void SubtargetFeatures::addFeaturesVector(45const ArrayRef<std::string> OtherFeatures) {46Features.insert(Features.cend(), OtherFeatures.begin(), OtherFeatures.end());47}4849SubtargetFeatures::SubtargetFeatures(StringRef Initial) {50// Break up string into separate features51Split(Features, Initial);52}5354std::string SubtargetFeatures::getString() const {55return join(Features.begin(), Features.end(), ",");56}5758void SubtargetFeatures::print(raw_ostream &OS) const {59for (const auto &F : Features)60OS << F << " ";61OS << "\n";62}6364#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)65LLVM_DUMP_METHOD void SubtargetFeatures::dump() const {66print(dbgs());67}68#endif6970void SubtargetFeatures::getDefaultSubtargetFeatures(const Triple& Triple) {71// FIXME: This is an inelegant way of specifying the features of a72// subtarget. It would be better if we could encode this information73// into the IR.74if (Triple.getVendor() == Triple::Apple) {75if (Triple.getArch() == Triple::ppc) {76// powerpc-apple-*77AddFeature("altivec");78} else if (Triple.getArch() == Triple::ppc64) {79// powerpc64-apple-*80AddFeature("64bit");81AddFeature("altivec");82}83}84}858687