Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 1 | // $Id$ |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 2 | //*************************************************************************** |
| 3 | // File: |
| 4 | // Sparc.cpp |
| 5 | // |
| 6 | // Purpose: |
| 7 | // |
| 8 | // History: |
| 9 | // 7/15/01 - Vikram Adve - Created |
| 10 | //**************************************************************************/ |
| 11 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 12 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 13 | #include "SparcInternals.h" |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 14 | #include "llvm/Target/Sparc.h" |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/InstrScheduling.h" |
| 16 | #include "llvm/CodeGen/InstrSelection.h" |
Chris Lattner | cf4525b | 2002-02-03 07:49:15 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 18 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
Chris Lattner | 6dd98a6 | 2002-02-04 00:33:08 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/RegisterAllocation.h" |
Chris Lattner | 699683c | 2002-02-04 05:59:25 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstr.h" |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 21 | #include "llvm/Method.h" |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 22 | #include "llvm/PassManager.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 23 | #include <iostream> |
| 24 | using std::cerr; |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 26 | // Build the MachineInstruction Description Array... |
| 27 | const MachineInstrDescriptor SparcMachineInstrDesc[] = { |
| 28 | #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 29 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \ |
| 30 | { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 31 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS }, |
| 32 | #include "SparcInstr.def" |
| 33 | }; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 34 | |
| 35 | //---------------------------------------------------------------------------- |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 36 | // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine |
| 37 | // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 38 | //---------------------------------------------------------------------------- |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 39 | // |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 41 | TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 42 | |
| 43 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 44 | //--------------------------------------------------------------------------- |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 45 | // class InsertPrologEpilogCode |
| 46 | // |
| 47 | // Insert SAVE/RESTORE instructions for the method |
| 48 | // |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 49 | // Insert prolog code at the unique method entry point. |
| 50 | // Insert epilog code at each method exit point. |
| 51 | // InsertPrologEpilog invokes these only if the method is not compiled |
| 52 | // with the leaf method optimization. |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 53 | // |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 54 | //--------------------------------------------------------------------------- |
Chris Lattner | 6dd98a6 | 2002-02-04 00:33:08 +0000 | [diff] [blame] | 55 | static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR]; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 57 | class InsertPrologEpilogCode : public MethodPass { |
| 58 | TargetMachine &Target; |
| 59 | public: |
| 60 | inline InsertPrologEpilogCode(TargetMachine &T) : Target(T) {} |
| 61 | bool runOnMethod(Method *M) { |
| 62 | MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(M); |
| 63 | if (!mcodeInfo.isCompiledAsLeafMethod()) { |
| 64 | InsertPrologCode(M); |
| 65 | InsertEpilogCode(M); |
| 66 | } |
| 67 | return false; |
| 68 | } |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 70 | void InsertPrologCode(Method *M); |
| 71 | void InsertEpilogCode(Method *M); |
| 72 | }; |
| 73 | |
| 74 | void InsertPrologEpilogCode::InsertPrologCode(Method* method) |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 75 | { |
| 76 | BasicBlock* entryBB = method->getEntryNode(); |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 77 | unsigned N = GetInstructionsForProlog(entryBB, Target, minstrVec); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 78 | assert(N <= MAX_INSTR_PER_VMINSTR); |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 79 | MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec(); |
| 80 | bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 84 | void InsertPrologEpilogCode::InsertEpilogCode(Method* method) |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 85 | { |
| 86 | for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I) |
| 87 | if ((*I)->getTerminator()->getOpcode() == Instruction::Ret) |
| 88 | { |
| 89 | BasicBlock* exitBB = *I; |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 90 | unsigned N = GetInstructionsForEpilog(exitBB, Target, minstrVec); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 91 | |
| 92 | MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec(); |
Chris Lattner | cf4525b | 2002-02-03 07:49:15 +0000 | [diff] [blame] | 93 | MachineCodeForInstruction &termMvec = |
| 94 | MachineCodeForInstruction::get(exitBB->getTerminator()); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 95 | |
| 96 | // Remove the NOPs in the delay slots of the return instruction |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 97 | const MachineInstrInfo &mii = Target.getInstrInfo(); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 98 | unsigned numNOPs = 0; |
| 99 | while (termMvec.back()->getOpCode() == NOP) |
| 100 | { |
| 101 | assert( termMvec.back() == bbMvec.back()); |
| 102 | termMvec.pop_back(); |
| 103 | bbMvec.pop_back(); |
| 104 | ++numNOPs; |
| 105 | } |
| 106 | assert(termMvec.back() == bbMvec.back()); |
| 107 | |
| 108 | // Check that we found the right number of NOPs and have the right |
| 109 | // number of instructions to replace them. |
| 110 | unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode()); |
| 111 | assert(numNOPs == ndelays && "Missing NOPs in delay slots?"); |
| 112 | assert(N == ndelays && "Cannot use epilog code for delay slots?"); |
| 113 | |
| 114 | // Append the epilog code to the end of the basic block. |
| 115 | bbMvec.push_back(minstrVec[0]); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 120 | //--------------------------------------------------------------------------- |
| 121 | // class UltraSparcFrameInfo |
| 122 | // |
| 123 | // Purpose: |
| 124 | // Interface to stack frame layout info for the UltraSPARC. |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 125 | // Starting offsets for each area of the stack frame are aligned at |
| 126 | // a multiple of getStackFrameSizeAlignment(). |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 127 | //--------------------------------------------------------------------------- |
| 128 | |
| 129 | int |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 130 | UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& , |
| 131 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 132 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 133 | pos = false; // static stack area grows downwards |
| 134 | return StaticAreaOffsetFromFP; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 138 | UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo, |
| 139 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 140 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 141 | pos = false; // static stack area grows downwards |
| 142 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 143 | if (int mod = autoVarsSize % getStackFrameSizeAlignment()) |
| 144 | autoVarsSize += (getStackFrameSizeAlignment() - mod); |
| 145 | return StaticAreaOffsetFromFP - autoVarsSize; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | int |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 149 | UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo, |
| 150 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 151 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 152 | pos = false; // static stack area grows downwards |
| 153 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
| 154 | unsigned int spillAreaSize = mcInfo.getRegSpillsSize(); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 155 | int offset = autoVarsSize + spillAreaSize; |
| 156 | if (int mod = offset % getStackFrameSizeAlignment()) |
| 157 | offset += (getStackFrameSizeAlignment() - mod); |
| 158 | return StaticAreaOffsetFromFP - offset; |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int |
| 162 | UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo, |
| 163 | bool& pos) const |
| 164 | { |
| 165 | // dynamic stack area grows downwards starting at top of opt-args area |
| 166 | unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize(); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 167 | int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP; |
| 168 | assert(offset % getStackFrameSizeAlignment() == 0); |
| 169 | return offset; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 173 | //--------------------------------------------------------------------------- |
| 174 | // class UltraSparcMachine |
| 175 | // |
| 176 | // Purpose: |
| 177 | // Primary interface to machine description for the UltraSPARC. |
| 178 | // Primarily just initializes machine-dependent parameters in |
| 179 | // class TargetMachine, and creates machine-dependent subclasses |
| 180 | // for classes such as MachineInstrInfo. |
| 181 | // |
| 182 | //--------------------------------------------------------------------------- |
| 183 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 184 | UltraSparc::UltraSparc() |
| 185 | : TargetMachine("UltraSparc-Native"), |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 186 | instrInfo(*this), |
| 187 | schedInfo(*this), |
| 188 | regInfo(*this), |
Vikram S. Adve | b704840 | 2001-11-09 02:16:04 +0000 | [diff] [blame] | 189 | frameInfo(*this), |
| 190 | cacheInfo(*this) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 191 | { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 192 | optSizeForSubWordData = 4; |
| 193 | minMemOpWordSize = 8; |
| 194 | maxAtomicMemOpWordSize = 8; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 198 | |
| 199 | //===---------------------------------------------------------------------===// |
| 200 | // GenerateCodeForTarget Pass |
| 201 | // |
| 202 | // Native code generation for a specified target. |
| 203 | //===---------------------------------------------------------------------===// |
| 204 | |
| 205 | class ConstructMachineCodeForMethod : public MethodPass { |
| 206 | TargetMachine &Target; |
| 207 | public: |
| 208 | inline ConstructMachineCodeForMethod(TargetMachine &T) : Target(T) {} |
| 209 | bool runOnMethod(Method *M) { |
| 210 | MachineCodeForMethod::construct(M, Target); |
| 211 | return false; |
| 212 | } |
| 213 | }; |
| 214 | |
| 215 | class InstructionSelection : public MethodPass { |
| 216 | TargetMachine &Target; |
| 217 | public: |
| 218 | inline InstructionSelection(TargetMachine &T) : Target(T) {} |
| 219 | bool runOnMethod(Method *M) { |
| 220 | if (SelectInstructionsForMethod(M, Target)) |
| 221 | cerr << "Instr selection failed for method " << M->getName() << "\n"; |
| 222 | return false; |
| 223 | } |
| 224 | }; |
| 225 | |
| 226 | class InstructionScheduling : public MethodPass { |
| 227 | TargetMachine &Target; |
| 228 | public: |
| 229 | inline InstructionScheduling(TargetMachine &T) : Target(T) {} |
| 230 | bool runOnMethod(Method *M) { |
| 231 | if (ScheduleInstructionsWithSSA(M, Target)) |
| 232 | cerr << "Instr scheduling failed for method " << M->getName() << "\n\n"; |
| 233 | return false; |
| 234 | } |
| 235 | }; |
| 236 | |
| 237 | struct FreeMachineCodeForMethod : public MethodPass { |
| 238 | static void freeMachineCode(Instruction *I) { |
| 239 | MachineCodeForInstruction::destroy(I); |
| 240 | } |
| 241 | |
| 242 | bool runOnMethod(Method *M) { |
| 243 | for_each(M->inst_begin(), M->inst_end(), freeMachineCode); |
| 244 | // Don't destruct MachineCodeForMethod - The global printer needs it |
| 245 | //MachineCodeForMethod::destruct(M); |
| 246 | return false; |
| 247 | } |
| 248 | }; |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 249 | |
| 250 | |
Chris Lattner | 6b04e71 | 2002-02-04 00:39:14 +0000 | [diff] [blame] | 251 | |
| 252 | // addPassesToEmitAssembly - This method controls the entire code generation |
| 253 | // process for the ultra sparc. |
| 254 | // |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 255 | void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 256 | // Construct and initialize the MachineCodeForMethod object for this method. |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 257 | PM.add(new ConstructMachineCodeForMethod(*this)); |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 259 | PM.add(new InstructionSelection(*this)); |
Ruchira Sasanka | d00982a | 2002-01-07 19:20:28 +0000 | [diff] [blame] | 260 | |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 261 | //PM.add(new InstructionScheduling(*this)); |
Chris Lattner | cf4525b | 2002-02-03 07:49:15 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 2f9b28e | 2002-02-04 15:54:09 +0000 | [diff] [blame^] | 263 | PM.add(getRegisterAllocator(*this)); |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 264 | |
| 265 | //PM.add(new OptimizeLeafProcedures()); |
| 266 | //PM.add(new DeleteFallThroughBranches()); |
| 267 | //PM.add(new RemoveChainedBranches()); // should be folded with previous |
| 268 | //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc. |
| 269 | |
| 270 | PM.add(new InsertPrologEpilogCode(*this)); |
| 271 | |
| 272 | // Output assembly language to the .s file. Assembly emission is split into |
| 273 | // two parts: Method output and Global value output. This is because method |
| 274 | // output is pipelined with all of the rest of code generation stuff, |
| 275 | // allowing machine code representations for methods to be free'd after the |
| 276 | // method has been emitted. |
| 277 | // |
| 278 | PM.add(getMethodAsmPrinterPass(PM, Out)); |
| 279 | PM.add(new FreeMachineCodeForMethod()); // Free stuff no longer needed |
Chris Lattner | cf4525b | 2002-02-03 07:49:15 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 0feb358 | 2002-02-03 23:41:51 +0000 | [diff] [blame] | 281 | // Emit Module level assembly after all of the methods have been processed. |
| 282 | PM.add(getModuleAsmPrinterPass(PM, Out)); |
Chris Lattner | cf4525b | 2002-02-03 07:49:15 +0000 | [diff] [blame] | 283 | } |