1 //===-- Sparc.cpp - General implementation file for the Sparc Target ------===//
3 // This file contains the code for the Sparc Target that does not fit in any of
4 // the other files in this directory.
6 //===----------------------------------------------------------------------===//
8 #include "SparcInternals.h"
9 #include "MappingInfo.h"
10 #include "llvm/Function.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/Assembly/PrintModulePass.h"
13 #include "llvm/Transforms/Scalar.h"
14 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineFunctionInfo.h"
16 #include "llvm/CodeGen/InstrSelection.h"
17 #include "llvm/CodeGen/InstrScheduling.h"
18 #include "llvm/CodeGen/MachineCodeForInstruction.h"
19 #include "llvm/CodeGen/Passes.h"
20 #include "llvm/Target/TargetMachineImpls.h"
21 #include "Support/CommandLine.h"
23 static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
24 // Build the MachineInstruction Description Array...
25 const TargetInstrDescriptor SparcMachineInstrDesc[] = {
26 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
27 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
28 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
29 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
30 ImplicitRegUseList, ImplicitRegUseList },
31 #include "SparcInstr.def"
34 //---------------------------------------------------------------------------
35 // Command line options to control choice of code generation passes.
36 //---------------------------------------------------------------------------
38 static cl::opt<bool> DisablePreOpt("disable-preopt",
39 cl::desc("Disable optimizations prior to instruction selection"));
41 static cl::opt<bool> DisableSched("disable-sched",
42 cl::desc("Disable local scheduling pass"));
44 static cl::opt<bool> DisablePeephole("disable-peephole",
45 cl::desc("Disable peephole optimization pass"));
47 static cl::opt<bool> EmitMappingInfo("enable-maps",
48 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
50 static cl::opt<bool> DisableStrip("disable-strip",
51 cl::desc("Do not strip the LLVM bytecode included in executable"));
53 static cl::opt<bool> DumpInput("dump-input",
54 cl::desc("Print bytecode before native code generation"),
57 //----------------------------------------------------------------------------
58 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
59 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
60 //----------------------------------------------------------------------------
62 TargetMachine *allocateSparcTargetMachine(const Module &M) {
63 return new UltraSparc();
66 //---------------------------------------------------------------------------
67 // class UltraSparcFrameInfo
69 // Interface to stack frame layout info for the UltraSPARC.
70 // Starting offsets for each area of the stack frame are aligned at
71 // a multiple of getStackFrameSizeAlignment().
72 //---------------------------------------------------------------------------
75 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
78 pos = false; // static stack area grows downwards
79 return StaticAreaOffsetFromFP;
83 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
86 // ensure no more auto vars are added
87 mcInfo.getInfo()->freezeAutomaticVarsArea();
89 pos = false; // static stack area grows downwards
90 unsigned autoVarsSize = mcInfo.getInfo()->getAutomaticVarsSize();
91 return StaticAreaOffsetFromFP - autoVarsSize;
95 UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
98 MachineFunctionInfo *MFI = mcInfo.getInfo();
99 MFI->freezeAutomaticVarsArea(); // ensure no more auto vars are added
100 MFI->freezeSpillsArea(); // ensure no more spill slots are added
102 pos = false; // static stack area grows downwards
103 unsigned autoVarsSize = MFI->getAutomaticVarsSize();
104 unsigned spillAreaSize = MFI->getRegSpillsSize();
105 int offset = autoVarsSize + spillAreaSize;
106 return StaticAreaOffsetFromFP - offset;
110 UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
113 // Dynamic stack area grows downwards starting at top of opt-args area.
114 // The opt-args, required-args, and register-save areas are empty except
115 // during calls and traps, so they are shifted downwards on each
116 // dynamic-size alloca.
118 unsigned optArgsSize = mcInfo.getInfo()->getMaxOptionalArgsSize();
119 if (int extra = optArgsSize % getStackFrameSizeAlignment())
120 optArgsSize += (getStackFrameSizeAlignment() - extra);
121 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
122 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
126 //---------------------------------------------------------------------------
127 // class UltraSparcMachine
130 // Primary interface to machine description for the UltraSPARC.
131 // Primarily just initializes machine-dependent parameters in
132 // class TargetMachine, and creates machine-dependent subclasses
133 // for classes such as TargetInstrInfo.
135 //---------------------------------------------------------------------------
137 UltraSparc::UltraSparc()
138 : TargetMachine("UltraSparc-Native", false),
146 // addPassesToEmitAssembly - This method controls the entire code generation
147 // process for the ultra sparc.
149 bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
151 // The following 3 passes used to be inserted specially by llc.
152 // Replace malloc and free instructions with library calls.
153 PM.add(createLowerAllocationsPass());
155 // Strip all of the symbols from the bytecode so that it will be smaller...
157 PM.add(createSymbolStrippingPass());
159 // FIXME: implement the switch instruction in the instruction selector.
160 PM.add(createLowerSwitchPass());
162 // FIXME: implement the invoke/unwind instructions!
163 PM.add(createLowerInvokePass());
165 // decompose multi-dimensional array references into single-dim refs
166 PM.add(createDecomposeMultiDimRefsPass());
168 // Construct and initialize the MachineFunction object for this fn.
169 PM.add(createMachineCodeConstructionPass(*this));
171 //Insert empty stackslots in the stack frame of each function
172 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
173 PM.add(createStackSlotsPass(*this));
175 if (!DisablePreOpt) {
176 // Specialize LLVM code for this target machine
177 PM.add(createPreSelectionPass(*this));
178 // Run basic dataflow optimizations on LLVM code
179 PM.add(createReassociatePass());
180 PM.add(createLICMPass());
181 PM.add(createGCSEPass());
184 // If LLVM dumping after transformations is requested, add it to the pipeline
186 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
189 PM.add(createInstructionSelectionPass(*this));
192 PM.add(createInstructionSchedulingWithSSAPass(*this));
194 PM.add(getRegisterAllocator(*this));
196 PM.add(getPrologEpilogInsertionPass());
198 if (!DisablePeephole)
199 PM.add(createPeepholeOptsPass(*this));
202 PM.add(getMappingInfoAsmPrinterPass(Out));
204 // Output assembly language to the .s file. Assembly emission is split into
205 // two parts: Function output and Global value output. This is because
206 // function output is pipelined with all of the rest of code generation stuff,
207 // allowing machine code representations for functions to be free'd after the
208 // function has been emitted.
210 PM.add(getFunctionAsmPrinterPass(Out));
211 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
213 // Emit Module level assembly after all of the functions have been processed.
214 PM.add(getModuleAsmPrinterPass(Out));
216 // Emit bytecode to the assembly file into its special section next
218 PM.add(getBytecodeAsmPrinterPass(Out));
223 // addPassesToJITCompile - This method controls the JIT method of code
224 // generation for the UltraSparc.
226 bool UltraSparc::addPassesToJITCompile(FunctionPassManager &PM) {
227 const TargetData &TD = getTargetData();
229 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
230 TD.getPointerAlignment(), TD.getDoubleAlignment()));
232 // Replace malloc and free instructions with library calls.
233 // Do this after tracing until lli implements these lib calls.
234 // For now, it will emulate malloc and free internally.
235 PM.add(createLowerAllocationsPass());
237 // FIXME: implement the switch instruction in the instruction selector.
238 PM.add(createLowerSwitchPass());
240 // FIXME: implement the invoke/unwind instructions!
241 PM.add(createLowerInvokePass());
243 // decompose multi-dimensional array references into single-dim refs
244 PM.add(createDecomposeMultiDimRefsPass());
246 // Construct and initialize the MachineFunction object for this fn.
247 PM.add(createMachineCodeConstructionPass(*this));
249 PM.add(createInstructionSelectionPass(*this));
251 // new pass: convert Value* in MachineOperand to an unsigned register
252 // this brings it in line with what the X86 JIT's RegisterAllocator expects
253 //PM.add(createAddRegNumToValuesPass());
255 PM.add(getRegisterAllocator(*this));
256 PM.add(getPrologEpilogInsertionPass());
258 if (!DisablePeephole)
259 PM.add(createPeepholeOptsPass(*this));
261 return false; // success!