blob: 8218cf12e83d0dc694fd21393e82839e1aa65262 [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,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100150 Instruction::RETURN_OBJECT,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100151 Instruction::CONST_4,
152 Instruction::CONST_16,
153 Instruction::CONST,
154 Instruction::CONST_STRING,
155 Instruction::MONITOR_ENTER,
156 Instruction::MONITOR_EXIT,
157 Instruction::THROW,
158 Instruction::GOTO,
159 Instruction::GOTO_16,
160 Instruction::GOTO_32,
Matteo Franchin5acc8b02014-06-05 15:10:35 +0100161 Instruction::PACKED_SWITCH,
162 Instruction::SPARSE_SWITCH,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100163 Instruction::IF_EQ,
164 Instruction::IF_NE,
165 Instruction::IF_LT,
166 Instruction::IF_GE,
167 Instruction::IF_GT,
168 Instruction::IF_LE,
169 Instruction::IF_EQZ,
170 Instruction::IF_NEZ,
171 Instruction::IF_LTZ,
172 Instruction::IF_GEZ,
173 Instruction::IF_GTZ,
174 Instruction::IF_LEZ,
175 Instruction::NEG_INT,
176 Instruction::NOT_INT,
177 Instruction::NEG_FLOAT,
178 Instruction::INT_TO_BYTE,
179 Instruction::INT_TO_CHAR,
180 Instruction::INT_TO_SHORT,
181 Instruction::ADD_INT,
182 Instruction::SUB_INT,
183 Instruction::MUL_INT,
184 Instruction::DIV_INT,
185 Instruction::REM_INT,
186 Instruction::AND_INT,
187 Instruction::OR_INT,
188 Instruction::XOR_INT,
189 Instruction::SHL_INT,
190 Instruction::SHR_INT,
191 Instruction::USHR_INT,
192 Instruction::ADD_FLOAT,
193 Instruction::SUB_FLOAT,
194 Instruction::MUL_FLOAT,
195 Instruction::DIV_FLOAT,
196 Instruction::ADD_INT_2ADDR,
197 Instruction::SUB_INT_2ADDR,
198 Instruction::MUL_INT_2ADDR,
199 Instruction::DIV_INT_2ADDR,
200 Instruction::REM_INT_2ADDR,
201 Instruction::AND_INT_2ADDR,
202 Instruction::OR_INT_2ADDR,
203 Instruction::XOR_INT_2ADDR,
204 Instruction::SHL_INT_2ADDR,
205 Instruction::SHR_INT_2ADDR,
206 Instruction::USHR_INT_2ADDR,
207 Instruction::ADD_FLOAT_2ADDR,
208 Instruction::SUB_FLOAT_2ADDR,
209 Instruction::MUL_FLOAT_2ADDR,
210 Instruction::DIV_FLOAT_2ADDR,
211 Instruction::ADD_INT_LIT16,
212 Instruction::RSUB_INT,
213 Instruction::MUL_INT_LIT16,
214 Instruction::DIV_INT_LIT16,
215 Instruction::REM_INT_LIT16,
216 Instruction::AND_INT_LIT16,
217 Instruction::OR_INT_LIT16,
218 Instruction::XOR_INT_LIT16,
219 Instruction::ADD_INT_LIT8,
220 Instruction::RSUB_INT_LIT8,
221 Instruction::MUL_INT_LIT8,
222 Instruction::DIV_INT_LIT8,
223 Instruction::REM_INT_LIT8,
224 Instruction::AND_INT_LIT8,
225 Instruction::OR_INT_LIT8,
226 Instruction::XOR_INT_LIT8,
227 Instruction::SHL_INT_LIT8,
228 Instruction::SHR_INT_LIT8,
229 Instruction::USHR_INT_LIT8,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100230 Instruction::SGET,
231 Instruction::SGET_BOOLEAN,
232 Instruction::SGET_BYTE,
233 Instruction::SGET_CHAR,
234 Instruction::SGET_SHORT,
235 Instruction::SGET_OBJECT,
236 Instruction::SPUT,
237 Instruction::SPUT_OBJECT,
238 Instruction::SPUT_BOOLEAN,
239 Instruction::SPUT_BYTE,
240 Instruction::SPUT_CHAR,
241 Instruction::SPUT_SHORT,
242 Instruction::MOVE_WIDE,
243 Instruction::MOVE_WIDE_FROM16,
244 Instruction::MOVE_WIDE_16,
245 Instruction::MOVE_OBJECT,
246 Instruction::MOVE_OBJECT_FROM16,
247 Instruction::MOVE_OBJECT_16,
248 Instruction::CMPL_FLOAT,
249 Instruction::CMPG_FLOAT,
250 Instruction::IGET,
251 Instruction::IGET_OBJECT,
252 Instruction::IGET_BOOLEAN,
253 Instruction::IGET_BYTE,
254 Instruction::IGET_CHAR,
255 Instruction::IGET_SHORT,
256 Instruction::IPUT,
257 Instruction::IPUT_OBJECT,
258 Instruction::IPUT_BOOLEAN,
259 Instruction::IPUT_BYTE,
260 Instruction::IPUT_CHAR,
261 Instruction::IPUT_SHORT,
262
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100263 // TODO(Arm64): Enable compiler pass
264 // ----- ExtendedMIROpcode -----
265 kMirOpPhi,
266 kMirOpCopy,
267 kMirOpFusedCmplFloat,
268 kMirOpFusedCmpgFloat,
269 kMirOpFusedCmplDouble,
270 kMirOpFusedCmpgDouble,
271 kMirOpFusedCmpLong,
272 kMirOpNop,
273 kMirOpNullCheck,
274 kMirOpRangeCheck,
275 kMirOpDivZeroCheck,
276 kMirOpCheck,
277 kMirOpCheckPart2,
278 kMirOpSelect,
279
280#if ARM64_USE_EXPERIMENTAL_OPCODES
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100281 // Instruction::MOVE_RESULT,
282 // Instruction::MOVE_RESULT_WIDE,
283 // Instruction::MOVE_RESULT_OBJECT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100284 // Instruction::CONST_HIGH16,
285 // Instruction::CONST_WIDE_16,
286 // Instruction::CONST_WIDE_32,
287 // Instruction::CONST_WIDE,
288 // Instruction::CONST_WIDE_HIGH16,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100289 // Instruction::CONST_STRING_JUMBO,
290 // Instruction::CONST_CLASS,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100291 // Instruction::CHECK_CAST,
292 // Instruction::INSTANCE_OF,
293 // Instruction::ARRAY_LENGTH,
294 // Instruction::NEW_INSTANCE,
295 // Instruction::NEW_ARRAY,
296 // Instruction::FILLED_NEW_ARRAY,
297 // Instruction::FILLED_NEW_ARRAY_RANGE,
298 // Instruction::FILL_ARRAY_DATA,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100299 Instruction::CMPL_DOUBLE,
300 Instruction::CMPG_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100301 Instruction::CMP_LONG,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100302 // Instruction::UNUSED_3E,
303 // Instruction::UNUSED_3F,
304 // Instruction::UNUSED_40,
305 // Instruction::UNUSED_41,
306 // Instruction::UNUSED_42,
307 // Instruction::UNUSED_43,
308 // Instruction::AGET,
309 // Instruction::AGET_WIDE,
310 // Instruction::AGET_OBJECT,
311 // Instruction::AGET_BOOLEAN,
312 // Instruction::AGET_BYTE,
313 // Instruction::AGET_CHAR,
314 // Instruction::AGET_SHORT,
315 // Instruction::APUT,
316 // Instruction::APUT_WIDE,
317 // Instruction::APUT_OBJECT,
318 // Instruction::APUT_BOOLEAN,
319 // Instruction::APUT_BYTE,
320 // Instruction::APUT_CHAR,
321 // Instruction::APUT_SHORT,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100322 // Instruction::IPUT_WIDE,
Matteo Franchinfd2e2912014-06-06 10:09:56 +0100323 // Instruction::IGET_WIDE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100324 // Instruction::SGET_WIDE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100325 // Instruction::SPUT_WIDE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100326 Instruction::INVOKE_VIRTUAL,
327 Instruction::INVOKE_SUPER,
328 Instruction::INVOKE_DIRECT,
329 Instruction::INVOKE_STATIC,
330 Instruction::INVOKE_INTERFACE,
331 // Instruction::RETURN_VOID_BARRIER,
332 // Instruction::INVOKE_VIRTUAL_RANGE,
333 // Instruction::INVOKE_SUPER_RANGE,
334 // Instruction::INVOKE_DIRECT_RANGE,
335 // Instruction::INVOKE_STATIC_RANGE,
336 // Instruction::INVOKE_INTERFACE_RANGE,
337 // Instruction::UNUSED_79,
338 // Instruction::UNUSED_7A,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100339 Instruction::NEG_LONG,
340 Instruction::NOT_LONG,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100341 Instruction::NEG_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100342 Instruction::INT_TO_LONG,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100343 Instruction::INT_TO_FLOAT,
344 Instruction::INT_TO_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100345 Instruction::LONG_TO_INT,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100346 Instruction::LONG_TO_FLOAT,
347 Instruction::LONG_TO_DOUBLE,
348 Instruction::FLOAT_TO_INT,
349 Instruction::FLOAT_TO_LONG,
350 Instruction::FLOAT_TO_DOUBLE,
351 Instruction::DOUBLE_TO_INT,
352 Instruction::DOUBLE_TO_LONG,
353 Instruction::DOUBLE_TO_FLOAT,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100354 Instruction::ADD_LONG,
355 Instruction::SUB_LONG,
356 Instruction::MUL_LONG,
357 Instruction::DIV_LONG,
358 Instruction::REM_LONG,
359 Instruction::AND_LONG,
360 Instruction::OR_LONG,
361 Instruction::XOR_LONG,
362 Instruction::SHL_LONG,
363 Instruction::SHR_LONG,
364 Instruction::USHR_LONG,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100365 // Instruction::REM_FLOAT,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100366 Instruction::ADD_DOUBLE,
367 Instruction::SUB_DOUBLE,
368 Instruction::MUL_DOUBLE,
369 Instruction::DIV_DOUBLE,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100370 // Instruction::REM_DOUBLE,
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100371 Instruction::ADD_LONG_2ADDR,
372 Instruction::SUB_LONG_2ADDR,
373 Instruction::MUL_LONG_2ADDR,
374 Instruction::DIV_LONG_2ADDR,
375 Instruction::REM_LONG_2ADDR,
376 Instruction::AND_LONG_2ADDR,
377 Instruction::OR_LONG_2ADDR,
378 Instruction::XOR_LONG_2ADDR,
379 Instruction::SHL_LONG_2ADDR,
380 Instruction::SHR_LONG_2ADDR,
381 Instruction::USHR_LONG_2ADDR,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100382 // Instruction::REM_FLOAT_2ADDR,
Serban Constantinescu032d3772014-05-23 17:38:18 +0100383 Instruction::ADD_DOUBLE_2ADDR,
384 Instruction::SUB_DOUBLE_2ADDR,
385 Instruction::MUL_DOUBLE_2ADDR,
386 Instruction::DIV_DOUBLE_2ADDR,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100387 // Instruction::REM_DOUBLE_2ADDR,
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100388 // Instruction::IGET_QUICK,
389 // Instruction::IGET_WIDE_QUICK,
390 // Instruction::IGET_OBJECT_QUICK,
391 // Instruction::IPUT_QUICK,
392 // Instruction::IPUT_WIDE_QUICK,
393 // Instruction::IPUT_OBJECT_QUICK,
394 // Instruction::INVOKE_VIRTUAL_QUICK,
395 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
396 // Instruction::UNUSED_EB,
397 // Instruction::UNUSED_EC,
398 // Instruction::UNUSED_ED,
399 // Instruction::UNUSED_EE,
400 // Instruction::UNUSED_EF,
401 // Instruction::UNUSED_F0,
402 // Instruction::UNUSED_F1,
403 // Instruction::UNUSED_F2,
404 // Instruction::UNUSED_F3,
405 // Instruction::UNUSED_F4,
406 // Instruction::UNUSED_F5,
407 // Instruction::UNUSED_F6,
408 // Instruction::UNUSED_F7,
409 // Instruction::UNUSED_F8,
410 // Instruction::UNUSED_F9,
411 // Instruction::UNUSED_FA,
412 // Instruction::UNUSED_FB,
413 // Instruction::UNUSED_FC,
414 // Instruction::UNUSED_FD,
415 // Instruction::UNUSED_FE,
416 // Instruction::UNUSED_FF,
Matteo Franchinec3f3d12014-05-29 14:24:10 +0100417#endif /* ARM64_USE_EXPERIMENTAL_OPCODES */
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100418};
419
420// TODO: Remove this when we are able to compile everything.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700421int x86_64_support_list[] = {
422 Instruction::NOP,
423 // Instruction::MOVE,
424 // Instruction::MOVE_FROM16,
425 // Instruction::MOVE_16,
426 // Instruction::MOVE_WIDE,
427 // Instruction::MOVE_WIDE_FROM16,
428 // Instruction::MOVE_WIDE_16,
429 // Instruction::MOVE_OBJECT,
430 // Instruction::MOVE_OBJECT_FROM16,
431 // Instruction::MOVE_OBJECT_16,
432 // Instruction::MOVE_RESULT,
433 // Instruction::MOVE_RESULT_WIDE,
434 // Instruction::MOVE_RESULT_OBJECT,
435 // Instruction::MOVE_EXCEPTION,
436 Instruction::RETURN_VOID,
437 Instruction::RETURN,
438 // Instruction::RETURN_WIDE,
439 Instruction::RETURN_OBJECT,
440 // Instruction::CONST_4,
441 // Instruction::CONST_16,
442 // Instruction::CONST,
443 // Instruction::CONST_HIGH16,
444 // Instruction::CONST_WIDE_16,
445 // Instruction::CONST_WIDE_32,
446 // Instruction::CONST_WIDE,
447 // Instruction::CONST_WIDE_HIGH16,
448 // Instruction::CONST_STRING,
449 // Instruction::CONST_STRING_JUMBO,
450 // Instruction::CONST_CLASS,
451 // Instruction::MONITOR_ENTER,
452 // Instruction::MONITOR_EXIT,
453 // Instruction::CHECK_CAST,
454 // Instruction::INSTANCE_OF,
455 // Instruction::ARRAY_LENGTH,
456 // Instruction::NEW_INSTANCE,
457 // Instruction::NEW_ARRAY,
458 // Instruction::FILLED_NEW_ARRAY,
459 // Instruction::FILLED_NEW_ARRAY_RANGE,
460 // Instruction::FILL_ARRAY_DATA,
461 // Instruction::THROW,
462 // Instruction::GOTO,
463 // Instruction::GOTO_16,
464 // Instruction::GOTO_32,
465 // Instruction::PACKED_SWITCH,
466 // Instruction::SPARSE_SWITCH,
467 // Instruction::CMPL_FLOAT,
468 // Instruction::CMPG_FLOAT,
469 // Instruction::CMPL_DOUBLE,
470 // Instruction::CMPG_DOUBLE,
471 // Instruction::CMP_LONG,
472 // Instruction::IF_EQ,
473 // Instruction::IF_NE,
474 // Instruction::IF_LT,
475 // Instruction::IF_GE,
476 // Instruction::IF_GT,
477 // Instruction::IF_LE,
478 // Instruction::IF_EQZ,
479 // Instruction::IF_NEZ,
480 // Instruction::IF_LTZ,
481 // Instruction::IF_GEZ,
482 // Instruction::IF_GTZ,
483 // Instruction::IF_LEZ,
484 // Instruction::UNUSED_3E,
485 // Instruction::UNUSED_3F,
486 // Instruction::UNUSED_40,
487 // Instruction::UNUSED_41,
488 // Instruction::UNUSED_42,
489 // Instruction::UNUSED_43,
490 // Instruction::AGET,
491 // Instruction::AGET_WIDE,
492 // Instruction::AGET_OBJECT,
493 // Instruction::AGET_BOOLEAN,
494 // Instruction::AGET_BYTE,
495 // Instruction::AGET_CHAR,
496 // Instruction::AGET_SHORT,
497 // Instruction::APUT,
498 // Instruction::APUT_WIDE,
499 // Instruction::APUT_OBJECT,
500 // Instruction::APUT_BOOLEAN,
501 // Instruction::APUT_BYTE,
502 // Instruction::APUT_CHAR,
503 // Instruction::APUT_SHORT,
504 // Instruction::IGET,
505 // Instruction::IGET_WIDE,
506 // Instruction::IGET_OBJECT,
507 // Instruction::IGET_BOOLEAN,
508 // Instruction::IGET_BYTE,
509 // Instruction::IGET_CHAR,
510 // Instruction::IGET_SHORT,
511 // Instruction::IPUT,
512 // Instruction::IPUT_WIDE,
513 // Instruction::IPUT_OBJECT,
514 // Instruction::IPUT_BOOLEAN,
515 // Instruction::IPUT_BYTE,
516 // Instruction::IPUT_CHAR,
517 // Instruction::IPUT_SHORT,
518 Instruction::SGET,
519 // Instruction::SGET_WIDE,
520 Instruction::SGET_OBJECT,
521 Instruction::SGET_BOOLEAN,
522 Instruction::SGET_BYTE,
523 Instruction::SGET_CHAR,
524 Instruction::SGET_SHORT,
525 Instruction::SPUT,
526 // Instruction::SPUT_WIDE,
527 Instruction::SPUT_OBJECT,
528 Instruction::SPUT_BOOLEAN,
529 Instruction::SPUT_BYTE,
530 Instruction::SPUT_CHAR,
531 Instruction::SPUT_SHORT,
532 Instruction::INVOKE_VIRTUAL,
533 Instruction::INVOKE_SUPER,
534 Instruction::INVOKE_DIRECT,
535 Instruction::INVOKE_STATIC,
536 Instruction::INVOKE_INTERFACE,
537 // Instruction::RETURN_VOID_BARRIER,
538 // Instruction::INVOKE_VIRTUAL_RANGE,
539 // Instruction::INVOKE_SUPER_RANGE,
540 // Instruction::INVOKE_DIRECT_RANGE,
541 // Instruction::INVOKE_STATIC_RANGE,
542 // Instruction::INVOKE_INTERFACE_RANGE,
543 // Instruction::UNUSED_79,
544 // Instruction::UNUSED_7A,
545 // Instruction::NEG_INT,
546 // Instruction::NOT_INT,
547 // Instruction::NEG_LONG,
548 // Instruction::NOT_LONG,
549 // Instruction::NEG_FLOAT,
550 // Instruction::NEG_DOUBLE,
551 // Instruction::INT_TO_LONG,
552 // Instruction::INT_TO_FLOAT,
553 // Instruction::INT_TO_DOUBLE,
554 // Instruction::LONG_TO_INT,
555 // Instruction::LONG_TO_FLOAT,
556 // Instruction::LONG_TO_DOUBLE,
557 // Instruction::FLOAT_TO_INT,
558 // Instruction::FLOAT_TO_LONG,
559 // Instruction::FLOAT_TO_DOUBLE,
560 // Instruction::DOUBLE_TO_INT,
561 // Instruction::DOUBLE_TO_LONG,
562 // Instruction::DOUBLE_TO_FLOAT,
563 // Instruction::INT_TO_BYTE,
564 // Instruction::INT_TO_CHAR,
565 // Instruction::INT_TO_SHORT,
566 // Instruction::ADD_INT,
567 // Instruction::SUB_INT,
568 // Instruction::MUL_INT,
569 // Instruction::DIV_INT,
570 // Instruction::REM_INT,
571 // Instruction::AND_INT,
572 // Instruction::OR_INT,
573 // Instruction::XOR_INT,
574 // Instruction::SHL_INT,
575 // Instruction::SHR_INT,
576 // Instruction::USHR_INT,
577 // Instruction::ADD_LONG,
578 // Instruction::SUB_LONG,
579 // Instruction::MUL_LONG,
580 // Instruction::DIV_LONG,
581 // Instruction::REM_LONG,
582 // Instruction::AND_LONG,
583 // Instruction::OR_LONG,
584 // Instruction::XOR_LONG,
585 // Instruction::SHL_LONG,
586 // Instruction::SHR_LONG,
587 // Instruction::USHR_LONG,
588 // Instruction::ADD_FLOAT,
589 // Instruction::SUB_FLOAT,
590 // Instruction::MUL_FLOAT,
591 // Instruction::DIV_FLOAT,
592 // Instruction::REM_FLOAT,
593 // Instruction::ADD_DOUBLE,
594 // Instruction::SUB_DOUBLE,
595 // Instruction::MUL_DOUBLE,
596 // Instruction::DIV_DOUBLE,
597 // Instruction::REM_DOUBLE,
598 // Instruction::ADD_INT_2ADDR,
599 // Instruction::SUB_INT_2ADDR,
600 // Instruction::MUL_INT_2ADDR,
601 // Instruction::DIV_INT_2ADDR,
602 // Instruction::REM_INT_2ADDR,
603 // Instruction::AND_INT_2ADDR,
604 // Instruction::OR_INT_2ADDR,
605 // Instruction::XOR_INT_2ADDR,
606 // Instruction::SHL_INT_2ADDR,
607 // Instruction::SHR_INT_2ADDR,
608 // Instruction::USHR_INT_2ADDR,
609 // Instruction::ADD_LONG_2ADDR,
610 // Instruction::SUB_LONG_2ADDR,
611 // Instruction::MUL_LONG_2ADDR,
612 // Instruction::DIV_LONG_2ADDR,
613 // Instruction::REM_LONG_2ADDR,
614 // Instruction::AND_LONG_2ADDR,
615 // Instruction::OR_LONG_2ADDR,
616 // Instruction::XOR_LONG_2ADDR,
617 // Instruction::SHL_LONG_2ADDR,
618 // Instruction::SHR_LONG_2ADDR,
619 // Instruction::USHR_LONG_2ADDR,
620 // Instruction::ADD_FLOAT_2ADDR,
621 // Instruction::SUB_FLOAT_2ADDR,
622 // Instruction::MUL_FLOAT_2ADDR,
623 // Instruction::DIV_FLOAT_2ADDR,
624 // Instruction::REM_FLOAT_2ADDR,
625 // Instruction::ADD_DOUBLE_2ADDR,
626 // Instruction::SUB_DOUBLE_2ADDR,
627 // Instruction::MUL_DOUBLE_2ADDR,
628 // Instruction::DIV_DOUBLE_2ADDR,
629 // Instruction::REM_DOUBLE_2ADDR,
630 // Instruction::ADD_INT_LIT16,
631 // Instruction::RSUB_INT,
632 // Instruction::MUL_INT_LIT16,
633 // Instruction::DIV_INT_LIT16,
634 // Instruction::REM_INT_LIT16,
635 // Instruction::AND_INT_LIT16,
636 // Instruction::OR_INT_LIT16,
637 // Instruction::XOR_INT_LIT16,
638 // Instruction::ADD_INT_LIT8,
639 // Instruction::RSUB_INT_LIT8,
640 // Instruction::MUL_INT_LIT8,
641 // Instruction::DIV_INT_LIT8,
642 // Instruction::REM_INT_LIT8,
643 // Instruction::AND_INT_LIT8,
644 // Instruction::OR_INT_LIT8,
645 // Instruction::XOR_INT_LIT8,
646 // Instruction::SHL_INT_LIT8,
647 // Instruction::SHR_INT_LIT8,
648 // Instruction::USHR_INT_LIT8,
649 // Instruction::IGET_QUICK,
650 // Instruction::IGET_WIDE_QUICK,
651 // Instruction::IGET_OBJECT_QUICK,
652 // Instruction::IPUT_QUICK,
653 // Instruction::IPUT_WIDE_QUICK,
654 // Instruction::IPUT_OBJECT_QUICK,
655 // Instruction::INVOKE_VIRTUAL_QUICK,
656 // Instruction::INVOKE_VIRTUAL_RANGE_QUICK,
657 // Instruction::UNUSED_EB,
658 // Instruction::UNUSED_EC,
659 // Instruction::UNUSED_ED,
660 // Instruction::UNUSED_EE,
661 // Instruction::UNUSED_EF,
662 // Instruction::UNUSED_F0,
663 // Instruction::UNUSED_F1,
664 // Instruction::UNUSED_F2,
665 // Instruction::UNUSED_F3,
666 // Instruction::UNUSED_F4,
667 // Instruction::UNUSED_F5,
668 // Instruction::UNUSED_F6,
669 // Instruction::UNUSED_F7,
670 // Instruction::UNUSED_F8,
671 // Instruction::UNUSED_F9,
672 // Instruction::UNUSED_FA,
673 // Instruction::UNUSED_FB,
674 // Instruction::UNUSED_FC,
675 // Instruction::UNUSED_FD,
676 // Instruction::UNUSED_FE,
677 // Instruction::UNUSED_FF,
678
679 // ----- ExtendedMIROpcode -----
680 // kMirOpPhi,
681 // kMirOpCopy,
682 // kMirOpFusedCmplFloat,
683 // kMirOpFusedCmpgFloat,
684 // kMirOpFusedCmplDouble,
685 // kMirOpFusedCmpgDouble,
686 // kMirOpFusedCmpLong,
687 // kMirOpNop,
688 // kMirOpNullCheck,
689 // kMirOpRangeCheck,
690 // kMirOpDivZeroCheck,
691 // kMirOpCheck,
692 // kMirOpCheckPart2,
693 // kMirOpSelect,
694 // kMirOpLast,
695};
696
697// Z : boolean
698// B : byte
699// S : short
700// C : char
701// I : int
Zheng Xu48241e72014-05-23 11:52:42 +0800702// J : long
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700703// F : float
704// D : double
705// L : reference(object, array)
706// V : void
707// (ARM64) Current calling conversion only support 32bit softfp
708// which has problems with long, float, double
Serban Constantinescu032d3772014-05-23 17:38:18 +0100709constexpr char arm64_supported_types[] = "ZBSCILVJFD";
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700710// (x84_64) We still have troubles with compiling longs/doubles/floats
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700711constexpr char x86_64_supported_types[] = "ZBSCILVJFD";
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700712
713// TODO: Remove this when we are able to compile everything.
714static bool CanCompileShorty(const char* shorty, InstructionSet instruction_set) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100715 uint32_t shorty_size = strlen(shorty);
716 CHECK_GE(shorty_size, 1u);
717 // Set a limitation on maximum number of parameters.
718 // Note : there is an implied "method*" parameter, and probably "this" as well.
719 // 1 is for the return type. Currently, we only accept 2 parameters at the most.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700720 // (x86_64): For now we have the same limitation. But we might want to split this
721 // check in future into two separate cases for arm64 and x86_64.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700722 if ((shorty_size > (1 + 2)) && (instruction_set != kX86_64)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100723 return false;
724 }
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700725
726 const char* supported_types = arm64_supported_types;
727 if (instruction_set == kX86_64) {
728 supported_types = x86_64_supported_types;
729 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100730 for (uint32_t i = 0; i < shorty_size; i++) {
731 if (strchr(supported_types, shorty[i]) == nullptr) {
732 return false;
733 }
734 }
735 return true;
736};
737
738// TODO: Remove this when we are able to compile everything.
739// Skip the method that we do not support currently.
740static bool CanCompileMethod(uint32_t method_idx, const DexFile& dex_file,
741 CompilationUnit& cu) {
742 // There is some limitation with current ARM 64 backend.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700743 if (cu.instruction_set == kArm64 || cu.instruction_set == kX86_64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100744 // Check if we can compile the prototype.
745 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700746 if (!CanCompileShorty(shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100747 VLOG(compiler) << "Unsupported shorty : " << shorty;
748 return false;
749 }
750
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700751 const int *support_list = arm64_support_list;
752 int support_list_size = arraysize(arm64_support_list);
753 if (cu.instruction_set == kX86_64) {
754 support_list = x86_64_support_list;
755 support_list_size = arraysize(x86_64_support_list);
756 }
757
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100758 for (int idx = 0; idx < cu.mir_graph->GetNumBlocks(); idx++) {
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700759 BasicBlock* bb = cu.mir_graph->GetBasicBlock(idx);
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100760 if (bb == NULL) continue;
761 if (bb->block_type == kDead) continue;
762 for (MIR* mir = bb->first_mir_insn; mir != nullptr; mir = mir->next) {
763 int opcode = mir->dalvikInsn.opcode;
764 // Check if we support the byte code.
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700765 if (std::find(support_list, support_list + support_list_size,
766 opcode) == support_list + support_list_size) {
buzbee35ba7f32014-05-31 08:59:01 -0700767 if (!cu.mir_graph->IsPseudoMirOp(opcode)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100768 VLOG(compiler) << "Unsupported dalvik byte code : "
769 << mir->dalvikInsn.opcode;
770 } else {
771 VLOG(compiler) << "Unsupported extended MIR opcode : "
772 << MIRGraph::extended_mir_op_names_[opcode - kMirOpFirst];
773 }
774 return false;
775 }
776 // Check if it invokes a prototype that we cannot support.
777 if (Instruction::INVOKE_VIRTUAL == opcode ||
778 Instruction::INVOKE_SUPER == opcode ||
779 Instruction::INVOKE_DIRECT == opcode ||
780 Instruction::INVOKE_STATIC == opcode ||
781 Instruction::INVOKE_INTERFACE == opcode) {
782 uint32_t invoke_method_idx = mir->dalvikInsn.vB;
783 const char* invoke_method_shorty = dex_file.GetMethodShorty(
784 dex_file.GetMethodId(invoke_method_idx));
Vladimir Kostyukov56784552014-05-13 12:12:00 +0700785 if (!CanCompileShorty(invoke_method_shorty, cu.instruction_set)) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100786 VLOG(compiler) << "Unsupported to invoke '"
787 << PrettyMethod(invoke_method_idx, dex_file)
788 << "' with shorty : " << invoke_method_shorty;
789 return false;
790 }
791 }
792 }
793 }
794
795 LOG(INFO) << "Using experimental instruction set A64 for "
796 << PrettyMethod(method_idx, dex_file);
797 }
798 return true;
799}
800
Ian Rogers3d504072014-03-01 09:16:49 -0800801static CompiledMethod* CompileMethod(CompilerDriver& driver,
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000802 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700803 const DexFile::CodeItem* code_item,
804 uint32_t access_flags, InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700805 uint16_t class_def_idx, uint32_t method_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +0000806 jobject class_loader, const DexFile& dex_file,
807 void* llvm_compilation_unit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700808 VLOG(compiler) << "Compiling " << PrettyMethod(method_idx, dex_file) << "...";
buzbeeb48819d2013-09-14 16:15:25 -0700809 if (code_item->insns_size_in_code_units_ >= 0x10000) {
810 LOG(INFO) << "Method size exceeds compiler limits: " << code_item->insns_size_in_code_units_
811 << " in " << PrettyMethod(method_idx, dex_file);
812 return NULL;
813 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700814
Jeff Hao4a200f52014-04-01 14:58:49 -0700815 if (!driver.GetCompilerOptions().IsCompilationEnabled()) {
Ian Rogersa03de6d2014-03-08 23:37:07 +0000816 return nullptr;
817 }
818
Brian Carlstrom7940e442013-07-12 13:46:57 -0700819 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Ian Rogers3d504072014-03-01 09:16:49 -0800820 CompilationUnit cu(driver.GetArenaPool());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700821
Ian Rogers3d504072014-03-01 09:16:49 -0800822 cu.compiler_driver = &driver;
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700823 cu.class_linker = class_linker;
Ian Rogers3d504072014-03-01 09:16:49 -0800824 cu.instruction_set = driver.GetInstructionSet();
Mathieu Chartier53bee422014-04-04 16:10:05 -0700825 if (cu.instruction_set == kArm) {
826 cu.instruction_set = kThumb2;
827 }
Andreas Gampeaf13ad92014-04-11 12:07:48 -0700828 cu.target64 = Is64BitInstructionSet(cu.instruction_set);
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000829 cu.compiler = compiler;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000830 // TODO: x86_64 & arm64 are not yet implemented.
Mathieu Chartier53bee422014-04-04 16:10:05 -0700831 CHECK((cu.instruction_set == kThumb2) ||
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100832 (cu.instruction_set == kArm64) ||
Mathieu Chartier53bee422014-04-04 16:10:05 -0700833 (cu.instruction_set == kX86) ||
834 (cu.instruction_set == kX86_64) ||
835 (cu.instruction_set == kMips));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700836
Brian Carlstrom7940e442013-07-12 13:46:57 -0700837 /* Adjust this value accordingly once inlining is performed */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700838 cu.num_dalvik_registers = code_item->registers_size_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700839 // TODO: set this from command line
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700840 cu.compiler_flip_match = false;
841 bool use_match = !cu.compiler_method_match.empty();
842 bool match = use_match && (cu.compiler_flip_match ^
843 (PrettyMethod(method_idx, dex_file).find(cu.compiler_method_match) !=
Brian Carlstrom7940e442013-07-12 13:46:57 -0700844 std::string::npos));
845 if (!use_match || match) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700846 cu.disable_opt = kCompilerOptimizerDisableFlags;
847 cu.enable_debug = kCompilerDebugFlags;
848 cu.verbose = VLOG_IS_ON(compiler) ||
849 (cu.enable_debug & (1 << kDebugVerbose));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700850 }
851
Mingyao Yang42d65c52014-04-18 16:49:39 -0700852 if (gVerboseMethods.size() != 0) {
853 cu.verbose = false;
854 for (size_t i = 0; i < gVerboseMethods.size(); ++i) {
855 if (PrettyMethod(method_idx, dex_file).find(gVerboseMethods[i])
856 != std::string::npos) {
857 cu.verbose = true;
858 break;
859 }
860 }
861 }
862
buzbeeb01bf152014-05-13 15:59:07 -0700863 if (cu.verbose) {
864 cu.enable_debug |= (1 << kDebugCodegenDump);
865 }
866
Brian Carlstrom7940e442013-07-12 13:46:57 -0700867 /*
868 * TODO: rework handling of optimization and debug flags. Should we split out
869 * MIR and backend flags? Need command-line setting as well.
870 */
871
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000872 compiler->InitCompilationUnit(cu);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700873
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700874 if (cu.instruction_set == kMips) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700875 // Disable some optimizations for mips for now
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700876 cu.disable_opt |= (
Brian Carlstrom7940e442013-07-12 13:46:57 -0700877 (1 << kLoadStoreElimination) |
878 (1 << kLoadHoisting) |
879 (1 << kSuppressLoads) |
880 (1 << kNullCheckElimination) |
881 (1 << kPromoteRegs) |
882 (1 << kTrackLiveTemps) |
883 (1 << kSafeOptimizations) |
884 (1 << kBBOpt) |
885 (1 << kMatch) |
886 (1 << kPromoteCompilerTemps));
887 }
888
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700889 if (cu.instruction_set == kArm64 || cu.instruction_set == kX86_64) {
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100890 // TODO(Arm64): enable optimizations once backend is mature enough.
Dmitry Petrochenkoba279d92014-05-16 18:36:39 +0700891 // TODO(X86_64): enable optimizations once backend is mature enough.
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100892 cu.disable_opt = ~(uint32_t)0;
Dmitry Petrochenko30022af2014-06-06 15:18:14 +0700893 if (cu.instruction_set == kArm64) {
894 cu.enable_debug |= (1 << kDebugCodegenDump);
895 }
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100896 }
897
buzbeea61f4952013-08-23 14:27:06 -0700898 cu.StartTimingSplit("BuildMIRGraph");
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700899 cu.mir_graph.reset(new MIRGraph(&cu, &cu.arena));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700900
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800901 /*
902 * After creation of the MIR graph, also create the code generator.
903 * The reason we do this is that optimizations on the MIR graph may need to get information
904 * that is only available if a CG exists.
905 */
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000906 cu.cg.reset(compiler->GetCodeGenerator(&cu, llvm_compilation_unit));
Razvan A Lupusoruda7a69b2014-01-08 15:09:50 -0800907
Brian Carlstrom7940e442013-07-12 13:46:57 -0700908 /* Gathering opcode stats? */
909 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700910 cu.mir_graph->EnableOpcodeCounting();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700911 }
912
Calin Juravlec1b643c2014-05-30 23:44:11 +0100913 // Check early if we should skip this compilation if the profiler is enabled.
Dave Allison39c3bfb2014-01-28 18:33:52 -0800914 if (cu.compiler_driver->ProfilePresent()) {
915 std::string methodname = PrettyMethod(method_idx, dex_file);
916 if (cu.mir_graph->SkipCompilation(methodname)) {
917 return NULL;
918 }
919 }
920
Brian Carlstrom7940e442013-07-12 13:46:57 -0700921 /* Build the raw MIR graph */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700922 cu.mir_graph->InlineMethod(code_item, access_flags, invoke_type, class_def_idx, method_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700923 class_loader, dex_file);
924
Zheng Xu9e06c8c2014-05-06 18:06:07 +0100925 // TODO(Arm64): Remove this when we are able to compile everything.
926 if (!CanCompileMethod(method_idx, dex_file, cu)) {
927 VLOG(compiler) << "Cannot compile method : " << PrettyMethod(method_idx, dex_file);
928 return nullptr;
929 }
930
buzbee1da1e2f2013-11-15 13:37:01 -0800931 cu.NewTimingSplit("MIROpt:CheckFilters");
Jeff Hao4a200f52014-04-01 14:58:49 -0700932 if (cu.mir_graph->SkipCompilation()) {
933 return NULL;
buzbeeee17e0a2013-07-31 10:47:37 -0700934 }
935
Jean Christophe Beyler4e97c532014-01-07 10:07:18 -0800936 /* Create the pass driver and launch it */
Jean Christophe Beyler2469e602014-05-06 20:36:55 -0700937 PassDriverMEOpts pass_driver(&cu);
Ian Rogers3d504072014-03-01 09:16:49 -0800938 pass_driver.Launch();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700939
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700940 if (cu.enable_debug & (1 << kDebugDumpCheckStats)) {
941 cu.mir_graph->DumpCheckStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700942 }
943
944 if (kCompilerDebugFlags & (1 << kDebugCountOpcodes)) {
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700945 cu.mir_graph->ShowOpcodeStats();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700946 }
947
buzbee1da1e2f2013-11-15 13:37:01 -0800948 /* Reassociate sreg names with original Dalvik vreg names. */
949 cu.mir_graph->RemapRegLocations();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700950
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000951 /* Free Arenas from the cu.arena_stack for reuse by the cu.arena in the codegen. */
952 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
953 if (cu.arena_stack.PeakBytesAllocated() > 256 * 1024) {
954 MemStats stack_stats(cu.arena_stack.GetPeakStats());
955 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(stack_stats);
956 }
957 }
958 cu.arena_stack.Reset();
959
Brian Carlstrom7940e442013-07-12 13:46:57 -0700960 CompiledMethod* result = NULL;
961
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700962 cu.cg->Materialize();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700963
buzbee1da1e2f2013-11-15 13:37:01 -0800964 cu.NewTimingSplit("Dedupe"); /* deduping takes up the vast majority of time in GetCompiledMethod(). */
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700965 result = cu.cg->GetCompiledMethod();
buzbee1da1e2f2013-11-15 13:37:01 -0800966 cu.NewTimingSplit("Cleanup");
Brian Carlstrom7940e442013-07-12 13:46:57 -0700967
968 if (result) {
969 VLOG(compiler) << "Compiled " << PrettyMethod(method_idx, dex_file);
970 } else {
971 VLOG(compiler) << "Deferred " << PrettyMethod(method_idx, dex_file);
972 }
973
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700974 if (cu.enable_debug & (1 << kDebugShowMemoryUsage)) {
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000975 if (cu.arena.BytesAllocated() > (1 * 1024 *1024)) {
Vladimir Marko83cc7ae2014-02-12 18:02:05 +0000976 MemStats mem_stats(cu.arena.GetMemStats());
Vladimir Marko53b6afc2014-03-21 14:21:20 +0000977 LOG(INFO) << PrettyMethod(method_idx, dex_file) << " " << Dumpable<MemStats>(mem_stats);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700978 }
979 }
980
Mathieu Chartierf6c4b3b2013-08-24 16:11:37 -0700981 if (cu.enable_debug & (1 << kDebugShowSummaryMemoryUsage)) {
982 LOG(INFO) << "MEMINFO " << cu.arena.BytesAllocated() << " " << cu.mir_graph->GetNumBlocks()
Brian Carlstrom7940e442013-07-12 13:46:57 -0700983 << " " << PrettyMethod(method_idx, dex_file);
984 }
985
buzbeea61f4952013-08-23 14:27:06 -0700986 cu.EndTiming();
Ian Rogers3d504072014-03-01 09:16:49 -0800987 driver.GetTimingsLogger()->AddLogger(cu.timings);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700988 return result;
989}
990
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +0000991CompiledMethod* CompileOneMethod(CompilerDriver& driver,
992 Compiler* compiler,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700993 const DexFile::CodeItem* code_item,
994 uint32_t access_flags,
995 InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -0700996 uint16_t class_def_idx,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700997 uint32_t method_idx,
998 jobject class_loader,
999 const DexFile& dex_file,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +00001000 void* compilation_unit) {
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001001 return CompileMethod(driver, compiler, code_item, access_flags, invoke_type, class_def_idx,
Nicolas Geoffrayf5df8972014-02-14 18:37:08 +00001002 method_idx, class_loader, dex_file, compilation_unit);
Brian Carlstrom7940e442013-07-12 13:46:57 -07001003}
1004
1005} // namespace art
1006
1007extern "C" art::CompiledMethod*
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001008 ArtQuickCompileMethod(art::CompilerDriver& driver,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001009 const art::DexFile::CodeItem* code_item,
1010 uint32_t access_flags, art::InvokeType invoke_type,
Ian Rogers8b2c0b92013-09-19 02:56:49 -07001011 uint16_t class_def_idx, uint32_t method_idx, jobject class_loader,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001012 const art::DexFile& dex_file) {
1013 // TODO: check method fingerprint here to determine appropriate backend type. Until then, use build default
Nicolas Geoffrayb34f69a2014-03-07 15:28:39 +00001014 art::Compiler* compiler = driver.GetCompiler();
1015 return art::CompileOneMethod(driver, compiler, code_item, access_flags, invoke_type,
Brian Carlstrom7940e442013-07-12 13:46:57 -07001016 class_def_idx, method_idx, class_loader, dex_file,
1017 NULL /* use thread llvm_info */);
1018}