2 //***************************************************************************
9 // 7/15/01 - Vikram Adve - Created
10 //**************************************************************************/
13 #include "SparcInternals.h"
14 #include "llvm/Target/Sparc.h"
15 #include "llvm/CodeGen/InstrScheduling.h"
16 #include "llvm/CodeGen/InstrSelection.h"
17 #include "llvm/CodeGen/PhyRegAlloc.h"
18 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
19 #include "llvm/Method.h"
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"
31 //----------------------------------------------------------------------------
32 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
33 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
34 //----------------------------------------------------------------------------
37 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
40 //----------------------------------------------------------------------------
41 // Entry point for register allocation for a module
42 //----------------------------------------------------------------------------
44 void AllocateRegisters(Method *M, TargetMachine &target)
47 if ( (M)->isExternal() ) // don't process prototypes
51 cerr << endl << "******************** Method "<< (M)->getName();
52 cerr << " ********************" <<endl;
55 MethodLiveVarInfo LVI(M ); // Analyze live varaibles
59 PhyRegAlloc PRA(M, target, &LVI); // allocate registers
60 PRA.allocateRegisters();
63 if( DEBUG_RA ) cerr << endl << "Register allocation complete!" << endl;
68 //---------------------------------------------------------------------------
69 // Function InsertPrologCode
70 // Function InsertEpilogCode
71 // Function InsertPrologEpilog
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 //---------------------------------------------------------------------------
79 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
82 InsertPrologCode(Method* method, TargetMachine& target)
84 BasicBlock* entryBB = method->getEntryNode();
85 unsigned N = GetInstructionsForProlog(entryBB, target, minstrVec);
86 assert(N <= MAX_INSTR_PER_VMINSTR);
89 MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
90 bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N);
96 InsertEpilogCode(Method* method, TargetMachine& target)
98 for (Method::iterator I=method->begin(), E=method->end(); I != E; ++I)
99 if ((*I)->getTerminator()->getOpcode() == Instruction::Ret)
101 BasicBlock* exitBB = *I;
102 unsigned N = GetInstructionsForEpilog(exitBB, target, minstrVec);
104 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
105 MachineCodeForVMInstr& termMvec =
106 exitBB->getTerminator()->getMachineInstrVec();
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)
113 assert( termMvec.back() == bbMvec.back());
118 assert(termMvec.back() == bbMvec.back());
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?");
126 // Append the epilog code to the end of the basic block.
127 bbMvec.push_back(minstrVec[0]);
132 // Insert SAVE/RESTORE instructions for the method
134 InsertPrologEpilog(Method *method, TargetMachine &target)
136 MachineCodeForMethod& mcodeInfo = MachineCodeForMethod::get(method);
137 if (mcodeInfo.isCompiledAsLeafMethod())
138 return; // nothing to do
140 InsertPrologCode(method, target);
141 InsertEpilogCode(method, target);
145 //---------------------------------------------------------------------------
146 // class UltraSparcSchedInfo
149 // Scheduling information for the UltraSPARC.
150 // Primarily just initializes machine-dependent parameters in
151 // class MachineSchedInfo.
152 //---------------------------------------------------------------------------
155 UltraSparcSchedInfo::UltraSparcSchedInfo(const TargetMachine& tgt)
156 : MachineSchedInfo(tgt,
157 (unsigned int) SPARC_NUM_SCHED_CLASSES,
159 SparcInstrUsageDeltas,
160 SparcInstrIssueDeltas,
161 sizeof(SparcInstrUsageDeltas)/sizeof(InstrRUsageDelta),
162 sizeof(SparcInstrIssueDeltas)/sizeof(InstrIssueDelta))
164 maxNumIssueTotal = 4;
165 longestIssueConflict = 0; // computed from issuesGaps[]
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
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
177 // must be called after above parameters are initialized.
178 this->initializeResources();
182 UltraSparcSchedInfo::initializeResources()
184 // Compute MachineSchedInfo::instrRUsages and MachineSchedInfo::issueGaps
185 MachineSchedInfo::initializeResources();
187 // Machine-dependent fixups go here. None for now.
191 //---------------------------------------------------------------------------
192 // class UltraSparcFrameInfo
195 // Interface to stack frame layout info for the UltraSPARC.
196 // Starting offsets for each area of the stack frame are aligned at
197 // a multiple of getStackFrameSizeAlignment().
198 //---------------------------------------------------------------------------
201 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
204 pos = false; // static stack area grows downwards
205 return StaticAreaOffsetFromFP;
209 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
212 pos = false; // static stack area grows downwards
213 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
214 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
215 autoVarsSize += (getStackFrameSizeAlignment() - mod);
216 return StaticAreaOffsetFromFP - autoVarsSize;
220 UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
223 pos = false; // static stack area grows downwards
224 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
225 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
226 int offset = autoVarsSize + spillAreaSize;
227 if (int mod = offset % getStackFrameSizeAlignment())
228 offset += (getStackFrameSizeAlignment() - mod);
229 return StaticAreaOffsetFromFP - offset;
233 UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
236 // dynamic stack area grows downwards starting at top of opt-args area
237 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
238 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
239 assert(offset % getStackFrameSizeAlignment() == 0);
244 //---------------------------------------------------------------------------
245 // class UltraSparcMachine
248 // Primary interface to machine description for the UltraSPARC.
249 // Primarily just initializes machine-dependent parameters in
250 // class TargetMachine, and creates machine-dependent subclasses
251 // for classes such as MachineInstrInfo.
253 //---------------------------------------------------------------------------
255 UltraSparc::UltraSparc()
256 : TargetMachine("UltraSparc-Native"),
263 optSizeForSubWordData = 4;
264 minMemOpWordSize = 8;
265 maxAtomicMemOpWordSize = 8;
270 ApplyPeepholeOptimizations(Method *method, TargetMachine &target)
274 // OptimizeLeafProcedures();
275 // DeleteFallThroughBranches();
276 // RemoveChainedBranches(); // should be folded with previous
277 // RemoveRedundantOps(); // operations with %g0, NOP, etc.
283 UltraSparc::compileMethod(Method *method)
285 // Construct and initialize the MachineCodeForMethod object for this method.
286 (void) MachineCodeForMethod::construct(method, *this);
288 if (SelectInstructionsForMethod(method, *this))
290 cerr << "Instruction selection failed for method " << method->getName()
295 if (ScheduleInstructionsWithSSA(method, *this))
297 cerr << "Instruction scheduling before allocation failed for method "
298 << method->getName() << "\n\n";
302 AllocateRegisters(method, *this); // allocate registers
304 ApplyPeepholeOptimizations(method, *this); // machine-dependent peephole opts
306 InsertPrologEpilog(method, *this);