blob: 705345b2797ed8c87c18a32950258d086fc6a223 [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 Geoffray827eedb2015-01-26 15:18:36 +000042#include "side_effects_analysis.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000043#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010044#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010045#include "ssa_liveness_analysis.h"
Calin Juravle10e244f2015-01-26 18:54:32 +000046#include "reference_type_propagation.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000047#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000048
49namespace art {
50
Nicolas Geoffray787c3072014-03-17 10:20:19 +000051/**
52 * Used by the code generator, to allocate the code in a vector.
53 */
54class CodeVectorAllocator FINAL : public CodeAllocator {
55 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010056 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000057
58 virtual uint8_t* Allocate(size_t size) {
59 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000060 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000061 return &memory_[0];
62 }
63
64 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000065 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000066
67 private:
68 std::vector<uint8_t> memory_;
69 size_t size_;
70
71 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
72};
73
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010074/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010075 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +000076 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010077 */
78static const char* kStringFilter = "";
79
Andreas Gampe53c913b2014-08-12 23:19:23 -070080class OptimizingCompiler FINAL : public Compiler {
81 public:
82 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010083 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070084
85 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
86 OVERRIDE;
87
88 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
89 uint32_t access_flags,
90 InvokeType invoke_type,
91 uint16_t class_def_idx,
92 uint32_t method_idx,
93 jobject class_loader,
94 const DexFile& dex_file) const OVERRIDE;
95
Andreas Gampe53c913b2014-08-12 23:19:23 -070096 CompiledMethod* JniCompile(uint32_t access_flags,
97 uint32_t method_idx,
98 const DexFile& dex_file) const OVERRIDE;
99
100 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
102
103 bool WriteElf(art::File* file,
104 OatWriter* oat_writer,
105 const std::vector<const art::DexFile*>& dex_files,
106 const std::string& android_root,
107 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
108
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000109 void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700110
David Brazdilee690a32014-12-01 17:04:16 +0000111 void Init() OVERRIDE;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700112
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000113 void UnInit() const OVERRIDE {}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700114
115 private:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100116 // Whether we should run any optimization or register allocation. If false, will
117 // just run the code generation after the graph was built.
118 const bool run_optimizations_;
Calin Juravle48c2b032014-12-09 18:11:36 +0000119
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000120 // Optimize and compile `graph`.
121 CompiledMethod* CompileOptimized(HGraph* graph,
122 CodeGenerator* codegen,
123 CompilerDriver* driver,
124 const DexCompilationUnit& dex_compilation_unit,
125 const HGraphVisualizer& visualizer) const;
126
127 // Just compile without doing optimizations.
128 CompiledMethod* CompileBaseline(CodeGenerator* codegen,
129 CompilerDriver* driver,
130 const DexCompilationUnit& dex_compilation_unit) const;
131
Calin Juravle48c2b032014-12-09 18:11:36 +0000132 mutable OptimizingCompilerStats compilation_stats_;
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100133
Andreas Gampe53c913b2014-08-12 23:19:23 -0700134 std::unique_ptr<std::ostream> visualizer_output_;
135
Andreas Gampe53c913b2014-08-12 23:19:23 -0700136 DISALLOW_COPY_AND_ASSIGN(OptimizingCompiler);
137};
138
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100139static const int kMaximumCompilationTimeBeforeWarning = 100; /* ms */
140
141OptimizingCompiler::OptimizingCompiler(CompilerDriver* driver)
142 : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
143 run_optimizations_(
144 driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
David Brazdilee690a32014-12-01 17:04:16 +0000145 compilation_stats_() {}
146
147void OptimizingCompiler::Init() {
148 // Enable C1visualizer output. Must be done in Init() because the compiler
149 // driver is not fully initialized when passed to the compiler's constructor.
150 CompilerDriver* driver = GetCompilerDriver();
David Brazdil866c0312015-01-13 21:21:31 +0000151 const std::string cfg_file_name = driver->GetDumpCfgFileName();
152 if (!cfg_file_name.empty()) {
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'.";
David Brazdil866c0312015-01-13 21:21:31 +0000156 visualizer_output_.reset(new std::ofstream(cfg_file_name));
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
Calin Juravle10e244f2015-01-26 18:54:32 +0000200static void RunOptimizations(HOptimization* optimizations[],
201 size_t length,
202 const HGraphVisualizer& visualizer) {
203 for (size_t i = 0; i < length; ++i) {
204 HOptimization* optimization = optimizations[i];
205 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false);
206 optimization->Run();
207 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true);
208 optimization->Check();
209 }
210}
211
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000212static void RunOptimizations(HGraph* graph,
213 CompilerDriver* driver,
214 OptimizingCompilerStats* stats,
215 const DexCompilationUnit& dex_compilation_unit,
216 const HGraphVisualizer& visualizer) {
217 SsaRedundantPhiElimination redundant_phi(graph);
218 SsaDeadPhiElimination dead_phi(graph);
219 HDeadCodeElimination dce(graph);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000220 HConstantFolding fold1(graph);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000221 InstructionSimplifier simplify1(graph);
222
223 HInliner inliner(graph, dex_compilation_unit, driver, stats);
224
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000225 HConstantFolding fold2(graph);
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000226 SideEffectsAnalysis side_effects(graph);
227 GVNOptimization gvn(graph, side_effects);
Mingyao Yangf384f882014-10-22 16:08:18 -0700228 BoundsCheckElimination bce(graph);
Calin Juravle10e244f2015-01-26 18:54:32 +0000229 ReferenceTypePropagation type_propagation(graph);
230 InstructionSimplifier simplify2(graph, "instruction_simplifier_after_types");
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000231
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800232 IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
233
Nicolas Geoffray31596742014-11-24 15:28:45 +0000234 HOptimization* optimizations[] = {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000235 &redundant_phi,
236 &dead_phi,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800237 &intrinsics,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000238 &dce,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000239 &fold1,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 &simplify1,
241 &inliner,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000242 &fold2,
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000243 &side_effects,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000244 &gvn,
Mingyao Yangf384f882014-10-22 16:08:18 -0700245 &bce,
Calin Juravle10e244f2015-01-26 18:54:32 +0000246 &type_propagation,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000247 &simplify2
Nicolas Geoffray31596742014-11-24 15:28:45 +0000248 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000249
Calin Juravle10e244f2015-01-26 18:54:32 +0000250 RunOptimizations(optimizations, arraysize(optimizations), visualizer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000251}
252
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000253// The stack map we generate must be 4-byte aligned on ARM. Since existing
254// maps are generated alongside these stack maps, we must also align them.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800255static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000256 size_t size = vector.size();
257 size_t aligned_size = RoundUp(size, 4);
258 for (; size < aligned_size; ++size) {
259 vector.push_back(0);
260 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800261 return ArrayRef<const uint8_t>(vector);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000262}
263
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000264
265CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
266 CodeGenerator* codegen,
267 CompilerDriver* compiler_driver,
268 const DexCompilationUnit& dex_compilation_unit,
269 const HGraphVisualizer& visualizer) const {
270 RunOptimizations(
271 graph, compiler_driver, &compilation_stats_, dex_compilation_unit, visualizer);
272
273 PrepareForRegisterAllocation(graph).Run();
274 SsaLivenessAnalysis liveness(*graph, codegen);
275 liveness.Analyze();
276 visualizer.DumpGraph(kLivenessPassName);
277
278 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
279 register_allocator.AllocateRegisters();
280 visualizer.DumpGraph(kRegisterAllocatorPassName);
281
282 CodeVectorAllocator allocator;
283 codegen->CompileOptimized(&allocator);
284
285 std::vector<uint8_t> stack_map;
286 codegen->BuildStackMaps(&stack_map);
287
288 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
289
290 return CompiledMethod::SwapAllocCompiledMethodStackMap(
291 compiler_driver,
292 codegen->GetInstructionSet(),
293 ArrayRef<const uint8_t>(allocator.GetMemory()),
294 codegen->GetFrameSize(),
295 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000296 codegen->GetFpuSpillMask(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000297 ArrayRef<const uint8_t>(stack_map));
298}
299
300
301CompiledMethod* OptimizingCompiler::CompileBaseline(
302 CodeGenerator* codegen,
303 CompilerDriver* compiler_driver,
304 const DexCompilationUnit& dex_compilation_unit) const {
305 CodeVectorAllocator allocator;
306 codegen->CompileBaseline(&allocator);
307
308 std::vector<uint8_t> mapping_table;
309 DefaultSrcMap src_mapping_table;
310 bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
311 codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
312 std::vector<uint8_t> vmap_table;
313 codegen->BuildVMapTable(&vmap_table);
314 std::vector<uint8_t> gc_map;
315 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
316
317 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
318 return CompiledMethod::SwapAllocCompiledMethod(compiler_driver,
319 codegen->GetInstructionSet(),
320 ArrayRef<const uint8_t>(allocator.GetMemory()),
321 codegen->GetFrameSize(),
322 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000323 codegen->GetFpuSpillMask(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000324 &src_mapping_table,
325 AlignVectorSize(mapping_table),
326 AlignVectorSize(vmap_table),
327 AlignVectorSize(gc_map),
328 ArrayRef<const uint8_t>());
329}
330
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000331CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
332 uint32_t access_flags,
333 InvokeType invoke_type,
334 uint16_t class_def_idx,
335 uint32_t method_idx,
336 jobject class_loader,
337 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700338 UNUSED(invoke_type);
Calin Juravle48c2b032014-12-09 18:11:36 +0000339 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000340 CompilerDriver* compiler_driver = GetCompilerDriver();
341 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100342 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
343 // overflow checks) assume thumb2.
344 if (instruction_set == kArm) {
345 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100346 }
347
348 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000349 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000350 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100351 return nullptr;
352 }
353
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000354 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000355 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000356 return nullptr;
357 }
358
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000359 DexCompilationUnit dex_compilation_unit(
360 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700361 class_def_idx, method_idx, access_flags,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000362 compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000363
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000364 std::string method_name = PrettyMethod(method_idx, dex_file);
365
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000366 // For testing purposes, we put a special marker on method names that should be compiled
367 // with this compiler. This makes sure we're not regressing.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000368 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
369 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000370
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000371 ArenaPool pool;
372 ArenaAllocator arena(&pool);
Calin Juravle48c2b032014-12-09 18:11:36 +0000373 HGraphBuilder builder(&arena,
374 &dex_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000375 &dex_compilation_unit,
376 &dex_file,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000377 compiler_driver,
Calin Juravle48c2b032014-12-09 18:11:36 +0000378 &compilation_stats_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100379
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000380 VLOG(compiler) << "Building " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000381 HGraph* graph = builder.BuildGraph(*code_item);
382 if (graph == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800383 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000384 return nullptr;
385 }
386
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000387 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000388 CodeGenerator::Create(graph,
389 instruction_set,
390 *compiler_driver->GetInstructionSetFeatures(),
391 compiler_driver->GetCompilerOptions()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000392 if (codegen.get() == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800393 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000394 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000395 return nullptr;
396 }
397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100398 HGraphVisualizer visualizer(
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000399 visualizer_output_.get(), graph, kStringFilter, *codegen.get(), method_name.c_str());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100400 visualizer.DumpGraph("builder");
401
Calin Juravle48c2b032014-12-09 18:11:36 +0000402 bool can_optimize = CanOptimize(*code_item);
403 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000404 CompiledMethod* result = nullptr;
Calin Juravle48c2b032014-12-09 18:11:36 +0000405 if (run_optimizations_ && can_optimize && can_allocate_registers) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000406 VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000407 if (!graph->TryBuildingSsa()) {
408 LOG(INFO) << "Skipping compilation of "
409 << PrettyMethod(method_idx, dex_file)
410 << ": it contains a non natural loop";
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000411 // We could not transform the graph to SSA, bailout.
Calin Juravle48c2b032014-12-09 18:11:36 +0000412 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000413 } else {
414 result = CompileOptimized(graph, codegen.get(), compiler_driver, dex_compilation_unit, visualizer);
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000415 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100416 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
417 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800418 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100419 } else {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000420 VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000421
422 if (!run_optimizations_) {
423 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
424 } else if (!can_optimize) {
425 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
426 } else if (!can_allocate_registers) {
427 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
428 }
429
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000430 result = CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100431 }
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000432 return result;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000433}
434
Andreas Gampe53c913b2014-08-12 23:19:23 -0700435Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
436 return new OptimizingCompiler(driver);
437}
438
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000439} // namespace art