optimizing_compiler.cc revision 09c2a6be63337ee060e2d54bd01cf18be7301d29
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "optimizing_compiler.h"
18
19#include <fstream>
20#include <memory>
21#include <stdint.h>
22
23#ifdef ART_ENABLE_CODEGEN_arm
24#include "dex_cache_array_fixups_arm.h"
25#endif
26
27#ifdef ART_ENABLE_CODEGEN_arm64
28#include "instruction_simplifier_arm64.h"
29#endif
30
31#ifdef ART_ENABLE_CODEGEN_x86
32#include "pc_relative_fixups_x86.h"
33#endif
34
35#include "art_method-inl.h"
36#include "base/arena_allocator.h"
37#include "base/arena_containers.h"
38#include "base/dumpable.h"
39#include "base/macros.h"
40#include "base/timing_logger.h"
41#include "bounds_check_elimination.h"
42#include "builder.h"
43#include "code_generator.h"
44#include "compiled_method.h"
45#include "compiler.h"
46#include "constant_folding.h"
47#include "dead_code_elimination.h"
48#include "debug/elf_debug_writer.h"
49#include "debug/method_debug_info.h"
50#include "dex/quick/dex_file_to_method_inliner_map.h"
51#include "dex/verification_results.h"
52#include "dex/verified_method.h"
53#include "driver/compiler_driver-inl.h"
54#include "driver/compiler_options.h"
55#include "driver/dex_compilation_unit.h"
56#include "elf_writer_quick.h"
57#include "graph_checker.h"
58#include "graph_visualizer.h"
59#include "gvn.h"
60#include "induction_var_analysis.h"
61#include "inliner.h"
62#include "instruction_simplifier.h"
63#include "instruction_simplifier_arm.h"
64#include "intrinsics.h"
65#include "jit/debugger_interface.h"
66#include "jit/jit_code_cache.h"
67#include "jni/quick/jni_compiler.h"
68#include "licm.h"
69#include "load_store_elimination.h"
70#include "nodes.h"
71#include "oat_quick_method_header.h"
72#include "prepare_for_register_allocation.h"
73#include "reference_type_propagation.h"
74#include "register_allocator.h"
75#include "select_generator.h"
76#include "sharpening.h"
77#include "side_effects_analysis.h"
78#include "ssa_builder.h"
79#include "ssa_liveness_analysis.h"
80#include "ssa_phi_elimination.h"
81#include "utils/assembler.h"
82#include "verifier/method_verifier.h"
83
84namespace art {
85
86/**
87 * Used by the code generator, to allocate the code in a vector.
88 */
89class CodeVectorAllocator FINAL : public CodeAllocator {
90 public:
91  explicit CodeVectorAllocator(ArenaAllocator* arena)
92      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
93        size_(0) {}
94
95  virtual uint8_t* Allocate(size_t size) {
96    size_ = size;
97    memory_.resize(size);
98    return &memory_[0];
99  }
100
101  size_t GetSize() const { return size_; }
102  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
103
104 private:
105  ArenaVector<uint8_t> memory_;
106  size_t size_;
107
108  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
109};
110
111/**
112 * Filter to apply to the visualizer. Methods whose name contain that filter will
113 * be dumped.
114 */
115static constexpr const char kStringFilter[] = "";
116
117class PassScope;
118
119class PassObserver : public ValueObject {
120 public:
121  PassObserver(HGraph* graph,
122               CodeGenerator* codegen,
123               std::ostream* visualizer_output,
124               CompilerDriver* compiler_driver)
125      : graph_(graph),
126        cached_method_name_(),
127        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
128        timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
129        disasm_info_(graph->GetArena()),
130        visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()),
131        visualizer_(visualizer_output, graph, *codegen),
132        graph_in_bad_state_(false) {
133    if (timing_logger_enabled_ || visualizer_enabled_) {
134      if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
135        timing_logger_enabled_ = visualizer_enabled_ = false;
136      }
137      if (visualizer_enabled_) {
138        visualizer_.PrintHeader(GetMethodName());
139        codegen->SetDisassemblyInformation(&disasm_info_);
140      }
141    }
142  }
143
144  ~PassObserver() {
145    if (timing_logger_enabled_) {
146      LOG(INFO) << "TIMINGS " << GetMethodName();
147      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
148    }
149  }
150
151  void DumpDisassembly() const {
152    if (visualizer_enabled_) {
153      visualizer_.DumpGraphWithDisassembly();
154    }
155  }
156
157  void SetGraphInBadState() { graph_in_bad_state_ = true; }
158
159  const char* GetMethodName() {
160    // PrettyMethod() is expensive, so we delay calling it until we actually have to.
161    if (cached_method_name_.empty()) {
162      cached_method_name_ = PrettyMethod(graph_->GetMethodIdx(), graph_->GetDexFile());
163    }
164    return cached_method_name_.c_str();
165  }
166
167 private:
168  void StartPass(const char* pass_name) {
169    // Dump graph first, then start timer.
170    if (visualizer_enabled_) {
171      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
172    }
173    if (timing_logger_enabled_) {
174      timing_logger_.StartTiming(pass_name);
175    }
176  }
177
178  void EndPass(const char* pass_name) {
179    // Pause timer first, then dump graph.
180    if (timing_logger_enabled_) {
181      timing_logger_.EndTiming();
182    }
183    if (visualizer_enabled_) {
184      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
185    }
186
187    // Validate the HGraph if running in debug mode.
188    if (kIsDebugBuild) {
189      if (!graph_in_bad_state_) {
190        GraphChecker checker(graph_);
191        checker.Run();
192        if (!checker.IsValid()) {
193          LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
194        }
195      }
196    }
197  }
198
199  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
200    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
201    // empty kStringFilter matching all methods.
202    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
203      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
204    }
205
206    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
207    // warning when the string is empty.
208    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
209    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
210      return true;
211    }
212
213    return false;
214  }
215
216  HGraph* const graph_;
217
218  std::string cached_method_name_;
219
220  bool timing_logger_enabled_;
221  TimingLogger timing_logger_;
222
223  DisassemblyInformation disasm_info_;
224
225  bool visualizer_enabled_;
226  HGraphVisualizer visualizer_;
227
228  // Flag to be set by the compiler if the pass failed and the graph is not
229  // expected to validate.
230  bool graph_in_bad_state_;
231
232  friend PassScope;
233
234  DISALLOW_COPY_AND_ASSIGN(PassObserver);
235};
236
237class PassScope : public ValueObject {
238 public:
239  PassScope(const char *pass_name, PassObserver* pass_observer)
240      : pass_name_(pass_name),
241        pass_observer_(pass_observer) {
242    pass_observer_->StartPass(pass_name_);
243  }
244
245  ~PassScope() {
246    pass_observer_->EndPass(pass_name_);
247  }
248
249 private:
250  const char* const pass_name_;
251  PassObserver* const pass_observer_;
252};
253
254class OptimizingCompiler FINAL : public Compiler {
255 public:
256  explicit OptimizingCompiler(CompilerDriver* driver);
257  ~OptimizingCompiler();
258
259  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
260      OVERRIDE;
261
262  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
263                          uint32_t access_flags,
264                          InvokeType invoke_type,
265                          uint16_t class_def_idx,
266                          uint32_t method_idx,
267                          jobject class_loader,
268                          const DexFile& dex_file,
269                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
270
271  CompiledMethod* JniCompile(uint32_t access_flags,
272                             uint32_t method_idx,
273                             const DexFile& dex_file) const OVERRIDE {
274    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
275  }
276
277  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
278      SHARED_REQUIRES(Locks::mutator_lock_) {
279    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
280        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
281  }
282
283  void InitCompilationUnit(CompilationUnit& cu) const OVERRIDE;
284
285  void Init() OVERRIDE;
286
287  void UnInit() const OVERRIDE;
288
289  void MaybeRecordStat(MethodCompilationStat compilation_stat) const {
290    if (compilation_stats_.get() != nullptr) {
291      compilation_stats_->RecordStat(compilation_stat);
292    }
293  }
294
295  bool JitCompile(Thread* self, jit::JitCodeCache* code_cache, ArtMethod* method, bool osr)
296      OVERRIDE
297      SHARED_REQUIRES(Locks::mutator_lock_);
298
299 private:
300  // Create a 'CompiledMethod' for an optimized graph.
301  CompiledMethod* Emit(ArenaAllocator* arena,
302                       CodeVectorAllocator* code_allocator,
303                       CodeGenerator* codegen,
304                       CompilerDriver* driver,
305                       const DexFile::CodeItem* item) const;
306
307  // Try compiling a method and return the code generator used for
308  // compiling it.
309  // This method:
310  // 1) Builds the graph. Returns null if it failed to build it.
311  // 2) Transforms the graph to SSA. Returns null if it failed.
312  // 3) Runs optimizations on the graph, including register allocator.
313  // 4) Generates code with the `code_allocator` provided.
314  CodeGenerator* TryCompile(ArenaAllocator* arena,
315                            CodeVectorAllocator* code_allocator,
316                            const DexFile::CodeItem* code_item,
317                            uint32_t access_flags,
318                            InvokeType invoke_type,
319                            uint16_t class_def_idx,
320                            uint32_t method_idx,
321                            jobject class_loader,
322                            const DexFile& dex_file,
323                            Handle<mirror::DexCache> dex_cache,
324                            bool osr) const;
325
326  std::unique_ptr<OptimizingCompilerStats> compilation_stats_;
327
328  std::unique_ptr<std::ostream> visualizer_output_;
329
330  DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
331};
332
333static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
334
335OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
336    : Compiler(driver, kMaximumCompilationTimeBeforeWarning) {}
337
338void OptimizingCompiler::Init() {
339  // Enable C1visualizer output. Must be done in Init() because the compiler
340  // driver is not fully initialized when passed to the compiler's constructor.
341  CompilerDriver* driver = GetCompilerDriver();
342  const std::string cfg_file_name = driver->GetCompilerOptions().GetDumpCfgFileName();
343  if (!cfg_file_name.empty()) {
344    CHECK_EQ(driver->GetThreadCount(), 1U)
345      << "Graph visualizer requires the compiler to run single-threaded. "
346      << "Invoke the compiler with '-j1'.";
347    std::ios_base::openmode cfg_file_mode =
348        driver->GetCompilerOptions().GetDumpCfgAppend() ? std::ofstream::app : std::ofstream::out;
349    visualizer_output_.reset(new std::ofstream(cfg_file_name, cfg_file_mode));
350  }
351  if (driver->GetDumpStats()) {
352    compilation_stats_.reset(new OptimizingCompilerStats());
353  }
354}
355
356void OptimizingCompiler::UnInit() const {
357}
358
359OptimizingCompiler::~OptimizingCompiler() {
360  if (compilation_stats_.get() != nullptr) {
361    compilation_stats_->Log();
362  }
363}
364
365void OptimizingCompiler::InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const {
366}
367
368bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
369                                          const DexFile& dex_file ATTRIBUTE_UNUSED,
370                                          CompilationUnit* cu ATTRIBUTE_UNUSED) const {
371  return true;
372}
373
374static bool IsInstructionSetSupported(InstructionSet instruction_set) {
375  return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat)
376      || instruction_set == kArm64
377      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
378      || instruction_set == kMips
379      || instruction_set == kMips64
380      || instruction_set == kX86
381      || instruction_set == kX86_64;
382}
383
384// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment.
385// TODO: Add support for other architectures and remove this function
386static bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) {
387  return instruction_set == kArm64
388      || instruction_set == kThumb2
389      || instruction_set == kX86
390      || instruction_set == kX86_64;
391}
392
393static void RunOptimizations(HOptimization* optimizations[],
394                             size_t length,
395                             PassObserver* pass_observer) {
396  for (size_t i = 0; i < length; ++i) {
397    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
398    optimizations[i]->Run();
399  }
400}
401
402static void MaybeRunInliner(HGraph* graph,
403                            CodeGenerator* codegen,
404                            CompilerDriver* driver,
405                            OptimizingCompilerStats* stats,
406                            const DexCompilationUnit& dex_compilation_unit,
407                            PassObserver* pass_observer,
408                            StackHandleScopeCollection* handles) {
409  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
410  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
411      && (compiler_options.GetInlineMaxCodeUnits() > 0);
412  if (!should_inline) {
413    return;
414  }
415  size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
416  HInliner* inliner = new (graph->GetArena()) HInliner(
417      graph,
418      graph,
419      codegen,
420      dex_compilation_unit,
421      dex_compilation_unit,
422      driver,
423      handles,
424      stats,
425      number_of_dex_registers,
426      /* depth */ 0);
427  HOptimization* optimizations[] = { inliner };
428
429  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
430}
431
432static void RunArchOptimizations(InstructionSet instruction_set,
433                                 HGraph* graph,
434                                 CodeGenerator* codegen,
435                                 OptimizingCompilerStats* stats,
436                                 PassObserver* pass_observer) {
437  ArenaAllocator* arena = graph->GetArena();
438  switch (instruction_set) {
439#ifdef ART_ENABLE_CODEGEN_arm
440    case kThumb2:
441    case kArm: {
442      arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats);
443      arm::InstructionSimplifierArm* simplifier =
444          new (arena) arm::InstructionSimplifierArm(graph, stats);
445      HOptimization* arm_optimizations[] = {
446        simplifier,
447        fixups
448      };
449      RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer);
450      break;
451    }
452#endif
453#ifdef ART_ENABLE_CODEGEN_arm64
454    case kArm64: {
455      arm64::InstructionSimplifierArm64* simplifier =
456          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
457      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
458      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
459      HOptimization* arm64_optimizations[] = {
460        simplifier,
461        side_effects,
462        gvn
463      };
464      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
465      break;
466    }
467#endif
468#ifdef ART_ENABLE_CODEGEN_x86
469    case kX86: {
470      x86::PcRelativeFixups* pc_relative_fixups =
471          new (arena) x86::PcRelativeFixups(graph, codegen, stats);
472      HOptimization* x86_optimizations[] = {
473          pc_relative_fixups
474      };
475      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
476      break;
477    }
478#endif
479    default:
480      break;
481  }
482}
483
484NO_INLINE  // Avoid increasing caller's frame size by large stack-allocated objects.
485static void AllocateRegisters(HGraph* graph,
486                              CodeGenerator* codegen,
487                              PassObserver* pass_observer) {
488  {
489    PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName,
490                    pass_observer);
491    PrepareForRegisterAllocation(graph).Run();
492  }
493  SsaLivenessAnalysis liveness(graph, codegen);
494  {
495    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
496    liveness.Analyze();
497  }
498  {
499    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
500    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
501  }
502}
503
504static void RunOptimizations(HGraph* graph,
505                             CodeGenerator* codegen,
506                             CompilerDriver* driver,
507                             OptimizingCompilerStats* stats,
508                             const DexCompilationUnit& dex_compilation_unit,
509                             PassObserver* pass_observer,
510                             StackHandleScopeCollection* handles) {
511  ArenaAllocator* arena = graph->GetArena();
512  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
513      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
514  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
515      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
516  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
517  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
518  HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, stats);
519  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
520  HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce");
521  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
522  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
523  LICM* licm = new (arena) LICM(graph, *side_effects, stats);
524  LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects);
525  HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
526  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction);
527  HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver);
528  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
529      graph, stats, "instruction_simplifier_after_bce");
530  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
531      graph, stats, "instruction_simplifier_before_codegen");
532  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver, stats);
533
534  HOptimization* optimizations1[] = {
535    intrinsics,
536    sharpening,
537    fold1,
538    simplify1,
539    dce1,
540  };
541  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
542
543  MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles);
544
545  HOptimization* optimizations2[] = {
546    // SelectGenerator depends on the InstructionSimplifier removing
547    // redundant suspend checks to recognize empty blocks.
548    select_generator,
549    fold2,  // TODO: if we don't inline we can also skip fold2.
550    side_effects,
551    gvn,
552    licm,
553    induction,
554    bce,
555    fold3,  // evaluates code generated by dynamic bce
556    simplify2,
557    lse,
558    dce2,
559    // The codegen has a few assumptions that only the instruction simplifier
560    // can satisfy. For example, the code generator does not expect to see a
561    // HTypeConversion from a type to the same type.
562    simplify3,
563  };
564  RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
565
566  RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, stats, pass_observer);
567  AllocateRegisters(graph, codegen, pass_observer);
568}
569
570static ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
571  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
572  codegen->EmitLinkerPatches(&linker_patches);
573
574  // Sort patches by literal offset. Required for .oat_patches encoding.
575  std::sort(linker_patches.begin(), linker_patches.end(),
576            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
577    return lhs.LiteralOffset() < rhs.LiteralOffset();
578  });
579
580  return linker_patches;
581}
582
583CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena,
584                                         CodeVectorAllocator* code_allocator,
585                                         CodeGenerator* codegen,
586                                         CompilerDriver* compiler_driver,
587                                         const DexFile::CodeItem* code_item) const {
588  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
589  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
590  stack_map.resize(codegen->ComputeStackMapsSize());
591  codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), *code_item);
592
593  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
594      compiler_driver,
595      codegen->GetInstructionSet(),
596      ArrayRef<const uint8_t>(code_allocator->GetMemory()),
597      // Follow Quick's behavior and set the frame size to zero if it is
598      // considered "empty" (see the definition of
599      // art::CodeGenerator::HasEmptyFrame).
600      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
601      codegen->GetCoreSpillMask(),
602      codegen->GetFpuSpillMask(),
603      ArrayRef<const SrcMapElem>(),
604      ArrayRef<const uint8_t>(),  // mapping_table.
605      ArrayRef<const uint8_t>(stack_map),
606      ArrayRef<const uint8_t>(),  // native_gc_map.
607      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
608      ArrayRef<const LinkerPatch>(linker_patches));
609
610  return compiled_method;
611}
612
613CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena,
614                                              CodeVectorAllocator* code_allocator,
615                                              const DexFile::CodeItem* code_item,
616                                              uint32_t access_flags,
617                                              InvokeType invoke_type,
618                                              uint16_t class_def_idx,
619                                              uint32_t method_idx,
620                                              jobject class_loader,
621                                              const DexFile& dex_file,
622                                              Handle<mirror::DexCache> dex_cache,
623                                              bool osr) const {
624  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
625  CompilerDriver* compiler_driver = GetCompilerDriver();
626  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
627
628  // Always use the Thumb-2 assembler: some runtime functionality
629  // (like implicit stack overflow checks) assume Thumb-2.
630  if (instruction_set == kArm) {
631    instruction_set = kThumb2;
632  }
633
634  // Do not attempt to compile on architectures we do not support.
635  if (!IsInstructionSetSupported(instruction_set)) {
636    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
637    return nullptr;
638  }
639
640  // When read barriers are enabled, do not attempt to compile for
641  // instruction sets that have no read barrier support.
642  if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) {
643    return nullptr;
644  }
645
646  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
647    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
648    return nullptr;
649  }
650
651  // Implementation of the space filter: do not compile a code item whose size in
652  // code units is bigger than 128.
653  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
654  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
655  if ((compiler_options.GetCompilerFilter() == CompilerOptions::kSpace)
656      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
657    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
658    return nullptr;
659  }
660
661  DexCompilationUnit dex_compilation_unit(
662    nullptr, class_loader, Runtime::Current()->GetClassLinker(), dex_file, code_item,
663    class_def_idx, method_idx, access_flags,
664    nullptr, dex_cache);
665
666  bool requires_barrier = dex_compilation_unit.IsConstructor()
667      && compiler_driver->RequiresConstructorBarrier(Thread::Current(),
668                                                     dex_compilation_unit.GetDexFile(),
669                                                     dex_compilation_unit.GetClassDefIndex());
670
671  HGraph* graph = new (arena) HGraph(
672      arena,
673      dex_file,
674      method_idx,
675      requires_barrier,
676      compiler_driver->GetInstructionSet(),
677      kInvalidInvokeType,
678      compiler_driver->GetCompilerOptions().GetDebuggable(),
679      osr);
680
681  const uint8_t* interpreter_metadata = nullptr;
682  {
683    ScopedObjectAccess soa(Thread::Current());
684    StackHandleScope<1> hs(soa.Self());
685    Handle<mirror::ClassLoader> loader(hs.NewHandle(
686        soa.Decode<mirror::ClassLoader*>(class_loader)));
687    ArtMethod* art_method = compiler_driver->ResolveMethod(
688        soa, dex_cache, loader, &dex_compilation_unit, method_idx, invoke_type);
689    // We may not get a method, for example if its class is erroneous.
690    if (art_method != nullptr) {
691      graph->SetArtMethod(art_method);
692      interpreter_metadata = art_method->GetQuickenedInfo();
693    }
694  }
695
696  std::unique_ptr<CodeGenerator> codegen(
697      CodeGenerator::Create(graph,
698                            instruction_set,
699                            *compiler_driver->GetInstructionSetFeatures(),
700                            compiler_driver->GetCompilerOptions()));
701  if (codegen.get() == nullptr) {
702    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
703    return nullptr;
704  }
705  codegen->GetAssembler()->cfi().SetEnabled(
706      compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
707
708  PassObserver pass_observer(graph,
709                             codegen.get(),
710                             visualizer_output_.get(),
711                             compiler_driver);
712
713  VLOG(compiler) << "Building " << pass_observer.GetMethodName();
714
715  {
716    ScopedObjectAccess soa(Thread::Current());
717    StackHandleScopeCollection handles(soa.Self());
718    // Do not hold `mutator_lock_` between optimizations.
719    ScopedThreadSuspension sts(soa.Self(), kNative);
720
721    {
722      PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
723      HGraphBuilder builder(graph,
724                            &dex_compilation_unit,
725                            &dex_compilation_unit,
726                            &dex_file,
727                            compiler_driver,
728                            compilation_stats_.get(),
729                            interpreter_metadata,
730                            dex_cache);
731      GraphAnalysisResult result = builder.BuildGraph(*code_item, &handles);
732      if (result != kAnalysisSuccess) {
733        switch (result) {
734          case kAnalysisInvalidBytecode:
735            break;
736          case kAnalysisFailThrowCatchLoop:
737            MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop);
738            break;
739          case kAnalysisFailAmbiguousArrayOp:
740            MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp);
741            break;
742          case kAnalysisSuccess:
743            UNREACHABLE();
744        }
745        pass_observer.SetGraphInBadState();
746        return nullptr;
747      }
748    }
749
750    RunOptimizations(graph,
751                     codegen.get(),
752                     compiler_driver,
753                     compilation_stats_.get(),
754                     dex_compilation_unit,
755                     &pass_observer,
756                     &handles);
757
758    codegen->Compile(code_allocator);
759    pass_observer.DumpDisassembly();
760  }
761
762  if (kArenaAllocatorCountAllocations) {
763    if (arena->BytesAllocated() > 4 * MB) {
764      MemStats mem_stats(arena->GetMemStats());
765      LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
766    }
767  }
768
769  return codegen.release();
770}
771
772static bool CanHandleVerificationFailure(const VerifiedMethod* verified_method) {
773  // For access errors the compiler will use the unresolved helpers (e.g. HInvokeUnresolved).
774  uint32_t unresolved_mask = verifier::VerifyError::VERIFY_ERROR_NO_CLASS
775      | verifier::VerifyError::VERIFY_ERROR_ACCESS_CLASS
776      | verifier::VerifyError::VERIFY_ERROR_ACCESS_FIELD
777      | verifier::VerifyError::VERIFY_ERROR_ACCESS_METHOD;
778  return (verified_method->GetEncounteredVerificationFailures() & (~unresolved_mask)) == 0;
779}
780
781CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
782                                            uint32_t access_flags,
783                                            InvokeType invoke_type,
784                                            uint16_t class_def_idx,
785                                            uint32_t method_idx,
786                                            jobject jclass_loader,
787                                            const DexFile& dex_file,
788                                            Handle<mirror::DexCache> dex_cache) const {
789  CompilerDriver* compiler_driver = GetCompilerDriver();
790  CompiledMethod* method = nullptr;
791  DCHECK(Runtime::Current()->IsAotCompiler());
792  const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
793  DCHECK(!verified_method->HasRuntimeThrow());
794  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
795      || CanHandleVerificationFailure(verified_method)) {
796    ArenaAllocator arena(Runtime::Current()->GetArenaPool());
797    CodeVectorAllocator code_allocator(&arena);
798    std::unique_ptr<CodeGenerator> codegen(
799        TryCompile(&arena,
800                   &code_allocator,
801                   code_item,
802                   access_flags,
803                   invoke_type,
804                   class_def_idx,
805                   method_idx,
806                   jclass_loader,
807                   dex_file,
808                   dex_cache,
809                   /* osr */ false));
810    if (codegen.get() != nullptr) {
811      MaybeRecordStat(MethodCompilationStat::kCompiled);
812      method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item);
813    }
814  } else {
815    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
816      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
817    } else {
818      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError);
819    }
820  }
821
822  if (kIsDebugBuild &&
823      IsCompilingWithCoreImage() &&
824      IsInstructionSetSupported(compiler_driver->GetInstructionSet()) &&
825      (!kEmitCompilerReadBarrier ||
826       InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) {
827    // For testing purposes, we put a special marker on method names
828    // that should be compiled with this compiler (when the the
829    // instruction set is supported -- and has support for read
830    // barriers, if they are enabled). This makes sure we're not
831    // regressing.
832    std::string method_name = PrettyMethod(method_idx, dex_file);
833    bool shouldCompile = method_name.find("$opt$") != std::string::npos;
834    DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
835  }
836
837  return method;
838}
839
840Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
841  return new OptimizingCompiler(driver);
842}
843
844bool IsCompilingWithCoreImage() {
845  const std::string& image = Runtime::Current()->GetImageLocation();
846  // TODO: This is under-approximating...
847  if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) {
848    return true;
849  }
850  return false;
851}
852
853bool OptimizingCompiler::JitCompile(Thread* self,
854                                    jit::JitCodeCache* code_cache,
855                                    ArtMethod* method,
856                                    bool osr) {
857  StackHandleScope<2> hs(self);
858  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
859      method->GetDeclaringClass()->GetClassLoader()));
860  Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
861
862  jobject jclass_loader = class_loader.ToJObject();
863  const DexFile* dex_file = method->GetDexFile();
864  const uint16_t class_def_idx = method->GetClassDefIndex();
865  const DexFile::CodeItem* code_item = dex_file->GetCodeItem(method->GetCodeItemOffset());
866  const uint32_t method_idx = method->GetDexMethodIndex();
867  const uint32_t access_flags = method->GetAccessFlags();
868  const InvokeType invoke_type = method->GetInvokeType();
869
870  ArenaAllocator arena(Runtime::Current()->GetJitArenaPool());
871  CodeVectorAllocator code_allocator(&arena);
872  std::unique_ptr<CodeGenerator> codegen;
873  {
874    // Go to native so that we don't block GC during compilation.
875    ScopedThreadSuspension sts(self, kNative);
876    codegen.reset(
877        TryCompile(&arena,
878                   &code_allocator,
879                   code_item,
880                   access_flags,
881                   invoke_type,
882                   class_def_idx,
883                   method_idx,
884                   jclass_loader,
885                   *dex_file,
886                   dex_cache,
887                   osr));
888    if (codegen.get() == nullptr) {
889      return false;
890    }
891  }
892
893  size_t stack_map_size = codegen->ComputeStackMapsSize();
894  uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size);
895  if (stack_map_data == nullptr) {
896    return false;
897  }
898  MaybeRecordStat(MethodCompilationStat::kCompiled);
899  codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), *code_item);
900  const void* code = code_cache->CommitCode(
901      self,
902      method,
903      nullptr,
904      stack_map_data,
905      nullptr,
906      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
907      codegen->GetCoreSpillMask(),
908      codegen->GetFpuSpillMask(),
909      code_allocator.GetMemory().data(),
910      code_allocator.GetSize(),
911      osr);
912
913  if (code == nullptr) {
914    code_cache->ClearData(self, stack_map_data);
915    return false;
916  }
917
918  const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
919  if (compiler_options.GetGenerateDebugInfo()) {
920    const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
921    const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
922    debug::MethodDebugInfo info = debug::MethodDebugInfo();
923    info.trampoline_name = nullptr;
924    info.dex_file = dex_file;
925    info.class_def_index = class_def_idx;
926    info.dex_method_index = method_idx;
927    info.access_flags = access_flags;
928    info.code_item = code_item;
929    info.isa = codegen->GetInstructionSet();
930    info.deduped = false;
931    info.is_native_debuggable = compiler_options.GetNativeDebuggable();
932    info.is_optimized = true;
933    info.is_code_address_text_relative = false;
934    info.code_address = code_address;
935    info.code_size = code_allocator.GetSize();
936    info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
937    info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
938    info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
939    ArrayRef<const uint8_t> elf_file = debug::WriteDebugElfFileForMethods(
940        GetCompilerDriver()->GetInstructionSet(),
941        GetCompilerDriver()->GetInstructionSetFeatures(),
942        ArrayRef<const debug::MethodDebugInfo>(&info, 1));
943    CreateJITCodeEntryForAddress(code_address,
944                                 std::unique_ptr<const uint8_t[]>(elf_file.data()),
945                                 elf_file.size());
946  }
947
948  return true;
949}
950
951}  // namespace art
952