blob: a122e3c11be22ab7a6fd49b12018e4744758a549 [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 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/InstrSelection.h"
20#include "llvm/CodeGen/InstrScheduling.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000021#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFunctionInfo.h"
Chris Lattner4f946372002-10-28 01:03:43 +000023#include "llvm/CodeGen/MachineCodeForInstruction.h"
Chris Lattner56d22512003-09-30 22:24:00 +000024#include "llvm/CodeGen/Passes.h"
Chris Lattner48e60792003-08-13 02:38:16 +000025#include "llvm/Target/TargetMachineImpls.h"
Misha Brukmand71295a2003-12-17 22:04:00 +000026#include "llvm/Transforms/Scalar.h"
27#include "MappingInfo.h"
28#include "SparcInternals.h"
29#include "SparcTargetMachine.h"
Chris Lattner4f946372002-10-28 01:03:43 +000030#include "Support/CommandLine.h"
Ruchira Sasankae38bd5332001-09-15 00:30:44 +000031
Misha Brukmand71295a2003-12-17 22:04:00 +000032using namespace llvm;
33
Brian Gaeked0fde302003-11-11 22:41:34 +000034namespace llvm {
35
Chris Lattner6af20402002-12-03 05:41:54 +000036static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000037// Build the MachineInstruction Description Array...
Chris Lattner3501fea2003-01-14 22:00:31 +000038const TargetInstrDescriptor SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000039#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
40 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
41 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000042 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
43 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000044#include "SparcInstr.def"
45};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000046
Chris Lattner4f946372002-10-28 01:03:43 +000047//---------------------------------------------------------------------------
48// Command line options to control choice of code generation passes.
49//---------------------------------------------------------------------------
50
Misha Brukman1be1a232003-11-13 00:16:28 +000051namespace {
52 cl::opt<bool> DisableSched("disable-sched",
53 cl::desc("Disable local scheduling pass"));
Chris Lattner4f946372002-10-28 01:03:43 +000054
Misha Brukman1be1a232003-11-13 00:16:28 +000055 cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000056 cl::desc("Disable peephole optimization pass"));
57
Misha Brukman1be1a232003-11-13 00:16:28 +000058 cl::opt<bool> EmitMappingInfo("enable-maps",
59 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000060
Misha Brukman1be1a232003-11-13 00:16:28 +000061 cl::opt<bool> DisableStrip("disable-strip",
62 cl::desc("Do not strip the LLVM bytecode in executable"));
Vikram S. Adve20a3be32003-08-12 15:51:02 +000063
Misha Brukman1be1a232003-11-13 00:16:28 +000064 cl::opt<bool> DumpInput("dump-input",
65 cl::desc("Print bytecode before code generation"),
66 cl::Hidden);
67}
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000068
Misha Brukmand71295a2003-12-17 22:04:00 +000069} // End llvm namespace
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +000070
Misha Brukmand71295a2003-12-17 22:04:00 +000071SparcTargetMachine::SparcTargetMachine()
Chris Lattner10daaa12003-04-26 20:11:09 +000072 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +000073 schedInfo(*this),
74 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +000075 frameInfo(*this),
Chris Lattner1e60a912003-12-20 01:22:19 +000076 cacheInfo(*this),
77 jitInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +000078}
79
Chris Lattner4f946372002-10-28 01:03:43 +000080// addPassesToEmitAssembly - This method controls the entire code generation
81// process for the ultra sparc.
82//
Misha Brukmand71295a2003-12-17 22:04:00 +000083bool
84SparcTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +000085{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000086 // The following 3 passes used to be inserted specially by llc.
87 // Replace malloc and free instructions with library calls.
88 PM.add(createLowerAllocationsPass());
89
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000090 // Strip all of the symbols from the bytecode so that it will be smaller...
91 if (!DisableStrip)
92 PM.add(createSymbolStrippingPass());
93
Chris Lattner155e68f2003-04-23 16:24:55 +000094 // FIXME: implement the switch instruction in the instruction selector.
95 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +000096
97 // FIXME: implement the invoke/unwind instructions!
98 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000099
Vikram S. Adve248932b2003-08-01 15:53:24 +0000100 // decompose multi-dimensional array references into single-dim refs
101 PM.add(createDecomposeMultiDimRefsPass());
102
Chris Lattner4f946372002-10-28 01:03:43 +0000103 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000104 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000105
106 //Insert empty stackslots in the stack frame of each function
107 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
108 PM.add(createStackSlotsPass(*this));
109
Misha Brukman35188172003-11-07 20:33:25 +0000110 // Specialize LLVM code for this target machine
111 PM.add(createPreSelectionPass(*this));
112 // Run basic dataflow optimizations on LLVM code
113 PM.add(createReassociatePass());
114 PM.add(createLICMPass());
115 PM.add(createGCSEPass());
116
Vikram S. Advedeb96342003-07-29 19:58:00 +0000117 // If LLVM dumping after transformations is requested, add it to the pipeline
118 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000119 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000120 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000121
122 PM.add(createInstructionSelectionPass(*this));
123
124 if (!DisableSched)
125 PM.add(createInstructionSchedulingWithSSAPass(*this));
126
127 PM.add(getRegisterAllocator(*this));
128
Misha Brukman1be1a232003-11-13 00:16:28 +0000129 PM.add(createPrologEpilogInsertionPass());
Chris Lattner4f946372002-10-28 01:03:43 +0000130
131 if (!DisablePeephole)
132 PM.add(createPeepholeOptsPass(*this));
133
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000134 if (EmitMappingInfo)
Brian Gaeke79c98032003-09-18 17:37:46 +0000135 PM.add(getMappingInfoAsmPrinterPass(Out));
Chris Lattner4f946372002-10-28 01:03:43 +0000136
137 // Output assembly language to the .s file. Assembly emission is split into
138 // two parts: Function output and Global value output. This is because
139 // function output is pipelined with all of the rest of code generation stuff,
140 // allowing machine code representations for functions to be free'd after the
141 // function has been emitted.
142 //
Misha Brukman1be1a232003-11-13 00:16:28 +0000143 PM.add(createAsmPrinterPass(Out, *this));
Chris Lattner227c3d32002-10-28 01:12:41 +0000144 PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000145
Chris Lattner4f946372002-10-28 01:03:43 +0000146 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000147 if (EmitMappingInfo)
Misha Brukman1be1a232003-11-13 00:16:28 +0000148 PM.add(createBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000149
Chris Lattner63342052002-10-29 21:12:46 +0000150 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000151}
Misha Brukman86172ab2003-05-27 22:24:48 +0000152
153// addPassesToJITCompile - This method controls the JIT method of code
154// generation for the UltraSparc.
155//
Chris Lattner1e60a912003-12-20 01:22:19 +0000156void SparcJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
157 const TargetData &TD = TM.getTargetData();
Misha Brukmana5124502003-06-06 07:11:16 +0000158
159 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
160 TD.getPointerAlignment(), TD.getDoubleAlignment()));
161
162 // Replace malloc and free instructions with library calls.
163 // Do this after tracing until lli implements these lib calls.
164 // For now, it will emulate malloc and free internally.
165 PM.add(createLowerAllocationsPass());
166
Misha Brukman86172ab2003-05-27 22:24:48 +0000167 // FIXME: implement the switch instruction in the instruction selector.
168 PM.add(createLowerSwitchPass());
169
Chris Lattnerd324e252003-10-05 19:16:09 +0000170 // FIXME: implement the invoke/unwind instructions!
171 PM.add(createLowerInvokePass());
172
Misha Brukman58ba50f2003-08-06 23:06:21 +0000173 // decompose multi-dimensional array references into single-dim refs
174 PM.add(createDecomposeMultiDimRefsPass());
175
Misha Brukman86172ab2003-05-27 22:24:48 +0000176 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner1e60a912003-12-20 01:22:19 +0000177 PM.add(createMachineCodeConstructionPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000178
Misha Brukman35188172003-11-07 20:33:25 +0000179 // Specialize LLVM code for this target machine and then
180 // run basic dataflow optimizations on LLVM code.
Chris Lattner1e60a912003-12-20 01:22:19 +0000181 PM.add(createPreSelectionPass(TM));
Misha Brukman35188172003-11-07 20:33:25 +0000182 // Run basic dataflow optimizations on LLVM code
183 PM.add(createReassociatePass());
Misha Brukman6d2670d2003-11-08 00:01:39 +0000184
185 // FIXME: these passes crash the FunctionPassManager when being added...
186 //PM.add(createLICMPass());
187 //PM.add(createGCSEPass());
Misha Brukman35188172003-11-07 20:33:25 +0000188
Chris Lattner1e60a912003-12-20 01:22:19 +0000189 PM.add(createInstructionSelectionPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000190
Chris Lattner1e60a912003-12-20 01:22:19 +0000191 PM.add(getRegisterAllocator(TM));
Misha Brukman1be1a232003-11-13 00:16:28 +0000192 PM.add(createPrologEpilogInsertionPass());
Misha Brukman86172ab2003-05-27 22:24:48 +0000193
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000194 if (!DisablePeephole)
Chris Lattner1e60a912003-12-20 01:22:19 +0000195 PM.add(createPeepholeOptsPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000196}
Brian Gaeked0fde302003-11-11 22:41:34 +0000197
Misha Brukmand71295a2003-12-17 22:04:00 +0000198//----------------------------------------------------------------------------
199// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
200// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
201//----------------------------------------------------------------------------
202
Chris Lattner1e60a912003-12-20 01:22:19 +0000203TargetMachine *llvm::allocateSparcTargetMachine(const Module &M) {
Misha Brukmand71295a2003-12-17 22:04:00 +0000204 return new SparcTargetMachine();
205}