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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Implements the info about CSKY target spec.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "CSKYTargetMachine.h"
14 #include "CSKY.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
24 using namespace llvm;
25
LLVMInitializeCSKYTarget()26 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeCSKYTarget() {
27 RegisterTargetMachine<CSKYTargetMachine> X(getTheCSKYTarget());
28
29 PassRegistry *Registry = PassRegistry::getPassRegistry();
30 initializeCSKYConstantIslandsPass(*Registry);
31 }
32
computeDataLayout(const Triple & TT)33 static std::string computeDataLayout(const Triple &TT) {
34 std::string Ret;
35
36 // Only support little endian for now.
37 // TODO: Add support for big endian.
38 Ret += "e";
39
40 // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
41 // It's a 4-byte aligned stack with ELF mangling only.
42 Ret += "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
43 "-v128:32:32-a:0:32-Fi32-n32";
44
45 return Ret;
46 }
47
CSKYTargetMachine(const Target & T,const Triple & TT,StringRef CPU,StringRef FS,const TargetOptions & Options,Optional<Reloc::Model> RM,Optional<CodeModel::Model> CM,CodeGenOpt::Level OL,bool JIT)48 CSKYTargetMachine::CSKYTargetMachine(const Target &T, const Triple &TT,
49 StringRef CPU, StringRef FS,
50 const TargetOptions &Options,
51 Optional<Reloc::Model> RM,
52 Optional<CodeModel::Model> CM,
53 CodeGenOpt::Level OL, bool JIT)
54 : LLVMTargetMachine(T, computeDataLayout(TT), TT, CPU, FS, Options,
55 RM.value_or(Reloc::Static),
56 getEffectiveCodeModel(CM, CodeModel::Small), OL),
57 TLOF(std::make_unique<CSKYELFTargetObjectFile>()) {
58 initAsmInfo();
59 }
60
61 const CSKYSubtarget *
getSubtargetImpl(const Function & F) const62 CSKYTargetMachine::getSubtargetImpl(const Function &F) const {
63 Attribute CPUAttr = F.getFnAttribute("target-cpu");
64 Attribute TuneAttr = F.getFnAttribute("tune-cpu");
65 Attribute FSAttr = F.getFnAttribute("target-features");
66
67 std::string CPU =
68 CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
69 std::string TuneCPU =
70 TuneAttr.isValid() ? TuneAttr.getValueAsString().str() : CPU;
71 std::string FS =
72 FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
73
74 std::string Key = CPU + TuneCPU + FS;
75 auto &I = SubtargetMap[Key];
76 if (!I) {
77 // This needs to be done before we create a new subtarget since any
78 // creation will depend on the TM and the code generation flags on the
79 // function that reside in TargetOptions.
80 resetTargetOptions(F);
81 I = std::make_unique<CSKYSubtarget>(TargetTriple, CPU, TuneCPU, FS, *this);
82 if (I->useHardFloat() && !I->hasAnyFloatExt())
83 errs() << "Hard-float can't be used with current CPU,"
84 " set to Soft-float\n";
85 }
86 return I.get();
87 }
88
89 namespace {
90 class CSKYPassConfig : public TargetPassConfig {
91 public:
CSKYPassConfig(CSKYTargetMachine & TM,PassManagerBase & PM)92 CSKYPassConfig(CSKYTargetMachine &TM, PassManagerBase &PM)
93 : TargetPassConfig(TM, PM) {}
94
getCSKYTargetMachine() const95 CSKYTargetMachine &getCSKYTargetMachine() const {
96 return getTM<CSKYTargetMachine>();
97 }
98
99 void addIRPasses() override;
100 bool addInstSelector() override;
101 void addPreEmitPass() override;
102 };
103
104 } // namespace
105
createPassConfig(PassManagerBase & PM)106 TargetPassConfig *CSKYTargetMachine::createPassConfig(PassManagerBase &PM) {
107 return new CSKYPassConfig(*this, PM);
108 }
109
addIRPasses()110 void CSKYPassConfig::addIRPasses() {
111 addPass(createAtomicExpandPass());
112 TargetPassConfig::addIRPasses();
113 }
114
addInstSelector()115 bool CSKYPassConfig::addInstSelector() {
116 addPass(createCSKYISelDag(getCSKYTargetMachine()));
117
118 return false;
119 }
120
addPreEmitPass()121 void CSKYPassConfig::addPreEmitPass() {
122 addPass(createCSKYConstantIslandPass());
123 }
124