1 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Implements the info about Mips target spec. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "MipsTargetMachine.h" 15 #include "MCTargetDesc/MipsABIInfo.h" 16 #include "MCTargetDesc/MipsMCTargetDesc.h" 17 #include "Mips.h" 18 #include "Mips16ISelDAGToDAG.h" 19 #include "MipsSEISelDAGToDAG.h" 20 #include "MipsSubtarget.h" 21 #include "MipsTargetObjectFile.h" 22 #include "llvm/ADT/Optional.h" 23 #include "llvm/ADT/STLExtras.h" 24 #include "llvm/ADT/StringRef.h" 25 #include "llvm/Analysis/TargetTransformInfo.h" 26 #include "llvm/CodeGen/BasicTTIImpl.h" 27 #include "llvm/CodeGen/MachineFunction.h" 28 #include "llvm/CodeGen/Passes.h" 29 #include "llvm/CodeGen/TargetPassConfig.h" 30 #include "llvm/IR/Attributes.h" 31 #include "llvm/IR/Function.h" 32 #include "llvm/Support/CodeGen.h" 33 #include "llvm/Support/Debug.h" 34 #include "llvm/Support/TargetRegistry.h" 35 #include "llvm/Support/raw_ostream.h" 36 #include "llvm/Target/TargetOptions.h" 37 #include <string> 38 39 using namespace llvm; 40 41 #define DEBUG_TYPE "mips" 42 43 extern "C" void LLVMInitializeMipsTarget() { 44 // Register the target. 45 RegisterTargetMachine<MipsebTargetMachine> X(getTheMipsTarget()); 46 RegisterTargetMachine<MipselTargetMachine> Y(getTheMipselTarget()); 47 RegisterTargetMachine<MipsebTargetMachine> A(getTheMips64Target()); 48 RegisterTargetMachine<MipselTargetMachine> B(getTheMips64elTarget()); 49 } 50 51 static std::string computeDataLayout(const Triple &TT, StringRef CPU, 52 const TargetOptions &Options, 53 bool isLittle) { 54 std::string Ret; 55 MipsABIInfo ABI = MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions); 56 57 // There are both little and big endian mips. 58 if (isLittle) 59 Ret += "e"; 60 else 61 Ret += "E"; 62 63 if (ABI.IsO32()) 64 Ret += "-m:m"; 65 else 66 Ret += "-m:e"; 67 68 // Pointers are 32 bit on some ABIs. 69 if (!ABI.IsN64()) 70 Ret += "-p:32:32"; 71 72 // 8 and 16 bit integers only need to have natural alignment, but try to 73 // align them to 32 bits. 64 bit integers have natural alignment. 74 Ret += "-i8:8:32-i16:16:32-i64:64"; 75 76 // 32 bit registers are always available and the stack is at least 64 bit 77 // aligned. On N64 64 bit registers are also available and the stack is 78 // 128 bit aligned. 79 if (ABI.IsN64() || ABI.IsN32()) 80 Ret += "-n32:64-S128"; 81 else 82 Ret += "-n32-S64"; 83 84 return Ret; 85 } 86 87 static Reloc::Model getEffectiveRelocModel(bool JIT, 88 Optional<Reloc::Model> RM) { 89 if (!RM.hasValue() || JIT) 90 return Reloc::Static; 91 return *RM; 92 } 93 94 static CodeModel::Model getEffectiveCodeModel(Optional<CodeModel::Model> CM) { 95 if (CM) 96 return *CM; 97 return CodeModel::Small; 98 } 99 100 // On function prologue, the stack is created by decrementing 101 // its pointer. Once decremented, all references are done with positive 102 // offset from the stack/frame pointer, using StackGrowsUp enables 103 // an easier handling. 104 // Using CodeModel::Large enables different CALL behavior. 105 MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT, 106 StringRef CPU, StringRef FS, 107 const TargetOptions &Options, 108 Optional<Reloc::Model> RM, 109 Optional<CodeModel::Model> CM, 110 CodeGenOpt::Level OL, bool JIT, 111 bool isLittle) 112 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT, 113 CPU, FS, Options, getEffectiveRelocModel(JIT, RM), 114 getEffectiveCodeModel(CM), OL), 115 isLittle(isLittle), TLOF(llvm::make_unique<MipsTargetObjectFile>()), 116 ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)), 117 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this), 118 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16", 119 isLittle, *this), 120 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16", 121 isLittle, *this) { 122 Subtarget = &DefaultSubtarget; 123 initAsmInfo(); 124 } 125 126 MipsTargetMachine::~MipsTargetMachine() = default; 127 128 void MipsebTargetMachine::anchor() {} 129 130 MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT, 131 StringRef CPU, StringRef FS, 132 const TargetOptions &Options, 133 Optional<Reloc::Model> RM, 134 Optional<CodeModel::Model> CM, 135 CodeGenOpt::Level OL, bool JIT) 136 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, false) {} 137 138 void MipselTargetMachine::anchor() {} 139 140 MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT, 141 StringRef CPU, StringRef FS, 142 const TargetOptions &Options, 143 Optional<Reloc::Model> RM, 144 Optional<CodeModel::Model> CM, 145 CodeGenOpt::Level OL, bool JIT) 146 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, JIT, true) {} 147 148 const MipsSubtarget * 149 MipsTargetMachine::getSubtargetImpl(const Function &F) const { 150 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 151 Attribute FSAttr = F.getFnAttribute("target-features"); 152 153 std::string CPU = !CPUAttr.hasAttribute(Attribute::None) 154 ? CPUAttr.getValueAsString().str() 155 : TargetCPU; 156 std::string FS = !FSAttr.hasAttribute(Attribute::None) 157 ? FSAttr.getValueAsString().str() 158 : TargetFS; 159 bool hasMips16Attr = 160 !F.getFnAttribute("mips16").hasAttribute(Attribute::None); 161 bool hasNoMips16Attr = 162 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None); 163 164 bool HasMicroMipsAttr = 165 !F.getFnAttribute("micromips").hasAttribute(Attribute::None); 166 bool HasNoMicroMipsAttr = 167 !F.getFnAttribute("nomicromips").hasAttribute(Attribute::None); 168 169 // FIXME: This is related to the code below to reset the target options, 170 // we need to know whether or not the soft float flag is set on the 171 // function, so we can enable it as a subtarget feature. 172 bool softFloat = 173 F.hasFnAttribute("use-soft-float") && 174 F.getFnAttribute("use-soft-float").getValueAsString() == "true"; 175 176 if (hasMips16Attr) 177 FS += FS.empty() ? "+mips16" : ",+mips16"; 178 else if (hasNoMips16Attr) 179 FS += FS.empty() ? "-mips16" : ",-mips16"; 180 if (HasMicroMipsAttr) 181 FS += FS.empty() ? "+micromips" : ",+micromips"; 182 else if (HasNoMicroMipsAttr) 183 FS += FS.empty() ? "-micromips" : ",-micromips"; 184 if (softFloat) 185 FS += FS.empty() ? "+soft-float" : ",+soft-float"; 186 187 auto &I = SubtargetMap[CPU + FS]; 188 if (!I) { 189 // This needs to be done before we create a new subtarget since any 190 // creation will depend on the TM and the code generation flags on the 191 // function that reside in TargetOptions. 192 resetTargetOptions(F); 193 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, 194 *this); 195 } 196 return I.get(); 197 } 198 199 void MipsTargetMachine::resetSubtarget(MachineFunction *MF) { 200 DEBUG(dbgs() << "resetSubtarget\n"); 201 202 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction())); 203 MF->setSubtarget(Subtarget); 204 } 205 206 namespace { 207 208 /// Mips Code Generator Pass Configuration Options. 209 class MipsPassConfig : public TargetPassConfig { 210 public: 211 MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM) 212 : TargetPassConfig(TM, PM) { 213 // The current implementation of long branch pass requires a scratch 214 // register ($at) to be available before branch instructions. Tail merging 215 // can break this requirement, so disable it when long branch pass is 216 // enabled. 217 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass(); 218 } 219 220 MipsTargetMachine &getMipsTargetMachine() const { 221 return getTM<MipsTargetMachine>(); 222 } 223 224 const MipsSubtarget &getMipsSubtarget() const { 225 return *getMipsTargetMachine().getSubtargetImpl(); 226 } 227 228 void addIRPasses() override; 229 bool addInstSelector() override; 230 void addPreEmitPass() override; 231 void addPreRegAlloc() override; 232 }; 233 234 } // end anonymous namespace 235 236 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) { 237 return new MipsPassConfig(*this, PM); 238 } 239 240 void MipsPassConfig::addIRPasses() { 241 TargetPassConfig::addIRPasses(); 242 addPass(createAtomicExpandPass()); 243 if (getMipsSubtarget().os16()) 244 addPass(createMipsOs16Pass()); 245 if (getMipsSubtarget().inMips16HardFloat()) 246 addPass(createMips16HardFloatPass()); 247 } 248 // Install an instruction selector pass using 249 // the ISelDag to gen Mips code. 250 bool MipsPassConfig::addInstSelector() { 251 addPass(createMipsModuleISelDagPass()); 252 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel())); 253 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel())); 254 return false; 255 } 256 257 void MipsPassConfig::addPreRegAlloc() { 258 addPass(createMipsOptimizePICCallPass()); 259 } 260 261 TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() { 262 return TargetIRAnalysis([this](const Function &F) { 263 if (Subtarget->allowMixed16_32()) { 264 DEBUG(errs() << "No Target Transform Info Pass Added\n"); 265 // FIXME: This is no longer necessary as the TTI returned is per-function. 266 return TargetTransformInfo(F.getParent()->getDataLayout()); 267 } 268 269 DEBUG(errs() << "Target Transform Info Pass Added\n"); 270 return TargetTransformInfo(BasicTTIImpl(this, F)); 271 }); 272 } 273 274 // Implemented by targets that want to run passes immediately before 275 // machine code is emitted. return true if -print-machineinstrs should 276 // print out the code after the passes. 277 void MipsPassConfig::addPreEmitPass() { 278 addPass(createMicroMipsSizeReductionPass()); 279 280 // The delay slot filler pass can potientially create forbidden slot (FS) 281 // hazards for MIPSR6 which the hazard schedule pass (HSP) will fix. Any 282 // (new) pass that creates compact branches after the HSP must handle FS 283 // hazards itself or be pipelined before the HSP. 284 addPass(createMipsDelaySlotFillerPass()); 285 addPass(createMipsHazardSchedule()); 286 addPass(createMipsLongBranchPass()); 287 addPass(createMipsConstantIslandPass()); 288 } 289