blob: d097500a77d7c2f40953bdd175e26a8370f09f0a [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) |
Vladimir Marko95a05972014-05-30 10:01:32 +010048 // (1 << kGlobalValueNumbering) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070049 // (1 << kPromoteRegs) |
buzbee30adc732014-05-09 15:10:18 -070050 // (1 << kTrackLiveTemps) |
Brian Carlstrom7934ac22013-07-26 10:54:15 -070051 // (1 << kSafeOptimizations) |
52 // (1 << kBBOpt) |
53 // (1 << kMatch) |
54 // (1 << kPromoteCompilerTemps) |
buzbee17189ac2013-11-08 11:07:02 -080055 // (1 << kSuppressExceptionEdges) |
Vladimir Marko9820b7c2014-01-02 16:40:37 +000056 // (1 << kSuppressMethodInlining) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070057 0;
58
59static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
Brian Carlstrom7934ac22013-07-26 10:54:15 -070060 // (1 << kDebugDisplayMissingTargets) |
61 // (1 << kDebugVerbose) |
62 // (1 << kDebugDumpCFG) |
63 // (1 << kDebugSlowFieldPath) |
64 // (1 << kDebugSlowInvokePath) |
65 // (1 << kDebugSlowStringPath) |
66 // (1 << kDebugSlowestFieldPath) |
67 // (1 << kDebugSlowestStringPath) |
68 // (1 << kDebugExerciseResolveMethod) |
69 // (1 << kDebugVerifyDataflow) |
70 // (1 << kDebugShowMemoryUsage) |
71 // (1 << kDebugShowNops) |
72 // (1 << kDebugCountOpcodes) |
73 // (1 << kDebugDumpCheckStats) |
74 // (1 << kDebugDumpBitcodeFile) |
75 // (1 << kDebugVerifyBitcode) |
76 // (1 << kDebugShowSummaryMemoryUsage) |
buzbeeee17e0a2013-07-31 10:47:37 -070077 // (1 << kDebugShowFilterStats) |
buzbeea61f4952013-08-23 14:27:06 -070078 // (1 << kDebugTimings) |
buzbeeb01bf152014-05-13 15:59:07 -070079 // (1 << kDebugCodegenDump) |
Brian Carlstrom7940e442013-07-12 13:46:57 -070080 0;
81
Andreas Gampe00caeed2014-07-10 01:43:08 -070082COMPILE_ASSERT(0U == static_cast<size_t>(kNone), kNone_not_0);
83COMPILE_ASSERT(1U == static_cast<size_t>(kArm), kArm_not_1);
84COMPILE_ASSERT(2U == static_cast<size_t>(kArm64), kArm64_not_2);
85COMPILE_ASSERT(3U == static_cast<size_t>(kThumb2), kThumb2_not_3);
86COMPILE_ASSERT(4U == static_cast<size_t>(kX86), kX86_not_4);
87COMPILE_ASSERT(5U == static_cast<size_t>(kX86_64), kX86_64_not_5);
88COMPILE_ASSERT(6U == static_cast<size_t>(kMips), kMips_not_6);
89COMPILE_ASSERT(7U == static_cast<size_t>(kMips64), kMips64_not_7);
Vladimir Marko25724ef2013-11-12 15:09:20 +000090
Andreas Gampe00caeed2014-07-10 01:43:08 -070091// Additional disabled optimizations (over generally disabled) per instruction set.
92static constexpr uint32_t kDisabledOptimizationsPerISA[] = {
93 // 0 = kNone.
94 ~0U,
95 // 1 = kArm, unused (will use kThumb2).
96 ~0U,
97 // 2 = kArm64. TODO(Arm64): enable optimizations once backend is mature enough.
98 (1 << kLoadStoreElimination) |
Andreas Gampe00caeed2014-07-10 01:43:08 -070099 0,
100 // 3 = kThumb2.
101 0,
102 // 4 = kX86.
103 0,
104 // 5 = kX86_64.
105 (1 << kLoadStoreElimination) |
106 0,
107 // 6 = kMips.
108 (1 << kLoadStoreElimination) |
109 (1 << kLoadHoisting) |
110 (1 << kSuppressLoads) |
111 (1 << kNullCheckElimination) |
112 (1 << kPromoteRegs) |
113 (1 << kTrackLiveTemps) |
114 (1 << kSafeOptimizations) |
115 (1 << kBBOpt) |
116 (1 << kMatch) |
117 (1 << kPromoteCompilerTemps) |
118 0,
119 // 7 = kMips64.
120 ~0U
121};
122COMPILE_ASSERT(sizeof(kDisabledOptimizationsPerISA) == 8 * sizeof(uint32_t), kDisabledOpts_unexp);
Vladimir Marko25724ef2013-11-12 15:09:20 +0000123
Andreas Gampe00caeed2014-07-10 01:43:08 -0700124// Supported shorty types per instruction set. nullptr means that all are available.
125// Z : boolean
126// B : byte
127// S : short
128// C : char
129// I : int
130// J : long
131// F : float
132// D : double
133// L : reference(object, array)
134// V : void
135static const char* kSupportedTypes[] = {
136 // 0 = kNone.
137 "",
138 // 1 = kArm, unused (will use kThumb2).
139 "",
140 // 2 = kArm64.
141 nullptr,
142 // 3 = kThumb2.
143 nullptr,
144 // 4 = kX86.
145 nullptr,
146 // 5 = kX86_64.
147 nullptr,
148 // 6 = kMips.
149 nullptr,
150 // 7 = kMips64.
151 ""
152};
153COMPILE_ASSERT(sizeof(kSupportedTypes) == 8 * sizeof(char*), kSupportedTypes_unexp);
buzbeea61f4952013-08-23 14:27:06 -0700154
Andreas Gampe00caeed2014-07-10 01:43:08 -0700155static int kAllOpcodes[] = {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100156 Instruction::NOP,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100157 Instruction::MOVE,
158 Instruction::MOVE_FROM16,
159 Instruction::MOVE_16,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800160 Instruction::MOVE_WIDE,
161 Instruction::MOVE_WIDE_FROM16,
162 Instruction::MOVE_WIDE_16,
163 Instruction::MOVE_OBJECT,
164 Instruction::MOVE_OBJECT_FROM16,
165 Instruction::MOVE_OBJECT_16,
buzbee54ee4442014-06-19 15:04:12 -0700166 Instruction::MOVE_RESULT,
167 Instruction::MOVE_RESULT_WIDE,
168 Instruction::MOVE_RESULT_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100169 Instruction::MOVE_EXCEPTION,
170 Instruction::RETURN_VOID,
171 Instruction::RETURN,
172 Instruction::RETURN_WIDE,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100173 Instruction::RETURN_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100174 Instruction::CONST_4,
175 Instruction::CONST_16,
176 Instruction::CONST,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800177 Instruction::CONST_HIGH16,
178 Instruction::CONST_WIDE_16,
179 Instruction::CONST_WIDE_32,
180 Instruction::CONST_WIDE,
181 Instruction::CONST_WIDE_HIGH16,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100182 Instruction::CONST_STRING,
buzbee54ee4442014-06-19 15:04:12 -0700183 Instruction::CONST_STRING_JUMBO,
184 Instruction::CONST_CLASS,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100185 Instruction::MONITOR_ENTER,
186 Instruction::MONITOR_EXIT,
buzbee54ee4442014-06-19 15:04:12 -0700187 Instruction::CHECK_CAST,
188 Instruction::INSTANCE_OF,
189 Instruction::ARRAY_LENGTH,
190 Instruction::NEW_INSTANCE,
191 Instruction::NEW_ARRAY,
192 Instruction::FILLED_NEW_ARRAY,
193 Instruction::FILLED_NEW_ARRAY_RANGE,
194 Instruction::FILL_ARRAY_DATA,
195 Instruction::THROW,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100196 Instruction::GOTO,
197 Instruction::GOTO_16,
198 Instruction::GOTO_32,
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100199 Instruction::PACKED_SWITCH,
200 Instruction::SPARSE_SWITCH,
Zheng Xue2eb29e2014-06-12 10:22:33 +0800201 Instruction::CMPL_FLOAT,
202 Instruction::CMPG_FLOAT,
203 Instruction::CMPL_DOUBLE,
204 Instruction::CMPG_DOUBLE,
205 Instruction::CMP_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100206 Instruction::IF_EQ,
207 Instruction::IF_NE,
208 Instruction::IF_LT,
209 Instruction::IF_GE,
210 Instruction::IF_GT,
211 Instruction::IF_LE,
212 Instruction::IF_EQZ,
213 Instruction::IF_NEZ,
214 Instruction::IF_LTZ,
215 Instruction::IF_GEZ,
216 Instruction::IF_GTZ,
217 Instruction::IF_LEZ,
buzbee54ee4442014-06-19 15:04:12 -0700218 Instruction::UNUSED_3E,
219 Instruction::UNUSED_3F,
220 Instruction::UNUSED_40,
221 Instruction::UNUSED_41,
222 Instruction::UNUSED_42,
223 Instruction::UNUSED_43,
224 Instruction::AGET,
225 Instruction::AGET_WIDE,
226 Instruction::AGET_OBJECT,
227 Instruction::AGET_BOOLEAN,
228 Instruction::AGET_BYTE,
229 Instruction::AGET_CHAR,
230 Instruction::AGET_SHORT,
231 Instruction::APUT,
232 Instruction::APUT_WIDE,
233 Instruction::APUT_OBJECT,
234 Instruction::APUT_BOOLEAN,
235 Instruction::APUT_BYTE,
236 Instruction::APUT_CHAR,
237 Instruction::APUT_SHORT,
238 Instruction::IGET,
239 Instruction::IGET_WIDE,
240 Instruction::IGET_OBJECT,
241 Instruction::IGET_BOOLEAN,
242 Instruction::IGET_BYTE,
243 Instruction::IGET_CHAR,
244 Instruction::IGET_SHORT,
245 Instruction::IPUT,
246 Instruction::IPUT_WIDE,
247 Instruction::IPUT_OBJECT,
248 Instruction::IPUT_BOOLEAN,
249 Instruction::IPUT_BYTE,
250 Instruction::IPUT_CHAR,
251 Instruction::IPUT_SHORT,
252 Instruction::SGET,
253 Instruction::SGET_WIDE,
254 Instruction::SGET_OBJECT,
255 Instruction::SGET_BOOLEAN,
256 Instruction::SGET_BYTE,
257 Instruction::SGET_CHAR,
258 Instruction::SGET_SHORT,
259 Instruction::SPUT,
260 Instruction::SPUT_WIDE,
261 Instruction::SPUT_OBJECT,
262 Instruction::SPUT_BOOLEAN,
263 Instruction::SPUT_BYTE,
264 Instruction::SPUT_CHAR,
265 Instruction::SPUT_SHORT,
266 Instruction::INVOKE_VIRTUAL,
267 Instruction::INVOKE_SUPER,
268 Instruction::INVOKE_DIRECT,
269 Instruction::INVOKE_STATIC,
270 Instruction::INVOKE_INTERFACE,
271 Instruction::RETURN_VOID_BARRIER,
272 Instruction::INVOKE_VIRTUAL_RANGE,
273 Instruction::INVOKE_SUPER_RANGE,
274 Instruction::INVOKE_DIRECT_RANGE,
275 Instruction::INVOKE_STATIC_RANGE,
276 Instruction::INVOKE_INTERFACE_RANGE,
277 Instruction::UNUSED_79,
278 Instruction::UNUSED_7A,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100279 Instruction::NEG_INT,
280 Instruction::NOT_INT,
buzbee54ee4442014-06-19 15:04:12 -0700281 Instruction::NEG_LONG,
282 Instruction::NOT_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100283 Instruction::NEG_FLOAT,
buzbee54ee4442014-06-19 15:04:12 -0700284 Instruction::NEG_DOUBLE,
285 Instruction::INT_TO_LONG,
286 Instruction::INT_TO_FLOAT,
287 Instruction::INT_TO_DOUBLE,
288 Instruction::LONG_TO_INT,
289 Instruction::LONG_TO_FLOAT,
290 Instruction::LONG_TO_DOUBLE,
291 Instruction::FLOAT_TO_INT,
292 Instruction::FLOAT_TO_LONG,
293 Instruction::FLOAT_TO_DOUBLE,
294 Instruction::DOUBLE_TO_INT,
295 Instruction::DOUBLE_TO_LONG,
296 Instruction::DOUBLE_TO_FLOAT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100297 Instruction::INT_TO_BYTE,
298 Instruction::INT_TO_CHAR,
299 Instruction::INT_TO_SHORT,
300 Instruction::ADD_INT,
301 Instruction::SUB_INT,
302 Instruction::MUL_INT,
303 Instruction::DIV_INT,
304 Instruction::REM_INT,
305 Instruction::AND_INT,
306 Instruction::OR_INT,
307 Instruction::XOR_INT,
308 Instruction::SHL_INT,
309 Instruction::SHR_INT,
310 Instruction::USHR_INT,
buzbee54ee4442014-06-19 15:04:12 -0700311 Instruction::ADD_LONG,
312 Instruction::SUB_LONG,
313 Instruction::MUL_LONG,
314 Instruction::DIV_LONG,
315 Instruction::REM_LONG,
316 Instruction::AND_LONG,
317 Instruction::OR_LONG,
318 Instruction::XOR_LONG,
319 Instruction::SHL_LONG,
320 Instruction::SHR_LONG,
321 Instruction::USHR_LONG,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100322 Instruction::ADD_FLOAT,
323 Instruction::SUB_FLOAT,
324 Instruction::MUL_FLOAT,
325 Instruction::DIV_FLOAT,
buzbee54ee4442014-06-19 15:04:12 -0700326 Instruction::REM_FLOAT,
327 Instruction::ADD_DOUBLE,
328 Instruction::SUB_DOUBLE,
329 Instruction::MUL_DOUBLE,
330 Instruction::DIV_DOUBLE,
331 Instruction::REM_DOUBLE,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100332 Instruction::ADD_INT_2ADDR,
333 Instruction::SUB_INT_2ADDR,
334 Instruction::MUL_INT_2ADDR,
335 Instruction::DIV_INT_2ADDR,
336 Instruction::REM_INT_2ADDR,
337 Instruction::AND_INT_2ADDR,
338 Instruction::OR_INT_2ADDR,
339 Instruction::XOR_INT_2ADDR,
340 Instruction::SHL_INT_2ADDR,
341 Instruction::SHR_INT_2ADDR,
342 Instruction::USHR_INT_2ADDR,
buzbee54ee4442014-06-19 15:04:12 -0700343 Instruction::ADD_LONG_2ADDR,
344 Instruction::SUB_LONG_2ADDR,
345 Instruction::MUL_LONG_2ADDR,
346 Instruction::DIV_LONG_2ADDR,
347 Instruction::REM_LONG_2ADDR,
348 Instruction::AND_LONG_2ADDR,
349 Instruction::OR_LONG_2ADDR,
350 Instruction::XOR_LONG_2ADDR,
351 Instruction::SHL_LONG_2ADDR,
352 Instruction::SHR_LONG_2ADDR,
353 Instruction::USHR_LONG_2ADDR,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100354 Instruction::ADD_FLOAT_2ADDR,
355 Instruction::SUB_FLOAT_2ADDR,
356 Instruction::MUL_FLOAT_2ADDR,
357 Instruction::DIV_FLOAT_2ADDR,
buzbee54ee4442014-06-19 15:04:12 -0700358 Instruction::REM_FLOAT_2ADDR,
359 Instruction::ADD_DOUBLE_2ADDR,
360 Instruction::SUB_DOUBLE_2ADDR,
361 Instruction::MUL_DOUBLE_2ADDR,
362 Instruction::DIV_DOUBLE_2ADDR,
363 Instruction::REM_DOUBLE_2ADDR,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100364 Instruction::ADD_INT_LIT16,
365 Instruction::RSUB_INT,
366 Instruction::MUL_INT_LIT16,
367 Instruction::DIV_INT_LIT16,
368 Instruction::REM_INT_LIT16,
369 Instruction::AND_INT_LIT16,
370 Instruction::OR_INT_LIT16,
371 Instruction::XOR_INT_LIT16,
372 Instruction::ADD_INT_LIT8,
373 Instruction::RSUB_INT_LIT8,
374 Instruction::MUL_INT_LIT8,
375 Instruction::DIV_INT_LIT8,
376 Instruction::REM_INT_LIT8,
377 Instruction::AND_INT_LIT8,
378 Instruction::OR_INT_LIT8,
379 Instruction::XOR_INT_LIT8,
380 Instruction::SHL_INT_LIT8,
381 Instruction::SHR_INT_LIT8,
382 Instruction::USHR_INT_LIT8,
buzbee54ee4442014-06-19 15:04:12 -0700383 Instruction::IGET_QUICK,
384 Instruction::IGET_WIDE_QUICK,
385 Instruction::IGET_OBJECT_QUICK,
386 Instruction::IPUT_QUICK,
387 Instruction::IPUT_WIDE_QUICK,
388 Instruction::IPUT_OBJECT_QUICK,
389 Instruction::INVOKE_VIRTUAL_QUICK,
390 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
391 Instruction::UNUSED_EB,
392 Instruction::UNUSED_EC,
393 Instruction::UNUSED_ED,
394 Instruction::UNUSED_EE,
395 Instruction::UNUSED_EF,
396 Instruction::UNUSED_F0,
397 Instruction::UNUSED_F1,
398 Instruction::UNUSED_F2,
399 Instruction::UNUSED_F3,
400 Instruction::UNUSED_F4,
401 Instruction::UNUSED_F5,
402 Instruction::UNUSED_F6,
403 Instruction::UNUSED_F7,
404 Instruction::UNUSED_F8,
405 Instruction::UNUSED_F9,
406 Instruction::UNUSED_FA,
407 Instruction::UNUSED_FB,
408 Instruction::UNUSED_FC,
409 Instruction::UNUSED_FD,
410 Instruction::UNUSED_FE,
411 Instruction::UNUSED_FF,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100412 // ----- ExtendedMIROpcode -----
413 kMirOpPhi,
414 kMirOpCopy,
415 kMirOpFusedCmplFloat,
416 kMirOpFusedCmpgFloat,
417 kMirOpFusedCmplDouble,
418 kMirOpFusedCmpgDouble,
419 kMirOpFusedCmpLong,
420 kMirOpNop,
421 kMirOpNullCheck,
422 kMirOpRangeCheck,
423 kMirOpDivZeroCheck,
424 kMirOpCheck,
425 kMirOpCheckPart2,
426 kMirOpSelect,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100427};
428
Andreas Gampe00caeed2014-07-10 01:43:08 -0700429// Unsupported opcodes. nullptr can be used when everything is supported. Size of the lists is
430// recorded below.
431static const int* kUnsupportedOpcodes[] = {
432 // 0 = kNone.
433 kAllOpcodes,
434 // 1 = kArm, unused (will use kThumb2).
435 kAllOpcodes,
436 // 2 = kArm64.
437 nullptr,
438 // 3 = kThumb2.
439 nullptr,
440 // 4 = kX86.
441 nullptr,
442 // 5 = kX86_64.
443 nullptr,
444 // 6 = kMips.
445 nullptr,
446 // 7 = kMips64.
447 kAllOpcodes
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700448};
Andreas Gampe00caeed2014-07-10 01:43:08 -0700449COMPILE_ASSERT(sizeof(kUnsupportedOpcodes) == 8 * sizeof(int*), kUnsupportedOpcodes_unexp);
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700450
Andreas Gampe00caeed2014-07-10 01:43:08 -0700451// Size of the arrays stored above.
452static const size_t kUnsupportedOpcodesSize[] = {
453 // 0 = kNone.
454 arraysize(kAllOpcodes),
455 // 1 = kArm, unused (will use kThumb2).
456 arraysize(kAllOpcodes),
457 // 2 = kArm64.
458 0,
459 // 3 = kThumb2.
460 0,
461 // 4 = kX86.
462 0,
463 // 5 = kX86_64.
464 0,
465 // 6 = kMips.
466 0,
467 // 7 = kMips64.
468 arraysize(kAllOpcodes),
469};
470COMPILE_ASSERT(sizeof(kUnsupportedOpcodesSize) == 8 * sizeof(size_t),
471 kUnsupportedOpcodesSize_unexp);
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700472
Andreas Gampe00caeed2014-07-10 01:43:08 -0700473CompilationUnit::CompilationUnit(ArenaPool* pool)
474 : compiler_driver(nullptr),
475 class_linker(nullptr),
476 dex_file(nullptr),
477 class_loader(nullptr),
478 class_def_idx(0),
479 method_idx(0),
480 code_item(nullptr),
481 access_flags(0),
482 invoke_type(kDirect),
483 shorty(nullptr),
484 disable_opt(0),
485 enable_debug(0),
486 verbose(false),
487 compiler(nullptr),
488 instruction_set(kNone),
489 target64(false),
490 num_dalvik_registers(0),
491 insns(nullptr),
492 num_ins(0),
493 num_outs(0),
494 num_regs(0),
495 compiler_flip_match(false),
496 arena(pool),
497 arena_stack(pool),
498 mir_graph(nullptr),
499 cg(nullptr),
500 timings("QuickCompiler", true, false),
501 print_pass(false) {
502}
503
504CompilationUnit::~CompilationUnit() {
505}
506
507void CompilationUnit::StartTimingSplit(const char* label) {
508 if (compiler_driver->GetDumpPasses()) {
509 timings.StartTiming(label);
510 }
511}
512
513void CompilationUnit::NewTimingSplit(const char* label) {
514 if (compiler_driver->GetDumpPasses()) {
515 timings.EndTiming();
516 timings.StartTiming(label);
517 }
518}
519
520void CompilationUnit::EndTiming() {
521 if (compiler_driver->GetDumpPasses()) {
522 timings.EndTiming();
523 if (enable_debug & (1 << kDebugTimings)) {
524 LOG(INFO) << "TIMINGS " << PrettyMethod(method_idx, *dex_file);
525 LOG(INFO) << Dumpable<TimingLogger>(timings);
526 }
527 }
528}
529
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700530static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
Andreas Gampe00caeed2014-07-10 01:43:08 -0700531 const char* supported_types = kSupportedTypes[instruction_set];
532 if (supported_types == nullptr) {
533 // Everything available.
534 return true;
535 }
536
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100537 uint32_t shorty_size = strlen(shorty);
538 CHECK_GE(shorty_size, 1u);
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700539
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100540 for (uint32_t i = 0; i < shorty_size; i++) {
541 if (strchr(supported_types, shorty[i]) == nullptr) {
542 return false;
543 }
544 }
545 return true;
546};
547
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100548// Skip the method that we do not support currently.
549static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
550 CompilationUnit& cu) {
Andreas Gampe00caeed2014-07-10 01:43:08 -0700551 // Check whether we do have limitations at all.
552 if (kSupportedTypes[cu.instruction_set] == nullptr &&
553 kUnsupportedOpcodesSize[cu.instruction_set] == 0U) {
554 return true;
555 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100556
Andreas Gampe00caeed2014-07-10 01:43:08 -0700557 // Check if we can compile the prototype.
558 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
559 if (!CanCompileShorty(shorty, cu.instruction_set)) {
560 VLOG(compiler) << "Unsupported shorty : " << shorty;
561 return false;
562 }
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700563
Andreas Gampe00caeed2014-07-10 01:43:08 -0700564 const int *unsupport_list = kUnsupportedOpcodes[cu.instruction_set];
565 int unsupport_list_size = kUnsupportedOpcodesSize[cu.instruction_set];
566
567 for (unsigned int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
568 BasicBlock* bb = cu.mir_graph->GetBasicBlock(idx);
569 if (bb == NULL) continue;
570 if (bb->block_type == kDead) continue;
571 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
572 int opcode = mir->dalvikInsn.opcode;
573 // Check if we support the byte code.
574 if (std::find(unsupport_list, unsupport_list + unsupport_list_size,
575 opcode) != unsupport_list + unsupport_list_size) {
Jean Christophe Beyler2ab40eb2014-06-02 09:03:14 -0700576 if (!MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
Andreas Gampe00caeed2014-07-10 01:43:08 -0700577 VLOG(compiler) << "Unsupported dalvik byte code : "
578 << mir->dalvikInsn.opcode;
579 } else {
580 VLOG(compiler) << "Unsupported extended MIR opcode : "
581 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100582 }
Andreas Gampe00caeed2014-07-10 01:43:08 -0700583 return false;
584 }
585 // Check if it invokes a prototype that we cannot support.
586 if (Instruction::INVOKE_VIRTUAL == opcode ||
587 Instruction::INVOKE_SUPER == opcode ||
588 Instruction::INVOKE_DIRECT == opcode ||
589 Instruction::INVOKE_STATIC == opcode ||
590 Instruction::INVOKE_INTERFACE == opcode) {
591 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
592 const char* invoke_method_shorty = dex_file.GetMethodShorty(
593 dex_file.GetMethodId(invoke_method_idx));
594 if (!CanCompileShorty(invoke_method_shorty, cu.instruction_set)) {
595 VLOG(compiler) << "Unsupported to invoke '"
596 << PrettyMethod(invoke_method_idx, dex_file)
597 << "' with shorty : " << invoke_method_shorty;
598 return false;
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100599 }
600 }
601 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100602 }
603 return true;
604}
605
Ian Rogers3d504072014-03-01 09:16:49 -0800606static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000607 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700608 const DexFile::CodeItem* code_item,
609 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700610 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000611 jobject class_loader, const DexFile& dex_file,
612 void* llvm_compilation_unit) {
Calin Juravle38a09042014-06-23 14:40:31 +0100613 std::string method_name = PrettyMethod(method_idx, dex_file);
614 VLOG(compiler) << "Compiling " << method_name << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700615 if (code_item->insns_size_in_code_units_ >= 0x10000) {
616 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
Calin Juravle38a09042014-06-23 14:40:31 +0100617 << " in " << method_name;
buzbeeb48819d2013-09-14 16:15:25 -0700618 return NULL;
619 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620
Jeff Hao4a200f52014-04-01 14:58:49 -0700621 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000622 return nullptr;
623 }
624
Brian Carlstrom7940e442013-07-12 13:46:57 -0700625 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800626 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627
Ian Rogers3d504072014-03-01 09:16:49 -0800628 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700629 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800630 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700631 if (cu.instruction_set == kArm) {
632 cu.instruction_set = kThumb2;
633 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700634 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000635 cu.compiler = compiler;
Andreas Gampe00caeed2014-07-10 01:43:08 -0700636 // TODO: Mips64 is not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700637 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100638 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700639 (cu.instruction_set == kX86) ||
640 (cu.instruction_set == kX86_64) ||
641 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642
Brian Carlstrom7940e442013-07-12 13:46:57 -0700643 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700644 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700646 cu.compiler_flip_match = false;
647 bool use_match = !cu.compiler_method_match.empty();
648 bool match = use_match && (cu.compiler_flip_match ^
Calin Juravle38a09042014-06-23 14:40:31 +0100649 (method_name.find(cu.compiler_method_match) != std::string::npos));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700650 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700651 cu.disable_opt = kCompilerOptimizerDisableFlags;
652 cu.enable_debug = kCompilerDebugFlags;
653 cu.verbose = VLOG_IS_ON(compiler) ||
654 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700655 }
656
Mingyao Yang42d65c52014-04-18 16:49:39 -0700657 if (gVerboseMethods.size() != 0) {
658 cu.verbose = false;
659 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
Calin Juravle38a09042014-06-23 14:40:31 +0100660 if (method_name.find(gVerboseMethods[i])
Mingyao Yang42d65c52014-04-18 16:49:39 -0700661 != std::string::npos) {
662 cu.verbose = true;
663 break;
664 }
665 }
666 }
667
buzbeeb01bf152014-05-13 15:59:07 -0700668 if (cu.verbose) {
669 cu.enable_debug |= (1 << kDebugCodegenDump);
670 }
671
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 /*
673 * TODO: rework handling of optimization and debug flags. Should we split out
674 * MIR and backend flags? Need command-line setting as well.
675 */
676
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000677 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700678
Andreas Gampe00caeed2014-07-10 01:43:08 -0700679 // Disable optimizations according to instruction set.
680 cu.disable_opt |= kDisabledOptimizationsPerISA[cu.instruction_set];
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100681
buzbeea61f4952013-08-23 14:27:06 -0700682 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700683 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700684
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800685 /*
686 * After creation of the MIR graph, also create the code generator.
687 * The reason we do this is that optimizations on the MIR graph may need to get information
688 * that is only available if a CG exists.
689 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000690 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800691
Brian Carlstrom7940e442013-07-12 13:46:57 -0700692 /* Gathering opcode stats? */
693 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700694 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700695 }
696
697 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700698 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700699 class_loader, dex_file);
700
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100701 if (!CanCompileMethod(method_idx, dex_file, cu)) {
Calin Juravle38a09042014-06-23 14:40:31 +0100702 VLOG(compiler) << cu.instruction_set << ": Cannot compile method : " << method_name;
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100703 return nullptr;
704 }
705
buzbee1da1e2f2013-11-15 13:37:01 -0800706 cu.NewTimingSplit("MIROpt:CheckFilters");
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700707 std::string skip_message;
708 if (cu.mir_graph->SkipCompilation(&skip_message)) {
709 VLOG(compiler) << cu.instruction_set << ": Skipping method : "
Calin Juravle38a09042014-06-23 14:40:31 +0100710 << method_name << " Reason = " << skip_message;
buzbee33ae5582014-06-12 14:56:32 -0700711 return nullptr;
buzbeeee17e0a2013-07-31 10:47:37 -0700712 }
713
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800714 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700715 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800716 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700717
Calin Juravle38a09042014-06-23 14:40:31 +0100718 /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */
719 if (cu.compiler_driver->ProfilePresent()
720 && !cu.mir_graph->MethodIsLeaf()
721 && cu.mir_graph->SkipCompilationByName(method_name)) {
722 return nullptr;
723 }
724
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700725 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
726 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727 }
728
729 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700730 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700731 }
732
buzbee1da1e2f2013-11-15 13:37:01 -0800733 /* Reassociate sreg names with original Dalvik vreg names. */
734 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700735
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000736 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
737 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
738 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
739 MemStats stack_stats(cu.arena_stack.GetPeakStats());
Calin Juravle38a09042014-06-23 14:40:31 +0100740 LOG(INFO) << method_name << " " << Dumpable<MemStats>(stack_stats);
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000741 }
742 }
743 cu.arena_stack.Reset();
744
Brian Carlstrom7940e442013-07-12 13:46:57 -0700745 CompiledMethod* result = NULL;
746
buzbee8c7a02a2014-06-14 12:33:09 -0700747 if (cu.mir_graph->PuntToInterpreter()) {
Calin Juravle38a09042014-06-23 14:40:31 +0100748 VLOG(compiler) << cu.instruction_set << ": Punted method to interpreter: " << method_name;
Andreas Gampe060e6fe2014-06-19 11:34:06 -0700749 return nullptr;
buzbee8c7a02a2014-06-14 12:33:09 -0700750 }
751
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700752 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700753
buzbee1da1e2f2013-11-15 13:37:01 -0800754 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700755 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800756 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700757
758 if (result) {
Calin Juravle38a09042014-06-23 14:40:31 +0100759 VLOG(compiler) << cu.instruction_set << ": Compiled " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700760 } else {
Calin Juravle38a09042014-06-23 14:40:31 +0100761 VLOG(compiler) << cu.instruction_set << ": Deferred " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700762 }
763
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700764 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000765 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000766 MemStats mem_stats(cu.arena.GetMemStats());
Calin Juravle38a09042014-06-23 14:40:31 +0100767 LOG(INFO) << method_name << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700768 }
769 }
770
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700771 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
772 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Calin Juravle38a09042014-06-23 14:40:31 +0100773 << " " << method_name;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700774 }
775
buzbeea61f4952013-08-23 14:27:06 -0700776 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800777 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700778 return result;
779}
780
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000781CompiledMethod* CompileOneMethod(CompilerDriver& driver,
782 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700783 const DexFile::CodeItem* code_item,
784 uint32_t access_flags,
785 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700786 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700787 uint32_t method_idx,
788 jobject class_loader,
789 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000790 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000791 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000792 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700793}
794
795} // namespace art
796
797extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000798 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700799 const art::DexFile::CodeItem* code_item,
800 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700801 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700802 const art::DexFile& dex_file) {
Andreas Gampe00caeed2014-07-10 01:43:08 -0700803 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use
804 // build default.
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000805 art::Compiler* compiler = driver.GetCompiler();
806 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700807 class_def_idx, method_idx, class_loader, dex_file,
808 NULL /* use thread llvm_info */);
809}