Chris Lattner | 1d21f3e | 2002-04-09 05:21:26 +0000 | [diff] [blame] | 1 | //===-- Sparc.cpp - General implementation file for the Sparc Target ------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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 Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 9 | // |
| 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 Lattner | 1d21f3e | 2002-04-09 05:21:26 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 15 | |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 17 | #include "llvm/PassManager.h" |
Chris Lattner | 48e6079 | 2003-08-13 02:38:16 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/PrintModulePass.h" |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/InstrSelection.h" |
| 20 | #include "llvm/CodeGen/InstrScheduling.h" |
Misha Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunction.h" |
| 22 | #include "llvm/CodeGen/MachineFunctionInfo.h" |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
Chris Lattner | 56d2251 | 2003-09-30 22:24:00 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/Passes.h" |
Chris Lattner | 48e6079 | 2003-08-13 02:38:16 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachineImpls.h" |
Misha Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Scalar.h" |
| 27 | #include "MappingInfo.h" |
| 28 | #include "SparcInternals.h" |
| 29 | #include "SparcTargetMachine.h" |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 30 | #include "Support/CommandLine.h" |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 31 | |
Misha Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
Chris Lattner | 6af2040 | 2002-12-03 05:41:54 +0000 | [diff] [blame] | 34 | static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */ |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 35 | // Build the MachineInstruction Description Array... |
Chris Lattner | 583b9d8 | 2003-12-20 09:17:40 +0000 | [diff] [blame] | 36 | const TargetInstrDescriptor llvm::SparcMachineInstrDesc[] = { |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 37 | #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 38 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \ |
| 39 | { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
Chris Lattner | 6af2040 | 2002-12-03 05:41:54 +0000 | [diff] [blame] | 40 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \ |
| 41 | ImplicitRegUseList, ImplicitRegUseList }, |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 42 | #include "SparcInstr.def" |
| 43 | }; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 45 | //--------------------------------------------------------------------------- |
| 46 | // Command line options to control choice of code generation passes. |
| 47 | //--------------------------------------------------------------------------- |
| 48 | |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 49 | namespace { |
| 50 | cl::opt<bool> DisableSched("disable-sched", |
| 51 | cl::desc("Disable local scheduling pass")); |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 52 | |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 53 | cl::opt<bool> DisablePeephole("disable-peephole", |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 54 | cl::desc("Disable peephole optimization pass")); |
| 55 | |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 56 | cl::opt<bool> EmitMappingInfo("enable-maps", |
| 57 | cl::desc("Emit LLVM-to-MachineCode mapping info to assembly")); |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 58 | |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 59 | cl::opt<bool> DisableStrip("disable-strip", |
| 60 | cl::desc("Do not strip the LLVM bytecode in executable")); |
Vikram S. Adve | 20a3be3 | 2003-08-12 15:51:02 +0000 | [diff] [blame] | 61 | |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 62 | cl::opt<bool> DumpInput("dump-input", |
| 63 | cl::desc("Print bytecode before code generation"), |
| 64 | cl::Hidden); |
| 65 | } |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 583b9d8 | 2003-12-20 09:17:40 +0000 | [diff] [blame] | 67 | //===---------------------------------------------------------------------===// |
| 68 | // Code generation/destruction passes |
| 69 | //===---------------------------------------------------------------------===// |
| 70 | |
| 71 | namespace { |
| 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 | |
Misha Brukman | 3615845 | 2003-12-22 03:47:58 +0000 | [diff] [blame^] | 102 | MachineFunction::destruct(&F); |
Chris Lattner | 583b9d8 | 2003-12-20 09:17:40 +0000 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | FunctionPass *createMachineCodeConstructionPass(TargetMachine &Target) { |
| 108 | return new ConstructMachineFunction(Target); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | FunctionPass *llvm::createSparcMachineCodeDestructionPass() { |
| 113 | return new DestroyMachineFunction(); |
| 114 | } |
| 115 | |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 116 | |
Misha Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 117 | SparcTargetMachine::SparcTargetMachine() |
Chris Lattner | 10daaa1 | 2003-04-26 20:11:09 +0000 | [diff] [blame] | 118 | : TargetMachine("UltraSparc-Native", false), |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 119 | schedInfo(*this), |
| 120 | regInfo(*this), |
Vikram S. Adve | b704840 | 2001-11-09 02:16:04 +0000 | [diff] [blame] | 121 | frameInfo(*this), |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 122 | cacheInfo(*this), |
| 123 | jitInfo(*this) { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 126 | // addPassesToEmitAssembly - This method controls the entire code generation |
| 127 | // process for the ultra sparc. |
| 128 | // |
Misha Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 129 | bool |
| 130 | SparcTargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 131 | { |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 132 | // The following 3 passes used to be inserted specially by llc. |
| 133 | // Replace malloc and free instructions with library calls. |
| 134 | PM.add(createLowerAllocationsPass()); |
| 135 | |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 136 | // Strip all of the symbols from the bytecode so that it will be smaller... |
| 137 | if (!DisableStrip) |
| 138 | PM.add(createSymbolStrippingPass()); |
| 139 | |
Chris Lattner | 155e68f | 2003-04-23 16:24:55 +0000 | [diff] [blame] | 140 | // FIXME: implement the switch instruction in the instruction selector. |
| 141 | PM.add(createLowerSwitchPass()); |
Chris Lattner | d324e25 | 2003-10-05 19:16:09 +0000 | [diff] [blame] | 142 | |
| 143 | // FIXME: implement the invoke/unwind instructions! |
| 144 | PM.add(createLowerInvokePass()); |
Brian Gaeke | 2e2f2dc | 2003-06-18 21:14:23 +0000 | [diff] [blame] | 145 | |
Vikram S. Adve | 248932b | 2003-08-01 15:53:24 +0000 | [diff] [blame] | 146 | // decompose multi-dimensional array references into single-dim refs |
| 147 | PM.add(createDecomposeMultiDimRefsPass()); |
| 148 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 149 | // Construct and initialize the MachineFunction object for this fn. |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 150 | PM.add(createMachineCodeConstructionPass(*this)); |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 151 | |
| 152 | //Insert empty stackslots in the stack frame of each function |
| 153 | //so %fp+offset-8 and %fp+offset-16 are empty slots now! |
| 154 | PM.add(createStackSlotsPass(*this)); |
| 155 | |
Misha Brukman | 3518817 | 2003-11-07 20:33:25 +0000 | [diff] [blame] | 156 | // Specialize LLVM code for this target machine |
| 157 | PM.add(createPreSelectionPass(*this)); |
| 158 | // Run basic dataflow optimizations on LLVM code |
| 159 | PM.add(createReassociatePass()); |
| 160 | PM.add(createLICMPass()); |
| 161 | PM.add(createGCSEPass()); |
| 162 | |
Vikram S. Adve | deb9634 | 2003-07-29 19:58:00 +0000 | [diff] [blame] | 163 | // If LLVM dumping after transformations is requested, add it to the pipeline |
| 164 | if (DumpInput) |
Brian Gaeke | 79c9803 | 2003-09-18 17:37:46 +0000 | [diff] [blame] | 165 | PM.add(new PrintFunctionPass("Input code to instr. selection:\n", |
Misha Brukman | 58ba50f | 2003-08-06 23:06:21 +0000 | [diff] [blame] | 166 | &std::cerr)); |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 167 | |
| 168 | PM.add(createInstructionSelectionPass(*this)); |
| 169 | |
| 170 | if (!DisableSched) |
| 171 | PM.add(createInstructionSchedulingWithSSAPass(*this)); |
| 172 | |
| 173 | PM.add(getRegisterAllocator(*this)); |
| 174 | |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 175 | PM.add(createPrologEpilogInsertionPass()); |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 176 | |
| 177 | if (!DisablePeephole) |
| 178 | PM.add(createPeepholeOptsPass(*this)); |
| 179 | |
Vikram S. Adve | 20a3be3 | 2003-08-12 15:51:02 +0000 | [diff] [blame] | 180 | if (EmitMappingInfo) |
Brian Gaeke | 79c9803 | 2003-09-18 17:37:46 +0000 | [diff] [blame] | 181 | PM.add(getMappingInfoAsmPrinterPass(Out)); |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 182 | |
| 183 | // Output assembly language to the .s file. Assembly emission is split into |
| 184 | // two parts: Function output and Global value output. This is because |
| 185 | // function output is pipelined with all of the rest of code generation stuff, |
| 186 | // allowing machine code representations for functions to be free'd after the |
| 187 | // function has been emitted. |
| 188 | // |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 189 | PM.add(createAsmPrinterPass(Out, *this)); |
Chris Lattner | 583b9d8 | 2003-12-20 09:17:40 +0000 | [diff] [blame] | 190 | PM.add(createSparcMachineCodeDestructionPass()); // Free stuff no longer needed |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 192 | // Emit bytecode to the assembly file into its special section next |
Brian Gaeke | 79c9803 | 2003-09-18 17:37:46 +0000 | [diff] [blame] | 193 | if (EmitMappingInfo) |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 194 | PM.add(createBytecodeAsmPrinterPass(Out)); |
Vikram S. Adve | 20a3be3 | 2003-08-12 15:51:02 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 196 | return false; |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 197 | } |
Misha Brukman | 86172ab | 2003-05-27 22:24:48 +0000 | [diff] [blame] | 198 | |
| 199 | // addPassesToJITCompile - This method controls the JIT method of code |
| 200 | // generation for the UltraSparc. |
| 201 | // |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 202 | void SparcJITInfo::addPassesToJITCompile(FunctionPassManager &PM) { |
| 203 | const TargetData &TD = TM.getTargetData(); |
Misha Brukman | a512450 | 2003-06-06 07:11:16 +0000 | [diff] [blame] | 204 | |
| 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 Brukman | 86172ab | 2003-05-27 22:24:48 +0000 | [diff] [blame] | 213 | // FIXME: implement the switch instruction in the instruction selector. |
| 214 | PM.add(createLowerSwitchPass()); |
| 215 | |
Chris Lattner | d324e25 | 2003-10-05 19:16:09 +0000 | [diff] [blame] | 216 | // FIXME: implement the invoke/unwind instructions! |
| 217 | PM.add(createLowerInvokePass()); |
| 218 | |
Misha Brukman | 58ba50f | 2003-08-06 23:06:21 +0000 | [diff] [blame] | 219 | // decompose multi-dimensional array references into single-dim refs |
| 220 | PM.add(createDecomposeMultiDimRefsPass()); |
| 221 | |
Misha Brukman | 86172ab | 2003-05-27 22:24:48 +0000 | [diff] [blame] | 222 | // Construct and initialize the MachineFunction object for this fn. |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 223 | PM.add(createMachineCodeConstructionPass(TM)); |
Misha Brukman | 86172ab | 2003-05-27 22:24:48 +0000 | [diff] [blame] | 224 | |
Misha Brukman | 3518817 | 2003-11-07 20:33:25 +0000 | [diff] [blame] | 225 | // Specialize LLVM code for this target machine and then |
| 226 | // run basic dataflow optimizations on LLVM code. |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 227 | PM.add(createPreSelectionPass(TM)); |
Misha Brukman | 3518817 | 2003-11-07 20:33:25 +0000 | [diff] [blame] | 228 | // Run basic dataflow optimizations on LLVM code |
| 229 | PM.add(createReassociatePass()); |
Misha Brukman | 6d2670d | 2003-11-08 00:01:39 +0000 | [diff] [blame] | 230 | |
| 231 | // FIXME: these passes crash the FunctionPassManager when being added... |
| 232 | //PM.add(createLICMPass()); |
| 233 | //PM.add(createGCSEPass()); |
Misha Brukman | 3518817 | 2003-11-07 20:33:25 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 235 | PM.add(createInstructionSelectionPass(TM)); |
Misha Brukman | 86172ab | 2003-05-27 22:24:48 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 237 | PM.add(getRegisterAllocator(TM)); |
Misha Brukman | 1be1a23 | 2003-11-13 00:16:28 +0000 | [diff] [blame] | 238 | PM.add(createPrologEpilogInsertionPass()); |
Misha Brukman | 86172ab | 2003-05-27 22:24:48 +0000 | [diff] [blame] | 239 | |
Misha Brukman | d1ef7a8 | 2003-05-30 20:00:13 +0000 | [diff] [blame] | 240 | if (!DisablePeephole) |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 241 | PM.add(createPeepholeOptsPass(TM)); |
Misha Brukman | 86172ab | 2003-05-27 22:24:48 +0000 | [diff] [blame] | 242 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 243 | |
Misha Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 244 | //---------------------------------------------------------------------------- |
| 245 | // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine |
| 246 | // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface) |
| 247 | //---------------------------------------------------------------------------- |
| 248 | |
Chris Lattner | 1e60a91 | 2003-12-20 01:22:19 +0000 | [diff] [blame] | 249 | TargetMachine *llvm::allocateSparcTargetMachine(const Module &M) { |
Misha Brukman | d71295a | 2003-12-17 22:04:00 +0000 | [diff] [blame] | 250 | return new SparcTargetMachine(); |
| 251 | } |