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