optimizing_compiler.cc revision c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74
1b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray/*
2b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Copyright (C) 2014 The Android Open Source Project
3b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *
4b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Licensed under the Apache License, Version 2.0 (the "License");
5b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * you may not use this file except in compliance with the License.
6b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * You may obtain a copy of the License at
7b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *
8b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *      http://www.apache.org/licenses/LICENSE-2.0
9b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray *
10b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * Unless required by applicable law or agreed to in writing, software
11b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * distributed under the License is distributed on an "AS IS" BASIS,
12b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * See the License for the specific language governing permissions and
14b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray * limitations under the License.
15b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray */
16b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
1753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "optimizing_compiler.h"
1853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
19f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include <fstream>
20787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include <stdint.h>
21787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
2244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
2344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#include "instruction_simplifier_arm64.h"
2444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
2544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
260616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
270616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#include "constant_area_fixups_x86.h"
280616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
290616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell
30e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
31b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
32f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko#include "base/arena_containers.h"
335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h"
34c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir Marko#include "base/macros.h"
355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h"
3646e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h"
37f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h"
38787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h"
39787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h"
4020f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h"
4153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h"
4275be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h"
4375be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h"
4471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
45f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h"
46f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
47787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
489523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray#include "driver/compiler_driver-inl.h"
4920f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
5092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
51e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
5269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil#include "graph_checker.h"
53f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
54d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
5522af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik#include "induction_var_analysis.h"
56e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
573c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
5871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
5982091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
60e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
61787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
6226a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
63f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
64a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
65827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
663159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
677dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
68804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
69c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
70175dc732c80e6f2afd83209348124df349290ba8Calin Juravle#include "verifier/method_verifier.h"
71b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
72b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
73b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
74787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
75787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
76787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
77787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
78787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
79f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  explicit CodeVectorAllocator(ArenaAllocator* arena)
80f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
81f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko        size_(0) {}
82787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
83787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
84787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
8592cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
86787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
87787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
88787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
89787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
90f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
91787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
92787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
93f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> memory_;
94787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
95787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
96787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
97787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
98787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
99f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
100f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
101ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
102f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
10353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampestatic constexpr const char kStringFilter[] = "";
104f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
10569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope;
106809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
10769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassObserver : public ValueObject {
1085e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
10969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver(HGraph* graph,
11069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               const char* method_name,
11169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CodeGenerator* codegen,
11269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               std::ostream* visualizer_output,
11369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil               CompilerDriver* compiler_driver)
11469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      : graph_(graph),
11569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        method_name_(method_name),
116809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1175e8b137d28c840b128e2488f954cccee3e86db14David Brazdil        timing_logger_(method_name, true, true),
118eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames        disasm_info_(graph->GetArena()),
119809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
12069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        visualizer_(visualizer_output, graph, *codegen),
12169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        graph_in_bad_state_(false) {
12253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (timing_logger_enabled_ || visualizer_enabled_) {
12353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (!IsVerboseMethod(compiler_driver, method_name)) {
12453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        timing_logger_enabled_ = visualizer_enabled_ = false;
12553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
12653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      if (visualizer_enabled_) {
12753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        visualizer_.PrintHeader(method_name_);
12853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe        codegen->SetDisassemblyInformation(&disasm_info_);
12953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      }
13062e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1315e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1325e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
13369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassObserver() {
134809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
135809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << "TIMINGS " << method_name_;
136809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
137809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
138809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
139809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
140eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  void DumpDisassembly() const {
141eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    if (visualizer_enabled_) {
142eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames      visualizer_.DumpGraphWithDisassembly();
143eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    }
144eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  }
145eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
14669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  void SetGraphInBadState() { graph_in_bad_state_ = true; }
14769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
148809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
149809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
152ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
1535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1555e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
159809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1625e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1635e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1645e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
165ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
1665e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
16769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
16869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    // Validate the HGraph if running in debug mode.
16969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    if (kIsDebugBuild) {
17069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      if (!graph_in_bad_state_) {
17169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        if (graph_->IsInSsaForm()) {
17269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          SSAChecker checker(graph_->GetArena(), graph_);
17369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
17469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
17569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<SSAChecker>(checker);
17669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
17769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        } else {
17869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          GraphChecker checker(graph_->GetArena(), graph_);
17969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          checker.Run();
18069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          if (!checker.IsValid()) {
18169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil            LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
18269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil          }
18369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        }
18469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      }
18569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    }
1865e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1875e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
18853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
18953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
19053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // empty kStringFilter matching all methods.
19153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
19253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
19353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
19453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
19553fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
19653fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    // warning when the string is empty.
19753fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
19853fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
19953fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe      return true;
20053fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    }
20153fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20253fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe    return false;
20353fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe  }
20453fcd0f2637207ecad2fd57a6a0f1cf8392fcc40Andreas Gampe
20569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  HGraph* const graph_;
2065e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  const char* method_name_;
2075e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
2085e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool timing_logger_enabled_;
2095e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  TimingLogger timing_logger_;
2105e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
211eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  DisassemblyInformation disasm_info_;
212eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames
2135e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool visualizer_enabled_;
2145e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphVisualizer visualizer_;
2155e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
21669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // Flag to be set by the compiler if the pass failed and the graph is not
21769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  // expected to validate.
21869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  bool graph_in_bad_state_;
219809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
22069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  friend PassScope;
22169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil
22269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  DISALLOW_COPY_AND_ASSIGN(PassObserver);
2235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil};
2245e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
22569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdilclass PassScope : public ValueObject {
226809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public:
22769ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassScope(const char *pass_name, PassObserver* pass_observer)
228809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      : pass_name_(pass_name),
22969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil        pass_observer_(pass_observer) {
23069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->StartPass(pass_name_);
231809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
232809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
23369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  ~PassScope() {
23469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    pass_observer_->EndPass(pass_name_);
235809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
236809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
237809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
238809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  const char* const pass_name_;
23969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver* const pass_observer_;
240809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil};
241809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
24253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
24353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
24453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
24588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
24653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
24853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
24953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
25053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
25153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
25253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
25353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
25453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
25553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
256736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          const DexFile& dex_file,
257736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
25853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
259216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
260216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t access_flags,
261216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             InvokeType invoke_type,
262216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint16_t class_def_idx,
263216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t method_idx,
264216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             jobject class_loader,
265736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             const DexFile& dex_file,
266736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                             Handle<mirror::DexCache> dex_cache) const;
267216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
26853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
26953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
270216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
271216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
272216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
274e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
27590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier      SHARED_REQUIRES(Locks::mutator_lock_) {
276216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
277216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
278216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
27953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
280216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
28153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
282ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
28353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
284216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
28553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2862be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2872be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2882be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2902be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2912be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
29253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
29388157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
29488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
29588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
29648c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
29712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Optimize and compile `graph`.
29812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileOptimized(HGraph* graph,
29912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CodeGenerator* codegen,
30012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CompilerDriver* driver,
30112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const DexCompilationUnit& dex_compilation_unit,
30269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                   PassObserver* pass_observer) const;
30312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
30412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Just compile without doing optimizations.
30512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileBaseline(CodeGenerator* codegen,
30612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  CompilerDriver* driver,
307eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames                                  const DexCompilationUnit& dex_compilation_unit,
30869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                  PassObserver* pass_observer) const;
30912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
3102be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
31188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
31253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
31353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
31453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
31553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
31653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
31788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
31888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
31988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
32088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
32188157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
322da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffray          driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime) {}
323ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
324ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
325ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
326ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
327ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
328866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
329866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
330ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
331ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
332ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
33387000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    std::ios_base::openmode cfg_file_mode =
33487000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle        driver->GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
33587000a948524cba7538ccc5438f6a9ecbd4e347eCalin Juravle    visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
336f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
3372be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
3382be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
3392be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
340f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
341787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
342216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
343216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
344216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
34588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
3462be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (compilation_stats_.get() != nullptr) {
3472be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_->Log();
3482be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
34988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
35088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
351da3a55bd07e1907d4286dd1b6de77ff42d861e16Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const {
352216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
353216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
354e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
355e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
356e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
357e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
35853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
35953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
3601ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
3611ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  return instruction_set == kArm64
3621ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
3634dda3376b71209fae07f5c3c8ac3eb4b54207aa8Alexey Frunze      || instruction_set == kMips64
3641ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
3651ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
3661ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
3671ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
36810e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
36910e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
37069ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer) {
37110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
37269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
37369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    optimizations[i]->Run();
37410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
37510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
37610e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
377ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravlestatic void MaybeRunInliner(HGraph* graph,
378ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            CompilerDriver* driver,
379ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            OptimizingCompilerStats* stats,
380ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            const DexCompilationUnit& dex_compilation_unit,
381ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            PassObserver* pass_observer,
382ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle                            StackHandleScopeCollection* handles) {
383ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
384ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
385ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle      && (compiler_options.GetInlineMaxCodeUnits() > 0);
386ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  if (!should_inline) {
387ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    return;
388ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  }
389ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
390ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ArenaAllocator* arena = graph->GetArena();
391ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HInliner* inliner = new (arena) HInliner(
392ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    graph, dex_compilation_unit, dex_compilation_unit, driver, handles, stats);
393ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  ReferenceTypePropagation* type_propagation =
394ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    new (arena) ReferenceTypePropagation(graph, handles,
395ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle        "reference_type_propagation_after_inlining");
396ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
397ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations[] = {
398ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    inliner,
399ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // Run another type propagation phase: inlining will open up more opportunities
400ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    // to remove checkcast/instanceof and null checks.
401ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    type_propagation,
402ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
403ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
404ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
405ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle}
406ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
40744b9cf937836bb33139123e15ca8b586b5853268Alexandre Ramesstatic void RunArchOptimizations(InstructionSet instruction_set,
40844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 HGraph* graph,
40944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 OptimizingCompilerStats* stats,
41044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames                                 PassObserver* pass_observer) {
41144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  ArenaAllocator* arena = graph->GetArena();
41244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  switch (instruction_set) {
41344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#ifdef ART_ENABLE_CODEGEN_arm64
41444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    case kArm64: {
41544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      arm64::InstructionSimplifierArm64* simplifier =
41644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
41744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
41844b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
41944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      HOptimization* arm64_optimizations[] = {
42044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        simplifier,
42144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        side_effects,
42244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames        gvn
42344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      };
42444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
42544b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
42644b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    }
42744b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames#endif
4280616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#ifdef ART_ENABLE_CODEGEN_x86
4290616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    case kX86: {
4300616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      x86::ConstantAreaFixups* constant_area_fixups =
4310616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell          new (arena) x86::ConstantAreaFixups(graph, stats);
4320616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      HOptimization* x86_optimizations[] = {
4330616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell        constant_area_fixups
4340616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      };
4350616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
4360616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell      break;
4370616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell    }
4380616ae081e648f4b9b64b33e2624a943c5fce977Mark Mendell#endif
43944b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames    default:
44044b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames      break;
44144b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  }
44244b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames}
44344b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames
444e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
445e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
446e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
447e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
44869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             PassObserver* pass_observer,
449acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle                             StackHandleScopeCollection* handles) {
450a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
451a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
452a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
453a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
454a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
455a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
456a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
457a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph);
458a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
459a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
460a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
461a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  LICM* licm = new (arena) LICM(graph, *side_effects);
46222af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
46322af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, induction);
464a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation =
465a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      new (arena) ReferenceTypePropagation(graph, handles);
466a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
467a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "instruction_simplifier_after_types");
468a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
469b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_after_bce");
470b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray  InstructionSimplifier* simplify4 = new (arena) InstructionSimplifier(
471b2bdfce7f805b00668a2521b1c939a0aafb2be49Nicolas Geoffray      graph, stats, "instruction_simplifier_before_codegen");
472a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
473a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver);
47471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
475ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  HOptimization* optimizations1[] = {
476a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
477a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
478a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
479a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation,
48018e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
481ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle    simplify2
482ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  };
483ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
484ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
485ec74835a7e4f2660250a2f3f9508cbbe5269e49aCalin Juravle
48677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  // TODO: Update passes incompatible with try/catch so we have the same
48777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  //       pipeline for all methods.
488bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  if (graph->HasTryCatch()) {
48977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    HOptimization* optimizations2[] = {
49077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      side_effects,
49177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      gvn,
49277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      dce2,
49377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // The codegen has a few assumptions that only the instruction simplifier
49477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // can satisfy. For example, the code generator does not expect to see a
49577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // HTypeConversion from a type to the same type.
49677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      simplify4,
49777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    };
49877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
49977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
50077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil  } else {
50177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    MaybeRunInliner(graph, driver, stats, dex_compilation_unit, pass_observer, handles);
50277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
50377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    HOptimization* optimizations2[] = {
50477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // BooleanSimplifier depends on the InstructionSimplifier removing
50577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // redundant suspend checks to recognize empty blocks.
50677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      boolean_simplify,
50777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      fold2,  // TODO: if we don't inline we can also skip fold2.
50877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      side_effects,
50977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      gvn,
51077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      licm,
51122af3bee34d0ab1a4bd186c71ccab00366882259Aart Bik      induction,
51277a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      bce,
51377a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      simplify3,
51477a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      dce2,
51577a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // The codegen has a few assumptions that only the instruction simplifier
51677a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // can satisfy. For example, the code generator does not expect to see a
51777a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      // HTypeConversion from a type to the same type.
51877a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil      simplify4,
51977a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    };
52077a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil
52177a48ae01bbc5b05ca009cf09e2fcb53e4c8ff23David Brazdil    RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
522bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  }
523bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil
52444b9cf937836bb33139123e15ca8b586b5853268Alexandre Rames  RunArchOptimizations(driver->GetInstructionSet(), graph, stats, pass_observer);
5255e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
5265e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
527376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
528376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
529f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Markostatic ArrayRef<const uint8_t> AlignVectorSize(ArenaVector<uint8_t>& vector) {
530376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
531376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
532376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
533376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
534376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
535e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
536376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
537376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
538c90d7c7cd3103a7b7ce62b40873d2dfcf306ca74Vladimir MarkoNO_INLINE  // Avoid increasing caller's frame size by large stack-allocated objects.
539c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampestatic void AllocateRegisters(HGraph* graph,
540c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe                              CodeGenerator* codegen,
54169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                              PassObserver* pass_observer) {
542c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  PrepareForRegisterAllocation(graph).Run();
5430d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
544c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
54569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
546c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    liveness.Analyze();
547c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
548c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
54969ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
550c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
551c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
552c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe}
55312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
554581550137ee3a068a14224870e71aeee924a0646Vladimir Markostatic ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
555581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
556581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  codegen->EmitLinkerPatches(&linker_patches);
557581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
558581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  // Sort patches by literal offset. Required for .oat_patches encoding.
559581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  std::sort(linker_patches.begin(), linker_patches.end(),
560581550137ee3a068a14224870e71aeee924a0646Vladimir Marko            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
561581550137ee3a068a14224870e71aeee924a0646Vladimir Marko    return lhs.LiteralOffset() < rhs.LiteralOffset();
562581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  });
563581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
564581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  return linker_patches;
565581550137ee3a068a14224870e71aeee924a0646Vladimir Marko}
566581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
56712df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
56812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CodeGenerator* codegen,
56912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CompilerDriver* compiler_driver,
57012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const DexCompilationUnit& dex_compilation_unit,
57169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                                                     PassObserver* pass_observer) const {
572ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  ScopedObjectAccess soa(Thread::Current());
573ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  StackHandleScopeCollection handles(soa.Self());
574ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromRunnableToSuspended(kNative);
5752be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  RunOptimizations(graph, compiler_driver, compilation_stats_.get(),
57669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                   dex_compilation_unit, pass_observer, &handles);
57712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
57869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  AllocateRegisters(graph, codegen, pass_observer);
57912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
580f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = graph->GetArena();
581f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
5822a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
5832a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
5842a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
5852a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
58612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileOptimized(&allocator);
58712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
588581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
589581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
590f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
59112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildStackMaps(&stack_map);
59212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
5932be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledOptimized);
59412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
595eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
59612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
59712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
59812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(allocator.GetMemory()),
599aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
600aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
601aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
602aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
60312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
604d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
605c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      &src_mapping_table,
606c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // mapping_table.
607c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
608c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // native_gc_map.
609c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
610581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
61169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
612ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier
613ed15000a5099f5e230c8ded5ac75692bae272650Mathieu Chartier  soa.Self()->TransitionFromSuspendedToRunnable();
614eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
61512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
61612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
61712df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline(
61812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
61912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CompilerDriver* compiler_driver,
620eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames    const DexCompilationUnit& dex_compilation_unit,
62169ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassObserver* pass_observer) const {
622f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaAllocator* arena = codegen->GetGraph()->GetArena();
623f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CodeVectorAllocator allocator(arena);
6242a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  DefaultSrcMap src_mapping_table;
6252a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban  codegen->SetSrcMap(compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()
6262a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         ? &src_mapping_table
6272a7c1ef95c850abae915b3a59fbafa87e6833967Yevgeny Rouban                         : nullptr);
62812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileBaseline(&allocator);
62912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
630581550137ee3a068a14224870e71aeee924a0646Vladimir Marko  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
631581550137ee3a068a14224870e71aeee924a0646Vladimir Marko
632f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> mapping_table(arena->Adapter(kArenaAllocBaselineMaps));
633c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->BuildMappingTable(&mapping_table);
634f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> vmap_table(arena->Adapter(kArenaAllocBaselineMaps));
63512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
636f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  ArenaVector<uint8_t> gc_map(arena->Adapter(kArenaAllocBaselineMaps));
63712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
63812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
6392be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledBaseline);
640eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
641aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      compiler_driver,
642aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetInstructionSet(),
643aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      ArrayRef<const uint8_t>(allocator.GetMemory()),
644aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
645aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
646aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
647aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
648aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetCoreSpillMask(),
649aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetFpuSpillMask(),
650aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      &src_mapping_table,
651aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(mapping_table),
652aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(vmap_table),
653aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(gc_map),
654c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
655581550137ee3a068a14224870e71aeee924a0646Vladimir Marko      ArrayRef<const LinkerPatch>(linker_patches));
65669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  pass_observer->DumpDisassembly();
657eb7b7399dbdb5e471b8ae00a567bf4f19edd3907Alexandre Rames  return compiled_method;
65812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
65912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
660216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
661216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t access_flags,
662216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               InvokeType invoke_type,
663216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint16_t class_def_idx,
664216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t method_idx,
665216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               jobject class_loader,
666736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               const DexFile& dex_file,
667736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                               Handle<mirror::DexCache> dex_cache) const {
6686a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNUSED(invoke_type);
6695e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  std::string method_name = PrettyMethod(method_idx, dex_file);
6702be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
671cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
672cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
6738d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
6748d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
6758d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
6768d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
6778fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6788fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
6798fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
6801ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
6812be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
6828fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
6838fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
6848fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
685b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
6862be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
687b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
688b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
689b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
69036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
691432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  // code units is bigger than 128.
692432bf3d525dd0736c91238d69e29b4db6a2f7b80Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
69336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
69436540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
69536540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
6962be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
69736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
69836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
69936540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
70092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
7019523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item,
70272d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
703736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    compiler_driver->GetVerifiedMethod(&dex_file, method_idx), dex_cache);
70492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
7053cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
7063cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
7073cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
7083cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
709579ea7d0d89a1d7f02c35ef1beb6328a37259735Nicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetArenaPool());
710e0fe7ae36180863e45cbb9d1e6e9c30b1b1a949cNicolas Geoffray  HGraph* graph = new (&arena) HGraph(
711e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      &arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(),
712e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable());
713e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
7148ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
7158ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
716e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  bool shouldCompile = method_name.find("$opt$") != std::string::npos;
717a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray  bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
7188ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
71912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
720cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
721cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
722cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
723cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
72412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
7255667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
7262be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
727787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
728787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
729c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
7308363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky      compiler_driver->GetCompilerOptions().GetGenerateDebugInfo());
731787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
73269ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil  PassObserver pass_observer(graph,
73369ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             method_name.c_str(),
73469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             codegen.get(),
73569ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             visualizer_output_.get(),
73669ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil                             compiler_driver);
7375e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7389523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  const uint8_t* interpreter_metadata = nullptr;
7399523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  {
7409523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ScopedObjectAccess soa(Thread::Current());
741736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier    StackHandleScope<1> hs(soa.Self());
7429523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    Handle<mirror::ClassLoader> loader(hs.NewHandle(
7439523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa.Decode<mirror::ClassLoader*>(class_loader)));
7449523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    ArtMethod* art_method = compiler_driver->ResolveMethod(
7459523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
7469523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // We may not get a method, for example if its class is erroneous.
7479523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    // TODO: Clean this up, the compiler driver should just pass the ArtMethod to compile.
7489523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    if (art_method != nullptr) {
7499523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray      interpreter_metadata = art_method->GetQuickenedInfo();
7509523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray    }
7519523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray  }
7525e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphBuilder builder(graph,
7535e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7545e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
7555e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_file,
7565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        compiler_driver,
7579523a3ed0ae6fa31cd978a3999fd88233218f98bNicolas Geoffray                        compilation_stats_.get(),
758736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        interpreter_metadata,
759736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                        dex_cache);
7605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
7615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  VLOG(compiler) << "Building " << method_name;
7625e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
763809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
76469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil    PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
765809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (!builder.BuildGraph(*code_item)) {
766335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray      DCHECK(!(IsCompilingWithCoreImage() && shouldCompile))
767335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray          << "Could not build graph in optimizing compiler";
76869ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      pass_observer.SetGraphInBadState();
769809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      return nullptr;
770809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
7715e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
772a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
77348c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
774216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
775216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // `run_optimizations_` is set explicitly (either through a compiler filter
776216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
777216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // to Quick.
7782e7cd752452d02499a2f5fbd604c5427aa372f00Nicolas Geoffray  bool can_use_baseline = !run_optimizations_ && builder.CanUseBaselineForStringInit();
779f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  CompiledMethod* compiled_method = nullptr;
780ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil  if (run_optimizations_ && can_allocate_registers) {
7815e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Optimizing " << method_name;
7825e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
783809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
78469ba7b7112c2277ac225615b37e6df74c055740dDavid Brazdil      PassScope scope(SsaBuilder::kSsaBuilderPassName, &pass_observer);
785809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      if (!graph->TryBuildingSsa()) {
786809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        // We could not transform the graph to SSA, bailout.
787809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
7882be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle        MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
789ffee3d33f3ea39aa6031c3d2ff29c4806c8dcc51David Brazdil        pass_observer.SetGraphInBadState();
790809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        return nullptr;
791809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      }
792f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
7935e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
794f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileOptimized(graph,
795f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       codegen.get(),
796f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       compiler_driver,
797f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       dex_compilation_unit,
798f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                       &pass_observer);
799bbd733e4ef277eff19bf9a6601032da081e9b68fDavid Brazdil  } else if (shouldOptimize && can_allocate_registers) {
80086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
8015667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
802216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else if (can_use_baseline) {
8035e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Compile baseline " << method_name;
80448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
80548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    if (!run_optimizations_) {
8062be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedDisabled);
80748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_allocate_registers) {
8082be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
80948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    }
81048c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
811f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    compiled_method = CompileBaseline(codegen.get(),
812f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      compiler_driver,
813f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      dex_compilation_unit,
814f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko                                      &pass_observer);
815216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
816f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
817f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  if (kArenaAllocatorCountAllocations) {
818f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    if (arena.BytesAllocated() > 4 * MB) {
819f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      MemStats mem_stats(arena.GetMemStats());
820f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko      LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
821f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko    }
822f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  }
823f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko
824f9f6441c665b5ff9004d3ed55014f46d416fb1bbVladimir Marko  return compiled_method;
825216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
826216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
82707380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravlestatic bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) {
82807380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved).
82907380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle  uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS
83007380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS
83107380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD
83207380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
833175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0;
834175dc732c80e6f2afd83209348124df349290ba8Calin Juravle}
835175dc732c80e6f2afd83209348124df349290ba8Calin Juravle
836216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
837216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
838216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
839216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
840216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
841f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
842736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            const DexFile& dex_file,
843736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                                            Handle<mirror::DexCache> dex_cache) const {
844f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
845f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
846175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
847175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  DCHECK(!verified_method->HasRuntimeThrow());
848175dc732c80e6f2afd83209348124df349290ba8Calin Juravle  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
84907380a2d6cb9feee02facc81f699eed5cb670cd5Calin Juravle      || CanHandleVerificationFailure(verified_method)) {
850f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle     method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
851736b560f2d2c89b63dc895888c671b5519afa4c8Mathieu Chartier                         method_idx, jclass_loader, dex_file, dex_cache);
852f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  } else {
853f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
8542be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
855f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    } else {
8562be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified);
857f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
858f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
859f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
86012be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
861b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
862b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
86353c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
86453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
86553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
86653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
867335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffraybool IsCompilingWithCoreImage() {
868335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  const std::string& image = Runtime::Current()->GetImageLocation();
869335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray  return EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art");
870335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray}
871335005e2b3a179f26b7a8ae64ca60a1406b669bdNicolas Geoffray
872b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
873