optimizing_compiler.cc revision 18e6873c469b48aaed22148451523479eece98e3
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
22e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier#include "art_method-inl.h"
23b666f4805c8ae707ea6fd7f6c7f375e0b000dba8Mathieu Chartier#include "base/arena_allocator.h"
245e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/dumpable.h"
255e8b137d28c840b128e2488f954cccee3e86db14David Brazdil#include "base/timing_logger.h"
2646e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil#include "boolean_simplifier.h"
27f384f88d4d1e89df82f47fbc7245a8acc9c2d49cMingyao Yang#include "bounds_check_elimination.h"
28787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "builder.h"
29787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "code_generator.h"
3020f85597828194c12be10d3a927999def066555eVladimir Marko#include "compiled_method.h"
3153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe#include "compiler.h"
3275be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "constant_folding.h"
3375be28332b278cff9039b54bfb228ac72f539cccRoland Levillain#include "dead_code_elimination.h"
3471fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "dex/quick/dex_file_to_method_inliner_map.h"
35f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verified_method.h"
36f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "dex/verification_results.h"
37787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "driver/compiler_driver.h"
3820f85597828194c12be10d3a927999def066555eVladimir Marko#include "driver/compiler_options.h"
3992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray#include "driver/dex_compilation_unit.h"
40e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "elf_writer_quick.h"
41f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray#include "graph_visualizer.h"
42d31cf3d55a0847c018c4eaa2b349b8eea509de64Nicolas Geoffray#include "gvn.h"
43e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray#include "inliner.h"
443c04974a90b0e03f4b509010bff49f0b2a3da57fNicolas Geoffray#include "instruction_simplifier.h"
4571fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe#include "intrinsics.h"
4682091dad38f3e5bfaf3b6984c9ab73069fb68310Nicolas Geoffray#include "licm.h"
47e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray#include "jni/quick/jni_compiler.h"
48787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray#include "nodes.h"
4926a25ef62a13f409f941aa39825a51b4d6f0f047Nicolas Geoffray#include "prepare_for_register_allocation.h"
50f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle#include "reference_type_propagation.h"
51a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray#include "register_allocator.h"
52827eedbfa882496407375f22b08243a38a5bd53bNicolas Geoffray#include "side_effects_analysis.h"
533159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray#include "ssa_builder.h"
547dc206a53a42a658f52d5cb0b7e79b47da370c9bNicolas Geoffray#include "ssa_phi_elimination.h"
55804d09372cc3d80d537da1489da4a45e0e19aa5dNicolas Geoffray#include "ssa_liveness_analysis.h"
56c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky#include "utils/assembler.h"
57b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
58b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffraynamespace art {
59b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
60787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray/**
61787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray * Used by the code generator, to allocate the code in a vector.
62787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray */
63787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffrayclass CodeVectorAllocator FINAL : public CodeAllocator {
64787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray public:
657c3952f423b8213083d60596a5f0bf4237ca3f7bAndreas Gampe  CodeVectorAllocator() : size_(0) {}
66787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
67787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  virtual uint8_t* Allocate(size_t size) {
68787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    size_ = size;
6992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    memory_.resize(size);
70787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return &memory_[0];
71787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
72787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
73787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t GetSize() const { return size_; }
7492cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  const std::vector<uint8_t>& GetMemory() const { return memory_; }
75787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
76787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray private:
77787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  std::vector<uint8_t> memory_;
78787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  size_t size_;
79787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
80787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
81787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray};
82787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
83f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray/**
84f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray * Filter to apply to the visualizer. Methods whose name contain that filter will
85ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil * be dumped.
86f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray */
87f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffraystatic const char* kStringFilter = "";
88f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray
89809658ec5c5515ae9eb87d32012ec5e088612949David Brazdilclass PassInfo;
90809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
915e8b137d28c840b128e2488f954cccee3e86db14David Brazdilclass PassInfoPrinter : public ValueObject {
925e8b137d28c840b128e2488f954cccee3e86db14David Brazdil public:
935e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  PassInfoPrinter(HGraph* graph,
945e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                  const char* method_name,
955e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                  const CodeGenerator& codegen,
965e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                  std::ostream* visualizer_output,
97809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                  CompilerDriver* compiler_driver)
985e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      : method_name_(method_name),
99809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
1005e8b137d28c840b128e2488f954cccee3e86db14David Brazdil        timing_logger_(method_name, true, true),
101809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        visualizer_enabled_(!compiler_driver->GetDumpCfgFileName().empty()),
10262e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil        visualizer_(visualizer_output, graph, codegen) {
1035e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (strstr(method_name, kStringFilter) == nullptr) {
1045e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_enabled_ = visualizer_enabled_ = false;
1055e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
10662e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    if (visualizer_enabled_) {
10762e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil      visualizer_.PrintHeader(method_name_);
10862e074f0d1a18f0f5aa6716f000825704f55e81bDavid Brazdil    }
1095e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1105e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
111809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  ~PassInfoPrinter() {
112809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (timing_logger_enabled_) {
113809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << "TIMINGS " << method_name_;
114809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
115809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
116809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
117809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
118809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
119809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void StartPass(const char* pass_name) {
1205e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Dump graph first, then start timer.
1215e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
1225e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false);
1235e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1245e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1255e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.StartTiming(pass_name);
1265e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1275e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1285e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
129809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  void EndPass(const char* pass_name) {
1305e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    // Pause timer first, then dump graph.
1315e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (timing_logger_enabled_) {
1325e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      timing_logger_.EndTiming();
1335e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1345e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    if (visualizer_enabled_) {
1355e8b137d28c840b128e2488f954cccee3e86db14David Brazdil      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true);
1365e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    }
1375e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
1385e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
1395e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  const char* method_name_;
1405e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
1415e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool timing_logger_enabled_;
1425e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  TimingLogger timing_logger_;
1435e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
1445e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  bool visualizer_enabled_;
1455e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphVisualizer visualizer_;
1465e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
147809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  friend PassInfo;
148809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
1495e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  DISALLOW_COPY_AND_ASSIGN(PassInfoPrinter);
1505e8b137d28c840b128e2488f954cccee3e86db14David Brazdil};
1515e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
152809658ec5c5515ae9eb87d32012ec5e088612949David Brazdilclass PassInfo : public ValueObject {
153809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil public:
154809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  PassInfo(const char *pass_name, PassInfoPrinter* pass_info_printer)
155809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      : pass_name_(pass_name),
156809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        pass_info_printer_(pass_info_printer) {
157809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    pass_info_printer_->StartPass(pass_name_);
158809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
159809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
160809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  ~PassInfo() {
161809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    pass_info_printer_->EndPass(pass_name_);
162809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  }
163809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
164809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil private:
165809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  const char* const pass_name_;
166809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  PassInfoPrinter* const pass_info_printer_;
167809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil};
168809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil
16953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampeclass OptimizingCompiler FINAL : public Compiler {
17053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe public:
17153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  explicit OptimizingCompiler(CompilerDriver* driver);
17288157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  ~OptimizingCompiler();
17353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
17453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
17553c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe      OVERRIDE;
17653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
17753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
17853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t access_flags,
17953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          InvokeType invoke_type,
18053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint16_t class_def_idx,
18153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          uint32_t method_idx,
18253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          jobject class_loader,
18353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                          const DexFile& dex_file) const OVERRIDE;
18453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
185216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  CompiledMethod* TryCompile(const DexFile::CodeItem* code_item,
186216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t access_flags,
187216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             InvokeType invoke_type,
188216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint16_t class_def_idx,
189216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             uint32_t method_idx,
190216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             jobject class_loader,
191216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const;
192216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
19353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  CompiledMethod* JniCompile(uint32_t access_flags,
19453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe                             uint32_t method_idx,
195216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                             const DexFile& dex_file) const OVERRIDE {
196216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
197216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
19853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
199e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
200216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
201216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
202216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
203216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
20453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
205216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
20653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
207ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  void Init() OVERRIDE;
20853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
209216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  void UnInit() const OVERRIDE;
21053c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2112be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
2122be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    if (compilation_stats_.get() != nullptr) {
2132be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      compilation_stats_->RecordStat(compilation_stat);
2142be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    }
2152be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
2162be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle
21753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe private:
21888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // Whether we should run any optimization or register allocation. If false, will
21988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  // just run the code generation after the graph was built.
22088157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray  const bool run_optimizations_;
22148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
22212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Optimize and compile `graph`.
22312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileOptimized(HGraph* graph,
22412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CodeGenerator* codegen,
22512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   CompilerDriver* driver,
22612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                   const DexCompilationUnit& dex_compilation_unit,
2275e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                                   PassInfoPrinter* pass_info) const;
22812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
22912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  // Just compile without doing optimizations.
23012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CompiledMethod* CompileBaseline(CodeGenerator* codegen,
23112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  CompilerDriver* driver,
23212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                  const DexCompilationUnit& dex_compilation_unit) const;
23312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
2342be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
23588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
23653c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  std::unique_ptr<std::ostream> visualizer_output_;
23753c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
238216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // Delegate to Quick in case the optimizing compiler cannot compile a method.
239216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  std::unique_ptr<Compiler> delegate_;
240216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
24153c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
24253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe};
24353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
24488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffraystatic const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
24588157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
24688157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
24788157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray    : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
24888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray      run_optimizations_(
249a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray          (driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime)
250a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray          && !driver->GetCompilerOptions().GetDebuggable()),
251216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray      delegate_(Create(driver, Compiler::Kind::kQuick)) {}
252ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil
253ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdilvoid OptimizingCompiler::Init() {
254216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->Init();
255ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // Enable C1visualizer output. Must be done in Init() because the compiler
256ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  // driver is not fully initialized when passed to the compiler's constructor.
257ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil  CompilerDriver* driver = GetCompilerDriver();
258866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  const std::string cfg_file_name = driver->GetDumpCfgFileName();
259866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil  if (!cfg_file_name.empty()) {
260ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil    CHECK_EQ(driver->GetThreadCount(), 1U)
261ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Graph visualizer requires the compiler to run single-threaded. "
262ee690a3dd364c025ebc1767d9f84097bb7473eecDavid Brazdil      << "Invoke the compiler with '-j1'.";
263866c03125a3fcd74c9fff04da87865f5eb1767d9David Brazdil    visualizer_output_.reset(new std::ofstream(cfg_file_name));
264f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray  }
2652be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (driver->GetDumpStats()) {
2662be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_.reset(new OptimizingCompilerStats());
2672be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
268f635e63318447ca04731b265a86a573c9ed1737cNicolas Geoffray}
269787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
270216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::UnInit() const {
271216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->UnInit();
272216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
273216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
27488157efc1e16707d4ae10775d4acb15121c50fe7Nicolas GeoffrayOptimizingCompiler::~OptimizingCompiler() {
2752be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  if (compilation_stats_.get() != nullptr) {
2762be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    compilation_stats_->Log();
2772be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  }
27888157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray}
27988157efc1e16707d4ae10775d4acb15121c50fe7Nicolas Geoffray
280216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffrayvoid OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu) const {
281216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  delegate_->InitCompilationUnit(cu);
282216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
283216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
284e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffraybool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
285e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
286e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
287e2dc6fa7aa319ee892d6ed407c986c5a3ec3dcd7Nicolas Geoffray  return true;
28853c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
28953c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
2901ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffraystatic bool IsInstructionSetSupported(InstructionSet instruction_set) {
2911ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  return instruction_set == kArm64
2921ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
2931ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86
2941ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray      || instruction_set == kX86_64;
2951ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray}
2961ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray
297de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffraystatic bool CanOptimize(const DexFile::CodeItem& code_item) {
298de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  // TODO: We currently cannot optimize methods with try/catch.
299de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray  return code_item.tries_size_ == 0;
300de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray}
301de58ab2c03ff8112b07ab827c8fa38f670dfc656Nicolas Geoffray
30210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravlestatic void RunOptimizations(HOptimization* optimizations[],
30310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle                             size_t length,
304809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                             PassInfoPrinter* pass_info_printer) {
30510e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  for (size_t i = 0; i < length; ++i) {
30610e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    HOptimization* optimization = optimizations[i];
307809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
308809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      PassInfo pass_info(optimization->GetPassName(), pass_info_printer);
309809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      optimization->Run();
310809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
31110e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle    optimization->Check();
31210e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle  }
31310e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle}
31410e244f9e7f6d96a95c910a2bedef5bd3810c637Calin Juravle
315e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffraystatic void RunOptimizations(HGraph* graph,
316e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             CompilerDriver* driver,
317e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             OptimizingCompilerStats* stats,
318e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray                             const DexCompilationUnit& dex_compilation_unit,
319acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle                             PassInfoPrinter* pass_info_printer,
320acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle                             StackHandleScopeCollection* handles) {
321a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ArenaAllocator* arena = graph->GetArena();
322a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
323a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
324a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
325a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
326a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
327a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
328a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HBooleanSimplifier* boolean_simplify = new (arena) HBooleanSimplifier(graph);
329a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
330a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HInliner* inliner = new (arena) HInliner(
331a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, dex_compilation_unit, dex_compilation_unit, driver, handles, stats);
332a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
333a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
334a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
335a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
336a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  LICM* licm = new (arena) LICM(graph, *side_effects);
337a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph);
338a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation =
339a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      new (arena) ReferenceTypePropagation(graph, handles);
340a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
341a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "instruction_simplifier_after_types");
342a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
343a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      graph, stats, "last_instruction_simplifier");
344a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  ReferenceTypePropagation* type_propagation2 =
345a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko      new (arena) ReferenceTypePropagation(graph, handles);
346a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko
347a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver);
34871fb52fee246b7d511f520febbd73dc7a9bbca79Andreas Gampe
3493159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  HOptimization* optimizations[] = {
350a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    intrinsics,
351a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold1,
352a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify1,
353a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation,
35418e6873c469b48aaed22148451523479eece98e3Nicolas Geoffray    dce1,
355a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify2,
356a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    inliner,
357454a481d116ec4e6dc36fab23a073017b1436d7fNicolas Geoffray    // Run another type propagation phase: inlining will open up more opprotunities
358454a481d116ec4e6dc36fab23a073017b1436d7fNicolas Geoffray    // to remove checkast/instanceof and null checks.
359a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    type_propagation2,
36046e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil    // BooleanSimplifier depends on the InstructionSimplifier removing redundant
36146e2a3915aa68c77426b71e95b9f3658250646b7David Brazdil    // suspend checks to recognize empty blocks.
362a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    boolean_simplify,
363a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    fold2,
364a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    side_effects,
365a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    gvn,
366a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    licm,
367a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    bce,
368a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    simplify3,
369a3a3c5943522e7325d60cfcbdd17aff1e138f53dVladimir Marko    dce2,
3703159674c0863f53cfbc1913d493550221ac47f02Nicolas Geoffray  };
3715e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
372809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  RunOptimizations(optimizations, arraysize(optimizations), pass_info_printer);
3735e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray}
3745e6916cea259897baaca019c5c7a5d05746306edNicolas Geoffray
375376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// The stack map we generate must be 4-byte aligned on ARM. Since existing
376376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray// maps are generated alongside these stack maps, we must also align them.
377e21dc3db191df04c100620965bee4617b3b24397Andreas Gampestatic ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
378376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t size = vector.size();
379376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  size_t aligned_size = RoundUp(size, 4);
380376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  for (; size < aligned_size; ++size) {
381376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray    vector.push_back(0);
382376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray  }
383e21dc3db191df04c100620965bee4617b3b24397Andreas Gampe  return ArrayRef<const uint8_t>(vector);
384376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray}
385376b2bbf7c39108223a7a01568a7b4b04d84eeacNicolas Geoffray
386c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampestatic void AllocateRegisters(HGraph* graph,
387c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe                              CodeGenerator* codegen,
388c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe                              PassInfoPrinter* pass_info_printer) {
389c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  PrepareForRegisterAllocation(graph).Run();
3900d9f17de8f21a10702de1510b73e89d07b3b9bbfNicolas Geoffray  SsaLivenessAnalysis liveness(graph, codegen);
391c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
392c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    PassInfo pass_info(SsaLivenessAnalysis::kLivenessPassName, pass_info_printer);
393c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    liveness.Analyze();
394c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
395c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  {
396c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    PassInfo pass_info(RegisterAllocator::kRegisterAllocatorPassName, pass_info_printer);
397c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
398c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  }
399c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe}
40012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
40112df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
40212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CodeGenerator* codegen,
40312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     CompilerDriver* compiler_driver,
40412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray                                                     const DexCompilationUnit& dex_compilation_unit,
405809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                                                     PassInfoPrinter* pass_info_printer) const {
406acf735c13998ad2a175f5a17e7bfce220073279dCalin Juravle  StackHandleScopeCollection handles(Thread::Current());
4072be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  RunOptimizations(graph, compiler_driver, compilation_stats_.get(),
408d5111bf05fc0a9974280a80eeb43db6d5227a81eNicolas Geoffray                   dex_compilation_unit, pass_info_printer, &handles);
40912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
410c2bcafe225d672611284ef247b777b9b19817c7dAndreas Gampe  AllocateRegisters(graph, codegen, pass_info_printer);
41112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
41212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CodeVectorAllocator allocator;
41312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileOptimized(&allocator);
41412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
415c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  DefaultSrcMap src_mapping_table;
4168363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky  if (compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()) {
417c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky    codegen->BuildSourceMap(&src_mapping_table);
418c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  }
419c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky
42012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> stack_map;
42112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildStackMaps(&stack_map);
42212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
4232be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledOptimized);
42412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
425c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  return CompiledMethod::SwapAllocCompiledMethod(
42612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      compiler_driver,
42712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetInstructionSet(),
42812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      ArrayRef<const uint8_t>(allocator.GetMemory()),
429aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
430aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
431aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
432aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
43312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray      codegen->GetCoreSpillMask(),
434d97dc40d186aec46bfd318b6a2026a98241d7e9cNicolas Geoffray      codegen->GetFpuSpillMask(),
435c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      &src_mapping_table,
436c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // mapping_table.
437c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(stack_map),
438c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(),  // native_gc_map.
439c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
440c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const LinkerPatch>());
44112df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
44212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
44312df9ebf72255544b0147c81b1dca6644a29764eNicolas GeoffrayCompiledMethod* OptimizingCompiler::CompileBaseline(
44412df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CodeGenerator* codegen,
44512df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    CompilerDriver* compiler_driver,
44612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray    const DexCompilationUnit& dex_compilation_unit) const {
44712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  CodeVectorAllocator allocator;
44812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->CompileBaseline(&allocator);
44912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
45012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> mapping_table;
451c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->BuildMappingTable(&mapping_table);
45212df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  DefaultSrcMap src_mapping_table;
4538363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky  if (compiler_driver->GetCompilerOptions().GetGenerateDebugInfo()) {
454c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky    codegen->BuildSourceMap(&src_mapping_table);
455c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  }
45612df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> vmap_table;
45712df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildVMapTable(&vmap_table);
45812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::vector<uint8_t> gc_map;
45912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
46012df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
4612be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kCompiledBaseline);
462aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain  return CompiledMethod::SwapAllocCompiledMethod(
463aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      compiler_driver,
464aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetInstructionSet(),
465aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      ArrayRef<const uint8_t>(allocator.GetMemory()),
466aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // Follow Quick's behavior and set the frame size to zero if it is
467aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // considered "empty" (see the definition of
468aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      // art::CodeGenerator::HasEmptyFrame).
469aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
470aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetCoreSpillMask(),
471aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      codegen->GetFpuSpillMask(),
472aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      &src_mapping_table,
473aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(mapping_table),
474aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(vmap_table),
475aa9b7c48069699e2aabedc6c0f62cb131fee0c73Roland Levillain      AlignVectorSize(gc_map),
476c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
477c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky      ArrayRef<const LinkerPatch>());
47812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray}
47912df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray
480216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::TryCompile(const DexFile::CodeItem* code_item,
481216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t access_flags,
482216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               InvokeType invoke_type,
483216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint16_t class_def_idx,
484216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               uint32_t method_idx,
485216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               jobject class_loader,
486216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                               const DexFile& dex_file) const {
4876a3c1fcb4ba42ad4d5d142c17a3712a6ddd3866fIan Rogers  UNUSED(invoke_type);
4885e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  std::string method_name = PrettyMethod(method_idx, dex_file);
4892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
490cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
491cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
4928d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // Always use the thumb2 assembler: some runtime functionality (like implicit stack
4938d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  // overflow checks) assume thumb2.
4948d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray  if (instruction_set == kArm) {
4958d486731559ba0c5e12c27b4a507181333702b7eNicolas Geoffray    instruction_set = kThumb2;
4968fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
4978fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
4988fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  // Do not attempt to compile on architectures we do not support.
4991ba0f596e9e4ddd778ab431237d11baa85594ebaNicolas Geoffray  if (!IsInstructionSetSupported(instruction_set)) {
5002be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
5018fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray    return nullptr;
5028fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray  }
5038fb5ce3a7e1dec587642f900be86729c10224174Nicolas Geoffray
504b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
5052be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
506b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray    return nullptr;
507b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray  }
508b5f62b3dc5ac2731ba8ad53cdf3d9bdb14fbf86bNicolas Geoffray
50936540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // Implementation of the space filter: do not compile a code item whose size in
51036540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  // code units is bigger than 256.
51136540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  static constexpr size_t kSpaceFilterOptimizingThreshold = 256;
51236540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
51336540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
51436540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
5152be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
51636540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray    return nullptr;
51736540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray  }
51836540cb4d549c706cc7cd23086684f1548a91042Nicolas Geoffray
51992cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray  DexCompilationUnit dex_compilation_unit(
52092cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray    nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
52172d32629303f8f39362a4099481f48646aed042fIan Rogers    class_def_idx, method_idx, access_flags,
522cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle    compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
52392cf83e001357329cbf41fa15a6e053fab6f4933Nicolas Geoffray
5243cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle  bool requires_barrier = dex_compilation_unit.IsConstructor()
5253cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
5263cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetDexFile(),
5273cd4fc8bbb40a57d2ffde85f543c124f53237c1dCalin Juravle                                                     dex_compilation_unit.GetClassDefIndex());
528579ea7d0d89a1d7f02c35ef1beb6328a37259735Nicolas Geoffray  ArenaAllocator arena(Runtime::Current()->GetArenaPool());
529e0fe7ae36180863e45cbb9d1e6e9c30b1b1a949cNicolas Geoffray  HGraph* graph = new (&arena) HGraph(
530e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      &arena, dex_file, method_idx, requires_barrier, compiler_driver->GetInstructionSet(),
531e401d146407d61eeb99f8d6176b2ac13c4df1e33Mathieu Chartier      kInvalidInvokeType, compiler_driver->GetCompilerOptions().GetDebuggable());
532e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray
5338ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // For testing purposes, we put a special marker on method names that should be compiled
5348ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray  // with this compiler. This makes sure we're not regressing.
535e53798a7e3267305f696bf658e418c92e63e0834Nicolas Geoffray  bool shouldCompile = method_name.find("$opt$") != std::string::npos;
536a3d90fb449e2fba87211a7de323faff63cba41fcNicolas Geoffray  bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos && run_optimizations_;
5378ccc3f5d06fd217cdaabd37e743adab2031d3720Nicolas Geoffray
53812df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  std::unique_ptr<CodeGenerator> codegen(
539cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle      CodeGenerator::Create(graph,
540cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            instruction_set,
541cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            *compiler_driver->GetInstructionSetFeatures(),
542cd6dffedf1bd8e6dfb3fb0c933551f9a90f7de3fCalin Juravle                            compiler_driver->GetCompilerOptions()));
54312df9ebf72255544b0147c81b1dca6644a29764eNicolas Geoffray  if (codegen.get() == nullptr) {
5445667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
5452be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
546787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray    return nullptr;
547787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray  }
548c6b4dd8980350aaf250f0185f73e9c42ec17cd57David Srbecky  codegen->GetAssembler()->cfi().SetEnabled(
5498363c772581bf00ebcdc2e38391b4bfae51beb75David Srbecky      compiler_driver->GetCompilerOptions().GetGenerateDebugInfo());
550787c3076635cf117eb646c5a89a9014b2072fb44Nicolas Geoffray
551809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  PassInfoPrinter pass_info_printer(graph,
552809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                                    method_name.c_str(),
553809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                                    *codegen.get(),
554809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                                    visualizer_output_.get(),
555809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                                    compiler_driver);
5565e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
5575e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  HGraphBuilder builder(graph,
5585e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
5595e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_compilation_unit,
5605e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        &dex_file,
5615e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                        compiler_driver,
5622be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle                        compilation_stats_.get());
5635e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
5645e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  VLOG(compiler) << "Building " << method_name;
5655e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
566809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil  {
5677c3952f423b8213083d60596a5f0bf4237ca3f7bAndreas Gampe    PassInfo pass_info(HGraphBuilder::kBuilderPassName, &pass_info_printer);
568809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    if (!builder.BuildGraph(*code_item)) {
569809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
570809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      return nullptr;
571809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    }
5725e8b137d28c840b128e2488f954cccee3e86db14David Brazdil  }
573a7062e05e6048c7f817d784a5b94e3122e25b1ecNicolas Geoffray
57448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_optimize = CanOptimize(*code_item);
57548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
576216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
577216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // `run_optimizations_` is set explicitly (either through a compiler filter
578216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // or the debuggable flag). If it is set, we can run baseline. Otherwise, we fall back
579216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  // to Quick.
580216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  bool can_use_baseline = !run_optimizations_;
58148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle  if (run_optimizations_ && can_optimize && can_allocate_registers) {
5825e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Optimizing " << method_name;
5835e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
584809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil    {
5857c3952f423b8213083d60596a5f0bf4237ca3f7bAndreas Gampe      PassInfo pass_info(SsaBuilder::kSsaBuilderPassName, &pass_info_printer);
586809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      if (!graph->TryBuildingSsa()) {
587809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        // We could not transform the graph to SSA, bailout.
588809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        LOG(INFO) << "Skipping compilation of " << method_name << ": it contains a non natural loop";
5892be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle        MaybeRecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
590809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil        return nullptr;
591809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil      }
592f537012ceb6cba8a78b36a5065beb9588451a250Nicolas Geoffray    }
5935e8b137d28c840b128e2488f954cccee3e86db14David Brazdil
5945e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    return CompileOptimized(graph,
5955e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                            codegen.get(),
5965e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                            compiler_driver,
5975e8b137d28c840b128e2488f954cccee3e86db14David Brazdil                            dex_compilation_unit,
598809658ec5c5515ae9eb87d32012ec5e088612949David Brazdil                            &pass_info_printer);
599234d69d075d1608f80adb647f7935077b62b6376Nicolas Geoffray  } else if (shouldOptimize && can_allocate_registers) {
60086dbb9a12119273039ce272b41c809fa548b37b6Nicolas Geoffray    LOG(FATAL) << "Could not allocate registers in optimizing compiler";
6015667fdbb6e441dee7534ade18b628ed396daf593Zheng Xu    UNREACHABLE();
602216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else if (can_use_baseline) {
6035e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    VLOG(compiler) << "Compile baseline " << method_name;
60448c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
60548c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    if (!run_optimizations_) {
6062be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedDisabled);
60748c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_optimize) {
6082be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
60948c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    } else if (!can_allocate_registers) {
6102be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
61148c2b03965830c73cdddeae8aea8030f08430137Calin Juravle    }
61248c2b03965830c73cdddeae8aea8030f08430137Calin Juravle
6135e8b137d28c840b128e2488f954cccee3e86db14David Brazdil    return CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
614216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  } else {
615216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return nullptr;
616216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  }
617216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray}
618216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray
619216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas GeoffrayCompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
620216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t access_flags,
621216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            InvokeType invoke_type,
622216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint16_t class_def_idx,
623216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            uint32_t method_idx,
624f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                                            jobject jclass_loader,
625216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray                                            const DexFile& dex_file) const {
626f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompilerDriver* compiler_driver = GetCompilerDriver();
627f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  CompiledMethod* method = nullptr;
628f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)) {
629f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle     method = TryCompile(code_item, access_flags, invoke_type, class_def_idx,
630f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                         method_idx, jclass_loader, dex_file);
631f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  } else {
632f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
6332be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
634f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    } else {
6352be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle      MaybeRecordStat(MethodCompilationStat::kNotCompiledClassNotVerified);
636f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle    }
637f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle  }
638f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle
639216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray  if (method != nullptr) {
640216eaa2927f424821a03d7c4c6bf701fdb48e865Nicolas Geoffray    return method;
6413946844c34ad965515f677084b07d663d70ad1b8Nicolas Geoffray  }
64212be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  method = delegate_->Compile(code_item, access_flags, invoke_type, class_def_idx, method_idx,
643f1c6d9e87cbfd27702103ccc7c7f08ce784dc872Calin Juravle                              jclass_loader, dex_file);
64412be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray
64512be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  if (method != nullptr) {
6462be39e05ea8c4475278fe6a6573c3c8f04e075d4Calin Juravle    MaybeRecordStat(MethodCompilationStat::kCompiledQuick);
64712be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  }
64812be74e37b587ef2dea838d480018f4658b95ad7Nicolas Geoffray  return method;
649b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}
650b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray
65153c913bb71b218714823c8c87a1f92830c336f61Andreas GampeCompiler* CreateOptimizingCompiler(CompilerDriver* driver) {
65253c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe  return new OptimizingCompiler(driver);
65353c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe}
65453c913bb71b218714823c8c87a1f92830c336f61Andreas Gampe
655b34f69ab43aaf7a6e6045c95f398baf566ef5023Nicolas Geoffray}  // namespace art
656