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