Path: blob/main/contrib/llvm-project/llvm/lib/Target/Xtensa/XtensaTargetMachine.cpp
35271 views
//===- XtensaTargetMachine.cpp - Define TargetMachine for Xtensa ----------===//1//2// The LLVM Compiler Infrastructure3//4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5// See https://llvm.org/LICENSE.txt for license information.6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7//8//===----------------------------------------------------------------------===//9//10// Implements the info about Xtensa target spec.11//12//===----------------------------------------------------------------------===//1314#include "XtensaTargetMachine.h"15#include "TargetInfo/XtensaTargetInfo.h"16#include "llvm/CodeGen/Passes.h"17#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"18#include "llvm/CodeGen/TargetPassConfig.h"19#include "llvm/MC/TargetRegistry.h"20#include "llvm/Transforms/Scalar.h"21#include <optional>2223using namespace llvm;2425extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeXtensaTarget() {26// Register the target.27RegisterTargetMachine<XtensaTargetMachine> A(getTheXtensaTarget());28}2930static std::string computeDataLayout(const Triple &TT, StringRef CPU,31const TargetOptions &Options,32bool IsLittle) {33std::string Ret = "e-m:e-p:32:32-i8:8:32-i16:16:32-i64:64-n32";34return Ret;35}3637static Reloc::Model getEffectiveRelocModel(bool JIT,38std::optional<Reloc::Model> RM) {39if (!RM || JIT)40return Reloc::Static;41return *RM;42}4344XtensaTargetMachine::XtensaTargetMachine(const Target &T, const Triple &TT,45StringRef CPU, StringRef FS,46const TargetOptions &Options,47std::optional<Reloc::Model> RM,48std::optional<CodeModel::Model> CM,49CodeGenOptLevel OL, bool JIT,50bool IsLittle)51: LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, IsLittle), TT,52CPU, FS, Options, getEffectiveRelocModel(JIT, RM),53getEffectiveCodeModel(CM, CodeModel::Small), OL),54TLOF(std::make_unique<TargetLoweringObjectFileELF>()) {55initAsmInfo();56}5758XtensaTargetMachine::XtensaTargetMachine(const Target &T, const Triple &TT,59StringRef CPU, StringRef FS,60const TargetOptions &Options,61std::optional<Reloc::Model> RM,62std::optional<CodeModel::Model> CM,63CodeGenOptLevel OL, bool JIT)64: XtensaTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {}6566const XtensaSubtarget *67XtensaTargetMachine::getSubtargetImpl(const Function &F) const {68Attribute CPUAttr = F.getFnAttribute("target-cpu");69Attribute FSAttr = F.getFnAttribute("target-features");7071auto CPU = CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;72auto FS = FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;7374auto &I = SubtargetMap[CPU + FS];75if (!I) {76// This needs to be done before we create a new subtarget since any77// creation will depend on the TM and the code generation flags on the78// function that reside in TargetOptions.79resetTargetOptions(F);80I = std::make_unique<XtensaSubtarget>(TargetTriple, CPU, FS, *this);81}82return I.get();83}8485namespace {86/// Xtensa Code Generator Pass Configuration Options.87class XtensaPassConfig : public TargetPassConfig {88public:89XtensaPassConfig(XtensaTargetMachine &TM, PassManagerBase &PM)90: TargetPassConfig(TM, PM) {}9192XtensaTargetMachine &getXtensaTargetMachine() const {93return getTM<XtensaTargetMachine>();94}9596bool addInstSelector() override;97};98} // end anonymous namespace99100bool XtensaPassConfig::addInstSelector() {101addPass(createXtensaISelDag(getXtensaTargetMachine(), getOptLevel()));102return false;103}104105TargetPassConfig *XtensaTargetMachine::createPassConfig(PassManagerBase &PM) {106return new XtensaPassConfig(*this, PM);107}108109110