optimizing_compiler.cc revision 5cc349f3dd578e974f78314c50b6a0267c23e591
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> 205cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include <memory> 21787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include <stdint.h> 22787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 2344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64 24b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#include "dex_cache_array_fixups_arm.h" 25b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif 26b4536b7de576b20c74c612406c5d3132998075efVladimir Marko 27b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm64 2844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#include "instruction_simplifier_arm64.h" 2944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif 3044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames 310616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86 320f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko#include "pc_relative_fixups_x86.h" 330616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif 340616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell 35e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h" 36b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h" 37f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko#include "base/arena_containers.h" 385e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h" 39c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir Marko#include "base/macros.h" 405e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h" 4146e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h" 42f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h" 43787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h" 44787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h" 4520f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h" 4653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h" 4775be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h" 4875be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h" 4971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h" 50f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h" 51f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h" 52787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h" 539523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h" 5420f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h" 5592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h" 565cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include "dwarf/method_debug_info.h" 575cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include "elf_writer_debug.h" 58e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h" 5969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h" 60f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h" 61d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h" 6222af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h" 63e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h" 643c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h" 6571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h" 665cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include "jit/debugger_interface.h" 67d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray#include "jit/jit_code_cache.h" 6882091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h" 69e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h" 708df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang#include "load_store_elimination.h" 71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h" 7226a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h" 73f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h" 74a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h" 755cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include "oat_quick_method_header.h" 76dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko#include "sharpening.h" 77827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h" 783159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h" 797dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h" 80804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h" 81c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h" 82175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h" 83b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 84b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 85b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 87787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 88787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 91f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko explicit CodeVectorAllocator(ArenaAllocator* arena) 92f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko : memory_(arena->Adapter(kArenaAllocCodeBuffer)), 93f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko size_(0) {} 94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 9792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 100787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 101787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 102f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko const ArenaVector<uint8_t>& GetMemory() const { return memory_; } 103787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 104787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 105f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> memory_; 106787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 107787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 108787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 109787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 110787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 111f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/** 112f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will 113ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped. 114f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */ 11553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = ""; 116f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 11769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope; 118809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 11969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject { 1205e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public: 12169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver(HGraph* graph, 12269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil CodeGenerator* codegen, 12369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil std::ostream* visualizer_output, 12469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil CompilerDriver* compiler_driver) 12569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil : graph_(graph), 1260d1caa5df8d99320036888600190337bbe540731Vladimir Marko cached_method_name_(), 127809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil timing_logger_enabled_(compiler_driver->GetDumpPasses()), 1280d1caa5df8d99320036888600190337bbe540731Vladimir Marko timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true), 129eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames disasm_info_(graph->GetArena()), 130809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()), 13169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil visualizer_(visualizer_output, graph, *codegen), 13269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil graph_in_bad_state_(false) { 13353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (timing_logger_enabled_ || visualizer_enabled_) { 1340d1caa5df8d99320036888600190337bbe540731Vladimir Marko if (!IsVerboseMethod(compiler_driver, GetMethodName())) { 13553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe timing_logger_enabled_ = visualizer_enabled_ = false; 13653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 13753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (visualizer_enabled_) { 1380d1caa5df8d99320036888600190337bbe540731Vladimir Marko visualizer_.PrintHeader(GetMethodName()); 13953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe codegen->SetDisassemblyInformation(&disasm_info_); 14053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 14162e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil } 1425e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1435e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 14469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil ~PassObserver() { 145809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (timing_logger_enabled_) { 1460d1caa5df8d99320036888600190337bbe540731Vladimir Marko LOG(INFO) << "TIMINGS " << GetMethodName(); 147809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); 148809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 149809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 150809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 151eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames void DumpDisassembly() const { 152eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames if (visualizer_enabled_) { 153eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames visualizer_.DumpGraphWithDisassembly(); 154eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames } 155eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames } 156eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 15769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil void SetGraphInBadState() { graph_in_bad_state_ = true; } 15869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 1590d1caa5df8d99320036888600190337bbe540731Vladimir Marko const char* GetMethodName() { 1600d1caa5df8d99320036888600190337bbe540731Vladimir Marko // PrettyMethod() is expensive, so we delay calling it until we actually have to. 1610d1caa5df8d99320036888600190337bbe540731Vladimir Marko if (cached_method_name_.empty()) { 1620d1caa5df8d99320036888600190337bbe540731Vladimir Marko cached_method_name_ = PrettyMethod(graph_->GetMethodIdx(), graph_->GetDexFile()); 1630d1caa5df8d99320036888600190337bbe540731Vladimir Marko } 1640d1caa5df8d99320036888600190337bbe540731Vladimir Marko return cached_method_name_.c_str(); 1650d1caa5df8d99320036888600190337bbe540731Vladimir Marko } 1660d1caa5df8d99320036888600190337bbe540731Vladimir Marko 167809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 168809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void StartPass(const char* pass_name) { 1695e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Dump graph first, then start timer. 1705e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 171ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_); 1725e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1735e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1745e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.StartTiming(pass_name); 1755e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1765e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1775e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 178809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void EndPass(const char* pass_name) { 1795e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Pause timer first, then dump graph. 1805e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1815e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.EndTiming(); 1825e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1835e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 184ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_); 1855e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 18669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 18769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // Validate the HGraph if running in debug mode. 18869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (kIsDebugBuild) { 18969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (!graph_in_bad_state_) { 19069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (graph_->IsInSsaForm()) { 191655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko SSAChecker checker(graph_); 19269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil checker.Run(); 19369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (!checker.IsValid()) { 19469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker); 19569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 19669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } else { 197655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko GraphChecker checker(graph_); 19869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil checker.Run(); 19969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (!checker.IsValid()) { 20069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker); 20169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 20269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 20369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 20469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 2055e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 2065e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 20753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) { 20853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an 20953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // empty kStringFilter matching all methods. 21053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) { 21153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name); 21253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 21353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 21453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code 21553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // warning when the string is empty. 21653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1; 21753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) { 21853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return true; 21953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 22053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 22153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return false; 22253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 22353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 22469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil HGraph* const graph_; 2250d1caa5df8d99320036888600190337bbe540731Vladimir Marko 2260d1caa5df8d99320036888600190337bbe540731Vladimir Marko std::string cached_method_name_; 2275e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 2285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool timing_logger_enabled_; 2295e8b137d28c840b128e2488f954cccee3e86db14David Brazdil TimingLogger timing_logger_; 2305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 231eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames DisassemblyInformation disasm_info_; 232eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 2335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool visualizer_enabled_; 2345e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphVisualizer visualizer_; 2355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 23669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // Flag to be set by the compiler if the pass failed and the graph is not 23769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // expected to validate. 23869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil bool graph_in_bad_state_; 239809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 24069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil friend PassScope; 24169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 24269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil DISALLOW_COPY_AND_ASSIGN(PassObserver); 2435e8b137d28c840b128e2488f954cccee3e86db14David Brazdil}; 2445e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 24569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject { 246809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public: 24769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope(const char *pass_name, PassObserver* pass_observer) 248809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil : pass_name_(pass_name), 24969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_(pass_observer) { 25069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_->StartPass(pass_name_); 251809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 252809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 25369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil ~PassScope() { 25469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_->EndPass(pass_name_); 255809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 256809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 257809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 258809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil const char* const pass_name_; 25969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver* const pass_observer_; 260809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil}; 261809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 26253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler { 26353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public: 26453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe explicit OptimizingCompiler(CompilerDriver* driver); 26588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray ~OptimizingCompiler(); 26653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const 26853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OVERRIDE; 26953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 27053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* Compile(const DexFile::CodeItem* code_item, 27153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 27253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 27353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 27453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 27553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 276736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier const DexFile& dex_file, 277736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier Handle<mirror::DexCache> dex_cache) const OVERRIDE; 27853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 27953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* JniCompile(uint32_t access_flags, 28053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 281216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray const DexFile& dex_file) const OVERRIDE { 282216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); 283216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 28453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 285e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE 28690443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier SHARED_REQUIRES(Locks::mutator_lock_) { 287216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( 288216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); 289216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 29053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 291216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE; 29253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 293ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil void Init() OVERRIDE; 29453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 295216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray void UnInit() const OVERRIDE; 29653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 2972be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle void MaybeRecordStat(MethodCompilationStat compilation_stat) const { 2982be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (compilation_stats_.get() != nullptr) { 2992be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_->RecordStat(compilation_stat); 3002be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 3012be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 3022be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle 303d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method) 304d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray OVERRIDE 305d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray SHARED_REQUIRES(Locks::mutator_lock_); 306d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 30753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private: 30888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // Whether we should run any optimization or register allocation. If false, will 30988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // just run the code generation after the graph was built. 31088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray const bool run_optimizations_; 31148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 312d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Create a 'CompiledMethod' for an optimized graph. 313d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompiledMethod* EmitOptimized(ArenaAllocator* arena, 314d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 315d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 316d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* driver) const; 317d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 318d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Create a 'CompiledMethod' for a non-optimized graph. 319d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompiledMethod* EmitBaseline(ArenaAllocator* arena, 320d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 321d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 322d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* driver) const; 323d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 324d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Try compiling a method and return the code generator used for 325d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // compiling it. 326d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // This method: 327d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 1) Builds the graph. Returns null if it failed to build it. 328d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 2) If `run_optimizations_` is set: 329d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 2.1) Transform the graph to SSA. Returns null if it failed. 330d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 2.2) Run optimizations on the graph, including register allocator. 331d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 3) Generate code with the `code_allocator` provided. 332d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* TryCompile(ArenaAllocator* arena, 333d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 334d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 335d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 336d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 337d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 338d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 339d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 340d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 341d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache) const; 34212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 3432be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle std::unique_ptr<OptimizingCompilerStats> compilation_stats_; 34488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 34553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<std::ostream> visualizer_output_; 34653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 34753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 34853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}; 34953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 35088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 35188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 35288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 35388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray : Compiler(driver, kMaximumCompilationTimeBeforeWarning), 35488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray run_optimizations_( 355da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffray driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime) {} 356ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil 357ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() { 358ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // Enable C1visualizer output. Must be done in Init() because the compiler 359ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // driver is not fully initialized when passed to the compiler's constructor. 360ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CompilerDriver* driver = GetCompilerDriver(); 361866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil const std::string cfg_file_name = driver->GetDumpCfgFileName(); 362866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil if (!cfg_file_name.empty()) { 363ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CHECK_EQ(driver->GetThreadCount(), 1U) 364ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Graph visualizer requires the compiler to run single-threaded. " 365ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Invoke the compiler with '-j1'."; 36687000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle std::ios_base::openmode cfg_file_mode = 36787000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle driver->GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; 36887000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); 369f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray } 3702be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (driver->GetDumpStats()) { 3712be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_.reset(new OptimizingCompilerStats()); 3722be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 373f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray} 374787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 375216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const { 376216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 377216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 37888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() { 3792be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (compilation_stats_.get() != nullptr) { 3802be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_->Log(); 3812be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 38288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray} 38388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 384da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const { 385216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 386216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 387e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, 388e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file ATTRIBUTE_UNUSED, 389e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray CompilationUnit* cu ATTRIBUTE_UNUSED) const { 390e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return true; 39153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 39253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 3931ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) { 394cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat) 395cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle || instruction_set == kArm64 3961ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 397f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic || instruction_set == kMips 3984dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze || instruction_set == kMips64 3991ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86 4001ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86_64; 4011ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray} 4021ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray 40322ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment. 4040d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain// TODO: Add support for other architectures and remove this function 4050d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillainstatic bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) { 40622ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain return instruction_set == kArm64 40722ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain || instruction_set == kThumb2 4083b359c71f2fb784589be113206932e76807787bbRoland Levillain || instruction_set == kX86 4090d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain || instruction_set == kX86_64; 4100d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain} 4110d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain 41210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[], 41310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle size_t length, 41469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver* pass_observer) { 41510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle for (size_t i = 0; i < length; ++i) { 41669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(optimizations[i]->GetPassName(), pass_observer); 41769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil optimizations[i]->Run(); 41810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle } 41910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle} 42010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle 421ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph, 422dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 423ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle CompilerDriver* driver, 424ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle OptimizingCompilerStats* stats, 425ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const DexCompilationUnit& dex_compilation_unit, 426ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle PassObserver* pass_observer, 427ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle StackHandleScopeCollection* handles) { 428ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const CompilerOptions& compiler_options = driver->GetCompilerOptions(); 429ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle bool should_inline = (compiler_options.GetInlineDepthLimit() > 0) 430ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle && (compiler_options.GetInlineMaxCodeUnits() > 0); 431ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle if (!should_inline) { 432ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle return; 433ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle } 4345949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_; 435cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle HInliner* inliner = new (graph->GetArena()) HInliner( 4365949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray graph, 4375949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray graph, 4385949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray codegen, 4395949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray dex_compilation_unit, 4405949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray dex_compilation_unit, 4415949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray driver, 4425949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray handles, 4435949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray stats, 4445949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray number_of_dex_registers, 4455949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray /* depth */ 0); 446cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle HOptimization* optimizations[] = { inliner }; 447ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 448ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations, arraysize(optimizations), pass_observer); 449ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle} 450ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 45144b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set, 45244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HGraph* graph, 45344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames OptimizingCompilerStats* stats, 45444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames PassObserver* pass_observer) { 45544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames ArenaAllocator* arena = graph->GetArena(); 45644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames switch (instruction_set) { 457b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm 458b4536b7de576b20c74c612406c5d3132998075efVladimir Marko case kThumb2: 459b4536b7de576b20c74c612406c5d3132998075efVladimir Marko case kArm: { 460b4536b7de576b20c74c612406c5d3132998075efVladimir Marko arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats); 461b4536b7de576b20c74c612406c5d3132998075efVladimir Marko HOptimization* arm_optimizations[] = { 462b4536b7de576b20c74c612406c5d3132998075efVladimir Marko fixups 463b4536b7de576b20c74c612406c5d3132998075efVladimir Marko }; 464b4536b7de576b20c74c612406c5d3132998075efVladimir Marko RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer); 465b4536b7de576b20c74c612406c5d3132998075efVladimir Marko break; 466b4536b7de576b20c74c612406c5d3132998075efVladimir Marko } 467b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif 46844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64 46944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames case kArm64: { 47044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames arm64::InstructionSimplifierArm64* simplifier = 47144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames new (arena) arm64::InstructionSimplifierArm64(graph, stats); 47244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 47344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch"); 47444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HOptimization* arm64_optimizations[] = { 47544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames simplifier, 47644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames side_effects, 47744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames gvn 47844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames }; 47944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer); 48044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames break; 48144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames } 48244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif 4830616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86 4840616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell case kX86: { 4850f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko x86::PcRelativeFixups* pc_relative_fixups = new (arena) x86::PcRelativeFixups(graph, stats); 4860616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell HOptimization* x86_optimizations[] = { 4870f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko pc_relative_fixups 4880616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell }; 4890616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer); 4900616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell break; 4910616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell } 4920616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif 49344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames default: 49444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames break; 49544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames } 49644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames} 49744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames 498d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayNO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects. 499d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraystatic void AllocateRegisters(HGraph* graph, 500d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 501d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassObserver* pass_observer) { 502d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PrepareForRegisterAllocation(graph).Run(); 503d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray SsaLivenessAnalysis liveness(graph, codegen); 504d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 505d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); 506d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray liveness.Analyze(); 507d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 508d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 509d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); 510d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); 511d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 512d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 513d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 514e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph, 515dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 516e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray CompilerDriver* driver, 517e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray OptimizingCompilerStats* stats, 518e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 5194833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil PassObserver* pass_observer, 5204833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil StackHandleScopeCollection* handles) { 521a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko ArenaAllocator* arena = graph->GetArena(); 522a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination( 523a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName); 524a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination( 525a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName); 526e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HConstantFolding* fold1 = new (arena) HConstantFolding(graph); 527a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats); 528e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph); 529e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining"); 5304a34277c55279ba57ab361f7580db846a201d9b1Aart Bik HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce"); 531a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 532a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects); 533a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko LICM* licm = new (arena) LICM(graph, *side_effects); 5348df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects); 53522af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph); 5364a34277c55279ba57ab361f7580db846a201d9b1Aart Bik BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction); 537dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver); 538a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier( 53968289a531484d26214e09f1eadd9833531a3bc3cAlex Light graph, stats, "instruction_simplifier_after_bce"); 5404833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier( 541b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray graph, stats, "instruction_simplifier_before_codegen"); 542e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver); 54371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe 544ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle HOptimization* optimizations1[] = { 545a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko intrinsics, 5464833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil sharpening, 547a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko fold1, 548a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko simplify1, 54918e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray dce1, 550ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle }; 551ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer); 552ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 5534833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles); 554951779839f0d35ed5336f399c8f521fd9a6b7c27David Brazdil 5558993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil HOptimization* optimizations2[] = { 5568993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // BooleanSimplifier depends on the InstructionSimplifier removing 5578993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // redundant suspend checks to recognize empty blocks. 5588993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil boolean_simplify, 5598993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil fold2, // TODO: if we don't inline we can also skip fold2. 5608993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil side_effects, 5618993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil gvn, 5628993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil licm, 5638993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil induction, 5648993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil bce, 5658993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil fold3, // evaluates code generated by dynamic bce 5664833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil simplify2, 5678993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil lse, 5688993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil dce2, 5698993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // The codegen has a few assumptions that only the instruction simplifier 5708993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // can satisfy. For example, the code generator does not expect to see a 5718993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // HTypeConversion from a type to the same type. 5724833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil simplify3, 5738993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil }; 5748993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); 57539fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil 57644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer); 577d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray AllocateRegisters(graph, codegen, pass_observer); 5785e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray} 5795e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 580376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing 581376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them. 582f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Markostatic ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) { 583376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t size = vector.size(); 584376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t aligned_size = RoundUp(size, 4); 585376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray for (; size < aligned_size; ++size) { 586376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray vector.push_back(0); 587376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray } 588e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe return ArrayRef<const uint8_t>(vector); 589376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray} 590376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray 591581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { 592581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter()); 593581550137ee3a068a14224870e71aeee924a0646Vladimir Marko codegen->EmitLinkerPatches(&linker_patches); 594581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 595581550137ee3a068a14224870e71aeee924a0646Vladimir Marko // Sort patches by literal offset. Required for .oat_patches encoding. 596581550137ee3a068a14224870e71aeee924a0646Vladimir Marko std::sort(linker_patches.begin(), linker_patches.end(), 597581550137ee3a068a14224870e71aeee924a0646Vladimir Marko [](const LinkerPatch& lhs, const LinkerPatch& rhs) { 598581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return lhs.LiteralOffset() < rhs.LiteralOffset(); 599581550137ee3a068a14224870e71aeee924a0646Vladimir Marko }); 600581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 601581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return linker_patches; 602581550137ee3a068a14224870e71aeee924a0646Vladimir Marko} 603581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 604d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCompiledMethod* OptimizingCompiler::EmitOptimized(ArenaAllocator* arena, 605d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 606d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 607d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* compiler_driver) const { 608581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 609f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps)); 610d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map.resize(codegen->ComputeStackMapsSize()); 611d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size())); 61212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 613eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 61412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray compiler_driver, 61512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetInstructionSet(), 616d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const uint8_t>(code_allocator->GetMemory()), 617aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 618aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 619aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 620aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 62112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetCoreSpillMask(), 622d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray codegen->GetFpuSpillMask(), 623f71b3ade9c99ce2fec2f5049ce9c5968721e1b81David Srbecky ArrayRef<const SrcMapElem>(), 624c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // mapping_table. 625c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(stack_map), 626c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // native_gc_map. 627c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 628581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArrayRef<const LinkerPatch>(linker_patches)); 629ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier 630eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames return compiled_method; 63112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 63212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 633d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCompiledMethod* OptimizingCompiler::EmitBaseline( 634d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator* arena, 635d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 63612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CodeGenerator* codegen, 637d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CompilerDriver* compiler_driver) const { 638581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 639581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 640f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps)); 641c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky codegen->BuildMappingTable(&mapping_table); 642f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps)); 64312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->BuildVMapTable(&vmap_table); 644f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps)); 645d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildNativeGCMap(&gc_map, *compiler_driver); 64612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 647eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 648aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain compiler_driver, 649aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetInstructionSet(), 650d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const uint8_t>(code_allocator->GetMemory()), 651aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 652aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 653aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 654aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 655aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetCoreSpillMask(), 656aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetFpuSpillMask(), 657f71b3ade9c99ce2fec2f5049ce9c5968721e1b81David Srbecky ArrayRef<const SrcMapElem>(), 658aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(mapping_table), 659aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(vmap_table), 660aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(gc_map), 661c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 662581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArrayRef<const LinkerPatch>(linker_patches)); 663eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames return compiled_method; 66412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 66512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 666d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena, 667d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 668d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 669d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 670d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 671d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 672d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 673d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 674d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 675d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache) const { 6762be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kAttemptCompilation); 677cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 678cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle InstructionSet instruction_set = compiler_driver->GetInstructionSet(); 679cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle 6803b359c71f2fb784589be113206932e76807787bbRoland Levillain // Always use the Thumb-2 assembler: some runtime functionality 6813b359c71f2fb784589be113206932e76807787bbRoland Levillain // (like implicit stack overflow checks) assume Thumb-2. 6828d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray if (instruction_set == kArm) { 6838d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray instruction_set = kThumb2; 6848fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6858fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 6868fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray // Do not attempt to compile on architectures we do not support. 6871ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray if (!IsInstructionSetSupported(instruction_set)) { 6882be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); 6898fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray return nullptr; 6908fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6918fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 6920d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // When read barriers are enabled, do not attempt to compile for 6930d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // instruction sets that have no read barrier support. 6940d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) { 6950d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain return nullptr; 6960d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain } 6970d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain 698b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 6992be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological); 700b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray return nullptr; 701b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 702b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray 70336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray // Implementation of the space filter: do not compile a code item whose size in 704432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray // code units is bigger than 128. 705432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray static constexpr size_t kSpaceFilterOptimizingThreshold = 128; 70636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); 70736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace) 70836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { 7092be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter); 71036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray return nullptr; 71136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray } 71236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray 71392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 7149523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item, 71572d32629303f8f39362a4099481f48646aed042fIan Rogers class_def_idx, method_idx, access_flags, 716736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache); 71792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 7183cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle bool requires_barrier = dex_compilation_unit.IsConstructor() 7193cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle && compiler_driver->RequiresConstructorBarrier(Thread::Current(), 7203cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetDexFile(), 7213cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetClassDefIndex()); 722d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray HGraph* graph = new (arena) HGraph( 723d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(), 724e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable()); 725e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 72612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 727cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CodeGenerator::Create(graph, 728cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle instruction_set, 729cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle *compiler_driver->GetInstructionSetFeatures(), 730cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle compiler_driver->GetCompilerOptions())); 73112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray if (codegen.get() == nullptr) { 7322be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen); 733787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 734787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 735c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky codegen->GetAssembler()->cfi().SetEnabled( 7368363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()); 737787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 73869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver pass_observer(graph, 73969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil codegen.get(), 74069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil visualizer_output_.get(), 74169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil compiler_driver); 7425e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 7439523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray const uint8_t* interpreter_metadata = nullptr; 7449523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray { 7459523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray ScopedObjectAccess soa(Thread::Current()); 746736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier StackHandleScope<1> hs(soa.Self()); 7479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray Handle<mirror::ClassLoader> loader(hs.NewHandle( 7489523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray soa.Decode<mirror::ClassLoader*>(class_loader))); 7499523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray ArtMethod* art_method = compiler_driver->ResolveMethod( 7509523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type); 7519523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray // We may not get a method, for example if its class is erroneous. 7529523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray if (art_method != nullptr) { 75373be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray graph->SetArtMethod(art_method); 7549523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray interpreter_metadata = art_method->GetQuickenedInfo(); 7559523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray } 7569523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray } 7575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphBuilder builder(graph, 7585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 7595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 7605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_file, 7615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil compiler_driver, 7629523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray compilation_stats_.get(), 763736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier interpreter_metadata, 764736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier dex_cache); 7655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 7660d1caa5df8d99320036888600190337bbe540731Vladimir Marko VLOG(compiler) << "Building " << pass_observer.GetMethodName(); 7675e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 768809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 76969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); 770809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (!builder.BuildGraph(*code_item)) { 77169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer.SetGraphInBadState(); 772809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil return nullptr; 773809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 7745e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 775a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 7760d1caa5df8d99320036888600190337bbe540731Vladimir Marko VLOG(compiler) << "Optimizing " << pass_observer.GetMethodName(); 7774833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil 778d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (run_optimizations_) { 7794833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil ScopedObjectAccess soa(Thread::Current()); 7804833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil StackHandleScopeCollection handles(soa.Self()); 7814833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil ScopedThreadSuspension sts(soa.Self(), kNative); 7824833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil 783809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 78469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer); 7854833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil BuildSsaResult result = graph->TryBuildingSsa(&handles); 7864833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil if (result != kBuildSsaSuccess) { 7874833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil switch (result) { 7884833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil case kBuildSsaFailNonNaturalLoop: 7894833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil MaybeRecordStat(MethodCompilationStat::kNotCompiledNonNaturalLoop); 7904833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil break; 7914833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil case kBuildSsaFailThrowCatchLoop: 7924833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop); 7934833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil break; 79415693bfdf9fa3ec79327a77b7e10315614d716ccDavid Brazdil case kBuildSsaFailAmbiguousArrayOp: 79515693bfdf9fa3ec79327a77b7e10315614d716ccDavid Brazdil MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp); 7964833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil break; 7974833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil case kBuildSsaSuccess: 7984833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil UNREACHABLE(); 7994833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil } 800ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil pass_observer.SetGraphInBadState(); 801809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil return nullptr; 802809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 803f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray } 8045e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 805d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray RunOptimizations(graph, 806d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen.get(), 807d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray compiler_driver, 808d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray compilation_stats_.get(), 809d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_compilation_unit, 8104833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil &pass_observer, 8114833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil &handles); 812d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->CompileOptimized(code_allocator); 813d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } else { 814d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->CompileBaseline(code_allocator); 815216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 816d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray pass_observer.DumpDisassembly(); 817f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko 818f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko if (kArenaAllocatorCountAllocations) { 819d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (arena->BytesAllocated() > 4 * MB) { 820d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MemStats mem_stats(arena->GetMemStats()); 821f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats); 822f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 823f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 824f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko 825d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return codegen.release(); 826216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 827216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 82807380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) { 82907380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved). 83007380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS 83107380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS 83207380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD 83307380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD; 834175dc732c80e6f2afd83209348124df349290ba8Calin Juravle return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0; 835175dc732c80e6f2afd83209348124df349290ba8Calin Juravle} 836175dc732c80e6f2afd83209348124df349290ba8Calin Juravle 837216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 838216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t access_flags, 839216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray InvokeType invoke_type, 840216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint16_t class_def_idx, 841216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t method_idx, 842f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle jobject jclass_loader, 843736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier const DexFile& dex_file, 844736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier Handle<mirror::DexCache> dex_cache) const { 845f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 846f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompiledMethod* method = nullptr; 847d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(Runtime::Current()->IsAotCompiler()); 848d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); 849d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(!verified_method->HasRuntimeThrow()); 850d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) 851d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray || CanHandleVerificationFailure(verified_method)) { 852d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 853d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 854d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 855d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 856d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 857d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 858d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 859d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 860d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 861d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 862d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 863d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_file, 864d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_cache)); 865d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() != nullptr) { 866ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle MaybeRecordStat(MethodCompilationStat::kCompiled); 867d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (run_optimizations_) { 868d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method = EmitOptimized(&arena, &code_allocator, codegen.get(), compiler_driver); 8690c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } else { 870d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method = EmitBaseline(&arena, &code_allocator, codegen.get(), compiler_driver); 8710c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } 872f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 8730c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } else { 874d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { 875d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime); 876d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } else { 877ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError); 878d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 879f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 880f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle 881cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle if (kIsDebugBuild && 882cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle IsCompilingWithCoreImage() && 8830d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain IsInstructionSetSupported(compiler_driver->GetInstructionSet()) && 8840d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain (!kEmitCompilerReadBarrier || 8850d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) { 8860d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // For testing purposes, we put a special marker on method names 8870d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // that should be compiled with this compiler (when the the 8880d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // instruction set is supported -- and has support for read 8890d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // barriers, if they are enabled). This makes sure we're not 8900d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // regressing. 89109b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle std::string method_name = PrettyMethod(method_idx, dex_file); 89209b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle bool shouldCompile = method_name.find("$opt$") != std::string::npos; 89309b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name; 89409b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle } 89509b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle 89612be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray return method; 897b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 898b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 89953c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) { 90053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return new OptimizingCompiler(driver); 90153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 90253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 903335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() { 904335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray const std::string& image = Runtime::Current()->GetImageLocation(); 905dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao // TODO: This is under-approximating... 906dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) { 907dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao return true; 908dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao } 909dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao return false; 910335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray} 911335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray 912d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraybool OptimizingCompiler::JitCompile(Thread* self, 913d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jit::JitCodeCache* code_cache, 914d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArtMethod* method) { 915d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray StackHandleScope<2> hs(self); 916d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 917d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method->GetDeclaringClass()->GetClassLoader())); 918d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); 919d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 920d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject jclass_loader = class_loader.ToJObject(); 921d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile* dex_file = method->GetDexFile(); 922d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint16_t class_def_idx = method->GetClassDefIndex(); 923d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); 924d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t method_idx = method->GetDexMethodIndex(); 925d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t access_flags = method->GetAccessFlags(); 926d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const InvokeType invoke_type = method->GetInvokeType(); 927d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 928d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 929d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 930d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen; 931d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 932d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Go to native so that we don't block GC during compilation. 933d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ScopedThreadSuspension sts(self, kNative); 934d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 935d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(run_optimizations_); 936d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen.reset( 937d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 938d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 939d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 940d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 941d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 942d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 943d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 944d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 945d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray *dex_file, 946d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_cache)); 947d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() == nullptr) { 948d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 949d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 950d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 951d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 952d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray size_t stack_map_size = codegen->ComputeStackMapsSize(); 953d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size); 954d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (stack_map_data == nullptr) { 955d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 956d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 95773be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kCompiled); 958d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size)); 959d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const void* code = code_cache->CommitCode( 960d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray self, 961d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method, 962d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 963d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map_data, 964d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 965d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 966d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetCoreSpillMask(), 967d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetFpuSpillMask(), 968d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_allocator.GetMemory().data(), 969d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_allocator.GetSize()); 970d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 971d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (code == nullptr) { 972d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_cache->ClearData(self, stack_map_data); 973d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 974d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 975d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 9765cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky if (GetCompilerDriver()->GetCompilerOptions().GetGenerateDebugInfo()) { 9775cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code); 9785cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode()); 9795cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky CompiledMethod compiled_method( 9805cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky GetCompilerDriver(), 9815cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->GetInstructionSet(), 9825cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(code_allocator.GetMemory()), 9835cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 9845cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->GetCoreSpillMask(), 9855cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->GetFpuSpillMask(), 9865cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const SrcMapElem>(), 9875cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(), // mapping_table. 9885cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(stack_map_data, stack_map_size), 9895cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(), // native_gc_map. 9905cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 9915cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const LinkerPatch>()); 9925cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky dwarf::MethodDebugInfo method_debug_info { 9935cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky dex_file, 9945cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky class_def_idx, 9955cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky method_idx, 9965cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky access_flags, 9975cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky code_item, 9985cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky false, // deduped. 9995cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky code_address, 10005cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky code_address + code_allocator.GetSize(), 10015cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky &compiled_method 10025cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky }; 10035cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t> elf_file = dwarf::WriteDebugElfFile(method_debug_info); 10045cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky CreateJITCodeEntryForAddress(code_address, 10055cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky std::unique_ptr<const uint8_t[]>(elf_file.data()), 10065cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky elf_file.size()); 10075cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky } 10085cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky 1009d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return true; 1010d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 1011d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 1012b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 1013