optimizing_compiler.cc revision 6a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866f
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 22787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h" 23787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h" 2453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h" 2575be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h" 2675be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h" 27787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h" 2892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h" 29f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h" 30d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h" 313c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h" 32787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h" 3326a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h" 34a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h" 357dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h" 36804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h" 37787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h" 38b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 39b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art { 40b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 41787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/** 42787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector. 43787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */ 44787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator { 45787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public: 4688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray CodeVectorAllocator() {} 47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 48787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray virtual uint8_t* Allocate(size_t size) { 49787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_ = size; 5092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray memory_.resize(size); 51787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return &memory_[0]; 52787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 53787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 54787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t GetSize() const { return size_; } 5592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray const std::vector<uint8_t>& GetMemory() const { return memory_; } 56787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 57787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private: 58787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray std::vector<uint8_t> memory_; 59787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray size_t size_; 60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 61787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator); 62787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray}; 63787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 64f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/** 65f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * If set to true, generates a file suitable for the c1visualizer tool and IRHydra. 66f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */ 67f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic bool kIsVisualizerEnabled = false; 68f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 69f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/** 70f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will 71f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * be in the file. 72f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */ 73f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic const char* kStringFilter = ""; 74f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 7553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler { 7653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public: 7753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe explicit OptimizingCompiler(CompilerDriver* driver); 7888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray ~OptimizingCompiler(); 7953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 8053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const 8153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OVERRIDE; 8253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 8353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* Compile(const DexFile::CodeItem* code_item, 8453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 8553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 8653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 8753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 8853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 8953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const OVERRIDE; 9053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 9153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* TryCompile(const DexFile::CodeItem* code_item, 9253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 9353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 9453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 9553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 9653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 9753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const; 9853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 9953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe // For the following methods we will use the fallback. This is a delegation pattern. 10053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* JniCompile(uint32_t access_flags, 10153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 10253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const OVERRIDE; 10353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 10453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE 10553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 10653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 10753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool WriteElf(art::File* file, 10853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe OatWriter* oat_writer, 10953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::vector<const art::DexFile*>& dex_files, 11053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::string& android_root, 11153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_); 11253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 11353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const OVERRIDE; 11453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 11553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE; 11653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 11753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe void Init() const OVERRIDE; 11853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 11953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe void UnInit() const OVERRIDE; 12053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 12153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private: 12288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // Whether we should run any optimization or register allocation. If false, will 12388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray // just run the code generation after the graph was built. 12488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray const bool run_optimizations_; 12588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray mutable AtomicInteger total_compiled_methods_; 12688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray mutable AtomicInteger unoptimized_compiled_methods_; 12788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray mutable AtomicInteger optimized_compiled_methods_; 12888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 12953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<std::ostream> visualizer_output_; 13053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 13153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe // Delegate to another compiler in case the optimizing compiler cannot compile a method. 13253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe // Currently the fallback is the quick compiler. 13353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe std::unique_ptr<Compiler> delegate_; 13453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 13553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler); 13653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}; 13753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 13888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */ 13988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 14088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver) 14188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray : Compiler(driver, kMaximumCompilationTimeBeforeWarning), 14288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray run_optimizations_( 14388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime), 14488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray total_compiled_methods_(0), 14588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray unoptimized_compiled_methods_(0), 14688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray optimized_compiled_methods_(0), 14788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray delegate_(Create(driver, Compiler::Kind::kQuick)) { 148f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray if (kIsVisualizerEnabled) { 149f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray visualizer_output_.reset(new std::ofstream("art.cfg")); 150f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray } 151f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray} 152787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 15353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampevoid OptimizingCompiler::Init() const { 15453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe delegate_->Init(); 15553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 15653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 15753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampevoid OptimizingCompiler::UnInit() const { 15853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe delegate_->UnInit(); 15953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 16053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 16188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() { 162ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray if (total_compiled_methods_ == 0) { 163ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray LOG(INFO) << "Did not compile any method."; 164ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray } else { 165ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray size_t unoptimized_percent = (unoptimized_compiled_methods_ * 100 / total_compiled_methods_); 166ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray size_t optimized_percent = (optimized_compiled_methods_ * 100 / total_compiled_methods_); 167ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray LOG(INFO) << "Compiled " << total_compiled_methods_ << " methods: " 168ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray << unoptimized_percent << "% (" << unoptimized_compiled_methods_ << ") unoptimized, " 169ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray << optimized_percent << "% (" << optimized_compiled_methods_ << ") optimized."; 170ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray } 17188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray} 17288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray 17353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, 17453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompilationUnit* cu) const { 17553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return delegate_->CanCompileMethod(method_idx, dex_file, cu); 17653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 17753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 17853c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags, 17953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 18053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const { 18153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return delegate_->JniCompile(access_flags, method_idx, dex_file); 18253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 18353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 18453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeuintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const { 18553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return delegate_->GetEntryPointOf(method); 18653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 18753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 18853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer, 18953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::vector<const art::DexFile*>& dex_files, 19053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const std::string& android_root, bool is_host) const { 19153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return delegate_->WriteElf(file, oat_writer, dex_files, android_root, is_host); 19253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 19353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 19453c913bb71b218714823c8c87a1f92830c336f61Andreas GampeBackend* OptimizingCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const { 19553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return delegate_->GetCodeGenerator(cu, compilation_unit); 19653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 19753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 19853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampevoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const { 19953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe delegate_->InitCompilationUnit(cu); 20053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 20153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 2021ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) { 2031ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray return instruction_set == kArm64 2041ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat) 2051ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86 2061ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray || instruction_set == kX86_64; 2071ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray} 2081ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray 20972d32629303f8f39362a4099481f48646aed042fIan RogersCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item, 210b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t access_flags, 211b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray InvokeType invoke_type, 212b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint16_t class_def_idx, 213b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray uint32_t method_idx, 214b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray jobject class_loader, 215b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray const DexFile& dex_file) const { 2166a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers UNUSED(invoke_type); 21788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray total_compiled_methods_++; 2188fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet(); 2198d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // Always use the thumb2 assembler: some runtime functionality (like implicit stack 2208d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray // overflow checks) assume thumb2. 2218d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray if (instruction_set == kArm) { 2228d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray instruction_set = kThumb2; 2238fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 2248fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 2258fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray // Do not attempt to compile on architectures we do not support. 2261ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray if (!IsInstructionSetSupported(instruction_set)) { 2278fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray return nullptr; 2288fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray } 2298fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray 230b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) { 231b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray return nullptr; 232b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray } 233b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray 23492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray DexCompilationUnit dex_compilation_unit( 23592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item, 23672d32629303f8f39362a4099481f48646aed042fIan Rogers class_def_idx, method_idx, access_flags, 23772d32629303f8f39362a4099481f48646aed042fIan Rogers GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx)); 23892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray 2398ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // For testing purposes, we put a special marker on method names that should be compiled 2408ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray // with this compiler. This makes sure we're not regressing. 2418ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos; 24286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray bool shouldOptimize = 24386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos; 2448ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray 245787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaPool pool; 246787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray ArenaAllocator arena(&pool); 247e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver()); 248f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray 249787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray HGraph* graph = builder.BuildGraph(*code_item); 250787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (graph == nullptr) { 2515667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu CHECK(!shouldCompile) << "Could not build graph in optimizing compiler"; 252787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 253787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 254787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 255787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set); 256787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray if (codegen == nullptr) { 2575667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler"; 258787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray return nullptr; 259787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray } 260787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 261a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray HGraphVisualizer visualizer( 262a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit); 263a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray visualizer.DumpGraph("builder"); 264a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray 265787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray CodeVectorAllocator allocator; 26686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 26788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray if (run_optimizations_ && RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) { 26888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray optimized_compiled_methods_++; 26986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray graph->BuildDominatorTree(); 27086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray graph->TransformToSSA(); 27186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray visualizer.DumpGraph("ssa"); 27286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray graph->FindNaturalLoops(); 2737dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray 27475be28332b278cff9039b54bfb228ac72f539cccRoland Levillain HDeadCodeElimination(graph, visualizer).Execute(); 27575be28332b278cff9039b54bfb228ac72f539cccRoland Levillain HConstantFolding(graph, visualizer).Execute(); 27675be28332b278cff9039b54bfb228ac72f539cccRoland Levillain 2777dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray SsaRedundantPhiElimination(graph).Run(); 2787dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray SsaDeadPhiElimination(graph).Run(); 2793c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray InstructionSimplifier(graph).Run(); 280d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray GlobalValueNumberer(graph->GetArena(), graph).Run(); 281d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray visualizer.DumpGraph(kGVNPassName); 28226a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray PrepareForRegisterAllocation(graph).Run(); 2837dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray 28486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray SsaLivenessAnalysis liveness(*graph, codegen); 28586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray liveness.Analyze(); 28686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray visualizer.DumpGraph(kLivenessPassName); 28786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 28886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness); 28986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray register_allocator.AllocateRegisters(); 29086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 29186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray visualizer.DumpGraph(kRegisterAllocatorPassName); 29286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray codegen->CompileOptimized(&allocator); 2933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 2943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> mapping_table; 2953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray SrcMap src_mapping_table; 2963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildMappingTable(&mapping_table, 2973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ? 2983946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray &src_mapping_table : nullptr); 2993946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> stack_map; 3013946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildStackMaps(&stack_map); 3023946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3033946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray return new CompiledMethod(GetCompilerDriver(), 3043946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray instruction_set, 3053946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray allocator.GetMemory(), 3063946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->GetFrameSize(), 3073946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->GetCoreSpillMask(), 3083946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 0, /* FPR spill mask, unused */ 3093946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray mapping_table, 3103946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray stack_map); 31186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) { 31286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray LOG(FATAL) << "Could not allocate registers in optimizing compiler"; 3135667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu UNREACHABLE(); 31486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray } else { 31588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray unoptimized_compiled_methods_++; 31686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray codegen->CompileBaseline(&allocator); 31786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray 31886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray // Run these phases to get some test coverage. 31986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray graph->BuildDominatorTree(); 32086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray graph->TransformToSSA(); 32186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray visualizer.DumpGraph("ssa"); 32286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray graph->FindNaturalLoops(); 323d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray SsaRedundantPhiElimination(graph).Run(); 324d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray SsaDeadPhiElimination(graph).Run(); 325d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray GlobalValueNumberer(graph->GetArena(), graph).Run(); 32686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray SsaLivenessAnalysis liveness(*graph, codegen); 32786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray liveness.Analyze(); 32886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray visualizer.DumpGraph(kLivenessPassName); 329787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray 3303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> mapping_table; 3313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray SrcMap src_mapping_table; 3323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildMappingTable(&mapping_table, 3333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ? 3343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray &src_mapping_table : nullptr); 3353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> vmap_table; 3363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildVMapTable(&vmap_table); 3373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray std::vector<uint8_t> gc_map; 3383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit); 3393946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 3403946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray return new CompiledMethod(GetCompilerDriver(), 3413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray instruction_set, 3423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray allocator.GetMemory(), 3433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->GetFrameSize(), 3443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray codegen->GetCoreSpillMask(), 3453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray 0, /* FPR spill mask, unused */ 3463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray &src_mapping_table, 3473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray mapping_table, 3483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray vmap_table, 3493946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray gc_map, 3503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray nullptr); 3513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray } 352b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} 353b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray 35453c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item, 35553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t access_flags, 35653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe InvokeType invoke_type, 35753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint16_t class_def_idx, 35853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe uint32_t method_idx, 35953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe jobject class_loader, 36053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe const DexFile& dex_file) const { 36153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx, 36253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe method_idx, class_loader, dex_file); 36353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe if (method != nullptr) { 36453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return method; 36553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe } 36653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 36753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx, 36853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe class_loader, dex_file); 36953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 37053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 37153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) { 37253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe return new OptimizingCompiler(driver); 37353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe} 37453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe 375b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray} // namespace art 376