optimizing_compiler.cc revision da3a55bd07e1907d4286dd1b6de77ff42d861e16
1b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray/*
2b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *
4b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * you may not use this file except in compliance with the License.
6b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * You may obtain a copy of the License at
7b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *
8b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *
10b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * See the License for the specific language governing permissions and
14b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * limitations under the License.
15b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray */
16b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
1753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "optimizing_compiler.h"
1853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
19f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include <fstream>
20787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include <stdint.h>
21787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
2244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
2344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#include "instruction_simplifier_arm64.h"
2444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
2544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
260616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
270616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#include "constant_area_fixups_x86.h"
280616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
290616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell
30e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
31b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
32f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko#include "base/arena_containers.h"
335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h"
345e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h"
3546e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h"
36f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h"
37787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h"
38787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h"
3920f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h"
4053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h"
4175be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h"
4275be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h"
4371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
44f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h"
45f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
46787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h"
4820f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
4992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
50e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
5169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h"
52f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
53d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
5422af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h"
55e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
563c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
5771fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
5882091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
59e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
6126a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
62f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
63a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
64827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
653159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
667dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
67804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
68c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
69175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h"
70b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
71b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
72b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
74787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
75787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
76787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
77787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
78f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  explicit CodeVectorAllocator(ArenaAllocator* arena)
79f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
80f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko        size_(0) {}
81787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
82787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
83787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
8492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
85787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
87787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
88787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
89f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
90787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
92f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> memory_;
93787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
98f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
99f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
100ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
101f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
10253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = "";
103f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
10469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope;
105809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
10669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject {
1075e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
10869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver(HGraph* graph,
10969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               const char* method_name,
11069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CodeGenerator* codegen,
11169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               std::ostream* visualizer_output,
11269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CompilerDriver* compiler_driver)
11369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      : graph_(graph),
11469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        method_name_(method_name),
115809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1165e8b137d28c840b128e2488f954cccee3e86db14David Brazdil        timing_logger_(method_name, true, true),
117eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames        disasm_info_(graph->GetArena()),
118809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
11969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        visualizer_(visualizer_output, graph, *codegen),
12069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        graph_in_bad_state_(false) {
12153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (timing_logger_enabled_ || visualizer_enabled_) {
12253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (!IsVerboseMethod(compiler_driver, method_name)) {
12353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        timing_logger_enabled_ = visualizer_enabled_ = false;
12453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
12553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (visualizer_enabled_) {
12653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        visualizer_.PrintHeader(method_name_);
12753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        codegen->SetDisassemblyInformation(&disasm_info_);
12853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
12962e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1315e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
13269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassObserver() {
133809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
134809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << "TIMINGS " << method_name_;
135809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
136809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
137809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
138809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
139eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  void DumpDisassembly() const {
140eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (visualizer_enabled_) {
141eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      visualizer_.DumpGraphWithDisassembly();
142eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
143eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
144eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
14569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  void SetGraphInBadState() { graph_in_bad_state_ = true; }
14669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
147809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
148809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1495e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
151ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
1525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1555e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
158809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1625e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1635e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
164ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
1655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
16669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
16769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    // Validate the HGraph if running in debug mode.
16869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    if (kIsDebugBuild) {
16969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      if (!graph_in_bad_state_) {
17069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        if (graph_->IsInSsaForm()) {
17169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          SSAChecker checker(graph_->GetArena(), graph_);
17269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
17369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
17469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker);
17569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
17669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        } else {
17769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          GraphChecker checker(graph_->GetArena(), graph_);
17869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
17969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
18069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
18169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
18269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        }
18369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      }
18469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    }
1855e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1865e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
18753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
18853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
18953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // empty kStringFilter matching all methods.
19053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
19153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
19253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
19353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
19453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
19553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // warning when the string is empty.
19653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
19753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
19853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return true;
19953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
20053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    return false;
20253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  }
20353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  HGraph* const graph_;
2055e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  const char* method_name_;
2065e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
2075e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool timing_logger_enabled_;
2085e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  TimingLogger timing_logger_;
2095e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
210eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  DisassemblyInformation disasm_info_;
211eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
2125e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool visualizer_enabled_;
2135e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphVisualizer visualizer_;
2145e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
21569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // Flag to be set by the compiler if the pass failed and the graph is not
21669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // expected to validate.
21769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  bool graph_in_bad_state_;
218809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
21969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  friend PassScope;
22069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
22169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  DISALLOW_COPY_AND_ASSIGN(PassObserver);
2225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil};
2235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
22469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject {
225809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public:
22669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassScope(const char *pass_name, PassObserver* pass_observer)
227809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      : pass_name_(pass_name),
22869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        pass_observer_(pass_observer) {
22969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->StartPass(pass_name_);
230809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
231809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
23269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassScope() {
23369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->EndPass(pass_name_);
234809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
235809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
236809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
237809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  const char* const pass_name_;
23869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver* const pass_observer_;
239809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil};
240809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
24153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
24253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
24353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
24488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
24553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
24753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
24853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
25053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
25153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
25253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
25353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
25453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
255736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          const DexFile& dex_file,
256736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
25753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
258216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
259216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t access_flags,
260216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             InvokeType invoke_type,
261216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint16_t class_def_idx,
262216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t method_idx,
263216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             jobject class_loader,
264736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             const DexFile& dex_file,
265736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             Handle<mirror::DexCache> dex_cache) const;
266216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
26753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
26853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
269216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
270216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
271216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
273e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
27490443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
275216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
276216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
277216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
279216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
28053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
281ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
28253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
283216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
28453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2852be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2862be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2872be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2882be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2902be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
29153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
29288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
29388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
29488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
29548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
29612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Optimize and compile `graph`.
29712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileOptimized(HGraph* graph,
29812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CodeGenerator* codegen,
29912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CompilerDriver* driver,
30012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const DexCompilationUnit& dex_compilation_unit,
30169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                   PassObserver* pass_observer) const;
30212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
30312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Just compile without doing optimizations.
30412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileBaseline(CodeGenerator* codegen,
30512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  CompilerDriver* driver,
306eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                  const DexCompilationUnit& dex_compilation_unit,
30769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                  PassObserver* pass_observer) const;
30812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
3092be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
31088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
31153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
31253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
31353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
31453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
31553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
31688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
31788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
31888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
31988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
32088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
321da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffray          driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime) {}
322ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
323ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
324ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
325ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
326ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
327866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
328866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
329ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
330ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
331ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
33287000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    std::ios_base::openmode cfg_file_mode =
33387000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle        driver->GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
33487000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
335f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
3362be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
3372be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
3382be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
339f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
340787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
341216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
342216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
343216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
34488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
3452be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (compilation_stats_.get() != nullptr) {
3462be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_->Log();
3472be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
34888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
34988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
350da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const {
351216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
352216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
353e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
354e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
355e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
356e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
35753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
35853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
3591ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
3601ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  return instruction_set == kArm64
3611ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
3624dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
3631ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
3641ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
3651ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
3661ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
36710e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
36810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
36969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer) {
37010e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
37169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
37269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    optimizations[i]->Run();
37310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
37410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
37510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
376ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph,
377ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            CompilerDriver* driver,
378ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            OptimizingCompilerStats* stats,
379ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            const DexCompilationUnit& dex_compilation_unit,
380ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            PassObserver* pass_observer,
381ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            StackHandleScopeCollection* handles) {
382ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
383ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
384ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle      && (compiler_options.GetInlineMaxCodeUnits() > 0);
385ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  if (!should_inline) {
386ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    return;
387ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  }
388ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
389ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ArenaAllocator* arena = graph->GetArena();
390ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HInliner* inliner = new (arena) HInliner(
391ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    graph, dex_compilation_unit, dex_compilation_unit, driver, handles, stats);
392ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ReferenceTypePropagation* type_propagation =
393ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    new (arena) ReferenceTypePropagation(graph, handles,
394ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle        "reference_type_propagation_after_inlining");
395ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
396ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations[] = {
397ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    inliner,
398ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // Run another type propagation phase: inlining will open up more opportunities
399ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // to remove checkcast/instanceof and null checks.
400ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    type_propagation,
401ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
402ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
403ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
404ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle}
405ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
40644b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set,
40744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 HGraph* graph,
40844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 OptimizingCompilerStats* stats,
40944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 PassObserver* pass_observer) {
41044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  ArenaAllocator* arena = graph->GetArena();
41144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  switch (instruction_set) {
41244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
41344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    case kArm64: {
41444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      arm64::InstructionSimplifierArm64* simplifier =
41544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
41644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
41744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
41844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      HOptimization* arm64_optimizations[] = {
41944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        simplifier,
42044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        side_effects,
42144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        gvn
42244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      };
42344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
42444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
42544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    }
42644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
4270616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
4280616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    case kX86: {
4290616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      x86::ConstantAreaFixups* constant_area_fixups =
4300616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell          new (arena) x86::ConstantAreaFixups(graph, stats);
4310616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      HOptimization* x86_optimizations[] = {
4320616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell        constant_area_fixups
4330616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      };
4340616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
4350616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      break;
4360616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    }
4370616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
43844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    default:
43944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
44044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  }
44144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames}
44244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
443e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
444e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
445e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
446e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
44769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer,
448acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle                             StackHandleScopeCollection* handles) {
449a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
450a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
451a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
452a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
453a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
454a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
455a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
456a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph);
457a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
458a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
459a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
460a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  LICM* licm = new (arena) LICM(graph, *side_effects);
46122af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
46222af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, induction);
463a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation =
464a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      new (arena) ReferenceTypePropagation(graph, handles);
465a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
466a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "instruction_simplifier_after_types");
467a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
468b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_after_bce");
469b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray  InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
470b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_before_codegen");
471a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
472a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver);
47371fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
474ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations1[] = {
475a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
476a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
477a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
478a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation,
47918e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
480ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    simplify2
481ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
482ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
483ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
484ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
48577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  // TODO: Update passes incompatible with try/catch so we have the same
48677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  //       pipeline for all methods.
487bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  if (graph->HasTryCatch()) {
48877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    HOptimization* optimizations2[] = {
48977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      side_effects,
49077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      gvn,
49177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      dce2,
49277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // The codegen has a few assumptions that only the instruction simplifier
49377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // can satisfy. For example, the code generator does not expect to see a
49477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // HTypeConversion from a type to the same type.
49577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      simplify4,
49677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    };
49777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
49877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
49977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  } else {
50077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    MaybeRunInliner(graph, driver, stats, dex_compilation_unit, pass_observer, handles);
50177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
50277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    HOptimization* optimizations2[] = {
50377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // BooleanSimplifier depends on the InstructionSimplifier removing
50477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // redundant suspend checks to recognize empty blocks.
50577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      boolean_simplify,
50677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      fold2,  // TODO: if we don't inline we can also skip fold2.
50777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      side_effects,
50877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      gvn,
50977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      licm,
51022af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik      induction,
51177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      bce,
51277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      simplify3,
51377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      dce2,
51477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // The codegen has a few assumptions that only the instruction simplifier
51577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // can satisfy. For example, the code generator does not expect to see a
51677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // HTypeConversion from a type to the same type.
51777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      simplify4,
51877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    };
51977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
52077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
521bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  }
522bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil
52344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer);
5245e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
5255e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
526376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
527376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
528f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Markostatic ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) {
529376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
530376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
531376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
532376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
533376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
534e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
535376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
536376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
537c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampestatic void AllocateRegisters(HGraph* graph,
538c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe                              CodeGenerator* codegen,
53969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                              PassObserver* pass_observer) {
540c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  PrepareForRegisterAllocation(graph).Run();
5410d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
542c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
54369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
544c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    liveness.Analyze();
545c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
546c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
54769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
548c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
549c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
550c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe}
55112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
552581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
553581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
554581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  codegen->EmitLinkerPatches(&linker_patches);
555581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
556581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Sort patches by literal offset. Required for .oat_patches encoding.
557581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  std::sort(linker_patches.begin(), linker_patches.end(),
558581550137ee3a068a14224870e71aeee924a0646Vladimir Marko            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
559581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    return lhs.LiteralOffset() < rhs.LiteralOffset();
560581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  });
561581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
562581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  return linker_patches;
563581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
564581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
56512df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
56612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CodeGenerator* codegen,
56712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CompilerDriver* compiler_driver,
56812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const DexCompilationUnit& dex_compilation_unit,
56969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                                     PassObserver* pass_observer) const {
570ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  ScopedObjectAccess soa(Thread::Current());
571ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  StackHandleScopeCollection handles(soa.Self());
572ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromRunnableToSuspended(kNative);
5732be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  RunOptimizations(graph, compiler_driver, compilation_stats_.get(),
57469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                   dex_compilation_unit, pass_observer, &handles);
57512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
57669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  AllocateRegisters(graph, codegen, pass_observer);
57712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
578f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = graph->GetArena();
579f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
5802a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
5812a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
5822a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
5832a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
58412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileOptimized(&allocator);
58512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
586581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
587581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
588f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
58912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildStackMaps(&stack_map);
59012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
5912be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledOptimized);
59212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
593eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
59412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
59512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
59612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(allocator.GetMemory()),
597aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
598aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
599aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
600aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
60112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
602d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
603c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      &src_mapping_table,
604c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // mapping_table.
605c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
606c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // native_gc_map.
607c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
608581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
60969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
610ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier
611ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromSuspendedToRunnable();
612eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
61312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
61412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
61512df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline(
61612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
61712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CompilerDriver* compiler_driver,
618eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    const DexCompilationUnit& dex_compilation_unit,
61969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassObserver* pass_observer) const {
620f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = codegen->GetGraph()->GetArena();
621f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
6222a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
6232a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
6242a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
6252a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
62612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileBaseline(&allocator);
62712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
628581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
629581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
630f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps));
631c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->BuildMappingTable(&mapping_table);
632f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps));
63312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
634f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps));
63512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
63612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
6372be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledBaseline);
638eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
639aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      compiler_driver,
640aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetInstructionSet(),
641aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      ArrayRef<const uint8_t>(allocator.GetMemory()),
642aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
643aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
644aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
645aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
646aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetCoreSpillMask(),
647aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetFpuSpillMask(),
648aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      &src_mapping_table,
649aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(mapping_table),
650aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(vmap_table),
651aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(gc_map),
652c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
653581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
65469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
655eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
65612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
65712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
658216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
659216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t access_flags,
660216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               InvokeType invoke_type,
661216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint16_t class_def_idx,
662216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t method_idx,
663216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               jobject class_loader,
664736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               const DexFile& dex_file,
665736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               Handle<mirror::DexCache> dex_cache) const {
6666a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNUSED(invoke_type);
6675e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  std::string method_name = PrettyMethod(method_idx, dex_file);
6682be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
669cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
670cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
6718d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
6728d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
6738d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
6748d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
6758fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6768fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
6778fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
6781ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
6792be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
6808fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
6818fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6828fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
683b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
6842be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
685b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
686b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
687b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
68836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
689432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  // code units is bigger than 128.
690432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
69136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
69236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
69336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
6942be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
69536540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
69636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
69736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
69892cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
6999523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item,
70072d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
701736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache);
70292cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
7033cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
7043cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
7053cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
7063cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
707579ea7d0d89a1d7f02c35ef1beb6328a37259735Nicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetArenaPool());
708e0fe7ae36180863e45cbb9d1e6e9c30b1b1a949cNicolas Geoffray  HGraph* graph = new (&arena) HGraph(
709e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      &arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(),
710e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable());
711e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
7128ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
7138ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
714e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  bool shouldCompile = method_name.find("$opt$") != std::string::npos;
715a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray  bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
7168ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
71712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
718cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
719cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
720cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
721cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
72212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
7235667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
7242be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
725787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
726787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
727c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
7288363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky      compiler_driver->GetCompilerOptions().GetGenerateDebugInfo());
729787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
73069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver pass_observer(graph,
73169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             method_name.c_str(),
73269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             codegen.get(),
73369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             visualizer_output_.get(),
73469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             compiler_driver);
7355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7369523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  const uint8_t* interpreter_metadata = nullptr;
7379523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  {
7389523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ScopedObjectAccess soa(Thread::Current());
739736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    StackHandleScope<1> hs(soa.Self());
7409523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    Handle<mirror::ClassLoader> loader(hs.NewHandle(
7419523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa.Decode<mirror::ClassLoader*>(class_loader)));
7429523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ArtMethod* art_method = compiler_driver->ResolveMethod(
7439523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
7449523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // We may not get a method, for example if its class is erroneous.
7459523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile.
7469523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    if (art_method != nullptr) {
7479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray      interpreter_metadata = art_method->GetQuickenedInfo();
7489523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    }
7499523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  }
7505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphBuilder builder(graph,
7515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_file,
7545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        compiler_driver,
7559523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray                        compilation_stats_.get(),
756736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        interpreter_metadata,
757736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        dex_cache);
7585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  VLOG(compiler) << "Building " << method_name;
7605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
761809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
76269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
763809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (!builder.BuildGraph(*code_item)) {
764335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray      DCHECK(!(IsCompilingWithCoreImage() && shouldCompile))
765335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray          << "Could not build graph in optimizing compiler";
76669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      pass_observer.SetGraphInBadState();
767809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      return nullptr;
768809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
7695e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
770a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
77148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
772216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
773216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // `run_optimizations_` is set explicitly (either through a compiler filter
774216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
775216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // to Quick.
7762e7cd752452d02499a2f5fbd604c5427aa372f00Nicolas Geoffray  bool can_use_baseline = !run_optimizations_ && builder.CanUseBaselineForStringInit();
777f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CompiledMethod* compiled_method = nullptr;
778ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil  if (run_optimizations_ && can_allocate_registers) {
7795e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Optimizing " << method_name;
7805e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
781809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
78269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer);
783809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      if (!graph->TryBuildingSsa()) {
784809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        // We could not transform the graph to SSA, bailout.
785809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
7862be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle        MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
787ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil        pass_observer.SetGraphInBadState();
788809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        return nullptr;
789809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      }
790f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
7915e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
792f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileOptimized(graph,
793f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       codegen.get(),
794f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       compiler_driver,
795f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       dex_compilation_unit,
796f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       &pass_observer);
797bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  } else if (shouldOptimize && can_allocate_registers) {
79886dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
7995667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
800216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else if (can_use_baseline) {
8015e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Compile baseline " << method_name;
80248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
80348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    if (!run_optimizations_) {
8042be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedDisabled);
80548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_allocate_registers) {
8062be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
80748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    }
80848c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
809f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileBaseline(codegen.get(),
810f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      compiler_driver,
811f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      dex_compilation_unit,
812f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      &pass_observer);
813216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
814f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
815f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  if (kArenaAllocatorCountAllocations) {
816f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    if (arena.BytesAllocated() > 4 * MB) {
817f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      MemStats mem_stats(arena.GetMemStats());
818f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
819f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    }
820f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  }
821f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
822f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  return compiled_method;
823216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
824216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
82507380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) {
82607380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved).
82707380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS
82807380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS
82907380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD
83007380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
831175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0;
832175dc732c80e6f2afd83209348124df349290ba8Calin Juravle}
833175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
834216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
835216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
836216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
837216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
838216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
839f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
840736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            const DexFile& dex_file,
841736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            Handle<mirror::DexCache> dex_cache) const {
842f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
843f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
844175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
845175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  DCHECK(!verified_method->HasRuntimeThrow());
846175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
84707380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      || CanHandleVerificationFailure(verified_method)) {
848f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle     method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
849736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                         method_idx, jclass_loader, dex_file, dex_cache);
850f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  } else {
851f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
8522be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
853f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    } else {
8542be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified);
855f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
856f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
857f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
85812be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
859b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
860b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
86153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
86253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
86353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
86453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
865335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() {
866335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  const std::string& image = Runtime::Current()->GetImageLocation();
867335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art");
868335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray}
869335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray
870b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
871