optimizing_compiler.cc revision 4a34277c55279ba57ab361f7580db846a201d9b1
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
23b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#include "dex_cache_array_fixups_arm.h"
24b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif
25b4536b7de576b20c74c612406c5d3132998075efVladimir Marko
26b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm64
2744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#include "instruction_simplifier_arm64.h"
2844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
2944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
300616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
310f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko#include "pc_relative_fixups_x86.h"
320616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
330616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell
34e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
35b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
36f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko#include "base/arena_containers.h"
375e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h"
38c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir Marko#include "base/macros.h"
395e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h"
4046e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h"
41f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h"
42787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h"
43787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h"
4420f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h"
4553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h"
4675be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h"
4775be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h"
4871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
49f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h"
50f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
51787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
529523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h"
5320f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
5492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
55e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
5669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h"
57f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
58d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
5922af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h"
60e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
613c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
6271fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
63d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray#include "jit/jit_code_cache.h"
6482091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
65e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
668df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang#include "load_store_elimination.h"
67787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
6826a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
69f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
70a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
71dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko#include "sharpening.h"
72827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
733159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
747dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
75804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
76c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
77175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h"
78b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
79b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
80b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
81787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
82787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
83787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
84787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
85787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
86f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  explicit CodeVectorAllocator(ArenaAllocator* arena)
87f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
88f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko        size_(0) {}
89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
9292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
93787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
97f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
100f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> memory_;
101787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
102787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
103787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
104787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
105787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
106f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
107f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
108ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
109f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
11053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = "";
111f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
11269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope;
113809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
11469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject {
1155e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
11669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver(HGraph* graph,
11769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CodeGenerator* codegen,
11869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               std::ostream* visualizer_output,
11969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CompilerDriver* compiler_driver)
12069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      : graph_(graph),
1210d1caa5df8d99320036888600190337bbe540731Vladimir Marko        cached_method_name_(),
122809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1230d1caa5df8d99320036888600190337bbe540731Vladimir Marko        timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
124eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames        disasm_info_(graph->GetArena()),
125809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
12669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        visualizer_(visualizer_output, graph, *codegen),
12769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        graph_in_bad_state_(false) {
12853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (timing_logger_enabled_ || visualizer_enabled_) {
1290d1caa5df8d99320036888600190337bbe540731Vladimir Marko      if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
13053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        timing_logger_enabled_ = visualizer_enabled_ = false;
13153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
13253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (visualizer_enabled_) {
1330d1caa5df8d99320036888600190337bbe540731Vladimir Marko        visualizer_.PrintHeader(GetMethodName());
13453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        codegen->SetDisassemblyInformation(&disasm_info_);
13553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
13662e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1375e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1385e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
13969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassObserver() {
140809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
1410d1caa5df8d99320036888600190337bbe540731Vladimir Marko      LOG(INFO) << "TIMINGS " << GetMethodName();
142809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
143809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
144809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
145809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
146eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  void DumpDisassembly() const {
147eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (visualizer_enabled_) {
148eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      visualizer_.DumpGraphWithDisassembly();
149eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
150eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
151eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
15269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  void SetGraphInBadState() { graph_in_bad_state_ = true; }
15369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
1540d1caa5df8d99320036888600190337bbe540731Vladimir Marko  const char* GetMethodName() {
1550d1caa5df8d99320036888600190337bbe540731Vladimir Marko    // PrettyMethod() is expensive, so we delay calling it until we actually have to.
1560d1caa5df8d99320036888600190337bbe540731Vladimir Marko    if (cached_method_name_.empty()) {
1570d1caa5df8d99320036888600190337bbe540731Vladimir Marko      cached_method_name_ = PrettyMethod(graph_->GetMethodIdx(), graph_->GetDexFile());
1580d1caa5df8d99320036888600190337bbe540731Vladimir Marko    }
1590d1caa5df8d99320036888600190337bbe540731Vladimir Marko    return cached_method_name_.c_str();
1600d1caa5df8d99320036888600190337bbe540731Vladimir Marko  }
1610d1caa5df8d99320036888600190337bbe540731Vladimir Marko
162809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
163809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1645e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
166ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
1675e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1685e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1695e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1705e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1715e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1725e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
173809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1745e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1755e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1765e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1775e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1785e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
179ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
1805e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
18169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
18269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    // Validate the HGraph if running in debug mode.
18369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    if (kIsDebugBuild) {
18469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      if (!graph_in_bad_state_) {
18569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        if (graph_->IsInSsaForm()) {
186655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko          SSAChecker checker(graph_);
18769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
18869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
18969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker);
19069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
19169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        } else {
192655e585073ac271cc9afa7c9d6ff5ab4dbe4b72eVladimir Marko          GraphChecker checker(graph_);
19369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
19469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
19569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
19669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
19769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        }
19869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      }
19969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    }
2005e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
2015e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
20253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
20353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
20453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // empty kStringFilter matching all methods.
20553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
20653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
20753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
20853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
21053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // warning when the string is empty.
21153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
21253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
21353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return true;
21453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
21553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
21653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    return false;
21753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  }
21853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
21969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  HGraph* const graph_;
2200d1caa5df8d99320036888600190337bbe540731Vladimir Marko
2210d1caa5df8d99320036888600190337bbe540731Vladimir Marko  std::string cached_method_name_;
2225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
2235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool timing_logger_enabled_;
2245e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  TimingLogger timing_logger_;
2255e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
226eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  DisassemblyInformation disasm_info_;
227eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
2285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool visualizer_enabled_;
2295e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphVisualizer visualizer_;
2305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
23169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // Flag to be set by the compiler if the pass failed and the graph is not
23269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // expected to validate.
23369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  bool graph_in_bad_state_;
234809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
23569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  friend PassScope;
23669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
23769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  DISALLOW_COPY_AND_ASSIGN(PassObserver);
2385e8b137d28c840b128e2488f954cccee3e86db14David Brazdil};
2395e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
24069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject {
241809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public:
24269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassScope(const char *pass_name, PassObserver* pass_observer)
243809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      : pass_name_(pass_name),
24469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        pass_observer_(pass_observer) {
24569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->StartPass(pass_name_);
246809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
247809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
24869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassScope() {
24969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->EndPass(pass_name_);
250809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
251809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
252809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
253809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  const char* const pass_name_;
25469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver* const pass_observer_;
255809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil};
256809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
25753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
25853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
25953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
26088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
26153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
26253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
26353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
26453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
26553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
26653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
26853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
26953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
27053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
271736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          const DexFile& dex_file,
272736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
27353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
27453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
27553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
276216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
277216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
278216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
280e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
28190443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
282216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
283216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
284216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
28553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
286216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
28753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
288ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
28953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
290216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
29153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2922be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2932be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2942be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2952be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2962be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2972be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
298d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method)
299d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      OVERRIDE
300d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      SHARED_REQUIRES(Locks::mutator_lock_);
301d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
30253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
30388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
30488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
30588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
30648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
307d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // Create a 'CompiledMethod' for an optimized graph.
308d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  CompiledMethod* EmitOptimized(ArenaAllocator* arena,
309d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                CodeVectorAllocator* code_allocator,
310d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                CodeGenerator* codegen,
311d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                CompilerDriver* driver) const;
312d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
313d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // Create a 'CompiledMethod' for a non-optimized graph.
314d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  CompiledMethod* EmitBaseline(ArenaAllocator* arena,
315d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                               CodeVectorAllocator* code_allocator,
316d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                               CodeGenerator* codegen,
317d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                               CompilerDriver* driver) const;
318d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
319d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // Try compiling a method and return the code generator used for
320d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // compiling it.
321d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // This method:
322d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // 1) Builds the graph. Returns null if it failed to build it.
323d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // 2) If `run_optimizations_` is set:
324d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  //    2.1) Transform the graph to SSA. Returns null if it failed.
325d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  //    2.2) Run optimizations on the graph, including register allocator.
326d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // 3) Generate code with the `code_allocator` provided.
327d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  CodeGenerator* TryCompile(ArenaAllocator* arena,
328d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            CodeVectorAllocator* code_allocator,
329d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            const DexFile::CodeItem* code_item,
330d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            uint32_t access_flags,
331d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            InvokeType invoke_type,
332d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            uint16_t class_def_idx,
333d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            uint32_t method_idx,
334d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            jobject class_loader,
335d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            const DexFile& dex_file,
336d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            Handle<mirror::DexCache> dex_cache) const;
33712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
3382be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
33988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
34053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
34153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
34253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
34353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
34453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
34588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
34688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
34788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
34888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
34988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
350da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffray          driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime) {}
351ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
352ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
353ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
354ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
355ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
356866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
357866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
358ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
359ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
360ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
36187000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    std::ios_base::openmode cfg_file_mode =
36287000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle        driver->GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
36387000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
364f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
3652be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
3662be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
3672be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
368f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
369787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
370216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
371216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
372216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
37388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
3742be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (compilation_stats_.get() != nullptr) {
3752be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_->Log();
3762be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
37788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
37888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
379da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const {
380216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
381216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
382e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
383e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
384e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
385e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
38653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
38753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
3881ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
389cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle  return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat)
390cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle      || instruction_set == kArm64
3911ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
392f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kMips
3934dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
3941ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
3951ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
3961ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
3971ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
39822ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment.
3990d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain// TODO: Add support for other architectures and remove this function
4000d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillainstatic bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) {
40122ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain  return instruction_set == kArm64
40222ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain      || instruction_set == kThumb2
4033b359c71f2fb784589be113206932e76807787bbRoland Levillain      || instruction_set == kX86
4040d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      || instruction_set == kX86_64;
4050d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain}
4060d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
40710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
40810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
40969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer) {
41010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
41169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
41269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    optimizations[i]->Run();
41310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
41410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
41510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
416ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph,
417dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                            CodeGenerator* codegen,
418ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            CompilerDriver* driver,
419ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            OptimizingCompilerStats* stats,
420ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            const DexCompilationUnit& dex_compilation_unit,
421ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            PassObserver* pass_observer,
422ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            StackHandleScopeCollection* handles) {
423ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
424ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
425ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle      && (compiler_options.GetInlineMaxCodeUnits() > 0);
426ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  if (!should_inline) {
427ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    return;
428ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  }
429cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle  HInliner* inliner = new (graph->GetArena()) HInliner(
430dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko    graph, codegen, dex_compilation_unit, dex_compilation_unit, driver, handles, stats);
431cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle  HOptimization* optimizations[] = { inliner };
432ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
433ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
434ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle}
435ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
43644b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set,
43744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 HGraph* graph,
43844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 OptimizingCompilerStats* stats,
43944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 PassObserver* pass_observer) {
44044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  ArenaAllocator* arena = graph->GetArena();
44144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  switch (instruction_set) {
442b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm
443b4536b7de576b20c74c612406c5d3132998075efVladimir Marko    case kThumb2:
444b4536b7de576b20c74c612406c5d3132998075efVladimir Marko    case kArm: {
445b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats);
446b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      HOptimization* arm_optimizations[] = {
447b4536b7de576b20c74c612406c5d3132998075efVladimir Marko        fixups
448b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      };
449b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer);
450b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      break;
451b4536b7de576b20c74c612406c5d3132998075efVladimir Marko    }
452b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif
45344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
45444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    case kArm64: {
45544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      arm64::InstructionSimplifierArm64* simplifier =
45644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
45744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
45844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
45944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      HOptimization* arm64_optimizations[] = {
46044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        simplifier,
46144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        side_effects,
46244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        gvn
46344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      };
46444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
46544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
46644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    }
46744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
4680616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
4690616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    case kX86: {
4700f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko      x86::PcRelativeFixups* pc_relative_fixups = new (arena) x86::PcRelativeFixups(graph, stats);
4710616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      HOptimization* x86_optimizations[] = {
4720f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko          pc_relative_fixups
4730616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      };
4740616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
4750616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      break;
4760616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    }
4770616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
47844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    default:
47944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
48044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  }
48144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames}
48244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
483d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayNO_INLINE  // Avoid increasing caller's frame size by large stack-allocated objects.
484d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraystatic void AllocateRegisters(HGraph* graph,
485d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                              CodeGenerator* codegen,
486d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                              PassObserver* pass_observer) {
487d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  PrepareForRegisterAllocation(graph).Run();
488d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
489d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  {
490d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
491d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    liveness.Analyze();
492d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
493d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  {
494d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
495d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
496d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
497d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray}
498d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
499e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
500dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                             CodeGenerator* codegen,
501e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
502e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
503e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
504d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                             PassObserver* pass_observer) {
505d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  ScopedObjectAccess soa(Thread::Current());
506d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  StackHandleScopeCollection handles(soa.Self());
507d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  ScopedThreadSuspension sts(soa.Self(), kNative);
508d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
509a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
510a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
511a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
512a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
513a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
514e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
515a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
516e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray  HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph);
517e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
5184a34277c55279ba57ab361f7580db846a201d9b1Aart Bik  HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce");
519a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
520a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
521a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  LICM* licm = new (arena) LICM(graph, *side_effects);
5228df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang  LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects);
52322af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
5244a34277c55279ba57ab361f7580db846a201d9b1Aart Bik  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction);
525a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation =
526d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      new (arena) ReferenceTypePropagation(graph, &handles);
527dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver);
528a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
529a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "instruction_simplifier_after_types");
530a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
531b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_after_bce");
532b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray  InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
533b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_before_codegen");
534a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
535e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver);
53671fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
537ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations1[] = {
538a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
539a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
540a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
541a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation,
542dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko    sharpening,
54318e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
544ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    simplify2
545ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
546ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
547ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
548ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
549d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, &handles);
550951779839f0d35ed5336f399c8f521fd9a6b7c27David Brazdil
55177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  // TODO: Update passes incompatible with try/catch so we have the same
55277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  //       pipeline for all methods.
553771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil  if (graph->HasTryCatch()) {
554771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil    HOptimization* optimizations2[] = {
5558a7c0fe837bb00b02dfcfc678910d81d07fb2136David Brazdil      boolean_simplify,
556771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      side_effects,
557771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      gvn,
558771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      dce2,
559771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // The codegen has a few assumptions that only the instruction simplifier
560771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // can satisfy. For example, the code generator does not expect to see a
561771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // HTypeConversion from a type to the same type.
562771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      simplify4,
563771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil    };
564771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil
565771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil    RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
566771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil  } else {
567771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil    HOptimization* optimizations2[] = {
568771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // BooleanSimplifier depends on the InstructionSimplifier removing
569771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // redundant suspend checks to recognize empty blocks.
570771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      boolean_simplify,
571771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      fold2,  // TODO: if we don't inline we can also skip fold2.
572771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      side_effects,
573771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      gvn,
574771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      licm,
575771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      induction,
576771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      bce,
5774a34277c55279ba57ab361f7580db846a201d9b1Aart Bik      fold3,  // evaluates code generated by dynamic bce
578771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      simplify3,
579771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      lse,
580771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      dce2,
581771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // The codegen has a few assumptions that only the instruction simplifier
582771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // can satisfy. For example, the code generator does not expect to see a
583771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      // HTypeConversion from a type to the same type.
584771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil      simplify4,
585771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil    };
586771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil
587771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil    RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
588771e5cc519665ce0cc76985bb4803f0dd50c3b40David Brazdil  }
58939fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil
59044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer);
591d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  AllocateRegisters(graph, codegen, pass_observer);
5925e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
5935e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
594376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
595376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
596f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Markostatic ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) {
597376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
598376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
599376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
600376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
601376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
602e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
603376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
604376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
605581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
606581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
607581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  codegen->EmitLinkerPatches(&linker_patches);
608581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
609581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Sort patches by literal offset. Required for .oat_patches encoding.
610581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  std::sort(linker_patches.begin(), linker_patches.end(),
611581550137ee3a068a14224870e71aeee924a0646Vladimir Marko            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
612581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    return lhs.LiteralOffset() < rhs.LiteralOffset();
613581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  });
614581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
615581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  return linker_patches;
616581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
617581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
618d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCompiledMethod* OptimizingCompiler::EmitOptimized(ArenaAllocator* arena,
619d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                                  CodeVectorAllocator* code_allocator,
620d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                                  CodeGenerator* codegen,
621d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                                  CompilerDriver* compiler_driver) const {
622581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
623f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
624d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  stack_map.resize(codegen->ComputeStackMapsSize());
625d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()));
62612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
6272be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledOptimized);
62812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
629eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
63012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
63112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
632d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      ArrayRef<const uint8_t>(code_allocator->GetMemory()),
633aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
634aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
635aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
636aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
63712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
638d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
639d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      ArrayRef<const SrcMapElem>(codegen->GetSrcMappingTable()),
640c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // mapping_table.
641c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
642c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // native_gc_map.
643c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
644581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
645ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier
646eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
64712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
64812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
649d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCompiledMethod* OptimizingCompiler::EmitBaseline(
650d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    ArenaAllocator* arena,
651d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    CodeVectorAllocator* code_allocator,
65212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
653d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    CompilerDriver* compiler_driver) const {
654581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
655581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
656f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps));
657c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->BuildMappingTable(&mapping_table);
658f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps));
65912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
660f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps));
661d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, *compiler_driver);
66212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
6632be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledBaseline);
664eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
665aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      compiler_driver,
666aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetInstructionSet(),
667d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      ArrayRef<const uint8_t>(code_allocator->GetMemory()),
668aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
669aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
670aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
671aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
672aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetCoreSpillMask(),
673aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetFpuSpillMask(),
674d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      ArrayRef<const SrcMapElem>(codegen->GetSrcMappingTable()),
675aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(mapping_table),
676aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(vmap_table),
677aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(gc_map),
678c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
679581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
680eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
68112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
68212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
683d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena,
684d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              CodeVectorAllocator* code_allocator,
685d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              const DexFile::CodeItem* code_item,
686d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              uint32_t access_flags,
687d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              InvokeType invoke_type,
688d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              uint16_t class_def_idx,
689d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              uint32_t method_idx,
690d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              jobject class_loader,
691d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              const DexFile& dex_file,
692d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              Handle<mirror::DexCache> dex_cache) const {
6932be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
694cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
695cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
696cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle
6973b359c71f2fb784589be113206932e76807787bbRoland Levillain  // Always use the Thumb-2 assembler: some runtime functionality
6983b359c71f2fb784589be113206932e76807787bbRoland Levillain  // (like implicit stack overflow checks) assume Thumb-2.
6998d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
7008d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
7018fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
7028fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
7038fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
7041ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
7052be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
7068fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
7078fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
7088fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
7090d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // When read barriers are enabled, do not attempt to compile for
7100d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // instruction sets that have no read barrier support.
7110d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) {
7120d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    return nullptr;
7130d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  }
7140d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
715b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
7162be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
717b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
718b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
719b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
72036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
721432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  // code units is bigger than 128.
722432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
72336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
72436540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
72536540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
7262be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
72736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
72836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
72936540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
73092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
7319523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item,
73272d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
733736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache);
73492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
7353cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
7363cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
7373cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
7383cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
739d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  HGraph* graph = new (arena) HGraph(
740d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(),
741e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable());
742e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
74312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
744cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
745cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
746cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
747cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
74812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
7492be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
750787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
751787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
752c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
7538363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky      compiler_driver->GetCompilerOptions().GetGenerateDebugInfo());
754787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
75569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver pass_observer(graph,
75669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             codegen.get(),
75769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             visualizer_output_.get(),
75869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             compiler_driver);
7595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7609523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  const uint8_t* interpreter_metadata = nullptr;
7619523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  {
7629523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ScopedObjectAccess soa(Thread::Current());
763736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    StackHandleScope<1> hs(soa.Self());
7649523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    Handle<mirror::ClassLoader> loader(hs.NewHandle(
7659523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa.Decode<mirror::ClassLoader*>(class_loader)));
7669523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ArtMethod* art_method = compiler_driver->ResolveMethod(
7679523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
7689523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // We may not get a method, for example if its class is erroneous.
7699523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile.
7709523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    if (art_method != nullptr) {
7719523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray      interpreter_metadata = art_method->GetQuickenedInfo();
7729523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    }
7739523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  }
7745e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphBuilder builder(graph,
7755e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7765e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7775e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_file,
7785e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        compiler_driver,
7799523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray                        compilation_stats_.get(),
780736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        interpreter_metadata,
781736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        dex_cache);
7825e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7830d1caa5df8d99320036888600190337bbe540731Vladimir Marko  VLOG(compiler) << "Building " << pass_observer.GetMethodName();
7845e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
785809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
78669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
787809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (!builder.BuildGraph(*code_item)) {
78869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      pass_observer.SetGraphInBadState();
789809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      return nullptr;
790809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
7915e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
792a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
7930d1caa5df8d99320036888600190337bbe540731Vladimir Marko  VLOG(compiler) << "Optimizing " << pass_observer.GetMethodName();
794d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  if (run_optimizations_) {
795809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
79669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer);
797809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      if (!graph->TryBuildingSsa()) {
798809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        // We could not transform the graph to SSA, bailout.
7990d1caa5df8d99320036888600190337bbe540731Vladimir Marko        LOG(INFO) << "Skipping compilation of " << pass_observer.GetMethodName()
8000d1caa5df8d99320036888600190337bbe540731Vladimir Marko            << ": it contains a non natural loop";
8012be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle        MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
802ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil        pass_observer.SetGraphInBadState();
803809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        return nullptr;
804809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      }
805f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
8065e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
807d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    RunOptimizations(graph,
808d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                     codegen.get(),
809d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                     compiler_driver,
810d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                     compilation_stats_.get(),
811d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                     dex_compilation_unit,
812d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                     &pass_observer);
813d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    codegen->CompileOptimized(code_allocator);
814d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  } else {
815d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    codegen->CompileBaseline(code_allocator);
816216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
817d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  pass_observer.DumpDisassembly();
818f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
819f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  if (kArenaAllocatorCountAllocations) {
820d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    if (arena->BytesAllocated() > 4 * MB) {
821d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      MemStats mem_stats(arena->GetMemStats());
822f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
823f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    }
824f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  }
825f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
826d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  return codegen.release();
827216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
828216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
82907380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) {
83007380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved).
83107380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS
83207380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS
83307380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD
83407380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
835175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0;
836175dc732c80e6f2afd83209348124df349290ba8Calin Juravle}
837175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
838216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
839216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
840216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
841216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
842216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
843f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
844736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            const DexFile& dex_file,
845736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            Handle<mirror::DexCache> dex_cache) const {
846f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
847f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
848d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  DCHECK(Runtime::Current()->IsAotCompiler());
849d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
850d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  DCHECK(!verified_method->HasRuntimeThrow());
851d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
852d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      || CanHandleVerificationFailure(verified_method)) {
853d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    ArenaAllocator arena(Runtime::Current()->GetArenaPool());
854d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    CodeVectorAllocator code_allocator(&arena);
855d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    std::unique_ptr<CodeGenerator> codegen(
856d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray        TryCompile(&arena,
857d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   &code_allocator,
858d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   code_item,
859d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   access_flags,
860d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   invoke_type,
861d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   class_def_idx,
862d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   method_idx,
863d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   jclass_loader,
864d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   dex_file,
865d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   dex_cache));
866d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    if (codegen.get() != nullptr) {
867d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      if (run_optimizations_) {
868d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray        method = EmitOptimized(&arena, &code_allocator, codegen.get(), compiler_driver);
8690c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray      } else {
870d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray        method = EmitBaseline(&arena, &code_allocator, codegen.get(), compiler_driver);
8710c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray      }
872f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
8730c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray  } else {
874d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
875d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
876d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    } else {
877d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified);
878d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    }
879f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
880f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
881cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle  if (kIsDebugBuild &&
882cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle      IsCompilingWithCoreImage() &&
8830d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      IsInstructionSetSupported(compiler_driver->GetInstructionSet()) &&
8840d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      (!kEmitCompilerReadBarrier ||
8850d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain       InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) {
8860d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // For testing purposes, we put a special marker on method names
8870d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // that should be compiled with this compiler (when the the
8880d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // instruction set is supported -- and has support for read
8890d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // barriers, if they are enabled). This makes sure we're not
8900d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // regressing.
89109b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    std::string method_name = PrettyMethod(method_idx, dex_file);
89209b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    bool shouldCompile = method_name.find("$opt$") != std::string::npos;
89309b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
89409b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle  }
89509b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle
89612be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
897b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
898b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
89953c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
90053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
90153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
90253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
903335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() {
904335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  const std::string& image = Runtime::Current()->GetImageLocation();
905335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art");
906335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray}
907335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray
908d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraybool OptimizingCompiler::JitCompile(Thread* self,
909d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                    jit::JitCodeCache* code_cache,
910d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                    ArtMethod* method) {
911d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  StackHandleScope<2> hs(self);
912d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
913d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      method->GetDeclaringClass()->GetClassLoader()));
914d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
915d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
916d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  jobject jclass_loader = class_loader.ToJObject();
917d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const DexFile* dex_file = method->GetDexFile();
918d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const uint16_t class_def_idx = method->GetClassDefIndex();
919d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
920d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const uint32_t method_idx = method->GetDexMethodIndex();
921d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const uint32_t access_flags = method->GetAccessFlags();
922d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const InvokeType invoke_type = method->GetInvokeType();
923d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
924d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetArenaPool());
925d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  CodeVectorAllocator code_allocator(&arena);
926d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen;
927d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  {
928d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    // Go to native so that we don't block GC during compilation.
929d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    ScopedThreadSuspension sts(self, kNative);
930d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
931d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    DCHECK(run_optimizations_);
932d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    codegen.reset(
933d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray        TryCompile(&arena,
934d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   &code_allocator,
935d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   code_item,
936d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   access_flags,
937d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   invoke_type,
938d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   class_def_idx,
939d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   method_idx,
940d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   jclass_loader,
941d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   *dex_file,
942d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   dex_cache));
943d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    if (codegen.get() == nullptr) {
944d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      return false;
945d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    }
946d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
947d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
948d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  size_t stack_map_size = codegen->ComputeStackMapsSize();
949d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size);
950d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  if (stack_map_data == nullptr) {
951d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    return false;
952d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
953d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size));
954d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const void* code = code_cache->CommitCode(
955d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      self,
956d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      method,
957d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      nullptr,
958d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      stack_map_data,
959d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      nullptr,
960d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
961d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      codegen->GetCoreSpillMask(),
962d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      codegen->GetFpuSpillMask(),
963d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      code_allocator.GetMemory().data(),
964d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      code_allocator.GetSize());
965d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
966d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  if (code == nullptr) {
967d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    code_cache->ClearData(self, stack_map_data);
968d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    return false;
969d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
970d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
971d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  return true;
972d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray}
973d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
974b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
975