optimizing_compiler.cc revision c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74
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 "optimizing_compiler.h" 18 19#include <fstream> 20#include <stdint.h> 21 22#ifdef ART_ENABLE_CODEGEN_arm64 23#include "instruction_simplifier_arm64.h" 24#endif 25 26#ifdef ART_ENABLE_CODEGEN_x86 27#include "constant_area_fixups_x86.h" 28#endif 29 30#include "art_method-inl.h" 31#include "base/arena_allocator.h" 32#include "base/arena_containers.h" 33#include "base/dumpable.h" 34#include "base/macros.h" 35#include "base/timing_logger.h" 36#include "boolean_simplifier.h" 37#include "bounds_check_elimination.h" 38#include "builder.h" 39#include "code_generator.h" 40#include "compiled_method.h" 41#include "compiler.h" 42#include "constant_folding.h" 43#include "dead_code_elimination.h" 44#include "dex/quick/dex_file_to_method_inliner_map.h" 45#include "dex/verified_method.h" 46#include "dex/verification_results.h" 47#include "driver/compiler_driver.h" 48#include "driver/compiler_driver-inl.h" 49#include "driver/compiler_options.h" 50#include "driver/dex_compilation_unit.h" 51#include "elf_writer_quick.h" 52#include "graph_checker.h" 53#include "graph_visualizer.h" 54#include "gvn.h" 55#include "induction_var_analysis.h" 56#include "inliner.h" 57#include "instruction_simplifier.h" 58#include "intrinsics.h" 59#include "licm.h" 60#include "jni/quick/jni_compiler.h" 61#include "nodes.h" 62#include "prepare_for_register_allocation.h" 63#include "reference_type_propagation.h" 64#include "register_allocator.h" 65#include "side_effects_analysis.h" 66#include "ssa_builder.h" 67#include "ssa_phi_elimination.h" 68#include "ssa_liveness_analysis.h" 69#include "utils/assembler.h" 70#include "verifier/method_verifier.h" 71 72namespace art { 73 74/** 75 * Used by the code generator, to allocate the code in a vector. 76 */ 77class CodeVectorAllocator FINAL : public CodeAllocator { 78 public: 79 explicit CodeVectorAllocator(ArenaAllocator* arena) 80 : memory_(arena->Adapter(kArenaAllocCodeBuffer)), 81 size_(0) {} 82 83 virtual uint8_t* Allocate(size_t size) { 84 size_ = size; 85 memory_.resize(size); 86 return &memory_[0]; 87 } 88 89 size_t GetSize() const { return size_; } 90 const ArenaVector<uint8_t>& GetMemory() const { return memory_; } 91 92 private: 93 ArenaVector<uint8_t> memory_; 94 size_t size_; 95 96 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 97}; 98 99/** 100 * Filter to apply to the visualizer. Methods whose name contain that filter will 101 * be dumped. 102 */ 103static constexpr const char kStringFilter[] = ""; 104 105class PassScope; 106 107class PassObserver : public ValueObject { 108 public: 109 PassObserver(HGraph* graph, 110 const char* method_name, 111 CodeGenerator* codegen, 112 std::ostream* visualizer_output, 113 CompilerDriver* compiler_driver) 114 : graph_(graph), 115 method_name_(method_name), 116 timing_logger_enabled_(compiler_driver->GetDumpPasses()), 117 timing_logger_(method_name, true, true), 118 disasm_info_(graph->GetArena()), 119 visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()), 120 visualizer_(visualizer_output, graph, *codegen), 121 graph_in_bad_state_(false) { 122 if (timing_logger_enabled_ || visualizer_enabled_) { 123 if (!IsVerboseMethod(compiler_driver, method_name)) { 124 timing_logger_enabled_ = visualizer_enabled_ = false; 125 } 126 if (visualizer_enabled_) { 127 visualizer_.PrintHeader(method_name_); 128 codegen->SetDisassemblyInformation(&disasm_info_); 129 } 130 } 131 } 132 133 ~PassObserver() { 134 if (timing_logger_enabled_) { 135 LOG(INFO) << "TIMINGS " << method_name_; 136 LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); 137 } 138 } 139 140 void DumpDisassembly() const { 141 if (visualizer_enabled_) { 142 visualizer_.DumpGraphWithDisassembly(); 143 } 144 } 145 146 void SetGraphInBadState() { graph_in_bad_state_ = true; } 147 148 private: 149 void StartPass(const char* pass_name) { 150 // Dump graph first, then start timer. 151 if (visualizer_enabled_) { 152 visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_); 153 } 154 if (timing_logger_enabled_) { 155 timing_logger_.StartTiming(pass_name); 156 } 157 } 158 159 void EndPass(const char* pass_name) { 160 // Pause timer first, then dump graph. 161 if (timing_logger_enabled_) { 162 timing_logger_.EndTiming(); 163 } 164 if (visualizer_enabled_) { 165 visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_); 166 } 167 168 // Validate the HGraph if running in debug mode. 169 if (kIsDebugBuild) { 170 if (!graph_in_bad_state_) { 171 if (graph_->IsInSsaForm()) { 172 SSAChecker checker(graph_->GetArena(), graph_); 173 checker.Run(); 174 if (!checker.IsValid()) { 175 LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker); 176 } 177 } else { 178 GraphChecker checker(graph_->GetArena(), graph_); 179 checker.Run(); 180 if (!checker.IsValid()) { 181 LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker); 182 } 183 } 184 } 185 } 186 } 187 188 static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) { 189 // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an 190 // empty kStringFilter matching all methods. 191 if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) { 192 return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name); 193 } 194 195 // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code 196 // warning when the string is empty. 197 constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1; 198 if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) { 199 return true; 200 } 201 202 return false; 203 } 204 205 HGraph* const graph_; 206 const char* method_name_; 207 208 bool timing_logger_enabled_; 209 TimingLogger timing_logger_; 210 211 DisassemblyInformation disasm_info_; 212 213 bool visualizer_enabled_; 214 HGraphVisualizer visualizer_; 215 216 // Flag to be set by the compiler if the pass failed and the graph is not 217 // expected to validate. 218 bool graph_in_bad_state_; 219 220 friend PassScope; 221 222 DISALLOW_COPY_AND_ASSIGN(PassObserver); 223}; 224 225class PassScope : public ValueObject { 226 public: 227 PassScope(const char *pass_name, PassObserver* pass_observer) 228 : pass_name_(pass_name), 229 pass_observer_(pass_observer) { 230 pass_observer_->StartPass(pass_name_); 231 } 232 233 ~PassScope() { 234 pass_observer_->EndPass(pass_name_); 235 } 236 237 private: 238 const char* const pass_name_; 239 PassObserver* const pass_observer_; 240}; 241 242class OptimizingCompiler FINAL : public Compiler { 243 public: 244 explicit OptimizingCompiler(CompilerDriver* driver); 245 ~OptimizingCompiler(); 246 247 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const 248 OVERRIDE; 249 250 CompiledMethod* Compile(const DexFile::CodeItem* code_item, 251 uint32_t access_flags, 252 InvokeType invoke_type, 253 uint16_t class_def_idx, 254 uint32_t method_idx, 255 jobject class_loader, 256 const DexFile& dex_file, 257 Handle<mirror::DexCache> dex_cache) const OVERRIDE; 258 259 CompiledMethod* TryCompile(const DexFile::CodeItem* code_item, 260 uint32_t access_flags, 261 InvokeType invoke_type, 262 uint16_t class_def_idx, 263 uint32_t method_idx, 264 jobject class_loader, 265 const DexFile& dex_file, 266 Handle<mirror::DexCache> dex_cache) const; 267 268 CompiledMethod* JniCompile(uint32_t access_flags, 269 uint32_t method_idx, 270 const DexFile& dex_file) const OVERRIDE { 271 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); 272 } 273 274 uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE 275 SHARED_REQUIRES(Locks::mutator_lock_) { 276 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( 277 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); 278 } 279 280 void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE; 281 282 void Init() OVERRIDE; 283 284 void UnInit() const OVERRIDE; 285 286 void MaybeRecordStat(MethodCompilationStat compilation_stat) const { 287 if (compilation_stats_.get() != nullptr) { 288 compilation_stats_->RecordStat(compilation_stat); 289 } 290 } 291 292 private: 293 // Whether we should run any optimization or register allocation. If false, will 294 // just run the code generation after the graph was built. 295 const bool run_optimizations_; 296 297 // Optimize and compile `graph`. 298 CompiledMethod* CompileOptimized(HGraph* graph, 299 CodeGenerator* codegen, 300 CompilerDriver* driver, 301 const DexCompilationUnit& dex_compilation_unit, 302 PassObserver* pass_observer) const; 303 304 // Just compile without doing optimizations. 305 CompiledMethod* CompileBaseline(CodeGenerator* codegen, 306 CompilerDriver* driver, 307 const DexCompilationUnit& dex_compilation_unit, 308 PassObserver* pass_observer) const; 309 310 std::unique_ptr<OptimizingCompilerStats> compilation_stats_; 311 312 std::unique_ptr<std::ostream> visualizer_output_; 313 314 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 315}; 316 317static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 318 319OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 320 : Compiler(driver, kMaximumCompilationTimeBeforeWarning), 321 run_optimizations_( 322 driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime) {} 323 324void OptimizingCompiler::Init() { 325 // Enable C1visualizer output. Must be done in Init() because the compiler 326 // driver is not fully initialized when passed to the compiler's constructor. 327 CompilerDriver* driver = GetCompilerDriver(); 328 const std::string cfg_file_name = driver->GetDumpCfgFileName(); 329 if (!cfg_file_name.empty()) { 330 CHECK_EQ(driver->GetThreadCount(), 1U) 331 << "Graph visualizer requires the compiler to run single-threaded. " 332 << "Invoke the compiler with '-j1'."; 333 std::ios_base::openmode cfg_file_mode = 334 driver->GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; 335 visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); 336 } 337 if (driver->GetDumpStats()) { 338 compilation_stats_.reset(new OptimizingCompilerStats()); 339 } 340} 341 342void OptimizingCompiler::UnInit() const { 343} 344 345OptimizingCompiler::~OptimizingCompiler() { 346 if (compilation_stats_.get() != nullptr) { 347 compilation_stats_->Log(); 348 } 349} 350 351void OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const { 352} 353 354bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, 355 const DexFile& dex_file ATTRIBUTE_UNUSED, 356 CompilationUnit* cu ATTRIBUTE_UNUSED) const { 357 return true; 358} 359 360static bool IsInstructionSetSupported(InstructionSet instruction_set) { 361 return instruction_set == kArm64 362 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 363 || instruction_set == kMips64 364 || instruction_set == kX86 365 || instruction_set == kX86_64; 366} 367 368static void RunOptimizations(HOptimization* optimizations[], 369 size_t length, 370 PassObserver* pass_observer) { 371 for (size_t i = 0; i < length; ++i) { 372 PassScope scope(optimizations[i]->GetPassName(), pass_observer); 373 optimizations[i]->Run(); 374 } 375} 376 377static void MaybeRunInliner(HGraph* graph, 378 CompilerDriver* driver, 379 OptimizingCompilerStats* stats, 380 const DexCompilationUnit& dex_compilation_unit, 381 PassObserver* pass_observer, 382 StackHandleScopeCollection* handles) { 383 const CompilerOptions& compiler_options = driver->GetCompilerOptions(); 384 bool should_inline = (compiler_options.GetInlineDepthLimit() > 0) 385 && (compiler_options.GetInlineMaxCodeUnits() > 0); 386 if (!should_inline) { 387 return; 388 } 389 390 ArenaAllocator* arena = graph->GetArena(); 391 HInliner* inliner = new (arena) HInliner( 392 graph, dex_compilation_unit, dex_compilation_unit, driver, handles, stats); 393 ReferenceTypePropagation* type_propagation = 394 new (arena) ReferenceTypePropagation(graph, handles, 395 "reference_type_propagation_after_inlining"); 396 397 HOptimization* optimizations[] = { 398 inliner, 399 // Run another type propagation phase: inlining will open up more opportunities 400 // to remove checkcast/instanceof and null checks. 401 type_propagation, 402 }; 403 404 RunOptimizations(optimizations, arraysize(optimizations), pass_observer); 405} 406 407static void RunArchOptimizations(InstructionSet instruction_set, 408 HGraph* graph, 409 OptimizingCompilerStats* stats, 410 PassObserver* pass_observer) { 411 ArenaAllocator* arena = graph->GetArena(); 412 switch (instruction_set) { 413#ifdef ART_ENABLE_CODEGEN_arm64 414 case kArm64: { 415 arm64::InstructionSimplifierArm64* simplifier = 416 new (arena) arm64::InstructionSimplifierArm64(graph, stats); 417 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 418 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch"); 419 HOptimization* arm64_optimizations[] = { 420 simplifier, 421 side_effects, 422 gvn 423 }; 424 RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer); 425 break; 426 } 427#endif 428#ifdef ART_ENABLE_CODEGEN_x86 429 case kX86: { 430 x86::ConstantAreaFixups* constant_area_fixups = 431 new (arena) x86::ConstantAreaFixups(graph, stats); 432 HOptimization* x86_optimizations[] = { 433 constant_area_fixups 434 }; 435 RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer); 436 break; 437 } 438#endif 439 default: 440 break; 441 } 442} 443 444static void RunOptimizations(HGraph* graph, 445 CompilerDriver* driver, 446 OptimizingCompilerStats* stats, 447 const DexCompilationUnit& dex_compilation_unit, 448 PassObserver* pass_observer, 449 StackHandleScopeCollection* handles) { 450 ArenaAllocator* arena = graph->GetArena(); 451 HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination( 452 graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName); 453 HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination( 454 graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName); 455 HConstantFolding* fold1 = new (arena) HConstantFolding(graph); 456 InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats); 457 HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph); 458 HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining"); 459 SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 460 GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects); 461 LICM* licm = new (arena) LICM(graph, *side_effects); 462 HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph); 463 BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, induction); 464 ReferenceTypePropagation* type_propagation = 465 new (arena) ReferenceTypePropagation(graph, handles); 466 InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier( 467 graph, stats, "instruction_simplifier_after_types"); 468 InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier( 469 graph, stats, "instruction_simplifier_after_bce"); 470 InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier( 471 graph, stats, "instruction_simplifier_before_codegen"); 472 473 IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver); 474 475 HOptimization* optimizations1[] = { 476 intrinsics, 477 fold1, 478 simplify1, 479 type_propagation, 480 dce1, 481 simplify2 482 }; 483 484 RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer); 485 486 // TODO: Update passes incompatible with try/catch so we have the same 487 // pipeline for all methods. 488 if (graph->HasTryCatch()) { 489 HOptimization* optimizations2[] = { 490 side_effects, 491 gvn, 492 dce2, 493 // The codegen has a few assumptions that only the instruction simplifier 494 // can satisfy. For example, the code generator does not expect to see a 495 // HTypeConversion from a type to the same type. 496 simplify4, 497 }; 498 499 RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); 500 } else { 501 MaybeRunInliner(graph, driver, stats, dex_compilation_unit, pass_observer, handles); 502 503 HOptimization* optimizations2[] = { 504 // BooleanSimplifier depends on the InstructionSimplifier removing 505 // redundant suspend checks to recognize empty blocks. 506 boolean_simplify, 507 fold2, // TODO: if we don't inline we can also skip fold2. 508 side_effects, 509 gvn, 510 licm, 511 induction, 512 bce, 513 simplify3, 514 dce2, 515 // The codegen has a few assumptions that only the instruction simplifier 516 // can satisfy. For example, the code generator does not expect to see a 517 // HTypeConversion from a type to the same type. 518 simplify4, 519 }; 520 521 RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); 522 } 523 524 RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer); 525} 526 527// The stack map we generate must be 4-byte aligned on ARM. Since existing 528// maps are generated alongside these stack maps, we must also align them. 529static ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) { 530 size_t size = vector.size(); 531 size_t aligned_size = RoundUp(size, 4); 532 for (; size < aligned_size; ++size) { 533 vector.push_back(0); 534 } 535 return ArrayRef<const uint8_t>(vector); 536} 537 538NO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects. 539static void AllocateRegisters(HGraph* graph, 540 CodeGenerator* codegen, 541 PassObserver* pass_observer) { 542 PrepareForRegisterAllocation(graph).Run(); 543 SsaLivenessAnalysis liveness(graph, codegen); 544 { 545 PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); 546 liveness.Analyze(); 547 } 548 { 549 PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); 550 RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); 551 } 552} 553 554static ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { 555 ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter()); 556 codegen->EmitLinkerPatches(&linker_patches); 557 558 // Sort patches by literal offset. Required for .oat_patches encoding. 559 std::sort(linker_patches.begin(), linker_patches.end(), 560 [](const LinkerPatch& lhs, const LinkerPatch& rhs) { 561 return lhs.LiteralOffset() < rhs.LiteralOffset(); 562 }); 563 564 return linker_patches; 565} 566 567CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph, 568 CodeGenerator* codegen, 569 CompilerDriver* compiler_driver, 570 const DexCompilationUnit& dex_compilation_unit, 571 PassObserver* pass_observer) const { 572 ScopedObjectAccess soa(Thread::Current()); 573 StackHandleScopeCollection handles(soa.Self()); 574 soa.Self()->TransitionFromRunnableToSuspended(kNative); 575 RunOptimizations(graph, compiler_driver, compilation_stats_.get(), 576 dex_compilation_unit, pass_observer, &handles); 577 578 AllocateRegisters(graph, codegen, pass_observer); 579 580 ArenaAllocator* arena = graph->GetArena(); 581 CodeVectorAllocator allocator(arena); 582 DefaultSrcMap src_mapping_table; 583 codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo() 584 ? &src_mapping_table 585 : nullptr); 586 codegen->CompileOptimized(&allocator); 587 588 ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 589 590 ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps)); 591 codegen->BuildStackMaps(&stack_map); 592 593 MaybeRecordStat(MethodCompilationStat::kCompiledOptimized); 594 595 CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 596 compiler_driver, 597 codegen->GetInstructionSet(), 598 ArrayRef<const uint8_t>(allocator.GetMemory()), 599 // Follow Quick's behavior and set the frame size to zero if it is 600 // considered "empty" (see the definition of 601 // art::CodeGenerator::HasEmptyFrame). 602 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 603 codegen->GetCoreSpillMask(), 604 codegen->GetFpuSpillMask(), 605 &src_mapping_table, 606 ArrayRef<const uint8_t>(), // mapping_table. 607 ArrayRef<const uint8_t>(stack_map), 608 ArrayRef<const uint8_t>(), // native_gc_map. 609 ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 610 ArrayRef<const LinkerPatch>(linker_patches)); 611 pass_observer->DumpDisassembly(); 612 613 soa.Self()->TransitionFromSuspendedToRunnable(); 614 return compiled_method; 615} 616 617CompiledMethod* OptimizingCompiler::CompileBaseline( 618 CodeGenerator* codegen, 619 CompilerDriver* compiler_driver, 620 const DexCompilationUnit& dex_compilation_unit, 621 PassObserver* pass_observer) const { 622 ArenaAllocator* arena = codegen->GetGraph()->GetArena(); 623 CodeVectorAllocator allocator(arena); 624 DefaultSrcMap src_mapping_table; 625 codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo() 626 ? &src_mapping_table 627 : nullptr); 628 codegen->CompileBaseline(&allocator); 629 630 ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 631 632 ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps)); 633 codegen->BuildMappingTable(&mapping_table); 634 ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps)); 635 codegen->BuildVMapTable(&vmap_table); 636 ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps)); 637 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); 638 639 MaybeRecordStat(MethodCompilationStat::kCompiledBaseline); 640 CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 641 compiler_driver, 642 codegen->GetInstructionSet(), 643 ArrayRef<const uint8_t>(allocator.GetMemory()), 644 // Follow Quick's behavior and set the frame size to zero if it is 645 // considered "empty" (see the definition of 646 // art::CodeGenerator::HasEmptyFrame). 647 codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 648 codegen->GetCoreSpillMask(), 649 codegen->GetFpuSpillMask(), 650 &src_mapping_table, 651 AlignVectorSize(mapping_table), 652 AlignVectorSize(vmap_table), 653 AlignVectorSize(gc_map), 654 ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 655 ArrayRef<const LinkerPatch>(linker_patches)); 656 pass_observer->DumpDisassembly(); 657 return compiled_method; 658} 659 660CompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item, 661 uint32_t access_flags, 662 InvokeType invoke_type, 663 uint16_t class_def_idx, 664 uint32_t method_idx, 665 jobject class_loader, 666 const DexFile& dex_file, 667 Handle<mirror::DexCache> dex_cache) const { 668 UNUSED(invoke_type); 669 std::string method_name = PrettyMethod(method_idx, dex_file); 670 MaybeRecordStat(MethodCompilationStat::kAttemptCompilation); 671 CompilerDriver* compiler_driver = GetCompilerDriver(); 672 InstructionSet instruction_set = compiler_driver->GetInstructionSet(); 673 // Always use the thumb2 assembler: some runtime functionality (like implicit stack 674 // overflow checks) assume thumb2. 675 if (instruction_set == kArm) { 676 instruction_set = kThumb2; 677 } 678 679 // Do not attempt to compile on architectures we do not support. 680 if (!IsInstructionSetSupported(instruction_set)) { 681 MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); 682 return nullptr; 683 } 684 685 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 686 MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological); 687 return nullptr; 688 } 689 690 // Implementation of the space filter: do not compile a code item whose size in 691 // code units is bigger than 128. 692 static constexpr size_t kSpaceFilterOptimizingThreshold = 128; 693 const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); 694 if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace) 695 && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { 696 MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter); 697 return nullptr; 698 } 699 700 DexCompilationUnit dex_compilation_unit( 701 nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item, 702 class_def_idx, method_idx, access_flags, 703 compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache); 704 705 bool requires_barrier = dex_compilation_unit.IsConstructor() 706 && compiler_driver->RequiresConstructorBarrier(Thread::Current(), 707 dex_compilation_unit.GetDexFile(), 708 dex_compilation_unit.GetClassDefIndex()); 709 ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 710 HGraph* graph = new (&arena) HGraph( 711 &arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(), 712 kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable()); 713 714 // For testing purposes, we put a special marker on method names that should be compiled 715 // with this compiler. This makes sure we're not regressing. 716 bool shouldCompile = method_name.find("$opt$") != std::string::npos; 717 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_; 718 719 std::unique_ptr<CodeGenerator> codegen( 720 CodeGenerator::Create(graph, 721 instruction_set, 722 *compiler_driver->GetInstructionSetFeatures(), 723 compiler_driver->GetCompilerOptions())); 724 if (codegen.get() == nullptr) { 725 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler"; 726 MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen); 727 return nullptr; 728 } 729 codegen->GetAssembler()->cfi().SetEnabled( 730 compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()); 731 732 PassObserver pass_observer(graph, 733 method_name.c_str(), 734 codegen.get(), 735 visualizer_output_.get(), 736 compiler_driver); 737 738 const uint8_t* interpreter_metadata = nullptr; 739 { 740 ScopedObjectAccess soa(Thread::Current()); 741 StackHandleScope<1> hs(soa.Self()); 742 Handle<mirror::ClassLoader> loader(hs.NewHandle( 743 soa.Decode<mirror::ClassLoader*>(class_loader))); 744 ArtMethod* art_method = compiler_driver->ResolveMethod( 745 soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type); 746 // We may not get a method, for example if its class is erroneous. 747 // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile. 748 if (art_method != nullptr) { 749 interpreter_metadata = art_method->GetQuickenedInfo(); 750 } 751 } 752 HGraphBuilder builder(graph, 753 &dex_compilation_unit, 754 &dex_compilation_unit, 755 &dex_file, 756 compiler_driver, 757 compilation_stats_.get(), 758 interpreter_metadata, 759 dex_cache); 760 761 VLOG(compiler) << "Building " << method_name; 762 763 { 764 PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); 765 if (!builder.BuildGraph(*code_item)) { 766 DCHECK(!(IsCompilingWithCoreImage() && shouldCompile)) 767 << "Could not build graph in optimizing compiler"; 768 pass_observer.SetGraphInBadState(); 769 return nullptr; 770 } 771 } 772 773 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set); 774 775 // `run_optimizations_` is set explicitly (either through a compiler filter 776 // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back 777 // to Quick. 778 bool can_use_baseline = !run_optimizations_ && builder.CanUseBaselineForStringInit(); 779 CompiledMethod* compiled_method = nullptr; 780 if (run_optimizations_ && can_allocate_registers) { 781 VLOG(compiler) << "Optimizing " << method_name; 782 783 { 784 PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer); 785 if (!graph->TryBuildingSsa()) { 786 // We could not transform the graph to SSA, bailout. 787 LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop"; 788 MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA); 789 pass_observer.SetGraphInBadState(); 790 return nullptr; 791 } 792 } 793 794 compiled_method = CompileOptimized(graph, 795 codegen.get(), 796 compiler_driver, 797 dex_compilation_unit, 798 &pass_observer); 799 } else if (shouldOptimize && can_allocate_registers) { 800 LOG(FATAL) << "Could not allocate registers in optimizing compiler"; 801 UNREACHABLE(); 802 } else if (can_use_baseline) { 803 VLOG(compiler) << "Compile baseline " << method_name; 804 805 if (!run_optimizations_) { 806 MaybeRecordStat(MethodCompilationStat::kNotOptimizedDisabled); 807 } else if (!can_allocate_registers) { 808 MaybeRecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator); 809 } 810 811 compiled_method = CompileBaseline(codegen.get(), 812 compiler_driver, 813 dex_compilation_unit, 814 &pass_observer); 815 } 816 817 if (kArenaAllocatorCountAllocations) { 818 if (arena.BytesAllocated() > 4 * MB) { 819 MemStats mem_stats(arena.GetMemStats()); 820 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats); 821 } 822 } 823 824 return compiled_method; 825} 826 827static bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) { 828 // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved). 829 uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS 830 | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS 831 | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD 832 | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD; 833 return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0; 834} 835 836CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 837 uint32_t access_flags, 838 InvokeType invoke_type, 839 uint16_t class_def_idx, 840 uint32_t method_idx, 841 jobject jclass_loader, 842 const DexFile& dex_file, 843 Handle<mirror::DexCache> dex_cache) const { 844 CompilerDriver* compiler_driver = GetCompilerDriver(); 845 CompiledMethod* method = nullptr; 846 const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); 847 DCHECK(!verified_method->HasRuntimeThrow()); 848 if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) 849 || CanHandleVerificationFailure(verified_method)) { 850 method = TryCompile(code_item, access_flags, invoke_type, class_def_idx, 851 method_idx, jclass_loader, dex_file, dex_cache); 852 } else { 853 if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { 854 MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime); 855 } else { 856 MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified); 857 } 858 } 859 860 return method; 861} 862 863Compiler* CreateOptimizingCompiler(CompilerDriver* driver) { 864 return new OptimizingCompiler(driver); 865} 866 867bool IsCompilingWithCoreImage() { 868 const std::string& image = Runtime::Current()->GetImageLocation(); 869 return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art"); 870} 871 872} // namespace art 873