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