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/Sparc.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 // Build the MachineInstruction Description Array...
27 const MachineInstrDescriptor SparcMachineInstrDesc[] = {
28 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
29 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
30 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
31 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS },
32 #include "SparcInstr.def"
35 //---------------------------------------------------------------------------
36 // Command line options to control choice of code generation passes.
37 //---------------------------------------------------------------------------
39 static cl::opt<bool> DisablePreSelect("nopreselect",
40 cl::desc("Disable preselection pass"));
42 static cl::opt<bool> DisableSched("nosched",
43 cl::desc("Disable local scheduling pass"));
45 static cl::opt<bool> DisablePeephole("nopeephole",
46 cl::desc("Disable peephole optimization pass"));
48 //----------------------------------------------------------------------------
49 // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
50 // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
51 //----------------------------------------------------------------------------
53 TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); }
57 //---------------------------------------------------------------------------
58 // class UltraSparcFrameInfo
61 // Interface to stack frame layout info for the UltraSPARC.
62 // Starting offsets for each area of the stack frame are aligned at
63 // a multiple of getStackFrameSizeAlignment().
64 //---------------------------------------------------------------------------
67 UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& ,
70 pos = false; // static stack area grows downwards
71 return StaticAreaOffsetFromFP;
75 UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo,
78 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
80 pos = false; // static stack area grows downwards
81 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
82 return StaticAreaOffsetFromFP - autoVarsSize;
86 UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo,
89 mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added
90 mcInfo.freezeSpillsArea(); // ensure no more spill slots are added
92 pos = false; // static stack area grows downwards
93 unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize();
94 unsigned int spillAreaSize = mcInfo.getRegSpillsSize();
95 int offset = autoVarsSize + spillAreaSize;
96 return StaticAreaOffsetFromFP - offset;
100 UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo,
103 // Dynamic stack area grows downwards starting at top of opt-args area.
104 // The opt-args, required-args, and register-save areas are empty except
105 // during calls and traps, so they are shifted downwards on each
106 // dynamic-size alloca.
108 unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize();
109 if (int extra = optArgsSize % getStackFrameSizeAlignment())
110 optArgsSize += (getStackFrameSizeAlignment() - extra);
111 int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP;
112 assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0);
116 //---------------------------------------------------------------------------
117 // class UltraSparcMachine
120 // Primary interface to machine description for the UltraSPARC.
121 // Primarily just initializes machine-dependent parameters in
122 // class TargetMachine, and creates machine-dependent subclasses
123 // for classes such as MachineInstrInfo.
125 //---------------------------------------------------------------------------
127 UltraSparc::UltraSparc()
128 : TargetMachine("UltraSparc-Native"),
136 optSizeForSubWordData = 4;
137 minMemOpWordSize = 8;
138 maxAtomicMemOpWordSize = 8;
142 // addPassesToEmitAssembly - This method controls the entire code generation
143 // process for the ultra sparc.
145 void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
147 // Construct and initialize the MachineFunction object for this fn.
148 PM.add(createMachineCodeConstructionPass(*this));
150 //Insert empty stackslots in the stack frame of each function
151 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
152 PM.add(createStackSlotsPass(*this));
154 // Specialize LLVM code for this target machine and then
155 // run basic dataflow optimizations on LLVM code.
156 if (!DisablePreSelect)
158 PM.add(createPreSelectionPass(*this));
159 /* PM.add(createReassociatePass()); */
160 PM.add(createLICMPass());
161 PM.add(createGCSEPass());
164 PM.add(createInstructionSelectionPass(*this));
167 PM.add(createInstructionSchedulingWithSSAPass(*this));
169 PM.add(getRegisterAllocator(*this));
171 PM.add(getPrologEpilogInsertionPass());
173 if (!DisablePeephole)
174 PM.add(createPeepholeOptsPass(*this));
176 PM.add(MappingInfoForFunction(Out));
178 // Output assembly language to the .s file. Assembly emission is split into
179 // two parts: Function output and Global value output. This is because
180 // function output is pipelined with all of the rest of code generation stuff,
181 // allowing machine code representations for functions to be free'd after the
182 // function has been emitted.
184 PM.add(getFunctionAsmPrinterPass(Out));
185 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
187 // Emit Module level assembly after all of the functions have been processed.
188 PM.add(getModuleAsmPrinterPass(Out));
190 // Emit bytecode to the assembly file into its special section next
191 PM.add(getEmitBytecodeToAsmPass(Out));
192 PM.add(getFunctionInfo(Out));