aa
IfNode.java
Go to the documentation of this file.
1 package com.cliffc.aa.node;
2 
3 import com.cliffc.aa.AA;
4 import com.cliffc.aa.Env;
5 import com.cliffc.aa.GVNGCM;
6 import com.cliffc.aa.type.*;
7 
8 // Split control
9 public class IfNode extends Node {
10  public IfNode( Node ctrl, Node pred ) { super(OP_IF,ctrl,pred); }
11 
12  @Override public Node ideal_reduce() {
13  if( _keep>0 ) return null;
14  Node ctl = in(0);
15  Node tst = in(1);
16  if( ctl._val == Type.XCTRL && in(1)!=Env.ANY )
17  return set_def(1,Env.ANY); // Kill test; control projections fold up other ways
18  // Binary test vs 0?
19  if( tst._defs._len==3 &&
20  (tst.val(1)==Type.XNIL || tst.val(2)==Type.XNIL) ) {
21  // Remove leading test-vs-0
22  if( tst instanceof PrimNode.EQ_I64 ) throw AA.unimpl();
23  if( tst instanceof PrimNode.EQ_F64 ) throw AA.unimpl();
24  if( tst instanceof PrimNode.EQ_OOP ) throw AA.unimpl();
25 
26  // Remove leading negation-vs-0 by inverting
27  if( tst instanceof PrimNode.NE_I64 ) throw AA.unimpl();
28  if( tst instanceof PrimNode.NE_F64 ) throw AA.unimpl();
29  if( tst instanceof PrimNode.NE_OOP ) throw AA.unimpl();
30  }
31 
32  if( tst instanceof PrimNode.Not && tst._uses._len==1 )
33  return flip(Env.GVN.xreduce(new IfNode(ctl,tst.in(1))));
34 
35  return null;
36  }
37  Node flip(Node that) {
38  ProjNode p0 = (ProjNode)_uses.atX(0);
39  ProjNode p1 = (ProjNode)_uses.atX(1);
40  if( p0==null || p0._keep>0 || p1==null || p1._keep>0 ) return null; // Not well formed
41  if( p0._idx==1 ) { ProjNode tmp=p0; p0=p1; p1=tmp; }
42  Node x0 = Env.GVN.xreduce(new CProjNode(that,0));
43  Node x1 = Env.GVN.xreduce(new CProjNode(that,1));
44  p0.subsume(x1);
45  p1.subsume(x0);
46  x0._live = x1._live = that._live = this._live;
47  return that;
48  }
49 
50 
51  @Override public TypeTuple value(GVNGCM.Mode opt_mode) {
52  // If the input is exactly zero, we can return false: {ANY,CONTROL}
53  // If the input excludes zero, we can return true : {CONTROL,ANY}
54  // If the input excludes both, we can return ANY: {ANY,ANY}
55  // If the input includes both, we can return both: {CONTROL,CONTROL}
56  Type ctrl = val(0);
57  if( ctrl!=Type.CTRL && ctrl != Type.ALL ) return TypeTuple.IF_ANY; // Test is dead
58  if( in(0) instanceof ProjNode && in(0).in(0)==this )
59  return TypeTuple.IF_ANY; // Test is dead cycle of self (during collapse of dead loops)
60  Type pred = val(1);
61  if( pred instanceof TypeTuple)return TypeTuple.IF_ANY;// Nonsense, so test is dead
62  if( pred instanceof TypeObj ) return TypeTuple.IF_ANY;// Nonsense, so test is dead
63  if( pred.isa(TypeInt.XINT1) ) return TypeTuple.IF_ANY; // Choice of {0,1}
64  if( TypeInt.BOOL.isa(pred) ) return TypeTuple.IF_ALL; // Can be either
65  if( pred == TypeInt.FALSE || pred == Type.NIL || pred==Type.XNIL )
66  return TypeTuple.IF_FALSE; // False only
67 
68  // Already checked for exactly NIL.
69  // If pred maybe a nil, then we can choose nil or something else
70  if( pred. may_nil() ) return TypeTuple.IF_ANY;
71  // If pred must include a nil, then we can see nil or something else
72  if( pred.must_nil() ) return TypeTuple.IF_ALL;
73  // Let input fall before deciding
74  if( pred.above_center() ) return TypeTuple.IF_ANY;
75  // If meeting a nil changes things, then the original excluded nil and so
76  // was always true.
77  if( pred.meet_nil(Type.NIL) != pred ) return TypeTuple.IF_TRUE;
78 
79  throw AA.unimpl(); // Dunno what test this is?
80  }
81  @Override public TypeMem all_live() { return TypeMem.ALIVE; }
82 
83  //@Override public TV2 new_tvar(String alloc_site) { return TV2.make("If",this,alloc_site); }
84 
85  @Override public Node is_copy(int idx) {
86  if( !(_val instanceof TypeTuple) ) return null;
87  TypeTuple tt = (TypeTuple) _val;
88  if( tt==TypeTuple.IF_ANY ) return Env.XCTRL;
89  if( tt==TypeTuple.IF_TRUE && idx==1 ) return in(0);
90  if( tt==TypeTuple.IF_FALSE && idx==0 ) return in(0);
91  return null;
92  }
93 }
com.cliffc.aa.node.PrimNode
Definition: PrimNode.java:20
com.cliffc.aa.type.Type.isa
boolean isa(Type t)
Definition: Type.java:623
com.cliffc.aa.Env.XCTRL
static ConNode XCTRL
Definition: Env.java:21
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
com.cliffc.aa.node.IfNode
Definition: IfNode.java:9
com.cliffc.aa.node.IfNode.value
TypeTuple value(GVNGCM.Mode opt_mode)
Definition: IfNode.java:51
com.cliffc.aa.node.Node
Definition: Node.java:16
com.cliffc.aa.node.Node.subsume
Node subsume(Node nnn)
Definition: Node.java:201
com.cliffc.aa.type.TypeInt
Definition: TypeInt.java:9
com.cliffc.aa.type.Type
an implementation of language AA
Definition: Type.java:94
com.cliffc.aa.node.CProjNode
Definition: CProjNode.java:10
com.cliffc.aa.type.Type.meet_nil
Type meet_nil(Type nil)
Definition: Type.java:904
com.cliffc.aa.type.TypeTuple
Definition: TypeTuple.java:11
com.cliffc.aa.node.IfNode.all_live
TypeMem all_live()
Definition: IfNode.java:81
com.cliffc.aa.node.Node._val
Type _val
Definition: Node.java:88
com.cliffc.aa.node.IfNode.is_copy
Node is_copy(int idx)
Definition: IfNode.java:85
com.cliffc.aa.node.PrimNode.NE_OOP
Definition: PrimNode.java:446
com.cliffc.aa.node.PrimNode.NE_F64
Definition: PrimNode.java:315
com.cliffc.aa.AA.unimpl
static RuntimeException unimpl()
Definition: AA.java:10
com.cliffc.aa.node.PrimNode.EQ_I64
Definition: PrimNode.java:411
com.cliffc.aa.type.TypeTuple.IF_TRUE
static final TypeTuple IF_TRUE
Definition: TypeTuple.java:125
com.cliffc.aa.type.Type.ALL
static final Type ALL
Definition: Type.java:324
com.cliffc.aa.type.TypeTuple.IF_ANY
static final TypeTuple IF_ANY
Definition: TypeTuple.java:124
com.cliffc.aa.Env.GVN
static final GVNGCM GVN
Definition: Env.java:13
com.cliffc.aa.type.TypeObj
Definition: TypeObj.java:15
com.cliffc.aa.type.Type.above_center
boolean above_center()
Definition: Type.java:741
com.cliffc.aa.node.IfNode.IfNode
IfNode(Node ctrl, Node pred)
Definition: IfNode.java:10
com.cliffc.aa.node.ProjNode._idx
int _idx
Definition: ProjNode.java:12
com.cliffc.aa.node.Node.OP_IF
static final byte OP_IF
Definition: Node.java:29
com.cliffc.aa.node.Node._keep
byte _keep
Definition: Node.java:86
com.cliffc.aa.GVNGCM.xreduce
Node xreduce(Node n)
Definition: GVNGCM.java:135
com.cliffc.aa.type.Type.CTRL
static final Type CTRL
Definition: Type.java:326
com.cliffc.aa.node.Node.in
Node in(int i)
Definition: Node.java:126
com.cliffc.aa.type.TypeTuple.IF_ALL
static final TypeTuple IF_ALL
Definition: TypeTuple.java:123
com.cliffc.aa.type.Type.must_nil
boolean must_nil()
Definition: Type.java:845
com.cliffc.aa.type.TypeMem.ALIVE
static final TypeMem ALIVE
Definition: TypeMem.java:226
com.cliffc.aa.type.Type.XCTRL
static final Type XCTRL
Definition: Type.java:327
com.cliffc.aa.GVNGCM
Definition: GVNGCM.java:12
com.cliffc.aa.node.IfNode.ideal_reduce
Node ideal_reduce()
Definition: IfNode.java:12
com.cliffc.aa.node.PrimNode.EQ_OOP
Definition: PrimNode.java:415
com.cliffc.aa.node.ProjNode
Definition: ProjNode.java:11
com.cliffc.aa.type.Type.NIL
static final Type NIL
Definition: Type.java:332
com.cliffc.aa.node.Node._uses
Ary< Node > _uses
Definition: Node.java:245
com.cliffc.aa.AA
an implementation of language AA
Definition: AA.java:9
com.cliffc.aa.type.TypeInt.XINT1
static final TypeInt XINT1
Definition: TypeInt.java:46
com.cliffc.aa.node.Node.val
Type val(int idx)
Definition: Node.java:470
com.cliffc.aa
Definition: AA.java:1
com.cliffc.aa.node.PrimNode.Not
Definition: PrimNode.java:473
com.cliffc.aa.node.PrimNode.NE_I64
Definition: PrimNode.java:412
com.cliffc.aa.type.TypeInt.FALSE
static final Type FALSE
Definition: TypeInt.java:45
com.cliffc.aa.node.Node.set_def
Node set_def(int idx, Node n)
Definition: Node.java:154
com.cliffc.aa.type.Type.XNIL
static final Type XNIL
Definition: Type.java:333
com.cliffc.aa.node.IfNode.flip
Node flip(Node that)
Definition: IfNode.java:37
com.cliffc.aa.node.PrimNode.EQ_F64
Definition: PrimNode.java:314
com.cliffc.aa.Env.ANY
static ConNode ANY
Definition: Env.java:24
com
com.cliffc.aa.Env
Definition: Env.java:12
com.cliffc.aa.type.TypeInt.BOOL
static final TypeInt BOOL
Definition: TypeInt.java:43
com.cliffc.aa.type.TypeTuple.IF_FALSE
static final TypeTuple IF_FALSE
Definition: TypeTuple.java:126
com.cliffc.aa.type
Definition: Bits.java:1
com.cliffc.aa.node.Node._defs
Ary< Node > _defs
Definition: Node.java:124
com.cliffc.aa.GVNGCM.Mode
Definition: GVNGCM.java:14