src/share/vm/opto/superword.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6639
42274879e644
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 #include "precompiled.hpp"
aoqi@0 25 #include "compiler/compileLog.hpp"
aoqi@0 26 #include "libadt/vectset.hpp"
aoqi@0 27 #include "memory/allocation.inline.hpp"
aoqi@0 28 #include "opto/addnode.hpp"
aoqi@0 29 #include "opto/callnode.hpp"
aoqi@0 30 #include "opto/divnode.hpp"
aoqi@0 31 #include "opto/matcher.hpp"
aoqi@0 32 #include "opto/memnode.hpp"
aoqi@0 33 #include "opto/mulnode.hpp"
aoqi@0 34 #include "opto/opcodes.hpp"
aoqi@0 35 #include "opto/superword.hpp"
aoqi@0 36 #include "opto/vectornode.hpp"
aoqi@0 37
aoqi@0 38 //
aoqi@0 39 // S U P E R W O R D T R A N S F O R M
aoqi@0 40 //=============================================================================
aoqi@0 41
aoqi@0 42 //------------------------------SuperWord---------------------------
aoqi@0 43 SuperWord::SuperWord(PhaseIdealLoop* phase) :
aoqi@0 44 _phase(phase),
aoqi@0 45 _igvn(phase->_igvn),
aoqi@0 46 _arena(phase->C->comp_arena()),
aoqi@0 47 _packset(arena(), 8, 0, NULL), // packs for the current block
aoqi@0 48 _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb
aoqi@0 49 _block(arena(), 8, 0, NULL), // nodes in current block
aoqi@0 50 _data_entry(arena(), 8, 0, NULL), // nodes with all inputs from outside
aoqi@0 51 _mem_slice_head(arena(), 8, 0, NULL), // memory slice heads
aoqi@0 52 _mem_slice_tail(arena(), 8, 0, NULL), // memory slice tails
aoqi@0 53 _node_info(arena(), 8, 0, SWNodeInfo::initial), // info needed per node
aoqi@0 54 _align_to_ref(NULL), // memory reference to align vectors to
aoqi@0 55 _disjoint_ptrs(arena(), 8, 0, OrderedPair::initial), // runtime disambiguated pointer pairs
aoqi@0 56 _dg(_arena), // dependence graph
aoqi@0 57 _visited(arena()), // visited node set
aoqi@0 58 _post_visited(arena()), // post visited node set
aoqi@0 59 _n_idx_list(arena(), 8), // scratch list of (node,index) pairs
aoqi@0 60 _stk(arena(), 8, 0, NULL), // scratch stack of nodes
aoqi@0 61 _nlist(arena(), 8, 0, NULL), // scratch list of nodes
aoqi@0 62 _lpt(NULL), // loop tree node
aoqi@0 63 _lp(NULL), // LoopNode
aoqi@0 64 _bb(NULL), // basic block
aoqi@0 65 _iv(NULL) // induction var
aoqi@0 66 {}
aoqi@0 67
aoqi@0 68 //------------------------------transform_loop---------------------------
aoqi@0 69 void SuperWord::transform_loop(IdealLoopTree* lpt) {
aoqi@0 70 assert(UseSuperWord, "should be");
aoqi@0 71 // Do vectors exist on this architecture?
aoqi@0 72 if (Matcher::vector_width_in_bytes(T_BYTE) < 2) return;
aoqi@0 73
aoqi@0 74 assert(lpt->_head->is_CountedLoop(), "must be");
aoqi@0 75 CountedLoopNode *cl = lpt->_head->as_CountedLoop();
aoqi@0 76
aoqi@0 77 if (!cl->is_valid_counted_loop()) return; // skip malformed counted loop
aoqi@0 78
aoqi@0 79 if (!cl->is_main_loop() ) return; // skip normal, pre, and post loops
aoqi@0 80
aoqi@0 81 // Check for no control flow in body (other than exit)
aoqi@0 82 Node *cl_exit = cl->loopexit();
aoqi@0 83 if (cl_exit->in(0) != lpt->_head) return;
aoqi@0 84
aoqi@0 85 // Make sure the are no extra control users of the loop backedge
aoqi@0 86 if (cl->back_control()->outcnt() != 1) {
aoqi@0 87 return;
aoqi@0 88 }
aoqi@0 89
aoqi@0 90 // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
aoqi@0 91 CountedLoopEndNode* pre_end = get_pre_loop_end(cl);
aoqi@0 92 if (pre_end == NULL) return;
aoqi@0 93 Node *pre_opaq1 = pre_end->limit();
aoqi@0 94 if (pre_opaq1->Opcode() != Op_Opaque1) return;
aoqi@0 95
aoqi@0 96 init(); // initialize data structures
aoqi@0 97
aoqi@0 98 set_lpt(lpt);
aoqi@0 99 set_lp(cl);
aoqi@0 100
aoqi@0 101 // For now, define one block which is the entire loop body
aoqi@0 102 set_bb(cl);
aoqi@0 103
aoqi@0 104 assert(_packset.length() == 0, "packset must be empty");
aoqi@0 105 SLP_extract();
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 //------------------------------SLP_extract---------------------------
aoqi@0 109 // Extract the superword level parallelism
aoqi@0 110 //
aoqi@0 111 // 1) A reverse post-order of nodes in the block is constructed. By scanning
aoqi@0 112 // this list from first to last, all definitions are visited before their uses.
aoqi@0 113 //
aoqi@0 114 // 2) A point-to-point dependence graph is constructed between memory references.
aoqi@0 115 // This simplies the upcoming "independence" checker.
aoqi@0 116 //
aoqi@0 117 // 3) The maximum depth in the node graph from the beginning of the block
aoqi@0 118 // to each node is computed. This is used to prune the graph search
aoqi@0 119 // in the independence checker.
aoqi@0 120 //
aoqi@0 121 // 4) For integer types, the necessary bit width is propagated backwards
aoqi@0 122 // from stores to allow packed operations on byte, char, and short
aoqi@0 123 // integers. This reverses the promotion to type "int" that javac
aoqi@0 124 // did for operations like: char c1,c2,c3; c1 = c2 + c3.
aoqi@0 125 //
aoqi@0 126 // 5) One of the memory references is picked to be an aligned vector reference.
aoqi@0 127 // The pre-loop trip count is adjusted to align this reference in the
aoqi@0 128 // unrolled body.
aoqi@0 129 //
aoqi@0 130 // 6) The initial set of pack pairs is seeded with memory references.
aoqi@0 131 //
aoqi@0 132 // 7) The set of pack pairs is extended by following use->def and def->use links.
aoqi@0 133 //
aoqi@0 134 // 8) The pairs are combined into vector sized packs.
aoqi@0 135 //
aoqi@0 136 // 9) Reorder the memory slices to co-locate members of the memory packs.
aoqi@0 137 //
aoqi@0 138 // 10) Generate ideal vector nodes for the final set of packs and where necessary,
aoqi@0 139 // inserting scalar promotion, vector creation from multiple scalars, and
aoqi@0 140 // extraction of scalar values from vectors.
aoqi@0 141 //
aoqi@0 142 void SuperWord::SLP_extract() {
aoqi@0 143
aoqi@0 144 // Ready the block
aoqi@0 145
aoqi@0 146 if (!construct_bb())
aoqi@0 147 return; // Exit if no interesting nodes or complex graph.
aoqi@0 148
aoqi@0 149 dependence_graph();
aoqi@0 150
aoqi@0 151 compute_max_depth();
aoqi@0 152
aoqi@0 153 compute_vector_element_type();
aoqi@0 154
aoqi@0 155 // Attempt vectorization
aoqi@0 156
aoqi@0 157 find_adjacent_refs();
aoqi@0 158
aoqi@0 159 extend_packlist();
aoqi@0 160
aoqi@0 161 combine_packs();
aoqi@0 162
aoqi@0 163 construct_my_pack_map();
aoqi@0 164
aoqi@0 165 filter_packs();
aoqi@0 166
aoqi@0 167 schedule();
aoqi@0 168
aoqi@0 169 output();
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 //------------------------------find_adjacent_refs---------------------------
aoqi@0 173 // Find the adjacent memory references and create pack pairs for them.
aoqi@0 174 // This is the initial set of packs that will then be extended by
aoqi@0 175 // following use->def and def->use links. The align positions are
aoqi@0 176 // assigned relative to the reference "align_to_ref"
aoqi@0 177 void SuperWord::find_adjacent_refs() {
aoqi@0 178 // Get list of memory operations
aoqi@0 179 Node_List memops;
aoqi@0 180 for (int i = 0; i < _block.length(); i++) {
aoqi@0 181 Node* n = _block.at(i);
aoqi@0 182 if (n->is_Mem() && !n->is_LoadStore() && in_bb(n) &&
aoqi@0 183 is_java_primitive(n->as_Mem()->memory_type())) {
aoqi@0 184 int align = memory_alignment(n->as_Mem(), 0);
aoqi@0 185 if (align != bottom_align) {
aoqi@0 186 memops.push(n);
aoqi@0 187 }
aoqi@0 188 }
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 Node_List align_to_refs;
aoqi@0 192 int best_iv_adjustment = 0;
aoqi@0 193 MemNode* best_align_to_mem_ref = NULL;
aoqi@0 194
aoqi@0 195 while (memops.size() != 0) {
aoqi@0 196 // Find a memory reference to align to.
aoqi@0 197 MemNode* mem_ref = find_align_to_ref(memops);
aoqi@0 198 if (mem_ref == NULL) break;
aoqi@0 199 align_to_refs.push(mem_ref);
aoqi@0 200 int iv_adjustment = get_iv_adjustment(mem_ref);
aoqi@0 201
aoqi@0 202 if (best_align_to_mem_ref == NULL) {
aoqi@0 203 // Set memory reference which is the best from all memory operations
aoqi@0 204 // to be used for alignment. The pre-loop trip count is modified to align
aoqi@0 205 // this reference to a vector-aligned address.
aoqi@0 206 best_align_to_mem_ref = mem_ref;
aoqi@0 207 best_iv_adjustment = iv_adjustment;
aoqi@0 208 }
aoqi@0 209
aoqi@0 210 SWPointer align_to_ref_p(mem_ref, this);
aoqi@0 211 // Set alignment relative to "align_to_ref" for all related memory operations.
aoqi@0 212 for (int i = memops.size() - 1; i >= 0; i--) {
aoqi@0 213 MemNode* s = memops.at(i)->as_Mem();
aoqi@0 214 if (isomorphic(s, mem_ref)) {
aoqi@0 215 SWPointer p2(s, this);
aoqi@0 216 if (p2.comparable(align_to_ref_p)) {
aoqi@0 217 int align = memory_alignment(s, iv_adjustment);
aoqi@0 218 set_alignment(s, align);
aoqi@0 219 }
aoqi@0 220 }
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 // Create initial pack pairs of memory operations for which
aoqi@0 224 // alignment is set and vectors will be aligned.
aoqi@0 225 bool create_pack = true;
aoqi@0 226 if (memory_alignment(mem_ref, best_iv_adjustment) == 0) {
aoqi@0 227 if (!Matcher::misaligned_vectors_ok()) {
aoqi@0 228 int vw = vector_width(mem_ref);
aoqi@0 229 int vw_best = vector_width(best_align_to_mem_ref);
aoqi@0 230 if (vw > vw_best) {
aoqi@0 231 // Do not vectorize a memory access with more elements per vector
aoqi@0 232 // if unaligned memory access is not allowed because number of
aoqi@0 233 // iterations in pre-loop will be not enough to align it.
aoqi@0 234 create_pack = false;
aoqi@0 235 }
aoqi@0 236 }
aoqi@0 237 } else {
aoqi@0 238 if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
aoqi@0 239 // Can't allow vectorization of unaligned memory accesses with the
aoqi@0 240 // same type since it could be overlapped accesses to the same array.
aoqi@0 241 create_pack = false;
aoqi@0 242 } else {
aoqi@0 243 // Allow independent (different type) unaligned memory operations
aoqi@0 244 // if HW supports them.
aoqi@0 245 if (!Matcher::misaligned_vectors_ok()) {
aoqi@0 246 create_pack = false;
aoqi@0 247 } else {
aoqi@0 248 // Check if packs of the same memory type but
aoqi@0 249 // with a different alignment were created before.
aoqi@0 250 for (uint i = 0; i < align_to_refs.size(); i++) {
aoqi@0 251 MemNode* mr = align_to_refs.at(i)->as_Mem();
aoqi@0 252 if (same_velt_type(mr, mem_ref) &&
aoqi@0 253 memory_alignment(mr, iv_adjustment) != 0)
aoqi@0 254 create_pack = false;
aoqi@0 255 }
aoqi@0 256 }
aoqi@0 257 }
aoqi@0 258 }
aoqi@0 259 if (create_pack) {
aoqi@0 260 for (uint i = 0; i < memops.size(); i++) {
aoqi@0 261 Node* s1 = memops.at(i);
aoqi@0 262 int align = alignment(s1);
aoqi@0 263 if (align == top_align) continue;
aoqi@0 264 for (uint j = 0; j < memops.size(); j++) {
aoqi@0 265 Node* s2 = memops.at(j);
aoqi@0 266 if (alignment(s2) == top_align) continue;
aoqi@0 267 if (s1 != s2 && are_adjacent_refs(s1, s2)) {
aoqi@0 268 if (stmts_can_pack(s1, s2, align)) {
aoqi@0 269 Node_List* pair = new Node_List();
aoqi@0 270 pair->push(s1);
aoqi@0 271 pair->push(s2);
aoqi@0 272 _packset.append(pair);
aoqi@0 273 }
aoqi@0 274 }
aoqi@0 275 }
aoqi@0 276 }
aoqi@0 277 } else { // Don't create unaligned pack
aoqi@0 278 // First, remove remaining memory ops of the same type from the list.
aoqi@0 279 for (int i = memops.size() - 1; i >= 0; i--) {
aoqi@0 280 MemNode* s = memops.at(i)->as_Mem();
aoqi@0 281 if (same_velt_type(s, mem_ref)) {
aoqi@0 282 memops.remove(i);
aoqi@0 283 }
aoqi@0 284 }
aoqi@0 285
aoqi@0 286 // Second, remove already constructed packs of the same type.
aoqi@0 287 for (int i = _packset.length() - 1; i >= 0; i--) {
aoqi@0 288 Node_List* p = _packset.at(i);
aoqi@0 289 MemNode* s = p->at(0)->as_Mem();
aoqi@0 290 if (same_velt_type(s, mem_ref)) {
aoqi@0 291 remove_pack_at(i);
aoqi@0 292 }
aoqi@0 293 }
aoqi@0 294
aoqi@0 295 // If needed find the best memory reference for loop alignment again.
aoqi@0 296 if (same_velt_type(mem_ref, best_align_to_mem_ref)) {
aoqi@0 297 // Put memory ops from remaining packs back on memops list for
aoqi@0 298 // the best alignment search.
aoqi@0 299 uint orig_msize = memops.size();
aoqi@0 300 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 301 Node_List* p = _packset.at(i);
aoqi@0 302 MemNode* s = p->at(0)->as_Mem();
aoqi@0 303 assert(!same_velt_type(s, mem_ref), "sanity");
aoqi@0 304 memops.push(s);
aoqi@0 305 }
aoqi@0 306 MemNode* best_align_to_mem_ref = find_align_to_ref(memops);
aoqi@0 307 if (best_align_to_mem_ref == NULL) break;
aoqi@0 308 best_iv_adjustment = get_iv_adjustment(best_align_to_mem_ref);
aoqi@0 309 // Restore list.
aoqi@0 310 while (memops.size() > orig_msize)
aoqi@0 311 (void)memops.pop();
aoqi@0 312 }
aoqi@0 313 } // unaligned memory accesses
aoqi@0 314
aoqi@0 315 // Remove used mem nodes.
aoqi@0 316 for (int i = memops.size() - 1; i >= 0; i--) {
aoqi@0 317 MemNode* m = memops.at(i)->as_Mem();
aoqi@0 318 if (alignment(m) != top_align) {
aoqi@0 319 memops.remove(i);
aoqi@0 320 }
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 } // while (memops.size() != 0
aoqi@0 324 set_align_to_ref(best_align_to_mem_ref);
aoqi@0 325
aoqi@0 326 #ifndef PRODUCT
aoqi@0 327 if (TraceSuperWord) {
aoqi@0 328 tty->print_cr("\nAfter find_adjacent_refs");
aoqi@0 329 print_packset();
aoqi@0 330 }
aoqi@0 331 #endif
aoqi@0 332 }
aoqi@0 333
aoqi@0 334 //------------------------------find_align_to_ref---------------------------
aoqi@0 335 // Find a memory reference to align the loop induction variable to.
aoqi@0 336 // Looks first at stores then at loads, looking for a memory reference
aoqi@0 337 // with the largest number of references similar to it.
aoqi@0 338 MemNode* SuperWord::find_align_to_ref(Node_List &memops) {
aoqi@0 339 GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0);
aoqi@0 340
aoqi@0 341 // Count number of comparable memory ops
aoqi@0 342 for (uint i = 0; i < memops.size(); i++) {
aoqi@0 343 MemNode* s1 = memops.at(i)->as_Mem();
aoqi@0 344 SWPointer p1(s1, this);
aoqi@0 345 // Discard if pre loop can't align this reference
aoqi@0 346 if (!ref_is_alignable(p1)) {
aoqi@0 347 *cmp_ct.adr_at(i) = 0;
aoqi@0 348 continue;
aoqi@0 349 }
aoqi@0 350 for (uint j = i+1; j < memops.size(); j++) {
aoqi@0 351 MemNode* s2 = memops.at(j)->as_Mem();
aoqi@0 352 if (isomorphic(s1, s2)) {
aoqi@0 353 SWPointer p2(s2, this);
aoqi@0 354 if (p1.comparable(p2)) {
aoqi@0 355 (*cmp_ct.adr_at(i))++;
aoqi@0 356 (*cmp_ct.adr_at(j))++;
aoqi@0 357 }
aoqi@0 358 }
aoqi@0 359 }
aoqi@0 360 }
aoqi@0 361
aoqi@0 362 // Find Store (or Load) with the greatest number of "comparable" references,
aoqi@0 363 // biggest vector size, smallest data size and smallest iv offset.
aoqi@0 364 int max_ct = 0;
aoqi@0 365 int max_vw = 0;
aoqi@0 366 int max_idx = -1;
aoqi@0 367 int min_size = max_jint;
aoqi@0 368 int min_iv_offset = max_jint;
aoqi@0 369 for (uint j = 0; j < memops.size(); j++) {
aoqi@0 370 MemNode* s = memops.at(j)->as_Mem();
aoqi@0 371 if (s->is_Store()) {
aoqi@0 372 int vw = vector_width_in_bytes(s);
aoqi@0 373 assert(vw > 1, "sanity");
aoqi@0 374 SWPointer p(s, this);
aoqi@0 375 if (cmp_ct.at(j) > max_ct ||
aoqi@0 376 cmp_ct.at(j) == max_ct &&
aoqi@0 377 (vw > max_vw ||
aoqi@0 378 vw == max_vw &&
aoqi@0 379 (data_size(s) < min_size ||
aoqi@0 380 data_size(s) == min_size &&
aoqi@0 381 (p.offset_in_bytes() < min_iv_offset)))) {
aoqi@0 382 max_ct = cmp_ct.at(j);
aoqi@0 383 max_vw = vw;
aoqi@0 384 max_idx = j;
aoqi@0 385 min_size = data_size(s);
aoqi@0 386 min_iv_offset = p.offset_in_bytes();
aoqi@0 387 }
aoqi@0 388 }
aoqi@0 389 }
aoqi@0 390 // If no stores, look at loads
aoqi@0 391 if (max_ct == 0) {
aoqi@0 392 for (uint j = 0; j < memops.size(); j++) {
aoqi@0 393 MemNode* s = memops.at(j)->as_Mem();
aoqi@0 394 if (s->is_Load()) {
aoqi@0 395 int vw = vector_width_in_bytes(s);
aoqi@0 396 assert(vw > 1, "sanity");
aoqi@0 397 SWPointer p(s, this);
aoqi@0 398 if (cmp_ct.at(j) > max_ct ||
aoqi@0 399 cmp_ct.at(j) == max_ct &&
aoqi@0 400 (vw > max_vw ||
aoqi@0 401 vw == max_vw &&
aoqi@0 402 (data_size(s) < min_size ||
aoqi@0 403 data_size(s) == min_size &&
aoqi@0 404 (p.offset_in_bytes() < min_iv_offset)))) {
aoqi@0 405 max_ct = cmp_ct.at(j);
aoqi@0 406 max_vw = vw;
aoqi@0 407 max_idx = j;
aoqi@0 408 min_size = data_size(s);
aoqi@0 409 min_iv_offset = p.offset_in_bytes();
aoqi@0 410 }
aoqi@0 411 }
aoqi@0 412 }
aoqi@0 413 }
aoqi@0 414
aoqi@0 415 #ifdef ASSERT
aoqi@0 416 if (TraceSuperWord && Verbose) {
aoqi@0 417 tty->print_cr("\nVector memops after find_align_to_refs");
aoqi@0 418 for (uint i = 0; i < memops.size(); i++) {
aoqi@0 419 MemNode* s = memops.at(i)->as_Mem();
aoqi@0 420 s->dump();
aoqi@0 421 }
aoqi@0 422 }
aoqi@0 423 #endif
aoqi@0 424
aoqi@0 425 if (max_ct > 0) {
aoqi@0 426 #ifdef ASSERT
aoqi@0 427 if (TraceSuperWord) {
aoqi@0 428 tty->print("\nVector align to node: ");
aoqi@0 429 memops.at(max_idx)->as_Mem()->dump();
aoqi@0 430 }
aoqi@0 431 #endif
aoqi@0 432 return memops.at(max_idx)->as_Mem();
aoqi@0 433 }
aoqi@0 434 return NULL;
aoqi@0 435 }
aoqi@0 436
aoqi@0 437 //------------------------------ref_is_alignable---------------------------
aoqi@0 438 // Can the preloop align the reference to position zero in the vector?
aoqi@0 439 bool SuperWord::ref_is_alignable(SWPointer& p) {
aoqi@0 440 if (!p.has_iv()) {
aoqi@0 441 return true; // no induction variable
aoqi@0 442 }
aoqi@0 443 CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop());
aoqi@0 444 assert(pre_end->stride_is_con(), "pre loop stride is constant");
aoqi@0 445 int preloop_stride = pre_end->stride_con();
aoqi@0 446
aoqi@0 447 int span = preloop_stride * p.scale_in_bytes();
aoqi@0 448
aoqi@0 449 // Stride one accesses are alignable.
aoqi@0 450 if (ABS(span) == p.memory_size())
aoqi@0 451 return true;
aoqi@0 452
aoqi@0 453 // If initial offset from start of object is computable,
aoqi@0 454 // compute alignment within the vector.
aoqi@0 455 int vw = vector_width_in_bytes(p.mem());
aoqi@0 456 assert(vw > 1, "sanity");
aoqi@0 457 if (vw % span == 0) {
aoqi@0 458 Node* init_nd = pre_end->init_trip();
aoqi@0 459 if (init_nd->is_Con() && p.invar() == NULL) {
aoqi@0 460 int init = init_nd->bottom_type()->is_int()->get_con();
aoqi@0 461
aoqi@0 462 int init_offset = init * p.scale_in_bytes() + p.offset_in_bytes();
aoqi@0 463 assert(init_offset >= 0, "positive offset from object start");
aoqi@0 464
aoqi@0 465 if (span > 0) {
aoqi@0 466 return (vw - (init_offset % vw)) % span == 0;
aoqi@0 467 } else {
aoqi@0 468 assert(span < 0, "nonzero stride * scale");
aoqi@0 469 return (init_offset % vw) % -span == 0;
aoqi@0 470 }
aoqi@0 471 }
aoqi@0 472 }
aoqi@0 473 return false;
aoqi@0 474 }
aoqi@0 475
aoqi@0 476 //---------------------------get_iv_adjustment---------------------------
aoqi@0 477 // Calculate loop's iv adjustment for this memory ops.
aoqi@0 478 int SuperWord::get_iv_adjustment(MemNode* mem_ref) {
aoqi@0 479 SWPointer align_to_ref_p(mem_ref, this);
aoqi@0 480 int offset = align_to_ref_p.offset_in_bytes();
aoqi@0 481 int scale = align_to_ref_p.scale_in_bytes();
aoqi@0 482 int vw = vector_width_in_bytes(mem_ref);
aoqi@0 483 assert(vw > 1, "sanity");
aoqi@0 484 int stride_sign = (scale * iv_stride()) > 0 ? 1 : -1;
aoqi@0 485 // At least one iteration is executed in pre-loop by default. As result
aoqi@0 486 // several iterations are needed to align memory operations in main-loop even
aoqi@0 487 // if offset is 0.
aoqi@0 488 int iv_adjustment_in_bytes = (stride_sign * vw - (offset % vw));
aoqi@0 489 int elt_size = align_to_ref_p.memory_size();
aoqi@0 490 assert(((ABS(iv_adjustment_in_bytes) % elt_size) == 0),
aoqi@0 491 err_msg_res("(%d) should be divisible by (%d)", iv_adjustment_in_bytes, elt_size));
aoqi@0 492 int iv_adjustment = iv_adjustment_in_bytes/elt_size;
aoqi@0 493
aoqi@0 494 #ifndef PRODUCT
aoqi@0 495 if (TraceSuperWord)
aoqi@0 496 tty->print_cr("\noffset = %d iv_adjust = %d elt_size = %d scale = %d iv_stride = %d vect_size %d",
aoqi@0 497 offset, iv_adjustment, elt_size, scale, iv_stride(), vw);
aoqi@0 498 #endif
aoqi@0 499 return iv_adjustment;
aoqi@0 500 }
aoqi@0 501
aoqi@0 502 //---------------------------dependence_graph---------------------------
aoqi@0 503 // Construct dependency graph.
aoqi@0 504 // Add dependence edges to load/store nodes for memory dependence
aoqi@0 505 // A.out()->DependNode.in(1) and DependNode.out()->B.prec(x)
aoqi@0 506 void SuperWord::dependence_graph() {
aoqi@0 507 // First, assign a dependence node to each memory node
aoqi@0 508 for (int i = 0; i < _block.length(); i++ ) {
aoqi@0 509 Node *n = _block.at(i);
aoqi@0 510 if (n->is_Mem() || n->is_Phi() && n->bottom_type() == Type::MEMORY) {
aoqi@0 511 _dg.make_node(n);
aoqi@0 512 }
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 // For each memory slice, create the dependences
aoqi@0 516 for (int i = 0; i < _mem_slice_head.length(); i++) {
aoqi@0 517 Node* n = _mem_slice_head.at(i);
aoqi@0 518 Node* n_tail = _mem_slice_tail.at(i);
aoqi@0 519
aoqi@0 520 // Get slice in predecessor order (last is first)
aoqi@0 521 mem_slice_preds(n_tail, n, _nlist);
aoqi@0 522
aoqi@0 523 // Make the slice dependent on the root
aoqi@0 524 DepMem* slice = _dg.dep(n);
aoqi@0 525 _dg.make_edge(_dg.root(), slice);
aoqi@0 526
aoqi@0 527 // Create a sink for the slice
aoqi@0 528 DepMem* slice_sink = _dg.make_node(NULL);
aoqi@0 529 _dg.make_edge(slice_sink, _dg.tail());
aoqi@0 530
aoqi@0 531 // Now visit each pair of memory ops, creating the edges
aoqi@0 532 for (int j = _nlist.length() - 1; j >= 0 ; j--) {
aoqi@0 533 Node* s1 = _nlist.at(j);
aoqi@0 534
aoqi@0 535 // If no dependency yet, use slice
aoqi@0 536 if (_dg.dep(s1)->in_cnt() == 0) {
aoqi@0 537 _dg.make_edge(slice, s1);
aoqi@0 538 }
aoqi@0 539 SWPointer p1(s1->as_Mem(), this);
aoqi@0 540 bool sink_dependent = true;
aoqi@0 541 for (int k = j - 1; k >= 0; k--) {
aoqi@0 542 Node* s2 = _nlist.at(k);
aoqi@0 543 if (s1->is_Load() && s2->is_Load())
aoqi@0 544 continue;
aoqi@0 545 SWPointer p2(s2->as_Mem(), this);
aoqi@0 546
aoqi@0 547 int cmp = p1.cmp(p2);
aoqi@0 548 if (SuperWordRTDepCheck &&
aoqi@0 549 p1.base() != p2.base() && p1.valid() && p2.valid()) {
aoqi@0 550 // Create a runtime check to disambiguate
aoqi@0 551 OrderedPair pp(p1.base(), p2.base());
aoqi@0 552 _disjoint_ptrs.append_if_missing(pp);
aoqi@0 553 } else if (!SWPointer::not_equal(cmp)) {
aoqi@0 554 // Possibly same address
aoqi@0 555 _dg.make_edge(s1, s2);
aoqi@0 556 sink_dependent = false;
aoqi@0 557 }
aoqi@0 558 }
aoqi@0 559 if (sink_dependent) {
aoqi@0 560 _dg.make_edge(s1, slice_sink);
aoqi@0 561 }
aoqi@0 562 }
aoqi@0 563 #ifndef PRODUCT
aoqi@0 564 if (TraceSuperWord) {
aoqi@0 565 tty->print_cr("\nDependence graph for slice: %d", n->_idx);
aoqi@0 566 for (int q = 0; q < _nlist.length(); q++) {
aoqi@0 567 _dg.print(_nlist.at(q));
aoqi@0 568 }
aoqi@0 569 tty->cr();
aoqi@0 570 }
aoqi@0 571 #endif
aoqi@0 572 _nlist.clear();
aoqi@0 573 }
aoqi@0 574
aoqi@0 575 #ifndef PRODUCT
aoqi@0 576 if (TraceSuperWord) {
aoqi@0 577 tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE");
aoqi@0 578 for (int r = 0; r < _disjoint_ptrs.length(); r++) {
aoqi@0 579 _disjoint_ptrs.at(r).print();
aoqi@0 580 tty->cr();
aoqi@0 581 }
aoqi@0 582 tty->cr();
aoqi@0 583 }
aoqi@0 584 #endif
aoqi@0 585 }
aoqi@0 586
aoqi@0 587 //---------------------------mem_slice_preds---------------------------
aoqi@0 588 // Return a memory slice (node list) in predecessor order starting at "start"
aoqi@0 589 void SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) {
aoqi@0 590 assert(preds.length() == 0, "start empty");
aoqi@0 591 Node* n = start;
aoqi@0 592 Node* prev = NULL;
aoqi@0 593 while (true) {
aoqi@0 594 assert(in_bb(n), "must be in block");
aoqi@0 595 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
aoqi@0 596 Node* out = n->fast_out(i);
aoqi@0 597 if (out->is_Load()) {
aoqi@0 598 if (in_bb(out)) {
aoqi@0 599 preds.push(out);
aoqi@0 600 }
aoqi@0 601 } else {
aoqi@0 602 // FIXME
aoqi@0 603 if (out->is_MergeMem() && !in_bb(out)) {
aoqi@0 604 // Either unrolling is causing a memory edge not to disappear,
aoqi@0 605 // or need to run igvn.optimize() again before SLP
aoqi@0 606 } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) {
aoqi@0 607 // Ditto. Not sure what else to check further.
aoqi@0 608 } else if (out->Opcode() == Op_StoreCM && out->in(MemNode::OopStore) == n) {
aoqi@0 609 // StoreCM has an input edge used as a precedence edge.
aoqi@0 610 // Maybe an issue when oop stores are vectorized.
aoqi@0 611 } else {
aoqi@0 612 assert(out == prev || prev == NULL, "no branches off of store slice");
aoqi@0 613 }
aoqi@0 614 }
aoqi@0 615 }
aoqi@0 616 if (n == stop) break;
aoqi@0 617 preds.push(n);
aoqi@0 618 prev = n;
aoqi@0 619 assert(n->is_Mem(), err_msg_res("unexpected node %s", n->Name()));
aoqi@0 620 n = n->in(MemNode::Memory);
aoqi@0 621 }
aoqi@0 622 }
aoqi@0 623
aoqi@0 624 //------------------------------stmts_can_pack---------------------------
aoqi@0 625 // Can s1 and s2 be in a pack with s1 immediately preceding s2 and
aoqi@0 626 // s1 aligned at "align"
aoqi@0 627 bool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) {
aoqi@0 628
aoqi@0 629 // Do not use superword for non-primitives
aoqi@0 630 BasicType bt1 = velt_basic_type(s1);
aoqi@0 631 BasicType bt2 = velt_basic_type(s2);
aoqi@0 632 if(!is_java_primitive(bt1) || !is_java_primitive(bt2))
aoqi@0 633 return false;
aoqi@0 634 if (Matcher::max_vector_size(bt1) < 2) {
aoqi@0 635 return false; // No vectors for this type
aoqi@0 636 }
aoqi@0 637
aoqi@0 638 if (isomorphic(s1, s2)) {
aoqi@0 639 if (independent(s1, s2)) {
aoqi@0 640 if (!exists_at(s1, 0) && !exists_at(s2, 1)) {
aoqi@0 641 if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) {
aoqi@0 642 int s1_align = alignment(s1);
aoqi@0 643 int s2_align = alignment(s2);
aoqi@0 644 if (s1_align == top_align || s1_align == align) {
aoqi@0 645 if (s2_align == top_align || s2_align == align + data_size(s1)) {
aoqi@0 646 return true;
aoqi@0 647 }
aoqi@0 648 }
aoqi@0 649 }
aoqi@0 650 }
aoqi@0 651 }
aoqi@0 652 }
aoqi@0 653 return false;
aoqi@0 654 }
aoqi@0 655
aoqi@0 656 //------------------------------exists_at---------------------------
aoqi@0 657 // Does s exist in a pack at position pos?
aoqi@0 658 bool SuperWord::exists_at(Node* s, uint pos) {
aoqi@0 659 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 660 Node_List* p = _packset.at(i);
aoqi@0 661 if (p->at(pos) == s) {
aoqi@0 662 return true;
aoqi@0 663 }
aoqi@0 664 }
aoqi@0 665 return false;
aoqi@0 666 }
aoqi@0 667
aoqi@0 668 //------------------------------are_adjacent_refs---------------------------
aoqi@0 669 // Is s1 immediately before s2 in memory?
aoqi@0 670 bool SuperWord::are_adjacent_refs(Node* s1, Node* s2) {
aoqi@0 671 if (!s1->is_Mem() || !s2->is_Mem()) return false;
aoqi@0 672 if (!in_bb(s1) || !in_bb(s2)) return false;
aoqi@0 673
aoqi@0 674 // Do not use superword for non-primitives
aoqi@0 675 if (!is_java_primitive(s1->as_Mem()->memory_type()) ||
aoqi@0 676 !is_java_primitive(s2->as_Mem()->memory_type())) {
aoqi@0 677 return false;
aoqi@0 678 }
aoqi@0 679
aoqi@0 680 // FIXME - co_locate_pack fails on Stores in different mem-slices, so
aoqi@0 681 // only pack memops that are in the same alias set until that's fixed.
aoqi@0 682 if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) !=
aoqi@0 683 _phase->C->get_alias_index(s2->as_Mem()->adr_type()))
aoqi@0 684 return false;
aoqi@0 685 SWPointer p1(s1->as_Mem(), this);
aoqi@0 686 SWPointer p2(s2->as_Mem(), this);
aoqi@0 687 if (p1.base() != p2.base() || !p1.comparable(p2)) return false;
aoqi@0 688 int diff = p2.offset_in_bytes() - p1.offset_in_bytes();
aoqi@0 689 return diff == data_size(s1);
aoqi@0 690 }
aoqi@0 691
aoqi@0 692 //------------------------------isomorphic---------------------------
aoqi@0 693 // Are s1 and s2 similar?
aoqi@0 694 bool SuperWord::isomorphic(Node* s1, Node* s2) {
aoqi@0 695 if (s1->Opcode() != s2->Opcode()) return false;
aoqi@0 696 if (s1->req() != s2->req()) return false;
aoqi@0 697 if (s1->in(0) != s2->in(0)) return false;
aoqi@0 698 if (!same_velt_type(s1, s2)) return false;
aoqi@0 699 return true;
aoqi@0 700 }
aoqi@0 701
aoqi@0 702 //------------------------------independent---------------------------
aoqi@0 703 // Is there no data path from s1 to s2 or s2 to s1?
aoqi@0 704 bool SuperWord::independent(Node* s1, Node* s2) {
aoqi@0 705 // assert(s1->Opcode() == s2->Opcode(), "check isomorphic first");
aoqi@0 706 int d1 = depth(s1);
aoqi@0 707 int d2 = depth(s2);
aoqi@0 708 if (d1 == d2) return s1 != s2;
aoqi@0 709 Node* deep = d1 > d2 ? s1 : s2;
aoqi@0 710 Node* shallow = d1 > d2 ? s2 : s1;
aoqi@0 711
aoqi@0 712 visited_clear();
aoqi@0 713
aoqi@0 714 return independent_path(shallow, deep);
aoqi@0 715 }
aoqi@0 716
aoqi@0 717 //------------------------------independent_path------------------------------
aoqi@0 718 // Helper for independent
aoqi@0 719 bool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) {
aoqi@0 720 if (dp >= 1000) return false; // stop deep recursion
aoqi@0 721 visited_set(deep);
aoqi@0 722 int shal_depth = depth(shallow);
aoqi@0 723 assert(shal_depth <= depth(deep), "must be");
aoqi@0 724 for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) {
aoqi@0 725 Node* pred = preds.current();
aoqi@0 726 if (in_bb(pred) && !visited_test(pred)) {
aoqi@0 727 if (shallow == pred) {
aoqi@0 728 return false;
aoqi@0 729 }
aoqi@0 730 if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) {
aoqi@0 731 return false;
aoqi@0 732 }
aoqi@0 733 }
aoqi@0 734 }
aoqi@0 735 return true;
aoqi@0 736 }
aoqi@0 737
aoqi@0 738 //------------------------------set_alignment---------------------------
aoqi@0 739 void SuperWord::set_alignment(Node* s1, Node* s2, int align) {
aoqi@0 740 set_alignment(s1, align);
aoqi@0 741 if (align == top_align || align == bottom_align) {
aoqi@0 742 set_alignment(s2, align);
aoqi@0 743 } else {
aoqi@0 744 set_alignment(s2, align + data_size(s1));
aoqi@0 745 }
aoqi@0 746 }
aoqi@0 747
aoqi@0 748 //------------------------------data_size---------------------------
aoqi@0 749 int SuperWord::data_size(Node* s) {
aoqi@0 750 int bsize = type2aelembytes(velt_basic_type(s));
aoqi@0 751 assert(bsize != 0, "valid size");
aoqi@0 752 return bsize;
aoqi@0 753 }
aoqi@0 754
aoqi@0 755 //------------------------------extend_packlist---------------------------
aoqi@0 756 // Extend packset by following use->def and def->use links from pack members.
aoqi@0 757 void SuperWord::extend_packlist() {
aoqi@0 758 bool changed;
aoqi@0 759 do {
aoqi@0 760 changed = false;
aoqi@0 761 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 762 Node_List* p = _packset.at(i);
aoqi@0 763 changed |= follow_use_defs(p);
aoqi@0 764 changed |= follow_def_uses(p);
aoqi@0 765 }
aoqi@0 766 } while (changed);
aoqi@0 767
aoqi@0 768 #ifndef PRODUCT
aoqi@0 769 if (TraceSuperWord) {
aoqi@0 770 tty->print_cr("\nAfter extend_packlist");
aoqi@0 771 print_packset();
aoqi@0 772 }
aoqi@0 773 #endif
aoqi@0 774 }
aoqi@0 775
aoqi@0 776 //------------------------------follow_use_defs---------------------------
aoqi@0 777 // Extend the packset by visiting operand definitions of nodes in pack p
aoqi@0 778 bool SuperWord::follow_use_defs(Node_List* p) {
aoqi@0 779 assert(p->size() == 2, "just checking");
aoqi@0 780 Node* s1 = p->at(0);
aoqi@0 781 Node* s2 = p->at(1);
aoqi@0 782 assert(s1->req() == s2->req(), "just checking");
aoqi@0 783 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
aoqi@0 784
aoqi@0 785 if (s1->is_Load()) return false;
aoqi@0 786
aoqi@0 787 int align = alignment(s1);
aoqi@0 788 bool changed = false;
aoqi@0 789 int start = s1->is_Store() ? MemNode::ValueIn : 1;
aoqi@0 790 int end = s1->is_Store() ? MemNode::ValueIn+1 : s1->req();
aoqi@0 791 for (int j = start; j < end; j++) {
aoqi@0 792 Node* t1 = s1->in(j);
aoqi@0 793 Node* t2 = s2->in(j);
aoqi@0 794 if (!in_bb(t1) || !in_bb(t2))
aoqi@0 795 continue;
aoqi@0 796 if (stmts_can_pack(t1, t2, align)) {
aoqi@0 797 if (est_savings(t1, t2) >= 0) {
aoqi@0 798 Node_List* pair = new Node_List();
aoqi@0 799 pair->push(t1);
aoqi@0 800 pair->push(t2);
aoqi@0 801 _packset.append(pair);
aoqi@0 802 set_alignment(t1, t2, align);
aoqi@0 803 changed = true;
aoqi@0 804 }
aoqi@0 805 }
aoqi@0 806 }
aoqi@0 807 return changed;
aoqi@0 808 }
aoqi@0 809
aoqi@0 810 //------------------------------follow_def_uses---------------------------
aoqi@0 811 // Extend the packset by visiting uses of nodes in pack p
aoqi@0 812 bool SuperWord::follow_def_uses(Node_List* p) {
aoqi@0 813 bool changed = false;
aoqi@0 814 Node* s1 = p->at(0);
aoqi@0 815 Node* s2 = p->at(1);
aoqi@0 816 assert(p->size() == 2, "just checking");
aoqi@0 817 assert(s1->req() == s2->req(), "just checking");
aoqi@0 818 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
aoqi@0 819
aoqi@0 820 if (s1->is_Store()) return false;
aoqi@0 821
aoqi@0 822 int align = alignment(s1);
aoqi@0 823 int savings = -1;
aoqi@0 824 Node* u1 = NULL;
aoqi@0 825 Node* u2 = NULL;
aoqi@0 826 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
aoqi@0 827 Node* t1 = s1->fast_out(i);
aoqi@0 828 if (!in_bb(t1)) continue;
aoqi@0 829 for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) {
aoqi@0 830 Node* t2 = s2->fast_out(j);
aoqi@0 831 if (!in_bb(t2)) continue;
aoqi@0 832 if (!opnd_positions_match(s1, t1, s2, t2))
aoqi@0 833 continue;
aoqi@0 834 if (stmts_can_pack(t1, t2, align)) {
aoqi@0 835 int my_savings = est_savings(t1, t2);
aoqi@0 836 if (my_savings > savings) {
aoqi@0 837 savings = my_savings;
aoqi@0 838 u1 = t1;
aoqi@0 839 u2 = t2;
aoqi@0 840 }
aoqi@0 841 }
aoqi@0 842 }
aoqi@0 843 }
aoqi@0 844 if (savings >= 0) {
aoqi@0 845 Node_List* pair = new Node_List();
aoqi@0 846 pair->push(u1);
aoqi@0 847 pair->push(u2);
aoqi@0 848 _packset.append(pair);
aoqi@0 849 set_alignment(u1, u2, align);
aoqi@0 850 changed = true;
aoqi@0 851 }
aoqi@0 852 return changed;
aoqi@0 853 }
aoqi@0 854
aoqi@0 855 //---------------------------opnd_positions_match-------------------------
aoqi@0 856 // Is the use of d1 in u1 at the same operand position as d2 in u2?
aoqi@0 857 bool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) {
aoqi@0 858 uint ct = u1->req();
aoqi@0 859 if (ct != u2->req()) return false;
aoqi@0 860 uint i1 = 0;
aoqi@0 861 uint i2 = 0;
aoqi@0 862 do {
aoqi@0 863 for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break;
aoqi@0 864 for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break;
aoqi@0 865 if (i1 != i2) {
aoqi@0 866 if ((i1 == (3-i2)) && (u2->is_Add() || u2->is_Mul())) {
aoqi@0 867 // Further analysis relies on operands position matching.
aoqi@0 868 u2->swap_edges(i1, i2);
aoqi@0 869 } else {
aoqi@0 870 return false;
aoqi@0 871 }
aoqi@0 872 }
aoqi@0 873 } while (i1 < ct);
aoqi@0 874 return true;
aoqi@0 875 }
aoqi@0 876
aoqi@0 877 //------------------------------est_savings---------------------------
aoqi@0 878 // Estimate the savings from executing s1 and s2 as a pack
aoqi@0 879 int SuperWord::est_savings(Node* s1, Node* s2) {
aoqi@0 880 int save_in = 2 - 1; // 2 operations per instruction in packed form
aoqi@0 881
aoqi@0 882 // inputs
aoqi@0 883 for (uint i = 1; i < s1->req(); i++) {
aoqi@0 884 Node* x1 = s1->in(i);
aoqi@0 885 Node* x2 = s2->in(i);
aoqi@0 886 if (x1 != x2) {
aoqi@0 887 if (are_adjacent_refs(x1, x2)) {
aoqi@0 888 save_in += adjacent_profit(x1, x2);
aoqi@0 889 } else if (!in_packset(x1, x2)) {
aoqi@0 890 save_in -= pack_cost(2);
aoqi@0 891 } else {
aoqi@0 892 save_in += unpack_cost(2);
aoqi@0 893 }
aoqi@0 894 }
aoqi@0 895 }
aoqi@0 896
aoqi@0 897 // uses of result
aoqi@0 898 uint ct = 0;
aoqi@0 899 int save_use = 0;
aoqi@0 900 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
aoqi@0 901 Node* s1_use = s1->fast_out(i);
aoqi@0 902 for (int j = 0; j < _packset.length(); j++) {
aoqi@0 903 Node_List* p = _packset.at(j);
aoqi@0 904 if (p->at(0) == s1_use) {
aoqi@0 905 for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) {
aoqi@0 906 Node* s2_use = s2->fast_out(k);
aoqi@0 907 if (p->at(p->size()-1) == s2_use) {
aoqi@0 908 ct++;
aoqi@0 909 if (are_adjacent_refs(s1_use, s2_use)) {
aoqi@0 910 save_use += adjacent_profit(s1_use, s2_use);
aoqi@0 911 }
aoqi@0 912 }
aoqi@0 913 }
aoqi@0 914 }
aoqi@0 915 }
aoqi@0 916 }
aoqi@0 917
aoqi@0 918 if (ct < s1->outcnt()) save_use += unpack_cost(1);
aoqi@0 919 if (ct < s2->outcnt()) save_use += unpack_cost(1);
aoqi@0 920
aoqi@0 921 return MAX2(save_in, save_use);
aoqi@0 922 }
aoqi@0 923
aoqi@0 924 //------------------------------costs---------------------------
aoqi@0 925 int SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; }
aoqi@0 926 int SuperWord::pack_cost(int ct) { return ct; }
aoqi@0 927 int SuperWord::unpack_cost(int ct) { return ct; }
aoqi@0 928
aoqi@0 929 //------------------------------combine_packs---------------------------
aoqi@0 930 // Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last
aoqi@0 931 void SuperWord::combine_packs() {
aoqi@0 932 bool changed = true;
aoqi@0 933 // Combine packs regardless max vector size.
aoqi@0 934 while (changed) {
aoqi@0 935 changed = false;
aoqi@0 936 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 937 Node_List* p1 = _packset.at(i);
aoqi@0 938 if (p1 == NULL) continue;
aoqi@0 939 for (int j = 0; j < _packset.length(); j++) {
aoqi@0 940 Node_List* p2 = _packset.at(j);
aoqi@0 941 if (p2 == NULL) continue;
aoqi@0 942 if (i == j) continue;
aoqi@0 943 if (p1->at(p1->size()-1) == p2->at(0)) {
aoqi@0 944 for (uint k = 1; k < p2->size(); k++) {
aoqi@0 945 p1->push(p2->at(k));
aoqi@0 946 }
aoqi@0 947 _packset.at_put(j, NULL);
aoqi@0 948 changed = true;
aoqi@0 949 }
aoqi@0 950 }
aoqi@0 951 }
aoqi@0 952 }
aoqi@0 953
aoqi@0 954 // Split packs which have size greater then max vector size.
aoqi@0 955 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 956 Node_List* p1 = _packset.at(i);
aoqi@0 957 if (p1 != NULL) {
aoqi@0 958 BasicType bt = velt_basic_type(p1->at(0));
aoqi@0 959 uint max_vlen = Matcher::max_vector_size(bt); // Max elements in vector
aoqi@0 960 assert(is_power_of_2(max_vlen), "sanity");
aoqi@0 961 uint psize = p1->size();
aoqi@0 962 if (!is_power_of_2(psize)) {
aoqi@0 963 // Skip pack which can't be vector.
aoqi@0 964 // case1: for(...) { a[i] = i; } elements values are different (i+x)
aoqi@0 965 // case2: for(...) { a[i] = b[i+1]; } can't align both, load and store
aoqi@0 966 _packset.at_put(i, NULL);
aoqi@0 967 continue;
aoqi@0 968 }
aoqi@0 969 if (psize > max_vlen) {
aoqi@0 970 Node_List* pack = new Node_List();
aoqi@0 971 for (uint j = 0; j < psize; j++) {
aoqi@0 972 pack->push(p1->at(j));
aoqi@0 973 if (pack->size() >= max_vlen) {
aoqi@0 974 assert(is_power_of_2(pack->size()), "sanity");
aoqi@0 975 _packset.append(pack);
aoqi@0 976 pack = new Node_List();
aoqi@0 977 }
aoqi@0 978 }
aoqi@0 979 _packset.at_put(i, NULL);
aoqi@0 980 }
aoqi@0 981 }
aoqi@0 982 }
aoqi@0 983
aoqi@0 984 // Compress list.
aoqi@0 985 for (int i = _packset.length() - 1; i >= 0; i--) {
aoqi@0 986 Node_List* p1 = _packset.at(i);
aoqi@0 987 if (p1 == NULL) {
aoqi@0 988 _packset.remove_at(i);
aoqi@0 989 }
aoqi@0 990 }
aoqi@0 991
aoqi@0 992 #ifndef PRODUCT
aoqi@0 993 if (TraceSuperWord) {
aoqi@0 994 tty->print_cr("\nAfter combine_packs");
aoqi@0 995 print_packset();
aoqi@0 996 }
aoqi@0 997 #endif
aoqi@0 998 }
aoqi@0 999
aoqi@0 1000 //-----------------------------construct_my_pack_map--------------------------
aoqi@0 1001 // Construct the map from nodes to packs. Only valid after the
aoqi@0 1002 // point where a node is only in one pack (after combine_packs).
aoqi@0 1003 void SuperWord::construct_my_pack_map() {
aoqi@0 1004 Node_List* rslt = NULL;
aoqi@0 1005 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 1006 Node_List* p = _packset.at(i);
aoqi@0 1007 for (uint j = 0; j < p->size(); j++) {
aoqi@0 1008 Node* s = p->at(j);
aoqi@0 1009 assert(my_pack(s) == NULL, "only in one pack");
aoqi@0 1010 set_my_pack(s, p);
aoqi@0 1011 }
aoqi@0 1012 }
aoqi@0 1013 }
aoqi@0 1014
aoqi@0 1015 //------------------------------filter_packs---------------------------
aoqi@0 1016 // Remove packs that are not implemented or not profitable.
aoqi@0 1017 void SuperWord::filter_packs() {
aoqi@0 1018
aoqi@0 1019 // Remove packs that are not implemented
aoqi@0 1020 for (int i = _packset.length() - 1; i >= 0; i--) {
aoqi@0 1021 Node_List* pk = _packset.at(i);
aoqi@0 1022 bool impl = implemented(pk);
aoqi@0 1023 if (!impl) {
aoqi@0 1024 #ifndef PRODUCT
aoqi@0 1025 if (TraceSuperWord && Verbose) {
aoqi@0 1026 tty->print_cr("Unimplemented");
aoqi@0 1027 pk->at(0)->dump();
aoqi@0 1028 }
aoqi@0 1029 #endif
aoqi@0 1030 remove_pack_at(i);
aoqi@0 1031 }
aoqi@0 1032 }
aoqi@0 1033
aoqi@0 1034 // Remove packs that are not profitable
aoqi@0 1035 bool changed;
aoqi@0 1036 do {
aoqi@0 1037 changed = false;
aoqi@0 1038 for (int i = _packset.length() - 1; i >= 0; i--) {
aoqi@0 1039 Node_List* pk = _packset.at(i);
aoqi@0 1040 bool prof = profitable(pk);
aoqi@0 1041 if (!prof) {
aoqi@0 1042 #ifndef PRODUCT
aoqi@0 1043 if (TraceSuperWord && Verbose) {
aoqi@0 1044 tty->print_cr("Unprofitable");
aoqi@0 1045 pk->at(0)->dump();
aoqi@0 1046 }
aoqi@0 1047 #endif
aoqi@0 1048 remove_pack_at(i);
aoqi@0 1049 changed = true;
aoqi@0 1050 }
aoqi@0 1051 }
aoqi@0 1052 } while (changed);
aoqi@0 1053
aoqi@0 1054 #ifndef PRODUCT
aoqi@0 1055 if (TraceSuperWord) {
aoqi@0 1056 tty->print_cr("\nAfter filter_packs");
aoqi@0 1057 print_packset();
aoqi@0 1058 tty->cr();
aoqi@0 1059 }
aoqi@0 1060 #endif
aoqi@0 1061 }
aoqi@0 1062
aoqi@0 1063 //------------------------------implemented---------------------------
aoqi@0 1064 // Can code be generated for pack p?
aoqi@0 1065 bool SuperWord::implemented(Node_List* p) {
aoqi@0 1066 Node* p0 = p->at(0);
aoqi@0 1067 return VectorNode::implemented(p0->Opcode(), p->size(), velt_basic_type(p0));
aoqi@0 1068 }
aoqi@0 1069
aoqi@0 1070 //------------------------------same_inputs--------------------------
aoqi@0 1071 // For pack p, are all idx operands the same?
aoqi@0 1072 static bool same_inputs(Node_List* p, int idx) {
aoqi@0 1073 Node* p0 = p->at(0);
aoqi@0 1074 uint vlen = p->size();
aoqi@0 1075 Node* p0_def = p0->in(idx);
aoqi@0 1076 for (uint i = 1; i < vlen; i++) {
aoqi@0 1077 Node* pi = p->at(i);
aoqi@0 1078 Node* pi_def = pi->in(idx);
aoqi@0 1079 if (p0_def != pi_def)
aoqi@0 1080 return false;
aoqi@0 1081 }
aoqi@0 1082 return true;
aoqi@0 1083 }
aoqi@0 1084
aoqi@0 1085 //------------------------------profitable---------------------------
aoqi@0 1086 // For pack p, are all operands and all uses (with in the block) vector?
aoqi@0 1087 bool SuperWord::profitable(Node_List* p) {
aoqi@0 1088 Node* p0 = p->at(0);
aoqi@0 1089 uint start, end;
aoqi@0 1090 VectorNode::vector_operands(p0, &start, &end);
aoqi@0 1091
aoqi@0 1092 // Return false if some inputs are not vectors or vectors with different
aoqi@0 1093 // size or alignment.
aoqi@0 1094 // Also, for now, return false if not scalar promotion case when inputs are
aoqi@0 1095 // the same. Later, implement PackNode and allow differing, non-vector inputs
aoqi@0 1096 // (maybe just the ones from outside the block.)
aoqi@0 1097 for (uint i = start; i < end; i++) {
aoqi@0 1098 if (!is_vector_use(p0, i))
aoqi@0 1099 return false;
aoqi@0 1100 }
aoqi@0 1101 if (VectorNode::is_shift(p0)) {
aoqi@0 1102 // For now, return false if shift count is vector or not scalar promotion
aoqi@0 1103 // case (different shift counts) because it is not supported yet.
aoqi@0 1104 Node* cnt = p0->in(2);
aoqi@0 1105 Node_List* cnt_pk = my_pack(cnt);
aoqi@0 1106 if (cnt_pk != NULL)
aoqi@0 1107 return false;
aoqi@0 1108 if (!same_inputs(p, 2))
aoqi@0 1109 return false;
aoqi@0 1110 }
aoqi@0 1111 if (!p0->is_Store()) {
aoqi@0 1112 // For now, return false if not all uses are vector.
aoqi@0 1113 // Later, implement ExtractNode and allow non-vector uses (maybe
aoqi@0 1114 // just the ones outside the block.)
aoqi@0 1115 for (uint i = 0; i < p->size(); i++) {
aoqi@0 1116 Node* def = p->at(i);
aoqi@0 1117 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1118 Node* use = def->fast_out(j);
aoqi@0 1119 for (uint k = 0; k < use->req(); k++) {
aoqi@0 1120 Node* n = use->in(k);
aoqi@0 1121 if (def == n) {
aoqi@0 1122 if (!is_vector_use(use, k)) {
aoqi@0 1123 return false;
aoqi@0 1124 }
aoqi@0 1125 }
aoqi@0 1126 }
aoqi@0 1127 }
aoqi@0 1128 }
aoqi@0 1129 }
aoqi@0 1130 return true;
aoqi@0 1131 }
aoqi@0 1132
aoqi@0 1133 //------------------------------schedule---------------------------
aoqi@0 1134 // Adjust the memory graph for the packed operations
aoqi@0 1135 void SuperWord::schedule() {
aoqi@0 1136
aoqi@0 1137 // Co-locate in the memory graph the members of each memory pack
aoqi@0 1138 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 1139 co_locate_pack(_packset.at(i));
aoqi@0 1140 }
aoqi@0 1141 }
aoqi@0 1142
aoqi@0 1143 //-------------------------------remove_and_insert-------------------
aoqi@0 1144 // Remove "current" from its current position in the memory graph and insert
aoqi@0 1145 // it after the appropriate insertion point (lip or uip).
aoqi@0 1146 void SuperWord::remove_and_insert(MemNode *current, MemNode *prev, MemNode *lip,
aoqi@0 1147 Node *uip, Unique_Node_List &sched_before) {
aoqi@0 1148 Node* my_mem = current->in(MemNode::Memory);
aoqi@0 1149 bool sched_up = sched_before.member(current);
aoqi@0 1150
aoqi@0 1151 // remove current_store from its current position in the memmory graph
aoqi@0 1152 for (DUIterator i = current->outs(); current->has_out(i); i++) {
aoqi@0 1153 Node* use = current->out(i);
aoqi@0 1154 if (use->is_Mem()) {
aoqi@0 1155 assert(use->in(MemNode::Memory) == current, "must be");
aoqi@0 1156 if (use == prev) { // connect prev to my_mem
aoqi@0 1157 _igvn.replace_input_of(use, MemNode::Memory, my_mem);
aoqi@0 1158 --i; //deleted this edge; rescan position
aoqi@0 1159 } else if (sched_before.member(use)) {
aoqi@0 1160 if (!sched_up) { // Will be moved together with current
aoqi@0 1161 _igvn.replace_input_of(use, MemNode::Memory, uip);
aoqi@0 1162 --i; //deleted this edge; rescan position
aoqi@0 1163 }
aoqi@0 1164 } else {
aoqi@0 1165 if (sched_up) { // Will be moved together with current
aoqi@0 1166 _igvn.replace_input_of(use, MemNode::Memory, lip);
aoqi@0 1167 --i; //deleted this edge; rescan position
aoqi@0 1168 }
aoqi@0 1169 }
aoqi@0 1170 }
aoqi@0 1171 }
aoqi@0 1172
aoqi@0 1173 Node *insert_pt = sched_up ? uip : lip;
aoqi@0 1174
aoqi@0 1175 // all uses of insert_pt's memory state should use current's instead
aoqi@0 1176 for (DUIterator i = insert_pt->outs(); insert_pt->has_out(i); i++) {
aoqi@0 1177 Node* use = insert_pt->out(i);
aoqi@0 1178 if (use->is_Mem()) {
aoqi@0 1179 assert(use->in(MemNode::Memory) == insert_pt, "must be");
aoqi@0 1180 _igvn.replace_input_of(use, MemNode::Memory, current);
aoqi@0 1181 --i; //deleted this edge; rescan position
aoqi@0 1182 } else if (!sched_up && use->is_Phi() && use->bottom_type() == Type::MEMORY) {
aoqi@0 1183 uint pos; //lip (lower insert point) must be the last one in the memory slice
aoqi@0 1184 for (pos=1; pos < use->req(); pos++) {
aoqi@0 1185 if (use->in(pos) == insert_pt) break;
aoqi@0 1186 }
aoqi@0 1187 _igvn.replace_input_of(use, pos, current);
aoqi@0 1188 --i;
aoqi@0 1189 }
aoqi@0 1190 }
aoqi@0 1191
aoqi@0 1192 //connect current to insert_pt
aoqi@0 1193 _igvn.replace_input_of(current, MemNode::Memory, insert_pt);
aoqi@0 1194 }
aoqi@0 1195
aoqi@0 1196 //------------------------------co_locate_pack----------------------------------
aoqi@0 1197 // To schedule a store pack, we need to move any sandwiched memory ops either before
aoqi@0 1198 // or after the pack, based upon dependence information:
aoqi@0 1199 // (1) If any store in the pack depends on the sandwiched memory op, the
aoqi@0 1200 // sandwiched memory op must be scheduled BEFORE the pack;
aoqi@0 1201 // (2) If a sandwiched memory op depends on any store in the pack, the
aoqi@0 1202 // sandwiched memory op must be scheduled AFTER the pack;
aoqi@0 1203 // (3) If a sandwiched memory op (say, memA) depends on another sandwiched
aoqi@0 1204 // memory op (say memB), memB must be scheduled before memA. So, if memA is
aoqi@0 1205 // scheduled before the pack, memB must also be scheduled before the pack;
aoqi@0 1206 // (4) If there is no dependence restriction for a sandwiched memory op, we simply
aoqi@0 1207 // schedule this store AFTER the pack
aoqi@0 1208 // (5) We know there is no dependence cycle, so there in no other case;
aoqi@0 1209 // (6) Finally, all memory ops in another single pack should be moved in the same direction.
aoqi@0 1210 //
aoqi@0 1211 // To schedule a load pack, we use the memory state of either the first or the last load in
aoqi@0 1212 // the pack, based on the dependence constraint.
aoqi@0 1213 void SuperWord::co_locate_pack(Node_List* pk) {
aoqi@0 1214 if (pk->at(0)->is_Store()) {
aoqi@0 1215 MemNode* first = executed_first(pk)->as_Mem();
aoqi@0 1216 MemNode* last = executed_last(pk)->as_Mem();
aoqi@0 1217 Unique_Node_List schedule_before_pack;
aoqi@0 1218 Unique_Node_List memops;
aoqi@0 1219
aoqi@0 1220 MemNode* current = last->in(MemNode::Memory)->as_Mem();
aoqi@0 1221 MemNode* previous = last;
aoqi@0 1222 while (true) {
aoqi@0 1223 assert(in_bb(current), "stay in block");
aoqi@0 1224 memops.push(previous);
aoqi@0 1225 for (DUIterator i = current->outs(); current->has_out(i); i++) {
aoqi@0 1226 Node* use = current->out(i);
aoqi@0 1227 if (use->is_Mem() && use != previous)
aoqi@0 1228 memops.push(use);
aoqi@0 1229 }
aoqi@0 1230 if (current == first) break;
aoqi@0 1231 previous = current;
aoqi@0 1232 current = current->in(MemNode::Memory)->as_Mem();
aoqi@0 1233 }
aoqi@0 1234
aoqi@0 1235 // determine which memory operations should be scheduled before the pack
aoqi@0 1236 for (uint i = 1; i < memops.size(); i++) {
aoqi@0 1237 Node *s1 = memops.at(i);
aoqi@0 1238 if (!in_pack(s1, pk) && !schedule_before_pack.member(s1)) {
aoqi@0 1239 for (uint j = 0; j< i; j++) {
aoqi@0 1240 Node *s2 = memops.at(j);
aoqi@0 1241 if (!independent(s1, s2)) {
aoqi@0 1242 if (in_pack(s2, pk) || schedule_before_pack.member(s2)) {
aoqi@0 1243 schedule_before_pack.push(s1); // s1 must be scheduled before
aoqi@0 1244 Node_List* mem_pk = my_pack(s1);
aoqi@0 1245 if (mem_pk != NULL) {
aoqi@0 1246 for (uint ii = 0; ii < mem_pk->size(); ii++) {
aoqi@0 1247 Node* s = mem_pk->at(ii); // follow partner
aoqi@0 1248 if (memops.member(s) && !schedule_before_pack.member(s))
aoqi@0 1249 schedule_before_pack.push(s);
aoqi@0 1250 }
aoqi@0 1251 }
aoqi@0 1252 break;
aoqi@0 1253 }
aoqi@0 1254 }
aoqi@0 1255 }
aoqi@0 1256 }
aoqi@0 1257 }
aoqi@0 1258
aoqi@0 1259 Node* upper_insert_pt = first->in(MemNode::Memory);
aoqi@0 1260 // Following code moves loads connected to upper_insert_pt below aliased stores.
aoqi@0 1261 // Collect such loads here and reconnect them back to upper_insert_pt later.
aoqi@0 1262 memops.clear();
aoqi@0 1263 for (DUIterator i = upper_insert_pt->outs(); upper_insert_pt->has_out(i); i++) {
aoqi@0 1264 Node* use = upper_insert_pt->out(i);
aoqi@0 1265 if (use->is_Mem() && !use->is_Store()) {
aoqi@0 1266 memops.push(use);
aoqi@0 1267 }
aoqi@0 1268 }
aoqi@0 1269
aoqi@0 1270 MemNode* lower_insert_pt = last;
aoqi@0 1271 previous = last; //previous store in pk
aoqi@0 1272 current = last->in(MemNode::Memory)->as_Mem();
aoqi@0 1273
aoqi@0 1274 // start scheduling from "last" to "first"
aoqi@0 1275 while (true) {
aoqi@0 1276 assert(in_bb(current), "stay in block");
aoqi@0 1277 assert(in_pack(previous, pk), "previous stays in pack");
aoqi@0 1278 Node* my_mem = current->in(MemNode::Memory);
aoqi@0 1279
aoqi@0 1280 if (in_pack(current, pk)) {
aoqi@0 1281 // Forward users of my memory state (except "previous) to my input memory state
aoqi@0 1282 for (DUIterator i = current->outs(); current->has_out(i); i++) {
aoqi@0 1283 Node* use = current->out(i);
aoqi@0 1284 if (use->is_Mem() && use != previous) {
aoqi@0 1285 assert(use->in(MemNode::Memory) == current, "must be");
aoqi@0 1286 if (schedule_before_pack.member(use)) {
aoqi@0 1287 _igvn.replace_input_of(use, MemNode::Memory, upper_insert_pt);
aoqi@0 1288 } else {
aoqi@0 1289 _igvn.replace_input_of(use, MemNode::Memory, lower_insert_pt);
aoqi@0 1290 }
aoqi@0 1291 --i; // deleted this edge; rescan position
aoqi@0 1292 }
aoqi@0 1293 }
aoqi@0 1294 previous = current;
aoqi@0 1295 } else { // !in_pack(current, pk) ==> a sandwiched store
aoqi@0 1296 remove_and_insert(current, previous, lower_insert_pt, upper_insert_pt, schedule_before_pack);
aoqi@0 1297 }
aoqi@0 1298
aoqi@0 1299 if (current == first) break;
aoqi@0 1300 current = my_mem->as_Mem();
aoqi@0 1301 } // end while
aoqi@0 1302
aoqi@0 1303 // Reconnect loads back to upper_insert_pt.
aoqi@0 1304 for (uint i = 0; i < memops.size(); i++) {
aoqi@0 1305 Node *ld = memops.at(i);
aoqi@0 1306 if (ld->in(MemNode::Memory) != upper_insert_pt) {
aoqi@0 1307 _igvn.replace_input_of(ld, MemNode::Memory, upper_insert_pt);
aoqi@0 1308 }
aoqi@0 1309 }
aoqi@0 1310 } else if (pk->at(0)->is_Load()) { //load
aoqi@0 1311 // all loads in the pack should have the same memory state. By default,
aoqi@0 1312 // we use the memory state of the last load. However, if any load could
aoqi@0 1313 // not be moved down due to the dependence constraint, we use the memory
aoqi@0 1314 // state of the first load.
aoqi@0 1315 Node* last_mem = executed_last(pk)->in(MemNode::Memory);
aoqi@0 1316 Node* first_mem = executed_first(pk)->in(MemNode::Memory);
aoqi@0 1317 bool schedule_last = true;
aoqi@0 1318 for (uint i = 0; i < pk->size(); i++) {
aoqi@0 1319 Node* ld = pk->at(i);
aoqi@0 1320 for (Node* current = last_mem; current != ld->in(MemNode::Memory);
aoqi@0 1321 current=current->in(MemNode::Memory)) {
aoqi@0 1322 assert(current != first_mem, "corrupted memory graph");
aoqi@0 1323 if(current->is_Mem() && !independent(current, ld)){
aoqi@0 1324 schedule_last = false; // a later store depends on this load
aoqi@0 1325 break;
aoqi@0 1326 }
aoqi@0 1327 }
aoqi@0 1328 }
aoqi@0 1329
aoqi@0 1330 Node* mem_input = schedule_last ? last_mem : first_mem;
aoqi@0 1331 _igvn.hash_delete(mem_input);
aoqi@0 1332 // Give each load the same memory state
aoqi@0 1333 for (uint i = 0; i < pk->size(); i++) {
aoqi@0 1334 LoadNode* ld = pk->at(i)->as_Load();
aoqi@0 1335 _igvn.replace_input_of(ld, MemNode::Memory, mem_input);
aoqi@0 1336 }
aoqi@0 1337 }
aoqi@0 1338 }
aoqi@0 1339
aoqi@0 1340 //------------------------------output---------------------------
aoqi@0 1341 // Convert packs into vector node operations
aoqi@0 1342 void SuperWord::output() {
aoqi@0 1343 if (_packset.length() == 0) return;
aoqi@0 1344
aoqi@0 1345 #ifndef PRODUCT
aoqi@0 1346 if (TraceLoopOpts) {
aoqi@0 1347 tty->print("SuperWord ");
aoqi@0 1348 lpt()->dump_head();
aoqi@0 1349 }
aoqi@0 1350 #endif
aoqi@0 1351
aoqi@0 1352 // MUST ENSURE main loop's initial value is properly aligned:
aoqi@0 1353 // (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0
aoqi@0 1354
aoqi@0 1355 align_initial_loop_index(align_to_ref());
aoqi@0 1356
aoqi@0 1357 // Insert extract (unpack) operations for scalar uses
aoqi@0 1358 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 1359 insert_extracts(_packset.at(i));
aoqi@0 1360 }
aoqi@0 1361
aoqi@0 1362 Compile* C = _phase->C;
aoqi@0 1363 uint max_vlen_in_bytes = 0;
aoqi@0 1364 for (int i = 0; i < _block.length(); i++) {
aoqi@0 1365 Node* n = _block.at(i);
aoqi@0 1366 Node_List* p = my_pack(n);
aoqi@0 1367 if (p && n == executed_last(p)) {
aoqi@0 1368 uint vlen = p->size();
aoqi@0 1369 uint vlen_in_bytes = 0;
aoqi@0 1370 Node* vn = NULL;
aoqi@0 1371 Node* low_adr = p->at(0);
aoqi@0 1372 Node* first = executed_first(p);
aoqi@0 1373 int opc = n->Opcode();
aoqi@0 1374 if (n->is_Load()) {
aoqi@0 1375 Node* ctl = n->in(MemNode::Control);
aoqi@0 1376 Node* mem = first->in(MemNode::Memory);
aoqi@0 1377 Node* adr = low_adr->in(MemNode::Address);
aoqi@0 1378 const TypePtr* atyp = n->adr_type();
aoqi@0 1379 vn = LoadVectorNode::make(C, opc, ctl, mem, adr, atyp, vlen, velt_basic_type(n));
aoqi@0 1380 vlen_in_bytes = vn->as_LoadVector()->memory_size();
aoqi@0 1381 } else if (n->is_Store()) {
aoqi@0 1382 // Promote value to be stored to vector
aoqi@0 1383 Node* val = vector_opd(p, MemNode::ValueIn);
aoqi@0 1384 Node* ctl = n->in(MemNode::Control);
aoqi@0 1385 Node* mem = first->in(MemNode::Memory);
aoqi@0 1386 Node* adr = low_adr->in(MemNode::Address);
aoqi@0 1387 const TypePtr* atyp = n->adr_type();
aoqi@0 1388 vn = StoreVectorNode::make(C, opc, ctl, mem, adr, atyp, val, vlen);
aoqi@0 1389 vlen_in_bytes = vn->as_StoreVector()->memory_size();
aoqi@0 1390 } else if (n->req() == 3) {
aoqi@0 1391 // Promote operands to vector
aoqi@0 1392 Node* in1 = vector_opd(p, 1);
aoqi@0 1393 Node* in2 = vector_opd(p, 2);
aoqi@0 1394 if (VectorNode::is_invariant_vector(in1) && (n->is_Add() || n->is_Mul())) {
aoqi@0 1395 // Move invariant vector input into second position to avoid register spilling.
aoqi@0 1396 Node* tmp = in1;
aoqi@0 1397 in1 = in2;
aoqi@0 1398 in2 = tmp;
aoqi@0 1399 }
aoqi@0 1400 vn = VectorNode::make(C, opc, in1, in2, vlen, velt_basic_type(n));
aoqi@0 1401 vlen_in_bytes = vn->as_Vector()->length_in_bytes();
aoqi@0 1402 } else {
aoqi@0 1403 ShouldNotReachHere();
aoqi@0 1404 }
aoqi@0 1405 assert(vn != NULL, "sanity");
aoqi@0 1406 _igvn.register_new_node_with_optimizer(vn);
aoqi@0 1407 _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0)));
aoqi@0 1408 for (uint j = 0; j < p->size(); j++) {
aoqi@0 1409 Node* pm = p->at(j);
aoqi@0 1410 _igvn.replace_node(pm, vn);
aoqi@0 1411 }
aoqi@0 1412 _igvn._worklist.push(vn);
aoqi@0 1413
aoqi@0 1414 if (vlen_in_bytes > max_vlen_in_bytes) {
aoqi@0 1415 max_vlen_in_bytes = vlen_in_bytes;
aoqi@0 1416 }
aoqi@0 1417 #ifdef ASSERT
aoqi@0 1418 if (TraceNewVectors) {
aoqi@0 1419 tty->print("new Vector node: ");
aoqi@0 1420 vn->dump();
aoqi@0 1421 }
aoqi@0 1422 #endif
aoqi@0 1423 }
aoqi@0 1424 }
aoqi@0 1425 C->set_max_vector_size(max_vlen_in_bytes);
aoqi@0 1426 }
aoqi@0 1427
aoqi@0 1428 //------------------------------vector_opd---------------------------
aoqi@0 1429 // Create a vector operand for the nodes in pack p for operand: in(opd_idx)
aoqi@0 1430 Node* SuperWord::vector_opd(Node_List* p, int opd_idx) {
aoqi@0 1431 Node* p0 = p->at(0);
aoqi@0 1432 uint vlen = p->size();
aoqi@0 1433 Node* opd = p0->in(opd_idx);
aoqi@0 1434
aoqi@0 1435 if (same_inputs(p, opd_idx)) {
aoqi@0 1436 if (opd->is_Vector() || opd->is_LoadVector()) {
aoqi@0 1437 assert(((opd_idx != 2) || !VectorNode::is_shift(p0)), "shift's count can't be vector");
aoqi@0 1438 return opd; // input is matching vector
aoqi@0 1439 }
aoqi@0 1440 if ((opd_idx == 2) && VectorNode::is_shift(p0)) {
aoqi@0 1441 Compile* C = _phase->C;
aoqi@0 1442 Node* cnt = opd;
aoqi@0 1443 // Vector instructions do not mask shift count, do it here.
aoqi@0 1444 juint mask = (p0->bottom_type() == TypeInt::INT) ? (BitsPerInt - 1) : (BitsPerLong - 1);
aoqi@0 1445 const TypeInt* t = opd->find_int_type();
aoqi@0 1446 if (t != NULL && t->is_con()) {
aoqi@0 1447 juint shift = t->get_con();
aoqi@0 1448 if (shift > mask) { // Unsigned cmp
aoqi@0 1449 cnt = ConNode::make(C, TypeInt::make(shift & mask));
aoqi@0 1450 }
aoqi@0 1451 } else {
aoqi@0 1452 if (t == NULL || t->_lo < 0 || t->_hi > (int)mask) {
aoqi@0 1453 cnt = ConNode::make(C, TypeInt::make(mask));
aoqi@0 1454 _igvn.register_new_node_with_optimizer(cnt);
aoqi@0 1455 cnt = new (C) AndINode(opd, cnt);
aoqi@0 1456 _igvn.register_new_node_with_optimizer(cnt);
aoqi@0 1457 _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
aoqi@0 1458 }
aoqi@0 1459 assert(opd->bottom_type()->isa_int(), "int type only");
aoqi@0 1460 // Move non constant shift count into vector register.
aoqi@0 1461 cnt = VectorNode::shift_count(C, p0, cnt, vlen, velt_basic_type(p0));
aoqi@0 1462 }
aoqi@0 1463 if (cnt != opd) {
aoqi@0 1464 _igvn.register_new_node_with_optimizer(cnt);
aoqi@0 1465 _phase->set_ctrl(cnt, _phase->get_ctrl(opd));
aoqi@0 1466 }
aoqi@0 1467 return cnt;
aoqi@0 1468 }
aoqi@0 1469 assert(!opd->is_StoreVector(), "such vector is not expected here");
aoqi@0 1470 // Convert scalar input to vector with the same number of elements as
aoqi@0 1471 // p0's vector. Use p0's type because size of operand's container in
aoqi@0 1472 // vector should match p0's size regardless operand's size.
aoqi@0 1473 const Type* p0_t = velt_type(p0);
aoqi@0 1474 VectorNode* vn = VectorNode::scalar2vector(_phase->C, opd, vlen, p0_t);
aoqi@0 1475
aoqi@0 1476 _igvn.register_new_node_with_optimizer(vn);
aoqi@0 1477 _phase->set_ctrl(vn, _phase->get_ctrl(opd));
aoqi@0 1478 #ifdef ASSERT
aoqi@0 1479 if (TraceNewVectors) {
aoqi@0 1480 tty->print("new Vector node: ");
aoqi@0 1481 vn->dump();
aoqi@0 1482 }
aoqi@0 1483 #endif
aoqi@0 1484 return vn;
aoqi@0 1485 }
aoqi@0 1486
aoqi@0 1487 // Insert pack operation
aoqi@0 1488 BasicType bt = velt_basic_type(p0);
aoqi@0 1489 PackNode* pk = PackNode::make(_phase->C, opd, vlen, bt);
aoqi@0 1490 DEBUG_ONLY( const BasicType opd_bt = opd->bottom_type()->basic_type(); )
aoqi@0 1491
aoqi@0 1492 for (uint i = 1; i < vlen; i++) {
aoqi@0 1493 Node* pi = p->at(i);
aoqi@0 1494 Node* in = pi->in(opd_idx);
aoqi@0 1495 assert(my_pack(in) == NULL, "Should already have been unpacked");
aoqi@0 1496 assert(opd_bt == in->bottom_type()->basic_type(), "all same type");
aoqi@0 1497 pk->add_opd(in);
aoqi@0 1498 }
aoqi@0 1499 _igvn.register_new_node_with_optimizer(pk);
aoqi@0 1500 _phase->set_ctrl(pk, _phase->get_ctrl(opd));
aoqi@0 1501 #ifdef ASSERT
aoqi@0 1502 if (TraceNewVectors) {
aoqi@0 1503 tty->print("new Vector node: ");
aoqi@0 1504 pk->dump();
aoqi@0 1505 }
aoqi@0 1506 #endif
aoqi@0 1507 return pk;
aoqi@0 1508 }
aoqi@0 1509
aoqi@0 1510 //------------------------------insert_extracts---------------------------
aoqi@0 1511 // If a use of pack p is not a vector use, then replace the
aoqi@0 1512 // use with an extract operation.
aoqi@0 1513 void SuperWord::insert_extracts(Node_List* p) {
aoqi@0 1514 if (p->at(0)->is_Store()) return;
aoqi@0 1515 assert(_n_idx_list.is_empty(), "empty (node,index) list");
aoqi@0 1516
aoqi@0 1517 // Inspect each use of each pack member. For each use that is
aoqi@0 1518 // not a vector use, replace the use with an extract operation.
aoqi@0 1519
aoqi@0 1520 for (uint i = 0; i < p->size(); i++) {
aoqi@0 1521 Node* def = p->at(i);
aoqi@0 1522 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
aoqi@0 1523 Node* use = def->fast_out(j);
aoqi@0 1524 for (uint k = 0; k < use->req(); k++) {
aoqi@0 1525 Node* n = use->in(k);
aoqi@0 1526 if (def == n) {
aoqi@0 1527 if (!is_vector_use(use, k)) {
aoqi@0 1528 _n_idx_list.push(use, k);
aoqi@0 1529 }
aoqi@0 1530 }
aoqi@0 1531 }
aoqi@0 1532 }
aoqi@0 1533 }
aoqi@0 1534
aoqi@0 1535 while (_n_idx_list.is_nonempty()) {
aoqi@0 1536 Node* use = _n_idx_list.node();
aoqi@0 1537 int idx = _n_idx_list.index();
aoqi@0 1538 _n_idx_list.pop();
aoqi@0 1539 Node* def = use->in(idx);
aoqi@0 1540
aoqi@0 1541 // Insert extract operation
aoqi@0 1542 _igvn.hash_delete(def);
aoqi@0 1543 int def_pos = alignment(def) / data_size(def);
aoqi@0 1544
aoqi@0 1545 Node* ex = ExtractNode::make(_phase->C, def, def_pos, velt_basic_type(def));
aoqi@0 1546 _igvn.register_new_node_with_optimizer(ex);
aoqi@0 1547 _phase->set_ctrl(ex, _phase->get_ctrl(def));
aoqi@0 1548 _igvn.replace_input_of(use, idx, ex);
aoqi@0 1549 _igvn._worklist.push(def);
aoqi@0 1550
aoqi@0 1551 bb_insert_after(ex, bb_idx(def));
aoqi@0 1552 set_velt_type(ex, velt_type(def));
aoqi@0 1553 }
aoqi@0 1554 }
aoqi@0 1555
aoqi@0 1556 //------------------------------is_vector_use---------------------------
aoqi@0 1557 // Is use->in(u_idx) a vector use?
aoqi@0 1558 bool SuperWord::is_vector_use(Node* use, int u_idx) {
aoqi@0 1559 Node_List* u_pk = my_pack(use);
aoqi@0 1560 if (u_pk == NULL) return false;
aoqi@0 1561 Node* def = use->in(u_idx);
aoqi@0 1562 Node_List* d_pk = my_pack(def);
aoqi@0 1563 if (d_pk == NULL) {
aoqi@0 1564 // check for scalar promotion
aoqi@0 1565 Node* n = u_pk->at(0)->in(u_idx);
aoqi@0 1566 for (uint i = 1; i < u_pk->size(); i++) {
aoqi@0 1567 if (u_pk->at(i)->in(u_idx) != n) return false;
aoqi@0 1568 }
aoqi@0 1569 return true;
aoqi@0 1570 }
aoqi@0 1571 if (u_pk->size() != d_pk->size())
aoqi@0 1572 return false;
aoqi@0 1573 for (uint i = 0; i < u_pk->size(); i++) {
aoqi@0 1574 Node* ui = u_pk->at(i);
aoqi@0 1575 Node* di = d_pk->at(i);
aoqi@0 1576 if (ui->in(u_idx) != di || alignment(ui) != alignment(di))
aoqi@0 1577 return false;
aoqi@0 1578 }
aoqi@0 1579 return true;
aoqi@0 1580 }
aoqi@0 1581
aoqi@0 1582 //------------------------------construct_bb---------------------------
aoqi@0 1583 // Construct reverse postorder list of block members
aoqi@0 1584 bool SuperWord::construct_bb() {
aoqi@0 1585 Node* entry = bb();
aoqi@0 1586
aoqi@0 1587 assert(_stk.length() == 0, "stk is empty");
aoqi@0 1588 assert(_block.length() == 0, "block is empty");
aoqi@0 1589 assert(_data_entry.length() == 0, "data_entry is empty");
aoqi@0 1590 assert(_mem_slice_head.length() == 0, "mem_slice_head is empty");
aoqi@0 1591 assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty");
aoqi@0 1592
aoqi@0 1593 // Find non-control nodes with no inputs from within block,
aoqi@0 1594 // create a temporary map from node _idx to bb_idx for use
aoqi@0 1595 // by the visited and post_visited sets,
aoqi@0 1596 // and count number of nodes in block.
aoqi@0 1597 int bb_ct = 0;
aoqi@0 1598 for (uint i = 0; i < lpt()->_body.size(); i++ ) {
aoqi@0 1599 Node *n = lpt()->_body.at(i);
aoqi@0 1600 set_bb_idx(n, i); // Create a temporary map
aoqi@0 1601 if (in_bb(n)) {
aoqi@0 1602 if (n->is_LoadStore() || n->is_MergeMem() ||
aoqi@0 1603 (n->is_Proj() && !n->as_Proj()->is_CFG())) {
aoqi@0 1604 // Bailout if the loop has LoadStore, MergeMem or data Proj
aoqi@0 1605 // nodes. Superword optimization does not work with them.
aoqi@0 1606 return false;
aoqi@0 1607 }
aoqi@0 1608 bb_ct++;
aoqi@0 1609 if (!n->is_CFG()) {
aoqi@0 1610 bool found = false;
aoqi@0 1611 for (uint j = 0; j < n->req(); j++) {
aoqi@0 1612 Node* def = n->in(j);
aoqi@0 1613 if (def && in_bb(def)) {
aoqi@0 1614 found = true;
aoqi@0 1615 break;
aoqi@0 1616 }
aoqi@0 1617 }
aoqi@0 1618 if (!found) {
aoqi@0 1619 assert(n != entry, "can't be entry");
aoqi@0 1620 _data_entry.push(n);
aoqi@0 1621 }
aoqi@0 1622 }
aoqi@0 1623 }
aoqi@0 1624 }
aoqi@0 1625
aoqi@0 1626 // Find memory slices (head and tail)
aoqi@0 1627 for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) {
aoqi@0 1628 Node *n = lp()->fast_out(i);
aoqi@0 1629 if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
aoqi@0 1630 Node* n_tail = n->in(LoopNode::LoopBackControl);
aoqi@0 1631 if (n_tail != n->in(LoopNode::EntryControl)) {
aoqi@0 1632 if (!n_tail->is_Mem()) {
aoqi@0 1633 assert(n_tail->is_Mem(), err_msg_res("unexpected node for memory slice: %s", n_tail->Name()));
aoqi@0 1634 return false; // Bailout
aoqi@0 1635 }
aoqi@0 1636 _mem_slice_head.push(n);
aoqi@0 1637 _mem_slice_tail.push(n_tail);
aoqi@0 1638 }
aoqi@0 1639 }
aoqi@0 1640 }
aoqi@0 1641
aoqi@0 1642 // Create an RPO list of nodes in block
aoqi@0 1643
aoqi@0 1644 visited_clear();
aoqi@0 1645 post_visited_clear();
aoqi@0 1646
aoqi@0 1647 // Push all non-control nodes with no inputs from within block, then control entry
aoqi@0 1648 for (int j = 0; j < _data_entry.length(); j++) {
aoqi@0 1649 Node* n = _data_entry.at(j);
aoqi@0 1650 visited_set(n);
aoqi@0 1651 _stk.push(n);
aoqi@0 1652 }
aoqi@0 1653 visited_set(entry);
aoqi@0 1654 _stk.push(entry);
aoqi@0 1655
aoqi@0 1656 // Do a depth first walk over out edges
aoqi@0 1657 int rpo_idx = bb_ct - 1;
aoqi@0 1658 int size;
aoqi@0 1659 while ((size = _stk.length()) > 0) {
aoqi@0 1660 Node* n = _stk.top(); // Leave node on stack
aoqi@0 1661 if (!visited_test_set(n)) {
aoqi@0 1662 // forward arc in graph
aoqi@0 1663 } else if (!post_visited_test(n)) {
aoqi@0 1664 // cross or back arc
aoqi@0 1665 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
aoqi@0 1666 Node *use = n->fast_out(i);
aoqi@0 1667 if (in_bb(use) && !visited_test(use) &&
aoqi@0 1668 // Don't go around backedge
aoqi@0 1669 (!use->is_Phi() || n == entry)) {
aoqi@0 1670 _stk.push(use);
aoqi@0 1671 }
aoqi@0 1672 }
aoqi@0 1673 if (_stk.length() == size) {
aoqi@0 1674 // There were no additional uses, post visit node now
aoqi@0 1675 _stk.pop(); // Remove node from stack
aoqi@0 1676 assert(rpo_idx >= 0, "");
aoqi@0 1677 _block.at_put_grow(rpo_idx, n);
aoqi@0 1678 rpo_idx--;
aoqi@0 1679 post_visited_set(n);
aoqi@0 1680 assert(rpo_idx >= 0 || _stk.is_empty(), "");
aoqi@0 1681 }
aoqi@0 1682 } else {
aoqi@0 1683 _stk.pop(); // Remove post-visited node from stack
aoqi@0 1684 }
aoqi@0 1685 }
aoqi@0 1686
aoqi@0 1687 // Create real map of block indices for nodes
aoqi@0 1688 for (int j = 0; j < _block.length(); j++) {
aoqi@0 1689 Node* n = _block.at(j);
aoqi@0 1690 set_bb_idx(n, j);
aoqi@0 1691 }
aoqi@0 1692
aoqi@0 1693 initialize_bb(); // Ensure extra info is allocated.
aoqi@0 1694
aoqi@0 1695 #ifndef PRODUCT
aoqi@0 1696 if (TraceSuperWord) {
aoqi@0 1697 print_bb();
aoqi@0 1698 tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE");
aoqi@0 1699 for (int m = 0; m < _data_entry.length(); m++) {
aoqi@0 1700 tty->print("%3d ", m);
aoqi@0 1701 _data_entry.at(m)->dump();
aoqi@0 1702 }
aoqi@0 1703 tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE");
aoqi@0 1704 for (int m = 0; m < _mem_slice_head.length(); m++) {
aoqi@0 1705 tty->print("%3d ", m); _mem_slice_head.at(m)->dump();
aoqi@0 1706 tty->print(" "); _mem_slice_tail.at(m)->dump();
aoqi@0 1707 }
aoqi@0 1708 }
aoqi@0 1709 #endif
aoqi@0 1710 assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found");
aoqi@0 1711 return (_mem_slice_head.length() > 0) || (_data_entry.length() > 0);
aoqi@0 1712 }
aoqi@0 1713
aoqi@0 1714 //------------------------------initialize_bb---------------------------
aoqi@0 1715 // Initialize per node info
aoqi@0 1716 void SuperWord::initialize_bb() {
aoqi@0 1717 Node* last = _block.at(_block.length() - 1);
aoqi@0 1718 grow_node_info(bb_idx(last));
aoqi@0 1719 }
aoqi@0 1720
aoqi@0 1721 //------------------------------bb_insert_after---------------------------
aoqi@0 1722 // Insert n into block after pos
aoqi@0 1723 void SuperWord::bb_insert_after(Node* n, int pos) {
aoqi@0 1724 int n_pos = pos + 1;
aoqi@0 1725 // Make room
aoqi@0 1726 for (int i = _block.length() - 1; i >= n_pos; i--) {
aoqi@0 1727 _block.at_put_grow(i+1, _block.at(i));
aoqi@0 1728 }
aoqi@0 1729 for (int j = _node_info.length() - 1; j >= n_pos; j--) {
aoqi@0 1730 _node_info.at_put_grow(j+1, _node_info.at(j));
aoqi@0 1731 }
aoqi@0 1732 // Set value
aoqi@0 1733 _block.at_put_grow(n_pos, n);
aoqi@0 1734 _node_info.at_put_grow(n_pos, SWNodeInfo::initial);
aoqi@0 1735 // Adjust map from node->_idx to _block index
aoqi@0 1736 for (int i = n_pos; i < _block.length(); i++) {
aoqi@0 1737 set_bb_idx(_block.at(i), i);
aoqi@0 1738 }
aoqi@0 1739 }
aoqi@0 1740
aoqi@0 1741 //------------------------------compute_max_depth---------------------------
aoqi@0 1742 // Compute max depth for expressions from beginning of block
aoqi@0 1743 // Use to prune search paths during test for independence.
aoqi@0 1744 void SuperWord::compute_max_depth() {
aoqi@0 1745 int ct = 0;
aoqi@0 1746 bool again;
aoqi@0 1747 do {
aoqi@0 1748 again = false;
aoqi@0 1749 for (int i = 0; i < _block.length(); i++) {
aoqi@0 1750 Node* n = _block.at(i);
aoqi@0 1751 if (!n->is_Phi()) {
aoqi@0 1752 int d_orig = depth(n);
aoqi@0 1753 int d_in = 0;
aoqi@0 1754 for (DepPreds preds(n, _dg); !preds.done(); preds.next()) {
aoqi@0 1755 Node* pred = preds.current();
aoqi@0 1756 if (in_bb(pred)) {
aoqi@0 1757 d_in = MAX2(d_in, depth(pred));
aoqi@0 1758 }
aoqi@0 1759 }
aoqi@0 1760 if (d_in + 1 != d_orig) {
aoqi@0 1761 set_depth(n, d_in + 1);
aoqi@0 1762 again = true;
aoqi@0 1763 }
aoqi@0 1764 }
aoqi@0 1765 }
aoqi@0 1766 ct++;
aoqi@0 1767 } while (again);
aoqi@0 1768 #ifndef PRODUCT
aoqi@0 1769 if (TraceSuperWord && Verbose)
aoqi@0 1770 tty->print_cr("compute_max_depth iterated: %d times", ct);
aoqi@0 1771 #endif
aoqi@0 1772 }
aoqi@0 1773
aoqi@0 1774 //-------------------------compute_vector_element_type-----------------------
aoqi@0 1775 // Compute necessary vector element type for expressions
aoqi@0 1776 // This propagates backwards a narrower integer type when the
aoqi@0 1777 // upper bits of the value are not needed.
aoqi@0 1778 // Example: char a,b,c; a = b + c;
aoqi@0 1779 // Normally the type of the add is integer, but for packed character
aoqi@0 1780 // operations the type of the add needs to be char.
aoqi@0 1781 void SuperWord::compute_vector_element_type() {
aoqi@0 1782 #ifndef PRODUCT
aoqi@0 1783 if (TraceSuperWord && Verbose)
aoqi@0 1784 tty->print_cr("\ncompute_velt_type:");
aoqi@0 1785 #endif
aoqi@0 1786
aoqi@0 1787 // Initial type
aoqi@0 1788 for (int i = 0; i < _block.length(); i++) {
aoqi@0 1789 Node* n = _block.at(i);
aoqi@0 1790 set_velt_type(n, container_type(n));
aoqi@0 1791 }
aoqi@0 1792
aoqi@0 1793 // Propagate integer narrowed type backwards through operations
aoqi@0 1794 // that don't depend on higher order bits
aoqi@0 1795 for (int i = _block.length() - 1; i >= 0; i--) {
aoqi@0 1796 Node* n = _block.at(i);
aoqi@0 1797 // Only integer types need be examined
aoqi@0 1798 const Type* vtn = velt_type(n);
aoqi@0 1799 if (vtn->basic_type() == T_INT) {
aoqi@0 1800 uint start, end;
aoqi@0 1801 VectorNode::vector_operands(n, &start, &end);
aoqi@0 1802
aoqi@0 1803 for (uint j = start; j < end; j++) {
aoqi@0 1804 Node* in = n->in(j);
aoqi@0 1805 // Don't propagate through a memory
aoqi@0 1806 if (!in->is_Mem() && in_bb(in) && velt_type(in)->basic_type() == T_INT &&
aoqi@0 1807 data_size(n) < data_size(in)) {
aoqi@0 1808 bool same_type = true;
aoqi@0 1809 for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
aoqi@0 1810 Node *use = in->fast_out(k);
aoqi@0 1811 if (!in_bb(use) || !same_velt_type(use, n)) {
aoqi@0 1812 same_type = false;
aoqi@0 1813 break;
aoqi@0 1814 }
aoqi@0 1815 }
aoqi@0 1816 if (same_type) {
aoqi@0 1817 // For right shifts of small integer types (bool, byte, char, short)
aoqi@0 1818 // we need precise information about sign-ness. Only Load nodes have
aoqi@0 1819 // this information because Store nodes are the same for signed and
aoqi@0 1820 // unsigned values. And any arithmetic operation after a load may
aoqi@0 1821 // expand a value to signed Int so such right shifts can't be used
aoqi@0 1822 // because vector elements do not have upper bits of Int.
aoqi@0 1823 const Type* vt = vtn;
aoqi@0 1824 if (VectorNode::is_shift(in)) {
aoqi@0 1825 Node* load = in->in(1);
aoqi@0 1826 if (load->is_Load() && in_bb(load) && (velt_type(load)->basic_type() == T_INT)) {
aoqi@0 1827 vt = velt_type(load);
aoqi@0 1828 } else if (in->Opcode() != Op_LShiftI) {
aoqi@0 1829 // Widen type to Int to avoid creation of right shift vector
aoqi@0 1830 // (align + data_size(s1) check in stmts_can_pack() will fail).
aoqi@0 1831 // Note, left shifts work regardless type.
aoqi@0 1832 vt = TypeInt::INT;
aoqi@0 1833 }
aoqi@0 1834 }
aoqi@0 1835 set_velt_type(in, vt);
aoqi@0 1836 }
aoqi@0 1837 }
aoqi@0 1838 }
aoqi@0 1839 }
aoqi@0 1840 }
aoqi@0 1841 #ifndef PRODUCT
aoqi@0 1842 if (TraceSuperWord && Verbose) {
aoqi@0 1843 for (int i = 0; i < _block.length(); i++) {
aoqi@0 1844 Node* n = _block.at(i);
aoqi@0 1845 velt_type(n)->dump();
aoqi@0 1846 tty->print("\t");
aoqi@0 1847 n->dump();
aoqi@0 1848 }
aoqi@0 1849 }
aoqi@0 1850 #endif
aoqi@0 1851 }
aoqi@0 1852
aoqi@0 1853 //------------------------------memory_alignment---------------------------
aoqi@0 1854 // Alignment within a vector memory reference
aoqi@0 1855 int SuperWord::memory_alignment(MemNode* s, int iv_adjust) {
aoqi@0 1856 SWPointer p(s, this);
aoqi@0 1857 if (!p.valid()) {
aoqi@0 1858 return bottom_align;
aoqi@0 1859 }
aoqi@0 1860 int vw = vector_width_in_bytes(s);
aoqi@0 1861 if (vw < 2) {
aoqi@0 1862 return bottom_align; // No vectors for this type
aoqi@0 1863 }
aoqi@0 1864 int offset = p.offset_in_bytes();
aoqi@0 1865 offset += iv_adjust*p.memory_size();
aoqi@0 1866 int off_rem = offset % vw;
aoqi@0 1867 int off_mod = off_rem >= 0 ? off_rem : off_rem + vw;
aoqi@0 1868 return off_mod;
aoqi@0 1869 }
aoqi@0 1870
aoqi@0 1871 //---------------------------container_type---------------------------
aoqi@0 1872 // Smallest type containing range of values
aoqi@0 1873 const Type* SuperWord::container_type(Node* n) {
aoqi@0 1874 if (n->is_Mem()) {
aoqi@0 1875 BasicType bt = n->as_Mem()->memory_type();
aoqi@0 1876 if (n->is_Store() && (bt == T_CHAR)) {
aoqi@0 1877 // Use T_SHORT type instead of T_CHAR for stored values because any
aoqi@0 1878 // preceding arithmetic operation extends values to signed Int.
aoqi@0 1879 bt = T_SHORT;
aoqi@0 1880 }
aoqi@0 1881 if (n->Opcode() == Op_LoadUB) {
aoqi@0 1882 // Adjust type for unsigned byte loads, it is important for right shifts.
aoqi@0 1883 // T_BOOLEAN is used because there is no basic type representing type
aoqi@0 1884 // TypeInt::UBYTE. Use of T_BOOLEAN for vectors is fine because only
aoqi@0 1885 // size (one byte) and sign is important.
aoqi@0 1886 bt = T_BOOLEAN;
aoqi@0 1887 }
aoqi@0 1888 return Type::get_const_basic_type(bt);
aoqi@0 1889 }
aoqi@0 1890 const Type* t = _igvn.type(n);
aoqi@0 1891 if (t->basic_type() == T_INT) {
aoqi@0 1892 // A narrow type of arithmetic operations will be determined by
aoqi@0 1893 // propagating the type of memory operations.
aoqi@0 1894 return TypeInt::INT;
aoqi@0 1895 }
aoqi@0 1896 return t;
aoqi@0 1897 }
aoqi@0 1898
aoqi@0 1899 bool SuperWord::same_velt_type(Node* n1, Node* n2) {
aoqi@0 1900 const Type* vt1 = velt_type(n1);
aoqi@0 1901 const Type* vt2 = velt_type(n2);
aoqi@0 1902 if (vt1->basic_type() == T_INT && vt2->basic_type() == T_INT) {
aoqi@0 1903 // Compare vectors element sizes for integer types.
aoqi@0 1904 return data_size(n1) == data_size(n2);
aoqi@0 1905 }
aoqi@0 1906 return vt1 == vt2;
aoqi@0 1907 }
aoqi@0 1908
aoqi@0 1909 //------------------------------in_packset---------------------------
aoqi@0 1910 // Are s1 and s2 in a pack pair and ordered as s1,s2?
aoqi@0 1911 bool SuperWord::in_packset(Node* s1, Node* s2) {
aoqi@0 1912 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 1913 Node_List* p = _packset.at(i);
aoqi@0 1914 assert(p->size() == 2, "must be");
aoqi@0 1915 if (p->at(0) == s1 && p->at(p->size()-1) == s2) {
aoqi@0 1916 return true;
aoqi@0 1917 }
aoqi@0 1918 }
aoqi@0 1919 return false;
aoqi@0 1920 }
aoqi@0 1921
aoqi@0 1922 //------------------------------in_pack---------------------------
aoqi@0 1923 // Is s in pack p?
aoqi@0 1924 Node_List* SuperWord::in_pack(Node* s, Node_List* p) {
aoqi@0 1925 for (uint i = 0; i < p->size(); i++) {
aoqi@0 1926 if (p->at(i) == s) {
aoqi@0 1927 return p;
aoqi@0 1928 }
aoqi@0 1929 }
aoqi@0 1930 return NULL;
aoqi@0 1931 }
aoqi@0 1932
aoqi@0 1933 //------------------------------remove_pack_at---------------------------
aoqi@0 1934 // Remove the pack at position pos in the packset
aoqi@0 1935 void SuperWord::remove_pack_at(int pos) {
aoqi@0 1936 Node_List* p = _packset.at(pos);
aoqi@0 1937 for (uint i = 0; i < p->size(); i++) {
aoqi@0 1938 Node* s = p->at(i);
aoqi@0 1939 set_my_pack(s, NULL);
aoqi@0 1940 }
aoqi@0 1941 _packset.remove_at(pos);
aoqi@0 1942 }
aoqi@0 1943
aoqi@0 1944 //------------------------------executed_first---------------------------
aoqi@0 1945 // Return the node executed first in pack p. Uses the RPO block list
aoqi@0 1946 // to determine order.
aoqi@0 1947 Node* SuperWord::executed_first(Node_List* p) {
aoqi@0 1948 Node* n = p->at(0);
aoqi@0 1949 int n_rpo = bb_idx(n);
aoqi@0 1950 for (uint i = 1; i < p->size(); i++) {
aoqi@0 1951 Node* s = p->at(i);
aoqi@0 1952 int s_rpo = bb_idx(s);
aoqi@0 1953 if (s_rpo < n_rpo) {
aoqi@0 1954 n = s;
aoqi@0 1955 n_rpo = s_rpo;
aoqi@0 1956 }
aoqi@0 1957 }
aoqi@0 1958 return n;
aoqi@0 1959 }
aoqi@0 1960
aoqi@0 1961 //------------------------------executed_last---------------------------
aoqi@0 1962 // Return the node executed last in pack p.
aoqi@0 1963 Node* SuperWord::executed_last(Node_List* p) {
aoqi@0 1964 Node* n = p->at(0);
aoqi@0 1965 int n_rpo = bb_idx(n);
aoqi@0 1966 for (uint i = 1; i < p->size(); i++) {
aoqi@0 1967 Node* s = p->at(i);
aoqi@0 1968 int s_rpo = bb_idx(s);
aoqi@0 1969 if (s_rpo > n_rpo) {
aoqi@0 1970 n = s;
aoqi@0 1971 n_rpo = s_rpo;
aoqi@0 1972 }
aoqi@0 1973 }
aoqi@0 1974 return n;
aoqi@0 1975 }
aoqi@0 1976
aoqi@0 1977 //----------------------------align_initial_loop_index---------------------------
aoqi@0 1978 // Adjust pre-loop limit so that in main loop, a load/store reference
aoqi@0 1979 // to align_to_ref will be a position zero in the vector.
aoqi@0 1980 // (iv + k) mod vector_align == 0
aoqi@0 1981 void SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
aoqi@0 1982 CountedLoopNode *main_head = lp()->as_CountedLoop();
aoqi@0 1983 assert(main_head->is_main_loop(), "");
aoqi@0 1984 CountedLoopEndNode* pre_end = get_pre_loop_end(main_head);
aoqi@0 1985 assert(pre_end != NULL, "");
aoqi@0 1986 Node *pre_opaq1 = pre_end->limit();
aoqi@0 1987 assert(pre_opaq1->Opcode() == Op_Opaque1, "");
aoqi@0 1988 Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
aoqi@0 1989 Node *lim0 = pre_opaq->in(1);
aoqi@0 1990
aoqi@0 1991 // Where we put new limit calculations
aoqi@0 1992 Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
aoqi@0 1993
aoqi@0 1994 // Ensure the original loop limit is available from the
aoqi@0 1995 // pre-loop Opaque1 node.
aoqi@0 1996 Node *orig_limit = pre_opaq->original_loop_limit();
aoqi@0 1997 assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, "");
aoqi@0 1998
aoqi@0 1999 SWPointer align_to_ref_p(align_to_ref, this);
aoqi@0 2000 assert(align_to_ref_p.valid(), "sanity");
aoqi@0 2001
aoqi@0 2002 // Given:
aoqi@0 2003 // lim0 == original pre loop limit
aoqi@0 2004 // V == v_align (power of 2)
aoqi@0 2005 // invar == extra invariant piece of the address expression
aoqi@0 2006 // e == offset [ +/- invar ]
aoqi@0 2007 //
aoqi@0 2008 // When reassociating expressions involving '%' the basic rules are:
aoqi@0 2009 // (a - b) % k == 0 => a % k == b % k
aoqi@0 2010 // and:
aoqi@0 2011 // (a + b) % k == 0 => a % k == (k - b) % k
aoqi@0 2012 //
aoqi@0 2013 // For stride > 0 && scale > 0,
aoqi@0 2014 // Derive the new pre-loop limit "lim" such that the two constraints:
aoqi@0 2015 // (1) lim = lim0 + N (where N is some positive integer < V)
aoqi@0 2016 // (2) (e + lim) % V == 0
aoqi@0 2017 // are true.
aoqi@0 2018 //
aoqi@0 2019 // Substituting (1) into (2),
aoqi@0 2020 // (e + lim0 + N) % V == 0
aoqi@0 2021 // solve for N:
aoqi@0 2022 // N = (V - (e + lim0)) % V
aoqi@0 2023 // substitute back into (1), so that new limit
aoqi@0 2024 // lim = lim0 + (V - (e + lim0)) % V
aoqi@0 2025 //
aoqi@0 2026 // For stride > 0 && scale < 0
aoqi@0 2027 // Constraints:
aoqi@0 2028 // lim = lim0 + N
aoqi@0 2029 // (e - lim) % V == 0
aoqi@0 2030 // Solving for lim:
aoqi@0 2031 // (e - lim0 - N) % V == 0
aoqi@0 2032 // N = (e - lim0) % V
aoqi@0 2033 // lim = lim0 + (e - lim0) % V
aoqi@0 2034 //
aoqi@0 2035 // For stride < 0 && scale > 0
aoqi@0 2036 // Constraints:
aoqi@0 2037 // lim = lim0 - N
aoqi@0 2038 // (e + lim) % V == 0
aoqi@0 2039 // Solving for lim:
aoqi@0 2040 // (e + lim0 - N) % V == 0
aoqi@0 2041 // N = (e + lim0) % V
aoqi@0 2042 // lim = lim0 - (e + lim0) % V
aoqi@0 2043 //
aoqi@0 2044 // For stride < 0 && scale < 0
aoqi@0 2045 // Constraints:
aoqi@0 2046 // lim = lim0 - N
aoqi@0 2047 // (e - lim) % V == 0
aoqi@0 2048 // Solving for lim:
aoqi@0 2049 // (e - lim0 + N) % V == 0
aoqi@0 2050 // N = (V - (e - lim0)) % V
aoqi@0 2051 // lim = lim0 - (V - (e - lim0)) % V
aoqi@0 2052
aoqi@0 2053 int vw = vector_width_in_bytes(align_to_ref);
aoqi@0 2054 int stride = iv_stride();
aoqi@0 2055 int scale = align_to_ref_p.scale_in_bytes();
aoqi@0 2056 int elt_size = align_to_ref_p.memory_size();
aoqi@0 2057 int v_align = vw / elt_size;
aoqi@0 2058 assert(v_align > 1, "sanity");
aoqi@0 2059 int offset = align_to_ref_p.offset_in_bytes() / elt_size;
aoqi@0 2060 Node *offsn = _igvn.intcon(offset);
aoqi@0 2061
aoqi@0 2062 Node *e = offsn;
aoqi@0 2063 if (align_to_ref_p.invar() != NULL) {
aoqi@0 2064 // incorporate any extra invariant piece producing (offset +/- invar) >>> log2(elt)
aoqi@0 2065 Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
aoqi@0 2066 Node* aref = new (_phase->C) URShiftINode(align_to_ref_p.invar(), log2_elt);
aoqi@0 2067 _igvn.register_new_node_with_optimizer(aref);
aoqi@0 2068 _phase->set_ctrl(aref, pre_ctrl);
aoqi@0 2069 if (align_to_ref_p.negate_invar()) {
aoqi@0 2070 e = new (_phase->C) SubINode(e, aref);
aoqi@0 2071 } else {
aoqi@0 2072 e = new (_phase->C) AddINode(e, aref);
aoqi@0 2073 }
aoqi@0 2074 _igvn.register_new_node_with_optimizer(e);
aoqi@0 2075 _phase->set_ctrl(e, pre_ctrl);
aoqi@0 2076 }
aoqi@0 2077 if (vw > ObjectAlignmentInBytes) {
aoqi@0 2078 // incorporate base e +/- base && Mask >>> log2(elt)
aoqi@0 2079 Node* xbase = new(_phase->C) CastP2XNode(NULL, align_to_ref_p.base());
aoqi@0 2080 _igvn.register_new_node_with_optimizer(xbase);
aoqi@0 2081 #ifdef _LP64
aoqi@0 2082 xbase = new (_phase->C) ConvL2INode(xbase);
aoqi@0 2083 _igvn.register_new_node_with_optimizer(xbase);
aoqi@0 2084 #endif
aoqi@0 2085 Node* mask = _igvn.intcon(vw-1);
aoqi@0 2086 Node* masked_xbase = new (_phase->C) AndINode(xbase, mask);
aoqi@0 2087 _igvn.register_new_node_with_optimizer(masked_xbase);
aoqi@0 2088 Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
aoqi@0 2089 Node* bref = new (_phase->C) URShiftINode(masked_xbase, log2_elt);
aoqi@0 2090 _igvn.register_new_node_with_optimizer(bref);
aoqi@0 2091 _phase->set_ctrl(bref, pre_ctrl);
aoqi@0 2092 e = new (_phase->C) AddINode(e, bref);
aoqi@0 2093 _igvn.register_new_node_with_optimizer(e);
aoqi@0 2094 _phase->set_ctrl(e, pre_ctrl);
aoqi@0 2095 }
aoqi@0 2096
aoqi@0 2097 // compute e +/- lim0
aoqi@0 2098 if (scale < 0) {
aoqi@0 2099 e = new (_phase->C) SubINode(e, lim0);
aoqi@0 2100 } else {
aoqi@0 2101 e = new (_phase->C) AddINode(e, lim0);
aoqi@0 2102 }
aoqi@0 2103 _igvn.register_new_node_with_optimizer(e);
aoqi@0 2104 _phase->set_ctrl(e, pre_ctrl);
aoqi@0 2105
aoqi@0 2106 if (stride * scale > 0) {
aoqi@0 2107 // compute V - (e +/- lim0)
aoqi@0 2108 Node* va = _igvn.intcon(v_align);
aoqi@0 2109 e = new (_phase->C) SubINode(va, e);
aoqi@0 2110 _igvn.register_new_node_with_optimizer(e);
aoqi@0 2111 _phase->set_ctrl(e, pre_ctrl);
aoqi@0 2112 }
aoqi@0 2113 // compute N = (exp) % V
aoqi@0 2114 Node* va_msk = _igvn.intcon(v_align - 1);
aoqi@0 2115 Node* N = new (_phase->C) AndINode(e, va_msk);
aoqi@0 2116 _igvn.register_new_node_with_optimizer(N);
aoqi@0 2117 _phase->set_ctrl(N, pre_ctrl);
aoqi@0 2118
aoqi@0 2119 // substitute back into (1), so that new limit
aoqi@0 2120 // lim = lim0 + N
aoqi@0 2121 Node* lim;
aoqi@0 2122 if (stride < 0) {
aoqi@0 2123 lim = new (_phase->C) SubINode(lim0, N);
aoqi@0 2124 } else {
aoqi@0 2125 lim = new (_phase->C) AddINode(lim0, N);
aoqi@0 2126 }
aoqi@0 2127 _igvn.register_new_node_with_optimizer(lim);
aoqi@0 2128 _phase->set_ctrl(lim, pre_ctrl);
aoqi@0 2129 Node* constrained =
aoqi@0 2130 (stride > 0) ? (Node*) new (_phase->C) MinINode(lim, orig_limit)
aoqi@0 2131 : (Node*) new (_phase->C) MaxINode(lim, orig_limit);
aoqi@0 2132 _igvn.register_new_node_with_optimizer(constrained);
aoqi@0 2133 _phase->set_ctrl(constrained, pre_ctrl);
aoqi@0 2134 _igvn.hash_delete(pre_opaq);
aoqi@0 2135 pre_opaq->set_req(1, constrained);
aoqi@0 2136 }
aoqi@0 2137
aoqi@0 2138 //----------------------------get_pre_loop_end---------------------------
aoqi@0 2139 // Find pre loop end from main loop. Returns null if none.
aoqi@0 2140 CountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode *cl) {
aoqi@0 2141 Node *ctrl = cl->in(LoopNode::EntryControl);
aoqi@0 2142 if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return NULL;
aoqi@0 2143 Node *iffm = ctrl->in(0);
aoqi@0 2144 if (!iffm->is_If()) return NULL;
aoqi@0 2145 Node *p_f = iffm->in(0);
aoqi@0 2146 if (!p_f->is_IfFalse()) return NULL;
aoqi@0 2147 if (!p_f->in(0)->is_CountedLoopEnd()) return NULL;
aoqi@0 2148 CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
aoqi@0 2149 if (!pre_end->loopnode()->is_pre_loop()) return NULL;
aoqi@0 2150 return pre_end;
aoqi@0 2151 }
aoqi@0 2152
aoqi@0 2153
aoqi@0 2154 //------------------------------init---------------------------
aoqi@0 2155 void SuperWord::init() {
aoqi@0 2156 _dg.init();
aoqi@0 2157 _packset.clear();
aoqi@0 2158 _disjoint_ptrs.clear();
aoqi@0 2159 _block.clear();
aoqi@0 2160 _data_entry.clear();
aoqi@0 2161 _mem_slice_head.clear();
aoqi@0 2162 _mem_slice_tail.clear();
aoqi@0 2163 _node_info.clear();
aoqi@0 2164 _align_to_ref = NULL;
aoqi@0 2165 _lpt = NULL;
aoqi@0 2166 _lp = NULL;
aoqi@0 2167 _bb = NULL;
aoqi@0 2168 _iv = NULL;
aoqi@0 2169 }
aoqi@0 2170
aoqi@0 2171 //------------------------------print_packset---------------------------
aoqi@0 2172 void SuperWord::print_packset() {
aoqi@0 2173 #ifndef PRODUCT
aoqi@0 2174 tty->print_cr("packset");
aoqi@0 2175 for (int i = 0; i < _packset.length(); i++) {
aoqi@0 2176 tty->print_cr("Pack: %d", i);
aoqi@0 2177 Node_List* p = _packset.at(i);
aoqi@0 2178 print_pack(p);
aoqi@0 2179 }
aoqi@0 2180 #endif
aoqi@0 2181 }
aoqi@0 2182
aoqi@0 2183 //------------------------------print_pack---------------------------
aoqi@0 2184 void SuperWord::print_pack(Node_List* p) {
aoqi@0 2185 for (uint i = 0; i < p->size(); i++) {
aoqi@0 2186 print_stmt(p->at(i));
aoqi@0 2187 }
aoqi@0 2188 }
aoqi@0 2189
aoqi@0 2190 //------------------------------print_bb---------------------------
aoqi@0 2191 void SuperWord::print_bb() {
aoqi@0 2192 #ifndef PRODUCT
aoqi@0 2193 tty->print_cr("\nBlock");
aoqi@0 2194 for (int i = 0; i < _block.length(); i++) {
aoqi@0 2195 Node* n = _block.at(i);
aoqi@0 2196 tty->print("%d ", i);
aoqi@0 2197 if (n) {
aoqi@0 2198 n->dump();
aoqi@0 2199 }
aoqi@0 2200 }
aoqi@0 2201 #endif
aoqi@0 2202 }
aoqi@0 2203
aoqi@0 2204 //------------------------------print_stmt---------------------------
aoqi@0 2205 void SuperWord::print_stmt(Node* s) {
aoqi@0 2206 #ifndef PRODUCT
aoqi@0 2207 tty->print(" align: %d \t", alignment(s));
aoqi@0 2208 s->dump();
aoqi@0 2209 #endif
aoqi@0 2210 }
aoqi@0 2211
aoqi@0 2212 //------------------------------blank---------------------------
aoqi@0 2213 char* SuperWord::blank(uint depth) {
aoqi@0 2214 static char blanks[101];
aoqi@0 2215 assert(depth < 101, "too deep");
aoqi@0 2216 for (uint i = 0; i < depth; i++) blanks[i] = ' ';
aoqi@0 2217 blanks[depth] = '\0';
aoqi@0 2218 return blanks;
aoqi@0 2219 }
aoqi@0 2220
aoqi@0 2221
aoqi@0 2222 //==============================SWPointer===========================
aoqi@0 2223
aoqi@0 2224 //----------------------------SWPointer------------------------
aoqi@0 2225 SWPointer::SWPointer(MemNode* mem, SuperWord* slp) :
aoqi@0 2226 _mem(mem), _slp(slp), _base(NULL), _adr(NULL),
aoqi@0 2227 _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {
aoqi@0 2228
aoqi@0 2229 Node* adr = mem->in(MemNode::Address);
aoqi@0 2230 if (!adr->is_AddP()) {
aoqi@0 2231 assert(!valid(), "too complex");
aoqi@0 2232 return;
aoqi@0 2233 }
aoqi@0 2234 // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant)
aoqi@0 2235 Node* base = adr->in(AddPNode::Base);
aoqi@0 2236 //unsafe reference could not be aligned appropriately without runtime checking
aoqi@0 2237 if (base == NULL || base->bottom_type() == Type::TOP) {
aoqi@0 2238 assert(!valid(), "unsafe access");
aoqi@0 2239 return;
aoqi@0 2240 }
aoqi@0 2241 for (int i = 0; i < 3; i++) {
aoqi@0 2242 if (!scaled_iv_plus_offset(adr->in(AddPNode::Offset))) {
aoqi@0 2243 assert(!valid(), "too complex");
aoqi@0 2244 return;
aoqi@0 2245 }
aoqi@0 2246 adr = adr->in(AddPNode::Address);
aoqi@0 2247 if (base == adr || !adr->is_AddP()) {
aoqi@0 2248 break; // stop looking at addp's
aoqi@0 2249 }
aoqi@0 2250 }
aoqi@0 2251 _base = base;
aoqi@0 2252 _adr = adr;
aoqi@0 2253 assert(valid(), "Usable");
aoqi@0 2254 }
aoqi@0 2255
aoqi@0 2256 // Following is used to create a temporary object during
aoqi@0 2257 // the pattern match of an address expression.
aoqi@0 2258 SWPointer::SWPointer(SWPointer* p) :
aoqi@0 2259 _mem(p->_mem), _slp(p->_slp), _base(NULL), _adr(NULL),
aoqi@0 2260 _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {}
aoqi@0 2261
aoqi@0 2262 //------------------------scaled_iv_plus_offset--------------------
aoqi@0 2263 // Match: k*iv + offset
aoqi@0 2264 // where: k is a constant that maybe zero, and
aoqi@0 2265 // offset is (k2 [+/- invariant]) where k2 maybe zero and invariant is optional
aoqi@0 2266 bool SWPointer::scaled_iv_plus_offset(Node* n) {
aoqi@0 2267 if (scaled_iv(n)) {
aoqi@0 2268 return true;
aoqi@0 2269 }
aoqi@0 2270 if (offset_plus_k(n)) {
aoqi@0 2271 return true;
aoqi@0 2272 }
aoqi@0 2273 int opc = n->Opcode();
aoqi@0 2274 if (opc == Op_AddI) {
aoqi@0 2275 if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2))) {
aoqi@0 2276 return true;
aoqi@0 2277 }
aoqi@0 2278 if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
aoqi@0 2279 return true;
aoqi@0 2280 }
aoqi@0 2281 } else if (opc == Op_SubI) {
aoqi@0 2282 if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2), true)) {
aoqi@0 2283 return true;
aoqi@0 2284 }
aoqi@0 2285 if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
aoqi@0 2286 _scale *= -1;
aoqi@0 2287 return true;
aoqi@0 2288 }
aoqi@0 2289 }
aoqi@0 2290 return false;
aoqi@0 2291 }
aoqi@0 2292
aoqi@0 2293 //----------------------------scaled_iv------------------------
aoqi@0 2294 // Match: k*iv where k is a constant that's not zero
aoqi@0 2295 bool SWPointer::scaled_iv(Node* n) {
aoqi@0 2296 if (_scale != 0) {
aoqi@0 2297 return false; // already found a scale
aoqi@0 2298 }
aoqi@0 2299 if (n == iv()) {
aoqi@0 2300 _scale = 1;
aoqi@0 2301 return true;
aoqi@0 2302 }
aoqi@0 2303 int opc = n->Opcode();
aoqi@0 2304 if (opc == Op_MulI) {
aoqi@0 2305 if (n->in(1) == iv() && n->in(2)->is_Con()) {
aoqi@0 2306 _scale = n->in(2)->get_int();
aoqi@0 2307 return true;
aoqi@0 2308 } else if (n->in(2) == iv() && n->in(1)->is_Con()) {
aoqi@0 2309 _scale = n->in(1)->get_int();
aoqi@0 2310 return true;
aoqi@0 2311 }
aoqi@0 2312 } else if (opc == Op_LShiftI) {
aoqi@0 2313 if (n->in(1) == iv() && n->in(2)->is_Con()) {
aoqi@0 2314 _scale = 1 << n->in(2)->get_int();
aoqi@0 2315 return true;
aoqi@0 2316 }
aoqi@0 2317 } else if (opc == Op_ConvI2L) {
aoqi@0 2318 if (scaled_iv_plus_offset(n->in(1))) {
aoqi@0 2319 return true;
aoqi@0 2320 }
aoqi@0 2321 } else if (opc == Op_LShiftL) {
aoqi@0 2322 if (!has_iv() && _invar == NULL) {
aoqi@0 2323 // Need to preserve the current _offset value, so
aoqi@0 2324 // create a temporary object for this expression subtree.
aoqi@0 2325 // Hacky, so should re-engineer the address pattern match.
aoqi@0 2326 SWPointer tmp(this);
aoqi@0 2327 if (tmp.scaled_iv_plus_offset(n->in(1))) {
aoqi@0 2328 if (tmp._invar == NULL) {
aoqi@0 2329 int mult = 1 << n->in(2)->get_int();
aoqi@0 2330 _scale = tmp._scale * mult;
aoqi@0 2331 _offset += tmp._offset * mult;
aoqi@0 2332 return true;
aoqi@0 2333 }
aoqi@0 2334 }
aoqi@0 2335 }
aoqi@0 2336 }
aoqi@0 2337 return false;
aoqi@0 2338 }
aoqi@0 2339
aoqi@0 2340 //----------------------------offset_plus_k------------------------
aoqi@0 2341 // Match: offset is (k [+/- invariant])
aoqi@0 2342 // where k maybe zero and invariant is optional, but not both.
aoqi@0 2343 bool SWPointer::offset_plus_k(Node* n, bool negate) {
aoqi@0 2344 int opc = n->Opcode();
aoqi@0 2345 if (opc == Op_ConI) {
aoqi@0 2346 _offset += negate ? -(n->get_int()) : n->get_int();
aoqi@0 2347 return true;
aoqi@0 2348 } else if (opc == Op_ConL) {
aoqi@0 2349 // Okay if value fits into an int
aoqi@0 2350 const TypeLong* t = n->find_long_type();
aoqi@0 2351 if (t->higher_equal(TypeLong::INT)) {
aoqi@0 2352 jlong loff = n->get_long();
aoqi@0 2353 jint off = (jint)loff;
aoqi@0 2354 _offset += negate ? -off : loff;
aoqi@0 2355 return true;
aoqi@0 2356 }
aoqi@0 2357 return false;
aoqi@0 2358 }
aoqi@0 2359 if (_invar != NULL) return false; // already have an invariant
aoqi@0 2360 if (opc == Op_AddI) {
aoqi@0 2361 if (n->in(2)->is_Con() && invariant(n->in(1))) {
aoqi@0 2362 _negate_invar = negate;
aoqi@0 2363 _invar = n->in(1);
aoqi@0 2364 _offset += negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
aoqi@0 2365 return true;
aoqi@0 2366 } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
aoqi@0 2367 _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
aoqi@0 2368 _negate_invar = negate;
aoqi@0 2369 _invar = n->in(2);
aoqi@0 2370 return true;
aoqi@0 2371 }
aoqi@0 2372 }
aoqi@0 2373 if (opc == Op_SubI) {
aoqi@0 2374 if (n->in(2)->is_Con() && invariant(n->in(1))) {
aoqi@0 2375 _negate_invar = negate;
aoqi@0 2376 _invar = n->in(1);
aoqi@0 2377 _offset += !negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
aoqi@0 2378 return true;
aoqi@0 2379 } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
aoqi@0 2380 _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
aoqi@0 2381 _negate_invar = !negate;
aoqi@0 2382 _invar = n->in(2);
aoqi@0 2383 return true;
aoqi@0 2384 }
aoqi@0 2385 }
aoqi@0 2386 if (invariant(n)) {
aoqi@0 2387 _negate_invar = negate;
aoqi@0 2388 _invar = n;
aoqi@0 2389 return true;
aoqi@0 2390 }
aoqi@0 2391 return false;
aoqi@0 2392 }
aoqi@0 2393
aoqi@0 2394 //----------------------------print------------------------
aoqi@0 2395 void SWPointer::print() {
aoqi@0 2396 #ifndef PRODUCT
aoqi@0 2397 tty->print("base: %d adr: %d scale: %d offset: %d invar: %c%d\n",
aoqi@0 2398 _base != NULL ? _base->_idx : 0,
aoqi@0 2399 _adr != NULL ? _adr->_idx : 0,
aoqi@0 2400 _scale, _offset,
aoqi@0 2401 _negate_invar?'-':'+',
aoqi@0 2402 _invar != NULL ? _invar->_idx : 0);
aoqi@0 2403 #endif
aoqi@0 2404 }
aoqi@0 2405
aoqi@0 2406 // ========================= OrderedPair =====================
aoqi@0 2407
aoqi@0 2408 const OrderedPair OrderedPair::initial;
aoqi@0 2409
aoqi@0 2410 // ========================= SWNodeInfo =====================
aoqi@0 2411
aoqi@0 2412 const SWNodeInfo SWNodeInfo::initial;
aoqi@0 2413
aoqi@0 2414
aoqi@0 2415 // ============================ DepGraph ===========================
aoqi@0 2416
aoqi@0 2417 //------------------------------make_node---------------------------
aoqi@0 2418 // Make a new dependence graph node for an ideal node.
aoqi@0 2419 DepMem* DepGraph::make_node(Node* node) {
aoqi@0 2420 DepMem* m = new (_arena) DepMem(node);
aoqi@0 2421 if (node != NULL) {
aoqi@0 2422 assert(_map.at_grow(node->_idx) == NULL, "one init only");
aoqi@0 2423 _map.at_put_grow(node->_idx, m);
aoqi@0 2424 }
aoqi@0 2425 return m;
aoqi@0 2426 }
aoqi@0 2427
aoqi@0 2428 //------------------------------make_edge---------------------------
aoqi@0 2429 // Make a new dependence graph edge from dpred -> dsucc
aoqi@0 2430 DepEdge* DepGraph::make_edge(DepMem* dpred, DepMem* dsucc) {
aoqi@0 2431 DepEdge* e = new (_arena) DepEdge(dpred, dsucc, dsucc->in_head(), dpred->out_head());
aoqi@0 2432 dpred->set_out_head(e);
aoqi@0 2433 dsucc->set_in_head(e);
aoqi@0 2434 return e;
aoqi@0 2435 }
aoqi@0 2436
aoqi@0 2437 // ========================== DepMem ========================
aoqi@0 2438
aoqi@0 2439 //------------------------------in_cnt---------------------------
aoqi@0 2440 int DepMem::in_cnt() {
aoqi@0 2441 int ct = 0;
aoqi@0 2442 for (DepEdge* e = _in_head; e != NULL; e = e->next_in()) ct++;
aoqi@0 2443 return ct;
aoqi@0 2444 }
aoqi@0 2445
aoqi@0 2446 //------------------------------out_cnt---------------------------
aoqi@0 2447 int DepMem::out_cnt() {
aoqi@0 2448 int ct = 0;
aoqi@0 2449 for (DepEdge* e = _out_head; e != NULL; e = e->next_out()) ct++;
aoqi@0 2450 return ct;
aoqi@0 2451 }
aoqi@0 2452
aoqi@0 2453 //------------------------------print-----------------------------
aoqi@0 2454 void DepMem::print() {
aoqi@0 2455 #ifndef PRODUCT
aoqi@0 2456 tty->print(" DepNode %d (", _node->_idx);
aoqi@0 2457 for (DepEdge* p = _in_head; p != NULL; p = p->next_in()) {
aoqi@0 2458 Node* pred = p->pred()->node();
aoqi@0 2459 tty->print(" %d", pred != NULL ? pred->_idx : 0);
aoqi@0 2460 }
aoqi@0 2461 tty->print(") [");
aoqi@0 2462 for (DepEdge* s = _out_head; s != NULL; s = s->next_out()) {
aoqi@0 2463 Node* succ = s->succ()->node();
aoqi@0 2464 tty->print(" %d", succ != NULL ? succ->_idx : 0);
aoqi@0 2465 }
aoqi@0 2466 tty->print_cr(" ]");
aoqi@0 2467 #endif
aoqi@0 2468 }
aoqi@0 2469
aoqi@0 2470 // =========================== DepEdge =========================
aoqi@0 2471
aoqi@0 2472 //------------------------------DepPreds---------------------------
aoqi@0 2473 void DepEdge::print() {
aoqi@0 2474 #ifndef PRODUCT
aoqi@0 2475 tty->print_cr("DepEdge: %d [ %d ]", _pred->node()->_idx, _succ->node()->_idx);
aoqi@0 2476 #endif
aoqi@0 2477 }
aoqi@0 2478
aoqi@0 2479 // =========================== DepPreds =========================
aoqi@0 2480 // Iterator over predecessor edges in the dependence graph.
aoqi@0 2481
aoqi@0 2482 //------------------------------DepPreds---------------------------
aoqi@0 2483 DepPreds::DepPreds(Node* n, DepGraph& dg) {
aoqi@0 2484 _n = n;
aoqi@0 2485 _done = false;
aoqi@0 2486 if (_n->is_Store() || _n->is_Load()) {
aoqi@0 2487 _next_idx = MemNode::Address;
aoqi@0 2488 _end_idx = n->req();
aoqi@0 2489 _dep_next = dg.dep(_n)->in_head();
aoqi@0 2490 } else if (_n->is_Mem()) {
aoqi@0 2491 _next_idx = 0;
aoqi@0 2492 _end_idx = 0;
aoqi@0 2493 _dep_next = dg.dep(_n)->in_head();
aoqi@0 2494 } else {
aoqi@0 2495 _next_idx = 1;
aoqi@0 2496 _end_idx = _n->req();
aoqi@0 2497 _dep_next = NULL;
aoqi@0 2498 }
aoqi@0 2499 next();
aoqi@0 2500 }
aoqi@0 2501
aoqi@0 2502 //------------------------------next---------------------------
aoqi@0 2503 void DepPreds::next() {
aoqi@0 2504 if (_dep_next != NULL) {
aoqi@0 2505 _current = _dep_next->pred()->node();
aoqi@0 2506 _dep_next = _dep_next->next_in();
aoqi@0 2507 } else if (_next_idx < _end_idx) {
aoqi@0 2508 _current = _n->in(_next_idx++);
aoqi@0 2509 } else {
aoqi@0 2510 _done = true;
aoqi@0 2511 }
aoqi@0 2512 }
aoqi@0 2513
aoqi@0 2514 // =========================== DepSuccs =========================
aoqi@0 2515 // Iterator over successor edges in the dependence graph.
aoqi@0 2516
aoqi@0 2517 //------------------------------DepSuccs---------------------------
aoqi@0 2518 DepSuccs::DepSuccs(Node* n, DepGraph& dg) {
aoqi@0 2519 _n = n;
aoqi@0 2520 _done = false;
aoqi@0 2521 if (_n->is_Load()) {
aoqi@0 2522 _next_idx = 0;
aoqi@0 2523 _end_idx = _n->outcnt();
aoqi@0 2524 _dep_next = dg.dep(_n)->out_head();
aoqi@0 2525 } else if (_n->is_Mem() || _n->is_Phi() && _n->bottom_type() == Type::MEMORY) {
aoqi@0 2526 _next_idx = 0;
aoqi@0 2527 _end_idx = 0;
aoqi@0 2528 _dep_next = dg.dep(_n)->out_head();
aoqi@0 2529 } else {
aoqi@0 2530 _next_idx = 0;
aoqi@0 2531 _end_idx = _n->outcnt();
aoqi@0 2532 _dep_next = NULL;
aoqi@0 2533 }
aoqi@0 2534 next();
aoqi@0 2535 }
aoqi@0 2536
aoqi@0 2537 //-------------------------------next---------------------------
aoqi@0 2538 void DepSuccs::next() {
aoqi@0 2539 if (_dep_next != NULL) {
aoqi@0 2540 _current = _dep_next->succ()->node();
aoqi@0 2541 _dep_next = _dep_next->next_out();
aoqi@0 2542 } else if (_next_idx < _end_idx) {
aoqi@0 2543 _current = _n->raw_out(_next_idx++);
aoqi@0 2544 } else {
aoqi@0 2545 _done = true;
aoqi@0 2546 }
aoqi@0 2547 }

mercurial