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