Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: sdk/lib/_internal/compiler/implementation/ssa/nodes.dart

Issue 692513002: Remove old dart2js code. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of ssa;
6
7 abstract class HVisitor<R> {
8 R visitAdd(HAdd node);
9 R visitBitAnd(HBitAnd node);
10 R visitBitNot(HBitNot node);
11 R visitBitOr(HBitOr node);
12 R visitBitXor(HBitXor node);
13 R visitBoolify(HBoolify node);
14 R visitBoundsCheck(HBoundsCheck node);
15 R visitBreak(HBreak node);
16 R visitConstant(HConstant node);
17 R visitContinue(HContinue node);
18 R visitDivide(HDivide node);
19 R visitExit(HExit node);
20 R visitExitTry(HExitTry node);
21 R visitFieldGet(HFieldGet node);
22 R visitFieldSet(HFieldSet node);
23 R visitForeign(HForeign node);
24 R visitForeignNew(HForeignNew node);
25 R visitGoto(HGoto node);
26 R visitGreater(HGreater node);
27 R visitGreaterEqual(HGreaterEqual node);
28 R visitIdentity(HIdentity node);
29 R visitIf(HIf node);
30 R visitIndex(HIndex node);
31 R visitIndexAssign(HIndexAssign node);
32 R visitInterceptor(HInterceptor node);
33 R visitInvokeClosure(HInvokeClosure node);
34 R visitInvokeDynamicGetter(HInvokeDynamicGetter node);
35 R visitInvokeDynamicMethod(HInvokeDynamicMethod node);
36 R visitInvokeDynamicSetter(HInvokeDynamicSetter node);
37 R visitInvokeStatic(HInvokeStatic node);
38 R visitInvokeSuper(HInvokeSuper node);
39 R visitInvokeConstructorBody(HInvokeConstructorBody node);
40 R visitIs(HIs node);
41 R visitIsViaInterceptor(HIsViaInterceptor node);
42 R visitLazyStatic(HLazyStatic node);
43 R visitLess(HLess node);
44 R visitLessEqual(HLessEqual node);
45 R visitLiteralList(HLiteralList node);
46 R visitLocalGet(HLocalGet node);
47 R visitLocalSet(HLocalSet node);
48 R visitLocalValue(HLocalValue node);
49 R visitLoopBranch(HLoopBranch node);
50 R visitMultiply(HMultiply node);
51 R visitNegate(HNegate node);
52 R visitNot(HNot node);
53 R visitOneShotInterceptor(HOneShotInterceptor node);
54 R visitParameterValue(HParameterValue node);
55 R visitPhi(HPhi node);
56 R visitRangeConversion(HRangeConversion node);
57 R visitReadModifyWrite(HReadModifyWrite node);
58 R visitReturn(HReturn node);
59 R visitShiftLeft(HShiftLeft node);
60 R visitShiftRight(HShiftRight node);
61 R visitStatic(HStatic node);
62 R visitStaticStore(HStaticStore node);
63 R visitStringConcat(HStringConcat node);
64 R visitStringify(HStringify node);
65 R visitSubtract(HSubtract node);
66 R visitSwitch(HSwitch node);
67 R visitThis(HThis node);
68 R visitThrow(HThrow node);
69 R visitThrowExpression(HThrowExpression node);
70 R visitTruncatingDivide(HTruncatingDivide node);
71 R visitTry(HTry node);
72 R visitTypeConversion(HTypeConversion node);
73 R visitTypeKnown(HTypeKnown node);
74 R visitReadTypeVariable(HReadTypeVariable node);
75 R visitFunctionType(HFunctionType node);
76 R visitVoidType(HVoidType node);
77 R visitInterfaceType(HInterfaceType node);
78 R visitDynamicType(HDynamicType node);
79 }
80
81 abstract class HGraphVisitor {
82 visitDominatorTree(HGraph graph) {
83 void visitBasicBlockAndSuccessors(HBasicBlock block) {
84 visitBasicBlock(block);
85 List dominated = block.dominatedBlocks;
86 for (int i = 0; i < dominated.length; i++) {
87 visitBasicBlockAndSuccessors(dominated[i]);
88 }
89 }
90
91 visitBasicBlockAndSuccessors(graph.entry);
92 }
93
94 visitPostDominatorTree(HGraph graph) {
95 void visitBasicBlockAndSuccessors(HBasicBlock block) {
96 List dominated = block.dominatedBlocks;
97 for (int i = dominated.length - 1; i >= 0; i--) {
98 visitBasicBlockAndSuccessors(dominated[i]);
99 }
100 visitBasicBlock(block);
101 }
102
103 visitBasicBlockAndSuccessors(graph.entry);
104 }
105
106 visitBasicBlock(HBasicBlock block);
107 }
108
109 abstract class HInstructionVisitor extends HGraphVisitor {
110 HBasicBlock currentBlock;
111
112 visitInstruction(HInstruction node);
113
114 visitBasicBlock(HBasicBlock node) {
115 void visitInstructionList(HInstructionList list) {
116 HInstruction instruction = list.first;
117 while (instruction != null) {
118 visitInstruction(instruction);
119 instruction = instruction.next;
120 assert(instruction != list.first);
121 }
122 }
123
124 currentBlock = node;
125 visitInstructionList(node);
126 }
127 }
128
129 class HGraph {
130 HBasicBlock entry;
131 HBasicBlock exit;
132 HThis thisInstruction;
133 /// Receiver parameter, set for methods using interceptor calling convention.
134 HParameterValue explicitReceiverParameter;
135 bool isRecursiveMethod = false;
136 bool calledInLoop = false;
137 final List<HBasicBlock> blocks;
138
139 // We canonicalize all constants used within a graph so we do not
140 // have to worry about them for global value numbering.
141 Map<ConstantValue, HConstant> constants;
142
143 HGraph()
144 : blocks = new List<HBasicBlock>(),
145 constants = new Map<ConstantValue, HConstant>() {
146 entry = addNewBlock();
147 // The exit block will be added later, so it has an id that is
148 // after all others in the system.
149 exit = new HBasicBlock();
150 }
151
152 void addBlock(HBasicBlock block) {
153 int id = blocks.length;
154 block.id = id;
155 blocks.add(block);
156 assert(identical(blocks[id], block));
157 }
158
159 HBasicBlock addNewBlock() {
160 HBasicBlock result = new HBasicBlock();
161 addBlock(result);
162 return result;
163 }
164
165 HBasicBlock addNewLoopHeaderBlock(JumpTarget target,
166 List<LabelDefinition> labels) {
167 HBasicBlock result = addNewBlock();
168 result.loopInformation =
169 new HLoopInformation(result, target, labels);
170 return result;
171 }
172
173 HConstant addConstant(ConstantValue constant, Compiler compiler) {
174 HConstant result = constants[constant];
175 if (result == null) {
176 TypeMask type = constant.computeMask(compiler);
177 result = new HConstant.internal(constant, type);
178 entry.addAtExit(result);
179 constants[constant] = result;
180 } else if (result.block == null) {
181 // The constant was not used anymore.
182 entry.addAtExit(result);
183 }
184 return result;
185 }
186
187 HConstant addDeferredConstant(ConstantValue constant, PrefixElement prefix,
188 Compiler compiler) {
189 ConstantValue wrapper = new DeferredConstantValue(constant, prefix);
190 compiler.deferredLoadTask.registerConstantDeferredUse(wrapper, prefix);
191 return addConstant(wrapper, compiler);
192 }
193
194 HConstant addConstantInt(int i, Compiler compiler) {
195 return addConstant(compiler.backend.constantSystem.createInt(i), compiler);
196 }
197
198 HConstant addConstantDouble(double d, Compiler compiler) {
199 return addConstant(
200 compiler.backend.constantSystem.createDouble(d), compiler);
201 }
202
203 HConstant addConstantString(ast.DartString str,
204 Compiler compiler) {
205 return addConstant(
206 compiler.backend.constantSystem.createString(str),
207 compiler);
208 }
209
210 HConstant addConstantBool(bool value, Compiler compiler) {
211 return addConstant(
212 compiler.backend.constantSystem.createBool(value), compiler);
213 }
214
215 HConstant addConstantNull(Compiler compiler) {
216 return addConstant(compiler.backend.constantSystem.createNull(), compiler);
217 }
218
219 void finalize() {
220 addBlock(exit);
221 exit.open();
222 exit.close(new HExit());
223 assignDominators();
224 }
225
226 void assignDominators() {
227 // Run through the blocks in order of increasing ids so we are
228 // guaranteed that we have computed dominators for all blocks
229 // higher up in the dominator tree.
230 for (int i = 0, length = blocks.length; i < length; i++) {
231 HBasicBlock block = blocks[i];
232 List<HBasicBlock> predecessors = block.predecessors;
233 if (block.isLoopHeader()) {
234 block.assignCommonDominator(predecessors[0]);
235 } else {
236 for (int j = predecessors.length - 1; j >= 0; j--) {
237 block.assignCommonDominator(predecessors[j]);
238 }
239 }
240 }
241 }
242
243 bool isValid() {
244 HValidator validator = new HValidator();
245 validator.visitGraph(this);
246 return validator.isValid;
247 }
248 }
249
250 class HBaseVisitor extends HGraphVisitor implements HVisitor {
251 HBasicBlock currentBlock;
252
253 visitBasicBlock(HBasicBlock node) {
254 currentBlock = node;
255
256 HInstruction instruction = node.first;
257 while (instruction != null) {
258 instruction.accept(this);
259 instruction = instruction.next;
260 }
261 }
262
263 visitInstruction(HInstruction instruction) {}
264
265 visitBinaryArithmetic(HBinaryArithmetic node) => visitInvokeBinary(node);
266 visitBinaryBitOp(HBinaryBitOp node) => visitInvokeBinary(node);
267 visitInvoke(HInvoke node) => visitInstruction(node);
268 visitInvokeBinary(HInvokeBinary node) => visitInstruction(node);
269 visitInvokeDynamic(HInvokeDynamic node) => visitInvoke(node);
270 visitInvokeDynamicField(HInvokeDynamicField node) => visitInvokeDynamic(node);
271 visitInvokeUnary(HInvokeUnary node) => visitInstruction(node);
272 visitConditionalBranch(HConditionalBranch node) => visitControlFlow(node);
273 visitControlFlow(HControlFlow node) => visitInstruction(node);
274 visitFieldAccess(HFieldAccess node) => visitInstruction(node);
275 visitRelational(HRelational node) => visitInvokeBinary(node);
276
277 visitAdd(HAdd node) => visitBinaryArithmetic(node);
278 visitBitAnd(HBitAnd node) => visitBinaryBitOp(node);
279 visitBitNot(HBitNot node) => visitInvokeUnary(node);
280 visitBitOr(HBitOr node) => visitBinaryBitOp(node);
281 visitBitXor(HBitXor node) => visitBinaryBitOp(node);
282 visitBoolify(HBoolify node) => visitInstruction(node);
283 visitBoundsCheck(HBoundsCheck node) => visitCheck(node);
284 visitBreak(HBreak node) => visitJump(node);
285 visitContinue(HContinue node) => visitJump(node);
286 visitCheck(HCheck node) => visitInstruction(node);
287 visitConstant(HConstant node) => visitInstruction(node);
288 visitDivide(HDivide node) => visitBinaryArithmetic(node);
289 visitExit(HExit node) => visitControlFlow(node);
290 visitExitTry(HExitTry node) => visitControlFlow(node);
291 visitFieldGet(HFieldGet node) => visitFieldAccess(node);
292 visitFieldSet(HFieldSet node) => visitFieldAccess(node);
293 visitForeign(HForeign node) => visitInstruction(node);
294 visitForeignNew(HForeignNew node) => visitForeign(node);
295 visitGoto(HGoto node) => visitControlFlow(node);
296 visitGreater(HGreater node) => visitRelational(node);
297 visitGreaterEqual(HGreaterEqual node) => visitRelational(node);
298 visitIdentity(HIdentity node) => visitRelational(node);
299 visitIf(HIf node) => visitConditionalBranch(node);
300 visitIndex(HIndex node) => visitInstruction(node);
301 visitIndexAssign(HIndexAssign node) => visitInstruction(node);
302 visitInterceptor(HInterceptor node) => visitInstruction(node);
303 visitInvokeClosure(HInvokeClosure node)
304 => visitInvokeDynamic(node);
305 visitInvokeConstructorBody(HInvokeConstructorBody node)
306 => visitInvokeStatic(node);
307 visitInvokeDynamicMethod(HInvokeDynamicMethod node)
308 => visitInvokeDynamic(node);
309 visitInvokeDynamicGetter(HInvokeDynamicGetter node)
310 => visitInvokeDynamicField(node);
311 visitInvokeDynamicSetter(HInvokeDynamicSetter node)
312 => visitInvokeDynamicField(node);
313 visitInvokeStatic(HInvokeStatic node) => visitInvoke(node);
314 visitInvokeSuper(HInvokeSuper node) => visitInvokeStatic(node);
315 visitJump(HJump node) => visitControlFlow(node);
316 visitLazyStatic(HLazyStatic node) => visitInstruction(node);
317 visitLess(HLess node) => visitRelational(node);
318 visitLessEqual(HLessEqual node) => visitRelational(node);
319 visitLiteralList(HLiteralList node) => visitInstruction(node);
320 visitLocalAccess(HLocalAccess node) => visitInstruction(node);
321 visitLocalGet(HLocalGet node) => visitLocalAccess(node);
322 visitLocalSet(HLocalSet node) => visitLocalAccess(node);
323 visitLocalValue(HLocalValue node) => visitInstruction(node);
324 visitLoopBranch(HLoopBranch node) => visitConditionalBranch(node);
325 visitNegate(HNegate node) => visitInvokeUnary(node);
326 visitNot(HNot node) => visitInstruction(node);
327 visitOneShotInterceptor(HOneShotInterceptor node)
328 => visitInvokeDynamic(node);
329 visitPhi(HPhi node) => visitInstruction(node);
330 visitMultiply(HMultiply node) => visitBinaryArithmetic(node);
331 visitParameterValue(HParameterValue node) => visitLocalValue(node);
332 visitRangeConversion(HRangeConversion node) => visitCheck(node);
333 visitReadModifyWrite(HReadModifyWrite node) => visitInstruction(node);
334 visitReturn(HReturn node) => visitControlFlow(node);
335 visitShiftLeft(HShiftLeft node) => visitBinaryBitOp(node);
336 visitShiftRight(HShiftRight node) => visitBinaryBitOp(node);
337 visitSubtract(HSubtract node) => visitBinaryArithmetic(node);
338 visitSwitch(HSwitch node) => visitControlFlow(node);
339 visitStatic(HStatic node) => visitInstruction(node);
340 visitStaticStore(HStaticStore node) => visitInstruction(node);
341 visitStringConcat(HStringConcat node) => visitInstruction(node);
342 visitStringify(HStringify node) => visitInstruction(node);
343 visitThis(HThis node) => visitParameterValue(node);
344 visitThrow(HThrow node) => visitControlFlow(node);
345 visitThrowExpression(HThrowExpression node) => visitInstruction(node);
346 visitTruncatingDivide(HTruncatingDivide node) => visitBinaryArithmetic(node);
347 visitTry(HTry node) => visitControlFlow(node);
348 visitIs(HIs node) => visitInstruction(node);
349 visitIsViaInterceptor(HIsViaInterceptor node) => visitInstruction(node);
350 visitTypeConversion(HTypeConversion node) => visitCheck(node);
351 visitTypeKnown(HTypeKnown node) => visitCheck(node);
352 visitReadTypeVariable(HReadTypeVariable node) => visitInstruction(node);
353 visitFunctionType(HFunctionType node) => visitInstruction(node);
354 visitVoidType(HVoidType node) => visitInstruction(node);
355 visitInterfaceType(HInterfaceType node) => visitInstruction(node);
356 visitDynamicType(HDynamicType node) => visitInstruction(node);
357 }
358
359 class SubGraph {
360 // The first and last block of the sub-graph.
361 final HBasicBlock start;
362 final HBasicBlock end;
363
364 const SubGraph(this.start, this.end);
365
366 bool contains(HBasicBlock block) {
367 assert(start != null);
368 assert(end != null);
369 assert(block != null);
370 return start.id <= block.id && block.id <= end.id;
371 }
372 }
373
374 class SubExpression extends SubGraph {
375 const SubExpression(HBasicBlock start, HBasicBlock end)
376 : super(start, end);
377
378 /** Find the condition expression if this sub-expression is a condition. */
379 HInstruction get conditionExpression {
380 HInstruction last = end.last;
381 if (last is HConditionalBranch || last is HSwitch) return last.inputs[0];
382 return null;
383 }
384 }
385
386 class HInstructionList {
387 HInstruction first = null;
388 HInstruction last = null;
389
390 bool get isEmpty {
391 return first == null;
392 }
393
394 void internalAddAfter(HInstruction cursor, HInstruction instruction) {
395 if (cursor == null) {
396 assert(isEmpty);
397 first = last = instruction;
398 } else if (identical(cursor, last)) {
399 last.next = instruction;
400 instruction.previous = last;
401 last = instruction;
402 } else {
403 instruction.previous = cursor;
404 instruction.next = cursor.next;
405 cursor.next.previous = instruction;
406 cursor.next = instruction;
407 }
408 }
409
410 void internalAddBefore(HInstruction cursor, HInstruction instruction) {
411 if (cursor == null) {
412 assert(isEmpty);
413 first = last = instruction;
414 } else if (identical(cursor, first)) {
415 first.previous = instruction;
416 instruction.next = first;
417 first = instruction;
418 } else {
419 instruction.next = cursor;
420 instruction.previous = cursor.previous;
421 cursor.previous.next = instruction;
422 cursor.previous = instruction;
423 }
424 }
425
426 void detach(HInstruction instruction) {
427 assert(contains(instruction));
428 assert(instruction.isInBasicBlock());
429 if (instruction.previous == null) {
430 first = instruction.next;
431 } else {
432 instruction.previous.next = instruction.next;
433 }
434 if (instruction.next == null) {
435 last = instruction.previous;
436 } else {
437 instruction.next.previous = instruction.previous;
438 }
439 instruction.previous = null;
440 instruction.next = null;
441 }
442
443 void remove(HInstruction instruction) {
444 assert(instruction.usedBy.isEmpty);
445 detach(instruction);
446 }
447
448 /** Linear search for [instruction]. */
449 bool contains(HInstruction instruction) {
450 HInstruction cursor = first;
451 while (cursor != null) {
452 if (identical(cursor, instruction)) return true;
453 cursor = cursor.next;
454 }
455 return false;
456 }
457 }
458
459 class HBasicBlock extends HInstructionList {
460 // The [id] must be such that any successor's id is greater than
461 // this [id]. The exception are back-edges.
462 int id;
463
464 static const int STATUS_NEW = 0;
465 static const int STATUS_OPEN = 1;
466 static const int STATUS_CLOSED = 2;
467 int status = STATUS_NEW;
468
469 HInstructionList phis;
470
471 HLoopInformation loopInformation = null;
472 HBlockFlow blockFlow = null;
473 HBasicBlock parentLoopHeader = null;
474 bool isLive = true;
475
476 final List<HBasicBlock> predecessors;
477 List<HBasicBlock> successors;
478
479 HBasicBlock dominator = null;
480 final List<HBasicBlock> dominatedBlocks;
481
482 HBasicBlock() : this.withId(null);
483 HBasicBlock.withId(this.id)
484 : phis = new HInstructionList(),
485 predecessors = <HBasicBlock>[],
486 successors = const <HBasicBlock>[],
487 dominatedBlocks = <HBasicBlock>[];
488
489 int get hashCode => id;
490
491 bool isNew() => status == STATUS_NEW;
492 bool isOpen() => status == STATUS_OPEN;
493 bool isClosed() => status == STATUS_CLOSED;
494
495 bool isLoopHeader() {
496 return loopInformation != null;
497 }
498
499 void setBlockFlow(HBlockInformation blockInfo, HBasicBlock continuation) {
500 blockFlow = new HBlockFlow(blockInfo, continuation);
501 }
502
503 bool isLabeledBlock() =>
504 blockFlow != null &&
505 blockFlow.body is HLabeledBlockInformation;
506
507 HBasicBlock get enclosingLoopHeader {
508 if (isLoopHeader()) return this;
509 return parentLoopHeader;
510 }
511
512 void open() {
513 assert(isNew());
514 status = STATUS_OPEN;
515 }
516
517 void close(HControlFlow end) {
518 assert(isOpen());
519 addAfter(last, end);
520 status = STATUS_CLOSED;
521 }
522
523 void addAtEntry(HInstruction instruction) {
524 assert(instruction is !HPhi);
525 internalAddBefore(first, instruction);
526 instruction.notifyAddedToBlock(this);
527 }
528
529 void addAtExit(HInstruction instruction) {
530 assert(isClosed());
531 assert(last is HControlFlow);
532 assert(instruction is !HPhi);
533 internalAddBefore(last, instruction);
534 instruction.notifyAddedToBlock(this);
535 }
536
537 void moveAtExit(HInstruction instruction) {
538 assert(instruction is !HPhi);
539 assert(instruction.isInBasicBlock());
540 assert(isClosed());
541 assert(last is HControlFlow);
542 internalAddBefore(last, instruction);
543 instruction.block = this;
544 assert(isValid());
545 }
546
547 void add(HInstruction instruction) {
548 assert(instruction is !HControlFlow);
549 assert(instruction is !HPhi);
550 internalAddAfter(last, instruction);
551 instruction.notifyAddedToBlock(this);
552 }
553
554 void addPhi(HPhi phi) {
555 assert(phi.inputs.length == 0 || phi.inputs.length == predecessors.length);
556 assert(phi.block == null);
557 phis.internalAddAfter(phis.last, phi);
558 phi.notifyAddedToBlock(this);
559 }
560
561 void removePhi(HPhi phi) {
562 phis.remove(phi);
563 assert(phi.block == this);
564 phi.notifyRemovedFromBlock();
565 }
566
567 void addAfter(HInstruction cursor, HInstruction instruction) {
568 assert(cursor is !HPhi);
569 assert(instruction is !HPhi);
570 assert(isOpen() || isClosed());
571 internalAddAfter(cursor, instruction);
572 instruction.notifyAddedToBlock(this);
573 }
574
575 void addBefore(HInstruction cursor, HInstruction instruction) {
576 assert(cursor is !HPhi);
577 assert(instruction is !HPhi);
578 assert(isOpen() || isClosed());
579 internalAddBefore(cursor, instruction);
580 instruction.notifyAddedToBlock(this);
581 }
582
583 void remove(HInstruction instruction) {
584 assert(isOpen() || isClosed());
585 assert(instruction is !HPhi);
586 super.remove(instruction);
587 assert(instruction.block == this);
588 instruction.notifyRemovedFromBlock();
589 }
590
591 void addSuccessor(HBasicBlock block) {
592 if (successors.isEmpty) {
593 successors = [block];
594 } else {
595 successors.add(block);
596 }
597 block.predecessors.add(this);
598 }
599
600 void postProcessLoopHeader() {
601 assert(isLoopHeader());
602 // Only the first entry into the loop is from outside the
603 // loop. All other entries must be back edges.
604 for (int i = 1, length = predecessors.length; i < length; i++) {
605 loopInformation.addBackEdge(predecessors[i]);
606 }
607 }
608
609 /**
610 * Rewrites all uses of the [from] instruction to using the [to]
611 * instruction instead.
612 */
613 void rewrite(HInstruction from, HInstruction to) {
614 for (HInstruction use in from.usedBy) {
615 use.rewriteInput(from, to);
616 }
617 to.usedBy.addAll(from.usedBy);
618 from.usedBy.clear();
619 }
620
621 /**
622 * Rewrites all uses of the [from] instruction to using either the
623 * [to] instruction, or a [HCheck] instruction that has better type
624 * information on [to], and that dominates the user.
625 */
626 void rewriteWithBetterUser(HInstruction from, HInstruction to) {
627 // BUG(11841): Turn this method into a phase to be run after GVN phases.
628 Link<HCheck> better = const Link<HCheck>();
629 for (HInstruction user in to.usedBy) {
630 if (user == from || user is! HCheck) continue;
631 HCheck check = user;
632 if (check.checkedInput == to) {
633 better = better.prepend(user);
634 }
635 }
636
637 if (better.isEmpty) return rewrite(from, to);
638
639 L1: for (HInstruction user in from.usedBy) {
640 for (HCheck check in better) {
641 if (check.dominates(user)) {
642 user.rewriteInput(from, check);
643 check.usedBy.add(user);
644 continue L1;
645 }
646 }
647 user.rewriteInput(from, to);
648 to.usedBy.add(user);
649 }
650 from.usedBy.clear();
651 }
652
653 bool isExitBlock() {
654 return identical(first, last) && first is HExit;
655 }
656
657 void addDominatedBlock(HBasicBlock block) {
658 assert(isClosed());
659 assert(id != null && block.id != null);
660 assert(dominatedBlocks.indexOf(block) < 0);
661 // Keep the list of dominated blocks sorted such that if there are two
662 // succeeding blocks in the list, the predecessor is before the successor.
663 // Assume that we add the dominated blocks in the right order.
664 int index = dominatedBlocks.length;
665 while (index > 0 && dominatedBlocks[index - 1].id > block.id) {
666 index--;
667 }
668 if (index == dominatedBlocks.length) {
669 dominatedBlocks.add(block);
670 } else {
671 dominatedBlocks.insert(index, block);
672 }
673 assert(block.dominator == null);
674 block.dominator = this;
675 }
676
677 void removeDominatedBlock(HBasicBlock block) {
678 assert(isClosed());
679 assert(id != null && block.id != null);
680 int index = dominatedBlocks.indexOf(block);
681 assert(index >= 0);
682 if (index == dominatedBlocks.length - 1) {
683 dominatedBlocks.removeLast();
684 } else {
685 dominatedBlocks.removeRange(index, index + 1);
686 }
687 assert(identical(block.dominator, this));
688 block.dominator = null;
689 }
690
691 void assignCommonDominator(HBasicBlock predecessor) {
692 assert(isClosed());
693 if (dominator == null) {
694 // If this basic block doesn't have a dominator yet we use the
695 // given predecessor as the dominator.
696 predecessor.addDominatedBlock(this);
697 } else if (predecessor.dominator != null) {
698 // If the predecessor has a dominator and this basic block has a
699 // dominator, we find a common parent in the dominator tree and
700 // use that as the dominator.
701 HBasicBlock block0 = dominator;
702 HBasicBlock block1 = predecessor;
703 while (!identical(block0, block1)) {
704 if (block0.id > block1.id) {
705 block0 = block0.dominator;
706 } else {
707 block1 = block1.dominator;
708 }
709 assert(block0 != null && block1 != null);
710 }
711 if (!identical(dominator, block0)) {
712 dominator.removeDominatedBlock(this);
713 block0.addDominatedBlock(this);
714 }
715 }
716 }
717
718 void forEachPhi(void f(HPhi phi)) {
719 HPhi current = phis.first;
720 while (current != null) {
721 HInstruction saved = current.next;
722 f(current);
723 current = saved;
724 }
725 }
726
727 void forEachInstruction(void f(HInstruction instruction)) {
728 HInstruction current = first;
729 while (current != null) {
730 HInstruction saved = current.next;
731 f(current);
732 current = saved;
733 }
734 }
735
736 bool isValid() {
737 assert(isClosed());
738 HValidator validator = new HValidator();
739 validator.visitBasicBlock(this);
740 return validator.isValid;
741 }
742
743 Map<HBasicBlock, bool> dominatesCache;
744
745 bool dominates(HBasicBlock other) {
746 if (dominatesCache == null) {
747 dominatesCache = new Map<HBasicBlock, bool>();
748 } else {
749 bool res = dominatesCache[other];
750 if (res != null) return res;
751 }
752 do {
753 if (identical(this, other)) return dominatesCache[other] = true;
754 other = other.dominator;
755 } while (other != null && other.id >= id);
756 return dominatesCache[other] = false;
757 }
758 }
759
760 abstract class HInstruction implements Spannable {
761 Entity sourceElement;
762 SourceFileLocation sourcePosition;
763
764 final int id;
765 static int idCounter;
766
767 final List<HInstruction> inputs;
768 final List<HInstruction> usedBy;
769
770 HBasicBlock block;
771 HInstruction previous = null;
772 HInstruction next = null;
773
774 SideEffects sideEffects = new SideEffects.empty();
775 bool _useGvn = false;
776
777 // Type codes.
778 static const int UNDEFINED_TYPECODE = -1;
779 static const int BOOLIFY_TYPECODE = 0;
780 static const int TYPE_GUARD_TYPECODE = 1;
781 static const int BOUNDS_CHECK_TYPECODE = 2;
782 static const int INTEGER_CHECK_TYPECODE = 3;
783 static const int INTERCEPTOR_TYPECODE = 4;
784 static const int ADD_TYPECODE = 5;
785 static const int DIVIDE_TYPECODE = 6;
786 static const int MULTIPLY_TYPECODE = 7;
787 static const int SUBTRACT_TYPECODE = 8;
788 static const int SHIFT_LEFT_TYPECODE = 9;
789 static const int BIT_OR_TYPECODE = 10;
790 static const int BIT_AND_TYPECODE = 11;
791 static const int BIT_XOR_TYPECODE = 12;
792 static const int NEGATE_TYPECODE = 13;
793 static const int BIT_NOT_TYPECODE = 14;
794 static const int NOT_TYPECODE = 15;
795 static const int IDENTITY_TYPECODE = 16;
796 static const int GREATER_TYPECODE = 17;
797 static const int GREATER_EQUAL_TYPECODE = 18;
798 static const int LESS_TYPECODE = 19;
799 static const int LESS_EQUAL_TYPECODE = 20;
800 static const int STATIC_TYPECODE = 21;
801 static const int STATIC_STORE_TYPECODE = 22;
802 static const int FIELD_GET_TYPECODE = 23;
803 static const int TYPE_CONVERSION_TYPECODE = 24;
804 static const int TYPE_KNOWN_TYPECODE = 25;
805 static const int INVOKE_STATIC_TYPECODE = 26;
806 static const int INDEX_TYPECODE = 27;
807 static const int IS_TYPECODE = 28;
808 static const int INVOKE_DYNAMIC_TYPECODE = 29;
809 static const int SHIFT_RIGHT_TYPECODE = 30;
810 static const int READ_TYPE_VARIABLE_TYPECODE = 31;
811 static const int FUNCTION_TYPE_TYPECODE = 32;
812 static const int VOID_TYPE_TYPECODE = 33;
813 static const int INTERFACE_TYPE_TYPECODE = 34;
814 static const int DYNAMIC_TYPE_TYPECODE = 35;
815 static const int TRUNCATING_DIVIDE_TYPECODE = 36;
816 static const int IS_VIA_INTERCEPTOR_TYPECODE = 37;
817
818 HInstruction(this.inputs, this.instructionType)
819 : id = idCounter++, usedBy = <HInstruction>[] {
820 assert(inputs.every((e) => e != null));
821 }
822
823 int get hashCode => id;
824
825 bool useGvn() => _useGvn;
826 void setUseGvn() { _useGvn = true; }
827
828 bool get isMovable => useGvn();
829
830 /**
831 * A pure instruction is an instruction that does not have any side
832 * effect, nor any dependency. They can be moved anywhere in the
833 * graph.
834 */
835 bool isPure() {
836 return !sideEffects.hasSideEffects()
837 && !sideEffects.dependsOnSomething()
838 && !canThrow();
839 }
840
841 /// Overridden by [HCheck] to return the actual non-[HCheck]
842 /// instruction it checks against.
843 HInstruction nonCheck() => this;
844
845 /// Can this node throw an exception?
846 bool canThrow() => false;
847
848 /// Does this node potentially affect control flow.
849 bool isControlFlow() => false;
850
851 bool isExact() => instructionType.isExact || isNull();
852
853 bool canBeNull() => instructionType.isNullable;
854
855 bool isNull() => instructionType.isEmpty && instructionType.isNullable;
856 bool isConflicting() {
857 return instructionType.isEmpty && !instructionType.isNullable;
858 }
859
860 bool canBePrimitive(Compiler compiler) {
861 return canBePrimitiveNumber(compiler)
862 || canBePrimitiveArray(compiler)
863 || canBePrimitiveBoolean(compiler)
864 || canBePrimitiveString(compiler)
865 || isNull();
866 }
867
868 bool canBePrimitiveNumber(Compiler compiler) {
869 ClassWorld classWorld = compiler.world;
870 JavaScriptBackend backend = compiler.backend;
871 // TODO(sra): It should be possible to test only jsDoubleClass and
872 // jsUInt31Class, since all others are superclasses of these two.
873 return instructionType.contains(backend.jsNumberClass, classWorld)
874 || instructionType.contains(backend.jsIntClass, classWorld)
875 || instructionType.contains(backend.jsPositiveIntClass, classWorld)
876 || instructionType.contains(backend.jsUInt32Class, classWorld)
877 || instructionType.contains(backend.jsUInt31Class, classWorld)
878 || instructionType.contains(backend.jsDoubleClass, classWorld);
879 }
880
881 bool canBePrimitiveBoolean(Compiler compiler) {
882 ClassWorld classWorld = compiler.world;
883 JavaScriptBackend backend = compiler.backend;
884 return instructionType.contains(backend.jsBoolClass, classWorld);
885 }
886
887 bool canBePrimitiveArray(Compiler compiler) {
888 ClassWorld classWorld = compiler.world;
889 JavaScriptBackend backend = compiler.backend;
890 return instructionType.contains(backend.jsArrayClass, classWorld)
891 || instructionType.contains(backend.jsFixedArrayClass, classWorld)
892 || instructionType.contains(backend.jsExtendableArrayClass, classWorld);
893 }
894
895 bool isIndexablePrimitive(Compiler compiler) {
896 ClassWorld classWorld = compiler.world;
897 JavaScriptBackend backend = compiler.backend;
898 return instructionType.containsOnlyString(classWorld)
899 || instructionType.satisfies(backend.jsIndexableClass, classWorld);
900 }
901
902 bool isFixedArray(Compiler compiler) {
903 JavaScriptBackend backend = compiler.backend;
904 return instructionType.containsOnly(backend.jsFixedArrayClass);
905 }
906
907 bool isExtendableArray(Compiler compiler) {
908 JavaScriptBackend backend = compiler.backend;
909 return instructionType.containsOnly(backend.jsExtendableArrayClass);
910 }
911
912 bool isMutableArray(Compiler compiler) {
913 ClassWorld classWorld = compiler.world;
914 JavaScriptBackend backend = compiler.backend;
915 return instructionType.satisfies(backend.jsMutableArrayClass, classWorld);
916 }
917
918 bool isReadableArray(Compiler compiler) {
919 ClassWorld classWorld = compiler.world;
920 JavaScriptBackend backend = compiler.backend;
921 return instructionType.satisfies(backend.jsArrayClass, classWorld);
922 }
923
924 bool isMutableIndexable(Compiler compiler) {
925 ClassWorld classWorld = compiler.world;
926 JavaScriptBackend backend = compiler.backend;
927 return instructionType.satisfies(
928 backend.jsMutableIndexableClass, classWorld);
929 }
930
931 bool isArray(Compiler compiler) => isReadableArray(compiler);
932
933 bool canBePrimitiveString(Compiler compiler) {
934 ClassWorld classWorld = compiler.world;
935 JavaScriptBackend backend = compiler.backend;
936 return instructionType.contains(backend.jsStringClass, classWorld);
937 }
938
939 bool isInteger(Compiler compiler) {
940 ClassWorld classWorld = compiler.world;
941 return instructionType.containsOnlyInt(classWorld)
942 && !instructionType.isNullable;
943 }
944
945 bool isUInt32(Compiler compiler) {
946 ClassWorld classWorld = compiler.world;
947 JavaScriptBackend backend = compiler.backend;
948 return !instructionType.isNullable
949 && instructionType.satisfies(backend.jsUInt32Class, classWorld);
950 }
951
952 bool isUInt31(Compiler compiler) {
953 ClassWorld classWorld = compiler.world;
954 JavaScriptBackend backend = compiler.backend;
955 return !instructionType.isNullable
956 && instructionType.satisfies(backend.jsUInt31Class, classWorld);
957 }
958
959 bool isPositiveInteger(Compiler compiler) {
960 ClassWorld classWorld = compiler.world;
961 JavaScriptBackend backend = compiler.backend;
962 return !instructionType.isNullable
963 && instructionType.satisfies(backend.jsPositiveIntClass, classWorld);
964 }
965
966 bool isPositiveIntegerOrNull(Compiler compiler) {
967 ClassWorld classWorld = compiler.world;
968 JavaScriptBackend backend = compiler.backend;
969 return instructionType.satisfies(backend.jsPositiveIntClass, classWorld);
970 }
971
972 bool isIntegerOrNull(Compiler compiler) {
973 ClassWorld classWorld = compiler.world;
974 return instructionType.containsOnlyInt(classWorld);
975 }
976
977 bool isNumber(Compiler compiler) {
978 ClassWorld classWorld = compiler.world;
979 return instructionType.containsOnlyNum(classWorld)
980 && !instructionType.isNullable;
981 }
982
983 bool isNumberOrNull(Compiler compiler) {
984 ClassWorld classWorld = compiler.world;
985 return instructionType.containsOnlyNum(classWorld);
986 }
987
988 bool isDouble(Compiler compiler) {
989 ClassWorld classWorld = compiler.world;
990 return instructionType.containsOnlyDouble(classWorld)
991 && !instructionType.isNullable;
992 }
993
994 bool isDoubleOrNull(Compiler compiler) {
995 ClassWorld classWorld = compiler.world;
996 return instructionType.containsOnlyDouble(classWorld);
997 }
998
999 bool isBoolean(Compiler compiler) {
1000 ClassWorld classWorld = compiler.world;
1001 return instructionType.containsOnlyBool(classWorld)
1002 && !instructionType.isNullable;
1003 }
1004
1005 bool isBooleanOrNull(Compiler compiler) {
1006 ClassWorld classWorld = compiler.world;
1007 return instructionType.containsOnlyBool(classWorld);
1008 }
1009
1010 bool isString(Compiler compiler) {
1011 ClassWorld classWorld = compiler.world;
1012 return instructionType.containsOnlyString(classWorld)
1013 && !instructionType.isNullable;
1014 }
1015
1016 bool isStringOrNull(Compiler compiler) {
1017 ClassWorld classWorld = compiler.world;
1018 return instructionType.containsOnlyString(classWorld);
1019 }
1020
1021 bool isPrimitive(Compiler compiler) {
1022 return (isPrimitiveOrNull(compiler) && !instructionType.isNullable)
1023 || isNull();
1024 }
1025
1026 bool isPrimitiveOrNull(Compiler compiler) {
1027 return isIndexablePrimitive(compiler)
1028 || isNumberOrNull(compiler)
1029 || isBooleanOrNull(compiler)
1030 || isNull();
1031 }
1032
1033 /**
1034 * Type of the instruction.
1035 */
1036 TypeMask instructionType;
1037
1038 Selector get selector => null;
1039 HInstruction getDartReceiver(Compiler compiler) => null;
1040 bool onlyThrowsNSM() => false;
1041
1042 bool isInBasicBlock() => block != null;
1043
1044 bool gvnEquals(HInstruction other) {
1045 assert(useGvn() && other.useGvn());
1046 // Check that the type and the sideEffects match.
1047 bool hasSameType = typeEquals(other);
1048 assert(hasSameType == (typeCode() == other.typeCode()));
1049 if (!hasSameType) return false;
1050 if (sideEffects != other.sideEffects) return false;
1051 // Check that the inputs match.
1052 final int inputsLength = inputs.length;
1053 final List<HInstruction> otherInputs = other.inputs;
1054 if (inputsLength != otherInputs.length) return false;
1055 for (int i = 0; i < inputsLength; i++) {
1056 if (!identical(inputs[i].nonCheck(), otherInputs[i].nonCheck())) {
1057 return false;
1058 }
1059 }
1060 // Check that the data in the instruction matches.
1061 return dataEquals(other);
1062 }
1063
1064 int gvnHashCode() {
1065 int result = typeCode();
1066 int length = inputs.length;
1067 for (int i = 0; i < length; i++) {
1068 result = (result * 19) + (inputs[i].nonCheck().id) + (result >> 7);
1069 }
1070 return result;
1071 }
1072
1073 // These methods should be overwritten by instructions that
1074 // participate in global value numbering.
1075 int typeCode() => HInstruction.UNDEFINED_TYPECODE;
1076 bool typeEquals(HInstruction other) => false;
1077 bool dataEquals(HInstruction other) => false;
1078
1079 accept(HVisitor visitor);
1080
1081 void notifyAddedToBlock(HBasicBlock targetBlock) {
1082 assert(!isInBasicBlock());
1083 assert(block == null);
1084 // Add [this] to the inputs' uses.
1085 for (int i = 0; i < inputs.length; i++) {
1086 assert(inputs[i].isInBasicBlock());
1087 inputs[i].usedBy.add(this);
1088 }
1089 block = targetBlock;
1090 assert(isValid());
1091 }
1092
1093 void notifyRemovedFromBlock() {
1094 assert(isInBasicBlock());
1095 assert(usedBy.isEmpty);
1096
1097 // Remove [this] from the inputs' uses.
1098 for (int i = 0; i < inputs.length; i++) {
1099 inputs[i].removeUser(this);
1100 }
1101 this.block = null;
1102 assert(isValid());
1103 }
1104
1105 /// Do a in-place change of [from] to [to]. Warning: this function
1106 /// does not update [inputs] and [usedBy]. Use [changeUse] instead.
1107 void rewriteInput(HInstruction from, HInstruction to) {
1108 for (int i = 0; i < inputs.length; i++) {
1109 if (identical(inputs[i], from)) inputs[i] = to;
1110 }
1111 }
1112
1113 /** Removes all occurrences of [instruction] from [list]. */
1114 void removeFromList(List<HInstruction> list, HInstruction instruction) {
1115 int length = list.length;
1116 int i = 0;
1117 while (i < length) {
1118 if (instruction == list[i]) {
1119 list[i] = list[length - 1];
1120 length--;
1121 } else {
1122 i++;
1123 }
1124 }
1125 list.length = length;
1126 }
1127
1128 /** Removes all occurrences of [user] from [usedBy]. */
1129 void removeUser(HInstruction user) {
1130 removeFromList(usedBy, user);
1131 }
1132
1133 // Change all uses of [oldInput] by [this] to [newInput]. Also
1134 // updates the [usedBy] of [oldInput] and [newInput].
1135 void changeUse(HInstruction oldInput, HInstruction newInput) {
1136 assert(newInput != null && !identical(oldInput, newInput));
1137 for (int i = 0; i < inputs.length; i++) {
1138 if (identical(inputs[i], oldInput)) {
1139 inputs[i] = newInput;
1140 newInput.usedBy.add(this);
1141 }
1142 }
1143 removeFromList(oldInput.usedBy, this);
1144 }
1145
1146 // Compute the set of users of this instruction that is dominated by
1147 // [other]. If [other] is a user of [this], it is included in the
1148 // returned set.
1149 Setlet<HInstruction> dominatedUsers(HInstruction other) {
1150 // Keep track of all instructions that we have to deal with later
1151 // and count the number of them that are in the current block.
1152 Setlet<HInstruction> users = new Setlet<HInstruction>();
1153 int usersInCurrentBlock = 0;
1154
1155 // Run through all the users and see if they are dominated or
1156 // potentially dominated by [other].
1157 HBasicBlock otherBlock = other.block;
1158 for (int i = 0, length = usedBy.length; i < length; i++) {
1159 HInstruction current = usedBy[i];
1160 if (otherBlock.dominates(current.block)) {
1161 if (identical(current.block, otherBlock)) usersInCurrentBlock++;
1162 users.add(current);
1163 }
1164 }
1165
1166 // Run through all the phis in the same block as [other] and remove them
1167 // from the users set.
1168 if (usersInCurrentBlock > 0) {
1169 for (HPhi phi = otherBlock.phis.first; phi != null; phi = phi.next) {
1170 if (users.contains(phi)) {
1171 users.remove(phi);
1172 if (--usersInCurrentBlock == 0) break;
1173 }
1174 }
1175 }
1176
1177 // Run through all the instructions before [other] and remove them
1178 // from the users set.
1179 if (usersInCurrentBlock > 0) {
1180 HInstruction current = otherBlock.first;
1181 while (!identical(current, other)) {
1182 if (users.contains(current)) {
1183 users.remove(current);
1184 if (--usersInCurrentBlock == 0) break;
1185 }
1186 current = current.next;
1187 }
1188 }
1189
1190 return users;
1191 }
1192
1193 void replaceAllUsersDominatedBy(HInstruction cursor,
1194 HInstruction newInstruction) {
1195 Setlet<HInstruction> users = dominatedUsers(cursor);
1196 for (HInstruction user in users) {
1197 user.changeUse(this, newInstruction);
1198 }
1199 }
1200
1201 void moveBefore(HInstruction other) {
1202 assert(this is !HControlFlow);
1203 assert(this is !HPhi);
1204 assert(other is !HPhi);
1205 block.detach(this);
1206 other.block.internalAddBefore(other, this);
1207 block = other.block;
1208 }
1209
1210 bool isConstant() => false;
1211 bool isConstantBoolean() => false;
1212 bool isConstantNull() => false;
1213 bool isConstantNumber() => false;
1214 bool isConstantInteger() => false;
1215 bool isConstantString() => false;
1216 bool isConstantList() => false;
1217 bool isConstantMap() => false;
1218 bool isConstantFalse() => false;
1219 bool isConstantTrue() => false;
1220
1221 bool isInterceptor(Compiler compiler) => false;
1222
1223 bool isValid() {
1224 HValidator validator = new HValidator();
1225 validator.currentBlock = block;
1226 validator.visitInstruction(this);
1227 return validator.isValid;
1228 }
1229
1230 bool isCodeMotionInvariant() => false;
1231
1232 bool isJsStatement() => false;
1233
1234 bool dominates(HInstruction other) {
1235 // An instruction does not dominates itself.
1236 if (this == other) return false;
1237 if (block != other.block) return block.dominates(other.block);
1238
1239 HInstruction current = this.next;
1240 while (current != null) {
1241 if (current == other) return true;
1242 current = current.next;
1243 }
1244 return false;
1245 }
1246
1247 HInstruction convertType(Compiler compiler, DartType type, int kind) {
1248 if (type == null) return this;
1249 type = type.unalias(compiler);
1250 // Only the builder knows how to create [HTypeConversion]
1251 // instructions with generics. It has the generic type context
1252 // available.
1253 assert(type.kind != TypeKind.TYPE_VARIABLE);
1254 assert(type.treatAsRaw || type.isFunctionType);
1255 if (type.isDynamic) return this;
1256 // The type element is either a class or the void element.
1257 Element element = type.element;
1258 if (identical(element, compiler.objectClass)) return this;
1259 JavaScriptBackend backend = compiler.backend;
1260 if (type.kind != TypeKind.INTERFACE) {
1261 return new HTypeConversion(type, kind, backend.dynamicType, this);
1262 } else if (kind == HTypeConversion.BOOLEAN_CONVERSION_CHECK) {
1263 // Boolean conversion checks work on non-nullable booleans.
1264 return new HTypeConversion(type, kind, backend.boolType, this);
1265 } else if (kind == HTypeConversion.CHECKED_MODE_CHECK && !type.treatAsRaw) {
1266 throw 'creating compound check to $type (this = ${this})';
1267 } else {
1268 TypeMask subtype = new TypeMask.subtype(element.declaration,
1269 compiler.world);
1270 return new HTypeConversion(type, kind, subtype, this);
1271 }
1272 }
1273
1274 /**
1275 * Return whether the instructions do not belong to a loop or
1276 * belong to the same loop.
1277 */
1278 bool hasSameLoopHeaderAs(HInstruction other) {
1279 return block.enclosingLoopHeader == other.block.enclosingLoopHeader;
1280 }
1281 }
1282
1283 /**
1284 * Late instructions are used after the main optimization phases. They capture
1285 * codegen decisions just prior to generating JavaScript.
1286 */
1287 abstract class HLateInstruction extends HInstruction {
1288 HLateInstruction(List<HInstruction> inputs, TypeMask type)
1289 : super(inputs, type);
1290 }
1291
1292 class HBoolify extends HInstruction {
1293 HBoolify(HInstruction value, TypeMask type)
1294 : super(<HInstruction>[value], type) {
1295 setUseGvn();
1296 }
1297
1298 accept(HVisitor visitor) => visitor.visitBoolify(this);
1299 int typeCode() => HInstruction.BOOLIFY_TYPECODE;
1300 bool typeEquals(other) => other is HBoolify;
1301 bool dataEquals(HInstruction other) => true;
1302 }
1303
1304 /**
1305 * A [HCheck] instruction is an instruction that might do a dynamic
1306 * check at runtime on another instruction. To have proper instruction
1307 * dependencies in the graph, instructions that depend on the check
1308 * being done reference the [HCheck] instruction instead of the
1309 * instruction itself.
1310 */
1311 abstract class HCheck extends HInstruction {
1312 HCheck(inputs, type) : super(inputs, type) {
1313 setUseGvn();
1314 }
1315 HInstruction get checkedInput => inputs[0];
1316 bool isJsStatement() => true;
1317 bool canThrow() => true;
1318
1319 HInstruction nonCheck() => checkedInput.nonCheck();
1320 }
1321
1322 class HBoundsCheck extends HCheck {
1323 static const int ALWAYS_FALSE = 0;
1324 static const int FULL_CHECK = 1;
1325 static const int ALWAYS_ABOVE_ZERO = 2;
1326 static const int ALWAYS_BELOW_LENGTH = 3;
1327 static const int ALWAYS_TRUE = 4;
1328 /**
1329 * Details which tests have been done statically during compilation.
1330 * Default is that all checks must be performed dynamically.
1331 */
1332 int staticChecks = FULL_CHECK;
1333
1334 HBoundsCheck(length, index, array, type)
1335 : super(<HInstruction>[length, index, array], type);
1336
1337 HInstruction get length => inputs[1];
1338 HInstruction get index => inputs[0];
1339 HInstruction get array => inputs[2];
1340 bool isControlFlow() => true;
1341
1342 accept(HVisitor visitor) => visitor.visitBoundsCheck(this);
1343 int typeCode() => HInstruction.BOUNDS_CHECK_TYPECODE;
1344 bool typeEquals(other) => other is HBoundsCheck;
1345 bool dataEquals(HInstruction other) => true;
1346 }
1347
1348 abstract class HConditionalBranch extends HControlFlow {
1349 HConditionalBranch(inputs) : super(inputs);
1350 HInstruction get condition => inputs[0];
1351 HBasicBlock get trueBranch => block.successors[0];
1352 HBasicBlock get falseBranch => block.successors[1];
1353 }
1354
1355 abstract class HControlFlow extends HInstruction {
1356 HControlFlow(inputs) : super(inputs, const TypeMask.nonNullEmpty());
1357 bool isControlFlow() => true;
1358 bool isJsStatement() => true;
1359 }
1360
1361 abstract class HInvoke extends HInstruction {
1362 /**
1363 * The first argument must be the target: either an [HStatic] node, or
1364 * the receiver of a method-call. The remaining inputs are the arguments
1365 * to the invocation.
1366 */
1367 HInvoke(List<HInstruction> inputs, type) : super(inputs, type) {
1368 sideEffects.setAllSideEffects();
1369 sideEffects.setDependsOnSomething();
1370 }
1371 static const int ARGUMENTS_OFFSET = 1;
1372 bool canThrow() => true;
1373
1374 /**
1375 * Returns whether this call is on an intercepted method.
1376 */
1377 bool get isInterceptedCall {
1378 // We know it's a selector call if it follows the interceptor
1379 // calling convention, which adds the actual receiver as a
1380 // parameter to the call.
1381 return (selector != null) && (inputs.length - 2 == selector.argumentCount);
1382 }
1383 }
1384
1385 abstract class HInvokeDynamic extends HInvoke {
1386 final InvokeDynamicSpecializer specializer;
1387 Selector selector;
1388 Element element;
1389
1390 HInvokeDynamic(Selector selector,
1391 this.element,
1392 List<HInstruction> inputs,
1393 TypeMask type,
1394 [bool isIntercepted = false])
1395 : super(inputs, type),
1396 this.selector = selector,
1397 specializer = isIntercepted
1398 ? InvokeDynamicSpecializer.lookupSpecializer(selector)
1399 : const InvokeDynamicSpecializer();
1400 toString() => 'invoke dynamic: $selector';
1401 HInstruction get receiver => inputs[0];
1402 HInstruction getDartReceiver(Compiler compiler) {
1403 return isCallOnInterceptor(compiler) ? inputs[1] : inputs[0];
1404 }
1405
1406 /**
1407 * Returns whether this call is on an interceptor object.
1408 */
1409 bool isCallOnInterceptor(Compiler compiler) {
1410 return isInterceptedCall && receiver.isInterceptor(compiler);
1411 }
1412
1413 int typeCode() => HInstruction.INVOKE_DYNAMIC_TYPECODE;
1414 bool typeEquals(other) => other is HInvokeDynamic;
1415 bool dataEquals(HInvokeDynamic other) {
1416 // Use the name and the kind instead of [Selector.operator==]
1417 // because we don't need to check the arity (already checked in
1418 // [gvnEquals]), and the receiver types may not be in sync.
1419 return selector.name == other.selector.name
1420 && selector.kind == other.selector.kind;
1421 }
1422 }
1423
1424 class HInvokeClosure extends HInvokeDynamic {
1425 HInvokeClosure(Selector selector, List<HInstruction> inputs, TypeMask type)
1426 : super(selector, null, inputs, type) {
1427 assert(selector.isClosureCall);
1428 }
1429 accept(HVisitor visitor) => visitor.visitInvokeClosure(this);
1430 }
1431
1432 class HInvokeDynamicMethod extends HInvokeDynamic {
1433 HInvokeDynamicMethod(Selector selector,
1434 List<HInstruction> inputs,
1435 TypeMask type,
1436 [bool isIntercepted = false])
1437 : super(selector, null, inputs, type, isIntercepted);
1438
1439 String toString() => 'invoke dynamic method: $selector';
1440 accept(HVisitor visitor) => visitor.visitInvokeDynamicMethod(this);
1441 }
1442
1443 abstract class HInvokeDynamicField extends HInvokeDynamic {
1444 HInvokeDynamicField(
1445 Selector selector, Element element, List<HInstruction> inputs,
1446 TypeMask type)
1447 : super(selector, element, inputs, type);
1448 toString() => 'invoke dynamic field: $selector';
1449 }
1450
1451 class HInvokeDynamicGetter extends HInvokeDynamicField {
1452 HInvokeDynamicGetter(selector, element, inputs, type)
1453 : super(selector, element, inputs, type);
1454 toString() => 'invoke dynamic getter: $selector';
1455 accept(HVisitor visitor) => visitor.visitInvokeDynamicGetter(this);
1456 }
1457
1458 class HInvokeDynamicSetter extends HInvokeDynamicField {
1459 HInvokeDynamicSetter(selector, element, inputs, type)
1460 : super(selector, element, inputs, type);
1461 toString() => 'invoke dynamic setter: $selector';
1462 accept(HVisitor visitor) => visitor.visitInvokeDynamicSetter(this);
1463 }
1464
1465 class HInvokeStatic extends HInvoke {
1466 final Element element;
1467
1468 final bool targetCanThrow;
1469
1470 bool canThrow() => targetCanThrow;
1471
1472 /// If this instruction is a call to a constructor, [instantiatedTypes]
1473 /// contains the type(s) used in the (Dart) `New` expression(s). The
1474 /// [instructionType] of this node is not enough, because we also need the
1475 /// type arguments. See also [SsaFromAstMixin.currentInlinedInstantiations].
1476 List<DartType> instantiatedTypes;
1477
1478 /** The first input must be the target. */
1479 HInvokeStatic(this.element, inputs, TypeMask type,
1480 {this.targetCanThrow: true})
1481 : super(inputs, type);
1482
1483 toString() => 'invoke static: $element';
1484 accept(HVisitor visitor) => visitor.visitInvokeStatic(this);
1485 int typeCode() => HInstruction.INVOKE_STATIC_TYPECODE;
1486 }
1487
1488 class HInvokeSuper extends HInvokeStatic {
1489 /** The class where the call to super is being done. */
1490 final ClassElement caller;
1491 final bool isSetter;
1492 final Selector selector;
1493
1494 HInvokeSuper(Element element,
1495 this.caller,
1496 this.selector,
1497 inputs,
1498 type,
1499 {this.isSetter})
1500 : super(element, inputs, type);
1501 toString() => 'invoke super: ${element.name}';
1502 accept(HVisitor visitor) => visitor.visitInvokeSuper(this);
1503
1504 HInstruction get value {
1505 assert(isSetter);
1506 // The 'inputs' are [receiver, value] or [interceptor, receiver, value].
1507 return inputs.last;
1508 }
1509 }
1510
1511 class HInvokeConstructorBody extends HInvokeStatic {
1512 // The 'inputs' are
1513 // [receiver, arg1, ..., argN] or
1514 // [interceptor, receiver, arg1, ... argN].
1515 HInvokeConstructorBody(element, inputs, type)
1516 : super(element, inputs, type);
1517
1518 String toString() => 'invoke constructor body: ${element.name}';
1519 accept(HVisitor visitor) => visitor.visitInvokeConstructorBody(this);
1520 }
1521
1522 abstract class HFieldAccess extends HInstruction {
1523 final Element element;
1524
1525 HFieldAccess(Element element, List<HInstruction> inputs, TypeMask type)
1526 : this.element = element, super(inputs, type);
1527
1528 HInstruction get receiver => inputs[0];
1529 }
1530
1531 class HFieldGet extends HFieldAccess {
1532 final bool isAssignable;
1533
1534 HFieldGet(Element element,
1535 HInstruction receiver,
1536 TypeMask type,
1537 {bool isAssignable})
1538 : this.isAssignable = (isAssignable != null)
1539 ? isAssignable
1540 : element.isAssignable,
1541 super(element, <HInstruction>[receiver], type) {
1542 sideEffects.clearAllSideEffects();
1543 sideEffects.clearAllDependencies();
1544 setUseGvn();
1545 if (this.isAssignable) {
1546 sideEffects.setDependsOnInstancePropertyStore();
1547 }
1548 }
1549
1550 bool isInterceptor(Compiler compiler) {
1551 if (sourceElement == null) return false;
1552 // In case of a closure inside an interceptor class, [:this:] is
1553 // stored in the generated closure class, and accessed through a
1554 // [HFieldGet].
1555 JavaScriptBackend backend = compiler.backend;
1556 if (sourceElement is ThisLocal) {
1557 ThisLocal thisLocal = sourceElement;
1558 return backend.isInterceptorClass(thisLocal.enclosingClass);
1559 }
1560 return false;
1561 }
1562
1563 bool canThrow() => receiver.canBeNull();
1564
1565 HInstruction getDartReceiver(Compiler compiler) => receiver;
1566 bool onlyThrowsNSM() => true;
1567 bool get isNullCheck => element == null;
1568
1569 accept(HVisitor visitor) => visitor.visitFieldGet(this);
1570
1571 int typeCode() => HInstruction.FIELD_GET_TYPECODE;
1572 bool typeEquals(other) => other is HFieldGet;
1573 bool dataEquals(HFieldGet other) => element == other.element;
1574 String toString() => "FieldGet $element";
1575 }
1576
1577 class HFieldSet extends HFieldAccess {
1578 HFieldSet(Element element,
1579 HInstruction receiver,
1580 HInstruction value)
1581 : super(element, <HInstruction>[receiver, value],
1582 const TypeMask.nonNullEmpty()) {
1583 sideEffects.clearAllSideEffects();
1584 sideEffects.clearAllDependencies();
1585 sideEffects.setChangesInstanceProperty();
1586 }
1587
1588 bool canThrow() => receiver.canBeNull();
1589
1590 HInstruction getDartReceiver(Compiler compiler) => receiver;
1591 bool onlyThrowsNSM() => true;
1592
1593 HInstruction get value => inputs[1];
1594 accept(HVisitor visitor) => visitor.visitFieldSet(this);
1595
1596 bool isJsStatement() => true;
1597 String toString() => "FieldSet $element";
1598 }
1599
1600 /**
1601 * HReadModifyWrite is a late stage instruction for a field (property) update
1602 * via an assignment operation or pre- or post-increment.
1603 */
1604 class HReadModifyWrite extends HLateInstruction {
1605 static const ASSIGN_OP = 0;
1606 static const PRE_OP = 1;
1607 static const POST_OP = 2;
1608 final Element element;
1609 final String jsOp;
1610 final int opKind;
1611
1612 HReadModifyWrite._(Element this.element, this.jsOp, this.opKind,
1613 List<HInstruction> inputs, TypeMask type)
1614 : super(inputs, type) {
1615 sideEffects.clearAllSideEffects();
1616 sideEffects.clearAllDependencies();
1617 sideEffects.setChangesInstanceProperty();
1618 sideEffects.setDependsOnInstancePropertyStore();
1619 }
1620
1621 HReadModifyWrite.assignOp(Element element, String jsOp,
1622 HInstruction receiver, HInstruction operand, TypeMask type)
1623 : this._(element, jsOp, ASSIGN_OP,
1624 <HInstruction>[receiver, operand], type);
1625
1626 HReadModifyWrite.preOp(Element element, String jsOp,
1627 HInstruction receiver, TypeMask type)
1628 : this._(element, jsOp, PRE_OP, <HInstruction>[receiver], type);
1629
1630 HReadModifyWrite.postOp(Element element, String jsOp,
1631 HInstruction receiver, TypeMask type)
1632 : this._(element, jsOp, POST_OP, <HInstruction>[receiver], type);
1633
1634 HInstruction get receiver => inputs[0];
1635
1636 bool get isPreOp => opKind == PRE_OP;
1637 bool get isPostOp => opKind == POST_OP;
1638 bool get isAssignOp => opKind == ASSIGN_OP;
1639
1640 bool canThrow() => receiver.canBeNull();
1641
1642 HInstruction getDartReceiver(Compiler compiler) => receiver;
1643 bool onlyThrowsNSM() => true;
1644
1645 HInstruction get value => inputs[1];
1646 accept(HVisitor visitor) => visitor.visitReadModifyWrite(this);
1647
1648 bool isJsStatement() => isAssignOp;
1649 String toString() => "ReadModifyWrite $jsOp $opKind $element";
1650 }
1651
1652 abstract class HLocalAccess extends HInstruction {
1653 final Local variable;
1654
1655 HLocalAccess(this.variable, List<HInstruction> inputs, TypeMask type)
1656 : super(inputs, type);
1657
1658 HInstruction get receiver => inputs[0];
1659 }
1660
1661 class HLocalGet extends HLocalAccess {
1662 // No need to use GVN for a [HLocalGet], it is just a local
1663 // access.
1664 HLocalGet(Local variable, HLocalValue local, TypeMask type)
1665 : super(variable, <HInstruction>[local], type);
1666
1667 accept(HVisitor visitor) => visitor.visitLocalGet(this);
1668
1669 HLocalValue get local => inputs[0];
1670 }
1671
1672 class HLocalSet extends HLocalAccess {
1673 HLocalSet(Local variable, HLocalValue local, HInstruction value)
1674 : super(variable, <HInstruction>[local, value],
1675 const TypeMask.nonNullEmpty());
1676
1677 accept(HVisitor visitor) => visitor.visitLocalSet(this);
1678
1679 HLocalValue get local => inputs[0];
1680 HInstruction get value => inputs[1];
1681 bool isJsStatement() => true;
1682 }
1683
1684 class HForeign extends HInstruction {
1685 final js.Template codeTemplate;
1686 final bool isStatement;
1687 final bool _canThrow;
1688 final native.NativeBehavior nativeBehavior;
1689
1690 HForeign(this.codeTemplate,
1691 TypeMask type,
1692 List<HInstruction> inputs,
1693 {this.isStatement: false,
1694 SideEffects effects,
1695 native.NativeBehavior nativeBehavior,
1696 canThrow: false})
1697 : this.nativeBehavior = nativeBehavior,
1698 this._canThrow = canThrow,
1699 super(inputs, type) {
1700 if (effects == null && nativeBehavior != null) {
1701 effects = nativeBehavior.sideEffects;
1702 }
1703 if (effects != null) sideEffects.add(effects);
1704 }
1705
1706 HForeign.statement(codeTemplate, List<HInstruction> inputs,
1707 SideEffects effects,
1708 native.NativeBehavior nativeBehavior,
1709 TypeMask type)
1710 : this(codeTemplate, type, inputs, isStatement: true,
1711 effects: effects, nativeBehavior: nativeBehavior);
1712
1713 accept(HVisitor visitor) => visitor.visitForeign(this);
1714
1715 bool isJsStatement() => isStatement;
1716 bool canThrow() {
1717 return _canThrow
1718 || sideEffects.hasSideEffects()
1719 || sideEffects.dependsOnSomething();
1720 }
1721 }
1722
1723 class HForeignNew extends HForeign {
1724 ClassElement element;
1725
1726 /// If this field is not `null`, this call is from an inlined constructor and
1727 /// we have to register the instantiated type in the code generator. The
1728 /// [instructionType] of this node is not enough, because we also need the
1729 /// type arguments. See also [SsaFromAstMixin.currentInlinedInstantiations].
1730 List<DartType> instantiatedTypes;
1731
1732 HForeignNew(this.element, TypeMask type, List<HInstruction> inputs,
1733 [this.instantiatedTypes])
1734 : super(null, type, inputs);
1735
1736 accept(HVisitor visitor) => visitor.visitForeignNew(this);
1737 }
1738
1739 abstract class HInvokeBinary extends HInstruction {
1740 final Selector selector;
1741 HInvokeBinary(HInstruction left, HInstruction right, this.selector, type)
1742 : super(<HInstruction>[left, right], type) {
1743 sideEffects.clearAllSideEffects();
1744 sideEffects.clearAllDependencies();
1745 setUseGvn();
1746 }
1747
1748 HInstruction get left => inputs[0];
1749 HInstruction get right => inputs[1];
1750
1751 BinaryOperation operation(ConstantSystem constantSystem);
1752 }
1753
1754 abstract class HBinaryArithmetic extends HInvokeBinary {
1755 HBinaryArithmetic(left, right, selector, type)
1756 : super(left, right, selector, type);
1757 BinaryOperation operation(ConstantSystem constantSystem);
1758 }
1759
1760 class HAdd extends HBinaryArithmetic {
1761 HAdd(left, right, selector, type) : super(left, right, selector, type);
1762 accept(HVisitor visitor) => visitor.visitAdd(this);
1763
1764 BinaryOperation operation(ConstantSystem constantSystem)
1765 => constantSystem.add;
1766 int typeCode() => HInstruction.ADD_TYPECODE;
1767 bool typeEquals(other) => other is HAdd;
1768 bool dataEquals(HInstruction other) => true;
1769 }
1770
1771 class HDivide extends HBinaryArithmetic {
1772 HDivide(left, right, selector, type) : super(left, right, selector, type);
1773 accept(HVisitor visitor) => visitor.visitDivide(this);
1774
1775 BinaryOperation operation(ConstantSystem constantSystem)
1776 => constantSystem.divide;
1777 int typeCode() => HInstruction.DIVIDE_TYPECODE;
1778 bool typeEquals(other) => other is HDivide;
1779 bool dataEquals(HInstruction other) => true;
1780 }
1781
1782 class HMultiply extends HBinaryArithmetic {
1783 HMultiply(left, right, selector, type) : super(left, right, selector, type);
1784 accept(HVisitor visitor) => visitor.visitMultiply(this);
1785
1786 BinaryOperation operation(ConstantSystem operations)
1787 => operations.multiply;
1788 int typeCode() => HInstruction.MULTIPLY_TYPECODE;
1789 bool typeEquals(other) => other is HMultiply;
1790 bool dataEquals(HInstruction other) => true;
1791 }
1792
1793 class HSubtract extends HBinaryArithmetic {
1794 HSubtract(left, right, selector, type) : super(left, right, selector, type);
1795 accept(HVisitor visitor) => visitor.visitSubtract(this);
1796
1797 BinaryOperation operation(ConstantSystem constantSystem)
1798 => constantSystem.subtract;
1799 int typeCode() => HInstruction.SUBTRACT_TYPECODE;
1800 bool typeEquals(other) => other is HSubtract;
1801 bool dataEquals(HInstruction other) => true;
1802 }
1803
1804 class HTruncatingDivide extends HBinaryArithmetic {
1805 HTruncatingDivide(left, right, selector, type)
1806 : super(left, right, selector, type);
1807 accept(HVisitor visitor) => visitor.visitTruncatingDivide(this);
1808
1809 BinaryOperation operation(ConstantSystem constantSystem)
1810 => constantSystem.truncatingDivide;
1811 int typeCode() => HInstruction.TRUNCATING_DIVIDE_TYPECODE;
1812 bool typeEquals(other) => other is HTruncatingDivide;
1813 bool dataEquals(HInstruction other) => true;
1814 }
1815
1816 /**
1817 * An [HSwitch] instruction has one input for the incoming
1818 * value, and one input per constant that it can switch on.
1819 * Its block has one successor per constant, and one for the default.
1820 */
1821 class HSwitch extends HControlFlow {
1822 HSwitch(List<HInstruction> inputs) : super(inputs);
1823
1824 HConstant constant(int index) => inputs[index + 1];
1825 HInstruction get expression => inputs[0];
1826
1827 /**
1828 * Provides the target to jump to if none of the constants match
1829 * the expression. If the switch had no default case, this is the
1830 * following join-block.
1831 */
1832 HBasicBlock get defaultTarget => block.successors.last;
1833
1834 accept(HVisitor visitor) => visitor.visitSwitch(this);
1835
1836 String toString() => "HSwitch cases = $inputs";
1837 }
1838
1839 abstract class HBinaryBitOp extends HInvokeBinary {
1840 HBinaryBitOp(left, right, selector, type)
1841 : super(left, right, selector, type);
1842 }
1843
1844 class HShiftLeft extends HBinaryBitOp {
1845 HShiftLeft(left, right, selector, type) : super(left, right, selector, type);
1846 accept(HVisitor visitor) => visitor.visitShiftLeft(this);
1847
1848 BinaryOperation operation(ConstantSystem constantSystem)
1849 => constantSystem.shiftLeft;
1850 int typeCode() => HInstruction.SHIFT_LEFT_TYPECODE;
1851 bool typeEquals(other) => other is HShiftLeft;
1852 bool dataEquals(HInstruction other) => true;
1853 }
1854
1855 class HShiftRight extends HBinaryBitOp {
1856 HShiftRight(left, right, selector, type) : super(left, right, selector, type);
1857 accept(HVisitor visitor) => visitor.visitShiftRight(this);
1858
1859 BinaryOperation operation(ConstantSystem constantSystem)
1860 => constantSystem.shiftRight;
1861 int typeCode() => HInstruction.SHIFT_RIGHT_TYPECODE;
1862 bool typeEquals(other) => other is HShiftRight;
1863 bool dataEquals(HInstruction other) => true;
1864 }
1865
1866 class HBitOr extends HBinaryBitOp {
1867 HBitOr(left, right, selector, type) : super(left, right, selector, type);
1868 accept(HVisitor visitor) => visitor.visitBitOr(this);
1869
1870 BinaryOperation operation(ConstantSystem constantSystem)
1871 => constantSystem.bitOr;
1872 int typeCode() => HInstruction.BIT_OR_TYPECODE;
1873 bool typeEquals(other) => other is HBitOr;
1874 bool dataEquals(HInstruction other) => true;
1875 }
1876
1877 class HBitAnd extends HBinaryBitOp {
1878 HBitAnd(left, right, selector, type) : super(left, right, selector, type);
1879 accept(HVisitor visitor) => visitor.visitBitAnd(this);
1880
1881 BinaryOperation operation(ConstantSystem constantSystem)
1882 => constantSystem.bitAnd;
1883 int typeCode() => HInstruction.BIT_AND_TYPECODE;
1884 bool typeEquals(other) => other is HBitAnd;
1885 bool dataEquals(HInstruction other) => true;
1886 }
1887
1888 class HBitXor extends HBinaryBitOp {
1889 HBitXor(left, right, selector, type) : super(left, right, selector, type);
1890 accept(HVisitor visitor) => visitor.visitBitXor(this);
1891
1892 BinaryOperation operation(ConstantSystem constantSystem)
1893 => constantSystem.bitXor;
1894 int typeCode() => HInstruction.BIT_XOR_TYPECODE;
1895 bool typeEquals(other) => other is HBitXor;
1896 bool dataEquals(HInstruction other) => true;
1897 }
1898
1899 abstract class HInvokeUnary extends HInstruction {
1900 final Selector selector;
1901 HInvokeUnary(HInstruction input, this.selector, type)
1902 : super(<HInstruction>[input], type) {
1903 sideEffects.clearAllSideEffects();
1904 sideEffects.clearAllDependencies();
1905 setUseGvn();
1906 }
1907
1908 HInstruction get operand => inputs[0];
1909
1910 UnaryOperation operation(ConstantSystem constantSystem);
1911 }
1912
1913 class HNegate extends HInvokeUnary {
1914 HNegate(input, selector, type) : super(input, selector, type);
1915 accept(HVisitor visitor) => visitor.visitNegate(this);
1916
1917 UnaryOperation operation(ConstantSystem constantSystem)
1918 => constantSystem.negate;
1919 int typeCode() => HInstruction.NEGATE_TYPECODE;
1920 bool typeEquals(other) => other is HNegate;
1921 bool dataEquals(HInstruction other) => true;
1922 }
1923
1924 class HBitNot extends HInvokeUnary {
1925 HBitNot(input, selector, type) : super(input, selector, type);
1926 accept(HVisitor visitor) => visitor.visitBitNot(this);
1927
1928 UnaryOperation operation(ConstantSystem constantSystem)
1929 => constantSystem.bitNot;
1930 int typeCode() => HInstruction.BIT_NOT_TYPECODE;
1931 bool typeEquals(other) => other is HBitNot;
1932 bool dataEquals(HInstruction other) => true;
1933 }
1934
1935 class HExit extends HControlFlow {
1936 HExit() : super(const <HInstruction>[]);
1937 toString() => 'exit';
1938 accept(HVisitor visitor) => visitor.visitExit(this);
1939 }
1940
1941 class HGoto extends HControlFlow {
1942 HGoto() : super(const <HInstruction>[]);
1943 toString() => 'goto';
1944 accept(HVisitor visitor) => visitor.visitGoto(this);
1945 }
1946
1947 abstract class HJump extends HControlFlow {
1948 final JumpTarget target;
1949 final LabelDefinition label;
1950 HJump(this.target) : label = null, super(const <HInstruction>[]);
1951 HJump.toLabel(LabelDefinition label)
1952 : label = label, target = label.target, super(const <HInstruction>[]);
1953 }
1954
1955 class HBreak extends HJump {
1956 /**
1957 * Signals that this is a special break instruction for the synthetic loop
1958 * generatedfor a switch statement with continue statements. See
1959 * [SsaFromAstMixin.buildComplexSwitchStatement] for detail.
1960 */
1961 final bool breakSwitchContinueLoop;
1962 HBreak(JumpTarget target, {bool this.breakSwitchContinueLoop: false})
1963 : super(target);
1964 HBreak.toLabel(LabelDefinition label)
1965 : breakSwitchContinueLoop = false, super.toLabel(label);
1966 toString() => (label != null) ? 'break ${label.labelName}' : 'break';
1967 accept(HVisitor visitor) => visitor.visitBreak(this);
1968 }
1969
1970 class HContinue extends HJump {
1971 HContinue(JumpTarget target) : super(target);
1972 HContinue.toLabel(LabelDefinition label) : super.toLabel(label);
1973 toString() => (label != null) ? 'continue ${label.labelName}' : 'continue';
1974 accept(HVisitor visitor) => visitor.visitContinue(this);
1975 }
1976
1977 class HTry extends HControlFlow {
1978 HLocalValue exception;
1979 HBasicBlock catchBlock;
1980 HBasicBlock finallyBlock;
1981 HTry() : super(const <HInstruction>[]);
1982 toString() => 'try';
1983 accept(HVisitor visitor) => visitor.visitTry(this);
1984 HBasicBlock get joinBlock => this.block.successors.last;
1985 }
1986
1987 // An [HExitTry] control flow node is used when the body of a try or
1988 // the body of a catch contains a return, break or continue. To build
1989 // the control flow graph, we explicitly mark the body that
1990 // leads to one of this instruction a predecessor of catch and
1991 // finally.
1992 class HExitTry extends HControlFlow {
1993 HExitTry() : super(const <HInstruction>[]);
1994 toString() => 'exit try';
1995 accept(HVisitor visitor) => visitor.visitExitTry(this);
1996 HBasicBlock get bodyTrySuccessor => block.successors[0];
1997 }
1998
1999 class HIf extends HConditionalBranch {
2000 HBlockFlow blockInformation = null;
2001 HIf(HInstruction condition) : super(<HInstruction>[condition]);
2002 toString() => 'if';
2003 accept(HVisitor visitor) => visitor.visitIf(this);
2004
2005 HBasicBlock get thenBlock {
2006 assert(identical(block.dominatedBlocks[0], block.successors[0]));
2007 return block.successors[0];
2008 }
2009
2010 HBasicBlock get elseBlock {
2011 assert(identical(block.dominatedBlocks[1], block.successors[1]));
2012 return block.successors[1];
2013 }
2014
2015 HBasicBlock get joinBlock => blockInformation.continuation;
2016 }
2017
2018 class HLoopBranch extends HConditionalBranch {
2019 static const int CONDITION_FIRST_LOOP = 0;
2020 static const int DO_WHILE_LOOP = 1;
2021
2022 final int kind;
2023 HLoopBranch(HInstruction condition, [this.kind = CONDITION_FIRST_LOOP])
2024 : super(<HInstruction>[condition]);
2025 toString() => 'loop-branch';
2026 accept(HVisitor visitor) => visitor.visitLoopBranch(this);
2027 }
2028
2029 class HConstant extends HInstruction {
2030 final ConstantValue constant;
2031 HConstant.internal(this.constant, TypeMask constantType)
2032 : super(<HInstruction>[], constantType);
2033
2034 toString() => 'literal: $constant';
2035 accept(HVisitor visitor) => visitor.visitConstant(this);
2036
2037 bool isConstant() => true;
2038 bool isConstantBoolean() => constant.isBool;
2039 bool isConstantNull() => constant.isNull;
2040 bool isConstantNumber() => constant.isNum;
2041 bool isConstantInteger() => constant.isInt;
2042 bool isConstantString() => constant.isString;
2043 bool isConstantList() => constant.isList;
2044 bool isConstantMap() => constant.isMap;
2045 bool isConstantFalse() => constant.isFalse;
2046 bool isConstantTrue() => constant.isTrue;
2047
2048 bool isInterceptor(Compiler compiler) => constant.isInterceptor;
2049
2050 // Maybe avoid this if the literal is big?
2051 bool isCodeMotionInvariant() => true;
2052
2053 set instructionType(type) {
2054 // Only lists can be specialized. The SSA builder uses the
2055 // inferrer for finding the type of a constant list. We should
2056 // have the constant know its type instead.
2057 if (!isConstantList()) return;
2058 super.instructionType = type;
2059 }
2060 }
2061
2062 class HNot extends HInstruction {
2063 HNot(HInstruction value, TypeMask type) : super(<HInstruction>[value], type) {
2064 setUseGvn();
2065 }
2066
2067 accept(HVisitor visitor) => visitor.visitNot(this);
2068 int typeCode() => HInstruction.NOT_TYPECODE;
2069 bool typeEquals(other) => other is HNot;
2070 bool dataEquals(HInstruction other) => true;
2071 }
2072
2073 /**
2074 * An [HLocalValue] represents a local. Unlike [HParameterValue]s its
2075 * first use must be in an HLocalSet. That is, [HParameterValue]s have a
2076 * value from the start, whereas [HLocalValue]s need to be initialized first.
2077 */
2078 class HLocalValue extends HInstruction {
2079 HLocalValue(Entity variable, TypeMask type)
2080 : super(<HInstruction>[], type) {
2081 sourceElement = variable;
2082 }
2083
2084 toString() => 'local ${sourceElement.name}';
2085 accept(HVisitor visitor) => visitor.visitLocalValue(this);
2086 }
2087
2088 class HParameterValue extends HLocalValue {
2089 HParameterValue(Entity variable, type) : super(variable, type);
2090
2091 toString() => 'parameter ${sourceElement.name}';
2092 accept(HVisitor visitor) => visitor.visitParameterValue(this);
2093 }
2094
2095 class HThis extends HParameterValue {
2096 HThis(ThisLocal element, TypeMask type) : super(element, type);
2097
2098 ThisLocal get sourceElement => super.sourceElement;
2099
2100 accept(HVisitor visitor) => visitor.visitThis(this);
2101
2102 bool isCodeMotionInvariant() => true;
2103
2104 bool isInterceptor(Compiler compiler) {
2105 JavaScriptBackend backend = compiler.backend;
2106 return backend.isInterceptorClass(sourceElement.enclosingClass);
2107 }
2108
2109 String toString() => 'this';
2110 }
2111
2112 class HPhi extends HInstruction {
2113 static const IS_NOT_LOGICAL_OPERATOR = 0;
2114 static const IS_AND = 1;
2115 static const IS_OR = 2;
2116
2117 int logicalOperatorType = IS_NOT_LOGICAL_OPERATOR;
2118
2119 // The order of the [inputs] must correspond to the order of the
2120 // predecessor-edges. That is if an input comes from the first predecessor
2121 // of the surrounding block, then the input must be the first in the [HPhi].
2122 HPhi(Local variable, List<HInstruction> inputs, TypeMask type)
2123 : super(inputs, type) {
2124 sourceElement = variable;
2125 }
2126 HPhi.noInputs(Local variable, TypeMask type)
2127 : this(variable, <HInstruction>[], type);
2128 HPhi.singleInput(Local variable, HInstruction input, TypeMask type)
2129 : this(variable, <HInstruction>[input], type);
2130 HPhi.manyInputs(Local variable,
2131 List<HInstruction> inputs,
2132 TypeMask type)
2133 : this(variable, inputs, type);
2134
2135 void addInput(HInstruction input) {
2136 assert(isInBasicBlock());
2137 inputs.add(input);
2138 assert(inputs.length <= block.predecessors.length);
2139 input.usedBy.add(this);
2140 }
2141
2142 toString() => 'phi';
2143 accept(HVisitor visitor) => visitor.visitPhi(this);
2144 }
2145
2146 abstract class HRelational extends HInvokeBinary {
2147 bool usesBoolifiedInterceptor = false;
2148 HRelational(left, right, selector, type) : super(left, right, selector, type);
2149 }
2150
2151 class HIdentity extends HRelational {
2152 // Cached codegen decision.
2153 String singleComparisonOp; // null, '===', '=='
2154
2155 HIdentity(left, right, selector, type) : super(left, right, selector, type);
2156 accept(HVisitor visitor) => visitor.visitIdentity(this);
2157
2158 BinaryOperation operation(ConstantSystem constantSystem)
2159 => constantSystem.identity;
2160 int typeCode() => HInstruction.IDENTITY_TYPECODE;
2161 bool typeEquals(other) => other is HIdentity;
2162 bool dataEquals(HInstruction other) => true;
2163 }
2164
2165 class HGreater extends HRelational {
2166 HGreater(left, right, selector, type) : super(left, right, selector, type);
2167 accept(HVisitor visitor) => visitor.visitGreater(this);
2168
2169 BinaryOperation operation(ConstantSystem constantSystem)
2170 => constantSystem.greater;
2171 int typeCode() => HInstruction.GREATER_TYPECODE;
2172 bool typeEquals(other) => other is HGreater;
2173 bool dataEquals(HInstruction other) => true;
2174 }
2175
2176 class HGreaterEqual extends HRelational {
2177 HGreaterEqual(left, right, selector, type)
2178 : super(left, right, selector, type);
2179 accept(HVisitor visitor) => visitor.visitGreaterEqual(this);
2180
2181 BinaryOperation operation(ConstantSystem constantSystem)
2182 => constantSystem.greaterEqual;
2183 int typeCode() => HInstruction.GREATER_EQUAL_TYPECODE;
2184 bool typeEquals(other) => other is HGreaterEqual;
2185 bool dataEquals(HInstruction other) => true;
2186 }
2187
2188 class HLess extends HRelational {
2189 HLess(left, right, selector, type) : super(left, right, selector, type);
2190 accept(HVisitor visitor) => visitor.visitLess(this);
2191
2192 BinaryOperation operation(ConstantSystem constantSystem)
2193 => constantSystem.less;
2194 int typeCode() => HInstruction.LESS_TYPECODE;
2195 bool typeEquals(other) => other is HLess;
2196 bool dataEquals(HInstruction other) => true;
2197 }
2198
2199 class HLessEqual extends HRelational {
2200 HLessEqual(left, right, selector, type) : super(left, right, selector, type);
2201 accept(HVisitor visitor) => visitor.visitLessEqual(this);
2202
2203 BinaryOperation operation(ConstantSystem constantSystem)
2204 => constantSystem.lessEqual;
2205 int typeCode() => HInstruction.LESS_EQUAL_TYPECODE;
2206 bool typeEquals(other) => other is HLessEqual;
2207 bool dataEquals(HInstruction other) => true;
2208 }
2209
2210 class HReturn extends HControlFlow {
2211 HReturn(value) : super(<HInstruction>[value]);
2212 toString() => 'return';
2213 accept(HVisitor visitor) => visitor.visitReturn(this);
2214 }
2215
2216 class HThrowExpression extends HInstruction {
2217 HThrowExpression(value)
2218 : super(<HInstruction>[value], const TypeMask.nonNullEmpty());
2219 toString() => 'throw expression';
2220 accept(HVisitor visitor) => visitor.visitThrowExpression(this);
2221 bool canThrow() => true;
2222 }
2223
2224 class HThrow extends HControlFlow {
2225 final bool isRethrow;
2226 HThrow(value, {this.isRethrow: false}) : super(<HInstruction>[value]);
2227 toString() => 'throw';
2228 accept(HVisitor visitor) => visitor.visitThrow(this);
2229 }
2230
2231 class HStatic extends HInstruction {
2232 final Element element;
2233 HStatic(this.element, type) : super(<HInstruction>[], type) {
2234 assert(element != null);
2235 assert(invariant(this, element.isDeclaration));
2236 sideEffects.clearAllSideEffects();
2237 sideEffects.clearAllDependencies();
2238 if (element.isAssignable) {
2239 sideEffects.setDependsOnStaticPropertyStore();
2240 }
2241 setUseGvn();
2242 }
2243 toString() => 'static ${element.name}';
2244 accept(HVisitor visitor) => visitor.visitStatic(this);
2245
2246 int gvnHashCode() => super.gvnHashCode() ^ element.hashCode;
2247 int typeCode() => HInstruction.STATIC_TYPECODE;
2248 bool typeEquals(other) => other is HStatic;
2249 bool dataEquals(HStatic other) => element == other.element;
2250 bool isCodeMotionInvariant() => !element.isAssignable;
2251 }
2252
2253 class HInterceptor extends HInstruction {
2254 // This field should originally be null to allow GVN'ing all
2255 // [HInterceptor] on the same input.
2256 Set<ClassElement> interceptedClasses;
2257 HInterceptor(HInstruction receiver, TypeMask type)
2258 : super(<HInstruction>[receiver], type) {
2259 sideEffects.clearAllSideEffects();
2260 sideEffects.clearAllDependencies();
2261 setUseGvn();
2262 }
2263 String toString() => 'interceptor on $interceptedClasses';
2264 accept(HVisitor visitor) => visitor.visitInterceptor(this);
2265 HInstruction get receiver => inputs[0];
2266 bool isInterceptor(Compiler compiler) => true;
2267
2268 int typeCode() => HInstruction.INTERCEPTOR_TYPECODE;
2269 bool typeEquals(other) => other is HInterceptor;
2270 bool dataEquals(HInterceptor other) {
2271 return interceptedClasses == other.interceptedClasses
2272 || (interceptedClasses.length == other.interceptedClasses.length
2273 && interceptedClasses.containsAll(other.interceptedClasses));
2274 }
2275 }
2276
2277 /**
2278 * A "one-shot" interceptor is a call to a synthetized method that
2279 * will fetch the interceptor of its first parameter, and make a call
2280 * on a given selector with the remaining parameters.
2281 *
2282 * In order to share the same optimizations with regular interceptor
2283 * calls, this class extends [HInvokeDynamic] and also has the null
2284 * constant as the first input.
2285 */
2286 class HOneShotInterceptor extends HInvokeDynamic {
2287 Set<ClassElement> interceptedClasses;
2288 HOneShotInterceptor(Selector selector,
2289 List<HInstruction> inputs,
2290 TypeMask type,
2291 this.interceptedClasses)
2292 : super(selector, null, inputs, type, true) {
2293 assert(inputs[0] is HConstant);
2294 assert(inputs[0].isNull());
2295 }
2296 bool isCallOnInterceptor(Compiler compiler) => true;
2297
2298 String toString() => 'one shot interceptor on $selector';
2299 accept(HVisitor visitor) => visitor.visitOneShotInterceptor(this);
2300 }
2301
2302 /** An [HLazyStatic] is a static that is initialized lazily at first read. */
2303 class HLazyStatic extends HInstruction {
2304 final Element element;
2305 HLazyStatic(this.element, type) : super(<HInstruction>[], type) {
2306 // TODO(4931): The first access has side-effects, but we afterwards we
2307 // should be able to GVN.
2308 sideEffects.setAllSideEffects();
2309 sideEffects.setDependsOnSomething();
2310 }
2311
2312 toString() => 'lazy static ${element.name}';
2313 accept(HVisitor visitor) => visitor.visitLazyStatic(this);
2314
2315 int typeCode() => 30;
2316 // TODO(4931): can we do better here?
2317 bool isCodeMotionInvariant() => false;
2318 bool canThrow() => true;
2319 }
2320
2321 class HStaticStore extends HInstruction {
2322 Element element;
2323 HStaticStore(this.element, HInstruction value)
2324 : super(<HInstruction>[value], const TypeMask.nonNullEmpty()) {
2325 sideEffects.clearAllSideEffects();
2326 sideEffects.clearAllDependencies();
2327 sideEffects.setChangesStaticProperty();
2328 }
2329 toString() => 'static store ${element.name}';
2330 accept(HVisitor visitor) => visitor.visitStaticStore(this);
2331
2332 int typeCode() => HInstruction.STATIC_STORE_TYPECODE;
2333 bool typeEquals(other) => other is HStaticStore;
2334 bool dataEquals(HStaticStore other) => element == other.element;
2335 bool isJsStatement() => true;
2336 }
2337
2338 class HLiteralList extends HInstruction {
2339 HLiteralList(List<HInstruction> inputs, TypeMask type) : super(inputs, type);
2340 toString() => 'literal list';
2341 accept(HVisitor visitor) => visitor.visitLiteralList(this);
2342 }
2343
2344 /**
2345 * The primitive array indexing operation. Note that this instruction
2346 * does not throw because we generate the checks explicitly.
2347 */
2348 class HIndex extends HInstruction {
2349 final Selector selector;
2350 HIndex(HInstruction receiver, HInstruction index, this.selector, type)
2351 : super(<HInstruction>[receiver, index], type) {
2352 sideEffects.clearAllSideEffects();
2353 sideEffects.clearAllDependencies();
2354 sideEffects.setDependsOnIndexStore();
2355 setUseGvn();
2356 }
2357
2358 String toString() => 'index operator';
2359 accept(HVisitor visitor) => visitor.visitIndex(this);
2360
2361 HInstruction get receiver => inputs[0];
2362 HInstruction get index => inputs[1];
2363
2364 HInstruction getDartReceiver(Compiler compiler) => receiver;
2365 bool onlyThrowsNSM() => true;
2366 bool canThrow() => receiver.canBeNull();
2367
2368 int typeCode() => HInstruction.INDEX_TYPECODE;
2369 bool typeEquals(HInstruction other) => other is HIndex;
2370 bool dataEquals(HIndex other) => true;
2371 }
2372
2373 /**
2374 * The primitive array assignment operation. Note that this instruction
2375 * does not throw because we generate the checks explicitly.
2376 */
2377 class HIndexAssign extends HInstruction {
2378 final Selector selector;
2379 HIndexAssign(HInstruction receiver,
2380 HInstruction index,
2381 HInstruction value,
2382 this.selector)
2383 : super(<HInstruction>[receiver, index, value],
2384 const TypeMask.nonNullEmpty()) {
2385 sideEffects.clearAllSideEffects();
2386 sideEffects.clearAllDependencies();
2387 sideEffects.setChangesIndex();
2388 }
2389 String toString() => 'index assign operator';
2390 accept(HVisitor visitor) => visitor.visitIndexAssign(this);
2391
2392 HInstruction get receiver => inputs[0];
2393 HInstruction get index => inputs[1];
2394 HInstruction get value => inputs[2];
2395
2396 HInstruction getDartReceiver(Compiler compiler) => receiver;
2397 bool onlyThrowsNSM() => true;
2398 bool canThrow() => receiver.canBeNull();
2399 }
2400
2401 class HIs extends HInstruction {
2402 /// A check against a raw type: 'o is int', 'o is A'.
2403 static const int RAW_CHECK = 0;
2404 /// A check against a type with type arguments: 'o is List<int>', 'o is C<T>'.
2405 static const int COMPOUND_CHECK = 1;
2406 /// A check against a single type variable: 'o is T'.
2407 static const int VARIABLE_CHECK = 2;
2408
2409 final DartType typeExpression;
2410 final int kind;
2411
2412 HIs.direct(DartType typeExpression,
2413 HInstruction expression,
2414 TypeMask type)
2415 : this.internal(typeExpression, [expression], RAW_CHECK, type);
2416
2417 HIs.raw(DartType typeExpression,
2418 HInstruction expression,
2419 HInterceptor interceptor,
2420 TypeMask type)
2421 : this.internal(
2422 typeExpression, [expression, interceptor], RAW_CHECK, type);
2423
2424 HIs.compound(DartType typeExpression,
2425 HInstruction expression,
2426 HInstruction call,
2427 TypeMask type)
2428 : this.internal(typeExpression, [expression, call], COMPOUND_CHECK, type);
2429
2430 HIs.variable(DartType typeExpression,
2431 HInstruction expression,
2432 HInstruction call,
2433 TypeMask type)
2434 : this.internal(typeExpression, [expression, call], VARIABLE_CHECK, type);
2435
2436 HIs.internal(this.typeExpression, List<HInstruction> inputs, this.kind, type)
2437 : super(inputs, type) {
2438 assert(kind >= RAW_CHECK && kind <= VARIABLE_CHECK);
2439 setUseGvn();
2440 }
2441
2442 HInstruction get expression => inputs[0];
2443
2444 HInstruction get interceptor {
2445 assert(kind == RAW_CHECK);
2446 return inputs.length > 1 ? inputs[1] : null;
2447 }
2448
2449 HInstruction get checkCall {
2450 assert(kind == VARIABLE_CHECK || kind == COMPOUND_CHECK);
2451 return inputs[1];
2452 }
2453
2454 bool get isRawCheck => kind == RAW_CHECK;
2455 bool get isVariableCheck => kind == VARIABLE_CHECK;
2456 bool get isCompoundCheck => kind == COMPOUND_CHECK;
2457
2458 accept(HVisitor visitor) => visitor.visitIs(this);
2459
2460 toString() => "$expression is $typeExpression";
2461
2462 int typeCode() => HInstruction.IS_TYPECODE;
2463
2464 bool typeEquals(HInstruction other) => other is HIs;
2465
2466 bool dataEquals(HIs other) {
2467 return typeExpression == other.typeExpression
2468 && kind == other.kind;
2469 }
2470 }
2471
2472 /**
2473 * HIsViaInterceptor is a late-stage instruction for a type test that can be
2474 * done entirely on an interceptor. It is not a HCheck because the checked
2475 * input is not one of the inputs.
2476 */
2477 class HIsViaInterceptor extends HLateInstruction {
2478 final DartType typeExpression;
2479 HIsViaInterceptor(this.typeExpression, HInstruction interceptor,
2480 TypeMask type)
2481 : super(<HInstruction>[interceptor], type) {
2482 setUseGvn();
2483 }
2484
2485 HInstruction get interceptor => inputs[0];
2486
2487 accept(HVisitor visitor) => visitor.visitIsViaInterceptor(this);
2488 toString() => "$interceptor is $typeExpression";
2489 int typeCode() => HInstruction.IS_VIA_INTERCEPTOR_TYPECODE;
2490 bool typeEquals(HInstruction other) => other is HIsViaInterceptor;
2491 bool dataEquals(HIs other) {
2492 return typeExpression == other.typeExpression;
2493 }
2494 }
2495
2496 class HTypeConversion extends HCheck {
2497 final DartType typeExpression;
2498 final int kind;
2499 final Selector receiverTypeCheckSelector;
2500 final bool contextIsTypeArguments;
2501 TypeMask checkedType; // Not final because we refine it.
2502
2503 static const int CHECKED_MODE_CHECK = 0;
2504 static const int ARGUMENT_TYPE_CHECK = 1;
2505 static const int CAST_TYPE_CHECK = 2;
2506 static const int BOOLEAN_CONVERSION_CHECK = 3;
2507 static const int RECEIVER_TYPE_CHECK = 4;
2508
2509 HTypeConversion(this.typeExpression, this.kind,
2510 TypeMask type, HInstruction input,
2511 [this.receiverTypeCheckSelector])
2512 : contextIsTypeArguments = false,
2513 checkedType = type,
2514 super(<HInstruction>[input], type) {
2515 assert(!isReceiverTypeCheck || receiverTypeCheckSelector != null);
2516 assert(typeExpression == null ||
2517 typeExpression.kind != TypeKind.TYPEDEF);
2518 sourceElement = input.sourceElement;
2519 }
2520
2521 HTypeConversion.withTypeRepresentation(this.typeExpression, this.kind,
2522 TypeMask type, HInstruction input,
2523 HInstruction typeRepresentation)
2524 : contextIsTypeArguments = false,
2525 checkedType = type,
2526 super(<HInstruction>[input, typeRepresentation],type),
2527 receiverTypeCheckSelector = null {
2528 assert(typeExpression.kind != TypeKind.TYPEDEF);
2529 sourceElement = input.sourceElement;
2530 }
2531
2532 HTypeConversion.withContext(this.typeExpression, this.kind,
2533 TypeMask type, HInstruction input,
2534 HInstruction context,
2535 {bool this.contextIsTypeArguments})
2536 : super(<HInstruction>[input, context], type),
2537 checkedType = type,
2538 receiverTypeCheckSelector = null {
2539 assert(typeExpression.kind != TypeKind.TYPEDEF);
2540 sourceElement = input.sourceElement;
2541 }
2542
2543 bool get hasTypeRepresentation {
2544 return typeExpression.isInterfaceType && inputs.length > 1;
2545 }
2546 HInstruction get typeRepresentation => inputs[1];
2547
2548 bool get hasContext {
2549 return typeExpression.isFunctionType && inputs.length > 1;
2550 }
2551 HInstruction get context => inputs[1];
2552
2553 HInstruction convertType(Compiler compiler, DartType type, int kind) {
2554 if (typeExpression == type) return this;
2555 return super.convertType(compiler, type, kind);
2556 }
2557
2558 bool get isCheckedModeCheck {
2559 return kind == CHECKED_MODE_CHECK
2560 || kind == BOOLEAN_CONVERSION_CHECK;
2561 }
2562 bool get isArgumentTypeCheck => kind == ARGUMENT_TYPE_CHECK;
2563 bool get isReceiverTypeCheck => kind == RECEIVER_TYPE_CHECK;
2564 bool get isCastTypeCheck => kind == CAST_TYPE_CHECK;
2565 bool get isBooleanConversionCheck => kind == BOOLEAN_CONVERSION_CHECK;
2566
2567 accept(HVisitor visitor) => visitor.visitTypeConversion(this);
2568
2569 bool isJsStatement() => isControlFlow();
2570 bool isControlFlow() => isArgumentTypeCheck || isReceiverTypeCheck;
2571
2572 int typeCode() => HInstruction.TYPE_CONVERSION_TYPECODE;
2573 bool typeEquals(HInstruction other) => other is HTypeConversion;
2574 bool isCodeMotionInvariant() => false;
2575
2576 bool dataEquals(HTypeConversion other) {
2577 return kind == other.kind
2578 && typeExpression == other.typeExpression
2579 && checkedType == other.checkedType
2580 && receiverTypeCheckSelector == other.receiverTypeCheckSelector;
2581 }
2582 }
2583
2584 /// The [HTypeKnown] instruction marks a value with a refined type.
2585 class HTypeKnown extends HCheck {
2586 TypeMask knownType;
2587 bool _isMovable;
2588
2589 HTypeKnown.pinned(TypeMask knownType, HInstruction input)
2590 : this.knownType = knownType,
2591 this._isMovable = false,
2592 super(<HInstruction>[input], knownType);
2593
2594 HTypeKnown.witnessed(TypeMask knownType, HInstruction input,
2595 HInstruction witness)
2596 : this.knownType = knownType,
2597 this._isMovable = true,
2598 super(<HInstruction>[input, witness], knownType);
2599
2600 toString() => 'TypeKnown $knownType';
2601 accept(HVisitor visitor) => visitor.visitTypeKnown(this);
2602
2603 bool isJsStatement() => false;
2604 bool isControlFlow() => false;
2605 bool canThrow() => false;
2606
2607 HInstruction get witness => inputs.length == 2 ? inputs[1] : null;
2608
2609 int typeCode() => HInstruction.TYPE_KNOWN_TYPECODE;
2610 bool typeEquals(HInstruction other) => other is HTypeKnown;
2611 bool isCodeMotionInvariant() => true;
2612 bool get isMovable => _isMovable && useGvn();
2613
2614 bool dataEquals(HTypeKnown other) {
2615 return knownType == other.knownType
2616 && instructionType == other.instructionType;
2617 }
2618 }
2619
2620 class HRangeConversion extends HCheck {
2621 HRangeConversion(HInstruction input, type)
2622 : super(<HInstruction>[input], type) {
2623 sourceElement = input.sourceElement;
2624 }
2625
2626 bool get isMovable => false;
2627
2628 accept(HVisitor visitor) => visitor.visitRangeConversion(this);
2629 }
2630
2631 class HStringConcat extends HInstruction {
2632 final ast.Node node;
2633 HStringConcat(HInstruction left, HInstruction right, this.node, TypeMask type)
2634 : super(<HInstruction>[left, right], type) {
2635 // TODO(sra): Until Issue 9293 is fixed, this false dependency keeps the
2636 // concats bunched with stringified inputs for much better looking code with
2637 // fewer temps.
2638 sideEffects.setDependsOnSomething();
2639 }
2640
2641 HInstruction get left => inputs[0];
2642 HInstruction get right => inputs[1];
2643
2644 accept(HVisitor visitor) => visitor.visitStringConcat(this);
2645 toString() => "string concat";
2646 }
2647
2648 /**
2649 * The part of string interpolation which converts and interpolated expression
2650 * into a String value.
2651 */
2652 class HStringify extends HInstruction {
2653 final ast.Node node;
2654 HStringify(HInstruction input, this.node, TypeMask type)
2655 : super(<HInstruction>[input], type) {
2656 sideEffects.setAllSideEffects();
2657 sideEffects.setDependsOnSomething();
2658 }
2659
2660 accept(HVisitor visitor) => visitor.visitStringify(this);
2661 toString() => "stringify";
2662 }
2663
2664 /** Non-block-based (aka. traditional) loop information. */
2665 class HLoopInformation {
2666 final HBasicBlock header;
2667 final List<HBasicBlock> blocks;
2668 final List<HBasicBlock> backEdges;
2669 final List<LabelDefinition> labels;
2670 final JumpTarget target;
2671
2672 /** Corresponding block information for the loop. */
2673 HLoopBlockInformation loopBlockInformation;
2674
2675 HLoopInformation(this.header, this.target, this.labels)
2676 : blocks = new List<HBasicBlock>(),
2677 backEdges = new List<HBasicBlock>();
2678
2679 void addBackEdge(HBasicBlock predecessor) {
2680 backEdges.add(predecessor);
2681 List<HBasicBlock> workQueue = <HBasicBlock>[predecessor];
2682 do {
2683 HBasicBlock current = workQueue.removeLast();
2684 addBlock(current, workQueue);
2685 } while (!workQueue.isEmpty);
2686 }
2687
2688 // Adds a block and transitively all its predecessors in the loop as
2689 // loop blocks.
2690 void addBlock(HBasicBlock block, List<HBasicBlock> workQueue) {
2691 if (identical(block, header)) return;
2692 HBasicBlock parentHeader = block.parentLoopHeader;
2693 if (identical(parentHeader, header)) {
2694 // Nothing to do in this case.
2695 } else if (parentHeader != null) {
2696 workQueue.add(parentHeader);
2697 } else {
2698 block.parentLoopHeader = header;
2699 blocks.add(block);
2700 workQueue.addAll(block.predecessors);
2701 }
2702 }
2703 }
2704
2705
2706 /**
2707 * Embedding of a [HBlockInformation] for block-structure based traversal
2708 * in a dominator based flow traversal by attaching it to a basic block.
2709 * To go back to dominator-based traversal, a [HSubGraphBlockInformation]
2710 * structure can be added in the block structure.
2711 */
2712 class HBlockFlow {
2713 final HBlockInformation body;
2714 final HBasicBlock continuation;
2715 HBlockFlow(this.body, this.continuation);
2716 }
2717
2718
2719 /**
2720 * Information about a syntactic-like structure.
2721 */
2722 abstract class HBlockInformation {
2723 HBasicBlock get start;
2724 HBasicBlock get end;
2725 bool accept(HBlockInformationVisitor visitor);
2726 }
2727
2728
2729 /**
2730 * Information about a statement-like structure.
2731 */
2732 abstract class HStatementInformation extends HBlockInformation {
2733 bool accept(HStatementInformationVisitor visitor);
2734 }
2735
2736
2737 /**
2738 * Information about an expression-like structure.
2739 */
2740 abstract class HExpressionInformation extends HBlockInformation {
2741 bool accept(HExpressionInformationVisitor visitor);
2742 HInstruction get conditionExpression;
2743 }
2744
2745
2746 abstract class HStatementInformationVisitor {
2747 bool visitLabeledBlockInfo(HLabeledBlockInformation info);
2748 bool visitLoopInfo(HLoopBlockInformation info);
2749 bool visitIfInfo(HIfBlockInformation info);
2750 bool visitTryInfo(HTryBlockInformation info);
2751 bool visitSwitchInfo(HSwitchBlockInformation info);
2752 bool visitSequenceInfo(HStatementSequenceInformation info);
2753 // Pseudo-structure embedding a dominator-based traversal into
2754 // the block-structure traversal. This will eventually go away.
2755 bool visitSubGraphInfo(HSubGraphBlockInformation info);
2756 }
2757
2758
2759 abstract class HExpressionInformationVisitor {
2760 bool visitAndOrInfo(HAndOrBlockInformation info);
2761 bool visitSubExpressionInfo(HSubExpressionBlockInformation info);
2762 }
2763
2764
2765 abstract class HBlockInformationVisitor
2766 implements HStatementInformationVisitor, HExpressionInformationVisitor {
2767 }
2768
2769
2770 /**
2771 * Generic class wrapping a [SubGraph] as a block-information until
2772 * all structures are handled properly.
2773 */
2774 class HSubGraphBlockInformation implements HStatementInformation {
2775 final SubGraph subGraph;
2776 HSubGraphBlockInformation(this.subGraph);
2777
2778 HBasicBlock get start => subGraph.start;
2779 HBasicBlock get end => subGraph.end;
2780
2781 bool accept(HStatementInformationVisitor visitor) =>
2782 visitor.visitSubGraphInfo(this);
2783 }
2784
2785 /**
2786 * Generic class wrapping a [SubExpression] as a block-information until
2787 * expressions structures are handled properly.
2788 */
2789 class HSubExpressionBlockInformation implements HExpressionInformation {
2790 final SubExpression subExpression;
2791 HSubExpressionBlockInformation(this.subExpression);
2792
2793 HBasicBlock get start => subExpression.start;
2794 HBasicBlock get end => subExpression.end;
2795
2796 HInstruction get conditionExpression => subExpression.conditionExpression;
2797
2798 bool accept(HExpressionInformationVisitor visitor) =>
2799 visitor.visitSubExpressionInfo(this);
2800 }
2801
2802 /** A sequence of separate statements. */
2803 class HStatementSequenceInformation implements HStatementInformation {
2804 final List<HStatementInformation> statements;
2805 HStatementSequenceInformation(this.statements);
2806
2807 HBasicBlock get start => statements[0].start;
2808 HBasicBlock get end => statements.last.end;
2809
2810 bool accept(HStatementInformationVisitor visitor) =>
2811 visitor.visitSequenceInfo(this);
2812 }
2813
2814 class HLabeledBlockInformation implements HStatementInformation {
2815 final HStatementInformation body;
2816 final List<LabelDefinition> labels;
2817 final JumpTarget target;
2818 final bool isContinue;
2819
2820 HLabeledBlockInformation(this.body,
2821 List<LabelDefinition> labels,
2822 {this.isContinue: false}) :
2823 this.labels = labels, this.target = labels[0].target;
2824
2825 HLabeledBlockInformation.implicit(this.body,
2826 this.target,
2827 {this.isContinue: false})
2828 : this.labels = const<LabelDefinition>[];
2829
2830 HBasicBlock get start => body.start;
2831 HBasicBlock get end => body.end;
2832
2833 bool accept(HStatementInformationVisitor visitor) =>
2834 visitor.visitLabeledBlockInfo(this);
2835 }
2836
2837 class LoopTypeVisitor extends ast.Visitor {
2838 const LoopTypeVisitor();
2839 int visitNode(ast.Node node) => HLoopBlockInformation.NOT_A_LOOP;
2840 int visitWhile(ast.While node) => HLoopBlockInformation.WHILE_LOOP;
2841 int visitFor(ast.For node) => HLoopBlockInformation.FOR_LOOP;
2842 int visitDoWhile(ast.DoWhile node) => HLoopBlockInformation.DO_WHILE_LOOP;
2843 int visitForIn(ast.ForIn node) => HLoopBlockInformation.FOR_IN_LOOP;
2844 int visitSwitchStatement(ast.SwitchStatement node) =>
2845 HLoopBlockInformation.SWITCH_CONTINUE_LOOP;
2846 }
2847
2848 class HLoopBlockInformation implements HStatementInformation {
2849 static const int WHILE_LOOP = 0;
2850 static const int FOR_LOOP = 1;
2851 static const int DO_WHILE_LOOP = 2;
2852 static const int FOR_IN_LOOP = 3;
2853 static const int SWITCH_CONTINUE_LOOP = 4;
2854 static const int NOT_A_LOOP = -1;
2855
2856 final int kind;
2857 final HExpressionInformation initializer;
2858 final HExpressionInformation condition;
2859 final HStatementInformation body;
2860 final HExpressionInformation updates;
2861 final JumpTarget target;
2862 final List<LabelDefinition> labels;
2863 final SourceFileLocation sourcePosition;
2864 final SourceFileLocation endSourcePosition;
2865
2866 HLoopBlockInformation(this.kind,
2867 this.initializer,
2868 this.condition,
2869 this.body,
2870 this.updates,
2871 this.target,
2872 this.labels,
2873 this.sourcePosition,
2874 this.endSourcePosition) {
2875 assert(
2876 (kind == DO_WHILE_LOOP ? body.start : condition.start).isLoopHeader());
2877 }
2878
2879 HBasicBlock get start {
2880 if (initializer != null) return initializer.start;
2881 if (kind == DO_WHILE_LOOP) {
2882 return body.start;
2883 }
2884 return condition.start;
2885 }
2886
2887 HBasicBlock get loopHeader {
2888 return kind == DO_WHILE_LOOP ? body.start : condition.start;
2889 }
2890
2891 HBasicBlock get end {
2892 if (updates != null) return updates.end;
2893 if (kind == DO_WHILE_LOOP && condition != null) {
2894 return condition.end;
2895 }
2896 return body.end;
2897 }
2898
2899 static int loopType(ast.Node node) {
2900 return node.accept(const LoopTypeVisitor());
2901 }
2902
2903 bool accept(HStatementInformationVisitor visitor) =>
2904 visitor.visitLoopInfo(this);
2905 }
2906
2907 class HIfBlockInformation implements HStatementInformation {
2908 final HExpressionInformation condition;
2909 final HStatementInformation thenGraph;
2910 final HStatementInformation elseGraph;
2911 HIfBlockInformation(this.condition,
2912 this.thenGraph,
2913 this.elseGraph);
2914
2915 HBasicBlock get start => condition.start;
2916 HBasicBlock get end => elseGraph == null ? thenGraph.end : elseGraph.end;
2917
2918 bool accept(HStatementInformationVisitor visitor) =>
2919 visitor.visitIfInfo(this);
2920 }
2921
2922 class HAndOrBlockInformation implements HExpressionInformation {
2923 final bool isAnd;
2924 final HExpressionInformation left;
2925 final HExpressionInformation right;
2926 HAndOrBlockInformation(this.isAnd,
2927 this.left,
2928 this.right);
2929
2930 HBasicBlock get start => left.start;
2931 HBasicBlock get end => right.end;
2932
2933 // We don't currently use HAndOrBlockInformation.
2934 HInstruction get conditionExpression {
2935 return null;
2936 }
2937 bool accept(HExpressionInformationVisitor visitor) =>
2938 visitor.visitAndOrInfo(this);
2939 }
2940
2941 class HTryBlockInformation implements HStatementInformation {
2942 final HStatementInformation body;
2943 final HLocalValue catchVariable;
2944 final HStatementInformation catchBlock;
2945 final HStatementInformation finallyBlock;
2946 HTryBlockInformation(this.body,
2947 this.catchVariable,
2948 this.catchBlock,
2949 this.finallyBlock);
2950
2951 HBasicBlock get start => body.start;
2952 HBasicBlock get end =>
2953 finallyBlock == null ? catchBlock.end : finallyBlock.end;
2954
2955 bool accept(HStatementInformationVisitor visitor) =>
2956 visitor.visitTryInfo(this);
2957 }
2958
2959 class HSwitchBlockInformation implements HStatementInformation {
2960 final HExpressionInformation expression;
2961 final List<HStatementInformation> statements;
2962 final JumpTarget target;
2963 final List<LabelDefinition> labels;
2964
2965 HSwitchBlockInformation(this.expression,
2966 this.statements,
2967 this.target,
2968 this.labels);
2969
2970 HBasicBlock get start => expression.start;
2971 HBasicBlock get end {
2972 // We don't create a switch block if there are no cases.
2973 assert(!statements.isEmpty);
2974 return statements.last.end;
2975 }
2976
2977 bool accept(HStatementInformationVisitor visitor) =>
2978 visitor.visitSwitchInfo(this);
2979 }
2980
2981 class HReadTypeVariable extends HInstruction {
2982 /// The type variable being read.
2983 final TypeVariableType dartType;
2984
2985 final bool hasReceiver;
2986
2987 HReadTypeVariable(this.dartType,
2988 HInstruction receiver,
2989 TypeMask instructionType)
2990 : hasReceiver = true,
2991 super(<HInstruction>[receiver], instructionType) {
2992 setUseGvn();
2993 }
2994
2995 HReadTypeVariable.noReceiver(this.dartType,
2996 HInstruction typeArgument,
2997 TypeMask instructionType)
2998 : hasReceiver = false,
2999 super(<HInstruction>[typeArgument], instructionType) {
3000 setUseGvn();
3001 }
3002
3003 accept(HVisitor visitor) => visitor.visitReadTypeVariable(this);
3004
3005 bool canThrow() => false;
3006
3007 int typeCode() => HInstruction.READ_TYPE_VARIABLE_TYPECODE;
3008 bool typeEquals(HInstruction other) => other is HReadTypeVariable;
3009
3010 bool dataEquals(HReadTypeVariable other) {
3011 return dartType.element == other.dartType.element
3012 && hasReceiver == other.hasReceiver;
3013 }
3014 }
3015
3016 abstract class HRuntimeType extends HInstruction {
3017 final DartType dartType;
3018
3019 HRuntimeType(List<HInstruction> inputs,
3020 this.dartType,
3021 TypeMask instructionType)
3022 : super(inputs, instructionType) {
3023 setUseGvn();
3024 }
3025
3026 bool canThrow() => false;
3027
3028 int typeCode() {
3029 throw 'abstract method';
3030 }
3031
3032 bool typeEquals(HInstruction other) {
3033 throw 'abstract method';
3034 }
3035
3036 bool dataEquals(HRuntimeType other) {
3037 return dartType == other.dartType;
3038 }
3039 }
3040
3041 class HFunctionType extends HRuntimeType {
3042 HFunctionType(List<HInstruction> inputs,
3043 FunctionType dartType,
3044 TypeMask instructionType)
3045 : super(inputs, dartType, instructionType);
3046
3047 accept(HVisitor visitor) => visitor.visitFunctionType(this);
3048
3049 int typeCode() => HInstruction.FUNCTION_TYPE_TYPECODE;
3050
3051 bool typeEquals(HInstruction other) => other is HFunctionType;
3052 }
3053
3054 class HVoidType extends HRuntimeType {
3055 HVoidType(VoidType dartType, TypeMask instructionType)
3056 : super(const <HInstruction>[], dartType, instructionType);
3057
3058 accept(HVisitor visitor) => visitor.visitVoidType(this);
3059
3060 int typeCode() => HInstruction.VOID_TYPE_TYPECODE;
3061
3062 bool typeEquals(HInstruction other) => other is HVoidType;
3063 }
3064
3065 class HInterfaceType extends HRuntimeType {
3066 HInterfaceType(List<HInstruction> inputs,
3067 InterfaceType dartType,
3068 TypeMask instructionType)
3069 : super(inputs, dartType, instructionType);
3070
3071 accept(HVisitor visitor) => visitor.visitInterfaceType(this);
3072
3073 int typeCode() => HInstruction.INTERFACE_TYPE_TYPECODE;
3074
3075 bool typeEquals(HInstruction other) => other is HInterfaceType;
3076 }
3077
3078 class HDynamicType extends HRuntimeType {
3079 HDynamicType(DynamicType dartType, TypeMask instructionType)
3080 : super(const <HInstruction>[], dartType, instructionType);
3081
3082 accept(HVisitor visitor) => visitor.visitDynamicType(this);
3083
3084 int typeCode() => HInstruction.DYNAMIC_TYPE_TYPECODE;
3085
3086 bool typeEquals(HInstruction other) => other is HDynamicType;
3087 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698