Jia Liu | 9f61011 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsTargetMachine.cpp - Define TargetMachine for Mips -------------===// |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 7 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 9 | // |
| 10 | // Implements the info about Mips target spec. |
| 11 | // |
Akira Hatanaka | e248912 | 2011-04-15 21:51:11 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 13 | |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 14 | #include "MipsTargetMachine.h" |
Craig Topper | b25fda9 | 2012-03-17 18:46:09 +0000 | [diff] [blame] | 15 | #include "Mips.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame^] | 16 | #include "Mips16FrameLowering.h" |
| 17 | #include "Mips16HardFloat.h" |
| 18 | #include "Mips16ISelDAGToDAG.h" |
| 19 | #include "Mips16ISelLowering.h" |
| 20 | #include "Mips16InstrInfo.h" |
Akira Hatanaka | fab8929 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 21 | #include "MipsFrameLowering.h" |
| 22 | #include "MipsInstrInfo.h" |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 23 | #include "MipsModuleISelDAGToDAG.h" |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 24 | #include "MipsOs16.h" |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 25 | #include "MipsSEFrameLowering.h" |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 26 | #include "MipsSEISelDAGToDAG.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame^] | 27 | #include "MipsSEISelLowering.h" |
| 28 | #include "MipsSEInstrInfo.h" |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/TargetTransformInfo.h" |
Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 30 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 31 | #include "llvm/PassManager.h" |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Evan Cheng | 2bb4035 | 2011-08-24 18:08:43 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame^] | 34 | #include "llvm/Support/raw_ostream.h" |
Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Scalar.h" |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 38 | |
| 39 | |
Daniel Dunbar | 5680b4f | 2009-07-25 06:49:55 +0000 | [diff] [blame] | 40 | extern "C" void LLVMInitializeMipsTarget() { |
| 41 | // Register the target. |
Akira Hatanaka | 3d673cc | 2011-09-21 03:00:58 +0000 | [diff] [blame] | 42 | RegisterTargetMachine<MipsebTargetMachine> X(TheMipsTarget); |
Eli Friedman | 57c11da | 2009-08-03 02:22:28 +0000 | [diff] [blame] | 43 | RegisterTargetMachine<MipselTargetMachine> Y(TheMipselTarget); |
Akira Hatanaka | 3065180 | 2012-07-31 21:39:17 +0000 | [diff] [blame] | 44 | RegisterTargetMachine<MipsebTargetMachine> A(TheMips64Target); |
| 45 | RegisterTargetMachine<MipselTargetMachine> B(TheMips64elTarget); |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Rafael Espindola | b2fb78d | 2013-12-11 01:41:10 +0000 | [diff] [blame] | 48 | static std::string computeDataLayout(const MipsSubtarget &ST) { |
| 49 | std::string Ret = ""; |
| 50 | |
| 51 | // There are both little and big endian mips. |
| 52 | if (ST.isLittle()) |
| 53 | Ret += "e"; |
| 54 | else |
| 55 | Ret += "E"; |
| 56 | |
Rafael Espindola | 5887356 | 2014-01-03 19:21:54 +0000 | [diff] [blame] | 57 | Ret += "-m:m"; |
| 58 | |
Rafael Espindola | 8afbb28 | 2013-12-16 17:15:29 +0000 | [diff] [blame] | 59 | // Pointers are 32 bit on some ABIs. |
| 60 | if (!ST.isABI_N64()) |
Rafael Espindola | bccb9d4 | 2013-12-16 18:01:51 +0000 | [diff] [blame] | 61 | Ret += "-p:32:32"; |
Rafael Espindola | b2fb78d | 2013-12-11 01:41:10 +0000 | [diff] [blame] | 62 | |
| 63 | // 8 and 16 bit integers only need no have natural alignment, but try to |
| 64 | // align them to 32 bits. 64 bit integers have natural alignment. |
Rafael Espindola | bccb9d4 | 2013-12-16 18:01:51 +0000 | [diff] [blame] | 65 | Ret += "-i8:8:32-i16:16:32-i64:64"; |
Rafael Espindola | b2fb78d | 2013-12-11 01:41:10 +0000 | [diff] [blame] | 66 | |
| 67 | // 32 bit registers are always available and the stack is at least 64 bit |
| 68 | // aligned. On N64 64 bit registers are also available and the stack is |
| 69 | // 128 bit aligned. |
Rafael Espindola | febb8d2 | 2013-12-17 23:15:58 +0000 | [diff] [blame] | 70 | if (ST.isABI_N64() || ST.isABI_N32()) |
Rafael Espindola | b2fb78d | 2013-12-11 01:41:10 +0000 | [diff] [blame] | 71 | Ret += "-n32:64-S128"; |
| 72 | else |
| 73 | Ret += "-n32-S64"; |
| 74 | |
| 75 | return Ret; |
| 76 | } |
| 77 | |
Bruno Cardoso Lopes | 4331883 | 2007-08-28 05:13:42 +0000 | [diff] [blame] | 78 | // On function prologue, the stack is created by decrementing |
| 79 | // its pointer. Once decremented, all references are done with positive |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 80 | // offset from the stack/frame pointer, using StackGrowsUp enables |
Bruno Cardoso Lopes | 4659aad | 2008-08-06 06:14:43 +0000 | [diff] [blame] | 81 | // an easier handling. |
Bruno Cardoso Lopes | c9c3f49 | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 82 | // Using CodeModel::Large enables different CALL behavior. |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 83 | MipsTargetMachine:: |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 84 | MipsTargetMachine(const Target &T, StringRef TT, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 85 | StringRef CPU, StringRef FS, const TargetOptions &Options, |
Evan Cheng | efd9b42 | 2011-07-20 07:51:56 +0000 | [diff] [blame] | 86 | Reloc::Model RM, CodeModel::Model CM, |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 87 | CodeGenOpt::Level OL, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 88 | bool isLittle) |
| 89 | : LLVMTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL), |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 90 | Subtarget(TT, CPU, FS, isLittle, RM, this), |
Rafael Espindola | b2fb78d | 2013-12-11 01:41:10 +0000 | [diff] [blame] | 91 | DL(computeDataLayout(Subtarget)), |
Akira Hatanaka | fab8929 | 2012-08-02 18:21:47 +0000 | [diff] [blame] | 92 | InstrInfo(MipsInstrInfo::create(*this)), |
| 93 | FrameLowering(MipsFrameLowering::create(*this, Subtarget)), |
Akira Hatanaka | 66bc419 | 2013-07-12 23:33:22 +0000 | [diff] [blame] | 94 | TLInfo(MipsTargetLowering::create(*this)), TSInfo(*this), |
| 95 | InstrItins(Subtarget.getInstrItineraryData()), JITInfo() { |
Rafael Espindola | 227144c | 2013-05-13 01:16:13 +0000 | [diff] [blame] | 96 | initAsmInfo(); |
Bruno Cardoso Lopes | 35d86e6 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 97 | } |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 98 | |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 99 | |
| 100 | void MipsTargetMachine::setHelperClassesMips16() { |
| 101 | InstrInfoSE.swap(InstrInfo); |
| 102 | FrameLoweringSE.swap(FrameLowering); |
| 103 | TLInfoSE.swap(TLInfo); |
| 104 | if (!InstrInfo16) { |
| 105 | InstrInfo.reset(MipsInstrInfo::create(*this)); |
| 106 | FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget)); |
| 107 | TLInfo.reset(MipsTargetLowering::create(*this)); |
| 108 | } else { |
| 109 | InstrInfo16.swap(InstrInfo); |
| 110 | FrameLowering16.swap(FrameLowering); |
| 111 | TLInfo16.swap(TLInfo); |
| 112 | } |
| 113 | assert(TLInfo && "null target lowering 16"); |
| 114 | assert(InstrInfo && "null instr info 16"); |
| 115 | assert(FrameLowering && "null frame lowering 16"); |
| 116 | } |
| 117 | |
| 118 | void MipsTargetMachine::setHelperClassesMipsSE() { |
| 119 | InstrInfo16.swap(InstrInfo); |
| 120 | FrameLowering16.swap(FrameLowering); |
| 121 | TLInfo16.swap(TLInfo); |
| 122 | if (!InstrInfoSE) { |
| 123 | InstrInfo.reset(MipsInstrInfo::create(*this)); |
| 124 | FrameLowering.reset(MipsFrameLowering::create(*this, Subtarget)); |
| 125 | TLInfo.reset(MipsTargetLowering::create(*this)); |
| 126 | } else { |
| 127 | InstrInfoSE.swap(InstrInfo); |
| 128 | FrameLoweringSE.swap(FrameLowering); |
| 129 | TLInfoSE.swap(TLInfo); |
| 130 | } |
| 131 | assert(TLInfo && "null target lowering in SE"); |
| 132 | assert(InstrInfo && "null instr info SE"); |
| 133 | assert(FrameLowering && "null frame lowering SE"); |
| 134 | } |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 135 | void MipsebTargetMachine::anchor() { } |
| 136 | |
Akira Hatanaka | 3d673cc | 2011-09-21 03:00:58 +0000 | [diff] [blame] | 137 | MipsebTargetMachine:: |
| 138 | MipsebTargetMachine(const Target &T, StringRef TT, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 139 | StringRef CPU, StringRef FS, const TargetOptions &Options, |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 140 | Reloc::Model RM, CodeModel::Model CM, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 141 | CodeGenOpt::Level OL) |
| 142 | : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, false) {} |
Akira Hatanaka | 3d673cc | 2011-09-21 03:00:58 +0000 | [diff] [blame] | 143 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 144 | void MipselTargetMachine::anchor() { } |
| 145 | |
Bruno Cardoso Lopes | 326a037 | 2008-06-04 01:45:25 +0000 | [diff] [blame] | 146 | MipselTargetMachine:: |
Evan Cheng | 2129f59 | 2011-07-19 06:37:02 +0000 | [diff] [blame] | 147 | MipselTargetMachine(const Target &T, StringRef TT, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 148 | StringRef CPU, StringRef FS, const TargetOptions &Options, |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 149 | Reloc::Model RM, CodeModel::Model CM, |
Nick Lewycky | 50f02cb | 2011-12-02 22:16:29 +0000 | [diff] [blame] | 150 | CodeGenOpt::Level OL) |
| 151 | : MipsTargetMachine(T, TT, CPU, FS, Options, RM, CM, OL, true) {} |
Bruno Cardoso Lopes | 326a037 | 2008-06-04 01:45:25 +0000 | [diff] [blame] | 152 | |
Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 153 | namespace { |
| 154 | /// Mips Code Generator Pass Configuration Options. |
| 155 | class MipsPassConfig : public TargetPassConfig { |
| 156 | public: |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 157 | MipsPassConfig(MipsTargetMachine *TM, PassManagerBase &PM) |
Akira Hatanaka | 3c0d6af | 2013-10-07 19:13:53 +0000 | [diff] [blame] | 158 | : TargetPassConfig(TM, PM) { |
| 159 | // The current implementation of long branch pass requires a scratch |
| 160 | // register ($at) to be available before branch instructions. Tail merging |
| 161 | // can break this requirement, so disable it when long branch pass is |
| 162 | // enabled. |
| 163 | EnableTailMerge = !getMipsSubtarget().enableLongBranchPass(); |
| 164 | } |
Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 165 | |
| 166 | MipsTargetMachine &getMipsTargetMachine() const { |
| 167 | return getTM<MipsTargetMachine>(); |
| 168 | } |
| 169 | |
| 170 | const MipsSubtarget &getMipsSubtarget() const { |
| 171 | return *getMipsTargetMachine().getSubtargetImpl(); |
| 172 | } |
| 173 | |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 174 | virtual void addIRPasses(); |
Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 175 | virtual bool addInstSelector(); |
Akira Hatanaka | 168d4e5 | 2013-11-27 23:38:42 +0000 | [diff] [blame] | 176 | virtual void addMachineSSAOptimization(); |
Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 177 | virtual bool addPreEmitPass(); |
| 178 | }; |
| 179 | } // namespace |
| 180 | |
Andrew Trick | f8ea108 | 2012-02-04 02:56:59 +0000 | [diff] [blame] | 181 | TargetPassConfig *MipsTargetMachine::createPassConfig(PassManagerBase &PM) { |
| 182 | return new MipsPassConfig(this, PM); |
Andrew Trick | ccb6736 | 2012-02-03 05:12:41 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 185 | void MipsPassConfig::addIRPasses() { |
| 186 | TargetPassConfig::addIRPasses(); |
| 187 | if (getMipsSubtarget().os16()) |
| 188 | addPass(createMipsOs16(getMipsTargetMachine())); |
Reed Kotler | 783c794 | 2013-05-10 22:25:39 +0000 | [diff] [blame] | 189 | if (getMipsSubtarget().inMips16HardFloat()) |
| 190 | addPass(createMips16HardFloat(getMipsTargetMachine())); |
Richard Sandiford | 37cd6cf | 2013-08-23 10:27:02 +0000 | [diff] [blame] | 191 | addPass(createPartiallyInlineLibCallsPass()); |
Reed Kotler | fe94cc3 | 2013-04-10 16:58:04 +0000 | [diff] [blame] | 192 | } |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 193 | // Install an instruction selector pass using |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 194 | // the ISelDag to gen Mips code. |
Bill Wendling | b12f16e | 2012-05-01 08:27:43 +0000 | [diff] [blame] | 195 | bool MipsPassConfig::addInstSelector() { |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 196 | if (getMipsSubtarget().allowMixed16_32()) { |
| 197 | addPass(createMipsModuleISelDag(getMipsTargetMachine())); |
| 198 | addPass(createMips16ISelDag(getMipsTargetMachine())); |
| 199 | addPass(createMipsSEISelDag(getMipsTargetMachine())); |
| 200 | } else { |
| 201 | addPass(createMipsISelDag(getMipsTargetMachine())); |
| 202 | } |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
Akira Hatanaka | 168d4e5 | 2013-11-27 23:38:42 +0000 | [diff] [blame] | 206 | void MipsPassConfig::addMachineSSAOptimization() { |
| 207 | addPass(createMipsOptimizePICCallPass(getMipsTargetMachine())); |
| 208 | TargetPassConfig::addMachineSSAOptimization(); |
| 209 | } |
| 210 | |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 211 | void MipsTargetMachine::addAnalysisPasses(PassManagerBase &PM) { |
| 212 | if (Subtarget.allowMixed16_32()) { |
| 213 | DEBUG(errs() << "No "); |
| 214 | //FIXME: The Basic Target Transform Info |
| 215 | // pass needs to become a function pass instead of |
| 216 | // being an immutable pass and then this method as it exists now |
| 217 | // would be unnecessary. |
| 218 | PM.add(createNoTargetTransformInfoPass()); |
| 219 | } else |
| 220 | LLVMTargetMachine::addAnalysisPasses(PM); |
| 221 | DEBUG(errs() << "Target Transform Info Pass Added\n"); |
| 222 | } |
| 223 | |
Anton Korobeynikov | f7183ed | 2010-11-15 00:06:54 +0000 | [diff] [blame] | 224 | // Implemented by targets that want to run passes immediately before |
| 225 | // machine code is emitted. return true if -print-machineinstrs should |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 226 | // print out the code after the passes. |
Bill Wendling | b12f16e | 2012-05-01 08:27:43 +0000 | [diff] [blame] | 227 | bool MipsPassConfig::addPreEmitPass() { |
Akira Hatanaka | eb36522 | 2012-06-14 01:19:35 +0000 | [diff] [blame] | 228 | MipsTargetMachine &TM = getMipsTargetMachine(); |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 229 | const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>(); |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 230 | addPass(createMipsDelaySlotFillerPass(TM)); |
Akira Hatanaka | eb36522 | 2012-06-14 01:19:35 +0000 | [diff] [blame] | 231 | |
Akira Hatanaka | a8a05be | 2013-10-07 19:06:57 +0000 | [diff] [blame] | 232 | if (Subtarget.enableLongBranchPass()) |
Bob Wilson | bbd38dd | 2012-07-02 19:48:31 +0000 | [diff] [blame] | 233 | addPass(createMipsLongBranchPass(TM)); |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 234 | if (Subtarget.inMips16Mode() || |
| 235 | Subtarget.allowMixed16_32()) |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 236 | addPass(createMipsConstantIslandPass(TM)); |
Akira Hatanaka | eb36522 | 2012-06-14 01:19:35 +0000 | [diff] [blame] | 237 | |
Bruno Cardoso Lopes | a746512 | 2007-08-18 01:58:15 +0000 | [diff] [blame] | 238 | return true; |
Bruno Cardoso Lopes | 35e43c4 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 239 | } |
Akira Hatanaka | 2791697 | 2011-04-15 19:52:08 +0000 | [diff] [blame] | 240 | |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 241 | bool MipsTargetMachine::addCodeEmitter(PassManagerBase &PM, |
Evan Cheng | ecb2908 | 2011-11-16 08:38:26 +0000 | [diff] [blame] | 242 | JITCodeEmitter &JCE) { |
Bruno Cardoso Lopes | d1d9c78 | 2011-07-21 16:28:51 +0000 | [diff] [blame] | 243 | // Machine code emitter pass for Mips. |
| 244 | PM.add(createMipsJITCodeEmitterPass(*this, JCE)); |
| 245 | return false; |
| 246 | } |