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