MipsTargetMachine.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #include "MipsTargetMachine.h"
  14. #include "Mips.h"
  15. #include "Mips16FrameLowering.h"
  16. #include "Mips16HardFloat.h"
  17. #include "Mips16ISelDAGToDAG.h"
  18. #include "Mips16ISelLowering.h"
  19. #include "Mips16InstrInfo.h"
  20. #include "MipsFrameLowering.h"
  21. #include "MipsInstrInfo.h"
  22. #include "MipsModuleISelDAGToDAG.h"
  23. #include "MipsOs16.h"
  24. #include "MipsSEFrameLowering.h"
  25. #include "MipsSEISelDAGToDAG.h"
  26. #include "MipsSEISelLowering.h"
  27. #include "MipsSEInstrInfo.h"
  28. #include "llvm/Analysis/TargetTransformInfo.h"
  29. #include "llvm/CodeGen/Passes.h"
  30. #include "llvm/PassManager.h"
  31. #include "llvm/Support/Debug.h"
  32. #include "llvm/Support/TargetRegistry.h"
  33. #include "llvm/Support/raw_ostream.h"
  34. #include "llvm/Transforms/Scalar.h"
  35. using namespace llvm;
  36. #define DEBUG_TYPE "mips"
  37. extern "C" void LLVMInitializeMipsTarget() {
  38. // Register the target.
  39. RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget);
  40. RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget);
  41. RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target);
  42. RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget);
  43. }
  44. // On function prologue, the stack is created by decrementing
  45. // its pointer. Once decremented, all references are done with positive
  46. // offset from the stack/frame pointer, using StackGrowsUp enables
  47. // an easier handling.
  48. // Using CodeModel::Large enables different CALL behavior.
  49. MipsTargetMachine::MipsTargetMachine(const Target &T, StringRef TT,
  50. StringRef CPU, StringRef FS,
  51. const TargetOptions &Options,
  52. Reloc::Model RM, CodeModel::Model CM,
  53. CodeGenOpt::Level OL, bool isLittle)
  54. : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL),
  55. Subtarget(nullptr), DefaultSubtarget(TT, CPU, FS, isLittle, this),
  56. NoMips16Subtarget(TT, CPU, FS.empty() ? "-mips16" : FS.str() + ",-mips16",
  57. isLittle, this),
  58. Mips16Subtarget(TT, CPU, FS.empty() ? "+mips16" : FS.str() + ",+mips16",
  59. isLittle, this) {
  60. Subtarget = &DefaultSubtarget;
  61. initAsmInfo();
  62. }
  63. void MipsebTargetMachine::anchor() { }
  64. MipsebTargetMachine::
  65. MipsebTargetMachine(const Target &T, StringRef TT,
  66. StringRef CPU, StringRef FS, const TargetOptions &Options,
  67. Reloc::Model RM, CodeModel::Model CM,
  68. CodeGenOpt::Level OL)
  69. : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {}
  70. void MipselTargetMachine::anchor() { }
  71. MipselTargetMachine::
  72. MipselTargetMachine(const Target &T, StringRef TT,
  73. StringRef CPU, StringRef FS, const TargetOptions &Options,
  74. Reloc::Model RM, CodeModel::Model CM,
  75. CodeGenOpt::Level OL)
  76. : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {}
  77. void MipsTargetMachine::resetSubtarget(MachineFunction *MF) {
  78. DEBUG(dbgs() << "resetSubtarget\n");
  79. AttributeSet FnAttrs = MF->getFunction()->getAttributes();
  80. bool Mips16Attr = FnAttrs.hasAttribute(AttributeSet::FunctionIndex, "mips16");
  81. bool NoMips16Attr =
  82. FnAttrs.hasAttribute(AttributeSet::FunctionIndex, "nomips16");
  83. assert(!(Mips16Attr && NoMips16Attr) &&
  84. "mips16 and nomips16 specified on the same function");
  85. if (Mips16Attr)
  86. Subtarget = &Mips16Subtarget;
  87. else if (NoMips16Attr)
  88. Subtarget = &NoMips16Subtarget;
  89. else
  90. Subtarget = &DefaultSubtarget;
  91. MF->setSubtarget(Subtarget);
  92. return;
  93. }
  94. namespace {
  95. /// Mips Code Generator Pass Configuration Options.
  96. class MipsPassConfig : public TargetPassConfig {
  97. public:
  98. MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM)
  99. : TargetPassConfig(TM, PM) {
  100. // The current implementation of long branch pass requires a scratch
  101. // register ($at) to be available before branch instructions. Tail merging
  102. // can break this requirement, so disable it when long branch pass is
  103. // enabled.
  104. EnableTailMerge = !getMipsSubtarget().enableLongBranchPass();
  105. }
  106. MipsTargetMachine &getMipsTargetMachine() const {
  107. return getTM<MipsTargetMachine>();
  108. }
  109. const MipsSubtarget &getMipsSubtarget() const {
  110. return *getMipsTargetMachine().getSubtargetImpl();
  111. }
  112. void addIRPasses() override;
  113. bool addInstSelector() override;
  114. void addMachineSSAOptimization() override;
  115. bool addPreEmitPass() override;
  116. bool addPreRegAlloc() override;
  117. };
  118. } // namespace
  119. TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) {
  120. return new MipsPassConfig(this, PM);
  121. }
  122. void MipsPassConfig::addIRPasses() {
  123. TargetPassConfig::addIRPasses();
  124. if (getMipsSubtarget().os16())
  125. addPass(createMipsOs16(getMipsTargetMachine()));
  126. if (getMipsSubtarget().inMips16HardFloat())
  127. addPass(createMips16HardFloat(getMipsTargetMachine()));
  128. }
  129. // Install an instruction selector pass using
  130. // the ISelDag to gen Mips code.
  131. bool MipsPassConfig::addInstSelector() {
  132. addPass(createMipsModuleISelDag(getMipsTargetMachine()));
  133. addPass(createMips16ISelDag(getMipsTargetMachine()));
  134. addPass(createMipsSEISelDag(getMipsTargetMachine()));
  135. return false;
  136. }
  137. void MipsPassConfig::addMachineSSAOptimization() {
  138. addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
  139. TargetPassConfig::addMachineSSAOptimization();
  140. }
  141. bool MipsPassConfig::addPreRegAlloc() {
  142. if (getOptLevel() == CodeGenOpt::None) {
  143. addPass(createMipsOptimizePICCallPass(getMipsTargetMachine()));
  144. return true;
  145. }
  146. else
  147. return false;
  148. }
  149. void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) {
  150. if (Subtarget->allowMixed16_32()) {
  151. DEBUG(errs() << "No ");
  152. //FIXME: The Basic Target Transform Info
  153. // pass needs to become a function pass instead of
  154. // being an immutable pass and then this method as it exists now
  155. // would be unnecessary.
  156. PM.add(createNoTargetTransformInfoPass());
  157. } else
  158. LLVMTargetMachine::addAnalysisPasses(PM);
  159. DEBUG(errs() << "Target Transform Info Pass Added\n");
  160. }
  161. // Implemented by targets that want to run passes immediately before
  162. // machine code is emitted. return true if -print-machineinstrs should
  163. // print out the code after the passes.
  164. bool MipsPassConfig::addPreEmitPass() {
  165. MipsTargetMachine &TM = getMipsTargetMachine();
  166. addPass(createMipsDelaySlotFillerPass(TM));
  167. addPass(createMipsLongBranchPass(TM));
  168. addPass(createMipsConstantIslandPass(TM));
  169. return true;
  170. }
  171. bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM,
  172. JITCodeEmitter &JCE) {
  173. // Machine code emitter pass for Mips.
  174. PM.add(createMipsJITCodeEmitterPass(*this, JCE));
  175. return false;
  176. }