Chris Lattner | 1d21f3e | 2002-04-09 05:21:26 +0000 | [diff] [blame] | 1 | //===-- Sparc.cpp - General implementation file for the Sparc Target ------===// |
| 2 | // |
| 3 | // This file contains the code for the Sparc Target that does not fit in any of |
| 4 | // the other files in this directory. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 7 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 8 | #include "SparcInternals.h" |
Chris Lattner | 600e699 | 2002-10-29 20:47:26 +0000 | [diff] [blame] | 9 | #include "llvm/Target/TargetMachineImpls.h" |
Chris Lattner | 2fbfdcf | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 10 | #include "llvm/Function.h" |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 11 | #include "llvm/PassManager.h" |
| 12 | #include "llvm/Transforms/Scalar.h" |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/MachineFunction.h" |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 14 | #include "llvm/CodeGen/PreSelection.h" |
| 15 | #include "llvm/CodeGen/StackSlots.h" |
| 16 | #include "llvm/CodeGen/PeepholeOpts.h" |
| 17 | #include "llvm/CodeGen/InstrSelection.h" |
| 18 | #include "llvm/CodeGen/InstrScheduling.h" |
| 19 | #include "llvm/CodeGen/RegisterAllocation.h" |
| 20 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 21 | #include "llvm/Reoptimizer/Mapping/MappingInfo.h" |
| 22 | #include "llvm/Reoptimizer/Mapping/FInfo.h" |
| 23 | #include "Support/CommandLine.h" |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 24 | using std::cerr; |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 6af2040 | 2002-12-03 05:41:54 +0000 | [diff] [blame] | 26 | static const unsigned ImplicitRegUseList[] = { 0 }; /* not used yet */ |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 27 | // Build the MachineInstruction Description Array... |
| 28 | const MachineInstrDescriptor SparcMachineInstrDesc[] = { |
| 29 | #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 30 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \ |
| 31 | { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
Chris Lattner | 6af2040 | 2002-12-03 05:41:54 +0000 | [diff] [blame] | 32 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS, 0, \ |
| 33 | ImplicitRegUseList, ImplicitRegUseList }, |
Chris Lattner | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 34 | #include "SparcInstr.def" |
| 35 | }; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 37 | //--------------------------------------------------------------------------- |
| 38 | // Command line options to control choice of code generation passes. |
| 39 | //--------------------------------------------------------------------------- |
| 40 | |
| 41 | static cl::opt<bool> DisablePreSelect("nopreselect", |
| 42 | cl::desc("Disable preselection pass")); |
| 43 | |
| 44 | static cl::opt<bool> DisableSched("nosched", |
| 45 | cl::desc("Disable local scheduling pass")); |
| 46 | |
| 47 | static cl::opt<bool> DisablePeephole("nopeephole", |
| 48 | cl::desc("Disable peephole optimization pass")); |
| 49 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 50 | //---------------------------------------------------------------------------- |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 51 | // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine |
| 52 | // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 53 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 55 | TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 56 | |
| 57 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 58 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 59 | //--------------------------------------------------------------------------- |
| 60 | // class UltraSparcFrameInfo |
| 61 | // |
| 62 | // Purpose: |
| 63 | // Interface to stack frame layout info for the UltraSPARC. |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 64 | // Starting offsets for each area of the stack frame are aligned at |
| 65 | // a multiple of getStackFrameSizeAlignment(). |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 66 | //--------------------------------------------------------------------------- |
| 67 | |
| 68 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 69 | UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& , |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 70 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 71 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 72 | pos = false; // static stack area grows downwards |
| 73 | return StaticAreaOffsetFromFP; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 77 | UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo, |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 78 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 79 | { |
Vikram S. Adve | 0bc0516 | 2002-04-25 04:43:45 +0000 | [diff] [blame] | 80 | mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added |
| 81 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 82 | pos = false; // static stack area grows downwards |
| 83 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 84 | return StaticAreaOffsetFromFP - autoVarsSize; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 88 | UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo, |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 89 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 90 | { |
Vikram S. Adve | 0bc0516 | 2002-04-25 04:43:45 +0000 | [diff] [blame] | 91 | mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added |
| 92 | mcInfo.freezeSpillsArea(); // ensure no more spill slots are added |
| 93 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 94 | pos = false; // static stack area grows downwards |
| 95 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
| 96 | unsigned int spillAreaSize = mcInfo.getRegSpillsSize(); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 97 | int offset = autoVarsSize + spillAreaSize; |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 98 | return StaticAreaOffsetFromFP - offset; |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 102 | UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo, |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 103 | bool& pos) const |
| 104 | { |
Vikram S. Adve | e6d2c41 | 2002-03-18 03:08:07 +0000 | [diff] [blame] | 105 | // Dynamic stack area grows downwards starting at top of opt-args area. |
| 106 | // The opt-args, required-args, and register-save areas are empty except |
| 107 | // during calls and traps, so they are shifted downwards on each |
| 108 | // dynamic-size alloca. |
| 109 | pos = false; |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 110 | unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize(); |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 111 | if (int extra = optArgsSize % getStackFrameSizeAlignment()) |
| 112 | optArgsSize += (getStackFrameSizeAlignment() - extra); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 113 | int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP; |
Vikram S. Adve | e6d2c41 | 2002-03-18 03:08:07 +0000 | [diff] [blame] | 114 | assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 115 | return offset; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 118 | //--------------------------------------------------------------------------- |
| 119 | // class UltraSparcMachine |
| 120 | // |
| 121 | // Purpose: |
| 122 | // Primary interface to machine description for the UltraSPARC. |
| 123 | // Primarily just initializes machine-dependent parameters in |
| 124 | // class TargetMachine, and creates machine-dependent subclasses |
| 125 | // for classes such as MachineInstrInfo. |
| 126 | // |
| 127 | //--------------------------------------------------------------------------- |
| 128 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 129 | UltraSparc::UltraSparc() |
Chris Lattner | c56406c | 2002-10-29 21:48:17 +0000 | [diff] [blame] | 130 | : TargetMachine("UltraSparc-Native", 4), |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 131 | schedInfo(*this), |
| 132 | regInfo(*this), |
Vikram S. Adve | b704840 | 2001-11-09 02:16:04 +0000 | [diff] [blame] | 133 | frameInfo(*this), |
Vikram S. Adve | d55697c | 2002-09-20 00:52:09 +0000 | [diff] [blame] | 134 | cacheInfo(*this), |
Chris Lattner | c56406c | 2002-10-29 21:48:17 +0000 | [diff] [blame] | 135 | optInfo(*this) { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 138 | |
| 139 | // addPassesToEmitAssembly - This method controls the entire code generation |
| 140 | // process for the ultra sparc. |
| 141 | // |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 142 | bool UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 143 | { |
| 144 | // Construct and initialize the MachineFunction object for this fn. |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 145 | PM.add(createMachineCodeConstructionPass(*this)); |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 146 | |
| 147 | //Insert empty stackslots in the stack frame of each function |
| 148 | //so %fp+offset-8 and %fp+offset-16 are empty slots now! |
| 149 | PM.add(createStackSlotsPass(*this)); |
| 150 | |
| 151 | // Specialize LLVM code for this target machine and then |
| 152 | // run basic dataflow optimizations on LLVM code. |
| 153 | if (!DisablePreSelect) |
| 154 | { |
| 155 | PM.add(createPreSelectionPass(*this)); |
Chris Lattner | 59b2b86 | 2002-10-31 17:16:18 +0000 | [diff] [blame] | 156 | PM.add(createReassociatePass()); |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 157 | PM.add(createLICMPass()); |
| 158 | PM.add(createGCSEPass()); |
| 159 | } |
| 160 | |
| 161 | PM.add(createInstructionSelectionPass(*this)); |
| 162 | |
| 163 | if (!DisableSched) |
| 164 | PM.add(createInstructionSchedulingWithSSAPass(*this)); |
| 165 | |
| 166 | PM.add(getRegisterAllocator(*this)); |
| 167 | |
| 168 | PM.add(getPrologEpilogInsertionPass()); |
| 169 | |
| 170 | if (!DisablePeephole) |
| 171 | PM.add(createPeepholeOptsPass(*this)); |
| 172 | |
| 173 | PM.add(MappingInfoForFunction(Out)); |
| 174 | |
| 175 | // Output assembly language to the .s file. Assembly emission is split into |
| 176 | // two parts: Function output and Global value output. This is because |
| 177 | // function output is pipelined with all of the rest of code generation stuff, |
| 178 | // allowing machine code representations for functions to be free'd after the |
| 179 | // function has been emitted. |
| 180 | // |
| 181 | PM.add(getFunctionAsmPrinterPass(Out)); |
Chris Lattner | 227c3d3 | 2002-10-28 01:12:41 +0000 | [diff] [blame] | 182 | PM.add(createMachineCodeDestructionPass()); // Free stuff no longer needed |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 183 | |
| 184 | // Emit Module level assembly after all of the functions have been processed. |
| 185 | PM.add(getModuleAsmPrinterPass(Out)); |
| 186 | |
| 187 | // Emit bytecode to the assembly file into its special section next |
| 188 | PM.add(getEmitBytecodeToAsmPass(Out)); |
| 189 | PM.add(getFunctionInfo(Out)); |
Chris Lattner | 6334205 | 2002-10-29 21:12:46 +0000 | [diff] [blame] | 190 | return false; |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame] | 191 | } |