summaryrefslogtreecommitdiff
path: root/src/main/java/com/seaofnodes/simple/codegen/FinalizeIR.java
blob: 69e0104d3b814e353839dfd682e1ad43b0b33b90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.seaofnodes.simple.codegen;

import com.seaofnodes.simple.node.*;
import java.util.Arrays;

// Comnpute field layouts only after SCCP and its peepholes converge.
// Keep surviving functions rooted without introducing unknown callers.
abstract class FinalizeIR {
    static void run(CodeGen code) {
        // Compute field layouts and replace symbolic field offsets by constants.
        for( int i=0; i<code._start.nOuts(); i++ ) {
            Node use = code._start.out(i);
            if( use instanceof ConFldOffNode off ) {
                Node con = off.asOffset();
                assert con != null : off._ts._name+"."+off._fname;
                off.subsume(con);
                i--;
            }
        }
        code._iter.iterate(code); // Propagate field offsets and clean up.

        // Retain surviving functions for emission without adding unknown callers.
        for( FunNode fun : code._linker )
            if( fun != null ) {
                // A folded static function field can leave a detached body
                // with internal cycles. It has no callers; later phases must
                // not revive it merely because its nodes are still allocated.
                if( fun.isDead() || fun._type.isHigh() ) {
                    code._linker.set(fun.sig().fidx(),null);
                    continue;
                }
                code.keepInCompUnit(fun);
            }
        // Hand off the wrapper lifetime before releasing its memory projection.
        for( CompUnit cu : code._compunits.values() )
            code.keepUntilUnlink(cu._start);
        code.releaseFinalizedNodes();
        // Stable print order: initializers first, then other Returns by NID.
        for( Node stop : code._stop._inputs )
            Arrays.sort(stop._inputs._es,0,stop.nIns(),
                        (x,y) -> ((((ReturnNode)x).fun().isClz() ? -1 : x._nid) -
                                  (((ReturnNode)y).fun().isClz() ? -1 : y._nid) ));
    }
}