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