Silly bug fix: Machine code vector could be empty for a no-op cast instruction,
[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/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"
20 #include <iostream>
21 using std::cerr;
22
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"
30 };
31
32 //----------------------------------------------------------------------------
33 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
34 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
35 //----------------------------------------------------------------------------
36
37 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
38
39
40
41 //---------------------------------------------------------------------------
42 // class UltraSparcFrameInfo 
43 // 
44 // Purpose:
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 //---------------------------------------------------------------------------
49
50 int
51 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineCodeForMethod& ,
52                                                 bool& pos) const
53 {
54   pos = false;                          // static stack area grows downwards
55   return StaticAreaOffsetFromFP;
56 }
57
58 int
59 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineCodeForMethod& mcInfo,
60                                            bool& pos) const
61 {
62   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
63   
64   pos = false;                          // static stack area grows downwards
65   unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
66   return StaticAreaOffsetFromFP - autoVarsSize; 
67 }
68
69 int
70 UltraSparcFrameInfo::getTmpAreaOffset(MachineCodeForMethod& mcInfo,
71                                       bool& pos) const
72 {
73   mcInfo.freezeAutomaticVarsArea();     // ensure no more auto vars are added
74   mcInfo.freezeSpillsArea();            // ensure no more spill slots are added
75   
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;
81 }
82
83 int
84 UltraSparcFrameInfo::getDynamicAreaOffset(MachineCodeForMethod& mcInfo,
85                                           bool& pos) const
86 {
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.
91   pos = false;
92   unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
93   int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
94   assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
95   return offset;
96 }
97
98
99 //---------------------------------------------------------------------------
100 // class UltraSparcMachine 
101 // 
102 // Purpose:
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. 
107 // 
108 //---------------------------------------------------------------------------
109
110 UltraSparc::UltraSparc()
111   : TargetMachine("UltraSparc-Native"),
112     instrInfo(*this),
113     schedInfo(*this),
114     regInfo(*this),
115     frameInfo(*this),
116     cacheInfo(*this)
117 {
118   optSizeForSubWordData = 4;
119   minMemOpWordSize = 8; 
120   maxAtomicMemOpWordSize = 8;
121 }
122
123
124
125 //===---------------------------------------------------------------------===//
126 // GenerateCodeForTarget Pass
127 // 
128 // Native code generation for a specified target.
129 //===---------------------------------------------------------------------===//
130
131 class ConstructMachineCodeForFunction : public FunctionPass {
132   TargetMachine &Target;
133 public:
134   inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {}
135
136   const char *getPassName() const {
137     return "Sparc ConstructMachineCodeForFunction";
138   }
139
140   bool runOnFunction(Function &F) {
141     MachineCodeForMethod::construct(&F, Target);
142     return false;
143   }
144 };
145
146 struct FreeMachineCodeForFunction : public FunctionPass {
147   const char *getPassName() const { return "Sparc FreeMachineCodeForFunction"; }
148
149   static void freeMachineCode(Instruction &I) {
150     MachineCodeForInstruction::destroy(&I);
151   }
152   
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();
157     
158     for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
159       for_each(FI->begin(), FI->end(), freeMachineCode);
160     
161     return false;
162   }
163 };
164
165 // addPassesToEmitAssembly - This method controls the entire code generation
166 // process for the ultra sparc.
167 //
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));
171
172   PM.add(createInstructionSelectionPass(*this));
173
174   PM.add(createInstructionSchedulingWithSSAPass(*this));
175
176   PM.add(getRegisterAllocator(*this));
177   
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.
182   
183   PM.add(createPrologEpilogCodeInserter(*this));
184
185   PM.add(MappingInfoForFunction(Out));  
186
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.
192   //
193   PM.add(getFunctionAsmPrinterPass(PM, Out));
194   PM.add(new FreeMachineCodeForFunction());  // Free stuff no longer needed
195  
196   // Emit Module level assembly after all of the functions have been processed.
197   PM.add(getModuleAsmPrinterPass(PM, Out));
198
199   // Emit bytecode to the sparc assembly file into its special section next
200   PM.add(getEmitBytecodeToAsmPass(Out));
201   PM.add(getFunctionInfo(Out)); 
202 }
203