119 lines · cpp
1//===--- CSKYTargetMachine.cpp - Define TargetMachine for CSKY ------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// Implements the info about CSKY target spec.10//11//===----------------------------------------------------------------------===//12 13#include "CSKYTargetMachine.h"14#include "CSKY.h"15#include "CSKYMachineFunctionInfo.h"16#include "CSKYSubtarget.h"17#include "CSKYTargetObjectFile.h"18#include "TargetInfo/CSKYTargetInfo.h"19#include "llvm/CodeGen/MachineFrameInfo.h"20#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"21#include "llvm/CodeGen/TargetPassConfig.h"22#include "llvm/CodeGen/TargetSubtargetInfo.h"23#include "llvm/MC/TargetRegistry.h"24#include <optional>25 26using namespace llvm;27 28extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTarget() {29 RegisterTargetMachine<CSKYTargetMachine> X(getTheCSKYTarget());30 31 PassRegistry *Registry = PassRegistry::getPassRegistry();32 initializeCSKYConstantIslandsPass(*Registry);33 initializeCSKYDAGToDAGISelLegacyPass(*Registry);34}35 36CSKYTargetMachine::CSKYTargetMachine(const Target &T, const Triple &TT,37 StringRef CPU, StringRef FS,38 const TargetOptions &Options,39 std::optional<Reloc::Model> RM,40 std::optional<CodeModel::Model> CM,41 CodeGenOptLevel OL, bool JIT)42 : CodeGenTargetMachineImpl(T, TT.computeDataLayout(), TT, CPU, FS, Options,43 RM.value_or(Reloc::Static),44 getEffectiveCodeModel(CM, CodeModel::Small), OL),45 TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {46 initAsmInfo();47}48 49const CSKYSubtarget *50CSKYTargetMachine::getSubtargetImpl(const Function &F) const {51 Attribute CPUAttr = F.getFnAttribute("target-cpu");52 Attribute TuneAttr = F.getFnAttribute("tune-cpu");53 Attribute FSAttr = F.getFnAttribute("target-features");54 55 std::string CPU =56 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;57 std::string TuneCPU =58 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;59 std::string FS =60 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;61 62 std::string Key = CPU + TuneCPU + FS;63 auto &I = SubtargetMap[Key];64 if (!I) {65 // This needs to be done before we create a new subtarget since any66 // creation will depend on the TM and the code generation flags on the67 // function that reside in TargetOptions.68 resetTargetOptions(F);69 I = std::make_unique<CSKYSubtarget>(TargetTriple, CPU, TuneCPU, FS, *this);70 if (I->useHardFloat() && !I->hasAnyFloatExt())71 errs() << "Hard-float can't be used with current CPU,"72 " set to Soft-float\n";73 }74 return I.get();75}76 77MachineFunctionInfo *CSKYTargetMachine::createMachineFunctionInfo(78 BumpPtrAllocator &Allocator, const Function &F,79 const TargetSubtargetInfo *STI) const {80 return CSKYMachineFunctionInfo::create<CSKYMachineFunctionInfo>(Allocator, F,81 STI);82}83 84namespace {85class CSKYPassConfig : public TargetPassConfig {86public:87 CSKYPassConfig(CSKYTargetMachine &TM, PassManagerBase &PM)88 : TargetPassConfig(TM, PM) {}89 90 CSKYTargetMachine &getCSKYTargetMachine() const {91 return getTM<CSKYTargetMachine>();92 }93 94 void addIRPasses() override;95 bool addInstSelector() override;96 void addPreEmitPass() override;97};98 99} // namespace100 101TargetPassConfig *CSKYTargetMachine::createPassConfig(PassManagerBase &PM) {102 return new CSKYPassConfig(*this, PM);103}104 105void CSKYPassConfig::addIRPasses() {106 addPass(createAtomicExpandLegacyPass());107 TargetPassConfig::addIRPasses();108}109 110bool CSKYPassConfig::addInstSelector() {111 addPass(createCSKYISelDag(getCSKYTargetMachine(), getOptLevel()));112 113 return false;114}115 116void CSKYPassConfig::addPreEmitPass() {117 addPass(createCSKYConstantIslandPass());118}119