1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_VM_OPTO_CFGNODE_HPP 26 #define SHARE_VM_OPTO_CFGNODE_HPP 27 28 #include "opto/multnode.hpp" 29 #include "opto/node.hpp" 30 #include "opto/opcodes.hpp" 31 #include "opto/type.hpp" 32 33 // Portions of code courtesy of Clifford Click 34 35 // Optimization - Graph Style 36 37 class Matcher; 38 class Node; 39 class RegionNode; 40 class TypeNode; 41 class PhiNode; 42 class GotoNode; 43 class MultiNode; 44 class MultiBranchNode; 45 class IfNode; 46 class PCTableNode; 47 class JumpNode; 48 class CatchNode; 49 class NeverBranchNode; 50 class ProjNode; 51 class CProjNode; 52 class IfTrueNode; 53 class IfFalseNode; 54 class CatchProjNode; 55 class JProjNode; 56 class JumpProjNode; 57 class SCMemProjNode; 58 class PhaseIdealLoop; 59 60 //------------------------------RegionNode------------------------------------- 61 // The class of RegionNodes, which can be mapped to basic blocks in the 62 // program. Their inputs point to Control sources. PhiNodes (described 63 // below) have an input point to a RegionNode. Merged data inputs to PhiNodes 64 // correspond 1-to-1 with RegionNode inputs. The zero input of a PhiNode is 65 // the RegionNode, and the zero input of the RegionNode is itself. 66 class RegionNode : public Node { 67 public: 68 // Node layout (parallels PhiNode): 69 enum { Region, // Generally points to self. 70 Control // Control arcs are [1..len) 71 }; 72 73 RegionNode( uint required ) : Node(required) { 74 init_class_id(Class_Region); 75 init_req(0,this); 76 } 77 78 Node* is_copy() const { 79 const Node* r = _in[Region]; 80 if (r == NULL) 81 return nonnull_req(); 82 return NULL; // not a copy! 83 } 84 PhiNode* has_phi() const; // returns an arbitrary phi user, or NULL 85 PhiNode* has_unique_phi() const; // returns the unique phi user, or NULL 86 // Is this region node unreachable from root? 87 bool is_unreachable_region(PhaseGVN *phase) const; 88 virtual int Opcode() const; 89 virtual bool pinned() const { return (const Node *)in(0) == this; } 90 virtual bool is_CFG () const { return true; } 91 virtual uint hash() const { return NO_HASH; } // CFG nodes do not hash 92 virtual bool depends_only_on_test() const { return false; } 93 virtual const Type *bottom_type() const { return Type::CONTROL; } 94 virtual const Type *Value( PhaseTransform *phase ) const; 95 virtual Node *Identity( PhaseTransform *phase ); 96 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape); 97 virtual const RegMask &out_RegMask() const; 98 bool try_clean_mem_phi(PhaseGVN *phase); 99 }; 100 101 //------------------------------JProjNode-------------------------------------- 102 // jump projection for node that produces multiple control-flow paths 103 class JProjNode : public ProjNode { 104 public: 105 JProjNode( Node* ctrl, uint idx ) : ProjNode(ctrl,idx) {} 106 virtual int Opcode() const; 107 virtual bool is_CFG() const { return true; } 108 virtual uint hash() const { return NO_HASH; } // CFG nodes do not hash 109 virtual const Node* is_block_proj() const { return in(0); } 110 virtual const RegMask& out_RegMask() const; 111 virtual uint ideal_reg() const { return 0; } 112 }; 113 114 //------------------------------PhiNode---------------------------------------- 115 // PhiNodes merge values from different Control paths. Slot 0 points to the 116 // controlling RegionNode. Other slots map 1-for-1 with incoming control flow 117 // paths to the RegionNode. For speed reasons (to avoid another pass) we 118 // can turn PhiNodes into copys in-place by NULL'ing out their RegionNode 119 // input in slot 0. 120 class PhiNode : public TypeNode { 121 const TypePtr* const _adr_type; // non-null only for Type::MEMORY nodes. 122 // The following fields are only used for data PhiNodes to indicate 123 // that the PhiNode represents the value of a known instance field. 124 int _inst_mem_id; // Instance memory id (node index of the memory Phi) 125 const int _inst_id; // Instance id of the memory slice. 126 const int _inst_index; // Alias index of the instance memory slice. 127 // Array elements references have the same alias_idx but different offset. 128 const int _inst_offset; // Offset of the instance memory slice. 129 // Size is bigger to hold the _adr_type field. 130 virtual uint hash() const; // Check the type 131 virtual uint cmp( const Node &n ) const; 132 virtual uint size_of() const { return sizeof(*this); } 133 134 // Determine if CMoveNode::is_cmove_id can be used at this join point. 135 Node* is_cmove_id(PhaseTransform* phase, int true_path); 136 137 public: 138 // Node layout (parallels RegionNode): 139 enum { Region, // Control input is the Phi's region. 140 Input // Input values are [1..len) 141 }; 142 143 PhiNode( Node *r, const Type *t, const TypePtr* at = NULL, 144 const int imid = -1, 145 const int iid = TypeOopPtr::InstanceTop, 146 const int iidx = Compile::AliasIdxTop, 147 const int ioffs = Type::OffsetTop ) 148 : TypeNode(t,r->req()), 149 _adr_type(at), 150 _inst_mem_id(imid), 151 _inst_id(iid), 152 _inst_index(iidx), 153 _inst_offset(ioffs) 154 { 155 init_class_id(Class_Phi); 156 init_req(0, r); 157 verify_adr_type(); 158 } 159 // create a new phi with in edges matching r and set (initially) to x 160 static PhiNode* make( Node* r, Node* x ); 161 // extra type arguments override the new phi's bottom_type and adr_type 162 static PhiNode* make( Node* r, Node* x, const Type *t, const TypePtr* at = NULL ); 163 // create a new phi with narrowed memory type 164 PhiNode* slice_memory(const TypePtr* adr_type) const; 165 PhiNode* split_out_instance(const TypePtr* at, PhaseIterGVN *igvn) const; 166 // like make(r, x), but does not initialize the in edges to x 167 static PhiNode* make_blank( Node* r, Node* x ); 168 169 // Accessors 170 RegionNode* region() const { Node* r = in(Region); assert(!r || r->is_Region(), ""); return (RegionNode*)r; } 171 172 Node* is_copy() const { 173 // The node is a real phi if _in[0] is a Region node. 174 DEBUG_ONLY(const Node* r = _in[Region];) 175 assert(r != NULL && r->is_Region(), "Not valid control"); 176 return NULL; // not a copy! 177 } 178 179 bool is_tripcount() const; 180 181 // Determine a unique non-trivial input, if any. 182 // Ignore casts if it helps. Return NULL on failure. 183 Node* unique_input(PhaseTransform *phase); 184 185 // Check for a simple dead loop. 186 enum LoopSafety { Safe = 0, Unsafe, UnsafeLoop }; 187 LoopSafety simple_data_loop_check(Node *in) const; 188 // Is it unsafe data loop? It becomes a dead loop if this phi node removed. 189 bool is_unsafe_data_reference(Node *in) const; 190 int is_diamond_phi(bool check_control_only = false) const; 191 virtual int Opcode() const; 192 virtual bool pinned() const { return in(0) != 0; } 193 virtual const TypePtr *adr_type() const { verify_adr_type(true); return _adr_type; } 194 195 void set_inst_mem_id(int inst_mem_id) { _inst_mem_id = inst_mem_id; } 196 const int inst_mem_id() const { return _inst_mem_id; } 197 const int inst_id() const { return _inst_id; } 198 const int inst_index() const { return _inst_index; } 199 const int inst_offset() const { return _inst_offset; } 200 bool is_same_inst_field(const Type* tp, int mem_id, int id, int index, int offset) { 201 return type()->basic_type() == tp->basic_type() && 202 inst_mem_id() == mem_id && 203 inst_id() == id && 204 inst_index() == index && 205 inst_offset() == offset && 206 type()->higher_equal(tp); 207 } 208 209 virtual const Type *Value( PhaseTransform *phase ) const; 210 virtual Node *Identity( PhaseTransform *phase ); 211 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape); 212 virtual const RegMask &out_RegMask() const; 213 virtual const RegMask &in_RegMask(uint) const; 214 #ifndef PRODUCT 215 virtual void dump_spec(outputStream *st) const; 216 #endif 217 #ifdef ASSERT 218 void verify_adr_type(VectorSet& visited, const TypePtr* at) const; 219 void verify_adr_type(bool recursive = false) const; 220 #else //ASSERT 221 void verify_adr_type(bool recursive = false) const {} 222 #endif //ASSERT 223 }; 224 225 //------------------------------GotoNode--------------------------------------- 226 // GotoNodes perform direct branches. 227 class GotoNode : public Node { 228 public: 229 GotoNode( Node *control ) : Node(control) {} 230 virtual int Opcode() const; 231 virtual bool pinned() const { return true; } 232 virtual bool is_CFG() const { return true; } 233 virtual uint hash() const { return NO_HASH; } // CFG nodes do not hash 234 virtual const Node *is_block_proj() const { return this; } 235 virtual bool depends_only_on_test() const { return false; } 236 virtual const Type *bottom_type() const { return Type::CONTROL; } 237 virtual const Type *Value( PhaseTransform *phase ) const; 238 virtual Node *Identity( PhaseTransform *phase ); 239 virtual const RegMask &out_RegMask() const; 240 }; 241 242 //------------------------------CProjNode-------------------------------------- 243 // control projection for node that produces multiple control-flow paths 244 class CProjNode : public ProjNode { 245 public: 246 CProjNode( Node *ctrl, uint idx ) : ProjNode(ctrl,idx) {} 247 virtual int Opcode() const; 248 virtual bool is_CFG() const { return true; } 249 virtual uint hash() const { return NO_HASH; } // CFG nodes do not hash 250 virtual const Node *is_block_proj() const { return in(0); } 251 virtual const RegMask &out_RegMask() const; 252 virtual uint ideal_reg() const { return 0; } 253 }; 254 255 //---------------------------MultiBranchNode----------------------------------- 256 // This class defines a MultiBranchNode, a MultiNode which yields multiple 257 // control values. These are distinguished from other types of MultiNodes 258 // which yield multiple values, but control is always and only projection #0. 259 class MultiBranchNode : public MultiNode { 260 public: 261 MultiBranchNode( uint required ) : MultiNode(required) { 262 init_class_id(Class_MultiBranch); 263 } 264 // returns required number of users to be well formed. 265 virtual int required_outcnt() const = 0; 266 }; 267 268 //------------------------------IfNode----------------------------------------- 269 // Output selected Control, based on a boolean test 270 class IfNode : public MultiBranchNode { 271 // Size is bigger to hold the probability field. However, _prob does not 272 // change the semantics so it does not appear in the hash & cmp functions. 273 virtual uint size_of() const { return sizeof(*this); } 274 public: 275 276 // Degrees of branch prediction probability by order of magnitude: 277 // PROB_UNLIKELY_1e(N) is a 1 in 1eN chance. 278 // PROB_LIKELY_1e(N) is a 1 - PROB_UNLIKELY_1e(N) 279 #define PROB_UNLIKELY_MAG(N) (1e- ## N ## f) 280 #define PROB_LIKELY_MAG(N) (1.0f-PROB_UNLIKELY_MAG(N)) 281 282 // Maximum and minimum branch prediction probabilties 283 // 1 in 1,000,000 (magnitude 6) 284 // 285 // Although PROB_NEVER == PROB_MIN and PROB_ALWAYS == PROB_MAX 286 // they are used to distinguish different situations: 287 // 288 // The name PROB_MAX (PROB_MIN) is for probabilities which correspond to 289 // very likely (unlikely) but with a concrete possibility of a rare 290 // contrary case. These constants would be used for pinning 291 // measurements, and as measures for assertions that have high 292 // confidence, but some evidence of occasional failure. 293 // 294 // The name PROB_ALWAYS (PROB_NEVER) is to stand for situations for which 295 // there is no evidence at all that the contrary case has ever occurred. 296 297 #define PROB_NEVER PROB_UNLIKELY_MAG(6) 298 #define PROB_ALWAYS PROB_LIKELY_MAG(6) 299 300 #define PROB_MIN PROB_UNLIKELY_MAG(6) 301 #define PROB_MAX PROB_LIKELY_MAG(6) 302 303 // Static branch prediction probabilities 304 // 1 in 10 (magnitude 1) 305 #define PROB_STATIC_INFREQUENT PROB_UNLIKELY_MAG(1) 306 #define PROB_STATIC_FREQUENT PROB_LIKELY_MAG(1) 307 308 // Fair probability 50/50 309 #define PROB_FAIR (0.5f) 310 311 // Unknown probability sentinel 312 #define PROB_UNKNOWN (-1.0f) 313 314 // Probability "constructors", to distinguish as a probability any manifest 315 // constant without a names 316 #define PROB_LIKELY(x) ((float) (x)) 317 #define PROB_UNLIKELY(x) (1.0f - (float)(x)) 318 319 // Other probabilities in use, but without a unique name, are documented 320 // here for lack of a better place: 321 // 322 // 1 in 1000 probabilities (magnitude 3): 323 // threshold for converting to conditional move 324 // likelihood of null check failure if a null HAS been seen before 325 // likelihood of slow path taken in library calls 326 // 327 // 1 in 10,000 probabilities (magnitude 4): 328 // threshold for making an uncommon trap probability more extreme 329 // threshold for for making a null check implicit 330 // likelihood of needing a gc if eden top moves during an allocation 331 // likelihood of a predicted call failure 332 // 333 // 1 in 100,000 probabilities (magnitude 5): 334 // threshold for ignoring counts when estimating path frequency 335 // likelihood of FP clipping failure 336 // likelihood of catching an exception from a try block 337 // likelihood of null check failure if a null has NOT been seen before 338 // 339 // Magic manifest probabilities such as 0.83, 0.7, ... can be found in 340 // gen_subtype_check() and catch_inline_exceptions(). 341 342 float _prob; // Probability of true path being taken. 343 float _fcnt; // Frequency counter 344 IfNode( Node *control, Node *b, float p, float fcnt ) 345 : MultiBranchNode(2), _prob(p), _fcnt(fcnt) { 346 init_class_id(Class_If); 347 init_req(0,control); 348 init_req(1,b); 349 } 350 virtual int Opcode() const; 351 virtual bool pinned() const { return true; } 352 virtual const Type *bottom_type() const { return TypeTuple::IFBOTH; } 353 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape); 354 virtual const Type *Value( PhaseTransform *phase ) const; 355 virtual int required_outcnt() const { return 2; } 356 virtual const RegMask &out_RegMask() const; 357 void dominated_by(Node* prev_dom, PhaseIterGVN* igvn); 358 int is_range_check(Node* &range, Node* &index, jint &offset); 359 Node* fold_compares(PhaseGVN* phase); 360 static Node* up_one_dom(Node* curr, bool linear_only = false); 361 362 // Takes the type of val and filters it through the test represented 363 // by if_proj and returns a more refined type if one is produced. 364 // Returns NULL is it couldn't improve the type. 365 static const TypeInt* filtered_int_type(PhaseGVN* phase, Node* val, Node* if_proj); 366 367 #ifndef PRODUCT 368 virtual void dump_spec(outputStream *st) const; 369 #endif 370 }; 371 372 class IfTrueNode : public CProjNode { 373 public: 374 IfTrueNode( IfNode *ifnode ) : CProjNode(ifnode,1) { 375 init_class_id(Class_IfTrue); 376 } 377 virtual int Opcode() const; 378 virtual Node *Identity( PhaseTransform *phase ); 379 }; 380 381 class IfFalseNode : public CProjNode { 382 public: 383 IfFalseNode( IfNode *ifnode ) : CProjNode(ifnode,0) { 384 init_class_id(Class_IfFalse); 385 } 386 virtual int Opcode() const; 387 virtual Node *Identity( PhaseTransform *phase ); 388 }; 389 390 391 //------------------------------PCTableNode------------------------------------ 392 // Build an indirect branch table. Given a control and a table index, 393 // control is passed to the Projection matching the table index. Used to 394 // implement switch statements and exception-handling capabilities. 395 // Undefined behavior if passed-in index is not inside the table. 396 class PCTableNode : public MultiBranchNode { 397 virtual uint hash() const; // Target count; table size 398 virtual uint cmp( const Node &n ) const; 399 virtual uint size_of() const { return sizeof(*this); } 400 401 public: 402 const uint _size; // Number of targets 403 404 PCTableNode( Node *ctrl, Node *idx, uint size ) : MultiBranchNode(2), _size(size) { 405 init_class_id(Class_PCTable); 406 init_req(0, ctrl); 407 init_req(1, idx); 408 } 409 virtual int Opcode() const; 410 virtual const Type *Value( PhaseTransform *phase ) const; 411 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape); 412 virtual const Type *bottom_type() const; 413 virtual bool pinned() const { return true; } 414 virtual int required_outcnt() const { return _size; } 415 }; 416 417 //------------------------------JumpNode--------------------------------------- 418 // Indirect branch. Uses PCTable above to implement a switch statement. 419 // It emits as a table load and local branch. 420 class JumpNode : public PCTableNode { 421 public: 422 JumpNode( Node* control, Node* switch_val, uint size) : PCTableNode(control, switch_val, size) { 423 init_class_id(Class_Jump); 424 } 425 virtual int Opcode() const; 426 virtual const RegMask& out_RegMask() const; 427 virtual const Node* is_block_proj() const { return this; } 428 }; 429 430 class JumpProjNode : public JProjNode { 431 virtual uint hash() const; 432 virtual uint cmp( const Node &n ) const; 433 virtual uint size_of() const { return sizeof(*this); } 434 435 private: 436 const int _dest_bci; 437 const uint _proj_no; 438 const int _switch_val; 439 public: 440 JumpProjNode(Node* jumpnode, uint proj_no, int dest_bci, int switch_val) 441 : JProjNode(jumpnode, proj_no), _dest_bci(dest_bci), _proj_no(proj_no), _switch_val(switch_val) { 442 init_class_id(Class_JumpProj); 443 } 444 445 virtual int Opcode() const; 446 virtual const Type* bottom_type() const { return Type::CONTROL; } 447 int dest_bci() const { return _dest_bci; } 448 int switch_val() const { return _switch_val; } 449 uint proj_no() const { return _proj_no; } 450 #ifndef PRODUCT 451 virtual void dump_spec(outputStream *st) const; 452 #endif 453 }; 454 455 //------------------------------CatchNode-------------------------------------- 456 // Helper node to fork exceptions. "Catch" catches any exceptions thrown by 457 // a just-prior call. Looks like a PCTableNode but emits no code - just the 458 // table. The table lookup and branch is implemented by RethrowNode. 459 class CatchNode : public PCTableNode { 460 public: 461 CatchNode( Node *ctrl, Node *idx, uint size ) : PCTableNode(ctrl,idx,size){ 462 init_class_id(Class_Catch); 463 } 464 virtual int Opcode() const; 465 virtual const Type *Value( PhaseTransform *phase ) const; 466 }; 467 468 // CatchProjNode controls which exception handler is targetted after a call. 469 // It is passed in the bci of the target handler, or no_handler_bci in case 470 // the projection doesn't lead to an exception handler. 471 class CatchProjNode : public CProjNode { 472 virtual uint hash() const; 473 virtual uint cmp( const Node &n ) const; 474 virtual uint size_of() const { return sizeof(*this); } 475 476 private: 477 const int _handler_bci; 478 479 public: 480 enum { 481 fall_through_index = 0, // the fall through projection index 482 catch_all_index = 1, // the projection index for catch-alls 483 no_handler_bci = -1 // the bci for fall through or catch-all projs 484 }; 485 486 CatchProjNode(Node* catchnode, uint proj_no, int handler_bci) 487 : CProjNode(catchnode, proj_no), _handler_bci(handler_bci) { 488 init_class_id(Class_CatchProj); 489 assert(proj_no != fall_through_index || handler_bci < 0, "fall through case must have bci < 0"); 490 } 491 492 virtual int Opcode() const; 493 virtual Node *Identity( PhaseTransform *phase ); 494 virtual const Type *bottom_type() const { return Type::CONTROL; } 495 int handler_bci() const { return _handler_bci; } 496 bool is_handler_proj() const { return _handler_bci >= 0; } 497 #ifndef PRODUCT 498 virtual void dump_spec(outputStream *st) const; 499 #endif 500 }; 501 502 503 //---------------------------------CreateExNode-------------------------------- 504 // Helper node to create the exception coming back from a call 505 class CreateExNode : public TypeNode { 506 public: 507 CreateExNode(const Type* t, Node* control, Node* i_o) : TypeNode(t, 2) { 508 init_req(0, control); 509 init_req(1, i_o); 510 } 511 virtual int Opcode() const; 512 virtual Node *Identity( PhaseTransform *phase ); 513 virtual bool pinned() const { return true; } 514 uint match_edge(uint idx) const { return 0; } 515 virtual uint ideal_reg() const { return Op_RegP; } 516 }; 517 518 //------------------------------NeverBranchNode------------------------------- 519 // The never-taken branch. Used to give the appearance of exiting infinite 520 // loops to those algorithms that like all paths to be reachable. Encodes 521 // empty. 522 class NeverBranchNode : public MultiBranchNode { 523 public: 524 NeverBranchNode( Node *ctrl ) : MultiBranchNode(1) { init_req(0,ctrl); } 525 virtual int Opcode() const; 526 virtual bool pinned() const { return true; }; 527 virtual const Type *bottom_type() const { return TypeTuple::IFBOTH; } 528 virtual const Type *Value( PhaseTransform *phase ) const; 529 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape); 530 virtual int required_outcnt() const { return 2; } 531 virtual void emit(CodeBuffer &cbuf, PhaseRegAlloc *ra_) const { } 532 virtual uint size(PhaseRegAlloc *ra_) const { return 0; } 533 #ifndef PRODUCT 534 virtual void format( PhaseRegAlloc *, outputStream *st ) const; 535 #endif 536 }; 537 538 #endif // SHARE_VM_OPTO_CFGNODE_HPP