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