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/MachineCodeForInstruction.h"
18 #include "llvm/CodeGen/MachineCodeForMethod.h"
19 #include "llvm/CodeGen/RegisterAllocation.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/Function.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/PassManager.h"
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"
36 //----------------------------------------------------------------------------
37 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
38 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
39 //----------------------------------------------------------------------------
42 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
45 //---------------------------------------------------------------------------
46 // class InsertPrologEpilogCode
48 // Insert SAVE/RESTORE instructions for the function
50 // Insert prolog code at the unique function entry point.
51 // Insert epilog code at each function exit point.
52 // InsertPrologEpilog invokes these only if the function is not compiled
53 // with the leaf function optimization.
55 //---------------------------------------------------------------------------
56 static MachineInstr* minstrVec[MAX_INSTR_PER_VMINSTR];
58 class InsertPrologEpilogCode : public MethodPass {
59 TargetMachine &Target;
61 inline InsertPrologEpilogCode(TargetMachine &T) : Target(T) {}
62 bool runOnMethod(Function *F) {
63 MachineCodeForMethod &mcodeInfo = MachineCodeForMethod::get(F);
64 if (!mcodeInfo.isCompiledAsLeafMethod()) {
71 void InsertPrologCode(Function *F);
72 void InsertEpilogCode(Function *F);
75 void InsertPrologEpilogCode::InsertPrologCode(Function *F)
77 BasicBlock *entryBB = F->getEntryNode();
78 unsigned N = GetInstructionsForProlog(entryBB, Target, minstrVec);
79 assert(N <= MAX_INSTR_PER_VMINSTR);
80 MachineCodeForBasicBlock& bbMvec = entryBB->getMachineInstrVec();
81 bbMvec.insert(bbMvec.begin(), minstrVec, minstrVec+N);
85 void InsertPrologEpilogCode::InsertEpilogCode(Function *F)
87 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
88 Instruction *TermInst = (Instruction*)(*I)->getTerminator();
89 if (TermInst->getOpcode() == Instruction::Ret)
91 BasicBlock* exitBB = *I;
92 unsigned N = GetInstructionsForEpilog(exitBB, Target, minstrVec);
94 MachineCodeForBasicBlock& bbMvec = exitBB->getMachineInstrVec();
95 MachineCodeForInstruction &termMvec =
96 MachineCodeForInstruction::get(TermInst);
98 // Remove the NOPs in the delay slots of the return instruction
99 const MachineInstrInfo &mii = Target.getInstrInfo();
100 unsigned numNOPs = 0;
101 while (termMvec.back()->getOpCode() == NOP)
103 assert( termMvec.back() == bbMvec.back());
108 assert(termMvec.back() == bbMvec.back());
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?");
116 // Append the epilog code to the end of the basic block.
117 bbMvec.insert(bbMvec.end(), minstrVec, minstrVec+N);
123 //---------------------------------------------------------------------------
124 // class UltraSparcFrameInfo
127 // Interface to stack frame layout info for the UltraSPARC.
128 // Starting offsets for each area of the stack frame are aligned at
129 // a multiple of getStackFrameSizeAlignment().
130 //---------------------------------------------------------------------------
133 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
136 pos = false; // static stack area grows downwards
137 return StaticAreaOffsetFromFP;
141 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
144 pos = false; // static stack area grows downwards
145 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
146 if (int mod = autoVarsSize % getStackFrameSizeAlignment())
147 autoVarsSize += (getStackFrameSizeAlignment() - mod);
148 return StaticAreaOffsetFromFP - autoVarsSize;
152 UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
155 pos = false; // static stack area grows downwards
156 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
157 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
158 int offset = autoVarsSize + spillAreaSize;
159 if (int mod = offset % getStackFrameSizeAlignment())
160 offset += (getStackFrameSizeAlignment() - mod);
161 return StaticAreaOffsetFromFP - offset;
165 UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
168 // Dynamic stack area grows downwards starting at top of opt-args area.
169 // The opt-args, required-args, and register-save areas are empty except
170 // during calls and traps, so they are shifted downwards on each
171 // dynamic-size alloca.
173 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
174 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
175 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
180 //---------------------------------------------------------------------------
181 // class UltraSparcMachine
184 // Primary interface to machine description for the UltraSPARC.
185 // Primarily just initializes machine-dependent parameters in
186 // class TargetMachine, and creates machine-dependent subclasses
187 // for classes such as MachineInstrInfo.
189 //---------------------------------------------------------------------------
191 UltraSparc::UltraSparc()
192 : TargetMachine("UltraSparc-Native"),
199 optSizeForSubWordData = 4;
200 minMemOpWordSize = 8;
201 maxAtomicMemOpWordSize = 8;
206 //===---------------------------------------------------------------------===//
207 // GenerateCodeForTarget Pass
209 // Native code generation for a specified target.
210 //===---------------------------------------------------------------------===//
212 class ConstructMachineCodeForFunction : public MethodPass {
213 TargetMachine &Target;
215 inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
216 bool runOnMethod(Function *F) {
217 MachineCodeForMethod::construct(F, Target);
222 class InstructionSelection : public MethodPass {
223 TargetMachine &Target;
225 inline InstructionSelection(TargetMachine &T) : Target(T) {}
226 bool runOnMethod(Function *F) {
227 if (SelectInstructionsForMethod(F, Target)) {
228 cerr << "Instr selection failed for function " << F->getName() << "\n";
235 struct FreeMachineCodeForFunction : public MethodPass {
236 static void freeMachineCode(Instruction *I) {
237 MachineCodeForInstruction::destroy(I);
240 bool runOnMethod(Function *F) {
241 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
242 for (BasicBlock::iterator I = (*FI)->begin(), E = (*FI)->end();
244 MachineCodeForInstruction::get(*I).dropAllReferences();
246 for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
247 for (BasicBlock::iterator I = (*FI)->begin(), E = (*FI)->end();
257 // addPassesToEmitAssembly - This method controls the entire code generation
258 // process for the ultra sparc.
260 void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
261 // Construct and initialize the MachineCodeForMethod object for this fn.
262 PM.add(new ConstructMachineCodeForFunction(*this));
264 PM.add(new InstructionSelection(*this));
266 PM.add(createInstructionSchedulingWithSSAPass(*this));
268 PM.add(getRegisterAllocator(*this));
270 //PM.add(new OptimizeLeafProcedures());
271 //PM.add(new DeleteFallThroughBranches());
272 //PM.add(new RemoveChainedBranches()); // should be folded with previous
273 //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc.
275 PM.add(new InsertPrologEpilogCode(*this));
277 // Output assembly language to the .s file. Assembly emission is split into
278 // two parts: Function output and Global value output. This is because
279 // function output is pipelined with all of the rest of code generation stuff,
280 // allowing machine code representations for functions to be free'd after the
281 // function has been emitted.
283 PM.add(getMethodAsmPrinterPass(PM, Out));
284 PM.add(new FreeMachineCodeForFunction()); // Free stuff no longer needed
286 // Emit Module level assembly after all of the functions have been processed.
287 PM.add(getModuleAsmPrinterPass(PM, Out));
289 // Emit bytecode to the sparc assembly file into its special section next
290 PM.add(getEmitBytecodeToAsmPass(Out));