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