buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_SRC_COMPILER_COMPILER_H_ |
| 18 | #define ART_SRC_COMPILER_COMPILER_H_ |
| 19 | |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 20 | #include "dex_file.h" |
Elliott Hughes | adb8c67 | 2012-03-06 16:49:32 -0800 | [diff] [blame] | 21 | #include "dex_instruction.h" |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 22 | |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 23 | namespace llvm { |
| 24 | class Module; |
| 25 | class LLVMContext; |
| 26 | } |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 27 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 28 | namespace art { |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 29 | namespace greenland { |
| 30 | class IntrinsicHelper; |
| 31 | class IRBuilder; |
| 32 | } |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 33 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 34 | #define COMPILER_TRACED(X) |
| 35 | #define COMPILER_TRACEE(X) |
| 36 | |
buzbee | 44b412b | 2012-02-04 08:50:53 -0800 | [diff] [blame] | 37 | /* |
| 38 | * Special offsets to denote method entry/exit for debugger update. |
| 39 | * NOTE: bit pattern must be loadable using 1 instruction and must |
| 40 | * not be a valid Dalvik offset. |
| 41 | */ |
| 42 | #define DEBUGGER_METHOD_ENTRY -1 |
| 43 | #define DEBUGGER_METHOD_EXIT -2 |
| 44 | |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Assembly is an iterative process, and usually terminates within |
| 47 | * two or three passes. This should be high enough to handle bizarre |
| 48 | * cases, but detect an infinite loop bug. |
| 49 | */ |
| 50 | #define MAX_ASSEMBLER_RETRIES 50 |
| 51 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 52 | /* Suppress optimization if corresponding bit set */ |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 53 | enum optControlVector { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 54 | kLoadStoreElimination = 0, |
| 55 | kLoadHoisting, |
| 56 | kSuppressLoads, |
| 57 | kNullCheckElimination, |
| 58 | kPromoteRegs, |
| 59 | kTrackLiveTemps, |
| 60 | kSkipLargeMethodOptimization, |
| 61 | kSafeOptimizations, |
| 62 | kBBOpt, |
| 63 | kMatch, |
| 64 | kPromoteCompilerTemps, |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 67 | /* Type of allocation for memory tuning */ |
| 68 | enum oatAllocKind { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 69 | kAllocMisc, |
| 70 | kAllocBB, |
| 71 | kAllocLIR, |
| 72 | kAllocMIR, |
| 73 | kAllocDFInfo, |
| 74 | kAllocGrowableList, |
| 75 | kAllocGrowableBitMap, |
| 76 | kAllocDalvikToSSAMap, |
| 77 | kAllocDebugInfo, |
| 78 | kAllocSuccessor, |
| 79 | kAllocRegAlloc, |
| 80 | kAllocData, |
| 81 | kAllocPredecessors, |
| 82 | kNumAllocKinds |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | /* Type of growable list for memory tuning */ |
| 86 | enum oatListKind { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 87 | kListMisc = 0, |
| 88 | kListBlockList, |
| 89 | kListSSAtoDalvikMap, |
| 90 | kListDfsOrder, |
| 91 | kListDfsPostOrder, |
| 92 | kListDomPostOrderTraversal, |
| 93 | kListThrowLaunchPads, |
| 94 | kListSuspendLaunchPads, |
| 95 | kListSwitchTables, |
| 96 | kListFillArrayData, |
| 97 | kListSuccessorBlocks, |
| 98 | kListPredecessors, |
| 99 | kNumListKinds |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | /* Type of growable bitmap for memory tuning */ |
| 103 | enum oatBitMapKind { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 104 | kBitMapMisc = 0, |
| 105 | kBitMapUse, |
| 106 | kBitMapDef, |
| 107 | kBitMapLiveIn, |
| 108 | kBitMapBMatrix, |
| 109 | kBitMapDominators, |
| 110 | kBitMapIDominated, |
| 111 | kBitMapDomFrontier, |
| 112 | kBitMapPhi, |
| 113 | kBitMapTmpBlocks, |
| 114 | kBitMapInputBlocks, |
| 115 | kBitMapRegisterV, |
| 116 | kBitMapTempSSARegisterV, |
| 117 | kBitMapNullCheck, |
| 118 | kBitMapTmpBlockV, |
| 119 | kBitMapPredecessors, |
| 120 | kNumBitMapKinds |
buzbee | 5abfa3e | 2012-01-31 17:01:43 -0800 | [diff] [blame] | 121 | }; |
| 122 | |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 123 | /* Force code generation paths for testing */ |
| 124 | enum debugControlVector { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 125 | kDebugDisplayMissingTargets, |
| 126 | kDebugVerbose, |
| 127 | kDebugDumpCFG, |
| 128 | kDebugSlowFieldPath, |
| 129 | kDebugSlowInvokePath, |
| 130 | kDebugSlowStringPath, |
| 131 | kDebugSlowTypePath, |
| 132 | kDebugSlowestFieldPath, |
| 133 | kDebugSlowestStringPath, |
| 134 | kDebugExerciseResolveMethod, |
| 135 | kDebugVerifyDataflow, |
| 136 | kDebugShowMemoryUsage, |
| 137 | kDebugShowNops, |
| 138 | kDebugCountOpcodes, |
buzbee | d1643e4 | 2012-09-05 14:06:51 -0700 | [diff] [blame] | 139 | kDebugDumpCheckStats, |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 140 | kDebugDumpBitcodeFile, |
Bill Buzbee | c9f40dd | 2012-08-15 11:35:25 -0700 | [diff] [blame] | 141 | kDebugVerifyBitcode, |
buzbee | ce30293 | 2011-10-04 14:32:18 -0700 | [diff] [blame] | 142 | }; |
| 143 | |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 144 | enum OatMethodAttributes { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 145 | kIsCallee = 0, /* Code is part of a callee (invoked by a hot trace) */ |
| 146 | kIsHot, /* Code is part of a hot trace */ |
| 147 | kIsLeaf, /* Method is leaf */ |
| 148 | kIsEmpty, /* Method is empty */ |
| 149 | kIsThrowFree, /* Method doesn't throw */ |
| 150 | kIsGetter, /* Method fits the getter pattern */ |
| 151 | kIsSetter, /* Method fits the setter pattern */ |
| 152 | kCannotCompile, /* Method cannot be compiled */ |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 153 | }; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 154 | |
| 155 | #define METHOD_IS_CALLEE (1 << kIsCallee) |
| 156 | #define METHOD_IS_HOT (1 << kIsHot) |
| 157 | #define METHOD_IS_LEAF (1 << kIsLeaf) |
| 158 | #define METHOD_IS_EMPTY (1 << kIsEmpty) |
| 159 | #define METHOD_IS_THROW_FREE (1 << kIsThrowFree) |
| 160 | #define METHOD_IS_GETTER (1 << kIsGetter) |
| 161 | #define METHOD_IS_SETTER (1 << kIsSetter) |
| 162 | #define METHOD_CANNOT_COMPILE (1 << kCannotCompile) |
| 163 | |
| 164 | /* Customized node traversal orders for different needs */ |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 165 | enum DataFlowAnalysisMode { |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 166 | kAllNodes = 0, // All nodes |
| 167 | kReachableNodes, // All reachable nodes |
| 168 | kPreOrderDFSTraversal, // Depth-First-Search / Pre-Order |
| 169 | kPostOrderDFSTraversal, // Depth-First-Search / Post-Order |
| 170 | kPostOrderDOMTraversal, // Dominator tree / Post-Order |
| 171 | kReversePostOrderTraversal, // Depth-First-Search / reverse Post-Order |
Elliott Hughes | 719ace4 | 2012-03-09 18:06:03 -0800 | [diff] [blame] | 172 | }; |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 173 | |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 174 | class LLVMInfo { |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 175 | public: |
buzbee | 4df2bbd | 2012-10-11 14:46:06 -0700 | [diff] [blame] | 176 | LLVMInfo(); |
| 177 | ~LLVMInfo(); |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 178 | |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 179 | llvm::LLVMContext* GetLLVMContext() { |
| 180 | return llvm_context_.get(); |
| 181 | } |
| 182 | |
| 183 | llvm::Module* GetLLVMModule() { |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 184 | return llvm_module_; |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | art::greenland::IntrinsicHelper* GetIntrinsicHelper() { |
| 188 | return intrinsic_helper_.get(); |
| 189 | } |
| 190 | |
| 191 | art::greenland::IRBuilder* GetIRBuilder() { |
| 192 | return ir_builder_.get(); |
| 193 | } |
| 194 | |
| 195 | private: |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 196 | UniquePtr<llvm::LLVMContext> llvm_context_; |
TDYa127 | 55e5e6c | 2012-09-11 15:14:42 -0700 | [diff] [blame] | 197 | llvm::Module* llvm_module_; // Managed by context_ |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 198 | UniquePtr<art::greenland::IntrinsicHelper> intrinsic_helper_; |
| 199 | UniquePtr<art::greenland::IRBuilder> ir_builder_; |
| 200 | }; |
buzbee | 692be80 | 2012-08-29 15:52:59 -0700 | [diff] [blame] | 201 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 202 | struct CompilationUnit; |
| 203 | struct BasicBlock; |
| 204 | struct SSARepresentation; |
| 205 | struct GrowableList; |
| 206 | struct MIR; |
| 207 | |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 208 | void oatInit(CompilationUnit* cUnit, const Compiler& compiler); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 209 | bool oatArchInit(void); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 210 | bool oatStartup(void); |
| 211 | void oatShutdown(void); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 212 | void oatScanAllClassPointers(void (*callback)(void* ptr)); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 213 | void oatInitializeSSAConversion(CompilationUnit* cUnit); |
| 214 | int SRegToVReg(const CompilationUnit* cUnit, int ssaReg); |
| 215 | int SRegToSubscript(const CompilationUnit* cUnit, int ssaReg); |
| 216 | bool oatFindLocalLiveIn(CompilationUnit* cUnit, BasicBlock* bb); |
| 217 | bool oatDoSSAConversion(CompilationUnit* cUnit, BasicBlock* bb); |
| 218 | bool oatDoConstantPropagation(CompilationUnit* cUnit, BasicBlock* bb); |
| 219 | bool oatFindInductionVariables(CompilationUnit* cUnit, BasicBlock* bb); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 220 | /* Clear the visited flag for each BB */ |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 221 | bool oatClearVisitedFlag(CompilationUnit* cUnit, BasicBlock* bb); |
| 222 | char* oatGetDalvikDisassembly(CompilationUnit* cUnit, const DecodedInstruction& insn, |
buzbee | ba938cb | 2012-02-03 14:47:55 -0800 | [diff] [blame] | 223 | const char* note); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 224 | char* oatFullDisassembler(CompilationUnit* cUnit, const MIR* mir); |
| 225 | char* oatGetSSAString(CompilationUnit* cUnit, SSARepresentation* ssaRep); |
| 226 | void oatDataFlowAnalysisDispatcher(CompilationUnit* cUnit, |
Bill Buzbee | a114add | 2012-05-03 15:00:40 -0700 | [diff] [blame] | 227 | bool (*func)(CompilationUnit* , BasicBlock*), |
| 228 | DataFlowAnalysisMode dfaMode, |
| 229 | bool isIterative); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 230 | void oatMethodSSATransformation(CompilationUnit* cUnit); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 231 | u8 oatGetRegResourceMask(int reg); |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 232 | void oatDumpCFG(CompilationUnit* cUnit, const char* dirPrefix); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 233 | void oatProcessSwitchTables(CompilationUnit* cUnit); |
buzbee | e3acd07 | 2012-02-25 17:03:10 -0800 | [diff] [blame] | 234 | bool oatIsFpReg(int reg); |
| 235 | uint32_t oatFpRegMask(void); |
buzbee | ad8f15e | 2012-06-18 14:49:45 -0700 | [diff] [blame] | 236 | void oatReplaceSpecialChars(std::string& str); |
buzbee | f58c12c | 2012-07-03 15:06:29 -0700 | [diff] [blame] | 237 | BasicBlock* oatFindBlock(CompilationUnit* cUnit, unsigned int codeOffset); |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 238 | |
Elliott Hughes | 11d1b0c | 2012-01-23 16:57:47 -0800 | [diff] [blame] | 239 | } // namespace art |
| 240 | |
Elliott Hughes | 3fa1b7e | 2012-03-13 17:06:22 -0700 | [diff] [blame] | 241 | extern "C" art::CompiledMethod* ArtCompileMethod(art::Compiler& compiler, |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 242 | const art::DexFile::CodeItem* code_item, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 243 | uint32_t access_flags, |
| 244 | art::InvokeType invoke_type, |
| 245 | uint32_t method_idx, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 246 | jobject class_loader, |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 247 | const art::DexFile& dex_file); |
| 248 | |
buzbee | 67bf885 | 2011-08-17 17:51:35 -0700 | [diff] [blame] | 249 | #endif // ART_SRC_COMPILER_COMPILER_H_ |