optimizing_compiler.cc revision 82091dad38f3e5bfaf3b6984c9ab73069fb68310
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"
2871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
29787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
3092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
31e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
32f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
33d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
34e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
353c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
3671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
3782091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
38e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
39e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "mirror/art_method-inl.h"
40787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
4126a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
42a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
43827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
443159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
457dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
46804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
4710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle#include "reference_type_propagation.h"
48787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h"
49b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
50b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
51b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
52787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
53787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
54787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
55787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
56787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
5788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  CodeVectorAllocator() {}
58787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
59787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
6192cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
62787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
63787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
64787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
65787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
6692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  const std::vector<uint8_t>& GetMemory() const { return memory_; }
67787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
68787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
69787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  std::vector<uint8_t> memory_;
70787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
72787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
74787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
75f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
76f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
77ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
78f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
79f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic const char* kStringFilter = "";
80f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
8153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
8253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
8353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
8488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
8553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
8653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
8753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
8853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
8953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(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 OVERRIDE;
9653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
9753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
9853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
9953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             const DexFile& dex_file) const OVERRIDE;
10053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
10153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
10253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
10353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
10453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool WriteElf(art::File* file,
10553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                OatWriter* oat_writer,
10653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                const std::vector<const art::DexFile*>& dex_files,
10753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                const std::string& android_root,
10853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
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
12112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Optimize and compile `graph`.
12212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileOptimized(HGraph* graph,
12312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CodeGenerator* codegen,
12412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CompilerDriver* driver,
12512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const DexCompilationUnit& dex_compilation_unit,
12612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const HGraphVisualizer& visualizer) const;
12712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
12812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Just compile without doing optimizations.
12912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileBaseline(CodeGenerator* codegen,
13012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  CompilerDriver* driver,
13112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  const DexCompilationUnit& dex_compilation_unit) const;
13212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
13348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  mutable OptimizingCompilerStats compilation_stats_;
13488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
13553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
13653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
13753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
13853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
13953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
14088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
14188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
14288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
14388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
14488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
14588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray          driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
146ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      compilation_stats_() {}
147ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
148ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
149ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
150ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
151ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
152866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
153866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
154ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
155ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
156ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
157866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil    visualizer_output_.reset(new std::ofstream(cfg_file_name));
158f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
159f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
160787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
16188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
16248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  compilation_stats_.Log();
16388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
16488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
165e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
166e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
167e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
168e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
16953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
17053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
17153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
17253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                               uint32_t method_idx,
17353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                               const DexFile& dex_file) const {
174e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
17553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
17653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
17753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeuintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
1784179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray  return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
1794179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray      InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
18053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
18153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
18253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
18353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                  const std::vector<const art::DexFile*>& dex_files,
18453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                  const std::string& android_root, bool is_host) const {
185e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
186e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                       *GetCompilerDriver());
18753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
18853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
1891ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
1901ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  return instruction_set == kArm64
1911ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
1921ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
1931ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
1941ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
1951ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
196de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffraystatic bool CanOptimize(const DexFile::CodeItem& code_item) {
197de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  // TODO: We currently cannot optimize methods with try/catch.
198de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  return code_item.tries_size_ == 0;
199de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray}
200de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray
20110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
20210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
20310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             const HGraphVisualizer& visualizer) {
20410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
20510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    HOptimization* optimization = optimizations[i];
20610e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false);
20710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    optimization->Run();
20810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true);
20910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    optimization->Check();
21010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
21110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
21210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
213e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
214e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
215e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
216e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
217e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const HGraphVisualizer& visualizer) {
218e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  SsaRedundantPhiElimination redundant_phi(graph);
219e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  SsaDeadPhiElimination dead_phi(graph);
220e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  HDeadCodeElimination dce(graph);
2219ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray  HConstantFolding fold1(graph);
222e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  InstructionSimplifier simplify1(graph);
223e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
224e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  HInliner inliner(graph, dex_compilation_unit, driver, stats);
225e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
2269ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray  HConstantFolding fold2(graph);
22786dde1658a1951c251dd5c6ff21ecc5c281879a6Nicolas Geoffray  SideEffectsAnalysis side_effects(graph);
22886dde1658a1951c251dd5c6ff21ecc5c281879a6Nicolas Geoffray  GVNOptimization gvn(graph, side_effects);
22982091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray  LICM licm(graph, side_effects);
230f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang  BoundsCheckElimination bce(graph);
23110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  ReferenceTypePropagation type_propagation(graph);
23210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  InstructionSimplifier simplify2(graph, "instruction_simplifier_after_types");
2335e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
23471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe  IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
23571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
2363159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  HOptimization* optimizations[] = {
237e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    &redundant_phi,
238e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    &dead_phi,
23971fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe    &intrinsics,
240e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    &dce,
2419ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray    &fold1,
242e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    &simplify1,
243e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    &inliner,
2449ee66183d8e046ea661f642ba884626f16b46e06Nicolas Geoffray    &fold2,
24586dde1658a1951c251dd5c6ff21ecc5c281879a6Nicolas Geoffray    &side_effects,
246e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    &gvn,
24782091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray    &licm,
248f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang    &bce,
24910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    &type_propagation,
250e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    &simplify2
2513159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  };
2525e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
25310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  RunOptimizations(optimizations, arraysize(optimizations), visualizer);
2545e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
2555e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
256376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
257376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
258e21dc3db191df04c100620965bee4617b3b24397Andreas Gampestatic ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
259376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
260376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
261376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
262376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
263376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
264e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
265376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
266376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
26712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
26812df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
26912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CodeGenerator* codegen,
27012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CompilerDriver* compiler_driver,
27112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const DexCompilationUnit& dex_compilation_unit,
27212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const HGraphVisualizer& visualizer) const {
27312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  RunOptimizations(
27412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      graph, compiler_driver, &compilation_stats_, dex_compilation_unit, visualizer);
27512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
27612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  PrepareForRegisterAllocation(graph).Run();
27712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  SsaLivenessAnalysis liveness(*graph, codegen);
27812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  liveness.Analyze();
27912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  visualizer.DumpGraph(kLivenessPassName);
28012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
28112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
28212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  register_allocator.AllocateRegisters();
28312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  visualizer.DumpGraph(kRegisterAllocatorPassName);
28412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
28512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CodeVectorAllocator allocator;
28612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileOptimized(&allocator);
28712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
28812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> stack_map;
28912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildStackMaps(&stack_map);
29012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
29112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
29212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
29312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  return CompiledMethod::SwapAllocCompiledMethodStackMap(
29412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
29512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
29612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(allocator.GetMemory()),
29712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetFrameSize(),
29812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
299d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
30012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(stack_map));
30112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
30212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
30312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
30412df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline(
30512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
30612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CompilerDriver* compiler_driver,
30712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    const DexCompilationUnit& dex_compilation_unit) const {
30812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CodeVectorAllocator allocator;
30912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileBaseline(&allocator);
31012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
31112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> mapping_table;
31212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  DefaultSrcMap src_mapping_table;
31312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
31412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
31512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> vmap_table;
31612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
31712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> gc_map;
31812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
31912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
32012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
32112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  return CompiledMethod::SwapAllocCompiledMethod(compiler_driver,
32212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 codegen->GetInstructionSet(),
32312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 ArrayRef<const uint8_t>(allocator.GetMemory()),
32412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 codegen->GetFrameSize(),
32512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 codegen->GetCoreSpillMask(),
326d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray                                                 codegen->GetFpuSpillMask(),
32712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 &src_mapping_table,
32812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 AlignVectorSize(mapping_table),
32912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 AlignVectorSize(vmap_table),
33012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 AlignVectorSize(gc_map),
33112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                 ArrayRef<const uint8_t>());
33212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
33312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
334e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
335e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            uint32_t access_flags,
336e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            InvokeType invoke_type,
337e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            uint16_t class_def_idx,
338e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            uint32_t method_idx,
339e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            jobject class_loader,
340e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            const DexFile& dex_file) const {
3416a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNUSED(invoke_type);
34248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
343cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
344cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
3458d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
3468d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
3478d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
3488d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
3498fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
3508fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
3518fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
3521ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
35348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
3548fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
3558fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
3568fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
357b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
35848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
359b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
360b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
361b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
36292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
36392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
36472d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
365cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle    compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
36692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
367e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  std::string method_name = PrettyMethod(method_idx, dex_file);
368e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
3698ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
3708ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
371e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  bool shouldCompile = method_name.find("$opt$") != std::string::npos;
372e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos;
3738ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
374787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  ArenaPool pool;
375787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  ArenaAllocator arena(&pool);
37648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  HGraphBuilder builder(&arena,
37748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle                        &dex_compilation_unit,
378e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                        &dex_compilation_unit,
379e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                        &dex_file,
380cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                        compiler_driver,
38148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle                        &compilation_stats_);
382f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
383e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  VLOG(compiler) << "Building " << PrettyMethod(method_idx, dex_file);
384787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  HGraph* graph = builder.BuildGraph(*code_item);
385787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  if (graph == nullptr) {
3865667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
387787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
388787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
389787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
39012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
391cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
392cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
393cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
394cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
39512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
3965667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
39748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
398787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
399787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
400787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
401a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  HGraphVisualizer visualizer(
40212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      visualizer_output_.get(), graph, kStringFilter, *codegen.get(), method_name.c_str());
403a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  visualizer.DumpGraph("builder");
404a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
40548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_optimize = CanOptimize(*code_item);
40648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
40712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* result = nullptr;
40848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  if (run_optimizations_ && can_optimize && can_allocate_registers) {
40943a539f780af20a1854bca81c3d4835a585f0620Nicolas Geoffray    VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file);
410e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray    if (!graph->TryBuildingSsa()) {
411e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray      LOG(INFO) << "Skipping compilation of "
412e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                << PrettyMethod(method_idx, dex_file)
413e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                << ": it contains a non natural loop";
414f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray      // We could not transform the graph to SSA, bailout.
41548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle      compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
41612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    } else {
41712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      result = CompileOptimized(graph, codegen.get(), compiler_driver, dex_compilation_unit, visualizer);
418f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
41986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
42086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
4215667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
42286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
42343a539f780af20a1854bca81c3d4835a585f0620Nicolas Geoffray    VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file);
42448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
42548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    if (!run_optimizations_) {
42648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle      compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
42748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_optimize) {
42848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle      compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
42948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_allocate_registers) {
43048c2b03965830c73cdddeae8aea8030f08430137Calin Juravle      compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
43148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    }
43248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
43312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    result = CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
4343946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
43512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  return result;
436b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
437b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
43853c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
43953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
44053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
44153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
442b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
443