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