blob: 0748018682d345aa572385362ab5d699ba40a49b [file] [log] [blame]
Brian Gaeke3ca4fcc2004-04-25 07:04:49 +00001//===-- SparcV9TargetMachine.cpp - SparcV9 Target Machine Implementation --===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00009//
Misha Brukmand71295a2003-12-17 22:04:00 +000010// 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.
Misha Brukmanb5f662f2005-04-21 23:30:14 +000013//
Chris Lattner1d21f3e2002-04-09 05:21:26 +000014//===----------------------------------------------------------------------===//
Vikram S. Adve9db43182001-10-22 13:44:23 +000015
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000016#include "llvm/Function.h"
Chris Lattner4f946372002-10-28 01:03:43 +000017#include "llvm/PassManager.h"
Chris Lattner48e60792003-08-13 02:38:16 +000018#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner4f946372002-10-28 01:03:43 +000019#include "llvm/CodeGen/InstrScheduling.h"
Chris Lattner30483732004-06-20 07:49:54 +000020#include "llvm/CodeGen/IntrinsicLowering.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000021#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner56d22512003-09-30 22:24:00 +000022#include "llvm/CodeGen/Passes.h"
Chris Lattner0cf0c372004-07-11 04:17:10 +000023#include "llvm/Target/TargetOptions.h"
Chris Lattnerd36c9702004-07-11 02:48:49 +000024#include "llvm/Target/TargetMachineRegistry.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000025#include "llvm/Transforms/Scalar.h"
Misha Brukmanb5f662f2005-04-21 23:30:14 +000026#include "MappingInfo.h"
Chris Lattner85015a02004-08-16 21:55:02 +000027#include "MachineFunctionInfo.h"
28#include "MachineCodeForInstruction.h"
Brian Gaekee3d68072004-02-25 18:44:15 +000029#include "SparcV9Internals.h"
30#include "SparcV9TargetMachine.h"
Brian Gaekeaceb7d82004-08-04 07:30:04 +000031#include "SparcV9BurgISel.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/Support/CommandLine.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000033using namespace llvm;
34
Chris Lattner6af20402002-12-03 05:41:54 +000035static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000036// Build the MachineInstruction Description Array...
Brian Gaekee3d68072004-02-25 18:44:15 +000037const TargetInstrDescriptor llvm::SparcV9MachineInstrDesc[] = {
Chris Lattner9a3d63b2001-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 Lattner6af20402002-12-03 05:41:54 +000041 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
Chris Lattnerc19d5622005-08-19 16:56:56 +000042 ImplicitRegUseList, ImplicitRegUseList, 0 },
Brian Gaekee3d68072004-02-25 18:44:15 +000043#include "SparcV9Instr.def"
Chris Lattner9a3d63b2001-09-19 15:56:23 +000044};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000045
Chris Lattner4f946372002-10-28 01:03:43 +000046//---------------------------------------------------------------------------
47// Command line options to control choice of code generation passes.
48//---------------------------------------------------------------------------
49
Brian Gaeke828c68a2004-09-30 20:20:01 +000050namespace llvm {
51 bool EmitMappingInfo = false;
52}
53
Misha Brukman1be1a232003-11-13 00:16:28 +000054namespace {
55 cl::opt<bool> DisableSched("disable-sched",
Chris Lattnerf8492192005-05-13 19:45:45 +000056 cl::desc("Disable sparcv9 local scheduling pass"));
Chris Lattner4f946372002-10-28 01:03:43 +000057
Misha Brukman1be1a232003-11-13 00:16:28 +000058 cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattnerf8492192005-05-13 19:45:45 +000059 cl::desc("Disable sparcv9 peephole optimization pass"));
Chris Lattner4f946372002-10-28 01:03:43 +000060
Chris Lattner6bc2dc72005-05-13 19:44:21 +000061 cl::opt<bool, true> EmitMappingInfoOpt("enable-maps", cl::ReallyHidden,
Brian Gaeke828c68a2004-09-30 20:20:01 +000062 cl::location(EmitMappingInfo),
63 cl::init(false),
Misha Brukman1be1a232003-11-13 00:16:28 +000064 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000065
Misha Brukmanb5f662f2005-04-21 23:30:14 +000066 cl::opt<bool> EnableModSched("enable-modsched",
Jeff Cohen9eb59ec2005-07-27 05:53:44 +000067 cl::desc("Enable modulo scheduling pass"), cl::Hidden);
Tanya Lattner770e9912005-06-17 04:00:22 +000068
69 cl::opt<bool> EnableSBModSched("enable-modschedSB",
Jeff Cohen9eb59ec2005-07-27 05:53:44 +000070 cl::desc("Enable superblock modulo scheduling (experimental)"), cl::Hidden);
Tanya Lattner75e84192004-11-18 18:38:01 +000071
Chris Lattnerd36c9702004-07-11 02:48:49 +000072 // Register the target.
Chris Lattner71d24aa2004-07-11 03:27:42 +000073 RegisterTarget<SparcV9TargetMachine> X("sparcv9", " SPARC V9");
Chris Lattnerd36c9702004-07-11 02:48:49 +000074}
75
76unsigned SparcV9TargetMachine::getJITMatchQuality() {
Misha Brukman1155d312004-09-29 23:01:17 +000077#if defined(__sparcv9)
Chris Lattnerd36c9702004-07-11 02:48:49 +000078 return 10;
79#else
80 return 0;
81#endif
82}
83
84unsigned SparcV9TargetMachine::getModuleMatchQuality(const Module &M) {
Chris Lattner3ea78c42004-12-12 17:40:28 +000085 // We strongly match "sparcv9-*".
86 std::string TT = M.getTargetTriple();
87 if (TT.size() >= 8 && std::string(TT.begin(), TT.begin()+8) == "sparcv9-")
88 return 20;
89
Chris Lattnerd36c9702004-07-11 02:48:49 +000090 if (M.getEndianness() == Module::BigEndian &&
91 M.getPointerSize() == Module::Pointer64)
Chris Lattner3ea78c42004-12-12 17:40:28 +000092 return 10; // Weak match
Chris Lattnerd36c9702004-07-11 02:48:49 +000093 else if (M.getEndianness() != Module::AnyEndianness ||
94 M.getPointerSize() != Module::AnyPointerSize)
95 return 0; // Match for some other target
96
97 return getJITMatchQuality()/2;
Misha Brukman1be1a232003-11-13 00:16:28 +000098}
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000099
Chris Lattner583b9d82003-12-20 09:17:40 +0000100//===---------------------------------------------------------------------===//
101// Code generation/destruction passes
102//===---------------------------------------------------------------------===//
103
104namespace {
105 class ConstructMachineFunction : public FunctionPass {
106 TargetMachine &Target;
107 public:
108 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000109
Chris Lattner583b9d82003-12-20 09:17:40 +0000110 const char *getPassName() const {
111 return "ConstructMachineFunction";
112 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000113
Chris Lattner583b9d82003-12-20 09:17:40 +0000114 bool runOnFunction(Function &F) {
Chris Lattnera1e51ff2004-08-18 18:13:37 +0000115 MachineFunction::construct(&F, Target).getInfo<SparcV9FunctionInfo>()->CalculateArgSize();
Chris Lattner583b9d82003-12-20 09:17:40 +0000116 return false;
117 }
118 };
119
120 struct DestroyMachineFunction : public FunctionPass {
Brian Gaeke3d1fdee2004-02-27 21:01:14 +0000121 const char *getPassName() const { return "DestroyMachineFunction"; }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000122
Chris Lattner583b9d82003-12-20 09:17:40 +0000123 static void freeMachineCode(Instruction &I) {
124 MachineCodeForInstruction::destroy(&I);
125 }
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000126
Chris Lattner583b9d82003-12-20 09:17:40 +0000127 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();
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000131
Chris Lattner583b9d82003-12-20 09:17:40 +0000132 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
133 for_each(FI->begin(), FI->end(), freeMachineCode);
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000134
Misha Brukman36158452003-12-22 03:47:58 +0000135 MachineFunction::destruct(&F);
Chris Lattner583b9d82003-12-20 09:17:40 +0000136 return false;
137 }
138 };
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000139
Chris Lattner583b9d82003-12-20 09:17:40 +0000140 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
141 return new ConstructMachineFunction(Target);
142 }
143}
144
Brian Gaekee3d68072004-02-25 18:44:15 +0000145FunctionPass *llvm::createSparcV9MachineCodeDestructionPass() {
Chris Lattner583b9d82003-12-20 09:17:40 +0000146 return new DestroyMachineFunction();
147}
148
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000149
Chris Lattnerd36c9702004-07-11 02:48:49 +0000150SparcV9TargetMachine::SparcV9TargetMachine(const Module &M,
Jim Laskeyb1e11802005-09-01 21:38:21 +0000151 IntrinsicLowering *il,
152 const std::string &FS)
Brian Gaekee3d68072004-02-25 18:44:15 +0000153 : TargetMachine("UltraSparcV9-Native", il, false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000154 schedInfo(*this),
155 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000156 frameInfo(*this),
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000157 jitInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000158}
159
Chris Lattner0431c962005-06-25 02:48:37 +0000160/// addPassesToEmitFile - This method controls the entire code generation
Misha Brukman2647c392004-02-09 23:18:42 +0000161/// process for the ultra sparc.
162///
Misha Brukmand71295a2003-12-17 22:04:00 +0000163bool
Chris Lattner0431c962005-06-25 02:48:37 +0000164SparcV9TargetMachine::addPassesToEmitFile(PassManager &PM, std::ostream &Out,
Chris Lattnerce8eb0c2005-11-08 02:11:51 +0000165 CodeGenFileType FileType,
166 bool Fast) {
Chris Lattner0431c962005-06-25 02:48:37 +0000167 if (FileType != TargetMachine::AssemblyFile) return true;
168
Chris Lattner99c59e82004-05-23 21:23:35 +0000169 // FIXME: Implement efficient support for garbage collection intrinsics.
170 PM.add(createLowerGCPass());
171
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000172 // Replace malloc and free instructions with library calls.
173 PM.add(createLowerAllocationsPass());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000174
Chris Lattnerd324e252003-10-05 19:16:09 +0000175 // FIXME: implement the invoke/unwind instructions!
176 PM.add(createLowerInvokePass());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000177
John Criswell9811ea42005-10-19 20:07:15 +0000178 // FIXME: implement the switch instruction in the instruction selector.
179 PM.add(createLowerSwitchPass());
180
Vikram S. Adve248932b2003-08-01 15:53:24 +0000181 // decompose multi-dimensional array references into single-dim refs
182 PM.add(createDecomposeMultiDimRefsPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000183
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000184 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
Misha Brukman35188172003-11-07 20:33:25 +0000185 PM.add(createPreSelectionPass(*this));
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000186 PM.add(createLowerSelectPass());
187
Brian Gaekeb9851582004-05-28 19:33:59 +0000188 // If the user's trying to read the generated code, they'll need to see the
189 // transformed input.
190 if (PrintMachineCode)
191 PM.add(new PrintModulePass());
192
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000193 // Construct and initialize the MachineFunction object for this fn.
194 PM.add(createMachineCodeConstructionPass(*this));
195
196 // Insert empty stackslots in the stack frame of each function
197 // so %fp+offset-8 and %fp+offset-16 are empty slots now!
198 PM.add(createStackSlotsPass(*this));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000199
Brian Gaekeaceb7d82004-08-04 07:30:04 +0000200 PM.add(createSparcV9BurgInstSelector(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000201
Tanya Lattner770e9912005-06-17 04:00:22 +0000202 if(!DisableSched && PrintMachineCode)
203 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before local scheduling:\n"));
204
Tanya Lattner4bcb0112005-02-10 17:02:06 +0000205 if (!DisableSched)
206 PM.add(createInstructionSchedulingWithSSAPass(*this));
207
Tanya Lattner00aaadf2005-02-24 02:14:44 +0000208 if(PrintMachineCode && EnableModSched)
Tanya Lattner75e84192004-11-18 18:38:01 +0000209 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before modulo scheduling:\n"));
Chris Lattner4f946372002-10-28 01:03:43 +0000210
Tanya Lattner75e84192004-11-18 18:38:01 +0000211 //Use ModuloScheduling if enabled, otherwise use local scheduling if not disabled.
212 if(EnableModSched)
213 PM.add(createModuloSchedulingPass(*this));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000214
Tanya Lattner770e9912005-06-17 04:00:22 +0000215 if(EnableSBModSched)
216 PM.add(createModuloSchedulingSBPass(*this));
217
Brian Gaeke215fb762004-03-12 21:19:08 +0000218 if (PrintMachineCode)
219 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
220
Chris Lattner4f946372002-10-28 01:03:43 +0000221 PM.add(getRegisterAllocator(*this));
Brian Gaeke323819e2004-03-04 19:16:23 +0000222
223 if (PrintMachineCode)
Brian Gaeke215fb762004-03-12 21:19:08 +0000224 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
Brian Gaeke323819e2004-03-04 19:16:23 +0000225
Misha Brukman1be1a232003-11-13 00:16:28 +0000226 PM.add(createPrologEpilogInsertionPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000227
228 if (!DisablePeephole)
229 PM.add(createPeepholeOptsPass(*this));
230
Brian Gaekef3210d22004-06-14 05:05:45 +0000231 if (PrintMachineCode)
232 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));
233
Brian Gaeke58215952004-06-03 05:03:01 +0000234 if (EmitMappingInfo) {
235 PM.add(createInternalGlobalMapperPass());
236 PM.add(getMappingInfoAsmPrinterPass(Out));
237 }
Chris Lattner4f946372002-10-28 01:03:43 +0000238
239 // Output assembly language to the .s file. Assembly emission is split into
240 // two parts: Function output and Global value output. This is because
241 // function output is pipelined with all of the rest of code generation stuff,
242 // allowing machine code representations for functions to be free'd after the
243 // function has been emitted.
Misha Brukman1be1a232003-11-13 00:16:28 +0000244 PM.add(createAsmPrinterPass(Out, *this));
Brian Gaeke9f78bf22004-02-27 21:15:40 +0000245
Brian Gaeke9f78bf22004-02-27 21:15:40 +0000246 // Free machine-code IR which is no longer needed:
Chris Lattnerec726a12004-03-01 15:28:27 +0000247 PM.add(createSparcV9MachineCodeDestructionPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000248
Chris Lattner4f946372002-10-28 01:03:43 +0000249 // Emit bytecode to the assembly file into its special section next
Chris Lattnerad791982004-12-02 21:05:01 +0000250 if (EmitMappingInfo)
Misha Brukman1be1a232003-11-13 00:16:28 +0000251 PM.add(createBytecodeAsmPrinterPass(Out));
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000252
Chris Lattner63342052002-10-29 21:12:46 +0000253 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000254}
Misha Brukman86172ab2003-05-27 22:24:48 +0000255
Misha Brukman2647c392004-02-09 23:18:42 +0000256/// addPassesToJITCompile - This method controls the JIT method of code
Brian Gaekee3d68072004-02-25 18:44:15 +0000257/// generation for the UltraSparcV9.
Misha Brukman2647c392004-02-09 23:18:42 +0000258///
Brian Gaekee3d68072004-02-25 18:44:15 +0000259void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
Chris Lattner99c59e82004-05-23 21:23:35 +0000260 // FIXME: Implement efficient support for garbage collection intrinsics.
261 PM.add(createLowerGCPass());
262
Misha Brukmana5124502003-06-06 07:11:16 +0000263 // Replace malloc and free instructions with library calls.
Misha Brukmana5124502003-06-06 07:11:16 +0000264 PM.add(createLowerAllocationsPass());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000265
Chris Lattnerd324e252003-10-05 19:16:09 +0000266 // FIXME: implement the invoke/unwind instructions!
267 PM.add(createLowerInvokePass());
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000268
John Criswell9811ea42005-10-19 20:07:15 +0000269 // FIXME: implement the switch instruction in the instruction selector.
270 PM.add(createLowerSwitchPass());
271
Misha Brukman58ba50f2003-08-06 23:06:21 +0000272 // decompose multi-dimensional array references into single-dim refs
273 PM.add(createDecomposeMultiDimRefsPass());
Misha Brukman86172ab2003-05-27 22:24:48 +0000274
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000275 // Lower LLVM code to the form expected by the SPARCv9 instruction selector.
Chris Lattner1e60a912003-12-20 01:22:19 +0000276 PM.add(createPreSelectionPass(TM));
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000277 PM.add(createLowerSelectPass());
278
Brian Gaekef3210d22004-06-14 05:05:45 +0000279 // If the user's trying to read the generated code, they'll need to see the
280 // transformed input.
281 if (PrintMachineCode)
282 PM.add(new PrintFunctionPass());
283
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000284 // Construct and initialize the MachineFunction object for this fn.
285 PM.add(createMachineCodeConstructionPass(TM));
286
Brian Gaekeaceb7d82004-08-04 07:30:04 +0000287 PM.add(createSparcV9BurgInstSelector(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000288
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000289 if (PrintMachineCode)
290 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
291
Chris Lattner1e60a912003-12-20 01:22:19 +0000292 PM.add(getRegisterAllocator(TM));
Brian Gaeke79ee87b2004-04-02 17:52:40 +0000293
294 if (PrintMachineCode)
295 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
296
Misha Brukman1be1a232003-11-13 00:16:28 +0000297 PM.add(createPrologEpilogInsertionPass());
Misha Brukman86172ab2003-05-27 22:24:48 +0000298
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000299 if (!DisablePeephole)
Chris Lattner1e60a912003-12-20 01:22:19 +0000300 PM.add(createPeepholeOptsPass(TM));
Brian Gaekef3210d22004-06-14 05:05:45 +0000301
302 if (PrintMachineCode)
303 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Final code:\n"));
Misha Brukman86172ab2003-05-27 22:24:48 +0000304}
Brian Gaeked0fde302003-11-11 22:41:34 +0000305