Wed, 13 Apr 2011 14:33:03 -0700
6988308: assert((cnt > 0.0f) && (prob > 0.0f)) failed: Bad frequency assignment in if
Summary: Make sure cnt doesn't become negative and integer overflow doesn't happen.
Reviewed-by: kvn, twisti
duke@435 | 1 | /* |
trims@1907 | 2 | * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@435 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@435 | 4 | * |
duke@435 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@435 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@435 | 7 | * published by the Free Software Foundation. |
duke@435 | 8 | * |
duke@435 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@435 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@435 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@435 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@435 | 13 | * accompanied this code). |
duke@435 | 14 | * |
duke@435 | 15 | * You should have received a copy of the GNU General Public License version |
duke@435 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@435 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@435 | 18 | * |
trims@1907 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 20 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 21 | * questions. |
duke@435 | 22 | * |
duke@435 | 23 | */ |
duke@435 | 24 | |
stefank@2314 | 25 | #include "precompiled.hpp" |
stefank@2314 | 26 | #include "compiler/compileLog.hpp" |
stefank@2314 | 27 | #include "memory/allocation.inline.hpp" |
stefank@2314 | 28 | #include "opto/addnode.hpp" |
stefank@2314 | 29 | #include "opto/callnode.hpp" |
stefank@2314 | 30 | #include "opto/cfgnode.hpp" |
stefank@2314 | 31 | #include "opto/connode.hpp" |
stefank@2314 | 32 | #include "opto/loopnode.hpp" |
stefank@2314 | 33 | #include "opto/matcher.hpp" |
stefank@2314 | 34 | #include "opto/mulnode.hpp" |
stefank@2314 | 35 | #include "opto/opcodes.hpp" |
stefank@2314 | 36 | #include "opto/phaseX.hpp" |
stefank@2314 | 37 | #include "opto/subnode.hpp" |
stefank@2314 | 38 | #include "runtime/sharedRuntime.hpp" |
stefank@2314 | 39 | |
duke@435 | 40 | // Portions of code courtesy of Clifford Click |
duke@435 | 41 | |
duke@435 | 42 | // Optimization - Graph Style |
duke@435 | 43 | |
duke@435 | 44 | #include "math.h" |
duke@435 | 45 | |
duke@435 | 46 | //============================================================================= |
duke@435 | 47 | //------------------------------Identity--------------------------------------- |
duke@435 | 48 | // If right input is a constant 0, return the left input. |
duke@435 | 49 | Node *SubNode::Identity( PhaseTransform *phase ) { |
duke@435 | 50 | assert(in(1) != this, "Must already have called Value"); |
duke@435 | 51 | assert(in(2) != this, "Must already have called Value"); |
duke@435 | 52 | |
duke@435 | 53 | // Remove double negation |
duke@435 | 54 | const Type *zero = add_id(); |
duke@435 | 55 | if( phase->type( in(1) )->higher_equal( zero ) && |
duke@435 | 56 | in(2)->Opcode() == Opcode() && |
duke@435 | 57 | phase->type( in(2)->in(1) )->higher_equal( zero ) ) { |
duke@435 | 58 | return in(2)->in(2); |
duke@435 | 59 | } |
duke@435 | 60 | |
never@647 | 61 | // Convert "(X+Y) - Y" into X and "(X+Y) - X" into Y |
duke@435 | 62 | if( in(1)->Opcode() == Op_AddI ) { |
duke@435 | 63 | if( phase->eqv(in(1)->in(2),in(2)) ) |
duke@435 | 64 | return in(1)->in(1); |
never@647 | 65 | if (phase->eqv(in(1)->in(1),in(2))) |
never@647 | 66 | return in(1)->in(2); |
never@647 | 67 | |
duke@435 | 68 | // Also catch: "(X + Opaque2(Y)) - Y". In this case, 'Y' is a loop-varying |
duke@435 | 69 | // trip counter and X is likely to be loop-invariant (that's how O2 Nodes |
duke@435 | 70 | // are originally used, although the optimizer sometimes jiggers things). |
duke@435 | 71 | // This folding through an O2 removes a loop-exit use of a loop-varying |
duke@435 | 72 | // value and generally lowers register pressure in and around the loop. |
duke@435 | 73 | if( in(1)->in(2)->Opcode() == Op_Opaque2 && |
duke@435 | 74 | phase->eqv(in(1)->in(2)->in(1),in(2)) ) |
duke@435 | 75 | return in(1)->in(1); |
duke@435 | 76 | } |
duke@435 | 77 | |
duke@435 | 78 | return ( phase->type( in(2) )->higher_equal( zero ) ) ? in(1) : this; |
duke@435 | 79 | } |
duke@435 | 80 | |
duke@435 | 81 | //------------------------------Value------------------------------------------ |
duke@435 | 82 | // A subtract node differences it's two inputs. |
duke@435 | 83 | const Type *SubNode::Value( PhaseTransform *phase ) const { |
duke@435 | 84 | const Node* in1 = in(1); |
duke@435 | 85 | const Node* in2 = in(2); |
duke@435 | 86 | // Either input is TOP ==> the result is TOP |
duke@435 | 87 | const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); |
duke@435 | 88 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 89 | const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); |
duke@435 | 90 | if( t2 == Type::TOP ) return Type::TOP; |
duke@435 | 91 | |
duke@435 | 92 | // Not correct for SubFnode and AddFNode (must check for infinity) |
duke@435 | 93 | // Equal? Subtract is zero |
duke@435 | 94 | if (phase->eqv_uncast(in1, in2)) return add_id(); |
duke@435 | 95 | |
duke@435 | 96 | // Either input is BOTTOM ==> the result is the local BOTTOM |
duke@435 | 97 | if( t1 == Type::BOTTOM || t2 == Type::BOTTOM ) |
duke@435 | 98 | return bottom_type(); |
duke@435 | 99 | |
duke@435 | 100 | return sub(t1,t2); // Local flavor of type subtraction |
duke@435 | 101 | |
duke@435 | 102 | } |
duke@435 | 103 | |
duke@435 | 104 | //============================================================================= |
duke@435 | 105 | |
duke@435 | 106 | //------------------------------Helper function-------------------------------- |
duke@435 | 107 | static bool ok_to_convert(Node* inc, Node* iv) { |
duke@435 | 108 | // Do not collapse (x+c0)-y if "+" is a loop increment, because the |
duke@435 | 109 | // "-" is loop invariant and collapsing extends the live-range of "x" |
duke@435 | 110 | // to overlap with the "+", forcing another register to be used in |
duke@435 | 111 | // the loop. |
duke@435 | 112 | // This test will be clearer with '&&' (apply DeMorgan's rule) |
duke@435 | 113 | // but I like the early cutouts that happen here. |
duke@435 | 114 | const PhiNode *phi; |
duke@435 | 115 | if( ( !inc->in(1)->is_Phi() || |
duke@435 | 116 | !(phi=inc->in(1)->as_Phi()) || |
duke@435 | 117 | phi->is_copy() || |
duke@435 | 118 | !phi->region()->is_CountedLoop() || |
duke@435 | 119 | inc != phi->region()->as_CountedLoop()->incr() ) |
duke@435 | 120 | && |
duke@435 | 121 | // Do not collapse (x+c0)-iv if "iv" is a loop induction variable, |
duke@435 | 122 | // because "x" maybe invariant. |
duke@435 | 123 | ( !iv->is_loop_iv() ) |
duke@435 | 124 | ) { |
duke@435 | 125 | return true; |
duke@435 | 126 | } else { |
duke@435 | 127 | return false; |
duke@435 | 128 | } |
duke@435 | 129 | } |
duke@435 | 130 | //------------------------------Ideal------------------------------------------ |
duke@435 | 131 | Node *SubINode::Ideal(PhaseGVN *phase, bool can_reshape){ |
duke@435 | 132 | Node *in1 = in(1); |
duke@435 | 133 | Node *in2 = in(2); |
duke@435 | 134 | uint op1 = in1->Opcode(); |
duke@435 | 135 | uint op2 = in2->Opcode(); |
duke@435 | 136 | |
duke@435 | 137 | #ifdef ASSERT |
duke@435 | 138 | // Check for dead loop |
duke@435 | 139 | if( phase->eqv( in1, this ) || phase->eqv( in2, this ) || |
duke@435 | 140 | ( op1 == Op_AddI || op1 == Op_SubI ) && |
duke@435 | 141 | ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) || |
duke@435 | 142 | phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) ) |
duke@435 | 143 | assert(false, "dead loop in SubINode::Ideal"); |
duke@435 | 144 | #endif |
duke@435 | 145 | |
duke@435 | 146 | const Type *t2 = phase->type( in2 ); |
duke@435 | 147 | if( t2 == Type::TOP ) return NULL; |
duke@435 | 148 | // Convert "x-c0" into "x+ -c0". |
duke@435 | 149 | if( t2->base() == Type::Int ){ // Might be bottom or top... |
duke@435 | 150 | const TypeInt *i = t2->is_int(); |
duke@435 | 151 | if( i->is_con() ) |
duke@435 | 152 | return new (phase->C, 3) AddINode(in1, phase->intcon(-i->get_con())); |
duke@435 | 153 | } |
duke@435 | 154 | |
duke@435 | 155 | // Convert "(x+c0) - y" into (x-y) + c0" |
duke@435 | 156 | // Do not collapse (x+c0)-y if "+" is a loop increment or |
duke@435 | 157 | // if "y" is a loop induction variable. |
duke@435 | 158 | if( op1 == Op_AddI && ok_to_convert(in1, in2) ) { |
duke@435 | 159 | const Type *tadd = phase->type( in1->in(2) ); |
duke@435 | 160 | if( tadd->singleton() && tadd != Type::TOP ) { |
duke@435 | 161 | Node *sub2 = phase->transform( new (phase->C, 3) SubINode( in1->in(1), in2 )); |
duke@435 | 162 | return new (phase->C, 3) AddINode( sub2, in1->in(2) ); |
duke@435 | 163 | } |
duke@435 | 164 | } |
duke@435 | 165 | |
duke@435 | 166 | |
duke@435 | 167 | // Convert "x - (y+c0)" into "(x-y) - c0" |
duke@435 | 168 | // Need the same check as in above optimization but reversed. |
duke@435 | 169 | if (op2 == Op_AddI && ok_to_convert(in2, in1)) { |
duke@435 | 170 | Node* in21 = in2->in(1); |
duke@435 | 171 | Node* in22 = in2->in(2); |
duke@435 | 172 | const TypeInt* tcon = phase->type(in22)->isa_int(); |
duke@435 | 173 | if (tcon != NULL && tcon->is_con()) { |
duke@435 | 174 | Node* sub2 = phase->transform( new (phase->C, 3) SubINode(in1, in21) ); |
duke@435 | 175 | Node* neg_c0 = phase->intcon(- tcon->get_con()); |
duke@435 | 176 | return new (phase->C, 3) AddINode(sub2, neg_c0); |
duke@435 | 177 | } |
duke@435 | 178 | } |
duke@435 | 179 | |
duke@435 | 180 | const Type *t1 = phase->type( in1 ); |
duke@435 | 181 | if( t1 == Type::TOP ) return NULL; |
duke@435 | 182 | |
duke@435 | 183 | #ifdef ASSERT |
duke@435 | 184 | // Check for dead loop |
duke@435 | 185 | if( ( op2 == Op_AddI || op2 == Op_SubI ) && |
duke@435 | 186 | ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) || |
duke@435 | 187 | phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) ) |
duke@435 | 188 | assert(false, "dead loop in SubINode::Ideal"); |
duke@435 | 189 | #endif |
duke@435 | 190 | |
duke@435 | 191 | // Convert "x - (x+y)" into "-y" |
duke@435 | 192 | if( op2 == Op_AddI && |
duke@435 | 193 | phase->eqv( in1, in2->in(1) ) ) |
duke@435 | 194 | return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(2)); |
duke@435 | 195 | // Convert "(x-y) - x" into "-y" |
duke@435 | 196 | if( op1 == Op_SubI && |
duke@435 | 197 | phase->eqv( in1->in(1), in2 ) ) |
duke@435 | 198 | return new (phase->C, 3) SubINode( phase->intcon(0),in1->in(2)); |
duke@435 | 199 | // Convert "x - (y+x)" into "-y" |
duke@435 | 200 | if( op2 == Op_AddI && |
duke@435 | 201 | phase->eqv( in1, in2->in(2) ) ) |
duke@435 | 202 | return new (phase->C, 3) SubINode( phase->intcon(0),in2->in(1)); |
duke@435 | 203 | |
duke@435 | 204 | // Convert "0 - (x-y)" into "y-x" |
duke@435 | 205 | if( t1 == TypeInt::ZERO && op2 == Op_SubI ) |
duke@435 | 206 | return new (phase->C, 3) SubINode( in2->in(2), in2->in(1) ); |
duke@435 | 207 | |
duke@435 | 208 | // Convert "0 - (x+con)" into "-con-x" |
duke@435 | 209 | jint con; |
duke@435 | 210 | if( t1 == TypeInt::ZERO && op2 == Op_AddI && |
duke@435 | 211 | (con = in2->in(2)->find_int_con(0)) != 0 ) |
duke@435 | 212 | return new (phase->C, 3) SubINode( phase->intcon(-con), in2->in(1) ); |
duke@435 | 213 | |
duke@435 | 214 | // Convert "(X+A) - (X+B)" into "A - B" |
duke@435 | 215 | if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(1) ) |
duke@435 | 216 | return new (phase->C, 3) SubINode( in1->in(2), in2->in(2) ); |
duke@435 | 217 | |
duke@435 | 218 | // Convert "(A+X) - (B+X)" into "A - B" |
duke@435 | 219 | if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(2) ) |
duke@435 | 220 | return new (phase->C, 3) SubINode( in1->in(1), in2->in(1) ); |
duke@435 | 221 | |
kvn@835 | 222 | // Convert "(A+X) - (X+B)" into "A - B" |
kvn@835 | 223 | if( op1 == Op_AddI && op2 == Op_AddI && in1->in(2) == in2->in(1) ) |
kvn@835 | 224 | return new (phase->C, 3) SubINode( in1->in(1), in2->in(2) ); |
kvn@835 | 225 | |
kvn@835 | 226 | // Convert "(X+A) - (B+X)" into "A - B" |
kvn@835 | 227 | if( op1 == Op_AddI && op2 == Op_AddI && in1->in(1) == in2->in(2) ) |
kvn@835 | 228 | return new (phase->C, 3) SubINode( in1->in(2), in2->in(1) ); |
kvn@835 | 229 | |
duke@435 | 230 | // Convert "A-(B-C)" into (A+C)-B", since add is commutative and generally |
duke@435 | 231 | // nicer to optimize than subtract. |
duke@435 | 232 | if( op2 == Op_SubI && in2->outcnt() == 1) { |
duke@435 | 233 | Node *add1 = phase->transform( new (phase->C, 3) AddINode( in1, in2->in(2) ) ); |
duke@435 | 234 | return new (phase->C, 3) SubINode( add1, in2->in(1) ); |
duke@435 | 235 | } |
duke@435 | 236 | |
duke@435 | 237 | return NULL; |
duke@435 | 238 | } |
duke@435 | 239 | |
duke@435 | 240 | //------------------------------sub-------------------------------------------- |
duke@435 | 241 | // A subtract node differences it's two inputs. |
duke@435 | 242 | const Type *SubINode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 243 | const TypeInt *r0 = t1->is_int(); // Handy access |
duke@435 | 244 | const TypeInt *r1 = t2->is_int(); |
duke@435 | 245 | int32 lo = r0->_lo - r1->_hi; |
duke@435 | 246 | int32 hi = r0->_hi - r1->_lo; |
duke@435 | 247 | |
duke@435 | 248 | // We next check for 32-bit overflow. |
duke@435 | 249 | // If that happens, we just assume all integers are possible. |
duke@435 | 250 | if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR |
duke@435 | 251 | ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND |
duke@435 | 252 | (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR |
duke@435 | 253 | ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs |
duke@435 | 254 | return TypeInt::make(lo,hi,MAX2(r0->_widen,r1->_widen)); |
duke@435 | 255 | else // Overflow; assume all integers |
duke@435 | 256 | return TypeInt::INT; |
duke@435 | 257 | } |
duke@435 | 258 | |
duke@435 | 259 | //============================================================================= |
duke@435 | 260 | //------------------------------Ideal------------------------------------------ |
duke@435 | 261 | Node *SubLNode::Ideal(PhaseGVN *phase, bool can_reshape) { |
duke@435 | 262 | Node *in1 = in(1); |
duke@435 | 263 | Node *in2 = in(2); |
duke@435 | 264 | uint op1 = in1->Opcode(); |
duke@435 | 265 | uint op2 = in2->Opcode(); |
duke@435 | 266 | |
duke@435 | 267 | #ifdef ASSERT |
duke@435 | 268 | // Check for dead loop |
duke@435 | 269 | if( phase->eqv( in1, this ) || phase->eqv( in2, this ) || |
duke@435 | 270 | ( op1 == Op_AddL || op1 == Op_SubL ) && |
duke@435 | 271 | ( phase->eqv( in1->in(1), this ) || phase->eqv( in1->in(2), this ) || |
duke@435 | 272 | phase->eqv( in1->in(1), in1 ) || phase->eqv( in1->in(2), in1 ) ) ) |
duke@435 | 273 | assert(false, "dead loop in SubLNode::Ideal"); |
duke@435 | 274 | #endif |
duke@435 | 275 | |
duke@435 | 276 | if( phase->type( in2 ) == Type::TOP ) return NULL; |
duke@435 | 277 | const TypeLong *i = phase->type( in2 )->isa_long(); |
duke@435 | 278 | // Convert "x-c0" into "x+ -c0". |
duke@435 | 279 | if( i && // Might be bottom or top... |
duke@435 | 280 | i->is_con() ) |
duke@435 | 281 | return new (phase->C, 3) AddLNode(in1, phase->longcon(-i->get_con())); |
duke@435 | 282 | |
duke@435 | 283 | // Convert "(x+c0) - y" into (x-y) + c0" |
duke@435 | 284 | // Do not collapse (x+c0)-y if "+" is a loop increment or |
duke@435 | 285 | // if "y" is a loop induction variable. |
duke@435 | 286 | if( op1 == Op_AddL && ok_to_convert(in1, in2) ) { |
duke@435 | 287 | Node *in11 = in1->in(1); |
duke@435 | 288 | const Type *tadd = phase->type( in1->in(2) ); |
duke@435 | 289 | if( tadd->singleton() && tadd != Type::TOP ) { |
duke@435 | 290 | Node *sub2 = phase->transform( new (phase->C, 3) SubLNode( in11, in2 )); |
duke@435 | 291 | return new (phase->C, 3) AddLNode( sub2, in1->in(2) ); |
duke@435 | 292 | } |
duke@435 | 293 | } |
duke@435 | 294 | |
duke@435 | 295 | // Convert "x - (y+c0)" into "(x-y) - c0" |
duke@435 | 296 | // Need the same check as in above optimization but reversed. |
duke@435 | 297 | if (op2 == Op_AddL && ok_to_convert(in2, in1)) { |
duke@435 | 298 | Node* in21 = in2->in(1); |
duke@435 | 299 | Node* in22 = in2->in(2); |
duke@435 | 300 | const TypeLong* tcon = phase->type(in22)->isa_long(); |
duke@435 | 301 | if (tcon != NULL && tcon->is_con()) { |
duke@435 | 302 | Node* sub2 = phase->transform( new (phase->C, 3) SubLNode(in1, in21) ); |
duke@435 | 303 | Node* neg_c0 = phase->longcon(- tcon->get_con()); |
duke@435 | 304 | return new (phase->C, 3) AddLNode(sub2, neg_c0); |
duke@435 | 305 | } |
duke@435 | 306 | } |
duke@435 | 307 | |
duke@435 | 308 | const Type *t1 = phase->type( in1 ); |
duke@435 | 309 | if( t1 == Type::TOP ) return NULL; |
duke@435 | 310 | |
duke@435 | 311 | #ifdef ASSERT |
duke@435 | 312 | // Check for dead loop |
duke@435 | 313 | if( ( op2 == Op_AddL || op2 == Op_SubL ) && |
duke@435 | 314 | ( phase->eqv( in2->in(1), this ) || phase->eqv( in2->in(2), this ) || |
duke@435 | 315 | phase->eqv( in2->in(1), in2 ) || phase->eqv( in2->in(2), in2 ) ) ) |
duke@435 | 316 | assert(false, "dead loop in SubLNode::Ideal"); |
duke@435 | 317 | #endif |
duke@435 | 318 | |
duke@435 | 319 | // Convert "x - (x+y)" into "-y" |
duke@435 | 320 | if( op2 == Op_AddL && |
duke@435 | 321 | phase->eqv( in1, in2->in(1) ) ) |
duke@435 | 322 | return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO), in2->in(2)); |
duke@435 | 323 | // Convert "x - (y+x)" into "-y" |
duke@435 | 324 | if( op2 == Op_AddL && |
duke@435 | 325 | phase->eqv( in1, in2->in(2) ) ) |
duke@435 | 326 | return new (phase->C, 3) SubLNode( phase->makecon(TypeLong::ZERO),in2->in(1)); |
duke@435 | 327 | |
duke@435 | 328 | // Convert "0 - (x-y)" into "y-x" |
duke@435 | 329 | if( phase->type( in1 ) == TypeLong::ZERO && op2 == Op_SubL ) |
duke@435 | 330 | return new (phase->C, 3) SubLNode( in2->in(2), in2->in(1) ); |
duke@435 | 331 | |
duke@435 | 332 | // Convert "(X+A) - (X+B)" into "A - B" |
duke@435 | 333 | if( op1 == Op_AddL && op2 == Op_AddL && in1->in(1) == in2->in(1) ) |
duke@435 | 334 | return new (phase->C, 3) SubLNode( in1->in(2), in2->in(2) ); |
duke@435 | 335 | |
duke@435 | 336 | // Convert "(A+X) - (B+X)" into "A - B" |
duke@435 | 337 | if( op1 == Op_AddL && op2 == Op_AddL && in1->in(2) == in2->in(2) ) |
duke@435 | 338 | return new (phase->C, 3) SubLNode( in1->in(1), in2->in(1) ); |
duke@435 | 339 | |
duke@435 | 340 | // Convert "A-(B-C)" into (A+C)-B" |
duke@435 | 341 | if( op2 == Op_SubL && in2->outcnt() == 1) { |
duke@435 | 342 | Node *add1 = phase->transform( new (phase->C, 3) AddLNode( in1, in2->in(2) ) ); |
duke@435 | 343 | return new (phase->C, 3) SubLNode( add1, in2->in(1) ); |
duke@435 | 344 | } |
duke@435 | 345 | |
duke@435 | 346 | return NULL; |
duke@435 | 347 | } |
duke@435 | 348 | |
duke@435 | 349 | //------------------------------sub-------------------------------------------- |
duke@435 | 350 | // A subtract node differences it's two inputs. |
duke@435 | 351 | const Type *SubLNode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 352 | const TypeLong *r0 = t1->is_long(); // Handy access |
duke@435 | 353 | const TypeLong *r1 = t2->is_long(); |
duke@435 | 354 | jlong lo = r0->_lo - r1->_hi; |
duke@435 | 355 | jlong hi = r0->_hi - r1->_lo; |
duke@435 | 356 | |
duke@435 | 357 | // We next check for 32-bit overflow. |
duke@435 | 358 | // If that happens, we just assume all integers are possible. |
duke@435 | 359 | if( (((r0->_lo ^ r1->_hi) >= 0) || // lo ends have same signs OR |
duke@435 | 360 | ((r0->_lo ^ lo) >= 0)) && // lo results have same signs AND |
duke@435 | 361 | (((r0->_hi ^ r1->_lo) >= 0) || // hi ends have same signs OR |
duke@435 | 362 | ((r0->_hi ^ hi) >= 0)) ) // hi results have same signs |
duke@435 | 363 | return TypeLong::make(lo,hi,MAX2(r0->_widen,r1->_widen)); |
duke@435 | 364 | else // Overflow; assume all integers |
duke@435 | 365 | return TypeLong::LONG; |
duke@435 | 366 | } |
duke@435 | 367 | |
duke@435 | 368 | //============================================================================= |
duke@435 | 369 | //------------------------------Value------------------------------------------ |
duke@435 | 370 | // A subtract node differences its two inputs. |
duke@435 | 371 | const Type *SubFPNode::Value( PhaseTransform *phase ) const { |
duke@435 | 372 | const Node* in1 = in(1); |
duke@435 | 373 | const Node* in2 = in(2); |
duke@435 | 374 | // Either input is TOP ==> the result is TOP |
duke@435 | 375 | const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); |
duke@435 | 376 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 377 | const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); |
duke@435 | 378 | if( t2 == Type::TOP ) return Type::TOP; |
duke@435 | 379 | |
duke@435 | 380 | // if both operands are infinity of same sign, the result is NaN; do |
duke@435 | 381 | // not replace with zero |
duke@435 | 382 | if( (t1->is_finite() && t2->is_finite()) ) { |
duke@435 | 383 | if( phase->eqv(in1, in2) ) return add_id(); |
duke@435 | 384 | } |
duke@435 | 385 | |
duke@435 | 386 | // Either input is BOTTOM ==> the result is the local BOTTOM |
duke@435 | 387 | const Type *bot = bottom_type(); |
duke@435 | 388 | if( (t1 == bot) || (t2 == bot) || |
duke@435 | 389 | (t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) ) |
duke@435 | 390 | return bot; |
duke@435 | 391 | |
duke@435 | 392 | return sub(t1,t2); // Local flavor of type subtraction |
duke@435 | 393 | } |
duke@435 | 394 | |
duke@435 | 395 | |
duke@435 | 396 | //============================================================================= |
duke@435 | 397 | //------------------------------Ideal------------------------------------------ |
duke@435 | 398 | Node *SubFNode::Ideal(PhaseGVN *phase, bool can_reshape) { |
duke@435 | 399 | const Type *t2 = phase->type( in(2) ); |
duke@435 | 400 | // Convert "x-c0" into "x+ -c0". |
duke@435 | 401 | if( t2->base() == Type::FloatCon ) { // Might be bottom or top... |
duke@435 | 402 | // return new (phase->C, 3) AddFNode(in(1), phase->makecon( TypeF::make(-t2->getf()) ) ); |
duke@435 | 403 | } |
duke@435 | 404 | |
duke@435 | 405 | // Not associative because of boundary conditions (infinity) |
duke@435 | 406 | if( IdealizedNumerics && !phase->C->method()->is_strict() ) { |
duke@435 | 407 | // Convert "x - (x+y)" into "-y" |
duke@435 | 408 | if( in(2)->is_Add() && |
duke@435 | 409 | phase->eqv(in(1),in(2)->in(1) ) ) |
duke@435 | 410 | return new (phase->C, 3) SubFNode( phase->makecon(TypeF::ZERO),in(2)->in(2)); |
duke@435 | 411 | } |
duke@435 | 412 | |
duke@435 | 413 | // Cannot replace 0.0-X with -X because a 'fsub' bytecode computes |
duke@435 | 414 | // 0.0-0.0 as +0.0, while a 'fneg' bytecode computes -0.0. |
duke@435 | 415 | //if( phase->type(in(1)) == TypeF::ZERO ) |
duke@435 | 416 | //return new (phase->C, 2) NegFNode(in(2)); |
duke@435 | 417 | |
duke@435 | 418 | return NULL; |
duke@435 | 419 | } |
duke@435 | 420 | |
duke@435 | 421 | //------------------------------sub-------------------------------------------- |
duke@435 | 422 | // A subtract node differences its two inputs. |
duke@435 | 423 | const Type *SubFNode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 424 | // no folding if one of operands is infinity or NaN, do not do constant folding |
duke@435 | 425 | if( g_isfinite(t1->getf()) && g_isfinite(t2->getf()) ) { |
duke@435 | 426 | return TypeF::make( t1->getf() - t2->getf() ); |
duke@435 | 427 | } |
duke@435 | 428 | else if( g_isnan(t1->getf()) ) { |
duke@435 | 429 | return t1; |
duke@435 | 430 | } |
duke@435 | 431 | else if( g_isnan(t2->getf()) ) { |
duke@435 | 432 | return t2; |
duke@435 | 433 | } |
duke@435 | 434 | else { |
duke@435 | 435 | return Type::FLOAT; |
duke@435 | 436 | } |
duke@435 | 437 | } |
duke@435 | 438 | |
duke@435 | 439 | //============================================================================= |
duke@435 | 440 | //------------------------------Ideal------------------------------------------ |
duke@435 | 441 | Node *SubDNode::Ideal(PhaseGVN *phase, bool can_reshape){ |
duke@435 | 442 | const Type *t2 = phase->type( in(2) ); |
duke@435 | 443 | // Convert "x-c0" into "x+ -c0". |
duke@435 | 444 | if( t2->base() == Type::DoubleCon ) { // Might be bottom or top... |
duke@435 | 445 | // return new (phase->C, 3) AddDNode(in(1), phase->makecon( TypeD::make(-t2->getd()) ) ); |
duke@435 | 446 | } |
duke@435 | 447 | |
duke@435 | 448 | // Not associative because of boundary conditions (infinity) |
duke@435 | 449 | if( IdealizedNumerics && !phase->C->method()->is_strict() ) { |
duke@435 | 450 | // Convert "x - (x+y)" into "-y" |
duke@435 | 451 | if( in(2)->is_Add() && |
duke@435 | 452 | phase->eqv(in(1),in(2)->in(1) ) ) |
duke@435 | 453 | return new (phase->C, 3) SubDNode( phase->makecon(TypeD::ZERO),in(2)->in(2)); |
duke@435 | 454 | } |
duke@435 | 455 | |
duke@435 | 456 | // Cannot replace 0.0-X with -X because a 'dsub' bytecode computes |
duke@435 | 457 | // 0.0-0.0 as +0.0, while a 'dneg' bytecode computes -0.0. |
duke@435 | 458 | //if( phase->type(in(1)) == TypeD::ZERO ) |
duke@435 | 459 | //return new (phase->C, 2) NegDNode(in(2)); |
duke@435 | 460 | |
duke@435 | 461 | return NULL; |
duke@435 | 462 | } |
duke@435 | 463 | |
duke@435 | 464 | //------------------------------sub-------------------------------------------- |
duke@435 | 465 | // A subtract node differences its two inputs. |
duke@435 | 466 | const Type *SubDNode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 467 | // no folding if one of operands is infinity or NaN, do not do constant folding |
duke@435 | 468 | if( g_isfinite(t1->getd()) && g_isfinite(t2->getd()) ) { |
duke@435 | 469 | return TypeD::make( t1->getd() - t2->getd() ); |
duke@435 | 470 | } |
duke@435 | 471 | else if( g_isnan(t1->getd()) ) { |
duke@435 | 472 | return t1; |
duke@435 | 473 | } |
duke@435 | 474 | else if( g_isnan(t2->getd()) ) { |
duke@435 | 475 | return t2; |
duke@435 | 476 | } |
duke@435 | 477 | else { |
duke@435 | 478 | return Type::DOUBLE; |
duke@435 | 479 | } |
duke@435 | 480 | } |
duke@435 | 481 | |
duke@435 | 482 | //============================================================================= |
duke@435 | 483 | //------------------------------Idealize--------------------------------------- |
duke@435 | 484 | // Unlike SubNodes, compare must still flatten return value to the |
duke@435 | 485 | // range -1, 0, 1. |
duke@435 | 486 | // And optimizations like those for (X + Y) - X fail if overflow happens. |
duke@435 | 487 | Node *CmpNode::Identity( PhaseTransform *phase ) { |
duke@435 | 488 | return this; |
duke@435 | 489 | } |
duke@435 | 490 | |
duke@435 | 491 | //============================================================================= |
duke@435 | 492 | //------------------------------cmp-------------------------------------------- |
duke@435 | 493 | // Simplify a CmpI (compare 2 integers) node, based on local information. |
duke@435 | 494 | // If both inputs are constants, compare them. |
duke@435 | 495 | const Type *CmpINode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 496 | const TypeInt *r0 = t1->is_int(); // Handy access |
duke@435 | 497 | const TypeInt *r1 = t2->is_int(); |
duke@435 | 498 | |
duke@435 | 499 | if( r0->_hi < r1->_lo ) // Range is always low? |
duke@435 | 500 | return TypeInt::CC_LT; |
duke@435 | 501 | else if( r0->_lo > r1->_hi ) // Range is always high? |
duke@435 | 502 | return TypeInt::CC_GT; |
duke@435 | 503 | |
duke@435 | 504 | else if( r0->is_con() && r1->is_con() ) { // comparing constants? |
duke@435 | 505 | assert(r0->get_con() == r1->get_con(), "must be equal"); |
duke@435 | 506 | return TypeInt::CC_EQ; // Equal results. |
duke@435 | 507 | } else if( r0->_hi == r1->_lo ) // Range is never high? |
duke@435 | 508 | return TypeInt::CC_LE; |
duke@435 | 509 | else if( r0->_lo == r1->_hi ) // Range is never low? |
duke@435 | 510 | return TypeInt::CC_GE; |
duke@435 | 511 | return TypeInt::CC; // else use worst case results |
duke@435 | 512 | } |
duke@435 | 513 | |
duke@435 | 514 | // Simplify a CmpU (compare 2 integers) node, based on local information. |
duke@435 | 515 | // If both inputs are constants, compare them. |
duke@435 | 516 | const Type *CmpUNode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 517 | assert(!t1->isa_ptr(), "obsolete usage of CmpU"); |
duke@435 | 518 | |
duke@435 | 519 | // comparing two unsigned ints |
duke@435 | 520 | const TypeInt *r0 = t1->is_int(); // Handy access |
duke@435 | 521 | const TypeInt *r1 = t2->is_int(); |
duke@435 | 522 | |
duke@435 | 523 | // Current installed version |
duke@435 | 524 | // Compare ranges for non-overlap |
duke@435 | 525 | juint lo0 = r0->_lo; |
duke@435 | 526 | juint hi0 = r0->_hi; |
duke@435 | 527 | juint lo1 = r1->_lo; |
duke@435 | 528 | juint hi1 = r1->_hi; |
duke@435 | 529 | |
duke@435 | 530 | // If either one has both negative and positive values, |
duke@435 | 531 | // it therefore contains both 0 and -1, and since [0..-1] is the |
duke@435 | 532 | // full unsigned range, the type must act as an unsigned bottom. |
duke@435 | 533 | bool bot0 = ((jint)(lo0 ^ hi0) < 0); |
duke@435 | 534 | bool bot1 = ((jint)(lo1 ^ hi1) < 0); |
duke@435 | 535 | |
duke@435 | 536 | if (bot0 || bot1) { |
duke@435 | 537 | // All unsigned values are LE -1 and GE 0. |
duke@435 | 538 | if (lo0 == 0 && hi0 == 0) { |
duke@435 | 539 | return TypeInt::CC_LE; // 0 <= bot |
duke@435 | 540 | } else if (lo1 == 0 && hi1 == 0) { |
duke@435 | 541 | return TypeInt::CC_GE; // bot >= 0 |
duke@435 | 542 | } |
duke@435 | 543 | } else { |
duke@435 | 544 | // We can use ranges of the form [lo..hi] if signs are the same. |
duke@435 | 545 | assert(lo0 <= hi0 && lo1 <= hi1, "unsigned ranges are valid"); |
duke@435 | 546 | // results are reversed, '-' > '+' for unsigned compare |
duke@435 | 547 | if (hi0 < lo1) { |
duke@435 | 548 | return TypeInt::CC_LT; // smaller |
duke@435 | 549 | } else if (lo0 > hi1) { |
duke@435 | 550 | return TypeInt::CC_GT; // greater |
duke@435 | 551 | } else if (hi0 == lo1 && lo0 == hi1) { |
duke@435 | 552 | return TypeInt::CC_EQ; // Equal results |
duke@435 | 553 | } else if (lo0 >= hi1) { |
duke@435 | 554 | return TypeInt::CC_GE; |
duke@435 | 555 | } else if (hi0 <= lo1) { |
duke@435 | 556 | // Check for special case in Hashtable::get. (See below.) |
duke@435 | 557 | if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && |
duke@435 | 558 | in(1)->Opcode() == Op_ModI && |
duke@435 | 559 | in(1)->in(2) == in(2) ) |
duke@435 | 560 | return TypeInt::CC_LT; |
duke@435 | 561 | return TypeInt::CC_LE; |
duke@435 | 562 | } |
duke@435 | 563 | } |
duke@435 | 564 | // Check for special case in Hashtable::get - the hash index is |
duke@435 | 565 | // mod'ed to the table size so the following range check is useless. |
duke@435 | 566 | // Check for: (X Mod Y) CmpU Y, where the mod result and Y both have |
duke@435 | 567 | // to be positive. |
duke@435 | 568 | // (This is a gross hack, since the sub method never |
duke@435 | 569 | // looks at the structure of the node in any other case.) |
duke@435 | 570 | if ((jint)lo0 >= 0 && (jint)lo1 >= 0 && |
duke@435 | 571 | in(1)->Opcode() == Op_ModI && |
duke@435 | 572 | in(1)->in(2)->uncast() == in(2)->uncast()) |
duke@435 | 573 | return TypeInt::CC_LT; |
duke@435 | 574 | return TypeInt::CC; // else use worst case results |
duke@435 | 575 | } |
duke@435 | 576 | |
duke@435 | 577 | //------------------------------Idealize--------------------------------------- |
duke@435 | 578 | Node *CmpINode::Ideal( PhaseGVN *phase, bool can_reshape ) { |
duke@435 | 579 | if (phase->type(in(2))->higher_equal(TypeInt::ZERO)) { |
duke@435 | 580 | switch (in(1)->Opcode()) { |
duke@435 | 581 | case Op_CmpL3: // Collapse a CmpL3/CmpI into a CmpL |
duke@435 | 582 | return new (phase->C, 3) CmpLNode(in(1)->in(1),in(1)->in(2)); |
duke@435 | 583 | case Op_CmpF3: // Collapse a CmpF3/CmpI into a CmpF |
duke@435 | 584 | return new (phase->C, 3) CmpFNode(in(1)->in(1),in(1)->in(2)); |
duke@435 | 585 | case Op_CmpD3: // Collapse a CmpD3/CmpI into a CmpD |
duke@435 | 586 | return new (phase->C, 3) CmpDNode(in(1)->in(1),in(1)->in(2)); |
duke@435 | 587 | //case Op_SubI: |
duke@435 | 588 | // If (x - y) cannot overflow, then ((x - y) <?> 0) |
duke@435 | 589 | // can be turned into (x <?> y). |
duke@435 | 590 | // This is handled (with more general cases) by Ideal_sub_algebra. |
duke@435 | 591 | } |
duke@435 | 592 | } |
duke@435 | 593 | return NULL; // No change |
duke@435 | 594 | } |
duke@435 | 595 | |
duke@435 | 596 | |
duke@435 | 597 | //============================================================================= |
duke@435 | 598 | // Simplify a CmpL (compare 2 longs ) node, based on local information. |
duke@435 | 599 | // If both inputs are constants, compare them. |
duke@435 | 600 | const Type *CmpLNode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 601 | const TypeLong *r0 = t1->is_long(); // Handy access |
duke@435 | 602 | const TypeLong *r1 = t2->is_long(); |
duke@435 | 603 | |
duke@435 | 604 | if( r0->_hi < r1->_lo ) // Range is always low? |
duke@435 | 605 | return TypeInt::CC_LT; |
duke@435 | 606 | else if( r0->_lo > r1->_hi ) // Range is always high? |
duke@435 | 607 | return TypeInt::CC_GT; |
duke@435 | 608 | |
duke@435 | 609 | else if( r0->is_con() && r1->is_con() ) { // comparing constants? |
duke@435 | 610 | assert(r0->get_con() == r1->get_con(), "must be equal"); |
duke@435 | 611 | return TypeInt::CC_EQ; // Equal results. |
duke@435 | 612 | } else if( r0->_hi == r1->_lo ) // Range is never high? |
duke@435 | 613 | return TypeInt::CC_LE; |
duke@435 | 614 | else if( r0->_lo == r1->_hi ) // Range is never low? |
duke@435 | 615 | return TypeInt::CC_GE; |
duke@435 | 616 | return TypeInt::CC; // else use worst case results |
duke@435 | 617 | } |
duke@435 | 618 | |
duke@435 | 619 | //============================================================================= |
duke@435 | 620 | //------------------------------sub-------------------------------------------- |
duke@435 | 621 | // Simplify an CmpP (compare 2 pointers) node, based on local information. |
duke@435 | 622 | // If both inputs are constants, compare them. |
duke@435 | 623 | const Type *CmpPNode::sub( const Type *t1, const Type *t2 ) const { |
duke@435 | 624 | const TypePtr *r0 = t1->is_ptr(); // Handy access |
duke@435 | 625 | const TypePtr *r1 = t2->is_ptr(); |
duke@435 | 626 | |
duke@435 | 627 | // Undefined inputs makes for an undefined result |
duke@435 | 628 | if( TypePtr::above_centerline(r0->_ptr) || |
duke@435 | 629 | TypePtr::above_centerline(r1->_ptr) ) |
duke@435 | 630 | return Type::TOP; |
duke@435 | 631 | |
duke@435 | 632 | if (r0 == r1 && r0->singleton()) { |
duke@435 | 633 | // Equal pointer constants (klasses, nulls, etc.) |
duke@435 | 634 | return TypeInt::CC_EQ; |
duke@435 | 635 | } |
duke@435 | 636 | |
duke@435 | 637 | // See if it is 2 unrelated classes. |
duke@435 | 638 | const TypeOopPtr* p0 = r0->isa_oopptr(); |
duke@435 | 639 | const TypeOopPtr* p1 = r1->isa_oopptr(); |
duke@435 | 640 | if (p0 && p1) { |
kvn@468 | 641 | Node* in1 = in(1)->uncast(); |
kvn@468 | 642 | Node* in2 = in(2)->uncast(); |
kvn@468 | 643 | AllocateNode* alloc1 = AllocateNode::Ideal_allocation(in1, NULL); |
kvn@468 | 644 | AllocateNode* alloc2 = AllocateNode::Ideal_allocation(in2, NULL); |
kvn@468 | 645 | if (MemNode::detect_ptr_independence(in1, alloc1, in2, alloc2, NULL)) { |
kvn@468 | 646 | return TypeInt::CC_GT; // different pointers |
kvn@468 | 647 | } |
duke@435 | 648 | ciKlass* klass0 = p0->klass(); |
duke@435 | 649 | bool xklass0 = p0->klass_is_exact(); |
duke@435 | 650 | ciKlass* klass1 = p1->klass(); |
duke@435 | 651 | bool xklass1 = p1->klass_is_exact(); |
duke@435 | 652 | int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0); |
duke@435 | 653 | if (klass0 && klass1 && |
duke@435 | 654 | kps != 1 && // both or neither are klass pointers |
never@1103 | 655 | klass0->is_loaded() && !klass0->is_interface() && // do not trust interfaces |
kvn@1218 | 656 | klass1->is_loaded() && !klass1->is_interface() && |
kvn@1218 | 657 | (!klass0->is_obj_array_klass() || |
kvn@1218 | 658 | !klass0->as_obj_array_klass()->base_element_klass()->is_interface()) && |
kvn@1218 | 659 | (!klass1->is_obj_array_klass() || |
kvn@1218 | 660 | !klass1->as_obj_array_klass()->base_element_klass()->is_interface())) { |
rasbold@731 | 661 | bool unrelated_classes = false; |
duke@435 | 662 | // See if neither subclasses the other, or if the class on top |
rasbold@731 | 663 | // is precise. In either of these cases, the compare is known |
rasbold@731 | 664 | // to fail if at least one of the pointers is provably not null. |
duke@435 | 665 | if (klass0->equals(klass1) || // if types are unequal but klasses are |
duke@435 | 666 | !klass0->is_java_klass() || // types not part of Java language? |
duke@435 | 667 | !klass1->is_java_klass()) { // types not part of Java language? |
duke@435 | 668 | // Do nothing; we know nothing for imprecise types |
duke@435 | 669 | } else if (klass0->is_subtype_of(klass1)) { |
rasbold@731 | 670 | // If klass1's type is PRECISE, then classes are unrelated. |
rasbold@731 | 671 | unrelated_classes = xklass1; |
duke@435 | 672 | } else if (klass1->is_subtype_of(klass0)) { |
rasbold@731 | 673 | // If klass0's type is PRECISE, then classes are unrelated. |
rasbold@731 | 674 | unrelated_classes = xklass0; |
duke@435 | 675 | } else { // Neither subtypes the other |
rasbold@731 | 676 | unrelated_classes = true; |
rasbold@731 | 677 | } |
rasbold@731 | 678 | if (unrelated_classes) { |
rasbold@731 | 679 | // The oops classes are known to be unrelated. If the joined PTRs of |
rasbold@731 | 680 | // two oops is not Null and not Bottom, then we are sure that one |
rasbold@731 | 681 | // of the two oops is non-null, and the comparison will always fail. |
rasbold@731 | 682 | TypePtr::PTR jp = r0->join_ptr(r1->_ptr); |
rasbold@731 | 683 | if (jp != TypePtr::Null && jp != TypePtr::BotPTR) { |
rasbold@731 | 684 | return TypeInt::CC_GT; |
rasbold@731 | 685 | } |
duke@435 | 686 | } |
duke@435 | 687 | } |
duke@435 | 688 | } |
duke@435 | 689 | |
duke@435 | 690 | // Known constants can be compared exactly |
duke@435 | 691 | // Null can be distinguished from any NotNull pointers |
duke@435 | 692 | // Unknown inputs makes an unknown result |
duke@435 | 693 | if( r0->singleton() ) { |
duke@435 | 694 | intptr_t bits0 = r0->get_con(); |
duke@435 | 695 | if( r1->singleton() ) |
duke@435 | 696 | return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT; |
duke@435 | 697 | return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC; |
duke@435 | 698 | } else if( r1->singleton() ) { |
duke@435 | 699 | intptr_t bits1 = r1->get_con(); |
duke@435 | 700 | return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC; |
duke@435 | 701 | } else |
duke@435 | 702 | return TypeInt::CC; |
duke@435 | 703 | } |
duke@435 | 704 | |
duke@435 | 705 | //------------------------------Ideal------------------------------------------ |
duke@435 | 706 | // Check for the case of comparing an unknown klass loaded from the primary |
duke@435 | 707 | // super-type array vs a known klass with no subtypes. This amounts to |
duke@435 | 708 | // checking to see an unknown klass subtypes a known klass with no subtypes; |
duke@435 | 709 | // this only happens on an exact match. We can shorten this test by 1 load. |
duke@435 | 710 | Node *CmpPNode::Ideal( PhaseGVN *phase, bool can_reshape ) { |
duke@435 | 711 | // Constant pointer on right? |
duke@435 | 712 | const TypeKlassPtr* t2 = phase->type(in(2))->isa_klassptr(); |
duke@435 | 713 | if (t2 == NULL || !t2->klass_is_exact()) |
duke@435 | 714 | return NULL; |
duke@435 | 715 | // Get the constant klass we are comparing to. |
duke@435 | 716 | ciKlass* superklass = t2->klass(); |
duke@435 | 717 | |
duke@435 | 718 | // Now check for LoadKlass on left. |
duke@435 | 719 | Node* ldk1 = in(1); |
kvn@728 | 720 | if (ldk1->is_DecodeN()) { |
kvn@728 | 721 | ldk1 = ldk1->in(1); |
kvn@728 | 722 | if (ldk1->Opcode() != Op_LoadNKlass ) |
kvn@728 | 723 | return NULL; |
kvn@728 | 724 | } else if (ldk1->Opcode() != Op_LoadKlass ) |
duke@435 | 725 | return NULL; |
duke@435 | 726 | // Take apart the address of the LoadKlass: |
duke@435 | 727 | Node* adr1 = ldk1->in(MemNode::Address); |
duke@435 | 728 | intptr_t con2 = 0; |
duke@435 | 729 | Node* ldk2 = AddPNode::Ideal_base_and_offset(adr1, phase, con2); |
duke@435 | 730 | if (ldk2 == NULL) |
duke@435 | 731 | return NULL; |
duke@435 | 732 | if (con2 == oopDesc::klass_offset_in_bytes()) { |
duke@435 | 733 | // We are inspecting an object's concrete class. |
duke@435 | 734 | // Short-circuit the check if the query is abstract. |
duke@435 | 735 | if (superklass->is_interface() || |
duke@435 | 736 | superklass->is_abstract()) { |
duke@435 | 737 | // Make it come out always false: |
duke@435 | 738 | this->set_req(2, phase->makecon(TypePtr::NULL_PTR)); |
duke@435 | 739 | return this; |
duke@435 | 740 | } |
duke@435 | 741 | } |
duke@435 | 742 | |
duke@435 | 743 | // Check for a LoadKlass from primary supertype array. |
duke@435 | 744 | // Any nested loadklass from loadklass+con must be from the p.s. array. |
kvn@728 | 745 | if (ldk2->is_DecodeN()) { |
kvn@728 | 746 | // Keep ldk2 as DecodeN since it could be used in CmpP below. |
kvn@728 | 747 | if (ldk2->in(1)->Opcode() != Op_LoadNKlass ) |
kvn@728 | 748 | return NULL; |
kvn@728 | 749 | } else if (ldk2->Opcode() != Op_LoadKlass) |
duke@435 | 750 | return NULL; |
duke@435 | 751 | |
duke@435 | 752 | // Verify that we understand the situation |
duke@435 | 753 | if (con2 != (intptr_t) superklass->super_check_offset()) |
duke@435 | 754 | return NULL; // Might be element-klass loading from array klass |
duke@435 | 755 | |
duke@435 | 756 | // If 'superklass' has no subklasses and is not an interface, then we are |
duke@435 | 757 | // assured that the only input which will pass the type check is |
duke@435 | 758 | // 'superklass' itself. |
duke@435 | 759 | // |
duke@435 | 760 | // We could be more liberal here, and allow the optimization on interfaces |
duke@435 | 761 | // which have a single implementor. This would require us to increase the |
duke@435 | 762 | // expressiveness of the add_dependency() mechanism. |
duke@435 | 763 | // %%% Do this after we fix TypeOopPtr: Deps are expressive enough now. |
duke@435 | 764 | |
duke@435 | 765 | // Object arrays must have their base element have no subtypes |
duke@435 | 766 | while (superklass->is_obj_array_klass()) { |
duke@435 | 767 | ciType* elem = superklass->as_obj_array_klass()->element_type(); |
duke@435 | 768 | superklass = elem->as_klass(); |
duke@435 | 769 | } |
duke@435 | 770 | if (superklass->is_instance_klass()) { |
duke@435 | 771 | ciInstanceKlass* ik = superklass->as_instance_klass(); |
duke@435 | 772 | if (ik->has_subklass() || ik->is_interface()) return NULL; |
duke@435 | 773 | // Add a dependency if there is a chance that a subclass will be added later. |
duke@435 | 774 | if (!ik->is_final()) { |
duke@435 | 775 | phase->C->dependencies()->assert_leaf_type(ik); |
duke@435 | 776 | } |
duke@435 | 777 | } |
duke@435 | 778 | |
duke@435 | 779 | // Bypass the dependent load, and compare directly |
duke@435 | 780 | this->set_req(1,ldk2); |
duke@435 | 781 | |
duke@435 | 782 | return this; |
duke@435 | 783 | } |
duke@435 | 784 | |
duke@435 | 785 | //============================================================================= |
coleenp@548 | 786 | //------------------------------sub-------------------------------------------- |
coleenp@548 | 787 | // Simplify an CmpN (compare 2 pointers) node, based on local information. |
coleenp@548 | 788 | // If both inputs are constants, compare them. |
coleenp@548 | 789 | const Type *CmpNNode::sub( const Type *t1, const Type *t2 ) const { |
kvn@656 | 790 | const TypePtr *r0 = t1->make_ptr(); // Handy access |
kvn@656 | 791 | const TypePtr *r1 = t2->make_ptr(); |
coleenp@548 | 792 | |
coleenp@548 | 793 | // Undefined inputs makes for an undefined result |
coleenp@548 | 794 | if( TypePtr::above_centerline(r0->_ptr) || |
coleenp@548 | 795 | TypePtr::above_centerline(r1->_ptr) ) |
coleenp@548 | 796 | return Type::TOP; |
coleenp@548 | 797 | |
coleenp@548 | 798 | if (r0 == r1 && r0->singleton()) { |
coleenp@548 | 799 | // Equal pointer constants (klasses, nulls, etc.) |
coleenp@548 | 800 | return TypeInt::CC_EQ; |
coleenp@548 | 801 | } |
coleenp@548 | 802 | |
coleenp@548 | 803 | // See if it is 2 unrelated classes. |
coleenp@548 | 804 | const TypeOopPtr* p0 = r0->isa_oopptr(); |
coleenp@548 | 805 | const TypeOopPtr* p1 = r1->isa_oopptr(); |
coleenp@548 | 806 | if (p0 && p1) { |
coleenp@548 | 807 | ciKlass* klass0 = p0->klass(); |
coleenp@548 | 808 | bool xklass0 = p0->klass_is_exact(); |
coleenp@548 | 809 | ciKlass* klass1 = p1->klass(); |
coleenp@548 | 810 | bool xklass1 = p1->klass_is_exact(); |
coleenp@548 | 811 | int kps = (p0->isa_klassptr()?1:0) + (p1->isa_klassptr()?1:0); |
coleenp@548 | 812 | if (klass0 && klass1 && |
coleenp@548 | 813 | kps != 1 && // both or neither are klass pointers |
coleenp@548 | 814 | !klass0->is_interface() && // do not trust interfaces |
coleenp@548 | 815 | !klass1->is_interface()) { |
rasbold@731 | 816 | bool unrelated_classes = false; |
coleenp@548 | 817 | // See if neither subclasses the other, or if the class on top |
rasbold@731 | 818 | // is precise. In either of these cases, the compare is known |
rasbold@731 | 819 | // to fail if at least one of the pointers is provably not null. |
coleenp@548 | 820 | if (klass0->equals(klass1) || // if types are unequal but klasses are |
coleenp@548 | 821 | !klass0->is_java_klass() || // types not part of Java language? |
coleenp@548 | 822 | !klass1->is_java_klass()) { // types not part of Java language? |
coleenp@548 | 823 | // Do nothing; we know nothing for imprecise types |
coleenp@548 | 824 | } else if (klass0->is_subtype_of(klass1)) { |
rasbold@731 | 825 | // If klass1's type is PRECISE, then classes are unrelated. |
rasbold@731 | 826 | unrelated_classes = xklass1; |
coleenp@548 | 827 | } else if (klass1->is_subtype_of(klass0)) { |
rasbold@731 | 828 | // If klass0's type is PRECISE, then classes are unrelated. |
rasbold@731 | 829 | unrelated_classes = xklass0; |
coleenp@548 | 830 | } else { // Neither subtypes the other |
rasbold@731 | 831 | unrelated_classes = true; |
rasbold@731 | 832 | } |
rasbold@731 | 833 | if (unrelated_classes) { |
rasbold@731 | 834 | // The oops classes are known to be unrelated. If the joined PTRs of |
rasbold@731 | 835 | // two oops is not Null and not Bottom, then we are sure that one |
rasbold@731 | 836 | // of the two oops is non-null, and the comparison will always fail. |
rasbold@731 | 837 | TypePtr::PTR jp = r0->join_ptr(r1->_ptr); |
rasbold@731 | 838 | if (jp != TypePtr::Null && jp != TypePtr::BotPTR) { |
rasbold@731 | 839 | return TypeInt::CC_GT; |
rasbold@731 | 840 | } |
coleenp@548 | 841 | } |
coleenp@548 | 842 | } |
coleenp@548 | 843 | } |
coleenp@548 | 844 | |
coleenp@548 | 845 | // Known constants can be compared exactly |
coleenp@548 | 846 | // Null can be distinguished from any NotNull pointers |
coleenp@548 | 847 | // Unknown inputs makes an unknown result |
coleenp@548 | 848 | if( r0->singleton() ) { |
coleenp@548 | 849 | intptr_t bits0 = r0->get_con(); |
coleenp@548 | 850 | if( r1->singleton() ) |
coleenp@548 | 851 | return bits0 == r1->get_con() ? TypeInt::CC_EQ : TypeInt::CC_GT; |
coleenp@548 | 852 | return ( r1->_ptr == TypePtr::NotNull && bits0==0 ) ? TypeInt::CC_GT : TypeInt::CC; |
coleenp@548 | 853 | } else if( r1->singleton() ) { |
coleenp@548 | 854 | intptr_t bits1 = r1->get_con(); |
coleenp@548 | 855 | return ( r0->_ptr == TypePtr::NotNull && bits1==0 ) ? TypeInt::CC_GT : TypeInt::CC; |
coleenp@548 | 856 | } else |
coleenp@548 | 857 | return TypeInt::CC; |
coleenp@548 | 858 | } |
coleenp@548 | 859 | |
coleenp@548 | 860 | //------------------------------Ideal------------------------------------------ |
coleenp@548 | 861 | Node *CmpNNode::Ideal( PhaseGVN *phase, bool can_reshape ) { |
coleenp@548 | 862 | return NULL; |
coleenp@548 | 863 | } |
coleenp@548 | 864 | |
coleenp@548 | 865 | //============================================================================= |
duke@435 | 866 | //------------------------------Value------------------------------------------ |
duke@435 | 867 | // Simplify an CmpF (compare 2 floats ) node, based on local information. |
duke@435 | 868 | // If both inputs are constants, compare them. |
duke@435 | 869 | const Type *CmpFNode::Value( PhaseTransform *phase ) const { |
duke@435 | 870 | const Node* in1 = in(1); |
duke@435 | 871 | const Node* in2 = in(2); |
duke@435 | 872 | // Either input is TOP ==> the result is TOP |
duke@435 | 873 | const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); |
duke@435 | 874 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 875 | const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); |
duke@435 | 876 | if( t2 == Type::TOP ) return Type::TOP; |
duke@435 | 877 | |
duke@435 | 878 | // Not constants? Don't know squat - even if they are the same |
duke@435 | 879 | // value! If they are NaN's they compare to LT instead of EQ. |
duke@435 | 880 | const TypeF *tf1 = t1->isa_float_constant(); |
duke@435 | 881 | const TypeF *tf2 = t2->isa_float_constant(); |
duke@435 | 882 | if( !tf1 || !tf2 ) return TypeInt::CC; |
duke@435 | 883 | |
duke@435 | 884 | // This implements the Java bytecode fcmpl, so unordered returns -1. |
duke@435 | 885 | if( tf1->is_nan() || tf2->is_nan() ) |
duke@435 | 886 | return TypeInt::CC_LT; |
duke@435 | 887 | |
duke@435 | 888 | if( tf1->_f < tf2->_f ) return TypeInt::CC_LT; |
duke@435 | 889 | if( tf1->_f > tf2->_f ) return TypeInt::CC_GT; |
duke@435 | 890 | assert( tf1->_f == tf2->_f, "do not understand FP behavior" ); |
duke@435 | 891 | return TypeInt::CC_EQ; |
duke@435 | 892 | } |
duke@435 | 893 | |
duke@435 | 894 | |
duke@435 | 895 | //============================================================================= |
duke@435 | 896 | //------------------------------Value------------------------------------------ |
duke@435 | 897 | // Simplify an CmpD (compare 2 doubles ) node, based on local information. |
duke@435 | 898 | // If both inputs are constants, compare them. |
duke@435 | 899 | const Type *CmpDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 900 | const Node* in1 = in(1); |
duke@435 | 901 | const Node* in2 = in(2); |
duke@435 | 902 | // Either input is TOP ==> the result is TOP |
duke@435 | 903 | const Type* t1 = (in1 == this) ? Type::TOP : phase->type(in1); |
duke@435 | 904 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 905 | const Type* t2 = (in2 == this) ? Type::TOP : phase->type(in2); |
duke@435 | 906 | if( t2 == Type::TOP ) return Type::TOP; |
duke@435 | 907 | |
duke@435 | 908 | // Not constants? Don't know squat - even if they are the same |
duke@435 | 909 | // value! If they are NaN's they compare to LT instead of EQ. |
duke@435 | 910 | const TypeD *td1 = t1->isa_double_constant(); |
duke@435 | 911 | const TypeD *td2 = t2->isa_double_constant(); |
duke@435 | 912 | if( !td1 || !td2 ) return TypeInt::CC; |
duke@435 | 913 | |
duke@435 | 914 | // This implements the Java bytecode dcmpl, so unordered returns -1. |
duke@435 | 915 | if( td1->is_nan() || td2->is_nan() ) |
duke@435 | 916 | return TypeInt::CC_LT; |
duke@435 | 917 | |
duke@435 | 918 | if( td1->_d < td2->_d ) return TypeInt::CC_LT; |
duke@435 | 919 | if( td1->_d > td2->_d ) return TypeInt::CC_GT; |
duke@435 | 920 | assert( td1->_d == td2->_d, "do not understand FP behavior" ); |
duke@435 | 921 | return TypeInt::CC_EQ; |
duke@435 | 922 | } |
duke@435 | 923 | |
duke@435 | 924 | //------------------------------Ideal------------------------------------------ |
duke@435 | 925 | Node *CmpDNode::Ideal(PhaseGVN *phase, bool can_reshape){ |
duke@435 | 926 | // Check if we can change this to a CmpF and remove a ConvD2F operation. |
duke@435 | 927 | // Change (CMPD (F2D (float)) (ConD value)) |
duke@435 | 928 | // To (CMPF (float) (ConF value)) |
duke@435 | 929 | // Valid when 'value' does not lose precision as a float. |
duke@435 | 930 | // Benefits: eliminates conversion, does not require 24-bit mode |
duke@435 | 931 | |
duke@435 | 932 | // NaNs prevent commuting operands. This transform works regardless of the |
duke@435 | 933 | // order of ConD and ConvF2D inputs by preserving the original order. |
duke@435 | 934 | int idx_f2d = 1; // ConvF2D on left side? |
duke@435 | 935 | if( in(idx_f2d)->Opcode() != Op_ConvF2D ) |
duke@435 | 936 | idx_f2d = 2; // No, swap to check for reversed args |
duke@435 | 937 | int idx_con = 3-idx_f2d; // Check for the constant on other input |
duke@435 | 938 | |
duke@435 | 939 | if( ConvertCmpD2CmpF && |
duke@435 | 940 | in(idx_f2d)->Opcode() == Op_ConvF2D && |
duke@435 | 941 | in(idx_con)->Opcode() == Op_ConD ) { |
duke@435 | 942 | const TypeD *t2 = in(idx_con)->bottom_type()->is_double_constant(); |
duke@435 | 943 | double t2_value_as_double = t2->_d; |
duke@435 | 944 | float t2_value_as_float = (float)t2_value_as_double; |
duke@435 | 945 | if( t2_value_as_double == (double)t2_value_as_float ) { |
duke@435 | 946 | // Test value can be represented as a float |
duke@435 | 947 | // Eliminate the conversion to double and create new comparison |
duke@435 | 948 | Node *new_in1 = in(idx_f2d)->in(1); |
duke@435 | 949 | Node *new_in2 = phase->makecon( TypeF::make(t2_value_as_float) ); |
duke@435 | 950 | if( idx_f2d != 1 ) { // Must flip args to match original order |
duke@435 | 951 | Node *tmp = new_in1; |
duke@435 | 952 | new_in1 = new_in2; |
duke@435 | 953 | new_in2 = tmp; |
duke@435 | 954 | } |
duke@435 | 955 | CmpFNode *new_cmp = (Opcode() == Op_CmpD3) |
duke@435 | 956 | ? new (phase->C, 3) CmpF3Node( new_in1, new_in2 ) |
duke@435 | 957 | : new (phase->C, 3) CmpFNode ( new_in1, new_in2 ) ; |
duke@435 | 958 | return new_cmp; // Changed to CmpFNode |
duke@435 | 959 | } |
duke@435 | 960 | // Testing value required the precision of a double |
duke@435 | 961 | } |
duke@435 | 962 | return NULL; // No change |
duke@435 | 963 | } |
duke@435 | 964 | |
duke@435 | 965 | |
duke@435 | 966 | //============================================================================= |
duke@435 | 967 | //------------------------------cc2logical------------------------------------- |
duke@435 | 968 | // Convert a condition code type to a logical type |
duke@435 | 969 | const Type *BoolTest::cc2logical( const Type *CC ) const { |
duke@435 | 970 | if( CC == Type::TOP ) return Type::TOP; |
duke@435 | 971 | if( CC->base() != Type::Int ) return TypeInt::BOOL; // Bottom or worse |
duke@435 | 972 | const TypeInt *ti = CC->is_int(); |
duke@435 | 973 | if( ti->is_con() ) { // Only 1 kind of condition codes set? |
duke@435 | 974 | // Match low order 2 bits |
duke@435 | 975 | int tmp = ((ti->get_con()&3) == (_test&3)) ? 1 : 0; |
duke@435 | 976 | if( _test & 4 ) tmp = 1-tmp; // Optionally complement result |
duke@435 | 977 | return TypeInt::make(tmp); // Boolean result |
duke@435 | 978 | } |
duke@435 | 979 | |
duke@435 | 980 | if( CC == TypeInt::CC_GE ) { |
duke@435 | 981 | if( _test == ge ) return TypeInt::ONE; |
duke@435 | 982 | if( _test == lt ) return TypeInt::ZERO; |
duke@435 | 983 | } |
duke@435 | 984 | if( CC == TypeInt::CC_LE ) { |
duke@435 | 985 | if( _test == le ) return TypeInt::ONE; |
duke@435 | 986 | if( _test == gt ) return TypeInt::ZERO; |
duke@435 | 987 | } |
duke@435 | 988 | |
duke@435 | 989 | return TypeInt::BOOL; |
duke@435 | 990 | } |
duke@435 | 991 | |
duke@435 | 992 | //------------------------------dump_spec------------------------------------- |
duke@435 | 993 | // Print special per-node info |
duke@435 | 994 | #ifndef PRODUCT |
duke@435 | 995 | void BoolTest::dump_on(outputStream *st) const { |
duke@435 | 996 | const char *msg[] = {"eq","gt","??","lt","ne","le","??","ge"}; |
duke@435 | 997 | st->print(msg[_test]); |
duke@435 | 998 | } |
duke@435 | 999 | #endif |
duke@435 | 1000 | |
duke@435 | 1001 | //============================================================================= |
duke@435 | 1002 | uint BoolNode::hash() const { return (Node::hash() << 3)|(_test._test+1); } |
duke@435 | 1003 | uint BoolNode::size_of() const { return sizeof(BoolNode); } |
duke@435 | 1004 | |
duke@435 | 1005 | //------------------------------operator==------------------------------------- |
duke@435 | 1006 | uint BoolNode::cmp( const Node &n ) const { |
duke@435 | 1007 | const BoolNode *b = (const BoolNode *)&n; // Cast up |
duke@435 | 1008 | return (_test._test == b->_test._test); |
duke@435 | 1009 | } |
duke@435 | 1010 | |
duke@435 | 1011 | //------------------------------clone_cmp-------------------------------------- |
duke@435 | 1012 | // Clone a compare/bool tree |
duke@435 | 1013 | static Node *clone_cmp( Node *cmp, Node *cmp1, Node *cmp2, PhaseGVN *gvn, BoolTest::mask test ) { |
duke@435 | 1014 | Node *ncmp = cmp->clone(); |
duke@435 | 1015 | ncmp->set_req(1,cmp1); |
duke@435 | 1016 | ncmp->set_req(2,cmp2); |
duke@435 | 1017 | ncmp = gvn->transform( ncmp ); |
duke@435 | 1018 | return new (gvn->C, 2) BoolNode( ncmp, test ); |
duke@435 | 1019 | } |
duke@435 | 1020 | |
duke@435 | 1021 | //-------------------------------make_predicate-------------------------------- |
duke@435 | 1022 | Node* BoolNode::make_predicate(Node* test_value, PhaseGVN* phase) { |
duke@435 | 1023 | if (test_value->is_Con()) return test_value; |
duke@435 | 1024 | if (test_value->is_Bool()) return test_value; |
duke@435 | 1025 | Compile* C = phase->C; |
duke@435 | 1026 | if (test_value->is_CMove() && |
duke@435 | 1027 | test_value->in(CMoveNode::Condition)->is_Bool()) { |
duke@435 | 1028 | BoolNode* bol = test_value->in(CMoveNode::Condition)->as_Bool(); |
duke@435 | 1029 | const Type* ftype = phase->type(test_value->in(CMoveNode::IfFalse)); |
duke@435 | 1030 | const Type* ttype = phase->type(test_value->in(CMoveNode::IfTrue)); |
duke@435 | 1031 | if (ftype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ttype)) { |
duke@435 | 1032 | return bol; |
duke@435 | 1033 | } else if (ttype == TypeInt::ZERO && !TypeInt::ZERO->higher_equal(ftype)) { |
duke@435 | 1034 | return phase->transform( bol->negate(phase) ); |
duke@435 | 1035 | } |
duke@435 | 1036 | // Else fall through. The CMove gets in the way of the test. |
duke@435 | 1037 | // It should be the case that make_predicate(bol->as_int_value()) == bol. |
duke@435 | 1038 | } |
duke@435 | 1039 | Node* cmp = new (C, 3) CmpINode(test_value, phase->intcon(0)); |
duke@435 | 1040 | cmp = phase->transform(cmp); |
duke@435 | 1041 | Node* bol = new (C, 2) BoolNode(cmp, BoolTest::ne); |
duke@435 | 1042 | return phase->transform(bol); |
duke@435 | 1043 | } |
duke@435 | 1044 | |
duke@435 | 1045 | //--------------------------------as_int_value--------------------------------- |
duke@435 | 1046 | Node* BoolNode::as_int_value(PhaseGVN* phase) { |
duke@435 | 1047 | // Inverse to make_predicate. The CMove probably boils down to a Conv2B. |
duke@435 | 1048 | Node* cmov = CMoveNode::make(phase->C, NULL, this, |
duke@435 | 1049 | phase->intcon(0), phase->intcon(1), |
duke@435 | 1050 | TypeInt::BOOL); |
duke@435 | 1051 | return phase->transform(cmov); |
duke@435 | 1052 | } |
duke@435 | 1053 | |
duke@435 | 1054 | //----------------------------------negate------------------------------------- |
duke@435 | 1055 | BoolNode* BoolNode::negate(PhaseGVN* phase) { |
duke@435 | 1056 | Compile* C = phase->C; |
duke@435 | 1057 | return new (C, 2) BoolNode(in(1), _test.negate()); |
duke@435 | 1058 | } |
duke@435 | 1059 | |
duke@435 | 1060 | |
duke@435 | 1061 | //------------------------------Ideal------------------------------------------ |
duke@435 | 1062 | Node *BoolNode::Ideal(PhaseGVN *phase, bool can_reshape) { |
duke@435 | 1063 | // Change "bool tst (cmp con x)" into "bool ~tst (cmp x con)". |
duke@435 | 1064 | // This moves the constant to the right. Helps value-numbering. |
duke@435 | 1065 | Node *cmp = in(1); |
duke@435 | 1066 | if( !cmp->is_Sub() ) return NULL; |
duke@435 | 1067 | int cop = cmp->Opcode(); |
duke@435 | 1068 | if( cop == Op_FastLock || cop == Op_FastUnlock ) return NULL; |
duke@435 | 1069 | Node *cmp1 = cmp->in(1); |
duke@435 | 1070 | Node *cmp2 = cmp->in(2); |
duke@435 | 1071 | if( !cmp1 ) return NULL; |
duke@435 | 1072 | |
duke@435 | 1073 | // Constant on left? |
duke@435 | 1074 | Node *con = cmp1; |
duke@435 | 1075 | uint op2 = cmp2->Opcode(); |
duke@435 | 1076 | // Move constants to the right of compare's to canonicalize. |
duke@435 | 1077 | // Do not muck with Opaque1 nodes, as this indicates a loop |
duke@435 | 1078 | // guard that cannot change shape. |
duke@435 | 1079 | if( con->is_Con() && !cmp2->is_Con() && op2 != Op_Opaque1 && |
duke@435 | 1080 | // Because of NaN's, CmpD and CmpF are not commutative |
duke@435 | 1081 | cop != Op_CmpD && cop != Op_CmpF && |
duke@435 | 1082 | // Protect against swapping inputs to a compare when it is used by a |
duke@435 | 1083 | // counted loop exit, which requires maintaining the loop-limit as in(2) |
duke@435 | 1084 | !is_counted_loop_exit_test() ) { |
duke@435 | 1085 | // Ok, commute the constant to the right of the cmp node. |
duke@435 | 1086 | // Clone the Node, getting a new Node of the same class |
duke@435 | 1087 | cmp = cmp->clone(); |
duke@435 | 1088 | // Swap inputs to the clone |
duke@435 | 1089 | cmp->swap_edges(1, 2); |
duke@435 | 1090 | cmp = phase->transform( cmp ); |
duke@435 | 1091 | return new (phase->C, 2) BoolNode( cmp, _test.commute() ); |
duke@435 | 1092 | } |
duke@435 | 1093 | |
duke@435 | 1094 | // Change "bool eq/ne (cmp (xor X 1) 0)" into "bool ne/eq (cmp X 0)". |
duke@435 | 1095 | // The XOR-1 is an idiom used to flip the sense of a bool. We flip the |
duke@435 | 1096 | // test instead. |
duke@435 | 1097 | int cmp1_op = cmp1->Opcode(); |
duke@435 | 1098 | const TypeInt* cmp2_type = phase->type(cmp2)->isa_int(); |
duke@435 | 1099 | if (cmp2_type == NULL) return NULL; |
duke@435 | 1100 | Node* j_xor = cmp1; |
duke@435 | 1101 | if( cmp2_type == TypeInt::ZERO && |
duke@435 | 1102 | cmp1_op == Op_XorI && |
duke@435 | 1103 | j_xor->in(1) != j_xor && // An xor of itself is dead |
duke@435 | 1104 | phase->type( j_xor->in(2) ) == TypeInt::ONE && |
duke@435 | 1105 | (_test._test == BoolTest::eq || |
duke@435 | 1106 | _test._test == BoolTest::ne) ) { |
duke@435 | 1107 | Node *ncmp = phase->transform(new (phase->C, 3) CmpINode(j_xor->in(1),cmp2)); |
duke@435 | 1108 | return new (phase->C, 2) BoolNode( ncmp, _test.negate() ); |
duke@435 | 1109 | } |
duke@435 | 1110 | |
duke@435 | 1111 | // Change "bool eq/ne (cmp (Conv2B X) 0)" into "bool eq/ne (cmp X 0)". |
duke@435 | 1112 | // This is a standard idiom for branching on a boolean value. |
duke@435 | 1113 | Node *c2b = cmp1; |
duke@435 | 1114 | if( cmp2_type == TypeInt::ZERO && |
duke@435 | 1115 | cmp1_op == Op_Conv2B && |
duke@435 | 1116 | (_test._test == BoolTest::eq || |
duke@435 | 1117 | _test._test == BoolTest::ne) ) { |
duke@435 | 1118 | Node *ncmp = phase->transform(phase->type(c2b->in(1))->isa_int() |
duke@435 | 1119 | ? (Node*)new (phase->C, 3) CmpINode(c2b->in(1),cmp2) |
duke@435 | 1120 | : (Node*)new (phase->C, 3) CmpPNode(c2b->in(1),phase->makecon(TypePtr::NULL_PTR)) |
duke@435 | 1121 | ); |
duke@435 | 1122 | return new (phase->C, 2) BoolNode( ncmp, _test._test ); |
duke@435 | 1123 | } |
duke@435 | 1124 | |
duke@435 | 1125 | // Comparing a SubI against a zero is equal to comparing the SubI |
duke@435 | 1126 | // arguments directly. This only works for eq and ne comparisons |
duke@435 | 1127 | // due to possible integer overflow. |
duke@435 | 1128 | if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) && |
duke@435 | 1129 | (cop == Op_CmpI) && |
duke@435 | 1130 | (cmp1->Opcode() == Op_SubI) && |
duke@435 | 1131 | ( cmp2_type == TypeInt::ZERO ) ) { |
duke@435 | 1132 | Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(1),cmp1->in(2))); |
duke@435 | 1133 | return new (phase->C, 2) BoolNode( ncmp, _test._test ); |
duke@435 | 1134 | } |
duke@435 | 1135 | |
duke@435 | 1136 | // Change (-A vs 0) into (A vs 0) by commuting the test. Disallow in the |
duke@435 | 1137 | // most general case because negating 0x80000000 does nothing. Needed for |
duke@435 | 1138 | // the CmpF3/SubI/CmpI idiom. |
duke@435 | 1139 | if( cop == Op_CmpI && |
duke@435 | 1140 | cmp1->Opcode() == Op_SubI && |
duke@435 | 1141 | cmp2_type == TypeInt::ZERO && |
duke@435 | 1142 | phase->type( cmp1->in(1) ) == TypeInt::ZERO && |
duke@435 | 1143 | phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) { |
duke@435 | 1144 | Node *ncmp = phase->transform( new (phase->C, 3) CmpINode(cmp1->in(2),cmp2)); |
duke@435 | 1145 | return new (phase->C, 2) BoolNode( ncmp, _test.commute() ); |
duke@435 | 1146 | } |
duke@435 | 1147 | |
duke@435 | 1148 | // The transformation below is not valid for either signed or unsigned |
duke@435 | 1149 | // comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE. |
duke@435 | 1150 | // This transformation can be resurrected when we are able to |
duke@435 | 1151 | // make inferences about the range of values being subtracted from |
duke@435 | 1152 | // (or added to) relative to the wraparound point. |
duke@435 | 1153 | // |
duke@435 | 1154 | // // Remove +/-1's if possible. |
duke@435 | 1155 | // // "X <= Y-1" becomes "X < Y" |
duke@435 | 1156 | // // "X+1 <= Y" becomes "X < Y" |
duke@435 | 1157 | // // "X < Y+1" becomes "X <= Y" |
duke@435 | 1158 | // // "X-1 < Y" becomes "X <= Y" |
duke@435 | 1159 | // // Do not this to compares off of the counted-loop-end. These guys are |
duke@435 | 1160 | // // checking the trip counter and they want to use the post-incremented |
duke@435 | 1161 | // // counter. If they use the PRE-incremented counter, then the counter has |
duke@435 | 1162 | // // to be incremented in a private block on a loop backedge. |
duke@435 | 1163 | // if( du && du->cnt(this) && du->out(this)[0]->Opcode() == Op_CountedLoopEnd ) |
duke@435 | 1164 | // return NULL; |
duke@435 | 1165 | // #ifndef PRODUCT |
duke@435 | 1166 | // // Do not do this in a wash GVN pass during verification. |
duke@435 | 1167 | // // Gets triggered by too many simple optimizations to be bothered with |
duke@435 | 1168 | // // re-trying it again and again. |
duke@435 | 1169 | // if( !phase->allow_progress() ) return NULL; |
duke@435 | 1170 | // #endif |
duke@435 | 1171 | // // Not valid for unsigned compare because of corner cases in involving zero. |
duke@435 | 1172 | // // For example, replacing "X-1 <u Y" with "X <=u Y" fails to throw an |
duke@435 | 1173 | // // exception in case X is 0 (because 0-1 turns into 4billion unsigned but |
duke@435 | 1174 | // // "0 <=u Y" is always true). |
duke@435 | 1175 | // if( cmp->Opcode() == Op_CmpU ) return NULL; |
duke@435 | 1176 | // int cmp2_op = cmp2->Opcode(); |
duke@435 | 1177 | // if( _test._test == BoolTest::le ) { |
duke@435 | 1178 | // if( cmp1_op == Op_AddI && |
duke@435 | 1179 | // phase->type( cmp1->in(2) ) == TypeInt::ONE ) |
duke@435 | 1180 | // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::lt ); |
duke@435 | 1181 | // else if( cmp2_op == Op_AddI && |
duke@435 | 1182 | // phase->type( cmp2->in(2) ) == TypeInt::MINUS_1 ) |
duke@435 | 1183 | // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::lt ); |
duke@435 | 1184 | // } else if( _test._test == BoolTest::lt ) { |
duke@435 | 1185 | // if( cmp1_op == Op_AddI && |
duke@435 | 1186 | // phase->type( cmp1->in(2) ) == TypeInt::MINUS_1 ) |
duke@435 | 1187 | // return clone_cmp( cmp, cmp1->in(1), cmp2, phase, BoolTest::le ); |
duke@435 | 1188 | // else if( cmp2_op == Op_AddI && |
duke@435 | 1189 | // phase->type( cmp2->in(2) ) == TypeInt::ONE ) |
duke@435 | 1190 | // return clone_cmp( cmp, cmp1, cmp2->in(1), phase, BoolTest::le ); |
duke@435 | 1191 | // } |
duke@435 | 1192 | |
duke@435 | 1193 | return NULL; |
duke@435 | 1194 | } |
duke@435 | 1195 | |
duke@435 | 1196 | //------------------------------Value------------------------------------------ |
duke@435 | 1197 | // Simplify a Bool (convert condition codes to boolean (1 or 0)) node, |
duke@435 | 1198 | // based on local information. If the input is constant, do it. |
duke@435 | 1199 | const Type *BoolNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1200 | return _test.cc2logical( phase->type( in(1) ) ); |
duke@435 | 1201 | } |
duke@435 | 1202 | |
duke@435 | 1203 | //------------------------------dump_spec-------------------------------------- |
duke@435 | 1204 | // Dump special per-node info |
duke@435 | 1205 | #ifndef PRODUCT |
duke@435 | 1206 | void BoolNode::dump_spec(outputStream *st) const { |
duke@435 | 1207 | st->print("["); |
duke@435 | 1208 | _test.dump_on(st); |
duke@435 | 1209 | st->print("]"); |
duke@435 | 1210 | } |
duke@435 | 1211 | #endif |
duke@435 | 1212 | |
duke@435 | 1213 | //------------------------------is_counted_loop_exit_test-------------------------------------- |
duke@435 | 1214 | // Returns true if node is used by a counted loop node. |
duke@435 | 1215 | bool BoolNode::is_counted_loop_exit_test() { |
duke@435 | 1216 | for( DUIterator_Fast imax, i = fast_outs(imax); i < imax; i++ ) { |
duke@435 | 1217 | Node* use = fast_out(i); |
duke@435 | 1218 | if (use->is_CountedLoopEnd()) { |
duke@435 | 1219 | return true; |
duke@435 | 1220 | } |
duke@435 | 1221 | } |
duke@435 | 1222 | return false; |
duke@435 | 1223 | } |
duke@435 | 1224 | |
duke@435 | 1225 | //============================================================================= |
duke@435 | 1226 | //------------------------------NegNode---------------------------------------- |
duke@435 | 1227 | Node *NegFNode::Ideal(PhaseGVN *phase, bool can_reshape) { |
duke@435 | 1228 | if( in(1)->Opcode() == Op_SubF ) |
duke@435 | 1229 | return new (phase->C, 3) SubFNode( in(1)->in(2), in(1)->in(1) ); |
duke@435 | 1230 | return NULL; |
duke@435 | 1231 | } |
duke@435 | 1232 | |
duke@435 | 1233 | Node *NegDNode::Ideal(PhaseGVN *phase, bool can_reshape) { |
duke@435 | 1234 | if( in(1)->Opcode() == Op_SubD ) |
duke@435 | 1235 | return new (phase->C, 3) SubDNode( in(1)->in(2), in(1)->in(1) ); |
duke@435 | 1236 | return NULL; |
duke@435 | 1237 | } |
duke@435 | 1238 | |
duke@435 | 1239 | |
duke@435 | 1240 | //============================================================================= |
duke@435 | 1241 | //------------------------------Value------------------------------------------ |
duke@435 | 1242 | // Compute sqrt |
duke@435 | 1243 | const Type *SqrtDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1244 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1245 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1246 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1247 | double d = t1->getd(); |
duke@435 | 1248 | if( d < 0.0 ) return Type::DOUBLE; |
duke@435 | 1249 | return TypeD::make( sqrt( d ) ); |
duke@435 | 1250 | } |
duke@435 | 1251 | |
duke@435 | 1252 | //============================================================================= |
duke@435 | 1253 | //------------------------------Value------------------------------------------ |
duke@435 | 1254 | // Compute cos |
duke@435 | 1255 | const Type *CosDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1256 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1257 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1258 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1259 | double d = t1->getd(); |
never@1609 | 1260 | return TypeD::make( StubRoutines::intrinsic_cos( d ) ); |
duke@435 | 1261 | } |
duke@435 | 1262 | |
duke@435 | 1263 | //============================================================================= |
duke@435 | 1264 | //------------------------------Value------------------------------------------ |
duke@435 | 1265 | // Compute sin |
duke@435 | 1266 | const Type *SinDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1267 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1268 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1269 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1270 | double d = t1->getd(); |
never@1609 | 1271 | return TypeD::make( StubRoutines::intrinsic_sin( d ) ); |
duke@435 | 1272 | } |
duke@435 | 1273 | |
duke@435 | 1274 | //============================================================================= |
duke@435 | 1275 | //------------------------------Value------------------------------------------ |
duke@435 | 1276 | // Compute tan |
duke@435 | 1277 | const Type *TanDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1278 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1279 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1280 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1281 | double d = t1->getd(); |
never@1609 | 1282 | return TypeD::make( StubRoutines::intrinsic_tan( d ) ); |
duke@435 | 1283 | } |
duke@435 | 1284 | |
duke@435 | 1285 | //============================================================================= |
duke@435 | 1286 | //------------------------------Value------------------------------------------ |
duke@435 | 1287 | // Compute log |
duke@435 | 1288 | const Type *LogDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1289 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1290 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1291 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1292 | double d = t1->getd(); |
never@1609 | 1293 | return TypeD::make( StubRoutines::intrinsic_log( d ) ); |
duke@435 | 1294 | } |
duke@435 | 1295 | |
duke@435 | 1296 | //============================================================================= |
duke@435 | 1297 | //------------------------------Value------------------------------------------ |
duke@435 | 1298 | // Compute log10 |
duke@435 | 1299 | const Type *Log10DNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1300 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1301 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1302 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1303 | double d = t1->getd(); |
never@1609 | 1304 | return TypeD::make( StubRoutines::intrinsic_log10( d ) ); |
duke@435 | 1305 | } |
duke@435 | 1306 | |
duke@435 | 1307 | //============================================================================= |
duke@435 | 1308 | //------------------------------Value------------------------------------------ |
duke@435 | 1309 | // Compute exp |
duke@435 | 1310 | const Type *ExpDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1311 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1312 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1313 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1314 | double d = t1->getd(); |
never@1609 | 1315 | return TypeD::make( StubRoutines::intrinsic_exp( d ) ); |
duke@435 | 1316 | } |
duke@435 | 1317 | |
duke@435 | 1318 | |
duke@435 | 1319 | //============================================================================= |
duke@435 | 1320 | //------------------------------Value------------------------------------------ |
duke@435 | 1321 | // Compute pow |
duke@435 | 1322 | const Type *PowDNode::Value( PhaseTransform *phase ) const { |
duke@435 | 1323 | const Type *t1 = phase->type( in(1) ); |
duke@435 | 1324 | if( t1 == Type::TOP ) return Type::TOP; |
duke@435 | 1325 | if( t1->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1326 | const Type *t2 = phase->type( in(2) ); |
duke@435 | 1327 | if( t2 == Type::TOP ) return Type::TOP; |
duke@435 | 1328 | if( t2->base() != Type::DoubleCon ) return Type::DOUBLE; |
duke@435 | 1329 | double d1 = t1->getd(); |
duke@435 | 1330 | double d2 = t2->getd(); |
duke@435 | 1331 | if( d1 < 0.0 ) return Type::DOUBLE; |
duke@435 | 1332 | if( d2 < 0.0 ) return Type::DOUBLE; |
never@1609 | 1333 | return TypeD::make( StubRoutines::intrinsic_pow( d1, d2 ) ); |
duke@435 | 1334 | } |