blob: f5ac9a5babb613fc0975e3819242b5c75246789c [file] [log] [blame]
Chris Lattner1d21f3e2002-04-09 05:21:26 +00001//===-- Sparc.cpp - General implementation file for the Sparc 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"
29#include "SparcInternals.h"
30#include "SparcTargetMachine.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...
Chris Lattner583b9d82003-12-20 09:17:40 +000037const TargetInstrDescriptor llvm::SparcMachineInstrDesc[] = {
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 },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000043#include "SparcInstr.def"
44};
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"));
Vikram S. Adve20a3be32003-08-12 15:51:02 +000062
Misha Brukman1be1a232003-11-13 00:16:28 +000063 cl::opt<bool> DumpInput("dump-input",
64 cl::desc("Print bytecode before code generation"),
65 cl::Hidden);
66}
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000067
Chris Lattner583b9d82003-12-20 09:17:40 +000068//===---------------------------------------------------------------------===//
69// Code generation/destruction passes
70//===---------------------------------------------------------------------===//
71
72namespace {
73 class ConstructMachineFunction : public FunctionPass {
74 TargetMachine &Target;
75 public:
76 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
77
78 const char *getPassName() const {
79 return "ConstructMachineFunction";
80 }
81
82 bool runOnFunction(Function &F) {
83 MachineFunction::construct(&F, Target).getInfo()->CalculateArgSize();
84 return false;
85 }
86 };
87
88 struct DestroyMachineFunction : public FunctionPass {
89 const char *getPassName() const { return "FreeMachineFunction"; }
90
91 static void freeMachineCode(Instruction &I) {
92 MachineCodeForInstruction::destroy(&I);
93 }
94
95 bool runOnFunction(Function &F) {
96 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
97 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
98 MachineCodeForInstruction::get(I).dropAllReferences();
99
100 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
101 for_each(FI->begin(), FI->end(), freeMachineCode);
102
Misha Brukman36158452003-12-22 03:47:58 +0000103 MachineFunction::destruct(&F);
Chris Lattner583b9d82003-12-20 09:17:40 +0000104 return false;
105 }
106 };
107
108 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
109 return new ConstructMachineFunction(Target);
110 }
111}
112
113FunctionPass *llvm::createSparcMachineCodeDestructionPass() {
114 return new DestroyMachineFunction();
115}
116
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000117
Chris Lattner37b18262003-12-28 09:46:33 +0000118SparcTargetMachine::SparcTargetMachine(IntrinsicLowering *il)
Chris Lattner10daaa12003-04-26 20:11:09 +0000119 : TargetMachine("UltraSparc-Native", false),
Chris Lattner37b18262003-12-28 09:46:33 +0000120 IL(il ? il : new DefaultIntrinsicLowering()),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000121 schedInfo(*this),
122 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000123 frameInfo(*this),
Chris Lattner1e60a912003-12-20 01:22:19 +0000124 cacheInfo(*this),
Chris Lattner37b18262003-12-28 09:46:33 +0000125 jitInfo(*this, *IL) {
126}
127
128SparcTargetMachine::~SparcTargetMachine() {
129 delete IL;
Chris Lattner20b1ea02001-09-14 03:47:57 +0000130}
131
Chris Lattner4f946372002-10-28 01:03:43 +0000132// addPassesToEmitAssembly - This method controls the entire code generation
133// process for the ultra sparc.
134//
Misha Brukmand71295a2003-12-17 22:04:00 +0000135bool
136SparcTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000137{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000138 // The following 3 passes used to be inserted specially by llc.
139 // Replace malloc and free instructions with library calls.
140 PM.add(createLowerAllocationsPass());
141
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000142 // Strip all of the symbols from the bytecode so that it will be smaller...
143 if (!DisableStrip)
144 PM.add(createSymbolStrippingPass());
145
Chris Lattner155e68f2003-04-23 16:24:55 +0000146 // FIXME: implement the switch instruction in the instruction selector.
147 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +0000148
149 // FIXME: implement the invoke/unwind instructions!
150 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000151
Vikram S. Adve248932b2003-08-01 15:53:24 +0000152 // decompose multi-dimensional array references into single-dim refs
153 PM.add(createDecomposeMultiDimRefsPass());
154
Chris Lattner4f946372002-10-28 01:03:43 +0000155 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000156 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000157
158 //Insert empty stackslots in the stack frame of each function
159 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
160 PM.add(createStackSlotsPass(*this));
161
Misha Brukman35188172003-11-07 20:33:25 +0000162 // Specialize LLVM code for this target machine
163 PM.add(createPreSelectionPass(*this));
164 // Run basic dataflow optimizations on LLVM code
165 PM.add(createReassociatePass());
166 PM.add(createLICMPass());
167 PM.add(createGCSEPass());
168
Vikram S. Advedeb96342003-07-29 19:58:00 +0000169 // If LLVM dumping after transformations is requested, add it to the pipeline
170 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000171 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000172 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000173
Chris Lattner37b18262003-12-28 09:46:33 +0000174 PM.add(createInstructionSelectionPass(*this, *IL));
Chris Lattner4f946372002-10-28 01:03:43 +0000175
176 if (!DisableSched)
177 PM.add(createInstructionSchedulingWithSSAPass(*this));
178
179 PM.add(getRegisterAllocator(*this));
180
Misha Brukman1be1a232003-11-13 00:16:28 +0000181 PM.add(createPrologEpilogInsertionPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000182
183 if (!DisablePeephole)
184 PM.add(createPeepholeOptsPass(*this));
185
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000186 if (EmitMappingInfo)
Brian Gaeke79c98032003-09-18 17:37:46 +0000187 PM.add(getMappingInfoAsmPrinterPass(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000188
189 // Output assembly language to the .s file. Assembly emission is split into
190 // two parts: Function output and Global value output. This is because
191 // function output is pipelined with all of the rest of code generation stuff,
192 // allowing machine code representations for functions to be free'd after the
193 // function has been emitted.
194 //
Misha Brukman1be1a232003-11-13 00:16:28 +0000195 PM.add(createAsmPrinterPass(Out, *this));
Chris Lattner37b18262003-12-28 09:46:33 +0000196 PM.add(createSparcMachineCodeDestructionPass()); // Free mem no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000197
Chris Lattner4f946372002-10-28 01:03:43 +0000198 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000199 if (EmitMappingInfo)
Misha Brukman1be1a232003-11-13 00:16:28 +0000200 PM.add(createBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000201
Chris Lattner63342052002-10-29 21:12:46 +0000202 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000203}
Misha Brukman86172ab2003-05-27 22:24:48 +0000204
205// addPassesToJITCompile - This method controls the JIT method of code
206// generation for the UltraSparc.
207//
Chris Lattner1e60a912003-12-20 01:22:19 +0000208void SparcJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
209 const TargetData &TD = TM.getTargetData();
Misha Brukmana5124502003-06-06 07:11:16 +0000210
211 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
212 TD.getPointerAlignment(), TD.getDoubleAlignment()));
213
214 // Replace malloc and free instructions with library calls.
215 // Do this after tracing until lli implements these lib calls.
216 // For now, it will emulate malloc and free internally.
217 PM.add(createLowerAllocationsPass());
218
Misha Brukman86172ab2003-05-27 22:24:48 +0000219 // FIXME: implement the switch instruction in the instruction selector.
220 PM.add(createLowerSwitchPass());
221
Chris Lattnerd324e252003-10-05 19:16:09 +0000222 // FIXME: implement the invoke/unwind instructions!
223 PM.add(createLowerInvokePass());
224
Misha Brukman58ba50f2003-08-06 23:06:21 +0000225 // decompose multi-dimensional array references into single-dim refs
226 PM.add(createDecomposeMultiDimRefsPass());
227
Misha Brukman86172ab2003-05-27 22:24:48 +0000228 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner1e60a912003-12-20 01:22:19 +0000229 PM.add(createMachineCodeConstructionPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000230
Misha Brukman35188172003-11-07 20:33:25 +0000231 // Specialize LLVM code for this target machine and then
232 // run basic dataflow optimizations on LLVM code.
Chris Lattner1e60a912003-12-20 01:22:19 +0000233 PM.add(createPreSelectionPass(TM));
Misha Brukman35188172003-11-07 20:33:25 +0000234 // Run basic dataflow optimizations on LLVM code
235 PM.add(createReassociatePass());
Misha Brukman6d2670d2003-11-08 00:01:39 +0000236
237 // FIXME: these passes crash the FunctionPassManager when being added...
238 //PM.add(createLICMPass());
239 //PM.add(createGCSEPass());
Misha Brukman35188172003-11-07 20:33:25 +0000240
Chris Lattner37b18262003-12-28 09:46:33 +0000241 PM.add(createInstructionSelectionPass(TM, IL));
Misha Brukman86172ab2003-05-27 22:24:48 +0000242
Chris Lattner1e60a912003-12-20 01:22:19 +0000243 PM.add(getRegisterAllocator(TM));
Misha Brukman1be1a232003-11-13 00:16:28 +0000244 PM.add(createPrologEpilogInsertionPass());
Misha Brukman86172ab2003-05-27 22:24:48 +0000245
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000246 if (!DisablePeephole)
Chris Lattner1e60a912003-12-20 01:22:19 +0000247 PM.add(createPeepholeOptsPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000248}
Brian Gaeked0fde302003-11-11 22:41:34 +0000249
Misha Brukmand71295a2003-12-17 22:04:00 +0000250//----------------------------------------------------------------------------
251// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
252// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
253//----------------------------------------------------------------------------
254
Chris Lattner37b18262003-12-28 09:46:33 +0000255TargetMachine *llvm::allocateSparcTargetMachine(const Module &M,
256 IntrinsicLowering *IL) {
257 return new SparcTargetMachine(IL);
Misha Brukmand71295a2003-12-17 22:04:00 +0000258}