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