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