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