package com.seaofnodes.simple.codegen; import com.seaofnodes.simple.node.*; import com.seaofnodes.simple.util.Ary; // Discard call-graph edges after LoopTree. Machine selection keeps only this // object's bodies; ideal evaluation keeps imports so their calls can execute. abstract class Unlink { // Disconnect compiler-only interprocedural edges before scheduling. Keep // signatures in the linker; runtime calls resolve FIDXs, not graph edges. static Ary forEval(CodeGen code, StartNode start, StopNode stop) { Ary nodes = new Ary<>(Node.class); start.walk(n -> { nodes.add(n); return null; }); Ary funs = new Ary<>(FunNode.class); for( Node n : nodes ) if( n instanceof FunNode fun ) { fun.ret().keep(); if( fun.summary()==null ) funs.add(fun); } for( Node n : nodes ) { if( n instanceof CallNode call ) call.unlink_all(); if( n instanceof CLInitNode init ) init.unlink(); if( n instanceof FunPtrNode ptr && ptr.ret()!=null ) ptr.setDef(1,null); } for( FunNode fun : funs ) { // Keep bodies rooted for scheduling after their callers detach. if( fun.nIns()==1 ) { fun.addDef(fun._compunit._start); for( Node use : fun._outputs ) if( use instanceof ParmNode parm ) { ConstantNode con = ConstantNode.raw(parm._type); con.setDef(0,start); parm.addDef(con); } } restoreRPC(fun); } code.releaseUnlinkedNodes(); for( Node n : nodes ) if( n instanceof StartCUNode unitStart ) detachEmptyStart(unitStart); // A summary is a dispatch target, not a schedulable implementation. // Retain its own wrapper graph without including it in this schedule. for( int i = 0; i 1 ) { if( fun.in(1) instanceof CallNode call ) call.unlink(fun,1); else fun.removeDeadPath(1); } StartCUNode start = fun._compunit._start; StopCUNode stop = fun._compunit._stop; // Unhook the Stop->Return edge also ReturnNode ret = fun.ret(); int idx = stop._inputs.find(ret); if( idx != -1 ) { stop._inputs.del(idx); ret.delUse(stop); } // if( stop.nIns()==0 ) { idx = code._stop._inputs.find(stop); if( idx != -1 ) code._stop.delDef(idx); if( start.nIns() > 0 && start.in(0) != null ) start.setDef(0,null); if( start.nIns() > 1 && start.in(1) != null ) start.setDef(1,null); } continue; } // Insert the post-Opto code-generation hook. Unlike an Opto-time // Start input, this no longer carries unknown-caller semantics. ReturnNode ret = fun.ret(); StartNode start = fun._compunit._start; assert start!=null; if( fun.nIns() <= 1 || fun.in(1) != start ) { fun.insertDef(1,start); for( Node use : fun._outputs ) if( use instanceof ParmNode parm ) parm.insertDef(1,ConstantNode.raw(parm._type)); if( fun._compunit._stop._inputs.find(ret) == -1 ) fun._compunit._stop.addDef(ret); } // Unlink from Call for( int i=2; i { if( n instanceof CallEndNode cend ) for( int i=1; i