optimizing_compiler.cc revision 8a7c0fe837bb00b02dfcfc678910d81d07fb2136
1b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray/* 2b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project 3b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 4b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License"); 5b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * you may not use this file except in compliance with the License. 6b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * You may obtain a copy of the License at 7b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 8b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * http://www.apache.org/licenses/LICENSE-2.0 9b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 10b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software 11b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS, 12b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * See the License for the specific language governing permissions and 14b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * limitations under the License. 15b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray */ 16b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 1753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "optimizing_compiler.h" 1853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 19f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include <fstream> 20787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include <stdint.h> 21787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 2244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64 2344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#include "instruction_simplifier_arm64.h" 2444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif 2544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames 260616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86 270616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#include "constant_area_fixups_x86.h" 280616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif 290616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell 30e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h" 31b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h" 32f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko#include "base/arena_containers.h" 335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h" 34c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir Marko#include "base/macros.h" 355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h" 3646e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h" 37f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h" 38787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h" 39787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h" 4020f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h" 4153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h" 4275be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h" 4375be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h" 4471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h" 45f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h" 46f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h" 47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h" 489523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h" 4920f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h" 5092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h" 51e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h" 5269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h" 53f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h" 54d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h" 5522af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h" 56e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h" 573c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h" 5871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h" 59d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray#include "jit/jit_code_cache.h" 6082091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h" 61e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h" 628df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang#include "load_store_elimination.h" 63787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h" 6426a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h" 65f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h" 66a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h" 67dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko#include "sharpening.h" 68827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h" 693159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h" 707dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h" 71804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h" 72c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h" 73175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h" 74b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 75b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 76b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 77787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 78787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 79787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 80787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 81787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 82f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko explicit CodeVectorAllocator(ArenaAllocator* arena) 83f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko : memory_(arena->Adapter(kArenaAllocCodeBuffer)), 84f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko size_(0) {} 85787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 87787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 8892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 92787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 93f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko const ArenaVector<uint8_t>& GetMemory() const { return memory_; } 94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 96f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> memory_; 97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 100787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 101787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 102f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/** 103f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will 104ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped. 105f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */ 10653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = ""; 107f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 10869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope; 109809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 11069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject { 1115e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public: 11269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver(HGraph* graph, 11369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil const char* method_name, 11469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil CodeGenerator* codegen, 11569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil std::ostream* visualizer_output, 11669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil CompilerDriver* compiler_driver) 11769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil : graph_(graph), 11869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil method_name_(method_name), 119809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil timing_logger_enabled_(compiler_driver->GetDumpPasses()), 1205e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_(method_name, true, true), 121eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames disasm_info_(graph->GetArena()), 122809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()), 12369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil visualizer_(visualizer_output, graph, *codegen), 12469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil graph_in_bad_state_(false) { 12553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (timing_logger_enabled_ || visualizer_enabled_) { 12653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (!IsVerboseMethod(compiler_driver, method_name)) { 12753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe timing_logger_enabled_ = visualizer_enabled_ = false; 12853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 12953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (visualizer_enabled_) { 13053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe visualizer_.PrintHeader(method_name_); 13153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe codegen->SetDisassemblyInformation(&disasm_info_); 13253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 13362e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil } 1345e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 13669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil ~PassObserver() { 137809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (timing_logger_enabled_) { 138809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << "TIMINGS " << method_name_; 139809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); 140809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 141809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 142809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 143eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames void DumpDisassembly() const { 144eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames if (visualizer_enabled_) { 145eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames visualizer_.DumpGraphWithDisassembly(); 146eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames } 147eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames } 148eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 14969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil void SetGraphInBadState() { graph_in_bad_state_ = true; } 15069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 151809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 152809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void StartPass(const char* pass_name) { 1535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Dump graph first, then start timer. 1545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 155ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_); 1565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.StartTiming(pass_name); 1595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 162809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void EndPass(const char* pass_name) { 1635e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Pause timer first, then dump graph. 1645e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.EndTiming(); 1665e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1675e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 168ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_); 1695e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 17069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 17169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // Validate the HGraph if running in debug mode. 17269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (kIsDebugBuild) { 17369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (!graph_in_bad_state_) { 17469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (graph_->IsInSsaForm()) { 175655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko SSAChecker checker(graph_); 17669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil checker.Run(); 17769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (!checker.IsValid()) { 17869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker); 17969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 18069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } else { 181655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko GraphChecker checker(graph_); 18269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil checker.Run(); 18369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (!checker.IsValid()) { 18469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker); 18569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 18669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 18769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 18869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 1895e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1905e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 19153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) { 19253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an 19353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // empty kStringFilter matching all methods. 19453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) { 19553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name); 19653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 19753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 19853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code 19953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // warning when the string is empty. 20053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1; 20153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) { 20253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return true; 20353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 20453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 20553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return false; 20653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 20753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 20869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil HGraph* const graph_; 2095e8b137d28c840b128e2488f954cccee3e86db14David Brazdil const char* method_name_; 2105e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 2115e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool timing_logger_enabled_; 2125e8b137d28c840b128e2488f954cccee3e86db14David Brazdil TimingLogger timing_logger_; 2135e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 214eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames DisassemblyInformation disasm_info_; 215eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 2165e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool visualizer_enabled_; 2175e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphVisualizer visualizer_; 2185e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 21969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // Flag to be set by the compiler if the pass failed and the graph is not 22069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // expected to validate. 22169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil bool graph_in_bad_state_; 222809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 22369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil friend PassScope; 22469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 22569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil DISALLOW_COPY_AND_ASSIGN(PassObserver); 2265e8b137d28c840b128e2488f954cccee3e86db14David Brazdil}; 2275e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 22869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject { 229809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public: 23069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope(const char *pass_name, PassObserver* pass_observer) 231809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil : pass_name_(pass_name), 23269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_(pass_observer) { 23369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_->StartPass(pass_name_); 234809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 235809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 23669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil ~PassScope() { 23769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_->EndPass(pass_name_); 238809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 239809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 240809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 241809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil const char* const pass_name_; 24269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver* const pass_observer_; 243809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil}; 244809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 24553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler { 24653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public: 24753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe explicit OptimizingCompiler(CompilerDriver* driver); 24888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray ~OptimizingCompiler(); 24953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 25053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const 25153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OVERRIDE; 25253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 25353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* Compile(const DexFile::CodeItem* code_item, 25453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 25553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 25653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 25753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 25853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 259736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier const DexFile& dex_file, 260736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier Handle<mirror::DexCache> dex_cache) const OVERRIDE; 26153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 26253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* JniCompile(uint32_t access_flags, 26353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 264216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray const DexFile& dex_file) const OVERRIDE { 265216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); 266216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 268e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE 26990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier SHARED_REQUIRES(Locks::mutator_lock_) { 270216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( 271216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); 272216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 27353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 274216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE; 27553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 276ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil void Init() OVERRIDE; 27753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 278216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray void UnInit() const OVERRIDE; 27953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 2802be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle void MaybeRecordStat(MethodCompilationStat compilation_stat) const { 2812be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (compilation_stats_.get() != nullptr) { 2822be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_->RecordStat(compilation_stat); 2832be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 2842be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 2852be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle 286d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method) 287d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray OVERRIDE 288d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray SHARED_REQUIRES(Locks::mutator_lock_); 289d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 29053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private: 29188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // Whether we should run any optimization or register allocation. If false, will 29288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // just run the code generation after the graph was built. 29388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray const bool run_optimizations_; 29448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 295d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Create a 'CompiledMethod' for an optimized graph. 296d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompiledMethod* EmitOptimized(ArenaAllocator* arena, 297d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 298d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 299d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* driver) const; 300d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 301d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Create a 'CompiledMethod' for a non-optimized graph. 302d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompiledMethod* EmitBaseline(ArenaAllocator* arena, 303d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 304d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 305d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* driver) const; 306d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 307d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Try compiling a method and return the code generator used for 308d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // compiling it. 309d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // This method: 310d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 1) Builds the graph. Returns null if it failed to build it. 311d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 2) If `run_optimizations_` is set: 312d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 2.1) Transform the graph to SSA. Returns null if it failed. 313d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 2.2) Run optimizations on the graph, including register allocator. 314d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 3) Generate code with the `code_allocator` provided. 315d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* TryCompile(ArenaAllocator* arena, 316d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 317d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 318d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 319d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 320d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 321d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 322d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 323d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 324d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache) const; 32512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 3262be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle std::unique_ptr<OptimizingCompilerStats> compilation_stats_; 32788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 32853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<std::ostream> visualizer_output_; 32953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 33053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 33153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}; 33253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 33388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 33488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 33588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 33688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray : Compiler(driver, kMaximumCompilationTimeBeforeWarning), 33788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray run_optimizations_( 338da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffray driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime) {} 339ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil 340ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() { 341ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // Enable C1visualizer output. Must be done in Init() because the compiler 342ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // driver is not fully initialized when passed to the compiler's constructor. 343ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CompilerDriver* driver = GetCompilerDriver(); 344866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil const std::string cfg_file_name = driver->GetDumpCfgFileName(); 345866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil if (!cfg_file_name.empty()) { 346ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CHECK_EQ(driver->GetThreadCount(), 1U) 347ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Graph visualizer requires the compiler to run single-threaded. " 348ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Invoke the compiler with '-j1'."; 34987000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle std::ios_base::openmode cfg_file_mode = 35087000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle driver->GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; 35187000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); 352f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray } 3532be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (driver->GetDumpStats()) { 3542be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_.reset(new OptimizingCompilerStats()); 3552be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 356f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray} 357787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 358216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const { 359216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 360216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 36188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() { 3622be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (compilation_stats_.get() != nullptr) { 3632be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_->Log(); 3642be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 36588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray} 36688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 367da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const { 368216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 369216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 370e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, 371e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file ATTRIBUTE_UNUSED, 372e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray CompilationUnit* cu ATTRIBUTE_UNUSED) const { 373e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return true; 37453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 37553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 3761ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) { 377cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat) 378cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle || instruction_set == kArm64 3791ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 380f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic || instruction_set == kMips 3814dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze || instruction_set == kMips64 3821ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86 3831ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86_64; 3841ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray} 3851ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray 38610e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[], 38710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle size_t length, 38869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver* pass_observer) { 38910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle for (size_t i = 0; i < length; ++i) { 39069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(optimizations[i]->GetPassName(), pass_observer); 39169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil optimizations[i]->Run(); 39210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle } 39310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle} 39410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle 395ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph, 396dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 397ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle CompilerDriver* driver, 398ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle OptimizingCompilerStats* stats, 399ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const DexCompilationUnit& dex_compilation_unit, 400ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle PassObserver* pass_observer, 401ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle StackHandleScopeCollection* handles) { 402ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const CompilerOptions& compiler_options = driver->GetCompilerOptions(); 403ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle bool should_inline = (compiler_options.GetInlineDepthLimit() > 0) 404ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle && (compiler_options.GetInlineMaxCodeUnits() > 0); 405ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle if (!should_inline) { 406ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle return; 407ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle } 408ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 409271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle ArenaAllocator* arena = graph->GetArena(); 410271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle HInliner* inliner = new (arena) HInliner( 411dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko graph, codegen, dex_compilation_unit, dex_compilation_unit, driver, handles, stats); 412271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle ReferenceTypePropagation* type_propagation = 413271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle new (arena) ReferenceTypePropagation(graph, handles, 414271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle "reference_type_propagation_after_inlining"); 415271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle 416271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle HOptimization* optimizations[] = { 417271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle inliner, 418271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle // Run another type propagation phase: inlining will open up more opportunities 419271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle // to remove checkcast/instanceof and null checks. 420271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle type_propagation, 421271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle }; 422ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 423ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations, arraysize(optimizations), pass_observer); 424ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle} 425ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 42644b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set, 42744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HGraph* graph, 42844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames OptimizingCompilerStats* stats, 42944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames PassObserver* pass_observer) { 43044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames ArenaAllocator* arena = graph->GetArena(); 43144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames switch (instruction_set) { 43244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64 43344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames case kArm64: { 43444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames arm64::InstructionSimplifierArm64* simplifier = 43544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames new (arena) arm64::InstructionSimplifierArm64(graph, stats); 43644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 43744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch"); 43844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HOptimization* arm64_optimizations[] = { 43944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames simplifier, 44044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames side_effects, 44144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames gvn 44244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames }; 44344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer); 44444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames break; 44544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames } 44644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif 4470616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86 4480616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell case kX86: { 4490616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell x86::ConstantAreaFixups* constant_area_fixups = 4500616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell new (arena) x86::ConstantAreaFixups(graph, stats); 4510616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell HOptimization* x86_optimizations[] = { 4520616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell constant_area_fixups 4530616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell }; 4540616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer); 4550616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell break; 4560616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell } 4570616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif 45844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames default: 45944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames break; 46044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames } 46144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames} 46244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames 463d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayNO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects. 464d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraystatic void AllocateRegisters(HGraph* graph, 465d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 466d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassObserver* pass_observer) { 467d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PrepareForRegisterAllocation(graph).Run(); 468d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray SsaLivenessAnalysis liveness(graph, codegen); 469d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 470d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); 471d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray liveness.Analyze(); 472d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 473d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 474d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); 475d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); 476d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 477d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 478d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 479e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph, 480dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 481e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray CompilerDriver* driver, 482e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray OptimizingCompilerStats* stats, 483e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 484d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassObserver* pass_observer) { 485d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ScopedObjectAccess soa(Thread::Current()); 486d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray StackHandleScopeCollection handles(soa.Self()); 487d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ScopedThreadSuspension sts(soa.Self(), kNative); 488d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 489a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko ArenaAllocator* arena = graph->GetArena(); 490a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination( 491a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName); 492a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination( 493a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName); 494a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HConstantFolding* fold1 = new (arena) HConstantFolding(graph); 495a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats); 496a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph); 497a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining"); 498a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 499a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects); 500a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko LICM* licm = new (arena) LICM(graph, *side_effects); 5018df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects); 50222af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph); 50322af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, induction); 504a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko ReferenceTypePropagation* type_propagation = 505d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray new (arena) ReferenceTypePropagation(graph, &handles); 506dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver); 507a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier( 508a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, "instruction_simplifier_after_types"); 509a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier( 510b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray graph, stats, "instruction_simplifier_after_bce"); 511b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier( 512b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray graph, stats, "instruction_simplifier_before_codegen"); 513a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko 514a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver); 51571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe 516ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle HOptimization* optimizations1[] = { 517a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko intrinsics, 518a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko fold1, 519a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko simplify1, 520a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko type_propagation, 521dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko sharpening, 52218e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray dce1, 523ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle simplify2 524ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle }; 525ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 526ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer); 527ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 528d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, &handles); 529951779839f0d35ed5336f399c8f521fd9a6b7c27David Brazdil 53077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // TODO: Update passes incompatible with try/catch so we have the same 53177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil // pipeline for all methods. 532771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil if (graph->HasTryCatch()) { 533771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil HOptimization* optimizations2[] = { 5348a7c0fe837bb00b02dfcfc678910d81d07fb2136David Brazdil boolean_simplify, 535771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil side_effects, 536771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil gvn, 537771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil dce2, 538771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // The codegen has a few assumptions that only the instruction simplifier 539771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // can satisfy. For example, the code generator does not expect to see a 540771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // HTypeConversion from a type to the same type. 541771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil simplify4, 542771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil }; 543771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil 544771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); 545771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil } else { 546771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil HOptimization* optimizations2[] = { 547771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // BooleanSimplifier depends on the InstructionSimplifier removing 548771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // redundant suspend checks to recognize empty blocks. 549771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil boolean_simplify, 550771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil fold2, // TODO: if we don't inline we can also skip fold2. 551771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil side_effects, 552771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil gvn, 553771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil licm, 554771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil induction, 555771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil bce, 556771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil simplify3, 557771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil lse, 558771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil dce2, 559771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // The codegen has a few assumptions that only the instruction simplifier 560771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // can satisfy. For example, the code generator does not expect to see a 561771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil // HTypeConversion from a type to the same type. 562771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil simplify4, 563771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil }; 564771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil 565771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); 566771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil } 56739fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil 56844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer); 569d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray AllocateRegisters(graph, codegen, pass_observer); 5705e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray} 5715e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 572376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing 573376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them. 574f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Markostatic ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) { 575376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t size = vector.size(); 576376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t aligned_size = RoundUp(size, 4); 577376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray for (; size < aligned_size; ++size) { 578376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray vector.push_back(0); 579376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray } 580e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe return ArrayRef<const uint8_t>(vector); 581376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray} 582376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray 583581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { 584581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter()); 585581550137ee3a068a14224870e71aeee924a0646Vladimir Marko codegen->EmitLinkerPatches(&linker_patches); 586581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 587581550137ee3a068a14224870e71aeee924a0646Vladimir Marko // Sort patches by literal offset. Required for .oat_patches encoding. 588581550137ee3a068a14224870e71aeee924a0646Vladimir Marko std::sort(linker_patches.begin(), linker_patches.end(), 589581550137ee3a068a14224870e71aeee924a0646Vladimir Marko [](const LinkerPatch& lhs, const LinkerPatch& rhs) { 590581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return lhs.LiteralOffset() < rhs.LiteralOffset(); 591581550137ee3a068a14224870e71aeee924a0646Vladimir Marko }); 592581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 593581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return linker_patches; 594581550137ee3a068a14224870e71aeee924a0646Vladimir Marko} 595581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 596d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCompiledMethod* OptimizingCompiler::EmitOptimized(ArenaAllocator* arena, 597d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 598d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 599d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* compiler_driver) const { 600581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 601f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps)); 602d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map.resize(codegen->ComputeStackMapsSize()); 603d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size())); 60412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 6052be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kCompiledOptimized); 60612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 607eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 60812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray compiler_driver, 60912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetInstructionSet(), 610d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const uint8_t>(code_allocator->GetMemory()), 611aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 612aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 613aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 614aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 61512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetCoreSpillMask(), 616d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray codegen->GetFpuSpillMask(), 617d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const SrcMapElem>(codegen->GetSrcMappingTable()), 618c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // mapping_table. 619c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(stack_map), 620c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // native_gc_map. 621c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 622581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArrayRef<const LinkerPatch>(linker_patches)); 623ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier 624eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames return compiled_method; 62512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 62612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 627d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCompiledMethod* OptimizingCompiler::EmitBaseline( 628d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator* arena, 629d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 63012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CodeGenerator* codegen, 631d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* compiler_driver) const { 632581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 633581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 634f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps)); 635c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky codegen->BuildMappingTable(&mapping_table); 636f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps)); 63712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->BuildVMapTable(&vmap_table); 638f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps)); 639d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildNativeGCMap(&gc_map, *compiler_driver); 64012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 6412be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kCompiledBaseline); 642eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 643aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain compiler_driver, 644aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetInstructionSet(), 645d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const uint8_t>(code_allocator->GetMemory()), 646aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 647aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 648aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 649aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 650aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetCoreSpillMask(), 651aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetFpuSpillMask(), 652d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const SrcMapElem>(codegen->GetSrcMappingTable()), 653aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(mapping_table), 654aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(vmap_table), 655aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(gc_map), 656c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 657581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArrayRef<const LinkerPatch>(linker_patches)); 658eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames return compiled_method; 65912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 66012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 661d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena, 662d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 663d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 664d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 665d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 666d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 667d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 668d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 669d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 670d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache) const { 6715e8b137d28c840b128e2488f954cccee3e86db14David Brazdil std::string method_name = PrettyMethod(method_idx, dex_file); 6722be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kAttemptCompilation); 673cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 674cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle InstructionSet instruction_set = compiler_driver->GetInstructionSet(); 675cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle 6768d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // Always use the thumb2 assembler: some runtime functionality (like implicit stack 6778d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // overflow checks) assume thumb2. 6788d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray if (instruction_set == kArm) { 6798d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray instruction_set = kThumb2; 6808fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6818fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 6828fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray // Do not attempt to compile on architectures we do not support. 6831ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray if (!IsInstructionSetSupported(instruction_set)) { 6842be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); 6858fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray return nullptr; 6868fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6878fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 688b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 6892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological); 690b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray return nullptr; 691b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 692b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray 69336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray // Implementation of the space filter: do not compile a code item whose size in 694432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray // code units is bigger than 128. 695432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray static constexpr size_t kSpaceFilterOptimizingThreshold = 128; 69636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); 69736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace) 69836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { 6992be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter); 70036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray return nullptr; 70136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray } 70236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray 70392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 7049523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item, 70572d32629303f8f39362a4099481f48646aed042fIan Rogers class_def_idx, method_idx, access_flags, 706736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache); 70792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 7083cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle bool requires_barrier = dex_compilation_unit.IsConstructor() 7093cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle && compiler_driver->RequiresConstructorBarrier(Thread::Current(), 7103cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetDexFile(), 7113cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetClassDefIndex()); 712d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray HGraph* graph = new (arena) HGraph( 713d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(), 714e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable()); 715e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 71612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 717cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CodeGenerator::Create(graph, 718cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle instruction_set, 719cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle *compiler_driver->GetInstructionSetFeatures(), 720cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle compiler_driver->GetCompilerOptions())); 72112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray if (codegen.get() == nullptr) { 7222be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen); 723787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 724787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 725c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky codegen->GetAssembler()->cfi().SetEnabled( 7268363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()); 727787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 72869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver pass_observer(graph, 72969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil method_name.c_str(), 73069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil codegen.get(), 73169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil visualizer_output_.get(), 73269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil compiler_driver); 7335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 7349523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray const uint8_t* interpreter_metadata = nullptr; 7359523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray { 7369523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray ScopedObjectAccess soa(Thread::Current()); 737736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier StackHandleScope<1> hs(soa.Self()); 7389523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray Handle<mirror::ClassLoader> loader(hs.NewHandle( 7399523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray soa.Decode<mirror::ClassLoader*>(class_loader))); 7409523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray ArtMethod* art_method = compiler_driver->ResolveMethod( 7419523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type); 7429523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray // We may not get a method, for example if its class is erroneous. 7439523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile. 7449523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray if (art_method != nullptr) { 7459523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray interpreter_metadata = art_method->GetQuickenedInfo(); 7469523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray } 7479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray } 7485e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphBuilder builder(graph, 7495e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 7505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 7515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_file, 7525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil compiler_driver, 7539523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray compilation_stats_.get(), 754736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier interpreter_metadata, 755736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier dex_cache); 7565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 7575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil VLOG(compiler) << "Building " << method_name; 7585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 759809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 76069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); 761809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (!builder.BuildGraph(*code_item)) { 76269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer.SetGraphInBadState(); 763809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil return nullptr; 764809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 7655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 766a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 767d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray VLOG(compiler) << "Optimizing " << method_name; 768d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (run_optimizations_) { 769809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 77069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer); 771809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (!graph->TryBuildingSsa()) { 772809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil // We could not transform the graph to SSA, bailout. 773809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop"; 7742be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA); 775ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil pass_observer.SetGraphInBadState(); 776809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil return nullptr; 777809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 778f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray } 7795e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 780d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray RunOptimizations(graph, 781d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen.get(), 782d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray compiler_driver, 783d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray compilation_stats_.get(), 784d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_compilation_unit, 785d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &pass_observer); 786d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->CompileOptimized(code_allocator); 787d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } else { 788d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->CompileBaseline(code_allocator); 789216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 790d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray pass_observer.DumpDisassembly(); 791f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko 792f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko if (kArenaAllocatorCountAllocations) { 793d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (arena->BytesAllocated() > 4 * MB) { 794d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MemStats mem_stats(arena->GetMemStats()); 795f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats); 796f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 797f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 798f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko 799d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return codegen.release(); 800216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 801216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 80207380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) { 80307380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved). 80407380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS 80507380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS 80607380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD 80707380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD; 808175dc732c80e6f2afd83209348124df349290ba8Calin Juravle return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0; 809175dc732c80e6f2afd83209348124df349290ba8Calin Juravle} 810175dc732c80e6f2afd83209348124df349290ba8Calin Juravle 811216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 812216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t access_flags, 813216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray InvokeType invoke_type, 814216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint16_t class_def_idx, 815216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t method_idx, 816f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle jobject jclass_loader, 817736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier const DexFile& dex_file, 818736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier Handle<mirror::DexCache> dex_cache) const { 819f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 820f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompiledMethod* method = nullptr; 821d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(Runtime::Current()->IsAotCompiler()); 822d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); 823d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(!verified_method->HasRuntimeThrow()); 824d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) 825d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray || CanHandleVerificationFailure(verified_method)) { 826d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 827d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 828d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 829d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 830d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 831d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 832d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 833d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 834d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 835d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 836d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 837d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_file, 838d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_cache)); 839d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() != nullptr) { 840d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (run_optimizations_) { 841d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method = EmitOptimized(&arena, &code_allocator, codegen.get(), compiler_driver); 8420c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } else { 843d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method = EmitBaseline(&arena, &code_allocator, codegen.get(), compiler_driver); 8440c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } 845f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 8460c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } else { 847d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { 848d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime); 849d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } else { 850d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified); 851d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 852f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 853f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle 854cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle if (kIsDebugBuild && 855cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle IsCompilingWithCoreImage() && 856cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle IsInstructionSetSupported(compiler_driver->GetInstructionSet())) { 85709b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle // For testing purposes, we put a special marker on method names that should be compiled 85809b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle // with this compiler. This makes sure we're not regressing. 85909b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle std::string method_name = PrettyMethod(method_idx, dex_file); 86009b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle bool shouldCompile = method_name.find("$opt$") != std::string::npos; 86109b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name; 86209b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle } 86309b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle 86412be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray return method; 865b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 866b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 86753c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) { 86853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return new OptimizingCompiler(driver); 86953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 87053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 871335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() { 872335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray const std::string& image = Runtime::Current()->GetImageLocation(); 873335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art"); 874335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray} 875335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray 876d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraybool OptimizingCompiler::JitCompile(Thread* self, 877d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jit::JitCodeCache* code_cache, 878d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArtMethod* method) { 879d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray StackHandleScope<2> hs(self); 880d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 881d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method->GetDeclaringClass()->GetClassLoader())); 882d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); 883d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 884d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject jclass_loader = class_loader.ToJObject(); 885d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile* dex_file = method->GetDexFile(); 886d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint16_t class_def_idx = method->GetClassDefIndex(); 887d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); 888d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t method_idx = method->GetDexMethodIndex(); 889d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t access_flags = method->GetAccessFlags(); 890d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const InvokeType invoke_type = method->GetInvokeType(); 891d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 892d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 893d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 894d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen; 895d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 896d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Go to native so that we don't block GC during compilation. 897d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ScopedThreadSuspension sts(self, kNative); 898d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 899d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(run_optimizations_); 900d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen.reset( 901d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 902d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 903d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 904d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 905d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 906d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 907d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 908d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 909d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray *dex_file, 910d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_cache)); 911d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() == nullptr) { 912d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 913d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 914d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 915d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 916d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray size_t stack_map_size = codegen->ComputeStackMapsSize(); 917d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size); 918d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (stack_map_data == nullptr) { 919d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 920d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 921d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size)); 922d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const void* code = code_cache->CommitCode( 923d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray self, 924d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method, 925d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 926d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map_data, 927d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 928d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 929d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetCoreSpillMask(), 930d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetFpuSpillMask(), 931d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_allocator.GetMemory().data(), 932d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_allocator.GetSize()); 933d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 934d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (code == nullptr) { 935d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_cache->ClearData(self, stack_map_data); 936d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 937d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 938d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 939d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return true; 940d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 941d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 942b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 943