1 //===--- CSKYTargetMachine.cpp - Define TargetMachine for CSKY ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Implements the info about CSKY target spec.
11 //===----------------------------------------------------------------------===//
13 #include "CSKYTargetMachine.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"
28 extern "C" LLVM_EXTERNAL_VISIBILITY
void LLVMInitializeCSKYTarget() {
29 RegisterTargetMachine
<CSKYTargetMachine
> X(getTheCSKYTarget());
31 PassRegistry
*Registry
= PassRegistry::getPassRegistry();
32 initializeCSKYConstantIslandsPass(*Registry
);
33 initializeCSKYDAGToDAGISelLegacyPass(*Registry
);
36 static std::string
computeDataLayout(const Triple
&TT
) {
39 // Only support little endian for now.
40 // TODO: Add support for big endian.
43 // CSKY is always 32-bit target with the CSKYv2 ABI as prefer now.
44 // It's a 4-byte aligned stack with ELF mangling only.
45 Ret
+= "-m:e-S32-p:32:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:32:32"
46 "-v128:32:32-a:0:32-Fi32-n32";
51 CSKYTargetMachine::CSKYTargetMachine(const Target
&T
, const Triple
&TT
,
52 StringRef CPU
, StringRef FS
,
53 const TargetOptions
&Options
,
54 std::optional
<Reloc::Model
> RM
,
55 std::optional
<CodeModel::Model
> CM
,
56 CodeGenOptLevel OL
, bool JIT
)
57 : CodeGenTargetMachineImpl(T
, computeDataLayout(TT
), TT
, CPU
, FS
, Options
,
58 RM
.value_or(Reloc::Static
),
59 getEffectiveCodeModel(CM
, CodeModel::Small
), OL
),
60 TLOF(std::make_unique
<CSKYELFTargetObjectFile
>()) {
65 CSKYTargetMachine::getSubtargetImpl(const Function
&F
) const {
66 Attribute CPUAttr
= F
.getFnAttribute("target-cpu");
67 Attribute TuneAttr
= F
.getFnAttribute("tune-cpu");
68 Attribute FSAttr
= F
.getFnAttribute("target-features");
71 CPUAttr
.isValid() ? CPUAttr
.getValueAsString().str() : TargetCPU
;
73 TuneAttr
.isValid() ? TuneAttr
.getValueAsString().str() : CPU
;
75 FSAttr
.isValid() ? FSAttr
.getValueAsString().str() : TargetFS
;
77 std::string Key
= CPU
+ TuneCPU
+ FS
;
78 auto &I
= SubtargetMap
[Key
];
80 // This needs to be done before we create a new subtarget since any
81 // creation will depend on the TM and the code generation flags on the
82 // function that reside in TargetOptions.
83 resetTargetOptions(F
);
84 I
= std::make_unique
<CSKYSubtarget
>(TargetTriple
, CPU
, TuneCPU
, FS
, *this);
85 if (I
->useHardFloat() && !I
->hasAnyFloatExt())
86 errs() << "Hard-float can't be used with current CPU,"
87 " set to Soft-float\n";
92 MachineFunctionInfo
*CSKYTargetMachine::createMachineFunctionInfo(
93 BumpPtrAllocator
&Allocator
, const Function
&F
,
94 const TargetSubtargetInfo
*STI
) const {
95 return CSKYMachineFunctionInfo::create
<CSKYMachineFunctionInfo
>(Allocator
, F
,
100 class CSKYPassConfig
: public TargetPassConfig
{
102 CSKYPassConfig(CSKYTargetMachine
&TM
, PassManagerBase
&PM
)
103 : TargetPassConfig(TM
, PM
) {}
105 CSKYTargetMachine
&getCSKYTargetMachine() const {
106 return getTM
<CSKYTargetMachine
>();
109 void addIRPasses() override
;
110 bool addInstSelector() override
;
111 void addPreEmitPass() override
;
116 TargetPassConfig
*CSKYTargetMachine::createPassConfig(PassManagerBase
&PM
) {
117 return new CSKYPassConfig(*this, PM
);
120 void CSKYPassConfig::addIRPasses() {
121 addPass(createAtomicExpandLegacyPass());
122 TargetPassConfig::addIRPasses();
125 bool CSKYPassConfig::addInstSelector() {
126 addPass(createCSKYISelDag(getCSKYTargetMachine(), getOptLevel()));
131 void CSKYPassConfig::addPreEmitPass() {
132 addPass(createCSKYConstantIslandPass());