1 /*
  2  * Copyright (c) 1997, 2025, 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_OPTO_CFGNODE_HPP
 26 #define SHARE_OPTO_CFGNODE_HPP
 27 
 28 #include "opto/multnode.hpp"
 29 #include "opto/node.hpp"
 30 #include "opto/opcodes.hpp"
 31 #include "opto/predicates_enums.hpp"
 32 #include "opto/type.hpp"
 33 
 34 // Portions of code courtesy of Clifford Click
 35 
 36 // Optimization - Graph Style
 37 
 38 class Matcher;
 39 class Node;
 40 class   RegionNode;
 41 class   TypeNode;
 42 class     PhiNode;
 43 class   GotoNode;
 44 class   MultiNode;
 45 class     MultiBranchNode;
 46 class       IfNode;
 47 class       PCTableNode;
 48 class         JumpNode;
 49 class         CatchNode;
 50 class       NeverBranchNode;
 51 class     BlackholeNode;
 52 class   ProjNode;
 53 class     CProjNode;
 54 class       IfTrueNode;
 55 class       IfFalseNode;
 56 class       CatchProjNode;
 57 class     JProjNode;
 58 class       JumpProjNode;
 59 class     SCMemProjNode;
 60 class PhaseIdealLoop;
 61 enum class AssertionPredicateType;
 62 enum class PredicateState;
 63 
 64 //------------------------------RegionNode-------------------------------------
 65 // The class of RegionNodes, which can be mapped to basic blocks in the
 66 // program.  Their inputs point to Control sources.  PhiNodes (described
 67 // below) have an input point to a RegionNode.  Merged data inputs to PhiNodes
 68 // correspond 1-to-1 with RegionNode inputs.  The zero input of a PhiNode is
 69 // the RegionNode, and the zero input of the RegionNode is itself.
 70 class RegionNode : public Node {
 71 public:
 72   enum LoopStatus {
 73     // No guarantee: the region may be an irreducible loop entry, thus we have to
 74     // be careful when removing entry control to it.
 75     MaybeIrreducibleEntry,
 76     // Limited guarantee: this region may be (nested) inside an irreducible loop,
 77     // but it will never be an irreducible loop entry.
 78     NeverIrreducibleEntry,
 79     // Strong guarantee: this region is not (nested) inside an irreducible loop.
 80     Reducible,
 81   };
 82 
 83 private:
 84   bool _is_unreachable_region;
 85   LoopStatus _loop_status;
 86 
 87   bool is_possible_unsafe_loop(const PhaseGVN* phase) const;
 88   bool is_unreachable_from_root(const PhaseGVN* phase) const;
 89 public:
 90   // Node layout (parallels PhiNode):
 91   enum { Region,                // Generally points to self.
 92          Control                // Control arcs are [1..len)
 93   };
 94 
 95   RegionNode(uint required)
 96     : Node(required),
 97       _is_unreachable_region(false),
 98       _loop_status(LoopStatus::NeverIrreducibleEntry)
 99   {
100     init_class_id(Class_Region);
101     init_req(0, this);
102   }
103 
104   Node* is_copy() const {
105     const Node* r = _in[Region];
106     if (r == nullptr)
107       return nonnull_req();
108     return nullptr;  // not a copy!
109   }
110   PhiNode* has_phi() const;        // returns an arbitrary phi user, or null
111   PhiNode* has_unique_phi() const; // returns the unique phi user, or null
112   // Is this region node unreachable from root?
113   bool is_unreachable_region(const PhaseGVN* phase);
114 #ifdef ASSERT
115   bool is_in_infinite_subgraph();
116   static bool are_all_nodes_in_infinite_subgraph(Unique_Node_List& worklist);
117 #endif //ASSERT
118   LoopStatus loop_status() const { return _loop_status; };
119   void set_loop_status(LoopStatus status);
120   bool can_be_irreducible_entry() const;
121 
122   virtual int Opcode() const;
123   virtual uint size_of() const { return sizeof(*this); }
124   virtual bool pinned() const { return (const Node*)in(0) == this; }
125   virtual bool is_CFG() const { return true; }
126   virtual uint hash() const { return NO_HASH; } // CFG nodes do not hash
127   virtual bool depends_only_on_test() const { return false; }
128   virtual const Type* bottom_type() const { return Type::CONTROL; }
129   virtual const Type* Value(PhaseGVN* phase) const;
130   virtual Node* Identity(PhaseGVN* phase);
131   virtual Node* Ideal(PhaseGVN* phase, bool can_reshape);
132   void remove_unreachable_subgraph(PhaseIterGVN* igvn);
133   virtual const RegMask &out_RegMask() const;
134   bool is_diamond() const;
135   void try_clean_mem_phis(PhaseIterGVN* phase);
136   bool optimize_trichotomy(PhaseIterGVN* igvn);
137   NOT_PRODUCT(virtual void dump_spec(outputStream* st) const;)
138 };
139 
140 //------------------------------JProjNode--------------------------------------
141 // jump projection for node that produces multiple control-flow paths
142 class JProjNode : public ProjNode {
143  public:
144   JProjNode( Node* ctrl, uint idx ) : ProjNode(ctrl,idx) {}
145   virtual int Opcode() const;
146   virtual bool  is_CFG() const { return true; }
147   virtual uint  hash() const { return NO_HASH; }  // CFG nodes do not hash
148   virtual const Node* is_block_proj() const { return in(0); }
149   virtual const RegMask& out_RegMask() const;
150   virtual uint  ideal_reg() const { return 0; }
151 };
152 
153 //------------------------------PhiNode----------------------------------------
154 // PhiNodes merge values from different Control paths.  Slot 0 points to the
155 // controlling RegionNode.  Other slots map 1-for-1 with incoming control flow
156 // paths to the RegionNode.
157 class PhiNode : public TypeNode {
158   friend class PhaseRenumberLive;
159 
160   const TypePtr* const _adr_type; // non-null only for Type::MEMORY nodes.
161   // The following fields are only used for data PhiNodes to indicate
162   // that the PhiNode represents the value of a known instance field.
163         int _inst_mem_id; // Instance memory id (node index of the memory Phi)
164         int _inst_id;     // Instance id of the memory slice.
165   const int _inst_index;  // Alias index of the instance memory slice.
166   // Array elements references have the same alias_idx but different offset.
167   const int _inst_offset; // Offset of the instance memory slice.
168   // Size is bigger to hold the _adr_type field.
169   virtual uint hash() const;    // Check the type
170   virtual bool cmp( const Node &n ) const;
171   virtual uint size_of() const { return sizeof(*this); }
172 
173   // Determine if CMoveNode::is_cmove_id can be used at this join point.
174   Node* is_cmove_id(PhaseTransform* phase, int true_path);
175   bool wait_for_region_igvn(PhaseGVN* phase);
176   bool is_data_loop(RegionNode* r, Node* uin, const PhaseGVN* phase);
177 
178   static Node* clone_through_phi(Node* root_phi, const Type* t, uint c, PhaseIterGVN* igvn);
179   static Node* merge_through_phi(Node* root_phi, PhaseIterGVN* igvn);
180 
181   bool must_wait_for_region_in_irreducible_loop(PhaseGVN* phase) const;
182 
183   bool is_split_through_mergemem_terminating() const;
184 
185 public:
186   // Node layout (parallels RegionNode):
187   enum { Region,                // Control input is the Phi's region.
188          Input                  // Input values are [1..len)
189   };
190 
191   PhiNode( Node *r, const Type *t, const TypePtr* at = nullptr,
192            const int imid = -1,
193            const int iid = TypeOopPtr::InstanceTop,
194            const int iidx = Compile::AliasIdxTop,
195            const int ioffs = Type::OffsetTop )
196     : TypeNode(t,r->req()),
197       _adr_type(at),
198       _inst_mem_id(imid),
199       _inst_id(iid),
200       _inst_index(iidx),
201       _inst_offset(ioffs)
202   {
203     init_class_id(Class_Phi);
204     init_req(0, r);
205     verify_adr_type();
206   }
207   // create a new phi with in edges matching r and set (initially) to x
208   static PhiNode* make( Node* r, Node* x );
209   // extra type arguments override the new phi's bottom_type and adr_type
210   static PhiNode* make( Node* r, Node* x, const Type *t, const TypePtr* at = nullptr );
211   // create a new phi with narrowed memory type
212   PhiNode* slice_memory(const TypePtr* adr_type) const;
213   PhiNode* split_out_instance(const TypePtr* at, PhaseIterGVN *igvn) const;
214   // like make(r, x), but does not initialize the in edges to x
215   static PhiNode* make_blank( Node* r, Node* x );
216 
217   // Accessors
218   RegionNode* region() const { Node* r = in(Region); assert(!r || r->is_Region(), ""); return (RegionNode*)r; }
219 
220   bool is_tripcount(BasicType bt) const;
221 
222   // Determine a unique non-trivial input, if any.
223   // Ignore casts if it helps.  Return null on failure.
224   Node* unique_input(PhaseValues* phase, bool uncast);
225   Node* unique_input(PhaseValues* phase) {
226     Node* uin = unique_input(phase, false);
227     if (uin == nullptr) {
228       uin = unique_input(phase, true);
229     }
230     return uin;
231   }
232 
233   // Check for a simple dead loop.
234   enum LoopSafety { Safe = 0, Unsafe, UnsafeLoop };
235   LoopSafety simple_data_loop_check(Node *in) const;
236   // Is it unsafe data loop? It becomes a dead loop if this phi node removed.
237   bool is_unsafe_data_reference(Node *in) const;
238   int is_diamond_phi() const;
239   bool try_clean_memory_phi(PhaseIterGVN* igvn);
240   virtual int Opcode() const;
241   virtual bool pinned() const { return in(0) != nullptr; }
242   virtual const TypePtr *adr_type() const { verify_adr_type(true); return _adr_type; }
243 
244   void  set_inst_mem_id(int inst_mem_id) { _inst_mem_id = inst_mem_id; }
245   int inst_mem_id() const { return _inst_mem_id; }
246   int inst_id()     const { return _inst_id; }
247   int inst_index()  const { return _inst_index; }
248   int inst_offset() const { return _inst_offset; }
249   bool is_same_inst_field(const Type* tp, int mem_id, int id, int index, int offset) {
250     return type()->basic_type() == tp->basic_type() &&
251            inst_mem_id() == mem_id &&
252            inst_id()     == id     &&
253            inst_index()  == index  &&
254            inst_offset() == offset &&
255            type()->higher_equal(tp);
256   }
257 
258   virtual const Type* Value(PhaseGVN* phase) const;
259   virtual Node* Identity(PhaseGVN* phase);
260   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
261   virtual const RegMask &out_RegMask() const;
262   virtual const RegMask &in_RegMask(uint) const;
263 #ifndef PRODUCT
264   virtual void dump_spec(outputStream *st) const;
265 #endif
266 #ifdef ASSERT
267   void verify_adr_type(VectorSet& visited, const TypePtr* at) const;
268   void verify_adr_type(bool recursive = false) const;
269 #else //ASSERT
270   void verify_adr_type(bool recursive = false) const {}
271 #endif //ASSERT
272 
273   const TypeTuple* collect_types(PhaseGVN* phase) const;
274 };
275 
276 //------------------------------GotoNode---------------------------------------
277 // GotoNodes perform direct branches.
278 class GotoNode : public Node {
279 public:
280   GotoNode( Node *control ) : Node(control) {}
281   virtual int Opcode() const;
282   virtual bool pinned() const { return true; }
283   virtual bool  is_CFG() const { return true; }
284   virtual uint hash() const { return NO_HASH; }  // CFG nodes do not hash
285   virtual const Node *is_block_proj() const { return this; }
286   virtual bool depends_only_on_test() const { return false; }
287   virtual const Type *bottom_type() const { return Type::CONTROL; }
288   virtual const Type* Value(PhaseGVN* phase) const;
289   virtual Node* Identity(PhaseGVN* phase);
290   virtual const RegMask &out_RegMask() const;
291 };
292 
293 //------------------------------CProjNode--------------------------------------
294 // control projection for node that produces multiple control-flow paths
295 class CProjNode : public ProjNode {
296 public:
297   CProjNode( Node *ctrl, uint idx ) : ProjNode(ctrl,idx) {}
298   virtual int Opcode() const;
299   virtual bool  is_CFG() const { return true; }
300   virtual uint hash() const { return NO_HASH; }  // CFG nodes do not hash
301   virtual const Node *is_block_proj() const { return in(0); }
302   virtual const RegMask &out_RegMask() const;
303   virtual uint ideal_reg() const { return 0; }
304 };
305 
306 //---------------------------MultiBranchNode-----------------------------------
307 // This class defines a MultiBranchNode, a MultiNode which yields multiple
308 // control values. These are distinguished from other types of MultiNodes
309 // which yield multiple values, but control is always and only projection #0.
310 class MultiBranchNode : public MultiNode {
311 public:
312   MultiBranchNode( uint required ) : MultiNode(required) {
313     init_class_id(Class_MultiBranch);
314   }
315   // returns required number of users to be well formed.
316   virtual int required_outcnt() const = 0;
317 };
318 
319 //------------------------------IfNode-----------------------------------------
320 // Output selected Control, based on a boolean test
321 class IfNode : public MultiBranchNode {
322  public:
323   float _prob;                           // Probability of true path being taken.
324   float _fcnt;                           // Frequency counter
325 
326  private:
327   AssertionPredicateType _assertion_predicate_type;
328 
329   void init_node(Node* control, Node* bol) {
330     init_class_id(Class_If);
331     init_req(0, control);
332     init_req(1, bol);
333   }
334 
335   // Size is bigger to hold the probability field.  However, _prob does not
336   // change the semantics so it does not appear in the hash & cmp functions.
337   virtual uint size_of() const { return sizeof(*this); }
338 
339   // Helper methods for fold_compares
340   bool cmpi_folds(PhaseIterGVN* igvn, bool fold_ne = false);
341   bool is_ctrl_folds(Node* ctrl, PhaseIterGVN* igvn);
342   bool has_shared_region(ProjNode* proj, ProjNode*& success, ProjNode*& fail);
343   bool has_only_uncommon_traps(ProjNode* proj, ProjNode*& success, ProjNode*& fail, PhaseIterGVN* igvn);
344   Node* merge_uncommon_traps(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn);
345   static void improve_address_types(Node* l, Node* r, ProjNode* fail, PhaseIterGVN* igvn);
346   bool is_cmp_with_loadrange(ProjNode* proj);
347   bool is_null_check(ProjNode* proj, PhaseIterGVN* igvn);
348   bool is_side_effect_free_test(ProjNode* proj, PhaseIterGVN* igvn);
349   void reroute_side_effect_free_unc(ProjNode* proj, ProjNode* dom_proj, PhaseIterGVN* igvn);
350   bool fold_compares_helper(ProjNode* proj, ProjNode* success, ProjNode* fail, PhaseIterGVN* igvn);
351   static bool is_dominator_unc(CallStaticJavaNode* dom_unc, CallStaticJavaNode* unc);
352 
353 protected:
354   ProjNode* range_check_trap_proj(int& flip, Node*& l, Node*& r);
355   Node* Ideal_common(PhaseGVN *phase, bool can_reshape);
356   Node* search_identical(int dist, PhaseIterGVN* igvn);
357 
358   Node* simple_subsuming(PhaseIterGVN* igvn);
359 
360 public:
361 
362   // Degrees of branch prediction probability by order of magnitude:
363   // PROB_UNLIKELY_1e(N) is a 1 in 1eN chance.
364   // PROB_LIKELY_1e(N) is a 1 - PROB_UNLIKELY_1e(N)
365 #define PROB_UNLIKELY_MAG(N)    (1e- ## N ## f)
366 #define PROB_LIKELY_MAG(N)      (1.0f-PROB_UNLIKELY_MAG(N))
367 
368   // Maximum and minimum branch prediction probabilties
369   // 1 in 1,000,000 (magnitude 6)
370   //
371   // Although PROB_NEVER == PROB_MIN and PROB_ALWAYS == PROB_MAX
372   // they are used to distinguish different situations:
373   //
374   // The name PROB_MAX (PROB_MIN) is for probabilities which correspond to
375   // very likely (unlikely) but with a concrete possibility of a rare
376   // contrary case.  These constants would be used for pinning
377   // measurements, and as measures for assertions that have high
378   // confidence, but some evidence of occasional failure.
379   //
380   // The name PROB_ALWAYS (PROB_NEVER) is to stand for situations for which
381   // there is no evidence at all that the contrary case has ever occurred.
382 
383 #define PROB_NEVER              PROB_UNLIKELY_MAG(6)
384 #define PROB_ALWAYS             PROB_LIKELY_MAG(6)
385 
386 #define PROB_MIN                PROB_UNLIKELY_MAG(6)
387 #define PROB_MAX                PROB_LIKELY_MAG(6)
388 
389   // Static branch prediction probabilities
390   // 1 in 10 (magnitude 1)
391 #define PROB_STATIC_INFREQUENT  PROB_UNLIKELY_MAG(1)
392 #define PROB_STATIC_FREQUENT    PROB_LIKELY_MAG(1)
393 
394   // Fair probability 50/50
395 #define PROB_FAIR               (0.5f)
396 
397   // Unknown probability sentinel
398 #define PROB_UNKNOWN            (-1.0f)
399 
400   // Probability "constructors", to distinguish as a probability any manifest
401   // constant without a names
402 #define PROB_LIKELY(x)          ((float) (x))
403 #define PROB_UNLIKELY(x)        (1.0f - (float)(x))
404 
405   // Other probabilities in use, but without a unique name, are documented
406   // here for lack of a better place:
407   //
408   // 1 in 1000 probabilities (magnitude 3):
409   //     threshold for converting to conditional move
410   //     likelihood of null check failure if a null HAS been seen before
411   //     likelihood of slow path taken in library calls
412   //
413   // 1 in 10,000 probabilities (magnitude 4):
414   //     threshold for making an uncommon trap probability more extreme
415   //     threshold for for making a null check implicit
416   //     likelihood of needing a gc if eden top moves during an allocation
417   //     likelihood of a predicted call failure
418   //
419   // 1 in 100,000 probabilities (magnitude 5):
420   //     threshold for ignoring counts when estimating path frequency
421   //     likelihood of FP clipping failure
422   //     likelihood of catching an exception from a try block
423   //     likelihood of null check failure if a null has NOT been seen before
424   //
425   // Magic manifest probabilities such as 0.83, 0.7, ... can be found in
426   // gen_subtype_check() and catch_inline_exceptions().
427 
428   IfNode(Node* control, Node* bol, float p, float fcnt);
429   IfNode(Node* control, Node* bol, float p, float fcnt, AssertionPredicateType assertion_predicate_type);
430 
431   static IfNode* make_with_same_profile(IfNode* if_node_profile, Node* ctrl, Node* bol);
432 
433   virtual int Opcode() const;
434   virtual bool pinned() const { return true; }
435   virtual const Type *bottom_type() const { return TypeTuple::IFBOTH; }
436   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
437   virtual const Type* Value(PhaseGVN* phase) const;
438   virtual int required_outcnt() const { return 2; }
439   virtual const RegMask &out_RegMask() const;
440   Node* fold_compares(PhaseIterGVN* phase);
441   static Node* up_one_dom(Node* curr, bool linear_only = false);
442   bool is_zero_trip_guard() const;
443   Node* dominated_by(Node* prev_dom, PhaseIterGVN* igvn, bool pin_array_access_nodes);
444   ProjNode* uncommon_trap_proj(CallStaticJavaNode*& call, Deoptimization::DeoptReason reason = Deoptimization::Reason_none) const;
445 
446   // Takes the type of val and filters it through the test represented
447   // by if_proj and returns a more refined type if one is produced.
448   // Returns null is it couldn't improve the type.
449   static const TypeInt* filtered_int_type(PhaseGVN* phase, Node* val, Node* if_proj);
450 
451   AssertionPredicateType assertion_predicate_type() const {
452     return _assertion_predicate_type;
453   }
454 
455 #ifndef PRODUCT
456   virtual void dump_spec(outputStream *st) const;
457 #endif
458 
459   bool same_condition(const Node* dom, PhaseIterGVN* igvn) const;
460 };
461 
462 class RangeCheckNode : public IfNode {
463 private:
464   int is_range_check(Node*& range, Node*& index, jint& offset);
465 
466 public:
467   RangeCheckNode(Node* control, Node* bol, float p, float fcnt) : IfNode(control, bol, p, fcnt) {
468     init_class_id(Class_RangeCheck);
469   }
470 
471   RangeCheckNode(Node* control, Node* bol, float p, float fcnt, AssertionPredicateType assertion_predicate_type)
472       : IfNode(control, bol, p, fcnt, assertion_predicate_type) {
473     init_class_id(Class_RangeCheck);
474   }
475 
476   virtual int Opcode() const;
477   virtual Node* Ideal(PhaseGVN *phase, bool can_reshape);
478 };
479 
480 // Special node that denotes a Parse Predicate added during parsing. A Parse Predicate serves as placeholder to later
481 // create Regular Predicates (Runtime Predicates with possible Assertion Predicates) above it. Together they form a
482 // Predicate Block. The Parse Predicate and Regular Predicates share the same uncommon trap.
483 // There are three kinds of Parse Predicates:
484 // Loop Parse Predicate, Profiled Loop Parse Predicate (both used by Loop Predication), and Loop Limit Check Parse
485 // Predicate (used for integer overflow checks when creating a counted loop).
486 // More information about predicates can be found in loopPredicate.cpp.
487 class ParsePredicateNode : public IfNode {
488   Deoptimization::DeoptReason _deopt_reason;
489 
490   // When a Parse Predicate loses its connection to a loop head, it will be marked useless by
491   // EliminateUselessPredicates and cleaned up by Value(). It can also become useless when cloning it to both loops
492   // during Loop Multiversioning - we no longer use the old version.
493   PredicateState _predicate_state;
494  public:
495   ParsePredicateNode(Node* control, Deoptimization::DeoptReason deopt_reason, PhaseGVN* gvn);
496   virtual int Opcode() const;
497   virtual uint size_of() const { return sizeof(*this); }
498 
499   Deoptimization::DeoptReason deopt_reason() const {
500     return _deopt_reason;
501   }
502 
503   bool is_useless() const {
504     return _predicate_state == PredicateState::Useless;
505   }
506 
507   void mark_useless(PhaseIterGVN& igvn);
508 
509   void mark_maybe_useful() {
510     _predicate_state = PredicateState::MaybeUseful;
511   }
512 
513   bool is_useful() const {
514     return _predicate_state == PredicateState::Useful;
515   }
516 
517   void mark_useful() {
518     _predicate_state = PredicateState::Useful;
519   }
520 
521   // Return the uncommon trap If projection of this Parse Predicate.
522   ParsePredicateUncommonProj* uncommon_proj() const {
523     return proj_out(0)->as_IfFalse();
524   }
525 
526   Node* uncommon_trap() const;
527 
528   Node* Ideal(PhaseGVN* phase, bool can_reshape) {
529     return nullptr; // Don't optimize
530   }
531 
532   const Type* Value(PhaseGVN* phase) const;
533   NOT_PRODUCT(void dump_spec(outputStream* st) const;)
534 };
535 
536 class IfProjNode : public CProjNode {
537 public:
538   IfProjNode(IfNode *ifnode, uint idx) : CProjNode(ifnode,idx) {}
539   virtual Node* Identity(PhaseGVN* phase);
540 
541   void pin_array_access_nodes(PhaseIterGVN* igvn);
542 
543 protected:
544   // Type of If input when this branch is always taken
545   virtual bool always_taken(const TypeTuple* t) const = 0;
546 };
547 
548 class IfTrueNode : public IfProjNode {
549 public:
550   IfTrueNode( IfNode *ifnode ) : IfProjNode(ifnode,1) {
551     init_class_id(Class_IfTrue);
552   }
553   virtual int Opcode() const;
554 
555 protected:
556   virtual bool always_taken(const TypeTuple* t) const { return t == TypeTuple::IFTRUE; }
557 };
558 
559 class IfFalseNode : public IfProjNode {
560 public:
561   IfFalseNode( IfNode *ifnode ) : IfProjNode(ifnode,0) {
562     init_class_id(Class_IfFalse);
563   }
564   virtual int Opcode() const;
565 
566 protected:
567   virtual bool always_taken(const TypeTuple* t) const { return t == TypeTuple::IFFALSE; }
568 };
569 
570 
571 //------------------------------PCTableNode------------------------------------
572 // Build an indirect branch table.  Given a control and a table index,
573 // control is passed to the Projection matching the table index.  Used to
574 // implement switch statements and exception-handling capabilities.
575 // Undefined behavior if passed-in index is not inside the table.
576 class PCTableNode : public MultiBranchNode {
577   virtual uint hash() const;    // Target count; table size
578   virtual bool cmp( const Node &n ) const;
579   virtual uint size_of() const { return sizeof(*this); }
580 
581 public:
582   const uint _size;             // Number of targets
583 
584   PCTableNode( Node *ctrl, Node *idx, uint size ) : MultiBranchNode(2), _size(size) {
585     init_class_id(Class_PCTable);
586     init_req(0, ctrl);
587     init_req(1, idx);
588   }
589   virtual int Opcode() const;
590   virtual const Type* Value(PhaseGVN* phase) const;
591   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
592   virtual const Type *bottom_type() const;
593   virtual bool pinned() const { return true; }
594   virtual int required_outcnt() const { return _size; }
595 };
596 
597 //------------------------------JumpNode---------------------------------------
598 // Indirect branch.  Uses PCTable above to implement a switch statement.
599 // It emits as a table load and local branch.
600 class JumpNode : public PCTableNode {
601   virtual uint size_of() const { return sizeof(*this); }
602 public:
603   float* _probs; // probability of each projection
604   float _fcnt;   // total number of times this Jump was executed
605   JumpNode( Node* control, Node* switch_val, uint size, float* probs, float cnt)
606     : PCTableNode(control, switch_val, size),
607       _probs(probs), _fcnt(cnt) {
608     init_class_id(Class_Jump);
609   }
610   virtual int   Opcode() const;
611   virtual const RegMask& out_RegMask() const;
612   virtual const Node* is_block_proj() const { return this; }
613 };
614 
615 class JumpProjNode : public JProjNode {
616   virtual uint hash() const;
617   virtual bool cmp( const Node &n ) const;
618   virtual uint size_of() const { return sizeof(*this); }
619 
620  private:
621   const int  _dest_bci;
622   const uint _proj_no;
623   const int  _switch_val;
624  public:
625   JumpProjNode(Node* jumpnode, uint proj_no, int dest_bci, int switch_val)
626     : JProjNode(jumpnode, proj_no), _dest_bci(dest_bci), _proj_no(proj_no), _switch_val(switch_val) {
627     init_class_id(Class_JumpProj);
628   }
629 
630   virtual int Opcode() const;
631   virtual const Type* bottom_type() const { return Type::CONTROL; }
632   int  dest_bci()    const { return _dest_bci; }
633   int  switch_val()  const { return _switch_val; }
634   uint proj_no()     const { return _proj_no; }
635 #ifndef PRODUCT
636   virtual void dump_spec(outputStream *st) const;
637   virtual void dump_compact_spec(outputStream *st) const;
638 #endif
639 };
640 
641 //------------------------------CatchNode--------------------------------------
642 // Helper node to fork exceptions.  "Catch" catches any exceptions thrown by
643 // a just-prior call.  Looks like a PCTableNode but emits no code - just the
644 // table.  The table lookup and branch is implemented by RethrowNode.
645 class CatchNode : public PCTableNode {
646 public:
647   CatchNode( Node *ctrl, Node *idx, uint size ) : PCTableNode(ctrl,idx,size){
648     init_class_id(Class_Catch);
649   }
650   virtual int Opcode() const;
651   virtual const Type* Value(PhaseGVN* phase) const;
652 };
653 
654 // CatchProjNode controls which exception handler is targeted after a call.
655 // It is passed in the bci of the target handler, or no_handler_bci in case
656 // the projection doesn't lead to an exception handler.
657 class CatchProjNode : public CProjNode {
658   virtual uint hash() const;
659   virtual bool cmp( const Node &n ) const;
660   virtual uint size_of() const { return sizeof(*this); }
661 
662 private:
663   const int _handler_bci;
664 
665 public:
666   enum {
667     fall_through_index =  0,      // the fall through projection index
668     catch_all_index    =  1,      // the projection index for catch-alls
669     no_handler_bci     = -1       // the bci for fall through or catch-all projs
670   };
671 
672   CatchProjNode(Node* catchnode, uint proj_no, int handler_bci)
673     : CProjNode(catchnode, proj_no), _handler_bci(handler_bci) {
674     init_class_id(Class_CatchProj);
675     assert(proj_no != fall_through_index || handler_bci < 0, "fall through case must have bci < 0");
676   }
677 
678   virtual int Opcode() const;
679   virtual Node* Identity(PhaseGVN* phase);
680   virtual const Type *bottom_type() const { return Type::CONTROL; }
681   int  handler_bci() const        { return _handler_bci; }
682   bool is_handler_proj() const    { return _handler_bci >= 0; }
683 #ifndef PRODUCT
684   virtual void dump_spec(outputStream *st) const;
685 #endif
686 };
687 
688 
689 //---------------------------------CreateExNode--------------------------------
690 // Helper node to create the exception coming back from a call
691 class CreateExNode : public TypeNode {
692 public:
693   CreateExNode(const Type* t, Node* control, Node* i_o) : TypeNode(t, 2) {
694     init_req(0, control);
695     init_req(1, i_o);
696   }
697   virtual int Opcode() const;
698   virtual Node* Identity(PhaseGVN* phase);
699   virtual bool pinned() const { return true; }
700   uint match_edge(uint idx) const { return 0; }
701   virtual uint ideal_reg() const { return Op_RegP; }
702 };
703 
704 //------------------------------NeverBranchNode-------------------------------
705 // The never-taken branch.  Used to give the appearance of exiting infinite
706 // loops to those algorithms that like all paths to be reachable.  Encodes
707 // empty.
708 class NeverBranchNode : public MultiBranchNode {
709 public:
710   NeverBranchNode(Node* ctrl) : MultiBranchNode(1) {
711     init_req(0, ctrl);
712     init_class_id(Class_NeverBranch);
713   }
714   virtual int Opcode() const;
715   virtual bool pinned() const { return true; };
716   virtual const Type *bottom_type() const { return TypeTuple::IFBOTH; }
717   virtual const Type* Value(PhaseGVN* phase) const;
718   virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
719   virtual int required_outcnt() const { return 2; }
720   virtual void emit(C2_MacroAssembler *masm, PhaseRegAlloc *ra_) const { }
721   virtual uint size(PhaseRegAlloc *ra_) const { return 0; }
722 #ifndef PRODUCT
723   virtual void format( PhaseRegAlloc *, outputStream *st ) const;
724 #endif
725 };
726 
727 //------------------------------BlackholeNode----------------------------
728 // Blackhole all arguments. This node would survive through the compiler
729 // the effects on its arguments, and would be finally matched to nothing.
730 class BlackholeNode : public MultiNode {
731 public:
732   BlackholeNode(Node* ctrl) : MultiNode(1) {
733     init_req(TypeFunc::Control, ctrl);
734   }
735   virtual int   Opcode() const;
736   virtual uint ideal_reg() const { return 0; } // not matched in the AD file
737   virtual const Type* bottom_type() const { return TypeTuple::MEMBAR; }
738 
739   const RegMask &in_RegMask(uint idx) const {
740     // Fake the incoming arguments mask for blackholes: accept all registers
741     // and all stack slots. This would avoid any redundant register moves
742     // for blackhole inputs.
743     return RegMask::All;
744   }
745 #ifndef PRODUCT
746   virtual void format(PhaseRegAlloc* ra, outputStream* st) const;
747 #endif
748 };
749 
750 
751 #endif // SHARE_OPTO_CFGNODE_HPP