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(CodeModel::Model CM, 88 Optional<Reloc::Model> RM) { 89 if (!RM.hasValue() || CM == CodeModel::JITDefault) 90 return Reloc::Static; 91 return *RM; 92 } 93 94 // On function prologue, the stack is created by decrementing 95 // its pointer. Once decremented, all references are done with positive 96 // offset from the stack/frame pointer, using StackGrowsUp enables 97 // an easier handling. 98 // Using CodeModel::Large enables different CALL behavior. 99 MipsTargetMachine::MipsTargetMachine(const Target &T, const Triple &TT, 100 StringRef CPU, StringRef FS, 101 const TargetOptions &Options, 102 Optional<Reloc::Model> RM, 103 CodeModel::Model CM, CodeGenOpt::Level OL, 104 bool isLittle) 105 : LLVMTargetMachine(T, computeDataLayout(TT, CPU, Options, isLittle), TT, 106 CPU, FS, Options, getEffectiveRelocModel(CM, RM), CM, 107 OL), 108 isLittle(isLittle), TLOF(llvm::make_unique<MipsTargetObjectFile>()), 109 ABI(MipsABIInfo::computeTargetABI(TT, CPU, Options.MCOptions)), 110 Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, *this), 111 NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16", 112 isLittle, *this), 113 Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16", 114 isLittle, *this) { 115 Subtarget = &DefaultSubtarget; 116 initAsmInfo(); 117 } 118 119 MipsTargetMachine::~MipsTargetMachine() = default; 120 121 void MipsebTargetMachine::anchor() {} 122 123 MipsebTargetMachine::MipsebTargetMachine(const Target &T, const Triple &TT, 124 StringRef CPU, StringRef FS, 125 const TargetOptions &Options, 126 Optional<Reloc::Model> RM, 127 CodeModel::Model CM, 128 CodeGenOpt::Level OL) 129 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {} 130 131 void MipselTargetMachine::anchor() {} 132 133 MipselTargetMachine::MipselTargetMachine(const Target &T, const Triple &TT, 134 StringRef CPU, StringRef FS, 135 const TargetOptions &Options, 136 Optional<Reloc::Model> RM, 137 CodeModel::Model CM, 138 CodeGenOpt::Level OL) 139 : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {} 140 141 const MipsSubtarget * 142 MipsTargetMachine::getSubtargetImpl(const Function &F) const { 143 Attribute CPUAttr = F.getFnAttribute("target-cpu"); 144 Attribute FSAttr = F.getFnAttribute("target-features"); 145 146 std::string CPU = !CPUAttr.hasAttribute(Attribute::None) 147 ? CPUAttr.getValueAsString().str() 148 : TargetCPU; 149 std::string FS = !FSAttr.hasAttribute(Attribute::None) 150 ? FSAttr.getValueAsString().str() 151 : TargetFS; 152 bool hasMips16Attr = 153 !F.getFnAttribute("mips16").hasAttribute(Attribute::None); 154 bool hasNoMips16Attr = 155 !F.getFnAttribute("nomips16").hasAttribute(Attribute::None); 156 157 bool HasMicroMipsAttr = 158 !F.getFnAttribute("micromips").hasAttribute(Attribute::None); 159 bool HasNoMicroMipsAttr = 160 !F.getFnAttribute("nomicromips").hasAttribute(Attribute::None); 161 162 // FIXME: This is related to the code below to reset the target options, 163 // we need to know whether or not the soft float flag is set on the 164 // function, so we can enable it as a subtarget feature. 165 bool softFloat = 166 F.hasFnAttribute("use-soft-float") && 167 F.getFnAttribute("use-soft-float").getValueAsString() == "true"; 168 169 if (hasMips16Attr) 170 FS += FS.empty() ? "+mips16" : ",+mips16"; 171 else if (hasNoMips16Attr) 172 FS += FS.empty() ? "-mips16" : ",-mips16"; 173 if (HasMicroMipsAttr) 174 FS += FS.empty() ? "+micromips" : ",+micromips"; 175 else if (HasNoMicroMipsAttr) 176 FS += FS.empty() ? "-micromips" : ",-micromips"; 177 if (softFloat) 178 FS += FS.empty() ? "+soft-float" : ",+soft-float"; 179 180 auto &I = SubtargetMap[CPU + FS]; 181 if (!I) { 182 // This needs to be done before we create a new subtarget since any 183 // creation will depend on the TM and the code generation flags on the 184 // function that reside in TargetOptions. 185 resetTargetOptions(F); 186 I = llvm::make_unique<MipsSubtarget>(TargetTriple, CPU, FS, isLittle, 187 *this); 188 } 189 return I.get(); 190 } 191 192 void MipsTargetMachine::resetSubtarget(MachineFunction *MF) { 193 DEBUG(dbgs() << "resetSubtarget\n"); 194 195 Subtarget = const_cast<MipsSubtarget *>(getSubtargetImpl(*MF->getFunction())); 196 MF->setSubtarget(Subtarget); 197 } 198 199 namespace { 200 201 /// Mips Code Generator Pass Configuration Options. 202 class MipsPassConfig : public TargetPassConfig { 203 public: 204 MipsPassConfig(MipsTargetMachine &TM, PassManagerBase &PM) 205 : TargetPassConfig(TM, PM) { 206 // The current implementation of long branch pass requires a scratch 207 // register ($at) to be available before branch instructions. Tail merging 208 // can break this requirement, so disable it when long branch pass is 209 // enabled. 210 EnableTailMerge = !getMipsSubtarget().enableLongBranchPass(); 211 } 212 213 MipsTargetMachine &getMipsTargetMachine() const { 214 return getTM<MipsTargetMachine>(); 215 } 216 217 const MipsSubtarget &getMipsSubtarget() const { 218 return *getMipsTargetMachine().getSubtargetImpl(); 219 } 220 221 void addIRPasses() override; 222 bool addInstSelector() override; 223 void addPreEmitPass() override; 224 void addPreRegAlloc() override; 225 }; 226 227 } // end anonymous namespace 228 229 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) { 230 return new MipsPassConfig(*this, PM); 231 } 232 233 void MipsPassConfig::addIRPasses() { 234 TargetPassConfig::addIRPasses(); 235 addPass(createAtomicExpandPass()); 236 if (getMipsSubtarget().os16()) 237 addPass(createMipsOs16Pass()); 238 if (getMipsSubtarget().inMips16HardFloat()) 239 addPass(createMips16HardFloatPass()); 240 } 241 // Install an instruction selector pass using 242 // the ISelDag to gen Mips code. 243 bool MipsPassConfig::addInstSelector() { 244 addPass(createMipsModuleISelDagPass()); 245 addPass(createMips16ISelDag(getMipsTargetMachine(), getOptLevel())); 246 addPass(createMipsSEISelDag(getMipsTargetMachine(), getOptLevel())); 247 return false; 248 } 249 250 void MipsPassConfig::addPreRegAlloc() { 251 addPass(createMipsOptimizePICCallPass()); 252 } 253 254 TargetIRAnalysis MipsTargetMachine::getTargetIRAnalysis() { 255 return TargetIRAnalysis([this](const Function &F) { 256 if (Subtarget->allowMixed16_32()) { 257 DEBUG(errs() << "No Target Transform Info Pass Added\n"); 258 // FIXME: This is no longer necessary as the TTI returned is per-function. 259 return TargetTransformInfo(F.getParent()->getDataLayout()); 260 } 261 262 DEBUG(errs() << "Target Transform Info Pass Added\n"); 263 return TargetTransformInfo(BasicTTIImpl(this, F)); 264 }); 265 } 266 267 // Implemented by targets that want to run passes immediately before 268 // machine code is emitted. return true if -print-machineinstrs should 269 // print out the code after the passes. 270 void MipsPassConfig::addPreEmitPass() { 271 addPass(createMicroMipsSizeReductionPass()); 272 273 // The delay slot filler pass can potientially create forbidden slot (FS) 274 // hazards for MIPSR6 which the hazard schedule pass (HSP) will fix. Any 275 // (new) pass that creates compact branches after the HSP must handle FS 276 // hazards itself or be pipelined before the HSP. 277 addPass(createMipsDelaySlotFillerPass()); 278 addPass(createMipsHazardSchedule()); 279 addPass(createMipsLongBranchPass()); 280 addPass(createMipsConstantIslandPass()); 281 } 282