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