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