blob: 50d7924186289c3ba04280bed809a9f554ae2414 [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 Geoffray82091da2015-01-26 10:02:45 +000037#include "licm.h"
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +000038#include "jni/quick/jni_compiler.h"
39#include "mirror/art_method-inl.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000040#include "nodes.h"
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +010041#include "prepare_for_register_allocation.h"
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010042#include "register_allocator.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000043#include "side_effects_analysis.h"
Nicolas Geoffray31596742014-11-24 15:28:45 +000044#include "ssa_builder.h"
Nicolas Geoffray7dc206a2014-07-11 09:49:49 +010045#include "ssa_phi_elimination.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010046#include "ssa_liveness_analysis.h"
Calin Juravle10e244f2015-01-26 18:54:32 +000047#include "reference_type_propagation.h"
Nicolas Geoffray787c3072014-03-17 10:20:19 +000048#include "utils/arena_allocator.h"
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000049
50namespace art {
51
Nicolas Geoffray787c3072014-03-17 10:20:19 +000052/**
53 * Used by the code generator, to allocate the code in a vector.
54 */
55class CodeVectorAllocator FINAL : public CodeAllocator {
56 public:
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010057 CodeVectorAllocator() {}
Nicolas Geoffray787c3072014-03-17 10:20:19 +000058
59 virtual uint8_t* Allocate(size_t size) {
60 size_ = size;
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000061 memory_.resize(size);
Nicolas Geoffray787c3072014-03-17 10:20:19 +000062 return &memory_[0];
63 }
64
65 size_t GetSize() const { return size_; }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000066 const std::vector<uint8_t>& GetMemory() const { return memory_; }
Nicolas Geoffray787c3072014-03-17 10:20:19 +000067
68 private:
69 std::vector<uint8_t> memory_;
70 size_t size_;
71
72 DISALLOW_COPY_AND_ASSIGN(CodeVectorAllocator);
73};
74
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010075/**
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010076 * Filter to apply to the visualizer. Methods whose name contain that filter will
David Brazdilee690a32014-12-01 17:04:16 +000077 * be dumped.
Nicolas Geoffrayf635e632014-05-14 09:43:38 +010078 */
79static const char* kStringFilter = "";
80
Andreas Gampe53c913b2014-08-12 23:19:23 -070081class OptimizingCompiler FINAL : public Compiler {
82 public:
83 explicit OptimizingCompiler(CompilerDriver* driver);
Nicolas Geoffray88157ef2014-09-12 10:29:53 +010084 ~OptimizingCompiler();
Andreas Gampe53c913b2014-08-12 23:19:23 -070085
86 bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file, CompilationUnit* cu) const
87 OVERRIDE;
88
89 CompiledMethod* Compile(const DexFile::CodeItem* code_item,
90 uint32_t access_flags,
91 InvokeType invoke_type,
92 uint16_t class_def_idx,
93 uint32_t method_idx,
94 jobject class_loader,
95 const DexFile& dex_file) const OVERRIDE;
96
Andreas Gampe53c913b2014-08-12 23:19:23 -070097 CompiledMethod* JniCompile(uint32_t access_flags,
98 uint32_t method_idx,
99 const DexFile& dex_file) const OVERRIDE;
100
101 uintptr_t GetEntryPointOf(mirror::ArtMethod* method) const OVERRIDE
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
103
104 bool WriteElf(art::File* file,
105 OatWriter* oat_writer,
106 const std::vector<const art::DexFile*>& dex_files,
107 const std::string& android_root,
108 bool is_host) const OVERRIDE SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
109
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();
David Brazdil866c0312015-01-13 21:21:31 +0000152 const std::string cfg_file_name = driver->GetDumpCfgFileName();
153 if (!cfg_file_name.empty()) {
David Brazdilee690a32014-12-01 17:04:16 +0000154 CHECK_EQ(driver->GetThreadCount(), 1U)
155 << "Graph visualizer requires the compiler to run single-threaded. "
156 << "Invoke the compiler with '-j1'.";
David Brazdil866c0312015-01-13 21:21:31 +0000157 visualizer_output_.reset(new std::ofstream(cfg_file_name));
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100158 }
159}
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000160
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100161OptimizingCompiler::~OptimizingCompiler() {
Calin Juravle48c2b032014-12-09 18:11:36 +0000162 compilation_stats_.Log();
Nicolas Geoffray88157ef2014-09-12 10:29:53 +0100163}
164
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000165bool OptimizingCompiler::CanCompileMethod(uint32_t method_idx ATTRIBUTE_UNUSED,
166 const DexFile& dex_file ATTRIBUTE_UNUSED,
167 CompilationUnit* cu ATTRIBUTE_UNUSED) const {
168 return true;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700169}
170
171CompiledMethod* OptimizingCompiler::JniCompile(uint32_t access_flags,
172 uint32_t method_idx,
173 const DexFile& dex_file) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000174 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700175}
176
177uintptr_t OptimizingCompiler::GetEntryPointOf(mirror::ArtMethod* method) const {
Nicolas Geoffray4179cc12014-11-19 08:35:17 +0000178 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
179 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700180}
181
182bool OptimizingCompiler::WriteElf(art::File* file, OatWriter* oat_writer,
183 const std::vector<const art::DexFile*>& dex_files,
184 const std::string& android_root, bool is_host) const {
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000185 return art::ElfWriterQuick32::Create(file, oat_writer, dex_files, android_root, is_host,
186 *GetCompilerDriver());
Andreas Gampe53c913b2014-08-12 23:19:23 -0700187}
188
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000189static bool IsInstructionSetSupported(InstructionSet instruction_set) {
190 return instruction_set == kArm64
191 || (instruction_set == kThumb2 && !kArm32QuickCodeUseSoftFloat)
192 || instruction_set == kX86
193 || instruction_set == kX86_64;
194}
195
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +0000196static bool CanOptimize(const DexFile::CodeItem& code_item) {
197 // TODO: We currently cannot optimize methods with try/catch.
198 return code_item.tries_size_ == 0;
199}
200
Calin Juravle10e244f2015-01-26 18:54:32 +0000201static void RunOptimizations(HOptimization* optimizations[],
202 size_t length,
203 const HGraphVisualizer& visualizer) {
204 for (size_t i = 0; i < length; ++i) {
205 HOptimization* optimization = optimizations[i];
206 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false);
207 optimization->Run();
208 visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true);
209 optimization->Check();
210 }
211}
212
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000213static void RunOptimizations(HGraph* graph,
214 CompilerDriver* driver,
215 OptimizingCompilerStats* stats,
216 const DexCompilationUnit& dex_compilation_unit,
217 const HGraphVisualizer& visualizer) {
218 SsaRedundantPhiElimination redundant_phi(graph);
219 SsaDeadPhiElimination dead_phi(graph);
220 HDeadCodeElimination dce(graph);
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000221 HConstantFolding fold1(graph);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000222 InstructionSimplifier simplify1(graph);
223
224 HInliner inliner(graph, dex_compilation_unit, driver, stats);
225
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000226 HConstantFolding fold2(graph);
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000227 SideEffectsAnalysis side_effects(graph);
228 GVNOptimization gvn(graph, side_effects);
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000229 LICM licm(graph, side_effects);
Mingyao Yangf384f882014-10-22 16:08:18 -0700230 BoundsCheckElimination bce(graph);
Calin Juravle10e244f2015-01-26 18:54:32 +0000231 ReferenceTypePropagation type_propagation(graph);
232 InstructionSimplifier simplify2(graph, "instruction_simplifier_after_types");
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000233
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800234 IntrinsicsRecognizer intrinsics(graph, dex_compilation_unit.GetDexFile(), driver);
235
Nicolas Geoffray31596742014-11-24 15:28:45 +0000236 HOptimization* optimizations[] = {
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000237 &redundant_phi,
238 &dead_phi,
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800239 &intrinsics,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000240 &dce,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000241 &fold1,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000242 &simplify1,
243 &inliner,
Nicolas Geoffray9ee66182015-01-16 12:35:40 +0000244 &fold2,
Nicolas Geoffray86dde162015-01-26 12:54:33 +0000245 &side_effects,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000246 &gvn,
Nicolas Geoffray82091da2015-01-26 10:02:45 +0000247 &licm,
Mingyao Yangf384f882014-10-22 16:08:18 -0700248 &bce,
Calin Juravle10e244f2015-01-26 18:54:32 +0000249 &type_propagation,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000250 &simplify2
Nicolas Geoffray31596742014-11-24 15:28:45 +0000251 };
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000252
Calin Juravle10e244f2015-01-26 18:54:32 +0000253 RunOptimizations(optimizations, arraysize(optimizations), visualizer);
Nicolas Geoffray5e6916c2014-11-18 16:53:35 +0000254}
255
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000256// The stack map we generate must be 4-byte aligned on ARM. Since existing
257// maps are generated alongside these stack maps, we must also align them.
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800258static ArrayRef<const uint8_t> AlignVectorSize(std::vector<uint8_t>& vector) {
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000259 size_t size = vector.size();
260 size_t aligned_size = RoundUp(size, 4);
261 for (; size < aligned_size; ++size) {
262 vector.push_back(0);
263 }
Andreas Gampee21dc3d2014-12-08 16:59:43 -0800264 return ArrayRef<const uint8_t>(vector);
Nicolas Geoffray376b2bb2014-12-09 14:26:32 +0000265}
266
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000267
268CompiledMethod* OptimizingCompiler::CompileOptimized(HGraph* graph,
269 CodeGenerator* codegen,
270 CompilerDriver* compiler_driver,
271 const DexCompilationUnit& dex_compilation_unit,
272 const HGraphVisualizer& visualizer) const {
273 RunOptimizations(
274 graph, compiler_driver, &compilation_stats_, dex_compilation_unit, visualizer);
275
276 PrepareForRegisterAllocation(graph).Run();
277 SsaLivenessAnalysis liveness(*graph, codegen);
278 liveness.Analyze();
279 visualizer.DumpGraph(kLivenessPassName);
280
281 RegisterAllocator register_allocator(graph->GetArena(), codegen, liveness);
282 register_allocator.AllocateRegisters();
283 visualizer.DumpGraph(kRegisterAllocatorPassName);
284
285 CodeVectorAllocator allocator;
286 codegen->CompileOptimized(&allocator);
287
288 std::vector<uint8_t> stack_map;
289 codegen->BuildStackMaps(&stack_map);
290
291 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledOptimized);
292
293 return CompiledMethod::SwapAllocCompiledMethodStackMap(
294 compiler_driver,
295 codegen->GetInstructionSet(),
296 ArrayRef<const uint8_t>(allocator.GetMemory()),
297 codegen->GetFrameSize(),
298 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000299 codegen->GetFpuSpillMask(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000300 ArrayRef<const uint8_t>(stack_map));
301}
302
303
304CompiledMethod* OptimizingCompiler::CompileBaseline(
305 CodeGenerator* codegen,
306 CompilerDriver* compiler_driver,
307 const DexCompilationUnit& dex_compilation_unit) const {
308 CodeVectorAllocator allocator;
309 codegen->CompileBaseline(&allocator);
310
311 std::vector<uint8_t> mapping_table;
312 DefaultSrcMap src_mapping_table;
313 bool include_debug_symbol = compiler_driver->GetCompilerOptions().GetIncludeDebugSymbols();
314 codegen->BuildMappingTable(&mapping_table, include_debug_symbol ? &src_mapping_table : nullptr);
315 std::vector<uint8_t> vmap_table;
316 codegen->BuildVMapTable(&vmap_table);
317 std::vector<uint8_t> gc_map;
318 codegen->BuildNativeGCMap(&gc_map, dex_compilation_unit);
319
320 compilation_stats_.RecordStat(MethodCompilationStat::kCompiledBaseline);
321 return CompiledMethod::SwapAllocCompiledMethod(compiler_driver,
322 codegen->GetInstructionSet(),
323 ArrayRef<const uint8_t>(allocator.GetMemory()),
324 codegen->GetFrameSize(),
325 codegen->GetCoreSpillMask(),
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000326 codegen->GetFpuSpillMask(),
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000327 &src_mapping_table,
328 AlignVectorSize(mapping_table),
329 AlignVectorSize(vmap_table),
330 AlignVectorSize(gc_map),
331 ArrayRef<const uint8_t>());
332}
333
Nicolas Geoffraye2dc6fa2014-11-17 12:55:12 +0000334CompiledMethod* OptimizingCompiler::Compile(const DexFile::CodeItem* code_item,
335 uint32_t access_flags,
336 InvokeType invoke_type,
337 uint16_t class_def_idx,
338 uint32_t method_idx,
339 jobject class_loader,
340 const DexFile& dex_file) const {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700341 UNUSED(invoke_type);
Calin Juravle48c2b032014-12-09 18:11:36 +0000342 compilation_stats_.RecordStat(MethodCompilationStat::kAttemptCompilation);
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000343 CompilerDriver* compiler_driver = GetCompilerDriver();
344 InstructionSet instruction_set = compiler_driver->GetInstructionSet();
Nicolas Geoffray8d486732014-07-16 16:23:40 +0100345 // Always use the thumb2 assembler: some runtime functionality (like implicit stack
346 // overflow checks) assume thumb2.
347 if (instruction_set == kArm) {
348 instruction_set = kThumb2;
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100349 }
350
351 // Do not attempt to compile on architectures we do not support.
Nicolas Geoffray1ba0f592014-10-27 15:14:55 +0000352 if (!IsInstructionSetSupported(instruction_set)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000353 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledUnsupportedIsa);
Nicolas Geoffray8fb5ce32014-07-04 09:43:26 +0100354 return nullptr;
355 }
356
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000357 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
Calin Juravle48c2b032014-12-09 18:11:36 +0000358 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledPathological);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000359 return nullptr;
360 }
361
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000362 DexCompilationUnit dex_compilation_unit(
363 nullptr, class_loader, art::Runtime::Current()->GetClassLinker(), dex_file, code_item,
Ian Rogers72d32622014-05-06 16:20:11 -0700364 class_def_idx, method_idx, access_flags,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000365 compiler_driver->GetVerifiedMethod(&dex_file, method_idx));
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000366
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000367 std::string method_name = PrettyMethod(method_idx, dex_file);
368
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000369 // For testing purposes, we put a special marker on method names that should be compiled
370 // with this compiler. This makes sure we're not regressing.
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000371 bool shouldCompile = method_name.find("$opt$") != std::string::npos;
372 bool shouldOptimize = method_name.find("$opt$reg$") != std::string::npos;
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +0000373
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000374 ArenaPool pool;
375 ArenaAllocator arena(&pool);
Calin Juravle48c2b032014-12-09 18:11:36 +0000376 HGraphBuilder builder(&arena,
377 &dex_compilation_unit,
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000378 &dex_compilation_unit,
379 &dex_file,
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000380 compiler_driver,
Calin Juravle48c2b032014-12-09 18:11:36 +0000381 &compilation_stats_);
Nicolas Geoffrayf635e632014-05-14 09:43:38 +0100382
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000383 VLOG(compiler) << "Building " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000384 HGraph* graph = builder.BuildGraph(*code_item);
385 if (graph == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800386 CHECK(!shouldCompile) << "Could not build graph in optimizing compiler";
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000387 return nullptr;
388 }
389
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000390 std::unique_ptr<CodeGenerator> codegen(
Calin Juravlecd6dffe2015-01-08 17:35:35 +0000391 CodeGenerator::Create(graph,
392 instruction_set,
393 *compiler_driver->GetInstructionSetFeatures(),
394 compiler_driver->GetCompilerOptions()));
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000395 if (codegen.get() == nullptr) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800396 CHECK(!shouldCompile) << "Could not find code generator for optimizing compiler";
Calin Juravle48c2b032014-12-09 18:11:36 +0000397 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledNoCodegen);
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000398 return nullptr;
399 }
400
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100401 HGraphVisualizer visualizer(
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000402 visualizer_output_.get(), graph, kStringFilter, *codegen.get(), method_name.c_str());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100403 visualizer.DumpGraph("builder");
404
Calin Juravle48c2b032014-12-09 18:11:36 +0000405 bool can_optimize = CanOptimize(*code_item);
406 bool can_allocate_registers = RegisterAllocator::CanAllocateRegistersFor(*graph, instruction_set);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000407 CompiledMethod* result = nullptr;
Calin Juravle48c2b032014-12-09 18:11:36 +0000408 if (run_optimizations_ && can_optimize && can_allocate_registers) {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000409 VLOG(compiler) << "Optimizing " << PrettyMethod(method_idx, dex_file);
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000410 if (!graph->TryBuildingSsa()) {
411 LOG(INFO) << "Skipping compilation of "
412 << PrettyMethod(method_idx, dex_file)
413 << ": it contains a non natural loop";
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000414 // We could not transform the graph to SSA, bailout.
Calin Juravle48c2b032014-12-09 18:11:36 +0000415 compilation_stats_.RecordStat(MethodCompilationStat::kNotCompiledCannotBuildSSA);
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000416 } else {
417 result = CompileOptimized(graph, codegen.get(), compiler_driver, dex_compilation_unit, visualizer);
Nicolas Geoffrayf5370122014-12-02 11:51:19 +0000418 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100419 } else if (shouldOptimize && RegisterAllocator::Supports(instruction_set)) {
420 LOG(FATAL) << "Could not allocate registers in optimizing compiler";
Zheng Xu5667fdb2014-10-23 18:29:55 +0800421 UNREACHABLE();
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100422 } else {
Nicolas Geoffray43a539f2014-12-02 10:19:51 +0000423 VLOG(compiler) << "Compile baseline " << PrettyMethod(method_idx, dex_file);
Calin Juravle48c2b032014-12-09 18:11:36 +0000424
425 if (!run_optimizations_) {
426 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedDisabled);
427 } else if (!can_optimize) {
428 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedTryCatch);
429 } else if (!can_allocate_registers) {
430 compilation_stats_.RecordStat(MethodCompilationStat::kNotOptimizedRegisterAllocator);
431 }
432
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000433 result = CompileBaseline(codegen.get(), compiler_driver, dex_compilation_unit);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100434 }
Nicolas Geoffray12df9eb2015-01-09 14:53:50 +0000435 return result;
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000436}
437
Andreas Gampe53c913b2014-08-12 23:19:23 -0700438Compiler* CreateOptimizingCompiler(CompilerDriver* driver) {
439 return new OptimizingCompiler(driver);
440}
441
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000442} // namespace art