1 //===-- Sparc.cpp - General implementation file for the Sparc Target ------===//
3 // This file contains the code for the Sparc Target that does not fit in any of
4 // the other files in this directory.
6 //===----------------------------------------------------------------------===//
8 #include "SparcInternals.h"
9 #include "llvm/Target/Sparc.h"
10 #include "llvm/CodeGen/InstrScheduling.h"
11 #include "llvm/CodeGen/InstrSelection.h"
12 #include "llvm/CodeGen/MachineCodeForInstruction.h"
13 #include "llvm/CodeGen/MachineCodeForMethod.h"
14 #include "llvm/CodeGen/RegisterAllocation.h"
15 #include "llvm/Reoptimizer/Mapping/MappingInfo.h"
16 #include "llvm/Reoptimizer/Mapping/FInfo.h"
17 #include "llvm/Function.h"
18 #include "llvm/BasicBlock.h"
19 #include "llvm/PassManager.h"
23 // Build the MachineInstruction Description Array...
24 const MachineInstrDescriptor SparcMachineInstrDesc[] = {
25 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
26 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
27 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
28 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
29 #include "SparcInstr.def"
32 //----------------------------------------------------------------------------
33 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
34 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
35 //----------------------------------------------------------------------------
37 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
41 //---------------------------------------------------------------------------
42 // class UltraSparcFrameInfo
45 // Interface to stack frame layout info for the UltraSPARC.
46 // Starting offsets for each area of the stack frame are aligned at
47 // a multiple of getStackFrameSizeAlignment().
48 //---------------------------------------------------------------------------
51 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
54 pos = false; // static stack area grows downwards
55 return StaticAreaOffsetFromFP;
59 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
62 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
64 pos = false; // static stack area grows downwards
65 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
66 return StaticAreaOffsetFromFP - autoVarsSize;
70 UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
73 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
74 mcInfo.freezeSpillsArea(); // ensure no more spill slots are added
76 pos = false; // static stack area grows downwards
77 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
78 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
79 int offset = autoVarsSize + spillAreaSize;
80 return StaticAreaOffsetFromFP - offset;
84 UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
87 // Dynamic stack area grows downwards starting at top of opt-args area.
88 // The opt-args, required-args, and register-save areas are empty except
89 // during calls and traps, so they are shifted downwards on each
90 // dynamic-size alloca.
92 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
93 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
94 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
99 //---------------------------------------------------------------------------
100 // class UltraSparcMachine
103 // Primary interface to machine description for the UltraSPARC.
104 // Primarily just initializes machine-dependent parameters in
105 // class TargetMachine, and creates machine-dependent subclasses
106 // for classes such as MachineInstrInfo.
108 //---------------------------------------------------------------------------
110 UltraSparc::UltraSparc()
111 : TargetMachine("UltraSparc-Native"),
118 optSizeForSubWordData = 4;
119 minMemOpWordSize = 8;
120 maxAtomicMemOpWordSize = 8;
125 //===---------------------------------------------------------------------===//
126 // GenerateCodeForTarget Pass
128 // Native code generation for a specified target.
129 //===---------------------------------------------------------------------===//
131 class ConstructMachineCodeForFunction : public FunctionPass {
132 TargetMachine &Target;
134 inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
136 const char *getPassName() const {
137 return "Sparc ConstructMachineCodeForFunction";
140 bool runOnFunction(Function &F) {
141 MachineCodeForMethod::construct(&F, Target);
146 struct FreeMachineCodeForFunction : public FunctionPass {
147 const char *getPassName() const { return "Sparc FreeMachineCodeForFunction"; }
149 static void freeMachineCode(Instruction &I) {
150 MachineCodeForInstruction::destroy(&I);
153 bool runOnFunction(Function &F) {
154 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
155 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
156 MachineCodeForInstruction::get(I).dropAllReferences();
158 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
159 for_each(FI->begin(), FI->end(), freeMachineCode);
165 // addPassesToEmitAssembly - This method controls the entire code generation
166 // process for the ultra sparc.
168 void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
169 // Construct and initialize the MachineCodeForMethod object for this fn.
170 PM.add(new ConstructMachineCodeForFunction(*this));
172 PM.add(createInstructionSelectionPass(*this));
174 PM.add(createInstructionSchedulingWithSSAPass(*this));
176 PM.add(getRegisterAllocator(*this));
178 //PM.add(new OptimizeLeafProcedures());
179 //PM.add(new DeleteFallThroughBranches());
180 //PM.add(new RemoveChainedBranches()); // should be folded with previous
181 //PM.add(new RemoveRedundantOps()); // operations with %g0, NOP, etc.
183 PM.add(createPrologEpilogCodeInserter(*this));
185 PM.add(MappingInfoForFunction(Out));
187 // Output assembly language to the .s file. Assembly emission is split into
188 // two parts: Function output and Global value output. This is because
189 // function output is pipelined with all of the rest of code generation stuff,
190 // allowing machine code representations for functions to be free'd after the
191 // function has been emitted.
193 PM.add(getFunctionAsmPrinterPass(PM, Out));
194 PM.add(new FreeMachineCodeForFunction()); // Free stuff no longer needed
196 // Emit Module level assembly after all of the functions have been processed.
197 PM.add(getModuleAsmPrinterPass(PM, Out));
199 // Emit bytecode to the sparc assembly file into its special section next
200 PM.add(getEmitBytecodeToAsmPass(Out));
201 PM.add(getFunctionInfo(Out));