MEGAPATCH checkin.
[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 FunctionPass {
130   TargetMachine &Target;
131 public:
132   inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
133
134   const char *getPassName() const {
135     return "Sparc ConstructMachineCodeForFunction";
136   }
137
138   bool runOnFunction(Function &F) {
139     MachineCodeForMethod::construct(&F, Target);
140     return false;
141   }
142 };
143
144 class InstructionSelection : public FunctionPass {
145   TargetMachine &Target;
146 public:
147   inline InstructionSelection(TargetMachine &T) : Target(T) {}
148   const char *getPassName() const { return "Sparc Instruction Selection"; }
149
150   bool runOnFunction(Function &F) {
151     if (SelectInstructionsForMethod(&F, Target)) {
152       cerr << "Instr selection failed for function " << F.getName() << "\n";
153       abort();
154     }
155     return false;
156   }
157 };
158
159 struct FreeMachineCodeForFunction : public FunctionPass {
160   const char *getPassName() const { return "Sparc FreeMachineCodeForFunction"; }
161
162   static void freeMachineCode(Instruction &I) {
163     MachineCodeForInstruction::destroy(&I);
164   }
165   
166   bool runOnFunction(Function &F) {
167     for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
168       for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
169         MachineCodeForInstruction::get(I).dropAllReferences();
170     
171     for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
172       for_each(FI->begin(), FI->end(), freeMachineCode);
173     
174     return false;
175   }
176 };
177
178
179
180 // addPassesToEmitAssembly - This method controls the entire code generation
181 // process for the ultra sparc.
182 //
183 void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) {
184   // Construct and initialize the MachineCodeForMethod object for this fn.
185   PM.add(new ConstructMachineCodeForFunction(*this));
186
187   PM.add(new InstructionSelection(*this));
188
189   PM.add(createInstructionSchedulingWithSSAPass(*this));
190
191   PM.add(getRegisterAllocator(*this));
192   
193   //PM.add(new OptimizeLeafProcedures());
194   //PM.add(new DeleteFallThroughBranches());
195   //PM.add(new RemoveChainedBranches());    // should be folded with previous
196   //PM.add(new RemoveRedundantOps());       // operations with %g0, NOP, etc.
197   
198   PM.add(createPrologEpilogCodeInserter(*this));
199   
200   // Output assembly language to the .s file.  Assembly emission is split into
201   // two parts: Function output and Global value output.  This is because
202   // function output is pipelined with all of the rest of code generation stuff,
203   // allowing machine code representations for functions to be free'd after the
204   // function has been emitted.
205   //
206   PM.add(getFunctionAsmPrinterPass(PM, Out));
207   PM.add(new FreeMachineCodeForFunction());  // Free stuff no longer needed
208
209   // Emit Module level assembly after all of the functions have been processed.
210   PM.add(getModuleAsmPrinterPass(PM, Out));
211
212   // Emit bytecode to the sparc assembly file into its special section next
213   PM.add(getEmitBytecodeToAsmPass(Out));
214 }