optimizing_compiler.cc revision 0616ae081e648f4b9b64b33e2624a943c5fce977
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
2244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
2344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#include "instruction_simplifier_arm64.h"
2444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
2544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
260616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
270616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#include "constant_area_fixups_x86.h"
280616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
290616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell
30e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
31b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
32f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko#include "base/arena_containers.h"
335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h"
345e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h"
3546e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h"
36f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h"
37787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h"
38787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h"
3920f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h"
4053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h"
4175be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h"
4275be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h"
4371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
44f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h"
45f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
46787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h"
4820f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
4992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
50e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
5169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h"
52f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
53d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
54e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
553c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
5671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
5782091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
58e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
59787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
6026a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
61f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
62a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
63827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
643159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
657dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
66804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
67c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
68b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
69b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
70b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
72787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
74787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
75787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
76f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  explicit CodeVectorAllocator(ArenaAllocator* arena)
77f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
78f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko        size_(0) {}
79787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
80787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
81787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
8292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
83787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
84787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
85787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
87f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
88787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
90f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> memory_;
91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
92787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
93787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
96f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
97f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
98ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
99f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
10053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = "";
101f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
10269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope;
103809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
10469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject {
1055e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
10669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver(HGraph* graph,
10769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               const char* method_name,
10869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CodeGenerator* codegen,
10969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               std::ostream* visualizer_output,
11069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CompilerDriver* compiler_driver)
11169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      : graph_(graph),
11269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        method_name_(method_name),
113809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1145e8b137d28c840b128e2488f954cccee3e86db14David Brazdil        timing_logger_(method_name, true, true),
115eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames        disasm_info_(graph->GetArena()),
116809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
11769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        visualizer_(visualizer_output, graph, *codegen),
11869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        graph_in_bad_state_(false) {
11953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (timing_logger_enabled_ || visualizer_enabled_) {
12053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (!IsVerboseMethod(compiler_driver, method_name)) {
12153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        timing_logger_enabled_ = visualizer_enabled_ = false;
12253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
12353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (visualizer_enabled_) {
12453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        visualizer_.PrintHeader(method_name_);
12553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        codegen->SetDisassemblyInformation(&disasm_info_);
12653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
12762e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1295e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
13069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassObserver() {
131809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
132809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << "TIMINGS " << method_name_;
133809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
134809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
135809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
136809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
137eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  void DumpDisassembly() const {
138eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (visualizer_enabled_) {
139eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      visualizer_.DumpGraphWithDisassembly();
140eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
141eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
142eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
14369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  void SetGraphInBadState() { graph_in_bad_state_ = true; }
14469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
145809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
146809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1475e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1485e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
149ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
1505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1555e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
156809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
162ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
1635e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
16469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
16569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    // Validate the HGraph if running in debug mode.
16669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    if (kIsDebugBuild) {
16769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      if (!graph_in_bad_state_) {
16869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        if (graph_->IsInSsaForm()) {
16969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          SSAChecker checker(graph_->GetArena(), graph_);
17069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
17169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
17269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker);
17369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
17469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        } else {
17569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          GraphChecker checker(graph_->GetArena(), graph_);
17669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
17769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
17869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
17969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
18069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        }
18169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      }
18269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    }
1835e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1845e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
18553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
18653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
18753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // empty kStringFilter matching all methods.
18853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
18953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
19053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
19153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
19253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
19353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // warning when the string is empty.
19453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
19553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
19653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return true;
19753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
19853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
19953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    return false;
20053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  }
20153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  HGraph* const graph_;
2035e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  const char* method_name_;
2045e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
2055e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool timing_logger_enabled_;
2065e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  TimingLogger timing_logger_;
2075e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
208eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  DisassemblyInformation disasm_info_;
209eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
2105e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool visualizer_enabled_;
2115e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphVisualizer visualizer_;
2125e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
21369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // Flag to be set by the compiler if the pass failed and the graph is not
21469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // expected to validate.
21569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  bool graph_in_bad_state_;
216809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
21769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  friend PassScope;
21869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
21969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  DISALLOW_COPY_AND_ASSIGN(PassObserver);
2205e8b137d28c840b128e2488f954cccee3e86db14David Brazdil};
2215e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
22269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject {
223809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public:
22469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassScope(const char *pass_name, PassObserver* pass_observer)
225809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      : pass_name_(pass_name),
22669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        pass_observer_(pass_observer) {
22769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->StartPass(pass_name_);
228809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
229809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
23069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassScope() {
23169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->EndPass(pass_name_);
232809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
233809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
234809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
235809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  const char* const pass_name_;
23669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver* const pass_observer_;
237809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil};
238809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
23953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
24053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
24153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
24288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
24353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
24553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
24653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
24853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
24953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
25053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
25153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
25253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
253736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          const DexFile& dex_file,
254736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
25553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
256216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
257216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t access_flags,
258216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             InvokeType invoke_type,
259216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint16_t class_def_idx,
260216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t method_idx,
261216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             jobject class_loader,
262736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             const DexFile& dex_file,
263736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             Handle<mirror::DexCache> dex_cache) const;
264216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
26553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
26653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
267216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
268216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
269216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
271e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
27290443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
273216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
274216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
275216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
277216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
27853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
279ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
28053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
281216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
28253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2832be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2842be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2852be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2862be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2872be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2882be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
28953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
29088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
29188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
29288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
29348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
29412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Optimize and compile `graph`.
29512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileOptimized(HGraph* graph,
29612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CodeGenerator* codegen,
29712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CompilerDriver* driver,
29812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const DexCompilationUnit& dex_compilation_unit,
29969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                   PassObserver* pass_observer) const;
30012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
30112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Just compile without doing optimizations.
30212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileBaseline(CodeGenerator* codegen,
30312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  CompilerDriver* driver,
304eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                  const DexCompilationUnit& dex_compilation_unit,
30569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                  PassObserver* pass_observer) const;
30612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
3072be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
30888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
30953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
31053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
311216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // Delegate to Quick in case the optimizing compiler cannot compile a method.
312216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  std::unique_ptr<Compiler> delegate_;
313216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
31453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
31553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
31653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
31788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
31888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
31988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
32088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
32188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
322a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray          (driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime)
323a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray          && !driver->GetCompilerOptions().GetDebuggable()),
324216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray      delegate_(Create(driver, Compiler::Kind::kQuick)) {}
325ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
326ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
327216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->Init();
328ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
329ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
330ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
331866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
332866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
333ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
334ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
335ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
336866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil    visualizer_output_.reset(new std::ofstream(cfg_file_name));
337f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
3382be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
3392be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
3402be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
341f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
342787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
343216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
344216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->UnInit();
345216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
346216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
34788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
3482be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (compilation_stats_.get() != nullptr) {
3492be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_->Log();
3502be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
35188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
35288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
353216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
354216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->InitCompilationUnit(cu);
355216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
356216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
357e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
358e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
359e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
360e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
36153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
36253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
3631ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
3641ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  return instruction_set == kArm64
3651ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
3664dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
3671ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
3681ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
3691ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
3701ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
37110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
37210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
37369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer) {
37410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
37569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
37669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    optimizations[i]->Run();
37710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
37810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
37910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
380ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph,
381ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            CompilerDriver* driver,
382ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            OptimizingCompilerStats* stats,
383ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            const DexCompilationUnit& dex_compilation_unit,
384ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            PassObserver* pass_observer,
385ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            StackHandleScopeCollection* handles) {
386ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
387ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
388ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle      && (compiler_options.GetInlineMaxCodeUnits() > 0);
389ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  if (!should_inline) {
390ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    return;
391ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  }
392ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
393ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ArenaAllocator* arena = graph->GetArena();
394ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HInliner* inliner = new (arena) HInliner(
395ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    graph, dex_compilation_unit, dex_compilation_unit, driver, handles, stats);
396ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ReferenceTypePropagation* type_propagation =
397ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    new (arena) ReferenceTypePropagation(graph, handles,
398ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle        "reference_type_propagation_after_inlining");
399ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
400ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations[] = {
401ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    inliner,
402ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // Run another type propagation phase: inlining will open up more opportunities
403ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // to remove checkcast/instanceof and null checks.
404ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    type_propagation,
405ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
406ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
407ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
408ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle}
409ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
41044b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set,
41144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 HGraph* graph,
41244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 OptimizingCompilerStats* stats,
41344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 PassObserver* pass_observer) {
41444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  ArenaAllocator* arena = graph->GetArena();
41544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  switch (instruction_set) {
41644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
41744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    case kArm64: {
41844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      arm64::InstructionSimplifierArm64* simplifier =
41944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
42044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
42144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
42244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      HOptimization* arm64_optimizations[] = {
42344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        simplifier,
42444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        side_effects,
42544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        gvn
42644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      };
42744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
42844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
42944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    }
43044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
4310616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
4320616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    case kX86: {
4330616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      x86::ConstantAreaFixups* constant_area_fixups =
4340616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell          new (arena) x86::ConstantAreaFixups(graph, stats);
4350616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      HOptimization* x86_optimizations[] = {
4360616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell        constant_area_fixups
4370616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      };
4380616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
4390616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      break;
4400616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    }
4410616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
44244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    default:
44344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
44444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  }
44544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames}
44644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
447e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
448e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
449e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
450e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
45169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer,
452acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle                             StackHandleScopeCollection* handles) {
453a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
454a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
455a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
456a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
457a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
458a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
459a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
460a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph);
461a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
462a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
463a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
464a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  LICM* licm = new (arena) LICM(graph, *side_effects);
465a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph);
466a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation =
467a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      new (arena) ReferenceTypePropagation(graph, handles);
468a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
469a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "instruction_simplifier_after_types");
470a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
471b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_after_bce");
472b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray  InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
473b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_before_codegen");
474a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
475a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver);
47671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
477ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations1[] = {
478a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
479a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
480a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
481a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation,
48218e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
483ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    simplify2
484ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
485ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
486ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
487ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
488bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  if (graph->HasTryCatch()) {
489bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    // TODO: Update the optimizations below to work correctly under try/catch
490bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    //       semantics. The optimizations above suffice for running codegen
491bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    //       in the meanwhile.
492bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    return;
493bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  }
494bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil
495ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  MaybeRunInliner(graph, driver, stats, dex_compilation_unit, pass_observer, handles);
496ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
497ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations2[] = {
49846e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil    // BooleanSimplifier depends on the InstructionSimplifier removing redundant
49946e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil    // suspend checks to recognize empty blocks.
500a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    boolean_simplify,
501ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    fold2,  // TODO: if we don't inline we can also skip fold2.
502a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    side_effects,
503a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    gvn,
504a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    licm,
505a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    bce,
506a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify3,
507a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    dce2,
508b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    // The codegen has a few assumptions that only the instruction simplifier can
509b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    // satisfy. For example, the code generator does not expect to see a
510b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    // HTypeConversion from a type to the same type.
511b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    simplify4,
5123159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  };
5135e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
514ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
51544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
51644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer);
5175e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
5185e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
519376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
520376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
521f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Markostatic ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) {
522376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
523376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
524376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
525376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
526376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
527e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
528376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
529376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
530c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampestatic void AllocateRegisters(HGraph* graph,
531c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe                              CodeGenerator* codegen,
53269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                              PassObserver* pass_observer) {
533c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  PrepareForRegisterAllocation(graph).Run();
5340d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
535c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
53669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
537c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    liveness.Analyze();
538c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
539c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
54069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
541c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
542c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
543c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe}
54412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
545581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
546581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
547581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  codegen->EmitLinkerPatches(&linker_patches);
548581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
549581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Sort patches by literal offset. Required for .oat_patches encoding.
550581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  std::sort(linker_patches.begin(), linker_patches.end(),
551581550137ee3a068a14224870e71aeee924a0646Vladimir Marko            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
552581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    return lhs.LiteralOffset() < rhs.LiteralOffset();
553581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  });
554581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
555581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  return linker_patches;
556581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
557581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
55812df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
55912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CodeGenerator* codegen,
56012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CompilerDriver* compiler_driver,
56112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const DexCompilationUnit& dex_compilation_unit,
56269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                                     PassObserver* pass_observer) const {
563ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  ScopedObjectAccess soa(Thread::Current());
564ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  StackHandleScopeCollection handles(soa.Self());
565ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromRunnableToSuspended(kNative);
5662be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  RunOptimizations(graph, compiler_driver, compilation_stats_.get(),
56769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                   dex_compilation_unit, pass_observer, &handles);
56812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
569bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  if (graph->HasTryCatch()) {
570ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier    soa.Self()->TransitionFromSuspendedToRunnable();
571bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    return nullptr;
572bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  }
573bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil
57469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  AllocateRegisters(graph, codegen, pass_observer);
57512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
576f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = graph->GetArena();
577f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
5782a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
5792a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
5802a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
5812a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
58212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileOptimized(&allocator);
58312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
584581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
585581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
586f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
58712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildStackMaps(&stack_map);
58812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
5892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledOptimized);
59012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
591eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
59212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
59312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
59412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(allocator.GetMemory()),
595aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
596aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
597aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
598aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
59912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
600d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
601c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      &src_mapping_table,
602c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // mapping_table.
603c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
604c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // native_gc_map.
605c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
606581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
60769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
608ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier
609ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromSuspendedToRunnable();
610eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
61112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
61212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
61312df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline(
61412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
61512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CompilerDriver* compiler_driver,
616eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    const DexCompilationUnit& dex_compilation_unit,
61769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassObserver* pass_observer) const {
618f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = codegen->GetGraph()->GetArena();
619f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
6202a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
6212a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
6222a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
6232a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
62412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileBaseline(&allocator);
62512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
626581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
627581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
628f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps));
629c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->BuildMappingTable(&mapping_table);
630f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps));
63112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
632f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps));
63312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
63412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
6352be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledBaseline);
636eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
637aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      compiler_driver,
638aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetInstructionSet(),
639aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      ArrayRef<const uint8_t>(allocator.GetMemory()),
640aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
641aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
642aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
643aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
644aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetCoreSpillMask(),
645aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetFpuSpillMask(),
646aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      &src_mapping_table,
647aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(mapping_table),
648aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(vmap_table),
649aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(gc_map),
650c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
651581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
65269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
653eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
65412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
65512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
656216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
657216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t access_flags,
658216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               InvokeType invoke_type,
659216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint16_t class_def_idx,
660216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t method_idx,
661216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               jobject class_loader,
662736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               const DexFile& dex_file,
663736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               Handle<mirror::DexCache> dex_cache) const {
6646a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNUSED(invoke_type);
6655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  std::string method_name = PrettyMethod(method_idx, dex_file);
6662be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
667cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
668cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
6698d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
6708d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
6718d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
6728d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
6738fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6748fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
6758fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
6761ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
6772be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
6788fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
6798fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6808fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
681b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
6822be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
683b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
684b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
685b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
68636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
687432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  // code units is bigger than 128.
688432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
68936540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
69036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
69136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
6922be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
69336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
69436540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
69536540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
69692cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
6979523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item,
69872d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
699736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache);
70092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
7013cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
7023cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
7033cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
7043cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
705579ea7d0d89a1d7f02c35ef1beb6328a37259735Nicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetArenaPool());
706e0fe7ae36180863e45cbb9d1e6e9c30b1b1a949cNicolas Geoffray  HGraph* graph = new (&arena) HGraph(
707e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      &arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(),
708e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable());
709e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
7108ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
7118ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
712e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  bool shouldCompile = method_name.find("$opt$") != std::string::npos;
713a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray  bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
7148ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
71512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
716cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
717cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
718cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
719cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
72012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
7215667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
7222be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
723787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
724787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
725c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
7268363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky      compiler_driver->GetCompilerOptions().GetGenerateDebugInfo());
727787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
72869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver pass_observer(graph,
72969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             method_name.c_str(),
73069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             codegen.get(),
73169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             visualizer_output_.get(),
73269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             compiler_driver);
7335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7349523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  const uint8_t* interpreter_metadata = nullptr;
7359523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  {
7369523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ScopedObjectAccess soa(Thread::Current());
737736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    StackHandleScope<1> hs(soa.Self());
7389523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    Handle<mirror::ClassLoader> loader(hs.NewHandle(
7399523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa.Decode<mirror::ClassLoader*>(class_loader)));
7409523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ArtMethod* art_method = compiler_driver->ResolveMethod(
7419523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
7429523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // We may not get a method, for example if its class is erroneous.
7439523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile.
7449523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    if (art_method != nullptr) {
7459523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray      interpreter_metadata = art_method->GetQuickenedInfo();
7469523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    }
7479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  }
7485e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphBuilder builder(graph,
7495e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_file,
7525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        compiler_driver,
7539523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray                        compilation_stats_.get(),
754736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        interpreter_metadata,
755736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        dex_cache);
7565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  VLOG(compiler) << "Building " << method_name;
7585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
759809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
76069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
761809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (!builder.BuildGraph(*code_item)) {
762335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray      DCHECK(!(IsCompilingWithCoreImage() && shouldCompile))
763335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray          << "Could not build graph in optimizing compiler";
76469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      pass_observer.SetGraphInBadState();
765809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      return nullptr;
766809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
7675e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
768a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
76948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
770216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
771216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // `run_optimizations_` is set explicitly (either through a compiler filter
772216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
773216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // to Quick.
7742e7cd752452d02499a2f5fbd604c5427aa372f00Nicolas Geoffray  bool can_use_baseline = !run_optimizations_ && builder.CanUseBaselineForStringInit();
775f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CompiledMethod* compiled_method = nullptr;
776ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil  if (run_optimizations_ && can_allocate_registers) {
7775e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Optimizing " << method_name;
7785e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
779809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
78069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer);
781809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      if (!graph->TryBuildingSsa()) {
782809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        // We could not transform the graph to SSA, bailout.
783809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
7842be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle        MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
785ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil        pass_observer.SetGraphInBadState();
786809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        return nullptr;
787809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      }
788f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
7895e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
790f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileOptimized(graph,
791f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       codegen.get(),
792f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       compiler_driver,
793f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       dex_compilation_unit,
794f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       &pass_observer);
795bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  } else if (shouldOptimize && can_allocate_registers) {
79686dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
7975667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
798216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else if (can_use_baseline) {
7995e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Compile baseline " << method_name;
80048c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
80148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    if (!run_optimizations_) {
8022be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedDisabled);
80348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_allocate_registers) {
8042be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
80548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    }
80648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
807f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileBaseline(codegen.get(),
808f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      compiler_driver,
809f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      dex_compilation_unit,
810f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      &pass_observer);
811216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
812f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
813f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  if (kArenaAllocatorCountAllocations) {
814f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    if (arena.BytesAllocated() > 4 * MB) {
815f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      MemStats mem_stats(arena.GetMemStats());
816f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
817f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    }
818f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  }
819f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
820f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  return compiled_method;
821216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
822216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
823216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
824216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
825216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
826216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
827216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
828f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
829736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            const DexFile& dex_file,
830736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            Handle<mirror::DexCache> dex_cache) const {
831f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
832f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
8330760a81257fa427646c309500d603194009265efAndreas Gampe  DCHECK(!compiler_driver->GetVerifiedMethod(&dex_file, method_idx)->HasRuntimeThrow());
8340760a81257fa427646c309500d603194009265efAndreas Gampe  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)) {
835f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle     method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
836736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                         method_idx, jclass_loader, dex_file, dex_cache);
837f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  } else {
838f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
8392be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
840f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    } else {
8412be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified);
842f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
843f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
844f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
845216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  if (method != nullptr) {
846216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return method;
8473946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
84812be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  method = delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
849736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                              jclass_loader, dex_file, dex_cache);
85012be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray
85112be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  if (method != nullptr) {
8522be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kCompiledQuick);
85312be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  }
85412be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
855b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
856b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
85753c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
85853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
85953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
86053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
861335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() {
862335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  const std::string& image = Runtime::Current()->GetImageLocation();
863335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art");
864335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray}
865335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray
866b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
867