optimizing_compiler.cc revision acf735c13998ad2a175f5a17e7bfce220073279d
1b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray/* 2b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project 3b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 4b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License"); 5b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * you may not use this file except in compliance with the License. 6b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * You may obtain a copy of the License at 7b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 8b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * http://www.apache.org/licenses/LICENSE-2.0 9b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * 10b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software 11b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS, 12b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * See the License for the specific language governing permissions and 14b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * limitations under the License. 15b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray */ 16b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 1753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "optimizing_compiler.h" 1853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 19f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include <fstream> 20787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include <stdint.h> 21787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h" 235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h" 24f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h" 25787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h" 26787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h" 2753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h" 2875be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h" 2975be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h" 3071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h" 31787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h" 3292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h" 33e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h" 34f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h" 35d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h" 36e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h" 373c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h" 3871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h" 3982091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h" 40e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h" 41e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "mirror/art_method-inl.h" 42787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h" 4326a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h" 44a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h" 45827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h" 463159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h" 477dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h" 48804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h" 4910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle#include "reference_type_propagation.h" 50787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h" 51b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 52b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 53b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 54787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 55787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 56787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 57787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 58787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 5988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray CodeVectorAllocator() {} 60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 61787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 62787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 6392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 64787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 65787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 66787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 67787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 6892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray const std::vector<uint8_t>& GetMemory() const { return memory_; } 69787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 70787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> memory_; 72787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 74787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 75787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 76787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 77f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/** 78f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will 79ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped. 80f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */ 81f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic const char* kStringFilter = ""; 82f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 83809658ec5c5515ae9eb87d32012ec5e088612949David Brazdilclass PassInfo; 84809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 855e8b137d28c840b128e2488f954cccee3e86db14David Brazdilclass PassInfoPrinter : public ValueObject { 865e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public: 875e8b137d28c840b128e2488f954cccee3e86db14David Brazdil PassInfoPrinter(HGraph* graph, 885e8b137d28c840b128e2488f954cccee3e86db14David Brazdil const char* method_name, 895e8b137d28c840b128e2488f954cccee3e86db14David Brazdil const CodeGenerator& codegen, 905e8b137d28c840b128e2488f954cccee3e86db14David Brazdil std::ostream* visualizer_output, 91809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil CompilerDriver* compiler_driver) 925e8b137d28c840b128e2488f954cccee3e86db14David Brazdil : method_name_(method_name), 93809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil timing_logger_enabled_(compiler_driver->GetDumpPasses()), 945e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_(method_name, true, true), 95809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()), 965e8b137d28c840b128e2488f954cccee3e86db14David Brazdil visualizer_(visualizer_output, graph, codegen, method_name_) { 975e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (strstr(method_name, kStringFilter) == nullptr) { 985e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_enabled_ = visualizer_enabled_ = false; 995e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1005e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1015e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 102809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil ~PassInfoPrinter() { 103809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (timing_logger_enabled_) { 104809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << "TIMINGS " << method_name_; 105809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << Dumpable<TimingLogger>(timing_logger_); 106809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 107809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 108809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 109809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 110809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void StartPass(const char* pass_name) { 1115e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Dump graph first, then start timer. 1125e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 1135e8b137d28c840b128e2488f954cccee3e86db14David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ false); 1145e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1155e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1165e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.StartTiming(pass_name); 1175e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1185e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1195e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 120809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil void EndPass(const char* pass_name) { 1215e8b137d28c840b128e2488f954cccee3e86db14David Brazdil // Pause timer first, then dump graph. 1225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (timing_logger_enabled_) { 1235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil timing_logger_.EndTiming(); 1245e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1255e8b137d28c840b128e2488f954cccee3e86db14David Brazdil if (visualizer_enabled_) { 1265e8b137d28c840b128e2488f954cccee3e86db14David Brazdil visualizer_.DumpGraph(pass_name, /* is_after_pass */ true); 1275e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 1295e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 1305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil const char* method_name_; 1315e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 1325e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool timing_logger_enabled_; 1335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil TimingLogger timing_logger_; 1345e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 1355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil bool visualizer_enabled_; 1365e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphVisualizer visualizer_; 1375e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 138809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil friend PassInfo; 139809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 1405e8b137d28c840b128e2488f954cccee3e86db14David Brazdil DISALLOW_COPY_AND_ASSIGN(PassInfoPrinter); 1415e8b137d28c840b128e2488f954cccee3e86db14David Brazdil}; 1425e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 143809658ec5c5515ae9eb87d32012ec5e088612949David Brazdilclass PassInfo : public ValueObject { 144809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public: 145809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfo(const char *pass_name, PassInfoPrinter* pass_info_printer) 146809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil : pass_name_(pass_name), 147809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil pass_info_printer_(pass_info_printer) { 148809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil pass_info_printer_->StartPass(pass_name_); 149809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 150809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 151809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil ~PassInfo() { 152809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil pass_info_printer_->EndPass(pass_name_); 153809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 154809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 155809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private: 156809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil const char* const pass_name_; 157809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfoPrinter* const pass_info_printer_; 158809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil}; 159809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil 16053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler { 16153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public: 16253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe explicit OptimizingCompiler(CompilerDriver* driver); 16388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray ~OptimizingCompiler(); 16453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 16553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const 16653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OVERRIDE; 16753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 16853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* Compile(const DexFile::CodeItem* code_item, 16953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 17053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 17153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 17253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 17353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 17453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const OVERRIDE; 17553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 17653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* JniCompile(uint32_t access_flags, 17753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 17853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const OVERRIDE; 17953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 18053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE 18153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 18253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 18353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool WriteElf(art::File* file, 18453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OatWriter* oat_writer, 18553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::vector<const art::DexFile*>& dex_files, 18653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::string& android_root, 18753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 18853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 189e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {} 19053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 191ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil void Init() OVERRIDE; 19253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 193e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray void UnInit() const OVERRIDE {} 19453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 19553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private: 19688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // Whether we should run any optimization or register allocation. If false, will 19788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // just run the code generation after the graph was built. 19888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray const bool run_optimizations_; 19948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 20012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray // Optimize and compile `graph`. 20112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CompiledMethod* CompileOptimized(HGraph* graph, 20212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CodeGenerator* codegen, 20312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CompilerDriver* driver, 204acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle const DexFile& dex_file, 20512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 2065e8b137d28c840b128e2488f954cccee3e86db14David Brazdil PassInfoPrinter* pass_info) const; 20712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 20812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray // Just compile without doing optimizations. 20912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CompiledMethod* CompileBaseline(CodeGenerator* codegen, 21012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CompilerDriver* driver, 21112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray const DexCompilationUnit& dex_compilation_unit) const; 21212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 21348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle mutable OptimizingCompilerStats compilation_stats_; 21488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 21553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<std::ostream> visualizer_output_; 21653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 21753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 21853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}; 21953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 22088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 22188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 22288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 22388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray : Compiler(driver, kMaximumCompilationTimeBeforeWarning), 22488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray run_optimizations_( 22588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime), 226ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil compilation_stats_() {} 227ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil 228ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() { 229ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // Enable C1visualizer output. Must be done in Init() because the compiler 230ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // driver is not fully initialized when passed to the compiler's constructor. 231ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CompilerDriver* driver = GetCompilerDriver(); 232866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil const std::string cfg_file_name = driver->GetDumpCfgFileName(); 233866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil if (!cfg_file_name.empty()) { 234ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CHECK_EQ(driver->GetThreadCount(), 1U) 235ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Graph visualizer requires the compiler to run single-threaded. " 236ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Invoke the compiler with '-j1'."; 237866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil visualizer_output_.reset(new std::ofstream(cfg_file_name)); 238f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray } 239f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray} 240787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 24188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() { 24248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.Log(); 24388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray} 24488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 245e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, 246e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file ATTRIBUTE_UNUSED, 247e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray CompilationUnit* cu ATTRIBUTE_UNUSED) const { 248e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return true; 24953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 25053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 25153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags, 25253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 25353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const { 254e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); 25553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 25653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 25753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeuintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const { 2584179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( 2594179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); 26053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 26153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 26253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer, 26353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::vector<const art::DexFile*>& dex_files, 26453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::string& android_root, bool is_host) const { 265e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host, 266e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray *GetCompilerDriver()); 26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 26853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 2691ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) { 2701ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray return instruction_set == kArm64 2711ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 2721ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86 2731ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86_64; 2741ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray} 2751ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray 276de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffraystatic bool CanOptimize(const DexFile::CodeItem& code_item) { 277de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray // TODO: We currently cannot optimize methods with try/catch. 278de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray return code_item.tries_size_ == 0; 279de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray} 280de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray 28110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[], 28210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle size_t length, 283809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfoPrinter* pass_info_printer) { 28410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle for (size_t i = 0; i < length; ++i) { 28510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle HOptimization* optimization = optimizations[i]; 286809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 287809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfo pass_info(optimization->GetPassName(), pass_info_printer); 288809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil optimization->Run(); 289809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 29010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle optimization->Check(); 29110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle } 29210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle} 29310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle 294e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph, 295e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray CompilerDriver* driver, 296e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray OptimizingCompilerStats* stats, 297acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle const DexFile& dex_file, 298e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 299acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle PassInfoPrinter* pass_info_printer, 300acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle StackHandleScopeCollection* handles) { 301e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray SsaRedundantPhiElimination redundant_phi(graph); 302e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray SsaDeadPhiElimination dead_phi(graph); 303e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray HDeadCodeElimination dce(graph); 3049ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray HConstantFolding fold1(graph); 305acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle InstructionSimplifier simplify1(graph, stats); 306e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 307e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray HInliner inliner(graph, dex_compilation_unit, driver, stats); 308e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 3099ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray HConstantFolding fold2(graph); 31086dde1658a1951c251dd5c6ff21ecc5c281879a6Nicolas Geoffray SideEffectsAnalysis side_effects(graph); 31186dde1658a1951c251dd5c6ff21ecc5c281879a6Nicolas Geoffray GVNOptimization gvn(graph, side_effects); 31282091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray LICM licm(graph, side_effects); 313f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang BoundsCheckElimination bce(graph); 314acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle ReferenceTypePropagation type_propagation(graph, dex_file, dex_compilation_unit, handles); 315acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle InstructionSimplifier simplify2(graph, stats, "instruction_simplifier_after_types"); 3165e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 31771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver); 31871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe 3193159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray HOptimization* optimizations[] = { 320e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &redundant_phi, 321e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &dead_phi, 32271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe &intrinsics, 323e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &dce, 3249ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray &fold1, 325e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &simplify1, 326e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &inliner, 3279ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray &fold2, 32886dde1658a1951c251dd5c6ff21ecc5c281879a6Nicolas Geoffray &side_effects, 329e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &gvn, 33082091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray &licm, 331f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang &bce, 33210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle &type_propagation, 333e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &simplify2 3343159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray }; 3355e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 336809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil RunOptimizations(optimizations, arraysize(optimizations), pass_info_printer); 3375e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray} 3385e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 339376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing 340376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them. 341e21dc3db191df04c100620965bee4617b3b24397Andreas Gampestatic ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) { 342376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t size = vector.size(); 343376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t aligned_size = RoundUp(size, 4); 344376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray for (; size < aligned_size; ++size) { 345376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray vector.push_back(0); 346376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray } 347e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe return ArrayRef<const uint8_t>(vector); 348376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray} 349376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray 35012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 35112df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph, 35212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CodeGenerator* codegen, 35312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CompilerDriver* compiler_driver, 354acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle const DexFile& dex_file, 35512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 356809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfoPrinter* pass_info_printer) const { 357acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle StackHandleScopeCollection handles(Thread::Current()); 358acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle RunOptimizations(graph, compiler_driver, &compilation_stats_, 359acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle dex_file, dex_compilation_unit, pass_info_printer, &handles); 36012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 36112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray PrepareForRegisterAllocation(graph).Run(); 36212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray SsaLivenessAnalysis liveness(*graph, codegen); 363809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 364809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfo pass_info(kLivenessPassName, pass_info_printer); 365809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil liveness.Analyze(); 366809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 367809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 368809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfo pass_info(kRegisterAllocatorPassName, pass_info_printer); 369809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters(); 370809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 37112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 37212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CodeVectorAllocator allocator; 37312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->CompileOptimized(&allocator); 37412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 37512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::vector<uint8_t> stack_map; 37612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->BuildStackMaps(&stack_map); 37712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 37812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized); 37912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 38012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray return CompiledMethod::SwapAllocCompiledMethodStackMap( 38112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray compiler_driver, 38212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetInstructionSet(), 38312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray ArrayRef<const uint8_t>(allocator.GetMemory()), 384aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 385aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 386aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 387aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 38812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->GetCoreSpillMask(), 389d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray codegen->GetFpuSpillMask(), 39012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray ArrayRef<const uint8_t>(stack_map)); 39112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 39212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 39312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 39412df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline( 39512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CodeGenerator* codegen, 39612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CompilerDriver* compiler_driver, 39712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray const DexCompilationUnit& dex_compilation_unit) const { 39812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray CodeVectorAllocator allocator; 39912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->CompileBaseline(&allocator); 40012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 40112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::vector<uint8_t> mapping_table; 40212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray DefaultSrcMap src_mapping_table; 40312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols(); 40412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr); 40512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::vector<uint8_t> vmap_table; 40612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->BuildVMapTable(&vmap_table); 40712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::vector<uint8_t> gc_map; 40812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); 40912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 41012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline); 411aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain return CompiledMethod::SwapAllocCompiledMethod( 412aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain compiler_driver, 413aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetInstructionSet(), 414aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain ArrayRef<const uint8_t>(allocator.GetMemory()), 415aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // Follow Quick's behavior and set the frame size to zero if it is 416aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // considered "empty" (see the definition of 417aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain // art::CodeGenerator::HasEmptyFrame). 418aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(), 419aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetCoreSpillMask(), 420aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain codegen->GetFpuSpillMask(), 421aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain &src_mapping_table, 422aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(mapping_table), 423aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(vmap_table), 424aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain AlignVectorSize(gc_map), 425aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain ArrayRef<const uint8_t>()); 42612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray} 42712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray 428e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 429e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray uint32_t access_flags, 430e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray InvokeType invoke_type, 431e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray uint16_t class_def_idx, 432e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray uint32_t method_idx, 433e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray jobject class_loader, 434e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file) const { 4356a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers UNUSED(invoke_type); 4365e8b137d28c840b128e2488f954cccee3e86db14David Brazdil std::string method_name = PrettyMethod(method_idx, dex_file); 43748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation); 438cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CompilerDriver* compiler_driver = GetCompilerDriver(); 439cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle InstructionSet instruction_set = compiler_driver->GetInstructionSet(); 4408d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // Always use the thumb2 assembler: some runtime functionality (like implicit stack 4418d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // overflow checks) assume thumb2. 4428d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray if (instruction_set == kArm) { 4438d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray instruction_set = kThumb2; 4448fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 4458fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 4468fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray // Do not attempt to compile on architectures we do not support. 4471ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray if (!IsInstructionSetSupported(instruction_set)) { 44848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); 4498fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray return nullptr; 4508fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 4518fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 452b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 45348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological); 454b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray return nullptr; 455b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 456b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray 45792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 45892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, 45972d32629303f8f39362a4099481f48646aed042fIan Rogers class_def_idx, method_idx, access_flags, 460cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle compiler_driver->GetVerifiedMethod(&dex_file, method_idx)); 46192cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 4625e8b137d28c840b128e2488f954cccee3e86db14David Brazdil ArenaPool pool; 4635e8b137d28c840b128e2488f954cccee3e86db14David Brazdil ArenaAllocator arena(&pool); 4645e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraph* graph = new (&arena) HGraph(&arena); 465e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 4668ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // For testing purposes, we put a special marker on method names that should be compiled 4678ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // with this compiler. This makes sure we're not regressing. 468e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray bool shouldCompile = method_name.find("$opt$") != std::string::npos; 469e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos; 4708ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray 47112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray std::unique_ptr<CodeGenerator> codegen( 472cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle CodeGenerator::Create(graph, 473cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle instruction_set, 474cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle *compiler_driver->GetInstructionSetFeatures(), 475cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle compiler_driver->GetCompilerOptions())); 47612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray if (codegen.get() == nullptr) { 4775667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler"; 47848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen); 479787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 480787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 481787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 482809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfoPrinter pass_info_printer(graph, 483809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil method_name.c_str(), 484809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil *codegen.get(), 485809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil visualizer_output_.get(), 486809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil compiler_driver); 4875e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 4885e8b137d28c840b128e2488f954cccee3e86db14David Brazdil HGraphBuilder builder(graph, 4895e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 4905e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_compilation_unit, 4915e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &dex_file, 4925e8b137d28c840b128e2488f954cccee3e86db14David Brazdil compiler_driver, 4935e8b137d28c840b128e2488f954cccee3e86db14David Brazdil &compilation_stats_); 4945e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 4955e8b137d28c840b128e2488f954cccee3e86db14David Brazdil VLOG(compiler) << "Building " << method_name; 4965e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 497809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 498809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfo pass_info(kBuilderPassName, &pass_info_printer); 499809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (!builder.BuildGraph(*code_item)) { 500809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil CHECK(!shouldCompile) << "Could not build graph in optimizing compiler"; 501809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil return nullptr; 502809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 5035e8b137d28c840b128e2488f954cccee3e86db14David Brazdil } 504a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 50548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle bool can_optimize = CanOptimize(*code_item); 50648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set); 50748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle if (run_optimizations_ && can_optimize && can_allocate_registers) { 5085e8b137d28c840b128e2488f954cccee3e86db14David Brazdil VLOG(compiler) << "Optimizing " << method_name; 5095e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 510809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil { 511809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil PassInfo pass_info(kSsaBuilderPassName, &pass_info_printer); 512809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil if (!graph->TryBuildingSsa()) { 513809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil // We could not transform the graph to SSA, bailout. 514809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop"; 515809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA); 516809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil return nullptr; 517809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil } 518f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray } 5195e8b137d28c840b128e2488f954cccee3e86db14David Brazdil 5205e8b137d28c840b128e2488f954cccee3e86db14David Brazdil return CompileOptimized(graph, 5215e8b137d28c840b128e2488f954cccee3e86db14David Brazdil codegen.get(), 5225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil compiler_driver, 523acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle dex_file, 5245e8b137d28c840b128e2488f954cccee3e86db14David Brazdil dex_compilation_unit, 525809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil &pass_info_printer); 52686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) { 52786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LOG(FATAL) << "Could not allocate registers in optimizing compiler"; 5285667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu UNREACHABLE(); 52986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 5305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil VLOG(compiler) << "Compile baseline " << method_name; 53148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 53248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle if (!run_optimizations_) { 53348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled); 53448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle } else if (!can_optimize) { 53548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch); 53648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle } else if (!can_allocate_registers) { 53748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator); 53848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle } 53948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 5405e8b137d28c840b128e2488f954cccee3e86db14David Brazdil return CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit); 5413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 542b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 543b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 54453c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) { 54553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return new OptimizingCompiler(driver); 54653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 54753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 548b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 549