de778e25628a5f1c67799fbd612648a08112895b
[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/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/Function.h"
16 #include "llvm/BasicBlock.h"
17 #include "llvm/PassManager.h"
18 #include <iostream>
19 using std::cerr;
20
21 // Build the MachineInstruction Description Array...
22 const MachineInstrDescriptor SparcMachineInstrDesc[] = {
23 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
24           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS)             \
25   { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE,             \
26           NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
27 #include "SparcInstr.def"
28 };
29
30 //----------------------------------------------------------------------------
31 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
32 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
33 //----------------------------------------------------------------------------
34
35 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
36
37
38
39 //---------------------------------------------------------------------------
40 // class UltraSparcFrameInfo 
41 // 
42 // Purpose:
43 //   Interface to stack frame layout info for the UltraSPARC.
44 //   Starting offsets for each area of the stack frame are aligned at
45 //   a multiple of getStackFrameSizeAlignment().
46 //---------------------------------------------------------------------------
47
48 int
49 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
50                                                 bool& pos) const
51 {
52   pos = false;                          // static stack area grows downwards
53   return StaticAreaOffsetFromFP;
54 }
55
56 int
57 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
58                                            bool& pos) const
59 {
60   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
61   
62   pos = false;                          // static stack area grows downwards
63   unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
64   return StaticAreaOffsetFromFP - autoVarsSize; 
65 }
66
67 int
68 UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
69                                       bool& pos) const
70 {
71   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
72   mcInfo.freezeSpillsArea();            // ensure no more spill slots are added
73   
74   pos = false;                          // static stack area grows downwards
75   unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
76   unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
77   int offset = autoVarsSize + spillAreaSize;
78   return StaticAreaOffsetFromFP - offset;
79 }
80
81 int
82 UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
83                                           bool& pos) const
84 {
85   // Dynamic stack area grows downwards starting at top of opt-args area.
86   // The opt-args, required-args, and register-save areas are empty except
87   // during calls and traps, so they are shifted downwards on each
88   // dynamic-size alloca.
89   pos = false;
90   unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
91   int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
92   assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
93   return offset;
94 }
95
96
97 //---------------------------------------------------------------------------
98 // class UltraSparcMachine 
99 // 
100 // Purpose:
101 //   Primary interface to machine description for the UltraSPARC.
102 //   Primarily just initializes machine-dependent parameters in
103 //   class TargetMachine, and creates machine-dependent subclasses
104 //   for classes such as MachineInstrInfo. 
105 // 
106 //---------------------------------------------------------------------------
107
108 UltraSparc::UltraSparc()
109   : TargetMachine("UltraSparc-Native"),
110     instrInfo(*this),
111     schedInfo(*this),
112     regInfo(*this),
113     frameInfo(*this),
114     cacheInfo(*this)
115 {
116   optSizeForSubWordData = 4;
117   minMemOpWordSize = 8; 
118   maxAtomicMemOpWordSize = 8;
119 }
120
121
122
123 //===---------------------------------------------------------------------===//
124 // GenerateCodeForTarget Pass
125 // 
126 // Native code generation for a specified target.
127 //===---------------------------------------------------------------------===//
128
129 class ConstructMachineCodeForFunction : public MethodPass {
130   TargetMachine &Target;
131 public:
132   inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
133   bool runOnMethod(Function *F) {
134     MachineCodeForMethod::construct(F, Target);
135     return false;
136   }
137 };
138
139 class InstructionSelection : public MethodPass {
140   TargetMachine &Target;
141 public:
142   inline InstructionSelection(TargetMachine &T) : Target(T) {}
143   bool runOnMethod(Function *F) {
144     if (SelectInstructionsForMethod(F, Target)) {
145       cerr << "Instr selection failed for function " << F->getName() << "\n";
146       abort();
147     }
148     return false;
149   }
150 };
151
152 struct FreeMachineCodeForFunction : public MethodPass {
153   static void freeMachineCode(Instruction *I) {
154     MachineCodeForInstruction::destroy(I);
155   }
156   
157   bool runOnMethod(Function *F) {
158     for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
159       for (BasicBlock::iterator I = (*FI)->begin(), E = (*FI)->end();
160            I != E; ++I)
161         MachineCodeForInstruction::get(*I).dropAllReferences();
162     
163     for (Function::iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
164       for (BasicBlock::iterator I = (*FI)->begin(), E = (*FI)->end();
165            I != E; ++I)
166         freeMachineCode(*I);
167     
168     return false;
169   }
170 };
171
172
173
174 // addPassesToEmitAssembly - This method controls the entire code generation
175 // process for the ultra sparc.
176 //
177 void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
178   // Construct and initialize the MachineCodeForMethod object for this fn.
179   PM.add(new ConstructMachineCodeForFunction(*this));
180
181   PM.add(new InstructionSelection(*this));
182
183   PM.add(createInstructionSchedulingWithSSAPass(*this));
184
185   PM.add(getRegisterAllocator(*this));
186   
187   //PM.add(new OptimizeLeafProcedures());
188   //PM.add(new DeleteFallThroughBranches());
189   //PM.add(new RemoveChainedBranches());    // should be folded with previous
190   //PM.add(new RemoveRedundantOps());       // operations with %g0, NOP, etc.
191   
192   PM.add(createPrologEpilogCodeInserter(*this));
193   
194   // Output assembly language to the .s file.  Assembly emission is split into
195   // two parts: Function output and Global value output.  This is because
196   // function output is pipelined with all of the rest of code generation stuff,
197   // allowing machine code representations for functions to be free'd after the
198   // function has been emitted.
199   //
200   PM.add(getMethodAsmPrinterPass(PM, Out));
201   PM.add(new FreeMachineCodeForFunction());  // Free stuff no longer needed
202
203   // Emit Module level assembly after all of the functions have been processed.
204   PM.add(getModuleAsmPrinterPass(PM, Out));
205
206   // Emit bytecode to the sparc assembly file into its special section next
207   PM.add(getEmitBytecodeToAsmPass(Out));
208 }