optimizing_compiler.cc revision a4f81546373f4cb5fa6dfc135307ee0a1d930872
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 23d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik#ifdef ART_ENABLE_CODEGEN_arm 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" 41f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h" 42787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h" 43787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h" 4420f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h" 4553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h" 4675be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h" 4775be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h" 48c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "debug/elf_debug_writer.h" 494fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "debug/method_debug_info.h" 5071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h" 51f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h" 524fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "dex/verified_method.h" 539523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h" 5420f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h" 5592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h" 56e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h" 5769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h" 58f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h" 59d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h" 6022af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h" 61e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h" 623c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h" 634a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko#include "instruction_simplifier_arm.h" 6471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h" 655cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include "jit/debugger_interface.h" 66a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray#include "jit/jit.h" 67d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray#include "jit/jit_code_cache.h" 68e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h" 694fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "licm.h" 708df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang#include "load_store_elimination.h" 71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h" 724fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "oat_quick_method_header.h" 7326a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h" 74f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h" 75a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h" 7674eb1b264691c4eb399d0858015a7fc13c476ac6David Brazdil#include "select_generator.h" 77dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko#include "sharpening.h" 78827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h" 793159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h" 80804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h" 814fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "ssa_phi_elimination.h" 82c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h" 83175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h" 84b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 85b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 86b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 87787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 88787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 92f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko explicit CodeVectorAllocator(ArenaAllocator* arena) 93f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko : memory_(arena->Adapter(kArenaAllocCodeBuffer)), 94f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko size_(0) {} 95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 9892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 100787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 101787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 102787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 103f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko const ArenaVector<uint8_t>& GetMemory() const { return memory_; } 104787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 105787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 106f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> memory_; 107787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 108787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 109787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 110787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 111787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 112f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/** 113f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will 114ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped. 115f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */ 11653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = ""; 117f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 11869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope; 119809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 12069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject { 1215e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public: 12269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver(HGraph* graph, 12369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil CodeGenerator* codegen, 12469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil std::ostream* visualizer_output, 12569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil CompilerDriver* compiler_driver) 12669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil : graph_(graph), 1270d1caa5df8d99320036888600190337bbe540731Vladimir Marko cached_method_name_(), 128809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil timing_logger_enabled_(compiler_driver->GetDumpPasses()), 1290d1caa5df8d99320036888600190337bbe540731Vladimir Marko timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true), 130eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames disasm_info_(graph->GetArena()), 131c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()), 13269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil visualizer_(visualizer_output, graph, *codegen), 13369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil graph_in_bad_state_(false) { 13453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (timing_logger_enabled_ || visualizer_enabled_) { 1350d1caa5df8d99320036888600190337bbe540731Vladimir Marko if (!IsVerboseMethod(compiler_driver, GetMethodName())) { 13653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe timing_logger_enabled_ = visualizer_enabled_ = false; 13753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 13853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (visualizer_enabled_) { 1390d1caa5df8d99320036888600190337bbe540731Vladimir Marko visualizer_.PrintHeader(GetMethodName()); 14053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe codegen->SetDisassemblyInformation(&disasm_info_); 14153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 14262e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil } 1435e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1445e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 14569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil ~PassObserver() { 146809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (timing_logger_enabled_) { 1470d1caa5df8d99320036888600190337bbe540731Vladimir Marko LOG(INFO) << "TIMINGS " << GetMethodName(); 148809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); 149809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 150809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 151809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 152eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames void DumpDisassembly() const { 153eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames if (visualizer_enabled_) { 154eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames visualizer_.DumpGraphWithDisassembly(); 155eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames } 156eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames } 157eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 15869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil void SetGraphInBadState() { graph_in_bad_state_ = true; } 15969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 1600d1caa5df8d99320036888600190337bbe540731Vladimir Marko const char* GetMethodName() { 1610d1caa5df8d99320036888600190337bbe540731Vladimir Marko // PrettyMethod() is expensive, so we delay calling it until we actually have to. 1620d1caa5df8d99320036888600190337bbe540731Vladimir Marko if (cached_method_name_.empty()) { 1630d1caa5df8d99320036888600190337bbe540731Vladimir Marko cached_method_name_ = PrettyMethod(graph_->GetMethodIdx(), graph_->GetDexFile()); 1640d1caa5df8d99320036888600190337bbe540731Vladimir Marko } 1650d1caa5df8d99320036888600190337bbe540731Vladimir Marko return cached_method_name_.c_str(); 1660d1caa5df8d99320036888600190337bbe540731Vladimir Marko } 1670d1caa5df8d99320036888600190337bbe540731Vladimir Marko 168809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 169809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void StartPass(const char* pass_name) { 1705e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Dump graph first, then start timer. 1715e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 172ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_); 1735e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1745e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1755e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.StartTiming(pass_name); 1765e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1775e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1785e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 179809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void EndPass(const char* pass_name) { 1805e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Pause timer first, then dump graph. 1815e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1825e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.EndTiming(); 1835e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1845e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 185ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_); 1865e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 18769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 18869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // Validate the HGraph if running in debug mode. 18969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (kIsDebugBuild) { 19069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil if (!graph_in_bad_state_) { 191badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil GraphChecker checker(graph_); 192badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil checker.Run(); 193badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil if (!checker.IsValid()) { 194badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker); 19569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 19669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 19769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil } 1985e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1995e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 20053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) { 20153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an 20253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // empty kStringFilter matching all methods. 20353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) { 20453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name); 20553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 20653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 20753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code 20853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe // warning when the string is empty. 20953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1; 21053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) { 21153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return true; 21253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 21353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 21453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe return false; 21553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe } 21653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe 21769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil HGraph* const graph_; 2180d1caa5df8d99320036888600190337bbe540731Vladimir Marko 2190d1caa5df8d99320036888600190337bbe540731Vladimir Marko std::string cached_method_name_; 2205e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 2215e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool timing_logger_enabled_; 2225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil TimingLogger timing_logger_; 2235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 224eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames DisassemblyInformation disasm_info_; 225eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames 2265e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool visualizer_enabled_; 2275e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphVisualizer visualizer_; 2285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 22969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // Flag to be set by the compiler if the pass failed and the graph is not 23069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil // expected to validate. 23169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil bool graph_in_bad_state_; 232809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 23369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil friend PassScope; 23469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil 23569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil DISALLOW_COPY_AND_ASSIGN(PassObserver); 2365e8b137d28c840b128e2488f954cccee3e86db14David Brazdil}; 2375e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 23869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject { 239809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public: 24069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope(const char *pass_name, PassObserver* pass_observer) 241809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil : pass_name_(pass_name), 24269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_(pass_observer) { 24369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_->StartPass(pass_name_); 244809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 245809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 24669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil ~PassScope() { 24769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil pass_observer_->EndPass(pass_name_); 248809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 249809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 250809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 251809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil const char* const pass_name_; 25269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver* const pass_observer_; 253809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil}; 254809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 25553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler { 25653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public: 25753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe explicit OptimizingCompiler(CompilerDriver* driver); 25888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray ~OptimizingCompiler(); 25953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 26053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const 26153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OVERRIDE; 26253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 26353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* Compile(const DexFile::CodeItem* code_item, 26453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 26553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 26653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 26853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 269736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier const DexFile& dex_file, 270736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier Handle<mirror::DexCache> dex_cache) const OVERRIDE; 27153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 27253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* JniCompile(uint32_t access_flags, 27353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 274216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray const DexFile& dex_file) const OVERRIDE { 275216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); 276216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 27753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 278e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE 27990443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier SHARED_REQUIRES(Locks::mutator_lock_) { 280216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( 281216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); 282216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 28353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 284216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE; 28553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 286ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil void Init() OVERRIDE; 28753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 288216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray void UnInit() const OVERRIDE; 28953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 2902be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle void MaybeRecordStat(MethodCompilationStat compilation_stat) const { 2912be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (compilation_stats_.get() != nullptr) { 2922be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_->RecordStat(compilation_stat); 2932be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 2942be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 2952be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle 296b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method, bool osr) 297d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray OVERRIDE 298d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray SHARED_REQUIRES(Locks::mutator_lock_); 299d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 30053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private: 301d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Create a 'CompiledMethod' for an optimized graph. 30258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CompiledMethod* Emit(ArenaAllocator* arena, 30358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeVectorAllocator* code_allocator, 30458282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeGenerator* codegen, 305b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray CompilerDriver* driver, 306b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray const DexFile::CodeItem* item) const; 307d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 308d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Try compiling a method and return the code generator used for 309d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // compiling it. 310d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // This method: 311d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // 1) Builds the graph. Returns null if it failed to build it. 31258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil // 2) Transforms the graph to SSA. Returns null if it failed. 31358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil // 3) Runs optimizations on the graph, including register allocator. 31458282f4510961317b8d5a364a6f740a78926716fDavid Brazdil // 4) Generates code with the `code_allocator` provided. 315d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeGenerator* TryCompile(ArenaAllocator* arena, 316d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 317d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 318d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 319d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 320d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 321d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 322d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 323d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 324b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray Handle<mirror::DexCache> dex_cache, 325b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray bool osr) const; 32612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 3272be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle std::unique_ptr<OptimizingCompilerStats> compilation_stats_; 32888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 32953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<std::ostream> visualizer_output_; 33053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 33153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 33253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}; 33353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 33488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 33588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 33688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 33758282f4510961317b8d5a364a6f740a78926716fDavid Brazdil : Compiler(driver, kMaximumCompilationTimeBeforeWarning) {} 338ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil 339ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() { 340ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // Enable C1visualizer output. Must be done in Init() because the compiler 341ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // driver is not fully initialized when passed to the compiler's constructor. 342ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CompilerDriver* driver = GetCompilerDriver(); 343c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName(); 344866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil if (!cfg_file_name.empty()) { 345ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CHECK_EQ(driver->GetThreadCount(), 1U) 346ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Graph visualizer requires the compiler to run single-threaded. " 347ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Invoke the compiler with '-j1'."; 34887000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle std::ios_base::openmode cfg_file_mode = 349c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out; 35087000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode)); 351f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray } 3522be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (driver->GetDumpStats()) { 3532be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_.reset(new OptimizingCompilerStats()); 3542be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 355f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray} 356787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 357216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const { 358216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 359216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 36088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() { 3612be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle if (compilation_stats_.get() != nullptr) { 3622be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle compilation_stats_->Log(); 3632be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle } 36488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray} 36588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 366da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const { 367216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 368216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 369e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, 370e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file ATTRIBUTE_UNUSED, 371e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray CompilationUnit* cu ATTRIBUTE_UNUSED) const { 372e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return true; 37353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 37453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 3751ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) { 376cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat) 377cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle || instruction_set == kArm64 3781ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 379f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic || instruction_set == kMips 3804dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze || instruction_set == kMips64 3811ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86 3821ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86_64; 3831ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray} 3841ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray 38522ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment. 3860d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain// TODO: Add support for other architectures and remove this function 3870d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillainstatic bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) { 38822ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain return instruction_set == kArm64 38922ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain || instruction_set == kThumb2 3903b359c71f2fb784589be113206932e76807787bbRoland Levillain || instruction_set == kX86 3910d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain || instruction_set == kX86_64; 3920d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain} 3930d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain 39410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[], 39510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle size_t length, 39669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver* pass_observer) { 39710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle for (size_t i = 0; i < length; ++i) { 39869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassScope scope(optimizations[i]->GetPassName(), pass_observer); 39969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil optimizations[i]->Run(); 40010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle } 40110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle} 40210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle 403ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph, 404dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 405ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle CompilerDriver* driver, 406ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle OptimizingCompilerStats* stats, 407ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const DexCompilationUnit& dex_compilation_unit, 408ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle PassObserver* pass_observer, 409ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle StackHandleScopeCollection* handles) { 410ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle const CompilerOptions& compiler_options = driver->GetCompilerOptions(); 411ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle bool should_inline = (compiler_options.GetInlineDepthLimit() > 0) 412ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle && (compiler_options.GetInlineMaxCodeUnits() > 0); 413ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle if (!should_inline) { 414ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle return; 415ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle } 4165949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_; 417cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle HInliner* inliner = new (graph->GetArena()) HInliner( 4185949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray graph, 4195949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray graph, 4205949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray codegen, 4215949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray dex_compilation_unit, 4225949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray dex_compilation_unit, 4235949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray driver, 4245949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray handles, 4255949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray stats, 4265949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray number_of_dex_registers, 4275949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray /* depth */ 0); 428cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle HOptimization* optimizations[] = { inliner }; 429ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 430ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations, arraysize(optimizations), pass_observer); 431ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle} 432ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 43344b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set, 43444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HGraph* graph, 435d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik CodeGenerator* codegen, 43644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames OptimizingCompilerStats* stats, 43744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames PassObserver* pass_observer) { 43844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames ArenaAllocator* arena = graph->GetArena(); 43944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames switch (instruction_set) { 440b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm 441b4536b7de576b20c74c612406c5d3132998075efVladimir Marko case kThumb2: 442b4536b7de576b20c74c612406c5d3132998075efVladimir Marko case kArm: { 443b4536b7de576b20c74c612406c5d3132998075efVladimir Marko arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats); 4444a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko arm::InstructionSimplifierArm* simplifier = 4454a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko new (arena) arm::InstructionSimplifierArm(graph, stats); 446b4536b7de576b20c74c612406c5d3132998075efVladimir Marko HOptimization* arm_optimizations[] = { 4474a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko simplifier, 4486b5afdd144d2bb3bf994240797834b5666b2cf98Nicolas Geoffray fixups 449b4536b7de576b20c74c612406c5d3132998075efVladimir Marko }; 450b4536b7de576b20c74c612406c5d3132998075efVladimir Marko RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer); 451b4536b7de576b20c74c612406c5d3132998075efVladimir Marko break; 452b4536b7de576b20c74c612406c5d3132998075efVladimir Marko } 453b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif 45444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64 45544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames case kArm64: { 45644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames arm64::InstructionSimplifierArm64* simplifier = 45744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames new (arena) arm64::InstructionSimplifierArm64(graph, stats); 45844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 45944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch"); 46044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames HOptimization* arm64_optimizations[] = { 46144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames simplifier, 46244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames side_effects, 46344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames gvn 46444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames }; 46544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer); 46644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames break; 46744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames } 46844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif 4690616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86 4700616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell case kX86: { 471d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik x86::PcRelativeFixups* pc_relative_fixups = 472d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik new (arena) x86::PcRelativeFixups(graph, codegen, 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) { 489700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang { 490700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName, 491700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang pass_observer); 492700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang PrepareForRegisterAllocation(graph).Run(); 493700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang } 494d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray SsaLivenessAnalysis liveness(graph, codegen); 495d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 496d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer); 497d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray liveness.Analyze(); 498d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 499d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 500d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer); 501d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); 502d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 503d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 504d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 505e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph, 506dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko CodeGenerator* codegen, 507e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray CompilerDriver* driver, 508e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray OptimizingCompilerStats* stats, 509e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 5104833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil PassObserver* pass_observer, 5114833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil StackHandleScopeCollection* handles) { 512a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko ArenaAllocator* arena = graph->GetArena(); 513a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination( 514a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName); 515a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination( 516a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName); 517e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HConstantFolding* fold1 = new (arena) HConstantFolding(graph); 518a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats); 51938e9e8046ea2196284bdb4638771c31108a30a4aJean-Philippe Halimi HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, stats); 520e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining"); 5214a34277c55279ba57ab361f7580db846a201d9b1Aart Bik HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce"); 522a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph); 523a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects); 52438e9e8046ea2196284bdb4638771c31108a30a4aJean-Philippe Halimi LICM* licm = new (arena) LICM(graph, *side_effects, stats); 5258df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects); 52622af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph); 5274a34277c55279ba57ab361f7580db846a201d9b1Aart Bik BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction); 528dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver); 529a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier( 53068289a531484d26214e09f1eadd9833531a3bc3cAlex Light graph, stats, "instruction_simplifier_after_bce"); 5314833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier( 532b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray graph, stats, "instruction_simplifier_before_codegen"); 53338e9e8046ea2196284bdb4638771c31108a30a4aJean-Philippe Halimi IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver, stats); 53471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe 535ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle HOptimization* optimizations1[] = { 536a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko intrinsics, 5374833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil sharpening, 538a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko fold1, 539a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko simplify1, 54018e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray dce1, 541ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle }; 542ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer); 543ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle 5444833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles); 545951779839f0d35ed5336f399c8f521fd9a6b7c27David Brazdil 5468993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil HOptimization* optimizations2[] = { 54774eb1b264691c4eb399d0858015a7fc13c476ac6David Brazdil // SelectGenerator depends on the InstructionSimplifier removing 5488993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // redundant suspend checks to recognize empty blocks. 54974eb1b264691c4eb399d0858015a7fc13c476ac6David Brazdil select_generator, 5508993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil fold2, // TODO: if we don't inline we can also skip fold2. 5518993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil side_effects, 5528993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil gvn, 5538993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil licm, 5548993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil induction, 5558993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil bce, 5568993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil fold3, // evaluates code generated by dynamic bce 5574833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil simplify2, 5588993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil lse, 5598993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil dce2, 5608993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // The codegen has a few assumptions that only the instruction simplifier 5618993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // can satisfy. For example, the code generator does not expect to see a 5628993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil // HTypeConversion from a type to the same type. 5634833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil simplify3, 5648993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil }; 5658993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer); 56639fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil 567d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, stats, pass_observer); 568d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray AllocateRegisters(graph, codegen, pass_observer); 5695e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray} 5705e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 571581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) { 572581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter()); 573581550137ee3a068a14224870e71aeee924a0646Vladimir Marko codegen->EmitLinkerPatches(&linker_patches); 574581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 575581550137ee3a068a14224870e71aeee924a0646Vladimir Marko // Sort patches by literal offset. Required for .oat_patches encoding. 576581550137ee3a068a14224870e71aeee924a0646Vladimir Marko std::sort(linker_patches.begin(), linker_patches.end(), 577581550137ee3a068a14224870e71aeee924a0646Vladimir Marko [](const LinkerPatch& lhs, const LinkerPatch& rhs) { 578581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return lhs.LiteralOffset() < rhs.LiteralOffset(); 579581550137ee3a068a14224870e71aeee924a0646Vladimir Marko }); 580581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 581581550137ee3a068a14224870e71aeee924a0646Vladimir Marko return linker_patches; 582581550137ee3a068a14224870e71aeee924a0646Vladimir Marko} 583581550137ee3a068a14224870e71aeee924a0646Vladimir Marko 58458282f4510961317b8d5a364a6f740a78926716fDavid BrazdilCompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena, 58558282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeVectorAllocator* code_allocator, 58658282f4510961317b8d5a364a6f740a78926716fDavid Brazdil CodeGenerator* codegen, 587b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray CompilerDriver* compiler_driver, 588b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray const DexFile::CodeItem* code_item) const { 589581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen); 590f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps)); 591d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map.resize(codegen->ComputeStackMapsSize()); 592b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), *code_item); 59312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 594eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod( 59512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray compiler_driver, 59612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetInstructionSet(), 597d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArrayRef<const uint8_t>(code_allocator->GetMemory()), 598aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 599aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 600aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 601aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 60212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetCoreSpillMask(), 603d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray codegen->GetFpuSpillMask(), 604f71b3ade9c99ce2fec2f5049ce9c5968721e1b81David Srbecky ArrayRef<const SrcMapElem>(), 605c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // mapping_table. 606c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(stack_map), 607c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(), // native_gc_map. 608c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()), 609581550137ee3a068a14224870e71aeee924a0646Vladimir Marko ArrayRef<const LinkerPatch>(linker_patches)); 610ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier 611eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames return compiled_method; 61212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 61312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 614d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena, 615d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator* code_allocator, 616d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item, 617d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t access_flags, 618d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray InvokeType invoke_type, 619d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint16_t class_def_idx, 620d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint32_t method_idx, 621d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject class_loader, 622d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile& dex_file, 623b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray Handle<mirror::DexCache> dex_cache, 624b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray bool osr) const { 6252be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kAttemptCompilation); 626cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 627cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle InstructionSet instruction_set = compiler_driver->GetInstructionSet(); 628cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle 6293b359c71f2fb784589be113206932e76807787bbRoland Levillain // Always use the Thumb-2 assembler: some runtime functionality 6303b359c71f2fb784589be113206932e76807787bbRoland Levillain // (like implicit stack overflow checks) assume Thumb-2. 6318d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray if (instruction_set == kArm) { 6328d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray instruction_set = kThumb2; 6338fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6348fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 6358fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray // Do not attempt to compile on architectures we do not support. 6361ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray if (!IsInstructionSetSupported(instruction_set)) { 6372be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); 6388fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray return nullptr; 6398fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 6408fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 6410d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // When read barriers are enabled, do not attempt to compile for 6420d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // instruction sets that have no read barrier support. 6430d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) { 6440d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain return nullptr; 6450d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain } 6460d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain 647b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 6482be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological); 649b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray return nullptr; 650b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 651b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray 65236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray // Implementation of the space filter: do not compile a code item whose size in 653432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray // code units is bigger than 128. 654432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray static constexpr size_t kSpaceFilterOptimizingThreshold = 128; 65536540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions(); 65636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace) 65736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) { 6582be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter); 65936540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray return nullptr; 66036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray } 66136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray 66292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 6639523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item, 66472d32629303f8f39362a4099481f48646aed042fIan Rogers class_def_idx, method_idx, access_flags, 6655b82d339955d1a0dc23eeb8d2d5659459ff987baNicolas Geoffray nullptr, dex_cache); 66692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 6673cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle bool requires_barrier = dex_compilation_unit.IsConstructor() 6683cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle && compiler_driver->RequiresConstructorBarrier(Thread::Current(), 6693cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetDexFile(), 6703cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle dex_compilation_unit.GetClassDefIndex()); 671badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil 672d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray HGraph* graph = new (arena) HGraph( 673b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray arena, 674b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray dex_file, 675b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray method_idx, 676b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray requires_barrier, 677b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray compiler_driver->GetInstructionSet(), 678b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray kInvalidInvokeType, 679b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray compiler_driver->GetCompilerOptions().GetDebuggable(), 680b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray osr); 681e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 682badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil const uint8_t* interpreter_metadata = nullptr; 683badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil { 684badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil ScopedObjectAccess soa(Thread::Current()); 685badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil StackHandleScope<1> hs(soa.Self()); 686badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil Handle<mirror::ClassLoader> loader(hs.NewHandle( 687badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil soa.Decode<mirror::ClassLoader*>(class_loader))); 688badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil ArtMethod* art_method = compiler_driver->ResolveMethod( 689badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type); 690badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil // We may not get a method, for example if its class is erroneous. 691badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil if (art_method != nullptr) { 692badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil graph->SetArtMethod(art_method); 693badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil interpreter_metadata = art_method->GetQuickenedInfo(); 694badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil } 695badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil } 696badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil 69712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 698cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CodeGenerator::Create(graph, 699cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle instruction_set, 700cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle *compiler_driver->GetInstructionSetFeatures(), 701cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle compiler_driver->GetCompilerOptions())); 70212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray if (codegen.get() == nullptr) { 7032be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen); 704787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 705787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 706c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky codegen->GetAssembler()->cfi().SetEnabled( 7075b1c2ca30dad519be285f0a1e839c23cc4e3a51dDavid Srbecky compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo()); 708787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 70969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil PassObserver pass_observer(graph, 71069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil codegen.get(), 71169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil visualizer_output_.get(), 71269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil compiler_driver); 7135e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 7140d1caa5df8d99320036888600190337bbe540731Vladimir Marko VLOG(compiler) << "Building " << pass_observer.GetMethodName(); 7155e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 716809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 717badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil ScopedObjectAccess soa(Thread::Current()); 718badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil StackHandleScopeCollection handles(soa.Self()); 719badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil // Do not hold `mutator_lock_` between optimizations. 720badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil ScopedThreadSuspension sts(soa.Self(), kNative); 721badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil 722badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil { 723badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer); 724badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil HGraphBuilder builder(graph, 725badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil &dex_compilation_unit, 726badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil &dex_compilation_unit, 727badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil &dex_file, 728badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil compiler_driver, 729badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil compilation_stats_.get(), 730badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil interpreter_metadata, 731badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil dex_cache); 732badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil GraphAnalysisResult result = builder.BuildGraph(*code_item, &handles); 733badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil if (result != kAnalysisSuccess) { 734badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil switch (result) { 735badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil case kAnalysisInvalidBytecode: 736badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil break; 737badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil case kAnalysisFailThrowCatchLoop: 738badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop); 739badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil break; 740badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil case kAnalysisFailAmbiguousArrayOp: 741badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp); 742badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil break; 743badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil case kAnalysisSuccess: 744badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil UNREACHABLE(); 745badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil } 746badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil pass_observer.SetGraphInBadState(); 747badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil return nullptr; 748badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil } 749809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 750a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 751badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil RunOptimizations(graph, 752badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil codegen.get(), 753badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil compiler_driver, 754badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil compilation_stats_.get(), 755badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil dex_compilation_unit, 756badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil &pass_observer, 757badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil &handles); 7584833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil 759badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil codegen->Compile(code_allocator); 760badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil pass_observer.DumpDisassembly(); 761216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray } 76258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil 763f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko if (kArenaAllocatorCountAllocations) { 764d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (arena->BytesAllocated() > 4 * MB) { 765d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MemStats mem_stats(arena->GetMemStats()); 766f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats); 767f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 768f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko } 769f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko 770d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return codegen.release(); 771216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray} 772216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray 77307380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) { 77407380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved). 77507380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS 77607380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS 77707380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD 77807380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD; 779175dc732c80e6f2afd83209348124df349290ba8Calin Juravle return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0; 780175dc732c80e6f2afd83209348124df349290ba8Calin Juravle} 781175dc732c80e6f2afd83209348124df349290ba8Calin Juravle 782216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 783216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t access_flags, 784216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray InvokeType invoke_type, 785216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint16_t class_def_idx, 786216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray uint32_t method_idx, 787f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle jobject jclass_loader, 788736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier const DexFile& dex_file, 789736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier Handle<mirror::DexCache> dex_cache) const { 790f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 791f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle CompiledMethod* method = nullptr; 792d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(Runtime::Current()->IsAotCompiler()); 793d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx); 794d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray DCHECK(!verified_method->HasRuntimeThrow()); 795d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file) 796d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray || CanHandleVerificationFailure(verified_method)) { 797d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetArenaPool()); 798d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 799d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 800d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 801d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 802d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 803d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 804d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 805d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 806d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 807d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 808d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray dex_file, 809b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray dex_cache, 810b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray /* osr */ false)); 811d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() != nullptr) { 812ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle MaybeRecordStat(MethodCompilationStat::kCompiled); 813b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item); 814f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 8150c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray } else { 816d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) { 817d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime); 818d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } else { 819ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError); 820d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 821f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle } 822f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle 823cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle if (kIsDebugBuild && 824cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle IsCompilingWithCoreImage() && 8250d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain IsInstructionSetSupported(compiler_driver->GetInstructionSet()) && 8260d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain (!kEmitCompilerReadBarrier || 8270d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) { 8280d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // For testing purposes, we put a special marker on method names 8290d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // that should be compiled with this compiler (when the the 8300d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // instruction set is supported -- and has support for read 8310d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // barriers, if they are enabled). This makes sure we're not 8320d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain // regressing. 83309b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle std::string method_name = PrettyMethod(method_idx, dex_file); 83409b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle bool shouldCompile = method_name.find("$opt$") != std::string::npos; 83509b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name; 83609b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle } 83709b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle 83812be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray return method; 839b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 840b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 84153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) { 84253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return new OptimizingCompiler(driver); 84353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 84453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 845335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() { 846335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray const std::string& image = Runtime::Current()->GetImageLocation(); 847dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao // TODO: This is under-approximating... 848dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) { 849dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao return true; 850dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao } 851dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao return false; 852335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray} 853335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray 854d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraybool OptimizingCompiler::JitCompile(Thread* self, 855d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jit::JitCodeCache* code_cache, 856b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray ArtMethod* method, 857b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray bool osr) { 858d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray StackHandleScope<2> hs(self); 859d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::ClassLoader> class_loader(hs.NewHandle( 860d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method->GetDeclaringClass()->GetClassLoader())); 861d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache())); 862d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 863d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jobject jclass_loader = class_loader.ToJObject(); 864d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile* dex_file = method->GetDexFile(); 865d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint16_t class_def_idx = method->GetClassDefIndex(); 866d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset()); 867d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t method_idx = method->GetDexMethodIndex(); 868d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const uint32_t access_flags = method->GetAccessFlags(); 869d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const InvokeType invoke_type = method->GetInvokeType(); 870d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 87125e0456b6ea13eba290b63ea88b6b7120ed89413Nicolas Geoffray ArenaAllocator arena(Runtime::Current()->GetJitArenaPool()); 872d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray CodeVectorAllocator code_allocator(&arena); 873d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray std::unique_ptr<CodeGenerator> codegen; 874d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray { 875d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray // Go to native so that we don't block GC during compilation. 876d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray ScopedThreadSuspension sts(self, kNative); 877d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen.reset( 878d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray TryCompile(&arena, 879d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray &code_allocator, 880d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_item, 881d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray access_flags, 882d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray invoke_type, 883d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray class_def_idx, 884d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method_idx, 885d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray jclass_loader, 886d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray *dex_file, 887b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray dex_cache, 888b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray osr)); 889d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (codegen.get() == nullptr) { 890d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 891d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 892d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 893d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 894d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray size_t stack_map_size = codegen->ComputeStackMapsSize(); 895d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size); 896d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (stack_map_data == nullptr) { 897d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 898d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 89973be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray MaybeRecordStat(MethodCompilationStat::kCompiled); 900b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), *code_item); 901d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray const void* code = code_cache->CommitCode( 902d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray self, 903d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray method, 904d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 905d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray stack_map_data, 906d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray nullptr, 907d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 908d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetCoreSpillMask(), 909d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray codegen->GetFpuSpillMask(), 910d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_allocator.GetMemory().data(), 911b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray code_allocator.GetSize(), 912b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray osr); 913d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 914d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray if (code == nullptr) { 915d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray code_cache->ClearData(self, stack_map_data); 916d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return false; 917d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray } 918d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 91991cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions(); 92091cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky if (compiler_options.GetGenerateDebugInfo()) { 9215cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code); 9225cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode()); 92309c2a6be63337ee060e2d54bd01cf18be7301d29David Srbecky debug::MethodDebugInfo info = debug::MethodDebugInfo(); 92409c2a6be63337ee060e2d54bd01cf18be7301d29David Srbecky info.trampoline_name = nullptr; 925197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.dex_file = dex_file; 926197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.class_def_index = class_def_idx; 927197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.dex_method_index = method_idx; 928197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.access_flags = access_flags; 929197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.code_item = code_item; 930197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.isa = codegen->GetInstructionSet(); 931197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.deduped = false; 932197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.is_native_debuggable = compiler_options.GetNativeDebuggable(); 933197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.is_optimized = true; 934197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.is_code_address_text_relative = false; 935197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.code_address = code_address; 936197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.code_size = code_allocator.GetSize(); 937197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.frame_size_in_bytes = method_header->GetFrameSizeInBytes(); 938197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.code_info = stack_map_size == 0 ? nullptr : stack_map_data; 939197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()); 940fe736b775c7774bb2f7072e022099ca47dd21a3cDavid Srbecky ArrayRef<const uint8_t> elf_file = debug::WriteDebugElfFileForMethods( 9415d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky GetCompilerDriver()->GetInstructionSet(), 9425d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky GetCompilerDriver()->GetInstructionSetFeatures(), 943fe736b775c7774bb2f7072e022099ca47dd21a3cDavid Srbecky ArrayRef<const debug::MethodDebugInfo>(&info, 1)); 9445cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky CreateJITCodeEntryForAddress(code_address, 9455cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky std::unique_ptr<const uint8_t[]>(elf_file.data()), 9465cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky elf_file.size()); 9475cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky } 9485cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky 949a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray Runtime::Current()->GetJit()->AddMemoryUsage(method, arena.BytesUsed()); 950a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray 951d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray return true; 952d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray} 953d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray 954b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 955