aa
NewNode.java
Go to the documentation of this file.
1 package com.cliffc.aa.node;
2 
3 import com.cliffc.aa.Env;
4 import com.cliffc.aa.GVNGCM;
5 import com.cliffc.aa.type.*;
6 import com.cliffc.aa.util.Ary;
7 import org.jetbrains.annotations.NotNull;
8 
9 import static com.cliffc.aa.AA.*;
10 
11 // Allocates a TypeObj and produces a Tuple with the TypeObj and a TypeMemPtr.
12 //
13 // NewNodes have a unique alias class - they do not alias with any other
14 // NewNode, even if they have the same type. Upon cloning both NewNodes get
15 // new aliases that inherit (tree-like) from the original alias.
16 
17 public abstract class NewNode<T extends TypeObj<T>> extends Node {
18  // Unique alias class, one class per unique memory allocation site.
19  // Only effectively-final, because the copy/clone sets a new alias value.
20  public int _alias; // Alias class
21 
22  // A list of field names and field-mods, folded into the initial state of
23  // this NewObj. These can come from initializers at parse-time, or stores
24  // folded in. There are no types stored here; types come from the inputs.
25  public T _ts; // Base object type, representing all possible future values
26 
27  // The memory state for Env.DEFMEM, the default memory. All non-final fields
28  // are ALL; final fields keep their value. All field flags are moved to
29  // bottom, e.g. as-if all fields are now final-stored. Will be set to
30  // TypeObj.UNUSE for never-allocated (eg dead allocations)
32 
33  // Just TMP.make(_alias,OBJ)
34  public TypeMemPtr _tptr;
35 
36  // News do not really need a ctrl; useful to bind the upward motion of
37  // closures so variable stores can more easily fold into them.
38 
39  // Takes a parent alias, and splits a child out from it.
40  public NewNode( byte type, int par_alias, T to ) { super(type,(Node)null); _init(BitsAlias.new_alias(par_alias),to); }
41  // Takes a alias and a field and uses them directly
42  public NewNode( byte type, int alias, T to,Node fld) { super(type, null, fld); _init( alias, to); }
43  private void _init(int alias, T ts) {
44  if( _elock ) unelock(); // Unlock before changing hash
45  _alias = alias;
47  sets(ts);
48  }
49  @Override public String xstr() { return "New"+"*"+_alias; } // Self short name
50  String str() { return "New"+_ts; } // Inline less-short name
51 
52  static int def_idx(int fld) { return fld+1; } // Skip ctl in slot 0
53  Node fld(int fld) { return in(def_idx(fld)); } // Node for field#
54 
55  // Recompute default memory cache on a change. Might not be monotonic,
56  // e.g. during Node create, or folding a Store.
57  public final void sets( T ts ) {
58  _ts = ts;
59  _crushed = ts.crush();
60  }
61  // Recompute default memory, expecting it to monotonically lift.
62  public final void setsm( T ts ) { assert ts.isa(_ts); sets(ts); }
63 
64  @Override public Node ideal_reduce() {
65  // If either the address or memory is not looked at then the memory
66  // contents are dead. The object might remain as a 'gensym' or 'sentinel'
67  // for identity tests.
68  if( _defs._len > 1 && captured() ) { kill2(); return this; }
69  return null;
70  }
71 
72  @Override public void add_flow_def_extra(Node chg) {
73  if( chg instanceof MrgProjNode && chg._live.at(_alias)==TypeObj.UNUSED )
74  Env.GVN.add_reduce(chg);
75  }
76  // Reducing a NewNode to 'any' changes DEFMEM
77  @Override public void add_reduce_extra() {
79  }
80 
81  @Override public Type value(GVNGCM.Mode opt_mode) {
82  return TypeTuple.make(Type.CTRL, is_unused() ? TypeObj.UNUSED : valueobj(),_tptr); // Complex obj, simple ptr.
83  }
84  abstract TypeObj valueobj();
85 
86  @Override public TypeMem all_live() { return TypeMem.ALLMEM; }
87 
88  // Only alive fields in the MrgProj escape
89  @Override public TypeMem live_use(GVNGCM.Mode opt_mode, Node def ) {
90  TypeObj to = _live.at(_alias);
91  return to.above_center() ? TypeMem.DEAD : TypeMem.ESCAPE;
92  }
93 
94  //@Override public TV2 new_tvar(String alloc_site) { return TV2.make("Obj",this,alloc_site); }
95 
96  //abstract public boolean unify( boolean test );
97 
98  @Override BitsAlias escapees() { return _tptr._aliases; }
99  abstract T dead_type();
100  boolean is_unused() { return _ts==dead_type(); }
101  // Kill all inputs, inform all users
102  void kill2() {
103  unelock();
104  while( !is_dead() && _defs._len > 1 )
105  pop(); // Kill all fields except memory
106  _crushed = _ts = dead_type();
108  Env.DEFMEM.set_def(_alias,Node.con(TypeObj.UNUSED));
109  Env.GVN.revalive(this,ProjNode.proj(this,0),Env.DEFMEM);
110  if( is_dead() ) return;
111  for( Node use : _uses )
112  Env.GVN.add_flow_uses(use); // Get FPtrs from MrgProj, and dead Ptrs into New
113  }
114 
115  // Basic escape analysis. If no escapes and no loads this object is dead.
116  private boolean captured( ) {
117  if( _keep > 0 ) return false;
118  if( _uses._len==0 ) return false; // Dead or being created
119  Node mem = _uses.at(0);
120  // If only either address or memory remains, then memory contents are dead
121  if( _uses._len==1 ) {
122  if( mem instanceof MrgProjNode ) return true; // No pointer, just dead memory
123  // Just a pointer; currently on Strings become memory constants and
124  // constant-fold - leaving the allocation dead.
125  return !(val(1) instanceof TypeStr);
126  }
127  Node ptr = _uses.at(1);
128  if( ptr instanceof MrgProjNode ) ptr = _uses.at(0); // Get ptr not mem
129  if( ptr._keep>0 ) return false;
130 
131  // Scan for memory contents being unreachable.
132  // Really stupid!
133  for( Node use : ptr._uses )
134  if( !(use instanceof IfNode) )
135  return false;
136  // Only used to nil-check (always not-nil) and equality (always unequal to
137  // other aliases).
138  return true;
139  }
140 
141  // clones during inlining all become unique new sites
142  @SuppressWarnings("unchecked")
143  @Override @NotNull public NewNode copy( boolean copy_edges) {
144  // Split the original '_alias' class into 2 sub-aliases
145  NewNode<T> nnn = (NewNode<T>)super.copy(copy_edges);
146  nnn._init(BitsAlias.new_alias(_alias),_ts); // Children alias classes, split from parent
147  _init(BitsAlias.new_alias(_alias),_ts); // The original NewNode also splits from the parent alias
148  Env.GVN.add_flow(this); // Alias changes flow
149  return nnn;
150  }
151 
152  @Override public int hashCode() { return super.hashCode()+ _alias; }
153  // Only ever equal to self, because of unique _alias. We can collapse equal
154  // NewNodes and join alias classes, but this is not the normal CSE and so is
155  // not done by default.
156  // TODO: Allow CSE if all fields are final at construction.
157  @Override public boolean equals(Object o) { return this==o; }
158  public MrgProjNode mrg() {
159  Node ptr = _uses.at(0);
160  if( !(ptr instanceof MrgProjNode) ) ptr = _uses.at(1);
161  return (MrgProjNode)ptr;
162  }
164  Node ptr = _uses.at(0);
165  if( ptr instanceof MrgProjNode ) ptr = _uses.at(1);
166  return (ProjNode)ptr;
167  }
168 
169  // --------------------------------------------------------------------------
170  public static abstract class NewPrimNode<T extends TypeObj<T>> extends NewNode<T> {
171  public final String _name; // Unique library call name
172  final TypeFunSig _sig; // Arguments
173  final boolean _reads; // Reads old memory (all of these ops *make* new memory, none *write* old memory)
174  final int _op_prec;
175  NewPrimNode(byte op, int parent_alias, T to, String name, boolean reads, int op_prec, Type... args) {
176  super(op,parent_alias,to);
177  _name = name;
178  _reads = reads;
179  assert (reads == (args[MEM_IDX]!=TypeMem.ALLMEM)); // If reading, then memory has some requirements
180  args[DSP_IDX] = Type.ALL; // No display
182  _op_prec = op_prec;
183  }
184  String bal_close() { return null; }
185 
186  private static final Ary<NewPrimNode> INTRINSICS = new Ary<>(NewPrimNode.class);
187  static { reset(); }
188  public static void reset() { INTRINSICS.clear(); }
189  public static Ary<NewPrimNode> INTRINSICS() {
190  if( INTRINSICS.isEmpty() ) {
193  }
194  return INTRINSICS;
195  }
196 
197  // Wrap the PrimNode with a Fun/Epilog wrapper that includes memory effects.
198  public FunPtrNode as_fun( GVNGCM gvn ) {
199  try(GVNGCM.Build<FunPtrNode> X = gvn.new Build<>()) {
200  assert in(0)==null && _uses._len==0;
201  FunNode fun = ( FunNode) X.xform(new FunNode(this).add_def(Env.ALL_CTRL));
202  ParmNode rpc = (ParmNode) X.xform(new ParmNode(0,"rpc",fun,Env.ALL_CALL,null));
203  Node memp= X.xform(new ParmNode(MEM_IDX,_sig._args[MEM_IDX],fun, TypeMem.MEM, Env.DEFMEM,null));
204  fun._bal_close = bal_close();
205 
206  // Add input edges to the intrinsic
207  add_def(_reads ? memp : null); // Memory for the primitive in slot MEM_IDX
208  add_def(null); // Closure for the primitive in slot DSP_IDX
209  for( int i=ARG_IDX; i<_sig.nargs(); i++ ) // Args follow
210  add_def( X.xform(new ParmNode(i,_sig._args[i],fun, (ConNode)Node.con(_sig.arg(i).simple_ptr()),null)));
211  NewNode nnn = (NewNode)X.xform(this);
212  Node mem = Env.DEFMEM.make_mem_proj(nnn,memp);
213  Node ptr = X.xform(new ProjNode(nnn,REZ_IDX));
214  RetNode ret = (RetNode)X.xform(new RetNode(fun,mem,ptr,rpc,fun));
215  return (X._ret = new FunPtrNode(_name,ret));
216  }
217  }
218  }
219 }
com.cliffc.aa.type.TypeObj.above_center
boolean above_center()
Definition: TypeObj.java:77
com.cliffc.aa.type.TypeMem.DEAD
static final TypeMem DEAD
Definition: TypeMem.java:226
com.cliffc.aa.type.TypeTuple.make_args
static TypeTuple make_args(Type[] ts)
Definition: TypeTuple.java:106
com.cliffc.aa.node.NewNode.captured
boolean captured()
Definition: NewNode.java:116
com.cliffc.aa.type.TypeMem
Memory type; the state of all of memory; memory edges order memory ops.
Definition: TypeMem.java:53
com.cliffc.aa.node.Node._live
TypeMem _live
Definition: Node.java:89
com.cliffc.aa.GVNGCM.add_flow_uses
void add_flow_uses(Node n)
Definition: GVNGCM.java:55
com.cliffc
com.cliffc.aa.Env.ALL_CALL
static ConNode ALL_CALL
Definition: Env.java:26
com.cliffc.aa.type.TypeObj.crush
TypeObj crush()
Definition: TypeObj.java:85
com.cliffc.aa.node.NewNode.NewPrimNode.INTRINSICS
static Ary< NewPrimNode > INTRINSICS()
Definition: NewNode.java:189
com.cliffc.aa.node.IfNode
Definition: IfNode.java:9
com.cliffc.aa.node.NewNode.xstr
String xstr()
Definition: NewNode.java:49
com.cliffc.aa.node.Node
Definition: Node.java:16
com.cliffc.aa.util
Definition: AbstractEntry.java:1
com.cliffc.aa.type.TypeMem.ESCAPE
static final TypeMem ESCAPE
Definition: TypeMem.java:227
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
com.cliffc.aa.type.TypeFunSig.make
static TypeFunSig make(String[] args, TypeTuple formals, TypeTuple ret)
Definition: TypeFunSig.java:71
com.cliffc.aa.util.Ary
Definition: Ary.java:11
com.cliffc.aa.type.BitsAlias
Definition: BitsAlias.java:8
com.cliffc.aa.node.NewNode.NewPrimNode
Definition: NewNode.java:170
com.cliffc.aa.GVNGCM.revalive
void revalive(Node... ns)
Definition: GVNGCM.java:103
com.cliffc.aa.type.TypeTuple
Definition: TypeTuple.java:11
com.cliffc.aa.node.NewAryNode.add_libs
static void add_libs(Ary< NewPrimNode > INTRINSICS)
Definition: NewAryNode.java:15
com.cliffc.aa.node.Node.unelock
void unelock()
Definition: Node.java:128
com.cliffc.aa.node.NewNode.copy
NewNode copy(boolean copy_edges)
Definition: NewNode.java:143
com.cliffc.aa.type.TypeMem.ALLMEM
static final TypeMem ALLMEM
Definition: TypeMem.java:228
com.cliffc.aa.node.NewNode.value
Type value(GVNGCM.Mode opt_mode)
Definition: NewNode.java:81
com.cliffc.aa.node.ConNode
Definition: ConNode.java:9
com.cliffc.aa.type.BitsAlias.new_alias
static int new_alias(int par)
Definition: BitsAlias.java:75
com.cliffc.aa.node.Node.add_def
Node add_def(Node n)
Definition: Node.java:152
com.cliffc.aa.node.FunPtrNode
Definition: FunPtrNode.java:40
com.cliffc.aa.node.NewNode.NewPrimNode.bal_close
String bal_close()
Definition: NewNode.java:184
com.cliffc.aa.node.NewNode.setsm
final void setsm(T ts)
Definition: NewNode.java:62
com.cliffc.aa.node.RetNode
Definition: RetNode.java:19
com.cliffc.aa.node.NewNode.add_flow_def_extra
void add_flow_def_extra(Node chg)
Definition: NewNode.java:72
com.cliffc.aa.node.NewNode.NewPrimNode.as_fun
FunPtrNode as_fun(GVNGCM gvn)
Definition: NewNode.java:198
com.cliffc.aa.node.NewNode.escapees
BitsAlias escapees()
Definition: NewNode.java:98
com.cliffc.aa.type.Type.ALL
static final Type ALL
Definition: Type.java:324
com.cliffc.aa.node.NewNode.kill2
void kill2()
Definition: NewNode.java:102
com.cliffc.aa.node.NewNode.ideal_reduce
Node ideal_reduce()
Definition: NewNode.java:64
com.cliffc.aa.node.NewNode.live_use
TypeMem live_use(GVNGCM.Mode opt_mode, Node def)
Definition: NewNode.java:89
com.cliffc.aa.node.NewNode._tptr
TypeMemPtr _tptr
Definition: NewNode.java:34
com.cliffc.aa.type.TypeFunSig._args
String[] _args
Definition: TypeFunSig.java:14
com.cliffc.aa.type.TypeFunSig.arg
Type arg(int idx)
Definition: TypeFunSig.java:88
com.cliffc.aa.Env.GVN
static final GVNGCM GVN
Definition: Env.java:13
com.cliffc.aa.node.NewNode.str
String str()
Definition: NewNode.java:50
com.cliffc.aa.type.TypeFunSig.nargs
int nargs()
Definition: TypeFunSig.java:87
com.cliffc.aa.type.TypeObj
Definition: TypeObj.java:15
com.cliffc.aa.node.NewNode.def_idx
static int def_idx(int fld)
Definition: NewNode.java:52
com.cliffc.aa.node.Node.is_dead
boolean is_dead()
Definition: Node.java:820
com.cliffc.aa.node.NewNode.fld
Node fld(int fld)
Definition: NewNode.java:53
com.cliffc.aa.node.NewNode.NewPrimNode.INTRINSICS
static final Ary< NewPrimNode > INTRINSICS
Definition: NewNode.java:186
com.cliffc.aa.type.TypeTuple.RET
static final TypeTuple RET
Definition: TypeTuple.java:130
com.cliffc.aa.node.Node._keep
byte _keep
Definition: Node.java:86
com.cliffc.aa.node.ProjNode.proj
static ProjNode proj(Node head, int idx)
Definition: ProjNode.java:50
com.cliffc.aa.node.NewNode._init
void _init(int alias, T ts)
Definition: NewNode.java:43
com.cliffc.aa.node.NewStrNode
Definition: NewStrNode.java:11
com.cliffc.aa.type.Type.CTRL
static final Type CTRL
Definition: Type.java:326
com.cliffc.aa.node.NewNode.add_reduce_extra
void add_reduce_extra()
Definition: NewNode.java:77
com.cliffc.aa.type.TypeObj.UNUSED
static final TypeObj UNUSED
Definition: TypeObj.java:46
com.cliffc.aa.type.TypeObj.ISUSED
static final TypeObj ISUSED
Definition: TypeObj.java:45
com.cliffc.aa.type.TypeTuple.make
static TypeTuple make(boolean any, Type[] ts)
Definition: TypeTuple.java:82
com.cliffc.aa.node.NewNode.NewNode
NewNode(byte type, int par_alias, T to)
Definition: NewNode.java:40
com.cliffc.aa.node.Node.in
Node in(int i)
Definition: Node.java:126
com.cliffc.aa.node.NewNode.ptr
ProjNode ptr()
Definition: NewNode.java:163
com.cliffc.aa.node.NewNode.dead_type
abstract T dead_type()
com.cliffc.aa.node.NewNode.NewPrimNode._name
final String _name
Definition: NewNode.java:171
com.cliffc.aa.GVNGCM
Definition: GVNGCM.java:12
com.cliffc.aa.node.NewNode.NewPrimNode._sig
final TypeFunSig _sig
Definition: NewNode.java:172
com.cliffc.aa.type.Type.simple_ptr
Type simple_ptr()
Definition: Type.java:358
com.cliffc.aa.node.NewAryNode
Definition: NewAryNode.java:9
com.cliffc.aa.type.TypeStr
Definition: TypeStr.java:14
com.cliffc.aa.node.NewNode.sets
final void sets(T ts)
Definition: NewNode.java:57
com.cliffc.aa.node.ProjNode
Definition: ProjNode.java:11
com.cliffc.aa.node.NewNode._ts
T _ts
Definition: NewNode.java:25
com.cliffc.aa.node.NewNode.NewPrimNode._op_prec
final int _op_prec
Definition: NewNode.java:174
com.cliffc.aa.node.NewNode.hashCode
int hashCode()
Definition: NewNode.java:152
com.cliffc.aa.node.Node.con
static Node con(Type t)
Definition: Node.java:670
com.cliffc.aa.node.Node._uses
Ary< Node > _uses
Definition: Node.java:245
com.cliffc.aa.node.NewNode.equals
boolean equals(Object o)
Definition: NewNode.java:157
com.cliffc.aa.AA
an implementation of language AA
Definition: AA.java:9
com.cliffc.aa.node.NewNode.mrg
MrgProjNode mrg()
Definition: NewNode.java:158
com.cliffc.aa.node.Node.val
Type val(int idx)
Definition: Node.java:470
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.type.BitsAlias.make0
static BitsAlias make0(int bit)
Definition: BitsAlias.java:72
com.cliffc.aa.node.NewNode
Definition: NewNode.java:17
com.cliffc.aa.node.Node._elock
boolean _elock
Definition: Node.java:87
com.cliffc.aa.node.MrgProjNode
Definition: MrgProjNode.java:10
com.cliffc.aa.node.NewStrNode.add_libs
static void add_libs(Ary< NewPrimNode > INTRINSICS)
Definition: NewStrNode.java:25
com.cliffc.aa.node.NewNode._crushed
TypeObj _crushed
Definition: NewNode.java:31
com.cliffc.aa.type.TypeMemPtr._aliases
BitsAlias _aliases
Definition: TypeMemPtr.java:16
com.cliffc.aa.type.TypeFunSig
Definition: TypeFunSig.java:10
com.cliffc.aa.GVNGCM.add_reduce
public< N extends Node > N add_reduce(N n)
Definition: GVNGCM.java:49
com.cliffc.aa.node.ParmNode
Definition: ParmNode.java:14
com.cliffc.aa.node.FunNode._bal_close
String _bal_close
Definition: FunNode.java:60
com.cliffc.aa.node.NewNode.NewPrimNode._reads
final boolean _reads
Definition: NewNode.java:173
com.cliffc.aa.node.Node.pop
Node pop()
Definition: Node.java:174
com.cliffc.aa.GVNGCM.Build
Definition: GVNGCM.java:356
com.cliffc.aa.node.NewNode.NewNode
NewNode(byte type, int alias, T to, Node fld)
Definition: NewNode.java:42
com.cliffc.aa.GVNGCM.add_flow
public< N extends Node > N add_flow(N n)
Definition: GVNGCM.java:50
com.cliffc.aa.node.NewNode._alias
int _alias
Definition: NewNode.java:20
com.cliffc.aa.node.FunNode
Definition: FunNode.java:58
com
com.cliffc.aa.node.NewNode.valueobj
abstract TypeObj valueobj()
com.cliffc.aa.node.NewNode.NewPrimNode.NewPrimNode
NewPrimNode(byte op, int parent_alias, T to, String name, boolean reads, int op_prec, Type... args)
Definition: NewNode.java:175
com.cliffc.aa.node.NewNode.NewPrimNode.reset
static void reset()
Definition: NewNode.java:188
com.cliffc.aa.type.TypeMem.MEM
static final TypeMem MEM
Definition: TypeMem.java:224
com.cliffc.aa.Env.DEFMEM
static DefMemNode DEFMEM
Definition: Env.java:19
com.cliffc.aa.type.TypeMem.at
TypeObj at(int alias)
Definition: TypeMem.java:135
com.cliffc.aa.node.NewNode.all_live
TypeMem all_live()
Definition: NewNode.java:86
com.cliffc.aa.node.Node.op_prec
byte op_prec()
Definition: Node.java:533
com.cliffc.aa.Env
Definition: Env.java:12
com.cliffc.aa.type
Definition: Bits.java:1
com.cliffc.aa.node.Node._defs
Ary< Node > _defs
Definition: Node.java:124
com.cliffc.aa.type.TypeMemPtr
Definition: TypeMemPtr.java:14
com.cliffc.aa.Env.ALL_CTRL
static ConNode ALL_CTRL
Definition: Env.java:20
com.cliffc.aa.GVNGCM.Mode
Definition: GVNGCM.java:14
com.cliffc.aa.node.NewNode.is_unused
boolean is_unused()
Definition: NewNode.java:100
com.cliffc.aa.type.TypeMemPtr.make
static TypeMemPtr make(BitsAlias aliases, TypeObj obj)
Definition: TypeMemPtr.java:66