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 "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"
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"
37 //---------------------------------------------------------------------------
38 // Command line options to control choice of code generation passes.
39 //---------------------------------------------------------------------------
41 static cl::opt<bool> DisablePreSelect("nopreselect",
42 cl::desc("Disable preselection pass"));
44 static cl::opt<bool> DisableSched("nosched",
45 cl::desc("Disable local scheduling pass"));
47 static cl::opt<bool> DisablePeephole("nopeephole",
48 cl::desc("Disable peephole optimization pass"));
50 //----------------------------------------------------------------------------
51 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
52 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
53 //----------------------------------------------------------------------------
55 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
59 //---------------------------------------------------------------------------
60 // class UltraSparcFrameInfo
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 //---------------------------------------------------------------------------
69 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
72 pos = false; // static stack area grows downwards
73 return StaticAreaOffsetFromFP;
77 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
80 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
82 pos = false; // static stack area grows downwards
83 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
84 return StaticAreaOffsetFromFP - autoVarsSize;
88 UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
91 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
92 mcInfo.freezeSpillsArea(); // ensure no more spill slots are added
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;
102 UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
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.
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);
118 //---------------------------------------------------------------------------
119 // class UltraSparcMachine
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.
127 //---------------------------------------------------------------------------
129 UltraSparc::UltraSparc()
130 : TargetMachine("UltraSparc-Native", 4),
139 // addPassesToEmitAssembly - This method controls the entire code generation
140 // process for the ultra sparc.
142 bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
144 // Construct and initialize the MachineFunction object for this fn.
145 PM.add(createMachineCodeConstructionPass(*this));
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));
151 // Specialize LLVM code for this target machine and then
152 // run basic dataflow optimizations on LLVM code.
153 if (!DisablePreSelect)
155 PM.add(createPreSelectionPass(*this));
156 PM.add(createReassociatePass());
157 PM.add(createLICMPass());
158 PM.add(createGCSEPass());
161 PM.add(createInstructionSelectionPass(*this));
164 PM.add(createInstructionSchedulingWithSSAPass(*this));
166 PM.add(getRegisterAllocator(*this));
168 PM.add(getPrologEpilogInsertionPass());
170 if (!DisablePeephole)
171 PM.add(createPeepholeOptsPass(*this));
173 PM.add(MappingInfoForFunction(Out));
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.
181 PM.add(getFunctionAsmPrinterPass(Out));
182 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
184 // Emit Module level assembly after all of the functions have been processed.
185 PM.add(getModuleAsmPrinterPass(Out));
187 // Emit bytecode to the assembly file into its special section next
188 PM.add(getEmitBytecodeToAsmPass(Out));
189 PM.add(getFunctionInfo(Out));