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