optimizing_compiler.cc revision e21dc3db191df04c100620965bee4617b3b24397
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 22f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h" 23787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h" 24787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h" 2553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h" 2675be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h" 2775be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h" 28787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h" 2992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h" 30e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h" 31f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h" 32d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h" 33e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h" 343c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h" 35e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h" 36e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "mirror/art_method-inl.h" 37787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h" 3826a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h" 39a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h" 403159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h" 417dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h" 42804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h" 43787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h" 44b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 45b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 46b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 48787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 49787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 50787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 51787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 5288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray CodeVectorAllocator() {} 53787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 54787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 55787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 5692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 57787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 58787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 59787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 6192cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray const std::vector<uint8_t>& GetMemory() const { return memory_; } 62787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 63787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 64787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> memory_; 65787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 66787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 67787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 68787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 69787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 70f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/** 71f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will 72ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped. 73f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */ 74f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic const char* kStringFilter = ""; 75f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 7653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler { 7753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public: 7853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe explicit OptimizingCompiler(CompilerDriver* driver); 7988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray ~OptimizingCompiler(); 8053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 8153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const 8253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OVERRIDE; 8353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 8453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* Compile(const DexFile::CodeItem* code_item, 8553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 8653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 8753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 8853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 8953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 9053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const OVERRIDE; 9153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 9253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* JniCompile(uint32_t access_flags, 9353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 9453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const OVERRIDE; 9553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 9653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE 9753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 9853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 9953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool WriteElf(art::File* file, 10053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OatWriter* oat_writer, 10153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::vector<const art::DexFile*>& dex_files, 10253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::string& android_root, 10353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 10453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 105e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED, 106e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE { 107e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return nullptr; 108e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray } 10953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 110e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {} 11153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 112ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil void Init() OVERRIDE; 11353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 114e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray void UnInit() const OVERRIDE {} 11553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 11653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private: 11788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // Whether we should run any optimization or register allocation. If false, will 11888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // just run the code generation after the graph was built. 11988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray const bool run_optimizations_; 12048c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 12148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle mutable OptimizingCompilerStats compilation_stats_; 12288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 12353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<std::ostream> visualizer_output_; 12453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 12553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 12653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}; 12753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 12888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 12988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 13088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 13188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray : Compiler(driver, kMaximumCompilationTimeBeforeWarning), 13288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray run_optimizations_( 13388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime), 134ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil compilation_stats_() {} 135ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil 136ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() { 137ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // Enable C1visualizer output. Must be done in Init() because the compiler 138ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil // driver is not fully initialized when passed to the compiler's constructor. 139ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CompilerDriver* driver = GetCompilerDriver(); 140ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil if (driver->GetDumpPasses()) { 141ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil CHECK_EQ(driver->GetThreadCount(), 1U) 142ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Graph visualizer requires the compiler to run single-threaded. " 143ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil << "Invoke the compiler with '-j1'."; 144f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray visualizer_output_.reset(new std::ofstream("art.cfg")); 145f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray } 146f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray} 147787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 14888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() { 14948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.Log(); 15088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray} 15188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 152e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED, 153e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file ATTRIBUTE_UNUSED, 154e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray CompilationUnit* cu ATTRIBUTE_UNUSED) const { 155e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return true; 15653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 15753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 15853c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags, 15953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 16053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const { 161e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file); 16253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 16353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 16453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeuintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const { 1654179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize( 1664179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet()))); 16753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 16853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 16953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer, 17053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::vector<const art::DexFile*>& dex_files, 17153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::string& android_root, bool is_host) const { 172e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host, 173e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray *GetCompilerDriver()); 17453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 17553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 1761ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) { 1771ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray return instruction_set == kArm64 1781ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 1791ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86 1801ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86_64; 1811ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray} 1821ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray 183de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffraystatic bool CanOptimize(const DexFile::CodeItem& code_item) { 184de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray // TODO: We currently cannot optimize methods with try/catch. 185de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray return code_item.tries_size_ == 0; 186de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray} 187de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray 188e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph, 189e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray CompilerDriver* driver, 190e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray OptimizingCompilerStats* stats, 191e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray const DexCompilationUnit& dex_compilation_unit, 192e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray const HGraphVisualizer& visualizer) { 193e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray SsaRedundantPhiElimination redundant_phi(graph); 194e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray SsaDeadPhiElimination dead_phi(graph); 195e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray HDeadCodeElimination dce(graph); 196e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray HConstantFolding fold(graph); 197e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray InstructionSimplifier simplify1(graph); 198e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 199e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray HInliner inliner(graph, dex_compilation_unit, driver, stats); 200e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 201e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray GVNOptimization gvn(graph); 202f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang BoundsCheckElimination bce(graph); 203e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray InstructionSimplifier simplify2(graph); 2045e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 2053159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray HOptimization* optimizations[] = { 206e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &redundant_phi, 207e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &dead_phi, 208e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &dce, 209e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &fold, 210e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &simplify1, 211e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &inliner, 212e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &gvn, 213f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang &bce, 214e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &simplify2 2153159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray }; 2165e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 2175e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray for (size_t i = 0; i < arraysize(optimizations); ++i) { 2185e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray HOptimization* optimization = optimizations[i]; 219ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false); 2205e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray optimization->Run(); 221ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true); 2223159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray optimization->Check(); 2235e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray } 2245e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray} 2255e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray 226376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing 227376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them. 228e21dc3db191df04c100620965bee4617b3b24397Andreas Gampestatic ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) { 229376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t size = vector.size(); 230376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray size_t aligned_size = RoundUp(size, 4); 231376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray for (; size < aligned_size; ++size) { 232376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray vector.push_back(0); 233376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray } 234e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe return ArrayRef<const uint8_t>(vector); 235376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray} 236376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray 237e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 238e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray uint32_t access_flags, 239e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray InvokeType invoke_type, 240e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray uint16_t class_def_idx, 241e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray uint32_t method_idx, 242e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray jobject class_loader, 243e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray const DexFile& dex_file) const { 2446a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers UNUSED(invoke_type); 24548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation); 2468fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet(); 2478d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // Always use the thumb2 assembler: some runtime functionality (like implicit stack 2488d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // overflow checks) assume thumb2. 2498d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray if (instruction_set == kArm) { 2508d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray instruction_set = kThumb2; 2518fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 2528fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 2538fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray // Do not attempt to compile on architectures we do not support. 2541ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray if (!IsInstructionSetSupported(instruction_set)) { 25548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa); 2568fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray return nullptr; 2578fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 2588fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 259b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 26048c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological); 261b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray return nullptr; 262b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 263b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray 26492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 26592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, 26672d32629303f8f39362a4099481f48646aed042fIan Rogers class_def_idx, method_idx, access_flags, 26772d32629303f8f39362a4099481f48646aed042fIan Rogers GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx)); 26892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 269e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray std::string method_name = PrettyMethod(method_idx, dex_file); 270e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray 2718ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // For testing purposes, we put a special marker on method names that should be compiled 2728ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // with this compiler. This makes sure we're not regressing. 273e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray bool shouldCompile = method_name.find("$opt$") != std::string::npos; 274e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos; 2758ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray 276787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaPool pool; 277787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaAllocator arena(&pool); 27848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle HGraphBuilder builder(&arena, 27948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle &dex_compilation_unit, 280e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &dex_compilation_unit, 281e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray &dex_file, 282e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray GetCompilerDriver(), 28348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle &compilation_stats_); 284f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 285e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray VLOG(compiler) << "Building " << PrettyMethod(method_idx, dex_file); 286787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray HGraph* graph = builder.BuildGraph(*code_item); 287787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (graph == nullptr) { 2885667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu CHECK(!shouldCompile) << "Could not build graph in optimizing compiler"; 289787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 290787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 291787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 292787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); 293787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (codegen == nullptr) { 2945667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler"; 29548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen); 296787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 297787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 298787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 299a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray HGraphVisualizer visualizer( 300e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray visualizer_output_.get(), graph, kStringFilter, *codegen, method_name.c_str()); 301a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray visualizer.DumpGraph("builder"); 302a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 303787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeVectorAllocator allocator; 30486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 30548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle bool can_optimize = CanOptimize(*code_item); 30648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set); 30748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle if (run_optimizations_ && can_optimize && can_allocate_registers) { 30843a539f780af20a1854bca81c3d4835a585f0620Nicolas Geoffray VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file); 309e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray if (!graph->TryBuildingSsa()) { 310e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray LOG(INFO) << "Skipping compilation of " 311e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray << PrettyMethod(method_idx, dex_file) 312e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray << ": it contains a non natural loop"; 313f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray // We could not transform the graph to SSA, bailout. 31448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA); 315f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray return nullptr; 316f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray } 317e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray RunOptimizations( 318e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray graph, GetCompilerDriver(), &compilation_stats_, dex_compilation_unit, visualizer); 31975be28332b278cff9039b54bfb228ac72f539cccRoland Levillain 32026a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray PrepareForRegisterAllocation(graph).Run(); 32186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray SsaLivenessAnalysis liveness(*graph, codegen); 32286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray liveness.Analyze(); 32386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray visualizer.DumpGraph(kLivenessPassName); 32486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 32586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness); 32686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray register_allocator.AllocateRegisters(); 32786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 32886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray visualizer.DumpGraph(kRegisterAllocatorPassName); 32986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray codegen->CompileOptimized(&allocator); 3303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> stack_map; 3323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildStackMaps(&stack_map); 3333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 33448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized); 335e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe return CompiledMethod::SwapAllocCompiledMethodStackMap( 336e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe GetCompilerDriver(), 337e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe instruction_set, 338e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe ArrayRef<const uint8_t>(allocator.GetMemory()), 339e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe codegen->GetFrameSize(), 340e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe codegen->GetCoreSpillMask(), 341e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe 0, /* FPR spill mask, unused */ 342e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe ArrayRef<const uint8_t>(stack_map)); 34386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) { 34486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LOG(FATAL) << "Could not allocate registers in optimizing compiler"; 3455667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu UNREACHABLE(); 34686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 34743a539f780af20a1854bca81c3d4835a585f0620Nicolas Geoffray VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file); 34848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 34948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle if (!run_optimizations_) { 35048c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled); 35148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle } else if (!can_optimize) { 35248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch); 35348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle } else if (!can_allocate_registers) { 35448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator); 35548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle } 35648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle 35786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray codegen->CompileBaseline(&allocator); 35886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> mapping_table; 360e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe DefaultSrcMap src_mapping_table; 3613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildMappingTable(&mapping_table, 3623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ? 3633946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray &src_mapping_table : nullptr); 3643946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> vmap_table; 3653946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildVMapTable(&vmap_table); 3663946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> gc_map; 3673946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); 3683946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 36948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline); 370e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe return CompiledMethod::SwapAllocCompiledMethod(GetCompilerDriver(), 371e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe instruction_set, 372e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe ArrayRef<const uint8_t>(allocator.GetMemory()), 373e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe codegen->GetFrameSize(), 374e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe codegen->GetCoreSpillMask(), 375e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe 0, /* FPR spill mask, unused */ 376e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe &src_mapping_table, 377e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe AlignVectorSize(mapping_table), 378e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe AlignVectorSize(vmap_table), 379e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe AlignVectorSize(gc_map), 380e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe ArrayRef<const uint8_t>()); 3813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 382b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 383b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 38453c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) { 38553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return new OptimizingCompiler(driver); 38653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 38753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 388b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 389