blob: a366a201c60a939953524f7c42ad43654c553265 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +000017#include "compiler.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070018#include "compiler_internals.h"
19#include "driver/compiler_driver.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080020#include "driver/compiler_options.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070021#include "dataflow_iterator-inl.h"
22#include "leb128.h"
23#include "mirror/object.h"
Jean Christophe Beyler2469e602014-05-06 20:36:55 -070024#include "pass_driver_me_opts.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "runtime.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070026#include "base/logging.h"
buzbeea61f4952013-08-23 14:27:06 -070027#include "base/timing_logger.h"
Dave Allison39c3bfb2014-01-28 18:33:52 -080028#include "driver/compiler_options.h"
Vladimir Marko5c96e6b2013-11-14 15:34:17 +000029#include "dex/quick/dex_file_to_method_inliner_map.h"
30
Brian Carlstrom7940e442013-07-12 13:46:57 -070031namespace art {
Brian Carlstrom7940e442013-07-12 13:46:57 -070032
Ian Rogers72d32622014-05-06 16:20:11 -070033extern "C" void ArtInitQuickCompilerContext(art::CompilerDriver* driver) {
34 CHECK(driver->GetCompilerContext() == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070035}
36
Ian Rogers72d32622014-05-06 16:20:11 -070037extern "C" void ArtUnInitQuickCompilerContext(art::CompilerDriver* driver) {
38 CHECK(driver->GetCompilerContext() == nullptr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070039}
40
41/* Default optimizer/debug setting for the compiler. */
Brian Carlstrom7934ac22013-07-26 10:54:15 -070042static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
buzbee091cc402014-03-31 10:14:40 -070043 (1 << kLoadStoreElimination) | // TODO: this pass has been broken for awhile - fix or delete.
Brian Carlstrom7934ac22013-07-26 10:54:15 -070044 // (1 << kLoadHoisting) |
45 // (1 << kSuppressLoads) |
46 // (1 << kNullCheckElimination) |
Vladimir Markobfea9c22014-01-17 17:49:33 +000047 // (1 << kClassInitCheckElimination) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070048 // (1 << kPromoteRegs) |
buzbee30adc732014-05-09 15:10:18 -070049 // (1 << kTrackLiveTemps) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070050 // (1 << kSafeOptimizations) |
51 // (1 << kBBOpt) |
52 // (1 << kMatch) |
53 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080054 // (1 << kSuppressExceptionEdges) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +000055 // (1 << kSuppressMethodInlining) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 0;
57
58static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070059 // (1 << kDebugDisplayMissingTargets) |
60 // (1 << kDebugVerbose) |
61 // (1 << kDebugDumpCFG) |
62 // (1 << kDebugSlowFieldPath) |
63 // (1 << kDebugSlowInvokePath) |
64 // (1 << kDebugSlowStringPath) |
65 // (1 << kDebugSlowestFieldPath) |
66 // (1 << kDebugSlowestStringPath) |
67 // (1 << kDebugExerciseResolveMethod) |
68 // (1 << kDebugVerifyDataflow) |
69 // (1 << kDebugShowMemoryUsage) |
70 // (1 << kDebugShowNops) |
71 // (1 << kDebugCountOpcodes) |
72 // (1 << kDebugDumpCheckStats) |
73 // (1 << kDebugDumpBitcodeFile) |
74 // (1 << kDebugVerifyBitcode) |
75 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070076 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070077 // (1 << kDebugTimings) |
buzbeeb01bf152014-05-13 15:59:07 -070078 // (1 << kDebugCodegenDump) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 0;
80
Vladimir Marko25724ef2013-11-12 15:09:20 +000081CompilationUnit::CompilationUnit(ArenaPool* pool)
Ian Rogers93dcff32014-05-15 09:11:23 -070082 : compiler_driver(nullptr),
83 class_linker(nullptr),
84 dex_file(nullptr),
85 class_loader(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000086 class_def_idx(0),
87 method_idx(0),
Ian Rogers93dcff32014-05-15 09:11:23 -070088 code_item(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000089 access_flags(0),
90 invoke_type(kDirect),
Ian Rogers93dcff32014-05-15 09:11:23 -070091 shorty(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000092 disable_opt(0),
93 enable_debug(0),
94 verbose(false),
Ian Rogers93dcff32014-05-15 09:11:23 -070095 compiler(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +000096 instruction_set(kNone),
Ian Rogers93dcff32014-05-15 09:11:23 -070097 target64(false),
Vladimir Marko25724ef2013-11-12 15:09:20 +000098 num_dalvik_registers(0),
Ian Rogers93dcff32014-05-15 09:11:23 -070099 insns(nullptr),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000100 num_ins(0),
101 num_outs(0),
102 num_regs(0),
Vladimir Marko25724ef2013-11-12 15:09:20 +0000103 compiler_flip_match(false),
104 arena(pool),
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000105 arena_stack(pool),
Ian Rogers93dcff32014-05-15 09:11:23 -0700106 mir_graph(nullptr),
107 cg(nullptr),
Jean Christophe Beyler8bcecce2014-04-29 13:42:08 -0700108 timings("QuickCompiler", true, false),
109 print_pass(false) {
Vladimir Marko25724ef2013-11-12 15:09:20 +0000110}
111
112CompilationUnit::~CompilationUnit() {
113}
114
buzbeea61f4952013-08-23 14:27:06 -0700115void CompilationUnit::StartTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000116 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700117 timings.StartSplit(label);
118 }
119}
120
121void CompilationUnit::NewTimingSplit(const char* label) {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000122 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700123 timings.NewSplit(label);
124 }
125}
126
127void CompilationUnit::EndTiming() {
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000128 if (compiler_driver->GetDumpPasses()) {
buzbeea61f4952013-08-23 14:27:06 -0700129 timings.EndSplit();
Nicolas Geoffrayea3fa0b2014-02-10 11:59:41 +0000130 if (enable_debug & (1 << kDebugTimings)) {
131 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
132 LOG(INFO) << Dumpable<TimingLogger>(timings);
133 }
buzbeea61f4952013-08-23 14:27:06 -0700134 }
135}
136
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100137// Enable opcodes that mostly work, but produce assertion errors (thus breaking libartd.so).
138#define ARM64_USE_EXPERIMENTAL_OPCODES 0
139
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100140// TODO: Remove this when we are able to compile everything.
141int arm64_support_list[] = {
142 Instruction::NOP,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100143 Instruction::MOVE,
144 Instruction::MOVE_FROM16,
145 Instruction::MOVE_16,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100146 Instruction::MOVE_EXCEPTION,
147 Instruction::RETURN_VOID,
148 Instruction::RETURN,
149 Instruction::RETURN_WIDE,
150 Instruction::CONST_4,
151 Instruction::CONST_16,
152 Instruction::CONST,
153 Instruction::CONST_STRING,
154 Instruction::MONITOR_ENTER,
155 Instruction::MONITOR_EXIT,
156 Instruction::THROW,
157 Instruction::GOTO,
158 Instruction::GOTO_16,
159 Instruction::GOTO_32,
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100160 Instruction::PACKED_SWITCH,
161 Instruction::SPARSE_SWITCH,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100162 Instruction::IF_EQ,
163 Instruction::IF_NE,
164 Instruction::IF_LT,
165 Instruction::IF_GE,
166 Instruction::IF_GT,
167 Instruction::IF_LE,
168 Instruction::IF_EQZ,
169 Instruction::IF_NEZ,
170 Instruction::IF_LTZ,
171 Instruction::IF_GEZ,
172 Instruction::IF_GTZ,
173 Instruction::IF_LEZ,
174 Instruction::NEG_INT,
175 Instruction::NOT_INT,
176 Instruction::NEG_FLOAT,
177 Instruction::INT_TO_BYTE,
178 Instruction::INT_TO_CHAR,
179 Instruction::INT_TO_SHORT,
180 Instruction::ADD_INT,
181 Instruction::SUB_INT,
182 Instruction::MUL_INT,
183 Instruction::DIV_INT,
184 Instruction::REM_INT,
185 Instruction::AND_INT,
186 Instruction::OR_INT,
187 Instruction::XOR_INT,
188 Instruction::SHL_INT,
189 Instruction::SHR_INT,
190 Instruction::USHR_INT,
191 Instruction::ADD_FLOAT,
192 Instruction::SUB_FLOAT,
193 Instruction::MUL_FLOAT,
194 Instruction::DIV_FLOAT,
195 Instruction::ADD_INT_2ADDR,
196 Instruction::SUB_INT_2ADDR,
197 Instruction::MUL_INT_2ADDR,
198 Instruction::DIV_INT_2ADDR,
199 Instruction::REM_INT_2ADDR,
200 Instruction::AND_INT_2ADDR,
201 Instruction::OR_INT_2ADDR,
202 Instruction::XOR_INT_2ADDR,
203 Instruction::SHL_INT_2ADDR,
204 Instruction::SHR_INT_2ADDR,
205 Instruction::USHR_INT_2ADDR,
206 Instruction::ADD_FLOAT_2ADDR,
207 Instruction::SUB_FLOAT_2ADDR,
208 Instruction::MUL_FLOAT_2ADDR,
209 Instruction::DIV_FLOAT_2ADDR,
210 Instruction::ADD_INT_LIT16,
211 Instruction::RSUB_INT,
212 Instruction::MUL_INT_LIT16,
213 Instruction::DIV_INT_LIT16,
214 Instruction::REM_INT_LIT16,
215 Instruction::AND_INT_LIT16,
216 Instruction::OR_INT_LIT16,
217 Instruction::XOR_INT_LIT16,
218 Instruction::ADD_INT_LIT8,
219 Instruction::RSUB_INT_LIT8,
220 Instruction::MUL_INT_LIT8,
221 Instruction::DIV_INT_LIT8,
222 Instruction::REM_INT_LIT8,
223 Instruction::AND_INT_LIT8,
224 Instruction::OR_INT_LIT8,
225 Instruction::XOR_INT_LIT8,
226 Instruction::SHL_INT_LIT8,
227 Instruction::SHR_INT_LIT8,
228 Instruction::USHR_INT_LIT8,
229 // TODO(Arm64): Enable compiler pass
230 // ----- ExtendedMIROpcode -----
231 kMirOpPhi,
232 kMirOpCopy,
233 kMirOpFusedCmplFloat,
234 kMirOpFusedCmpgFloat,
235 kMirOpFusedCmplDouble,
236 kMirOpFusedCmpgDouble,
237 kMirOpFusedCmpLong,
238 kMirOpNop,
239 kMirOpNullCheck,
240 kMirOpRangeCheck,
241 kMirOpDivZeroCheck,
242 kMirOpCheck,
243 kMirOpCheckPart2,
244 kMirOpSelect,
245
246#if ARM64_USE_EXPERIMENTAL_OPCODES
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100247 Instruction::MOVE_WIDE,
248 Instruction::MOVE_WIDE_FROM16,
249 Instruction::MOVE_WIDE_16,
250 Instruction::MOVE_OBJECT,
251 Instruction::MOVE_OBJECT_FROM16,
252 Instruction::MOVE_OBJECT_16,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100253 // Instruction::MOVE_RESULT,
254 // Instruction::MOVE_RESULT_WIDE,
255 // Instruction::MOVE_RESULT_OBJECT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100256 // Instruction::RETURN_OBJECT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100257 // Instruction::CONST_HIGH16,
258 // Instruction::CONST_WIDE_16,
259 // Instruction::CONST_WIDE_32,
260 // Instruction::CONST_WIDE,
261 // Instruction::CONST_WIDE_HIGH16,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100262 // Instruction::CONST_STRING_JUMBO,
263 // Instruction::CONST_CLASS,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100264 // Instruction::CHECK_CAST,
265 // Instruction::INSTANCE_OF,
266 // Instruction::ARRAY_LENGTH,
267 // Instruction::NEW_INSTANCE,
268 // Instruction::NEW_ARRAY,
269 // Instruction::FILLED_NEW_ARRAY,
270 // Instruction::FILLED_NEW_ARRAY_RANGE,
271 // Instruction::FILL_ARRAY_DATA,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100272 Instruction::CMPL_FLOAT,
273 Instruction::CMPG_FLOAT,
274 Instruction::CMPL_DOUBLE,
275 Instruction::CMPG_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100276 Instruction::CMP_LONG,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100277 // Instruction::UNUSED_3E,
278 // Instruction::UNUSED_3F,
279 // Instruction::UNUSED_40,
280 // Instruction::UNUSED_41,
281 // Instruction::UNUSED_42,
282 // Instruction::UNUSED_43,
283 // Instruction::AGET,
284 // Instruction::AGET_WIDE,
285 // Instruction::AGET_OBJECT,
286 // Instruction::AGET_BOOLEAN,
287 // Instruction::AGET_BYTE,
288 // Instruction::AGET_CHAR,
289 // Instruction::AGET_SHORT,
290 // Instruction::APUT,
291 // Instruction::APUT_WIDE,
292 // Instruction::APUT_OBJECT,
293 // Instruction::APUT_BOOLEAN,
294 // Instruction::APUT_BYTE,
295 // Instruction::APUT_CHAR,
296 // Instruction::APUT_SHORT,
297 // Instruction::IGET,
298 // Instruction::IGET_WIDE,
299 // Instruction::IGET_OBJECT,
300 // Instruction::IGET_BOOLEAN,
301 // Instruction::IGET_BYTE,
302 // Instruction::IGET_CHAR,
303 // Instruction::IGET_SHORT,
304 // Instruction::IPUT,
305 // Instruction::IPUT_WIDE,
306 // Instruction::IPUT_OBJECT,
307 // Instruction::IPUT_BOOLEAN,
308 // Instruction::IPUT_BYTE,
309 // Instruction::IPUT_CHAR,
310 // Instruction::IPUT_SHORT,
Zheng Xuc8304302014-05-15 17:21:01 +0100311 Instruction::SGET,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100312 // Instruction::SGET_WIDE,
Zheng Xuc8304302014-05-15 17:21:01 +0100313 Instruction::SGET_OBJECT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100314 // Instruction::SGET_BOOLEAN,
315 // Instruction::SGET_BYTE,
316 // Instruction::SGET_CHAR,
317 // Instruction::SGET_SHORT,
Zheng Xuc8304302014-05-15 17:21:01 +0100318 Instruction::SPUT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100319 // Instruction::SPUT_WIDE,
320 // Instruction::SPUT_OBJECT,
321 // Instruction::SPUT_BOOLEAN,
322 // Instruction::SPUT_BYTE,
323 // Instruction::SPUT_CHAR,
324 // Instruction::SPUT_SHORT,
325 Instruction::INVOKE_VIRTUAL,
326 Instruction::INVOKE_SUPER,
327 Instruction::INVOKE_DIRECT,
328 Instruction::INVOKE_STATIC,
329 Instruction::INVOKE_INTERFACE,
330 // Instruction::RETURN_VOID_BARRIER,
331 // Instruction::INVOKE_VIRTUAL_RANGE,
332 // Instruction::INVOKE_SUPER_RANGE,
333 // Instruction::INVOKE_DIRECT_RANGE,
334 // Instruction::INVOKE_STATIC_RANGE,
335 // Instruction::INVOKE_INTERFACE_RANGE,
336 // Instruction::UNUSED_79,
337 // Instruction::UNUSED_7A,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100338 Instruction::NEG_LONG,
339 Instruction::NOT_LONG,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100340 Instruction::NEG_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100341 Instruction::INT_TO_LONG,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100342 Instruction::INT_TO_FLOAT,
343 Instruction::INT_TO_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100344 Instruction::LONG_TO_INT,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100345 Instruction::LONG_TO_FLOAT,
346 Instruction::LONG_TO_DOUBLE,
347 Instruction::FLOAT_TO_INT,
348 Instruction::FLOAT_TO_LONG,
349 Instruction::FLOAT_TO_DOUBLE,
350 Instruction::DOUBLE_TO_INT,
351 Instruction::DOUBLE_TO_LONG,
352 Instruction::DOUBLE_TO_FLOAT,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100353 Instruction::ADD_LONG,
354 Instruction::SUB_LONG,
355 Instruction::MUL_LONG,
356 Instruction::DIV_LONG,
357 Instruction::REM_LONG,
358 Instruction::AND_LONG,
359 Instruction::OR_LONG,
360 Instruction::XOR_LONG,
361 Instruction::SHL_LONG,
362 Instruction::SHR_LONG,
363 Instruction::USHR_LONG,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100364 // Instruction::REM_FLOAT,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100365 Instruction::ADD_DOUBLE,
366 Instruction::SUB_DOUBLE,
367 Instruction::MUL_DOUBLE,
368 Instruction::DIV_DOUBLE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100369 // Instruction::REM_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100370 Instruction::ADD_LONG_2ADDR,
371 Instruction::SUB_LONG_2ADDR,
372 Instruction::MUL_LONG_2ADDR,
373 Instruction::DIV_LONG_2ADDR,
374 Instruction::REM_LONG_2ADDR,
375 Instruction::AND_LONG_2ADDR,
376 Instruction::OR_LONG_2ADDR,
377 Instruction::XOR_LONG_2ADDR,
378 Instruction::SHL_LONG_2ADDR,
379 Instruction::SHR_LONG_2ADDR,
380 Instruction::USHR_LONG_2ADDR,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100381 // Instruction::REM_FLOAT_2ADDR,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100382 Instruction::ADD_DOUBLE_2ADDR,
383 Instruction::SUB_DOUBLE_2ADDR,
384 Instruction::MUL_DOUBLE_2ADDR,
385 Instruction::DIV_DOUBLE_2ADDR,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100386 // Instruction::REM_DOUBLE_2ADDR,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100387 // Instruction::IGET_QUICK,
388 // Instruction::IGET_WIDE_QUICK,
389 // Instruction::IGET_OBJECT_QUICK,
390 // Instruction::IPUT_QUICK,
391 // Instruction::IPUT_WIDE_QUICK,
392 // Instruction::IPUT_OBJECT_QUICK,
393 // Instruction::INVOKE_VIRTUAL_QUICK,
394 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
395 // Instruction::UNUSED_EB,
396 // Instruction::UNUSED_EC,
397 // Instruction::UNUSED_ED,
398 // Instruction::UNUSED_EE,
399 // Instruction::UNUSED_EF,
400 // Instruction::UNUSED_F0,
401 // Instruction::UNUSED_F1,
402 // Instruction::UNUSED_F2,
403 // Instruction::UNUSED_F3,
404 // Instruction::UNUSED_F4,
405 // Instruction::UNUSED_F5,
406 // Instruction::UNUSED_F6,
407 // Instruction::UNUSED_F7,
408 // Instruction::UNUSED_F8,
409 // Instruction::UNUSED_F9,
410 // Instruction::UNUSED_FA,
411 // Instruction::UNUSED_FB,
412 // Instruction::UNUSED_FC,
413 // Instruction::UNUSED_FD,
414 // Instruction::UNUSED_FE,
415 // Instruction::UNUSED_FF,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100416#endif /* ARM64_USE_EXPERIMENTAL_OPCODES */
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100417};
418
419// TODO: Remove this when we are able to compile everything.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700420int x86_64_support_list[] = {
421 Instruction::NOP,
422 // Instruction::MOVE,
423 // Instruction::MOVE_FROM16,
424 // Instruction::MOVE_16,
425 // Instruction::MOVE_WIDE,
426 // Instruction::MOVE_WIDE_FROM16,
427 // Instruction::MOVE_WIDE_16,
428 // Instruction::MOVE_OBJECT,
429 // Instruction::MOVE_OBJECT_FROM16,
430 // Instruction::MOVE_OBJECT_16,
431 // Instruction::MOVE_RESULT,
432 // Instruction::MOVE_RESULT_WIDE,
433 // Instruction::MOVE_RESULT_OBJECT,
434 // Instruction::MOVE_EXCEPTION,
435 Instruction::RETURN_VOID,
436 Instruction::RETURN,
437 // Instruction::RETURN_WIDE,
438 Instruction::RETURN_OBJECT,
439 // Instruction::CONST_4,
440 // Instruction::CONST_16,
441 // Instruction::CONST,
442 // Instruction::CONST_HIGH16,
443 // Instruction::CONST_WIDE_16,
444 // Instruction::CONST_WIDE_32,
445 // Instruction::CONST_WIDE,
446 // Instruction::CONST_WIDE_HIGH16,
447 // Instruction::CONST_STRING,
448 // Instruction::CONST_STRING_JUMBO,
449 // Instruction::CONST_CLASS,
450 // Instruction::MONITOR_ENTER,
451 // Instruction::MONITOR_EXIT,
452 // Instruction::CHECK_CAST,
453 // Instruction::INSTANCE_OF,
454 // Instruction::ARRAY_LENGTH,
455 // Instruction::NEW_INSTANCE,
456 // Instruction::NEW_ARRAY,
457 // Instruction::FILLED_NEW_ARRAY,
458 // Instruction::FILLED_NEW_ARRAY_RANGE,
459 // Instruction::FILL_ARRAY_DATA,
460 // Instruction::THROW,
461 // Instruction::GOTO,
462 // Instruction::GOTO_16,
463 // Instruction::GOTO_32,
464 // Instruction::PACKED_SWITCH,
465 // Instruction::SPARSE_SWITCH,
466 // Instruction::CMPL_FLOAT,
467 // Instruction::CMPG_FLOAT,
468 // Instruction::CMPL_DOUBLE,
469 // Instruction::CMPG_DOUBLE,
470 // Instruction::CMP_LONG,
471 // Instruction::IF_EQ,
472 // Instruction::IF_NE,
473 // Instruction::IF_LT,
474 // Instruction::IF_GE,
475 // Instruction::IF_GT,
476 // Instruction::IF_LE,
477 // Instruction::IF_EQZ,
478 // Instruction::IF_NEZ,
479 // Instruction::IF_LTZ,
480 // Instruction::IF_GEZ,
481 // Instruction::IF_GTZ,
482 // Instruction::IF_LEZ,
483 // Instruction::UNUSED_3E,
484 // Instruction::UNUSED_3F,
485 // Instruction::UNUSED_40,
486 // Instruction::UNUSED_41,
487 // Instruction::UNUSED_42,
488 // Instruction::UNUSED_43,
489 // Instruction::AGET,
490 // Instruction::AGET_WIDE,
491 // Instruction::AGET_OBJECT,
492 // Instruction::AGET_BOOLEAN,
493 // Instruction::AGET_BYTE,
494 // Instruction::AGET_CHAR,
495 // Instruction::AGET_SHORT,
496 // Instruction::APUT,
497 // Instruction::APUT_WIDE,
498 // Instruction::APUT_OBJECT,
499 // Instruction::APUT_BOOLEAN,
500 // Instruction::APUT_BYTE,
501 // Instruction::APUT_CHAR,
502 // Instruction::APUT_SHORT,
503 // Instruction::IGET,
504 // Instruction::IGET_WIDE,
505 // Instruction::IGET_OBJECT,
506 // Instruction::IGET_BOOLEAN,
507 // Instruction::IGET_BYTE,
508 // Instruction::IGET_CHAR,
509 // Instruction::IGET_SHORT,
510 // Instruction::IPUT,
511 // Instruction::IPUT_WIDE,
512 // Instruction::IPUT_OBJECT,
513 // Instruction::IPUT_BOOLEAN,
514 // Instruction::IPUT_BYTE,
515 // Instruction::IPUT_CHAR,
516 // Instruction::IPUT_SHORT,
517 Instruction::SGET,
518 // Instruction::SGET_WIDE,
519 Instruction::SGET_OBJECT,
520 Instruction::SGET_BOOLEAN,
521 Instruction::SGET_BYTE,
522 Instruction::SGET_CHAR,
523 Instruction::SGET_SHORT,
524 Instruction::SPUT,
525 // Instruction::SPUT_WIDE,
526 Instruction::SPUT_OBJECT,
527 Instruction::SPUT_BOOLEAN,
528 Instruction::SPUT_BYTE,
529 Instruction::SPUT_CHAR,
530 Instruction::SPUT_SHORT,
531 Instruction::INVOKE_VIRTUAL,
532 Instruction::INVOKE_SUPER,
533 Instruction::INVOKE_DIRECT,
534 Instruction::INVOKE_STATIC,
535 Instruction::INVOKE_INTERFACE,
536 // Instruction::RETURN_VOID_BARRIER,
537 // Instruction::INVOKE_VIRTUAL_RANGE,
538 // Instruction::INVOKE_SUPER_RANGE,
539 // Instruction::INVOKE_DIRECT_RANGE,
540 // Instruction::INVOKE_STATIC_RANGE,
541 // Instruction::INVOKE_INTERFACE_RANGE,
542 // Instruction::UNUSED_79,
543 // Instruction::UNUSED_7A,
544 // Instruction::NEG_INT,
545 // Instruction::NOT_INT,
546 // Instruction::NEG_LONG,
547 // Instruction::NOT_LONG,
548 // Instruction::NEG_FLOAT,
549 // Instruction::NEG_DOUBLE,
550 // Instruction::INT_TO_LONG,
551 // Instruction::INT_TO_FLOAT,
552 // Instruction::INT_TO_DOUBLE,
553 // Instruction::LONG_TO_INT,
554 // Instruction::LONG_TO_FLOAT,
555 // Instruction::LONG_TO_DOUBLE,
556 // Instruction::FLOAT_TO_INT,
557 // Instruction::FLOAT_TO_LONG,
558 // Instruction::FLOAT_TO_DOUBLE,
559 // Instruction::DOUBLE_TO_INT,
560 // Instruction::DOUBLE_TO_LONG,
561 // Instruction::DOUBLE_TO_FLOAT,
562 // Instruction::INT_TO_BYTE,
563 // Instruction::INT_TO_CHAR,
564 // Instruction::INT_TO_SHORT,
565 // Instruction::ADD_INT,
566 // Instruction::SUB_INT,
567 // Instruction::MUL_INT,
568 // Instruction::DIV_INT,
569 // Instruction::REM_INT,
570 // Instruction::AND_INT,
571 // Instruction::OR_INT,
572 // Instruction::XOR_INT,
573 // Instruction::SHL_INT,
574 // Instruction::SHR_INT,
575 // Instruction::USHR_INT,
576 // Instruction::ADD_LONG,
577 // Instruction::SUB_LONG,
578 // Instruction::MUL_LONG,
579 // Instruction::DIV_LONG,
580 // Instruction::REM_LONG,
581 // Instruction::AND_LONG,
582 // Instruction::OR_LONG,
583 // Instruction::XOR_LONG,
584 // Instruction::SHL_LONG,
585 // Instruction::SHR_LONG,
586 // Instruction::USHR_LONG,
587 // Instruction::ADD_FLOAT,
588 // Instruction::SUB_FLOAT,
589 // Instruction::MUL_FLOAT,
590 // Instruction::DIV_FLOAT,
591 // Instruction::REM_FLOAT,
592 // Instruction::ADD_DOUBLE,
593 // Instruction::SUB_DOUBLE,
594 // Instruction::MUL_DOUBLE,
595 // Instruction::DIV_DOUBLE,
596 // Instruction::REM_DOUBLE,
597 // Instruction::ADD_INT_2ADDR,
598 // Instruction::SUB_INT_2ADDR,
599 // Instruction::MUL_INT_2ADDR,
600 // Instruction::DIV_INT_2ADDR,
601 // Instruction::REM_INT_2ADDR,
602 // Instruction::AND_INT_2ADDR,
603 // Instruction::OR_INT_2ADDR,
604 // Instruction::XOR_INT_2ADDR,
605 // Instruction::SHL_INT_2ADDR,
606 // Instruction::SHR_INT_2ADDR,
607 // Instruction::USHR_INT_2ADDR,
608 // Instruction::ADD_LONG_2ADDR,
609 // Instruction::SUB_LONG_2ADDR,
610 // Instruction::MUL_LONG_2ADDR,
611 // Instruction::DIV_LONG_2ADDR,
612 // Instruction::REM_LONG_2ADDR,
613 // Instruction::AND_LONG_2ADDR,
614 // Instruction::OR_LONG_2ADDR,
615 // Instruction::XOR_LONG_2ADDR,
616 // Instruction::SHL_LONG_2ADDR,
617 // Instruction::SHR_LONG_2ADDR,
618 // Instruction::USHR_LONG_2ADDR,
619 // Instruction::ADD_FLOAT_2ADDR,
620 // Instruction::SUB_FLOAT_2ADDR,
621 // Instruction::MUL_FLOAT_2ADDR,
622 // Instruction::DIV_FLOAT_2ADDR,
623 // Instruction::REM_FLOAT_2ADDR,
624 // Instruction::ADD_DOUBLE_2ADDR,
625 // Instruction::SUB_DOUBLE_2ADDR,
626 // Instruction::MUL_DOUBLE_2ADDR,
627 // Instruction::DIV_DOUBLE_2ADDR,
628 // Instruction::REM_DOUBLE_2ADDR,
629 // Instruction::ADD_INT_LIT16,
630 // Instruction::RSUB_INT,
631 // Instruction::MUL_INT_LIT16,
632 // Instruction::DIV_INT_LIT16,
633 // Instruction::REM_INT_LIT16,
634 // Instruction::AND_INT_LIT16,
635 // Instruction::OR_INT_LIT16,
636 // Instruction::XOR_INT_LIT16,
637 // Instruction::ADD_INT_LIT8,
638 // Instruction::RSUB_INT_LIT8,
639 // Instruction::MUL_INT_LIT8,
640 // Instruction::DIV_INT_LIT8,
641 // Instruction::REM_INT_LIT8,
642 // Instruction::AND_INT_LIT8,
643 // Instruction::OR_INT_LIT8,
644 // Instruction::XOR_INT_LIT8,
645 // Instruction::SHL_INT_LIT8,
646 // Instruction::SHR_INT_LIT8,
647 // Instruction::USHR_INT_LIT8,
648 // Instruction::IGET_QUICK,
649 // Instruction::IGET_WIDE_QUICK,
650 // Instruction::IGET_OBJECT_QUICK,
651 // Instruction::IPUT_QUICK,
652 // Instruction::IPUT_WIDE_QUICK,
653 // Instruction::IPUT_OBJECT_QUICK,
654 // Instruction::INVOKE_VIRTUAL_QUICK,
655 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
656 // Instruction::UNUSED_EB,
657 // Instruction::UNUSED_EC,
658 // Instruction::UNUSED_ED,
659 // Instruction::UNUSED_EE,
660 // Instruction::UNUSED_EF,
661 // Instruction::UNUSED_F0,
662 // Instruction::UNUSED_F1,
663 // Instruction::UNUSED_F2,
664 // Instruction::UNUSED_F3,
665 // Instruction::UNUSED_F4,
666 // Instruction::UNUSED_F5,
667 // Instruction::UNUSED_F6,
668 // Instruction::UNUSED_F7,
669 // Instruction::UNUSED_F8,
670 // Instruction::UNUSED_F9,
671 // Instruction::UNUSED_FA,
672 // Instruction::UNUSED_FB,
673 // Instruction::UNUSED_FC,
674 // Instruction::UNUSED_FD,
675 // Instruction::UNUSED_FE,
676 // Instruction::UNUSED_FF,
677
678 // ----- ExtendedMIROpcode -----
679 // kMirOpPhi,
680 // kMirOpCopy,
681 // kMirOpFusedCmplFloat,
682 // kMirOpFusedCmpgFloat,
683 // kMirOpFusedCmplDouble,
684 // kMirOpFusedCmpgDouble,
685 // kMirOpFusedCmpLong,
686 // kMirOpNop,
687 // kMirOpNullCheck,
688 // kMirOpRangeCheck,
689 // kMirOpDivZeroCheck,
690 // kMirOpCheck,
691 // kMirOpCheckPart2,
692 // kMirOpSelect,
693 // kMirOpLast,
694};
695
696// Z : boolean
697// B : byte
698// S : short
699// C : char
700// I : int
Zheng Xu48241e72014-05-23 11:52:42 +0800701// J : long
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700702// F : float
703// D : double
704// L : reference(object, array)
705// V : void
706// (ARM64) Current calling conversion only support 32bit softfp
707// which has problems with long, float, double
Serban Constantinescu032d3772014-05-23 17:38:18 +0100708constexpr char arm64_supported_types[] = "ZBSCILVJFD";
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700709// (x84_64) We still have troubles with compiling longs/doubles/floats
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700710constexpr char x86_64_supported_types[] = "ZBSCILVJFD";
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700711
712// TODO: Remove this when we are able to compile everything.
713static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100714 uint32_t shorty_size = strlen(shorty);
715 CHECK_GE(shorty_size, 1u);
716 // Set a limitation on maximum number of parameters.
717 // Note : there is an implied "method*" parameter, and probably "this" as well.
718 // 1 is for the return type. Currently, we only accept 2 parameters at the most.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700719 // (x86_64): For now we have the same limitation. But we might want to split this
720 // check in future into two separate cases for arm64 and x86_64.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700721 if ((shorty_size > (1 + 2)) && (instruction_set != kX86_64)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100722 return false;
723 }
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700724
725 const char* supported_types = arm64_supported_types;
726 if (instruction_set == kX86_64) {
727 supported_types = x86_64_supported_types;
728 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100729 for (uint32_t i = 0; i < shorty_size; i++) {
730 if (strchr(supported_types, shorty[i]) == nullptr) {
731 return false;
732 }
733 }
734 return true;
735};
736
737// TODO: Remove this when we are able to compile everything.
738// Skip the method that we do not support currently.
739static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
740 CompilationUnit& cu) {
741 // There is some limitation with current ARM 64 backend.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700742 if (cu.instruction_set == kArm64 || cu.instruction_set == kX86_64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100743 // Check if we can compile the prototype.
744 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700745 if (!CanCompileShorty(shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100746 VLOG(compiler) << "Unsupported shorty : " << shorty;
747 return false;
748 }
749
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700750 const int *support_list = arm64_support_list;
751 int support_list_size = arraysize(arm64_support_list);
752 if (cu.instruction_set == kX86_64) {
753 support_list = x86_64_support_list;
754 support_list_size = arraysize(x86_64_support_list);
755 }
756
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100757 for (int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700758 BasicBlock* bb = cu.mir_graph->GetBasicBlock(idx);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100759 if (bb == NULL) continue;
760 if (bb->block_type == kDead) continue;
761 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
762 int opcode = mir->dalvikInsn.opcode;
763 // Check if we support the byte code.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700764 if (std::find(support_list, support_list + support_list_size,
765 opcode) == support_list + support_list_size) {
buzbee35ba7f32014-05-31 08:59:01 -0700766 if (!cu.mir_graph->IsPseudoMirOp(opcode)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100767 VLOG(compiler) << "Unsupported dalvik byte code : "
768 << mir->dalvikInsn.opcode;
769 } else {
770 VLOG(compiler) << "Unsupported extended MIR opcode : "
771 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
772 }
773 return false;
774 }
775 // Check if it invokes a prototype that we cannot support.
776 if (Instruction::INVOKE_VIRTUAL == opcode ||
777 Instruction::INVOKE_SUPER == opcode ||
778 Instruction::INVOKE_DIRECT == opcode ||
779 Instruction::INVOKE_STATIC == opcode ||
780 Instruction::INVOKE_INTERFACE == opcode) {
781 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
782 const char* invoke_method_shorty = dex_file.GetMethodShorty(
783 dex_file.GetMethodId(invoke_method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700784 if (!CanCompileShorty(invoke_method_shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100785 VLOG(compiler) << "Unsupported to invoke '"
786 << PrettyMethod(invoke_method_idx, dex_file)
787 << "' with shorty : " << invoke_method_shorty;
788 return false;
789 }
790 }
791 }
792 }
793
794 LOG(INFO) << "Using experimental instruction set A64 for "
795 << PrettyMethod(method_idx, dex_file);
796 }
797 return true;
798}
799
Ian Rogers3d504072014-03-01 09:16:49 -0800800static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000801 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 const DexFile::CodeItem* code_item,
803 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700804 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000805 jobject class_loader, const DexFile& dex_file,
806 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700808 if (code_item->insns_size_in_code_units_ >= 0x10000) {
809 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
810 << " in " << PrettyMethod(method_idx, dex_file);
811 return NULL;
812 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700813
Jeff Hao4a200f52014-04-01 14:58:49 -0700814 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000815 return nullptr;
816 }
817
Brian Carlstrom7940e442013-07-12 13:46:57 -0700818 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800819 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700820
Ian Rogers3d504072014-03-01 09:16:49 -0800821 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700822 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800823 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700824 if (cu.instruction_set == kArm) {
825 cu.instruction_set = kThumb2;
826 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700827 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000828 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000829 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700830 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100831 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700832 (cu.instruction_set == kX86) ||
833 (cu.instruction_set == kX86_64) ||
834 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700835
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700837 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700838 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700839 cu.compiler_flip_match = false;
840 bool use_match = !cu.compiler_method_match.empty();
841 bool match = use_match && (cu.compiler_flip_match ^
842 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700843 std::string::npos));
844 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700845 cu.disable_opt = kCompilerOptimizerDisableFlags;
846 cu.enable_debug = kCompilerDebugFlags;
847 cu.verbose = VLOG_IS_ON(compiler) ||
848 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700849 }
850
Mingyao Yang42d65c52014-04-18 16:49:39 -0700851 if (gVerboseMethods.size() != 0) {
852 cu.verbose = false;
853 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
854 if (PrettyMethod(method_idx, dex_file).find(gVerboseMethods[i])
855 != std::string::npos) {
856 cu.verbose = true;
857 break;
858 }
859 }
860 }
861
buzbeeb01bf152014-05-13 15:59:07 -0700862 if (cu.verbose) {
863 cu.enable_debug |= (1 << kDebugCodegenDump);
864 }
865
Brian Carlstrom7940e442013-07-12 13:46:57 -0700866 /*
867 * TODO: rework handling of optimization and debug flags. Should we split out
868 * MIR and backend flags? Need command-line setting as well.
869 */
870
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000871 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700872
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700873 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700874 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700875 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700876 (1 << kLoadStoreElimination) |
877 (1 << kLoadHoisting) |
878 (1 << kSuppressLoads) |
879 (1 << kNullCheckElimination) |
880 (1 << kPromoteRegs) |
881 (1 << kTrackLiveTemps) |
882 (1 << kSafeOptimizations) |
883 (1 << kBBOpt) |
884 (1 << kMatch) |
885 (1 << kPromoteCompilerTemps));
886 }
887
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700888 if (cu.instruction_set == kArm64 || cu.instruction_set == kX86_64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100889 // TODO(Arm64): enable optimizations once backend is mature enough.
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700890 // TODO(X86_64): enable optimizations once backend is mature enough.
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100891 cu.disable_opt = ~(uint32_t)0;
buzbeeb01bf152014-05-13 15:59:07 -0700892 cu.enable_debug |= (1 << kDebugCodegenDump);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100893 }
894
buzbeea61f4952013-08-23 14:27:06 -0700895 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700896 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700897
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800898 /*
899 * After creation of the MIR graph, also create the code generator.
900 * The reason we do this is that optimizations on the MIR graph may need to get information
901 * that is only available if a CG exists.
902 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000903 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800904
Brian Carlstrom7940e442013-07-12 13:46:57 -0700905 /* Gathering opcode stats? */
906 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700907 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 }
909
Calin Juravlec1b643c2014-05-30 23:44:11 +0100910 // Check early if we should skip this compilation if the profiler is enabled.
Dave Allison39c3bfb2014-01-28 18:33:52 -0800911 if (cu.compiler_driver->ProfilePresent()) {
912 std::string methodname = PrettyMethod(method_idx, dex_file);
913 if (cu.mir_graph->SkipCompilation(methodname)) {
914 return NULL;
915 }
916 }
917
Brian Carlstrom7940e442013-07-12 13:46:57 -0700918 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700919 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700920 class_loader, dex_file);
921
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100922 // TODO(Arm64): Remove this when we are able to compile everything.
923 if (!CanCompileMethod(method_idx, dex_file, cu)) {
924 VLOG(compiler) << "Cannot compile method : " << PrettyMethod(method_idx, dex_file);
925 return nullptr;
926 }
927
buzbee1da1e2f2013-11-15 13:37:01 -0800928 cu.NewTimingSplit("MIROpt:CheckFilters");
Jeff Hao4a200f52014-04-01 14:58:49 -0700929 if (cu.mir_graph->SkipCompilation()) {
930 return NULL;
buzbeeee17e0a2013-07-31 10:47:37 -0700931 }
932
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800933 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700934 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800935 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700936
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700937 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
938 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939 }
940
941 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700942 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700943 }
944
buzbee1da1e2f2013-11-15 13:37:01 -0800945 /* Reassociate sreg names with original Dalvik vreg names. */
946 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700947
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000948 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
949 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
950 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
951 MemStats stack_stats(cu.arena_stack.GetPeakStats());
952 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
953 }
954 }
955 cu.arena_stack.Reset();
956
Brian Carlstrom7940e442013-07-12 13:46:57 -0700957 CompiledMethod* result = NULL;
958
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700959 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960
buzbee1da1e2f2013-11-15 13:37:01 -0800961 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700962 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800963 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700964
965 if (result) {
966 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
967 } else {
968 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
969 }
970
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700971 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000972 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000973 MemStats mem_stats(cu.arena.GetMemStats());
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000974 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700975 }
976 }
977
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700978 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
979 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700980 << " " << PrettyMethod(method_idx, dex_file);
981 }
982
buzbeea61f4952013-08-23 14:27:06 -0700983 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800984 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700985 return result;
986}
987
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000988CompiledMethod* CompileOneMethod(CompilerDriver& driver,
989 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700990 const DexFile::CodeItem* code_item,
991 uint32_t access_flags,
992 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700993 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700994 uint32_t method_idx,
995 jobject class_loader,
996 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000997 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000998 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000999 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001000}
1001
1002} // namespace art
1003
1004extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001005 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001006 const art::DexFile::CodeItem* code_item,
1007 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001008 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 const art::DexFile& dex_file) {
1010 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001011 art::Compiler* compiler = driver.GetCompiler();
1012 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001013 class_def_idx, method_idx, class_loader, dex_file,
1014 NULL /* use thread llvm_info */);
1015}