Sparc is not little endian
[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/TargetMachineImpls.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 static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
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, 0,          \
33           ImplicitRegUseList, ImplicitRegUseList },
34 #include "SparcInstr.def"
35 };
36
37 //---------------------------------------------------------------------------
38 // Command line options to control choice of code generation passes.
39 //---------------------------------------------------------------------------
40
41 static cl::opt<bool> DisablePreSelect("nopreselect",
42                                       cl::desc("Disable preselection pass"));
43
44 static cl::opt<bool> DisableSched("nosched",
45                                   cl::desc("Disable local scheduling pass"));
46
47 static cl::opt<bool> DisablePeephole("nopeephole",
48                                 cl::desc("Disable peephole optimization pass"));
49
50 //----------------------------------------------------------------------------
51 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
52 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
53 //----------------------------------------------------------------------------
54
55 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
56
57
58
59 //---------------------------------------------------------------------------
60 // class UltraSparcFrameInfo 
61 // 
62 // Purpose:
63 //   Interface to stack frame layout info for the UltraSPARC.
64 //   Starting offsets for each area of the stack frame are aligned at
65 //   a multiple of getStackFrameSizeAlignment().
66 //---------------------------------------------------------------------------
67
68 int
69 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
70                                                 bool& pos) const
71 {
72   pos = false;                          // static stack area grows downwards
73   return StaticAreaOffsetFromFP;
74 }
75
76 int
77 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
78                                            bool& pos) const
79 {
80   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
81   
82   pos = false;                          // static stack area grows downwards
83   unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
84   return StaticAreaOffsetFromFP - autoVarsSize; 
85 }
86
87 int
88 UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
89                                       bool& pos) const
90 {
91   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
92   mcInfo.freezeSpillsArea();            // ensure no more spill slots are added
93   
94   pos = false;                          // static stack area grows downwards
95   unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
96   unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
97   int offset = autoVarsSize + spillAreaSize;
98   return StaticAreaOffsetFromFP - offset;
99 }
100
101 int
102 UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
103                                           bool& pos) const
104 {
105   // Dynamic stack area grows downwards starting at top of opt-args area.
106   // The opt-args, required-args, and register-save areas are empty except
107   // during calls and traps, so they are shifted downwards on each
108   // dynamic-size alloca.
109   pos = false;
110   unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
111   if (int extra = optArgsSize % getStackFrameSizeAlignment())
112     optArgsSize += (getStackFrameSizeAlignment() - extra);
113   int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
114   assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
115   return offset;
116 }
117
118 //---------------------------------------------------------------------------
119 // class UltraSparcMachine 
120 // 
121 // Purpose:
122 //   Primary interface to machine description for the UltraSPARC.
123 //   Primarily just initializes machine-dependent parameters in
124 //   class TargetMachine, and creates machine-dependent subclasses
125 //   for classes such as MachineInstrInfo. 
126 // 
127 //---------------------------------------------------------------------------
128
129 UltraSparc::UltraSparc()
130   : TargetMachine("UltraSparc-Native", false, 4),
131     schedInfo(*this),
132     regInfo(*this),
133     frameInfo(*this),
134     cacheInfo(*this),
135     optInfo(*this) {
136 }
137
138
139 // addPassesToEmitAssembly - This method controls the entire code generation
140 // process for the ultra sparc.
141 //
142 bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
143 {
144   // Construct and initialize the MachineFunction object for this fn.
145   PM.add(createMachineCodeConstructionPass(*this));
146
147   //Insert empty stackslots in the stack frame of each function
148   //so %fp+offset-8 and %fp+offset-16 are empty slots now!
149   PM.add(createStackSlotsPass(*this));
150
151   // Specialize LLVM code for this target machine and then
152   // run basic dataflow optimizations on LLVM code.
153   if (!DisablePreSelect)
154     {
155       PM.add(createPreSelectionPass(*this));
156       PM.add(createReassociatePass());
157       PM.add(createLICMPass());
158       PM.add(createGCSEPass());
159     }
160
161   PM.add(createInstructionSelectionPass(*this));
162
163   if (!DisableSched)
164     PM.add(createInstructionSchedulingWithSSAPass(*this));
165
166   PM.add(getRegisterAllocator(*this));
167
168   PM.add(getPrologEpilogInsertionPass());
169
170   if (!DisablePeephole)
171     PM.add(createPeepholeOptsPass(*this));
172
173   PM.add(MappingInfoForFunction(Out));  
174
175   // Output assembly language to the .s file.  Assembly emission is split into
176   // two parts: Function output and Global value output.  This is because
177   // function output is pipelined with all of the rest of code generation stuff,
178   // allowing machine code representations for functions to be free'd after the
179   // function has been emitted.
180   //
181   PM.add(getFunctionAsmPrinterPass(Out));
182   PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
183
184   // Emit Module level assembly after all of the functions have been processed.
185   PM.add(getModuleAsmPrinterPass(Out));
186
187   // Emit bytecode to the assembly file into its special section next
188   PM.add(getEmitBytecodeToAsmPass(Out));
189   PM.add(getFunctionInfo(Out)); 
190   return false;
191 }