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>
205cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include <memory>
21787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include <stdint.h>
22787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
23d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik#ifdef ART_ENABLE_CODEGEN_arm
24b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#include "dex_cache_array_fixups_arm.h"
25b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif
26b4536b7de576b20c74c612406c5d3132998075efVladimir Marko
27b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm64
2844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#include "instruction_simplifier_arm64.h"
2944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
3044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
310616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
320f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko#include "pc_relative_fixups_x86.h"
330616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
340616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell
35e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
36b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
37f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko#include "base/arena_containers.h"
385e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h"
39c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir Marko#include "base/macros.h"
405e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.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"
48c5bfa97c47d656b76f297af8abcd5f7502987399David Srbecky#include "debug/elf_debug_writer.h"
494fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "debug/method_debug_info.h"
5071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
51f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
524fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "dex/verified_method.h"
539523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h"
5420f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
5592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
56e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
5769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h"
58f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
59d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
6022af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h"
61e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
623c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
634a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko#include "instruction_simplifier_arm.h"
6471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
655cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky#include "jit/debugger_interface.h"
66a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray#include "jit/jit.h"
67d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray#include "jit/jit_code_cache.h"
68e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
694fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "licm.h"
708df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang#include "load_store_elimination.h"
71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
724fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "oat_quick_method_header.h"
7326a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
74f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
75a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
7674eb1b264691c4eb399d0858015a7fc13c476ac6David Brazdil#include "select_generator.h"
77dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko#include "sharpening.h"
78827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
793159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
80804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
814fda4eb799c95be266f52aaf3461a440ea86b841David Srbecky#include "ssa_phi_elimination.h"
82c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
83175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h"
84b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
85b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
86b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
873a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Markostatic constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB;
883a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko
89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
92787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
93787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
94f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  explicit CodeVectorAllocator(ArenaAllocator* arena)
95f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
96f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko        size_(0) {}
97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
99787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
10092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
101787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
102787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
103787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
104787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
105f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
106787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
107787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
108f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> memory_;
109787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
110787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
111787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
112787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
113787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
114f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
115f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
116ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
117f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
11853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = "";
119f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
12069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope;
121809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
12269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject {
1235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
12469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver(HGraph* graph,
12569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CodeGenerator* codegen,
12669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               std::ostream* visualizer_output,
12769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CompilerDriver* compiler_driver)
12869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      : graph_(graph),
1290d1caa5df8d99320036888600190337bbe540731Vladimir Marko        cached_method_name_(),
130809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1310d1caa5df8d99320036888600190337bbe540731Vladimir Marko        timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
132eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames        disasm_info_(graph->GetArena()),
133c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray        visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()),
13469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        visualizer_(visualizer_output, graph, *codegen),
13569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        graph_in_bad_state_(false) {
13653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (timing_logger_enabled_ || visualizer_enabled_) {
1370d1caa5df8d99320036888600190337bbe540731Vladimir Marko      if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
13853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        timing_logger_enabled_ = visualizer_enabled_ = false;
13953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
14053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (visualizer_enabled_) {
1410d1caa5df8d99320036888600190337bbe540731Vladimir Marko        visualizer_.PrintHeader(GetMethodName());
14253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        codegen->SetDisassemblyInformation(&disasm_info_);
14353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
14462e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1455e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1465e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
14769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassObserver() {
148809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
1490d1caa5df8d99320036888600190337bbe540731Vladimir Marko      LOG(INFO) << "TIMINGS " << GetMethodName();
150809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
151809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
152809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
153809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
154eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  void DumpDisassembly() const {
155eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (visualizer_enabled_) {
156eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      visualizer_.DumpGraphWithDisassembly();
157eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
158eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
159eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
16069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  void SetGraphInBadState() { graph_in_bad_state_ = true; }
16169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
1620d1caa5df8d99320036888600190337bbe540731Vladimir Marko  const char* GetMethodName() {
1630d1caa5df8d99320036888600190337bbe540731Vladimir Marko    // PrettyMethod() is expensive, so we delay calling it until we actually have to.
1640d1caa5df8d99320036888600190337bbe540731Vladimir Marko    if (cached_method_name_.empty()) {
1650d1caa5df8d99320036888600190337bbe540731Vladimir Marko      cached_method_name_ = PrettyMethod(graph_->GetMethodIdx(), graph_->GetDexFile());
1660d1caa5df8d99320036888600190337bbe540731Vladimir Marko    }
1670d1caa5df8d99320036888600190337bbe540731Vladimir Marko    return cached_method_name_.c_str();
1680d1caa5df8d99320036888600190337bbe540731Vladimir Marko  }
1690d1caa5df8d99320036888600190337bbe540731Vladimir Marko
170809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
171809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1725e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1735e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
174ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
1755e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1765e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1775e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1785e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1795e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1805e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
181809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1825e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1835e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1845e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1855e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1865e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
187ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
1885e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
18969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
19069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    // Validate the HGraph if running in debug mode.
19169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    if (kIsDebugBuild) {
19269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      if (!graph_in_bad_state_) {
193badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        GraphChecker checker(graph_);
194badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        checker.Run();
195badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        if (!checker.IsValid()) {
196badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil          LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
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
262df739841b781dbd49c247e3795696389c4799020Vladimir Marko  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE;
26353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
26453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
26553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
26653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
26853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
26953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
270736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          const DexFile& dex_file,
271736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
27253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
27353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
27453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
275216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
276216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
277216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
279e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
28090443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
281216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
282216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
283216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
28453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
285ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
28653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
287216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
28853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2902be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2912be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2922be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2932be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2942be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
295b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method, bool osr)
296d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      OVERRIDE
297d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      SHARED_REQUIRES(Locks::mutator_lock_);
298d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
29953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
300d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // Create a 'CompiledMethod' for an optimized graph.
30158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  CompiledMethod* Emit(ArenaAllocator* arena,
30258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil                       CodeVectorAllocator* code_allocator,
30358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil                       CodeGenerator* codegen,
304b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                       CompilerDriver* driver,
305b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                       const DexFile::CodeItem* item) const;
306d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
307d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // Try compiling a method and return the code generator used for
308d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // compiling it.
309d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // This method:
310d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  // 1) Builds the graph. Returns null if it failed to build it.
31158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  // 2) Transforms the graph to SSA. Returns null if it failed.
31258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  // 3) Runs optimizations on the graph, including register allocator.
31358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil  // 4) Generates code with the `code_allocator` provided.
314d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  CodeGenerator* TryCompile(ArenaAllocator* arena,
315d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            CodeVectorAllocator* code_allocator,
316d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            const DexFile::CodeItem* code_item,
317d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            uint32_t access_flags,
318d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            InvokeType invoke_type,
319d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            uint16_t class_def_idx,
320d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            uint32_t method_idx,
321d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            jobject class_loader,
322d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                            const DexFile& dex_file,
323b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                            Handle<mirror::DexCache> dex_cache,
3242dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray                            ArtMethod* method,
325b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                            bool osr) const;
32612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
3272be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
32888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
32953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
33053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
33153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
33253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
33353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
33488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
33588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
33688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
33758282f4510961317b8d5a364a6f740a78926716fDavid Brazdil    : Compiler(driver, kMaximumCompilationTimeBeforeWarning) {}
338ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
339ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
340ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
341ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
342ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
343c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray  const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName();
344866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
345ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
346ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
347ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
34887000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    std::ios_base::openmode cfg_file_mode =
349c903b6af634927479915eaa9516d493eea23f911Nicolas Geoffray        driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
35087000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
351f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
3522be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
3532be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
3542be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
355f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
356787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
357216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
358216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
359216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
36088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
3612be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (compilation_stats_.get() != nullptr) {
3622be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_->Log();
3632be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
36488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
36588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
366e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
367df739841b781dbd49c247e3795696389c4799020Vladimir Marko                                          const DexFile& dex_file ATTRIBUTE_UNUSED) const {
368e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
36953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
37053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
3711ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
372cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle  return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat)
373cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle      || instruction_set == kArm64
3741ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
375f652cecb984c104d44a0223c3c98400ef8ed8ce2Goran Jakovljevic      || instruction_set == kMips
3764dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
3771ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
3781ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
3791ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
3801ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
38122ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment.
3820d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain// TODO: Add support for other architectures and remove this function
3830d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillainstatic bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) {
38422ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain  return instruction_set == kArm64
38522ccc3a93d32fa6991535eaebb17daf5abaf4ebfRoland Levillain      || instruction_set == kThumb2
3863b359c71f2fb784589be113206932e76807787bbRoland Levillain      || instruction_set == kX86
3870d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      || instruction_set == kX86_64;
3880d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain}
3890d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
39010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
39110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
39269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer) {
39310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
39469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
39569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    optimizations[i]->Run();
39610e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
39710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
39810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
399ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph,
400dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                            CodeGenerator* codegen,
401ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            CompilerDriver* driver,
402ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            OptimizingCompilerStats* stats,
403ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            const DexCompilationUnit& dex_compilation_unit,
404ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            PassObserver* pass_observer,
405ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            StackHandleScopeCollection* handles) {
406ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
407ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
408ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle      && (compiler_options.GetInlineMaxCodeUnits() > 0);
409ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  if (!should_inline) {
410ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    return;
411ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  }
4125949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray  size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
413cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle  HInliner* inliner = new (graph->GetArena()) HInliner(
4145949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      graph,
4155949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      graph,
4165949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      codegen,
4175949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      dex_compilation_unit,
4185949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      dex_compilation_unit,
4195949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      driver,
4205949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      handles,
4215949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      stats,
4225949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      number_of_dex_registers,
4235949fa0cb9a8d26ac20b9b02065a63b4b20443beNicolas Geoffray      /* depth */ 0);
424cdfed3dc422d0e1a9a0a948863308e58c39d01baCalin Juravle  HOptimization* optimizations[] = { inliner };
425ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
426ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
427ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle}
428ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
42944b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set,
43044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 HGraph* graph,
431d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik                                 CodeGenerator* codegen,
43244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 OptimizingCompilerStats* stats,
43344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 PassObserver* pass_observer) {
43444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  ArenaAllocator* arena = graph->GetArena();
43544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  switch (instruction_set) {
436b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#ifdef ART_ENABLE_CODEGEN_arm
437b4536b7de576b20c74c612406c5d3132998075efVladimir Marko    case kThumb2:
438b4536b7de576b20c74c612406c5d3132998075efVladimir Marko    case kArm: {
439b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats);
4404a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko      arm::InstructionSimplifierArm* simplifier =
4414a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko          new (arena) arm::InstructionSimplifierArm(graph, stats);
442b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      HOptimization* arm_optimizations[] = {
4434a0dad67867f389e01a5a6c0fe381d210f687c0dArtem Udovichenko        simplifier,
4446b5afdd144d2bb3bf994240797834b5666b2cf98Nicolas Geoffray        fixups
445b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      };
446b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer);
447b4536b7de576b20c74c612406c5d3132998075efVladimir Marko      break;
448b4536b7de576b20c74c612406c5d3132998075efVladimir Marko    }
449b4536b7de576b20c74c612406c5d3132998075efVladimir Marko#endif
45044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
45144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    case kArm64: {
45244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      arm64::InstructionSimplifierArm64* simplifier =
45344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
45444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
45544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
45644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      HOptimization* arm64_optimizations[] = {
45744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        simplifier,
45844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        side_effects,
45944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        gvn
46044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      };
46144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
46244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
46344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    }
46444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
4650616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
4660616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    case kX86: {
467d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik      x86::PcRelativeFixups* pc_relative_fixups =
468d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik          new (arena) x86::PcRelativeFixups(graph, codegen, stats);
4690616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      HOptimization* x86_optimizations[] = {
4700f7dca4ca0be8d2f8776794d35edf8b51b5bc997Vladimir Marko          pc_relative_fixups
4710616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      };
4720616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
4730616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      break;
4740616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    }
4750616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
47644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    default:
47744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
47844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  }
47944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames}
48044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
481d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayNO_INLINE  // Avoid increasing caller's frame size by large stack-allocated objects.
482d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraystatic void AllocateRegisters(HGraph* graph,
483d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                              CodeGenerator* codegen,
484d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                              PassObserver* pass_observer) {
485700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang  {
486700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang    PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName,
487700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang                    pass_observer);
488700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang    PrepareForRegisterAllocation(graph).Run();
489700347e9bc6c2ed29046e0c13122a5ad57d2fc51Mingyao Yang  }
490d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
491d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  {
492d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
493d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    liveness.Analyze();
494d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
495d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  {
496d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
497d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
498d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
499d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray}
500d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
501e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
502dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko                             CodeGenerator* codegen,
503e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
504e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
505e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
5064833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil                             PassObserver* pass_observer,
5074833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil                             StackHandleScopeCollection* handles) {
508a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
509a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
510a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
511a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
512a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
513e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
514a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
51538e9e8046ea2196284bdb4638771c31108a30a4aJean-Philippe Halimi  HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, stats);
516e34648dec914453f7e8b6c517dd272823319cd6dNicolas Geoffray  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
5174a34277c55279ba57ab361f7580db846a201d9b1Aart Bik  HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce");
518a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
519a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
52038e9e8046ea2196284bdb4638771c31108a30a4aJean-Philippe Halimi  LICM* licm = new (arena) LICM(graph, *side_effects, stats);
5218df69d42a9e3ccd9456ff72fac8dbd1999f98755Mingyao Yang  LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects);
52222af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
5234a34277c55279ba57ab361f7580db846a201d9b1Aart Bik  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction);
524dc151b2346bb8a4fdeed0c06e54c2fca21d59b5dVladimir Marko  HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver);
525a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
52668289a531484d26214e09f1eadd9833531a3bc3cAlex Light      graph, stats, "instruction_simplifier_after_bce");
5274833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
528b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_before_codegen");
52938e9e8046ea2196284bdb4638771c31108a30a4aJean-Philippe Halimi  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver, stats);
53071fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
531ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations1[] = {
532a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
5334833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil    sharpening,
534a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
535a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
53618e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
537ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
538ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
539ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
5404833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil  MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles);
541951779839f0d35ed5336f399c8f521fd9a6b7c27David Brazdil
5428993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil  HOptimization* optimizations2[] = {
54374eb1b264691c4eb399d0858015a7fc13c476ac6David Brazdil    // SelectGenerator depends on the InstructionSimplifier removing
5448993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    // redundant suspend checks to recognize empty blocks.
54574eb1b264691c4eb399d0858015a7fc13c476ac6David Brazdil    select_generator,
5468993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    fold2,  // TODO: if we don't inline we can also skip fold2.
5478993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    side_effects,
5488993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    gvn,
5498993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    licm,
5508993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    induction,
5518993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    bce,
5528993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    fold3,  // evaluates code generated by dynamic bce
5534833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil    simplify2,
5548993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    lse,
5558993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    dce2,
5568993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    // The codegen has a few assumptions that only the instruction simplifier
5578993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    // can satisfy. For example, the code generator does not expect to see a
5588993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil    // HTypeConversion from a type to the same type.
5594833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil    simplify3,
5608993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil  };
5618993caf6571863c8e5eec0b2ce4d72f6f1793f31David Brazdil  RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
56239fabd6bb6fcf7a712b370d3b6fd0ada83e2e5d8David Brazdil
563d1c4045fb4d4703642f3f79985727b9a12cf5c49Aart Bik  RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, stats, pass_observer);
564d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  AllocateRegisters(graph, codegen, pass_observer);
5655e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
5665e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
567581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
568581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
569581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  codegen->EmitLinkerPatches(&linker_patches);
570581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
571581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Sort patches by literal offset. Required for .oat_patches encoding.
572581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  std::sort(linker_patches.begin(), linker_patches.end(),
573581550137ee3a068a14224870e71aeee924a0646Vladimir Marko            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
574581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    return lhs.LiteralOffset() < rhs.LiteralOffset();
575581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  });
576581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
577581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  return linker_patches;
578581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
579581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
58058282f4510961317b8d5a364a6f740a78926716fDavid BrazdilCompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena,
58158282f4510961317b8d5a364a6f740a78926716fDavid Brazdil                                         CodeVectorAllocator* code_allocator,
58258282f4510961317b8d5a364a6f740a78926716fDavid Brazdil                                         CodeGenerator* codegen,
583b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                         CompilerDriver* compiler_driver,
584b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                         const DexFile::CodeItem* code_item) const {
585581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
586f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
587d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  stack_map.resize(codegen->ComputeStackMapsSize());
588b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), *code_item);
58912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
590eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
59112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
59212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
593d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      ArrayRef<const uint8_t>(code_allocator->GetMemory()),
594aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
595aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
596aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
597aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
59812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
599d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
600f71b3ade9c99ce2fec2f5049ce9c5968721e1b81David Srbecky      ArrayRef<const SrcMapElem>(),
601c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
602c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
603581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
604ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier
605eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
60612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
60712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
608d28b969c273ab777ca9b147b87fcef671b4f695fNicolas GeoffrayCodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena,
609d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              CodeVectorAllocator* code_allocator,
610d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              const DexFile::CodeItem* code_item,
611d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              uint32_t access_flags,
612d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              InvokeType invoke_type,
613d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              uint16_t class_def_idx,
614d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              uint32_t method_idx,
615d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              jobject class_loader,
616d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                              const DexFile& dex_file,
617b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                              Handle<mirror::DexCache> dex_cache,
6182dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray                                              ArtMethod* method,
619b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                              bool osr) const {
6202be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
621cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
622cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
623cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle
6243b359c71f2fb784589be113206932e76807787bbRoland Levillain  // Always use the Thumb-2 assembler: some runtime functionality
6253b359c71f2fb784589be113206932e76807787bbRoland Levillain  // (like implicit stack overflow checks) assume Thumb-2.
6268d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
6278d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
6288fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6298fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
6308fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
6311ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
6322be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
6338fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
6348fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6358fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
6360d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // When read barriers are enabled, do not attempt to compile for
6370d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  // instruction sets that have no read barrier support.
6380d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) {
6390d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    return nullptr;
6400d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain  }
6410d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain
642b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
6432be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
644b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
645b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
646b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
64736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
648432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  // code units is bigger than 128.
649432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
65036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
65129d38e77c553c6cf71fc4dafe2d22b4e3f814872Andreas Gampe  if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace)
65236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
6532be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
65436540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
65536540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
65636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
65792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
658df739841b781dbd49c247e3795696389c4799020Vladimir Marko      class_loader,
659df739841b781dbd49c247e3795696389c4799020Vladimir Marko      Runtime::Current()->GetClassLinker(),
660df739841b781dbd49c247e3795696389c4799020Vladimir Marko      dex_file,
661df739841b781dbd49c247e3795696389c4799020Vladimir Marko      code_item,
662df739841b781dbd49c247e3795696389c4799020Vladimir Marko      class_def_idx,
663df739841b781dbd49c247e3795696389c4799020Vladimir Marko      method_idx,
664df739841b781dbd49c247e3795696389c4799020Vladimir Marko      access_flags,
665df739841b781dbd49c247e3795696389c4799020Vladimir Marko      /* verified_method */ nullptr,
666df739841b781dbd49c247e3795696389c4799020Vladimir Marko      dex_cache);
66792cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
6683cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
6693cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
6703cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
6713cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
672badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil
673d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  HGraph* graph = new (arena) HGraph(
674b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      arena,
675b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      dex_file,
676b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      method_idx,
677b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      requires_barrier,
678b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      compiler_driver->GetInstructionSet(),
679b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      kInvalidInvokeType,
680b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      compiler_driver->GetCompilerOptions().GetDebuggable(),
681b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      osr);
682e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
683badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  const uint8_t* interpreter_metadata = nullptr;
6842dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray  if (method == nullptr) {
685badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    ScopedObjectAccess soa(Thread::Current());
686badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    StackHandleScope<1> hs(soa.Self());
687badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    Handle<mirror::ClassLoader> loader(hs.NewHandle(
688badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        soa.Decode<mirror::ClassLoader*>(class_loader)));
6892dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray    method = compiler_driver->ResolveMethod(
690badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
6912dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray  }
6922dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray  // For AOT compilation, we may not get a method, for example if its class is erroneous.
6932dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray  // JIT should always have a method.
6942dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray  DCHECK(Runtime::Current()->IsAotCompiler() || method != nullptr);
6952dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray  if (method != nullptr) {
6962dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray    graph->SetArtMethod(method);
6972dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray    ScopedObjectAccess soa(Thread::Current());
6982dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray    interpreter_metadata = method->GetQuickenedInfo();
6992f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    uint16_t type_index = method->GetDeclaringClass()->GetDexTypeIndex();
7002f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray
7012f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    // Update the dex cache if the type is not in it yet. Note that under AOT,
7022f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    // the verifier must have set it, but under JIT, there's no guarantee, as we
7032f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    // don't necessarily run the verifier.
7042f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    // The compiler and the compiler driver assume the compiling class is
7052f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    // in the dex cache.
7062f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    if (dex_cache->GetResolvedType(type_index) == nullptr) {
7072f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray      dex_cache->SetResolvedType(type_index, method->GetDeclaringClass());
7082f116815ae4344c30a9eab1984563b3d969bd769Nicolas Geoffray    }
709badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil  }
710badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil
71112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
712cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
713cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
714cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
7152ae48182573da7087bffc2873730bc758ec29696Calin Juravle                            compiler_driver->GetCompilerOptions(),
7162ae48182573da7087bffc2873730bc758ec29696Calin Juravle                            compilation_stats_.get()));
71712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
7182be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
719787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
720787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
721c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
7225b1c2ca30dad519be285f0a1e839c23cc4e3a51dDavid Srbecky      compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
723787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
72469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver pass_observer(graph,
72569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             codegen.get(),
72669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             visualizer_output_.get(),
72769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             compiler_driver);
7285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7290d1caa5df8d99320036888600190337bbe540731Vladimir Marko  VLOG(compiler) << "Building " << pass_observer.GetMethodName();
7305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
731809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
732badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    ScopedObjectAccess soa(Thread::Current());
733badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    StackHandleScopeCollection handles(soa.Self());
734badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    // Do not hold `mutator_lock_` between optimizations.
735badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    ScopedThreadSuspension sts(soa.Self(), kNative);
736badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil
737badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    {
738badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil      PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
739badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil      HGraphBuilder builder(graph,
740badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                            &dex_compilation_unit,
741badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                            &dex_compilation_unit,
742badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                            &dex_file,
74386ea7eeabe30c98bbe1651a51d03cb89776724e7David Brazdil                            *code_item,
744badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                            compiler_driver,
745badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                            compilation_stats_.get(),
746badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                            interpreter_metadata,
747dee58d6bb6d567fcd0c4f39d8d690c3acaf0e432David Brazdil                            dex_cache,
748dee58d6bb6d567fcd0c4f39d8d690c3acaf0e432David Brazdil                            &handles);
749dee58d6bb6d567fcd0c4f39d8d690c3acaf0e432David Brazdil      GraphAnalysisResult result = builder.BuildGraph();
750badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil      if (result != kAnalysisSuccess) {
751badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        switch (result) {
75286ea7eeabe30c98bbe1651a51d03cb89776724e7David Brazdil          case kAnalysisSkipped:
75386ea7eeabe30c98bbe1651a51d03cb89776724e7David Brazdil            MaybeRecordStat(MethodCompilationStat::kNotCompiledSkipped);
75486ea7eeabe30c98bbe1651a51d03cb89776724e7David Brazdil            break;
755badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil          case kAnalysisInvalidBytecode:
75686ea7eeabe30c98bbe1651a51d03cb89776724e7David Brazdil            MaybeRecordStat(MethodCompilationStat::kNotCompiledInvalidBytecode);
757badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil            break;
758badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil          case kAnalysisFailThrowCatchLoop:
759badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil            MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop);
760badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil            break;
761badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil          case kAnalysisFailAmbiguousArrayOp:
762badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil            MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp);
763badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil            break;
764badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil          case kAnalysisSuccess:
765badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil            UNREACHABLE();
766badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        }
767badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        pass_observer.SetGraphInBadState();
768badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil        return nullptr;
769badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil      }
770809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
771a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
772badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    RunOptimizations(graph,
773badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                     codegen.get(),
774badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                     compiler_driver,
775badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                     compilation_stats_.get(),
776badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                     dex_compilation_unit,
777badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                     &pass_observer,
778badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil                     &handles);
7794833f5a1990c76bc2be89504225fb13cca22bedfDavid Brazdil
780badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    codegen->Compile(code_allocator);
781badd826664896d4a9628a5a89b78016894aa414bDavid Brazdil    pass_observer.DumpDisassembly();
782216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
78358282f4510961317b8d5a364a6f740a78926716fDavid Brazdil
784d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  return codegen.release();
785216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
786216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
787216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
788216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
789216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
790216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
791216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
792f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
793736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            const DexFile& dex_file,
794736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            Handle<mirror::DexCache> dex_cache) const {
795f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
796f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
797d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  DCHECK(Runtime::Current()->IsAotCompiler());
798d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
799d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  DCHECK(!verified_method->HasRuntimeThrow());
800d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
80162e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray      || verifier::MethodVerifier::CanCompilerHandleVerificationFailure(
80262e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray            verified_method->GetEncounteredVerificationFailures())) {
803d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    ArenaAllocator arena(Runtime::Current()->GetArenaPool());
804d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    CodeVectorAllocator code_allocator(&arena);
805d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    std::unique_ptr<CodeGenerator> codegen(
806d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray        TryCompile(&arena,
807d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   &code_allocator,
808d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   code_item,
809d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   access_flags,
810d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   invoke_type,
811d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   class_def_idx,
812d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   method_idx,
813d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   jclass_loader,
814d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   dex_file,
815b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                   dex_cache,
8162dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray                   nullptr,
817b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                   /* osr */ false));
818d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    if (codegen.get() != nullptr) {
819ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle      MaybeRecordStat(MethodCompilationStat::kCompiled);
820b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item);
8213a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko
8223a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko      if (kArenaAllocatorCountAllocations) {
8233a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko        if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
8243a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko          MemStats mem_stats(arena.GetMemStats());
8253a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko          LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
8263a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko        }
8273a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko      }
828f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
8290c3c2668ef44fdbd18d97f9134a85d1a7d561aa4Nicolas Geoffray  } else {
830d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
831d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
832d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    } else {
833ad543383b5883d6e32993efaefc704eff3225ffeCalin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError);
834d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    }
835f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
836f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
837cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle  if (kIsDebugBuild &&
838cff8cc7818dd901d3667025ad0ff654100bbb629Calin Juravle      IsCompilingWithCoreImage() &&
8390d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      IsInstructionSetSupported(compiler_driver->GetInstructionSet()) &&
8400d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain      (!kEmitCompilerReadBarrier ||
8410d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain       InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) {
8420d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // For testing purposes, we put a special marker on method names
8430d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // that should be compiled with this compiler (when the the
8440d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // instruction set is supported -- and has support for read
8450d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // barriers, if they are enabled). This makes sure we're not
8460d5a281c671444bfa75d63caf1427a8c0e6e1177Roland Levillain    // regressing.
84709b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    std::string method_name = PrettyMethod(method_idx, dex_file);
84809b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    bool shouldCompile = method_name.find("$opt$") != std::string::npos;
84909b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle    DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
85009b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle  }
85109b1d6f749437bdd47f61e7c4f924f6d16b59536Calin Juravle
85212be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
853b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
854b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
85553c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
85653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
85753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
85853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
859335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() {
860335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  const std::string& image = Runtime::Current()->GetImageLocation();
861dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  // TODO: This is under-approximating...
862dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) {
863dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao    return true;
864dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  }
865dcdc85bbd569f0ee66c331b4219c19304a616214Jeff Hao  return false;
866335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray}
867335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray
868d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffraybool OptimizingCompiler::JitCompile(Thread* self,
869d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                                    jit::JitCodeCache* code_cache,
870b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                    ArtMethod* method,
871b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                                    bool osr) {
872d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  StackHandleScope<2> hs(self);
873d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
874d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      method->GetDeclaringClass()->GetClassLoader()));
875d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
87662e631a678ef18c80f37862a2dc74004954a8502Nicolas Geoffray  DCHECK(method->IsCompilable());
877d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
878d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  jobject jclass_loader = class_loader.ToJObject();
879d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const DexFile* dex_file = method->GetDexFile();
880d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const uint16_t class_def_idx = method->GetClassDefIndex();
881d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
882d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const uint32_t method_idx = method->GetDexMethodIndex();
883d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const uint32_t access_flags = method->GetAccessFlags();
884d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const InvokeType invoke_type = method->GetInvokeType();
885d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
88625e0456b6ea13eba290b63ea88b6b7120ed89413Nicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetJitArenaPool());
887d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  CodeVectorAllocator code_allocator(&arena);
888d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen;
889d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  {
890d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    // Go to native so that we don't block GC during compilation.
891d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    ScopedThreadSuspension sts(self, kNative);
892d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    codegen.reset(
893d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray        TryCompile(&arena,
894d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   &code_allocator,
895d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   code_item,
896d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   access_flags,
897d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   invoke_type,
898d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   class_def_idx,
899d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   method_idx,
900d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   jclass_loader,
901d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray                   *dex_file,
902b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                   dex_cache,
9032dc77ecf375882f51ff7c09712c05b80e58abb6bNicolas Geoffray                   method,
904b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray                   osr));
905d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    if (codegen.get() == nullptr) {
906d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      return false;
907d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    }
9083a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko
9093a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko    if (kArenaAllocatorCountAllocations) {
9103a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko      if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
9113a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko        MemStats mem_stats(arena.GetMemStats());
9123a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko        LOG(INFO) << PrettyMethod(method_idx, *dex_file) << " " << Dumpable<MemStats>(mem_stats);
9133a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko      }
9143a40bf2fc1b9823e3bc8fcf96d5242668b5c088bVladimir Marko    }
915d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
916d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
917d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  size_t stack_map_size = codegen->ComputeStackMapsSize();
918933330a192d43d7c7304120999907857a89c2f96Nicolas Geoffray  uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size, method);
919d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  if (stack_map_data == nullptr) {
920d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    return false;
921d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
92273be1e8f8609708f6624bb297c9628de44fd8b6fNicolas Geoffray  MaybeRecordStat(MethodCompilationStat::kCompiled);
923b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray  codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), *code_item);
924d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  const void* code = code_cache->CommitCode(
925d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      self,
926d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      method,
927d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      stack_map_data,
928d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
929d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      codegen->GetCoreSpillMask(),
930d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      codegen->GetFpuSpillMask(),
931d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray      code_allocator.GetMemory().data(),
932b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      code_allocator.GetSize(),
933b331febbab8e916680faba722cc84b66b84218a3Nicolas Geoffray      osr);
934d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
935d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  if (code == nullptr) {
936d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    code_cache->ClearData(self, stack_map_data);
937d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray    return false;
938d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  }
939d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
94091cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky  const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
94191cc06c1814bd1d0fd6635bc3d7632a2bb7b0e7cDavid Srbecky  if (compiler_options.GetGenerateDebugInfo()) {
9425cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky    const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
9435cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky    const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
94409c2a6be63337ee060e2d54bd01cf18be7301d29David Srbecky    debug::MethodDebugInfo info = debug::MethodDebugInfo();
94509c2a6be63337ee060e2d54bd01cf18be7301d29David Srbecky    info.trampoline_name = nullptr;
946197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.dex_file = dex_file;
947197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.class_def_index = class_def_idx;
948197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.dex_method_index = method_idx;
949197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.access_flags = access_flags;
950197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.code_item = code_item;
951197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.isa = codegen->GetInstructionSet();
952197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.deduped = false;
953197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.is_native_debuggable = compiler_options.GetNativeDebuggable();
954197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.is_optimized = true;
955197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.is_code_address_text_relative = false;
956197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.code_address = code_address;
957197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.code_size = code_allocator.GetSize();
958197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
959197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
960197160d47f34238cb5e7444fa4c2de300db8e2c6David Srbecky    info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
961d1ee80948144526b985afb44a0574248cf7da58aVladimir Marko    std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForMethods(
9625d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky        GetCompilerDriver()->GetInstructionSet(),
9635d8112029d0e085c5a0099257daa4c7e29c12310David Srbecky        GetCompilerDriver()->GetInstructionSetFeatures(),
964fe736b775c7774bb2f7072e022099ca47dd21a3cDavid Srbecky        ArrayRef<const debug::MethodDebugInfo>(&info, 1));
965d1ee80948144526b985afb44a0574248cf7da58aVladimir Marko    CreateJITCodeEntryForAddress(code_address, std::move(elf_file));
9665cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky  }
9675cc349f3dd578e974f78314c50b6a0267c23e591David Srbecky
968a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray  Runtime::Current()->GetJit()->AddMemoryUsage(method, arena.BytesUsed());
969a4f81546373f4cb5fa6dfc135307ee0a1d930872Nicolas Geoffray
970d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray  return true;
971d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray}
972d28b969c273ab777ca9b147b87fcef671b4f695fNicolas Geoffray
973b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
974