Move addPassesToEmitAssembly from TargetMachine to UltraSparc because it
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9TargetMachine.cpp
1 //===-- Sparc.cpp - General implementation file for the Sparc Target ------===//
2 //
3 // This file contains the code for the Sparc Target that does not fit in any of
4 // the other files in this directory.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "SparcInternals.h"
9 #include "llvm/Target/Sparc.h"
10 #include "llvm/Function.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/Transforms/Scalar.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/PreSelection.h"
15 #include "llvm/CodeGen/StackSlots.h"
16 #include "llvm/CodeGen/PeepholeOpts.h"
17 #include "llvm/CodeGen/InstrSelection.h"
18 #include "llvm/CodeGen/InstrScheduling.h"
19 #include "llvm/CodeGen/RegisterAllocation.h"
20 #include "llvm/CodeGen/MachineCodeForInstruction.h"
21 #include "llvm/Reoptimizer/Mapping/MappingInfo.h" 
22 #include "llvm/Reoptimizer/Mapping/FInfo.h" 
23 #include "Support/CommandLine.h"
24 using std::cerr;
25
26 // Build the MachineInstruction Description Array...
27 const MachineInstrDescriptor SparcMachineInstrDesc[] = {
28 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
29           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS)             \
30   { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE,             \
31           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
32 #include "SparcInstr.def"
33 };
34
35 //---------------------------------------------------------------------------
36 // Command line options to control choice of code generation passes.
37 //---------------------------------------------------------------------------
38
39 static cl::opt<bool> DisablePreSelect("nopreselect",
40                                       cl::desc("Disable preselection pass"));
41
42 static cl::opt<bool> DisableSched("nosched",
43                                   cl::desc("Disable local scheduling pass"));
44
45 static cl::opt<bool> DisablePeephole("nopeephole",
46                                 cl::desc("Disable peephole optimization pass"));
47
48 //----------------------------------------------------------------------------
49 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
50 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
51 //----------------------------------------------------------------------------
52
53 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
54
55
56
57 //---------------------------------------------------------------------------
58 // class UltraSparcFrameInfo 
59 // 
60 // Purpose:
61 //   Interface to stack frame layout info for the UltraSPARC.
62 //   Starting offsets for each area of the stack frame are aligned at
63 //   a multiple of getStackFrameSizeAlignment().
64 //---------------------------------------------------------------------------
65
66 int
67 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
68                                                 bool& pos) const
69 {
70   pos = false;                          // static stack area grows downwards
71   return StaticAreaOffsetFromFP;
72 }
73
74 int
75 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
76                                            bool& pos) const
77 {
78   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
79   
80   pos = false;                          // static stack area grows downwards
81   unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
82   return StaticAreaOffsetFromFP - autoVarsSize; 
83 }
84
85 int
86 UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
87                                       bool& pos) const
88 {
89   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
90   mcInfo.freezeSpillsArea();            // ensure no more spill slots are added
91   
92   pos = false;                          // static stack area grows downwards
93   unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
94   unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
95   int offset = autoVarsSize + spillAreaSize;
96   return StaticAreaOffsetFromFP - offset;
97 }
98
99 int
100 UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
101                                           bool& pos) const
102 {
103   // Dynamic stack area grows downwards starting at top of opt-args area.
104   // The opt-args, required-args, and register-save areas are empty except
105   // during calls and traps, so they are shifted downwards on each
106   // dynamic-size alloca.
107   pos = false;
108   unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
109   if (int extra = optArgsSize % getStackFrameSizeAlignment())
110     optArgsSize += (getStackFrameSizeAlignment() - extra);
111   int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
112   assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
113   return offset;
114 }
115
116 //===---------------------------------------------------------------------===//
117 // Default code generation passes.
118 // 
119 // Native code generation for a specified target.
120 //===---------------------------------------------------------------------===//
121
122 class ConstructMachineCodeForFunction : public FunctionPass {
123   TargetMachine &Target;
124 public:
125   ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
126
127   const char *getPassName() const {
128     return "ConstructMachineCodeForFunction";
129   }
130
131   bool runOnFunction(Function &F) {
132     MachineFunction::construct(&F, Target);
133     return false;
134   }
135 };
136
137 struct FreeMachineCodeForFunction : public FunctionPass {
138   const char *getPassName() const { return "FreeMachineCodeForFunction"; }
139
140   static void freeMachineCode(Instruction &I) {
141     MachineCodeForInstruction::destroy(&I);
142   }
143   
144   bool runOnFunction(Function &F) {
145     for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
146       for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
147         MachineCodeForInstruction::get(I).dropAllReferences();
148     
149     for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
150       for_each(FI->begin(), FI->end(), freeMachineCode);
151     
152     return false;
153   }
154 };
155
156
157
158 //---------------------------------------------------------------------------
159 // class UltraSparcMachine 
160 // 
161 // Purpose:
162 //   Primary interface to machine description for the UltraSPARC.
163 //   Primarily just initializes machine-dependent parameters in
164 //   class TargetMachine, and creates machine-dependent subclasses
165 //   for classes such as MachineInstrInfo. 
166 // 
167 //---------------------------------------------------------------------------
168
169 UltraSparc::UltraSparc()
170   : TargetMachine("UltraSparc-Native"),
171     instrInfo(*this),
172     schedInfo(*this),
173     regInfo(*this),
174     frameInfo(*this),
175     cacheInfo(*this),
176     optInfo(*this)
177 {
178   optSizeForSubWordData = 4;
179   minMemOpWordSize = 8; 
180   maxAtomicMemOpWordSize = 8;
181 }
182
183
184 // addPassesToEmitAssembly - This method controls the entire code generation
185 // process for the ultra sparc.
186 //
187 void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
188 {
189   // Construct and initialize the MachineFunction object for this fn.
190   PM.add(new ConstructMachineCodeForFunction(*this));
191
192   //Insert empty stackslots in the stack frame of each function
193   //so %fp+offset-8 and %fp+offset-16 are empty slots now!
194   PM.add(createStackSlotsPass(*this));
195
196   // Specialize LLVM code for this target machine and then
197   // run basic dataflow optimizations on LLVM code.
198   if (!DisablePreSelect)
199     {
200       PM.add(createPreSelectionPass(*this));
201       /* PM.add(createReassociatePass()); */
202       PM.add(createLICMPass());
203       PM.add(createGCSEPass());
204     }
205
206   PM.add(createInstructionSelectionPass(*this));
207
208   if (!DisableSched)
209     PM.add(createInstructionSchedulingWithSSAPass(*this));
210
211   PM.add(getRegisterAllocator(*this));
212
213   PM.add(getPrologEpilogInsertionPass());
214
215   if (!DisablePeephole)
216     PM.add(createPeepholeOptsPass(*this));
217
218   PM.add(MappingInfoForFunction(Out));  
219
220   // Output assembly language to the .s file.  Assembly emission is split into
221   // two parts: Function output and Global value output.  This is because
222   // function output is pipelined with all of the rest of code generation stuff,
223   // allowing machine code representations for functions to be free'd after the
224   // function has been emitted.
225   //
226   PM.add(getFunctionAsmPrinterPass(Out));
227   PM.add(new FreeMachineCodeForFunction());  // Free stuff no longer needed
228
229   // Emit Module level assembly after all of the functions have been processed.
230   PM.add(getModuleAsmPrinterPass(Out));
231
232   // Emit bytecode to the assembly file into its special section next
233   PM.add(getEmitBytecodeToAsmPass(Out));
234   PM.add(getFunctionInfo(Out)); 
235 }