• 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.
14
15### IsInstance
16
17`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.
18`IsInstanceVisitor` also used in `Peephole` optimizations.
19
20### CheckCast
21
22If 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.
23`CheckCastVisitor` also used in `CheckCast` optimizations.
24
25## Pseudocode
26    TODO
27
28## Examples
29
30```
31.record A {}
32.record B <extends=A> {}
33.record C {}
34
35...
36    newobj v0, B
37    lda.obj v0
38    isinstance A // will replaced by 1
39    newobj v0, C
40    lda.obj v0
41    isinstance A // will replaced by 0
42...
43    newobj v0, B
44    lda.obj v0
45    checkcast A // will removed
46    checkcast C // will replaced by deoptimze
47```
48
49## Links
50Source code:
51[object_type_check_elimination.h](../optimizer/optimizations/object_type_check_elimination.h)
52[object_type_check_elimination.cpp](../optimizer/optimizations/object_type_check_elimination.cpp)
53
54Tests:
55[isinstance_elimination_test.cpp](../../tests/checked/isinstance_elimination_test.pa)
56[checkcast_elimination_test.cpp](../../tests/checked/checkcast_elimination_test.pa)
57