optimizing_compiler.cc revision 0760a81257fa427646c309500d603194009265ef
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
26e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
27b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h"
295e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h"
3046e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h"
31f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h"
32787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h"
33787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h"
3420f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h"
3553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h"
3675be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h"
3775be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h"
3871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
39f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h"
40f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
41787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
429523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h"
4320f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
4492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
45e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
4669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h"
47f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
48d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
49e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
503c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
5171fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
5282091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
53e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
54787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
5526a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
56f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
57a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
58827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
593159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
607dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
61804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
62c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
63b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
64b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
65b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
66787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
67787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
68787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
69787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
70787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
717c3952f423b8213083d60596a5f0bf4237ca3f7bAndreas Gampe  CodeVectorAllocator() : size_(0) {}
72787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
74787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
7592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
76787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
77787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
78787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
79787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
8092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  const std::vector<uint8_t>& GetMemory() const { return memory_; }
81787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
82787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
83787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  std::vector<uint8_t> memory_;
84787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
85787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
87787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
88787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
89f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
90f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
91ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
92f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
9353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = "";
94f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
9569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope;
96809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
9769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject {
985e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
9969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver(HGraph* graph,
10069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               const char* method_name,
10169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CodeGenerator* codegen,
10269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               std::ostream* visualizer_output,
10369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CompilerDriver* compiler_driver)
10469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      : graph_(graph),
10569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        method_name_(method_name),
106809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1075e8b137d28c840b128e2488f954cccee3e86db14David Brazdil        timing_logger_(method_name, true, true),
108eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames        disasm_info_(graph->GetArena()),
109809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
11069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        visualizer_(visualizer_output, graph, *codegen),
11169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        graph_in_bad_state_(false) {
11253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (timing_logger_enabled_ || visualizer_enabled_) {
11353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (!IsVerboseMethod(compiler_driver, method_name)) {
11453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        timing_logger_enabled_ = visualizer_enabled_ = false;
11553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
11653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (visualizer_enabled_) {
11753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        visualizer_.PrintHeader(method_name_);
11853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        codegen->SetDisassemblyInformation(&disasm_info_);
11953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
12062e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1215e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
12369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassObserver() {
124809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
125809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << "TIMINGS " << method_name_;
126809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
127809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
128809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
129809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
130eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  void DumpDisassembly() const {
131eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (visualizer_enabled_) {
132eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      visualizer_.DumpGraphWithDisassembly();
133eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
134eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
135eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
13669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  void SetGraphInBadState() { graph_in_bad_state_ = true; }
13769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
138809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
139809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1405e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1415e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
142ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
1435e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1445e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1455e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1465e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1475e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1485e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
149809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
155ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
1565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
15769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
15869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    // Validate the HGraph if running in debug mode.
15969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    if (kIsDebugBuild) {
16069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      if (!graph_in_bad_state_) {
16169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        if (graph_->IsInSsaForm()) {
16269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          SSAChecker checker(graph_->GetArena(), graph_);
16369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
16469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
16569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker);
16669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
16769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        } else {
16869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          GraphChecker checker(graph_->GetArena(), graph_);
16969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
17069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
17169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
17269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
17369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        }
17469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      }
17569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    }
1765e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1775e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
17853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
17953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
18053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // empty kStringFilter matching all methods.
18153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
18253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
18353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
18453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
18553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
18653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // warning when the string is empty.
18753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
18853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
18953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return true;
19053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
19153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
19253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    return false;
19353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  }
19453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
19569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  HGraph* const graph_;
1965e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  const char* method_name_;
1975e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
1985e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool timing_logger_enabled_;
1995e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  TimingLogger timing_logger_;
2005e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
201eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  DisassemblyInformation disasm_info_;
202eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
2035e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool visualizer_enabled_;
2045e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphVisualizer visualizer_;
2055e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
20669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // Flag to be set by the compiler if the pass failed and the graph is not
20769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // expected to validate.
20869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  bool graph_in_bad_state_;
209809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
21069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  friend PassScope;
21169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
21269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  DISALLOW_COPY_AND_ASSIGN(PassObserver);
2135e8b137d28c840b128e2488f954cccee3e86db14David Brazdil};
2145e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
21569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject {
216809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public:
21769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassScope(const char *pass_name, PassObserver* pass_observer)
218809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      : pass_name_(pass_name),
21969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        pass_observer_(pass_observer) {
22069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->StartPass(pass_name_);
221809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
222809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
22369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassScope() {
22469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->EndPass(pass_name_);
225809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
226809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
227809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
228809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  const char* const pass_name_;
22969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver* const pass_observer_;
230809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil};
231809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
23253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
23353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
23453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
23588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
23653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
23753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
23853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
23953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
24153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
24253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
24353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
24453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
24553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
24653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          const DexFile& dex_file) const OVERRIDE;
24753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
248216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
249216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t access_flags,
250216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             InvokeType invoke_type,
251216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint16_t class_def_idx,
252216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t method_idx,
253216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             jobject class_loader,
254216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const;
255216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
25653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
25753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
258216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
259216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
260216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
26153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
262e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
26390443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
264216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
265216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
266216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
268216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
26953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
270ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
27153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
272216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
27353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2742be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2752be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2762be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2772be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2782be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2792be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
28053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
28188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
28288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
28388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
28448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
28512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Optimize and compile `graph`.
28612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileOptimized(HGraph* graph,
28712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CodeGenerator* codegen,
28812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CompilerDriver* driver,
28912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const DexCompilationUnit& dex_compilation_unit,
29069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                   PassObserver* pass_observer) const;
29112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
29212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Just compile without doing optimizations.
29312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileBaseline(CodeGenerator* codegen,
29412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  CompilerDriver* driver,
295eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                  const DexCompilationUnit& dex_compilation_unit,
29669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                  PassObserver* pass_observer) const;
29712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
2982be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
29988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
30053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
30153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
302216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // Delegate to Quick in case the optimizing compiler cannot compile a method.
303216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  std::unique_ptr<Compiler> delegate_;
304216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
30553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
30653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
30753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
30888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
30988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
31088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
31188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
31288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
313a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray          (driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime)
314a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray          && !driver->GetCompilerOptions().GetDebuggable()),
315216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray      delegate_(Create(driver, Compiler::Kind::kQuick)) {}
316ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
317ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
318216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->Init();
319ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
320ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
321ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
322866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
323866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
324ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
325ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
326ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
327866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil    visualizer_output_.reset(new std::ofstream(cfg_file_name));
328f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
3292be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
3302be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
3312be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
332f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
333787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
334216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
335216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->UnInit();
336216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
337216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
33888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
3392be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (compilation_stats_.get() != nullptr) {
3402be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_->Log();
3412be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
34288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
34388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
344216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
345216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->InitCompilationUnit(cu);
346216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
347216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
348e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
349e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
350e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
351e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
35253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
35353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
3541ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
3551ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  return instruction_set == kArm64
3561ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
3574dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
3581ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
3591ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
3601ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
3611ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
36210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
36310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
36469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer) {
36510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
36669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
36769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    optimizations[i]->Run();
36810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
36910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
37010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
371ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph,
372ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            CompilerDriver* driver,
373ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            OptimizingCompilerStats* stats,
374ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            const DexCompilationUnit& dex_compilation_unit,
375ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            PassObserver* pass_observer,
376ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            StackHandleScopeCollection* handles) {
377ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
378ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
379ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle      && (compiler_options.GetInlineMaxCodeUnits() > 0);
380ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  if (!should_inline) {
381ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    return;
382ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  }
383ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
384ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ArenaAllocator* arena = graph->GetArena();
385ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HInliner* inliner = new (arena) HInliner(
386ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    graph, dex_compilation_unit, dex_compilation_unit, driver, handles, stats);
387ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ReferenceTypePropagation* type_propagation =
388ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    new (arena) ReferenceTypePropagation(graph, handles,
389ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle        "reference_type_propagation_after_inlining");
390ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
391ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations[] = {
392ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    inliner,
393ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // Run another type propagation phase: inlining will open up more opportunities
394ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // to remove checkcast/instanceof and null checks.
395ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    type_propagation,
396ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
397ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
398ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
399ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle}
400ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
40144b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set,
40244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 HGraph* graph,
40344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 OptimizingCompilerStats* stats,
40444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 PassObserver* pass_observer) {
40544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  ArenaAllocator* arena = graph->GetArena();
40644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  switch (instruction_set) {
40744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
40844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    case kArm64: {
40944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      arm64::InstructionSimplifierArm64* simplifier =
41044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
41144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
41244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
41344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      HOptimization* arm64_optimizations[] = {
41444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        simplifier,
41544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        side_effects,
41644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        gvn
41744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      };
41844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
41944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
42044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    }
42144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
42244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    default:
42344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
42444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  }
42544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames}
42644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
427e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
428e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
429e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
430e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
43169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer,
432acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle                             StackHandleScopeCollection* handles) {
433a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
434a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
435a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
436a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
437a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
438a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
439a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
440a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph);
441a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
442a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
443a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
444a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  LICM* licm = new (arena) LICM(graph, *side_effects);
445a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph);
446a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation =
447a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      new (arena) ReferenceTypePropagation(graph, handles);
448a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
449a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "instruction_simplifier_after_types");
450a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
451b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_after_bce");
452b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray  InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
453b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_before_codegen");
454a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
455a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver);
45671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
457ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations1[] = {
458a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
459a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
460a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
461a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation,
46218e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
463ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    simplify2
464ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
465ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
466ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
467ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
468bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  if (graph->HasTryCatch()) {
469bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    // TODO: Update the optimizations below to work correctly under try/catch
470bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    //       semantics. The optimizations above suffice for running codegen
471bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    //       in the meanwhile.
472bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    return;
473bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  }
474bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil
475ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  MaybeRunInliner(graph, driver, stats, dex_compilation_unit, pass_observer, handles);
476ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
477ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations2[] = {
47846e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil    // BooleanSimplifier depends on the InstructionSimplifier removing redundant
47946e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil    // suspend checks to recognize empty blocks.
480a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    boolean_simplify,
481ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    fold2,  // TODO: if we don't inline we can also skip fold2.
482a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    side_effects,
483a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    gvn,
484a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    licm,
485a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    bce,
486a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify3,
487a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    dce2,
488b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    // The codegen has a few assumptions that only the instruction simplifier can
489b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    // satisfy. For example, the code generator does not expect to see a
490b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    // HTypeConversion from a type to the same type.
491b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray    simplify4,
4923159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  };
4935e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
494ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
49544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
49644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer);
4975e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
4985e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
499376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
500376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
501e21dc3db191df04c100620965bee4617b3b24397Andreas Gampestatic ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
502376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
503376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
504376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
505376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
506376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
507e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
508376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
509376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
510c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampestatic void AllocateRegisters(HGraph* graph,
511c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe                              CodeGenerator* codegen,
51269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                              PassObserver* pass_observer) {
513c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  PrepareForRegisterAllocation(graph).Run();
5140d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
515c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
51669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
517c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    liveness.Analyze();
518c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
519c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
52069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
521c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
522c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
523c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe}
52412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
525581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
526581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
527581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  codegen->EmitLinkerPatches(&linker_patches);
528581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
529581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Sort patches by literal offset. Required for .oat_patches encoding.
530581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  std::sort(linker_patches.begin(), linker_patches.end(),
531581550137ee3a068a14224870e71aeee924a0646Vladimir Marko            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
532581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    return lhs.LiteralOffset() < rhs.LiteralOffset();
533581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  });
534581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
535581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  return linker_patches;
536581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
537581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
53812df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
53912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CodeGenerator* codegen,
54012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CompilerDriver* compiler_driver,
54112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const DexCompilationUnit& dex_compilation_unit,
54269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                                     PassObserver* pass_observer) const {
543acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle  StackHandleScopeCollection handles(Thread::Current());
5442be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  RunOptimizations(graph, compiler_driver, compilation_stats_.get(),
54569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                   dex_compilation_unit, pass_observer, &handles);
54612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
547bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  if (graph->HasTryCatch()) {
548bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    return nullptr;
549bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  }
550bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil
55169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  AllocateRegisters(graph, codegen, pass_observer);
55212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
55312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CodeVectorAllocator allocator;
55412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileOptimized(&allocator);
55512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
556581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
557581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
558c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  DefaultSrcMap src_mapping_table;
5598363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky  if (compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()) {
560c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky    codegen->BuildSourceMap(&src_mapping_table);
561c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  }
562c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky
56312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> stack_map;
56412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildStackMaps(&stack_map);
56512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
5662be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledOptimized);
56712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
568eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
56912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
57012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
57112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(allocator.GetMemory()),
572aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
573aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
574aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
575aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
57612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
577d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
578c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      &src_mapping_table,
579c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // mapping_table.
580c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
581c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // native_gc_map.
582c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
583581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
58469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
585eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
58612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
58712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
58812df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline(
58912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
59012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CompilerDriver* compiler_driver,
591eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    const DexCompilationUnit& dex_compilation_unit,
59269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassObserver* pass_observer) const {
59312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CodeVectorAllocator allocator;
59412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileBaseline(&allocator);
59512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
596581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
597581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
59812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> mapping_table;
599c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->BuildMappingTable(&mapping_table);
60012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  DefaultSrcMap src_mapping_table;
6018363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky  if (compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()) {
602c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky    codegen->BuildSourceMap(&src_mapping_table);
603c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  }
60412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> vmap_table;
60512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
60612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> gc_map;
60712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
60812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
6092be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledBaseline);
610eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
611aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      compiler_driver,
612aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetInstructionSet(),
613aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      ArrayRef<const uint8_t>(allocator.GetMemory()),
614aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
615aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
616aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
617aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
618aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetCoreSpillMask(),
619aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetFpuSpillMask(),
620aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      &src_mapping_table,
621aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(mapping_table),
622aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(vmap_table),
623aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(gc_map),
624c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
625581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
62669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
627eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
62812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
62912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
630216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
631216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t access_flags,
632216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               InvokeType invoke_type,
633216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint16_t class_def_idx,
634216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t method_idx,
635216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               jobject class_loader,
636216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               const DexFile& dex_file) const {
6376a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNUSED(invoke_type);
6385e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  std::string method_name = PrettyMethod(method_idx, dex_file);
6392be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
640cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
641cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
6428d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
6438d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
6448d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
6458d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
6468fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6478fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
6488fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
6491ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
6502be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
6518fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
6528fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6538fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
654b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
6552be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
656b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
657b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
658b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
65936540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
660432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  // code units is bigger than 128.
661432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
66236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
66336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
66436540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
6652be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
66636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
66736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
66836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
66992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
6709523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item,
67172d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
672cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle    compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
67392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
6743cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
6753cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
6763cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
6773cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
678579ea7d0d89a1d7f02c35ef1beb6328a37259735Nicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetArenaPool());
679e0fe7ae36180863e45cbb9d1e6e9c30b1b1a949cNicolas Geoffray  HGraph* graph = new (&arena) HGraph(
680e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      &arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(),
681e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable());
682e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
6838ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
6848ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
685e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  bool shouldCompile = method_name.find("$opt$") != std::string::npos;
686a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray  bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
6878ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
68812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
689cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
690cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
691cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
692cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
69312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
6945667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
6952be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
696787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
697787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
698c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
6998363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky      compiler_driver->GetCompilerOptions().GetGenerateDebugInfo());
700787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
70169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver pass_observer(graph,
70269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             method_name.c_str(),
70369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             codegen.get(),
70469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             visualizer_output_.get(),
70569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             compiler_driver);
7065e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7079523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  const uint8_t* interpreter_metadata = nullptr;
7089523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  {
7099523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ScopedObjectAccess soa(Thread::Current());
7109523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    StackHandleScope<4> hs(soa.Self());
7119523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ClassLinker* class_linker = dex_compilation_unit.GetClassLinker();
7129523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    Handle<mirror::DexCache> dex_cache(hs.NewHandle(class_linker->FindDexCache(dex_file)));
7139523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    Handle<mirror::ClassLoader> loader(hs.NewHandle(
7149523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa.Decode<mirror::ClassLoader*>(class_loader)));
7159523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ArtMethod* art_method = compiler_driver->ResolveMethod(
7169523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
7179523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // We may not get a method, for example if its class is erroneous.
7189523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile.
7199523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    if (art_method != nullptr) {
7209523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray      interpreter_metadata = art_method->GetQuickenedInfo();
7219523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    }
7229523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  }
7235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphBuilder builder(graph,
7245e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7255e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7265e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_file,
7275e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        compiler_driver,
7289523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray                        compilation_stats_.get(),
7299523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray                        interpreter_metadata);
7305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7315e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  VLOG(compiler) << "Building " << method_name;
7325e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
733809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
73469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
735809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (!builder.BuildGraph(*code_item)) {
736335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray      DCHECK(!(IsCompilingWithCoreImage() && shouldCompile))
737335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray          << "Could not build graph in optimizing compiler";
73869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      pass_observer.SetGraphInBadState();
739809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      return nullptr;
740809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
7415e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
742a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
74348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
744216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
745216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // `run_optimizations_` is set explicitly (either through a compiler filter
746216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
747216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // to Quick.
7482e7cd752452d02499a2f5fbd604c5427aa372f00Nicolas Geoffray  bool can_use_baseline = !run_optimizations_ && builder.CanUseBaselineForStringInit();
749ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil  if (run_optimizations_ && can_allocate_registers) {
7505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Optimizing " << method_name;
7515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
752809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
75369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer);
754809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      if (!graph->TryBuildingSsa()) {
755809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        // We could not transform the graph to SSA, bailout.
756809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
7572be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle        MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
758ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil        pass_observer.SetGraphInBadState();
759809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        return nullptr;
760809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      }
761f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
7625e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
763bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil    return CompileOptimized(graph,
764bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil                            codegen.get(),
765bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil                            compiler_driver,
766bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil                            dex_compilation_unit,
767bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil                            &pass_observer);
768bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  } else if (shouldOptimize && can_allocate_registers) {
76986dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
7705667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
771216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else if (can_use_baseline) {
7725e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Compile baseline " << method_name;
77348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
77448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    if (!run_optimizations_) {
7752be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedDisabled);
77648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_allocate_registers) {
7772be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
77848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    }
77948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
780eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    return CompileBaseline(codegen.get(),
781eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                           compiler_driver,
782eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                           dex_compilation_unit,
78369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                           &pass_observer);
784216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else {
785216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return nullptr;
786216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
787216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
788216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
789216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
790216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
791216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
792216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
793216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
794f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
795216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            const DexFile& dex_file) const {
796f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
797f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
7980760a81257fa427646c309500d603194009265efAndreas Gampe  DCHECK(!compiler_driver->GetVerifiedMethod(&dex_file, method_idx)->HasRuntimeThrow());
7990760a81257fa427646c309500d603194009265efAndreas Gampe  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)) {
800f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle     method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
801f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                         method_idx, jclass_loader, dex_file);
802f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  } else {
803f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
8042be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
805f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    } else {
8062be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified);
807f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
808f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
809f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
810216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  if (method != nullptr) {
811216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return method;
8123946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
81312be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  method = delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
814f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                              jclass_loader, dex_file);
81512be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray
81612be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  if (method != nullptr) {
8172be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kCompiledQuick);
81812be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  }
81912be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
820b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
821b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
82253c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
82353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
82453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
82553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
826335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() {
827335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  const std::string& image = Runtime::Current()->GetImageLocation();
828335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art");
829335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray}
830335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray
831b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
832