src/share/vm/opto/addnode.cpp

changeset 835
cc80376deb0c
parent 755
2b73d212b1fd
child 1077
660978a2a31a
     1.1 --- a/src/share/vm/opto/addnode.cpp	Tue Sep 30 15:53:55 2008 -0700
     1.2 +++ b/src/share/vm/opto/addnode.cpp	Thu Oct 02 08:37:44 2008 -0700
     1.3 @@ -156,7 +156,8 @@
     1.4    if( add1_op == this_op && !con_right ) {
     1.5      Node *a12 = add1->in(2);
     1.6      const Type *t12 = phase->type( a12 );
     1.7 -    if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) ) {
     1.8 +    if( t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
     1.9 +       !(add1->in(1)->is_Phi() && add1->in(1)->as_Phi()->is_tripcount()) ) {
    1.10        assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
    1.11        add2 = add1->clone();
    1.12        add2->set_req(2, in(2));
    1.13 @@ -173,7 +174,8 @@
    1.14    if( add2_op == this_op && !con_left ) {
    1.15      Node *a22 = add2->in(2);
    1.16      const Type *t22 = phase->type( a22 );
    1.17 -    if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) ) {
    1.18 +    if( t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
    1.19 +       !(add2->in(1)->is_Phi() && add2->in(1)->as_Phi()->is_tripcount()) ) {
    1.20        assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
    1.21        Node *addx = add2->clone();
    1.22        addx->set_req(1, in(1));
    1.23 @@ -225,34 +227,63 @@
    1.24  //=============================================================================
    1.25  //------------------------------Idealize---------------------------------------
    1.26  Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
    1.27 -  int op1 = in(1)->Opcode();
    1.28 -  int op2 = in(2)->Opcode();
    1.29 +  Node* in1 = in(1);
    1.30 +  Node* in2 = in(2);
    1.31 +  int op1 = in1->Opcode();
    1.32 +  int op2 = in2->Opcode();
    1.33    // Fold (con1-x)+con2 into (con1+con2)-x
    1.34 +  if ( op1 == Op_AddI && op2 == Op_SubI ) {
    1.35 +    // Swap edges to try optimizations below
    1.36 +    in1 = in2;
    1.37 +    in2 = in(1);
    1.38 +    op1 = op2;
    1.39 +    op2 = in2->Opcode();
    1.40 +  }
    1.41    if( op1 == Op_SubI ) {
    1.42 -    const Type *t_sub1 = phase->type( in(1)->in(1) );
    1.43 -    const Type *t_2    = phase->type( in(2)        );
    1.44 +    const Type *t_sub1 = phase->type( in1->in(1) );
    1.45 +    const Type *t_2    = phase->type( in2        );
    1.46      if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
    1.47        return new (phase->C, 3) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ),
    1.48 -                              in(1)->in(2) );
    1.49 +                              in1->in(2) );
    1.50      // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
    1.51      if( op2 == Op_SubI ) {
    1.52        // Check for dead cycle: d = (a-b)+(c-d)
    1.53 -      assert( in(1)->in(2) != this && in(2)->in(2) != this,
    1.54 +      assert( in1->in(2) != this && in2->in(2) != this,
    1.55                "dead loop in AddINode::Ideal" );
    1.56        Node *sub  = new (phase->C, 3) SubINode(NULL, NULL);
    1.57 -      sub->init_req(1, phase->transform(new (phase->C, 3) AddINode(in(1)->in(1), in(2)->in(1) ) ));
    1.58 -      sub->init_req(2, phase->transform(new (phase->C, 3) AddINode(in(1)->in(2), in(2)->in(2) ) ));
    1.59 +      sub->init_req(1, phase->transform(new (phase->C, 3) AddINode(in1->in(1), in2->in(1) ) ));
    1.60 +      sub->init_req(2, phase->transform(new (phase->C, 3) AddINode(in1->in(2), in2->in(2) ) ));
    1.61        return sub;
    1.62      }
    1.63 +    // Convert "(a-b)+(b+c)" into "(a+c)"
    1.64 +    if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {
    1.65 +      assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
    1.66 +      return new (phase->C, 3) AddINode(in1->in(1), in2->in(2));
    1.67 +    }
    1.68 +    // Convert "(a-b)+(c+b)" into "(a+c)"
    1.69 +    if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {
    1.70 +      assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
    1.71 +      return new (phase->C, 3) AddINode(in1->in(1), in2->in(1));
    1.72 +    }
    1.73 +    // Convert "(a-b)+(b-c)" into "(a-c)"
    1.74 +    if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {
    1.75 +      assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
    1.76 +      return new (phase->C, 3) SubINode(in1->in(1), in2->in(2));
    1.77 +    }
    1.78 +    // Convert "(a-b)+(c-a)" into "(c-b)"
    1.79 +    if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {
    1.80 +      assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
    1.81 +      return new (phase->C, 3) SubINode(in2->in(1), in1->in(2));
    1.82 +    }
    1.83    }
    1.84  
    1.85    // Convert "x+(0-y)" into "(x-y)"
    1.86 -  if( op2 == Op_SubI && phase->type(in(2)->in(1)) == TypeInt::ZERO )
    1.87 -    return new (phase->C, 3) SubINode(in(1), in(2)->in(2) );
    1.88 +  if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )
    1.89 +    return new (phase->C, 3) SubINode(in1, in2->in(2) );
    1.90  
    1.91    // Convert "(0-y)+x" into "(x-y)"
    1.92 -  if( op1 == Op_SubI && phase->type(in(1)->in(1)) == TypeInt::ZERO )
    1.93 -    return new (phase->C, 3) SubINode( in(2), in(1)->in(2) );
    1.94 +  if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )
    1.95 +    return new (phase->C, 3) SubINode( in2, in1->in(2) );
    1.96  
    1.97    // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
    1.98    // Helps with array allocation math constant folding
    1.99 @@ -266,15 +297,15 @@
   1.100    // Have not observed cases where type information exists to support
   1.101    // positive y and (x <= -(y << z))
   1.102    if( op1 == Op_URShiftI && op2 == Op_ConI &&
   1.103 -      in(1)->in(2)->Opcode() == Op_ConI ) {
   1.104 -    jint z = phase->type( in(1)->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
   1.105 -    jint y = phase->type( in(2) )->is_int()->get_con();
   1.106 +      in1->in(2)->Opcode() == Op_ConI ) {
   1.107 +    jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
   1.108 +    jint y = phase->type( in2 )->is_int()->get_con();
   1.109  
   1.110      if( z < 5 && -5 < y && y < 0 ) {
   1.111 -      const Type *t_in11 = phase->type(in(1)->in(1));
   1.112 +      const Type *t_in11 = phase->type(in1->in(1));
   1.113        if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
   1.114 -        Node *a = phase->transform( new (phase->C, 3) AddINode( in(1)->in(1), phase->intcon(y<<z) ) );
   1.115 -        return new (phase->C, 3) URShiftINode( a, in(1)->in(2) );
   1.116 +        Node *a = phase->transform( new (phase->C, 3) AddINode( in1->in(1), phase->intcon(y<<z) ) );
   1.117 +        return new (phase->C, 3) URShiftINode( a, in1->in(2) );
   1.118        }
   1.119      }
   1.120    }
   1.121 @@ -328,39 +359,73 @@
   1.122  //=============================================================================
   1.123  //------------------------------Idealize---------------------------------------
   1.124  Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
   1.125 -  int op1 = in(1)->Opcode();
   1.126 -  int op2 = in(2)->Opcode();
   1.127 +  Node* in1 = in(1);
   1.128 +  Node* in2 = in(2);
   1.129 +  int op1 = in1->Opcode();
   1.130 +  int op2 = in2->Opcode();
   1.131 +  // Fold (con1-x)+con2 into (con1+con2)-x
   1.132 +  if ( op1 == Op_AddL && op2 == Op_SubL ) {
   1.133 +    // Swap edges to try optimizations below
   1.134 +    in1 = in2;
   1.135 +    in2 = in(1);
   1.136 +    op1 = op2;
   1.137 +    op2 = in2->Opcode();
   1.138 +  }
   1.139    // Fold (con1-x)+con2 into (con1+con2)-x
   1.140    if( op1 == Op_SubL ) {
   1.141 -    const Type *t_sub1 = phase->type( in(1)->in(1) );
   1.142 -    const Type *t_2    = phase->type( in(2)        );
   1.143 +    const Type *t_sub1 = phase->type( in1->in(1) );
   1.144 +    const Type *t_2    = phase->type( in2        );
   1.145      if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
   1.146        return new (phase->C, 3) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ),
   1.147 -                              in(1)->in(2) );
   1.148 +                              in1->in(2) );
   1.149      // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
   1.150      if( op2 == Op_SubL ) {
   1.151        // Check for dead cycle: d = (a-b)+(c-d)
   1.152 -      assert( in(1)->in(2) != this && in(2)->in(2) != this,
   1.153 +      assert( in1->in(2) != this && in2->in(2) != this,
   1.154                "dead loop in AddLNode::Ideal" );
   1.155        Node *sub  = new (phase->C, 3) SubLNode(NULL, NULL);
   1.156 -      sub->init_req(1, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(1), in(2)->in(1) ) ));
   1.157 -      sub->init_req(2, phase->transform(new (phase->C, 3) AddLNode(in(1)->in(2), in(2)->in(2) ) ));
   1.158 +      sub->init_req(1, phase->transform(new (phase->C, 3) AddLNode(in1->in(1), in2->in(1) ) ));
   1.159 +      sub->init_req(2, phase->transform(new (phase->C, 3) AddLNode(in1->in(2), in2->in(2) ) ));
   1.160        return sub;
   1.161      }
   1.162 +    // Convert "(a-b)+(b+c)" into "(a+c)"
   1.163 +    if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {
   1.164 +      assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
   1.165 +      return new (phase->C, 3) AddLNode(in1->in(1), in2->in(2));
   1.166 +    }
   1.167 +    // Convert "(a-b)+(c+b)" into "(a+c)"
   1.168 +    if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {
   1.169 +      assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
   1.170 +      return new (phase->C, 3) AddLNode(in1->in(1), in2->in(1));
   1.171 +    }
   1.172 +    // Convert "(a-b)+(b-c)" into "(a-c)"
   1.173 +    if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {
   1.174 +      assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
   1.175 +      return new (phase->C, 3) SubLNode(in1->in(1), in2->in(2));
   1.176 +    }
   1.177 +    // Convert "(a-b)+(c-a)" into "(c-b)"
   1.178 +    if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) {
   1.179 +      assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
   1.180 +      return new (phase->C, 3) SubLNode(in2->in(1), in1->in(2));
   1.181 +    }
   1.182    }
   1.183  
   1.184    // Convert "x+(0-y)" into "(x-y)"
   1.185 -  if( op2 == Op_SubL && phase->type(in(2)->in(1)) == TypeLong::ZERO )
   1.186 -    return new (phase->C, 3) SubLNode(in(1), in(2)->in(2) );
   1.187 +  if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )
   1.188 +    return new (phase->C, 3) SubLNode( in1, in2->in(2) );
   1.189 +
   1.190 +  // Convert "(0-y)+x" into "(x-y)"
   1.191 +  if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO )
   1.192 +    return new (phase->C, 3) SubLNode( in2, in1->in(2) );
   1.193  
   1.194    // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"
   1.195    // into "(X<<1)+Y" and let shift-folding happen.
   1.196    if( op2 == Op_AddL &&
   1.197 -      in(2)->in(1) == in(1) &&
   1.198 +      in2->in(1) == in1 &&
   1.199        op1 != Op_ConL &&
   1.200        0 ) {
   1.201 -    Node *shift = phase->transform(new (phase->C, 3) LShiftLNode(in(1),phase->intcon(1)));
   1.202 -    return new (phase->C, 3) AddLNode(shift,in(2)->in(2));
   1.203 +    Node *shift = phase->transform(new (phase->C, 3) LShiftLNode(in1,phase->intcon(1)));
   1.204 +    return new (phase->C, 3) AddLNode(shift,in2->in(2));
   1.205    }
   1.206  
   1.207    return AddNode::Ideal(phase, can_reshape);

mercurial