summaryrefslogtreecommitdiff
path: root/src/main/java/com/seaofnodes/simple/node/ParmNode.java
blob: 899d76393546dba6f6e69a91fde2eeef5b2f791a (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package com.seaofnodes.simple.node;

import com.seaofnodes.print.ExprPrinter;

import com.seaofnodes.simple.type.*;
import com.seaofnodes.simple.util.BAOS;

import java.util.HashMap;
import java.util.IdentityHashMap;

public class ParmNode extends PhiNode {

    // Argument indices are mapped one-to-one on CallNode inputs
    public final int _idx;             // Argument index
    public Type _declaredType;

    public ParmNode(String label, int idx, Type declaredType, Node... inputs) {
        super(label, inputs);
        _idx = idx;
        _declaredType = declaredType;
        _type = declaredType;
    }
    public ParmNode(ParmNode parm) { super(parm, parm._label ); _idx = parm._idx; _declaredType = parm._declaredType; }
    @Override public Tag serialTag() { return Tag.Parm; }
    @Override public void packed( BAOS baos, HashMap<String,Integer> strs, HashMap<Type,Integer> types, IdentityHashMap<Node, Integer> anodes ) {
        baos.packed1(nIns());   // Number of linked calls
        baos.packed2(_label==null ? 0 : strs.get(_label));
        baos.packed2(types.get(_declaredType)); // NPE if fails lookup
        baos.packed1(_idx);
    }
    static Node make( BAOS bais, String[] strs, Type[] types)  {
        Node[] ins = new Node[bais.packed1()];
        String label =   strs[bais.packed2()];
        Type minType =  types[bais.packed2()];
        return new ParmNode(label, bais.packed1(), minType, ins);
    }

    @Override public String label() { return MemOpNode.mlabel(_label); }

    @Override boolean _upgradeType( HashMap<String,Type> TYPES) {
        Type t = _declaredType.upgradeType(TYPES);
        if( t == _declaredType ) return false;
        unlock();
        _declaredType = t;
        return true;
    }

    public FunNode fun() { return (FunNode)in(0); }

    @Override
    protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
        if( fun()._name!=null && fun()._name.endsWith("<clinit>") && _label.equals("arg") )
            return p.p("arg");
        p.p("Parm_").p(_label).open();
        for( Node in : _inputs ) p.n(in).p(",");
        return p.unchar(',').close();
    }


    // Always in-progress until we run out of unknown callers
    @Override public boolean inProgress() { return in(0) instanceof FunNode fun && fun.inProgress(); }

    @Override
    public Type compute() {
        if( !(region() instanceof RegionNode r) )
            return region()._type==Type.XCONTROL || region()._type==Type.TOP ? (_type instanceof TypeMem ? TypeMem.TOP : Type.TOP) : _type;
        // During parsing Phis have to be computed type pessimistically.
        if( r.inProgress() || in(nIns()-1)==null )
            return _declaredType;
        // Set type to local top of the starting type
        Type t = Type.TOP;
        for( int i = 1; i < nIns(); i++ ) {
            // If the region's control input is live, add this as a dependency
            // to the control because we can be peeped should it become dead.
            Type ctrl = addDep(r.in(i))._type;
            if( ctrl != Type.XCONTROL && ctrl != Type.TOP ) {
                if( in(i)._type==Type.BOTTOM )
                    return Type.BOTTOM;
                t = t.meet(in(i)._type);
            }
        }
        return t.join(_declaredType);
    }

    @Override
    public Node idealize() {
        if( !(region() instanceof FunNode) )
            // A dead function can lose every caller before the Parm itself is
            // removed; there is no replacement input in that transient form.
            return nIns()==1 ? null : in(1); // Input has collapse to e.g. starting control.
        // If function is folding, do all possible peeps
        if( fun()._folding ) return super.idealize();

        // Skip most phi optimizations on parms
        return null;
    }

    @Override public boolean eq( Node n ) {
        return ((ParmNode)n)._idx==_idx && super.eq(n);
    }
}