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));
182 if (EmitMappingInfo) {
183 PM.add(createInternalGlobalMapperPass());
184 PM.add(getMappingInfoAsmPrinterPass(Out));
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 PM.add(createAsmPrinterPass(Out, *this));
194 // Free machine-code IR which is no longer needed:
195 PM.add(createSparcV9MachineCodeDestructionPass());
197 // Emit bytecode to the assembly file into its special section next
198 if (EmitMappingInfo) {
199 // Strip all of the symbols from the bytecode so that it will be smaller...
201 PM.add(createSymbolStrippingPass());
202 PM.add(createBytecodeAsmPrinterPass(Out));
208 /// addPassesToJITCompile - This method controls the JIT method of code
209 /// generation for the UltraSparcV9.
211 void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
212 // FIXME: Implement efficient support for garbage collection intrinsics.
213 PM.add(createLowerGCPass());
215 // Replace malloc and free instructions with library calls.
216 PM.add(createLowerAllocationsPass());
218 // FIXME: implement the switch instruction in the instruction selector.
219 PM.add(createLowerSwitchPass());
221 // FIXME: implement the invoke/unwind instructions!
222 PM.add(createLowerInvokePass());
224 // decompose multi-dimensional array references into single-dim refs
225 PM.add(createDecomposeMultiDimRefsPass());
227 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
228 PM.add(createPreSelectionPass(TM));
229 PM.add(createLowerSelectPass());
231 // Run basic LLVM dataflow optimizations, to clean up after pre-selection.
232 PM.add(createReassociatePass());
233 // FIXME: these passes crash the FunctionPassManager when being added...
234 //PM.add(createLICMPass());
235 //PM.add(createGCSEPass());
237 // Construct and initialize the MachineFunction object for this fn.
238 PM.add(createMachineCodeConstructionPass(TM));
240 PM.add(createInstructionSelectionPass(TM));
242 if (PrintMachineCode)
243 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
245 PM.add(getRegisterAllocator(TM));
247 if (PrintMachineCode)
248 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
250 PM.add(createPrologEpilogInsertionPass());
252 if (!DisablePeephole)
253 PM.add(createPeepholeOptsPass(TM));
256 /// allocateSparcV9TargetMachine - Allocate and return a subclass of TargetMachine
257 /// that implements the SparcV9 backend. (the llvm/CodeGen/SparcV9.h interface)
259 TargetMachine *llvm::allocateSparcV9TargetMachine(const Module &M,
260 IntrinsicLowering *IL) {
261 return new SparcV9TargetMachine(IL);