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/PassManager.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/CodeGen/InstrScheduling.h"
20 #include "llvm/CodeGen/IntrinsicLowering.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetOptions.h"
24 #include "llvm/Target/TargetMachineRegistry.h"
25 #include "llvm/Transforms/Scalar.h"
26 #include "MappingInfo.h"
27 #include "MachineFunctionInfo.h"
28 #include "MachineCodeForInstruction.h"
29 #include "SparcV9Internals.h"
30 #include "SparcV9TargetMachine.h"
31 #include "SparcV9BurgISel.h"
32 #include "llvm/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 bool EmitMappingInfo = false;
55 cl::opt<bool> DisableSched("disable-sched",
56 cl::desc("Disable sparcv9 local scheduling pass"));
58 cl::opt<bool> DisablePeephole("disable-peephole",
59 cl::desc("Disable sparcv9 peephole optimization pass"));
61 cl::opt<bool, true> EmitMappingInfoOpt("enable-maps", cl::ReallyHidden,
62 cl::location(EmitMappingInfo),
64 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
66 cl::opt<bool> EnableModSched("enable-modsched",
67 cl::desc("Enable modulo scheduling pass"), cl::Hidden);
69 cl::opt<bool> EnableSBModSched("enable-modschedSB",
70 cl::desc("Enable superblock modulo scheduling (experimental)"), cl::Hidden);
72 // Register the target.
73 RegisterTarget<SparcV9TargetMachine> X("sparcv9", " SPARC V9");
76 unsigned SparcV9TargetMachine::getJITMatchQuality() {
77 #if defined(__sparcv9)
84 unsigned SparcV9TargetMachine::getModuleMatchQuality(const Module &M) {
85 // We strongly match "sparcv9-*".
86 std::string TT = M.getTargetTriple();
87 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "sparcv9-")
90 if (M.getEndianness() == Module::BigEndian &&
91 M.getPointerSize() == Module::Pointer64)
92 return 10; // Weak match
93 else if (M.getEndianness() != Module::AnyEndianness ||
94 M.getPointerSize() != Module::AnyPointerSize)
95 return 0; // Match for some other target
97 return getJITMatchQuality()/2;
100 //===---------------------------------------------------------------------===//
101 // Code generation/destruction passes
102 //===---------------------------------------------------------------------===//
105 class ConstructMachineFunction : public FunctionPass {
106 TargetMachine &Target;
108 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
110 const char *getPassName() const {
111 return "ConstructMachineFunction";
114 bool runOnFunction(Function &F) {
115 MachineFunction::construct(&F, Target).getInfo<SparcV9FunctionInfo>()->CalculateArgSize();
120 struct DestroyMachineFunction : public FunctionPass {
121 const char *getPassName() const { return "DestroyMachineFunction"; }
123 static void freeMachineCode(Instruction &I) {
124 MachineCodeForInstruction::destroy(&I);
127 bool runOnFunction(Function &F) {
128 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
129 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
130 MachineCodeForInstruction::get(I).dropAllReferences();
132 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
133 for_each(FI->begin(), FI->end(), freeMachineCode);
135 MachineFunction::destruct(&F);
140 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
141 return new ConstructMachineFunction(Target);
145 FunctionPass *llvm::createSparcV9MachineCodeDestructionPass() {
146 return new DestroyMachineFunction();
150 SparcV9TargetMachine::SparcV9TargetMachine(const Module &M,
151 IntrinsicLowering *il)
152 : TargetMachine("UltraSparcV9-Native", il, false),
159 /// addPassesToEmitFile - This method controls the entire code generation
160 /// process for the ultra sparc.
163 SparcV9TargetMachine::addPassesToEmitFile(PassManager &PM, std::ostream &Out,
164 CodeGenFileType FileType) {
165 if (FileType != TargetMachine::AssemblyFile) return true;
167 // FIXME: Implement efficient support for garbage collection intrinsics.
168 PM.add(createLowerGCPass());
170 // Replace malloc and free instructions with library calls.
171 PM.add(createLowerAllocationsPass());
173 // FIXME: implement the switch instruction in the instruction selector.
174 PM.add(createLowerSwitchPass());
176 // FIXME: implement the invoke/unwind instructions!
177 PM.add(createLowerInvokePass());
179 // decompose multi-dimensional array references into single-dim refs
180 PM.add(createDecomposeMultiDimRefsPass());
182 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
183 PM.add(createPreSelectionPass(*this));
184 PM.add(createLowerSelectPass());
186 // If the user's trying to read the generated code, they'll need to see the
187 // transformed input.
188 if (PrintMachineCode)
189 PM.add(new PrintModulePass());
191 // Construct and initialize the MachineFunction object for this fn.
192 PM.add(createMachineCodeConstructionPass(*this));
194 // Insert empty stackslots in the stack frame of each function
195 // so %fp+offset-8 and %fp+offset-16 are empty slots now!
196 PM.add(createStackSlotsPass(*this));
198 PM.add(createSparcV9BurgInstSelector(*this));
200 if(!DisableSched && PrintMachineCode)
201 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before local scheduling:\n"));
204 PM.add(createInstructionSchedulingWithSSAPass(*this));
206 if(PrintMachineCode && EnableModSched)
207 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before modulo scheduling:\n"));
209 //Use ModuloScheduling if enabled, otherwise use local scheduling if not disabled.
211 PM.add(createModuloSchedulingPass(*this));
214 PM.add(createModuloSchedulingSBPass(*this));
216 if (PrintMachineCode)
217 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
219 PM.add(getRegisterAllocator(*this));
221 if (PrintMachineCode)
222 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
224 PM.add(createPrologEpilogInsertionPass());
226 if (!DisablePeephole)
227 PM.add(createPeepholeOptsPass(*this));
229 if (PrintMachineCode)
230 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));
232 if (EmitMappingInfo) {
233 PM.add(createInternalGlobalMapperPass());
234 PM.add(getMappingInfoAsmPrinterPass(Out));
237 // Output assembly language to the .s file. Assembly emission is split into
238 // two parts: Function output and Global value output. This is because
239 // function output is pipelined with all of the rest of code generation stuff,
240 // allowing machine code representations for functions to be free'd after the
241 // function has been emitted.
242 PM.add(createAsmPrinterPass(Out, *this));
244 // Free machine-code IR which is no longer needed:
245 PM.add(createSparcV9MachineCodeDestructionPass());
247 // Emit bytecode to the assembly file into its special section next
249 PM.add(createBytecodeAsmPrinterPass(Out));
254 /// addPassesToJITCompile - This method controls the JIT method of code
255 /// generation for the UltraSparcV9.
257 void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
258 // FIXME: Implement efficient support for garbage collection intrinsics.
259 PM.add(createLowerGCPass());
261 // Replace malloc and free instructions with library calls.
262 PM.add(createLowerAllocationsPass());
264 // FIXME: implement the switch instruction in the instruction selector.
265 PM.add(createLowerSwitchPass());
267 // FIXME: implement the invoke/unwind instructions!
268 PM.add(createLowerInvokePass());
270 // decompose multi-dimensional array references into single-dim refs
271 PM.add(createDecomposeMultiDimRefsPass());
273 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
274 PM.add(createPreSelectionPass(TM));
275 PM.add(createLowerSelectPass());
277 // If the user's trying to read the generated code, they'll need to see the
278 // transformed input.
279 if (PrintMachineCode)
280 PM.add(new PrintFunctionPass());
282 // Construct and initialize the MachineFunction object for this fn.
283 PM.add(createMachineCodeConstructionPass(TM));
285 PM.add(createSparcV9BurgInstSelector(TM));
287 if (PrintMachineCode)
288 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
290 PM.add(getRegisterAllocator(TM));
292 if (PrintMachineCode)
293 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
295 PM.add(createPrologEpilogInsertionPass());
297 if (!DisablePeephole)
298 PM.add(createPeepholeOptsPass(TM));
300 if (PrintMachineCode)
301 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));