optimizing_compiler.cc revision 26a25ef62a13f409f941aa39825a51b4d6f0f047
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"
25787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
2692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
27f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
28d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
293c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
30787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
3126a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
32a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
337dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
34804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
35787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h"
36b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
37b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
38b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
39787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
40787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
41787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
42787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
43787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
4488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  CodeVectorAllocator() {}
45787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
46787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
4892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
49787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
50787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
51787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
52787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
5392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  const std::vector<uint8_t>& GetMemory() const { return memory_; }
54787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
55787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
56787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  std::vector<uint8_t> memory_;
57787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
58787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
59787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
61787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
62f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
63f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
64f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
65f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic bool kIsVisualizerEnabled = false;
66f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
67f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
68f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
69f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * be in the file.
70f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
71f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic const char* kStringFilter = "";
72f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
7353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
7453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
7553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
7688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
7753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
7853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
7953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
8053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
8153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
8253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
8353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
8453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
8553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
8653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
8753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          const DexFile& dex_file) const OVERRIDE;
8853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
8953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
9053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t access_flags,
9153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             InvokeType invoke_type,
9253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint16_t class_def_idx,
9353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
9453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             jobject class_loader,
9553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             const DexFile& dex_file) const;
9653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
9753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  // For the following methods we will use the fallback. This is a delegation pattern.
9853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
9953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
10053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             const DexFile& dex_file) const OVERRIDE;
10153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
10253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
10353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
10453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
10553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool WriteElf(art::File* file,
10653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                OatWriter* oat_writer,
10753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                const std::vector<const art::DexFile*>& dex_files,
10853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                const std::string& android_root,
10953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
11053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
11153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  Backend* GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const OVERRIDE;
11253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
11353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
11453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
11553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  void Init() const OVERRIDE;
11653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
11753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  void UnInit() const OVERRIDE;
11853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
11953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
12088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
12188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
12288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
12388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  mutable AtomicInteger total_compiled_methods_;
12488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  mutable AtomicInteger unoptimized_compiled_methods_;
12588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  mutable AtomicInteger optimized_compiled_methods_;
12688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
12753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
12853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
12953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  // Delegate to another compiler in case the optimizing compiler cannot compile a method.
13053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  // Currently the fallback is the quick compiler.
13153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<Compiler> delegate_;
13253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
13353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
13453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
13553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
13688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
13788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
13888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
13988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
14088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
14188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray          driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
14288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      total_compiled_methods_(0),
14388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      unoptimized_compiled_methods_(0),
14488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      optimized_compiled_methods_(0),
14588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      delegate_(Create(driver, Compiler::Kind::kQuick)) {
146f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  if (kIsVisualizerEnabled) {
147f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray    visualizer_output_.reset(new std::ofstream("art.cfg"));
148f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
149f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
150787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
15153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampevoid OptimizingCompiler::Init() const {
15253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  delegate_->Init();
15353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
15453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
15553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampevoid OptimizingCompiler::UnInit() const {
15653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  delegate_->UnInit();
15753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
15853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
15988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
160ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray  if (total_compiled_methods_ == 0) {
161ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    LOG(INFO) << "Did not compile any method.";
162ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray  } else {
163ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    size_t unoptimized_percent = (unoptimized_compiled_methods_ * 100 / total_compiled_methods_);
164ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    size_t optimized_percent = (optimized_compiled_methods_ * 100 / total_compiled_methods_);
165ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    LOG(INFO) << "Compiled " << total_compiled_methods_ << " methods: "
166ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray              << unoptimized_percent << "% (" << unoptimized_compiled_methods_ << ") unoptimized, "
167ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray              << optimized_percent << "% (" << optimized_compiled_methods_ << ") optimized.";
168ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray  }
16988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
17088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
17153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
17253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                          CompilationUnit* cu) const {
17353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return delegate_->CanCompileMethod(method_idx, dex_file, cu);
17453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
17553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
17653c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
17753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                               uint32_t method_idx,
17853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                               const DexFile& dex_file) const {
17953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return delegate_->JniCompile(access_flags, method_idx, dex_file);
18053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
18153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
18253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeuintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
18353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return delegate_->GetEntryPointOf(method);
18453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
18553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
18653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
18753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                  const std::vector<const art::DexFile*>& dex_files,
18853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                  const std::string& android_root, bool is_host) const {
18953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return delegate_->WriteElf(file, oat_writer, dex_files, android_root, is_host);
19053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
19153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
19253c913bb71b218714823c8c87a1f92830c336f61Andreas GampeBackend* OptimizingCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) const {
19353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return delegate_->GetCodeGenerator(cu, compilation_unit);
19453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
19553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
19653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampevoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
19753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  delegate_->InitCompilationUnit(cu);
19853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
19953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
20072d32629303f8f39362a4099481f48646aed042fIan RogersCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
201b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               uint32_t access_flags,
202b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               InvokeType invoke_type,
203b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               uint16_t class_def_idx,
204b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               uint32_t method_idx,
205b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               jobject class_loader,
206b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray                                               const DexFile& dex_file) const {
20788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  total_compiled_methods_++;
2088fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
2098d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
2108d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
2118d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
2128d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
2138fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
2148fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
2158fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
2168d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set != kX86 && instruction_set != kX86_64 && instruction_set != kThumb2) {
2178fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
2188fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
2198fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
22092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
22192cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
22272d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
22372d32629303f8f39362a4099481f48646aed042fIan Rogers    GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
22492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
2258ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
2268ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
2278ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
22886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool shouldOptimize =
22986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
2308ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
231787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  ArenaPool pool;
232787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  ArenaAllocator arena(&pool);
233e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
234f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
235787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  HGraph* graph = builder.BuildGraph(*code_item);
236787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  if (graph == nullptr) {
2378ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray    if (shouldCompile) {
2388ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray      LOG(FATAL) << "Could not build graph in optimizing compiler";
2398ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray    }
240787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
241787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
242787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
243787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
244787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  if (codegen == nullptr) {
2458ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray    if (shouldCompile) {
2468ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray      LOG(FATAL) << "Could not find code generator for optimizing compiler";
2478ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray    }
248787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
249787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
250787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
251a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  HGraphVisualizer visualizer(
252a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
253a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  visualizer.DumpGraph("builder");
254a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
255787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  CodeVectorAllocator allocator;
25686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
25788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  if (run_optimizations_ && RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
25888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    optimized_compiled_methods_++;
25986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    graph->BuildDominatorTree();
26086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    graph->TransformToSSA();
26186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    visualizer.DumpGraph("ssa");
26286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    graph->FindNaturalLoops();
2637dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray
2647dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray    SsaRedundantPhiElimination(graph).Run();
2657dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray    SsaDeadPhiElimination(graph).Run();
2663c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray    InstructionSimplifier(graph).Run();
267d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray    GlobalValueNumberer(graph->GetArena(), graph).Run();
268d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray    visualizer.DumpGraph(kGVNPassName);
26926a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray    PrepareForRegisterAllocation(graph).Run();
2707dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray
27186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    SsaLivenessAnalysis liveness(*graph, codegen);
27286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    liveness.Analyze();
27386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    visualizer.DumpGraph(kLivenessPassName);
27486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
27586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
27686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    register_allocator.AllocateRegisters();
27786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
27886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    visualizer.DumpGraph(kRegisterAllocatorPassName);
27986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    codegen->CompileOptimized(&allocator);
2803946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2813946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> mapping_table;
2823946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    SrcMap src_mapping_table;
2833946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildMappingTable(&mapping_table,
2843946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
2853946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                 &src_mapping_table : nullptr);
2863946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2873946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> stack_map;
2883946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildStackMaps(&stack_map);
2893946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
2903946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    return new CompiledMethod(GetCompilerDriver(),
2913946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              instruction_set,
2923946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              allocator.GetMemory(),
2933946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetFrameSize(),
2943946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetCoreSpillMask(),
2953946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              0, /* FPR spill mask, unused */
2963946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              mapping_table,
2973946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              stack_map);
29886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
29986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
3003946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    return nullptr;
30186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
30288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    unoptimized_compiled_methods_++;
30386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    codegen->CompileBaseline(&allocator);
30486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
30586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    // Run these phases to get some test coverage.
30686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    graph->BuildDominatorTree();
30786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    graph->TransformToSSA();
30886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    visualizer.DumpGraph("ssa");
30986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    graph->FindNaturalLoops();
310d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray    SsaRedundantPhiElimination(graph).Run();
311d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray    SsaDeadPhiElimination(graph).Run();
312d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray    GlobalValueNumberer(graph->GetArena(), graph).Run();
31386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    SsaLivenessAnalysis liveness(*graph, codegen);
31486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    liveness.Analyze();
31586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    visualizer.DumpGraph(kLivenessPassName);
316787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
3173946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> mapping_table;
3183946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    SrcMap src_mapping_table;
3193946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildMappingTable(&mapping_table,
3203946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
3213946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                 &src_mapping_table : nullptr);
3223946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> vmap_table;
3233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildVMapTable(&vmap_table);
3243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> gc_map;
3253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
3263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    return new CompiledMethod(GetCompilerDriver(),
3283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              instruction_set,
3293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              allocator.GetMemory(),
3303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetFrameSize(),
3313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetCoreSpillMask(),
3323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              0, /* FPR spill mask, unused */
3333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              &src_mapping_table,
3343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              mapping_table,
3353946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              vmap_table,
3363946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              gc_map,
3373946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              nullptr);
3383946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
339b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
340b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
34153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
34253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                            uint32_t access_flags,
34353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                            InvokeType invoke_type,
34453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                            uint16_t class_def_idx,
34553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                            uint32_t method_idx,
34653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                            jobject class_loader,
34753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                            const DexFile& dex_file) const {
34853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
34953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                      method_idx, class_loader, dex_file);
35053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  if (method != nullptr) {
35153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe    return method;
35253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  }
35353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
35453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
35553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                            class_loader, dex_file);
35653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
35753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
35853c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
35953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
36053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
36153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
362b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
363