Chris Lattner | b26bcc5 | 2001-09-14 05:34:53 +0000 | [diff] [blame] | 1 | //===-- TargetMachine.cpp - General Target Information ---------------------==// |
| 2 | // |
| 3 | // This file describes the general parts of a Target machine. |
Vikram S. Adve | 4a48c33 | 2001-11-09 02:20:18 +0000 | [diff] [blame] | 4 | // This file also implements MachineInstrInfo and MachineCacheInfo. |
Chris Lattner | b26bcc5 | 2001-09-14 05:34:53 +0000 | [diff] [blame] | 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | daae699 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 7 | |
Vikram S. Adve | 4a48c33 | 2001-11-09 02:20:18 +0000 | [diff] [blame] | 8 | #include "llvm/Target/TargetMachine.h" |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 9 | #include "llvm/Target/MachineInstrInfo.h" |
Vikram S. Adve | 4a48c33 | 2001-11-09 02:20:18 +0000 | [diff] [blame] | 10 | #include "llvm/Target/MachineCacheInfo.h" |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/PreSelection.h" |
Anand Shukla | c078930 | 2002-09-21 05:01:21 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/StackSlots.h" |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/InstrSelection.h" |
| 14 | #include "llvm/CodeGen/InstrScheduling.h" |
| 15 | #include "llvm/CodeGen/RegisterAllocation.h" |
Vikram S. Adve | e5b2565 | 2002-09-20 00:52:43 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/PeepholeOpts.h" |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineCodeForMethod.h" |
| 18 | #include "llvm/CodeGen/MachineCodeForInstruction.h" |
| 19 | #include "llvm/Reoptimizer/Mapping/MappingInfo.h" |
| 20 | #include "llvm/Reoptimizer/Mapping/FInfo.h" |
| 21 | #include "llvm/Transforms/Scalar.h" |
| 22 | #include "Support/CommandLine.h" |
| 23 | #include "llvm/PassManager.h" |
| 24 | #include "llvm/Function.h" |
Chris Lattner | 68498ce | 2001-07-21 23:24:48 +0000 | [diff] [blame] | 25 | #include "llvm/DerivedTypes.h" |
Vikram S. Adve | daae699 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 26 | |
Vikram S. Adve | daae699 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 27 | //--------------------------------------------------------------------------- |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 28 | // Command line options to control choice of code generation passes. |
| 29 | //--------------------------------------------------------------------------- |
| 30 | |
| 31 | static cl::opt<bool> DisablePreSelect("nopreselect", |
| 32 | cl::desc("Disable preselection pass")); |
| 33 | |
| 34 | static cl::opt<bool> DisableSched("nosched", |
| 35 | cl::desc("Disable local scheduling pass")); |
| 36 | |
Vikram S. Adve | e5b2565 | 2002-09-20 00:52:43 +0000 | [diff] [blame] | 37 | static cl::opt<bool> DisablePeephole("nopeephole", |
| 38 | cl::desc("Disable peephole optimization pass")); |
| 39 | |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 40 | //--------------------------------------------------------------------------- |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 41 | // class TargetMachine |
Vikram S. Adve | daae699 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 42 | // |
| 43 | // Purpose: |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 44 | // Machine description. |
| 45 | // |
Vikram S. Adve | daae699 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 46 | //--------------------------------------------------------------------------- |
| 47 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 48 | |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 49 | // function TargetMachine::findOptimalStorageSize |
| 50 | // |
| 51 | // Purpose: |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 52 | // This default implementation assumes that all sub-word data items use |
| 53 | // space equal to optSizeForSubWordData, and all other primitive data |
| 54 | // items use space according to the type. |
| 55 | // |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 56 | unsigned int |
| 57 | TargetMachine::findOptimalStorageSize(const Type* ty) const |
| 58 | { |
| 59 | switch(ty->getPrimitiveID()) |
| 60 | { |
| 61 | case Type::BoolTyID: |
| 62 | case Type::UByteTyID: |
| 63 | case Type::SByteTyID: |
| 64 | case Type::UShortTyID: |
| 65 | case Type::ShortTyID: |
| 66 | return optSizeForSubWordData; |
Vikram S. Adve | daae699 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 67 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 68 | default: |
| 69 | return DataLayout.getTypeSize(ty); |
| 70 | } |
Vikram S. Adve | daae699 | 2001-07-21 12:42:08 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 73 | |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 74 | //===---------------------------------------------------------------------===// |
| 75 | // Default code generation passes. |
| 76 | // |
| 77 | // Native code generation for a specified target. |
| 78 | //===---------------------------------------------------------------------===// |
| 79 | |
| 80 | class ConstructMachineCodeForFunction : public FunctionPass { |
| 81 | TargetMachine &Target; |
| 82 | public: |
| 83 | inline ConstructMachineCodeForFunction(TargetMachine &T) : Target(T) {} |
| 84 | |
| 85 | const char *getPassName() const { |
| 86 | return "ConstructMachineCodeForFunction"; |
| 87 | } |
| 88 | |
| 89 | bool runOnFunction(Function &F) { |
| 90 | MachineCodeForMethod::construct(&F, Target); |
| 91 | return false; |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | struct FreeMachineCodeForFunction : public FunctionPass { |
| 96 | const char *getPassName() const { return "FreeMachineCodeForFunction"; } |
| 97 | |
| 98 | static void freeMachineCode(Instruction &I) { |
| 99 | MachineCodeForInstruction::destroy(&I); |
| 100 | } |
| 101 | |
| 102 | bool runOnFunction(Function &F) { |
| 103 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 104 | for (BasicBlock::iterator I = FI->begin(), E = FI->end(); I != E; ++I) |
| 105 | MachineCodeForInstruction::get(I).dropAllReferences(); |
| 106 | |
| 107 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) |
| 108 | for_each(FI->begin(), FI->end(), freeMachineCode); |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | // addPassesToEmitAssembly - This method controls the entire code generation |
| 115 | // process for the ultra sparc. |
| 116 | // |
| 117 | void |
| 118 | TargetMachine::addPassesToEmitAssembly(PassManager &PM, std::ostream &Out) |
| 119 | { |
| 120 | // Construct and initialize the MachineCodeForMethod object for this fn. |
| 121 | PM.add(new ConstructMachineCodeForFunction(*this)); |
| 122 | |
Anand Shukla | c078930 | 2002-09-21 05:01:21 +0000 | [diff] [blame] | 123 | //Insert empty stackslots in the stack frame of each function |
| 124 | //so %fp+offset-8 and %fp+offset-16 are empty slots now! |
| 125 | PM.add(createStackSlotsPass(*this)); |
| 126 | |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 127 | // Specialize LLVM code for this target machine and then |
| 128 | // run basic dataflow optimizations on LLVM code. |
| 129 | if (!DisablePreSelect) |
| 130 | { |
| 131 | PM.add(createPreSelectionPass(*this)); |
Vikram S. Adve | c308aef | 2002-09-23 12:55:50 +0000 | [diff] [blame^] | 132 | /* PM.add(createReassociatePass()); */ |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 133 | PM.add(createGCSEPass()); |
| 134 | PM.add(createLICMPass()); |
| 135 | } |
| 136 | |
| 137 | PM.add(createInstructionSelectionPass(*this)); |
| 138 | |
| 139 | if (!DisableSched) |
| 140 | PM.add(createInstructionSchedulingWithSSAPass(*this)); |
| 141 | |
| 142 | PM.add(getRegisterAllocator(*this)); |
| 143 | |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 144 | PM.add(getPrologEpilogInsertionPass()); |
| 145 | |
Vikram S. Adve | e5b2565 | 2002-09-20 00:52:43 +0000 | [diff] [blame] | 146 | if (!DisablePeephole) |
| 147 | PM.add(createPeepholeOptsPass(*this)); |
| 148 | |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 149 | PM.add(MappingInfoForFunction(Out)); |
| 150 | |
| 151 | // Output assembly language to the .s file. Assembly emission is split into |
| 152 | // two parts: Function output and Global value output. This is because |
| 153 | // function output is pipelined with all of the rest of code generation stuff, |
| 154 | // allowing machine code representations for functions to be free'd after the |
| 155 | // function has been emitted. |
| 156 | // |
| 157 | PM.add(getFunctionAsmPrinterPass(Out)); |
| 158 | PM.add(new FreeMachineCodeForFunction()); // Free stuff no longer needed |
| 159 | |
| 160 | // Emit Module level assembly after all of the functions have been processed. |
| 161 | PM.add(getModuleAsmPrinterPass(Out)); |
| 162 | |
| 163 | // Emit bytecode to the assembly file into its special section next |
| 164 | PM.add(getEmitBytecodeToAsmPass(Out)); |
| 165 | PM.add(getFunctionInfo(Out)); |
| 166 | } |
| 167 | |
| 168 | |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 169 | //--------------------------------------------------------------------------- |
| 170 | // class MachineInstructionInfo |
| 171 | // Interface to description of machine instructions |
| 172 | //--------------------------------------------------------------------------- |
| 173 | |
| 174 | |
| 175 | /*ctor*/ |
Vikram S. Adve | 7a2f1e7 | 2001-11-08 05:15:08 +0000 | [diff] [blame] | 176 | MachineInstrInfo::MachineInstrInfo(const TargetMachine& tgt, |
| 177 | const MachineInstrDescriptor* _desc, |
Vikram S. Adve | bf24233 | 2001-08-28 23:09:36 +0000 | [diff] [blame] | 178 | unsigned int _descSize, |
| 179 | unsigned int _numRealOpCodes) |
Vikram S. Adve | 7a2f1e7 | 2001-11-08 05:15:08 +0000 | [diff] [blame] | 180 | : target(tgt), |
| 181 | desc(_desc), descSize(_descSize), numRealOpCodes(_numRealOpCodes) |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 182 | { |
Chris Lattner | 78a81a2 | 2001-09-14 16:08:12 +0000 | [diff] [blame] | 183 | // FIXME: TargetInstrDescriptors should not be global |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 184 | assert(TargetInstrDescriptors == NULL && desc != NULL); |
| 185 | TargetInstrDescriptors = desc; // initialize global variable |
| 186 | } |
| 187 | |
| 188 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 189 | MachineInstrInfo::~MachineInstrInfo() |
| 190 | { |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 191 | TargetInstrDescriptors = NULL; // reset global variable |
| 192 | } |
| 193 | |
| 194 | |
| 195 | bool |
| 196 | MachineInstrInfo::constantFitsInImmedField(MachineOpCode opCode, |
| 197 | int64_t intValue) const |
| 198 | { |
| 199 | // First, check if opCode has an immed field. |
| 200 | bool isSignExtended; |
Chris Lattner | c763461 | 2001-09-14 15:43:58 +0000 | [diff] [blame] | 201 | uint64_t maxImmedValue = maxImmedConstant(opCode, isSignExtended); |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 202 | if (maxImmedValue != 0) |
| 203 | { |
Vikram S. Adve | e1f7280 | 2002-09-16 15:39:26 +0000 | [diff] [blame] | 204 | // NEED TO HANDLE UNSIGNED VALUES SINCE THEY MAY BECOME MUCH |
| 205 | // SMALLER AFTER CASTING TO SIGN-EXTENDED int, short, or char. |
| 206 | // See CreateUIntSetInstruction in SparcInstrInfo.cpp. |
| 207 | |
Vikram S. Adve | 0799fc4 | 2001-09-18 12:58:33 +0000 | [diff] [blame] | 208 | // Now check if the constant fits |
| 209 | if (intValue <= (int64_t) maxImmedValue && |
| 210 | intValue >= -((int64_t) maxImmedValue+1)) |
| 211 | return true; |
| 212 | } |
Vikram S. Adve | 44a853c | 2001-07-28 04:09:37 +0000 | [diff] [blame] | 213 | |
| 214 | return false; |
| 215 | } |
Vikram S. Adve | 4a48c33 | 2001-11-09 02:20:18 +0000 | [diff] [blame] | 216 | |
| 217 | |
| 218 | //--------------------------------------------------------------------------- |
| 219 | // class MachineCacheInfo |
| 220 | // |
| 221 | // Purpose: |
| 222 | // Describes properties of the target cache architecture. |
| 223 | //--------------------------------------------------------------------------- |
| 224 | |
| 225 | /*ctor*/ |
| 226 | MachineCacheInfo::MachineCacheInfo(const TargetMachine& tgt) |
| 227 | : target(tgt) |
| 228 | { |
| 229 | Initialize(); |
| 230 | } |
| 231 | |
| 232 | void |
| 233 | MachineCacheInfo::Initialize() |
| 234 | { |
| 235 | numLevels = 2; |
| 236 | cacheLineSizes.push_back(16); cacheLineSizes.push_back(32); |
| 237 | cacheSizes.push_back(1 << 15); cacheSizes.push_back(1 << 20); |
| 238 | cacheAssoc.push_back(1); cacheAssoc.push_back(4); |
| 239 | } |