blob: 28c485a41c131a921653e098599ac60b75a047d9 [file] [log] [blame]
Andreas Gampe53c913b2014-08-12 23:19:23 -07001/*
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
17#include "quick_compiler.h"
18
19#include <cstdint>
20
Mathieu Chartiere401d142015-04-22 13:56:20 -070021#include "art_method-inl.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080022#include "base/dumpable.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080023#include "base/logging.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080024#include "base/macros.h"
25#include "base/timing_logger.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070026#include "compiler.h"
Elliott Hughes956af0f2014-12-11 14:34:28 -080027#include "dex_file-inl.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080028#include "dex_file_to_method_inliner_map.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080029#include "dex/compiler_ir.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080030#include "dex/dex_flags.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070031#include "dex/mir_graph.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080032#include "dex/pass_driver_me_opts.h"
Mathieu Chartier5bdab122015-01-26 18:30:19 -080033#include "dex/pass_driver_me_post_opt.h"
34#include "dex/pass_manager.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070035#include "dex/quick/mir_to_lir.h"
Nicolas Geoffray4824c272015-06-24 15:53:03 +010036#include "dex/verified_method.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070037#include "driver/compiler_driver.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080038#include "driver/compiler_options.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070039#include "elf_writer_quick.h"
40#include "jni/quick/jni_compiler.h"
Andreas Gampe9c462082015-01-27 14:31:40 -080041#include "mir_to_lir.h"
Andreas Gampe0e92f4f2015-01-26 17:37:27 -080042#include "mirror/object.h"
43#include "runtime.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070044
45// Specific compiler backends.
46#include "dex/quick/arm/backend_arm.h"
47#include "dex/quick/arm64/backend_arm64.h"
48#include "dex/quick/mips/backend_mips.h"
49#include "dex/quick/x86/backend_x86.h"
50
51namespace art {
52
Andreas Gampe785d2f22014-11-03 22:57:30 -080053static_assert(0U == static_cast<size_t>(kNone), "kNone not 0");
54static_assert(1U == static_cast<size_t>(kArm), "kArm not 1");
55static_assert(2U == static_cast<size_t>(kArm64), "kArm64 not 2");
56static_assert(3U == static_cast<size_t>(kThumb2), "kThumb2 not 3");
57static_assert(4U == static_cast<size_t>(kX86), "kX86 not 4");
58static_assert(5U == static_cast<size_t>(kX86_64), "kX86_64 not 5");
59static_assert(6U == static_cast<size_t>(kMips), "kMips not 6");
60static_assert(7U == static_cast<size_t>(kMips64), "kMips64 not 7");
Andreas Gampe53c913b2014-08-12 23:19:23 -070061
62// Additional disabled optimizations (over generally disabled) per instruction set.
63static constexpr uint32_t kDisabledOptimizationsPerISA[] = {
64 // 0 = kNone.
65 ~0U,
66 // 1 = kArm, unused (will use kThumb2).
67 ~0U,
68 // 2 = kArm64.
69 0,
70 // 3 = kThumb2.
71 0,
72 // 4 = kX86.
73 (1 << kLoadStoreElimination) |
74 0,
75 // 5 = kX86_64.
76 (1 << kLoadStoreElimination) |
77 0,
78 // 6 = kMips.
79 (1 << kLoadStoreElimination) |
80 (1 << kLoadHoisting) |
81 (1 << kSuppressLoads) |
82 (1 << kNullCheckElimination) |
83 (1 << kPromoteRegs) |
84 (1 << kTrackLiveTemps) |
85 (1 << kSafeOptimizations) |
86 (1 << kBBOpt) |
87 (1 << kMatch) |
88 (1 << kPromoteCompilerTemps) |
89 0,
90 // 7 = kMips64.
Maja Gagic6ea651f2015-02-24 16:55:04 +010091 (1 << kLoadStoreElimination) |
92 (1 << kLoadHoisting) |
93 (1 << kSuppressLoads) |
94 (1 << kNullCheckElimination) |
95 (1 << kPromoteRegs) |
96 (1 << kTrackLiveTemps) |
97 (1 << kSafeOptimizations) |
98 (1 << kBBOpt) |
99 (1 << kMatch) |
100 (1 << kPromoteCompilerTemps) |
101 0
Andreas Gampe53c913b2014-08-12 23:19:23 -0700102};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800103static_assert(sizeof(kDisabledOptimizationsPerISA) == 8 * sizeof(uint32_t),
104 "kDisabledOpts unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700105
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700106// Supported shorty types per instruction set. null means that all are available.
Andreas Gampe53c913b2014-08-12 23:19:23 -0700107// Z : boolean
108// B : byte
109// S : short
110// C : char
111// I : int
112// J : long
113// F : float
114// D : double
115// L : reference(object, array)
116// V : void
117static const char* kSupportedTypes[] = {
118 // 0 = kNone.
119 "",
120 // 1 = kArm, unused (will use kThumb2).
121 "",
122 // 2 = kArm64.
123 nullptr,
124 // 3 = kThumb2.
125 nullptr,
126 // 4 = kX86.
127 nullptr,
128 // 5 = kX86_64.
129 nullptr,
130 // 6 = kMips.
131 nullptr,
132 // 7 = kMips64.
Maja Gagic6ea651f2015-02-24 16:55:04 +0100133 nullptr
Andreas Gampe53c913b2014-08-12 23:19:23 -0700134};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800135static_assert(sizeof(kSupportedTypes) == 8 * sizeof(char*), "kSupportedTypes unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700136
137static int kAllOpcodes[] = {
138 Instruction::NOP,
139 Instruction::MOVE,
140 Instruction::MOVE_FROM16,
141 Instruction::MOVE_16,
142 Instruction::MOVE_WIDE,
143 Instruction::MOVE_WIDE_FROM16,
144 Instruction::MOVE_WIDE_16,
145 Instruction::MOVE_OBJECT,
146 Instruction::MOVE_OBJECT_FROM16,
147 Instruction::MOVE_OBJECT_16,
148 Instruction::MOVE_RESULT,
149 Instruction::MOVE_RESULT_WIDE,
150 Instruction::MOVE_RESULT_OBJECT,
151 Instruction::MOVE_EXCEPTION,
152 Instruction::RETURN_VOID,
153 Instruction::RETURN,
154 Instruction::RETURN_WIDE,
155 Instruction::RETURN_OBJECT,
156 Instruction::CONST_4,
157 Instruction::CONST_16,
158 Instruction::CONST,
159 Instruction::CONST_HIGH16,
160 Instruction::CONST_WIDE_16,
161 Instruction::CONST_WIDE_32,
162 Instruction::CONST_WIDE,
163 Instruction::CONST_WIDE_HIGH16,
164 Instruction::CONST_STRING,
165 Instruction::CONST_STRING_JUMBO,
166 Instruction::CONST_CLASS,
167 Instruction::MONITOR_ENTER,
168 Instruction::MONITOR_EXIT,
169 Instruction::CHECK_CAST,
170 Instruction::INSTANCE_OF,
171 Instruction::ARRAY_LENGTH,
172 Instruction::NEW_INSTANCE,
173 Instruction::NEW_ARRAY,
174 Instruction::FILLED_NEW_ARRAY,
175 Instruction::FILLED_NEW_ARRAY_RANGE,
176 Instruction::FILL_ARRAY_DATA,
177 Instruction::THROW,
178 Instruction::GOTO,
179 Instruction::GOTO_16,
180 Instruction::GOTO_32,
181 Instruction::PACKED_SWITCH,
182 Instruction::SPARSE_SWITCH,
183 Instruction::CMPL_FLOAT,
184 Instruction::CMPG_FLOAT,
185 Instruction::CMPL_DOUBLE,
186 Instruction::CMPG_DOUBLE,
187 Instruction::CMP_LONG,
188 Instruction::IF_EQ,
189 Instruction::IF_NE,
190 Instruction::IF_LT,
191 Instruction::IF_GE,
192 Instruction::IF_GT,
193 Instruction::IF_LE,
194 Instruction::IF_EQZ,
195 Instruction::IF_NEZ,
196 Instruction::IF_LTZ,
197 Instruction::IF_GEZ,
198 Instruction::IF_GTZ,
199 Instruction::IF_LEZ,
200 Instruction::UNUSED_3E,
201 Instruction::UNUSED_3F,
202 Instruction::UNUSED_40,
203 Instruction::UNUSED_41,
204 Instruction::UNUSED_42,
205 Instruction::UNUSED_43,
206 Instruction::AGET,
207 Instruction::AGET_WIDE,
208 Instruction::AGET_OBJECT,
209 Instruction::AGET_BOOLEAN,
210 Instruction::AGET_BYTE,
211 Instruction::AGET_CHAR,
212 Instruction::AGET_SHORT,
213 Instruction::APUT,
214 Instruction::APUT_WIDE,
215 Instruction::APUT_OBJECT,
216 Instruction::APUT_BOOLEAN,
217 Instruction::APUT_BYTE,
218 Instruction::APUT_CHAR,
219 Instruction::APUT_SHORT,
220 Instruction::IGET,
221 Instruction::IGET_WIDE,
222 Instruction::IGET_OBJECT,
223 Instruction::IGET_BOOLEAN,
224 Instruction::IGET_BYTE,
225 Instruction::IGET_CHAR,
226 Instruction::IGET_SHORT,
227 Instruction::IPUT,
228 Instruction::IPUT_WIDE,
229 Instruction::IPUT_OBJECT,
230 Instruction::IPUT_BOOLEAN,
231 Instruction::IPUT_BYTE,
232 Instruction::IPUT_CHAR,
233 Instruction::IPUT_SHORT,
234 Instruction::SGET,
235 Instruction::SGET_WIDE,
236 Instruction::SGET_OBJECT,
237 Instruction::SGET_BOOLEAN,
238 Instruction::SGET_BYTE,
239 Instruction::SGET_CHAR,
240 Instruction::SGET_SHORT,
241 Instruction::SPUT,
242 Instruction::SPUT_WIDE,
243 Instruction::SPUT_OBJECT,
244 Instruction::SPUT_BOOLEAN,
245 Instruction::SPUT_BYTE,
246 Instruction::SPUT_CHAR,
247 Instruction::SPUT_SHORT,
248 Instruction::INVOKE_VIRTUAL,
249 Instruction::INVOKE_SUPER,
250 Instruction::INVOKE_DIRECT,
251 Instruction::INVOKE_STATIC,
252 Instruction::INVOKE_INTERFACE,
Mathieu Chartierd7cbf8a2015-03-19 12:43:20 -0700253 Instruction::RETURN_VOID_NO_BARRIER,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700254 Instruction::INVOKE_VIRTUAL_RANGE,
255 Instruction::INVOKE_SUPER_RANGE,
256 Instruction::INVOKE_DIRECT_RANGE,
257 Instruction::INVOKE_STATIC_RANGE,
258 Instruction::INVOKE_INTERFACE_RANGE,
259 Instruction::UNUSED_79,
260 Instruction::UNUSED_7A,
261 Instruction::NEG_INT,
262 Instruction::NOT_INT,
263 Instruction::NEG_LONG,
264 Instruction::NOT_LONG,
265 Instruction::NEG_FLOAT,
266 Instruction::NEG_DOUBLE,
267 Instruction::INT_TO_LONG,
268 Instruction::INT_TO_FLOAT,
269 Instruction::INT_TO_DOUBLE,
270 Instruction::LONG_TO_INT,
271 Instruction::LONG_TO_FLOAT,
272 Instruction::LONG_TO_DOUBLE,
273 Instruction::FLOAT_TO_INT,
274 Instruction::FLOAT_TO_LONG,
275 Instruction::FLOAT_TO_DOUBLE,
276 Instruction::DOUBLE_TO_INT,
277 Instruction::DOUBLE_TO_LONG,
278 Instruction::DOUBLE_TO_FLOAT,
279 Instruction::INT_TO_BYTE,
280 Instruction::INT_TO_CHAR,
281 Instruction::INT_TO_SHORT,
282 Instruction::ADD_INT,
283 Instruction::SUB_INT,
284 Instruction::MUL_INT,
285 Instruction::DIV_INT,
286 Instruction::REM_INT,
287 Instruction::AND_INT,
288 Instruction::OR_INT,
289 Instruction::XOR_INT,
290 Instruction::SHL_INT,
291 Instruction::SHR_INT,
292 Instruction::USHR_INT,
293 Instruction::ADD_LONG,
294 Instruction::SUB_LONG,
295 Instruction::MUL_LONG,
296 Instruction::DIV_LONG,
297 Instruction::REM_LONG,
298 Instruction::AND_LONG,
299 Instruction::OR_LONG,
300 Instruction::XOR_LONG,
301 Instruction::SHL_LONG,
302 Instruction::SHR_LONG,
303 Instruction::USHR_LONG,
304 Instruction::ADD_FLOAT,
305 Instruction::SUB_FLOAT,
306 Instruction::MUL_FLOAT,
307 Instruction::DIV_FLOAT,
308 Instruction::REM_FLOAT,
309 Instruction::ADD_DOUBLE,
310 Instruction::SUB_DOUBLE,
311 Instruction::MUL_DOUBLE,
312 Instruction::DIV_DOUBLE,
313 Instruction::REM_DOUBLE,
314 Instruction::ADD_INT_2ADDR,
315 Instruction::SUB_INT_2ADDR,
316 Instruction::MUL_INT_2ADDR,
317 Instruction::DIV_INT_2ADDR,
318 Instruction::REM_INT_2ADDR,
319 Instruction::AND_INT_2ADDR,
320 Instruction::OR_INT_2ADDR,
321 Instruction::XOR_INT_2ADDR,
322 Instruction::SHL_INT_2ADDR,
323 Instruction::SHR_INT_2ADDR,
324 Instruction::USHR_INT_2ADDR,
325 Instruction::ADD_LONG_2ADDR,
326 Instruction::SUB_LONG_2ADDR,
327 Instruction::MUL_LONG_2ADDR,
328 Instruction::DIV_LONG_2ADDR,
329 Instruction::REM_LONG_2ADDR,
330 Instruction::AND_LONG_2ADDR,
331 Instruction::OR_LONG_2ADDR,
332 Instruction::XOR_LONG_2ADDR,
333 Instruction::SHL_LONG_2ADDR,
334 Instruction::SHR_LONG_2ADDR,
335 Instruction::USHR_LONG_2ADDR,
336 Instruction::ADD_FLOAT_2ADDR,
337 Instruction::SUB_FLOAT_2ADDR,
338 Instruction::MUL_FLOAT_2ADDR,
339 Instruction::DIV_FLOAT_2ADDR,
340 Instruction::REM_FLOAT_2ADDR,
341 Instruction::ADD_DOUBLE_2ADDR,
342 Instruction::SUB_DOUBLE_2ADDR,
343 Instruction::MUL_DOUBLE_2ADDR,
344 Instruction::DIV_DOUBLE_2ADDR,
345 Instruction::REM_DOUBLE_2ADDR,
346 Instruction::ADD_INT_LIT16,
347 Instruction::RSUB_INT,
348 Instruction::MUL_INT_LIT16,
349 Instruction::DIV_INT_LIT16,
350 Instruction::REM_INT_LIT16,
351 Instruction::AND_INT_LIT16,
352 Instruction::OR_INT_LIT16,
353 Instruction::XOR_INT_LIT16,
354 Instruction::ADD_INT_LIT8,
355 Instruction::RSUB_INT_LIT8,
356 Instruction::MUL_INT_LIT8,
357 Instruction::DIV_INT_LIT8,
358 Instruction::REM_INT_LIT8,
359 Instruction::AND_INT_LIT8,
360 Instruction::OR_INT_LIT8,
361 Instruction::XOR_INT_LIT8,
362 Instruction::SHL_INT_LIT8,
363 Instruction::SHR_INT_LIT8,
364 Instruction::USHR_INT_LIT8,
365 Instruction::IGET_QUICK,
366 Instruction::IGET_WIDE_QUICK,
367 Instruction::IGET_OBJECT_QUICK,
368 Instruction::IPUT_QUICK,
369 Instruction::IPUT_WIDE_QUICK,
370 Instruction::IPUT_OBJECT_QUICK,
371 Instruction::INVOKE_VIRTUAL_QUICK,
372 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
Fred Shih37f05ef2014-07-16 18:38:08 -0700373 Instruction::IPUT_BOOLEAN_QUICK,
374 Instruction::IPUT_BYTE_QUICK,
375 Instruction::IPUT_CHAR_QUICK,
376 Instruction::IPUT_SHORT_QUICK,
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800377 Instruction::IGET_BOOLEAN_QUICK,
378 Instruction::IGET_BYTE_QUICK,
379 Instruction::IGET_CHAR_QUICK,
380 Instruction::IGET_SHORT_QUICK,
Igor Murashkin158f35c2015-06-10 15:55:30 -0700381 Instruction::INVOKE_LAMBDA,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700382 Instruction::UNUSED_F4,
383 Instruction::UNUSED_F5,
Igor Murashkin158f35c2015-06-10 15:55:30 -0700384 Instruction::CREATE_LAMBDA,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700385 Instruction::UNUSED_F7,
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700386 Instruction::BOX_LAMBDA,
387 Instruction::UNBOX_LAMBDA,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700388 Instruction::UNUSED_FA,
389 Instruction::UNUSED_FB,
390 Instruction::UNUSED_FC,
391 Instruction::UNUSED_FD,
392 Instruction::UNUSED_FE,
393 Instruction::UNUSED_FF,
394 // ----- ExtendedMIROpcode -----
395 kMirOpPhi,
396 kMirOpCopy,
397 kMirOpFusedCmplFloat,
398 kMirOpFusedCmpgFloat,
399 kMirOpFusedCmplDouble,
400 kMirOpFusedCmpgDouble,
401 kMirOpFusedCmpLong,
402 kMirOpNop,
403 kMirOpNullCheck,
404 kMirOpRangeCheck,
405 kMirOpDivZeroCheck,
406 kMirOpCheck,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700407 kMirOpSelect,
408};
409
Zheng Xu5667fdb2014-10-23 18:29:55 +0800410static int kInvokeOpcodes[] = {
411 Instruction::INVOKE_VIRTUAL,
412 Instruction::INVOKE_SUPER,
413 Instruction::INVOKE_DIRECT,
414 Instruction::INVOKE_STATIC,
415 Instruction::INVOKE_INTERFACE,
416 Instruction::INVOKE_VIRTUAL_RANGE,
417 Instruction::INVOKE_SUPER_RANGE,
418 Instruction::INVOKE_DIRECT_RANGE,
419 Instruction::INVOKE_STATIC_RANGE,
420 Instruction::INVOKE_INTERFACE_RANGE,
421 Instruction::INVOKE_VIRTUAL_QUICK,
422 Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
423};
424
Igor Murashkin158f35c2015-06-10 15:55:30 -0700425// TODO: Add support for lambda opcodes to the quick compiler.
426static const int kUnsupportedLambdaOpcodes[] = {
427 Instruction::INVOKE_LAMBDA,
428 Instruction::CREATE_LAMBDA,
Igor Murashkin2ee54e22015-06-18 10:05:11 -0700429 Instruction::BOX_LAMBDA,
430 Instruction::UNBOX_LAMBDA,
Igor Murashkin158f35c2015-06-10 15:55:30 -0700431};
432
433// Unsupported opcodes. Null can be used when everything is supported. Size of the lists is
Andreas Gampe53c913b2014-08-12 23:19:23 -0700434// recorded below.
435static const int* kUnsupportedOpcodes[] = {
436 // 0 = kNone.
437 kAllOpcodes,
438 // 1 = kArm, unused (will use kThumb2).
439 kAllOpcodes,
440 // 2 = kArm64.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700441 kUnsupportedLambdaOpcodes,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700442 // 3 = kThumb2.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700443 kUnsupportedLambdaOpcodes,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700444 // 4 = kX86.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700445 kUnsupportedLambdaOpcodes,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700446 // 5 = kX86_64.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700447 kUnsupportedLambdaOpcodes,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700448 // 6 = kMips.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700449 kUnsupportedLambdaOpcodes,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700450 // 7 = kMips64.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700451 kUnsupportedLambdaOpcodes,
Andreas Gampe53c913b2014-08-12 23:19:23 -0700452};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800453static_assert(sizeof(kUnsupportedOpcodes) == 8 * sizeof(int*), "kUnsupportedOpcodes unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700454
455// Size of the arrays stored above.
456static const size_t kUnsupportedOpcodesSize[] = {
457 // 0 = kNone.
458 arraysize(kAllOpcodes),
459 // 1 = kArm, unused (will use kThumb2).
460 arraysize(kAllOpcodes),
461 // 2 = kArm64.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700462 arraysize(kUnsupportedLambdaOpcodes),
Andreas Gampe53c913b2014-08-12 23:19:23 -0700463 // 3 = kThumb2.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700464 arraysize(kUnsupportedLambdaOpcodes),
Andreas Gampe53c913b2014-08-12 23:19:23 -0700465 // 4 = kX86.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700466 arraysize(kUnsupportedLambdaOpcodes),
Andreas Gampe53c913b2014-08-12 23:19:23 -0700467 // 5 = kX86_64.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700468 arraysize(kUnsupportedLambdaOpcodes),
Andreas Gampe53c913b2014-08-12 23:19:23 -0700469 // 6 = kMips.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700470 arraysize(kUnsupportedLambdaOpcodes),
Andreas Gampe53c913b2014-08-12 23:19:23 -0700471 // 7 = kMips64.
Igor Murashkin158f35c2015-06-10 15:55:30 -0700472 arraysize(kUnsupportedLambdaOpcodes),
Andreas Gampe53c913b2014-08-12 23:19:23 -0700473};
Andreas Gampe785d2f22014-11-03 22:57:30 -0800474static_assert(sizeof(kUnsupportedOpcodesSize) == 8 * sizeof(size_t),
475 "kUnsupportedOpcodesSize unexpected");
Andreas Gampe53c913b2014-08-12 23:19:23 -0700476
Igor Murashkin158f35c2015-06-10 15:55:30 -0700477static bool IsUnsupportedExperimentalLambdasOnly(size_t i) {
478 DCHECK_LE(i, arraysize(kUnsupportedOpcodes));
479 return kUnsupportedOpcodes[i] == kUnsupportedLambdaOpcodes;
480}
481
Andreas Gampe53c913b2014-08-12 23:19:23 -0700482// The maximum amount of Dalvik register in a method for which we will start compiling. Tries to
483// avoid an abort when we need to manage more SSA registers than we can.
484static constexpr size_t kMaxAllowedDalvikRegisters = INT16_MAX / 2;
485
486static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
487 const char* supported_types = kSupportedTypes[instruction_set];
488 if (supported_types == nullptr) {
489 // Everything available.
490 return true;
491 }
492
493 uint32_t shorty_size = strlen(shorty);
494 CHECK_GE(shorty_size, 1u);
495
496 for (uint32_t i = 0; i < shorty_size; i++) {
497 if (strchr(supported_types, shorty[i]) == nullptr) {
498 return false;
499 }
500 }
501 return true;
Andreas Gampec8ccf682014-09-29 20:07:43 -0700502}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700503
Igor Murashkin158f35c2015-06-10 15:55:30 -0700504// If the ISA has unsupported opcodes, should we skip scanning over them?
505//
506// Most of the time we're compiling non-experimental files, so scanning just slows
507// performance down by as much as 6% with 4 threads.
508// In the rare cases we compile experimental opcodes, the runtime has an option to enable it,
509// which will force scanning for any unsupported opcodes.
510static bool SkipScanningUnsupportedOpcodes(InstructionSet instruction_set) {
511 if (UNLIKELY(kUnsupportedOpcodesSize[instruction_set] == 0U)) {
512 // All opcodes are supported no matter what. Usually not the case
513 // since experimental opcodes are not implemented in the quick compiler.
514 return true;
515 } else if (LIKELY(!Runtime::Current()->AreExperimentalLambdasEnabled())) {
516 // Experimental opcodes are disabled.
517 //
518 // If all unsupported opcodes are experimental we don't need to do scanning.
519 return IsUnsupportedExperimentalLambdasOnly(instruction_set);
520 } else {
521 // Experimental opcodes are enabled.
522 //
523 // Do the opcode scanning if the ISA has any unsupported opcodes.
524 return false;
525 }
526}
527
Andreas Gampe53c913b2014-08-12 23:19:23 -0700528// Skip the method that we do not support currently.
529bool QuickCompiler::CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
530 CompilationUnit* cu) const {
531 // This is a limitation in mir_graph. See MirGraph::SetNumSSARegs.
Razvan A Lupusoru8d0d03e2014-06-06 17:04:52 -0700532 if (cu->mir_graph->GetNumOfCodeAndTempVRs() > kMaxAllowedDalvikRegisters) {
533 VLOG(compiler) << "Too many dalvik registers : " << cu->mir_graph->GetNumOfCodeAndTempVRs();
Andreas Gampe53c913b2014-08-12 23:19:23 -0700534 return false;
535 }
536
537 // Check whether we do have limitations at all.
538 if (kSupportedTypes[cu->instruction_set] == nullptr &&
Igor Murashkin158f35c2015-06-10 15:55:30 -0700539 SkipScanningUnsupportedOpcodes(cu->instruction_set)) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700540 return true;
541 }
542
543 // Check if we can compile the prototype.
544 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
545 if (!CanCompileShorty(shorty, cu->instruction_set)) {
546 VLOG(compiler) << "Unsupported shorty : " << shorty;
547 return false;
548 }
549
550 const int *unsupport_list = kUnsupportedOpcodes[cu->instruction_set];
551 int unsupport_list_size = kUnsupportedOpcodesSize[cu->instruction_set];
552
553 for (unsigned int idx = 0; idx < cu->mir_graph->GetNumBlocks(); idx++) {
554 BasicBlock* bb = cu->mir_graph->GetBasicBlock(idx);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700555 if (bb == nullptr) continue;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700556 if (bb->block_type == kDead) continue;
557 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
558 int opcode = mir->dalvikInsn.opcode;
559 // Check if we support the byte code.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800560 if (std::find(unsupport_list, unsupport_list + unsupport_list_size, opcode)
561 != unsupport_list + unsupport_list_size) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700562 if (!MIR::DecodedInstruction::IsPseudoMirOp(opcode)) {
563 VLOG(compiler) << "Unsupported dalvik byte code : "
564 << mir->dalvikInsn.opcode;
565 } else {
566 VLOG(compiler) << "Unsupported extended MIR opcode : "
567 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
568 }
569 return false;
570 }
571 // Check if it invokes a prototype that we cannot support.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800572 if (std::find(kInvokeOpcodes, kInvokeOpcodes + arraysize(kInvokeOpcodes), opcode)
573 != kInvokeOpcodes + arraysize(kInvokeOpcodes)) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700574 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
575 const char* invoke_method_shorty = dex_file.GetMethodShorty(
576 dex_file.GetMethodId(invoke_method_idx));
577 if (!CanCompileShorty(invoke_method_shorty, cu->instruction_set)) {
578 VLOG(compiler) << "Unsupported to invoke '"
579 << PrettyMethod(invoke_method_idx, dex_file)
580 << "' with shorty : " << invoke_method_shorty;
581 return false;
582 }
583 }
584 }
585 }
586 return true;
587}
588
589void QuickCompiler::InitCompilationUnit(CompilationUnit& cu) const {
590 // Disable optimizations according to instruction set.
591 cu.disable_opt |= kDisabledOptimizationsPerISA[cu.instruction_set];
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800592 if (Runtime::Current()->UseJit()) {
593 // Disable these optimizations for JIT until quickened byte codes are done being implemented.
594 // TODO: Find a cleaner way to do this.
595 cu.disable_opt |= 1u << kLocalValueNumbering;
596 }
Andreas Gampe53c913b2014-08-12 23:19:23 -0700597}
598
David Brazdilee690a32014-12-01 17:04:16 +0000599void QuickCompiler::Init() {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700600 CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr);
601}
602
603void QuickCompiler::UnInit() const {
604 CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr);
605}
606
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800607/* Default optimizer/debug setting for the compiler. */
608static uint32_t kCompilerOptimizerDisableFlags = 0 | // Disable specific optimizations
609 // (1 << kLoadStoreElimination) |
610 // (1 << kLoadHoisting) |
611 // (1 << kSuppressLoads) |
612 // (1 << kNullCheckElimination) |
613 // (1 << kClassInitCheckElimination) |
614 // (1 << kGlobalValueNumbering) |
Vladimir Markoa5e69e82015-04-24 19:03:51 +0100615 // (1 << kGvnDeadCodeElimination) |
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800616 // (1 << kLocalValueNumbering) |
617 // (1 << kPromoteRegs) |
618 // (1 << kTrackLiveTemps) |
619 // (1 << kSafeOptimizations) |
620 // (1 << kBBOpt) |
621 // (1 << kSuspendCheckElimination) |
622 // (1 << kMatch) |
623 // (1 << kPromoteCompilerTemps) |
624 // (1 << kSuppressExceptionEdges) |
625 // (1 << kSuppressMethodInlining) |
626 0;
627
628static uint32_t kCompilerDebugFlags = 0 | // Enable debug/testing modes
629 // (1 << kDebugDisplayMissingTargets) |
630 // (1 << kDebugVerbose) |
631 // (1 << kDebugDumpCFG) |
632 // (1 << kDebugSlowFieldPath) |
633 // (1 << kDebugSlowInvokePath) |
634 // (1 << kDebugSlowStringPath) |
635 // (1 << kDebugSlowestFieldPath) |
636 // (1 << kDebugSlowestStringPath) |
637 // (1 << kDebugExerciseResolveMethod) |
638 // (1 << kDebugVerifyDataflow) |
639 // (1 << kDebugShowMemoryUsage) |
640 // (1 << kDebugShowNops) |
641 // (1 << kDebugCountOpcodes) |
642 // (1 << kDebugDumpCheckStats) |
643 // (1 << kDebugShowSummaryMemoryUsage) |
644 // (1 << kDebugShowFilterStats) |
645 // (1 << kDebugTimings) |
646 // (1 << kDebugCodegenDump) |
647 0;
648
Andreas Gampe53c913b2014-08-12 23:19:23 -0700649CompiledMethod* QuickCompiler::Compile(const DexFile::CodeItem* code_item,
650 uint32_t access_flags,
651 InvokeType invoke_type,
652 uint16_t class_def_idx,
653 uint32_t method_idx,
654 jobject class_loader,
655 const DexFile& dex_file) const {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700656 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use
657 // build default.
658 CompilerDriver* driver = GetCompilerDriver();
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800659
660 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
661 if (Compiler::IsPathologicalCase(*code_item, method_idx, dex_file)) {
662 return nullptr;
663 }
664
Nicolas Geoffray4824c272015-06-24 15:53:03 +0100665 if (driver->GetVerifiedMethod(&dex_file, method_idx)->HasRuntimeThrow()) {
666 return nullptr;
667 }
668
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800669 DCHECK(driver->GetCompilerOptions().IsCompilationEnabled());
670
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700671 Runtime* const runtime = Runtime::Current();
672 ClassLinker* const class_linker = runtime->GetClassLinker();
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800673 InstructionSet instruction_set = driver->GetInstructionSet();
674 if (instruction_set == kArm) {
675 instruction_set = kThumb2;
676 }
Mathieu Chartier9b34b242015-03-09 11:30:17 -0700677 CompilationUnit cu(runtime->GetArenaPool(), instruction_set, driver, class_linker);
Vladimir Marko20f85592015-03-19 10:07:02 +0000678 cu.dex_file = &dex_file;
679 cu.class_def_idx = class_def_idx;
680 cu.method_idx = method_idx;
681 cu.access_flags = access_flags;
682 cu.invoke_type = invoke_type;
683 cu.shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800684
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800685 CHECK((cu.instruction_set == kThumb2) ||
686 (cu.instruction_set == kArm64) ||
687 (cu.instruction_set == kX86) ||
688 (cu.instruction_set == kX86_64) ||
Maja Gagic6ea651f2015-02-24 16:55:04 +0100689 (cu.instruction_set == kMips) ||
690 (cu.instruction_set == kMips64));
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800691
692 // TODO: set this from command line
693 constexpr bool compiler_flip_match = false;
694 const std::string compiler_method_match = "";
695
696 bool use_match = !compiler_method_match.empty();
697 bool match = use_match && (compiler_flip_match ^
698 (PrettyMethod(method_idx, dex_file).find(compiler_method_match) != std::string::npos));
699 if (!use_match || match) {
700 cu.disable_opt = kCompilerOptimizerDisableFlags;
701 cu.enable_debug = kCompilerDebugFlags;
702 cu.verbose = VLOG_IS_ON(compiler) ||
703 (cu.enable_debug & (1 << kDebugVerbose));
704 }
705
706 if (driver->GetCompilerOptions().HasVerboseMethods()) {
707 cu.verbose = driver->GetCompilerOptions().IsVerboseMethod(PrettyMethod(method_idx, dex_file));
708 }
709
710 if (cu.verbose) {
711 cu.enable_debug |= (1 << kDebugCodegenDump);
712 }
713
714 /*
715 * TODO: rework handling of optimization and debug flags. Should we split out
716 * MIR and backend flags? Need command-line setting as well.
717 */
718
719 InitCompilationUnit(cu);
720
721 cu.StartTimingSplit("BuildMIRGraph");
722 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
723
724 /*
725 * After creation of the MIR graph, also create the code generator.
726 * The reason we do this is that optimizations on the MIR graph may need to get information
727 * that is only available if a CG exists.
728 */
729 cu.cg.reset(GetCodeGenerator(&cu, nullptr));
730
731 /* Gathering opcode stats? */
732 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
733 cu.mir_graph->EnableOpcodeCounting();
734 }
735
736 /* Build the raw MIR graph */
737 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
738 class_loader, dex_file);
739
740 if (!CanCompileMethod(method_idx, dex_file, &cu)) {
741 VLOG(compiler) << cu.instruction_set << ": Cannot compile method : "
742 << PrettyMethod(method_idx, dex_file);
743 cu.EndTiming();
744 return nullptr;
745 }
746
747 cu.NewTimingSplit("MIROpt:CheckFilters");
748 std::string skip_message;
749 if (cu.mir_graph->SkipCompilation(&skip_message)) {
750 VLOG(compiler) << cu.instruction_set << ": Skipping method : "
751 << PrettyMethod(method_idx, dex_file) << " Reason = " << skip_message;
752 cu.EndTiming();
753 return nullptr;
754 }
755
756 /* Create the pass driver and launch it */
Nicolas Geoffray216eaa22015-03-17 17:09:30 +0000757 PassDriverMEOpts pass_driver(GetPreOptPassManager(), GetPostOptPassManager(), &cu);
Andreas Gampe0e92f4f2015-01-26 17:37:27 -0800758 pass_driver.Launch();
759
760 /* For non-leaf methods check if we should skip compilation when the profiler is enabled. */
761 if (cu.compiler_driver->ProfilePresent()
762 && !cu.mir_graph->MethodIsLeaf()
763 && cu.mir_graph->SkipCompilationByName(PrettyMethod(method_idx, dex_file))) {
764 cu.EndTiming();
765 return nullptr;
766 }
767
768 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
769 cu.mir_graph->DumpCheckStats();
770 }
771
772 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
773 cu.mir_graph->ShowOpcodeStats();
774 }
775
776 /* Reassociate sreg names with original Dalvik vreg names. */
777 cu.mir_graph->RemapRegLocations();
778
779 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
780 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
781 if (cu.arena_stack.PeakBytesAllocated() > 1 * 1024 * 1024) {
782 MemStats stack_stats(cu.arena_stack.GetPeakStats());
783 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
784 }
785 }
786 cu.arena_stack.Reset();
787
788 CompiledMethod* result = nullptr;
789
790 if (cu.mir_graph->PuntToInterpreter()) {
791 VLOG(compiler) << cu.instruction_set << ": Punted method to interpreter: "
792 << PrettyMethod(method_idx, dex_file);
793 cu.EndTiming();
794 return nullptr;
795 }
796
797 cu.cg->Materialize();
798
799 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
800 result = cu.cg->GetCompiledMethod();
801 cu.NewTimingSplit("Cleanup");
802
803 if (result) {
804 VLOG(compiler) << cu.instruction_set << ": Compiled " << PrettyMethod(method_idx, dex_file);
805 } else {
806 VLOG(compiler) << cu.instruction_set << ": Deferred " << PrettyMethod(method_idx, dex_file);
807 }
808
809 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
810 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
811 MemStats mem_stats(cu.arena.GetMemStats());
812 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
813 }
814 }
815
816 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
817 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
818 << " " << PrettyMethod(method_idx, dex_file);
819 }
820
821 cu.EndTiming();
822 driver->GetTimingsLogger()->AddLogger(cu.timings);
823 return result;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700824}
825
826CompiledMethod* QuickCompiler::JniCompile(uint32_t access_flags,
827 uint32_t method_idx,
828 const DexFile& dex_file) const {
829 return ArtQuickJniCompileMethod(GetCompilerDriver(), access_flags, method_idx, dex_file);
830}
831
Mathieu Chartiere401d142015-04-22 13:56:20 -0700832uintptr_t QuickCompiler::GetEntryPointOf(ArtMethod* method) const {
Mathieu Chartier130914e2014-11-18 15:34:09 -0800833 return reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCodePtrSize(
834 InstructionSetPointerSize(GetCompilerDriver()->GetInstructionSet())));
Andreas Gampe53c913b2014-08-12 23:19:23 -0700835}
836
David Srbecky1109fb32015-04-07 20:21:06 +0100837Mir2Lir* QuickCompiler::GetCodeGenerator(CompilationUnit* cu, void* compilation_unit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700838 UNUSED(compilation_unit);
Andreas Gampe53c913b2014-08-12 23:19:23 -0700839 Mir2Lir* mir_to_lir = nullptr;
840 switch (cu->instruction_set) {
841 case kThumb2:
842 mir_to_lir = ArmCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
843 break;
844 case kArm64:
845 mir_to_lir = Arm64CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
846 break;
847 case kMips:
Goran Jakovljevic10957932015-03-24 18:42:56 +0100848 // Fall-through.
Maja Gagic6ea651f2015-02-24 16:55:04 +0100849 case kMips64:
Goran Jakovljevic10957932015-03-24 18:42:56 +0100850 mir_to_lir = MipsCodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
Maja Gagic6ea651f2015-02-24 16:55:04 +0100851 break;
Andreas Gampe53c913b2014-08-12 23:19:23 -0700852 case kX86:
853 // Fall-through.
854 case kX86_64:
855 mir_to_lir = X86CodeGenerator(cu, cu->mir_graph.get(), &cu->arena);
856 break;
857 default:
858 LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
859 }
860
861 /* The number of compiler temporaries depends on backend so set it up now if possible */
862 if (mir_to_lir) {
863 size_t max_temps = mir_to_lir->GetMaxPossibleCompilerTemps();
864 bool set_max = cu->mir_graph->SetMaxAvailableNonSpecialCompilerTemps(max_temps);
865 CHECK(set_max);
866 }
867 return mir_to_lir;
868}
869
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800870QuickCompiler::QuickCompiler(CompilerDriver* driver) : Compiler(driver, 100) {
871 const auto& compiler_options = driver->GetCompilerOptions();
872 auto* pass_manager_options = compiler_options.GetPassManagerOptions();
873 pre_opt_pass_manager_.reset(new PassManager(*pass_manager_options));
874 CHECK(pre_opt_pass_manager_.get() != nullptr);
875 PassDriverMEOpts::SetupPasses(pre_opt_pass_manager_.get());
876 pre_opt_pass_manager_->CreateDefaultPassList();
877 if (pass_manager_options->GetPrintPassOptions()) {
878 PassDriverMEOpts::PrintPassOptions(pre_opt_pass_manager_.get());
879 }
880 // TODO: Different options for pre vs post opts?
881 post_opt_pass_manager_.reset(new PassManager(PassManagerOptions()));
882 CHECK(post_opt_pass_manager_.get() != nullptr);
883 PassDriverMEPostOpt::SetupPasses(post_opt_pass_manager_.get());
884 post_opt_pass_manager_->CreateDefaultPassList();
885 if (pass_manager_options->GetPrintPassOptions()) {
886 PassDriverMEPostOpt::PrintPassOptions(post_opt_pass_manager_.get());
887 }
888}
889
890QuickCompiler::~QuickCompiler() {
891}
Andreas Gampe53c913b2014-08-12 23:19:23 -0700892
893Compiler* CreateQuickCompiler(CompilerDriver* driver) {
Mathieu Chartier5bdab122015-01-26 18:30:19 -0800894 return QuickCompiler::Create(driver);
895}
896
897Compiler* QuickCompiler::Create(CompilerDriver* driver) {
Andreas Gampe53c913b2014-08-12 23:19:23 -0700898 return new QuickCompiler(driver);
899}
900
901} // namespace art