blob: 692d452f542d6222d61c24e318686e162cc65e27 [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"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000028#include "driver/compiler_driver.h"
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000029#include "driver/dex_compilation_unit.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000030#include "elf_writer_quick.h"
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010031#include "graph_visualizer.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010032#include "gvn.h"
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000033#include "inliner.h"
Nicolas Geoffray3c049742014-09-24 18:10:46 +010034#include "instruction_simplifier.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000035#include "jni/quick/jni_compiler.h"
36#include "mirror/art_method-inl.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000037#include "nodes.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010038#include "prepare_for_register_allocation.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010039#include "register_allocator.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000040#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010041#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010042#include "ssa_liveness_analysis.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000043#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000044
45namespace art {
46
Nicolas Geoffray787c3072014-03-17 10:20:19 +000047/**
48 * Used by the code generator, to allocate the code in a vector.
49 */
50class CodeVectorAllocator FINAL : public CodeAllocator {
51 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010052 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000053
54 virtual uint8_t* Allocate(size_t size) {
55 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000056 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000057 return &memory_[0];
58 }
59
60 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000061 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000062
63 private:
64 std::vector<uint8_t> memory_;
65 size_t size_;
66
67 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
68};
69
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010070/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010071 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +000072 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010073 */
74static const char* kStringFilter = "";
75
Andreas Gampe53c913b2014-08-12 23:19:23 -070076class OptimizingCompiler FINAL : public Compiler {
77 public:
78 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010079 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070080
81 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
82 OVERRIDE;
83
84 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
85 uint32_t access_flags,
86 InvokeType invoke_type,
87 uint16_t class_def_idx,
88 uint32_t method_idx,
89 jobject class_loader,
90 const DexFile& dex_file) const OVERRIDE;
91
Andreas Gampe53c913b2014-08-12 23:19:23 -070092 CompiledMethod* JniCompile(uint32_t access_flags,
93 uint32_t method_idx,
94 const DexFile& dex_file) const OVERRIDE;
95
96 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
97 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
98
99 bool WriteElf(art::File* file,
100 OatWriter* oat_writer,
101 const std::vector<const art::DexFile*>& dex_files,
102 const std::string& android_root,
103 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
104
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000105 Backend* GetCodeGenerator(CompilationUnit* cu ATTRIBUTE_UNUSED,
106 void* compilation_unit ATTRIBUTE_UNUSED) const OVERRIDE {
107 return nullptr;
108 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700109
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000110 void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700111
David Brazdilee690a32014-12-01 17:04:16 +0000112 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700113
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000114 void UnInit() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700115
116 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100117 // Whether we should run any optimization or register allocation. If false, will
118 // just run the code generation after the graph was built.
119 const bool run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000120
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000121 // Optimize and compile `graph`.
122 CompiledMethod* CompileOptimized(HGraph* graph,
123 CodeGenerator* codegen,
124 CompilerDriver* driver,
125 const DexCompilationUnit& dex_compilation_unit,
126 const HGraphVisualizer& visualizer) const;
127
128 // Just compile without doing optimizations.
129 CompiledMethod* CompileBaseline(CodeGenerator* codegen,
130 CompilerDriver* driver,
131 const DexCompilationUnit& dex_compilation_unit) const;
132
Calin Juravle48c2b032014-12-09 18:11:36 +0000133 mutable OptimizingCompilerStats compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100134
Andreas Gampe53c913b2014-08-12 23:19:23 -0700135 std::unique_ptr<std::ostream> visualizer_output_;
136
Andreas Gampe53c913b2014-08-12 23:19:23 -0700137 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
138};
139
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100140static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
141
142OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
143 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
144 run_optimizations_(
145 driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
David Brazdilee690a32014-12-01 17:04:16 +0000146 compilation_stats_() {}
147
148void OptimizingCompiler::Init() {
149 // Enable C1visualizer output. Must be done in Init() because the compiler
150 // driver is not fully initialized when passed to the compiler's constructor.
151 CompilerDriver* driver = GetCompilerDriver();
Andreas Gampe6e732722015-01-13 19:11:14 +0000152 if (driver->GetDumpPasses()) {
David Brazdilee690a32014-12-01 17:04:16 +0000153 CHECK_EQ(driver->GetThreadCount(), 1U)
154 << "Graph visualizer requires the compiler to run single-threaded. "
155 << "Invoke the compiler with '-j1'.";
Andreas Gampe6e732722015-01-13 19:11:14 +0000156 visualizer_output_.reset(new std::ofstream("art.cfg"));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100157 }
158}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000159
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100160OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle48c2b032014-12-09 18:11:36 +0000161 compilation_stats_.Log();
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100162}
163
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000164bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
165 const DexFile& dex_file ATTRIBUTE_UNUSED,
166 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
167 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700168}
169
170CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
171 uint32_t method_idx,
172 const DexFile& dex_file) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000173 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700174}
175
176uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Nicolas Geoffray4179cc12014-11-19 08:35:17 +0000177 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
178 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700179}
180
181bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
182 const std::vector<const art::DexFile*>& dex_files,
183 const std::string& android_root, bool is_host) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000184 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
185 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700186}
187
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000188static bool IsInstructionSetSupported(InstructionSet instruction_set) {
189 return instruction_set == kArm64
190 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
191 || instruction_set == kX86
192 || instruction_set == kX86_64;
193}
194
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000195static bool CanOptimize(const DexFile::CodeItem& code_item) {
196 // TODO: We currently cannot optimize methods with try/catch.
197 return code_item.tries_size_ == 0;
198}
199
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000200static void RunOptimizations(HGraph* graph,
201 CompilerDriver* driver,
202 OptimizingCompilerStats* stats,
203 const DexCompilationUnit& dex_compilation_unit,
204 const HGraphVisualizer& visualizer) {
205 SsaRedundantPhiElimination redundant_phi(graph);
206 SsaDeadPhiElimination dead_phi(graph);
207 HDeadCodeElimination dce(graph);
208 HConstantFolding fold(graph);
209 InstructionSimplifier simplify1(graph);
210
211 HInliner inliner(graph, dex_compilation_unit, driver, stats);
212
213 GVNOptimization gvn(graph);
Mingyao Yangf384f882014-10-22 16:08:18 -0700214 BoundsCheckElimination bce(graph);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000215 InstructionSimplifier simplify2(graph);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000216
Nicolas Geoffray31596742014-11-24 15:28:45 +0000217 HOptimization* optimizations[] = {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000218 &redundant_phi,
219 &dead_phi,
220 &dce,
221 &fold,
222 &simplify1,
223 &inliner,
224 &gvn,
Mingyao Yangf384f882014-10-22 16:08:18 -0700225 &bce,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000226 &simplify2
Nicolas Geoffray31596742014-11-24 15:28:45 +0000227 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000228
229 for (size_t i = 0; i < arraysize(optimizations); ++i) {
230 HOptimization* optimization = optimizations[i];
David Brazdilee690a32014-12-01 17:04:16 +0000231 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000232 optimization->Run();
David Brazdilee690a32014-12-01 17:04:16 +0000233 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true);
Nicolas Geoffray31596742014-11-24 15:28:45 +0000234 optimization->Check();
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000235 }
236}
237
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000238// The stack map we generate must be 4-byte aligned on ARM. Since existing
239// maps are generated alongside these stack maps, we must also align them.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800240static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000241 size_t size = vector.size();
242 size_t aligned_size = RoundUp(size, 4);
243 for (; size < aligned_size; ++size) {
244 vector.push_back(0);
245 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800246 return ArrayRef<const uint8_t>(vector);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000247}
248
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000249
250CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
251 CodeGenerator* codegen,
252 CompilerDriver* compiler_driver,
253 const DexCompilationUnit& dex_compilation_unit,
254 const HGraphVisualizer& visualizer) const {
255 RunOptimizations(
256 graph, compiler_driver, &compilation_stats_, dex_compilation_unit, visualizer);
257
258 PrepareForRegisterAllocation(graph).Run();
259 SsaLivenessAnalysis liveness(*graph, codegen);
260 liveness.Analyze();
261 visualizer.DumpGraph(kLivenessPassName);
262
263 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
264 register_allocator.AllocateRegisters();
265 visualizer.DumpGraph(kRegisterAllocatorPassName);
266
267 CodeVectorAllocator allocator;
268 codegen->CompileOptimized(&allocator);
269
270 std::vector<uint8_t> stack_map;
271 codegen->BuildStackMaps(&stack_map);
272
273 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
274
275 return CompiledMethod::SwapAllocCompiledMethodStackMap(
276 compiler_driver,
277 codegen->GetInstructionSet(),
278 ArrayRef<const uint8_t>(allocator.GetMemory()),
279 codegen->GetFrameSize(),
280 codegen->GetCoreSpillMask(),
281 0, /* FPR spill mask, unused */
282 ArrayRef<const uint8_t>(stack_map));
283}
284
285
286CompiledMethod* OptimizingCompiler::CompileBaseline(
287 CodeGenerator* codegen,
288 CompilerDriver* compiler_driver,
289 const DexCompilationUnit& dex_compilation_unit) const {
290 CodeVectorAllocator allocator;
291 codegen->CompileBaseline(&allocator);
292
293 std::vector<uint8_t> mapping_table;
294 DefaultSrcMap src_mapping_table;
295 bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
296 codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
297 std::vector<uint8_t> vmap_table;
298 codegen->BuildVMapTable(&vmap_table);
299 std::vector<uint8_t> gc_map;
300 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
301
302 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
303 return CompiledMethod::SwapAllocCompiledMethod(compiler_driver,
304 codegen->GetInstructionSet(),
305 ArrayRef<const uint8_t>(allocator.GetMemory()),
306 codegen->GetFrameSize(),
307 codegen->GetCoreSpillMask(),
308 0, /* FPR spill mask, unused */
309 &src_mapping_table,
310 AlignVectorSize(mapping_table),
311 AlignVectorSize(vmap_table),
312 AlignVectorSize(gc_map),
313 ArrayRef<const uint8_t>());
314}
315
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000316CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
317 uint32_t access_flags,
318 InvokeType invoke_type,
319 uint16_t class_def_idx,
320 uint32_t method_idx,
321 jobject class_loader,
322 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700323 UNUSED(invoke_type);
Calin Juravle48c2b032014-12-09 18:11:36 +0000324 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100325 InstructionSet instruction_set = GetCompilerDriver()->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100326 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
327 // overflow checks) assume thumb2.
328 if (instruction_set == kArm) {
329 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100330 }
331
332 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000333 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000334 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100335 return nullptr;
336 }
337
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000338 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000339 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000340 return nullptr;
341 }
342
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000343 DexCompilationUnit dex_compilation_unit(
344 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700345 class_def_idx, method_idx, access_flags,
346 GetCompilerDriver()->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000347
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000348 std::string method_name = PrettyMethod(method_idx, dex_file);
349
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000350 // For testing purposes, we put a special marker on method names that should be compiled
351 // with this compiler. This makes sure we're not regressing.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000352 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
353 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000354
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000355 ArenaPool pool;
356 ArenaAllocator arena(&pool);
Calin Juravle48c2b032014-12-09 18:11:36 +0000357 HGraphBuilder builder(&arena,
358 &dex_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000359 &dex_compilation_unit,
360 &dex_file,
361 GetCompilerDriver(),
Calin Juravle48c2b032014-12-09 18:11:36 +0000362 &compilation_stats_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100363
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000364 VLOG(compiler) << "Building " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000365 HGraph* graph = builder.BuildGraph(*code_item);
366 if (graph == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800367 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000368 return nullptr;
369 }
370
Calin Juravle34166012014-12-19 17:22:29 +0000371 CompilerDriver* compiler_driver = GetCompilerDriver();
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000372 std::unique_ptr<CodeGenerator> codegen(
373 CodeGenerator::Create(graph, instruction_set, *compiler_driver->GetInstructionSetFeatures()));
374 if (codegen.get() == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800375 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000376 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000377 return nullptr;
378 }
379
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100380 HGraphVisualizer visualizer(
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000381 visualizer_output_.get(), graph, kStringFilter, *codegen.get(), method_name.c_str());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100382 visualizer.DumpGraph("builder");
383
Calin Juravle48c2b032014-12-09 18:11:36 +0000384 bool can_optimize = CanOptimize(*code_item);
385 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000386 CompiledMethod* result = nullptr;
Calin Juravle48c2b032014-12-09 18:11:36 +0000387 if (run_optimizations_ && can_optimize && can_allocate_registers) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000388 VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000389 if (!graph->TryBuildingSsa()) {
390 LOG(INFO) << "Skipping compilation of "
391 << PrettyMethod(method_idx, dex_file)
392 << ": it contains a non natural loop";
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000393 // We could not transform the graph to SSA, bailout.
Calin Juravle48c2b032014-12-09 18:11:36 +0000394 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000395 } else {
396 result = CompileOptimized(graph, codegen.get(), compiler_driver, dex_compilation_unit, visualizer);
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000397 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100398 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
399 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800400 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100401 } else {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000402 VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000403
404 if (!run_optimizations_) {
405 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
406 } else if (!can_optimize) {
407 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
408 } else if (!can_allocate_registers) {
409 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
410 }
411
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000412 result = CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100413 }
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000414 return result;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000415}
416
Andreas Gampe53c913b2014-08-12 23:19:23 -0700417Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
418 return new OptimizingCompiler(driver);
419}
420
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000421} // namespace art