Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | #include "quick_compiler.h" |
| 18 | |
| 19 | #include <cstdint> |
| 20 | |
| 21 | #include "compiler.h" |
| 22 | #include "dex/frontend.h" |
| 23 | #include "dex/mir_graph.h" |
| 24 | #include "dex/quick/mir_to_lir.h" |
| 25 | #include "driver/compiler_driver.h" |
| 26 | #include "elf_writer_quick.h" |
| 27 | #include "jni/quick/jni_compiler.h" |
| 28 | #include "mirror/art_method-inl.h" |
| 29 | #include "base/logging.h" |
| 30 | |
| 31 | // Specific compiler backends. |
| 32 | #include "dex/quick/arm/backend_arm.h" |
| 33 | #include "dex/quick/arm64/backend_arm64.h" |
| 34 | #include "dex/quick/mips/backend_mips.h" |
| 35 | #include "dex/quick/x86/backend_x86.h" |
| 36 | |
| 37 | namespace art { |
| 38 | |
| 39 | class QuickCompiler : public Compiler { |
| 40 | public: |
| 41 | explicit QuickCompiler(CompilerDriver* driver) : Compiler(driver, 100) {} |
| 42 | |
| 43 | void Init() const OVERRIDE; |
| 44 | |
| 45 | void UnInit() const OVERRIDE; |
| 46 | |
| 47 | bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const |
| 48 | OVERRIDE; |
| 49 | |
| 50 | CompiledMethod* Compile(const DexFile::CodeItem* code_item, |
| 51 | uint32_t access_flags, |
| 52 | InvokeType invoke_type, |
| 53 | uint16_t class_def_idx, |
| 54 | uint32_t method_idx, |
| 55 | jobject class_loader, |
| 56 | const DexFile& dex_file) const OVERRIDE; |
| 57 | |
| 58 | CompiledMethod* JniCompile(uint32_t access_flags, |
| 59 | uint32_t method_idx, |
| 60 | const DexFile& dex_file) const OVERRIDE; |
| 61 | |
| 62 | uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE |
| 63 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 64 | |
| 65 | bool WriteElf(art::File* file, |
| 66 | OatWriter* oat_writer, |
| 67 | const std::vector<const art::DexFile*>& dex_files, |
| 68 | const std::string& android_root, |
| 69 | bool is_host) const |
| 70 | OVERRIDE |
| 71 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); |
| 72 | |
| 73 | Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const OVERRIDE; |
| 74 | |
| 75 | void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE; |
| 76 | |
| 77 | private: |
| 78 | DISALLOW_COPY_AND_ASSIGN(QuickCompiler); |
| 79 | }; |
| 80 | |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 81 | static_assert(0U == static_cast<size_t>(kNone), "kNone not 0"); |
| 82 | static_assert(1U == static_cast<size_t>(kArm), "kArm not 1"); |
| 83 | static_assert(2U == static_cast<size_t>(kArm64), "kArm64 not 2"); |
| 84 | static_assert(3U == static_cast<size_t>(kThumb2), "kThumb2 not 3"); |
| 85 | static_assert(4U == static_cast<size_t>(kX86), "kX86 not 4"); |
| 86 | static_assert(5U == static_cast<size_t>(kX86_64), "kX86_64 not 5"); |
| 87 | static_assert(6U == static_cast<size_t>(kMips), "kMips not 6"); |
| 88 | static_assert(7U == static_cast<size_t>(kMips64), "kMips64 not 7"); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 89 | |
| 90 | // Additional disabled optimizations (over generally disabled) per instruction set. |
| 91 | static constexpr uint32_t kDisabledOptimizationsPerISA[] = { |
| 92 | // 0 = kNone. |
| 93 | ~0U, |
| 94 | // 1 = kArm, unused (will use kThumb2). |
| 95 | ~0U, |
| 96 | // 2 = kArm64. |
| 97 | 0, |
| 98 | // 3 = kThumb2. |
| 99 | 0, |
| 100 | // 4 = kX86. |
| 101 | (1 << kLoadStoreElimination) | |
| 102 | 0, |
| 103 | // 5 = kX86_64. |
| 104 | (1 << kLoadStoreElimination) | |
| 105 | 0, |
| 106 | // 6 = kMips. |
| 107 | (1 << kLoadStoreElimination) | |
| 108 | (1 << kLoadHoisting) | |
| 109 | (1 << kSuppressLoads) | |
| 110 | (1 << kNullCheckElimination) | |
| 111 | (1 << kPromoteRegs) | |
| 112 | (1 << kTrackLiveTemps) | |
| 113 | (1 << kSafeOptimizations) | |
| 114 | (1 << kBBOpt) | |
| 115 | (1 << kMatch) | |
| 116 | (1 << kPromoteCompilerTemps) | |
| 117 | 0, |
| 118 | // 7 = kMips64. |
| 119 | ~0U |
| 120 | }; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 121 | static_assert(sizeof(kDisabledOptimizationsPerISA) == 8 * sizeof(uint32_t), |
| 122 | "kDisabledOpts unexpected"); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 123 | |
| 124 | // Supported shorty types per instruction set. nullptr means that all are available. |
| 125 | // Z : boolean |
| 126 | // B : byte |
| 127 | // S : short |
| 128 | // C : char |
| 129 | // I : int |
| 130 | // J : long |
| 131 | // F : float |
| 132 | // D : double |
| 133 | // L : reference(object, array) |
| 134 | // V : void |
| 135 | static const char* kSupportedTypes[] = { |
| 136 | // 0 = kNone. |
| 137 | "", |
| 138 | // 1 = kArm, unused (will use kThumb2). |
| 139 | "", |
| 140 | // 2 = kArm64. |
| 141 | nullptr, |
| 142 | // 3 = kThumb2. |
| 143 | nullptr, |
| 144 | // 4 = kX86. |
| 145 | nullptr, |
| 146 | // 5 = kX86_64. |
| 147 | nullptr, |
| 148 | // 6 = kMips. |
| 149 | nullptr, |
| 150 | // 7 = kMips64. |
| 151 | "" |
| 152 | }; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 153 | static_assert(sizeof(kSupportedTypes) == 8 * sizeof(char*), "kSupportedTypes unexpected"); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 154 | |
| 155 | static int kAllOpcodes[] = { |
| 156 | Instruction::NOP, |
| 157 | Instruction::MOVE, |
| 158 | Instruction::MOVE_FROM16, |
| 159 | Instruction::MOVE_16, |
| 160 | Instruction::MOVE_WIDE, |
| 161 | Instruction::MOVE_WIDE_FROM16, |
| 162 | Instruction::MOVE_WIDE_16, |
| 163 | Instruction::MOVE_OBJECT, |
| 164 | Instruction::MOVE_OBJECT_FROM16, |
| 165 | Instruction::MOVE_OBJECT_16, |
| 166 | Instruction::MOVE_RESULT, |
| 167 | Instruction::MOVE_RESULT_WIDE, |
| 168 | Instruction::MOVE_RESULT_OBJECT, |
| 169 | Instruction::MOVE_EXCEPTION, |
| 170 | Instruction::RETURN_VOID, |
| 171 | Instruction::RETURN, |
| 172 | Instruction::RETURN_WIDE, |
| 173 | Instruction::RETURN_OBJECT, |
| 174 | Instruction::CONST_4, |
| 175 | Instruction::CONST_16, |
| 176 | Instruction::CONST, |
| 177 | Instruction::CONST_HIGH16, |
| 178 | Instruction::CONST_WIDE_16, |
| 179 | Instruction::CONST_WIDE_32, |
| 180 | Instruction::CONST_WIDE, |
| 181 | Instruction::CONST_WIDE_HIGH16, |
| 182 | Instruction::CONST_STRING, |
| 183 | Instruction::CONST_STRING_JUMBO, |
| 184 | Instruction::CONST_CLASS, |
| 185 | Instruction::MONITOR_ENTER, |
| 186 | Instruction::MONITOR_EXIT, |
| 187 | Instruction::CHECK_CAST, |
| 188 | Instruction::INSTANCE_OF, |
| 189 | Instruction::ARRAY_LENGTH, |
| 190 | Instruction::NEW_INSTANCE, |
| 191 | Instruction::NEW_ARRAY, |
| 192 | Instruction::FILLED_NEW_ARRAY, |
| 193 | Instruction::FILLED_NEW_ARRAY_RANGE, |
| 194 | Instruction::FILL_ARRAY_DATA, |
| 195 | Instruction::THROW, |
| 196 | Instruction::GOTO, |
| 197 | Instruction::GOTO_16, |
| 198 | Instruction::GOTO_32, |
| 199 | Instruction::PACKED_SWITCH, |
| 200 | Instruction::SPARSE_SWITCH, |
| 201 | Instruction::CMPL_FLOAT, |
| 202 | Instruction::CMPG_FLOAT, |
| 203 | Instruction::CMPL_DOUBLE, |
| 204 | Instruction::CMPG_DOUBLE, |
| 205 | Instruction::CMP_LONG, |
| 206 | Instruction::IF_EQ, |
| 207 | Instruction::IF_NE, |
| 208 | Instruction::IF_LT, |
| 209 | Instruction::IF_GE, |
| 210 | Instruction::IF_GT, |
| 211 | Instruction::IF_LE, |
| 212 | Instruction::IF_EQZ, |
| 213 | Instruction::IF_NEZ, |
| 214 | Instruction::IF_LTZ, |
| 215 | Instruction::IF_GEZ, |
| 216 | Instruction::IF_GTZ, |
| 217 | Instruction::IF_LEZ, |
| 218 | Instruction::UNUSED_3E, |
| 219 | Instruction::UNUSED_3F, |
| 220 | Instruction::UNUSED_40, |
| 221 | Instruction::UNUSED_41, |
| 222 | Instruction::UNUSED_42, |
| 223 | Instruction::UNUSED_43, |
| 224 | Instruction::AGET, |
| 225 | Instruction::AGET_WIDE, |
| 226 | Instruction::AGET_OBJECT, |
| 227 | Instruction::AGET_BOOLEAN, |
| 228 | Instruction::AGET_BYTE, |
| 229 | Instruction::AGET_CHAR, |
| 230 | Instruction::AGET_SHORT, |
| 231 | Instruction::APUT, |
| 232 | Instruction::APUT_WIDE, |
| 233 | Instruction::APUT_OBJECT, |
| 234 | Instruction::APUT_BOOLEAN, |
| 235 | Instruction::APUT_BYTE, |
| 236 | Instruction::APUT_CHAR, |
| 237 | Instruction::APUT_SHORT, |
| 238 | Instruction::IGET, |
| 239 | Instruction::IGET_WIDE, |
| 240 | Instruction::IGET_OBJECT, |
| 241 | Instruction::IGET_BOOLEAN, |
| 242 | Instruction::IGET_BYTE, |
| 243 | Instruction::IGET_CHAR, |
| 244 | Instruction::IGET_SHORT, |
| 245 | Instruction::IPUT, |
| 246 | Instruction::IPUT_WIDE, |
| 247 | Instruction::IPUT_OBJECT, |
| 248 | Instruction::IPUT_BOOLEAN, |
| 249 | Instruction::IPUT_BYTE, |
| 250 | Instruction::IPUT_CHAR, |
| 251 | Instruction::IPUT_SHORT, |
| 252 | Instruction::SGET, |
| 253 | Instruction::SGET_WIDE, |
| 254 | Instruction::SGET_OBJECT, |
| 255 | Instruction::SGET_BOOLEAN, |
| 256 | Instruction::SGET_BYTE, |
| 257 | Instruction::SGET_CHAR, |
| 258 | Instruction::SGET_SHORT, |
| 259 | Instruction::SPUT, |
| 260 | Instruction::SPUT_WIDE, |
| 261 | Instruction::SPUT_OBJECT, |
| 262 | Instruction::SPUT_BOOLEAN, |
| 263 | Instruction::SPUT_BYTE, |
| 264 | Instruction::SPUT_CHAR, |
| 265 | Instruction::SPUT_SHORT, |
| 266 | Instruction::INVOKE_VIRTUAL, |
| 267 | Instruction::INVOKE_SUPER, |
| 268 | Instruction::INVOKE_DIRECT, |
| 269 | Instruction::INVOKE_STATIC, |
| 270 | Instruction::INVOKE_INTERFACE, |
| 271 | Instruction::RETURN_VOID_BARRIER, |
| 272 | Instruction::INVOKE_VIRTUAL_RANGE, |
| 273 | Instruction::INVOKE_SUPER_RANGE, |
| 274 | Instruction::INVOKE_DIRECT_RANGE, |
| 275 | Instruction::INVOKE_STATIC_RANGE, |
| 276 | Instruction::INVOKE_INTERFACE_RANGE, |
| 277 | Instruction::UNUSED_79, |
| 278 | Instruction::UNUSED_7A, |
| 279 | Instruction::NEG_INT, |
| 280 | Instruction::NOT_INT, |
| 281 | Instruction::NEG_LONG, |
| 282 | Instruction::NOT_LONG, |
| 283 | Instruction::NEG_FLOAT, |
| 284 | Instruction::NEG_DOUBLE, |
| 285 | Instruction::INT_TO_LONG, |
| 286 | Instruction::INT_TO_FLOAT, |
| 287 | Instruction::INT_TO_DOUBLE, |
| 288 | Instruction::LONG_TO_INT, |
| 289 | Instruction::LONG_TO_FLOAT, |
| 290 | Instruction::LONG_TO_DOUBLE, |
| 291 | Instruction::FLOAT_TO_INT, |
| 292 | Instruction::FLOAT_TO_LONG, |
| 293 | Instruction::FLOAT_TO_DOUBLE, |
| 294 | Instruction::DOUBLE_TO_INT, |
| 295 | Instruction::DOUBLE_TO_LONG, |
| 296 | Instruction::DOUBLE_TO_FLOAT, |
| 297 | Instruction::INT_TO_BYTE, |
| 298 | Instruction::INT_TO_CHAR, |
| 299 | Instruction::INT_TO_SHORT, |
| 300 | Instruction::ADD_INT, |
| 301 | Instruction::SUB_INT, |
| 302 | Instruction::MUL_INT, |
| 303 | Instruction::DIV_INT, |
| 304 | Instruction::REM_INT, |
| 305 | Instruction::AND_INT, |
| 306 | Instruction::OR_INT, |
| 307 | Instruction::XOR_INT, |
| 308 | Instruction::SHL_INT, |
| 309 | Instruction::SHR_INT, |
| 310 | Instruction::USHR_INT, |
| 311 | Instruction::ADD_LONG, |
| 312 | Instruction::SUB_LONG, |
| 313 | Instruction::MUL_LONG, |
| 314 | Instruction::DIV_LONG, |
| 315 | Instruction::REM_LONG, |
| 316 | Instruction::AND_LONG, |
| 317 | Instruction::OR_LONG, |
| 318 | Instruction::XOR_LONG, |
| 319 | Instruction::SHL_LONG, |
| 320 | Instruction::SHR_LONG, |
| 321 | Instruction::USHR_LONG, |
| 322 | Instruction::ADD_FLOAT, |
| 323 | Instruction::SUB_FLOAT, |
| 324 | Instruction::MUL_FLOAT, |
| 325 | Instruction::DIV_FLOAT, |
| 326 | Instruction::REM_FLOAT, |
| 327 | Instruction::ADD_DOUBLE, |
| 328 | Instruction::SUB_DOUBLE, |
| 329 | Instruction::MUL_DOUBLE, |
| 330 | Instruction::DIV_DOUBLE, |
| 331 | Instruction::REM_DOUBLE, |
| 332 | Instruction::ADD_INT_2ADDR, |
| 333 | Instruction::SUB_INT_2ADDR, |
| 334 | Instruction::MUL_INT_2ADDR, |
| 335 | Instruction::DIV_INT_2ADDR, |
| 336 | Instruction::REM_INT_2ADDR, |
| 337 | Instruction::AND_INT_2ADDR, |
| 338 | Instruction::OR_INT_2ADDR, |
| 339 | Instruction::XOR_INT_2ADDR, |
| 340 | Instruction::SHL_INT_2ADDR, |
| 341 | Instruction::SHR_INT_2ADDR, |
| 342 | Instruction::USHR_INT_2ADDR, |
| 343 | Instruction::ADD_LONG_2ADDR, |
| 344 | Instruction::SUB_LONG_2ADDR, |
| 345 | Instruction::MUL_LONG_2ADDR, |
| 346 | Instruction::DIV_LONG_2ADDR, |
| 347 | Instruction::REM_LONG_2ADDR, |
| 348 | Instruction::AND_LONG_2ADDR, |
| 349 | Instruction::OR_LONG_2ADDR, |
| 350 | Instruction::XOR_LONG_2ADDR, |
| 351 | Instruction::SHL_LONG_2ADDR, |
| 352 | Instruction::SHR_LONG_2ADDR, |
| 353 | Instruction::USHR_LONG_2ADDR, |
| 354 | Instruction::ADD_FLOAT_2ADDR, |
| 355 | Instruction::SUB_FLOAT_2ADDR, |
| 356 | Instruction::MUL_FLOAT_2ADDR, |
| 357 | Instruction::DIV_FLOAT_2ADDR, |
| 358 | Instruction::REM_FLOAT_2ADDR, |
| 359 | Instruction::ADD_DOUBLE_2ADDR, |
| 360 | Instruction::SUB_DOUBLE_2ADDR, |
| 361 | Instruction::MUL_DOUBLE_2ADDR, |
| 362 | Instruction::DIV_DOUBLE_2ADDR, |
| 363 | Instruction::REM_DOUBLE_2ADDR, |
| 364 | Instruction::ADD_INT_LIT16, |
| 365 | Instruction::RSUB_INT, |
| 366 | Instruction::MUL_INT_LIT16, |
| 367 | Instruction::DIV_INT_LIT16, |
| 368 | Instruction::REM_INT_LIT16, |
| 369 | Instruction::AND_INT_LIT16, |
| 370 | Instruction::OR_INT_LIT16, |
| 371 | Instruction::XOR_INT_LIT16, |
| 372 | Instruction::ADD_INT_LIT8, |
| 373 | Instruction::RSUB_INT_LIT8, |
| 374 | Instruction::MUL_INT_LIT8, |
| 375 | Instruction::DIV_INT_LIT8, |
| 376 | Instruction::REM_INT_LIT8, |
| 377 | Instruction::AND_INT_LIT8, |
| 378 | Instruction::OR_INT_LIT8, |
| 379 | Instruction::XOR_INT_LIT8, |
| 380 | Instruction::SHL_INT_LIT8, |
| 381 | Instruction::SHR_INT_LIT8, |
| 382 | Instruction::USHR_INT_LIT8, |
| 383 | Instruction::IGET_QUICK, |
| 384 | Instruction::IGET_WIDE_QUICK, |
| 385 | Instruction::IGET_OBJECT_QUICK, |
| 386 | Instruction::IPUT_QUICK, |
| 387 | Instruction::IPUT_WIDE_QUICK, |
| 388 | Instruction::IPUT_OBJECT_QUICK, |
| 389 | Instruction::INVOKE_VIRTUAL_QUICK, |
| 390 | Instruction::INVOKE_VIRTUAL_RANGE_QUICK, |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 391 | Instruction::IPUT_BOOLEAN_QUICK, |
| 392 | Instruction::IPUT_BYTE_QUICK, |
| 393 | Instruction::IPUT_CHAR_QUICK, |
| 394 | Instruction::IPUT_SHORT_QUICK, |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 395 | Instruction::UNUSED_EF, |
| 396 | Instruction::UNUSED_F0, |
| 397 | Instruction::UNUSED_F1, |
| 398 | Instruction::UNUSED_F2, |
| 399 | Instruction::UNUSED_F3, |
| 400 | Instruction::UNUSED_F4, |
| 401 | Instruction::UNUSED_F5, |
| 402 | Instruction::UNUSED_F6, |
| 403 | Instruction::UNUSED_F7, |
| 404 | Instruction::UNUSED_F8, |
| 405 | Instruction::UNUSED_F9, |
| 406 | Instruction::UNUSED_FA, |
| 407 | Instruction::UNUSED_FB, |
| 408 | Instruction::UNUSED_FC, |
| 409 | Instruction::UNUSED_FD, |
| 410 | Instruction::UNUSED_FE, |
| 411 | Instruction::UNUSED_FF, |
| 412 | // ----- ExtendedMIROpcode ----- |
| 413 | kMirOpPhi, |
| 414 | kMirOpCopy, |
| 415 | kMirOpFusedCmplFloat, |
| 416 | kMirOpFusedCmpgFloat, |
| 417 | kMirOpFusedCmplDouble, |
| 418 | kMirOpFusedCmpgDouble, |
| 419 | kMirOpFusedCmpLong, |
| 420 | kMirOpNop, |
| 421 | kMirOpNullCheck, |
| 422 | kMirOpRangeCheck, |
| 423 | kMirOpDivZeroCheck, |
| 424 | kMirOpCheck, |
| 425 | kMirOpCheckPart2, |
| 426 | kMirOpSelect, |
| 427 | }; |
| 428 | |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 429 | static int kInvokeOpcodes[] = { |
| 430 | Instruction::INVOKE_VIRTUAL, |
| 431 | Instruction::INVOKE_SUPER, |
| 432 | Instruction::INVOKE_DIRECT, |
| 433 | Instruction::INVOKE_STATIC, |
| 434 | Instruction::INVOKE_INTERFACE, |
| 435 | Instruction::INVOKE_VIRTUAL_RANGE, |
| 436 | Instruction::INVOKE_SUPER_RANGE, |
| 437 | Instruction::INVOKE_DIRECT_RANGE, |
| 438 | Instruction::INVOKE_STATIC_RANGE, |
| 439 | Instruction::INVOKE_INTERFACE_RANGE, |
| 440 | Instruction::INVOKE_VIRTUAL_QUICK, |
| 441 | Instruction::INVOKE_VIRTUAL_RANGE_QUICK, |
| 442 | }; |
| 443 | |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 444 | // Unsupported opcodes. nullptr can be used when everything is supported. Size of the lists is |
| 445 | // recorded below. |
| 446 | static const int* kUnsupportedOpcodes[] = { |
| 447 | // 0 = kNone. |
| 448 | kAllOpcodes, |
| 449 | // 1 = kArm, unused (will use kThumb2). |
| 450 | kAllOpcodes, |
| 451 | // 2 = kArm64. |
| 452 | nullptr, |
| 453 | // 3 = kThumb2. |
| 454 | nullptr, |
| 455 | // 4 = kX86. |
| 456 | nullptr, |
| 457 | // 5 = kX86_64. |
| 458 | nullptr, |
| 459 | // 6 = kMips. |
| 460 | nullptr, |
| 461 | // 7 = kMips64. |
| 462 | kAllOpcodes |
| 463 | }; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 464 | static_assert(sizeof(kUnsupportedOpcodes) == 8 * sizeof(int*), "kUnsupportedOpcodes unexpected"); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 465 | |
| 466 | // Size of the arrays stored above. |
| 467 | static const size_t kUnsupportedOpcodesSize[] = { |
| 468 | // 0 = kNone. |
| 469 | arraysize(kAllOpcodes), |
| 470 | // 1 = kArm, unused (will use kThumb2). |
| 471 | arraysize(kAllOpcodes), |
| 472 | // 2 = kArm64. |
| 473 | 0, |
| 474 | // 3 = kThumb2. |
| 475 | 0, |
| 476 | // 4 = kX86. |
| 477 | 0, |
| 478 | // 5 = kX86_64. |
| 479 | 0, |
| 480 | // 6 = kMips. |
| 481 | 0, |
| 482 | // 7 = kMips64. |
| 483 | arraysize(kAllOpcodes), |
| 484 | }; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 485 | static_assert(sizeof(kUnsupportedOpcodesSize) == 8 * sizeof(size_t), |
| 486 | "kUnsupportedOpcodesSize unexpected"); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 487 | |
| 488 | // The maximum amount of Dalvik register in a method for which we will start compiling. Tries to |
| 489 | // avoid an abort when we need to manage more SSA registers than we can. |
| 490 | static constexpr size_t kMaxAllowedDalvikRegisters = INT16_MAX / 2; |
| 491 | |
| 492 | static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) { |
| 493 | const char* supported_types = kSupportedTypes[instruction_set]; |
| 494 | if (supported_types == nullptr) { |
| 495 | // Everything available. |
| 496 | return true; |
| 497 | } |
| 498 | |
| 499 | uint32_t shorty_size = strlen(shorty); |
| 500 | CHECK_GE(shorty_size, 1u); |
| 501 | |
| 502 | for (uint32_t i = 0; i < shorty_size; i++) { |
| 503 | if (strchr(supported_types, shorty[i]) == nullptr) { |
| 504 | return false; |
| 505 | } |
| 506 | } |
| 507 | return true; |
Andreas Gampe | c8ccf68 | 2014-09-29 20:07:43 -0700 | [diff] [blame] | 508 | } |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 509 | |
| 510 | // Skip the method that we do not support currently. |
| 511 | bool QuickCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, |
| 512 | CompilationUnit* cu) const { |
| 513 | // This is a limitation in mir_graph. See MirGraph::SetNumSSARegs. |
Razvan A Lupusoru | 8d0d03e | 2014-06-06 17:04:52 -0700 | [diff] [blame] | 514 | if (cu->mir_graph->GetNumOfCodeAndTempVRs() > kMaxAllowedDalvikRegisters) { |
| 515 | VLOG(compiler) << "Too many dalvik registers : " << cu->mir_graph->GetNumOfCodeAndTempVRs(); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // Check whether we do have limitations at all. |
| 520 | if (kSupportedTypes[cu->instruction_set] == nullptr && |
| 521 | kUnsupportedOpcodesSize[cu->instruction_set] == 0U) { |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | // Check if we can compile the prototype. |
| 526 | const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx)); |
| 527 | if (!CanCompileShorty(shorty, cu->instruction_set)) { |
| 528 | VLOG(compiler) << "Unsupported shorty : " << shorty; |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | const int *unsupport_list = kUnsupportedOpcodes[cu->instruction_set]; |
| 533 | int unsupport_list_size = kUnsupportedOpcodesSize[cu->instruction_set]; |
| 534 | |
| 535 | for (unsigned int idx = 0; idx < cu->mir_graph->GetNumBlocks(); idx++) { |
| 536 | BasicBlock* bb = cu->mir_graph->GetBasicBlock(idx); |
| 537 | if (bb == NULL) continue; |
| 538 | if (bb->block_type == kDead) continue; |
| 539 | for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) { |
| 540 | int opcode = mir->dalvikInsn.opcode; |
| 541 | // Check if we support the byte code. |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 542 | if (std::find(unsupport_list, unsupport_list + unsupport_list_size, opcode) |
| 543 | != unsupport_list + unsupport_list_size) { |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 544 | if (!MIR::DecodedInstruction::IsPseudoMirOp(opcode)) { |
| 545 | VLOG(compiler) << "Unsupported dalvik byte code : " |
| 546 | << mir->dalvikInsn.opcode; |
| 547 | } else { |
| 548 | VLOG(compiler) << "Unsupported extended MIR opcode : " |
| 549 | << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst]; |
| 550 | } |
| 551 | return false; |
| 552 | } |
| 553 | // Check if it invokes a prototype that we cannot support. |
Zheng Xu | 5667fdb | 2014-10-23 18:29:55 +0800 | [diff] [blame] | 554 | if (std::find(kInvokeOpcodes, kInvokeOpcodes + arraysize(kInvokeOpcodes), opcode) |
| 555 | != kInvokeOpcodes + arraysize(kInvokeOpcodes)) { |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 556 | uint32_t invoke_method_idx = mir->dalvikInsn.vB; |
| 557 | const char* invoke_method_shorty = dex_file.GetMethodShorty( |
| 558 | dex_file.GetMethodId(invoke_method_idx)); |
| 559 | if (!CanCompileShorty(invoke_method_shorty, cu->instruction_set)) { |
| 560 | VLOG(compiler) << "Unsupported to invoke '" |
| 561 | << PrettyMethod(invoke_method_idx, dex_file) |
| 562 | << "' with shorty : " << invoke_method_shorty; |
| 563 | return false; |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | return true; |
| 569 | } |
| 570 | |
| 571 | void QuickCompiler::InitCompilationUnit(CompilationUnit& cu) const { |
| 572 | // Disable optimizations according to instruction set. |
| 573 | cu.disable_opt |= kDisabledOptimizationsPerISA[cu.instruction_set]; |
| 574 | } |
| 575 | |
| 576 | void QuickCompiler::Init() const { |
| 577 | CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr); |
| 578 | } |
| 579 | |
| 580 | void QuickCompiler::UnInit() const { |
| 581 | CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr); |
| 582 | } |
| 583 | |
| 584 | CompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item, |
| 585 | uint32_t access_flags, |
| 586 | InvokeType invoke_type, |
| 587 | uint16_t class_def_idx, |
| 588 | uint32_t method_idx, |
| 589 | jobject class_loader, |
| 590 | const DexFile& dex_file) const { |
| 591 | CompiledMethod* method = TryCompileWithSeaIR(code_item, |
| 592 | access_flags, |
| 593 | invoke_type, |
| 594 | class_def_idx, |
| 595 | method_idx, |
| 596 | class_loader, |
| 597 | dex_file); |
| 598 | if (method != nullptr) { |
| 599 | return method; |
| 600 | } |
| 601 | |
| 602 | // TODO: check method fingerprint here to determine appropriate backend type. Until then, use |
| 603 | // build default. |
| 604 | CompilerDriver* driver = GetCompilerDriver(); |
| 605 | return CompileOneMethod(driver, this, code_item, access_flags, invoke_type, class_def_idx, |
| 606 | method_idx, class_loader, dex_file, nullptr /* use thread llvm_info */); |
| 607 | } |
| 608 | |
| 609 | CompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags, |
| 610 | uint32_t method_idx, |
| 611 | const DexFile& dex_file) const { |
| 612 | return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); |
| 613 | } |
| 614 | |
| 615 | uintptr_t QuickCompiler::GetEntryPointOf(mirror::ArtMethod* method) const { |
| 616 | return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()); |
| 617 | } |
| 618 | |
| 619 | bool QuickCompiler::WriteElf(art::File* file, |
| 620 | OatWriter* oat_writer, |
| 621 | const std::vector<const art::DexFile*>& dex_files, |
| 622 | const std::string& android_root, |
| 623 | bool is_host) const { |
Nicolas Geoffray | f9b87b1 | 2014-09-02 08:12:09 +0000 | [diff] [blame] | 624 | return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host, |
| 625 | *GetCompilerDriver()); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | Backend* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const { |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 629 | UNUSED(compilation_unit); |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 630 | Mir2Lir* mir_to_lir = nullptr; |
| 631 | switch (cu->instruction_set) { |
| 632 | case kThumb2: |
| 633 | mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 634 | break; |
| 635 | case kArm64: |
| 636 | mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 637 | break; |
| 638 | case kMips: |
| 639 | mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 640 | break; |
| 641 | case kX86: |
| 642 | // Fall-through. |
| 643 | case kX86_64: |
| 644 | mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena); |
| 645 | break; |
| 646 | default: |
| 647 | LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set; |
| 648 | } |
| 649 | |
| 650 | /* The number of compiler temporaries depends on backend so set it up now if possible */ |
| 651 | if (mir_to_lir) { |
| 652 | size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps(); |
| 653 | bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps); |
| 654 | CHECK(set_max); |
| 655 | } |
| 656 | return mir_to_lir; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | Compiler* CreateQuickCompiler(CompilerDriver* driver) { |
| 661 | return new QuickCompiler(driver); |
| 662 | } |
| 663 | |
| 664 | } // namespace art |