blob: c060f75a790f84275e8e9a3c8b4c68c6d4a6294d [file] [log] [blame]
Brian Gaekee3d68072004-02-25 18:44:15 +00001//===-- SparcV9.cpp - General implementation file for the SparcV9 Target ------===//
John Criswellb576c942003-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 Brukmand71295a2003-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 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 Lattner37b18262003-12-28 09:46:33 +000017#include "llvm/IntrinsicLowering.h"
Chris Lattner4f946372002-10-28 01:03:43 +000018#include "llvm/PassManager.h"
Chris Lattner48e60792003-08-13 02:38:16 +000019#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner4f946372002-10-28 01:03:43 +000020#include "llvm/CodeGen/InstrSelection.h"
21#include "llvm/CodeGen/InstrScheduling.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner4f946372002-10-28 01:03:43 +000024#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner56d22512003-09-30 22:24:00 +000025#include "llvm/CodeGen/Passes.h"
Chris Lattner48e60792003-08-13 02:38:16 +000026#include "llvm/Target/TargetMachineImpls.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000027#include "llvm/Transforms/Scalar.h"
28#include "MappingInfo.h"
Brian Gaekee3d68072004-02-25 18:44:15 +000029#include "SparcV9Internals.h"
30#include "SparcV9TargetMachine.h"
Chris Lattner4f946372002-10-28 01:03:43 +000031#include "Support/CommandLine.h"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000032
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, \
42 ImplicitRegUseList, ImplicitRegUseList },
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
Misha Brukman1be1a232003-11-13 00:16:28 +000050namespace {
51 cl::opt<bool> DisableSched("disable-sched",
52 cl::desc("Disable local scheduling pass"));
Chris Lattner4f946372002-10-28 01:03:43 +000053
Misha Brukman1be1a232003-11-13 00:16:28 +000054 cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000055 cl::desc("Disable peephole optimization pass"));
56
Misha Brukman1be1a232003-11-13 00:16:28 +000057 cl::opt<bool> EmitMappingInfo("enable-maps",
58 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000059
Misha Brukman1be1a232003-11-13 00:16:28 +000060 cl::opt<bool> DisableStrip("disable-strip",
61 cl::desc("Do not strip the LLVM bytecode in executable"));
Misha Brukman1be1a232003-11-13 00:16:28 +000062}
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000063
Chris Lattner583b9d82003-12-20 09:17:40 +000064//===---------------------------------------------------------------------===//
65// Code generation/destruction passes
66//===---------------------------------------------------------------------===//
67
68namespace {
69 class ConstructMachineFunction : public FunctionPass {
70 TargetMachine &Target;
71 public:
72 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
73
74 const char *getPassName() const {
75 return "ConstructMachineFunction";
76 }
77
78 bool runOnFunction(Function &F) {
79 MachineFunction::construct(&F, Target).getInfo()->CalculateArgSize();
80 return false;
81 }
82 };
83
84 struct DestroyMachineFunction : public FunctionPass {
Brian Gaeke3d1fdee2004-02-27 21:01:14 +000085 const char *getPassName() const { return "DestroyMachineFunction"; }
Chris Lattner583b9d82003-12-20 09:17:40 +000086
87 static void freeMachineCode(Instruction &I) {
88 MachineCodeForInstruction::destroy(&I);
89 }
90
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();
95
96 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
97 for_each(FI->begin(), FI->end(), freeMachineCode);
98
Misha Brukman36158452003-12-22 03:47:58 +000099 MachineFunction::destruct(&F);
Chris Lattner583b9d82003-12-20 09:17:40 +0000100 return false;
101 }
102 };
103
104 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
105 return new ConstructMachineFunction(Target);
106 }
107}
108
Brian Gaekee3d68072004-02-25 18:44:15 +0000109FunctionPass *llvm::createSparcV9MachineCodeDestructionPass() {
Chris Lattner583b9d82003-12-20 09:17:40 +0000110 return new DestroyMachineFunction();
111}
112
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000113
Brian Gaekee3d68072004-02-25 18:44:15 +0000114SparcV9TargetMachine::SparcV9TargetMachine(IntrinsicLowering *il)
115 : TargetMachine("UltraSparcV9-Native", il, false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000116 schedInfo(*this),
117 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000118 frameInfo(*this),
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000119 jitInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000120}
121
Misha Brukman2647c392004-02-09 23:18:42 +0000122/// addPassesToEmitAssembly - This method controls the entire code generation
123/// process for the ultra sparc.
124///
Misha Brukmand71295a2003-12-17 22:04:00 +0000125bool
Brian Gaekee3d68072004-02-25 18:44:15 +0000126SparcV9TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000127{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000128 // The following 3 passes used to be inserted specially by llc.
129 // Replace malloc and free instructions with library calls.
130 PM.add(createLowerAllocationsPass());
131
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000132 // Strip all of the symbols from the bytecode so that it will be smaller...
133 if (!DisableStrip)
134 PM.add(createSymbolStrippingPass());
135
Chris Lattner155e68f2003-04-23 16:24:55 +0000136 // FIXME: implement the switch instruction in the instruction selector.
137 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +0000138
Chris Lattner5abd61f2004-03-30 18:41:59 +0000139 PM.add(createLowerSelectPass());
140
Chris Lattnerd324e252003-10-05 19:16:09 +0000141 // FIXME: implement the invoke/unwind instructions!
142 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000143
Vikram S. Adve248932b2003-08-01 15:53:24 +0000144 // decompose multi-dimensional array references into single-dim refs
145 PM.add(createDecomposeMultiDimRefsPass());
146
Chris Lattner4f946372002-10-28 01:03:43 +0000147 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000148 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000149
150 //Insert empty stackslots in the stack frame of each function
151 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
152 PM.add(createStackSlotsPass(*this));
153
Brian Gaekef9543262004-01-13 19:26:21 +0000154 // Specialize LLVM code for this target machine and then
155 // run basic dataflow optimizations on LLVM code.
Misha Brukman35188172003-11-07 20:33:25 +0000156 PM.add(createPreSelectionPass(*this));
Misha Brukman35188172003-11-07 20:33:25 +0000157 PM.add(createReassociatePass());
158 PM.add(createLICMPass());
159 PM.add(createGCSEPass());
160
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000161 PM.add(createInstructionSelectionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000162
163 if (!DisableSched)
164 PM.add(createInstructionSchedulingWithSSAPass(*this));
165
Brian Gaeke215fb762004-03-12 21:19:08 +0000166 if (PrintMachineCode)
167 PM.add(createMachineFunctionPrinterPass(&std::cerr, "Before reg alloc:\n"));
168
Chris Lattner4f946372002-10-28 01:03:43 +0000169 PM.add(getRegisterAllocator(*this));
Brian Gaeke323819e2004-03-04 19:16:23 +0000170
171 if (PrintMachineCode)
Brian Gaeke215fb762004-03-12 21:19:08 +0000172 PM.add(createMachineFunctionPrinterPass(&std::cerr, "After reg alloc:\n"));
Brian Gaeke323819e2004-03-04 19:16:23 +0000173
Misha Brukman1be1a232003-11-13 00:16:28 +0000174 PM.add(createPrologEpilogInsertionPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000175
176 if (!DisablePeephole)
177 PM.add(createPeepholeOptsPass(*this));
178
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000179 if (EmitMappingInfo)
Brian Gaeke79c98032003-09-18 17:37:46 +0000180 PM.add(getMappingInfoAsmPrinterPass(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000181
182 // Output assembly language to the .s file. Assembly emission is split into
183 // two parts: Function output and Global value output. This is because
184 // function output is pipelined with all of the rest of code generation stuff,
185 // allowing machine code representations for functions to be free'd after the
186 // function has been emitted.
Misha Brukman1be1a232003-11-13 00:16:28 +0000187 PM.add(createAsmPrinterPass(Out, *this));
Brian Gaeke9f78bf22004-02-27 21:15:40 +0000188
Brian Gaeke9f78bf22004-02-27 21:15:40 +0000189 // Free machine-code IR which is no longer needed:
Chris Lattnerec726a12004-03-01 15:28:27 +0000190 PM.add(createSparcV9MachineCodeDestructionPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000191
Chris Lattner4f946372002-10-28 01:03:43 +0000192 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000193 if (EmitMappingInfo)
Misha Brukman1be1a232003-11-13 00:16:28 +0000194 PM.add(createBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000195
Chris Lattner63342052002-10-29 21:12:46 +0000196 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000197}
Misha Brukman86172ab2003-05-27 22:24:48 +0000198
Misha Brukman2647c392004-02-09 23:18:42 +0000199/// addPassesToJITCompile - This method controls the JIT method of code
Brian Gaekee3d68072004-02-25 18:44:15 +0000200/// generation for the UltraSparcV9.
Misha Brukman2647c392004-02-09 23:18:42 +0000201///
Brian Gaekee3d68072004-02-25 18:44:15 +0000202void SparcV9JITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
Chris Lattner1e60a912003-12-20 01:22:19 +0000203 const TargetData &TD = TM.getTargetData();
Misha Brukmana5124502003-06-06 07:11:16 +0000204
205 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
206 TD.getPointerAlignment(), TD.getDoubleAlignment()));
207
208 // Replace malloc and free instructions with library calls.
209 // Do this after tracing until lli implements these lib calls.
210 // For now, it will emulate malloc and free internally.
211 PM.add(createLowerAllocationsPass());
212
Misha Brukman86172ab2003-05-27 22:24:48 +0000213 // FIXME: implement the switch instruction in the instruction selector.
214 PM.add(createLowerSwitchPass());
215
Chris Lattner5abd61f2004-03-30 18:41:59 +0000216 PM.add(createLowerSelectPass());
217
Chris Lattnerd324e252003-10-05 19:16:09 +0000218 // FIXME: implement the invoke/unwind instructions!
219 PM.add(createLowerInvokePass());
220
Misha Brukman58ba50f2003-08-06 23:06:21 +0000221 // decompose multi-dimensional array references into single-dim refs
222 PM.add(createDecomposeMultiDimRefsPass());
223
Misha Brukman86172ab2003-05-27 22:24:48 +0000224 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner1e60a912003-12-20 01:22:19 +0000225 PM.add(createMachineCodeConstructionPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000226
Misha Brukman35188172003-11-07 20:33:25 +0000227 // Specialize LLVM code for this target machine and then
228 // run basic dataflow optimizations on LLVM code.
Chris Lattner1e60a912003-12-20 01:22:19 +0000229 PM.add(createPreSelectionPass(TM));
Misha Brukman35188172003-11-07 20:33:25 +0000230 PM.add(createReassociatePass());
Misha Brukman6d2670d2003-11-08 00:01:39 +0000231 // FIXME: these passes crash the FunctionPassManager when being added...
232 //PM.add(createLICMPass());
233 //PM.add(createGCSEPass());
Misha Brukman35188172003-11-07 20:33:25 +0000234
Chris Lattnerf70e0c22003-12-28 21:23:38 +0000235 PM.add(createInstructionSelectionPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000236
Chris Lattner1e60a912003-12-20 01:22:19 +0000237 PM.add(getRegisterAllocator(TM));
Misha Brukman1be1a232003-11-13 00:16:28 +0000238 PM.add(createPrologEpilogInsertionPass());
Misha Brukman86172ab2003-05-27 22:24:48 +0000239
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000240 if (!DisablePeephole)
Chris Lattner1e60a912003-12-20 01:22:19 +0000241 PM.add(createPeepholeOptsPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000242}
Brian Gaeked0fde302003-11-11 22:41:34 +0000243
Brian Gaekee3d68072004-02-25 18:44:15 +0000244/// allocateSparcV9TargetMachine - Allocate and return a subclass of TargetMachine
245/// that implements the SparcV9 backend. (the llvm/CodeGen/SparcV9.h interface)
Misha Brukman2647c392004-02-09 23:18:42 +0000246///
Brian Gaekee3d68072004-02-25 18:44:15 +0000247TargetMachine *llvm::allocateSparcV9TargetMachine(const Module &M,
Chris Lattner37b18262003-12-28 09:46:33 +0000248 IntrinsicLowering *IL) {
Brian Gaekee3d68072004-02-25 18:44:15 +0000249 return new SparcV9TargetMachine(IL);
Misha Brukmand71295a2003-12-17 22:04:00 +0000250}