blob: e660ef89d86a3811bb59898d34ace180c0c425e8 [file] [log] [blame]
Brian Gaeke03cac372004-04-25 07:04:49 +00001//===-- SparcV9TargetMachine.cpp - SparcV9 Target Machine Implementation --===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Misha Brukmanb01a80a2003-12-17 22:04:00 +00009//
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.
13//
Chris Lattner929d4a82002-04-09 05:21:26 +000014//===----------------------------------------------------------------------===//
Vikram S. Adve9090b502001-10-22 13:44:23 +000015
Chris Lattner62b7fd12002-04-07 20:49:59 +000016#include "llvm/Function.h"
Chris Lattnere94db672002-10-28 01:03:43 +000017#include "llvm/PassManager.h"
Chris Lattnerec611ae2003-08-13 02:38:16 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattnere94db672002-10-28 01:03:43 +000019#include "llvm/CodeGen/InstrScheduling.h"
Chris Lattnerbcdadf32004-06-20 07:49:54 +000020#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukmanb01a80a2003-12-17 22:04:00 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner14409022003-09-30 22:24:00 +000022#include "llvm/CodeGen/Passes.h"
Chris Lattnerc2726e62004-07-11 04:17:10 +000023#include "llvm/Target/TargetOptions.h"
Chris Lattner4266f802004-07-11 02:48:49 +000024#include "llvm/Target/TargetMachineRegistry.h"
Misha Brukmanb01a80a2003-12-17 22:04:00 +000025#include "llvm/Transforms/Scalar.h"
26#include "MappingInfo.h"
Chris Lattner9fb30a42004-08-16 21:55:02 +000027#include "MachineFunctionInfo.h"
28#include "MachineCodeForInstruction.h"
Brian Gaeke94e95d22004-02-25 18:44:15 +000029#include "SparcV9Internals.h"
30#include "SparcV9TargetMachine.h"
Brian Gaekedef3b452004-08-04 07:30:04 +000031#include "SparcV9BurgISel.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
Misha Brukmanb01a80a2003-12-17 22:04:00 +000033using namespace llvm;
34
Chris Lattnerc6d6d622002-12-03 05:41:54 +000035static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattnere86a0232001-09-19 15:56:23 +000036// Build the MachineInstruction Description Array...
Brian Gaeke94e95d22004-02-25 18:44:15 +000037const TargetInstrDescriptor llvm::SparcV9MachineInstrDesc[] = {
Chris Lattnere86a0232001-09-19 15:56:23 +000038#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
39 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
40 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattnerc6d6d622002-12-03 05:41:54 +000041 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
42 ImplicitRegUseList, ImplicitRegUseList },
Brian Gaeke94e95d22004-02-25 18:44:15 +000043#include "SparcV9Instr.def"
Chris Lattnere86a0232001-09-19 15:56:23 +000044};
Vikram S. Adve49d908a2001-09-18 13:01:29 +000045
Chris Lattnere94db672002-10-28 01:03:43 +000046//---------------------------------------------------------------------------
47// Command line options to control choice of code generation passes.
48//---------------------------------------------------------------------------
49
Brian Gaekedd9bb502004-09-30 20:20:01 +000050namespace llvm {
51 bool EmitMappingInfo = false;
52}
53
Misha Brukmanb26c6fa2003-11-13 00:16:28 +000054namespace {
55 cl::opt<bool> DisableSched("disable-sched",
56 cl::desc("Disable local scheduling pass"));
Chris Lattnere94db672002-10-28 01:03:43 +000057
Misha Brukmanb26c6fa2003-11-13 00:16:28 +000058 cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattnere94db672002-10-28 01:03:43 +000059 cl::desc("Disable peephole optimization pass"));
60
Brian Gaekedd9bb502004-09-30 20:20:01 +000061 cl::opt<bool, true> EmitMappingInfoOpt("enable-maps",
62 cl::location(EmitMappingInfo),
63 cl::init(false),
Misha Brukmanb26c6fa2003-11-13 00:16:28 +000064 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaekecf8c4f52003-06-18 21:14:23 +000065
Tanya Lattner2e4f2222004-11-19 23:34:33 +000066 cl::opt<bool> EnableModSched("enable-modsched",
67 cl::desc("Enable modulo scheduling pass instead of local scheduling"), cl::Hidden);
Tanya Lattner21590db82004-11-18 18:38:01 +000068
Chris Lattner4266f802004-07-11 02:48:49 +000069 // Register the target.
Chris Lattner73eb9432004-07-11 03:27:42 +000070 RegisterTarget<SparcV9TargetMachine> X("sparcv9", " SPARC V9");
Chris Lattner4266f802004-07-11 02:48:49 +000071}
72
73unsigned SparcV9TargetMachine::getJITMatchQuality() {
Misha Brukman4592f862004-09-29 23:01:17 +000074#if defined(__sparcv9)
Chris Lattner4266f802004-07-11 02:48:49 +000075 return 10;
76#else
77 return 0;
78#endif
79}
80
81unsigned SparcV9TargetMachine::getModuleMatchQuality(const Module &M) {
Chris Lattner17550c42004-12-12 17:40:28 +000082 // We strongly match "sparcv9-*".
83 std::string TT = M.getTargetTriple();
84 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "sparcv9-")
85 return 20;
86
Chris Lattner4266f802004-07-11 02:48:49 +000087 if (M.getEndianness() == Module::BigEndian &&
88 M.getPointerSize() == Module::Pointer64)
Chris Lattner17550c42004-12-12 17:40:28 +000089 return 10; // Weak match
Chris Lattner4266f802004-07-11 02:48:49 +000090 else if (M.getEndianness() != Module::AnyEndianness ||
91 M.getPointerSize() != Module::AnyPointerSize)
92 return 0; // Match for some other target
93
94 return getJITMatchQuality()/2;
Misha Brukmanb26c6fa2003-11-13 00:16:28 +000095}
Brian Gaekecf8c4f52003-06-18 21:14:23 +000096
Chris Lattner9a489892003-12-20 09:17:40 +000097//===---------------------------------------------------------------------===//
98// Code generation/destruction passes
99//===---------------------------------------------------------------------===//
100
101namespace {
102 class ConstructMachineFunction : public FunctionPass {
103 TargetMachine &Target;
104 public:
105 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
106
107 const char *getPassName() const {
108 return "ConstructMachineFunction";
109 }
110
111 bool runOnFunction(Function &F) {
Chris Lattner1ea6d202004-08-18 18:13:37 +0000112 MachineFunction::construct(&F, Target).getInfo<SparcV9FunctionInfo>()->CalculateArgSize();
Chris Lattner9a489892003-12-20 09:17:40 +0000113 return false;
114 }
115 };
116
117 struct DestroyMachineFunction : public FunctionPass {
Brian Gaeke50d19bf2004-02-27 21:01:14 +0000118 const char *getPassName() const { return "DestroyMachineFunction"; }
Chris Lattner9a489892003-12-20 09:17:40 +0000119
120 static void freeMachineCode(Instruction &I) {
121 MachineCodeForInstruction::destroy(&I);
122 }
123
124 bool runOnFunction(Function &F) {
125 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
126 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
127 MachineCodeForInstruction::get(I).dropAllReferences();
128
129 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
130 for_each(FI->begin(), FI->end(), freeMachineCode);
131
Misha Brukman69aa1cc2003-12-22 03:47:58 +0000132 MachineFunction::destruct(&F);
Chris Lattner9a489892003-12-20 09:17:40 +0000133 return false;
134 }
135 };
136
137 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
138 return new ConstructMachineFunction(Target);
139 }
140}
141
Brian Gaeke94e95d22004-02-25 18:44:15 +0000142FunctionPass *llvm::createSparcV9MachineCodeDestructionPass() {
Chris Lattner9a489892003-12-20 09:17:40 +0000143 return new DestroyMachineFunction();
144}
145
Ruchira Sasanka086bf0f2001-10-15 16:25:28 +0000146
Chris Lattner4266f802004-07-11 02:48:49 +0000147SparcV9TargetMachine::SparcV9TargetMachine(const Module &M,
148 IntrinsicLowering *il)
Brian Gaeke94e95d22004-02-25 18:44:15 +0000149 : TargetMachine("UltraSparcV9-Native", il, false),
Vikram S. Adve6fcb2892001-11-08 04:55:13 +0000150 schedInfo(*this),
151 regInfo(*this),
Vikram S. Advef34fa692001-11-09 02:16:04 +0000152 frameInfo(*this),
Chris Lattner5d236002003-12-28 21:23:38 +0000153 jitInfo(*this) {
Chris Lattnerb0ddffa2001-09-14 03:47:57 +0000154}
155
Misha Brukman7f7a8422004-02-09 23:18:42 +0000156/// addPassesToEmitAssembly - This method controls the entire code generation
157/// process for the ultra sparc.
158///
Misha Brukmanb01a80a2003-12-17 22:04:00 +0000159bool
Brian Gaeke94e95d22004-02-25 18:44:15 +0000160SparcV9TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattnere94db672002-10-28 01:03:43 +0000161{
Chris Lattner6e4edd62004-05-23 21:23:35 +0000162 // FIXME: Implement efficient support for garbage collection intrinsics.
163 PM.add(createLowerGCPass());
164
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000165 // Replace malloc and free instructions with library calls.
166 PM.add(createLowerAllocationsPass());
167
Chris Lattner524608a2003-04-23 16:24:55 +0000168 // FIXME: implement the switch instruction in the instruction selector.
169 PM.add(createLowerSwitchPass());
Chris Lattnerfb253742003-10-05 19:16:09 +0000170
171 // FIXME: implement the invoke/unwind instructions!
172 PM.add(createLowerInvokePass());
Brian Gaekecf8c4f52003-06-18 21:14:23 +0000173
Vikram S. Adve36fcc5d2003-08-01 15:53:24 +0000174 // decompose multi-dimensional array references into single-dim refs
175 PM.add(createDecomposeMultiDimRefsPass());
Chris Lattnere94db672002-10-28 01:03:43 +0000176
Brian Gaeke28459362004-04-02 17:52:40 +0000177 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
Misha Brukman3b2343e2003-11-07 20:33:25 +0000178 PM.add(createPreSelectionPass(*this));
Brian Gaeke28459362004-04-02 17:52:40 +0000179 PM.add(createLowerSelectPass());
180
Brian Gaeke26925ff2004-05-28 19:33:59 +0000181 // If the user's trying to read the generated code, they'll need to see the
182 // transformed input.
183 if (PrintMachineCode)
184 PM.add(new PrintModulePass());
185
Brian Gaeke28459362004-04-02 17:52:40 +0000186 // Construct and initialize the MachineFunction object for this fn.
187 PM.add(createMachineCodeConstructionPass(*this));
188
189 // Insert empty stackslots in the stack frame of each function
190 // so %fp+offset-8 and %fp+offset-16 are empty slots now!
191 PM.add(createStackSlotsPass(*this));
192
Brian Gaekedef3b452004-08-04 07:30:04 +0000193 PM.add(createSparcV9BurgInstSelector(*this));
Chris Lattnere94db672002-10-28 01:03:43 +0000194
Tanya Lattner21590db82004-11-18 18:38:01 +0000195 if(PrintMachineCode)
196 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before modulo scheduling:\n"));
Chris Lattnere94db672002-10-28 01:03:43 +0000197
Tanya Lattner21590db82004-11-18 18:38:01 +0000198 //Use ModuloScheduling if enabled, otherwise use local scheduling if not disabled.
199 if(EnableModSched)
200 PM.add(createModuloSchedulingPass(*this));
201 else {
202 if (!DisableSched)
203 PM.add(createInstructionSchedulingWithSSAPass(*this));
204 }
205
Brian Gaeke1694daf2004-03-12 21:19:08 +0000206 if (PrintMachineCode)
207 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
208
Chris Lattnere94db672002-10-28 01:03:43 +0000209 PM.add(getRegisterAllocator(*this));
Brian Gaeke8351d8c2004-03-04 19:16:23 +0000210
211 if (PrintMachineCode)
Brian Gaeke1694daf2004-03-12 21:19:08 +0000212 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
Brian Gaeke8351d8c2004-03-04 19:16:23 +0000213
Misha Brukmanb26c6fa2003-11-13 00:16:28 +0000214 PM.add(createPrologEpilogInsertionPass());
Chris Lattnere94db672002-10-28 01:03:43 +0000215
216 if (!DisablePeephole)
217 PM.add(createPeepholeOptsPass(*this));
218
Brian Gaeke99c65392004-06-14 05:05:45 +0000219 if (PrintMachineCode)
220 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));
221
Brian Gaeke0a48c0d2004-06-03 05:03:01 +0000222 if (EmitMappingInfo) {
223 PM.add(createInternalGlobalMapperPass());
224 PM.add(getMappingInfoAsmPrinterPass(Out));
225 }
Chris Lattnere94db672002-10-28 01:03:43 +0000226
227 // Output assembly language to the .s file. Assembly emission is split into
228 // two parts: Function output and Global value output. This is because
229 // function output is pipelined with all of the rest of code generation stuff,
230 // allowing machine code representations for functions to be free'd after the
231 // function has been emitted.
Misha Brukmanb26c6fa2003-11-13 00:16:28 +0000232 PM.add(createAsmPrinterPass(Out, *this));
Brian Gaekeffeef642004-02-27 21:15:40 +0000233
Brian Gaekeffeef642004-02-27 21:15:40 +0000234 // Free machine-code IR which is no longer needed:
Chris Lattnerf96405a2004-03-01 15:28:27 +0000235 PM.add(createSparcV9MachineCodeDestructionPass());
Chris Lattnere94db672002-10-28 01:03:43 +0000236
Chris Lattnere94db672002-10-28 01:03:43 +0000237 // Emit bytecode to the assembly file into its special section next
Chris Lattner76516142004-12-02 21:05:01 +0000238 if (EmitMappingInfo)
Misha Brukmanb26c6fa2003-11-13 00:16:28 +0000239 PM.add(createBytecodeAsmPrinterPass(Out));
Brian Gaeke28459362004-04-02 17:52:40 +0000240
Chris Lattner5667f0e2002-10-29 21:12:46 +0000241 return false;
Chris Lattnere94db672002-10-28 01:03:43 +0000242}
Misha Brukman81104392003-05-27 22:24:48 +0000243
Misha Brukman7f7a8422004-02-09 23:18:42 +0000244/// addPassesToJITCompile - This method controls the JIT method of code
Brian Gaeke94e95d22004-02-25 18:44:15 +0000245/// generation for the UltraSparcV9.
Misha Brukman7f7a8422004-02-09 23:18:42 +0000246///
Brian Gaeke94e95d22004-02-25 18:44:15 +0000247void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
Chris Lattner6e4edd62004-05-23 21:23:35 +0000248 // FIXME: Implement efficient support for garbage collection intrinsics.
249 PM.add(createLowerGCPass());
250
Misha Brukman0941cd12003-06-06 07:11:16 +0000251 // Replace malloc and free instructions with library calls.
Misha Brukman0941cd12003-06-06 07:11:16 +0000252 PM.add(createLowerAllocationsPass());
Brian Gaeke28459362004-04-02 17:52:40 +0000253
Misha Brukman81104392003-05-27 22:24:48 +0000254 // FIXME: implement the switch instruction in the instruction selector.
255 PM.add(createLowerSwitchPass());
256
Chris Lattnerfb253742003-10-05 19:16:09 +0000257 // FIXME: implement the invoke/unwind instructions!
258 PM.add(createLowerInvokePass());
Brian Gaeke28459362004-04-02 17:52:40 +0000259
Misha Brukman8f189382003-08-06 23:06:21 +0000260 // decompose multi-dimensional array references into single-dim refs
261 PM.add(createDecomposeMultiDimRefsPass());
Misha Brukman81104392003-05-27 22:24:48 +0000262
Brian Gaeke28459362004-04-02 17:52:40 +0000263 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
Chris Lattner833c3c22003-12-20 01:22:19 +0000264 PM.add(createPreSelectionPass(TM));
Brian Gaeke28459362004-04-02 17:52:40 +0000265 PM.add(createLowerSelectPass());
266
Brian Gaeke99c65392004-06-14 05:05:45 +0000267 // If the user's trying to read the generated code, they'll need to see the
268 // transformed input.
269 if (PrintMachineCode)
270 PM.add(new PrintFunctionPass());
271
Brian Gaeke28459362004-04-02 17:52:40 +0000272 // Construct and initialize the MachineFunction object for this fn.
273 PM.add(createMachineCodeConstructionPass(TM));
274
Brian Gaekedef3b452004-08-04 07:30:04 +0000275 PM.add(createSparcV9BurgInstSelector(TM));
Misha Brukman81104392003-05-27 22:24:48 +0000276
Brian Gaeke28459362004-04-02 17:52:40 +0000277 if (PrintMachineCode)
278 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
279
Chris Lattner833c3c22003-12-20 01:22:19 +0000280 PM.add(getRegisterAllocator(TM));
Brian Gaeke28459362004-04-02 17:52:40 +0000281
282 if (PrintMachineCode)
283 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
284
Misha Brukmanb26c6fa2003-11-13 00:16:28 +0000285 PM.add(createPrologEpilogInsertionPass());
Misha Brukman81104392003-05-27 22:24:48 +0000286
Misha Brukmanc1830a42003-05-30 20:00:13 +0000287 if (!DisablePeephole)
Chris Lattner833c3c22003-12-20 01:22:19 +0000288 PM.add(createPeepholeOptsPass(TM));
Brian Gaeke99c65392004-06-14 05:05:45 +0000289
290 if (PrintMachineCode)
291 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));
Misha Brukman81104392003-05-27 22:24:48 +0000292}
Brian Gaeke960707c2003-11-11 22:41:34 +0000293