optimizing_compiler.cc revision 5b1c2ca30dad519be285f0a1e839c23cc4e3a51d
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()), 130c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray visualizer_enabled_(!compiler_driver->GetCompilerOptions().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: 308d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Create a 'CompiledMethod' for an optimized graph. 30958282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CompiledMethod* Emit(ArenaAllocator* arena, 31058282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeVectorAllocator* code_allocator, 31158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeGenerator* codegen, 31258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CompilerDriver* driver) const; 313d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 314d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Try compiling a method and return the code generator used for 315d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // compiling it. 316d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // This method: 317d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 1) Builds the graph. Returns null if it failed to build it. 31858282f4510961317b8d5a364a6f740a78926716fDavid Brazdil // 2) Transforms the graph to SSA. Returns null if it failed. 31958282f4510961317b8d5a364a6f740a78926716fDavid Brazdil // 3) Runs optimizations on the graph, including register allocator. 32058282f4510961317b8d5a364a6f740a78926716fDavid Brazdil // 4) Generates code with the `code_allocator` provided. 321d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* TryCompile(ArenaAllocator* arena, 322d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 323d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 324d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 325d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 326d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 327d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 328d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 329d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 330d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache) const; 33112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 3322be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle std::unique_ptr<OptimizingCompilerStats> compilation_stats_; 33388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 33453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<std::ostream> visualizer_output_; 33553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 33653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 33753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}; 33853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 33988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 34088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 34188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 34258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil : Compiler(driver, kMaximumCompilationTimeBeforeWarning) {} 343ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil 344ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() { 345ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // Enable C1visualizer output. Must be done in Init() because the compiler 346ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // driver is not fully initialized when passed to the compiler's constructor. 347ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CompilerDriver* driver = GetCompilerDriver(); 348c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName(); 349866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil if (!cfg_file_name.empty()) { 350ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CHECK_EQ(driver->GetThreadCount(), 1U) 351ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Graph visualizer requires the compiler to run single-threaded. " 352ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Invoke the compiler with '-j1'."; 35387000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle std::ios_base::openmode cfg_file_mode = 354c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; 35587000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); 356f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray } 3572be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (driver->GetDumpStats()) { 3582be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_.reset(new OptimizingCompilerStats()); 3592be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 360f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray} 361787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 362216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const { 363216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 364216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 36588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() { 3662be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (compilation_stats_.get() != nullptr) { 3672be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_->Log(); 3682be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 36988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray} 37088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 371da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const { 372216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 373216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 374e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, 375e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file ATTRIBUTE_UNUSED, 376e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray CompilationUnit* cu ATTRIBUTE_UNUSED) const { 377e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return true; 37853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 37953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 3801ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) { 381cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat) 382cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle || instruction_set == kArm64 3831ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 384f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic || instruction_set == kMips 3854dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze || instruction_set == kMips64 3861ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86 3871ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86_64; 3881ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray} 3891ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray 39022ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment. 3910d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain// TODO: Add support for other architectures and remove this function 3920d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillainstatic bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) { 39322ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain return instruction_set == kArm64 39422ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain || instruction_set == kThumb2 3953b359c71f2fb784589be113206932e76807787bbRoland Levillain || instruction_set == kX86 3960d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain || instruction_set == kX86_64; 3970d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain} 3980d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain 39910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[], 40010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle size_t length, 40169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver* pass_observer) { 40210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle for (size_t i = 0; i < length; ++i) { 40369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(optimizations[i]->GetPassName(), pass_observer); 40469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil optimizations[i]->Run(); 40510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle } 40610e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle} 40710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle 408ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph, 409dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 410ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle CompilerDriver* driver, 411ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle OptimizingCompilerStats* stats, 412ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const DexCompilationUnit& dex_compilation_unit, 413ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle PassObserver* pass_observer, 414ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle StackHandleScopeCollection* handles) { 415ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const CompilerOptions& compiler_options = driver->GetCompilerOptions(); 416ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle bool should_inline = (compiler_options.GetInlineDepthLimit() > 0) 417ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle && (compiler_options.GetInlineMaxCodeUnits() > 0); 418ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle if (!should_inline) { 419ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle return; 420ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle } 4215949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_; 422cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle HInliner* inliner = new (graph->GetArena()) HInliner( 4235949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray graph, 4245949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray graph, 4255949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray codegen, 4265949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray dex_compilation_unit, 4275949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray dex_compilation_unit, 4285949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray driver, 4295949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray handles, 4305949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray stats, 4315949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray number_of_dex_registers, 4325949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray /* depth */ 0); 433cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle HOptimization* optimizations[] = { inliner }; 434ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 435ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations, arraysize(optimizations), pass_observer); 436ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle} 437ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 43844b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set, 43944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HGraph* graph, 44044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames OptimizingCompilerStats* stats, 44144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames PassObserver* pass_observer) { 44244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames ArenaAllocator* arena = graph->GetArena(); 44344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames switch (instruction_set) { 444b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm 445b4536b7de576b20c74c612406c5d3132998075efVladimir Marko case kThumb2: 446b4536b7de576b20c74c612406c5d3132998075efVladimir Marko case kArm: { 447b4536b7de576b20c74c612406c5d3132998075efVladimir Marko arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats); 448b4536b7de576b20c74c612406c5d3132998075efVladimir Marko HOptimization* arm_optimizations[] = { 4496b5afdd144d2bb3bf994240797834b5666b2cf98Nicolas Geoffray fixups 450b4536b7de576b20c74c612406c5d3132998075efVladimir Marko }; 451b4536b7de576b20c74c612406c5d3132998075efVladimir Marko RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer); 452b4536b7de576b20c74c612406c5d3132998075efVladimir Marko break; 453b4536b7de576b20c74c612406c5d3132998075efVladimir Marko } 454b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif 45544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64 45644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames case kArm64: { 45744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames arm64::InstructionSimplifierArm64* simplifier = 45844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames new (arena) arm64::InstructionSimplifierArm64(graph, stats); 45944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 46044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch"); 46144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HOptimization* arm64_optimizations[] = { 46244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames simplifier, 46344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames side_effects, 46444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames gvn 46544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames }; 46644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer); 46744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames break; 46844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames } 46944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif 4700616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86 4710616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell case kX86: { 4720f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko x86::PcRelativeFixups* pc_relative_fixups = new (arena) x86::PcRelativeFixups(graph, stats); 4730616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell HOptimization* x86_optimizations[] = { 4740f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko pc_relative_fixups 4750616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell }; 4760616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer); 4770616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell break; 4780616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell } 4790616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif 48044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames default: 48144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames break; 48244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames } 48344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames} 48444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames 485d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayNO_INLINE // Avoid increasing caller's frame size by large stack-allocated objects. 486d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraystatic void AllocateRegisters(HGraph* graph, 487d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* codegen, 488d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassObserver* pass_observer) { 489d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PrepareForRegisterAllocation(graph).Run(); 490d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray SsaLivenessAnalysis liveness(graph, codegen); 491d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 492d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); 493d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray liveness.Analyze(); 494d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 495d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 496d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); 497d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); 498d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 499d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 500d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 501e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph, 502dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 503e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray CompilerDriver* driver, 504e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray OptimizingCompilerStats* stats, 505e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 5064833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil PassObserver* pass_observer, 5074833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil StackHandleScopeCollection* handles) { 508a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko ArenaAllocator* arena = graph->GetArena(); 509a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination( 510a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName); 511a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination( 512a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName); 513e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HConstantFolding* fold1 = new (arena) HConstantFolding(graph); 514a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats); 515e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph); 516e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining"); 5174a34277c55279ba57ab361f7580db846a201d9b1Aart Bik HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce"); 518a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 519a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects); 520a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko LICM* licm = new (arena) LICM(graph, *side_effects); 5218df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects); 52222af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph); 5234a34277c55279ba57ab361f7580db846a201d9b1Aart Bik BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction); 524dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver); 525a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier( 52668289a531484d26214e09f1eadd9833531a3bc3cAlex Light graph, stats, "instruction_simplifier_after_bce"); 5274833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier( 528b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray graph, stats, "instruction_simplifier_before_codegen"); 529e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver); 53071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe 531ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle HOptimization* optimizations1[] = { 532a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko intrinsics, 5334833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil sharpening, 534a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko fold1, 535a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko simplify1, 53618e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray dce1, 537ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle }; 538ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer); 539ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 5404833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles); 541951779839f0d35ed5336f399c8f521fd9a6b7c27David Brazdil 5428993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil HOptimization* optimizations2[] = { 5438993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // BooleanSimplifier depends on the InstructionSimplifier removing 5448993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // redundant suspend checks to recognize empty blocks. 5458993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil boolean_simplify, 5468993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil fold2, // TODO: if we don't inline we can also skip fold2. 5478993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil side_effects, 5488993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil gvn, 5498993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil licm, 5508993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil induction, 5518993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil bce, 5528993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil fold3, // evaluates code generated by dynamic bce 5534833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil simplify2, 5548993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil lse, 5558993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil dce2, 5568993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // The codegen has a few assumptions that only the instruction simplifier 5578993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // can satisfy. For example, the code generator does not expect to see a 5588993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // HTypeConversion from a type to the same type. 5594833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil simplify3, 5608993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil }; 5618993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); 56239fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil 56344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer); 564d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray AllocateRegisters(graph, codegen, pass_observer); 5655e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray} 5665e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 567581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { 568581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter()); 569581550137ee3a068a14224870e71aeee924a0646Vladimir Marko codegen->EmitLinkerPatches(&linker_patches); 570581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 571581550137ee3a068a14224870e71aeee924a0646Vladimir Marko // Sort patches by literal offset. Required for .oat_patches encoding. 572581550137ee3a068a14224870e71aeee924a0646Vladimir Marko std::sort(linker_patches.begin(), linker_patches.end(), 573581550137ee3a068a14224870e71aeee924a0646Vladimir Marko [](const LinkerPatch& lhs, const LinkerPatch& rhs) { 574581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return lhs.LiteralOffset() < rhs.LiteralOffset(); 575581550137ee3a068a14224870e71aeee924a0646Vladimir Marko }); 576581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 577581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return linker_patches; 578581550137ee3a068a14224870e71aeee924a0646Vladimir Marko} 579581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 58058282f4510961317b8d5a364a6f740a78926716fDavid BrazdilCompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena, 58158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeVectorAllocator* code_allocator, 58258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeGenerator* codegen, 58358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CompilerDriver* compiler_driver) const { 584581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 585f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps)); 586d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map.resize(codegen->ComputeStackMapsSize()); 587d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size())); 58812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 589eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 59012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray compiler_driver, 59112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetInstructionSet(), 592d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const uint8_t>(code_allocator->GetMemory()), 593aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 594aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 595aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 596aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 59712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetCoreSpillMask(), 598d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray codegen->GetFpuSpillMask(), 599f71b3ade9c99ce2fec2f5049ce9c5968721e1b81David Srbecky ArrayRef<const SrcMapElem>(), 600c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // mapping_table. 601c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(stack_map), 602c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // native_gc_map. 603c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 604581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArrayRef<const LinkerPatch>(linker_patches)); 605ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier 606eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames return compiled_method; 60712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 60812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 609d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena, 610d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 611d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 612d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 613d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 614d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 615d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 616d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 617d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 618d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache) const { 6192be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kAttemptCompilation); 620cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 621cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle InstructionSet instruction_set = compiler_driver->GetInstructionSet(); 622cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle 6233b359c71f2fb784589be113206932e76807787bbRoland Levillain // Always use the Thumb-2 assembler: some runtime functionality 6243b359c71f2fb784589be113206932e76807787bbRoland Levillain // (like implicit stack overflow checks) assume Thumb-2. 6258d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray if (instruction_set == kArm) { 6268d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray instruction_set = kThumb2; 6278fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6288fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 6298fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray // Do not attempt to compile on architectures we do not support. 6301ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray if (!IsInstructionSetSupported(instruction_set)) { 6312be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); 6328fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray return nullptr; 6338fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6348fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 6350d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // When read barriers are enabled, do not attempt to compile for 6360d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // instruction sets that have no read barrier support. 6370d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) { 6380d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain return nullptr; 6390d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain } 6400d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain 641b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 6422be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological); 643b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray return nullptr; 644b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 645b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray 64636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray // Implementation of the space filter: do not compile a code item whose size in 647432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray // code units is bigger than 128. 648432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray static constexpr size_t kSpaceFilterOptimizingThreshold = 128; 64936540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); 65036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace) 65136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { 6522be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter); 65336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray return nullptr; 65436540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray } 65536540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray 65692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 6579523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item, 65872d32629303f8f39362a4099481f48646aed042fIan Rogers class_def_idx, method_idx, access_flags, 659736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache); 66092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 6613cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle bool requires_barrier = dex_compilation_unit.IsConstructor() 6623cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle && compiler_driver->RequiresConstructorBarrier(Thread::Current(), 6633cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetDexFile(), 6643cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetClassDefIndex()); 665d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray HGraph* graph = new (arena) HGraph( 666d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(), 667e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable()); 668e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 66912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 670cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CodeGenerator::Create(graph, 671cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle instruction_set, 672cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle *compiler_driver->GetInstructionSetFeatures(), 673cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle compiler_driver->GetCompilerOptions())); 67412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray if (codegen.get() == nullptr) { 6752be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen); 676787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 677787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 678c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky codegen->GetAssembler()->cfi().SetEnabled( 6795b1c2ca30dad519be285f0a1e839c23cc4e3a51dDavid Srbecky compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo()); 680787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 68169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver pass_observer(graph, 68269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil codegen.get(), 68369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil visualizer_output_.get(), 68469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil compiler_driver); 6855e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 6869523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray const uint8_t* interpreter_metadata = nullptr; 6879523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray { 6889523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray ScopedObjectAccess soa(Thread::Current()); 689736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier StackHandleScope<1> hs(soa.Self()); 6909523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray Handle<mirror::ClassLoader> loader(hs.NewHandle( 6919523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray soa.Decode<mirror::ClassLoader*>(class_loader))); 6929523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray ArtMethod* art_method = compiler_driver->ResolveMethod( 6939523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type); 6949523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray // We may not get a method, for example if its class is erroneous. 6959523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray if (art_method != nullptr) { 69673be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray graph->SetArtMethod(art_method); 6979523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray interpreter_metadata = art_method->GetQuickenedInfo(); 6989523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray } 6999523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray } 7005e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphBuilder builder(graph, 7015e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 7025e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 7035e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_file, 7045e8b137d28c840b128e2488f954cccee3e86db14David Brazdil compiler_driver, 7059523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray compilation_stats_.get(), 706736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier interpreter_metadata, 707736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier dex_cache); 7085e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 7090d1caa5df8d99320036888600190337bbe540731Vladimir Marko VLOG(compiler) << "Building " << pass_observer.GetMethodName(); 7105e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 711809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 71269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); 713809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (!builder.BuildGraph(*code_item)) { 71469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer.SetGraphInBadState(); 715809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil return nullptr; 716809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 7175e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 718a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 7190d1caa5df8d99320036888600190337bbe540731Vladimir Marko VLOG(compiler) << "Optimizing " << pass_observer.GetMethodName(); 7204833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil 72158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil ScopedObjectAccess soa(Thread::Current()); 72258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil StackHandleScopeCollection handles(soa.Self()); 72358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil ScopedThreadSuspension sts(soa.Self(), kNative); 72458282f4510961317b8d5a364a6f740a78926716fDavid Brazdil 72558282f4510961317b8d5a364a6f740a78926716fDavid Brazdil { 72658282f4510961317b8d5a364a6f740a78926716fDavid Brazdil PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer); 72758282f4510961317b8d5a364a6f740a78926716fDavid Brazdil GraphAnalysisResult result = graph->TryBuildingSsa(&handles); 72858282f4510961317b8d5a364a6f740a78926716fDavid Brazdil if (result != kAnalysisSuccess) { 72958282f4510961317b8d5a364a6f740a78926716fDavid Brazdil switch (result) { 73058282f4510961317b8d5a364a6f740a78926716fDavid Brazdil case kAnalysisFailThrowCatchLoop: 73158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop); 73258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil break; 73358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil case kAnalysisFailAmbiguousArrayOp: 73458282f4510961317b8d5a364a6f740a78926716fDavid Brazdil MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp); 73558282f4510961317b8d5a364a6f740a78926716fDavid Brazdil break; 73658282f4510961317b8d5a364a6f740a78926716fDavid Brazdil case kAnalysisSuccess: 73758282f4510961317b8d5a364a6f740a78926716fDavid Brazdil UNREACHABLE(); 738809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 73958282f4510961317b8d5a364a6f740a78926716fDavid Brazdil pass_observer.SetGraphInBadState(); 74058282f4510961317b8d5a364a6f740a78926716fDavid Brazdil return nullptr; 741f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray } 742216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 74358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil 74458282f4510961317b8d5a364a6f740a78926716fDavid Brazdil RunOptimizations(graph, 74558282f4510961317b8d5a364a6f740a78926716fDavid Brazdil codegen.get(), 74658282f4510961317b8d5a364a6f740a78926716fDavid Brazdil compiler_driver, 74758282f4510961317b8d5a364a6f740a78926716fDavid Brazdil compilation_stats_.get(), 74858282f4510961317b8d5a364a6f740a78926716fDavid Brazdil dex_compilation_unit, 74958282f4510961317b8d5a364a6f740a78926716fDavid Brazdil &pass_observer, 75058282f4510961317b8d5a364a6f740a78926716fDavid Brazdil &handles); 75158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil codegen->Compile(code_allocator); 752d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray pass_observer.DumpDisassembly(); 753f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko 754f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko if (kArenaAllocatorCountAllocations) { 755d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (arena->BytesAllocated() > 4 * MB) { 756d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MemStats mem_stats(arena->GetMemStats()); 757f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats); 758f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 759f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 760f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko 761d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return codegen.release(); 762216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 763216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 76407380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) { 76507380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved). 76607380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS 76707380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS 76807380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD 76907380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD; 770175dc732c80e6f2afd83209348124df349290ba8Calin Juravle return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0; 771175dc732c80e6f2afd83209348124df349290ba8Calin Juravle} 772175dc732c80e6f2afd83209348124df349290ba8Calin Juravle 773216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 774216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t access_flags, 775216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray InvokeType invoke_type, 776216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint16_t class_def_idx, 777216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t method_idx, 778f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle jobject jclass_loader, 779736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier const DexFile& dex_file, 780736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier Handle<mirror::DexCache> dex_cache) const { 781f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 782f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompiledMethod* method = nullptr; 783d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(Runtime::Current()->IsAotCompiler()); 784d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); 785d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(!verified_method->HasRuntimeThrow()); 786d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) 787d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray || CanHandleVerificationFailure(verified_method)) { 788d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 789d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 790d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 791d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 792d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 793d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 794d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 795d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 796d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 797d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 798d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 799d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_file, 800d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_cache)); 801d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() != nullptr) { 802ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle MaybeRecordStat(MethodCompilationStat::kCompiled); 80358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver); 804f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 8050c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } else { 806d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { 807d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime); 808d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } else { 809ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError); 810d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 811f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 812f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle 813cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle if (kIsDebugBuild && 814cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle IsCompilingWithCoreImage() && 8150d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain IsInstructionSetSupported(compiler_driver->GetInstructionSet()) && 8160d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain (!kEmitCompilerReadBarrier || 8170d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) { 8180d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // For testing purposes, we put a special marker on method names 8190d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // that should be compiled with this compiler (when the the 8200d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // instruction set is supported -- and has support for read 8210d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // barriers, if they are enabled). This makes sure we're not 8220d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // regressing. 82309b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle std::string method_name = PrettyMethod(method_idx, dex_file); 82409b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle bool shouldCompile = method_name.find("$opt$") != std::string::npos; 82509b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name; 82609b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle } 82709b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle 82812be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray return method; 829b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 830b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 83153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) { 83253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return new OptimizingCompiler(driver); 83353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 83453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 835335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() { 836335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray const std::string& image = Runtime::Current()->GetImageLocation(); 837dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao // TODO: This is under-approximating... 838dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) { 839dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao return true; 840dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao } 841dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao return false; 842335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray} 843335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray 844d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraybool OptimizingCompiler::JitCompile(Thread* self, 845d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jit::JitCodeCache* code_cache, 846d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArtMethod* method) { 847d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray StackHandleScope<2> hs(self); 848d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 849d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method->GetDeclaringClass()->GetClassLoader())); 850d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); 851d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 852d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject jclass_loader = class_loader.ToJObject(); 853d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile* dex_file = method->GetDexFile(); 854d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint16_t class_def_idx = method->GetClassDefIndex(); 855d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); 856d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t method_idx = method->GetDexMethodIndex(); 857d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t access_flags = method->GetAccessFlags(); 858d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const InvokeType invoke_type = method->GetInvokeType(); 859d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 860d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 861d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 862d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen; 863d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 864d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Go to native so that we don't block GC during compilation. 865d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ScopedThreadSuspension sts(self, kNative); 866d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen.reset( 867d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 868d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 869d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 870d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 871d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 872d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 873d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 874d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 875d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray *dex_file, 876d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_cache)); 877d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() == nullptr) { 878d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 879d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 880d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 881d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 882d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray size_t stack_map_size = codegen->ComputeStackMapsSize(); 883d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size); 884d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (stack_map_data == nullptr) { 885d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 886d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 88773be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kCompiled); 888d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size)); 889d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const void* code = code_cache->CommitCode( 890d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray self, 891d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method, 892d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 893d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map_data, 894d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 895d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 896d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetCoreSpillMask(), 897d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetFpuSpillMask(), 898d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_allocator.GetMemory().data(), 899d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_allocator.GetSize()); 900d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 901d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (code == nullptr) { 902d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_cache->ClearData(self, stack_map_data); 903d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 904d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 905d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 9065cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky if (GetCompilerDriver()->GetCompilerOptions().GetGenerateDebugInfo()) { 9075cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code); 9085cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode()); 9095cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky CompiledMethod compiled_method( 9105cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky GetCompilerDriver(), 9115cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->GetInstructionSet(), 9125cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(code_allocator.GetMemory()), 9135cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 9145cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->GetCoreSpillMask(), 9155cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky codegen->GetFpuSpillMask(), 9165cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const SrcMapElem>(), 9175cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(), // mapping_table. 9185cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(stack_map_data, stack_map_size), 9195cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(), // native_gc_map. 9205cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 9215cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky ArrayRef<const LinkerPatch>()); 9225cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky dwarf::MethodDebugInfo method_debug_info { 9235cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky dex_file, 9245cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky class_def_idx, 9255cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky method_idx, 9265cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky access_flags, 9275cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky code_item, 9285cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky false, // deduped. 9295cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky code_address, 9305cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky code_address + code_allocator.GetSize(), 9315cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky &compiled_method 9325cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky }; 93386e4278d5edc3685465b8846dcb17efa83c86d75Tamas Berghammer ArrayRef<const uint8_t> elf_file = dwarf::WriteDebugElfFileForMethod(method_debug_info); 9345cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky CreateJITCodeEntryForAddress(code_address, 9355cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky std::unique_ptr<const uint8_t[]>(elf_file.data()), 9365cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky elf_file.size()); 9375cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky } 9385cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky 939d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return true; 940d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 941d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 942b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 943