optimizing_compiler.cc revision 62e631a678ef18c80f37862a2dc74004954a8502
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.h"
67#include "jit/jit_code_cache.h"
68#include "jni/quick/jni_compiler.h"
69#include "licm.h"
70#include "load_store_elimination.h"
71#include "nodes.h"
72#include "oat_quick_method_header.h"
73#include "prepare_for_register_allocation.h"
74#include "reference_type_propagation.h"
75#include "register_allocator.h"
76#include "select_generator.h"
77#include "sharpening.h"
78#include "side_effects_analysis.h"
79#include "ssa_builder.h"
80#include "ssa_liveness_analysis.h"
81#include "ssa_phi_elimination.h"
82#include "utils/assembler.h"
83#include "verifier/method_verifier.h"
84
85namespace art {
86
87static constexpr size_t kArenaAllocatorMemoryReportThreshold = 8 * MB;
88
89/**
90 * Used by the code generator, to allocate the code in a vector.
91 */
92class CodeVectorAllocator FINAL : public CodeAllocator {
93 public:
94  explicit CodeVectorAllocator(ArenaAllocator* arena)
95      : memory_(arena->Adapter(kArenaAllocCodeBuffer)),
96        size_(0) {}
97
98  virtual uint8_t* Allocate(size_t size) {
99    size_ = size;
100    memory_.resize(size);
101    return &memory_[0];
102  }
103
104  size_t GetSize() const { return size_; }
105  const ArenaVector<uint8_t>& GetMemory() const { return memory_; }
106
107 private:
108  ArenaVector<uint8_t> memory_;
109  size_t size_;
110
111  DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
112};
113
114/**
115 * Filter to apply to the visualizer. Methods whose name contain that filter will
116 * be dumped.
117 */
118static constexpr const char kStringFilter[] = "";
119
120class PassScope;
121
122class PassObserver : public ValueObject {
123 public:
124  PassObserver(HGraph* graph,
125               CodeGenerator* codegen,
126               std::ostream* visualizer_output,
127               CompilerDriver* compiler_driver)
128      : graph_(graph),
129        cached_method_name_(),
130        timing_logger_enabled_(compiler_driver->GetDumpPasses()),
131        timing_logger_(timing_logger_enabled_ ? GetMethodName() : "", true, true),
132        disasm_info_(graph->GetArena()),
133        visualizer_enabled_(!compiler_driver->GetCompilerOptions().GetDumpCfgFileName().empty()),
134        visualizer_(visualizer_output, graph, *codegen),
135        graph_in_bad_state_(false) {
136    if (timing_logger_enabled_ || visualizer_enabled_) {
137      if (!IsVerboseMethod(compiler_driver, GetMethodName())) {
138        timing_logger_enabled_ = visualizer_enabled_ = false;
139      }
140      if (visualizer_enabled_) {
141        visualizer_.PrintHeader(GetMethodName());
142        codegen->SetDisassemblyInformation(&disasm_info_);
143      }
144    }
145  }
146
147  ~PassObserver() {
148    if (timing_logger_enabled_) {
149      LOG(INFO) << "TIMINGS " << GetMethodName();
150      LOG(INFO) << Dumpable<TimingLogger>(timing_logger_);
151    }
152  }
153
154  void DumpDisassembly() const {
155    if (visualizer_enabled_) {
156      visualizer_.DumpGraphWithDisassembly();
157    }
158  }
159
160  void SetGraphInBadState() { graph_in_bad_state_ = true; }
161
162  const char* GetMethodName() {
163    // PrettyMethod() is expensive, so we delay calling it until we actually have to.
164    if (cached_method_name_.empty()) {
165      cached_method_name_ = PrettyMethod(graph_->GetMethodIdx(), graph_->GetDexFile());
166    }
167    return cached_method_name_.c_str();
168  }
169
170 private:
171  void StartPass(const char* pass_name) {
172    // Dump graph first, then start timer.
173    if (visualizer_enabled_) {
174      visualizer_.DumpGraph(pass_name, /* is_after_pass */ false, graph_in_bad_state_);
175    }
176    if (timing_logger_enabled_) {
177      timing_logger_.StartTiming(pass_name);
178    }
179  }
180
181  void EndPass(const char* pass_name) {
182    // Pause timer first, then dump graph.
183    if (timing_logger_enabled_) {
184      timing_logger_.EndTiming();
185    }
186    if (visualizer_enabled_) {
187      visualizer_.DumpGraph(pass_name, /* is_after_pass */ true, graph_in_bad_state_);
188    }
189
190    // Validate the HGraph if running in debug mode.
191    if (kIsDebugBuild) {
192      if (!graph_in_bad_state_) {
193        GraphChecker checker(graph_);
194        checker.Run();
195        if (!checker.IsValid()) {
196          LOG(FATAL) << "Error after " << pass_name << ": " << Dumpable<GraphChecker>(checker);
197        }
198      }
199    }
200  }
201
202  static bool IsVerboseMethod(CompilerDriver* compiler_driver, const char* method_name) {
203    // Test an exact match to --verbose-methods. If verbose-methods is set, this overrides an
204    // empty kStringFilter matching all methods.
205    if (compiler_driver->GetCompilerOptions().HasVerboseMethods()) {
206      return compiler_driver->GetCompilerOptions().IsVerboseMethod(method_name);
207    }
208
209    // Test the kStringFilter sub-string. constexpr helper variable to silence unreachable-code
210    // warning when the string is empty.
211    constexpr bool kStringFilterEmpty = arraysize(kStringFilter) <= 1;
212    if (kStringFilterEmpty || strstr(method_name, kStringFilter) != nullptr) {
213      return true;
214    }
215
216    return false;
217  }
218
219  HGraph* const graph_;
220
221  std::string cached_method_name_;
222
223  bool timing_logger_enabled_;
224  TimingLogger timing_logger_;
225
226  DisassemblyInformation disasm_info_;
227
228  bool visualizer_enabled_;
229  HGraphVisualizer visualizer_;
230
231  // Flag to be set by the compiler if the pass failed and the graph is not
232  // expected to validate.
233  bool graph_in_bad_state_;
234
235  friend PassScope;
236
237  DISALLOW_COPY_AND_ASSIGN(PassObserver);
238};
239
240class PassScope : public ValueObject {
241 public:
242  PassScope(const char *pass_name, PassObserver* pass_observer)
243      : pass_name_(pass_name),
244        pass_observer_(pass_observer) {
245    pass_observer_->StartPass(pass_name_);
246  }
247
248  ~PassScope() {
249    pass_observer_->EndPass(pass_name_);
250  }
251
252 private:
253  const char* const pass_name_;
254  PassObserver* const pass_observer_;
255};
256
257class OptimizingCompiler FINAL : public Compiler {
258 public:
259  explicit OptimizingCompiler(CompilerDriver* driver);
260  ~OptimizingCompiler();
261
262  bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file) const OVERRIDE;
263
264  CompiledMethod* Compile(const DexFile::CodeItem* code_item,
265                          uint32_t access_flags,
266                          InvokeType invoke_type,
267                          uint16_t class_def_idx,
268                          uint32_t method_idx,
269                          jobject class_loader,
270                          const DexFile& dex_file,
271                          Handle<mirror::DexCache> dex_cache) const OVERRIDE;
272
273  CompiledMethod* JniCompile(uint32_t access_flags,
274                             uint32_t method_idx,
275                             const DexFile& dex_file) const OVERRIDE {
276    return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
277  }
278
279  uintptr_t GetEntryPointOf(ArtMethod* method) const OVERRIDE
280      SHARED_REQUIRES(Locks::mutator_lock_) {
281    return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
282        InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
283  }
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
365bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
366                                          const DexFile& dex_file ATTRIBUTE_UNUSED) const {
367  return true;
368}
369
370static bool IsInstructionSetSupported(InstructionSet instruction_set) {
371  return (instruction_set == kArm && !kArm32QuickCodeUseSoftFloat)
372      || instruction_set == kArm64
373      || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
374      || instruction_set == kMips
375      || instruction_set == kMips64
376      || instruction_set == kX86
377      || instruction_set == kX86_64;
378}
379
380// Read barrier are supported on ARM, ARM64, x86 and x86-64 at the moment.
381// TODO: Add support for other architectures and remove this function
382static bool InstructionSetSupportsReadBarrier(InstructionSet instruction_set) {
383  return instruction_set == kArm64
384      || instruction_set == kThumb2
385      || instruction_set == kX86
386      || instruction_set == kX86_64;
387}
388
389static void RunOptimizations(HOptimization* optimizations[],
390                             size_t length,
391                             PassObserver* pass_observer) {
392  for (size_t i = 0; i < length; ++i) {
393    PassScope scope(optimizations[i]->GetPassName(), pass_observer);
394    optimizations[i]->Run();
395  }
396}
397
398static void MaybeRunInliner(HGraph* graph,
399                            CodeGenerator* codegen,
400                            CompilerDriver* driver,
401                            OptimizingCompilerStats* stats,
402                            const DexCompilationUnit& dex_compilation_unit,
403                            PassObserver* pass_observer,
404                            StackHandleScopeCollection* handles) {
405  const CompilerOptions& compiler_options = driver->GetCompilerOptions();
406  bool should_inline = (compiler_options.GetInlineDepthLimit() > 0)
407      && (compiler_options.GetInlineMaxCodeUnits() > 0);
408  if (!should_inline) {
409    return;
410  }
411  size_t number_of_dex_registers = dex_compilation_unit.GetCodeItem()->registers_size_;
412  HInliner* inliner = new (graph->GetArena()) HInliner(
413      graph,
414      graph,
415      codegen,
416      dex_compilation_unit,
417      dex_compilation_unit,
418      driver,
419      handles,
420      stats,
421      number_of_dex_registers,
422      /* depth */ 0);
423  HOptimization* optimizations[] = { inliner };
424
425  RunOptimizations(optimizations, arraysize(optimizations), pass_observer);
426}
427
428static void RunArchOptimizations(InstructionSet instruction_set,
429                                 HGraph* graph,
430                                 CodeGenerator* codegen,
431                                 OptimizingCompilerStats* stats,
432                                 PassObserver* pass_observer) {
433  ArenaAllocator* arena = graph->GetArena();
434  switch (instruction_set) {
435#ifdef ART_ENABLE_CODEGEN_arm
436    case kThumb2:
437    case kArm: {
438      arm::DexCacheArrayFixups* fixups = new (arena) arm::DexCacheArrayFixups(graph, stats);
439      arm::InstructionSimplifierArm* simplifier =
440          new (arena) arm::InstructionSimplifierArm(graph, stats);
441      HOptimization* arm_optimizations[] = {
442        simplifier,
443        fixups
444      };
445      RunOptimizations(arm_optimizations, arraysize(arm_optimizations), pass_observer);
446      break;
447    }
448#endif
449#ifdef ART_ENABLE_CODEGEN_arm64
450    case kArm64: {
451      arm64::InstructionSimplifierArm64* simplifier =
452          new (arena) arm64::InstructionSimplifierArm64(graph, stats);
453      SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
454      GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects, "GVN_after_arch");
455      HOptimization* arm64_optimizations[] = {
456        simplifier,
457        side_effects,
458        gvn
459      };
460      RunOptimizations(arm64_optimizations, arraysize(arm64_optimizations), pass_observer);
461      break;
462    }
463#endif
464#ifdef ART_ENABLE_CODEGEN_x86
465    case kX86: {
466      x86::PcRelativeFixups* pc_relative_fixups =
467          new (arena) x86::PcRelativeFixups(graph, codegen, stats);
468      HOptimization* x86_optimizations[] = {
469          pc_relative_fixups
470      };
471      RunOptimizations(x86_optimizations, arraysize(x86_optimizations), pass_observer);
472      break;
473    }
474#endif
475    default:
476      break;
477  }
478}
479
480NO_INLINE  // Avoid increasing caller's frame size by large stack-allocated objects.
481static void AllocateRegisters(HGraph* graph,
482                              CodeGenerator* codegen,
483                              PassObserver* pass_observer) {
484  {
485    PassScope scope(PrepareForRegisterAllocation::kPrepareForRegisterAllocationPassName,
486                    pass_observer);
487    PrepareForRegisterAllocation(graph).Run();
488  }
489  SsaLivenessAnalysis liveness(graph, codegen);
490  {
491    PassScope scope(SsaLivenessAnalysis::kLivenessPassName, pass_observer);
492    liveness.Analyze();
493  }
494  {
495    PassScope scope(RegisterAllocator::kRegisterAllocatorPassName, pass_observer);
496    RegisterAllocator(graph->GetArena(), codegen, liveness).AllocateRegisters();
497  }
498}
499
500static void RunOptimizations(HGraph* graph,
501                             CodeGenerator* codegen,
502                             CompilerDriver* driver,
503                             OptimizingCompilerStats* stats,
504                             const DexCompilationUnit& dex_compilation_unit,
505                             PassObserver* pass_observer,
506                             StackHandleScopeCollection* handles) {
507  ArenaAllocator* arena = graph->GetArena();
508  HDeadCodeElimination* dce1 = new (arena) HDeadCodeElimination(
509      graph, stats, HDeadCodeElimination::kInitialDeadCodeEliminationPassName);
510  HDeadCodeElimination* dce2 = new (arena) HDeadCodeElimination(
511      graph, stats, HDeadCodeElimination::kFinalDeadCodeEliminationPassName);
512  HConstantFolding* fold1 = new (arena) HConstantFolding(graph);
513  InstructionSimplifier* simplify1 = new (arena) InstructionSimplifier(graph, stats);
514  HSelectGenerator* select_generator = new (arena) HSelectGenerator(graph, stats);
515  HConstantFolding* fold2 = new (arena) HConstantFolding(graph, "constant_folding_after_inlining");
516  HConstantFolding* fold3 = new (arena) HConstantFolding(graph, "constant_folding_after_bce");
517  SideEffectsAnalysis* side_effects = new (arena) SideEffectsAnalysis(graph);
518  GVNOptimization* gvn = new (arena) GVNOptimization(graph, *side_effects);
519  LICM* licm = new (arena) LICM(graph, *side_effects, stats);
520  LoadStoreElimination* lse = new (arena) LoadStoreElimination(graph, *side_effects);
521  HInductionVarAnalysis* induction = new (arena) HInductionVarAnalysis(graph);
522  BoundsCheckElimination* bce = new (arena) BoundsCheckElimination(graph, *side_effects, induction);
523  HSharpening* sharpening = new (arena) HSharpening(graph, codegen, dex_compilation_unit, driver);
524  InstructionSimplifier* simplify2 = new (arena) InstructionSimplifier(
525      graph, stats, "instruction_simplifier_after_bce");
526  InstructionSimplifier* simplify3 = new (arena) InstructionSimplifier(
527      graph, stats, "instruction_simplifier_before_codegen");
528  IntrinsicsRecognizer* intrinsics = new (arena) IntrinsicsRecognizer(graph, driver, stats);
529
530  HOptimization* optimizations1[] = {
531    intrinsics,
532    sharpening,
533    fold1,
534    simplify1,
535    dce1,
536  };
537  RunOptimizations(optimizations1, arraysize(optimizations1), pass_observer);
538
539  MaybeRunInliner(graph, codegen, driver, stats, dex_compilation_unit, pass_observer, handles);
540
541  HOptimization* optimizations2[] = {
542    // SelectGenerator depends on the InstructionSimplifier removing
543    // redundant suspend checks to recognize empty blocks.
544    select_generator,
545    fold2,  // TODO: if we don't inline we can also skip fold2.
546    side_effects,
547    gvn,
548    licm,
549    induction,
550    bce,
551    fold3,  // evaluates code generated by dynamic bce
552    simplify2,
553    lse,
554    dce2,
555    // The codegen has a few assumptions that only the instruction simplifier
556    // can satisfy. For example, the code generator does not expect to see a
557    // HTypeConversion from a type to the same type.
558    simplify3,
559  };
560  RunOptimizations(optimizations2, arraysize(optimizations2), pass_observer);
561
562  RunArchOptimizations(driver->GetInstructionSet(), graph, codegen, stats, pass_observer);
563  AllocateRegisters(graph, codegen, pass_observer);
564}
565
566static ArenaVector<LinkerPatch> EmitAndSortLinkerPatches(CodeGenerator* codegen) {
567  ArenaVector<LinkerPatch> linker_patches(codegen->GetGraph()->GetArena()->Adapter());
568  codegen->EmitLinkerPatches(&linker_patches);
569
570  // Sort patches by literal offset. Required for .oat_patches encoding.
571  std::sort(linker_patches.begin(), linker_patches.end(),
572            [](const LinkerPatch& lhs, const LinkerPatch& rhs) {
573    return lhs.LiteralOffset() < rhs.LiteralOffset();
574  });
575
576  return linker_patches;
577}
578
579CompiledMethod* OptimizingCompiler::Emit(ArenaAllocator* arena,
580                                         CodeVectorAllocator* code_allocator,
581                                         CodeGenerator* codegen,
582                                         CompilerDriver* compiler_driver,
583                                         const DexFile::CodeItem* code_item) const {
584  ArenaVector<LinkerPatch> linker_patches = EmitAndSortLinkerPatches(codegen);
585  ArenaVector<uint8_t> stack_map(arena->Adapter(kArenaAllocStackMaps));
586  stack_map.resize(codegen->ComputeStackMapsSize());
587  codegen->BuildStackMaps(MemoryRegion(stack_map.data(), stack_map.size()), *code_item);
588
589  CompiledMethod* compiled_method = CompiledMethod::SwapAllocCompiledMethod(
590      compiler_driver,
591      codegen->GetInstructionSet(),
592      ArrayRef<const uint8_t>(code_allocator->GetMemory()),
593      // Follow Quick's behavior and set the frame size to zero if it is
594      // considered "empty" (see the definition of
595      // art::CodeGenerator::HasEmptyFrame).
596      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
597      codegen->GetCoreSpillMask(),
598      codegen->GetFpuSpillMask(),
599      ArrayRef<const SrcMapElem>(),
600      ArrayRef<const uint8_t>(stack_map),
601      ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data()),
602      ArrayRef<const LinkerPatch>(linker_patches));
603
604  return compiled_method;
605}
606
607CodeGenerator* OptimizingCompiler::TryCompile(ArenaAllocator* arena,
608                                              CodeVectorAllocator* code_allocator,
609                                              const DexFile::CodeItem* code_item,
610                                              uint32_t access_flags,
611                                              InvokeType invoke_type,
612                                              uint16_t class_def_idx,
613                                              uint32_t method_idx,
614                                              jobject class_loader,
615                                              const DexFile& dex_file,
616                                              Handle<mirror::DexCache> dex_cache,
617                                              bool osr) const {
618  MaybeRecordStat(MethodCompilationStat::kAttemptCompilation);
619  CompilerDriver* compiler_driver = GetCompilerDriver();
620  InstructionSet instruction_set = compiler_driver->GetInstructionSet();
621
622  // Always use the Thumb-2 assembler: some runtime functionality
623  // (like implicit stack overflow checks) assume Thumb-2.
624  if (instruction_set == kArm) {
625    instruction_set = kThumb2;
626  }
627
628  // Do not attempt to compile on architectures we do not support.
629  if (!IsInstructionSetSupported(instruction_set)) {
630    MaybeRecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
631    return nullptr;
632  }
633
634  // When read barriers are enabled, do not attempt to compile for
635  // instruction sets that have no read barrier support.
636  if (kEmitCompilerReadBarrier && !InstructionSetSupportsReadBarrier(instruction_set)) {
637    return nullptr;
638  }
639
640  if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
641    MaybeRecordStat(MethodCompilationStat::kNotCompiledPathological);
642    return nullptr;
643  }
644
645  // Implementation of the space filter: do not compile a code item whose size in
646  // code units is bigger than 128.
647  static constexpr size_t kSpaceFilterOptimizingThreshold = 128;
648  const CompilerOptions& compiler_options = compiler_driver->GetCompilerOptions();
649  if ((compiler_options.GetCompilerFilter() == CompilerFilter::kSpace)
650      && (code_item->insns_size_in_code_units_ > kSpaceFilterOptimizingThreshold)) {
651    MaybeRecordStat(MethodCompilationStat::kNotCompiledSpaceFilter);
652    return nullptr;
653  }
654
655  DexCompilationUnit dex_compilation_unit(
656      class_loader,
657      Runtime::Current()->GetClassLinker(),
658      dex_file,
659      code_item,
660      class_def_idx,
661      method_idx,
662      access_flags,
663      /* verified_method */ nullptr,
664      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                            compilation_stats_.get()));
702  if (codegen.get() == nullptr) {
703    MaybeRecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
704    return nullptr;
705  }
706  codegen->GetAssembler()->cfi().SetEnabled(
707      compiler_driver->GetCompilerOptions().GenerateAnyDebugInfo());
708
709  PassObserver pass_observer(graph,
710                             codegen.get(),
711                             visualizer_output_.get(),
712                             compiler_driver);
713
714  VLOG(compiler) << "Building " << pass_observer.GetMethodName();
715
716  {
717    ScopedObjectAccess soa(Thread::Current());
718    StackHandleScopeCollection handles(soa.Self());
719    // Do not hold `mutator_lock_` between optimizations.
720    ScopedThreadSuspension sts(soa.Self(), kNative);
721
722    {
723      PassScope scope(HGraphBuilder::kBuilderPassName, &pass_observer);
724      HGraphBuilder builder(graph,
725                            &dex_compilation_unit,
726                            &dex_compilation_unit,
727                            &dex_file,
728                            *code_item,
729                            compiler_driver,
730                            compilation_stats_.get(),
731                            interpreter_metadata,
732                            dex_cache,
733                            &handles);
734      GraphAnalysisResult result = builder.BuildGraph();
735      if (result != kAnalysisSuccess) {
736        switch (result) {
737          case kAnalysisSkipped:
738            MaybeRecordStat(MethodCompilationStat::kNotCompiledSkipped);
739            break;
740          case kAnalysisInvalidBytecode:
741            MaybeRecordStat(MethodCompilationStat::kNotCompiledInvalidBytecode);
742            break;
743          case kAnalysisFailThrowCatchLoop:
744            MaybeRecordStat(MethodCompilationStat::kNotCompiledThrowCatchLoop);
745            break;
746          case kAnalysisFailAmbiguousArrayOp:
747            MaybeRecordStat(MethodCompilationStat::kNotCompiledAmbiguousArrayOp);
748            break;
749          case kAnalysisSuccess:
750            UNREACHABLE();
751        }
752        pass_observer.SetGraphInBadState();
753        return nullptr;
754      }
755    }
756
757    RunOptimizations(graph,
758                     codegen.get(),
759                     compiler_driver,
760                     compilation_stats_.get(),
761                     dex_compilation_unit,
762                     &pass_observer,
763                     &handles);
764
765    codegen->Compile(code_allocator);
766    pass_observer.DumpDisassembly();
767  }
768
769  return codegen.release();
770}
771
772CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
773                                            uint32_t access_flags,
774                                            InvokeType invoke_type,
775                                            uint16_t class_def_idx,
776                                            uint32_t method_idx,
777                                            jobject jclass_loader,
778                                            const DexFile& dex_file,
779                                            Handle<mirror::DexCache> dex_cache) const {
780  CompilerDriver* compiler_driver = GetCompilerDriver();
781  CompiledMethod* method = nullptr;
782  DCHECK(Runtime::Current()->IsAotCompiler());
783  const VerifiedMethod* verified_method = compiler_driver->GetVerifiedMethod(&dex_file, method_idx);
784  DCHECK(!verified_method->HasRuntimeThrow());
785  if (compiler_driver->IsMethodVerifiedWithoutFailures(method_idx, class_def_idx, dex_file)
786      || verifier::MethodVerifier::CanCompilerHandleVerificationFailure(
787            verified_method->GetEncounteredVerificationFailures())) {
788    ArenaAllocator arena(Runtime::Current()->GetArenaPool());
789    CodeVectorAllocator code_allocator(&arena);
790    std::unique_ptr<CodeGenerator> codegen(
791        TryCompile(&arena,
792                   &code_allocator,
793                   code_item,
794                   access_flags,
795                   invoke_type,
796                   class_def_idx,
797                   method_idx,
798                   jclass_loader,
799                   dex_file,
800                   dex_cache,
801                   /* osr */ false));
802    if (codegen.get() != nullptr) {
803      MaybeRecordStat(MethodCompilationStat::kCompiled);
804      method = Emit(&arena, &code_allocator, codegen.get(), compiler_driver, code_item);
805
806      if (kArenaAllocatorCountAllocations) {
807        if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
808          MemStats mem_stats(arena.GetMemStats());
809          LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
810        }
811      }
812    }
813  } else {
814    if (compiler_driver->GetCompilerOptions().VerifyAtRuntime()) {
815      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerifyAtRuntime);
816    } else {
817      MaybeRecordStat(MethodCompilationStat::kNotCompiledVerificationError);
818    }
819  }
820
821  if (kIsDebugBuild &&
822      IsCompilingWithCoreImage() &&
823      IsInstructionSetSupported(compiler_driver->GetInstructionSet()) &&
824      (!kEmitCompilerReadBarrier ||
825       InstructionSetSupportsReadBarrier(compiler_driver->GetInstructionSet()))) {
826    // For testing purposes, we put a special marker on method names
827    // that should be compiled with this compiler (when the the
828    // instruction set is supported -- and has support for read
829    // barriers, if they are enabled). This makes sure we're not
830    // regressing.
831    std::string method_name = PrettyMethod(method_idx, dex_file);
832    bool shouldCompile = method_name.find("$opt$") != std::string::npos;
833    DCHECK((method != nullptr) || !shouldCompile) << "Didn't compile " << method_name;
834  }
835
836  return method;
837}
838
839Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
840  return new OptimizingCompiler(driver);
841}
842
843bool IsCompilingWithCoreImage() {
844  const std::string& image = Runtime::Current()->GetImageLocation();
845  // TODO: This is under-approximating...
846  if (EndsWith(image, "core.art") || EndsWith(image, "core-optimizing.art")) {
847    return true;
848  }
849  return false;
850}
851
852bool OptimizingCompiler::JitCompile(Thread* self,
853                                    jit::JitCodeCache* code_cache,
854                                    ArtMethod* method,
855                                    bool osr) {
856  StackHandleScope<2> hs(self);
857  Handle<mirror::ClassLoader> class_loader(hs.NewHandle(
858      method->GetDeclaringClass()->GetClassLoader()));
859  Handle<mirror::DexCache> dex_cache(hs.NewHandle(method->GetDexCache()));
860  DCHECK(method->IsCompilable());
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    if (kArenaAllocatorCountAllocations) {
893      if (arena.BytesAllocated() > kArenaAllocatorMemoryReportThreshold) {
894        MemStats mem_stats(arena.GetMemStats());
895        LOG(INFO) << PrettyMethod(method_idx, *dex_file) << " " << Dumpable<MemStats>(mem_stats);
896      }
897    }
898  }
899
900  size_t stack_map_size = codegen->ComputeStackMapsSize();
901  uint8_t* stack_map_data = code_cache->ReserveData(self, stack_map_size, method);
902  if (stack_map_data == nullptr) {
903    return false;
904  }
905  MaybeRecordStat(MethodCompilationStat::kCompiled);
906  codegen->BuildStackMaps(MemoryRegion(stack_map_data, stack_map_size), *code_item);
907  const void* code = code_cache->CommitCode(
908      self,
909      method,
910      stack_map_data,
911      codegen->HasEmptyFrame() ? 0 : codegen->GetFrameSize(),
912      codegen->GetCoreSpillMask(),
913      codegen->GetFpuSpillMask(),
914      code_allocator.GetMemory().data(),
915      code_allocator.GetSize(),
916      osr);
917
918  if (code == nullptr) {
919    code_cache->ClearData(self, stack_map_data);
920    return false;
921  }
922
923  const CompilerOptions& compiler_options = GetCompilerDriver()->GetCompilerOptions();
924  if (compiler_options.GetGenerateDebugInfo()) {
925    const auto* method_header = reinterpret_cast<const OatQuickMethodHeader*>(code);
926    const uintptr_t code_address = reinterpret_cast<uintptr_t>(method_header->GetCode());
927    debug::MethodDebugInfo info = debug::MethodDebugInfo();
928    info.trampoline_name = nullptr;
929    info.dex_file = dex_file;
930    info.class_def_index = class_def_idx;
931    info.dex_method_index = method_idx;
932    info.access_flags = access_flags;
933    info.code_item = code_item;
934    info.isa = codegen->GetInstructionSet();
935    info.deduped = false;
936    info.is_native_debuggable = compiler_options.GetNativeDebuggable();
937    info.is_optimized = true;
938    info.is_code_address_text_relative = false;
939    info.code_address = code_address;
940    info.code_size = code_allocator.GetSize();
941    info.frame_size_in_bytes = method_header->GetFrameSizeInBytes();
942    info.code_info = stack_map_size == 0 ? nullptr : stack_map_data;
943    info.cfi = ArrayRef<const uint8_t>(*codegen->GetAssembler()->cfi().data());
944    std::vector<uint8_t> elf_file = debug::WriteDebugElfFileForMethods(
945        GetCompilerDriver()->GetInstructionSet(),
946        GetCompilerDriver()->GetInstructionSetFeatures(),
947        ArrayRef<const debug::MethodDebugInfo>(&info, 1));
948    CreateJITCodeEntryForAddress(code_address, std::move(elf_file));
949  }
950
951  Runtime::Current()->GetJit()->AddMemoryUsage(method, arena.BytesUsed());
952
953  return true;
954}
955
956}  // namespace art
957