optimizing_compiler.cc revision f537012ceb6cba8a78b36a5065beb9588451a250
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"
29e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
30f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
31d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
323c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
33e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
34e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "mirror/art_method-inl.h"
35787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
3626a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
37a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
383159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
397dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
40804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
41787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "utils/arena_allocator.h"
42b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
43b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
44b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
45787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
46787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
48787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
49787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
5088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  CodeVectorAllocator() {}
51787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
52787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
53787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
5492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
55787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
56787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
57787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
58787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
5992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  const std::vector<uint8_t>& GetMemory() const { return memory_; }
60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
61787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
62787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  std::vector<uint8_t> memory_;
63787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
64787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
65787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
66787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
67787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
68f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
69f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
70f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
71f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic bool kIsVisualizerEnabled = false;
72f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
73f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
74f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
75f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * be in the file.
76f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
77f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic const char* kStringFilter = "";
78f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
7953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
8053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
8153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
8288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
8353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
8453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
8553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
8653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
8753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
8853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
8953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
9053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
9153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
9253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
9353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          const DexFile& dex_file) const OVERRIDE;
9453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
9553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
9653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
9753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             const DexFile& dex_file) const OVERRIDE;
9853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
9953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
10053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
10153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
10253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool WriteElf(art::File* file,
10353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                OatWriter* oat_writer,
10453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                const std::vector<const art::DexFile*>& dex_files,
10553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                const std::string& android_root,
10653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
10753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
108e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED,
109e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                            void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE {
110e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray    return nullptr;
111e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  }
11253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
113e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
11453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
115e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  void Init() const OVERRIDE {}
11653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
117e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  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  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
13053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
13153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
13288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
13388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
13488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
13588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
13688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
13788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray          driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
13888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      total_compiled_methods_(0),
13988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      unoptimized_compiled_methods_(0),
140e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray      optimized_compiled_methods_(0) {
141f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  if (kIsVisualizerEnabled) {
142f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray    visualizer_output_.reset(new std::ofstream("art.cfg"));
143f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
144f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
145787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
14688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
147ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray  if (total_compiled_methods_ == 0) {
148ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    LOG(INFO) << "Did not compile any method.";
149ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray  } else {
150ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    size_t unoptimized_percent = (unoptimized_compiled_methods_ * 100 / total_compiled_methods_);
151ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    size_t optimized_percent = (optimized_compiled_methods_ * 100 / total_compiled_methods_);
152ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray    LOG(INFO) << "Compiled " << total_compiled_methods_ << " methods: "
153ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray              << unoptimized_percent << "% (" << unoptimized_compiled_methods_ << ") unoptimized, "
154ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray              << optimized_percent << "% (" << optimized_compiled_methods_ << ") optimized.";
155ce71ae7daca2e9ae4eec42d7445013c37d96e385Nicolas Geoffray  }
15688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
15788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
158e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
159e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
160e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
161e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
16253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
16353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
16453c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
16553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                               uint32_t method_idx,
16653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                               const DexFile& dex_file) const {
167e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
16853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
16953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
17053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeuintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
1714179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray  return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
1724179cc148734fbda4677846369ae4a4db68677bfNicolas Geoffray      InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
17353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
17453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
17553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampebool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
17653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                  const std::vector<const art::DexFile*>& dex_files,
17753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                                  const std::string& android_root, bool is_host) const {
178e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
179e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                       *GetCompilerDriver());
18053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
18153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
1821ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
1831ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  return instruction_set == kArm64
1841ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
1851ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
1861ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
1871ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
1881ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
189de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffraystatic bool CanOptimize(const DexFile::CodeItem& code_item) {
190de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  // TODO: We currently cannot optimize methods with try/catch.
191de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  return code_item.tries_size_ == 0;
192de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray}
193de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray
1945e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffraystatic void RunOptimizations(HGraph* graph, const HGraphVisualizer& visualizer) {
1955e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  HDeadCodeElimination opt1(graph);
1965e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  HConstantFolding opt2(graph);
1975e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  SsaRedundantPhiElimination opt3(graph);
1985e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  SsaDeadPhiElimination opt4(graph);
1995e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  InstructionSimplifier opt5(graph);
2003159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  GVNOptimization opt6(graph);
2015e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  InstructionSimplifier opt7(graph);
2025e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
2033159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  HOptimization* optimizations[] = {
2043159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    &opt1,
2053159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    &opt2,
2063159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    &opt3,
2073159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    &opt4,
2083159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    &opt5,
2093159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    &opt6,
2103159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    &opt7
2113159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  };
2125e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
2135e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  for (size_t i = 0; i < arraysize(optimizations); ++i) {
2145e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray    HOptimization* optimization = optimizations[i];
2155e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray    optimization->Run();
2165e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray    visualizer.DumpGraph(optimization->GetPassName());
2173159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray    optimization->Check();
2185e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray  }
2195e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
2205e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
221f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffraystatic bool TryBuildingSsa(HGraph* graph,
222f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray                           const DexCompilationUnit& dex_compilation_unit,
223f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray                           const HGraphVisualizer& visualizer) {
224f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray  graph->BuildDominatorTree();
225f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray  graph->TransformToSSA();
226f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray
227f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray  if (!graph->AnalyzeNaturalLoops()) {
228f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    LOG(INFO) << "Skipping compilation of "
229f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray              << PrettyMethod(dex_compilation_unit.GetDexMethodIndex(),
230f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray                              *dex_compilation_unit.GetDexFile())
231f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray              << ": it contains a non natural loop";
232f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    return false;
233f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray  }
234f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray  visualizer.DumpGraph("ssa transform");
235f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray  return true;
236f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray}
237f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray
238e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
239e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            uint32_t access_flags,
240e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            InvokeType invoke_type,
241e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            uint16_t class_def_idx,
242e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            uint32_t method_idx,
243e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            jobject class_loader,
244e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                            const DexFile& dex_file) const {
2456a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNUSED(invoke_type);
24688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  total_compiled_methods_++;
2478fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
2488d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
2498d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
2508d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
2518d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
2528fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
2538fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
2548fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
2551ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
2568fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
2578fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
2588fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
259b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
260b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
261b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
262b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
26392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
26492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
26572d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
26672d32629303f8f39362a4099481f48646aed042fIan Rogers    GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
26792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
2688ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
2698ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
2708ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  bool shouldCompile = dex_compilation_unit.GetSymbol().find("00024opt_00024") != std::string::npos;
27186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  bool shouldOptimize =
27286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray      dex_compilation_unit.GetSymbol().find("00024reg_00024") != std::string::npos;
2738ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
274787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  ArenaPool pool;
275787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  ArenaAllocator arena(&pool);
276e50383288a75244255d3ecedcc79ffe9caf774cbNicolas Geoffray  HGraphBuilder builder(&arena, &dex_compilation_unit, &dex_file, GetCompilerDriver());
277f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
278787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  HGraph* graph = builder.BuildGraph(*code_item);
279787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  if (graph == nullptr) {
2805667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
281787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
282787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
283787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
284787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  CodeGenerator* codegen = CodeGenerator::Create(&arena, graph, instruction_set);
285787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  if (codegen == nullptr) {
2865667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
287787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
288787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
289787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
290a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  HGraphVisualizer visualizer(
291a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray      visualizer_output_.get(), graph, kStringFilter, *codegen, dex_compilation_unit);
292a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray  visualizer.DumpGraph("builder");
293a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
294787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  CodeVectorAllocator allocator;
29586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
296de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  if (run_optimizations_
297de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray      && CanOptimize(*code_item)
298de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray      && RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set)) {
29988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    optimized_compiled_methods_++;
300f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    if (!TryBuildingSsa(graph, dex_compilation_unit, visualizer)) {
301f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray      // We could not transform the graph to SSA, bailout.
302f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray      return nullptr;
303f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
3045e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray    RunOptimizations(graph, visualizer);
30575be28332b278cff9039b54bfb228ac72f539cccRoland Levillain
30626a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray    PrepareForRegisterAllocation(graph).Run();
30786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    SsaLivenessAnalysis liveness(*graph, codegen);
30886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    liveness.Analyze();
30986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    visualizer.DumpGraph(kLivenessPassName);
31086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
31186dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
31286dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    register_allocator.AllocateRegisters();
31386dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
31486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    visualizer.DumpGraph(kRegisterAllocatorPassName);
31586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    codegen->CompileOptimized(&allocator);
3163946844c34ad965515f677084b07d663d70ad1b8Nicolas 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
3233946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> stack_map;
3243946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildStackMaps(&stack_map);
3253946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3263946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    return new CompiledMethod(GetCompilerDriver(),
3273946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              instruction_set,
3283946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              allocator.GetMemory(),
3293946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetFrameSize(),
3303946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetCoreSpillMask(),
3313946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              0, /* FPR spill mask, unused */
3323946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              mapping_table,
3333946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              stack_map);
33486dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
33586dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
3365667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
33786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray  } else {
33888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    unoptimized_compiled_methods_++;
33986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    codegen->CompileBaseline(&allocator);
34086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray
3413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> mapping_table;
3423946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    SrcMap src_mapping_table;
3433946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildMappingTable(&mapping_table,
3443946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray            GetCompilerDriver()->GetCompilerOptions().GetIncludeDebugSymbols() ?
3453946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                 &src_mapping_table : nullptr);
3463946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> vmap_table;
3473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildVMapTable(&vmap_table);
3483946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    std::vector<uint8_t> gc_map;
3493946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
3503946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray
3513946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray    return new CompiledMethod(GetCompilerDriver(),
3523946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              instruction_set,
3533946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              allocator.GetMemory(),
3543946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetFrameSize(),
3553946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              codegen->GetCoreSpillMask(),
3563946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              0, /* FPR spill mask, unused */
3573946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              &src_mapping_table,
3583946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              mapping_table,
3593946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              vmap_table,
3603946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              gc_map,
3613946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray                              nullptr);
3623946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
363b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
364b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
36553c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
36653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
36753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
36853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
369b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
370