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 #include "precompiled.hpp" 26 #include "memory/allocation.inline.hpp" 27 #include "opto/connode.hpp" 28 #include "opto/convertnode.hpp" 29 #include "opto/loopnode.hpp" 30 #include "opto/opaquenode.hpp" 31 #include "opto/rootnode.hpp" 32 33 //================= Loop Unswitching ===================== 34 // 35 // orig: transformed: 36 // if (invariant-test) then 37 // predicate predicate 38 // loop loop 39 // stmt1 stmt1 40 // if (invariant-test) then stmt2 41 // stmt2 stmt4 42 // else endloop 43 // stmt3 else 44 // endif predicate [clone] 45 // stmt4 loop [clone] 46 // endloop stmt1 [clone] 47 // stmt3 48 // stmt4 [clone] 49 // endloop 50 // endif 51 // 52 // Note: the "else" clause may be empty 53 54 //------------------------------policy_unswitching----------------------------- 55 // Return TRUE or FALSE if the loop should be unswitched 56 // (ie. clone loop with an invariant test that does not exit the loop) 57 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const { 58 if (!LoopUnswitching) { 59 return false; 60 } 61 if (!_head->is_Loop()) { 62 return false; 63 } 64 65 // If nodes are depleted, some transform has miscalculated its needs. 66 assert(!phase->exceeding_node_budget(), "sanity"); 67 68 // check for vectorized loops, any unswitching was already applied 69 if (_head->is_CountedLoop() && _head->as_CountedLoop()->is_unroll_only()) { 70 return false; 71 } 72 73 LoopNode* head = _head->as_Loop(); 74 if (head->unswitch_count() + 1 > head->unswitch_max()) { 75 return false; 76 } 77 if (phase->find_unswitching_candidate(this) == NULL) { 78 return false; 79 } 80 81 // Too speculative if running low on nodes. 82 return phase->may_require_nodes(est_loop_clone_sz(3, _body.size())); 83 } 84 85 //------------------------------find_unswitching_candidate----------------------------- 86 // Find candidate "if" for unswitching 87 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const { 88 89 // Find first invariant test that doesn't exit the loop 90 LoopNode *head = loop->_head->as_Loop(); 91 IfNode* unswitch_iff = NULL; 92 Node* n = head->in(LoopNode::LoopBackControl); 93 while (n != head) { 94 Node* n_dom = idom(n); 95 if (n->is_Region()) { 96 if (n_dom->is_If()) { 97 IfNode* iff = n_dom->as_If(); 98 if (iff->in(1)->is_Bool()) { 99 BoolNode* bol = iff->in(1)->as_Bool(); 100 if (bol->in(1)->is_Cmp()) { 101 // If condition is invariant and not a loop exit, 102 // then found reason to unswitch. 103 if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) { 104 unswitch_iff = iff; 105 } 106 } 107 } 108 } 109 } 110 n = n_dom; 111 } 112 return unswitch_iff; 113 } 114 115 //------------------------------do_unswitching----------------------------- 116 // Clone loop with an invariant test (that does not exit) and 117 // insert a clone of the test that selects which version to 118 // execute. 119 void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) { 120 121 // Find first invariant test that doesn't exit the loop 122 LoopNode *head = loop->_head->as_Loop(); 123 124 IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop); 125 assert(unswitch_iff != NULL, "should be at least one"); 126 127 #ifndef PRODUCT 128 if (TraceLoopOpts) { 129 tty->print("Unswitch %d ", head->unswitch_count()+1); 130 loop->dump_head(); 131 } 132 #endif 133 134 // Need to revert back to normal loop 135 if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) { 136 head->as_CountedLoop()->set_normal_loop(); 137 } 138 139 ProjNode* proj_true = create_slow_version_of_loop(loop, old_new, unswitch_iff->Opcode(), CloneIncludesStripMined); 140 141 #ifdef ASSERT 142 Node* uniqc = proj_true->unique_ctrl_out(); 143 Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl); 144 Node* predicate = find_predicate(entry); 145 if (predicate != NULL) { 151 if (n != NULL) { 152 predicate = n; 153 entry = skip_loop_predicates(entry); 154 } 155 } 156 if (predicate != NULL && UseProfiledLoopPredicate) { 157 entry = find_predicate(entry); 158 if (entry != NULL) predicate = entry; 159 } 160 if (predicate != NULL) predicate = predicate->in(0); 161 assert(proj_true->is_IfTrue() && 162 (predicate == NULL && uniqc == head && !head->is_strip_mined() || 163 predicate == NULL && uniqc == head->in(LoopNode::EntryControl) && head->is_strip_mined() || 164 predicate != NULL && uniqc == predicate), "by construction"); 165 #endif 166 // Increment unswitch count 167 LoopNode* head_clone = old_new[head->_idx]->as_Loop(); 168 int nct = head->unswitch_count() + 1; 169 head->set_unswitch_count(nct); 170 head_clone->set_unswitch_count(nct); 171 172 // Add test to new "if" outside of loop 173 IfNode* invar_iff = proj_true->in(0)->as_If(); 174 Node* invar_iff_c = invar_iff->in(0); 175 BoolNode* bol = unswitch_iff->in(1)->as_Bool(); 176 invar_iff->set_req(1, bol); 177 invar_iff->_prob = unswitch_iff->_prob; 178 179 ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj(); 180 181 // Hoist invariant casts out of each loop to the appropriate 182 // control projection. 183 184 Node_List worklist; 185 186 for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) { 187 ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj(); 188 // Copy to a worklist for easier manipulation 189 for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) { 190 Node* use = proj->fast_out(j); 191 if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) { 192 worklist.push(use); 193 } 194 } 195 ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj(); 196 while (worklist.size() > 0) { 197 Node* use = worklist.pop(); 198 Node* nuse = use->clone(); 199 nuse->set_req(0, invar_proj); 200 _igvn.replace_input_of(use, 1, nuse); 201 register_new_node(nuse, invar_proj); 202 // Same for the clone 203 Node* use_clone = old_new[use->_idx]; 204 _igvn.replace_input_of(use_clone, 1, nuse); 205 } 206 } 207 208 // Hardwire the control paths in the loops into if(true) and if(false) 209 _igvn.rehash_node_delayed(unswitch_iff); 210 short_circuit_if(unswitch_iff, proj_true); 211 212 IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If(); 213 _igvn.rehash_node_delayed(unswitch_iff_clone); 214 short_circuit_if(unswitch_iff_clone, proj_false); 215 216 // Reoptimize loops 217 loop->record_for_igvn(); 218 for(int i = loop->_body.size() - 1; i >= 0 ; i--) { 219 Node *n = loop->_body[i]; 220 Node *n_clone = old_new[n->_idx]; 221 _igvn._worklist.push(n_clone); 222 } 223 224 #ifndef PRODUCT 225 if (TraceLoopUnswitching) { 226 tty->print_cr("Loop unswitching orig: %d @ %d new: %d @ %d", 227 head->_idx, unswitch_iff->_idx, 228 old_new[head->_idx]->_idx, unswitch_iff_clone->_idx); 229 } 230 #endif 231 232 C->set_major_progress(); 233 } 234 | 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 #include "precompiled.hpp" 26 #include "memory/allocation.inline.hpp" 27 #include "opto/mulnode.hpp" 28 #include "opto/addnode.hpp" 29 #include "opto/connode.hpp" 30 #include "opto/convertnode.hpp" 31 #include "opto/loopnode.hpp" 32 #include "opto/opaquenode.hpp" 33 #include "opto/rootnode.hpp" 34 35 //================= Loop Unswitching ===================== 36 // 37 // orig: transformed: 38 // if (invariant-test) then 39 // predicate predicate 40 // loop loop 41 // stmt1 stmt1 42 // if (invariant-test) then stmt2 43 // stmt2 stmt4 44 // else endloop 45 // stmt3 else 46 // endif predicate [clone] 47 // stmt4 loop [clone] 48 // endloop stmt1 [clone] 49 // stmt3 50 // stmt4 [clone] 51 // endloop 52 // endif 53 // 54 // Note: the "else" clause may be empty 55 56 static bool is_flattened_array_check(Node* iff, PhaseTransform* phase) { 57 if (iff->Opcode() != Op_If) { 58 return false; 59 } 60 Node* bol = iff->in(1); 61 if (!bol->is_Bool() || bol->as_Bool()->_test._test != BoolTest::ne) { 62 return false; 63 } 64 Node* cmp = bol->in(1); 65 if (cmp->Opcode() != Op_CmpI) { 66 return false; 67 } 68 Node* cmp_in1 = cmp->in(1); 69 Node* cmp_in2 = cmp->in(2); 70 if ((unsigned int)cmp_in2->find_int_con(0) != Klass::_lh_array_tag_vt_value) { 71 return false; 72 } 73 if (cmp_in1->Opcode() != Op_RShiftI) { 74 return false; 75 } 76 Node* shift_in1 = cmp_in1->in(1); 77 Node* shift_in2 = cmp_in1->in(2); 78 if ((unsigned int)shift_in2->find_int_con(0) != Klass::_lh_array_tag_shift) { 79 return false; 80 } 81 if (shift_in1->Opcode() != Op_LoadI) { 82 return false; 83 } 84 intptr_t offset; 85 Node* addr = AddPNode::Ideal_base_and_offset(shift_in1->in(MemNode::Address), phase, offset); 86 if (addr == NULL || offset != in_bytes(Klass::layout_helper_offset())) { 87 return false; 88 } 89 if (!phase->type(addr)->isa_klassptr()) { 90 return false; 91 } 92 93 return true; 94 } 95 96 //------------------------------policy_unswitching----------------------------- 97 // Return TRUE or FALSE if the loop should be unswitched 98 // (ie. clone loop with an invariant test that does not exit the loop) 99 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const { 100 if (!LoopUnswitching) { 101 return false; 102 } 103 if (!_head->is_Loop()) { 104 return false; 105 } 106 107 // If nodes are depleted, some transform has miscalculated its needs. 108 assert(!phase->exceeding_node_budget(), "sanity"); 109 110 // check for vectorized loops, any unswitching was already applied 111 if (_head->is_CountedLoop() && _head->as_CountedLoop()->is_unroll_only()) { 112 return false; 113 } 114 115 LoopNode* head = _head->as_Loop(); 116 if (head->unswitch_count() + 1 > head->unswitch_max()) { 117 return false; 118 } 119 120 if (head->is_flattened_arrays()) { 121 return false; 122 } 123 124 Node_List flattened_checks; 125 if (phase->find_unswitching_candidate(this, flattened_checks) == NULL && flattened_checks.size() == 0) { 126 return false; 127 } 128 129 // Too speculative if running low on nodes. 130 return phase->may_require_nodes(est_loop_clone_sz(3, _body.size())); 131 } 132 133 //------------------------------find_unswitching_candidate----------------------------- 134 // Find candidate "if" for unswitching 135 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop, Node_List& flattened_checks) const { 136 137 // Find first invariant test that doesn't exit the loop 138 LoopNode *head = loop->_head->as_Loop(); 139 IfNode* unswitch_iff = NULL; 140 Node* n = head->in(LoopNode::LoopBackControl); 141 while (n != head) { 142 Node* n_dom = idom(n); 143 if (n->is_Region()) { 144 if (n_dom->is_If()) { 145 IfNode* iff = n_dom->as_If(); 146 if (iff->in(1)->is_Bool()) { 147 BoolNode* bol = iff->in(1)->as_Bool(); 148 if (bol->in(1)->is_Cmp()) { 149 // If condition is invariant and not a loop exit, 150 // then found reason to unswitch. 151 if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) { 152 unswitch_iff = iff; 153 } 154 } 155 } 156 } 157 } 158 n = n_dom; 159 } 160 161 if (unswitch_iff == NULL || is_flattened_array_check(unswitch_iff, &_igvn)) { 162 // collect all flattened array checks 163 for (uint i = 0; i < loop->_body.size(); i++) { 164 Node* n = loop->_body.at(i); 165 if (is_flattened_array_check(n, &_igvn) && 166 loop->is_invariant(n->in(1)) && 167 !loop->is_loop_exit(n)) { 168 flattened_checks.push(n); 169 } 170 } 171 unswitch_iff = NULL; 172 } 173 174 return unswitch_iff; 175 } 176 177 //------------------------------do_unswitching----------------------------- 178 // Clone loop with an invariant test (that does not exit) and 179 // insert a clone of the test that selects which version to 180 // execute. 181 void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) { 182 183 // Find first invariant test that doesn't exit the loop 184 LoopNode *head = loop->_head->as_Loop(); 185 186 Node_List flattened_checks; 187 IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop, flattened_checks); 188 assert(unswitch_iff != NULL || flattened_checks.size() > 0, "should be at least one"); 189 if (unswitch_iff == NULL) { 190 unswitch_iff = flattened_checks.at(0)->as_If(); 191 } 192 193 #ifndef PRODUCT 194 if (TraceLoopOpts) { 195 tty->print("Unswitch %d ", head->unswitch_count()+1); 196 loop->dump_head(); 197 } 198 #endif 199 200 // Need to revert back to normal loop 201 if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) { 202 head->as_CountedLoop()->set_normal_loop(); 203 } 204 205 ProjNode* proj_true = create_slow_version_of_loop(loop, old_new, unswitch_iff->Opcode(), CloneIncludesStripMined); 206 207 #ifdef ASSERT 208 Node* uniqc = proj_true->unique_ctrl_out(); 209 Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl); 210 Node* predicate = find_predicate(entry); 211 if (predicate != NULL) { 217 if (n != NULL) { 218 predicate = n; 219 entry = skip_loop_predicates(entry); 220 } 221 } 222 if (predicate != NULL && UseProfiledLoopPredicate) { 223 entry = find_predicate(entry); 224 if (entry != NULL) predicate = entry; 225 } 226 if (predicate != NULL) predicate = predicate->in(0); 227 assert(proj_true->is_IfTrue() && 228 (predicate == NULL && uniqc == head && !head->is_strip_mined() || 229 predicate == NULL && uniqc == head->in(LoopNode::EntryControl) && head->is_strip_mined() || 230 predicate != NULL && uniqc == predicate), "by construction"); 231 #endif 232 // Increment unswitch count 233 LoopNode* head_clone = old_new[head->_idx]->as_Loop(); 234 int nct = head->unswitch_count() + 1; 235 head->set_unswitch_count(nct); 236 head_clone->set_unswitch_count(nct); 237 head_clone->mark_flattened_arrays(); 238 239 // Add test to new "if" outside of loop 240 IfNode* invar_iff = proj_true->in(0)->as_If(); 241 Node* invar_iff_c = invar_iff->in(0); 242 invar_iff->_prob = unswitch_iff->_prob; 243 if (flattened_checks.size() > 0) { 244 // Flattened array checks are used in 245 // Parse::array_store()/Parse::array_load() to switch between a 246 // legacy object array access and a flattened value array 247 // access. We want the performance impact on legacy accesses to be 248 // as small as possible so we make 2 copies of the loops: a fast 249 // one where all accesses are known to be legacy, a slow one where 250 // some accesses are to flattened arrays. Flattened array checks 251 // can be removed from the first one but not from the second one 252 // as it can have a mix of flattened/legacy accesses. 253 BoolNode* bol = unswitch_iff->in(1)->clone()->as_Bool(); 254 register_new_node(bol, invar_iff->in(0)); 255 Node* cmp = bol->in(1)->clone(); 256 register_new_node(cmp, invar_iff->in(0)); 257 bol->set_req(1, cmp); 258 Node* in1 = NULL; 259 for (uint i = 0; i < flattened_checks.size(); i++) { 260 Node* v = flattened_checks.at(i)->in(1)->in(1)->in(1); 261 v = new AndINode(v, _igvn.intcon(Klass::_lh_array_tag_vt_value)); 262 register_new_node(v, invar_iff->in(0)); 263 if (in1 == NULL) { 264 in1 = v; 265 } else { 266 in1 = new OrINode(in1, v); 267 register_new_node(in1, invar_iff->in(0)); 268 } 269 } 270 cmp->set_req(1, in1); 271 invar_iff->set_req(1, bol); 272 } else { 273 BoolNode* bol = unswitch_iff->in(1)->as_Bool(); 274 invar_iff->set_req(1, bol); 275 } 276 277 ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj(); 278 279 // Hoist invariant casts out of each loop to the appropriate 280 // control projection. 281 282 Node_List worklist; 283 284 for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) { 285 ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj(); 286 // Copy to a worklist for easier manipulation 287 for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) { 288 Node* use = proj->fast_out(j); 289 if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) { 290 worklist.push(use); 291 } 292 } 293 ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj(); 294 while (worklist.size() > 0) { 295 Node* use = worklist.pop(); 296 Node* nuse = use->clone(); 297 nuse->set_req(0, invar_proj); 298 _igvn.replace_input_of(use, 1, nuse); 299 register_new_node(nuse, invar_proj); 300 // Same for the clone 301 Node* use_clone = old_new[use->_idx]; 302 _igvn.replace_input_of(use_clone, 1, nuse); 303 } 304 } 305 306 IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If(); 307 if (flattened_checks.size() > 0) { 308 for (uint i = 0; i < flattened_checks.size(); i++) { 309 IfNode* iff = flattened_checks.at(i)->as_If(); 310 _igvn.rehash_node_delayed(iff); 311 short_circuit_if(iff, proj_true); 312 } 313 } else { 314 // Hardwire the control paths in the loops into if(true) and if(false) 315 _igvn.rehash_node_delayed(unswitch_iff); 316 short_circuit_if(unswitch_iff, proj_true); 317 318 _igvn.rehash_node_delayed(unswitch_iff_clone); 319 short_circuit_if(unswitch_iff_clone, proj_false); 320 } 321 322 // Reoptimize loops 323 loop->record_for_igvn(); 324 for(int i = loop->_body.size() - 1; i >= 0 ; i--) { 325 Node *n = loop->_body[i]; 326 Node *n_clone = old_new[n->_idx]; 327 _igvn._worklist.push(n_clone); 328 } 329 330 #ifndef PRODUCT 331 if (TraceLoopUnswitching) { 332 tty->print_cr("Loop unswitching orig: %d @ %d new: %d @ %d", 333 head->_idx, unswitch_iff->_idx, 334 old_new[head->_idx]->_idx, unswitch_iff_clone->_idx); 335 } 336 #endif 337 338 C->set_major_progress(); 339 } 340 |