optimizing_compiler.cc revision 39fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8
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"
34c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir Marko#include "base/macros.h"
355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h"
3646e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h"
37f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h"
38787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h"
39787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h"
4020f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h"
4153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h"
4275be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h"
4375be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h"
4471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
45f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h"
46f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
489523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h"
4920f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
5092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
51e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
5269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h"
53f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
54d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
5522af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h"
56e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
573c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
5871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
5982091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
60e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
618df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang#include "load_store_elimination.h"
62787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
6326a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
64f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
65a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
66dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko#include "sharpening.h"
67827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
683159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
697dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
70804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
71c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
72175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h"
73b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
74b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
75b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
76787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
77787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
78787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
79787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
80787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
81f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  explicit CodeVectorAllocator(ArenaAllocator* arena)
82f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
83f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko        size_(0) {}
84787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
85787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
8792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
88787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
92f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
93787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
95f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> memory_;
96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
100787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
101f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
102f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
103ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
104f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
10553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = "";
106f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
10769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope;
108809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
10969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject {
1105e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
11169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver(HGraph* graph,
11269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               const char* method_name,
11369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CodeGenerator* codegen,
11469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               std::ostream* visualizer_output,
11569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CompilerDriver* compiler_driver)
11669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      : graph_(graph),
11769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        method_name_(method_name),
118809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1195e8b137d28c840b128e2488f954cccee3e86db14David Brazdil        timing_logger_(method_name, true, true),
120eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames        disasm_info_(graph->GetArena()),
121809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
12269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        visualizer_(visualizer_output, graph, *codegen),
12369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        graph_in_bad_state_(false) {
12453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (timing_logger_enabled_ || visualizer_enabled_) {
12553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (!IsVerboseMethod(compiler_driver, method_name)) {
12653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        timing_logger_enabled_ = visualizer_enabled_ = false;
12753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
12853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (visualizer_enabled_) {
12953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        visualizer_.PrintHeader(method_name_);
13053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        codegen->SetDisassemblyInformation(&disasm_info_);
13153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
13262e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1345e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
13569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassObserver() {
136809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
137809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << "TIMINGS " << method_name_;
138809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
139809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
140809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
141809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
142eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  void DumpDisassembly() const {
143eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (visualizer_enabled_) {
144eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      visualizer_.DumpGraphWithDisassembly();
145eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
146eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
147eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
14869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  void SetGraphInBadState() { graph_in_bad_state_ = true; }
14969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
150809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
151809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
154ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
1555e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
161809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1625e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1635e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1645e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1665e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
167ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
1685e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
16969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
17069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    // Validate the HGraph if running in debug mode.
17169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    if (kIsDebugBuild) {
17269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      if (!graph_in_bad_state_) {
17369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        if (graph_->IsInSsaForm()) {
174655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko          SSAChecker checker(graph_);
17569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
17669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
17769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker);
17869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
17969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        } else {
180655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko          GraphChecker checker(graph_);
18169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
18269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
18369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
18469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
18569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        }
18669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      }
18769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    }
1885e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1895e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
19053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
19153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
19253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // empty kStringFilter matching all methods.
19353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
19453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
19553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
19653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
19753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
19853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // warning when the string is empty.
19953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
20053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
20153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return true;
20253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
20353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    return false;
20553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  }
20653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  HGraph* const graph_;
2085e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  const char* method_name_;
2095e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
2105e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool timing_logger_enabled_;
2115e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  TimingLogger timing_logger_;
2125e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
213eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  DisassemblyInformation disasm_info_;
214eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
2155e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool visualizer_enabled_;
2165e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphVisualizer visualizer_;
2175e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
21869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // Flag to be set by the compiler if the pass failed and the graph is not
21969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // expected to validate.
22069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  bool graph_in_bad_state_;
221809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
22269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  friend PassScope;
22369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
22469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  DISALLOW_COPY_AND_ASSIGN(PassObserver);
2255e8b137d28c840b128e2488f954cccee3e86db14David Brazdil};
2265e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
22769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject {
228809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public:
22969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassScope(const char *pass_name, PassObserver* pass_observer)
230809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      : pass_name_(pass_name),
23169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        pass_observer_(pass_observer) {
23269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->StartPass(pass_name_);
233809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
234809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
23569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassScope() {
23669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->EndPass(pass_name_);
237809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
238809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
239809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
240809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  const char* const pass_name_;
24169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver* const pass_observer_;
242809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil};
243809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
24453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
24553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
24653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
24788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
24853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
25053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
25153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
25253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
25353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
25453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
25553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
25653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
25753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
258736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          const DexFile& dex_file,
259736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
26053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
261216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
262216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t access_flags,
263216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             InvokeType invoke_type,
264216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint16_t class_def_idx,
265216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t method_idx,
266216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             jobject class_loader,
267736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             const DexFile& dex_file,
268736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             Handle<mirror::DexCache> dex_cache) const;
269216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
27053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
27153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
272216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
273216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
274216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
276e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
27790443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
278216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
279216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
280216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
28153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
282216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
28353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
284ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
28553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
286216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
28753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2882be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2902be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2912be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2922be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2932be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
29453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
29588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
29688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
29788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
29848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
29912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Optimize and compile `graph`.
30012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileOptimized(HGraph* graph,
30112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CodeGenerator* codegen,
30212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CompilerDriver* driver,
30312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const DexCompilationUnit& dex_compilation_unit,
30469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                   PassObserver* pass_observer) const;
30512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
30612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Just compile without doing optimizations.
30712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileBaseline(CodeGenerator* codegen,
30812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  CompilerDriver* driver,
309eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                  const DexCompilationUnit& dex_compilation_unit,
31069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                  PassObserver* pass_observer) const;
31112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
3122be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
31388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
31453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
31553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
31653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
31753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
31853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
31988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
32088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
32188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
32288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
32388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
324da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffray          driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime) {}
325ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
326ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
327ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
328ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
329ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
330866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
331866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
332ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
333ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
334ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
33587000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    std::ios_base::openmode cfg_file_mode =
33687000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle        driver->GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
33787000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
338f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
3392be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
3402be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
3412be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
342f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
343787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
344216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
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
353da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const {
354216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
355216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
356e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
357e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
358e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
359e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
36053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
36153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
3621ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
363cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle  return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat)
364cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle      || instruction_set == kArm64
3651ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
366f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kMips
3674dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
3681ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
3691ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
3701ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
3711ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
37210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
37310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
37469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer) {
37510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
37669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
37769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    optimizations[i]->Run();
37810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
37910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
38010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
381ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph,
382dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                            CodeGenerator* codegen,
383ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            CompilerDriver* driver,
384ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            OptimizingCompilerStats* stats,
385ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            const DexCompilationUnit& dex_compilation_unit,
386ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            PassObserver* pass_observer,
387ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            StackHandleScopeCollection* handles) {
388ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
389ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
390ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle      && (compiler_options.GetInlineMaxCodeUnits() > 0);
391ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  if (!should_inline) {
392ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    return;
393ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  }
394ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
395271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle  ArenaAllocator* arena = graph->GetArena();
396271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle  HInliner* inliner = new (arena) HInliner(
397dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko    graph, codegen, dex_compilation_unit, dex_compilation_unit, driver, handles, stats);
398271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle  ReferenceTypePropagation* type_propagation =
399271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle    new (arena) ReferenceTypePropagation(graph, handles,
400271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle        "reference_type_propagation_after_inlining");
401271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle
402271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle  HOptimization* optimizations[] = {
403271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle    inliner,
404271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle    // Run another type propagation phase: inlining will open up more opportunities
405271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle    // to remove checkcast/instanceof and null checks.
406271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle    type_propagation,
407271743601650308c7ac5c7a3ec35025d8130a298Calin Juravle  };
408ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
409ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
410ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle}
411ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
41244b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set,
41344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 HGraph* graph,
41444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 OptimizingCompilerStats* stats,
41544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 PassObserver* pass_observer) {
41644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  ArenaAllocator* arena = graph->GetArena();
41744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  switch (instruction_set) {
41844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
41944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    case kArm64: {
42044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      arm64::InstructionSimplifierArm64* simplifier =
42144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
42244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
42344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
42444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      HOptimization* arm64_optimizations[] = {
42544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        simplifier,
42644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        side_effects,
42744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        gvn
42844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      };
42944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
43044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
43144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    }
43244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
4330616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
4340616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    case kX86: {
4350616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      x86::ConstantAreaFixups* constant_area_fixups =
4360616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell          new (arena) x86::ConstantAreaFixups(graph, stats);
4370616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      HOptimization* x86_optimizations[] = {
4380616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell        constant_area_fixups
4390616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      };
4400616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
4410616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      break;
4420616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    }
4430616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
44444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    default:
44544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
44644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  }
44744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames}
44844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
449e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
450dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                             CodeGenerator* codegen,
451e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
452e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
453e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
45469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer,
455acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle                             StackHandleScopeCollection* handles) {
456a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
457a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
458a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
459a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
460a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
461a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
462a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
463a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph);
464a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
465a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
466a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
467a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  LICM* licm = new (arena) LICM(graph, *side_effects);
4688df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang  LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects);
46922af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
47022af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, induction);
471a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation =
472a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      new (arena) ReferenceTypePropagation(graph, handles);
473dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver);
474a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
475a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "instruction_simplifier_after_types");
476a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
477b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_after_bce");
478b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray  InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
479b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_before_codegen");
480a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
481a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver);
48271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
483ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations1[] = {
484a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
485a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
486a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
487a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation,
488dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko    sharpening,
48918e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
490ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    simplify2
491ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
492ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
493ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
494ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
49577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  // TODO: Update passes incompatible with try/catch so we have the same
49677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  //       pipeline for all methods.
49739fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil  if (!graph->HasTryCatch()) {
498dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko    MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles);
499bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  }
500bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil
50139fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil  HOptimization* optimizations2[] = {
50239fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    // BooleanSimplifier depends on the InstructionSimplifier removing
50339fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    // redundant suspend checks to recognize empty blocks.
50439fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    boolean_simplify,
50539fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    fold2,  // TODO: if we don't inline we can also skip fold2.
50639fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    side_effects,
50739fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    gvn,
50839fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    licm,
50939fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    induction,
51039fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    bce,
51139fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    simplify3,
51239fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    lse,
51339fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    dce2,
51439fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    // The codegen has a few assumptions that only the instruction simplifier
51539fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    // can satisfy. For example, the code generator does not expect to see a
51639fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    // HTypeConversion from a type to the same type.
51739fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil    simplify4,
51839fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil  };
51939fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil
52039fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil  RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
52139fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil
52244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer);
5235e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
5245e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
525376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
526376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
527f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Markostatic ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) {
528376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
529376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
530376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
531376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
532376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
533e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
534376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
535376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
536c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir MarkoNO_INLINE  // Avoid increasing caller's frame size by large stack-allocated objects.
537c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampestatic void AllocateRegisters(HGraph* graph,
538c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe                              CodeGenerator* codegen,
53969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                              PassObserver* pass_observer) {
540c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  PrepareForRegisterAllocation(graph).Run();
5410d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
542c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
54369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
544c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    liveness.Analyze();
545c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
546c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
54769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
548c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
549c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
550c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe}
55112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
552581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
553581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
554581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  codegen->EmitLinkerPatches(&linker_patches);
555581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
556581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Sort patches by literal offset. Required for .oat_patches encoding.
557581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  std::sort(linker_patches.begin(), linker_patches.end(),
558581550137ee3a068a14224870e71aeee924a0646Vladimir Marko            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
559581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    return lhs.LiteralOffset() < rhs.LiteralOffset();
560581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  });
561581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
562581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  return linker_patches;
563581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
564581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
56512df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
56612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CodeGenerator* codegen,
56712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CompilerDriver* compiler_driver,
56812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const DexCompilationUnit& dex_compilation_unit,
56969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                                     PassObserver* pass_observer) const {
570ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  ScopedObjectAccess soa(Thread::Current());
571ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  StackHandleScopeCollection handles(soa.Self());
572ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromRunnableToSuspended(kNative);
573dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  RunOptimizations(graph,
574dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                   codegen,
575dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                   compiler_driver,
576dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                   compilation_stats_.get(),
577dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                   dex_compilation_unit,
578dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                   pass_observer,
579dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                   &handles);
58012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
58169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  AllocateRegisters(graph, codegen, pass_observer);
58212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
583f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = graph->GetArena();
584f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
5852a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
5862a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
5872a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
5882a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
58912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileOptimized(&allocator);
59012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
591581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
592581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
593f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
59412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildStackMaps(&stack_map);
59512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
5962be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledOptimized);
59712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
598eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
59912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
60012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
60112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(allocator.GetMemory()),
602aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
603aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
604aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
605aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
60612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
607d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
60835831e8bfa1c0944d4c978d99c4c5b9577945170Vladimir Marko      ArrayRef<const SrcMapElem>(src_mapping_table),
609c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // mapping_table.
610c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
611c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // native_gc_map.
612c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
613581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
61469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
615ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier
616ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromSuspendedToRunnable();
617eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
61812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
61912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
62012df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline(
62112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
62212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CompilerDriver* compiler_driver,
623eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    const DexCompilationUnit& dex_compilation_unit,
62469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassObserver* pass_observer) const {
625f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = codegen->GetGraph()->GetArena();
626f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
6272a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
6282a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
6292a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
6302a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
63112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileBaseline(&allocator);
63212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
633581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
634581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
635f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps));
636c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->BuildMappingTable(&mapping_table);
637f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps));
63812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
639f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps));
64012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
64112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
6422be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledBaseline);
643eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
644aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      compiler_driver,
645aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetInstructionSet(),
646aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      ArrayRef<const uint8_t>(allocator.GetMemory()),
647aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
648aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
649aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
650aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
651aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetCoreSpillMask(),
652aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetFpuSpillMask(),
65335831e8bfa1c0944d4c978d99c4c5b9577945170Vladimir Marko      ArrayRef<const SrcMapElem>(src_mapping_table),
654aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(mapping_table),
655aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(vmap_table),
656aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(gc_map),
657c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
658581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
65969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
660eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
66112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
66212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
663216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
664216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t access_flags,
665216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               InvokeType invoke_type,
666216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint16_t class_def_idx,
667216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t method_idx,
668216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               jobject class_loader,
669736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               const DexFile& dex_file,
670736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               Handle<mirror::DexCache> dex_cache) const {
6715e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  std::string method_name = PrettyMethod(method_idx, dex_file);
6722be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
673cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
674cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
675cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle
6768d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
6778d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
6788d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
6798d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
6808fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6818fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
6828fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
6831ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
6842be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
6858fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
6868fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6878fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
688b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
6892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
690b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
691b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
692b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
69336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
694432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  // code units is bigger than 128.
695432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
69636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
69736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
69836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
6992be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
70036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
70136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
70236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
70392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
7049523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item,
70572d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
706736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache);
70792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
7083cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
7093cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
7103cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
7113cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
712579ea7d0d89a1d7f02c35ef1beb6328a37259735Nicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetArenaPool());
713e0fe7ae36180863e45cbb9d1e6e9c30b1b1a949cNicolas Geoffray  HGraph* graph = new (&arena) HGraph(
714e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      &arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(),
715e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable());
716e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
717a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray  bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
7188ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
71912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
720cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
721cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
722cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
723cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
72412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
7252be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
726787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
727787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
728c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
7298363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky      compiler_driver->GetCompilerOptions().GetGenerateDebugInfo());
730787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
73169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver pass_observer(graph,
73269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             method_name.c_str(),
73369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             codegen.get(),
73469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             visualizer_output_.get(),
73569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             compiler_driver);
7365e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7379523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  const uint8_t* interpreter_metadata = nullptr;
7389523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  {
7399523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ScopedObjectAccess soa(Thread::Current());
740736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    StackHandleScope<1> hs(soa.Self());
7419523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    Handle<mirror::ClassLoader> loader(hs.NewHandle(
7429523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa.Decode<mirror::ClassLoader*>(class_loader)));
7439523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ArtMethod* art_method = compiler_driver->ResolveMethod(
7449523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
7459523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // We may not get a method, for example if its class is erroneous.
7469523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile.
7479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    if (art_method != nullptr) {
7489523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray      interpreter_metadata = art_method->GetQuickenedInfo();
7499523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    }
7509523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  }
7515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphBuilder builder(graph,
7525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_file,
7555e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        compiler_driver,
7569523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray                        compilation_stats_.get(),
757736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        interpreter_metadata,
758736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        dex_cache);
7595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  VLOG(compiler) << "Building " << method_name;
7615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
762809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
76369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
764809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (!builder.BuildGraph(*code_item)) {
76569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      pass_observer.SetGraphInBadState();
766809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      return nullptr;
767809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
7685e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
769a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
77048c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
771216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
772216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // `run_optimizations_` is set explicitly (either through a compiler filter
773216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
774216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // to Quick.
7752e7cd752452d02499a2f5fbd604c5427aa372f00Nicolas Geoffray  bool can_use_baseline = !run_optimizations_ && builder.CanUseBaselineForStringInit();
776f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CompiledMethod* compiled_method = nullptr;
777ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil  if (run_optimizations_ && can_allocate_registers) {
7785e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Optimizing " << method_name;
7795e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
780809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
78169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer);
782809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      if (!graph->TryBuildingSsa()) {
783809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        // We could not transform the graph to SSA, bailout.
784809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
7852be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle        MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
786ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil        pass_observer.SetGraphInBadState();
787809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        return nullptr;
788809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      }
789f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
7905e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
791f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileOptimized(graph,
792f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       codegen.get(),
793f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       compiler_driver,
794f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       dex_compilation_unit,
795f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       &pass_observer);
796bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  } else if (shouldOptimize && can_allocate_registers) {
79786dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
7985667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
799216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else if (can_use_baseline) {
8005e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Compile baseline " << method_name;
80148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
80248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    if (!run_optimizations_) {
8032be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedDisabled);
80448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_allocate_registers) {
8052be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
80648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    }
80748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
808f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileBaseline(codegen.get(),
809f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      compiler_driver,
810f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      dex_compilation_unit,
811f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      &pass_observer);
812216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
813f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
814f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  if (kArenaAllocatorCountAllocations) {
815f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    if (arena.BytesAllocated() > 4 * MB) {
816f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      MemStats mem_stats(arena.GetMemStats());
817f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
818f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    }
819f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  }
820f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
821f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  return compiled_method;
822216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
823216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
82407380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) {
82507380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved).
82607380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS
82707380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS
82807380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD
82907380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
830175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0;
831175dc732c80e6f2afd83209348124df349290ba8Calin Juravle}
832175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
833216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
834216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
835216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
836216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
837216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
838f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
839736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            const DexFile& dex_file,
840736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            Handle<mirror::DexCache> dex_cache) const {
841f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
842f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
8430c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray  if (Runtime::Current()->IsAotCompiler()) {
8440c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray    const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
8450c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray    DCHECK(!verified_method->HasRuntimeThrow());
8460c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray    if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
8470c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray        || CanHandleVerificationFailure(verified_method)) {
8480c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray       method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
8490c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray                           method_idx, jclass_loader, dex_file, dex_cache);
850f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    } else {
8510c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray      if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
8520c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray        MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
8530c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray      } else {
8540c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray        MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified);
8550c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray      }
856f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
8570c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray  } else {
8580c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray    // This is for the JIT compiler, which has already ensured the class is verified.
8590c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray    // We can go straight to compiling.
8600c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray    DCHECK(Runtime::Current()->UseJit());
8610c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray    method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
8620c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray                        method_idx, jclass_loader, dex_file, dex_cache);
863f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
864f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
865cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle  if (kIsDebugBuild &&
866cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle      IsCompilingWithCoreImage() &&
867cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle      IsInstructionSetSupported(compiler_driver->GetInstructionSet())) {
86809b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    // For testing purposes, we put a special marker on method names that should be compiled
86909b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    // with this compiler. This makes sure we're not regressing.
87009b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    std::string method_name = PrettyMethod(method_idx, dex_file);
87109b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    bool shouldCompile = method_name.find("$opt$") != std::string::npos;
87209b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
87309b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle  }
87409b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle
87512be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
876b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
877b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
87853c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
87953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
88053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
88153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
882335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() {
883335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  const std::string& image = Runtime::Current()->GetImageLocation();
884335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art");
885335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray}
886335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray
887b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
888