1 //===-- SparcV9TargetMachine.cpp - SparcV9 Target Machine Implementation --===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Primary interface to machine description for the UltraSPARC. Primarily just
11 // initializes machine-dependent parameters in class TargetMachine, and creates
12 // machine-dependent subclasses for classes such as TargetInstrInfo.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Function.h"
17 #include "llvm/IntrinsicLowering.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Assembly/PrintModulePass.h"
20 #include "llvm/CodeGen/InstrSelection.h"
21 #include "llvm/CodeGen/InstrScheduling.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionInfo.h"
24 #include "llvm/CodeGen/MachineCodeForInstruction.h"
25 #include "llvm/CodeGen/Passes.h"
26 #include "llvm/Target/TargetMachineImpls.h"
27 #include "llvm/Transforms/Scalar.h"
28 #include "MappingInfo.h"
29 #include "SparcV9Internals.h"
30 #include "SparcV9TargetMachine.h"
31 #include "Support/CommandLine.h"
35 static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
36 // Build the MachineInstruction Description Array...
37 const TargetInstrDescriptor llvm::SparcV9MachineInstrDesc[] = {
38 #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
39 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
40 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
41 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
42 ImplicitRegUseList, ImplicitRegUseList },
43 #include "SparcV9Instr.def"
46 //---------------------------------------------------------------------------
47 // Command line options to control choice of code generation passes.
48 //---------------------------------------------------------------------------
51 cl::opt<bool> DisableSched("disable-sched",
52 cl::desc("Disable local scheduling pass"));
54 cl::opt<bool> DisablePeephole("disable-peephole",
55 cl::desc("Disable peephole optimization pass"));
57 cl::opt<bool> EmitMappingInfo("enable-maps",
58 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
60 cl::opt<bool> DisableStrip("disable-strip",
61 cl::desc("Do not strip the LLVM bytecode in executable"));
64 //===---------------------------------------------------------------------===//
65 // Code generation/destruction passes
66 //===---------------------------------------------------------------------===//
69 class ConstructMachineFunction : public FunctionPass {
70 TargetMachine &Target;
72 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
74 const char *getPassName() const {
75 return "ConstructMachineFunction";
78 bool runOnFunction(Function &F) {
79 MachineFunction::construct(&F, Target).getInfo()->CalculateArgSize();
84 struct DestroyMachineFunction : public FunctionPass {
85 const char *getPassName() const { return "DestroyMachineFunction"; }
87 static void freeMachineCode(Instruction &I) {
88 MachineCodeForInstruction::destroy(&I);
91 bool runOnFunction(Function &F) {
92 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
93 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
94 MachineCodeForInstruction::get(I).dropAllReferences();
96 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
97 for_each(FI->begin(), FI->end(), freeMachineCode);
99 MachineFunction::destruct(&F);
104 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
105 return new ConstructMachineFunction(Target);
109 FunctionPass *llvm::createSparcV9MachineCodeDestructionPass() {
110 return new DestroyMachineFunction();
114 SparcV9TargetMachine::SparcV9TargetMachine(IntrinsicLowering *il)
115 : TargetMachine("UltraSparcV9-Native", il, false),
122 /// addPassesToEmitAssembly - This method controls the entire code generation
123 /// process for the ultra sparc.
126 SparcV9TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
128 // FIXME: Implement efficient support for garbage collection intrinsics.
129 PM.add(createLowerGCPass());
131 // Replace malloc and free instructions with library calls.
132 PM.add(createLowerAllocationsPass());
134 // FIXME: implement the switch instruction in the instruction selector.
135 PM.add(createLowerSwitchPass());
137 // FIXME: implement the invoke/unwind instructions!
138 PM.add(createLowerInvokePass());
140 // decompose multi-dimensional array references into single-dim refs
141 PM.add(createDecomposeMultiDimRefsPass());
143 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
144 PM.add(createPreSelectionPass(*this));
145 PM.add(createLowerSelectPass());
147 // Run basic LLVM dataflow optimizations, to clean up after pre-selection.
148 PM.add(createReassociatePass());
149 PM.add(createLICMPass());
150 PM.add(createGCSEPass());
152 // If the user's trying to read the generated code, they'll need to see the
153 // transformed input.
154 if (PrintMachineCode)
155 PM.add(new PrintModulePass());
157 // Construct and initialize the MachineFunction object for this fn.
158 PM.add(createMachineCodeConstructionPass(*this));
160 // Insert empty stackslots in the stack frame of each function
161 // so %fp+offset-8 and %fp+offset-16 are empty slots now!
162 PM.add(createStackSlotsPass(*this));
164 PM.add(createInstructionSelectionPass(*this));
167 PM.add(createInstructionSchedulingWithSSAPass(*this));
169 if (PrintMachineCode)
170 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
172 PM.add(getRegisterAllocator(*this));
174 if (PrintMachineCode)
175 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
177 PM.add(createPrologEpilogInsertionPass());
179 if (!DisablePeephole)
180 PM.add(createPeepholeOptsPass(*this));
183 PM.add(getMappingInfoAsmPrinterPass(Out));
185 // Output assembly language to the .s file. Assembly emission is split into
186 // two parts: Function output and Global value output. This is because
187 // function output is pipelined with all of the rest of code generation stuff,
188 // allowing machine code representations for functions to be free'd after the
189 // function has been emitted.
190 PM.add(createAsmPrinterPass(Out, *this));
192 // Free machine-code IR which is no longer needed:
193 PM.add(createSparcV9MachineCodeDestructionPass());
195 // Emit bytecode to the assembly file into its special section next
196 if (EmitMappingInfo) {
197 // Strip all of the symbols from the bytecode so that it will be smaller...
199 PM.add(createSymbolStrippingPass());
200 PM.add(createBytecodeAsmPrinterPass(Out));
206 /// addPassesToJITCompile - This method controls the JIT method of code
207 /// generation for the UltraSparcV9.
209 void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
210 // FIXME: Implement efficient support for garbage collection intrinsics.
211 PM.add(createLowerGCPass());
213 // Replace malloc and free instructions with library calls.
214 PM.add(createLowerAllocationsPass());
216 // FIXME: implement the switch instruction in the instruction selector.
217 PM.add(createLowerSwitchPass());
219 // FIXME: implement the invoke/unwind instructions!
220 PM.add(createLowerInvokePass());
222 // decompose multi-dimensional array references into single-dim refs
223 PM.add(createDecomposeMultiDimRefsPass());
225 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
226 PM.add(createPreSelectionPass(TM));
227 PM.add(createLowerSelectPass());
229 // Run basic LLVM dataflow optimizations, to clean up after pre-selection.
230 PM.add(createReassociatePass());
231 // FIXME: these passes crash the FunctionPassManager when being added...
232 //PM.add(createLICMPass());
233 //PM.add(createGCSEPass());
235 // Construct and initialize the MachineFunction object for this fn.
236 PM.add(createMachineCodeConstructionPass(TM));
238 PM.add(createInstructionSelectionPass(TM));
240 if (PrintMachineCode)
241 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
243 PM.add(getRegisterAllocator(TM));
245 if (PrintMachineCode)
246 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
248 PM.add(createPrologEpilogInsertionPass());
250 if (!DisablePeephole)
251 PM.add(createPeepholeOptsPass(TM));
254 /// allocateSparcV9TargetMachine - Allocate and return a subclass of TargetMachine
255 /// that implements the SparcV9 backend. (the llvm/CodeGen/SparcV9.h interface)
257 TargetMachine *llvm::allocateSparcV9TargetMachine(const Module &M,
258 IntrinsicLowering *IL) {
259 return new SparcV9TargetMachine(IL);