src/share/vm/opto/loopnode.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_OPTO_LOOPNODE_HPP
aoqi@0 26 #define SHARE_VM_OPTO_LOOPNODE_HPP
aoqi@0 27
aoqi@0 28 #include "opto/cfgnode.hpp"
aoqi@0 29 #include "opto/multnode.hpp"
aoqi@0 30 #include "opto/phaseX.hpp"
aoqi@0 31 #include "opto/subnode.hpp"
aoqi@0 32 #include "opto/type.hpp"
aoqi@0 33
aoqi@0 34 class CmpNode;
aoqi@0 35 class CountedLoopEndNode;
aoqi@0 36 class CountedLoopNode;
aoqi@0 37 class IdealLoopTree;
aoqi@0 38 class LoopNode;
aoqi@0 39 class Node;
aoqi@0 40 class PhaseIdealLoop;
aoqi@0 41 class VectorSet;
aoqi@0 42 class Invariance;
aoqi@0 43 struct small_cache;
aoqi@0 44
aoqi@0 45 //
aoqi@0 46 // I D E A L I Z E D L O O P S
aoqi@0 47 //
aoqi@0 48 // Idealized loops are the set of loops I perform more interesting
aoqi@0 49 // transformations on, beyond simple hoisting.
aoqi@0 50
aoqi@0 51 //------------------------------LoopNode---------------------------------------
aoqi@0 52 // Simple loop header. Fall in path on left, loop-back path on right.
aoqi@0 53 class LoopNode : public RegionNode {
aoqi@0 54 // Size is bigger to hold the flags. However, the flags do not change
aoqi@0 55 // the semantics so it does not appear in the hash & cmp functions.
aoqi@0 56 virtual uint size_of() const { return sizeof(*this); }
aoqi@0 57 protected:
aoqi@0 58 short _loop_flags;
aoqi@0 59 // Names for flag bitfields
aoqi@0 60 enum { Normal=0, Pre=1, Main=2, Post=3, PreMainPostFlagsMask=3,
aoqi@0 61 MainHasNoPreLoop=4,
aoqi@0 62 HasExactTripCount=8,
aoqi@0 63 InnerLoop=16,
aoqi@0 64 PartialPeelLoop=32,
aoqi@0 65 PartialPeelFailed=64 };
aoqi@0 66 char _unswitch_count;
aoqi@0 67 enum { _unswitch_max=3 };
aoqi@0 68
aoqi@0 69 public:
aoqi@0 70 // Names for edge indices
aoqi@0 71 enum { Self=0, EntryControl, LoopBackControl };
aoqi@0 72
aoqi@0 73 int is_inner_loop() const { return _loop_flags & InnerLoop; }
aoqi@0 74 void set_inner_loop() { _loop_flags |= InnerLoop; }
aoqi@0 75
aoqi@0 76 int is_partial_peel_loop() const { return _loop_flags & PartialPeelLoop; }
aoqi@0 77 void set_partial_peel_loop() { _loop_flags |= PartialPeelLoop; }
aoqi@0 78 int partial_peel_has_failed() const { return _loop_flags & PartialPeelFailed; }
aoqi@0 79 void mark_partial_peel_failed() { _loop_flags |= PartialPeelFailed; }
aoqi@0 80
aoqi@0 81 int unswitch_max() { return _unswitch_max; }
aoqi@0 82 int unswitch_count() { return _unswitch_count; }
aoqi@0 83 void set_unswitch_count(int val) {
aoqi@0 84 assert (val <= unswitch_max(), "too many unswitches");
aoqi@0 85 _unswitch_count = val;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 LoopNode( Node *entry, Node *backedge ) : RegionNode(3), _loop_flags(0), _unswitch_count(0) {
aoqi@0 89 init_class_id(Class_Loop);
aoqi@0 90 init_req(EntryControl, entry);
aoqi@0 91 init_req(LoopBackControl, backedge);
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
aoqi@0 95 virtual int Opcode() const;
aoqi@0 96 bool can_be_counted_loop(PhaseTransform* phase) const {
aoqi@0 97 return req() == 3 && in(0) != NULL &&
aoqi@0 98 in(1) != NULL && phase->type(in(1)) != Type::TOP &&
aoqi@0 99 in(2) != NULL && phase->type(in(2)) != Type::TOP;
aoqi@0 100 }
aoqi@0 101 bool is_valid_counted_loop() const;
aoqi@0 102 #ifndef PRODUCT
aoqi@0 103 virtual void dump_spec(outputStream *st) const;
aoqi@0 104 #endif
aoqi@0 105 };
aoqi@0 106
aoqi@0 107 //------------------------------Counted Loops----------------------------------
aoqi@0 108 // Counted loops are all trip-counted loops, with exactly 1 trip-counter exit
aoqi@0 109 // path (and maybe some other exit paths). The trip-counter exit is always
aoqi@0 110 // last in the loop. The trip-counter have to stride by a constant;
aoqi@0 111 // the exit value is also loop invariant.
aoqi@0 112
aoqi@0 113 // CountedLoopNodes and CountedLoopEndNodes come in matched pairs. The
aoqi@0 114 // CountedLoopNode has the incoming loop control and the loop-back-control
aoqi@0 115 // which is always the IfTrue before the matching CountedLoopEndNode. The
aoqi@0 116 // CountedLoopEndNode has an incoming control (possibly not the
aoqi@0 117 // CountedLoopNode if there is control flow in the loop), the post-increment
aoqi@0 118 // trip-counter value, and the limit. The trip-counter value is always of
aoqi@0 119 // the form (Op old-trip-counter stride). The old-trip-counter is produced
aoqi@0 120 // by a Phi connected to the CountedLoopNode. The stride is constant.
aoqi@0 121 // The Op is any commutable opcode, including Add, Mul, Xor. The
aoqi@0 122 // CountedLoopEndNode also takes in the loop-invariant limit value.
aoqi@0 123
aoqi@0 124 // From a CountedLoopNode I can reach the matching CountedLoopEndNode via the
aoqi@0 125 // loop-back control. From CountedLoopEndNodes I can reach CountedLoopNodes
aoqi@0 126 // via the old-trip-counter from the Op node.
aoqi@0 127
aoqi@0 128 //------------------------------CountedLoopNode--------------------------------
aoqi@0 129 // CountedLoopNodes head simple counted loops. CountedLoopNodes have as
aoqi@0 130 // inputs the incoming loop-start control and the loop-back control, so they
aoqi@0 131 // act like RegionNodes. They also take in the initial trip counter, the
aoqi@0 132 // loop-invariant stride and the loop-invariant limit value. CountedLoopNodes
aoqi@0 133 // produce a loop-body control and the trip counter value. Since
aoqi@0 134 // CountedLoopNodes behave like RegionNodes I still have a standard CFG model.
aoqi@0 135
aoqi@0 136 class CountedLoopNode : public LoopNode {
aoqi@0 137 // Size is bigger to hold _main_idx. However, _main_idx does not change
aoqi@0 138 // the semantics so it does not appear in the hash & cmp functions.
aoqi@0 139 virtual uint size_of() const { return sizeof(*this); }
aoqi@0 140
aoqi@0 141 // For Pre- and Post-loops during debugging ONLY, this holds the index of
aoqi@0 142 // the Main CountedLoop. Used to assert that we understand the graph shape.
aoqi@0 143 node_idx_t _main_idx;
aoqi@0 144
aoqi@0 145 // Known trip count calculated by compute_exact_trip_count()
aoqi@0 146 uint _trip_count;
aoqi@0 147
aoqi@0 148 // Expected trip count from profile data
aoqi@0 149 float _profile_trip_cnt;
aoqi@0 150
aoqi@0 151 // Log2 of original loop bodies in unrolled loop
aoqi@0 152 int _unrolled_count_log2;
aoqi@0 153
aoqi@0 154 // Node count prior to last unrolling - used to decide if
aoqi@0 155 // unroll,optimize,unroll,optimize,... is making progress
aoqi@0 156 int _node_count_before_unroll;
aoqi@0 157
aoqi@0 158 public:
aoqi@0 159 CountedLoopNode( Node *entry, Node *backedge )
aoqi@0 160 : LoopNode(entry, backedge), _main_idx(0), _trip_count(max_juint),
aoqi@0 161 _profile_trip_cnt(COUNT_UNKNOWN), _unrolled_count_log2(0),
aoqi@0 162 _node_count_before_unroll(0) {
aoqi@0 163 init_class_id(Class_CountedLoop);
aoqi@0 164 // Initialize _trip_count to the largest possible value.
aoqi@0 165 // Will be reset (lower) if the loop's trip count is known.
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 virtual int Opcode() const;
aoqi@0 169 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
aoqi@0 170
aoqi@0 171 Node *init_control() const { return in(EntryControl); }
aoqi@0 172 Node *back_control() const { return in(LoopBackControl); }
aoqi@0 173 CountedLoopEndNode *loopexit() const;
aoqi@0 174 Node *init_trip() const;
aoqi@0 175 Node *stride() const;
aoqi@0 176 int stride_con() const;
aoqi@0 177 bool stride_is_con() const;
aoqi@0 178 Node *limit() const;
aoqi@0 179 Node *incr() const;
aoqi@0 180 Node *phi() const;
aoqi@0 181
aoqi@0 182 // Match increment with optional truncation
aoqi@0 183 static Node* match_incr_with_optional_truncation(Node* expr, Node** trunc1, Node** trunc2, const TypeInt** trunc_type);
aoqi@0 184
aoqi@0 185 // A 'main' loop has a pre-loop and a post-loop. The 'main' loop
aoqi@0 186 // can run short a few iterations and may start a few iterations in.
aoqi@0 187 // It will be RCE'd and unrolled and aligned.
aoqi@0 188
aoqi@0 189 // A following 'post' loop will run any remaining iterations. Used
aoqi@0 190 // during Range Check Elimination, the 'post' loop will do any final
aoqi@0 191 // iterations with full checks. Also used by Loop Unrolling, where
aoqi@0 192 // the 'post' loop will do any epilog iterations needed. Basically,
aoqi@0 193 // a 'post' loop can not profitably be further unrolled or RCE'd.
aoqi@0 194
aoqi@0 195 // A preceding 'pre' loop will run at least 1 iteration (to do peeling),
aoqi@0 196 // it may do under-flow checks for RCE and may do alignment iterations
aoqi@0 197 // so the following main loop 'knows' that it is striding down cache
aoqi@0 198 // lines.
aoqi@0 199
aoqi@0 200 // A 'main' loop that is ONLY unrolled or peeled, never RCE'd or
aoqi@0 201 // Aligned, may be missing it's pre-loop.
aoqi@0 202 int is_normal_loop() const { return (_loop_flags&PreMainPostFlagsMask) == Normal; }
aoqi@0 203 int is_pre_loop () const { return (_loop_flags&PreMainPostFlagsMask) == Pre; }
aoqi@0 204 int is_main_loop () const { return (_loop_flags&PreMainPostFlagsMask) == Main; }
aoqi@0 205 int is_post_loop () const { return (_loop_flags&PreMainPostFlagsMask) == Post; }
aoqi@0 206 int is_main_no_pre_loop() const { return _loop_flags & MainHasNoPreLoop; }
aoqi@0 207 void set_main_no_pre_loop() { _loop_flags |= MainHasNoPreLoop; }
aoqi@0 208
aoqi@0 209 int main_idx() const { return _main_idx; }
aoqi@0 210
aoqi@0 211
aoqi@0 212 void set_pre_loop (CountedLoopNode *main) { assert(is_normal_loop(),""); _loop_flags |= Pre ; _main_idx = main->_idx; }
aoqi@0 213 void set_main_loop ( ) { assert(is_normal_loop(),""); _loop_flags |= Main; }
aoqi@0 214 void set_post_loop (CountedLoopNode *main) { assert(is_normal_loop(),""); _loop_flags |= Post; _main_idx = main->_idx; }
aoqi@0 215 void set_normal_loop( ) { _loop_flags &= ~PreMainPostFlagsMask; }
aoqi@0 216
aoqi@0 217 void set_trip_count(uint tc) { _trip_count = tc; }
aoqi@0 218 uint trip_count() { return _trip_count; }
aoqi@0 219
aoqi@0 220 bool has_exact_trip_count() const { return (_loop_flags & HasExactTripCount) != 0; }
aoqi@0 221 void set_exact_trip_count(uint tc) {
aoqi@0 222 _trip_count = tc;
aoqi@0 223 _loop_flags |= HasExactTripCount;
aoqi@0 224 }
aoqi@0 225 void set_nonexact_trip_count() {
aoqi@0 226 _loop_flags &= ~HasExactTripCount;
aoqi@0 227 }
aoqi@0 228
aoqi@0 229 void set_profile_trip_cnt(float ptc) { _profile_trip_cnt = ptc; }
aoqi@0 230 float profile_trip_cnt() { return _profile_trip_cnt; }
aoqi@0 231
aoqi@0 232 void double_unrolled_count() { _unrolled_count_log2++; }
aoqi@0 233 int unrolled_count() { return 1 << MIN2(_unrolled_count_log2, BitsPerInt-3); }
aoqi@0 234
aoqi@0 235 void set_node_count_before_unroll(int ct) { _node_count_before_unroll = ct; }
aoqi@0 236 int node_count_before_unroll() { return _node_count_before_unroll; }
aoqi@0 237
aoqi@0 238 #ifndef PRODUCT
aoqi@0 239 virtual void dump_spec(outputStream *st) const;
aoqi@0 240 #endif
aoqi@0 241 };
aoqi@0 242
aoqi@0 243 //------------------------------CountedLoopEndNode-----------------------------
aoqi@0 244 // CountedLoopEndNodes end simple trip counted loops. They act much like
aoqi@0 245 // IfNodes.
aoqi@0 246 class CountedLoopEndNode : public IfNode {
aoqi@0 247 public:
aoqi@0 248 enum { TestControl, TestValue };
aoqi@0 249
aoqi@0 250 CountedLoopEndNode( Node *control, Node *test, float prob, float cnt )
aoqi@0 251 : IfNode( control, test, prob, cnt) {
aoqi@0 252 init_class_id(Class_CountedLoopEnd);
aoqi@0 253 }
aoqi@0 254 virtual int Opcode() const;
aoqi@0 255
aoqi@0 256 Node *cmp_node() const { return (in(TestValue)->req() >=2) ? in(TestValue)->in(1) : NULL; }
aoqi@0 257 Node *incr() const { Node *tmp = cmp_node(); return (tmp && tmp->req()==3) ? tmp->in(1) : NULL; }
aoqi@0 258 Node *limit() const { Node *tmp = cmp_node(); return (tmp && tmp->req()==3) ? tmp->in(2) : NULL; }
aoqi@0 259 Node *stride() const { Node *tmp = incr (); return (tmp && tmp->req()==3) ? tmp->in(2) : NULL; }
aoqi@0 260 Node *phi() const { Node *tmp = incr (); return (tmp && tmp->req()==3) ? tmp->in(1) : NULL; }
aoqi@0 261 Node *init_trip() const { Node *tmp = phi (); return (tmp && tmp->req()==3) ? tmp->in(1) : NULL; }
aoqi@0 262 int stride_con() const;
aoqi@0 263 bool stride_is_con() const { Node *tmp = stride (); return (tmp != NULL && tmp->is_Con()); }
aoqi@0 264 BoolTest::mask test_trip() const { return in(TestValue)->as_Bool()->_test._test; }
aoqi@0 265 CountedLoopNode *loopnode() const {
aoqi@0 266 // The CountedLoopNode that goes with this CountedLoopEndNode may
aoqi@0 267 // have been optimized out by the IGVN so be cautious with the
aoqi@0 268 // pattern matching on the graph
aoqi@0 269 if (phi() == NULL) {
aoqi@0 270 return NULL;
aoqi@0 271 }
aoqi@0 272 Node *ln = phi()->in(0);
aoqi@0 273 if (ln->is_CountedLoop() && ln->as_CountedLoop()->loopexit() == this) {
aoqi@0 274 return (CountedLoopNode*)ln;
aoqi@0 275 }
aoqi@0 276 return NULL;
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 #ifndef PRODUCT
aoqi@0 280 virtual void dump_spec(outputStream *st) const;
aoqi@0 281 #endif
aoqi@0 282 };
aoqi@0 283
aoqi@0 284
aoqi@0 285 inline CountedLoopEndNode *CountedLoopNode::loopexit() const {
aoqi@0 286 Node *bc = back_control();
aoqi@0 287 if( bc == NULL ) return NULL;
aoqi@0 288 Node *le = bc->in(0);
aoqi@0 289 if( le->Opcode() != Op_CountedLoopEnd )
aoqi@0 290 return NULL;
aoqi@0 291 return (CountedLoopEndNode*)le;
aoqi@0 292 }
aoqi@0 293 inline Node *CountedLoopNode::init_trip() const { return loopexit() ? loopexit()->init_trip() : NULL; }
aoqi@0 294 inline Node *CountedLoopNode::stride() const { return loopexit() ? loopexit()->stride() : NULL; }
aoqi@0 295 inline int CountedLoopNode::stride_con() const { return loopexit() ? loopexit()->stride_con() : 0; }
aoqi@0 296 inline bool CountedLoopNode::stride_is_con() const { return loopexit() && loopexit()->stride_is_con(); }
aoqi@0 297 inline Node *CountedLoopNode::limit() const { return loopexit() ? loopexit()->limit() : NULL; }
aoqi@0 298 inline Node *CountedLoopNode::incr() const { return loopexit() ? loopexit()->incr() : NULL; }
aoqi@0 299 inline Node *CountedLoopNode::phi() const { return loopexit() ? loopexit()->phi() : NULL; }
aoqi@0 300
aoqi@0 301 //------------------------------LoopLimitNode-----------------------------
aoqi@0 302 // Counted Loop limit node which represents exact final iterator value:
aoqi@0 303 // trip_count = (limit - init_trip + stride - 1)/stride
aoqi@0 304 // final_value= trip_count * stride + init_trip.
aoqi@0 305 // Use HW instructions to calculate it when it can overflow in integer.
aoqi@0 306 // Note, final_value should fit into integer since counted loop has
aoqi@0 307 // limit check: limit <= max_int-stride.
aoqi@0 308 class LoopLimitNode : public Node {
aoqi@0 309 enum { Init=1, Limit=2, Stride=3 };
aoqi@0 310 public:
aoqi@0 311 LoopLimitNode( Compile* C, Node *init, Node *limit, Node *stride ) : Node(0,init,limit,stride) {
aoqi@0 312 // Put it on the Macro nodes list to optimize during macro nodes expansion.
aoqi@0 313 init_flags(Flag_is_macro);
aoqi@0 314 C->add_macro_node(this);
aoqi@0 315 }
aoqi@0 316 virtual int Opcode() const;
aoqi@0 317 virtual const Type *bottom_type() const { return TypeInt::INT; }
aoqi@0 318 virtual uint ideal_reg() const { return Op_RegI; }
aoqi@0 319 virtual const Type *Value( PhaseTransform *phase ) const;
aoqi@0 320 virtual Node *Ideal(PhaseGVN *phase, bool can_reshape);
aoqi@0 321 virtual Node *Identity( PhaseTransform *phase );
aoqi@0 322 };
aoqi@0 323
aoqi@0 324 // -----------------------------IdealLoopTree----------------------------------
aoqi@0 325 class IdealLoopTree : public ResourceObj {
aoqi@0 326 public:
aoqi@0 327 IdealLoopTree *_parent; // Parent in loop tree
aoqi@0 328 IdealLoopTree *_next; // Next sibling in loop tree
aoqi@0 329 IdealLoopTree *_child; // First child in loop tree
aoqi@0 330
aoqi@0 331 // The head-tail backedge defines the loop.
aoqi@0 332 // If tail is NULL then this loop has multiple backedges as part of the
aoqi@0 333 // same loop. During cleanup I'll peel off the multiple backedges; merge
aoqi@0 334 // them at the loop bottom and flow 1 real backedge into the loop.
aoqi@0 335 Node *_head; // Head of loop
aoqi@0 336 Node *_tail; // Tail of loop
aoqi@0 337 inline Node *tail(); // Handle lazy update of _tail field
aoqi@0 338 PhaseIdealLoop* _phase;
aoqi@0 339
aoqi@0 340 Node_List _body; // Loop body for inner loops
aoqi@0 341
aoqi@0 342 uint8 _nest; // Nesting depth
aoqi@0 343 uint8 _irreducible:1, // True if irreducible
aoqi@0 344 _has_call:1, // True if has call safepoint
aoqi@0 345 _has_sfpt:1, // True if has non-call safepoint
aoqi@0 346 _rce_candidate:1; // True if candidate for range check elimination
aoqi@0 347
aoqi@0 348 Node_List* _safepts; // List of safepoints in this loop
aoqi@0 349 Node_List* _required_safept; // A inner loop cannot delete these safepts;
aoqi@0 350 bool _allow_optimizations; // Allow loop optimizations
aoqi@0 351
aoqi@0 352 IdealLoopTree( PhaseIdealLoop* phase, Node *head, Node *tail )
aoqi@0 353 : _parent(0), _next(0), _child(0),
aoqi@0 354 _head(head), _tail(tail),
aoqi@0 355 _phase(phase),
aoqi@0 356 _safepts(NULL),
aoqi@0 357 _required_safept(NULL),
aoqi@0 358 _allow_optimizations(true),
aoqi@0 359 _nest(0), _irreducible(0), _has_call(0), _has_sfpt(0), _rce_candidate(0)
aoqi@0 360 { }
aoqi@0 361
aoqi@0 362 // Is 'l' a member of 'this'?
aoqi@0 363 int is_member( const IdealLoopTree *l ) const; // Test for nested membership
aoqi@0 364
aoqi@0 365 // Set loop nesting depth. Accumulate has_call bits.
aoqi@0 366 int set_nest( uint depth );
aoqi@0 367
aoqi@0 368 // Split out multiple fall-in edges from the loop header. Move them to a
aoqi@0 369 // private RegionNode before the loop. This becomes the loop landing pad.
aoqi@0 370 void split_fall_in( PhaseIdealLoop *phase, int fall_in_cnt );
aoqi@0 371
aoqi@0 372 // Split out the outermost loop from this shared header.
aoqi@0 373 void split_outer_loop( PhaseIdealLoop *phase );
aoqi@0 374
aoqi@0 375 // Merge all the backedges from the shared header into a private Region.
aoqi@0 376 // Feed that region as the one backedge to this loop.
aoqi@0 377 void merge_many_backedges( PhaseIdealLoop *phase );
aoqi@0 378
aoqi@0 379 // Split shared headers and insert loop landing pads.
aoqi@0 380 // Insert a LoopNode to replace the RegionNode.
aoqi@0 381 // Returns TRUE if loop tree is structurally changed.
aoqi@0 382 bool beautify_loops( PhaseIdealLoop *phase );
aoqi@0 383
aoqi@0 384 // Perform optimization to use the loop predicates for null checks and range checks.
aoqi@0 385 // Applies to any loop level (not just the innermost one)
aoqi@0 386 bool loop_predication( PhaseIdealLoop *phase);
aoqi@0 387
aoqi@0 388 // Perform iteration-splitting on inner loops. Split iterations to
aoqi@0 389 // avoid range checks or one-shot null checks. Returns false if the
aoqi@0 390 // current round of loop opts should stop.
aoqi@0 391 bool iteration_split( PhaseIdealLoop *phase, Node_List &old_new );
aoqi@0 392
aoqi@0 393 // Driver for various flavors of iteration splitting. Returns false
aoqi@0 394 // if the current round of loop opts should stop.
aoqi@0 395 bool iteration_split_impl( PhaseIdealLoop *phase, Node_List &old_new );
aoqi@0 396
aoqi@0 397 // Given dominators, try to find loops with calls that must always be
aoqi@0 398 // executed (call dominates loop tail). These loops do not need non-call
aoqi@0 399 // safepoints (ncsfpt).
aoqi@0 400 void check_safepts(VectorSet &visited, Node_List &stack);
aoqi@0 401
aoqi@0 402 // Allpaths backwards scan from loop tail, terminating each path at first safepoint
aoqi@0 403 // encountered.
aoqi@0 404 void allpaths_check_safepts(VectorSet &visited, Node_List &stack);
aoqi@0 405
aoqi@0 406 // Convert to counted loops where possible
aoqi@0 407 void counted_loop( PhaseIdealLoop *phase );
aoqi@0 408
aoqi@0 409 // Check for Node being a loop-breaking test
aoqi@0 410 Node *is_loop_exit(Node *iff) const;
aoqi@0 411
aoqi@0 412 // Returns true if ctrl is executed on every complete iteration
aoqi@0 413 bool dominates_backedge(Node* ctrl);
aoqi@0 414
aoqi@0 415 // Remove simplistic dead code from loop body
aoqi@0 416 void DCE_loop_body();
aoqi@0 417
aoqi@0 418 // Look for loop-exit tests with my 50/50 guesses from the Parsing stage.
aoqi@0 419 // Replace with a 1-in-10 exit guess.
aoqi@0 420 void adjust_loop_exit_prob( PhaseIdealLoop *phase );
aoqi@0 421
aoqi@0 422 // Return TRUE or FALSE if the loop should never be RCE'd or aligned.
aoqi@0 423 // Useful for unrolling loops with NO array accesses.
aoqi@0 424 bool policy_peel_only( PhaseIdealLoop *phase ) const;
aoqi@0 425
aoqi@0 426 // Return TRUE or FALSE if the loop should be unswitched -- clone
aoqi@0 427 // loop with an invariant test
aoqi@0 428 bool policy_unswitching( PhaseIdealLoop *phase ) const;
aoqi@0 429
aoqi@0 430 // Micro-benchmark spamming. Remove empty loops.
aoqi@0 431 bool policy_do_remove_empty_loop( PhaseIdealLoop *phase );
aoqi@0 432
aoqi@0 433 // Convert one iteration loop into normal code.
aoqi@0 434 bool policy_do_one_iteration_loop( PhaseIdealLoop *phase );
aoqi@0 435
aoqi@0 436 // Return TRUE or FALSE if the loop should be peeled or not. Peel if we can
aoqi@0 437 // make some loop-invariant test (usually a null-check) happen before the
aoqi@0 438 // loop.
aoqi@0 439 bool policy_peeling( PhaseIdealLoop *phase ) const;
aoqi@0 440
aoqi@0 441 // Return TRUE or FALSE if the loop should be maximally unrolled. Stash any
aoqi@0 442 // known trip count in the counted loop node.
aoqi@0 443 bool policy_maximally_unroll( PhaseIdealLoop *phase ) const;
aoqi@0 444
aoqi@0 445 // Return TRUE or FALSE if the loop should be unrolled or not. Unroll if
aoqi@0 446 // the loop is a CountedLoop and the body is small enough.
aoqi@0 447 bool policy_unroll( PhaseIdealLoop *phase ) const;
aoqi@0 448
aoqi@0 449 // Return TRUE or FALSE if the loop should be range-check-eliminated.
aoqi@0 450 // Gather a list of IF tests that are dominated by iteration splitting;
aoqi@0 451 // also gather the end of the first split and the start of the 2nd split.
aoqi@0 452 bool policy_range_check( PhaseIdealLoop *phase ) const;
aoqi@0 453
aoqi@0 454 // Return TRUE or FALSE if the loop should be cache-line aligned.
aoqi@0 455 // Gather the expression that does the alignment. Note that only
aoqi@0 456 // one array base can be aligned in a loop (unless the VM guarantees
aoqi@0 457 // mutual alignment). Note that if we vectorize short memory ops
aoqi@0 458 // into longer memory ops, we may want to increase alignment.
aoqi@0 459 bool policy_align( PhaseIdealLoop *phase ) const;
aoqi@0 460
aoqi@0 461 // Return TRUE if "iff" is a range check.
aoqi@0 462 bool is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const;
aoqi@0 463
aoqi@0 464 // Compute loop exact trip count if possible
aoqi@0 465 void compute_exact_trip_count( PhaseIdealLoop *phase );
aoqi@0 466
aoqi@0 467 // Compute loop trip count from profile data
aoqi@0 468 void compute_profile_trip_cnt( PhaseIdealLoop *phase );
aoqi@0 469
aoqi@0 470 // Reassociate invariant expressions.
aoqi@0 471 void reassociate_invariants(PhaseIdealLoop *phase);
aoqi@0 472 // Reassociate invariant add and subtract expressions.
aoqi@0 473 Node* reassociate_add_sub(Node* n1, PhaseIdealLoop *phase);
aoqi@0 474 // Return nonzero index of invariant operand if invariant and variant
aoqi@0 475 // are combined with an Add or Sub. Helper for reassociate_invariants.
aoqi@0 476 int is_invariant_addition(Node* n, PhaseIdealLoop *phase);
aoqi@0 477
aoqi@0 478 // Return true if n is invariant
aoqi@0 479 bool is_invariant(Node* n) const;
aoqi@0 480
aoqi@0 481 // Put loop body on igvn work list
aoqi@0 482 void record_for_igvn();
aoqi@0 483
aoqi@0 484 bool is_loop() { return !_irreducible && _tail && !_tail->is_top(); }
aoqi@0 485 bool is_inner() { return is_loop() && _child == NULL; }
aoqi@0 486 bool is_counted() { return is_loop() && _head != NULL && _head->is_CountedLoop(); }
aoqi@0 487
aoqi@0 488 #ifndef PRODUCT
aoqi@0 489 void dump_head( ) const; // Dump loop head only
aoqi@0 490 void dump() const; // Dump this loop recursively
aoqi@0 491 void verify_tree(IdealLoopTree *loop, const IdealLoopTree *parent) const;
aoqi@0 492 #endif
aoqi@0 493
aoqi@0 494 };
aoqi@0 495
aoqi@0 496 // -----------------------------PhaseIdealLoop---------------------------------
aoqi@0 497 // Computes the mapping from Nodes to IdealLoopTrees. Organizes IdealLoopTrees into a
aoqi@0 498 // loop tree. Drives the loop-based transformations on the ideal graph.
aoqi@0 499 class PhaseIdealLoop : public PhaseTransform {
aoqi@0 500 friend class IdealLoopTree;
aoqi@0 501 friend class SuperWord;
aoqi@0 502 // Pre-computed def-use info
aoqi@0 503 PhaseIterGVN &_igvn;
aoqi@0 504
aoqi@0 505 // Head of loop tree
aoqi@0 506 IdealLoopTree *_ltree_root;
aoqi@0 507
aoqi@0 508 // Array of pre-order numbers, plus post-visited bit.
aoqi@0 509 // ZERO for not pre-visited. EVEN for pre-visited but not post-visited.
aoqi@0 510 // ODD for post-visited. Other bits are the pre-order number.
aoqi@0 511 uint *_preorders;
aoqi@0 512 uint _max_preorder;
aoqi@0 513
aoqi@0 514 const PhaseIdealLoop* _verify_me;
aoqi@0 515 bool _verify_only;
aoqi@0 516
aoqi@0 517 // Allocate _preorders[] array
aoqi@0 518 void allocate_preorders() {
aoqi@0 519 _max_preorder = C->unique()+8;
aoqi@0 520 _preorders = NEW_RESOURCE_ARRAY(uint, _max_preorder);
aoqi@0 521 memset(_preorders, 0, sizeof(uint) * _max_preorder);
aoqi@0 522 }
aoqi@0 523
aoqi@0 524 // Allocate _preorders[] array
aoqi@0 525 void reallocate_preorders() {
aoqi@0 526 if ( _max_preorder < C->unique() ) {
aoqi@0 527 _preorders = REALLOC_RESOURCE_ARRAY(uint, _preorders, _max_preorder, C->unique());
aoqi@0 528 _max_preorder = C->unique();
aoqi@0 529 }
aoqi@0 530 memset(_preorders, 0, sizeof(uint) * _max_preorder);
aoqi@0 531 }
aoqi@0 532
aoqi@0 533 // Check to grow _preorders[] array for the case when build_loop_tree_impl()
aoqi@0 534 // adds new nodes.
aoqi@0 535 void check_grow_preorders( ) {
aoqi@0 536 if ( _max_preorder < C->unique() ) {
aoqi@0 537 uint newsize = _max_preorder<<1; // double size of array
aoqi@0 538 _preorders = REALLOC_RESOURCE_ARRAY(uint, _preorders, _max_preorder, newsize);
aoqi@0 539 memset(&_preorders[_max_preorder],0,sizeof(uint)*(newsize-_max_preorder));
aoqi@0 540 _max_preorder = newsize;
aoqi@0 541 }
aoqi@0 542 }
aoqi@0 543 // Check for pre-visited. Zero for NOT visited; non-zero for visited.
aoqi@0 544 int is_visited( Node *n ) const { return _preorders[n->_idx]; }
aoqi@0 545 // Pre-order numbers are written to the Nodes array as low-bit-set values.
aoqi@0 546 void set_preorder_visited( Node *n, int pre_order ) {
aoqi@0 547 assert( !is_visited( n ), "already set" );
aoqi@0 548 _preorders[n->_idx] = (pre_order<<1);
aoqi@0 549 };
aoqi@0 550 // Return pre-order number.
aoqi@0 551 int get_preorder( Node *n ) const { assert( is_visited(n), "" ); return _preorders[n->_idx]>>1; }
aoqi@0 552
aoqi@0 553 // Check for being post-visited.
aoqi@0 554 // Should be previsited already (checked with assert(is_visited(n))).
aoqi@0 555 int is_postvisited( Node *n ) const { assert( is_visited(n), "" ); return _preorders[n->_idx]&1; }
aoqi@0 556
aoqi@0 557 // Mark as post visited
aoqi@0 558 void set_postvisited( Node *n ) { assert( !is_postvisited( n ), "" ); _preorders[n->_idx] |= 1; }
aoqi@0 559
aoqi@0 560 // Set/get control node out. Set lower bit to distinguish from IdealLoopTree
aoqi@0 561 // Returns true if "n" is a data node, false if it's a control node.
aoqi@0 562 bool has_ctrl( Node *n ) const { return ((intptr_t)_nodes[n->_idx]) & 1; }
aoqi@0 563
aoqi@0 564 // clear out dead code after build_loop_late
aoqi@0 565 Node_List _deadlist;
aoqi@0 566
aoqi@0 567 // Support for faster execution of get_late_ctrl()/dom_lca()
aoqi@0 568 // when a node has many uses and dominator depth is deep.
aoqi@0 569 Node_Array _dom_lca_tags;
aoqi@0 570 void init_dom_lca_tags();
aoqi@0 571 void clear_dom_lca_tags();
aoqi@0 572
aoqi@0 573 // Helper for debugging bad dominance relationships
aoqi@0 574 bool verify_dominance(Node* n, Node* use, Node* LCA, Node* early);
aoqi@0 575
aoqi@0 576 Node* compute_lca_of_uses(Node* n, Node* early, bool verify = false);
aoqi@0 577
aoqi@0 578 // Inline wrapper for frequent cases:
aoqi@0 579 // 1) only one use
aoqi@0 580 // 2) a use is the same as the current LCA passed as 'n1'
aoqi@0 581 Node *dom_lca_for_get_late_ctrl( Node *lca, Node *n, Node *tag ) {
aoqi@0 582 assert( n->is_CFG(), "" );
aoqi@0 583 // Fast-path NULL lca
aoqi@0 584 if( lca != NULL && lca != n ) {
aoqi@0 585 assert( lca->is_CFG(), "" );
aoqi@0 586 // find LCA of all uses
aoqi@0 587 n = dom_lca_for_get_late_ctrl_internal( lca, n, tag );
aoqi@0 588 }
aoqi@0 589 return find_non_split_ctrl(n);
aoqi@0 590 }
aoqi@0 591 Node *dom_lca_for_get_late_ctrl_internal( Node *lca, Node *n, Node *tag );
aoqi@0 592
aoqi@0 593 // Helper function for directing control inputs away from CFG split
aoqi@0 594 // points.
aoqi@0 595 Node *find_non_split_ctrl( Node *ctrl ) const {
aoqi@0 596 if (ctrl != NULL) {
aoqi@0 597 if (ctrl->is_MultiBranch()) {
aoqi@0 598 ctrl = ctrl->in(0);
aoqi@0 599 }
aoqi@0 600 assert(ctrl->is_CFG(), "CFG");
aoqi@0 601 }
aoqi@0 602 return ctrl;
aoqi@0 603 }
aoqi@0 604
aoqi@0 605 public:
aoqi@0 606 bool has_node( Node* n ) const {
aoqi@0 607 guarantee(n != NULL, "No Node.");
aoqi@0 608 return _nodes[n->_idx] != NULL;
aoqi@0 609 }
aoqi@0 610 // check if transform created new nodes that need _ctrl recorded
aoqi@0 611 Node *get_late_ctrl( Node *n, Node *early );
aoqi@0 612 Node *get_early_ctrl( Node *n );
aoqi@0 613 Node *get_early_ctrl_for_expensive(Node *n, Node* earliest);
aoqi@0 614 void set_early_ctrl( Node *n );
aoqi@0 615 void set_subtree_ctrl( Node *root );
aoqi@0 616 void set_ctrl( Node *n, Node *ctrl ) {
aoqi@0 617 assert( !has_node(n) || has_ctrl(n), "" );
aoqi@0 618 assert( ctrl->in(0), "cannot set dead control node" );
aoqi@0 619 assert( ctrl == find_non_split_ctrl(ctrl), "must set legal crtl" );
aoqi@0 620 _nodes.map( n->_idx, (Node*)((intptr_t)ctrl + 1) );
aoqi@0 621 }
aoqi@0 622 // Set control and update loop membership
aoqi@0 623 void set_ctrl_and_loop(Node* n, Node* ctrl) {
aoqi@0 624 IdealLoopTree* old_loop = get_loop(get_ctrl(n));
aoqi@0 625 IdealLoopTree* new_loop = get_loop(ctrl);
aoqi@0 626 if (old_loop != new_loop) {
aoqi@0 627 if (old_loop->_child == NULL) old_loop->_body.yank(n);
aoqi@0 628 if (new_loop->_child == NULL) new_loop->_body.push(n);
aoqi@0 629 }
aoqi@0 630 set_ctrl(n, ctrl);
aoqi@0 631 }
aoqi@0 632 // Control nodes can be replaced or subsumed. During this pass they
aoqi@0 633 // get their replacement Node in slot 1. Instead of updating the block
aoqi@0 634 // location of all Nodes in the subsumed block, we lazily do it. As we
aoqi@0 635 // pull such a subsumed block out of the array, we write back the final
aoqi@0 636 // correct block.
aoqi@0 637 Node *get_ctrl( Node *i ) {
aoqi@0 638 assert(has_node(i), "");
aoqi@0 639 Node *n = get_ctrl_no_update(i);
aoqi@0 640 _nodes.map( i->_idx, (Node*)((intptr_t)n + 1) );
aoqi@0 641 assert(has_node(i) && has_ctrl(i), "");
aoqi@0 642 assert(n == find_non_split_ctrl(n), "must return legal ctrl" );
aoqi@0 643 return n;
aoqi@0 644 }
aoqi@0 645 // true if CFG node d dominates CFG node n
aoqi@0 646 bool is_dominator(Node *d, Node *n);
aoqi@0 647 // return get_ctrl for a data node and self(n) for a CFG node
aoqi@0 648 Node* ctrl_or_self(Node* n) {
aoqi@0 649 if (has_ctrl(n))
aoqi@0 650 return get_ctrl(n);
aoqi@0 651 else {
aoqi@0 652 assert (n->is_CFG(), "must be a CFG node");
aoqi@0 653 return n;
aoqi@0 654 }
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 private:
aoqi@0 658 Node *get_ctrl_no_update( Node *i ) const {
aoqi@0 659 assert( has_ctrl(i), "" );
aoqi@0 660 Node *n = (Node*)(((intptr_t)_nodes[i->_idx]) & ~1);
aoqi@0 661 if (!n->in(0)) {
aoqi@0 662 // Skip dead CFG nodes
aoqi@0 663 do {
aoqi@0 664 n = (Node*)(((intptr_t)_nodes[n->_idx]) & ~1);
aoqi@0 665 } while (!n->in(0));
aoqi@0 666 n = find_non_split_ctrl(n);
aoqi@0 667 }
aoqi@0 668 return n;
aoqi@0 669 }
aoqi@0 670
aoqi@0 671 // Check for loop being set
aoqi@0 672 // "n" must be a control node. Returns true if "n" is known to be in a loop.
aoqi@0 673 bool has_loop( Node *n ) const {
aoqi@0 674 assert(!has_node(n) || !has_ctrl(n), "");
aoqi@0 675 return has_node(n);
aoqi@0 676 }
aoqi@0 677 // Set loop
aoqi@0 678 void set_loop( Node *n, IdealLoopTree *loop ) {
aoqi@0 679 _nodes.map(n->_idx, (Node*)loop);
aoqi@0 680 }
aoqi@0 681 // Lazy-dazy update of 'get_ctrl' and 'idom_at' mechanisms. Replace
aoqi@0 682 // the 'old_node' with 'new_node'. Kill old-node. Add a reference
aoqi@0 683 // from old_node to new_node to support the lazy update. Reference
aoqi@0 684 // replaces loop reference, since that is not needed for dead node.
aoqi@0 685 public:
aoqi@0 686 void lazy_update( Node *old_node, Node *new_node ) {
aoqi@0 687 assert( old_node != new_node, "no cycles please" );
aoqi@0 688 //old_node->set_req( 1, new_node /*NO DU INFO*/ );
aoqi@0 689 // Nodes always have DU info now, so re-use the side array slot
aoqi@0 690 // for this node to provide the forwarding pointer.
aoqi@0 691 _nodes.map( old_node->_idx, (Node*)((intptr_t)new_node + 1) );
aoqi@0 692 }
aoqi@0 693 void lazy_replace( Node *old_node, Node *new_node ) {
aoqi@0 694 _igvn.replace_node( old_node, new_node );
aoqi@0 695 lazy_update( old_node, new_node );
aoqi@0 696 }
aoqi@0 697 void lazy_replace_proj( Node *old_node, Node *new_node ) {
aoqi@0 698 assert( old_node->req() == 1, "use this for Projs" );
aoqi@0 699 _igvn.hash_delete(old_node); // Must hash-delete before hacking edges
aoqi@0 700 old_node->add_req( NULL );
aoqi@0 701 lazy_replace( old_node, new_node );
aoqi@0 702 }
aoqi@0 703
aoqi@0 704 private:
aoqi@0 705
aoqi@0 706 // Place 'n' in some loop nest, where 'n' is a CFG node
aoqi@0 707 void build_loop_tree();
aoqi@0 708 int build_loop_tree_impl( Node *n, int pre_order );
aoqi@0 709 // Insert loop into the existing loop tree. 'innermost' is a leaf of the
aoqi@0 710 // loop tree, not the root.
aoqi@0 711 IdealLoopTree *sort( IdealLoopTree *loop, IdealLoopTree *innermost );
aoqi@0 712
aoqi@0 713 // Place Data nodes in some loop nest
aoqi@0 714 void build_loop_early( VectorSet &visited, Node_List &worklist, Node_Stack &nstack );
aoqi@0 715 void build_loop_late ( VectorSet &visited, Node_List &worklist, Node_Stack &nstack );
aoqi@0 716 void build_loop_late_post ( Node* n );
aoqi@0 717
aoqi@0 718 // Array of immediate dominance info for each CFG node indexed by node idx
aoqi@0 719 private:
aoqi@0 720 uint _idom_size;
aoqi@0 721 Node **_idom; // Array of immediate dominators
aoqi@0 722 uint *_dom_depth; // Used for fast LCA test
aoqi@0 723 GrowableArray<uint>* _dom_stk; // For recomputation of dom depth
aoqi@0 724
aoqi@0 725 Node* idom_no_update(Node* d) const {
aoqi@0 726 assert(d->_idx < _idom_size, "oob");
aoqi@0 727 Node* n = _idom[d->_idx];
aoqi@0 728 assert(n != NULL,"Bad immediate dominator info.");
aoqi@0 729 while (n->in(0) == NULL) { // Skip dead CFG nodes
aoqi@0 730 //n = n->in(1);
aoqi@0 731 n = (Node*)(((intptr_t)_nodes[n->_idx]) & ~1);
aoqi@0 732 assert(n != NULL,"Bad immediate dominator info.");
aoqi@0 733 }
aoqi@0 734 return n;
aoqi@0 735 }
aoqi@0 736 Node *idom(Node* d) const {
aoqi@0 737 uint didx = d->_idx;
aoqi@0 738 Node *n = idom_no_update(d);
aoqi@0 739 _idom[didx] = n; // Lazily remove dead CFG nodes from table.
aoqi@0 740 return n;
aoqi@0 741 }
aoqi@0 742 uint dom_depth(Node* d) const {
aoqi@0 743 guarantee(d != NULL, "Null dominator info.");
aoqi@0 744 guarantee(d->_idx < _idom_size, "");
aoqi@0 745 return _dom_depth[d->_idx];
aoqi@0 746 }
aoqi@0 747 void set_idom(Node* d, Node* n, uint dom_depth);
aoqi@0 748 // Locally compute IDOM using dom_lca call
aoqi@0 749 Node *compute_idom( Node *region ) const;
aoqi@0 750 // Recompute dom_depth
aoqi@0 751 void recompute_dom_depth();
aoqi@0 752
aoqi@0 753 // Is safept not required by an outer loop?
aoqi@0 754 bool is_deleteable_safept(Node* sfpt);
aoqi@0 755
aoqi@0 756 // Replace parallel induction variable (parallel to trip counter)
aoqi@0 757 void replace_parallel_iv(IdealLoopTree *loop);
aoqi@0 758
aoqi@0 759 // Perform verification that the graph is valid.
aoqi@0 760 PhaseIdealLoop( PhaseIterGVN &igvn) :
aoqi@0 761 PhaseTransform(Ideal_Loop),
aoqi@0 762 _igvn(igvn),
aoqi@0 763 _dom_lca_tags(arena()), // Thread::resource_area
aoqi@0 764 _verify_me(NULL),
aoqi@0 765 _verify_only(true) {
aoqi@0 766 build_and_optimize(false, false);
aoqi@0 767 }
aoqi@0 768
aoqi@0 769 // build the loop tree and perform any requested optimizations
aoqi@0 770 void build_and_optimize(bool do_split_if, bool skip_loop_opts);
aoqi@0 771
aoqi@0 772 public:
aoqi@0 773 // Dominators for the sea of nodes
aoqi@0 774 void Dominators();
aoqi@0 775 Node *dom_lca( Node *n1, Node *n2 ) const {
aoqi@0 776 return find_non_split_ctrl(dom_lca_internal(n1, n2));
aoqi@0 777 }
aoqi@0 778 Node *dom_lca_internal( Node *n1, Node *n2 ) const;
aoqi@0 779
aoqi@0 780 // Compute the Ideal Node to Loop mapping
aoqi@0 781 PhaseIdealLoop( PhaseIterGVN &igvn, bool do_split_ifs, bool skip_loop_opts = false) :
aoqi@0 782 PhaseTransform(Ideal_Loop),
aoqi@0 783 _igvn(igvn),
aoqi@0 784 _dom_lca_tags(arena()), // Thread::resource_area
aoqi@0 785 _verify_me(NULL),
aoqi@0 786 _verify_only(false) {
aoqi@0 787 build_and_optimize(do_split_ifs, skip_loop_opts);
aoqi@0 788 }
aoqi@0 789
aoqi@0 790 // Verify that verify_me made the same decisions as a fresh run.
aoqi@0 791 PhaseIdealLoop( PhaseIterGVN &igvn, const PhaseIdealLoop *verify_me) :
aoqi@0 792 PhaseTransform(Ideal_Loop),
aoqi@0 793 _igvn(igvn),
aoqi@0 794 _dom_lca_tags(arena()), // Thread::resource_area
aoqi@0 795 _verify_me(verify_me),
aoqi@0 796 _verify_only(false) {
aoqi@0 797 build_and_optimize(false, false);
aoqi@0 798 }
aoqi@0 799
aoqi@0 800 // Build and verify the loop tree without modifying the graph. This
aoqi@0 801 // is useful to verify that all inputs properly dominate their uses.
aoqi@0 802 static void verify(PhaseIterGVN& igvn) {
aoqi@0 803 #ifdef ASSERT
aoqi@0 804 PhaseIdealLoop v(igvn);
aoqi@0 805 #endif
aoqi@0 806 }
aoqi@0 807
aoqi@0 808 // True if the method has at least 1 irreducible loop
aoqi@0 809 bool _has_irreducible_loops;
aoqi@0 810
aoqi@0 811 // Per-Node transform
aoqi@0 812 virtual Node *transform( Node *a_node ) { return 0; }
aoqi@0 813
aoqi@0 814 bool is_counted_loop( Node *x, IdealLoopTree *loop );
aoqi@0 815
aoqi@0 816 Node* exact_limit( IdealLoopTree *loop );
aoqi@0 817
aoqi@0 818 // Return a post-walked LoopNode
aoqi@0 819 IdealLoopTree *get_loop( Node *n ) const {
aoqi@0 820 // Dead nodes have no loop, so return the top level loop instead
aoqi@0 821 if (!has_node(n)) return _ltree_root;
aoqi@0 822 assert(!has_ctrl(n), "");
aoqi@0 823 return (IdealLoopTree*)_nodes[n->_idx];
aoqi@0 824 }
aoqi@0 825
aoqi@0 826 // Is 'n' a (nested) member of 'loop'?
aoqi@0 827 int is_member( const IdealLoopTree *loop, Node *n ) const {
aoqi@0 828 return loop->is_member(get_loop(n)); }
aoqi@0 829
aoqi@0 830 // This is the basic building block of the loop optimizations. It clones an
aoqi@0 831 // entire loop body. It makes an old_new loop body mapping; with this
aoqi@0 832 // mapping you can find the new-loop equivalent to an old-loop node. All
aoqi@0 833 // new-loop nodes are exactly equal to their old-loop counterparts, all
aoqi@0 834 // edges are the same. All exits from the old-loop now have a RegionNode
aoqi@0 835 // that merges the equivalent new-loop path. This is true even for the
aoqi@0 836 // normal "loop-exit" condition. All uses of loop-invariant old-loop values
aoqi@0 837 // now come from (one or more) Phis that merge their new-loop equivalents.
aoqi@0 838 // Parameter side_by_side_idom:
aoqi@0 839 // When side_by_size_idom is NULL, the dominator tree is constructed for
aoqi@0 840 // the clone loop to dominate the original. Used in construction of
aoqi@0 841 // pre-main-post loop sequence.
aoqi@0 842 // When nonnull, the clone and original are side-by-side, both are
aoqi@0 843 // dominated by the passed in side_by_side_idom node. Used in
aoqi@0 844 // construction of unswitched loops.
aoqi@0 845 void clone_loop( IdealLoopTree *loop, Node_List &old_new, int dom_depth,
aoqi@0 846 Node* side_by_side_idom = NULL);
aoqi@0 847
aoqi@0 848 // If we got the effect of peeling, either by actually peeling or by
aoqi@0 849 // making a pre-loop which must execute at least once, we can remove
aoqi@0 850 // all loop-invariant dominated tests in the main body.
aoqi@0 851 void peeled_dom_test_elim( IdealLoopTree *loop, Node_List &old_new );
aoqi@0 852
aoqi@0 853 // Generate code to do a loop peel for the given loop (and body).
aoqi@0 854 // old_new is a temp array.
aoqi@0 855 void do_peeling( IdealLoopTree *loop, Node_List &old_new );
aoqi@0 856
aoqi@0 857 // Add pre and post loops around the given loop. These loops are used
aoqi@0 858 // during RCE, unrolling and aligning loops.
aoqi@0 859 void insert_pre_post_loops( IdealLoopTree *loop, Node_List &old_new, bool peel_only );
aoqi@0 860 // If Node n lives in the back_ctrl block, we clone a private version of n
aoqi@0 861 // in preheader_ctrl block and return that, otherwise return n.
aoqi@0 862 Node *clone_up_backedge_goo( Node *back_ctrl, Node *preheader_ctrl, Node *n, VectorSet &visited, Node_Stack &clones );
aoqi@0 863
aoqi@0 864 // Take steps to maximally unroll the loop. Peel any odd iterations, then
aoqi@0 865 // unroll to do double iterations. The next round of major loop transforms
aoqi@0 866 // will repeat till the doubled loop body does all remaining iterations in 1
aoqi@0 867 // pass.
aoqi@0 868 void do_maximally_unroll( IdealLoopTree *loop, Node_List &old_new );
aoqi@0 869
aoqi@0 870 // Unroll the loop body one step - make each trip do 2 iterations.
aoqi@0 871 void do_unroll( IdealLoopTree *loop, Node_List &old_new, bool adjust_min_trip );
aoqi@0 872
aoqi@0 873 // Return true if exp is a constant times an induction var
aoqi@0 874 bool is_scaled_iv(Node* exp, Node* iv, int* p_scale);
aoqi@0 875
aoqi@0 876 // Return true if exp is a scaled induction var plus (or minus) constant
aoqi@0 877 bool is_scaled_iv_plus_offset(Node* exp, Node* iv, int* p_scale, Node** p_offset, int depth = 0);
aoqi@0 878
aoqi@0 879 // Create a new if above the uncommon_trap_if_pattern for the predicate to be promoted
aoqi@0 880 ProjNode* create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
aoqi@0 881 Deoptimization::DeoptReason reason);
aoqi@0 882 void register_control(Node* n, IdealLoopTree *loop, Node* pred);
aoqi@0 883
aoqi@0 884 // Clone loop predicates to cloned loops (peeled, unswitched)
aoqi@0 885 static ProjNode* clone_predicate(ProjNode* predicate_proj, Node* new_entry,
aoqi@0 886 Deoptimization::DeoptReason reason,
aoqi@0 887 PhaseIdealLoop* loop_phase,
aoqi@0 888 PhaseIterGVN* igvn);
aoqi@0 889
aoqi@0 890 static Node* clone_loop_predicates(Node* old_entry, Node* new_entry,
aoqi@0 891 bool clone_limit_check,
aoqi@0 892 PhaseIdealLoop* loop_phase,
aoqi@0 893 PhaseIterGVN* igvn);
aoqi@0 894 Node* clone_loop_predicates(Node* old_entry, Node* new_entry, bool clone_limit_check);
aoqi@0 895
aoqi@0 896 static Node* skip_loop_predicates(Node* entry);
aoqi@0 897
aoqi@0 898 // Find a good location to insert a predicate
aoqi@0 899 static ProjNode* find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason);
aoqi@0 900 // Find a predicate
aoqi@0 901 static Node* find_predicate(Node* entry);
aoqi@0 902 // Construct a range check for a predicate if
aoqi@0 903 BoolNode* rc_predicate(IdealLoopTree *loop, Node* ctrl,
aoqi@0 904 int scale, Node* offset,
aoqi@0 905 Node* init, Node* limit, Node* stride,
aoqi@0 906 Node* range, bool upper);
aoqi@0 907
aoqi@0 908 // Implementation of the loop predication to promote checks outside the loop
aoqi@0 909 bool loop_predication_impl(IdealLoopTree *loop);
aoqi@0 910
aoqi@0 911 // Helper function to collect predicate for eliminating the useless ones
aoqi@0 912 void collect_potentially_useful_predicates(IdealLoopTree *loop, Unique_Node_List &predicate_opaque1);
aoqi@0 913 void eliminate_useless_predicates();
aoqi@0 914
aoqi@0 915 // Change the control input of expensive nodes to allow commoning by
aoqi@0 916 // IGVN when it is guaranteed to not result in a more frequent
aoqi@0 917 // execution of the expensive node. Return true if progress.
aoqi@0 918 bool process_expensive_nodes();
aoqi@0 919
aoqi@0 920 // Check whether node has become unreachable
aoqi@0 921 bool is_node_unreachable(Node *n) const {
aoqi@0 922 return !has_node(n) || n->is_unreachable(_igvn);
aoqi@0 923 }
aoqi@0 924
aoqi@0 925 // Eliminate range-checks and other trip-counter vs loop-invariant tests.
aoqi@0 926 void do_range_check( IdealLoopTree *loop, Node_List &old_new );
aoqi@0 927
aoqi@0 928 // Create a slow version of the loop by cloning the loop
aoqi@0 929 // and inserting an if to select fast-slow versions.
aoqi@0 930 ProjNode* create_slow_version_of_loop(IdealLoopTree *loop,
aoqi@0 931 Node_List &old_new);
aoqi@0 932
aoqi@0 933 // Clone loop with an invariant test (that does not exit) and
aoqi@0 934 // insert a clone of the test that selects which version to
aoqi@0 935 // execute.
aoqi@0 936 void do_unswitching (IdealLoopTree *loop, Node_List &old_new);
aoqi@0 937
aoqi@0 938 // Find candidate "if" for unswitching
aoqi@0 939 IfNode* find_unswitching_candidate(const IdealLoopTree *loop) const;
aoqi@0 940
aoqi@0 941 // Range Check Elimination uses this function!
aoqi@0 942 // Constrain the main loop iterations so the affine function:
aoqi@0 943 // low_limit <= scale_con * I + offset < upper_limit
aoqi@0 944 // always holds true. That is, either increase the number of iterations in
aoqi@0 945 // the pre-loop or the post-loop until the condition holds true in the main
aoqi@0 946 // loop. Scale_con, offset and limit are all loop invariant.
aoqi@0 947 void add_constraint( int stride_con, int scale_con, Node *offset, Node *low_limit, Node *upper_limit, Node *pre_ctrl, Node **pre_limit, Node **main_limit );
aoqi@0 948 // Helper function for add_constraint().
aoqi@0 949 Node* adjust_limit( int stride_con, Node * scale, Node *offset, Node *rc_limit, Node *loop_limit, Node *pre_ctrl );
aoqi@0 950
aoqi@0 951 // Partially peel loop up through last_peel node.
aoqi@0 952 bool partial_peel( IdealLoopTree *loop, Node_List &old_new );
aoqi@0 953
aoqi@0 954 // Create a scheduled list of nodes control dependent on ctrl set.
aoqi@0 955 void scheduled_nodelist( IdealLoopTree *loop, VectorSet& ctrl, Node_List &sched );
aoqi@0 956 // Has a use in the vector set
aoqi@0 957 bool has_use_in_set( Node* n, VectorSet& vset );
aoqi@0 958 // Has use internal to the vector set (ie. not in a phi at the loop head)
aoqi@0 959 bool has_use_internal_to_set( Node* n, VectorSet& vset, IdealLoopTree *loop );
aoqi@0 960 // clone "n" for uses that are outside of loop
aoqi@0 961 int clone_for_use_outside_loop( IdealLoopTree *loop, Node* n, Node_List& worklist );
aoqi@0 962 // clone "n" for special uses that are in the not_peeled region
aoqi@0 963 void clone_for_special_use_inside_loop( IdealLoopTree *loop, Node* n,
aoqi@0 964 VectorSet& not_peel, Node_List& sink_list, Node_List& worklist );
aoqi@0 965 // Insert phi(lp_entry_val, back_edge_val) at use->in(idx) for loop lp if phi does not already exist
aoqi@0 966 void insert_phi_for_loop( Node* use, uint idx, Node* lp_entry_val, Node* back_edge_val, LoopNode* lp );
aoqi@0 967 #ifdef ASSERT
aoqi@0 968 // Validate the loop partition sets: peel and not_peel
aoqi@0 969 bool is_valid_loop_partition( IdealLoopTree *loop, VectorSet& peel, Node_List& peel_list, VectorSet& not_peel );
aoqi@0 970 // Ensure that uses outside of loop are of the right form
aoqi@0 971 bool is_valid_clone_loop_form( IdealLoopTree *loop, Node_List& peel_list,
aoqi@0 972 uint orig_exit_idx, uint clone_exit_idx);
aoqi@0 973 bool is_valid_clone_loop_exit_use( IdealLoopTree *loop, Node* use, uint exit_idx);
aoqi@0 974 #endif
aoqi@0 975
aoqi@0 976 // Returns nonzero constant stride if-node is a possible iv test (otherwise returns zero.)
aoqi@0 977 int stride_of_possible_iv( Node* iff );
aoqi@0 978 bool is_possible_iv_test( Node* iff ) { return stride_of_possible_iv(iff) != 0; }
aoqi@0 979 // Return the (unique) control output node that's in the loop (if it exists.)
aoqi@0 980 Node* stay_in_loop( Node* n, IdealLoopTree *loop);
aoqi@0 981 // Insert a signed compare loop exit cloned from an unsigned compare.
aoqi@0 982 IfNode* insert_cmpi_loop_exit(IfNode* if_cmpu, IdealLoopTree *loop);
aoqi@0 983 void remove_cmpi_loop_exit(IfNode* if_cmp, IdealLoopTree *loop);
aoqi@0 984 // Utility to register node "n" with PhaseIdealLoop
aoqi@0 985 void register_node(Node* n, IdealLoopTree *loop, Node* pred, int ddepth);
aoqi@0 986 // Utility to create an if-projection
aoqi@0 987 ProjNode* proj_clone(ProjNode* p, IfNode* iff);
aoqi@0 988 // Force the iff control output to be the live_proj
aoqi@0 989 Node* short_circuit_if(IfNode* iff, ProjNode* live_proj);
aoqi@0 990 // Insert a region before an if projection
aoqi@0 991 RegionNode* insert_region_before_proj(ProjNode* proj);
aoqi@0 992 // Insert a new if before an if projection
aoqi@0 993 ProjNode* insert_if_before_proj(Node* left, bool Signed, BoolTest::mask relop, Node* right, ProjNode* proj);
aoqi@0 994
aoqi@0 995 // Passed in a Phi merging (recursively) some nearly equivalent Bool/Cmps.
aoqi@0 996 // "Nearly" because all Nodes have been cloned from the original in the loop,
aoqi@0 997 // but the fall-in edges to the Cmp are different. Clone bool/Cmp pairs
aoqi@0 998 // through the Phi recursively, and return a Bool.
aoqi@0 999 BoolNode *clone_iff( PhiNode *phi, IdealLoopTree *loop );
aoqi@0 1000 CmpNode *clone_bool( PhiNode *phi, IdealLoopTree *loop );
aoqi@0 1001
aoqi@0 1002
aoqi@0 1003 // Rework addressing expressions to get the most loop-invariant stuff
aoqi@0 1004 // moved out. We'd like to do all associative operators, but it's especially
aoqi@0 1005 // important (common) to do address expressions.
aoqi@0 1006 Node *remix_address_expressions( Node *n );
aoqi@0 1007
aoqi@0 1008 // Attempt to use a conditional move instead of a phi/branch
aoqi@0 1009 Node *conditional_move( Node *n );
aoqi@0 1010
aoqi@0 1011 // Reorganize offset computations to lower register pressure.
aoqi@0 1012 // Mostly prevent loop-fallout uses of the pre-incremented trip counter
aoqi@0 1013 // (which are then alive with the post-incremented trip counter
aoqi@0 1014 // forcing an extra register move)
aoqi@0 1015 void reorg_offsets( IdealLoopTree *loop );
aoqi@0 1016
aoqi@0 1017 // Check for aggressive application of 'split-if' optimization,
aoqi@0 1018 // using basic block level info.
aoqi@0 1019 void split_if_with_blocks ( VectorSet &visited, Node_Stack &nstack );
aoqi@0 1020 Node *split_if_with_blocks_pre ( Node *n );
aoqi@0 1021 void split_if_with_blocks_post( Node *n );
aoqi@0 1022 Node *has_local_phi_input( Node *n );
aoqi@0 1023 // Mark an IfNode as being dominated by a prior test,
aoqi@0 1024 // without actually altering the CFG (and hence IDOM info).
aoqi@0 1025 void dominated_by( Node *prevdom, Node *iff, bool flip = false, bool exclude_loop_predicate = false );
aoqi@0 1026
aoqi@0 1027 // Split Node 'n' through merge point
aoqi@0 1028 Node *split_thru_region( Node *n, Node *region );
aoqi@0 1029 // Split Node 'n' through merge point if there is enough win.
aoqi@0 1030 Node *split_thru_phi( Node *n, Node *region, int policy );
aoqi@0 1031 // Found an If getting its condition-code input from a Phi in the
aoqi@0 1032 // same block. Split thru the Region.
aoqi@0 1033 void do_split_if( Node *iff );
aoqi@0 1034
aoqi@0 1035 // Conversion of fill/copy patterns into intrisic versions
aoqi@0 1036 bool do_intrinsify_fill();
aoqi@0 1037 bool intrinsify_fill(IdealLoopTree* lpt);
aoqi@0 1038 bool match_fill_loop(IdealLoopTree* lpt, Node*& store, Node*& store_value,
aoqi@0 1039 Node*& shift, Node*& offset);
aoqi@0 1040
aoqi@0 1041 private:
aoqi@0 1042 // Return a type based on condition control flow
aoqi@0 1043 const TypeInt* filtered_type( Node *n, Node* n_ctrl);
aoqi@0 1044 const TypeInt* filtered_type( Node *n ) { return filtered_type(n, NULL); }
aoqi@0 1045 // Helpers for filtered type
aoqi@0 1046 const TypeInt* filtered_type_from_dominators( Node* val, Node *val_ctrl);
aoqi@0 1047
aoqi@0 1048 // Helper functions
aoqi@0 1049 Node *spinup( Node *iff, Node *new_false, Node *new_true, Node *region, Node *phi, small_cache *cache );
aoqi@0 1050 Node *find_use_block( Node *use, Node *def, Node *old_false, Node *new_false, Node *old_true, Node *new_true );
aoqi@0 1051 void handle_use( Node *use, Node *def, small_cache *cache, Node *region_dom, Node *new_false, Node *new_true, Node *old_false, Node *old_true );
aoqi@0 1052 bool split_up( Node *n, Node *blk1, Node *blk2 );
aoqi@0 1053 void sink_use( Node *use, Node *post_loop );
aoqi@0 1054 Node *place_near_use( Node *useblock ) const;
aoqi@0 1055
aoqi@0 1056 bool _created_loop_node;
aoqi@0 1057 public:
aoqi@0 1058 void set_created_loop_node() { _created_loop_node = true; }
aoqi@0 1059 bool created_loop_node() { return _created_loop_node; }
aoqi@0 1060 void register_new_node( Node *n, Node *blk );
aoqi@0 1061
aoqi@0 1062 #ifdef ASSERT
aoqi@0 1063 void dump_bad_graph(const char* msg, Node* n, Node* early, Node* LCA);
aoqi@0 1064 #endif
aoqi@0 1065
aoqi@0 1066 #ifndef PRODUCT
aoqi@0 1067 void dump( ) const;
aoqi@0 1068 void dump( IdealLoopTree *loop, uint rpo_idx, Node_List &rpo_list ) const;
aoqi@0 1069 void rpo( Node *start, Node_Stack &stk, VectorSet &visited, Node_List &rpo_list ) const;
aoqi@0 1070 void verify() const; // Major slow :-)
aoqi@0 1071 void verify_compare( Node *n, const PhaseIdealLoop *loop_verify, VectorSet &visited ) const;
aoqi@0 1072 IdealLoopTree *get_loop_idx(Node* n) const {
aoqi@0 1073 // Dead nodes have no loop, so return the top level loop instead
aoqi@0 1074 return _nodes[n->_idx] ? (IdealLoopTree*)_nodes[n->_idx] : _ltree_root;
aoqi@0 1075 }
aoqi@0 1076 // Print some stats
aoqi@0 1077 static void print_statistics();
aoqi@0 1078 static int _loop_invokes; // Count of PhaseIdealLoop invokes
aoqi@0 1079 static int _loop_work; // Sum of PhaseIdealLoop x _unique
aoqi@0 1080 #endif
aoqi@0 1081 };
aoqi@0 1082
aoqi@0 1083 inline Node* IdealLoopTree::tail() {
aoqi@0 1084 // Handle lazy update of _tail field
aoqi@0 1085 Node *n = _tail;
aoqi@0 1086 //while( !n->in(0) ) // Skip dead CFG nodes
aoqi@0 1087 //n = n->in(1);
aoqi@0 1088 if (n->in(0) == NULL)
aoqi@0 1089 n = _phase->get_ctrl(n);
aoqi@0 1090 _tail = n;
aoqi@0 1091 return n;
aoqi@0 1092 }
aoqi@0 1093
aoqi@0 1094
aoqi@0 1095 // Iterate over the loop tree using a preorder, left-to-right traversal.
aoqi@0 1096 //
aoqi@0 1097 // Example that visits all counted loops from within PhaseIdealLoop
aoqi@0 1098 //
aoqi@0 1099 // for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) {
aoqi@0 1100 // IdealLoopTree* lpt = iter.current();
aoqi@0 1101 // if (!lpt->is_counted()) continue;
aoqi@0 1102 // ...
aoqi@0 1103 class LoopTreeIterator : public StackObj {
aoqi@0 1104 private:
aoqi@0 1105 IdealLoopTree* _root;
aoqi@0 1106 IdealLoopTree* _curnt;
aoqi@0 1107
aoqi@0 1108 public:
aoqi@0 1109 LoopTreeIterator(IdealLoopTree* root) : _root(root), _curnt(root) {}
aoqi@0 1110
aoqi@0 1111 bool done() { return _curnt == NULL; } // Finished iterating?
aoqi@0 1112
aoqi@0 1113 void next(); // Advance to next loop tree
aoqi@0 1114
aoqi@0 1115 IdealLoopTree* current() { return _curnt; } // Return current value of iterator.
aoqi@0 1116 };
aoqi@0 1117
aoqi@0 1118 #endif // SHARE_VM_OPTO_LOOPNODE_HPP

mercurial