1 /* 2 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "precompiled.hpp" 25 #include "compiler/compileLog.hpp" 26 #include "libadt/vectset.hpp" 27 #include "memory/allocation.inline.hpp" 28 #include "opto/addnode.hpp" 29 #include "opto/callnode.hpp" 30 #include "opto/castnode.hpp" 31 #include "opto/convertnode.hpp" 32 #include "opto/divnode.hpp" 33 #include "opto/matcher.hpp" 34 #include "opto/memnode.hpp" 35 #include "opto/mulnode.hpp" 36 #include "opto/opcodes.hpp" 37 #include "opto/opaquenode.hpp" 38 #include "opto/superword.hpp" 39 #include "opto/vectornode.hpp" 40 41 // 42 // S U P E R W O R D T R A N S F O R M 43 //============================================================================= 44 45 //------------------------------SuperWord--------------------------- 46 SuperWord::SuperWord(PhaseIdealLoop* phase) : 47 _phase(phase), 48 _igvn(phase->_igvn), 49 _arena(phase->C->comp_arena()), 50 _packset(arena(), 8, 0, NULL), // packs for the current block 51 _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb 52 _block(arena(), 8, 0, NULL), // nodes in current block 53 _data_entry(arena(), 8, 0, NULL), // nodes with all inputs from outside 54 _mem_slice_head(arena(), 8, 0, NULL), // memory slice heads 55 _mem_slice_tail(arena(), 8, 0, NULL), // memory slice tails 56 _node_info(arena(), 8, 0, SWNodeInfo::initial), // info needed per node 57 _clone_map(phase->C->clone_map()), // map of nodes created in cloning 58 _align_to_ref(NULL), // memory reference to align vectors to 59 _disjoint_ptrs(arena(), 8, 0, OrderedPair::initial), // runtime disambiguated pointer pairs 60 _dg(_arena), // dependence graph 61 _visited(arena()), // visited node set 62 _post_visited(arena()), // post visited node set 63 _n_idx_list(arena(), 8), // scratch list of (node,index) pairs 64 _stk(arena(), 8, 0, NULL), // scratch stack of nodes 65 _nlist(arena(), 8, 0, NULL), // scratch list of nodes 66 _lpt(NULL), // loop tree node 67 _lp(NULL), // LoopNode 68 _bb(NULL), // basic block 69 _iv(NULL), // induction var 70 _race_possible(false), // cases where SDMU is true 71 _early_return(true), // analysis evaluations routine 72 _num_work_vecs(0), // amount of vector work we have 73 _num_reductions(0), // amount of reduction work we have 74 _do_vector_loop(phase->C->do_vector_loop()), // whether to do vectorization/simd style 75 _ii_first(-1), // first loop generation index - only if do_vector_loop() 76 _ii_last(-1), // last loop generation index - only if do_vector_loop() 77 _ii_order(arena(), 8, 0, 0) 78 { 79 #ifndef PRODUCT 80 _vector_loop_debug = 0; 81 if (_phase->C->method() != NULL) { 82 _phase->C->method()->has_option_value("VectorizeDebug", _vector_loop_debug); 83 } 84 _CountedLoopReserveKit_debug = 0; 85 if (_phase->C->method() != NULL) { 86 _phase->C->method()->has_option_value("DoReserveCopyInSuperWordDebug", _CountedLoopReserveKit_debug); 87 } 88 #endif 89 } 90 91 //------------------------------transform_loop--------------------------- 92 void SuperWord::transform_loop(IdealLoopTree* lpt, bool do_optimization) { 93 assert(UseSuperWord, "should be"); 94 // Do vectors exist on this architecture? 95 if (Matcher::vector_width_in_bytes(T_BYTE) < 2) return; 96 97 assert(lpt->_head->is_CountedLoop(), "must be"); 98 CountedLoopNode *cl = lpt->_head->as_CountedLoop(); 99 100 if (!cl->is_valid_counted_loop()) return; // skip malformed counted loop 101 102 if (!cl->is_main_loop() ) return; // skip normal, pre, and post loops 103 // Check for no control flow in body (other than exit) 104 Node *cl_exit = cl->loopexit(); 105 if (cl_exit->in(0) != lpt->_head) return; 106 107 // Make sure the are no extra control users of the loop backedge 108 if (cl->back_control()->outcnt() != 1) { 109 return; 110 } 111 112 // We only re-enter slp when we vector mapped a queried loop and we want to 113 // continue unrolling, in this case, slp is not subsequently done. 114 if (cl->do_unroll_only()) return; 115 116 // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit)))) 117 CountedLoopEndNode* pre_end = get_pre_loop_end(cl); 118 if (pre_end == NULL) return; 119 Node *pre_opaq1 = pre_end->limit(); 120 if (pre_opaq1->Opcode() != Op_Opaque1) return; 121 122 init(); // initialize data structures 123 124 set_lpt(lpt); 125 set_lp(cl); 126 127 // For now, define one block which is the entire loop body 128 set_bb(cl); 129 130 if (do_optimization) { 131 assert(_packset.length() == 0, "packset must be empty"); 132 SLP_extract(); 133 } 134 } 135 136 //------------------------------early unrolling analysis------------------------------ 137 void SuperWord::unrolling_analysis(int &local_loop_unroll_factor) { 138 bool is_slp = true; 139 ResourceMark rm; 140 size_t ignored_size = lpt()->_body.size(); 141 int *ignored_loop_nodes = NEW_RESOURCE_ARRAY(int, ignored_size); 142 Node_Stack nstack((int)ignored_size); 143 CountedLoopNode *cl = lpt()->_head->as_CountedLoop(); 144 Node *cl_exit = cl->loopexit(); 145 146 // First clear the entries 147 for (uint i = 0; i < lpt()->_body.size(); i++) { 148 ignored_loop_nodes[i] = -1; 149 } 150 151 int max_vector = Matcher::max_vector_size(T_INT); 152 153 // Process the loop, some/all of the stack entries will not be in order, ergo 154 // need to preprocess the ignored initial state before we process the loop 155 for (uint i = 0; i < lpt()->_body.size(); i++) { 156 Node* n = lpt()->_body.at(i); 157 if (n == cl->incr() || 158 n->is_reduction() || 159 n->is_AddP() || 160 n->is_Cmp() || 161 n->is_IfTrue() || 162 n->is_CountedLoop() || 163 (n == cl_exit)) { 164 ignored_loop_nodes[i] = n->_idx; 165 continue; 166 } 167 168 if (n->is_If()) { 169 IfNode *iff = n->as_If(); 170 if (iff->_fcnt != COUNT_UNKNOWN && iff->_prob != PROB_UNKNOWN) { 171 if (lpt()->is_loop_exit(iff)) { 172 ignored_loop_nodes[i] = n->_idx; 173 continue; 174 } 175 } 176 } 177 178 if (n->is_Phi() && (n->bottom_type() == Type::MEMORY)) { 179 Node* n_tail = n->in(LoopNode::LoopBackControl); 180 if (n_tail != n->in(LoopNode::EntryControl)) { 181 if (!n_tail->is_Mem()) { 182 is_slp = false; 183 break; 184 } 185 } 186 } 187 188 // This must happen after check of phi/if 189 if (n->is_Phi() || n->is_If()) { 190 ignored_loop_nodes[i] = n->_idx; 191 continue; 192 } 193 194 if (n->is_LoadStore() || n->is_MergeMem() || 195 (n->is_Proj() && !n->as_Proj()->is_CFG())) { 196 is_slp = false; 197 break; 198 } 199 200 // Ignore nodes with non-primitive type. 201 BasicType bt; 202 if (n->is_Mem()) { 203 bt = n->as_Mem()->memory_type(); 204 } else { 205 bt = n->bottom_type()->basic_type(); 206 } 207 if (is_java_primitive(bt) == false) { 208 ignored_loop_nodes[i] = n->_idx; 209 continue; 210 } 211 212 if (n->is_Mem()) { 213 MemNode* current = n->as_Mem(); 214 Node* adr = n->in(MemNode::Address); 215 Node* n_ctrl = _phase->get_ctrl(adr); 216 217 // save a queue of post process nodes 218 if (n_ctrl != NULL && lpt()->is_member(_phase->get_loop(n_ctrl))) { 219 // Process the memory expression 220 int stack_idx = 0; 221 bool have_side_effects = true; 222 if (adr->is_AddP() == false) { 223 nstack.push(adr, stack_idx++); 224 } else { 225 // Mark the components of the memory operation in nstack 226 SWPointer p1(current, this, &nstack, true); 227 have_side_effects = p1.node_stack()->is_nonempty(); 228 } 229 230 // Process the pointer stack 231 while (have_side_effects) { 232 Node* pointer_node = nstack.node(); 233 for (uint j = 0; j < lpt()->_body.size(); j++) { 234 Node* cur_node = lpt()->_body.at(j); 235 if (cur_node == pointer_node) { 236 ignored_loop_nodes[j] = cur_node->_idx; 237 break; 238 } 239 } 240 nstack.pop(); 241 have_side_effects = nstack.is_nonempty(); 242 } 243 } 244 } 245 } 246 247 if (is_slp) { 248 // Now we try to find the maximum supported consistent vector which the machine 249 // description can use 250 for (uint i = 0; i < lpt()->_body.size(); i++) { 251 if (ignored_loop_nodes[i] != -1) continue; 252 253 BasicType bt; 254 Node* n = lpt()->_body.at(i); 255 if (n->is_Mem()) { 256 bt = n->as_Mem()->memory_type(); 257 } else { 258 bt = n->bottom_type()->basic_type(); 259 } 260 if (is_java_primitive(bt) == false) continue; 261 262 int cur_max_vector = Matcher::max_vector_size(bt); 263 264 // If a max vector exists which is not larger than _local_loop_unroll_factor 265 // stop looking, we already have the max vector to map to. 266 if (cur_max_vector < local_loop_unroll_factor) { 267 is_slp = false; 268 NOT_PRODUCT(if (TraceSuperWordLoopUnrollAnalysis) tty->print_cr("slp analysis fails: unroll limit greater than max vector\n")); 269 break; 270 } 271 272 // Map the maximal common vector 273 if (VectorNode::implemented(n->Opcode(), cur_max_vector, bt)) { 274 if (cur_max_vector < max_vector) { 275 max_vector = cur_max_vector; 276 } 277 } 278 } 279 if (is_slp) { 280 local_loop_unroll_factor = max_vector; 281 cl->mark_passed_slp(); 282 } 283 cl->mark_was_slp(); 284 cl->set_slp_max_unroll(local_loop_unroll_factor); 285 } 286 } 287 288 //------------------------------SLP_extract--------------------------- 289 // Extract the superword level parallelism 290 // 291 // 1) A reverse post-order of nodes in the block is constructed. By scanning 292 // this list from first to last, all definitions are visited before their uses. 293 // 294 // 2) A point-to-point dependence graph is constructed between memory references. 295 // This simplies the upcoming "independence" checker. 296 // 297 // 3) The maximum depth in the node graph from the beginning of the block 298 // to each node is computed. This is used to prune the graph search 299 // in the independence checker. 300 // 301 // 4) For integer types, the necessary bit width is propagated backwards 302 // from stores to allow packed operations on byte, char, and short 303 // integers. This reverses the promotion to type "int" that javac 304 // did for operations like: char c1,c2,c3; c1 = c2 + c3. 305 // 306 // 5) One of the memory references is picked to be an aligned vector reference. 307 // The pre-loop trip count is adjusted to align this reference in the 308 // unrolled body. 309 // 310 // 6) The initial set of pack pairs is seeded with memory references. 311 // 312 // 7) The set of pack pairs is extended by following use->def and def->use links. 313 // 314 // 8) The pairs are combined into vector sized packs. 315 // 316 // 9) Reorder the memory slices to co-locate members of the memory packs. 317 // 318 // 10) Generate ideal vector nodes for the final set of packs and where necessary, 319 // inserting scalar promotion, vector creation from multiple scalars, and 320 // extraction of scalar values from vectors. 321 // 322 void SuperWord::SLP_extract() { 323 324 #ifndef PRODUCT 325 if (_do_vector_loop && TraceSuperWord) { 326 tty->print("SuperWord::SLP_extract\n"); 327 tty->print("input loop\n"); 328 _lpt->dump_head(); 329 _lpt->dump(); 330 for (uint i = 0; i < _lpt->_body.size(); i++) { 331 _lpt->_body.at(i)->dump(); 332 } 333 } 334 #endif 335 // Ready the block 336 if (!construct_bb()) { 337 return; // Exit if no interesting nodes or complex graph. 338 } 339 // build _dg, _disjoint_ptrs 340 dependence_graph(); 341 342 // compute function depth(Node*) 343 compute_max_depth(); 344 345 if (_do_vector_loop) { 346 if (mark_generations() != -1) { 347 hoist_loads_in_graph(); // this only rebuild the graph; all basic structs need rebuild explicitly 348 349 if (!construct_bb()) { 350 return; // Exit if no interesting nodes or complex graph. 351 } 352 dependence_graph(); 353 compute_max_depth(); 354 } 355 356 #ifndef PRODUCT 357 if (TraceSuperWord) { 358 tty->print_cr("\nSuperWord::_do_vector_loop: graph after hoist_loads_in_graph"); 359 _lpt->dump_head(); 360 for (int j = 0; j < _block.length(); j++) { 361 Node* n = _block.at(j); 362 int d = depth(n); 363 for (int i = 0; i < d; i++) tty->print("%s", " "); 364 tty->print("%d :", d); 365 n->dump(); 366 } 367 } 368 #endif 369 } 370 371 compute_vector_element_type(); 372 373 // Attempt vectorization 374 375 find_adjacent_refs(); 376 377 extend_packlist(); 378 379 if (_do_vector_loop) { 380 if (_packset.length() == 0) { 381 #ifndef PRODUCT 382 if (TraceSuperWord) { 383 tty->print_cr("\nSuperWord::_do_vector_loop DFA could not build packset, now trying to build anyway"); 384 } 385 #endif 386 pack_parallel(); 387 } 388 } 389 390 combine_packs(); 391 392 construct_my_pack_map(); 393 394 filter_packs(); 395 396 schedule(); 397 398 output(); 399 } 400 401 //------------------------------find_adjacent_refs--------------------------- 402 // Find the adjacent memory references and create pack pairs for them. 403 // This is the initial set of packs that will then be extended by 404 // following use->def and def->use links. The align positions are 405 // assigned relative to the reference "align_to_ref" 406 void SuperWord::find_adjacent_refs() { 407 // Get list of memory operations 408 Node_List memops; 409 for (int i = 0; i < _block.length(); i++) { 410 Node* n = _block.at(i); 411 if (n->is_Mem() && !n->is_LoadStore() && in_bb(n) && 412 is_java_primitive(n->as_Mem()->memory_type())) { 413 int align = memory_alignment(n->as_Mem(), 0); 414 if (align != bottom_align) { 415 memops.push(n); 416 } 417 } 418 } 419 420 Node_List align_to_refs; 421 int best_iv_adjustment = 0; 422 MemNode* best_align_to_mem_ref = NULL; 423 424 while (memops.size() != 0) { 425 // Find a memory reference to align to. 426 MemNode* mem_ref = find_align_to_ref(memops); 427 if (mem_ref == NULL) break; 428 align_to_refs.push(mem_ref); 429 int iv_adjustment = get_iv_adjustment(mem_ref); 430 431 if (best_align_to_mem_ref == NULL) { 432 // Set memory reference which is the best from all memory operations 433 // to be used for alignment. The pre-loop trip count is modified to align 434 // this reference to a vector-aligned address. 435 best_align_to_mem_ref = mem_ref; 436 best_iv_adjustment = iv_adjustment; 437 NOT_PRODUCT(find_adjacent_refs_trace_1(best_align_to_mem_ref, best_iv_adjustment);) 438 } 439 440 SWPointer align_to_ref_p(mem_ref, this, NULL, false); 441 // Set alignment relative to "align_to_ref" for all related memory operations. 442 for (int i = memops.size() - 1; i >= 0; i--) { 443 MemNode* s = memops.at(i)->as_Mem(); 444 if (isomorphic(s, mem_ref) && 445 (!_do_vector_loop || same_origin_idx(s, mem_ref))) { 446 SWPointer p2(s, this, NULL, false); 447 if (p2.comparable(align_to_ref_p)) { 448 int align = memory_alignment(s, iv_adjustment); 449 set_alignment(s, align); 450 } 451 } 452 } 453 454 // Create initial pack pairs of memory operations for which 455 // alignment is set and vectors will be aligned. 456 bool create_pack = true; 457 if (memory_alignment(mem_ref, best_iv_adjustment) == 0 || _do_vector_loop) { 458 if (!Matcher::misaligned_vectors_ok()) { 459 int vw = vector_width(mem_ref); 460 int vw_best = vector_width(best_align_to_mem_ref); 461 if (vw > vw_best) { 462 // Do not vectorize a memory access with more elements per vector 463 // if unaligned memory access is not allowed because number of 464 // iterations in pre-loop will be not enough to align it. 465 create_pack = false; 466 } else { 467 SWPointer p2(best_align_to_mem_ref, this, NULL, false); 468 if (align_to_ref_p.invar() != p2.invar()) { 469 // Do not vectorize memory accesses with different invariants 470 // if unaligned memory accesses are not allowed. 471 create_pack = false; 472 } 473 } 474 } 475 } else { 476 if (same_velt_type(mem_ref, best_align_to_mem_ref)) { 477 // Can't allow vectorization of unaligned memory accesses with the 478 // same type since it could be overlapped accesses to the same array. 479 create_pack = false; 480 } else { 481 // Allow independent (different type) unaligned memory operations 482 // if HW supports them. 483 if (!Matcher::misaligned_vectors_ok()) { 484 create_pack = false; 485 } else { 486 // Check if packs of the same memory type but 487 // with a different alignment were created before. 488 for (uint i = 0; i < align_to_refs.size(); i++) { 489 MemNode* mr = align_to_refs.at(i)->as_Mem(); 490 if (same_velt_type(mr, mem_ref) && 491 memory_alignment(mr, iv_adjustment) != 0) 492 create_pack = false; 493 } 494 } 495 } 496 } 497 if (create_pack) { 498 for (uint i = 0; i < memops.size(); i++) { 499 Node* s1 = memops.at(i); 500 int align = alignment(s1); 501 if (align == top_align) continue; 502 for (uint j = 0; j < memops.size(); j++) { 503 Node* s2 = memops.at(j); 504 if (alignment(s2) == top_align) continue; 505 if (s1 != s2 && are_adjacent_refs(s1, s2)) { 506 if (stmts_can_pack(s1, s2, align)) { 507 Node_List* pair = new Node_List(); 508 pair->push(s1); 509 pair->push(s2); 510 if (!_do_vector_loop || same_origin_idx(s1, s2)) { 511 _packset.append(pair); 512 } 513 } 514 } 515 } 516 } 517 } else { // Don't create unaligned pack 518 // First, remove remaining memory ops of the same type from the list. 519 for (int i = memops.size() - 1; i >= 0; i--) { 520 MemNode* s = memops.at(i)->as_Mem(); 521 if (same_velt_type(s, mem_ref)) { 522 memops.remove(i); 523 } 524 } 525 526 // Second, remove already constructed packs of the same type. 527 for (int i = _packset.length() - 1; i >= 0; i--) { 528 Node_List* p = _packset.at(i); 529 MemNode* s = p->at(0)->as_Mem(); 530 if (same_velt_type(s, mem_ref)) { 531 remove_pack_at(i); 532 } 533 } 534 535 // If needed find the best memory reference for loop alignment again. 536 if (same_velt_type(mem_ref, best_align_to_mem_ref)) { 537 // Put memory ops from remaining packs back on memops list for 538 // the best alignment search. 539 uint orig_msize = memops.size(); 540 for (int i = 0; i < _packset.length(); i++) { 541 Node_List* p = _packset.at(i); 542 MemNode* s = p->at(0)->as_Mem(); 543 assert(!same_velt_type(s, mem_ref), "sanity"); 544 memops.push(s); 545 } 546 MemNode* best_align_to_mem_ref = find_align_to_ref(memops); 547 if (best_align_to_mem_ref == NULL) { 548 NOT_PRODUCT(if (TraceSuperWord) tty->print_cr("SuperWord::find_adjacent_refs(): best_align_to_mem_ref == NULL");) 549 break; 550 } 551 best_iv_adjustment = get_iv_adjustment(best_align_to_mem_ref); 552 NOT_PRODUCT(find_adjacent_refs_trace_1(best_align_to_mem_ref, best_iv_adjustment);) 553 // Restore list. 554 while (memops.size() > orig_msize) 555 (void)memops.pop(); 556 } 557 } // unaligned memory accesses 558 559 // Remove used mem nodes. 560 for (int i = memops.size() - 1; i >= 0; i--) { 561 MemNode* m = memops.at(i)->as_Mem(); 562 if (alignment(m) != top_align) { 563 memops.remove(i); 564 } 565 } 566 567 } // while (memops.size() != 0 568 set_align_to_ref(best_align_to_mem_ref); 569 570 #ifndef PRODUCT 571 if (TraceSuperWord) { 572 tty->print_cr("\nAfter find_adjacent_refs"); 573 print_packset(); 574 } 575 #endif 576 } 577 578 #ifndef PRODUCT 579 void SuperWord::find_adjacent_refs_trace_1(Node* best_align_to_mem_ref, int best_iv_adjustment) { 580 if (is_trace_adjacent()) { 581 tty->print("SuperWord::find_adjacent_refs best_align_to_mem_ref = %d, best_iv_adjustment = %d", 582 best_align_to_mem_ref->_idx, best_iv_adjustment); 583 best_align_to_mem_ref->dump(); 584 } 585 } 586 #endif 587 588 //------------------------------find_align_to_ref--------------------------- 589 // Find a memory reference to align the loop induction variable to. 590 // Looks first at stores then at loads, looking for a memory reference 591 // with the largest number of references similar to it. 592 MemNode* SuperWord::find_align_to_ref(Node_List &memops) { 593 GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0); 594 595 // Count number of comparable memory ops 596 for (uint i = 0; i < memops.size(); i++) { 597 MemNode* s1 = memops.at(i)->as_Mem(); 598 SWPointer p1(s1, this, NULL, false); 599 // Discard if pre loop can't align this reference 600 if (!ref_is_alignable(p1)) { 601 *cmp_ct.adr_at(i) = 0; 602 continue; 603 } 604 for (uint j = i+1; j < memops.size(); j++) { 605 MemNode* s2 = memops.at(j)->as_Mem(); 606 if (isomorphic(s1, s2)) { 607 SWPointer p2(s2, this, NULL, false); 608 if (p1.comparable(p2)) { 609 (*cmp_ct.adr_at(i))++; 610 (*cmp_ct.adr_at(j))++; 611 } 612 } 613 } 614 } 615 616 // Find Store (or Load) with the greatest number of "comparable" references, 617 // biggest vector size, smallest data size and smallest iv offset. 618 int max_ct = 0; 619 int max_vw = 0; 620 int max_idx = -1; 621 int min_size = max_jint; 622 int min_iv_offset = max_jint; 623 for (uint j = 0; j < memops.size(); j++) { 624 MemNode* s = memops.at(j)->as_Mem(); 625 if (s->is_Store()) { 626 int vw = vector_width_in_bytes(s); 627 assert(vw > 1, "sanity"); 628 SWPointer p(s, this, NULL, false); 629 if (cmp_ct.at(j) > max_ct || 630 cmp_ct.at(j) == max_ct && 631 (vw > max_vw || 632 vw == max_vw && 633 (data_size(s) < min_size || 634 data_size(s) == min_size && 635 (p.offset_in_bytes() < min_iv_offset)))) { 636 max_ct = cmp_ct.at(j); 637 max_vw = vw; 638 max_idx = j; 639 min_size = data_size(s); 640 min_iv_offset = p.offset_in_bytes(); 641 } 642 } 643 } 644 // If no stores, look at loads 645 if (max_ct == 0) { 646 for (uint j = 0; j < memops.size(); j++) { 647 MemNode* s = memops.at(j)->as_Mem(); 648 if (s->is_Load()) { 649 int vw = vector_width_in_bytes(s); 650 assert(vw > 1, "sanity"); 651 SWPointer p(s, this, NULL, false); 652 if (cmp_ct.at(j) > max_ct || 653 cmp_ct.at(j) == max_ct && 654 (vw > max_vw || 655 vw == max_vw && 656 (data_size(s) < min_size || 657 data_size(s) == min_size && 658 (p.offset_in_bytes() < min_iv_offset)))) { 659 max_ct = cmp_ct.at(j); 660 max_vw = vw; 661 max_idx = j; 662 min_size = data_size(s); 663 min_iv_offset = p.offset_in_bytes(); 664 } 665 } 666 } 667 } 668 669 #ifdef ASSERT 670 if (TraceSuperWord && Verbose) { 671 tty->print_cr("\nVector memops after find_align_to_ref"); 672 for (uint i = 0; i < memops.size(); i++) { 673 MemNode* s = memops.at(i)->as_Mem(); 674 s->dump(); 675 } 676 } 677 #endif 678 679 if (max_ct > 0) { 680 #ifdef ASSERT 681 if (TraceSuperWord) { 682 tty->print("\nVector align to node: "); 683 memops.at(max_idx)->as_Mem()->dump(); 684 } 685 #endif 686 return memops.at(max_idx)->as_Mem(); 687 } 688 return NULL; 689 } 690 691 //------------------------------ref_is_alignable--------------------------- 692 // Can the preloop align the reference to position zero in the vector? 693 bool SuperWord::ref_is_alignable(SWPointer& p) { 694 if (!p.has_iv()) { 695 return true; // no induction variable 696 } 697 CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop()); 698 assert(pre_end != NULL, "we must have a correct pre-loop"); 699 assert(pre_end->stride_is_con(), "pre loop stride is constant"); 700 int preloop_stride = pre_end->stride_con(); 701 702 int span = preloop_stride * p.scale_in_bytes(); 703 int mem_size = p.memory_size(); 704 int offset = p.offset_in_bytes(); 705 // Stride one accesses are alignable if offset is aligned to memory operation size. 706 // Offset can be unaligned when UseUnalignedAccesses is used. 707 if (ABS(span) == mem_size && (ABS(offset) % mem_size) == 0) { 708 return true; 709 } 710 // If the initial offset from start of the object is computable, 711 // check if the pre-loop can align the final offset accordingly. 712 // 713 // In other words: Can we find an i such that the offset 714 // after i pre-loop iterations is aligned to vw? 715 // (init_offset + pre_loop) % vw == 0 (1) 716 // where 717 // pre_loop = i * span 718 // is the number of bytes added to the offset by i pre-loop iterations. 719 // 720 // For this to hold we need pre_loop to increase init_offset by 721 // pre_loop = vw - (init_offset % vw) 722 // 723 // This is only possible if pre_loop is divisible by span because each 724 // pre-loop iteration increases the initial offset by 'span' bytes: 725 // (vw - (init_offset % vw)) % span == 0 726 // 727 int vw = vector_width_in_bytes(p.mem()); 728 assert(vw > 1, "sanity"); 729 Node* init_nd = pre_end->init_trip(); 730 if (init_nd->is_Con() && p.invar() == NULL) { 731 int init = init_nd->bottom_type()->is_int()->get_con(); 732 int init_offset = init * p.scale_in_bytes() + offset; 733 assert(init_offset >= 0, "positive offset from object start"); 734 if (vw % span == 0) { 735 // If vm is a multiple of span, we use formula (1). 736 if (span > 0) { 737 return (vw - (init_offset % vw)) % span == 0; 738 } else { 739 assert(span < 0, "nonzero stride * scale"); 740 return (init_offset % vw) % -span == 0; 741 } 742 } else if (span % vw == 0) { 743 // If span is a multiple of vw, we can simplify formula (1) to: 744 // (init_offset + i * span) % vw == 0 745 // => 746 // (init_offset % vw) + ((i * span) % vw) == 0 747 // => 748 // init_offset % vw == 0 749 // 750 // Because we add a multiple of vw to the initial offset, the final 751 // offset is a multiple of vw if and only if init_offset is a multiple. 752 // 753 return (init_offset % vw) == 0; 754 } 755 } 756 return false; 757 } 758 759 //---------------------------get_iv_adjustment--------------------------- 760 // Calculate loop's iv adjustment for this memory ops. 761 int SuperWord::get_iv_adjustment(MemNode* mem_ref) { 762 SWPointer align_to_ref_p(mem_ref, this, NULL, false); 763 int offset = align_to_ref_p.offset_in_bytes(); 764 int scale = align_to_ref_p.scale_in_bytes(); 765 int elt_size = align_to_ref_p.memory_size(); 766 int vw = vector_width_in_bytes(mem_ref); 767 assert(vw > 1, "sanity"); 768 int iv_adjustment; 769 if (scale != 0) { 770 int stride_sign = (scale * iv_stride()) > 0 ? 1 : -1; 771 // At least one iteration is executed in pre-loop by default. As result 772 // several iterations are needed to align memory operations in main-loop even 773 // if offset is 0. 774 int iv_adjustment_in_bytes = (stride_sign * vw - (offset % vw)); 775 assert(((ABS(iv_adjustment_in_bytes) % elt_size) == 0), 776 err_msg_res("(%d) should be divisible by (%d)", iv_adjustment_in_bytes, elt_size)); 777 iv_adjustment = iv_adjustment_in_bytes/elt_size; 778 } else { 779 // This memory op is not dependent on iv (scale == 0) 780 iv_adjustment = 0; 781 } 782 783 #ifndef PRODUCT 784 if (TraceSuperWord) { 785 tty->print("SuperWord::get_iv_adjustment: n = %d, noffset = %d iv_adjust = %d elt_size = %d scale = %d iv_stride = %d vect_size %d: ", 786 mem_ref->_idx, offset, iv_adjustment, elt_size, scale, iv_stride(), vw); 787 mem_ref->dump(); 788 } 789 #endif 790 return iv_adjustment; 791 } 792 793 //---------------------------dependence_graph--------------------------- 794 // Construct dependency graph. 795 // Add dependence edges to load/store nodes for memory dependence 796 // A.out()->DependNode.in(1) and DependNode.out()->B.prec(x) 797 void SuperWord::dependence_graph() { 798 // First, assign a dependence node to each memory node 799 for (int i = 0; i < _block.length(); i++ ) { 800 Node *n = _block.at(i); 801 if (n->is_Mem() || n->is_Phi() && n->bottom_type() == Type::MEMORY) { 802 _dg.make_node(n); 803 } 804 } 805 806 // For each memory slice, create the dependences 807 for (int i = 0; i < _mem_slice_head.length(); i++) { 808 Node* n = _mem_slice_head.at(i); 809 Node* n_tail = _mem_slice_tail.at(i); 810 811 // Get slice in predecessor order (last is first) 812 mem_slice_preds(n_tail, n, _nlist); 813 814 #ifndef PRODUCT 815 if(TraceSuperWord && Verbose) { 816 tty->print_cr("SuperWord::dependence_graph: built a new mem slice"); 817 for (int j = _nlist.length() - 1; j >= 0 ; j--) { 818 _nlist.at(j)->dump(); 819 } 820 } 821 #endif 822 // Make the slice dependent on the root 823 DepMem* slice = _dg.dep(n); 824 _dg.make_edge(_dg.root(), slice); 825 826 // Create a sink for the slice 827 DepMem* slice_sink = _dg.make_node(NULL); 828 _dg.make_edge(slice_sink, _dg.tail()); 829 830 // Now visit each pair of memory ops, creating the edges 831 for (int j = _nlist.length() - 1; j >= 0 ; j--) { 832 Node* s1 = _nlist.at(j); 833 834 // If no dependency yet, use slice 835 if (_dg.dep(s1)->in_cnt() == 0) { 836 _dg.make_edge(slice, s1); 837 } 838 SWPointer p1(s1->as_Mem(), this, NULL, false); 839 bool sink_dependent = true; 840 for (int k = j - 1; k >= 0; k--) { 841 Node* s2 = _nlist.at(k); 842 if (s1->is_Load() && s2->is_Load()) 843 continue; 844 SWPointer p2(s2->as_Mem(), this, NULL, false); 845 846 int cmp = p1.cmp(p2); 847 if (SuperWordRTDepCheck && 848 p1.base() != p2.base() && p1.valid() && p2.valid()) { 849 // Create a runtime check to disambiguate 850 OrderedPair pp(p1.base(), p2.base()); 851 _disjoint_ptrs.append_if_missing(pp); 852 } else if (!SWPointer::not_equal(cmp)) { 853 // Possibly same address 854 _dg.make_edge(s1, s2); 855 sink_dependent = false; 856 } 857 } 858 if (sink_dependent) { 859 _dg.make_edge(s1, slice_sink); 860 } 861 } 862 #ifndef PRODUCT 863 if (TraceSuperWord) { 864 tty->print_cr("\nDependence graph for slice: %d", n->_idx); 865 for (int q = 0; q < _nlist.length(); q++) { 866 _dg.print(_nlist.at(q)); 867 } 868 tty->cr(); 869 } 870 #endif 871 _nlist.clear(); 872 } 873 874 #ifndef PRODUCT 875 if (TraceSuperWord) { 876 tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE"); 877 for (int r = 0; r < _disjoint_ptrs.length(); r++) { 878 _disjoint_ptrs.at(r).print(); 879 tty->cr(); 880 } 881 tty->cr(); 882 } 883 #endif 884 } 885 886 //---------------------------mem_slice_preds--------------------------- 887 // Return a memory slice (node list) in predecessor order starting at "start" 888 void SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) { 889 assert(preds.length() == 0, "start empty"); 890 Node* n = start; 891 Node* prev = NULL; 892 while (true) { 893 NOT_PRODUCT( if(is_trace_mem_slice()) tty->print_cr("SuperWord::mem_slice_preds: n %d", n->_idx);) 894 assert(in_bb(n), "must be in block"); 895 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 896 Node* out = n->fast_out(i); 897 if (out->is_Load()) { 898 if (in_bb(out)) { 899 preds.push(out); 900 NOT_PRODUCT(if (TraceSuperWord && Verbose) tty->print_cr("SuperWord::mem_slice_preds: added pred(%d)", out->_idx);) 901 } 902 } else { 903 // FIXME 904 if (out->is_MergeMem() && !in_bb(out)) { 905 // Either unrolling is causing a memory edge not to disappear, 906 // or need to run igvn.optimize() again before SLP 907 } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) { 908 // Ditto. Not sure what else to check further. 909 } else if (out->Opcode() == Op_StoreCM && out->in(MemNode::OopStore) == n) { 910 // StoreCM has an input edge used as a precedence edge. 911 // Maybe an issue when oop stores are vectorized. 912 } else { 913 assert(out == prev || prev == NULL, "no branches off of store slice"); 914 } 915 }//else 916 }//for 917 if (n == stop) break; 918 preds.push(n); 919 NOT_PRODUCT(if (TraceSuperWord && Verbose) tty->print_cr("SuperWord::mem_slice_preds: added pred(%d)", n->_idx);) 920 prev = n; 921 assert(n->is_Mem(), err_msg_res("unexpected node %s", n->Name())); 922 n = n->in(MemNode::Memory); 923 } 924 } 925 926 //------------------------------stmts_can_pack--------------------------- 927 // Can s1 and s2 be in a pack with s1 immediately preceding s2 and 928 // s1 aligned at "align" 929 bool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) { 930 931 // Do not use superword for non-primitives 932 BasicType bt1 = velt_basic_type(s1); 933 BasicType bt2 = velt_basic_type(s2); 934 if(!is_java_primitive(bt1) || !is_java_primitive(bt2)) 935 return false; 936 if (Matcher::max_vector_size(bt1) < 2) { 937 return false; // No vectors for this type 938 } 939 940 if (isomorphic(s1, s2)) { 941 if (independent(s1, s2) || reduction(s1, s2)) { 942 if (!exists_at(s1, 0) && !exists_at(s2, 1)) { 943 if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) { 944 int s1_align = alignment(s1); 945 int s2_align = alignment(s2); 946 if (s1_align == top_align || s1_align == align) { 947 if (s2_align == top_align || s2_align == align + data_size(s1)) { 948 return true; 949 } 950 } 951 } 952 } 953 } 954 } 955 return false; 956 } 957 958 //------------------------------exists_at--------------------------- 959 // Does s exist in a pack at position pos? 960 bool SuperWord::exists_at(Node* s, uint pos) { 961 for (int i = 0; i < _packset.length(); i++) { 962 Node_List* p = _packset.at(i); 963 if (p->at(pos) == s) { 964 return true; 965 } 966 } 967 return false; 968 } 969 970 //------------------------------are_adjacent_refs--------------------------- 971 // Is s1 immediately before s2 in memory? 972 bool SuperWord::are_adjacent_refs(Node* s1, Node* s2) { 973 if (!s1->is_Mem() || !s2->is_Mem()) return false; 974 if (!in_bb(s1) || !in_bb(s2)) return false; 975 976 // Do not use superword for non-primitives 977 if (!is_java_primitive(s1->as_Mem()->memory_type()) || 978 !is_java_primitive(s2->as_Mem()->memory_type())) { 979 return false; 980 } 981 982 // FIXME - co_locate_pack fails on Stores in different mem-slices, so 983 // only pack memops that are in the same alias set until that's fixed. 984 if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) != 985 _phase->C->get_alias_index(s2->as_Mem()->adr_type())) 986 return false; 987 SWPointer p1(s1->as_Mem(), this, NULL, false); 988 SWPointer p2(s2->as_Mem(), this, NULL, false); 989 if (p1.base() != p2.base() || !p1.comparable(p2)) return false; 990 int diff = p2.offset_in_bytes() - p1.offset_in_bytes(); 991 return diff == data_size(s1); 992 } 993 994 //------------------------------isomorphic--------------------------- 995 // Are s1 and s2 similar? 996 bool SuperWord::isomorphic(Node* s1, Node* s2) { 997 if (s1->Opcode() != s2->Opcode()) return false; 998 if (s1->req() != s2->req()) return false; 999 if (s1->in(0) != s2->in(0)) return false; 1000 if (!same_velt_type(s1, s2)) return false; 1001 return true; 1002 } 1003 1004 //------------------------------independent--------------------------- 1005 // Is there no data path from s1 to s2 or s2 to s1? 1006 bool SuperWord::independent(Node* s1, Node* s2) { 1007 // assert(s1->Opcode() == s2->Opcode(), "check isomorphic first"); 1008 int d1 = depth(s1); 1009 int d2 = depth(s2); 1010 if (d1 == d2) return s1 != s2; 1011 Node* deep = d1 > d2 ? s1 : s2; 1012 Node* shallow = d1 > d2 ? s2 : s1; 1013 1014 visited_clear(); 1015 1016 return independent_path(shallow, deep); 1017 } 1018 1019 //------------------------------reduction--------------------------- 1020 // Is there a data path between s1 and s2 and the nodes reductions? 1021 bool SuperWord::reduction(Node* s1, Node* s2) { 1022 bool retValue = false; 1023 int d1 = depth(s1); 1024 int d2 = depth(s2); 1025 if (d1 + 1 == d2) { 1026 if (s1->is_reduction() && s2->is_reduction()) { 1027 // This is an ordered set, so s1 should define s2 1028 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) { 1029 Node* t1 = s1->fast_out(i); 1030 if (t1 == s2) { 1031 // both nodes are reductions and connected 1032 retValue = true; 1033 } 1034 } 1035 } 1036 } 1037 1038 return retValue; 1039 } 1040 1041 //------------------------------independent_path------------------------------ 1042 // Helper for independent 1043 bool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) { 1044 if (dp >= 1000) return false; // stop deep recursion 1045 visited_set(deep); 1046 int shal_depth = depth(shallow); 1047 assert(shal_depth <= depth(deep), "must be"); 1048 for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) { 1049 Node* pred = preds.current(); 1050 if (in_bb(pred) && !visited_test(pred)) { 1051 if (shallow == pred) { 1052 return false; 1053 } 1054 if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) { 1055 return false; 1056 } 1057 } 1058 } 1059 return true; 1060 } 1061 1062 //------------------------------set_alignment--------------------------- 1063 void SuperWord::set_alignment(Node* s1, Node* s2, int align) { 1064 set_alignment(s1, align); 1065 if (align == top_align || align == bottom_align) { 1066 set_alignment(s2, align); 1067 } else { 1068 set_alignment(s2, align + data_size(s1)); 1069 } 1070 } 1071 1072 //------------------------------data_size--------------------------- 1073 int SuperWord::data_size(Node* s) { 1074 int bsize = type2aelembytes(velt_basic_type(s)); 1075 assert(bsize != 0, "valid size"); 1076 return bsize; 1077 } 1078 1079 //------------------------------extend_packlist--------------------------- 1080 // Extend packset by following use->def and def->use links from pack members. 1081 void SuperWord::extend_packlist() { 1082 bool changed; 1083 do { 1084 packset_sort(_packset.length()); 1085 changed = false; 1086 for (int i = 0; i < _packset.length(); i++) { 1087 Node_List* p = _packset.at(i); 1088 changed |= follow_use_defs(p); 1089 changed |= follow_def_uses(p); 1090 } 1091 } while (changed); 1092 1093 if (_race_possible) { 1094 for (int i = 0; i < _packset.length(); i++) { 1095 Node_List* p = _packset.at(i); 1096 order_def_uses(p); 1097 } 1098 } 1099 1100 #ifndef PRODUCT 1101 if (TraceSuperWord) { 1102 tty->print_cr("\nAfter extend_packlist"); 1103 print_packset(); 1104 } 1105 #endif 1106 } 1107 1108 //------------------------------follow_use_defs--------------------------- 1109 // Extend the packset by visiting operand definitions of nodes in pack p 1110 bool SuperWord::follow_use_defs(Node_List* p) { 1111 assert(p->size() == 2, "just checking"); 1112 Node* s1 = p->at(0); 1113 Node* s2 = p->at(1); 1114 assert(s1->req() == s2->req(), "just checking"); 1115 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking"); 1116 1117 if (s1->is_Load()) return false; 1118 1119 int align = alignment(s1); 1120 bool changed = false; 1121 int start = s1->is_Store() ? MemNode::ValueIn : 1; 1122 int end = s1->is_Store() ? MemNode::ValueIn+1 : s1->req(); 1123 for (int j = start; j < end; j++) { 1124 Node* t1 = s1->in(j); 1125 Node* t2 = s2->in(j); 1126 if (!in_bb(t1) || !in_bb(t2)) 1127 continue; 1128 if (stmts_can_pack(t1, t2, align)) { 1129 if (est_savings(t1, t2) >= 0) { 1130 Node_List* pair = new Node_List(); 1131 pair->push(t1); 1132 pair->push(t2); 1133 _packset.append(pair); 1134 set_alignment(t1, t2, align); 1135 changed = true; 1136 } 1137 } 1138 } 1139 return changed; 1140 } 1141 1142 //------------------------------follow_def_uses--------------------------- 1143 // Extend the packset by visiting uses of nodes in pack p 1144 bool SuperWord::follow_def_uses(Node_List* p) { 1145 bool changed = false; 1146 Node* s1 = p->at(0); 1147 Node* s2 = p->at(1); 1148 assert(p->size() == 2, "just checking"); 1149 assert(s1->req() == s2->req(), "just checking"); 1150 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking"); 1151 1152 if (s1->is_Store()) return false; 1153 1154 int align = alignment(s1); 1155 int savings = -1; 1156 int num_s1_uses = 0; 1157 Node* u1 = NULL; 1158 Node* u2 = NULL; 1159 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) { 1160 Node* t1 = s1->fast_out(i); 1161 num_s1_uses++; 1162 if (!in_bb(t1)) continue; 1163 for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) { 1164 Node* t2 = s2->fast_out(j); 1165 if (!in_bb(t2)) continue; 1166 if (!opnd_positions_match(s1, t1, s2, t2)) 1167 continue; 1168 if (stmts_can_pack(t1, t2, align)) { 1169 int my_savings = est_savings(t1, t2); 1170 if (my_savings > savings) { 1171 savings = my_savings; 1172 u1 = t1; 1173 u2 = t2; 1174 } 1175 } 1176 } 1177 } 1178 if (num_s1_uses > 1) { 1179 _race_possible = true; 1180 } 1181 if (savings >= 0) { 1182 Node_List* pair = new Node_List(); 1183 pair->push(u1); 1184 pair->push(u2); 1185 _packset.append(pair); 1186 set_alignment(u1, u2, align); 1187 changed = true; 1188 } 1189 return changed; 1190 } 1191 1192 //------------------------------order_def_uses--------------------------- 1193 // For extended packsets, ordinally arrange uses packset by major component 1194 void SuperWord::order_def_uses(Node_List* p) { 1195 Node* s1 = p->at(0); 1196 1197 if (s1->is_Store()) return; 1198 1199 // reductions are always managed beforehand 1200 if (s1->is_reduction()) return; 1201 1202 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) { 1203 Node* t1 = s1->fast_out(i); 1204 1205 // Only allow operand swap on commuting operations 1206 if (!t1->is_Add() && !t1->is_Mul()) { 1207 break; 1208 } 1209 1210 // Now find t1's packset 1211 Node_List* p2 = NULL; 1212 for (int j = 0; j < _packset.length(); j++) { 1213 p2 = _packset.at(j); 1214 Node* first = p2->at(0); 1215 if (t1 == first) { 1216 break; 1217 } 1218 p2 = NULL; 1219 } 1220 // Arrange all sub components by the major component 1221 if (p2 != NULL) { 1222 for (uint j = 1; j < p->size(); j++) { 1223 Node* d1 = p->at(j); 1224 Node* u1 = p2->at(j); 1225 opnd_positions_match(s1, t1, d1, u1); 1226 } 1227 } 1228 } 1229 } 1230 1231 //---------------------------opnd_positions_match------------------------- 1232 // Is the use of d1 in u1 at the same operand position as d2 in u2? 1233 bool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) { 1234 // check reductions to see if they are marshalled to represent the reduction 1235 // operator in a specified opnd 1236 if (u1->is_reduction() && u2->is_reduction()) { 1237 // ensure reductions have phis and reduction definitions feeding the 1st operand 1238 Node* first = u1->in(2); 1239 if (first->is_Phi() || first->is_reduction()) { 1240 u1->swap_edges(1, 2); 1241 } 1242 // ensure reductions have phis and reduction definitions feeding the 1st operand 1243 first = u2->in(2); 1244 if (first->is_Phi() || first->is_reduction()) { 1245 u2->swap_edges(1, 2); 1246 } 1247 return true; 1248 } 1249 1250 uint ct = u1->req(); 1251 if (ct != u2->req()) return false; 1252 uint i1 = 0; 1253 uint i2 = 0; 1254 do { 1255 for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break; 1256 for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break; 1257 if (i1 != i2) { 1258 if ((i1 == (3-i2)) && (u2->is_Add() || u2->is_Mul())) { 1259 // Further analysis relies on operands position matching. 1260 u2->swap_edges(i1, i2); 1261 } else { 1262 return false; 1263 } 1264 } 1265 } while (i1 < ct); 1266 return true; 1267 } 1268 1269 //------------------------------est_savings--------------------------- 1270 // Estimate the savings from executing s1 and s2 as a pack 1271 int SuperWord::est_savings(Node* s1, Node* s2) { 1272 int save_in = 2 - 1; // 2 operations per instruction in packed form 1273 1274 // inputs 1275 for (uint i = 1; i < s1->req(); i++) { 1276 Node* x1 = s1->in(i); 1277 Node* x2 = s2->in(i); 1278 if (x1 != x2) { 1279 if (are_adjacent_refs(x1, x2)) { 1280 save_in += adjacent_profit(x1, x2); 1281 } else if (!in_packset(x1, x2)) { 1282 save_in -= pack_cost(2); 1283 } else { 1284 save_in += unpack_cost(2); 1285 } 1286 } 1287 } 1288 1289 // uses of result 1290 uint ct = 0; 1291 int save_use = 0; 1292 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) { 1293 Node* s1_use = s1->fast_out(i); 1294 for (int j = 0; j < _packset.length(); j++) { 1295 Node_List* p = _packset.at(j); 1296 if (p->at(0) == s1_use) { 1297 for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) { 1298 Node* s2_use = s2->fast_out(k); 1299 if (p->at(p->size()-1) == s2_use) { 1300 ct++; 1301 if (are_adjacent_refs(s1_use, s2_use)) { 1302 save_use += adjacent_profit(s1_use, s2_use); 1303 } 1304 } 1305 } 1306 } 1307 } 1308 } 1309 1310 if (ct < s1->outcnt()) save_use += unpack_cost(1); 1311 if (ct < s2->outcnt()) save_use += unpack_cost(1); 1312 1313 return MAX2(save_in, save_use); 1314 } 1315 1316 //------------------------------costs--------------------------- 1317 int SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; } 1318 int SuperWord::pack_cost(int ct) { return ct; } 1319 int SuperWord::unpack_cost(int ct) { return ct; } 1320 1321 //------------------------------combine_packs--------------------------- 1322 // Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last 1323 void SuperWord::combine_packs() { 1324 bool changed = true; 1325 // Combine packs regardless max vector size. 1326 while (changed) { 1327 changed = false; 1328 for (int i = 0; i < _packset.length(); i++) { 1329 Node_List* p1 = _packset.at(i); 1330 if (p1 == NULL) continue; 1331 // Because of sorting we can start at i + 1 1332 for (int j = i + 1; j < _packset.length(); j++) { 1333 Node_List* p2 = _packset.at(j); 1334 if (p2 == NULL) continue; 1335 if (i == j) continue; 1336 if (p1->at(p1->size()-1) == p2->at(0)) { 1337 for (uint k = 1; k < p2->size(); k++) { 1338 p1->push(p2->at(k)); 1339 } 1340 _packset.at_put(j, NULL); 1341 changed = true; 1342 } 1343 } 1344 } 1345 } 1346 1347 // Split packs which have size greater then max vector size. 1348 for (int i = 0; i < _packset.length(); i++) { 1349 Node_List* p1 = _packset.at(i); 1350 if (p1 != NULL) { 1351 BasicType bt = velt_basic_type(p1->at(0)); 1352 uint max_vlen = Matcher::max_vector_size(bt); // Max elements in vector 1353 assert(is_power_of_2(max_vlen), "sanity"); 1354 uint psize = p1->size(); 1355 if (!is_power_of_2(psize)) { 1356 // Skip pack which can't be vector. 1357 // case1: for(...) { a[i] = i; } elements values are different (i+x) 1358 // case2: for(...) { a[i] = b[i+1]; } can't align both, load and store 1359 _packset.at_put(i, NULL); 1360 continue; 1361 } 1362 if (psize > max_vlen) { 1363 Node_List* pack = new Node_List(); 1364 for (uint j = 0; j < psize; j++) { 1365 pack->push(p1->at(j)); 1366 if (pack->size() >= max_vlen) { 1367 assert(is_power_of_2(pack->size()), "sanity"); 1368 _packset.append(pack); 1369 pack = new Node_List(); 1370 } 1371 } 1372 _packset.at_put(i, NULL); 1373 } 1374 } 1375 } 1376 1377 // Compress list. 1378 for (int i = _packset.length() - 1; i >= 0; i--) { 1379 Node_List* p1 = _packset.at(i); 1380 if (p1 == NULL) { 1381 _packset.remove_at(i); 1382 } 1383 } 1384 1385 #ifndef PRODUCT 1386 if (TraceSuperWord) { 1387 tty->print_cr("\nAfter combine_packs"); 1388 print_packset(); 1389 } 1390 #endif 1391 } 1392 1393 //-----------------------------construct_my_pack_map-------------------------- 1394 // Construct the map from nodes to packs. Only valid after the 1395 // point where a node is only in one pack (after combine_packs). 1396 void SuperWord::construct_my_pack_map() { 1397 Node_List* rslt = NULL; 1398 for (int i = 0; i < _packset.length(); i++) { 1399 Node_List* p = _packset.at(i); 1400 for (uint j = 0; j < p->size(); j++) { 1401 Node* s = p->at(j); 1402 assert(my_pack(s) == NULL, "only in one pack"); 1403 set_my_pack(s, p); 1404 } 1405 } 1406 } 1407 1408 //------------------------------filter_packs--------------------------- 1409 // Remove packs that are not implemented or not profitable. 1410 void SuperWord::filter_packs() { 1411 // Remove packs that are not implemented 1412 for (int i = _packset.length() - 1; i >= 0; i--) { 1413 Node_List* pk = _packset.at(i); 1414 bool impl = implemented(pk); 1415 if (!impl) { 1416 #ifndef PRODUCT 1417 if (TraceSuperWord && Verbose) { 1418 tty->print_cr("Unimplemented"); 1419 pk->at(0)->dump(); 1420 } 1421 #endif 1422 remove_pack_at(i); 1423 } 1424 Node *n = pk->at(0); 1425 if (n->is_reduction()) { 1426 _num_reductions++; 1427 } else { 1428 _num_work_vecs++; 1429 } 1430 } 1431 1432 // Remove packs that are not profitable 1433 bool changed; 1434 do { 1435 changed = false; 1436 for (int i = _packset.length() - 1; i >= 0; i--) { 1437 Node_List* pk = _packset.at(i); 1438 bool prof = profitable(pk); 1439 if (!prof) { 1440 #ifndef PRODUCT 1441 if (TraceSuperWord && Verbose) { 1442 tty->print_cr("Unprofitable"); 1443 pk->at(0)->dump(); 1444 } 1445 #endif 1446 remove_pack_at(i); 1447 changed = true; 1448 } 1449 } 1450 } while (changed); 1451 1452 #ifndef PRODUCT 1453 if (TraceSuperWord) { 1454 tty->print_cr("\nAfter filter_packs"); 1455 print_packset(); 1456 tty->cr(); 1457 } 1458 #endif 1459 } 1460 1461 //------------------------------implemented--------------------------- 1462 // Can code be generated for pack p? 1463 bool SuperWord::implemented(Node_List* p) { 1464 bool retValue = false; 1465 Node* p0 = p->at(0); 1466 if (p0 != NULL) { 1467 int opc = p0->Opcode(); 1468 uint size = p->size(); 1469 if (p0->is_reduction()) { 1470 const Type *arith_type = p0->bottom_type(); 1471 // Length 2 reductions of INT/LONG do not offer performance benefits 1472 if (((arith_type->basic_type() == T_INT) || (arith_type->basic_type() == T_LONG)) && (size == 2)) { 1473 retValue = false; 1474 } else { 1475 retValue = ReductionNode::implemented(opc, size, arith_type->basic_type()); 1476 } 1477 } else { 1478 retValue = VectorNode::implemented(opc, size, velt_basic_type(p0)); 1479 } 1480 } 1481 return retValue; 1482 } 1483 1484 //------------------------------same_inputs-------------------------- 1485 // For pack p, are all idx operands the same? 1486 static bool same_inputs(Node_List* p, int idx) { 1487 Node* p0 = p->at(0); 1488 uint vlen = p->size(); 1489 Node* p0_def = p0->in(idx); 1490 for (uint i = 1; i < vlen; i++) { 1491 Node* pi = p->at(i); 1492 Node* pi_def = pi->in(idx); 1493 if (p0_def != pi_def) 1494 return false; 1495 } 1496 return true; 1497 } 1498 1499 //------------------------------profitable--------------------------- 1500 // For pack p, are all operands and all uses (with in the block) vector? 1501 bool SuperWord::profitable(Node_List* p) { 1502 Node* p0 = p->at(0); 1503 uint start, end; 1504 VectorNode::vector_operands(p0, &start, &end); 1505 1506 // Return false if some inputs are not vectors or vectors with different 1507 // size or alignment. 1508 // Also, for now, return false if not scalar promotion case when inputs are 1509 // the same. Later, implement PackNode and allow differing, non-vector inputs 1510 // (maybe just the ones from outside the block.) 1511 for (uint i = start; i < end; i++) { 1512 if (!is_vector_use(p0, i)) 1513 return false; 1514 } 1515 // Check if reductions are connected 1516 if (p0->is_reduction()) { 1517 Node* second_in = p0->in(2); 1518 Node_List* second_pk = my_pack(second_in); 1519 if ((second_pk == NULL) || (_num_work_vecs == _num_reductions)) { 1520 // Remove reduction flag if no parent pack or if not enough work 1521 // to cover reduction expansion overhead 1522 p0->remove_flag(Node::Flag_is_reduction); 1523 return false; 1524 } else if (second_pk->size() != p->size()) { 1525 return false; 1526 } 1527 } 1528 if (VectorNode::is_shift(p0)) { 1529 // For now, return false if shift count is vector or not scalar promotion 1530 // case (different shift counts) because it is not supported yet. 1531 Node* cnt = p0->in(2); 1532 Node_List* cnt_pk = my_pack(cnt); 1533 if (cnt_pk != NULL) 1534 return false; 1535 if (!same_inputs(p, 2)) 1536 return false; 1537 } 1538 if (!p0->is_Store()) { 1539 // For now, return false if not all uses are vector. 1540 // Later, implement ExtractNode and allow non-vector uses (maybe 1541 // just the ones outside the block.) 1542 for (uint i = 0; i < p->size(); i++) { 1543 Node* def = p->at(i); 1544 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) { 1545 Node* use = def->fast_out(j); 1546 for (uint k = 0; k < use->req(); k++) { 1547 Node* n = use->in(k); 1548 if (def == n) { 1549 // reductions can be loop carried dependences 1550 if (def->is_reduction() && use->is_Phi()) 1551 continue; 1552 if (!is_vector_use(use, k)) { 1553 return false; 1554 } 1555 } 1556 } 1557 } 1558 } 1559 } 1560 return true; 1561 } 1562 1563 //------------------------------schedule--------------------------- 1564 // Adjust the memory graph for the packed operations 1565 void SuperWord::schedule() { 1566 1567 // Co-locate in the memory graph the members of each memory pack 1568 for (int i = 0; i < _packset.length(); i++) { 1569 co_locate_pack(_packset.at(i)); 1570 } 1571 } 1572 1573 //-------------------------------remove_and_insert------------------- 1574 // Remove "current" from its current position in the memory graph and insert 1575 // it after the appropriate insertion point (lip or uip). 1576 void SuperWord::remove_and_insert(MemNode *current, MemNode *prev, MemNode *lip, 1577 Node *uip, Unique_Node_List &sched_before) { 1578 Node* my_mem = current->in(MemNode::Memory); 1579 bool sched_up = sched_before.member(current); 1580 1581 // remove current_store from its current position in the memmory graph 1582 for (DUIterator i = current->outs(); current->has_out(i); i++) { 1583 Node* use = current->out(i); 1584 if (use->is_Mem()) { 1585 assert(use->in(MemNode::Memory) == current, "must be"); 1586 if (use == prev) { // connect prev to my_mem 1587 _igvn.replace_input_of(use, MemNode::Memory, my_mem); 1588 --i; //deleted this edge; rescan position 1589 } else if (sched_before.member(use)) { 1590 if (!sched_up) { // Will be moved together with current 1591 _igvn.replace_input_of(use, MemNode::Memory, uip); 1592 --i; //deleted this edge; rescan position 1593 } 1594 } else { 1595 if (sched_up) { // Will be moved together with current 1596 _igvn.replace_input_of(use, MemNode::Memory, lip); 1597 --i; //deleted this edge; rescan position 1598 } 1599 } 1600 } 1601 } 1602 1603 Node *insert_pt = sched_up ? uip : lip; 1604 1605 // all uses of insert_pt's memory state should use current's instead 1606 for (DUIterator i = insert_pt->outs(); insert_pt->has_out(i); i++) { 1607 Node* use = insert_pt->out(i); 1608 if (use->is_Mem()) { 1609 assert(use->in(MemNode::Memory) == insert_pt, "must be"); 1610 _igvn.replace_input_of(use, MemNode::Memory, current); 1611 --i; //deleted this edge; rescan position 1612 } else if (!sched_up && use->is_Phi() && use->bottom_type() == Type::MEMORY) { 1613 uint pos; //lip (lower insert point) must be the last one in the memory slice 1614 for (pos=1; pos < use->req(); pos++) { 1615 if (use->in(pos) == insert_pt) break; 1616 } 1617 _igvn.replace_input_of(use, pos, current); 1618 --i; 1619 } 1620 } 1621 1622 //connect current to insert_pt 1623 _igvn.replace_input_of(current, MemNode::Memory, insert_pt); 1624 } 1625 1626 //------------------------------co_locate_pack---------------------------------- 1627 // To schedule a store pack, we need to move any sandwiched memory ops either before 1628 // or after the pack, based upon dependence information: 1629 // (1) If any store in the pack depends on the sandwiched memory op, the 1630 // sandwiched memory op must be scheduled BEFORE the pack; 1631 // (2) If a sandwiched memory op depends on any store in the pack, the 1632 // sandwiched memory op must be scheduled AFTER the pack; 1633 // (3) If a sandwiched memory op (say, memA) depends on another sandwiched 1634 // memory op (say memB), memB must be scheduled before memA. So, if memA is 1635 // scheduled before the pack, memB must also be scheduled before the pack; 1636 // (4) If there is no dependence restriction for a sandwiched memory op, we simply 1637 // schedule this store AFTER the pack 1638 // (5) We know there is no dependence cycle, so there in no other case; 1639 // (6) Finally, all memory ops in another single pack should be moved in the same direction. 1640 // 1641 // To schedule a load pack, we use the memory state of either the first or the last load in 1642 // the pack, based on the dependence constraint. 1643 void SuperWord::co_locate_pack(Node_List* pk) { 1644 if (pk->at(0)->is_Store()) { 1645 MemNode* first = executed_first(pk)->as_Mem(); 1646 MemNode* last = executed_last(pk)->as_Mem(); 1647 Unique_Node_List schedule_before_pack; 1648 Unique_Node_List memops; 1649 1650 MemNode* current = last->in(MemNode::Memory)->as_Mem(); 1651 MemNode* previous = last; 1652 while (true) { 1653 assert(in_bb(current), "stay in block"); 1654 memops.push(previous); 1655 for (DUIterator i = current->outs(); current->has_out(i); i++) { 1656 Node* use = current->out(i); 1657 if (use->is_Mem() && use != previous) 1658 memops.push(use); 1659 } 1660 if (current == first) break; 1661 previous = current; 1662 current = current->in(MemNode::Memory)->as_Mem(); 1663 } 1664 1665 // determine which memory operations should be scheduled before the pack 1666 for (uint i = 1; i < memops.size(); i++) { 1667 Node *s1 = memops.at(i); 1668 if (!in_pack(s1, pk) && !schedule_before_pack.member(s1)) { 1669 for (uint j = 0; j< i; j++) { 1670 Node *s2 = memops.at(j); 1671 if (!independent(s1, s2)) { 1672 if (in_pack(s2, pk) || schedule_before_pack.member(s2)) { 1673 schedule_before_pack.push(s1); // s1 must be scheduled before 1674 Node_List* mem_pk = my_pack(s1); 1675 if (mem_pk != NULL) { 1676 for (uint ii = 0; ii < mem_pk->size(); ii++) { 1677 Node* s = mem_pk->at(ii); // follow partner 1678 if (memops.member(s) && !schedule_before_pack.member(s)) 1679 schedule_before_pack.push(s); 1680 } 1681 } 1682 break; 1683 } 1684 } 1685 } 1686 } 1687 } 1688 1689 Node* upper_insert_pt = first->in(MemNode::Memory); 1690 // Following code moves loads connected to upper_insert_pt below aliased stores. 1691 // Collect such loads here and reconnect them back to upper_insert_pt later. 1692 memops.clear(); 1693 for (DUIterator i = upper_insert_pt->outs(); upper_insert_pt->has_out(i); i++) { 1694 Node* use = upper_insert_pt->out(i); 1695 if (use->is_Mem() && !use->is_Store()) { 1696 memops.push(use); 1697 } 1698 } 1699 1700 MemNode* lower_insert_pt = last; 1701 previous = last; //previous store in pk 1702 current = last->in(MemNode::Memory)->as_Mem(); 1703 1704 // start scheduling from "last" to "first" 1705 while (true) { 1706 assert(in_bb(current), "stay in block"); 1707 assert(in_pack(previous, pk), "previous stays in pack"); 1708 Node* my_mem = current->in(MemNode::Memory); 1709 1710 if (in_pack(current, pk)) { 1711 // Forward users of my memory state (except "previous) to my input memory state 1712 for (DUIterator i = current->outs(); current->has_out(i); i++) { 1713 Node* use = current->out(i); 1714 if (use->is_Mem() && use != previous) { 1715 assert(use->in(MemNode::Memory) == current, "must be"); 1716 if (schedule_before_pack.member(use)) { 1717 _igvn.replace_input_of(use, MemNode::Memory, upper_insert_pt); 1718 } else { 1719 _igvn.replace_input_of(use, MemNode::Memory, lower_insert_pt); 1720 } 1721 --i; // deleted this edge; rescan position 1722 } 1723 } 1724 previous = current; 1725 } else { // !in_pack(current, pk) ==> a sandwiched store 1726 remove_and_insert(current, previous, lower_insert_pt, upper_insert_pt, schedule_before_pack); 1727 } 1728 1729 if (current == first) break; 1730 current = my_mem->as_Mem(); 1731 } // end while 1732 1733 // Reconnect loads back to upper_insert_pt. 1734 for (uint i = 0; i < memops.size(); i++) { 1735 Node *ld = memops.at(i); 1736 if (ld->in(MemNode::Memory) != upper_insert_pt) { 1737 _igvn.replace_input_of(ld, MemNode::Memory, upper_insert_pt); 1738 } 1739 } 1740 } else if (pk->at(0)->is_Load()) { //load 1741 // all loads in the pack should have the same memory state. By default, 1742 // we use the memory state of the last load. However, if any load could 1743 // not be moved down due to the dependence constraint, we use the memory 1744 // state of the first load. 1745 Node* last_mem = executed_last(pk)->in(MemNode::Memory); 1746 Node* first_mem = executed_first(pk)->in(MemNode::Memory); 1747 bool schedule_last = true; 1748 for (uint i = 0; i < pk->size(); i++) { 1749 Node* ld = pk->at(i); 1750 for (Node* current = last_mem; current != ld->in(MemNode::Memory); 1751 current=current->in(MemNode::Memory)) { 1752 assert(current != first_mem, "corrupted memory graph"); 1753 if(current->is_Mem() && !independent(current, ld)){ 1754 schedule_last = false; // a later store depends on this load 1755 break; 1756 } 1757 } 1758 } 1759 1760 Node* mem_input = schedule_last ? last_mem : first_mem; 1761 _igvn.hash_delete(mem_input); 1762 // Give each load the same memory state 1763 for (uint i = 0; i < pk->size(); i++) { 1764 LoadNode* ld = pk->at(i)->as_Load(); 1765 _igvn.replace_input_of(ld, MemNode::Memory, mem_input); 1766 } 1767 } 1768 } 1769 1770 #ifndef PRODUCT 1771 void SuperWord::print_loop(bool whole) { 1772 Node_Stack stack(_arena, _phase->C->unique() >> 2); 1773 Node_List rpo_list; 1774 VectorSet visited(_arena); 1775 visited.set(lpt()->_head->_idx); 1776 _phase->rpo(lpt()->_head, stack, visited, rpo_list); 1777 _phase->dump(lpt(), rpo_list.size(), rpo_list ); 1778 if(whole) { 1779 tty->print_cr("\n Whole loop tree"); 1780 _phase->dump(); 1781 tty->print_cr(" End of whole loop tree\n"); 1782 } 1783 } 1784 #endif 1785 1786 //------------------------------output--------------------------- 1787 // Convert packs into vector node operations 1788 void SuperWord::output() { 1789 if (_packset.length() == 0) return; 1790 1791 #ifndef PRODUCT 1792 if (TraceLoopOpts) { 1793 tty->print("SuperWord::output "); 1794 lpt()->dump_head(); 1795 } 1796 #endif 1797 1798 // MUST ENSURE main loop's initial value is properly aligned: 1799 // (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0 1800 1801 align_initial_loop_index(align_to_ref()); 1802 1803 // Insert extract (unpack) operations for scalar uses 1804 for (int i = 0; i < _packset.length(); i++) { 1805 insert_extracts(_packset.at(i)); 1806 } 1807 1808 Compile* C = _phase->C; 1809 CountedLoopNode *cl = lpt()->_head->as_CountedLoop(); 1810 uint max_vlen_in_bytes = 0; 1811 uint max_vlen = 0; 1812 1813 NOT_PRODUCT(if(_CountedLoopReserveKit_debug > 0) {tty->print_cr("SWPointer::output: print loop before create_reserve_version_of_loop"); print_loop(true);}) 1814 1815 CountedLoopReserveKit make_reversable(_phase, _lpt, DoReserveCopyInSuperWord); 1816 1817 NOT_PRODUCT(if(_CountedLoopReserveKit_debug > 0) {tty->print_cr("SWPointer::output: print loop after create_reserve_version_of_loop"); print_loop(true);}) 1818 1819 if (DoReserveCopyInSuperWord && !make_reversable.has_reserved()) { 1820 NOT_PRODUCT({tty->print_cr("SWPointer::output: loop was not reserved correctly, exiting SuperWord");}) 1821 return; 1822 } 1823 1824 for (int i = 0; i < _block.length(); i++) { 1825 Node* n = _block.at(i); 1826 Node_List* p = my_pack(n); 1827 if (p && n == executed_last(p)) { 1828 uint vlen = p->size(); 1829 uint vlen_in_bytes = 0; 1830 Node* vn = NULL; 1831 Node* low_adr = p->at(0); 1832 Node* first = executed_first(p); 1833 int opc = n->Opcode(); 1834 if (n->is_Load()) { 1835 Node* ctl = n->in(MemNode::Control); 1836 Node* mem = first->in(MemNode::Memory); 1837 SWPointer p1(n->as_Mem(), this, NULL, false); 1838 // Identify the memory dependency for the new loadVector node by 1839 // walking up through memory chain. 1840 // This is done to give flexibility to the new loadVector node so that 1841 // it can move above independent storeVector nodes. 1842 while (mem->is_StoreVector()) { 1843 SWPointer p2(mem->as_Mem(), this, NULL, false); 1844 int cmp = p1.cmp(p2); 1845 if (SWPointer::not_equal(cmp) || !SWPointer::comparable(cmp)) { 1846 mem = mem->in(MemNode::Memory); 1847 } else { 1848 break; // dependent memory 1849 } 1850 } 1851 Node* adr = low_adr->in(MemNode::Address); 1852 const TypePtr* atyp = n->adr_type(); 1853 vn = LoadVectorNode::make(opc, ctl, mem, adr, atyp, vlen, velt_basic_type(n), control_dependency(p)); 1854 vlen_in_bytes = vn->as_LoadVector()->memory_size(); 1855 } else if (n->is_Store()) { 1856 // Promote value to be stored to vector 1857 Node* val = vector_opd(p, MemNode::ValueIn); 1858 Node* ctl = n->in(MemNode::Control); 1859 Node* mem = first->in(MemNode::Memory); 1860 Node* adr = low_adr->in(MemNode::Address); 1861 const TypePtr* atyp = n->adr_type(); 1862 vn = StoreVectorNode::make(opc, ctl, mem, adr, atyp, val, vlen); 1863 vlen_in_bytes = vn->as_StoreVector()->memory_size(); 1864 } else if (n->req() == 3) { 1865 // Promote operands to vector 1866 Node* in1 = NULL; 1867 bool node_isa_reduction = n->is_reduction(); 1868 if (node_isa_reduction) { 1869 // the input to the first reduction operation is retained 1870 in1 = low_adr->in(1); 1871 } else { 1872 in1 = vector_opd(p, 1); 1873 } 1874 Node* in2 = vector_opd(p, 2); 1875 if (VectorNode::is_invariant_vector(in1) && (node_isa_reduction == false) && (n->is_Add() || n->is_Mul())) { 1876 // Move invariant vector input into second position to avoid register spilling. 1877 Node* tmp = in1; 1878 in1 = in2; 1879 in2 = tmp; 1880 } 1881 if (node_isa_reduction) { 1882 const Type *arith_type = n->bottom_type(); 1883 vn = ReductionNode::make(opc, NULL, in1, in2, arith_type->basic_type()); 1884 if (in2->is_Load()) { 1885 vlen_in_bytes = in2->as_LoadVector()->memory_size(); 1886 } else { 1887 vlen_in_bytes = in2->as_Vector()->length_in_bytes(); 1888 } 1889 } else { 1890 vn = VectorNode::make(opc, in1, in2, vlen, velt_basic_type(n)); 1891 vlen_in_bytes = vn->as_Vector()->length_in_bytes(); 1892 } 1893 } else if (opc == Op_SqrtD || opc == Op_AbsF || opc == Op_AbsD || opc == Op_NegF || opc == Op_NegD) { 1894 // Promote operand to vector (Sqrt/Abs/Neg are 2 address instructions) 1895 Node* in = vector_opd(p, 1); 1896 vn = VectorNode::make(opc, in, NULL, vlen, velt_basic_type(n)); 1897 vlen_in_bytes = vn->as_Vector()->length_in_bytes(); 1898 } else { 1899 ShouldNotReachHere(); 1900 } 1901 assert(vn != NULL, "sanity"); 1902 _igvn.register_new_node_with_optimizer(vn); 1903 _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0))); 1904 for (uint j = 0; j < p->size(); j++) { 1905 Node* pm = p->at(j); 1906 _igvn.replace_node(pm, vn); 1907 } 1908 _igvn._worklist.push(vn); 1909 1910 if (vlen_in_bytes > max_vlen_in_bytes) { 1911 max_vlen = vlen; 1912 max_vlen_in_bytes = vlen_in_bytes; 1913 } 1914 #ifdef ASSERT 1915 if (TraceNewVectors) { 1916 tty->print("new Vector node: "); 1917 vn->dump(); 1918 } 1919 #endif 1920 } 1921 } 1922 C->set_max_vector_size(max_vlen_in_bytes); 1923 1924 if (SuperWordLoopUnrollAnalysis) { 1925 if (cl->has_passed_slp()) { 1926 uint slp_max_unroll_factor = cl->slp_max_unroll(); 1927 if (slp_max_unroll_factor == max_vlen) { 1928 NOT_PRODUCT(if (TraceSuperWordLoopUnrollAnalysis) tty->print_cr("vector loop(unroll=%d, len=%d)\n", max_vlen, max_vlen_in_bytes*BitsPerByte)); 1929 // For atomic unrolled loops which are vector mapped, instigate more unrolling. 1930 cl->set_notpassed_slp(); 1931 C->set_major_progress(); 1932 cl->mark_do_unroll_only(); 1933 } 1934 } 1935 } 1936 1937 if (DoReserveCopyInSuperWord) { 1938 make_reversable.use_new(); 1939 } 1940 NOT_PRODUCT(if(_CountedLoopReserveKit_debug > 0) {tty->print_cr("\n Final loop after SuperWord"); print_loop(true);}) 1941 return; 1942 } 1943 1944 //------------------------------vector_opd--------------------------- 1945 // Create a vector operand for the nodes in pack p for operand: in(opd_idx) 1946 Node* SuperWord::vector_opd(Node_List* p, int opd_idx) { 1947 Node* p0 = p->at(0); 1948 uint vlen = p->size(); 1949 Node* opd = p0->in(opd_idx); 1950 1951 if (same_inputs(p, opd_idx)) { 1952 if (opd->is_Vector() || opd->is_LoadVector()) { 1953 assert(((opd_idx != 2) || !VectorNode::is_shift(p0)), "shift's count can't be vector"); 1954 return opd; // input is matching vector 1955 } 1956 if ((opd_idx == 2) && VectorNode::is_shift(p0)) { 1957 Compile* C = _phase->C; 1958 Node* cnt = opd; 1959 // Vector instructions do not mask shift count, do it here. 1960 juint mask = (p0->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1); 1961 const TypeInt* t = opd->find_int_type(); 1962 if (t != NULL && t->is_con()) { 1963 juint shift = t->get_con(); 1964 if (shift > mask) { // Unsigned cmp 1965 cnt = ConNode::make(TypeInt::make(shift & mask)); 1966 } 1967 } else { 1968 if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) { 1969 cnt = ConNode::make(TypeInt::make(mask)); 1970 _igvn.register_new_node_with_optimizer(cnt); 1971 cnt = new AndINode(opd, cnt); 1972 _igvn.register_new_node_with_optimizer(cnt); 1973 _phase->set_ctrl(cnt, _phase->get_ctrl(opd)); 1974 } 1975 assert(opd->bottom_type()->isa_int(), "int type only"); 1976 // Move non constant shift count into vector register. 1977 cnt = VectorNode::shift_count(p0, cnt, vlen, velt_basic_type(p0)); 1978 } 1979 if (cnt != opd) { 1980 _igvn.register_new_node_with_optimizer(cnt); 1981 _phase->set_ctrl(cnt, _phase->get_ctrl(opd)); 1982 } 1983 return cnt; 1984 } 1985 assert(!opd->is_StoreVector(), "such vector is not expected here"); 1986 // Convert scalar input to vector with the same number of elements as 1987 // p0's vector. Use p0's type because size of operand's container in 1988 // vector should match p0's size regardless operand's size. 1989 const Type* p0_t = velt_type(p0); 1990 VectorNode* vn = VectorNode::scalar2vector(opd, vlen, p0_t); 1991 1992 _igvn.register_new_node_with_optimizer(vn); 1993 _phase->set_ctrl(vn, _phase->get_ctrl(opd)); 1994 #ifdef ASSERT 1995 if (TraceNewVectors) { 1996 tty->print("new Vector node: "); 1997 vn->dump(); 1998 } 1999 #endif 2000 return vn; 2001 } 2002 2003 // Insert pack operation 2004 BasicType bt = velt_basic_type(p0); 2005 PackNode* pk = PackNode::make(opd, vlen, bt); 2006 DEBUG_ONLY( const BasicType opd_bt = opd->bottom_type()->basic_type(); ) 2007 2008 for (uint i = 1; i < vlen; i++) { 2009 Node* pi = p->at(i); 2010 Node* in = pi->in(opd_idx); 2011 assert(my_pack(in) == NULL, "Should already have been unpacked"); 2012 assert(opd_bt == in->bottom_type()->basic_type(), "all same type"); 2013 pk->add_opd(in); 2014 } 2015 _igvn.register_new_node_with_optimizer(pk); 2016 _phase->set_ctrl(pk, _phase->get_ctrl(opd)); 2017 #ifdef ASSERT 2018 if (TraceNewVectors) { 2019 tty->print("new Vector node: "); 2020 pk->dump(); 2021 } 2022 #endif 2023 return pk; 2024 } 2025 2026 //------------------------------insert_extracts--------------------------- 2027 // If a use of pack p is not a vector use, then replace the 2028 // use with an extract operation. 2029 void SuperWord::insert_extracts(Node_List* p) { 2030 if (p->at(0)->is_Store()) return; 2031 assert(_n_idx_list.is_empty(), "empty (node,index) list"); 2032 2033 // Inspect each use of each pack member. For each use that is 2034 // not a vector use, replace the use with an extract operation. 2035 2036 for (uint i = 0; i < p->size(); i++) { 2037 Node* def = p->at(i); 2038 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) { 2039 Node* use = def->fast_out(j); 2040 for (uint k = 0; k < use->req(); k++) { 2041 Node* n = use->in(k); 2042 if (def == n) { 2043 if (!is_vector_use(use, k)) { 2044 _n_idx_list.push(use, k); 2045 } 2046 } 2047 } 2048 } 2049 } 2050 2051 while (_n_idx_list.is_nonempty()) { 2052 Node* use = _n_idx_list.node(); 2053 int idx = _n_idx_list.index(); 2054 _n_idx_list.pop(); 2055 Node* def = use->in(idx); 2056 2057 if (def->is_reduction()) continue; 2058 2059 // Insert extract operation 2060 _igvn.hash_delete(def); 2061 int def_pos = alignment(def) / data_size(def); 2062 2063 Node* ex = ExtractNode::make(def, def_pos, velt_basic_type(def)); 2064 _igvn.register_new_node_with_optimizer(ex); 2065 _phase->set_ctrl(ex, _phase->get_ctrl(def)); 2066 _igvn.replace_input_of(use, idx, ex); 2067 _igvn._worklist.push(def); 2068 2069 bb_insert_after(ex, bb_idx(def)); 2070 set_velt_type(ex, velt_type(def)); 2071 } 2072 } 2073 2074 //------------------------------is_vector_use--------------------------- 2075 // Is use->in(u_idx) a vector use? 2076 bool SuperWord::is_vector_use(Node* use, int u_idx) { 2077 Node_List* u_pk = my_pack(use); 2078 if (u_pk == NULL) return false; 2079 if (use->is_reduction()) return true; 2080 Node* def = use->in(u_idx); 2081 Node_List* d_pk = my_pack(def); 2082 if (d_pk == NULL) { 2083 // check for scalar promotion 2084 Node* n = u_pk->at(0)->in(u_idx); 2085 for (uint i = 1; i < u_pk->size(); i++) { 2086 if (u_pk->at(i)->in(u_idx) != n) return false; 2087 } 2088 return true; 2089 } 2090 if (u_pk->size() != d_pk->size()) 2091 return false; 2092 for (uint i = 0; i < u_pk->size(); i++) { 2093 Node* ui = u_pk->at(i); 2094 Node* di = d_pk->at(i); 2095 if (ui->in(u_idx) != di || alignment(ui) != alignment(di)) 2096 return false; 2097 } 2098 return true; 2099 } 2100 2101 //------------------------------construct_bb--------------------------- 2102 // Construct reverse postorder list of block members 2103 bool SuperWord::construct_bb() { 2104 Node* entry = bb(); 2105 2106 assert(_stk.length() == 0, "stk is empty"); 2107 assert(_block.length() == 0, "block is empty"); 2108 assert(_data_entry.length() == 0, "data_entry is empty"); 2109 assert(_mem_slice_head.length() == 0, "mem_slice_head is empty"); 2110 assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty"); 2111 2112 // Find non-control nodes with no inputs from within block, 2113 // create a temporary map from node _idx to bb_idx for use 2114 // by the visited and post_visited sets, 2115 // and count number of nodes in block. 2116 int bb_ct = 0; 2117 for (uint i = 0; i < lpt()->_body.size(); i++) { 2118 Node *n = lpt()->_body.at(i); 2119 set_bb_idx(n, i); // Create a temporary map 2120 if (in_bb(n)) { 2121 if (n->is_LoadStore() || n->is_MergeMem() || 2122 (n->is_Proj() && !n->as_Proj()->is_CFG())) { 2123 // Bailout if the loop has LoadStore, MergeMem or data Proj 2124 // nodes. Superword optimization does not work with them. 2125 return false; 2126 } 2127 bb_ct++; 2128 if (!n->is_CFG()) { 2129 bool found = false; 2130 for (uint j = 0; j < n->req(); j++) { 2131 Node* def = n->in(j); 2132 if (def && in_bb(def)) { 2133 found = true; 2134 break; 2135 } 2136 } 2137 if (!found) { 2138 assert(n != entry, "can't be entry"); 2139 _data_entry.push(n); 2140 } 2141 } 2142 } 2143 } 2144 2145 // Find memory slices (head and tail) 2146 for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) { 2147 Node *n = lp()->fast_out(i); 2148 if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) { 2149 Node* n_tail = n->in(LoopNode::LoopBackControl); 2150 if (n_tail != n->in(LoopNode::EntryControl)) { 2151 if (!n_tail->is_Mem()) { 2152 assert(n_tail->is_Mem(), err_msg_res("unexpected node for memory slice: %s", n_tail->Name())); 2153 return false; // Bailout 2154 } 2155 _mem_slice_head.push(n); 2156 _mem_slice_tail.push(n_tail); 2157 } 2158 } 2159 } 2160 2161 // Create an RPO list of nodes in block 2162 2163 visited_clear(); 2164 post_visited_clear(); 2165 2166 // Push all non-control nodes with no inputs from within block, then control entry 2167 for (int j = 0; j < _data_entry.length(); j++) { 2168 Node* n = _data_entry.at(j); 2169 visited_set(n); 2170 _stk.push(n); 2171 } 2172 visited_set(entry); 2173 _stk.push(entry); 2174 2175 // Do a depth first walk over out edges 2176 int rpo_idx = bb_ct - 1; 2177 int size; 2178 int reduction_uses = 0; 2179 while ((size = _stk.length()) > 0) { 2180 Node* n = _stk.top(); // Leave node on stack 2181 if (!visited_test_set(n)) { 2182 // forward arc in graph 2183 } else if (!post_visited_test(n)) { 2184 // cross or back arc 2185 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 2186 Node *use = n->fast_out(i); 2187 if (in_bb(use) && !visited_test(use) && 2188 // Don't go around backedge 2189 (!use->is_Phi() || n == entry)) { 2190 if (use->is_reduction()) { 2191 // First see if we can map the reduction on the given system we are on, then 2192 // make a data entry operation for each reduction we see. 2193 BasicType bt = use->bottom_type()->basic_type(); 2194 if (ReductionNode::implemented(use->Opcode(), Matcher::min_vector_size(bt), bt)) { 2195 reduction_uses++; 2196 } 2197 } 2198 _stk.push(use); 2199 } 2200 } 2201 if (_stk.length() == size) { 2202 // There were no additional uses, post visit node now 2203 _stk.pop(); // Remove node from stack 2204 assert(rpo_idx >= 0, ""); 2205 _block.at_put_grow(rpo_idx, n); 2206 rpo_idx--; 2207 post_visited_set(n); 2208 assert(rpo_idx >= 0 || _stk.is_empty(), ""); 2209 } 2210 } else { 2211 _stk.pop(); // Remove post-visited node from stack 2212 } 2213 }//while 2214 2215 int ii_current = -1; 2216 unsigned int load_idx = (unsigned int)-1; 2217 _ii_order.clear(); 2218 // Create real map of block indices for nodes 2219 for (int j = 0; j < _block.length(); j++) { 2220 Node* n = _block.at(j); 2221 set_bb_idx(n, j); 2222 if (_do_vector_loop && n->is_Load()) { 2223 if (ii_current == -1) { 2224 ii_current = _clone_map.gen(n->_idx); 2225 _ii_order.push(ii_current); 2226 load_idx = _clone_map.idx(n->_idx); 2227 } else if (_clone_map.idx(n->_idx) == load_idx && _clone_map.gen(n->_idx) != ii_current) { 2228 ii_current = _clone_map.gen(n->_idx); 2229 _ii_order.push(ii_current); 2230 } 2231 } 2232 }//for 2233 2234 // Ensure extra info is allocated. 2235 initialize_bb(); 2236 2237 #ifndef PRODUCT 2238 if (_vector_loop_debug && _ii_order.length() > 0) { 2239 tty->print("SuperWord::construct_bb: List of generations: "); 2240 for (int jj = 0; jj < _ii_order.length(); ++jj) { 2241 tty->print(" %d:%d", jj, _ii_order.at(jj)); 2242 } 2243 tty->print_cr(" "); 2244 } 2245 if (TraceSuperWord) { 2246 print_bb(); 2247 tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE"); 2248 for (int m = 0; m < _data_entry.length(); m++) { 2249 tty->print("%3d ", m); 2250 _data_entry.at(m)->dump(); 2251 } 2252 tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE"); 2253 for (int m = 0; m < _mem_slice_head.length(); m++) { 2254 tty->print("%3d ", m); _mem_slice_head.at(m)->dump(); 2255 tty->print(" "); _mem_slice_tail.at(m)->dump(); 2256 } 2257 } 2258 #endif 2259 assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found"); 2260 return (_mem_slice_head.length() > 0) || (reduction_uses > 0) || (_data_entry.length() > 0); 2261 } 2262 2263 //------------------------------initialize_bb--------------------------- 2264 // Initialize per node info 2265 void SuperWord::initialize_bb() { 2266 Node* last = _block.at(_block.length() - 1); 2267 grow_node_info(bb_idx(last)); 2268 } 2269 2270 //------------------------------bb_insert_after--------------------------- 2271 // Insert n into block after pos 2272 void SuperWord::bb_insert_after(Node* n, int pos) { 2273 int n_pos = pos + 1; 2274 // Make room 2275 for (int i = _block.length() - 1; i >= n_pos; i--) { 2276 _block.at_put_grow(i+1, _block.at(i)); 2277 } 2278 for (int j = _node_info.length() - 1; j >= n_pos; j--) { 2279 _node_info.at_put_grow(j+1, _node_info.at(j)); 2280 } 2281 // Set value 2282 _block.at_put_grow(n_pos, n); 2283 _node_info.at_put_grow(n_pos, SWNodeInfo::initial); 2284 // Adjust map from node->_idx to _block index 2285 for (int i = n_pos; i < _block.length(); i++) { 2286 set_bb_idx(_block.at(i), i); 2287 } 2288 } 2289 2290 //------------------------------compute_max_depth--------------------------- 2291 // Compute max depth for expressions from beginning of block 2292 // Use to prune search paths during test for independence. 2293 void SuperWord::compute_max_depth() { 2294 int ct = 0; 2295 bool again; 2296 do { 2297 again = false; 2298 for (int i = 0; i < _block.length(); i++) { 2299 Node* n = _block.at(i); 2300 if (!n->is_Phi()) { 2301 int d_orig = depth(n); 2302 int d_in = 0; 2303 for (DepPreds preds(n, _dg); !preds.done(); preds.next()) { 2304 Node* pred = preds.current(); 2305 if (in_bb(pred)) { 2306 d_in = MAX2(d_in, depth(pred)); 2307 } 2308 } 2309 if (d_in + 1 != d_orig) { 2310 set_depth(n, d_in + 1); 2311 again = true; 2312 } 2313 } 2314 } 2315 ct++; 2316 } while (again); 2317 #ifndef PRODUCT 2318 if (TraceSuperWord && Verbose) 2319 tty->print_cr("compute_max_depth iterated: %d times", ct); 2320 #endif 2321 } 2322 2323 //-------------------------compute_vector_element_type----------------------- 2324 // Compute necessary vector element type for expressions 2325 // This propagates backwards a narrower integer type when the 2326 // upper bits of the value are not needed. 2327 // Example: char a,b,c; a = b + c; 2328 // Normally the type of the add is integer, but for packed character 2329 // operations the type of the add needs to be char. 2330 void SuperWord::compute_vector_element_type() { 2331 #ifndef PRODUCT 2332 if (TraceSuperWord && Verbose) 2333 tty->print_cr("\ncompute_velt_type:"); 2334 #endif 2335 2336 // Initial type 2337 for (int i = 0; i < _block.length(); i++) { 2338 Node* n = _block.at(i); 2339 set_velt_type(n, container_type(n)); 2340 } 2341 2342 // Propagate integer narrowed type backwards through operations 2343 // that don't depend on higher order bits 2344 for (int i = _block.length() - 1; i >= 0; i--) { 2345 Node* n = _block.at(i); 2346 // Only integer types need be examined 2347 const Type* vtn = velt_type(n); 2348 if (vtn->basic_type() == T_INT) { 2349 uint start, end; 2350 VectorNode::vector_operands(n, &start, &end); 2351 2352 for (uint j = start; j < end; j++) { 2353 Node* in = n->in(j); 2354 // Don't propagate through a memory 2355 if (!in->is_Mem() && in_bb(in) && velt_type(in)->basic_type() == T_INT && 2356 data_size(n) < data_size(in)) { 2357 bool same_type = true; 2358 for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) { 2359 Node *use = in->fast_out(k); 2360 if (!in_bb(use) || !same_velt_type(use, n)) { 2361 same_type = false; 2362 break; 2363 } 2364 } 2365 if (same_type) { 2366 // For right shifts of small integer types (bool, byte, char, short) 2367 // we need precise information about sign-ness. Only Load nodes have 2368 // this information because Store nodes are the same for signed and 2369 // unsigned values. And any arithmetic operation after a load may 2370 // expand a value to signed Int so such right shifts can't be used 2371 // because vector elements do not have upper bits of Int. 2372 const Type* vt = vtn; 2373 if (VectorNode::is_shift(in)) { 2374 Node* load = in->in(1); 2375 if (load->is_Load() && in_bb(load) && (velt_type(load)->basic_type() == T_INT)) { 2376 vt = velt_type(load); 2377 } else if (in->Opcode() != Op_LShiftI) { 2378 // Widen type to Int to avoid creation of right shift vector 2379 // (align + data_size(s1) check in stmts_can_pack() will fail). 2380 // Note, left shifts work regardless type. 2381 vt = TypeInt::INT; 2382 } 2383 } 2384 set_velt_type(in, vt); 2385 } 2386 } 2387 } 2388 } 2389 } 2390 #ifndef PRODUCT 2391 if (TraceSuperWord && Verbose) { 2392 for (int i = 0; i < _block.length(); i++) { 2393 Node* n = _block.at(i); 2394 velt_type(n)->dump(); 2395 tty->print("\t"); 2396 n->dump(); 2397 } 2398 } 2399 #endif 2400 } 2401 2402 //------------------------------memory_alignment--------------------------- 2403 // Alignment within a vector memory reference 2404 int SuperWord::memory_alignment(MemNode* s, int iv_adjust) { 2405 #ifndef PRODUCT 2406 if(TraceSuperWord && Verbose) { 2407 tty->print("SuperWord::memory_alignment within a vector memory reference for %d: ", s->_idx); s->dump(); 2408 } 2409 #endif 2410 NOT_PRODUCT(SWPointer::Tracer::Depth ddd(0);) 2411 SWPointer p(s, this, NULL, false); 2412 if (!p.valid()) { 2413 NOT_PRODUCT(if(is_trace_alignment()) tty->print("SWPointer::memory_alignment: SWPointer p invalid, return bottom_align");) 2414 return bottom_align; 2415 } 2416 int vw = vector_width_in_bytes(s); 2417 if (vw < 2) { 2418 NOT_PRODUCT(if(is_trace_alignment()) tty->print_cr("SWPointer::memory_alignment: vector_width_in_bytes < 2, return bottom_align");) 2419 return bottom_align; // No vectors for this type 2420 } 2421 int offset = p.offset_in_bytes(); 2422 offset += iv_adjust*p.memory_size(); 2423 int off_rem = offset % vw; 2424 int off_mod = off_rem >= 0 ? off_rem : off_rem + vw; 2425 NOT_PRODUCT(if(TraceSuperWord && Verbose) tty->print_cr("SWPointer::memory_alignment: off_rem = %d, off_mod = %d", off_rem, off_mod);) 2426 return off_mod; 2427 } 2428 2429 //---------------------------container_type--------------------------- 2430 // Smallest type containing range of values 2431 const Type* SuperWord::container_type(Node* n) { 2432 if (n->is_Mem()) { 2433 BasicType bt = n->as_Mem()->memory_type(); 2434 if (n->is_Store() && (bt == T_CHAR)) { 2435 // Use T_SHORT type instead of T_CHAR for stored values because any 2436 // preceding arithmetic operation extends values to signed Int. 2437 bt = T_SHORT; 2438 } 2439 if (n->Opcode() == Op_LoadUB) { 2440 // Adjust type for unsigned byte loads, it is important for right shifts. 2441 // T_BOOLEAN is used because there is no basic type representing type 2442 // TypeInt::UBYTE. Use of T_BOOLEAN for vectors is fine because only 2443 // size (one byte) and sign is important. 2444 bt = T_BOOLEAN; 2445 } 2446 return Type::get_const_basic_type(bt); 2447 } 2448 const Type* t = _igvn.type(n); 2449 if (t->basic_type() == T_INT) { 2450 // A narrow type of arithmetic operations will be determined by 2451 // propagating the type of memory operations. 2452 return TypeInt::INT; 2453 } 2454 return t; 2455 } 2456 2457 bool SuperWord::same_velt_type(Node* n1, Node* n2) { 2458 const Type* vt1 = velt_type(n1); 2459 const Type* vt2 = velt_type(n2); 2460 if (vt1->basic_type() == T_INT && vt2->basic_type() == T_INT) { 2461 // Compare vectors element sizes for integer types. 2462 return data_size(n1) == data_size(n2); 2463 } 2464 return vt1 == vt2; 2465 } 2466 2467 //------------------------------in_packset--------------------------- 2468 // Are s1 and s2 in a pack pair and ordered as s1,s2? 2469 bool SuperWord::in_packset(Node* s1, Node* s2) { 2470 for (int i = 0; i < _packset.length(); i++) { 2471 Node_List* p = _packset.at(i); 2472 assert(p->size() == 2, "must be"); 2473 if (p->at(0) == s1 && p->at(p->size()-1) == s2) { 2474 return true; 2475 } 2476 } 2477 return false; 2478 } 2479 2480 //------------------------------in_pack--------------------------- 2481 // Is s in pack p? 2482 Node_List* SuperWord::in_pack(Node* s, Node_List* p) { 2483 for (uint i = 0; i < p->size(); i++) { 2484 if (p->at(i) == s) { 2485 return p; 2486 } 2487 } 2488 return NULL; 2489 } 2490 2491 //------------------------------remove_pack_at--------------------------- 2492 // Remove the pack at position pos in the packset 2493 void SuperWord::remove_pack_at(int pos) { 2494 Node_List* p = _packset.at(pos); 2495 for (uint i = 0; i < p->size(); i++) { 2496 Node* s = p->at(i); 2497 set_my_pack(s, NULL); 2498 } 2499 _packset.remove_at(pos); 2500 } 2501 2502 void SuperWord::packset_sort(int n) { 2503 // simple bubble sort so that we capitalize with O(n) when its already sorted 2504 while (n != 0) { 2505 bool swapped = false; 2506 for (int i = 1; i < n; i++) { 2507 Node_List* q_low = _packset.at(i-1); 2508 Node_List* q_i = _packset.at(i); 2509 2510 // only swap when we find something to swap 2511 if (alignment(q_low->at(0)) > alignment(q_i->at(0))) { 2512 Node_List* t = q_i; 2513 *(_packset.adr_at(i)) = q_low; 2514 *(_packset.adr_at(i-1)) = q_i; 2515 swapped = true; 2516 } 2517 } 2518 if (swapped == false) break; 2519 n--; 2520 } 2521 } 2522 2523 //------------------------------executed_first--------------------------- 2524 // Return the node executed first in pack p. Uses the RPO block list 2525 // to determine order. 2526 Node* SuperWord::executed_first(Node_List* p) { 2527 Node* n = p->at(0); 2528 int n_rpo = bb_idx(n); 2529 for (uint i = 1; i < p->size(); i++) { 2530 Node* s = p->at(i); 2531 int s_rpo = bb_idx(s); 2532 if (s_rpo < n_rpo) { 2533 n = s; 2534 n_rpo = s_rpo; 2535 } 2536 } 2537 return n; 2538 } 2539 2540 //------------------------------executed_last--------------------------- 2541 // Return the node executed last in pack p. 2542 Node* SuperWord::executed_last(Node_List* p) { 2543 Node* n = p->at(0); 2544 int n_rpo = bb_idx(n); 2545 for (uint i = 1; i < p->size(); i++) { 2546 Node* s = p->at(i); 2547 int s_rpo = bb_idx(s); 2548 if (s_rpo > n_rpo) { 2549 n = s; 2550 n_rpo = s_rpo; 2551 } 2552 } 2553 return n; 2554 } 2555 2556 LoadNode::ControlDependency SuperWord::control_dependency(Node_List* p) { 2557 LoadNode::ControlDependency dep = LoadNode::DependsOnlyOnTest; 2558 for (uint i = 0; i < p->size(); i++) { 2559 Node* n = p->at(i); 2560 assert(n->is_Load(), "only meaningful for loads"); 2561 if (!n->depends_only_on_test()) { 2562 dep = LoadNode::Pinned; 2563 } 2564 } 2565 return dep; 2566 } 2567 2568 2569 //----------------------------align_initial_loop_index--------------------------- 2570 // Adjust pre-loop limit so that in main loop, a load/store reference 2571 // to align_to_ref will be a position zero in the vector. 2572 // (iv + k) mod vector_align == 0 2573 void SuperWord::align_initial_loop_index(MemNode* align_to_ref) { 2574 CountedLoopNode *main_head = lp()->as_CountedLoop(); 2575 assert(main_head->is_main_loop(), ""); 2576 CountedLoopEndNode* pre_end = get_pre_loop_end(main_head); 2577 assert(pre_end != NULL, "we must have a correct pre-loop"); 2578 Node *pre_opaq1 = pre_end->limit(); 2579 assert(pre_opaq1->Opcode() == Op_Opaque1, ""); 2580 Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1; 2581 Node *lim0 = pre_opaq->in(1); 2582 2583 // Where we put new limit calculations 2584 Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl); 2585 2586 // Ensure the original loop limit is available from the 2587 // pre-loop Opaque1 node. 2588 Node *orig_limit = pre_opaq->original_loop_limit(); 2589 assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, ""); 2590 2591 SWPointer align_to_ref_p(align_to_ref, this, NULL, false); 2592 assert(align_to_ref_p.valid(), "sanity"); 2593 2594 // Given: 2595 // lim0 == original pre loop limit 2596 // V == v_align (power of 2) 2597 // invar == extra invariant piece of the address expression 2598 // e == offset [ +/- invar ] 2599 // 2600 // When reassociating expressions involving '%' the basic rules are: 2601 // (a - b) % k == 0 => a % k == b % k 2602 // and: 2603 // (a + b) % k == 0 => a % k == (k - b) % k 2604 // 2605 // For stride > 0 && scale > 0, 2606 // Derive the new pre-loop limit "lim" such that the two constraints: 2607 // (1) lim = lim0 + N (where N is some positive integer < V) 2608 // (2) (e + lim) % V == 0 2609 // are true. 2610 // 2611 // Substituting (1) into (2), 2612 // (e + lim0 + N) % V == 0 2613 // solve for N: 2614 // N = (V - (e + lim0)) % V 2615 // substitute back into (1), so that new limit 2616 // lim = lim0 + (V - (e + lim0)) % V 2617 // 2618 // For stride > 0 && scale < 0 2619 // Constraints: 2620 // lim = lim0 + N 2621 // (e - lim) % V == 0 2622 // Solving for lim: 2623 // (e - lim0 - N) % V == 0 2624 // N = (e - lim0) % V 2625 // lim = lim0 + (e - lim0) % V 2626 // 2627 // For stride < 0 && scale > 0 2628 // Constraints: 2629 // lim = lim0 - N 2630 // (e + lim) % V == 0 2631 // Solving for lim: 2632 // (e + lim0 - N) % V == 0 2633 // N = (e + lim0) % V 2634 // lim = lim0 - (e + lim0) % V 2635 // 2636 // For stride < 0 && scale < 0 2637 // Constraints: 2638 // lim = lim0 - N 2639 // (e - lim) % V == 0 2640 // Solving for lim: 2641 // (e - lim0 + N) % V == 0 2642 // N = (V - (e - lim0)) % V 2643 // lim = lim0 - (V - (e - lim0)) % V 2644 2645 int vw = vector_width_in_bytes(align_to_ref); 2646 int stride = iv_stride(); 2647 int scale = align_to_ref_p.scale_in_bytes(); 2648 int elt_size = align_to_ref_p.memory_size(); 2649 int v_align = vw / elt_size; 2650 assert(v_align > 1, "sanity"); 2651 int offset = align_to_ref_p.offset_in_bytes() / elt_size; 2652 Node *offsn = _igvn.intcon(offset); 2653 2654 Node *e = offsn; 2655 if (align_to_ref_p.invar() != NULL) { 2656 // incorporate any extra invariant piece producing (offset +/- invar) >>> log2(elt) 2657 Node* log2_elt = _igvn.intcon(exact_log2(elt_size)); 2658 Node* aref = new URShiftINode(align_to_ref_p.invar(), log2_elt); 2659 _igvn.register_new_node_with_optimizer(aref); 2660 _phase->set_ctrl(aref, pre_ctrl); 2661 if (align_to_ref_p.negate_invar()) { 2662 e = new SubINode(e, aref); 2663 } else { 2664 e = new AddINode(e, aref); 2665 } 2666 _igvn.register_new_node_with_optimizer(e); 2667 _phase->set_ctrl(e, pre_ctrl); 2668 } 2669 if (vw > ObjectAlignmentInBytes) { 2670 // incorporate base e +/- base && Mask >>> log2(elt) 2671 Node* xbase = new CastP2XNode(NULL, align_to_ref_p.base()); 2672 _igvn.register_new_node_with_optimizer(xbase); 2673 #ifdef _LP64 2674 xbase = new ConvL2INode(xbase); 2675 _igvn.register_new_node_with_optimizer(xbase); 2676 #endif 2677 Node* mask = _igvn.intcon(vw-1); 2678 Node* masked_xbase = new AndINode(xbase, mask); 2679 _igvn.register_new_node_with_optimizer(masked_xbase); 2680 Node* log2_elt = _igvn.intcon(exact_log2(elt_size)); 2681 Node* bref = new URShiftINode(masked_xbase, log2_elt); 2682 _igvn.register_new_node_with_optimizer(bref); 2683 _phase->set_ctrl(bref, pre_ctrl); 2684 e = new AddINode(e, bref); 2685 _igvn.register_new_node_with_optimizer(e); 2686 _phase->set_ctrl(e, pre_ctrl); 2687 } 2688 2689 // compute e +/- lim0 2690 if (scale < 0) { 2691 e = new SubINode(e, lim0); 2692 } else { 2693 e = new AddINode(e, lim0); 2694 } 2695 _igvn.register_new_node_with_optimizer(e); 2696 _phase->set_ctrl(e, pre_ctrl); 2697 2698 if (stride * scale > 0) { 2699 // compute V - (e +/- lim0) 2700 Node* va = _igvn.intcon(v_align); 2701 e = new SubINode(va, e); 2702 _igvn.register_new_node_with_optimizer(e); 2703 _phase->set_ctrl(e, pre_ctrl); 2704 } 2705 // compute N = (exp) % V 2706 Node* va_msk = _igvn.intcon(v_align - 1); 2707 Node* N = new AndINode(e, va_msk); 2708 _igvn.register_new_node_with_optimizer(N); 2709 _phase->set_ctrl(N, pre_ctrl); 2710 2711 // substitute back into (1), so that new limit 2712 // lim = lim0 + N 2713 Node* lim; 2714 if (stride < 0) { 2715 lim = new SubINode(lim0, N); 2716 } else { 2717 lim = new AddINode(lim0, N); 2718 } 2719 _igvn.register_new_node_with_optimizer(lim); 2720 _phase->set_ctrl(lim, pre_ctrl); 2721 Node* constrained = 2722 (stride > 0) ? (Node*) new MinINode(lim, orig_limit) 2723 : (Node*) new MaxINode(lim, orig_limit); 2724 _igvn.register_new_node_with_optimizer(constrained); 2725 _phase->set_ctrl(constrained, pre_ctrl); 2726 _igvn.hash_delete(pre_opaq); 2727 pre_opaq->set_req(1, constrained); 2728 } 2729 2730 //----------------------------get_pre_loop_end--------------------------- 2731 // Find pre loop end from main loop. Returns null if none. 2732 CountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode* cl) { 2733 Node* ctrl = cl->in(LoopNode::EntryControl); 2734 if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return NULL; 2735 Node* iffm = ctrl->in(0); 2736 if (!iffm->is_If()) return NULL; 2737 Node* bolzm = iffm->in(1); 2738 if (!bolzm->is_Bool()) return NULL; 2739 Node* cmpzm = bolzm->in(1); 2740 if (!cmpzm->is_Cmp()) return NULL; 2741 Node* opqzm = cmpzm->in(2); 2742 // Can not optimize a loop if zero-trip Opaque1 node is optimized 2743 // away and then another round of loop opts attempted. 2744 if (opqzm->Opcode() != Op_Opaque1) { 2745 return NULL; 2746 } 2747 Node* p_f = iffm->in(0); 2748 if (!p_f->is_IfFalse()) return NULL; 2749 if (!p_f->in(0)->is_CountedLoopEnd()) return NULL; 2750 CountedLoopEndNode* pre_end = p_f->in(0)->as_CountedLoopEnd(); 2751 CountedLoopNode* loop_node = pre_end->loopnode(); 2752 if (loop_node == NULL || !loop_node->is_pre_loop()) return NULL; 2753 return pre_end; 2754 } 2755 2756 2757 //------------------------------init--------------------------- 2758 void SuperWord::init() { 2759 _dg.init(); 2760 _packset.clear(); 2761 _disjoint_ptrs.clear(); 2762 _block.clear(); 2763 _data_entry.clear(); 2764 _mem_slice_head.clear(); 2765 _mem_slice_tail.clear(); 2766 _iteration_first.clear(); 2767 _iteration_last.clear(); 2768 _node_info.clear(); 2769 _align_to_ref = NULL; 2770 _lpt = NULL; 2771 _lp = NULL; 2772 _bb = NULL; 2773 _iv = NULL; 2774 _race_possible = 0; 2775 _early_return = false; 2776 _num_work_vecs = 0; 2777 _num_reductions = 0; 2778 } 2779 2780 //------------------------------restart--------------------------- 2781 void SuperWord::restart() { 2782 _dg.init(); 2783 _packset.clear(); 2784 _disjoint_ptrs.clear(); 2785 _block.clear(); 2786 _data_entry.clear(); 2787 _mem_slice_head.clear(); 2788 _mem_slice_tail.clear(); 2789 _node_info.clear(); 2790 } 2791 2792 //------------------------------print_packset--------------------------- 2793 void SuperWord::print_packset() { 2794 #ifndef PRODUCT 2795 tty->print_cr("packset"); 2796 for (int i = 0; i < _packset.length(); i++) { 2797 tty->print_cr("Pack: %d", i); 2798 Node_List* p = _packset.at(i); 2799 print_pack(p); 2800 } 2801 #endif 2802 } 2803 2804 //------------------------------print_pack--------------------------- 2805 void SuperWord::print_pack(Node_List* p) { 2806 for (uint i = 0; i < p->size(); i++) { 2807 print_stmt(p->at(i)); 2808 } 2809 } 2810 2811 //------------------------------print_bb--------------------------- 2812 void SuperWord::print_bb() { 2813 #ifndef PRODUCT 2814 tty->print_cr("\nBlock"); 2815 for (int i = 0; i < _block.length(); i++) { 2816 Node* n = _block.at(i); 2817 tty->print("%d ", i); 2818 if (n) { 2819 n->dump(); 2820 } 2821 } 2822 #endif 2823 } 2824 2825 //------------------------------print_stmt--------------------------- 2826 void SuperWord::print_stmt(Node* s) { 2827 #ifndef PRODUCT 2828 tty->print(" align: %d \t", alignment(s)); 2829 s->dump(); 2830 #endif 2831 } 2832 2833 //------------------------------blank--------------------------- 2834 char* SuperWord::blank(uint depth) { 2835 static char blanks[101]; 2836 assert(depth < 101, "too deep"); 2837 for (uint i = 0; i < depth; i++) blanks[i] = ' '; 2838 blanks[depth] = '\0'; 2839 return blanks; 2840 } 2841 2842 2843 //==============================SWPointer=========================== 2844 #ifndef PRODUCT 2845 int SWPointer::Tracer::_depth = 0; 2846 #endif 2847 //----------------------------SWPointer------------------------ 2848 SWPointer::SWPointer(MemNode* mem, SuperWord* slp, Node_Stack *nstack, bool analyze_only) : 2849 _mem(mem), _slp(slp), _base(NULL), _adr(NULL), 2850 _scale(0), _offset(0), _invar(NULL), _negate_invar(false), 2851 _nstack(nstack), _analyze_only(analyze_only), 2852 _stack_idx(0) 2853 #ifndef PRODUCT 2854 , _tracer(slp) 2855 #endif 2856 { 2857 NOT_PRODUCT(_tracer.ctor_1(mem);) 2858 2859 Node* adr = mem->in(MemNode::Address); 2860 if (!adr->is_AddP()) { 2861 assert(!valid(), "too complex"); 2862 return; 2863 } 2864 // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant) 2865 Node* base = adr->in(AddPNode::Base); 2866 // The base address should be loop invariant 2867 if (!invariant(base)) { 2868 assert(!valid(), "base address is loop variant"); 2869 return; 2870 } 2871 //unsafe reference could not be aligned appropriately without runtime checking 2872 if (base == NULL || base->bottom_type() == Type::TOP) { 2873 assert(!valid(), "unsafe access"); 2874 return; 2875 } 2876 2877 NOT_PRODUCT(if(_slp->is_trace_alignment()) _tracer.store_depth();) 2878 NOT_PRODUCT(_tracer.ctor_2(adr);) 2879 2880 int i; 2881 for (i = 0; i < 3; i++) { 2882 NOT_PRODUCT(_tracer.ctor_3(adr, i);) 2883 2884 if (!scaled_iv_plus_offset(adr->in(AddPNode::Offset))) { 2885 assert(!valid(), "too complex"); 2886 return; 2887 } 2888 adr = adr->in(AddPNode::Address); 2889 NOT_PRODUCT(_tracer.ctor_4(adr, i);) 2890 2891 if (base == adr || !adr->is_AddP()) { 2892 NOT_PRODUCT(_tracer.ctor_5(adr, base, i);) 2893 break; // stop looking at addp's 2894 } 2895 } 2896 NOT_PRODUCT(if(_slp->is_trace_alignment()) _tracer.restore_depth();) 2897 NOT_PRODUCT(_tracer.ctor_6(mem);) 2898 2899 _base = base; 2900 _adr = adr; 2901 assert(valid(), "Usable"); 2902 } 2903 2904 // Following is used to create a temporary object during 2905 // the pattern match of an address expression. 2906 SWPointer::SWPointer(SWPointer* p) : 2907 _mem(p->_mem), _slp(p->_slp), _base(NULL), _adr(NULL), 2908 _scale(0), _offset(0), _invar(NULL), _negate_invar(false), 2909 _nstack(p->_nstack), _analyze_only(p->_analyze_only), 2910 _stack_idx(p->_stack_idx) 2911 #ifndef PRODUCT 2912 , _tracer(p->_slp) 2913 #endif 2914 {} 2915 2916 2917 bool SWPointer::invariant(Node* n) { 2918 NOT_PRODUCT(Tracer::Depth dd;) 2919 Node *n_c = phase()->get_ctrl(n); 2920 NOT_PRODUCT(_tracer.invariant_1(n, n_c);) 2921 return !lpt()->is_member(phase()->get_loop(n_c)); 2922 } 2923 //------------------------scaled_iv_plus_offset-------------------- 2924 // Match: k*iv + offset 2925 // where: k is a constant that maybe zero, and 2926 // offset is (k2 [+/- invariant]) where k2 maybe zero and invariant is optional 2927 bool SWPointer::scaled_iv_plus_offset(Node* n) { 2928 NOT_PRODUCT(Tracer::Depth ddd;) 2929 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_1(n);) 2930 2931 if (scaled_iv(n)) { 2932 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_2(n);) 2933 return true; 2934 } 2935 2936 if (offset_plus_k(n)) { 2937 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_3(n);) 2938 return true; 2939 } 2940 2941 int opc = n->Opcode(); 2942 if (opc == Op_AddI) { 2943 if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2))) { 2944 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_4(n);) 2945 return true; 2946 } 2947 if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) { 2948 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_5(n);) 2949 return true; 2950 } 2951 } else if (opc == Op_SubI) { 2952 if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2), true)) { 2953 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_6(n);) 2954 return true; 2955 } 2956 if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) { 2957 _scale *= -1; 2958 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_7(n);) 2959 return true; 2960 } 2961 } 2962 2963 NOT_PRODUCT(_tracer.scaled_iv_plus_offset_8(n);) 2964 return false; 2965 } 2966 2967 //----------------------------scaled_iv------------------------ 2968 // Match: k*iv where k is a constant that's not zero 2969 bool SWPointer::scaled_iv(Node* n) { 2970 NOT_PRODUCT(Tracer::Depth ddd;) 2971 NOT_PRODUCT(_tracer.scaled_iv_1(n);) 2972 2973 if (_scale != 0) { // already found a scale 2974 NOT_PRODUCT(_tracer.scaled_iv_2(n, _scale);) 2975 return false; 2976 } 2977 2978 if (n == iv()) { 2979 _scale = 1; 2980 NOT_PRODUCT(_tracer.scaled_iv_3(n, _scale);) 2981 return true; 2982 } 2983 if (_analyze_only && (invariant(n) == false)) { 2984 _nstack->push(n, _stack_idx++); 2985 } 2986 2987 int opc = n->Opcode(); 2988 if (opc == Op_MulI) { 2989 if (n->in(1) == iv() && n->in(2)->is_Con()) { 2990 _scale = n->in(2)->get_int(); 2991 NOT_PRODUCT(_tracer.scaled_iv_4(n, _scale);) 2992 return true; 2993 } else if (n->in(2) == iv() && n->in(1)->is_Con()) { 2994 _scale = n->in(1)->get_int(); 2995 NOT_PRODUCT(_tracer.scaled_iv_5(n, _scale);) 2996 return true; 2997 } 2998 } else if (opc == Op_LShiftI) { 2999 if (n->in(1) == iv() && n->in(2)->is_Con()) { 3000 _scale = 1 << n->in(2)->get_int(); 3001 NOT_PRODUCT(_tracer.scaled_iv_6(n, _scale);) 3002 return true; 3003 } 3004 } else if (opc == Op_ConvI2L) { 3005 if (scaled_iv_plus_offset(n->in(1))) { 3006 NOT_PRODUCT(_tracer.scaled_iv_7(n);) 3007 return true; 3008 } 3009 } else if (opc == Op_LShiftL) { 3010 if (!has_iv() && _invar == NULL) { 3011 // Need to preserve the current _offset value, so 3012 // create a temporary object for this expression subtree. 3013 // Hacky, so should re-engineer the address pattern match. 3014 NOT_PRODUCT(Tracer::Depth dddd;) 3015 SWPointer tmp(this); 3016 NOT_PRODUCT(_tracer.scaled_iv_8(n, &tmp);) 3017 3018 if (tmp.scaled_iv_plus_offset(n->in(1))) { 3019 if (tmp._invar == NULL || _slp->do_vector_loop()) { 3020 int mult = 1 << n->in(2)->get_int(); 3021 _scale = tmp._scale * mult; 3022 _offset += tmp._offset * mult; 3023 NOT_PRODUCT(_tracer.scaled_iv_9(n, _scale, _offset, mult);) 3024 return true; 3025 } 3026 } 3027 } 3028 } 3029 NOT_PRODUCT(_tracer.scaled_iv_10(n);) 3030 return false; 3031 } 3032 3033 //----------------------------offset_plus_k------------------------ 3034 // Match: offset is (k [+/- invariant]) 3035 // where k maybe zero and invariant is optional, but not both. 3036 bool SWPointer::offset_plus_k(Node* n, bool negate) { 3037 NOT_PRODUCT(Tracer::Depth ddd;) 3038 NOT_PRODUCT(_tracer.offset_plus_k_1(n);) 3039 3040 int opc = n->Opcode(); 3041 if (opc == Op_ConI) { 3042 _offset += negate ? -(n->get_int()) : n->get_int(); 3043 NOT_PRODUCT(_tracer.offset_plus_k_2(n, _offset);) 3044 return true; 3045 } else if (opc == Op_ConL) { 3046 // Okay if value fits into an int 3047 const TypeLong* t = n->find_long_type(); 3048 if (t->higher_equal(TypeLong::INT)) { 3049 jlong loff = n->get_long(); 3050 jint off = (jint)loff; 3051 _offset += negate ? -off : loff; 3052 NOT_PRODUCT(_tracer.offset_plus_k_3(n, _offset);) 3053 return true; 3054 } 3055 NOT_PRODUCT(_tracer.offset_plus_k_4(n);) 3056 return false; 3057 } 3058 if (_invar != NULL) { // already has an invariant 3059 NOT_PRODUCT(_tracer.offset_plus_k_5(n, _invar);) 3060 return false; 3061 } 3062 3063 if (_analyze_only && (invariant(n) == false)) { 3064 _nstack->push(n, _stack_idx++); 3065 } 3066 if (opc == Op_AddI) { 3067 if (n->in(2)->is_Con() && invariant(n->in(1))) { 3068 _negate_invar = negate; 3069 _invar = n->in(1); 3070 _offset += negate ? -(n->in(2)->get_int()) : n->in(2)->get_int(); 3071 NOT_PRODUCT(_tracer.offset_plus_k_6(n, _invar, _negate_invar, _offset);) 3072 return true; 3073 } else if (n->in(1)->is_Con() && invariant(n->in(2))) { 3074 _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int(); 3075 _negate_invar = negate; 3076 _invar = n->in(2); 3077 NOT_PRODUCT(_tracer.offset_plus_k_7(n, _invar, _negate_invar, _offset);) 3078 return true; 3079 } 3080 } 3081 if (opc == Op_SubI) { 3082 if (n->in(2)->is_Con() && invariant(n->in(1))) { 3083 _negate_invar = negate; 3084 _invar = n->in(1); 3085 _offset += !negate ? -(n->in(2)->get_int()) : n->in(2)->get_int(); 3086 NOT_PRODUCT(_tracer.offset_plus_k_8(n, _invar, _negate_invar, _offset);) 3087 return true; 3088 } else if (n->in(1)->is_Con() && invariant(n->in(2))) { 3089 _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int(); 3090 _negate_invar = !negate; 3091 _invar = n->in(2); 3092 NOT_PRODUCT(_tracer.offset_plus_k_9(n, _invar, _negate_invar, _offset);) 3093 return true; 3094 } 3095 } 3096 if (invariant(n)) { 3097 if (opc == Op_ConvI2L) { 3098 n = n->in(1); 3099 } 3100 _negate_invar = negate; 3101 _invar = n; 3102 NOT_PRODUCT(_tracer.offset_plus_k_10(n, _invar, _negate_invar, _offset);) 3103 return true; 3104 } 3105 3106 NOT_PRODUCT(_tracer.offset_plus_k_11(n);) 3107 return false; 3108 } 3109 3110 //----------------------------print------------------------ 3111 void SWPointer::print() { 3112 #ifndef PRODUCT 3113 tty->print("base: %d adr: %d scale: %d offset: %d invar: %c%d\n", 3114 _base != NULL ? _base->_idx : 0, 3115 _adr != NULL ? _adr->_idx : 0, 3116 _scale, _offset, 3117 _negate_invar?'-':'+', 3118 _invar != NULL ? _invar->_idx : 0); 3119 #endif 3120 } 3121 3122 //----------------------------tracing------------------------ 3123 #ifndef PRODUCT 3124 void SWPointer::Tracer::print_depth() { 3125 for (int ii = 0; ii<_depth; ++ii) tty->print(" "); 3126 } 3127 3128 void SWPointer::Tracer::ctor_1 (Node* mem) { 3129 if(_slp->is_trace_alignment()) { 3130 print_depth(); tty->print(" %d SWPointer::SWPointer: start alignment analysis", mem->_idx); mem->dump(); 3131 } 3132 } 3133 3134 void SWPointer::Tracer::ctor_2(Node* adr) { 3135 if(_slp->is_trace_alignment()) { 3136 //store_depth(); 3137 inc_depth(); 3138 print_depth(); tty->print(" %d (adr) SWPointer::SWPointer: ", adr->_idx); adr->dump(); 3139 inc_depth(); 3140 print_depth(); tty->print(" %d (base) SWPointer::SWPointer: ", adr->in(AddPNode::Base)->_idx); adr->in(AddPNode::Base)->dump(); 3141 } 3142 } 3143 3144 void SWPointer::Tracer::ctor_3(Node* adr, int i) { 3145 if(_slp->is_trace_alignment()) { 3146 inc_depth(); 3147 Node* offset = adr->in(AddPNode::Offset); 3148 print_depth(); tty->print(" %d (offset) SWPointer::SWPointer: i = %d: ", offset->_idx, i); offset->dump(); 3149 } 3150 } 3151 3152 void SWPointer::Tracer::ctor_4(Node* adr, int i) { 3153 if(_slp->is_trace_alignment()) { 3154 inc_depth(); 3155 print_depth(); tty->print(" %d (adr) SWPointer::SWPointer: i = %d: ", adr->_idx, i); adr->dump(); 3156 } 3157 } 3158 3159 void SWPointer::Tracer::ctor_5(Node* adr, Node* base, int i) { 3160 if(_slp->is_trace_alignment()) { 3161 inc_depth(); 3162 if (base == adr) { 3163 print_depth(); tty->print_cr(" \\ %d (adr) == %d (base) SWPointer::SWPointer: breaking analysis at i = %d", adr->_idx, base->_idx, i); 3164 } else if (!adr->is_AddP()) { 3165 print_depth(); tty->print_cr(" \\ %d (adr) is NOT Addp SWPointer::SWPointer: breaking analysis at i = %d", adr->_idx, i); 3166 } 3167 } 3168 } 3169 3170 void SWPointer::Tracer::ctor_6(Node* mem) { 3171 if(_slp->is_trace_alignment()) { 3172 //restore_depth(); 3173 print_depth(); tty->print_cr(" %d (adr) SWPointer::SWPointer: stop analysis", mem->_idx); 3174 } 3175 } 3176 3177 void SWPointer::Tracer::invariant_1(Node *n, Node *n_c) { 3178 if (_slp->do_vector_loop() && _slp->is_debug() && _slp->_lpt->is_member(_slp->_phase->get_loop(n_c)) != (int)_slp->in_bb(n)) { 3179 int is_member = _slp->_lpt->is_member(_slp->_phase->get_loop(n_c)); 3180 int in_bb = _slp->in_bb(n); 3181 print_depth(); tty->print(" \\ "); tty->print_cr(" %d SWPointer::invariant conditions differ: n_c %d", n->_idx, n_c->_idx); 3182 print_depth(); tty->print(" \\ "); tty->print_cr("is_member %d, in_bb %d", is_member, in_bb); 3183 print_depth(); tty->print(" \\ "); n->dump(); 3184 print_depth(); tty->print(" \\ "); n_c->dump(); 3185 } 3186 } 3187 3188 void SWPointer::Tracer::scaled_iv_plus_offset_1(Node* n) { 3189 if(_slp->is_trace_alignment()) { 3190 print_depth(); tty->print(" %d SWPointer::scaled_iv_plus_offset testing node: ", n->_idx); 3191 n->dump(); 3192 } 3193 } 3194 3195 void SWPointer::Tracer::scaled_iv_plus_offset_2(Node* n) { 3196 if(_slp->is_trace_alignment()) { 3197 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv_plus_offset: PASSED", n->_idx); 3198 } 3199 } 3200 3201 void SWPointer::Tracer::scaled_iv_plus_offset_3(Node* n) { 3202 if(_slp->is_trace_alignment()) { 3203 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv_plus_offset: PASSED", n->_idx); 3204 } 3205 } 3206 3207 void SWPointer::Tracer::scaled_iv_plus_offset_4(Node* n) { 3208 if(_slp->is_trace_alignment()) { 3209 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv_plus_offset: Op_AddI PASSED", n->_idx); 3210 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(1) is scaled_iv: ", n->in(1)->_idx); n->in(1)->dump(); 3211 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(2) is offset_plus_k: ", n->in(2)->_idx); n->in(2)->dump(); 3212 } 3213 } 3214 3215 void SWPointer::Tracer::scaled_iv_plus_offset_5(Node* n) { 3216 if(_slp->is_trace_alignment()) { 3217 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv_plus_offset: Op_AddI PASSED", n->_idx); 3218 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(2) is scaled_iv: ", n->in(2)->_idx); n->in(2)->dump(); 3219 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(1) is offset_plus_k: ", n->in(1)->_idx); n->in(1)->dump(); 3220 } 3221 } 3222 3223 void SWPointer::Tracer::scaled_iv_plus_offset_6(Node* n) { 3224 if(_slp->is_trace_alignment()) { 3225 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv_plus_offset: Op_SubI PASSED", n->_idx); 3226 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(1) is scaled_iv: ", n->in(1)->_idx); n->in(1)->dump(); 3227 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(2) is offset_plus_k: ", n->in(2)->_idx); n->in(2)->dump(); 3228 } 3229 } 3230 3231 void SWPointer::Tracer::scaled_iv_plus_offset_7(Node* n) { 3232 if(_slp->is_trace_alignment()) { 3233 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv_plus_offset: Op_SubI PASSED", n->_idx); 3234 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(2) is scaled_iv: ", n->in(2)->_idx); n->in(2)->dump(); 3235 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv_plus_offset: in(1) is offset_plus_k: ", n->in(1)->_idx); n->in(1)->dump(); 3236 } 3237 } 3238 3239 void SWPointer::Tracer::scaled_iv_plus_offset_8(Node* n) { 3240 if(_slp->is_trace_alignment()) { 3241 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv_plus_offset: FAILED", n->_idx); 3242 } 3243 } 3244 3245 void SWPointer::Tracer::scaled_iv_1(Node* n) { 3246 if(_slp->is_trace_alignment()) { 3247 print_depth(); tty->print(" %d SWPointer::scaled_iv: testing node: ", n->_idx); n->dump(); 3248 } 3249 } 3250 3251 void SWPointer::Tracer::scaled_iv_2(Node* n, int scale) { 3252 if(_slp->is_trace_alignment()) { 3253 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: FAILED since another _scale has been detected before", n->_idx); 3254 print_depth(); tty->print_cr(" \\ SWPointer::scaled_iv: _scale (%d) != 0", scale); 3255 } 3256 } 3257 3258 void SWPointer::Tracer::scaled_iv_3(Node* n, int scale) { 3259 if(_slp->is_trace_alignment()) { 3260 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: is iv, setting _scale = %d", n->_idx, scale); 3261 } 3262 } 3263 3264 void SWPointer::Tracer::scaled_iv_4(Node* n, int scale) { 3265 if(_slp->is_trace_alignment()) { 3266 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: Op_MulI PASSED, setting _scale = %d", n->_idx, scale); 3267 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv: in(1) is iv: ", n->in(1)->_idx); n->in(1)->dump(); 3268 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv: in(2) is Con: ", n->in(2)->_idx); n->in(2)->dump(); 3269 } 3270 } 3271 3272 void SWPointer::Tracer::scaled_iv_5(Node* n, int scale) { 3273 if(_slp->is_trace_alignment()) { 3274 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: Op_MulI PASSED, setting _scale = %d", n->_idx, scale); 3275 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv: in(2) is iv: ", n->in(2)->_idx); n->in(2)->dump(); 3276 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv: in(1) is Con: ", n->in(1)->_idx); n->in(1)->dump(); 3277 } 3278 } 3279 3280 void SWPointer::Tracer::scaled_iv_6(Node* n, int scale) { 3281 if(_slp->is_trace_alignment()) { 3282 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: Op_LShiftI PASSED, setting _scale = %d", n->_idx, scale); 3283 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv: in(1) is iv: ", n->in(1)->_idx); n->in(1)->dump(); 3284 print_depth(); tty->print(" \\ %d SWPointer::scaled_iv: in(2) is Con: ", n->in(2)->_idx); n->in(2)->dump(); 3285 } 3286 } 3287 3288 void SWPointer::Tracer::scaled_iv_7(Node* n) { 3289 if(_slp->is_trace_alignment()) { 3290 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: Op_ConvI2L PASSED", n->_idx); 3291 print_depth(); tty->print_cr(" \\ SWPointer::scaled_iv: in(1) %d is scaled_iv_plus_offset: ", n->in(1)->_idx); 3292 inc_depth(); inc_depth(); 3293 print_depth(); n->in(1)->dump(); 3294 dec_depth(); dec_depth(); 3295 } 3296 } 3297 3298 void SWPointer::Tracer::scaled_iv_8(Node* n, SWPointer* tmp) { 3299 if(_slp->is_trace_alignment()) { 3300 print_depth(); tty->print(" %d SWPointer::scaled_iv: Op_LShiftL, creating tmp SWPointer: ", n->_idx); tmp->print(); 3301 } 3302 } 3303 3304 void SWPointer::Tracer::scaled_iv_9(Node* n, int scale, int _offset, int mult) { 3305 if(_slp->is_trace_alignment()) { 3306 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: Op_LShiftL PASSED, setting _scale = %d, _offset = %d", n->_idx, scale, _offset); 3307 print_depth(); tty->print_cr(" \\ SWPointer::scaled_iv: in(1) %d is scaled_iv_plus_offset, in(2) %d used to get mult = %d: _scale = %d, _offset = %d", 3308 n->in(1)->_idx, n->in(2)->_idx, mult, scale, _offset); 3309 inc_depth(); inc_depth(); 3310 print_depth(); n->in(1)->dump(); 3311 print_depth(); n->in(2)->dump(); 3312 dec_depth(); dec_depth(); 3313 } 3314 } 3315 3316 void SWPointer::Tracer::scaled_iv_10(Node* n) { 3317 if(_slp->is_trace_alignment()) { 3318 print_depth(); tty->print_cr(" %d SWPointer::scaled_iv: FAILED", n->_idx); 3319 } 3320 } 3321 3322 void SWPointer::Tracer::offset_plus_k_1(Node* n) { 3323 if(_slp->is_trace_alignment()) { 3324 print_depth(); tty->print(" %d SWPointer::offset_plus_k: testing node: ", n->_idx); n->dump(); 3325 } 3326 } 3327 3328 void SWPointer::Tracer::offset_plus_k_2(Node* n, int _offset) { 3329 if(_slp->is_trace_alignment()) { 3330 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: Op_ConI PASSED, setting _offset = %d", n->_idx, _offset); 3331 } 3332 } 3333 3334 void SWPointer::Tracer::offset_plus_k_3(Node* n, int _offset) { 3335 if(_slp->is_trace_alignment()) { 3336 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: Op_ConL PASSED, setting _offset = %d", n->_idx, _offset); 3337 } 3338 } 3339 3340 void SWPointer::Tracer::offset_plus_k_4(Node* n) { 3341 if(_slp->is_trace_alignment()) { 3342 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: FAILED", n->_idx); 3343 print_depth(); tty->print_cr(" \\ " JLONG_FORMAT " SWPointer::offset_plus_k: Op_ConL FAILED, k is too big", n->get_long()); 3344 } 3345 } 3346 3347 void SWPointer::Tracer::offset_plus_k_5(Node* n, Node* _invar) { 3348 if(_slp->is_trace_alignment()) { 3349 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: FAILED since another invariant has been detected before", n->_idx); 3350 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: _invar != NULL: ", _invar->_idx); _invar->dump(); 3351 } 3352 } 3353 3354 void SWPointer::Tracer::offset_plus_k_6(Node* n, Node* _invar, bool _negate_invar, int _offset) { 3355 if(_slp->is_trace_alignment()) { 3356 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: Op_AddI PASSED, setting _negate_invar = %d, _invar = %d, _offset = %d", 3357 n->_idx, _negate_invar, _invar->_idx, _offset); 3358 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(2) is Con: ", n->in(2)->_idx); n->in(2)->dump(); 3359 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(1) is invariant: ", _invar->_idx); _invar->dump(); 3360 } 3361 } 3362 3363 void SWPointer::Tracer::offset_plus_k_7(Node* n, Node* _invar, bool _negate_invar, int _offset) { 3364 if(_slp->is_trace_alignment()) { 3365 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: Op_AddI PASSED, setting _negate_invar = %d, _invar = %d, _offset = %d", 3366 n->_idx, _negate_invar, _invar->_idx, _offset); 3367 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(1) is Con: ", n->in(1)->_idx); n->in(1)->dump(); 3368 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(2) is invariant: ", _invar->_idx); _invar->dump(); 3369 } 3370 } 3371 3372 void SWPointer::Tracer::offset_plus_k_8(Node* n, Node* _invar, bool _negate_invar, int _offset) { 3373 if(_slp->is_trace_alignment()) { 3374 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: Op_SubI is PASSED, setting _negate_invar = %d, _invar = %d, _offset = %d", 3375 n->_idx, _negate_invar, _invar->_idx, _offset); 3376 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(2) is Con: ", n->in(2)->_idx); n->in(2)->dump(); 3377 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(1) is invariant: ", _invar->_idx); _invar->dump(); 3378 } 3379 } 3380 3381 void SWPointer::Tracer::offset_plus_k_9(Node* n, Node* _invar, bool _negate_invar, int _offset) { 3382 if(_slp->is_trace_alignment()) { 3383 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: Op_SubI PASSED, setting _negate_invar = %d, _invar = %d, _offset = %d", n->_idx, _negate_invar, _invar->_idx, _offset); 3384 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(1) is Con: ", n->in(1)->_idx); n->in(1)->dump(); 3385 print_depth(); tty->print(" \\ %d SWPointer::offset_plus_k: in(2) is invariant: ", _invar->_idx); _invar->dump(); 3386 } 3387 } 3388 3389 void SWPointer::Tracer::offset_plus_k_10(Node* n, Node* _invar, bool _negate_invar, int _offset) { 3390 if(_slp->is_trace_alignment()) { 3391 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: PASSED, setting _negate_invar = %d, _invar = %d, _offset = %d", n->_idx, _negate_invar, _invar->_idx, _offset); 3392 print_depth(); tty->print_cr(" \\ %d SWPointer::offset_plus_k: is invariant", n->_idx); 3393 } 3394 } 3395 3396 void SWPointer::Tracer::offset_plus_k_11(Node* n) { 3397 if(_slp->is_trace_alignment()) { 3398 print_depth(); tty->print_cr(" %d SWPointer::offset_plus_k: FAILED", n->_idx); 3399 } 3400 } 3401 3402 #endif 3403 // ========================= OrderedPair ===================== 3404 3405 const OrderedPair OrderedPair::initial; 3406 3407 // ========================= SWNodeInfo ===================== 3408 3409 const SWNodeInfo SWNodeInfo::initial; 3410 3411 3412 // ============================ DepGraph =========================== 3413 3414 //------------------------------make_node--------------------------- 3415 // Make a new dependence graph node for an ideal node. 3416 DepMem* DepGraph::make_node(Node* node) { 3417 DepMem* m = new (_arena) DepMem(node); 3418 if (node != NULL) { 3419 assert(_map.at_grow(node->_idx) == NULL, "one init only"); 3420 _map.at_put_grow(node->_idx, m); 3421 } 3422 return m; 3423 } 3424 3425 //------------------------------make_edge--------------------------- 3426 // Make a new dependence graph edge from dpred -> dsucc 3427 DepEdge* DepGraph::make_edge(DepMem* dpred, DepMem* dsucc) { 3428 DepEdge* e = new (_arena) DepEdge(dpred, dsucc, dsucc->in_head(), dpred->out_head()); 3429 dpred->set_out_head(e); 3430 dsucc->set_in_head(e); 3431 return e; 3432 } 3433 3434 // ========================== DepMem ======================== 3435 3436 //------------------------------in_cnt--------------------------- 3437 int DepMem::in_cnt() { 3438 int ct = 0; 3439 for (DepEdge* e = _in_head; e != NULL; e = e->next_in()) ct++; 3440 return ct; 3441 } 3442 3443 //------------------------------out_cnt--------------------------- 3444 int DepMem::out_cnt() { 3445 int ct = 0; 3446 for (DepEdge* e = _out_head; e != NULL; e = e->next_out()) ct++; 3447 return ct; 3448 } 3449 3450 //------------------------------print----------------------------- 3451 void DepMem::print() { 3452 #ifndef PRODUCT 3453 tty->print(" DepNode %d (", _node->_idx); 3454 for (DepEdge* p = _in_head; p != NULL; p = p->next_in()) { 3455 Node* pred = p->pred()->node(); 3456 tty->print(" %d", pred != NULL ? pred->_idx : 0); 3457 } 3458 tty->print(") ["); 3459 for (DepEdge* s = _out_head; s != NULL; s = s->next_out()) { 3460 Node* succ = s->succ()->node(); 3461 tty->print(" %d", succ != NULL ? succ->_idx : 0); 3462 } 3463 tty->print_cr(" ]"); 3464 #endif 3465 } 3466 3467 // =========================== DepEdge ========================= 3468 3469 //------------------------------DepPreds--------------------------- 3470 void DepEdge::print() { 3471 #ifndef PRODUCT 3472 tty->print_cr("DepEdge: %d [ %d ]", _pred->node()->_idx, _succ->node()->_idx); 3473 #endif 3474 } 3475 3476 // =========================== DepPreds ========================= 3477 // Iterator over predecessor edges in the dependence graph. 3478 3479 //------------------------------DepPreds--------------------------- 3480 DepPreds::DepPreds(Node* n, DepGraph& dg) { 3481 _n = n; 3482 _done = false; 3483 if (_n->is_Store() || _n->is_Load()) { 3484 _next_idx = MemNode::Address; 3485 _end_idx = n->req(); 3486 _dep_next = dg.dep(_n)->in_head(); 3487 } else if (_n->is_Mem()) { 3488 _next_idx = 0; 3489 _end_idx = 0; 3490 _dep_next = dg.dep(_n)->in_head(); 3491 } else { 3492 _next_idx = 1; 3493 _end_idx = _n->req(); 3494 _dep_next = NULL; 3495 } 3496 next(); 3497 } 3498 3499 //------------------------------next--------------------------- 3500 void DepPreds::next() { 3501 if (_dep_next != NULL) { 3502 _current = _dep_next->pred()->node(); 3503 _dep_next = _dep_next->next_in(); 3504 } else if (_next_idx < _end_idx) { 3505 _current = _n->in(_next_idx++); 3506 } else { 3507 _done = true; 3508 } 3509 } 3510 3511 // =========================== DepSuccs ========================= 3512 // Iterator over successor edges in the dependence graph. 3513 3514 //------------------------------DepSuccs--------------------------- 3515 DepSuccs::DepSuccs(Node* n, DepGraph& dg) { 3516 _n = n; 3517 _done = false; 3518 if (_n->is_Load()) { 3519 _next_idx = 0; 3520 _end_idx = _n->outcnt(); 3521 _dep_next = dg.dep(_n)->out_head(); 3522 } else if (_n->is_Mem() || _n->is_Phi() && _n->bottom_type() == Type::MEMORY) { 3523 _next_idx = 0; 3524 _end_idx = 0; 3525 _dep_next = dg.dep(_n)->out_head(); 3526 } else { 3527 _next_idx = 0; 3528 _end_idx = _n->outcnt(); 3529 _dep_next = NULL; 3530 } 3531 next(); 3532 } 3533 3534 //-------------------------------next--------------------------- 3535 void DepSuccs::next() { 3536 if (_dep_next != NULL) { 3537 _current = _dep_next->succ()->node(); 3538 _dep_next = _dep_next->next_out(); 3539 } else if (_next_idx < _end_idx) { 3540 _current = _n->raw_out(_next_idx++); 3541 } else { 3542 _done = true; 3543 } 3544 } 3545 3546 // 3547 // --------------------------------- vectorization/simd ----------------------------------- 3548 // 3549 bool SuperWord::same_origin_idx(Node* a, Node* b) const { 3550 return a != NULL && b != NULL && _clone_map.same_idx(a->_idx, b->_idx); 3551 } 3552 bool SuperWord::same_generation(Node* a, Node* b) const { 3553 return a != NULL && b != NULL && _clone_map.same_gen(a->_idx, b->_idx); 3554 } 3555 3556 Node* SuperWord::find_phi_for_mem_dep(LoadNode* ld) { 3557 assert(in_bb(ld), "must be in block"); 3558 if (_clone_map.gen(ld->_idx) == _ii_first) { 3559 #ifndef PRODUCT 3560 if (_vector_loop_debug) { 3561 tty->print_cr("SuperWord::find_phi_for_mem_dep _clone_map.gen(ld->_idx)=%d", 3562 _clone_map.gen(ld->_idx)); 3563 } 3564 #endif 3565 return NULL; //we think that any ld in the first gen being vectorizable 3566 } 3567 3568 Node* mem = ld->in(MemNode::Memory); 3569 if (mem->outcnt() <= 1) { 3570 // we don't want to remove the only edge from mem node to load 3571 #ifndef PRODUCT 3572 if (_vector_loop_debug) { 3573 tty->print_cr("SuperWord::find_phi_for_mem_dep input node %d to load %d has no other outputs and edge mem->load cannot be removed", 3574 mem->_idx, ld->_idx); 3575 ld->dump(); 3576 mem->dump(); 3577 } 3578 #endif 3579 return NULL; 3580 } 3581 if (!in_bb(mem) || same_generation(mem, ld)) { 3582 #ifndef PRODUCT 3583 if (_vector_loop_debug) { 3584 tty->print_cr("SuperWord::find_phi_for_mem_dep _clone_map.gen(mem->_idx)=%d", 3585 _clone_map.gen(mem->_idx)); 3586 } 3587 #endif 3588 return NULL; // does not depend on loop volatile node or depends on the same generation 3589 } 3590 3591 //otherwise first node should depend on mem-phi 3592 Node* first = first_node(ld); 3593 assert(first->is_Load(), "must be Load"); 3594 Node* phi = first->as_Load()->in(MemNode::Memory); 3595 if (!phi->is_Phi() || phi->bottom_type() != Type::MEMORY) { 3596 #ifndef PRODUCT 3597 if (_vector_loop_debug) { 3598 tty->print_cr("SuperWord::find_phi_for_mem_dep load is not vectorizable node, since it's `first` does not take input from mem phi"); 3599 ld->dump(); 3600 first->dump(); 3601 } 3602 #endif 3603 return NULL; 3604 } 3605 3606 Node* tail = 0; 3607 for (int m = 0; m < _mem_slice_head.length(); m++) { 3608 if (_mem_slice_head.at(m) == phi) { 3609 tail = _mem_slice_tail.at(m); 3610 } 3611 } 3612 if (tail == 0) { //test that found phi is in the list _mem_slice_head 3613 #ifndef PRODUCT 3614 if (_vector_loop_debug) { 3615 tty->print_cr("SuperWord::find_phi_for_mem_dep load %d is not vectorizable node, its phi %d is not _mem_slice_head", 3616 ld->_idx, phi->_idx); 3617 ld->dump(); 3618 phi->dump(); 3619 } 3620 #endif 3621 return NULL; 3622 } 3623 3624 // now all conditions are met 3625 return phi; 3626 } 3627 3628 Node* SuperWord::first_node(Node* nd) { 3629 for (int ii = 0; ii < _iteration_first.length(); ii++) { 3630 Node* nnn = _iteration_first.at(ii); 3631 if (same_origin_idx(nnn, nd)) { 3632 #ifndef PRODUCT 3633 if (_vector_loop_debug) { 3634 tty->print_cr("SuperWord::first_node: %d is the first iteration node for %d (_clone_map.idx(nnn->_idx) = %d)", 3635 nnn->_idx, nd->_idx, _clone_map.idx(nnn->_idx)); 3636 } 3637 #endif 3638 return nnn; 3639 } 3640 } 3641 3642 #ifndef PRODUCT 3643 if (_vector_loop_debug) { 3644 tty->print_cr("SuperWord::first_node: did not find first iteration node for %d (_clone_map.idx(nd->_idx)=%d)", 3645 nd->_idx, _clone_map.idx(nd->_idx)); 3646 } 3647 #endif 3648 return 0; 3649 } 3650 3651 Node* SuperWord::last_node(Node* nd) { 3652 for (int ii = 0; ii < _iteration_last.length(); ii++) { 3653 Node* nnn = _iteration_last.at(ii); 3654 if (same_origin_idx(nnn, nd)) { 3655 #ifndef PRODUCT 3656 if (_vector_loop_debug) { 3657 tty->print_cr("SuperWord::last_node _clone_map.idx(nnn->_idx)=%d, _clone_map.idx(nd->_idx)=%d", 3658 _clone_map.idx(nnn->_idx), _clone_map.idx(nd->_idx)); 3659 } 3660 #endif 3661 return nnn; 3662 } 3663 } 3664 return 0; 3665 } 3666 3667 int SuperWord::mark_generations() { 3668 Node *ii_err = 0, *tail_err; 3669 for (int i = 0; i < _mem_slice_head.length(); i++) { 3670 Node* phi = _mem_slice_head.at(i); 3671 assert(phi->is_Phi(), "must be phi"); 3672 3673 Node* tail = _mem_slice_tail.at(i); 3674 if (_ii_last == -1) { 3675 tail_err = tail; 3676 _ii_last = _clone_map.gen(tail->_idx); 3677 } 3678 else if (_ii_last != _clone_map.gen(tail->_idx)) { 3679 #ifndef PRODUCT 3680 if (TraceSuperWord && Verbose) { 3681 tty->print_cr("SuperWord::mark_generations _ii_last error - found different generations in two tail nodes "); 3682 tail->dump(); 3683 tail_err->dump(); 3684 } 3685 #endif 3686 return -1; 3687 } 3688 3689 // find first iteration in the loop 3690 for (DUIterator_Fast imax, i = phi->fast_outs(imax); i < imax; i++) { 3691 Node* ii = phi->fast_out(i); 3692 if (in_bb(ii) && ii->is_Store()) { // we speculate that normally Stores of one and one only generation have deps from mem phi 3693 if (_ii_first == -1) { 3694 ii_err = ii; 3695 _ii_first = _clone_map.gen(ii->_idx); 3696 } else if (_ii_first != _clone_map.gen(ii->_idx)) { 3697 #ifndef PRODUCT 3698 if (TraceSuperWord && Verbose) { 3699 tty->print_cr("SuperWord::mark_generations: _ii_first was found before and not equal to one in this node (%d)", _ii_first); 3700 ii->dump(); 3701 if (ii_err!= 0) { 3702 ii_err->dump(); 3703 } 3704 } 3705 #endif 3706 return -1; // this phi has Stores from different generations of unroll and cannot be simd/vectorized 3707 } 3708 } 3709 }//for (DUIterator_Fast imax, 3710 }//for (int i... 3711 3712 if (_ii_first == -1 || _ii_last == -1) { 3713 #ifndef PRODUCT 3714 if (TraceSuperWord && Verbose) { 3715 tty->print_cr("SuperWord::mark_generations unknown error, something vent wrong"); 3716 } 3717 #endif 3718 return -1; // something vent wrong 3719 } 3720 // collect nodes in the first and last generations 3721 assert(_iteration_first.length() == 0, "_iteration_first must be empty"); 3722 assert(_iteration_last.length() == 0, "_iteration_last must be empty"); 3723 for (int j = 0; j < _block.length(); j++) { 3724 Node* n = _block.at(j); 3725 node_idx_t gen = _clone_map.gen(n->_idx); 3726 if ((signed)gen == _ii_first) { 3727 _iteration_first.push(n); 3728 } else if ((signed)gen == _ii_last) { 3729 _iteration_last.push(n); 3730 } 3731 } 3732 3733 // building order of iterations 3734 if (_ii_order.length() == 0 && ii_err != 0) { 3735 assert(in_bb(ii_err) && ii_err->is_Store(), "should be Store in bb"); 3736 Node* nd = ii_err; 3737 while(_clone_map.gen(nd->_idx) != _ii_last) { 3738 _ii_order.push(_clone_map.gen(nd->_idx)); 3739 bool found = false; 3740 for (DUIterator_Fast imax, i = nd->fast_outs(imax); i < imax; i++) { 3741 Node* use = nd->fast_out(i); 3742 if (same_origin_idx(use, nd) && use->as_Store()->in(MemNode::Memory) == nd) { 3743 found = true; 3744 nd = use; 3745 break; 3746 } 3747 }//for 3748 3749 if (found == false) { 3750 #ifndef PRODUCT 3751 if (TraceSuperWord && Verbose) { 3752 tty->print_cr("SuperWord::mark_generations: Cannot build order of iterations - no dependent Store for %d", nd->_idx); 3753 } 3754 #endif 3755 _ii_order.clear(); 3756 return -1; 3757 } 3758 } //while 3759 _ii_order.push(_clone_map.gen(nd->_idx)); 3760 } 3761 3762 #ifndef PRODUCT 3763 if (_vector_loop_debug) { 3764 tty->print_cr("SuperWord::mark_generations"); 3765 tty->print_cr("First generation (%d) nodes:", _ii_first); 3766 for (int ii = 0; ii < _iteration_first.length(); ii++) _iteration_first.at(ii)->dump(); 3767 tty->print_cr("Last generation (%d) nodes:", _ii_last); 3768 for (int ii = 0; ii < _iteration_last.length(); ii++) _iteration_last.at(ii)->dump(); 3769 tty->print_cr(" "); 3770 3771 tty->print("SuperWord::List of generations: "); 3772 for (int jj = 0; jj < _ii_order.length(); ++jj) { 3773 tty->print("%d:%d ", jj, _ii_order.at(jj)); 3774 } 3775 tty->print_cr(" "); 3776 } 3777 #endif 3778 3779 return _ii_first; 3780 } 3781 3782 bool SuperWord::fix_commutative_inputs(Node* gold, Node* fix) { 3783 assert(gold->is_Add() && fix->is_Add() || gold->is_Mul() && fix->is_Mul(), "should be only Add or Mul nodes"); 3784 assert(same_origin_idx(gold, fix), "should be clones of the same node"); 3785 Node* gin1 = gold->in(1); 3786 Node* gin2 = gold->in(2); 3787 Node* fin1 = fix->in(1); 3788 Node* fin2 = fix->in(2); 3789 bool swapped = false; 3790 3791 if (in_bb(gin1) && in_bb(gin2) && in_bb(fin1) && in_bb(fin1)) { 3792 if (same_origin_idx(gin1, fin1) && 3793 same_origin_idx(gin2, fin2)) { 3794 return true; // nothing to fix 3795 } 3796 if (same_origin_idx(gin1, fin2) && 3797 same_origin_idx(gin2, fin1)) { 3798 fix->swap_edges(1, 2); 3799 swapped = true; 3800 } 3801 } 3802 // at least one input comes from outside of bb 3803 if (gin1->_idx == fin1->_idx) { 3804 return true; // nothing to fix 3805 } 3806 if (!swapped && (gin1->_idx == fin2->_idx || gin2->_idx == fin1->_idx)) { //swapping is expensive, check condition first 3807 fix->swap_edges(1, 2); 3808 swapped = true; 3809 } 3810 3811 if (swapped) { 3812 #ifndef PRODUCT 3813 if (_vector_loop_debug) { 3814 tty->print_cr("SuperWord::fix_commutative_inputs: fixed node %d", fix->_idx); 3815 } 3816 #endif 3817 return true; 3818 } 3819 3820 #ifndef PRODUCT 3821 if (TraceSuperWord && Verbose) { 3822 tty->print_cr("SuperWord::fix_commutative_inputs: cannot fix node %d", fix->_idx); 3823 } 3824 #endif 3825 return false; 3826 } 3827 3828 bool SuperWord::pack_parallel() { 3829 #ifndef PRODUCT 3830 if (_vector_loop_debug) { 3831 tty->print_cr("SuperWord::pack_parallel: START"); 3832 } 3833 #endif 3834 3835 _packset.clear(); 3836 3837 for (int ii = 0; ii < _iteration_first.length(); ii++) { 3838 Node* nd = _iteration_first.at(ii); 3839 if (in_bb(nd) && (nd->is_Load() || nd->is_Store() || nd->is_Add() || nd->is_Mul())) { 3840 Node_List* pk = new Node_List(); 3841 pk->push(nd); 3842 for (int gen = 1; gen < _ii_order.length(); ++gen) { 3843 for (int kk = 0; kk < _block.length(); kk++) { 3844 Node* clone = _block.at(kk); 3845 if (same_origin_idx(clone, nd) && 3846 _clone_map.gen(clone->_idx) == _ii_order.at(gen)) { 3847 if (nd->is_Add() || nd->is_Mul()) { 3848 fix_commutative_inputs(nd, clone); 3849 } 3850 pk->push(clone); 3851 if (pk->size() == 4) { 3852 _packset.append(pk); 3853 #ifndef PRODUCT 3854 if (_vector_loop_debug) { 3855 tty->print_cr("SuperWord::pack_parallel: added pack "); 3856 pk->dump(); 3857 } 3858 #endif 3859 if (_clone_map.gen(clone->_idx) != _ii_last) { 3860 pk = new Node_List(); 3861 } 3862 } 3863 break; 3864 } 3865 } 3866 }//for 3867 }//if 3868 }//for 3869 3870 #ifndef PRODUCT 3871 if (_vector_loop_debug) { 3872 tty->print_cr("SuperWord::pack_parallel: END"); 3873 } 3874 #endif 3875 3876 return true; 3877 } 3878 3879 bool SuperWord::hoist_loads_in_graph() { 3880 GrowableArray<Node*> loads; 3881 3882 #ifndef PRODUCT 3883 if (_vector_loop_debug) { 3884 tty->print_cr("SuperWord::hoist_loads_in_graph: total number _mem_slice_head.length() = %d", _mem_slice_head.length()); 3885 } 3886 #endif 3887 3888 for (int i = 0; i < _mem_slice_head.length(); i++) { 3889 Node* n = _mem_slice_head.at(i); 3890 if ( !in_bb(n) || !n->is_Phi() || n->bottom_type() != Type::MEMORY) { 3891 #ifndef PRODUCT 3892 if (TraceSuperWord && Verbose) { 3893 tty->print_cr("SuperWord::hoist_loads_in_graph: skipping unexpected node n=%d", n->_idx); 3894 } 3895 #endif 3896 continue; 3897 } 3898 3899 #ifndef PRODUCT 3900 if (_vector_loop_debug) { 3901 tty->print_cr("SuperWord::hoist_loads_in_graph: processing phi %d = _mem_slice_head.at(%d);", n->_idx, i); 3902 } 3903 #endif 3904 3905 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) { 3906 Node* ld = n->fast_out(i); 3907 if (ld->is_Load() && ld->as_Load()->in(MemNode::Memory) == n && in_bb(ld)) { 3908 for (int i = 0; i < _block.length(); i++) { 3909 Node* ld2 = _block.at(i); 3910 if (ld2->is_Load() && same_origin_idx(ld, ld2) && 3911 !same_generation(ld, ld2)) { // <= do not collect the first generation ld 3912 #ifndef PRODUCT 3913 if (_vector_loop_debug) { 3914 tty->print_cr("SuperWord::hoist_loads_in_graph: will try to hoist load ld2->_idx=%d, cloned from %d (ld->_idx=%d)", 3915 ld2->_idx, _clone_map.idx(ld->_idx), ld->_idx); 3916 } 3917 #endif 3918 // could not do on-the-fly, since iterator is immutable 3919 loads.push(ld2); 3920 } 3921 }// for 3922 }//if 3923 }//for (DUIterator_Fast imax, 3924 }//for (int i = 0; i 3925 3926 for (int i = 0; i < loads.length(); i++) { 3927 LoadNode* ld = loads.at(i)->as_Load(); 3928 Node* phi = find_phi_for_mem_dep(ld); 3929 if (phi != NULL) { 3930 #ifndef PRODUCT 3931 if (_vector_loop_debug) { 3932 tty->print_cr("SuperWord::hoist_loads_in_graph replacing MemNode::Memory(%d) edge in %d with one from %d", 3933 MemNode::Memory, ld->_idx, phi->_idx); 3934 } 3935 #endif 3936 _igvn.replace_input_of(ld, MemNode::Memory, phi); 3937 } 3938 }//for 3939 3940 restart(); // invalidate all basic structures, since we rebuilt the graph 3941 3942 #ifndef PRODUCT 3943 if (TraceSuperWord && Verbose) { 3944 tty->print_cr("\nSuperWord::hoist_loads_in_graph() the graph was rebuilt, all structures invalidated and need rebuild"); 3945 } 3946 #endif 3947 return true; 3948 } 3949