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" |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/PhyRegAlloc.h" |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h" |
| 19 | #include "llvm/Method.h" |
| 20 | |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 21 | |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 22 | // Build the MachineInstruction Description Array... |
| 23 | const MachineInstrDescriptor SparcMachineInstrDesc[] = { |
| 24 | #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 25 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \ |
| 26 | { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 27 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS }, |
| 28 | #include "SparcInstr.def" |
| 29 | }; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 30 | |
| 31 | //---------------------------------------------------------------------------- |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 32 | // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine |
| 33 | // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 34 | //---------------------------------------------------------------------------- |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 35 | // |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 37 | TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 38 | |
| 39 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 40 | //---------------------------------------------------------------------------- |
| 41 | // Entry point for register allocation for a module |
| 42 | //---------------------------------------------------------------------------- |
| 43 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 44 | void AllocateRegisters(Method *M, TargetMachine &target) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 45 | { |
| 46 | |
| 47 | if ( (M)->isExternal() ) // don't process prototypes |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 48 | return; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 49 | |
| 50 | if( DEBUG_RA ) { |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 51 | cerr << endl << "******************** Method "<< (M)->getName(); |
| 52 | cerr << " ********************" <<endl; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | MethodLiveVarInfo LVI(M ); // Analyze live varaibles |
| 56 | LVI.analyze(); |
| 57 | |
| 58 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 59 | PhyRegAlloc PRA(M, target, &LVI); // allocate registers |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 60 | PRA.allocateRegisters(); |
| 61 | |
| 62 | |
Chris Lattner | 1e23ed7 | 2001-10-15 18:15:27 +0000 | [diff] [blame] | 63 | if( DEBUG_RA ) cerr << endl << "Register allocation complete!" << endl; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 64 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 67 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 68 | //--------------------------------------------------------------------------- |
| 69 | // Function InsertPrologCode |
| 70 | // Function InsertEpilogCode |
| 71 | // Function InsertPrologEpilog |
| 72 | // |
| 73 | // Insert prolog code at the unique method entry point. |
| 74 | // Insert epilog code at each method exit point. |
| 75 | // InsertPrologEpilog invokes these only if the method is not compiled |
| 76 | // with the leaf method optimization. |
| 77 | //--------------------------------------------------------------------------- |
| 78 | |
| 79 | static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR]; |
| 80 | |
| 81 | static void |
| 82 | InsertPrologCode(Method* method, TargetMachine& target) |
| 83 | { |
| 84 | BasicBlock* entryBB = method->getEntryNode(); |
| 85 | unsigned N = GetInstructionsForProlog(entryBB, target, minstrVec); |
| 86 | assert(N <= MAX_INSTR_PER_VMINSTR); |
| 87 | if (N > 0) |
| 88 | { |
| 89 | MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec(); |
| 90 | bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static void |
| 96 | InsertEpilogCode(Method* method, TargetMachine& target) |
| 97 | { |
| 98 | for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I) |
| 99 | if ((*I)->getTerminator()->getOpcode() == Instruction::Ret) |
| 100 | { |
| 101 | BasicBlock* exitBB = *I; |
| 102 | unsigned N = GetInstructionsForEpilog(exitBB, target, minstrVec); |
| 103 | |
| 104 | MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec(); |
| 105 | MachineCodeForVMInstr& termMvec = |
| 106 | exitBB->getTerminator()->getMachineInstrVec(); |
| 107 | |
| 108 | // Remove the NOPs in the delay slots of the return instruction |
| 109 | const MachineInstrInfo& mii = target.getInstrInfo(); |
| 110 | unsigned numNOPs = 0; |
| 111 | while (termMvec.back()->getOpCode() == NOP) |
| 112 | { |
| 113 | assert( termMvec.back() == bbMvec.back()); |
| 114 | termMvec.pop_back(); |
| 115 | bbMvec.pop_back(); |
| 116 | ++numNOPs; |
| 117 | } |
| 118 | assert(termMvec.back() == bbMvec.back()); |
| 119 | |
| 120 | // Check that we found the right number of NOPs and have the right |
| 121 | // number of instructions to replace them. |
| 122 | unsigned ndelays = mii.getNumDelaySlots(termMvec.back()->getOpCode()); |
| 123 | assert(numNOPs == ndelays && "Missing NOPs in delay slots?"); |
| 124 | assert(N == ndelays && "Cannot use epilog code for delay slots?"); |
| 125 | |
| 126 | // Append the epilog code to the end of the basic block. |
| 127 | bbMvec.push_back(minstrVec[0]); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | // Insert SAVE/RESTORE instructions for the method |
| 133 | static void |
| 134 | InsertPrologEpilog(Method *method, TargetMachine &target) |
| 135 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 136 | MachineCodeForMethod& mcodeInfo = MachineCodeForMethod::get(method); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 137 | if (mcodeInfo.isCompiledAsLeafMethod()) |
| 138 | return; // nothing to do |
| 139 | |
| 140 | InsertPrologCode(method, target); |
| 141 | InsertEpilogCode(method, target); |
| 142 | } |
| 143 | |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 145 | //--------------------------------------------------------------------------- |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 146 | // class UltraSparcSchedInfo |
| 147 | // |
| 148 | // Purpose: |
| 149 | // Scheduling information for the UltraSPARC. |
| 150 | // Primarily just initializes machine-dependent parameters in |
| 151 | // class MachineSchedInfo. |
| 152 | //--------------------------------------------------------------------------- |
| 153 | |
| 154 | /*ctor*/ |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 155 | UltraSparcSchedInfo::UltraSparcSchedInfo(const TargetMachine& tgt) |
| 156 | : MachineSchedInfo(tgt, |
| 157 | (unsigned int) SPARC_NUM_SCHED_CLASSES, |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 158 | SparcRUsageDesc, |
| 159 | SparcInstrUsageDeltas, |
| 160 | SparcInstrIssueDeltas, |
| 161 | sizeof(SparcInstrUsageDeltas)/sizeof(InstrRUsageDelta), |
| 162 | sizeof(SparcInstrIssueDeltas)/sizeof(InstrIssueDelta)) |
| 163 | { |
| 164 | maxNumIssueTotal = 4; |
| 165 | longestIssueConflict = 0; // computed from issuesGaps[] |
| 166 | |
| 167 | branchMispredictPenalty = 4; // 4 for SPARC IIi |
| 168 | branchTargetUnknownPenalty = 2; // 2 for SPARC IIi |
| 169 | l1DCacheMissPenalty = 8; // 7 or 9 for SPARC IIi |
| 170 | l1ICacheMissPenalty = 8; // ? for SPARC IIi |
| 171 | |
| 172 | inOrderLoads = true; // true for SPARC IIi |
| 173 | inOrderIssue = true; // true for SPARC IIi |
| 174 | inOrderExec = false; // false for most architectures |
| 175 | inOrderRetire= true; // true for most architectures |
| 176 | |
| 177 | // must be called after above parameters are initialized. |
| 178 | this->initializeResources(); |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | UltraSparcSchedInfo::initializeResources() |
| 183 | { |
| 184 | // Compute MachineSchedInfo::instrRUsages and MachineSchedInfo::issueGaps |
| 185 | MachineSchedInfo::initializeResources(); |
| 186 | |
| 187 | // Machine-dependent fixups go here. None for now. |
| 188 | } |
| 189 | |
| 190 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 191 | //--------------------------------------------------------------------------- |
| 192 | // class UltraSparcFrameInfo |
| 193 | // |
| 194 | // Purpose: |
| 195 | // Interface to stack frame layout info for the UltraSPARC. |
| 196 | // Note that there is no machine-independent interface to this information |
| 197 | //--------------------------------------------------------------------------- |
| 198 | |
| 199 | int |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 200 | UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& , |
| 201 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 202 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 203 | pos = false; // static stack area grows downwards |
| 204 | return StaticAreaOffsetFromFP; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | int |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 208 | UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo, |
| 209 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 210 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 211 | pos = false; // static stack area grows downwards |
| 212 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
| 213 | return StaticAreaOffsetFromFP - autoVarsSize; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | int |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 217 | UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo, |
| 218 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 219 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 220 | pos = false; // static stack area grows downwards |
| 221 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
| 222 | unsigned int spillAreaSize = mcInfo.getRegSpillsSize(); |
| 223 | return StaticAreaOffsetFromFP - (autoVarsSize + spillAreaSize); |
| 224 | } |
| 225 | |
| 226 | int |
| 227 | UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo, |
| 228 | bool& pos) const |
| 229 | { |
| 230 | // dynamic stack area grows downwards starting at top of opt-args area |
| 231 | unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize(); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 232 | return optArgsSize + FirstOptionalOutgoingArgOffsetFromSP; |
| 233 | } |
| 234 | |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 235 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 236 | //--------------------------------------------------------------------------- |
| 237 | // class UltraSparcMachine |
| 238 | // |
| 239 | // Purpose: |
| 240 | // Primary interface to machine description for the UltraSPARC. |
| 241 | // Primarily just initializes machine-dependent parameters in |
| 242 | // class TargetMachine, and creates machine-dependent subclasses |
| 243 | // for classes such as MachineInstrInfo. |
| 244 | // |
| 245 | //--------------------------------------------------------------------------- |
| 246 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 247 | UltraSparc::UltraSparc() |
| 248 | : TargetMachine("UltraSparc-Native"), |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 249 | instrInfo(*this), |
| 250 | schedInfo(*this), |
| 251 | regInfo(*this), |
Vikram S. Adve | b704840 | 2001-11-09 02:16:04 +0000 | [diff] [blame] | 252 | frameInfo(*this), |
| 253 | cacheInfo(*this) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 254 | { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 255 | optSizeForSubWordData = 4; |
| 256 | minMemOpWordSize = 8; |
| 257 | maxAtomicMemOpWordSize = 8; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 260 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 261 | void |
| 262 | ApplyPeepholeOptimizations(Method *method, TargetMachine &target) |
| 263 | { |
| 264 | return; |
| 265 | |
| 266 | // OptimizeLeafProcedures(); |
| 267 | // DeleteFallThroughBranches(); |
| 268 | // RemoveChainedBranches(); // should be folded with previous |
| 269 | // RemoveRedundantOps(); // operations with %g0, NOP, etc. |
| 270 | } |
Ruchira Sasanka | 89fb46b | 2001-09-18 22:52:44 +0000 | [diff] [blame] | 271 | |
| 272 | |
| 273 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 274 | bool |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 275 | UltraSparc::compileMethod(Method *method) |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 276 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 277 | // Construct and initialize the MachineCodeForMethod object for this method. |
| 278 | (void) MachineCodeForMethod::construct(method, *this); |
| 279 | |
| 280 | if (SelectInstructionsForMethod(method, *this)) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 281 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 282 | cerr << "Instruction selection failed for method " << method->getName() |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 283 | << "\n\n"; |
| 284 | return true; |
| 285 | } |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 286 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 287 | if (ScheduleInstructionsWithSSA(method, *this)) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 288 | { |
| 289 | cerr << "Instruction scheduling before allocation failed for method " |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 290 | << method->getName() << "\n\n"; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 291 | return true; |
| 292 | } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 293 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 294 | AllocateRegisters(method, *this); // allocate registers |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 295 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 296 | ApplyPeepholeOptimizations(method, *this); // machine-dependent peephole opts |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 297 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 298 | InsertPrologEpilog(method, *this); |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |