Path: blob/main/contrib/llvm-project/llvm/lib/Target/CSKY/CSKYTargetMachine.cpp
35266 views
//===--- CSKYTargetMachine.cpp - Define TargetMachine for CSKY ------------===//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// Implements the info about CSKY target spec.9//10//===----------------------------------------------------------------------===//1112#include "CSKYTargetMachine.h"13#include "CSKY.h"14#include "CSKYMachineFunctionInfo.h"15#include "CSKYSubtarget.h"16#include "CSKYTargetObjectFile.h"17#include "TargetInfo/CSKYTargetInfo.h"18#include "llvm/CodeGen/MachineFrameInfo.h"19#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"20#include "llvm/CodeGen/TargetPassConfig.h"21#include "llvm/CodeGen/TargetSubtargetInfo.h"22#include "llvm/MC/TargetRegistry.h"23#include <optional>2425using namespace llvm;2627extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTarget() {28RegisterTargetMachine<CSKYTargetMachine> X(getTheCSKYTarget());2930PassRegistry *Registry = PassRegistry::getPassRegistry();31initializeCSKYConstantIslandsPass(*Registry);32initializeCSKYDAGToDAGISelLegacyPass(*Registry);33}3435static std::string computeDataLayout(const Triple &TT) {36std::string Ret;3738// Only support little endian for now.39// TODO: Add support for big endian.40Ret += "e";4142// CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.43// It's a 4-byte aligned stack with ELF mangling only.44Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"45"-v128:32:32-a:0:32-Fi32-n32";4647return Ret;48}4950CSKYTargetMachine::CSKYTargetMachine(const Target &T, const Triple &TT,51StringRef CPU, StringRef FS,52const TargetOptions &Options,53std::optional<Reloc::Model> RM,54std::optional<CodeModel::Model> CM,55CodeGenOptLevel OL, bool JIT)56: LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,57RM.value_or(Reloc::Static),58getEffectiveCodeModel(CM, CodeModel::Small), OL),59TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {60initAsmInfo();61}6263const CSKYSubtarget *64CSKYTargetMachine::getSubtargetImpl(const Function &F) const {65Attribute CPUAttr = F.getFnAttribute("target-cpu");66Attribute TuneAttr = F.getFnAttribute("tune-cpu");67Attribute FSAttr = F.getFnAttribute("target-features");6869std::string CPU =70CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;71std::string TuneCPU =72TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;73std::string FS =74FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;7576std::string Key = CPU + TuneCPU + FS;77auto &I = SubtargetMap[Key];78if (!I) {79// This needs to be done before we create a new subtarget since any80// creation will depend on the TM and the code generation flags on the81// function that reside in TargetOptions.82resetTargetOptions(F);83I = std::make_unique<CSKYSubtarget>(TargetTriple, CPU, TuneCPU, FS, *this);84if (I->useHardFloat() && !I->hasAnyFloatExt())85errs() << "Hard-float can't be used with current CPU,"86" set to Soft-float\n";87}88return I.get();89}9091MachineFunctionInfo *CSKYTargetMachine::createMachineFunctionInfo(92BumpPtrAllocator &Allocator, const Function &F,93const TargetSubtargetInfo *STI) const {94return CSKYMachineFunctionInfo::create<CSKYMachineFunctionInfo>(Allocator, F,95STI);96}9798namespace {99class CSKYPassConfig : public TargetPassConfig {100public:101CSKYPassConfig(CSKYTargetMachine &TM, PassManagerBase &PM)102: TargetPassConfig(TM, PM) {}103104CSKYTargetMachine &getCSKYTargetMachine() const {105return getTM<CSKYTargetMachine>();106}107108void addIRPasses() override;109bool addInstSelector() override;110void addPreEmitPass() override;111};112113} // namespace114115TargetPassConfig *CSKYTargetMachine::createPassConfig(PassManagerBase &PM) {116return new CSKYPassConfig(*this, PM);117}118119void CSKYPassConfig::addIRPasses() {120addPass(createAtomicExpandLegacyPass());121TargetPassConfig::addIRPasses();122}123124bool CSKYPassConfig::addInstSelector() {125addPass(createCSKYISelDag(getCSKYTargetMachine(), getOptLevel()));126127return false;128}129130void CSKYPassConfig::addPreEmitPass() {131addPass(createCSKYConstantIslandPass());132}133134135