• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Object Type Check Elimination
2## Overview
3**Object Type Check Elimination** - optimization which try to reduce number of IsInstance/CheckCast instructions.
4
5## Rationality
6Reduce number of instructions and remove unnecessary data-flow dependencies.
7
8## Dependences
9* RPO
10* ObjectTypePropagation
11
12## Algorithm
13Visit `IsInstance` and `CheckCast` instructions in RPO order and try to elimiate them.
14If instruction couldn't be eliminated, `ObjectTypeInfo` for input is dropped.
15
16### IsInstance
17
18`IsInstance` is replaced by 1 if an object in input can be cast to the resolved type, else replaced by 0. 'null' object is not an instance of any class.
19`IsInstanceVisitor` also used in `Peephole` optimizations.
20
21### CheckCast
22
23If an object in input can't be cast to the resolved type `CheckCast` is replaced by deoptimize, else removed. 'null' object reference can be cast to every type.
24`CheckCastVisitor` also used in `CheckCast` optimizations.
25
26## Pseudocode
27    TODO
28
29## Examples
30
31```
32.record A {}
33.record B <extends=A> {}
34.record C {}
35
36...
37    newobj v0, B
38    lda.obj v0
39    isinstance A // will replaced by 1
40    newobj v0, C
41    lda.obj v0
42    isinstance A // will replaced by 0
43...
44    newobj v0, B
45    lda.obj v0
46    checkcast A // will removed
47    checkcast C // will replaced by deoptimze
48```
49
50## Links
51Source code:
52[object_type_check_elimination.h](../optimizer/optimizations/object_type_check_elimination.h)
53[object_type_check_elimination.cpp](../optimizer/optimizations/object_type_check_elimination.cpp)
54
55Tests:
56[isinstance_elimination_test.cpp](../../tests/checked/isinstance_elimination_test.pa)
57[checkcast_elimination_test.cpp](../../tests/checked/checkcast_elimination_test.pa)
58