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