LLVM API Documentation

MipsTargetMachine.cpp
Go to the documentation of this file.
00001 //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Implements the info about Mips target spec.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "MipsTargetMachine.h"
00015 #include "Mips.h"
00016 #include "Mips16FrameLowering.h"
00017 #include "Mips16HardFloat.h"
00018 #include "Mips16ISelDAGToDAG.h"
00019 #include "Mips16ISelLowering.h"
00020 #include "Mips16InstrInfo.h"
00021 #include "MipsFrameLowering.h"
00022 #include "MipsInstrInfo.h"
00023 #include "MipsModuleISelDAGToDAG.h"
00024 #include "MipsOs16.h"
00025 #include "MipsSEFrameLowering.h"
00026 #include "MipsSEISelDAGToDAG.h"
00027 #include "MipsSEISelLowering.h"
00028 #include "MipsSEInstrInfo.h"
00029 #include "llvm/Analysis/TargetTransformInfo.h"
00030 #include "llvm/CodeGen/Passes.h"
00031 #include "llvm/PassManager.h"
00032 #include "llvm/Support/Debug.h"
00033 #include "llvm/Support/TargetRegistry.h"
00034 #include "llvm/Support/raw_ostream.h"
00035 #include "llvm/Transforms/Scalar.h"
00036 using namespace llvm;
00037 
00038 #define DEBUG_TYPE "mips"
00039 
00040 extern "C" void LLVMInitializeMipsTarget() {
00041   // Register the target.
00042   RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
00043   RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
00044   RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
00045   RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
00046 }
00047 
00048 // On function prologue, the stack is created by decrementing
00049 // its pointer. Once decremented, all references are done with positive
00050 // offset from the stack/frame pointer, using StackGrowsUp enables
00051 // an easier handling.
00052 // Using CodeModel::Large enables different CALL behavior.
00053 MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
00054                                      StringRef CPU, StringRef FS,
00055                                      const TargetOptions &Options,
00056                                      Reloc::Model RM, CodeModel::Model CM,
00057                                      CodeGenOpt::Level OL, bool isLittle)
00058     : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
00059       Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, this),
00060       NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
00061                         isLittle, this),
00062       Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
00063                       isLittle, this) {
00064   Subtarget = &DefaultSubtarget;
00065   initAsmInfo();
00066 }
00067 
00068 void MipsebTargetMachine::anchor() { }
00069 
00070 MipsebTargetMachine::
00071 MipsebTargetMachine(const Target &T, StringRef TT,
00072                     StringRef CPU, StringRef FS, const TargetOptions &Options,
00073                     Reloc::Model RM, CodeModel::Model CM,
00074                     CodeGenOpt::Level OL)
00075   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
00076 
00077 void MipselTargetMachine::anchor() { }
00078 
00079 MipselTargetMachine::
00080 MipselTargetMachine(const Target &T, StringRef TT,
00081                     StringRef CPU, StringRef FS, const TargetOptions &Options,
00082                     Reloc::Model RM, CodeModel::Model CM,
00083                     CodeGenOpt::Level OL)
00084   : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
00085 
00086 void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
00087   DEBUG(dbgs() << "resetSubtarget\n");
00088   AttributeSet FnAttrs = MF->getFunction()->getAttributes();
00089   bool Mips16Attr = FnAttrs.hasAttribute(AttributeSet::FunctionIndex, "mips16");
00090   bool NoMips16Attr =
00091       FnAttrs.hasAttribute(AttributeSet::FunctionIndex, "nomips16");
00092   assert(!(Mips16Attr && NoMips16Attr) &&
00093          "mips16 and nomips16 specified on the same function");
00094   if (Mips16Attr)
00095     Subtarget = &Mips16Subtarget;
00096   else if (NoMips16Attr)
00097     Subtarget = &NoMips16Subtarget;
00098   else
00099     Subtarget = &DefaultSubtarget;
00100   MF->setSubtarget(Subtarget);
00101   return;
00102 }
00103 
00104 namespace {
00105 /// Mips Code Generator Pass Configuration Options.
00106 class MipsPassConfig : public TargetPassConfig {
00107 public:
00108   MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
00109     : TargetPassConfig(TM, PM) {
00110     // The current implementation of long branch pass requires a scratch
00111     // register ($at) to be available before branch instructions. Tail merging
00112     // can break this requirement, so disable it when long branch pass is
00113     // enabled.
00114     EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
00115   }
00116 
00117   MipsTargetMachine &getMipsTargetMachine() const {
00118     return getTM<MipsTargetMachine>();
00119   }
00120 
00121   const MipsSubtarget &getMipsSubtarget() const {
00122     return *getMipsTargetMachine().getSubtargetImpl();
00123   }
00124 
00125   void addIRPasses() override;
00126   bool addInstSelector() override;
00127   void addMachineSSAOptimization() override;
00128   bool addPreEmitPass() override;
00129 
00130   bool addPreRegAlloc() override;
00131 
00132 };
00133 } // namespace
00134 
00135 TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
00136   return new MipsPassConfig(this, PM);
00137 }
00138 
00139 void MipsPassConfig::addIRPasses() {
00140   TargetPassConfig::addIRPasses();
00141   if (getMipsSubtarget().os16())
00142     addPass(createMipsOs16(getMipsTargetMachine()));
00143   if (getMipsSubtarget().inMips16HardFloat())
00144     addPass(createMips16HardFloat(getMipsTargetMachine()));
00145 }
00146 // Install an instruction selector pass using
00147 // the ISelDag to gen Mips code.
00148 bool MipsPassConfig::addInstSelector() {
00149   addPass(createMipsModuleISelDag(getMipsTargetMachine()));
00150   addPass(createMips16ISelDag(getMipsTargetMachine()));
00151   addPass(createMipsSEISelDag(getMipsTargetMachine()));
00152   return false;
00153 }
00154 
00155 void MipsPassConfig::addMachineSSAOptimization() {
00156   addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
00157   TargetPassConfig::addMachineSSAOptimization();
00158 }
00159 
00160 bool MipsPassConfig::addPreRegAlloc() {
00161   if (getOptLevel() == CodeGenOpt::None) {
00162     addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
00163     return true;
00164   }
00165   else
00166     return false;
00167 }
00168 
00169 void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
00170   if (Subtarget->allowMixed16_32()) {
00171     DEBUG(errs() << "No ");
00172     //FIXME: The Basic Target Transform Info
00173     // pass needs to become a function pass instead of
00174     // being an immutable pass and then this method as it exists now
00175     // would be unnecessary.
00176     PM.add(createNoTargetTransformInfoPass());
00177   } else
00178     LLVMTargetMachine::addAnalysisPasses(PM);
00179   DEBUG(errs() << "Target Transform Info Pass Added\n");
00180 }
00181 
00182 // Implemented by targets that want to run passes immediately before
00183 // machine code is emitted. return true if -print-machineinstrs should
00184 // print out the code after the passes.
00185 bool MipsPassConfig::addPreEmitPass() {
00186   MipsTargetMachine &TM = getMipsTargetMachine();
00187   addPass(createMipsDelaySlotFillerPass(TM));
00188   addPass(createMipsLongBranchPass(TM));
00189   addPass(createMipsConstantIslandPass(TM));
00190   return true;
00191 }