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" |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 9 | #include "llvm/Target/Sparc.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 | 9a3d63b | 2001-09-19 15:56:23 +0000 | [diff] [blame] | 26 | // Build the MachineInstruction Description Array... |
| 27 | const MachineInstrDescriptor SparcMachineInstrDesc[] = { |
| 28 | #define I(ENUM, OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 29 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS) \ |
| 30 | { OPCODESTRING, NUMOPERANDS, RESULTPOS, MAXIMM, IMMSE, \ |
| 31 | NUMDELAYSLOTS, LATENCY, SCHEDCLASS, INSTFLAGS }, |
| 32 | #include "SparcInstr.def" |
| 33 | }; |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame^] | 35 | //--------------------------------------------------------------------------- |
| 36 | // Command line options to control choice of code generation passes. |
| 37 | //--------------------------------------------------------------------------- |
| 38 | |
| 39 | static cl::opt<bool> DisablePreSelect("nopreselect", |
| 40 | cl::desc("Disable preselection pass")); |
| 41 | |
| 42 | static cl::opt<bool> DisableSched("nosched", |
| 43 | cl::desc("Disable local scheduling pass")); |
| 44 | |
| 45 | static cl::opt<bool> DisablePeephole("nopeephole", |
| 46 | cl::desc("Disable peephole optimization pass")); |
| 47 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 48 | //---------------------------------------------------------------------------- |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 49 | // allocateSparcTargetMachine - Allocate and return a subclass of TargetMachine |
| 50 | // that implements the Sparc backend. (the llvm/CodeGen/Sparc.h interface) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 51 | //---------------------------------------------------------------------------- |
Ruchira Sasanka | cc3ccac | 2001-10-15 16:25:28 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 46cbff6 | 2001-09-14 16:56:32 +0000 | [diff] [blame] | 53 | TargetMachine *allocateSparcTargetMachine() { return new UltraSparc(); } |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 54 | |
| 55 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 56 | |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 57 | //--------------------------------------------------------------------------- |
| 58 | // class UltraSparcFrameInfo |
| 59 | // |
| 60 | // Purpose: |
| 61 | // Interface to stack frame layout info for the UltraSPARC. |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 62 | // Starting offsets for each area of the stack frame are aligned at |
| 63 | // a multiple of getStackFrameSizeAlignment(). |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 64 | //--------------------------------------------------------------------------- |
| 65 | |
| 66 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 67 | UltraSparcFrameInfo::getFirstAutomaticVarOffset(MachineFunction& , |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 68 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 69 | { |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 70 | pos = false; // static stack area grows downwards |
| 71 | return StaticAreaOffsetFromFP; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 75 | UltraSparcFrameInfo::getRegSpillAreaOffset(MachineFunction& mcInfo, |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 76 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 77 | { |
Vikram S. Adve | 0bc0516 | 2002-04-25 04:43:45 +0000 | [diff] [blame] | 78 | mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added |
| 79 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 80 | pos = false; // static stack area grows downwards |
| 81 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 82 | return StaticAreaOffsetFromFP - autoVarsSize; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 86 | UltraSparcFrameInfo::getTmpAreaOffset(MachineFunction& mcInfo, |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 87 | bool& pos) const |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 88 | { |
Vikram S. Adve | 0bc0516 | 2002-04-25 04:43:45 +0000 | [diff] [blame] | 89 | mcInfo.freezeAutomaticVarsArea(); // ensure no more auto vars are added |
| 90 | mcInfo.freezeSpillsArea(); // ensure no more spill slots are added |
| 91 | |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 92 | pos = false; // static stack area grows downwards |
| 93 | unsigned int autoVarsSize = mcInfo.getAutomaticVarsSize(); |
| 94 | unsigned int spillAreaSize = mcInfo.getRegSpillsSize(); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 95 | int offset = autoVarsSize + spillAreaSize; |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 96 | return StaticAreaOffsetFromFP - offset; |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | int |
Misha Brukman | fce1143 | 2002-10-28 00:28:31 +0000 | [diff] [blame] | 100 | UltraSparcFrameInfo::getDynamicAreaOffset(MachineFunction& mcInfo, |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 101 | bool& pos) const |
| 102 | { |
Vikram S. Adve | e6d2c41 | 2002-03-18 03:08:07 +0000 | [diff] [blame] | 103 | // Dynamic stack area grows downwards starting at top of opt-args area. |
| 104 | // The opt-args, required-args, and register-save areas are empty except |
| 105 | // during calls and traps, so they are shifted downwards on each |
| 106 | // dynamic-size alloca. |
| 107 | pos = false; |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 108 | unsigned int optArgsSize = mcInfo.getMaxOptionalArgsSize(); |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 109 | if (int extra = optArgsSize % getStackFrameSizeAlignment()) |
| 110 | optArgsSize += (getStackFrameSizeAlignment() - extra); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 111 | int offset = optArgsSize + FirstOptionalOutgoingArgOffsetFromSP; |
Vikram S. Adve | e6d2c41 | 2002-03-18 03:08:07 +0000 | [diff] [blame] | 112 | assert((offset - OFFSET) % getStackFrameSizeAlignment() == 0); |
Vikram S. Adve | 00521d7 | 2001-11-12 23:26:35 +0000 | [diff] [blame] | 113 | return offset; |
Vikram S. Adve | 9db4318 | 2001-10-22 13:44:23 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame^] | 116 | //===---------------------------------------------------------------------===// |
| 117 | // Default code generation passes. |
| 118 | // |
| 119 | // Native code generation for a specified target. |
| 120 | //===---------------------------------------------------------------------===// |
| 121 | |
| 122 | class ConstructMachineCodeForFunction : public FunctionPass { |
| 123 | TargetMachine &Target; |
| 124 | public: |
| 125 | ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {} |
| 126 | |
| 127 | const char *getPassName() const { |
| 128 | return "ConstructMachineCodeForFunction"; |
| 129 | } |
| 130 | |
| 131 | bool runOnFunction(Function &F) { |
| 132 | MachineFunction::construct(&F, Target); |
| 133 | return false; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | struct FreeMachineCodeForFunction : public FunctionPass { |
| 138 | const char *getPassName() const { return "FreeMachineCodeForFunction"; } |
| 139 | |
| 140 | static void freeMachineCode(Instruction &I) { |
| 141 | MachineCodeForInstruction::destroy(&I); |
| 142 | } |
| 143 | |
| 144 | bool runOnFunction(Function &F) { |
| 145 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 146 | for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I) |
| 147 | MachineCodeForInstruction::get(I).dropAllReferences(); |
| 148 | |
| 149 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 150 | for_each(FI->begin(), FI->end(), freeMachineCode); |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | |
Ruchira Sasanka | e38bd533 | 2001-09-15 00:30:44 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 158 | //--------------------------------------------------------------------------- |
| 159 | // class UltraSparcMachine |
| 160 | // |
| 161 | // Purpose: |
| 162 | // Primary interface to machine description for the UltraSPARC. |
| 163 | // Primarily just initializes machine-dependent parameters in |
| 164 | // class TargetMachine, and creates machine-dependent subclasses |
| 165 | // for classes such as MachineInstrInfo. |
| 166 | // |
| 167 | //--------------------------------------------------------------------------- |
| 168 | |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 169 | UltraSparc::UltraSparc() |
| 170 | : TargetMachine("UltraSparc-Native"), |
Vikram S. Adve | 7f37fe5 | 2001-11-08 04:55:13 +0000 | [diff] [blame] | 171 | instrInfo(*this), |
| 172 | schedInfo(*this), |
| 173 | regInfo(*this), |
Vikram S. Adve | b704840 | 2001-11-09 02:16:04 +0000 | [diff] [blame] | 174 | frameInfo(*this), |
Vikram S. Adve | d55697c | 2002-09-20 00:52:09 +0000 | [diff] [blame] | 175 | cacheInfo(*this), |
| 176 | optInfo(*this) |
Vikram S. Adve | 0fb4980 | 2001-09-18 13:01:29 +0000 | [diff] [blame] | 177 | { |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 178 | optSizeForSubWordData = 4; |
| 179 | minMemOpWordSize = 8; |
| 180 | maxAtomicMemOpWordSize = 8; |
Chris Lattner | 20b1ea0 | 2001-09-14 03:47:57 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | 4f94637 | 2002-10-28 01:03:43 +0000 | [diff] [blame^] | 183 | |
| 184 | // addPassesToEmitAssembly - This method controls the entire code generation |
| 185 | // process for the ultra sparc. |
| 186 | // |
| 187 | void UltraSparc::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) |
| 188 | { |
| 189 | // Construct and initialize the MachineFunction object for this fn. |
| 190 | PM.add(new ConstructMachineCodeForFunction(*this)); |
| 191 | |
| 192 | //Insert empty stackslots in the stack frame of each function |
| 193 | //so %fp+offset-8 and %fp+offset-16 are empty slots now! |
| 194 | PM.add(createStackSlotsPass(*this)); |
| 195 | |
| 196 | // Specialize LLVM code for this target machine and then |
| 197 | // run basic dataflow optimizations on LLVM code. |
| 198 | if (!DisablePreSelect) |
| 199 | { |
| 200 | PM.add(createPreSelectionPass(*this)); |
| 201 | /* PM.add(createReassociatePass()); */ |
| 202 | PM.add(createLICMPass()); |
| 203 | PM.add(createGCSEPass()); |
| 204 | } |
| 205 | |
| 206 | PM.add(createInstructionSelectionPass(*this)); |
| 207 | |
| 208 | if (!DisableSched) |
| 209 | PM.add(createInstructionSchedulingWithSSAPass(*this)); |
| 210 | |
| 211 | PM.add(getRegisterAllocator(*this)); |
| 212 | |
| 213 | PM.add(getPrologEpilogInsertionPass()); |
| 214 | |
| 215 | if (!DisablePeephole) |
| 216 | PM.add(createPeepholeOptsPass(*this)); |
| 217 | |
| 218 | PM.add(MappingInfoForFunction(Out)); |
| 219 | |
| 220 | // Output assembly language to the .s file. Assembly emission is split into |
| 221 | // two parts: Function output and Global value output. This is because |
| 222 | // function output is pipelined with all of the rest of code generation stuff, |
| 223 | // allowing machine code representations for functions to be free'd after the |
| 224 | // function has been emitted. |
| 225 | // |
| 226 | PM.add(getFunctionAsmPrinterPass(Out)); |
| 227 | PM.add(new FreeMachineCodeForFunction()); // Free stuff no longer needed |
| 228 | |
| 229 | // Emit Module level assembly after all of the functions have been processed. |
| 230 | PM.add(getModuleAsmPrinterPass(Out)); |
| 231 | |
| 232 | // Emit bytecode to the assembly file into its special section next |
| 233 | PM.add(getEmitBytecodeToAsmPass(Out)); |
| 234 | PM.add(getFunctionInfo(Out)); |
| 235 | } |