blob: 605637300f277455cd8f0cf5d8e54b0a0c4bc733 [file] [log] [blame]
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001/*
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
Andreas Gampe53c913b2014-08-12 23:19:23 -070017#include "optimizing_compiler.h"
18
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010019#include <fstream>
Nicolas Geoffray787c3072014-03-17 10:20:19 +000020#include <stdint.h>
21
Mingyao Yangf384f882014-10-22 16:08:18 -070022#include "bounds_check_elimination.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000023#include "builder.h"
24#include "code_generator.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070025#include "compiler.h"
Roland Levillain75be2832014-10-17 17:02:00 +010026#include "constant_folding.h"
27#include "dead_code_elimination.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080028#include "dex/quick/dex_file_to_method_inliner_map.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000029#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000030#include "driver/dex_compilation_unit.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000031#include "elf_writer_quick.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010032#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010033#include "gvn.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000034#include "inliner.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010035#include "instruction_simplifier.h"
Andreas Gampe71fb52f2014-12-29 17:43:08 -080036#include "intrinsics.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000037#include "jni/quick/jni_compiler.h"
38#include "mirror/art_method-inl.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000039#include "nodes.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010040#include "prepare_for_register_allocation.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010041#include "register_allocator.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000042#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010043#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010044#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000045#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000046
47namespace art {
48
Nicolas Geoffray787c3072014-03-17 10:20:19 +000049/**
50 * Used by the code generator, to allocate the code in a vector.
51 */
52class CodeVectorAllocator FINAL : public CodeAllocator {
53 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010054 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000055
56 virtual uint8_t* Allocate(size_t size) {
57 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000058 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000059 return &memory_[0];
60 }
61
62 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000063 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000064
65 private:
66 std::vector<uint8_t> memory_;
67 size_t size_;
68
69 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
70};
71
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010072/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010073 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +000074 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010075 */
76static const char* kStringFilter = "";
77
Andreas Gampe53c913b2014-08-12 23:19:23 -070078class OptimizingCompiler FINAL : public Compiler {
79 public:
80 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010081 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070082
83 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
84 OVERRIDE;
85
86 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
87 uint32_t access_flags,
88 InvokeType invoke_type,
89 uint16_t class_def_idx,
90 uint32_t method_idx,
91 jobject class_loader,
92 const DexFile& dex_file) const OVERRIDE;
93
Andreas Gampe53c913b2014-08-12 23:19:23 -070094 CompiledMethod* JniCompile(uint32_t access_flags,
95 uint32_t method_idx,
96 const DexFile& dex_file) const OVERRIDE;
97
98 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
99 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
100
101 bool WriteElf(art::File* file,
102 OatWriter* oat_writer,
103 const std::vector<const art::DexFile*>& dex_files,
104 const std::string& android_root,
105 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
106
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000107 Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED,
108 void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE {
109 return nullptr;
110 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700111
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000112 void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700113
David Brazdilee690a32014-12-01 17:04:16 +0000114 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700115
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000116 void UnInit() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700117
118 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100119 // Whether we should run any optimization or register allocation. If false, will
120 // just run the code generation after the graph was built.
121 const bool run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000122
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000123 // Optimize and compile `graph`.
124 CompiledMethod* CompileOptimized(HGraph* graph,
125 CodeGenerator* codegen,
126 CompilerDriver* driver,
127 const DexCompilationUnit& dex_compilation_unit,
128 const HGraphVisualizer& visualizer) const;
129
130 // Just compile without doing optimizations.
131 CompiledMethod* CompileBaseline(CodeGenerator* codegen,
132 CompilerDriver* driver,
133 const DexCompilationUnit& dex_compilation_unit) const;
134
Calin Juravle48c2b032014-12-09 18:11:36 +0000135 mutable OptimizingCompilerStats compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100136
Andreas Gampe53c913b2014-08-12 23:19:23 -0700137 std::unique_ptr<std::ostream> visualizer_output_;
138
Andreas Gampe53c913b2014-08-12 23:19:23 -0700139 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
140};
141
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100142static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
143
144OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
145 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
146 run_optimizations_(
147 driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
David Brazdilee690a32014-12-01 17:04:16 +0000148 compilation_stats_() {}
149
150void OptimizingCompiler::Init() {
151 // Enable C1visualizer output. Must be done in Init() because the compiler
152 // driver is not fully initialized when passed to the compiler's constructor.
153 CompilerDriver* driver = GetCompilerDriver();
David Brazdil866c0312015-01-13 21:21:31 +0000154 const std::string cfg_file_name = driver->GetDumpCfgFileName();
155 if (!cfg_file_name.empty()) {
David Brazdilee690a32014-12-01 17:04:16 +0000156 CHECK_EQ(driver->GetThreadCount(), 1U)
157 << "Graph visualizer requires the compiler to run single-threaded. "
158 << "Invoke the compiler with '-j1'.";
David Brazdil866c0312015-01-13 21:21:31 +0000159 visualizer_output_.reset(new std::ofstream(cfg_file_name));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100160 }
161}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000162
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100163OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle48c2b032014-12-09 18:11:36 +0000164 compilation_stats_.Log();
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100165}
166
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000167bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
168 const DexFile& dex_file ATTRIBUTE_UNUSED,
169 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
170 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700171}
172
173CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
174 uint32_t method_idx,
175 const DexFile& dex_file) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000176 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700177}
178
179uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Nicolas Geoffray4179cc12014-11-19 08:35:17 +0000180 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
181 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700182}
183
184bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
185 const std::vector<const art::DexFile*>& dex_files,
186 const std::string& android_root, bool is_host) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000187 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
188 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700189}
190
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000191static bool IsInstructionSetSupported(InstructionSet instruction_set) {
192 return instruction_set == kArm64
193 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
194 || instruction_set == kX86
195 || instruction_set == kX86_64;
196}
197
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000198static bool CanOptimize(const DexFile::CodeItem& code_item) {
199 // TODO: We currently cannot optimize methods with try/catch.
200 return code_item.tries_size_ == 0;
201}
202
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000203static void RunOptimizations(HGraph* graph,
204 CompilerDriver* driver,
205 OptimizingCompilerStats* stats,
206 const DexCompilationUnit& dex_compilation_unit,
207 const HGraphVisualizer& visualizer) {
208 SsaRedundantPhiElimination redundant_phi(graph);
209 SsaDeadPhiElimination dead_phi(graph);
210 HDeadCodeElimination dce(graph);
211 HConstantFolding fold(graph);
212 InstructionSimplifier simplify1(graph);
213
214 HInliner inliner(graph, dex_compilation_unit, driver, stats);
215
216 GVNOptimization gvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700217 BoundsCheckElimination bce(graph);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 InstructionSimplifier simplify2(graph);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000219
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800220 IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
221
Nicolas Geoffray31596742014-11-24 15:28:45 +0000222 HOptimization* optimizations[] = {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000223 &redundant_phi,
224 &dead_phi,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800225 &intrinsics,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 &dce,
227 &fold,
228 &simplify1,
229 &inliner,
230 &gvn,
Mingyao Yangf384f882014-10-22 16:08:18 -0700231 &bce,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000232 &simplify2
Nicolas Geoffray31596742014-11-24 15:28:45 +0000233 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000234
235 for (size_t i = 0; i < arraysize(optimizations); ++i) {
236 HOptimization* optimization = optimizations[i];
David Brazdilee690a32014-12-01 17:04:16 +0000237 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000238 optimization->Run();
David Brazdilee690a32014-12-01 17:04:16 +0000239 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000240 optimization->Check();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000241 }
242}
243
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000244// The stack map we generate must be 4-byte aligned on ARM. Since existing
245// maps are generated alongside these stack maps, we must also align them.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800246static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000247 size_t size = vector.size();
248 size_t aligned_size = RoundUp(size, 4);
249 for (; size < aligned_size; ++size) {
250 vector.push_back(0);
251 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800252 return ArrayRef<const uint8_t>(vector);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000253}
254
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000255
256CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
257 CodeGenerator* codegen,
258 CompilerDriver* compiler_driver,
259 const DexCompilationUnit& dex_compilation_unit,
260 const HGraphVisualizer& visualizer) const {
261 RunOptimizations(
262 graph, compiler_driver, &compilation_stats_, dex_compilation_unit, visualizer);
263
264 PrepareForRegisterAllocation(graph).Run();
265 SsaLivenessAnalysis liveness(*graph, codegen);
266 liveness.Analyze();
267 visualizer.DumpGraph(kLivenessPassName);
268
269 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
270 register_allocator.AllocateRegisters();
271 visualizer.DumpGraph(kRegisterAllocatorPassName);
272
273 CodeVectorAllocator allocator;
274 codegen->CompileOptimized(&allocator);
275
276 std::vector<uint8_t> stack_map;
277 codegen->BuildStackMaps(&stack_map);
278
279 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
280
281 return CompiledMethod::SwapAllocCompiledMethodStackMap(
282 compiler_driver,
283 codegen->GetInstructionSet(),
284 ArrayRef<const uint8_t>(allocator.GetMemory()),
285 codegen->GetFrameSize(),
286 codegen->GetCoreSpillMask(),
287 0, /* FPR spill mask, unused */
288 ArrayRef<const uint8_t>(stack_map));
289}
290
291
292CompiledMethod* OptimizingCompiler::CompileBaseline(
293 CodeGenerator* codegen,
294 CompilerDriver* compiler_driver,
295 const DexCompilationUnit& dex_compilation_unit) const {
296 CodeVectorAllocator allocator;
297 codegen->CompileBaseline(&allocator);
298
299 std::vector<uint8_t> mapping_table;
300 DefaultSrcMap src_mapping_table;
301 bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
302 codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
303 std::vector<uint8_t> vmap_table;
304 codegen->BuildVMapTable(&vmap_table);
305 std::vector<uint8_t> gc_map;
306 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
307
308 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
309 return CompiledMethod::SwapAllocCompiledMethod(compiler_driver,
310 codegen->GetInstructionSet(),
311 ArrayRef<const uint8_t>(allocator.GetMemory()),
312 codegen->GetFrameSize(),
313 codegen->GetCoreSpillMask(),
314 0, /* FPR spill mask, unused */
315 &src_mapping_table,
316 AlignVectorSize(mapping_table),
317 AlignVectorSize(vmap_table),
318 AlignVectorSize(gc_map),
319 ArrayRef<const uint8_t>());
320}
321
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000322CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
323 uint32_t access_flags,
324 InvokeType invoke_type,
325 uint16_t class_def_idx,
326 uint32_t method_idx,
327 jobject class_loader,
328 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700329 UNUSED(invoke_type);
Calin Juravle48c2b032014-12-09 18:11:36 +0000330 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100331 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100332 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
333 // overflow checks) assume thumb2.
334 if (instruction_set == kArm) {
335 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100336 }
337
338 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000339 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000340 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100341 return nullptr;
342 }
343
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000344 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000345 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000346 return nullptr;
347 }
348
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000349 DexCompilationUnit dex_compilation_unit(
350 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700351 class_def_idx, method_idx, access_flags,
352 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000353
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000354 std::string method_name = PrettyMethod(method_idx, dex_file);
355
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000356 // For testing purposes, we put a special marker on method names that should be compiled
357 // with this compiler. This makes sure we're not regressing.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000358 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
359 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000360
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000361 ArenaPool pool;
362 ArenaAllocator arena(&pool);
Calin Juravle48c2b032014-12-09 18:11:36 +0000363 HGraphBuilder builder(&arena,
364 &dex_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000365 &dex_compilation_unit,
366 &dex_file,
367 GetCompilerDriver(),
Calin Juravle48c2b032014-12-09 18:11:36 +0000368 &compilation_stats_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100369
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000370 VLOG(compiler) << "Building " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 HGraph* graph = builder.BuildGraph(*code_item);
372 if (graph == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800373 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000374 return nullptr;
375 }
376
Calin Juravle34166012014-12-19 17:22:29 +0000377 CompilerDriver* compiler_driver = GetCompilerDriver();
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000378 std::unique_ptr<CodeGenerator> codegen(
379 CodeGenerator::Create(graph, instruction_set, *compiler_driver->GetInstructionSetFeatures()));
380 if (codegen.get() == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800381 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000382 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000383 return nullptr;
384 }
385
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100386 HGraphVisualizer visualizer(
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000387 visualizer_output_.get(), graph, kStringFilter, *codegen.get(), method_name.c_str());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100388 visualizer.DumpGraph("builder");
389
Calin Juravle48c2b032014-12-09 18:11:36 +0000390 bool can_optimize = CanOptimize(*code_item);
391 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000392 CompiledMethod* result = nullptr;
Calin Juravle48c2b032014-12-09 18:11:36 +0000393 if (run_optimizations_ && can_optimize && can_allocate_registers) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000394 VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000395 if (!graph->TryBuildingSsa()) {
396 LOG(INFO) << "Skipping compilation of "
397 << PrettyMethod(method_idx, dex_file)
398 << ": it contains a non natural loop";
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000399 // We could not transform the graph to SSA, bailout.
Calin Juravle48c2b032014-12-09 18:11:36 +0000400 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000401 } else {
402 result = CompileOptimized(graph, codegen.get(), compiler_driver, dex_compilation_unit, visualizer);
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000403 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100404 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
405 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800406 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100407 } else {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000408 VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000409
410 if (!run_optimizations_) {
411 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
412 } else if (!can_optimize) {
413 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
414 } else if (!can_allocate_registers) {
415 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
416 }
417
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000418 result = CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100419 }
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000420 return result;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000421}
422
Andreas Gampe53c913b2014-08-12 23:19:23 -0700423Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
424 return new OptimizingCompiler(driver);
425}
426
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000427} // namespace art