blob: 505aeb8fe0c068ad86cd40065d94e915d37ea271 [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
Chris Lattner6af20402002-12-03 05:41:54 +000034static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */
Chris Lattner9a3d63b2001-09-19 15:56:23 +000035// Build the MachineInstruction Description Array...
Chris Lattner583b9d82003-12-20 09:17:40 +000036const TargetInstrDescriptor llvm::SparcMachineInstrDesc[] = {
Chris Lattner9a3d63b2001-09-19 15:56:23 +000037#define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
38 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \
39 { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \
Chris Lattner6af20402002-12-03 05:41:54 +000040 NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \
41 ImplicitRegUseList, ImplicitRegUseList },
Chris Lattner9a3d63b2001-09-19 15:56:23 +000042#include "SparcInstr.def"
43};
Vikram S. Adve0fb49802001-09-18 13:01:29 +000044
Chris Lattner4f946372002-10-28 01:03:43 +000045//---------------------------------------------------------------------------
46// Command line options to control choice of code generation passes.
47//---------------------------------------------------------------------------
48
Misha Brukman1be1a232003-11-13 00:16:28 +000049namespace {
50 cl::opt<bool> DisableSched("disable-sched",
51 cl::desc("Disable local scheduling pass"));
Chris Lattner4f946372002-10-28 01:03:43 +000052
Misha Brukman1be1a232003-11-13 00:16:28 +000053 cl::opt<bool> DisablePeephole("disable-peephole",
Chris Lattner4f946372002-10-28 01:03:43 +000054 cl::desc("Disable peephole optimization pass"));
55
Misha Brukman1be1a232003-11-13 00:16:28 +000056 cl::opt<bool> EmitMappingInfo("enable-maps",
57 cl::desc("Emit LLVM-to-MachineCode mapping info to assembly"));
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000058
Misha Brukman1be1a232003-11-13 00:16:28 +000059 cl::opt<bool> DisableStrip("disable-strip",
60 cl::desc("Do not strip the LLVM bytecode in executable"));
Vikram S. Adve20a3be32003-08-12 15:51:02 +000061
Misha Brukman1be1a232003-11-13 00:16:28 +000062 cl::opt<bool> DumpInput("dump-input",
63 cl::desc("Print bytecode before code generation"),
64 cl::Hidden);
65}
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +000066
Chris Lattner583b9d82003-12-20 09:17:40 +000067//===---------------------------------------------------------------------===//
68// Code generation/destruction passes
69//===---------------------------------------------------------------------===//
70
71namespace {
72 class ConstructMachineFunction : public FunctionPass {
73 TargetMachine &Target;
74 public:
75 ConstructMachineFunction(TargetMachine &T) : Target(T) {}
76
77 const char *getPassName() const {
78 return "ConstructMachineFunction";
79 }
80
81 bool runOnFunction(Function &F) {
82 MachineFunction::construct(&F, Target).getInfo()->CalculateArgSize();
83 return false;
84 }
85 };
86
87 struct DestroyMachineFunction : public FunctionPass {
88 const char *getPassName() const { return "FreeMachineFunction"; }
89
90 static void freeMachineCode(Instruction &I) {
91 MachineCodeForInstruction::destroy(&I);
92 }
93
94 bool runOnFunction(Function &F) {
95 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
96 for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I)
97 MachineCodeForInstruction::get(I).dropAllReferences();
98
99 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
100 for_each(FI->begin(), FI->end(), freeMachineCode);
101
102 return false;
103 }
104 };
105
106 FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) {
107 return new ConstructMachineFunction(Target);
108 }
109}
110
111FunctionPass *llvm::createSparcMachineCodeDestructionPass() {
112 return new DestroyMachineFunction();
113}
114
Ruchira Sasankacc3ccac2001-10-15 16:25:28 +0000115
Misha Brukmand71295a2003-12-17 22:04:00 +0000116SparcTargetMachine::SparcTargetMachine()
Chris Lattner10daaa12003-04-26 20:11:09 +0000117 : TargetMachine("UltraSparc-Native", false),
Vikram S. Adve7f37fe52001-11-08 04:55:13 +0000118 schedInfo(*this),
119 regInfo(*this),
Vikram S. Adveb7048402001-11-09 02:16:04 +0000120 frameInfo(*this),
Chris Lattner1e60a912003-12-20 01:22:19 +0000121 cacheInfo(*this),
122 jitInfo(*this) {
Chris Lattner20b1ea02001-09-14 03:47:57 +0000123}
124
Chris Lattner4f946372002-10-28 01:03:43 +0000125// addPassesToEmitAssembly - This method controls the entire code generation
126// process for the ultra sparc.
127//
Misha Brukmand71295a2003-12-17 22:04:00 +0000128bool
129SparcTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out)
Chris Lattner4f946372002-10-28 01:03:43 +0000130{
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000131 // The following 3 passes used to be inserted specially by llc.
132 // Replace malloc and free instructions with library calls.
133 PM.add(createLowerAllocationsPass());
134
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000135 // Strip all of the symbols from the bytecode so that it will be smaller...
136 if (!DisableStrip)
137 PM.add(createSymbolStrippingPass());
138
Chris Lattner155e68f2003-04-23 16:24:55 +0000139 // FIXME: implement the switch instruction in the instruction selector.
140 PM.add(createLowerSwitchPass());
Chris Lattnerd324e252003-10-05 19:16:09 +0000141
142 // FIXME: implement the invoke/unwind instructions!
143 PM.add(createLowerInvokePass());
Brian Gaeke2e2f2dc2003-06-18 21:14:23 +0000144
Vikram S. Adve248932b2003-08-01 15:53:24 +0000145 // decompose multi-dimensional array references into single-dim refs
146 PM.add(createDecomposeMultiDimRefsPass());
147
Chris Lattner4f946372002-10-28 01:03:43 +0000148 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner227c3d32002-10-28 01:12:41 +0000149 PM.add(createMachineCodeConstructionPass(*this));
Chris Lattner4f946372002-10-28 01:03:43 +0000150
151 //Insert empty stackslots in the stack frame of each function
152 //so %fp+offset-8 and %fp+offset-16 are empty slots now!
153 PM.add(createStackSlotsPass(*this));
154
Misha Brukman35188172003-11-07 20:33:25 +0000155 // Specialize LLVM code for this target machine
156 PM.add(createPreSelectionPass(*this));
157 // Run basic dataflow optimizations on LLVM code
158 PM.add(createReassociatePass());
159 PM.add(createLICMPass());
160 PM.add(createGCSEPass());
161
Vikram S. Advedeb96342003-07-29 19:58:00 +0000162 // If LLVM dumping after transformations is requested, add it to the pipeline
163 if (DumpInput)
Brian Gaeke79c98032003-09-18 17:37:46 +0000164 PM.add(new PrintFunctionPass("Input code to instr. selection:\n",
Misha Brukman58ba50f2003-08-06 23:06:21 +0000165 &std::cerr));
Chris Lattner4f946372002-10-28 01:03:43 +0000166
167 PM.add(createInstructionSelectionPass(*this));
168
169 if (!DisableSched)
170 PM.add(createInstructionSchedulingWithSSAPass(*this));
171
172 PM.add(getRegisterAllocator(*this));
173
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.
187 //
Misha Brukman1be1a232003-11-13 00:16:28 +0000188 PM.add(createAsmPrinterPass(Out, *this));
Chris Lattner583b9d82003-12-20 09:17:40 +0000189 PM.add(createSparcMachineCodeDestructionPass()); // Free stuff no longer needed
Chris Lattner4f946372002-10-28 01:03:43 +0000190
Chris Lattner4f946372002-10-28 01:03:43 +0000191 // Emit bytecode to the assembly file into its special section next
Brian Gaeke79c98032003-09-18 17:37:46 +0000192 if (EmitMappingInfo)
Misha Brukman1be1a232003-11-13 00:16:28 +0000193 PM.add(createBytecodeAsmPrinterPass(Out));
Vikram S. Adve20a3be32003-08-12 15:51:02 +0000194
Chris Lattner63342052002-10-29 21:12:46 +0000195 return false;
Chris Lattner4f946372002-10-28 01:03:43 +0000196}
Misha Brukman86172ab2003-05-27 22:24:48 +0000197
198// addPassesToJITCompile - This method controls the JIT method of code
199// generation for the UltraSparc.
200//
Chris Lattner1e60a912003-12-20 01:22:19 +0000201void SparcJITInfo::addPassesToJITCompile(FunctionPassManager &PM) {
202 const TargetData &TD = TM.getTargetData();
Misha Brukmana5124502003-06-06 07:11:16 +0000203
204 PM.add(new TargetData("lli", TD.isLittleEndian(), TD.getPointerSize(),
205 TD.getPointerAlignment(), TD.getDoubleAlignment()));
206
207 // Replace malloc and free instructions with library calls.
208 // Do this after tracing until lli implements these lib calls.
209 // For now, it will emulate malloc and free internally.
210 PM.add(createLowerAllocationsPass());
211
Misha Brukman86172ab2003-05-27 22:24:48 +0000212 // FIXME: implement the switch instruction in the instruction selector.
213 PM.add(createLowerSwitchPass());
214
Chris Lattnerd324e252003-10-05 19:16:09 +0000215 // FIXME: implement the invoke/unwind instructions!
216 PM.add(createLowerInvokePass());
217
Misha Brukman58ba50f2003-08-06 23:06:21 +0000218 // decompose multi-dimensional array references into single-dim refs
219 PM.add(createDecomposeMultiDimRefsPass());
220
Misha Brukman86172ab2003-05-27 22:24:48 +0000221 // Construct and initialize the MachineFunction object for this fn.
Chris Lattner1e60a912003-12-20 01:22:19 +0000222 PM.add(createMachineCodeConstructionPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000223
Misha Brukman35188172003-11-07 20:33:25 +0000224 // Specialize LLVM code for this target machine and then
225 // run basic dataflow optimizations on LLVM code.
Chris Lattner1e60a912003-12-20 01:22:19 +0000226 PM.add(createPreSelectionPass(TM));
Misha Brukman35188172003-11-07 20:33:25 +0000227 // Run basic dataflow optimizations on LLVM code
228 PM.add(createReassociatePass());
Misha Brukman6d2670d2003-11-08 00:01:39 +0000229
230 // FIXME: these passes crash the FunctionPassManager when being added...
231 //PM.add(createLICMPass());
232 //PM.add(createGCSEPass());
Misha Brukman35188172003-11-07 20:33:25 +0000233
Chris Lattner1e60a912003-12-20 01:22:19 +0000234 PM.add(createInstructionSelectionPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000235
Chris Lattner1e60a912003-12-20 01:22:19 +0000236 PM.add(getRegisterAllocator(TM));
Misha Brukman1be1a232003-11-13 00:16:28 +0000237 PM.add(createPrologEpilogInsertionPass());
Misha Brukman86172ab2003-05-27 22:24:48 +0000238
Misha Brukmand1ef7a82003-05-30 20:00:13 +0000239 if (!DisablePeephole)
Chris Lattner1e60a912003-12-20 01:22:19 +0000240 PM.add(createPeepholeOptsPass(TM));
Misha Brukman86172ab2003-05-27 22:24:48 +0000241}
Brian Gaeked0fde302003-11-11 22:41:34 +0000242
Misha Brukmand71295a2003-12-17 22:04:00 +0000243//----------------------------------------------------------------------------
244// allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine
245// that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface)
246//----------------------------------------------------------------------------
247
Chris Lattner1e60a912003-12-20 01:22:19 +0000248TargetMachine *llvm::allocateSparcTargetMachine(const Module &M) {
Misha Brukmand71295a2003-12-17 22:04:00 +0000249 return new SparcTargetMachine();
250}