Tue, 09 Mar 2010 20:16:19 +0100
6919934: JSR 292 needs to support x86 C1
Summary: This implements JSR 292 support for C1 x86.
Reviewed-by: never, jrose, kvn
1 /*
2 * Copyright 2007-2009 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
24 #include "incls/_precompiled.incl"
25 #include "incls/_superword.cpp.incl"
27 //
28 // S U P E R W O R D T R A N S F O R M
29 //=============================================================================
31 //------------------------------SuperWord---------------------------
32 SuperWord::SuperWord(PhaseIdealLoop* phase) :
33 _phase(phase),
34 _igvn(phase->_igvn),
35 _arena(phase->C->comp_arena()),
36 _packset(arena(), 8, 0, NULL), // packs for the current block
37 _bb_idx(arena(), (int)(1.10 * phase->C->unique()), 0, 0), // node idx to index in bb
38 _block(arena(), 8, 0, NULL), // nodes in current block
39 _data_entry(arena(), 8, 0, NULL), // nodes with all inputs from outside
40 _mem_slice_head(arena(), 8, 0, NULL), // memory slice heads
41 _mem_slice_tail(arena(), 8, 0, NULL), // memory slice tails
42 _node_info(arena(), 8, 0, SWNodeInfo::initial), // info needed per node
43 _align_to_ref(NULL), // memory reference to align vectors to
44 _disjoint_ptrs(arena(), 8, 0, OrderedPair::initial), // runtime disambiguated pointer pairs
45 _dg(_arena), // dependence graph
46 _visited(arena()), // visited node set
47 _post_visited(arena()), // post visited node set
48 _n_idx_list(arena(), 8), // scratch list of (node,index) pairs
49 _stk(arena(), 8, 0, NULL), // scratch stack of nodes
50 _nlist(arena(), 8, 0, NULL), // scratch list of nodes
51 _lpt(NULL), // loop tree node
52 _lp(NULL), // LoopNode
53 _bb(NULL), // basic block
54 _iv(NULL) // induction var
55 {}
57 //------------------------------transform_loop---------------------------
58 void SuperWord::transform_loop(IdealLoopTree* lpt) {
59 assert(lpt->_head->is_CountedLoop(), "must be");
60 CountedLoopNode *cl = lpt->_head->as_CountedLoop();
62 if (!cl->is_main_loop() ) return; // skip normal, pre, and post loops
64 // Check for no control flow in body (other than exit)
65 Node *cl_exit = cl->loopexit();
66 if (cl_exit->in(0) != lpt->_head) return;
68 // Make sure the are no extra control users of the loop backedge
69 if (cl->back_control()->outcnt() != 1) {
70 return;
71 }
73 // Check for pre-loop ending with CountedLoopEnd(Bool(Cmp(x,Opaque1(limit))))
74 CountedLoopEndNode* pre_end = get_pre_loop_end(cl);
75 if (pre_end == NULL) return;
76 Node *pre_opaq1 = pre_end->limit();
77 if (pre_opaq1->Opcode() != Op_Opaque1) return;
79 // Do vectors exist on this architecture?
80 if (vector_width_in_bytes() == 0) return;
82 init(); // initialize data structures
84 set_lpt(lpt);
85 set_lp(cl);
87 // For now, define one block which is the entire loop body
88 set_bb(cl);
90 assert(_packset.length() == 0, "packset must be empty");
91 SLP_extract();
92 }
94 //------------------------------SLP_extract---------------------------
95 // Extract the superword level parallelism
96 //
97 // 1) A reverse post-order of nodes in the block is constructed. By scanning
98 // this list from first to last, all definitions are visited before their uses.
99 //
100 // 2) A point-to-point dependence graph is constructed between memory references.
101 // This simplies the upcoming "independence" checker.
102 //
103 // 3) The maximum depth in the node graph from the beginning of the block
104 // to each node is computed. This is used to prune the graph search
105 // in the independence checker.
106 //
107 // 4) For integer types, the necessary bit width is propagated backwards
108 // from stores to allow packed operations on byte, char, and short
109 // integers. This reverses the promotion to type "int" that javac
110 // did for operations like: char c1,c2,c3; c1 = c2 + c3.
111 //
112 // 5) One of the memory references is picked to be an aligned vector reference.
113 // The pre-loop trip count is adjusted to align this reference in the
114 // unrolled body.
115 //
116 // 6) The initial set of pack pairs is seeded with memory references.
117 //
118 // 7) The set of pack pairs is extended by following use->def and def->use links.
119 //
120 // 8) The pairs are combined into vector sized packs.
121 //
122 // 9) Reorder the memory slices to co-locate members of the memory packs.
123 //
124 // 10) Generate ideal vector nodes for the final set of packs and where necessary,
125 // inserting scalar promotion, vector creation from multiple scalars, and
126 // extraction of scalar values from vectors.
127 //
128 void SuperWord::SLP_extract() {
130 // Ready the block
132 construct_bb();
134 dependence_graph();
136 compute_max_depth();
138 compute_vector_element_type();
140 // Attempt vectorization
142 find_adjacent_refs();
144 extend_packlist();
146 combine_packs();
148 construct_my_pack_map();
150 filter_packs();
152 schedule();
154 output();
155 }
157 //------------------------------find_adjacent_refs---------------------------
158 // Find the adjacent memory references and create pack pairs for them.
159 // This is the initial set of packs that will then be extended by
160 // following use->def and def->use links. The align positions are
161 // assigned relative to the reference "align_to_ref"
162 void SuperWord::find_adjacent_refs() {
163 // Get list of memory operations
164 Node_List memops;
165 for (int i = 0; i < _block.length(); i++) {
166 Node* n = _block.at(i);
167 if (n->is_Mem() && in_bb(n) &&
168 is_java_primitive(n->as_Mem()->memory_type())) {
169 int align = memory_alignment(n->as_Mem(), 0);
170 if (align != bottom_align) {
171 memops.push(n);
172 }
173 }
174 }
175 if (memops.size() == 0) return;
177 // Find a memory reference to align to. The pre-loop trip count
178 // is modified to align this reference to a vector-aligned address
179 find_align_to_ref(memops);
180 if (align_to_ref() == NULL) return;
182 SWPointer align_to_ref_p(align_to_ref(), this);
183 int offset = align_to_ref_p.offset_in_bytes();
184 int scale = align_to_ref_p.scale_in_bytes();
185 int vw = vector_width_in_bytes();
186 int stride_sign = (scale * iv_stride()) > 0 ? 1 : -1;
187 int iv_adjustment = (stride_sign * vw - (offset % vw)) % vw;
189 #ifndef PRODUCT
190 if (TraceSuperWord)
191 tty->print_cr("\noffset = %d iv_adjustment = %d elt_align = %d scale = %d iv_stride = %d",
192 offset, iv_adjustment, align_to_ref_p.memory_size(), align_to_ref_p.scale_in_bytes(), iv_stride());
193 #endif
195 // Set alignment relative to "align_to_ref"
196 for (int i = memops.size() - 1; i >= 0; i--) {
197 MemNode* s = memops.at(i)->as_Mem();
198 SWPointer p2(s, this);
199 if (p2.comparable(align_to_ref_p)) {
200 int align = memory_alignment(s, iv_adjustment);
201 set_alignment(s, align);
202 } else {
203 memops.remove(i);
204 }
205 }
207 // Create initial pack pairs of memory operations
208 for (uint i = 0; i < memops.size(); i++) {
209 Node* s1 = memops.at(i);
210 for (uint j = 0; j < memops.size(); j++) {
211 Node* s2 = memops.at(j);
212 if (s1 != s2 && are_adjacent_refs(s1, s2)) {
213 int align = alignment(s1);
214 if (stmts_can_pack(s1, s2, align)) {
215 Node_List* pair = new Node_List();
216 pair->push(s1);
217 pair->push(s2);
218 _packset.append(pair);
219 }
220 }
221 }
222 }
224 #ifndef PRODUCT
225 if (TraceSuperWord) {
226 tty->print_cr("\nAfter find_adjacent_refs");
227 print_packset();
228 }
229 #endif
230 }
232 //------------------------------find_align_to_ref---------------------------
233 // Find a memory reference to align the loop induction variable to.
234 // Looks first at stores then at loads, looking for a memory reference
235 // with the largest number of references similar to it.
236 void SuperWord::find_align_to_ref(Node_List &memops) {
237 GrowableArray<int> cmp_ct(arena(), memops.size(), memops.size(), 0);
239 // Count number of comparable memory ops
240 for (uint i = 0; i < memops.size(); i++) {
241 MemNode* s1 = memops.at(i)->as_Mem();
242 SWPointer p1(s1, this);
243 // Discard if pre loop can't align this reference
244 if (!ref_is_alignable(p1)) {
245 *cmp_ct.adr_at(i) = 0;
246 continue;
247 }
248 for (uint j = i+1; j < memops.size(); j++) {
249 MemNode* s2 = memops.at(j)->as_Mem();
250 if (isomorphic(s1, s2)) {
251 SWPointer p2(s2, this);
252 if (p1.comparable(p2)) {
253 (*cmp_ct.adr_at(i))++;
254 (*cmp_ct.adr_at(j))++;
255 }
256 }
257 }
258 }
260 // Find Store (or Load) with the greatest number of "comparable" references
261 int max_ct = 0;
262 int max_idx = -1;
263 int min_size = max_jint;
264 int min_iv_offset = max_jint;
265 for (uint j = 0; j < memops.size(); j++) {
266 MemNode* s = memops.at(j)->as_Mem();
267 if (s->is_Store()) {
268 SWPointer p(s, this);
269 if (cmp_ct.at(j) > max_ct ||
270 cmp_ct.at(j) == max_ct && (data_size(s) < min_size ||
271 data_size(s) == min_size &&
272 p.offset_in_bytes() < min_iv_offset)) {
273 max_ct = cmp_ct.at(j);
274 max_idx = j;
275 min_size = data_size(s);
276 min_iv_offset = p.offset_in_bytes();
277 }
278 }
279 }
280 // If no stores, look at loads
281 if (max_ct == 0) {
282 for (uint j = 0; j < memops.size(); j++) {
283 MemNode* s = memops.at(j)->as_Mem();
284 if (s->is_Load()) {
285 SWPointer p(s, this);
286 if (cmp_ct.at(j) > max_ct ||
287 cmp_ct.at(j) == max_ct && (data_size(s) < min_size ||
288 data_size(s) == min_size &&
289 p.offset_in_bytes() < min_iv_offset)) {
290 max_ct = cmp_ct.at(j);
291 max_idx = j;
292 min_size = data_size(s);
293 min_iv_offset = p.offset_in_bytes();
294 }
295 }
296 }
297 }
299 if (max_ct > 0)
300 set_align_to_ref(memops.at(max_idx)->as_Mem());
302 #ifndef PRODUCT
303 if (TraceSuperWord && Verbose) {
304 tty->print_cr("\nVector memops after find_align_to_refs");
305 for (uint i = 0; i < memops.size(); i++) {
306 MemNode* s = memops.at(i)->as_Mem();
307 s->dump();
308 }
309 }
310 #endif
311 }
313 //------------------------------ref_is_alignable---------------------------
314 // Can the preloop align the reference to position zero in the vector?
315 bool SuperWord::ref_is_alignable(SWPointer& p) {
316 if (!p.has_iv()) {
317 return true; // no induction variable
318 }
319 CountedLoopEndNode* pre_end = get_pre_loop_end(lp()->as_CountedLoop());
320 assert(pre_end->stride_is_con(), "pre loop stride is constant");
321 int preloop_stride = pre_end->stride_con();
323 int span = preloop_stride * p.scale_in_bytes();
325 // Stride one accesses are alignable.
326 if (ABS(span) == p.memory_size())
327 return true;
329 // If initial offset from start of object is computable,
330 // compute alignment within the vector.
331 int vw = vector_width_in_bytes();
332 if (vw % span == 0) {
333 Node* init_nd = pre_end->init_trip();
334 if (init_nd->is_Con() && p.invar() == NULL) {
335 int init = init_nd->bottom_type()->is_int()->get_con();
337 int init_offset = init * p.scale_in_bytes() + p.offset_in_bytes();
338 assert(init_offset >= 0, "positive offset from object start");
340 if (span > 0) {
341 return (vw - (init_offset % vw)) % span == 0;
342 } else {
343 assert(span < 0, "nonzero stride * scale");
344 return (init_offset % vw) % -span == 0;
345 }
346 }
347 }
348 return false;
349 }
351 //---------------------------dependence_graph---------------------------
352 // Construct dependency graph.
353 // Add dependence edges to load/store nodes for memory dependence
354 // A.out()->DependNode.in(1) and DependNode.out()->B.prec(x)
355 void SuperWord::dependence_graph() {
356 // First, assign a dependence node to each memory node
357 for (int i = 0; i < _block.length(); i++ ) {
358 Node *n = _block.at(i);
359 if (n->is_Mem() || n->is_Phi() && n->bottom_type() == Type::MEMORY) {
360 _dg.make_node(n);
361 }
362 }
364 // For each memory slice, create the dependences
365 for (int i = 0; i < _mem_slice_head.length(); i++) {
366 Node* n = _mem_slice_head.at(i);
367 Node* n_tail = _mem_slice_tail.at(i);
369 // Get slice in predecessor order (last is first)
370 mem_slice_preds(n_tail, n, _nlist);
372 // Make the slice dependent on the root
373 DepMem* slice = _dg.dep(n);
374 _dg.make_edge(_dg.root(), slice);
376 // Create a sink for the slice
377 DepMem* slice_sink = _dg.make_node(NULL);
378 _dg.make_edge(slice_sink, _dg.tail());
380 // Now visit each pair of memory ops, creating the edges
381 for (int j = _nlist.length() - 1; j >= 0 ; j--) {
382 Node* s1 = _nlist.at(j);
384 // If no dependency yet, use slice
385 if (_dg.dep(s1)->in_cnt() == 0) {
386 _dg.make_edge(slice, s1);
387 }
388 SWPointer p1(s1->as_Mem(), this);
389 bool sink_dependent = true;
390 for (int k = j - 1; k >= 0; k--) {
391 Node* s2 = _nlist.at(k);
392 if (s1->is_Load() && s2->is_Load())
393 continue;
394 SWPointer p2(s2->as_Mem(), this);
396 int cmp = p1.cmp(p2);
397 if (SuperWordRTDepCheck &&
398 p1.base() != p2.base() && p1.valid() && p2.valid()) {
399 // Create a runtime check to disambiguate
400 OrderedPair pp(p1.base(), p2.base());
401 _disjoint_ptrs.append_if_missing(pp);
402 } else if (!SWPointer::not_equal(cmp)) {
403 // Possibly same address
404 _dg.make_edge(s1, s2);
405 sink_dependent = false;
406 }
407 }
408 if (sink_dependent) {
409 _dg.make_edge(s1, slice_sink);
410 }
411 }
412 #ifndef PRODUCT
413 if (TraceSuperWord) {
414 tty->print_cr("\nDependence graph for slice: %d", n->_idx);
415 for (int q = 0; q < _nlist.length(); q++) {
416 _dg.print(_nlist.at(q));
417 }
418 tty->cr();
419 }
420 #endif
421 _nlist.clear();
422 }
424 #ifndef PRODUCT
425 if (TraceSuperWord) {
426 tty->print_cr("\ndisjoint_ptrs: %s", _disjoint_ptrs.length() > 0 ? "" : "NONE");
427 for (int r = 0; r < _disjoint_ptrs.length(); r++) {
428 _disjoint_ptrs.at(r).print();
429 tty->cr();
430 }
431 tty->cr();
432 }
433 #endif
434 }
436 //---------------------------mem_slice_preds---------------------------
437 // Return a memory slice (node list) in predecessor order starting at "start"
438 void SuperWord::mem_slice_preds(Node* start, Node* stop, GrowableArray<Node*> &preds) {
439 assert(preds.length() == 0, "start empty");
440 Node* n = start;
441 Node* prev = NULL;
442 while (true) {
443 assert(in_bb(n), "must be in block");
444 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
445 Node* out = n->fast_out(i);
446 if (out->is_Load()) {
447 if (in_bb(out)) {
448 preds.push(out);
449 }
450 } else {
451 // FIXME
452 if (out->is_MergeMem() && !in_bb(out)) {
453 // Either unrolling is causing a memory edge not to disappear,
454 // or need to run igvn.optimize() again before SLP
455 } else if (out->is_Phi() && out->bottom_type() == Type::MEMORY && !in_bb(out)) {
456 // Ditto. Not sure what else to check further.
457 } else if (out->Opcode() == Op_StoreCM && out->in(MemNode::OopStore) == n) {
458 // StoreCM has an input edge used as a precedence edge.
459 // Maybe an issue when oop stores are vectorized.
460 } else {
461 assert(out == prev || prev == NULL, "no branches off of store slice");
462 }
463 }
464 }
465 if (n == stop) break;
466 preds.push(n);
467 prev = n;
468 n = n->in(MemNode::Memory);
469 }
470 }
472 //------------------------------stmts_can_pack---------------------------
473 // Can s1 and s2 be in a pack with s1 immediately preceding s2 and
474 // s1 aligned at "align"
475 bool SuperWord::stmts_can_pack(Node* s1, Node* s2, int align) {
477 // Do not use superword for non-primitives
478 if((s1->is_Mem() && !is_java_primitive(s1->as_Mem()->memory_type())) ||
479 (s2->is_Mem() && !is_java_primitive(s2->as_Mem()->memory_type())))
480 return false;
482 if (isomorphic(s1, s2)) {
483 if (independent(s1, s2)) {
484 if (!exists_at(s1, 0) && !exists_at(s2, 1)) {
485 if (!s1->is_Mem() || are_adjacent_refs(s1, s2)) {
486 int s1_align = alignment(s1);
487 int s2_align = alignment(s2);
488 if (s1_align == top_align || s1_align == align) {
489 if (s2_align == top_align || s2_align == align + data_size(s1)) {
490 return true;
491 }
492 }
493 }
494 }
495 }
496 }
497 return false;
498 }
500 //------------------------------exists_at---------------------------
501 // Does s exist in a pack at position pos?
502 bool SuperWord::exists_at(Node* s, uint pos) {
503 for (int i = 0; i < _packset.length(); i++) {
504 Node_List* p = _packset.at(i);
505 if (p->at(pos) == s) {
506 return true;
507 }
508 }
509 return false;
510 }
512 //------------------------------are_adjacent_refs---------------------------
513 // Is s1 immediately before s2 in memory?
514 bool SuperWord::are_adjacent_refs(Node* s1, Node* s2) {
515 if (!s1->is_Mem() || !s2->is_Mem()) return false;
516 if (!in_bb(s1) || !in_bb(s2)) return false;
517 // FIXME - co_locate_pack fails on Stores in different mem-slices, so
518 // only pack memops that are in the same alias set until that's fixed.
519 if (_phase->C->get_alias_index(s1->as_Mem()->adr_type()) !=
520 _phase->C->get_alias_index(s2->as_Mem()->adr_type()))
521 return false;
522 SWPointer p1(s1->as_Mem(), this);
523 SWPointer p2(s2->as_Mem(), this);
524 if (p1.base() != p2.base() || !p1.comparable(p2)) return false;
525 int diff = p2.offset_in_bytes() - p1.offset_in_bytes();
526 return diff == data_size(s1);
527 }
529 //------------------------------isomorphic---------------------------
530 // Are s1 and s2 similar?
531 bool SuperWord::isomorphic(Node* s1, Node* s2) {
532 if (s1->Opcode() != s2->Opcode()) return false;
533 if (s1->req() != s2->req()) return false;
534 if (s1->in(0) != s2->in(0)) return false;
535 if (velt_type(s1) != velt_type(s2)) return false;
536 return true;
537 }
539 //------------------------------independent---------------------------
540 // Is there no data path from s1 to s2 or s2 to s1?
541 bool SuperWord::independent(Node* s1, Node* s2) {
542 // assert(s1->Opcode() == s2->Opcode(), "check isomorphic first");
543 int d1 = depth(s1);
544 int d2 = depth(s2);
545 if (d1 == d2) return s1 != s2;
546 Node* deep = d1 > d2 ? s1 : s2;
547 Node* shallow = d1 > d2 ? s2 : s1;
549 visited_clear();
551 return independent_path(shallow, deep);
552 }
554 //------------------------------independent_path------------------------------
555 // Helper for independent
556 bool SuperWord::independent_path(Node* shallow, Node* deep, uint dp) {
557 if (dp >= 1000) return false; // stop deep recursion
558 visited_set(deep);
559 int shal_depth = depth(shallow);
560 assert(shal_depth <= depth(deep), "must be");
561 for (DepPreds preds(deep, _dg); !preds.done(); preds.next()) {
562 Node* pred = preds.current();
563 if (in_bb(pred) && !visited_test(pred)) {
564 if (shallow == pred) {
565 return false;
566 }
567 if (shal_depth < depth(pred) && !independent_path(shallow, pred, dp+1)) {
568 return false;
569 }
570 }
571 }
572 return true;
573 }
575 //------------------------------set_alignment---------------------------
576 void SuperWord::set_alignment(Node* s1, Node* s2, int align) {
577 set_alignment(s1, align);
578 set_alignment(s2, align + data_size(s1));
579 }
581 //------------------------------data_size---------------------------
582 int SuperWord::data_size(Node* s) {
583 const Type* t = velt_type(s);
584 BasicType bt = t->array_element_basic_type();
585 int bsize = type2aelembytes(bt);
586 assert(bsize != 0, "valid size");
587 return bsize;
588 }
590 //------------------------------extend_packlist---------------------------
591 // Extend packset by following use->def and def->use links from pack members.
592 void SuperWord::extend_packlist() {
593 bool changed;
594 do {
595 changed = false;
596 for (int i = 0; i < _packset.length(); i++) {
597 Node_List* p = _packset.at(i);
598 changed |= follow_use_defs(p);
599 changed |= follow_def_uses(p);
600 }
601 } while (changed);
603 #ifndef PRODUCT
604 if (TraceSuperWord) {
605 tty->print_cr("\nAfter extend_packlist");
606 print_packset();
607 }
608 #endif
609 }
611 //------------------------------follow_use_defs---------------------------
612 // Extend the packset by visiting operand definitions of nodes in pack p
613 bool SuperWord::follow_use_defs(Node_List* p) {
614 Node* s1 = p->at(0);
615 Node* s2 = p->at(1);
616 assert(p->size() == 2, "just checking");
617 assert(s1->req() == s2->req(), "just checking");
618 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
620 if (s1->is_Load()) return false;
622 int align = alignment(s1);
623 bool changed = false;
624 int start = s1->is_Store() ? MemNode::ValueIn : 1;
625 int end = s1->is_Store() ? MemNode::ValueIn+1 : s1->req();
626 for (int j = start; j < end; j++) {
627 Node* t1 = s1->in(j);
628 Node* t2 = s2->in(j);
629 if (!in_bb(t1) || !in_bb(t2))
630 continue;
631 if (stmts_can_pack(t1, t2, align)) {
632 if (est_savings(t1, t2) >= 0) {
633 Node_List* pair = new Node_List();
634 pair->push(t1);
635 pair->push(t2);
636 _packset.append(pair);
637 set_alignment(t1, t2, align);
638 changed = true;
639 }
640 }
641 }
642 return changed;
643 }
645 //------------------------------follow_def_uses---------------------------
646 // Extend the packset by visiting uses of nodes in pack p
647 bool SuperWord::follow_def_uses(Node_List* p) {
648 bool changed = false;
649 Node* s1 = p->at(0);
650 Node* s2 = p->at(1);
651 assert(p->size() == 2, "just checking");
652 assert(s1->req() == s2->req(), "just checking");
653 assert(alignment(s1) + data_size(s1) == alignment(s2), "just checking");
655 if (s1->is_Store()) return false;
657 int align = alignment(s1);
658 int savings = -1;
659 Node* u1 = NULL;
660 Node* u2 = NULL;
661 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
662 Node* t1 = s1->fast_out(i);
663 if (!in_bb(t1)) continue;
664 for (DUIterator_Fast jmax, j = s2->fast_outs(jmax); j < jmax; j++) {
665 Node* t2 = s2->fast_out(j);
666 if (!in_bb(t2)) continue;
667 if (!opnd_positions_match(s1, t1, s2, t2))
668 continue;
669 if (stmts_can_pack(t1, t2, align)) {
670 int my_savings = est_savings(t1, t2);
671 if (my_savings > savings) {
672 savings = my_savings;
673 u1 = t1;
674 u2 = t2;
675 }
676 }
677 }
678 }
679 if (savings >= 0) {
680 Node_List* pair = new Node_List();
681 pair->push(u1);
682 pair->push(u2);
683 _packset.append(pair);
684 set_alignment(u1, u2, align);
685 changed = true;
686 }
687 return changed;
688 }
690 //---------------------------opnd_positions_match-------------------------
691 // Is the use of d1 in u1 at the same operand position as d2 in u2?
692 bool SuperWord::opnd_positions_match(Node* d1, Node* u1, Node* d2, Node* u2) {
693 uint ct = u1->req();
694 if (ct != u2->req()) return false;
695 uint i1 = 0;
696 uint i2 = 0;
697 do {
698 for (i1++; i1 < ct; i1++) if (u1->in(i1) == d1) break;
699 for (i2++; i2 < ct; i2++) if (u2->in(i2) == d2) break;
700 if (i1 != i2) {
701 return false;
702 }
703 } while (i1 < ct);
704 return true;
705 }
707 //------------------------------est_savings---------------------------
708 // Estimate the savings from executing s1 and s2 as a pack
709 int SuperWord::est_savings(Node* s1, Node* s2) {
710 int save = 2 - 1; // 2 operations per instruction in packed form
712 // inputs
713 for (uint i = 1; i < s1->req(); i++) {
714 Node* x1 = s1->in(i);
715 Node* x2 = s2->in(i);
716 if (x1 != x2) {
717 if (are_adjacent_refs(x1, x2)) {
718 save += adjacent_profit(x1, x2);
719 } else if (!in_packset(x1, x2)) {
720 save -= pack_cost(2);
721 } else {
722 save += unpack_cost(2);
723 }
724 }
725 }
727 // uses of result
728 uint ct = 0;
729 for (DUIterator_Fast imax, i = s1->fast_outs(imax); i < imax; i++) {
730 Node* s1_use = s1->fast_out(i);
731 for (int j = 0; j < _packset.length(); j++) {
732 Node_List* p = _packset.at(j);
733 if (p->at(0) == s1_use) {
734 for (DUIterator_Fast kmax, k = s2->fast_outs(kmax); k < kmax; k++) {
735 Node* s2_use = s2->fast_out(k);
736 if (p->at(p->size()-1) == s2_use) {
737 ct++;
738 if (are_adjacent_refs(s1_use, s2_use)) {
739 save += adjacent_profit(s1_use, s2_use);
740 }
741 }
742 }
743 }
744 }
745 }
747 if (ct < s1->outcnt()) save += unpack_cost(1);
748 if (ct < s2->outcnt()) save += unpack_cost(1);
750 return save;
751 }
753 //------------------------------costs---------------------------
754 int SuperWord::adjacent_profit(Node* s1, Node* s2) { return 2; }
755 int SuperWord::pack_cost(int ct) { return ct; }
756 int SuperWord::unpack_cost(int ct) { return ct; }
758 //------------------------------combine_packs---------------------------
759 // Combine packs A and B with A.last == B.first into A.first..,A.last,B.second,..B.last
760 void SuperWord::combine_packs() {
761 bool changed;
762 do {
763 changed = false;
764 for (int i = 0; i < _packset.length(); i++) {
765 Node_List* p1 = _packset.at(i);
766 if (p1 == NULL) continue;
767 for (int j = 0; j < _packset.length(); j++) {
768 Node_List* p2 = _packset.at(j);
769 if (p2 == NULL) continue;
770 if (p1->at(p1->size()-1) == p2->at(0)) {
771 for (uint k = 1; k < p2->size(); k++) {
772 p1->push(p2->at(k));
773 }
774 _packset.at_put(j, NULL);
775 changed = true;
776 }
777 }
778 }
779 } while (changed);
781 for (int i = _packset.length() - 1; i >= 0; i--) {
782 Node_List* p1 = _packset.at(i);
783 if (p1 == NULL) {
784 _packset.remove_at(i);
785 }
786 }
788 #ifndef PRODUCT
789 if (TraceSuperWord) {
790 tty->print_cr("\nAfter combine_packs");
791 print_packset();
792 }
793 #endif
794 }
796 //-----------------------------construct_my_pack_map--------------------------
797 // Construct the map from nodes to packs. Only valid after the
798 // point where a node is only in one pack (after combine_packs).
799 void SuperWord::construct_my_pack_map() {
800 Node_List* rslt = NULL;
801 for (int i = 0; i < _packset.length(); i++) {
802 Node_List* p = _packset.at(i);
803 for (uint j = 0; j < p->size(); j++) {
804 Node* s = p->at(j);
805 assert(my_pack(s) == NULL, "only in one pack");
806 set_my_pack(s, p);
807 }
808 }
809 }
811 //------------------------------filter_packs---------------------------
812 // Remove packs that are not implemented or not profitable.
813 void SuperWord::filter_packs() {
815 // Remove packs that are not implemented
816 for (int i = _packset.length() - 1; i >= 0; i--) {
817 Node_List* pk = _packset.at(i);
818 bool impl = implemented(pk);
819 if (!impl) {
820 #ifndef PRODUCT
821 if (TraceSuperWord && Verbose) {
822 tty->print_cr("Unimplemented");
823 pk->at(0)->dump();
824 }
825 #endif
826 remove_pack_at(i);
827 }
828 }
830 // Remove packs that are not profitable
831 bool changed;
832 do {
833 changed = false;
834 for (int i = _packset.length() - 1; i >= 0; i--) {
835 Node_List* pk = _packset.at(i);
836 bool prof = profitable(pk);
837 if (!prof) {
838 #ifndef PRODUCT
839 if (TraceSuperWord && Verbose) {
840 tty->print_cr("Unprofitable");
841 pk->at(0)->dump();
842 }
843 #endif
844 remove_pack_at(i);
845 changed = true;
846 }
847 }
848 } while (changed);
850 #ifndef PRODUCT
851 if (TraceSuperWord) {
852 tty->print_cr("\nAfter filter_packs");
853 print_packset();
854 tty->cr();
855 }
856 #endif
857 }
859 //------------------------------implemented---------------------------
860 // Can code be generated for pack p?
861 bool SuperWord::implemented(Node_List* p) {
862 Node* p0 = p->at(0);
863 int vopc = VectorNode::opcode(p0->Opcode(), p->size(), velt_type(p0));
864 return vopc > 0 && Matcher::has_match_rule(vopc);
865 }
867 //------------------------------profitable---------------------------
868 // For pack p, are all operands and all uses (with in the block) vector?
869 bool SuperWord::profitable(Node_List* p) {
870 Node* p0 = p->at(0);
871 uint start, end;
872 vector_opd_range(p0, &start, &end);
874 // Return false if some input is not vector and inside block
875 for (uint i = start; i < end; i++) {
876 if (!is_vector_use(p0, i)) {
877 // For now, return false if not scalar promotion case (inputs are the same.)
878 // Later, implement PackNode and allow differing, non-vector inputs
879 // (maybe just the ones from outside the block.)
880 Node* p0_def = p0->in(i);
881 for (uint j = 1; j < p->size(); j++) {
882 Node* use = p->at(j);
883 Node* def = use->in(i);
884 if (p0_def != def)
885 return false;
886 }
887 }
888 }
889 if (!p0->is_Store()) {
890 // For now, return false if not all uses are vector.
891 // Later, implement ExtractNode and allow non-vector uses (maybe
892 // just the ones outside the block.)
893 for (uint i = 0; i < p->size(); i++) {
894 Node* def = p->at(i);
895 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
896 Node* use = def->fast_out(j);
897 for (uint k = 0; k < use->req(); k++) {
898 Node* n = use->in(k);
899 if (def == n) {
900 if (!is_vector_use(use, k)) {
901 return false;
902 }
903 }
904 }
905 }
906 }
907 }
908 return true;
909 }
911 //------------------------------schedule---------------------------
912 // Adjust the memory graph for the packed operations
913 void SuperWord::schedule() {
915 // Co-locate in the memory graph the members of each memory pack
916 for (int i = 0; i < _packset.length(); i++) {
917 co_locate_pack(_packset.at(i));
918 }
919 }
921 //-------------------------------remove_and_insert-------------------
922 //remove "current" from its current position in the memory graph and insert
923 //it after the appropriate insertion point (lip or uip)
924 void SuperWord::remove_and_insert(MemNode *current, MemNode *prev, MemNode *lip,
925 Node *uip, Unique_Node_List &sched_before) {
926 Node* my_mem = current->in(MemNode::Memory);
927 _igvn.hash_delete(current);
928 _igvn.hash_delete(my_mem);
930 //remove current_store from its current position in the memmory graph
931 for (DUIterator i = current->outs(); current->has_out(i); i++) {
932 Node* use = current->out(i);
933 if (use->is_Mem()) {
934 assert(use->in(MemNode::Memory) == current, "must be");
935 _igvn.hash_delete(use);
936 if (use == prev) { // connect prev to my_mem
937 use->set_req(MemNode::Memory, my_mem);
938 } else if (sched_before.member(use)) {
939 _igvn.hash_delete(uip);
940 use->set_req(MemNode::Memory, uip);
941 } else {
942 _igvn.hash_delete(lip);
943 use->set_req(MemNode::Memory, lip);
944 }
945 _igvn._worklist.push(use);
946 --i; //deleted this edge; rescan position
947 }
948 }
950 bool sched_up = sched_before.member(current);
951 Node *insert_pt = sched_up ? uip : lip;
952 _igvn.hash_delete(insert_pt);
954 // all uses of insert_pt's memory state should use current's instead
955 for (DUIterator i = insert_pt->outs(); insert_pt->has_out(i); i++) {
956 Node* use = insert_pt->out(i);
957 if (use->is_Mem()) {
958 assert(use->in(MemNode::Memory) == insert_pt, "must be");
959 _igvn.hash_delete(use);
960 use->set_req(MemNode::Memory, current);
961 _igvn._worklist.push(use);
962 --i; //deleted this edge; rescan position
963 } else if (!sched_up && use->is_Phi() && use->bottom_type() == Type::MEMORY) {
964 uint pos; //lip (lower insert point) must be the last one in the memory slice
965 _igvn.hash_delete(use);
966 for (pos=1; pos < use->req(); pos++) {
967 if (use->in(pos) == insert_pt) break;
968 }
969 use->set_req(pos, current);
970 _igvn._worklist.push(use);
971 --i;
972 }
973 }
975 //connect current to insert_pt
976 current->set_req(MemNode::Memory, insert_pt);
977 _igvn._worklist.push(current);
978 }
980 //------------------------------co_locate_pack----------------------------------
981 // To schedule a store pack, we need to move any sandwiched memory ops either before
982 // or after the pack, based upon dependence information:
983 // (1) If any store in the pack depends on the sandwiched memory op, the
984 // sandwiched memory op must be scheduled BEFORE the pack;
985 // (2) If a sandwiched memory op depends on any store in the pack, the
986 // sandwiched memory op must be scheduled AFTER the pack;
987 // (3) If a sandwiched memory op (say, memA) depends on another sandwiched
988 // memory op (say memB), memB must be scheduled before memA. So, if memA is
989 // scheduled before the pack, memB must also be scheduled before the pack;
990 // (4) If there is no dependence restriction for a sandwiched memory op, we simply
991 // schedule this store AFTER the pack
992 // (5) We know there is no dependence cycle, so there in no other case;
993 // (6) Finally, all memory ops in another single pack should be moved in the same direction.
994 //
995 // To schedule a load pack, we use the memory state of either the first or the last load in
996 // the pack, based on the dependence constraint.
997 void SuperWord::co_locate_pack(Node_List* pk) {
998 if (pk->at(0)->is_Store()) {
999 MemNode* first = executed_first(pk)->as_Mem();
1000 MemNode* last = executed_last(pk)->as_Mem();
1001 Unique_Node_List schedule_before_pack;
1002 Unique_Node_List memops;
1004 MemNode* current = last->in(MemNode::Memory)->as_Mem();
1005 MemNode* previous = last;
1006 while (true) {
1007 assert(in_bb(current), "stay in block");
1008 memops.push(previous);
1009 for (DUIterator i = current->outs(); current->has_out(i); i++) {
1010 Node* use = current->out(i);
1011 if (use->is_Mem() && use != previous)
1012 memops.push(use);
1013 }
1014 if(current == first) break;
1015 previous = current;
1016 current = current->in(MemNode::Memory)->as_Mem();
1017 }
1019 // determine which memory operations should be scheduled before the pack
1020 for (uint i = 1; i < memops.size(); i++) {
1021 Node *s1 = memops.at(i);
1022 if (!in_pack(s1, pk) && !schedule_before_pack.member(s1)) {
1023 for (uint j = 0; j< i; j++) {
1024 Node *s2 = memops.at(j);
1025 if (!independent(s1, s2)) {
1026 if (in_pack(s2, pk) || schedule_before_pack.member(s2)) {
1027 schedule_before_pack.push(s1); //s1 must be scheduled before
1028 Node_List* mem_pk = my_pack(s1);
1029 if (mem_pk != NULL) {
1030 for (uint ii = 0; ii < mem_pk->size(); ii++) {
1031 Node* s = mem_pk->at(ii); // follow partner
1032 if (memops.member(s) && !schedule_before_pack.member(s))
1033 schedule_before_pack.push(s);
1034 }
1035 }
1036 }
1037 }
1038 }
1039 }
1040 }
1042 MemNode* lower_insert_pt = last;
1043 Node* upper_insert_pt = first->in(MemNode::Memory);
1044 previous = last; //previous store in pk
1045 current = last->in(MemNode::Memory)->as_Mem();
1047 //start scheduling from "last" to "first"
1048 while (true) {
1049 assert(in_bb(current), "stay in block");
1050 assert(in_pack(previous, pk), "previous stays in pack");
1051 Node* my_mem = current->in(MemNode::Memory);
1053 if (in_pack(current, pk)) {
1054 // Forward users of my memory state (except "previous) to my input memory state
1055 _igvn.hash_delete(current);
1056 for (DUIterator i = current->outs(); current->has_out(i); i++) {
1057 Node* use = current->out(i);
1058 if (use->is_Mem() && use != previous) {
1059 assert(use->in(MemNode::Memory) == current, "must be");
1060 _igvn.hash_delete(use);
1061 if (schedule_before_pack.member(use)) {
1062 _igvn.hash_delete(upper_insert_pt);
1063 use->set_req(MemNode::Memory, upper_insert_pt);
1064 } else {
1065 _igvn.hash_delete(lower_insert_pt);
1066 use->set_req(MemNode::Memory, lower_insert_pt);
1067 }
1068 _igvn._worklist.push(use);
1069 --i; // deleted this edge; rescan position
1070 }
1071 }
1072 previous = current;
1073 } else { // !in_pack(current, pk) ==> a sandwiched store
1074 remove_and_insert(current, previous, lower_insert_pt, upper_insert_pt, schedule_before_pack);
1075 }
1077 if (current == first) break;
1078 current = my_mem->as_Mem();
1079 } // end while
1080 } else if (pk->at(0)->is_Load()) { //load
1081 // all loads in the pack should have the same memory state. By default,
1082 // we use the memory state of the last load. However, if any load could
1083 // not be moved down due to the dependence constraint, we use the memory
1084 // state of the first load.
1085 Node* last_mem = executed_last(pk)->in(MemNode::Memory);
1086 Node* first_mem = executed_first(pk)->in(MemNode::Memory);
1087 bool schedule_last = true;
1088 for (uint i = 0; i < pk->size(); i++) {
1089 Node* ld = pk->at(i);
1090 for (Node* current = last_mem; current != ld->in(MemNode::Memory);
1091 current=current->in(MemNode::Memory)) {
1092 assert(current != first_mem, "corrupted memory graph");
1093 if(current->is_Mem() && !independent(current, ld)){
1094 schedule_last = false; // a later store depends on this load
1095 break;
1096 }
1097 }
1098 }
1100 Node* mem_input = schedule_last ? last_mem : first_mem;
1101 _igvn.hash_delete(mem_input);
1102 // Give each load the same memory state
1103 for (uint i = 0; i < pk->size(); i++) {
1104 LoadNode* ld = pk->at(i)->as_Load();
1105 _igvn.hash_delete(ld);
1106 ld->set_req(MemNode::Memory, mem_input);
1107 _igvn._worklist.push(ld);
1108 }
1109 }
1110 }
1112 //------------------------------output---------------------------
1113 // Convert packs into vector node operations
1114 void SuperWord::output() {
1115 if (_packset.length() == 0) return;
1117 // MUST ENSURE main loop's initial value is properly aligned:
1118 // (iv_initial_value + min_iv_offset) % vector_width_in_bytes() == 0
1120 align_initial_loop_index(align_to_ref());
1122 // Insert extract (unpack) operations for scalar uses
1123 for (int i = 0; i < _packset.length(); i++) {
1124 insert_extracts(_packset.at(i));
1125 }
1127 for (int i = 0; i < _block.length(); i++) {
1128 Node* n = _block.at(i);
1129 Node_List* p = my_pack(n);
1130 if (p && n == executed_last(p)) {
1131 uint vlen = p->size();
1132 Node* vn = NULL;
1133 Node* low_adr = p->at(0);
1134 Node* first = executed_first(p);
1135 if (n->is_Load()) {
1136 int opc = n->Opcode();
1137 Node* ctl = n->in(MemNode::Control);
1138 Node* mem = first->in(MemNode::Memory);
1139 Node* adr = low_adr->in(MemNode::Address);
1140 const TypePtr* atyp = n->adr_type();
1141 vn = VectorLoadNode::make(_phase->C, opc, ctl, mem, adr, atyp, vlen);
1143 } else if (n->is_Store()) {
1144 // Promote value to be stored to vector
1145 VectorNode* val = vector_opd(p, MemNode::ValueIn);
1147 int opc = n->Opcode();
1148 Node* ctl = n->in(MemNode::Control);
1149 Node* mem = first->in(MemNode::Memory);
1150 Node* adr = low_adr->in(MemNode::Address);
1151 const TypePtr* atyp = n->adr_type();
1152 vn = VectorStoreNode::make(_phase->C, opc, ctl, mem, adr, atyp, val, vlen);
1154 } else if (n->req() == 3) {
1155 // Promote operands to vector
1156 Node* in1 = vector_opd(p, 1);
1157 Node* in2 = vector_opd(p, 2);
1158 vn = VectorNode::make(_phase->C, n->Opcode(), in1, in2, vlen, velt_type(n));
1160 } else {
1161 ShouldNotReachHere();
1162 }
1164 _phase->_igvn.register_new_node_with_optimizer(vn);
1165 _phase->set_ctrl(vn, _phase->get_ctrl(p->at(0)));
1166 for (uint j = 0; j < p->size(); j++) {
1167 Node* pm = p->at(j);
1168 _igvn.hash_delete(pm);
1169 _igvn.subsume_node(pm, vn);
1170 }
1171 _igvn._worklist.push(vn);
1172 }
1173 }
1174 }
1176 //------------------------------vector_opd---------------------------
1177 // Create a vector operand for the nodes in pack p for operand: in(opd_idx)
1178 VectorNode* SuperWord::vector_opd(Node_List* p, int opd_idx) {
1179 Node* p0 = p->at(0);
1180 uint vlen = p->size();
1181 Node* opd = p0->in(opd_idx);
1183 bool same_opd = true;
1184 for (uint i = 1; i < vlen; i++) {
1185 Node* pi = p->at(i);
1186 Node* in = pi->in(opd_idx);
1187 if (opd != in) {
1188 same_opd = false;
1189 break;
1190 }
1191 }
1193 if (same_opd) {
1194 if (opd->is_Vector()) {
1195 return (VectorNode*)opd; // input is matching vector
1196 }
1197 // Convert scalar input to vector. Use p0's type because it's container
1198 // maybe smaller than the operand's container.
1199 const Type* opd_t = velt_type(!in_bb(opd) ? p0 : opd);
1200 const Type* p0_t = velt_type(p0);
1201 if (p0_t->higher_equal(opd_t)) opd_t = p0_t;
1202 VectorNode* vn = VectorNode::scalar2vector(_phase->C, opd, vlen, opd_t);
1204 _phase->_igvn.register_new_node_with_optimizer(vn);
1205 _phase->set_ctrl(vn, _phase->get_ctrl(opd));
1206 return vn;
1207 }
1209 // Insert pack operation
1210 const Type* opd_t = velt_type(!in_bb(opd) ? p0 : opd);
1211 PackNode* pk = PackNode::make(_phase->C, opd, opd_t);
1213 for (uint i = 1; i < vlen; i++) {
1214 Node* pi = p->at(i);
1215 Node* in = pi->in(opd_idx);
1216 assert(my_pack(in) == NULL, "Should already have been unpacked");
1217 assert(opd_t == velt_type(!in_bb(in) ? pi : in), "all same type");
1218 pk->add_opd(in);
1219 }
1220 _phase->_igvn.register_new_node_with_optimizer(pk);
1221 _phase->set_ctrl(pk, _phase->get_ctrl(opd));
1222 return pk;
1223 }
1225 //------------------------------insert_extracts---------------------------
1226 // If a use of pack p is not a vector use, then replace the
1227 // use with an extract operation.
1228 void SuperWord::insert_extracts(Node_List* p) {
1229 if (p->at(0)->is_Store()) return;
1230 assert(_n_idx_list.is_empty(), "empty (node,index) list");
1232 // Inspect each use of each pack member. For each use that is
1233 // not a vector use, replace the use with an extract operation.
1235 for (uint i = 0; i < p->size(); i++) {
1236 Node* def = p->at(i);
1237 for (DUIterator_Fast jmax, j = def->fast_outs(jmax); j < jmax; j++) {
1238 Node* use = def->fast_out(j);
1239 for (uint k = 0; k < use->req(); k++) {
1240 Node* n = use->in(k);
1241 if (def == n) {
1242 if (!is_vector_use(use, k)) {
1243 _n_idx_list.push(use, k);
1244 }
1245 }
1246 }
1247 }
1248 }
1250 while (_n_idx_list.is_nonempty()) {
1251 Node* use = _n_idx_list.node();
1252 int idx = _n_idx_list.index();
1253 _n_idx_list.pop();
1254 Node* def = use->in(idx);
1256 // Insert extract operation
1257 _igvn.hash_delete(def);
1258 _igvn.hash_delete(use);
1259 int def_pos = alignment(def) / data_size(def);
1260 const Type* def_t = velt_type(def);
1262 Node* ex = ExtractNode::make(_phase->C, def, def_pos, def_t);
1263 _phase->_igvn.register_new_node_with_optimizer(ex);
1264 _phase->set_ctrl(ex, _phase->get_ctrl(def));
1265 use->set_req(idx, ex);
1266 _igvn._worklist.push(def);
1267 _igvn._worklist.push(use);
1269 bb_insert_after(ex, bb_idx(def));
1270 set_velt_type(ex, def_t);
1271 }
1272 }
1274 //------------------------------is_vector_use---------------------------
1275 // Is use->in(u_idx) a vector use?
1276 bool SuperWord::is_vector_use(Node* use, int u_idx) {
1277 Node_List* u_pk = my_pack(use);
1278 if (u_pk == NULL) return false;
1279 Node* def = use->in(u_idx);
1280 Node_List* d_pk = my_pack(def);
1281 if (d_pk == NULL) {
1282 // check for scalar promotion
1283 Node* n = u_pk->at(0)->in(u_idx);
1284 for (uint i = 1; i < u_pk->size(); i++) {
1285 if (u_pk->at(i)->in(u_idx) != n) return false;
1286 }
1287 return true;
1288 }
1289 if (u_pk->size() != d_pk->size())
1290 return false;
1291 for (uint i = 0; i < u_pk->size(); i++) {
1292 Node* ui = u_pk->at(i);
1293 Node* di = d_pk->at(i);
1294 if (ui->in(u_idx) != di || alignment(ui) != alignment(di))
1295 return false;
1296 }
1297 return true;
1298 }
1300 //------------------------------construct_bb---------------------------
1301 // Construct reverse postorder list of block members
1302 void SuperWord::construct_bb() {
1303 Node* entry = bb();
1305 assert(_stk.length() == 0, "stk is empty");
1306 assert(_block.length() == 0, "block is empty");
1307 assert(_data_entry.length() == 0, "data_entry is empty");
1308 assert(_mem_slice_head.length() == 0, "mem_slice_head is empty");
1309 assert(_mem_slice_tail.length() == 0, "mem_slice_tail is empty");
1311 // Find non-control nodes with no inputs from within block,
1312 // create a temporary map from node _idx to bb_idx for use
1313 // by the visited and post_visited sets,
1314 // and count number of nodes in block.
1315 int bb_ct = 0;
1316 for (uint i = 0; i < lpt()->_body.size(); i++ ) {
1317 Node *n = lpt()->_body.at(i);
1318 set_bb_idx(n, i); // Create a temporary map
1319 if (in_bb(n)) {
1320 bb_ct++;
1321 if (!n->is_CFG()) {
1322 bool found = false;
1323 for (uint j = 0; j < n->req(); j++) {
1324 Node* def = n->in(j);
1325 if (def && in_bb(def)) {
1326 found = true;
1327 break;
1328 }
1329 }
1330 if (!found) {
1331 assert(n != entry, "can't be entry");
1332 _data_entry.push(n);
1333 }
1334 }
1335 }
1336 }
1338 // Find memory slices (head and tail)
1339 for (DUIterator_Fast imax, i = lp()->fast_outs(imax); i < imax; i++) {
1340 Node *n = lp()->fast_out(i);
1341 if (in_bb(n) && (n->is_Phi() && n->bottom_type() == Type::MEMORY)) {
1342 Node* n_tail = n->in(LoopNode::LoopBackControl);
1343 if (n_tail != n->in(LoopNode::EntryControl)) {
1344 _mem_slice_head.push(n);
1345 _mem_slice_tail.push(n_tail);
1346 }
1347 }
1348 }
1350 // Create an RPO list of nodes in block
1352 visited_clear();
1353 post_visited_clear();
1355 // Push all non-control nodes with no inputs from within block, then control entry
1356 for (int j = 0; j < _data_entry.length(); j++) {
1357 Node* n = _data_entry.at(j);
1358 visited_set(n);
1359 _stk.push(n);
1360 }
1361 visited_set(entry);
1362 _stk.push(entry);
1364 // Do a depth first walk over out edges
1365 int rpo_idx = bb_ct - 1;
1366 int size;
1367 while ((size = _stk.length()) > 0) {
1368 Node* n = _stk.top(); // Leave node on stack
1369 if (!visited_test_set(n)) {
1370 // forward arc in graph
1371 } else if (!post_visited_test(n)) {
1372 // cross or back arc
1373 for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
1374 Node *use = n->fast_out(i);
1375 if (in_bb(use) && !visited_test(use) &&
1376 // Don't go around backedge
1377 (!use->is_Phi() || n == entry)) {
1378 _stk.push(use);
1379 }
1380 }
1381 if (_stk.length() == size) {
1382 // There were no additional uses, post visit node now
1383 _stk.pop(); // Remove node from stack
1384 assert(rpo_idx >= 0, "");
1385 _block.at_put_grow(rpo_idx, n);
1386 rpo_idx--;
1387 post_visited_set(n);
1388 assert(rpo_idx >= 0 || _stk.is_empty(), "");
1389 }
1390 } else {
1391 _stk.pop(); // Remove post-visited node from stack
1392 }
1393 }
1395 // Create real map of block indices for nodes
1396 for (int j = 0; j < _block.length(); j++) {
1397 Node* n = _block.at(j);
1398 set_bb_idx(n, j);
1399 }
1401 initialize_bb(); // Ensure extra info is allocated.
1403 #ifndef PRODUCT
1404 if (TraceSuperWord) {
1405 print_bb();
1406 tty->print_cr("\ndata entry nodes: %s", _data_entry.length() > 0 ? "" : "NONE");
1407 for (int m = 0; m < _data_entry.length(); m++) {
1408 tty->print("%3d ", m);
1409 _data_entry.at(m)->dump();
1410 }
1411 tty->print_cr("\nmemory slices: %s", _mem_slice_head.length() > 0 ? "" : "NONE");
1412 for (int m = 0; m < _mem_slice_head.length(); m++) {
1413 tty->print("%3d ", m); _mem_slice_head.at(m)->dump();
1414 tty->print(" "); _mem_slice_tail.at(m)->dump();
1415 }
1416 }
1417 #endif
1418 assert(rpo_idx == -1 && bb_ct == _block.length(), "all block members found");
1419 }
1421 //------------------------------initialize_bb---------------------------
1422 // Initialize per node info
1423 void SuperWord::initialize_bb() {
1424 Node* last = _block.at(_block.length() - 1);
1425 grow_node_info(bb_idx(last));
1426 }
1428 //------------------------------bb_insert_after---------------------------
1429 // Insert n into block after pos
1430 void SuperWord::bb_insert_after(Node* n, int pos) {
1431 int n_pos = pos + 1;
1432 // Make room
1433 for (int i = _block.length() - 1; i >= n_pos; i--) {
1434 _block.at_put_grow(i+1, _block.at(i));
1435 }
1436 for (int j = _node_info.length() - 1; j >= n_pos; j--) {
1437 _node_info.at_put_grow(j+1, _node_info.at(j));
1438 }
1439 // Set value
1440 _block.at_put_grow(n_pos, n);
1441 _node_info.at_put_grow(n_pos, SWNodeInfo::initial);
1442 // Adjust map from node->_idx to _block index
1443 for (int i = n_pos; i < _block.length(); i++) {
1444 set_bb_idx(_block.at(i), i);
1445 }
1446 }
1448 //------------------------------compute_max_depth---------------------------
1449 // Compute max depth for expressions from beginning of block
1450 // Use to prune search paths during test for independence.
1451 void SuperWord::compute_max_depth() {
1452 int ct = 0;
1453 bool again;
1454 do {
1455 again = false;
1456 for (int i = 0; i < _block.length(); i++) {
1457 Node* n = _block.at(i);
1458 if (!n->is_Phi()) {
1459 int d_orig = depth(n);
1460 int d_in = 0;
1461 for (DepPreds preds(n, _dg); !preds.done(); preds.next()) {
1462 Node* pred = preds.current();
1463 if (in_bb(pred)) {
1464 d_in = MAX2(d_in, depth(pred));
1465 }
1466 }
1467 if (d_in + 1 != d_orig) {
1468 set_depth(n, d_in + 1);
1469 again = true;
1470 }
1471 }
1472 }
1473 ct++;
1474 } while (again);
1475 #ifndef PRODUCT
1476 if (TraceSuperWord && Verbose)
1477 tty->print_cr("compute_max_depth iterated: %d times", ct);
1478 #endif
1479 }
1481 //-------------------------compute_vector_element_type-----------------------
1482 // Compute necessary vector element type for expressions
1483 // This propagates backwards a narrower integer type when the
1484 // upper bits of the value are not needed.
1485 // Example: char a,b,c; a = b + c;
1486 // Normally the type of the add is integer, but for packed character
1487 // operations the type of the add needs to be char.
1488 void SuperWord::compute_vector_element_type() {
1489 #ifndef PRODUCT
1490 if (TraceSuperWord && Verbose)
1491 tty->print_cr("\ncompute_velt_type:");
1492 #endif
1494 // Initial type
1495 for (int i = 0; i < _block.length(); i++) {
1496 Node* n = _block.at(i);
1497 const Type* t = n->is_Mem() ? Type::get_const_basic_type(n->as_Mem()->memory_type())
1498 : _igvn.type(n);
1499 const Type* vt = container_type(t);
1500 set_velt_type(n, vt);
1501 }
1503 // Propagate narrowed type backwards through operations
1504 // that don't depend on higher order bits
1505 for (int i = _block.length() - 1; i >= 0; i--) {
1506 Node* n = _block.at(i);
1507 // Only integer types need be examined
1508 if (n->bottom_type()->isa_int()) {
1509 uint start, end;
1510 vector_opd_range(n, &start, &end);
1511 const Type* vt = velt_type(n);
1513 for (uint j = start; j < end; j++) {
1514 Node* in = n->in(j);
1515 // Don't propagate through a type conversion
1516 if (n->bottom_type() != in->bottom_type())
1517 continue;
1518 switch(in->Opcode()) {
1519 case Op_AddI: case Op_AddL:
1520 case Op_SubI: case Op_SubL:
1521 case Op_MulI: case Op_MulL:
1522 case Op_AndI: case Op_AndL:
1523 case Op_OrI: case Op_OrL:
1524 case Op_XorI: case Op_XorL:
1525 case Op_LShiftI: case Op_LShiftL:
1526 case Op_CMoveI: case Op_CMoveL:
1527 if (in_bb(in)) {
1528 bool same_type = true;
1529 for (DUIterator_Fast kmax, k = in->fast_outs(kmax); k < kmax; k++) {
1530 Node *use = in->fast_out(k);
1531 if (!in_bb(use) || velt_type(use) != vt) {
1532 same_type = false;
1533 break;
1534 }
1535 }
1536 if (same_type) {
1537 set_velt_type(in, vt);
1538 }
1539 }
1540 }
1541 }
1542 }
1543 }
1544 #ifndef PRODUCT
1545 if (TraceSuperWord && Verbose) {
1546 for (int i = 0; i < _block.length(); i++) {
1547 Node* n = _block.at(i);
1548 velt_type(n)->dump();
1549 tty->print("\t");
1550 n->dump();
1551 }
1552 }
1553 #endif
1554 }
1556 //------------------------------memory_alignment---------------------------
1557 // Alignment within a vector memory reference
1558 int SuperWord::memory_alignment(MemNode* s, int iv_adjust_in_bytes) {
1559 SWPointer p(s, this);
1560 if (!p.valid()) {
1561 return bottom_align;
1562 }
1563 int offset = p.offset_in_bytes();
1564 offset += iv_adjust_in_bytes;
1565 int off_rem = offset % vector_width_in_bytes();
1566 int off_mod = off_rem >= 0 ? off_rem : off_rem + vector_width_in_bytes();
1567 return off_mod;
1568 }
1570 //---------------------------container_type---------------------------
1571 // Smallest type containing range of values
1572 const Type* SuperWord::container_type(const Type* t) {
1573 const Type* tp = t->make_ptr();
1574 if (tp && tp->isa_aryptr()) {
1575 t = tp->is_aryptr()->elem();
1576 }
1577 if (t->basic_type() == T_INT) {
1578 if (t->higher_equal(TypeInt::BOOL)) return TypeInt::BOOL;
1579 if (t->higher_equal(TypeInt::BYTE)) return TypeInt::BYTE;
1580 if (t->higher_equal(TypeInt::CHAR)) return TypeInt::CHAR;
1581 if (t->higher_equal(TypeInt::SHORT)) return TypeInt::SHORT;
1582 return TypeInt::INT;
1583 }
1584 return t;
1585 }
1587 //-------------------------vector_opd_range-----------------------
1588 // (Start, end] half-open range defining which operands are vector
1589 void SuperWord::vector_opd_range(Node* n, uint* start, uint* end) {
1590 switch (n->Opcode()) {
1591 case Op_LoadB: case Op_LoadUS:
1592 case Op_LoadI: case Op_LoadL:
1593 case Op_LoadF: case Op_LoadD:
1594 case Op_LoadP:
1595 *start = 0;
1596 *end = 0;
1597 return;
1598 case Op_StoreB: case Op_StoreC:
1599 case Op_StoreI: case Op_StoreL:
1600 case Op_StoreF: case Op_StoreD:
1601 case Op_StoreP:
1602 *start = MemNode::ValueIn;
1603 *end = *start + 1;
1604 return;
1605 case Op_LShiftI: case Op_LShiftL:
1606 *start = 1;
1607 *end = 2;
1608 return;
1609 case Op_CMoveI: case Op_CMoveL: case Op_CMoveF: case Op_CMoveD:
1610 *start = 2;
1611 *end = n->req();
1612 return;
1613 }
1614 *start = 1;
1615 *end = n->req(); // default is all operands
1616 }
1618 //------------------------------in_packset---------------------------
1619 // Are s1 and s2 in a pack pair and ordered as s1,s2?
1620 bool SuperWord::in_packset(Node* s1, Node* s2) {
1621 for (int i = 0; i < _packset.length(); i++) {
1622 Node_List* p = _packset.at(i);
1623 assert(p->size() == 2, "must be");
1624 if (p->at(0) == s1 && p->at(p->size()-1) == s2) {
1625 return true;
1626 }
1627 }
1628 return false;
1629 }
1631 //------------------------------in_pack---------------------------
1632 // Is s in pack p?
1633 Node_List* SuperWord::in_pack(Node* s, Node_List* p) {
1634 for (uint i = 0; i < p->size(); i++) {
1635 if (p->at(i) == s) {
1636 return p;
1637 }
1638 }
1639 return NULL;
1640 }
1642 //------------------------------remove_pack_at---------------------------
1643 // Remove the pack at position pos in the packset
1644 void SuperWord::remove_pack_at(int pos) {
1645 Node_List* p = _packset.at(pos);
1646 for (uint i = 0; i < p->size(); i++) {
1647 Node* s = p->at(i);
1648 set_my_pack(s, NULL);
1649 }
1650 _packset.remove_at(pos);
1651 }
1653 //------------------------------executed_first---------------------------
1654 // Return the node executed first in pack p. Uses the RPO block list
1655 // to determine order.
1656 Node* SuperWord::executed_first(Node_List* p) {
1657 Node* n = p->at(0);
1658 int n_rpo = bb_idx(n);
1659 for (uint i = 1; i < p->size(); i++) {
1660 Node* s = p->at(i);
1661 int s_rpo = bb_idx(s);
1662 if (s_rpo < n_rpo) {
1663 n = s;
1664 n_rpo = s_rpo;
1665 }
1666 }
1667 return n;
1668 }
1670 //------------------------------executed_last---------------------------
1671 // Return the node executed last in pack p.
1672 Node* SuperWord::executed_last(Node_List* p) {
1673 Node* n = p->at(0);
1674 int n_rpo = bb_idx(n);
1675 for (uint i = 1; i < p->size(); i++) {
1676 Node* s = p->at(i);
1677 int s_rpo = bb_idx(s);
1678 if (s_rpo > n_rpo) {
1679 n = s;
1680 n_rpo = s_rpo;
1681 }
1682 }
1683 return n;
1684 }
1686 //----------------------------align_initial_loop_index---------------------------
1687 // Adjust pre-loop limit so that in main loop, a load/store reference
1688 // to align_to_ref will be a position zero in the vector.
1689 // (iv + k) mod vector_align == 0
1690 void SuperWord::align_initial_loop_index(MemNode* align_to_ref) {
1691 CountedLoopNode *main_head = lp()->as_CountedLoop();
1692 assert(main_head->is_main_loop(), "");
1693 CountedLoopEndNode* pre_end = get_pre_loop_end(main_head);
1694 assert(pre_end != NULL, "");
1695 Node *pre_opaq1 = pre_end->limit();
1696 assert(pre_opaq1->Opcode() == Op_Opaque1, "");
1697 Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
1698 Node *lim0 = pre_opaq->in(1);
1700 // Where we put new limit calculations
1701 Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
1703 // Ensure the original loop limit is available from the
1704 // pre-loop Opaque1 node.
1705 Node *orig_limit = pre_opaq->original_loop_limit();
1706 assert(orig_limit != NULL && _igvn.type(orig_limit) != Type::TOP, "");
1708 SWPointer align_to_ref_p(align_to_ref, this);
1710 // Given:
1711 // lim0 == original pre loop limit
1712 // V == v_align (power of 2)
1713 // invar == extra invariant piece of the address expression
1714 // e == k [ +/- invar ]
1715 //
1716 // When reassociating expressions involving '%' the basic rules are:
1717 // (a - b) % k == 0 => a % k == b % k
1718 // and:
1719 // (a + b) % k == 0 => a % k == (k - b) % k
1720 //
1721 // For stride > 0 && scale > 0,
1722 // Derive the new pre-loop limit "lim" such that the two constraints:
1723 // (1) lim = lim0 + N (where N is some positive integer < V)
1724 // (2) (e + lim) % V == 0
1725 // are true.
1726 //
1727 // Substituting (1) into (2),
1728 // (e + lim0 + N) % V == 0
1729 // solve for N:
1730 // N = (V - (e + lim0)) % V
1731 // substitute back into (1), so that new limit
1732 // lim = lim0 + (V - (e + lim0)) % V
1733 //
1734 // For stride > 0 && scale < 0
1735 // Constraints:
1736 // lim = lim0 + N
1737 // (e - lim) % V == 0
1738 // Solving for lim:
1739 // (e - lim0 - N) % V == 0
1740 // N = (e - lim0) % V
1741 // lim = lim0 + (e - lim0) % V
1742 //
1743 // For stride < 0 && scale > 0
1744 // Constraints:
1745 // lim = lim0 - N
1746 // (e + lim) % V == 0
1747 // Solving for lim:
1748 // (e + lim0 - N) % V == 0
1749 // N = (e + lim0) % V
1750 // lim = lim0 - (e + lim0) % V
1751 //
1752 // For stride < 0 && scale < 0
1753 // Constraints:
1754 // lim = lim0 - N
1755 // (e - lim) % V == 0
1756 // Solving for lim:
1757 // (e - lim0 + N) % V == 0
1758 // N = (V - (e - lim0)) % V
1759 // lim = lim0 - (V - (e - lim0)) % V
1761 int stride = iv_stride();
1762 int scale = align_to_ref_p.scale_in_bytes();
1763 int elt_size = align_to_ref_p.memory_size();
1764 int v_align = vector_width_in_bytes() / elt_size;
1765 int k = align_to_ref_p.offset_in_bytes() / elt_size;
1767 Node *kn = _igvn.intcon(k);
1769 Node *e = kn;
1770 if (align_to_ref_p.invar() != NULL) {
1771 // incorporate any extra invariant piece producing k +/- invar >>> log2(elt)
1772 Node* log2_elt = _igvn.intcon(exact_log2(elt_size));
1773 Node* aref = new (_phase->C, 3) URShiftINode(align_to_ref_p.invar(), log2_elt);
1774 _phase->_igvn.register_new_node_with_optimizer(aref);
1775 _phase->set_ctrl(aref, pre_ctrl);
1776 if (align_to_ref_p.negate_invar()) {
1777 e = new (_phase->C, 3) SubINode(e, aref);
1778 } else {
1779 e = new (_phase->C, 3) AddINode(e, aref);
1780 }
1781 _phase->_igvn.register_new_node_with_optimizer(e);
1782 _phase->set_ctrl(e, pre_ctrl);
1783 }
1785 // compute e +/- lim0
1786 if (scale < 0) {
1787 e = new (_phase->C, 3) SubINode(e, lim0);
1788 } else {
1789 e = new (_phase->C, 3) AddINode(e, lim0);
1790 }
1791 _phase->_igvn.register_new_node_with_optimizer(e);
1792 _phase->set_ctrl(e, pre_ctrl);
1794 if (stride * scale > 0) {
1795 // compute V - (e +/- lim0)
1796 Node* va = _igvn.intcon(v_align);
1797 e = new (_phase->C, 3) SubINode(va, e);
1798 _phase->_igvn.register_new_node_with_optimizer(e);
1799 _phase->set_ctrl(e, pre_ctrl);
1800 }
1801 // compute N = (exp) % V
1802 Node* va_msk = _igvn.intcon(v_align - 1);
1803 Node* N = new (_phase->C, 3) AndINode(e, va_msk);
1804 _phase->_igvn.register_new_node_with_optimizer(N);
1805 _phase->set_ctrl(N, pre_ctrl);
1807 // substitute back into (1), so that new limit
1808 // lim = lim0 + N
1809 Node* lim;
1810 if (stride < 0) {
1811 lim = new (_phase->C, 3) SubINode(lim0, N);
1812 } else {
1813 lim = new (_phase->C, 3) AddINode(lim0, N);
1814 }
1815 _phase->_igvn.register_new_node_with_optimizer(lim);
1816 _phase->set_ctrl(lim, pre_ctrl);
1817 Node* constrained =
1818 (stride > 0) ? (Node*) new (_phase->C,3) MinINode(lim, orig_limit)
1819 : (Node*) new (_phase->C,3) MaxINode(lim, orig_limit);
1820 _phase->_igvn.register_new_node_with_optimizer(constrained);
1821 _phase->set_ctrl(constrained, pre_ctrl);
1822 _igvn.hash_delete(pre_opaq);
1823 pre_opaq->set_req(1, constrained);
1824 }
1826 //----------------------------get_pre_loop_end---------------------------
1827 // Find pre loop end from main loop. Returns null if none.
1828 CountedLoopEndNode* SuperWord::get_pre_loop_end(CountedLoopNode *cl) {
1829 Node *ctrl = cl->in(LoopNode::EntryControl);
1830 if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return NULL;
1831 Node *iffm = ctrl->in(0);
1832 if (!iffm->is_If()) return NULL;
1833 Node *p_f = iffm->in(0);
1834 if (!p_f->is_IfFalse()) return NULL;
1835 if (!p_f->in(0)->is_CountedLoopEnd()) return NULL;
1836 CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
1837 if (!pre_end->loopnode()->is_pre_loop()) return NULL;
1838 return pre_end;
1839 }
1842 //------------------------------init---------------------------
1843 void SuperWord::init() {
1844 _dg.init();
1845 _packset.clear();
1846 _disjoint_ptrs.clear();
1847 _block.clear();
1848 _data_entry.clear();
1849 _mem_slice_head.clear();
1850 _mem_slice_tail.clear();
1851 _node_info.clear();
1852 _align_to_ref = NULL;
1853 _lpt = NULL;
1854 _lp = NULL;
1855 _bb = NULL;
1856 _iv = NULL;
1857 }
1859 //------------------------------print_packset---------------------------
1860 void SuperWord::print_packset() {
1861 #ifndef PRODUCT
1862 tty->print_cr("packset");
1863 for (int i = 0; i < _packset.length(); i++) {
1864 tty->print_cr("Pack: %d", i);
1865 Node_List* p = _packset.at(i);
1866 print_pack(p);
1867 }
1868 #endif
1869 }
1871 //------------------------------print_pack---------------------------
1872 void SuperWord::print_pack(Node_List* p) {
1873 for (uint i = 0; i < p->size(); i++) {
1874 print_stmt(p->at(i));
1875 }
1876 }
1878 //------------------------------print_bb---------------------------
1879 void SuperWord::print_bb() {
1880 #ifndef PRODUCT
1881 tty->print_cr("\nBlock");
1882 for (int i = 0; i < _block.length(); i++) {
1883 Node* n = _block.at(i);
1884 tty->print("%d ", i);
1885 if (n) {
1886 n->dump();
1887 }
1888 }
1889 #endif
1890 }
1892 //------------------------------print_stmt---------------------------
1893 void SuperWord::print_stmt(Node* s) {
1894 #ifndef PRODUCT
1895 tty->print(" align: %d \t", alignment(s));
1896 s->dump();
1897 #endif
1898 }
1900 //------------------------------blank---------------------------
1901 char* SuperWord::blank(uint depth) {
1902 static char blanks[101];
1903 assert(depth < 101, "too deep");
1904 for (uint i = 0; i < depth; i++) blanks[i] = ' ';
1905 blanks[depth] = '\0';
1906 return blanks;
1907 }
1910 //==============================SWPointer===========================
1912 //----------------------------SWPointer------------------------
1913 SWPointer::SWPointer(MemNode* mem, SuperWord* slp) :
1914 _mem(mem), _slp(slp), _base(NULL), _adr(NULL),
1915 _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {
1917 Node* adr = mem->in(MemNode::Address);
1918 if (!adr->is_AddP()) {
1919 assert(!valid(), "too complex");
1920 return;
1921 }
1922 // Match AddP(base, AddP(ptr, k*iv [+ invariant]), constant)
1923 Node* base = adr->in(AddPNode::Base);
1924 //unsafe reference could not be aligned appropriately without runtime checking
1925 if (base == NULL || base->bottom_type() == Type::TOP) {
1926 assert(!valid(), "unsafe access");
1927 return;
1928 }
1929 for (int i = 0; i < 3; i++) {
1930 if (!scaled_iv_plus_offset(adr->in(AddPNode::Offset))) {
1931 assert(!valid(), "too complex");
1932 return;
1933 }
1934 adr = adr->in(AddPNode::Address);
1935 if (base == adr || !adr->is_AddP()) {
1936 break; // stop looking at addp's
1937 }
1938 }
1939 _base = base;
1940 _adr = adr;
1941 assert(valid(), "Usable");
1942 }
1944 // Following is used to create a temporary object during
1945 // the pattern match of an address expression.
1946 SWPointer::SWPointer(SWPointer* p) :
1947 _mem(p->_mem), _slp(p->_slp), _base(NULL), _adr(NULL),
1948 _scale(0), _offset(0), _invar(NULL), _negate_invar(false) {}
1950 //------------------------scaled_iv_plus_offset--------------------
1951 // Match: k*iv + offset
1952 // where: k is a constant that maybe zero, and
1953 // offset is (k2 [+/- invariant]) where k2 maybe zero and invariant is optional
1954 bool SWPointer::scaled_iv_plus_offset(Node* n) {
1955 if (scaled_iv(n)) {
1956 return true;
1957 }
1958 if (offset_plus_k(n)) {
1959 return true;
1960 }
1961 int opc = n->Opcode();
1962 if (opc == Op_AddI) {
1963 if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2))) {
1964 return true;
1965 }
1966 if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
1967 return true;
1968 }
1969 } else if (opc == Op_SubI) {
1970 if (scaled_iv(n->in(1)) && offset_plus_k(n->in(2), true)) {
1971 return true;
1972 }
1973 if (scaled_iv(n->in(2)) && offset_plus_k(n->in(1))) {
1974 _scale *= -1;
1975 return true;
1976 }
1977 }
1978 return false;
1979 }
1981 //----------------------------scaled_iv------------------------
1982 // Match: k*iv where k is a constant that's not zero
1983 bool SWPointer::scaled_iv(Node* n) {
1984 if (_scale != 0) {
1985 return false; // already found a scale
1986 }
1987 if (n == iv()) {
1988 _scale = 1;
1989 return true;
1990 }
1991 int opc = n->Opcode();
1992 if (opc == Op_MulI) {
1993 if (n->in(1) == iv() && n->in(2)->is_Con()) {
1994 _scale = n->in(2)->get_int();
1995 return true;
1996 } else if (n->in(2) == iv() && n->in(1)->is_Con()) {
1997 _scale = n->in(1)->get_int();
1998 return true;
1999 }
2000 } else if (opc == Op_LShiftI) {
2001 if (n->in(1) == iv() && n->in(2)->is_Con()) {
2002 _scale = 1 << n->in(2)->get_int();
2003 return true;
2004 }
2005 } else if (opc == Op_ConvI2L) {
2006 if (scaled_iv_plus_offset(n->in(1))) {
2007 return true;
2008 }
2009 } else if (opc == Op_LShiftL) {
2010 if (!has_iv() && _invar == NULL) {
2011 // Need to preserve the current _offset value, so
2012 // create a temporary object for this expression subtree.
2013 // Hacky, so should re-engineer the address pattern match.
2014 SWPointer tmp(this);
2015 if (tmp.scaled_iv_plus_offset(n->in(1))) {
2016 if (tmp._invar == NULL) {
2017 int mult = 1 << n->in(2)->get_int();
2018 _scale = tmp._scale * mult;
2019 _offset += tmp._offset * mult;
2020 return true;
2021 }
2022 }
2023 }
2024 }
2025 return false;
2026 }
2028 //----------------------------offset_plus_k------------------------
2029 // Match: offset is (k [+/- invariant])
2030 // where k maybe zero and invariant is optional, but not both.
2031 bool SWPointer::offset_plus_k(Node* n, bool negate) {
2032 int opc = n->Opcode();
2033 if (opc == Op_ConI) {
2034 _offset += negate ? -(n->get_int()) : n->get_int();
2035 return true;
2036 } else if (opc == Op_ConL) {
2037 // Okay if value fits into an int
2038 const TypeLong* t = n->find_long_type();
2039 if (t->higher_equal(TypeLong::INT)) {
2040 jlong loff = n->get_long();
2041 jint off = (jint)loff;
2042 _offset += negate ? -off : loff;
2043 return true;
2044 }
2045 return false;
2046 }
2047 if (_invar != NULL) return false; // already have an invariant
2048 if (opc == Op_AddI) {
2049 if (n->in(2)->is_Con() && invariant(n->in(1))) {
2050 _negate_invar = negate;
2051 _invar = n->in(1);
2052 _offset += negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
2053 return true;
2054 } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
2055 _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
2056 _negate_invar = negate;
2057 _invar = n->in(2);
2058 return true;
2059 }
2060 }
2061 if (opc == Op_SubI) {
2062 if (n->in(2)->is_Con() && invariant(n->in(1))) {
2063 _negate_invar = negate;
2064 _invar = n->in(1);
2065 _offset += !negate ? -(n->in(2)->get_int()) : n->in(2)->get_int();
2066 return true;
2067 } else if (n->in(1)->is_Con() && invariant(n->in(2))) {
2068 _offset += negate ? -(n->in(1)->get_int()) : n->in(1)->get_int();
2069 _negate_invar = !negate;
2070 _invar = n->in(2);
2071 return true;
2072 }
2073 }
2074 if (invariant(n)) {
2075 _negate_invar = negate;
2076 _invar = n;
2077 return true;
2078 }
2079 return false;
2080 }
2082 //----------------------------print------------------------
2083 void SWPointer::print() {
2084 #ifndef PRODUCT
2085 tty->print("base: %d adr: %d scale: %d offset: %d invar: %c%d\n",
2086 _base != NULL ? _base->_idx : 0,
2087 _adr != NULL ? _adr->_idx : 0,
2088 _scale, _offset,
2089 _negate_invar?'-':'+',
2090 _invar != NULL ? _invar->_idx : 0);
2091 #endif
2092 }
2094 // ========================= OrderedPair =====================
2096 const OrderedPair OrderedPair::initial;
2098 // ========================= SWNodeInfo =====================
2100 const SWNodeInfo SWNodeInfo::initial;
2103 // ============================ DepGraph ===========================
2105 //------------------------------make_node---------------------------
2106 // Make a new dependence graph node for an ideal node.
2107 DepMem* DepGraph::make_node(Node* node) {
2108 DepMem* m = new (_arena) DepMem(node);
2109 if (node != NULL) {
2110 assert(_map.at_grow(node->_idx) == NULL, "one init only");
2111 _map.at_put_grow(node->_idx, m);
2112 }
2113 return m;
2114 }
2116 //------------------------------make_edge---------------------------
2117 // Make a new dependence graph edge from dpred -> dsucc
2118 DepEdge* DepGraph::make_edge(DepMem* dpred, DepMem* dsucc) {
2119 DepEdge* e = new (_arena) DepEdge(dpred, dsucc, dsucc->in_head(), dpred->out_head());
2120 dpred->set_out_head(e);
2121 dsucc->set_in_head(e);
2122 return e;
2123 }
2125 // ========================== DepMem ========================
2127 //------------------------------in_cnt---------------------------
2128 int DepMem::in_cnt() {
2129 int ct = 0;
2130 for (DepEdge* e = _in_head; e != NULL; e = e->next_in()) ct++;
2131 return ct;
2132 }
2134 //------------------------------out_cnt---------------------------
2135 int DepMem::out_cnt() {
2136 int ct = 0;
2137 for (DepEdge* e = _out_head; e != NULL; e = e->next_out()) ct++;
2138 return ct;
2139 }
2141 //------------------------------print-----------------------------
2142 void DepMem::print() {
2143 #ifndef PRODUCT
2144 tty->print(" DepNode %d (", _node->_idx);
2145 for (DepEdge* p = _in_head; p != NULL; p = p->next_in()) {
2146 Node* pred = p->pred()->node();
2147 tty->print(" %d", pred != NULL ? pred->_idx : 0);
2148 }
2149 tty->print(") [");
2150 for (DepEdge* s = _out_head; s != NULL; s = s->next_out()) {
2151 Node* succ = s->succ()->node();
2152 tty->print(" %d", succ != NULL ? succ->_idx : 0);
2153 }
2154 tty->print_cr(" ]");
2155 #endif
2156 }
2158 // =========================== DepEdge =========================
2160 //------------------------------DepPreds---------------------------
2161 void DepEdge::print() {
2162 #ifndef PRODUCT
2163 tty->print_cr("DepEdge: %d [ %d ]", _pred->node()->_idx, _succ->node()->_idx);
2164 #endif
2165 }
2167 // =========================== DepPreds =========================
2168 // Iterator over predecessor edges in the dependence graph.
2170 //------------------------------DepPreds---------------------------
2171 DepPreds::DepPreds(Node* n, DepGraph& dg) {
2172 _n = n;
2173 _done = false;
2174 if (_n->is_Store() || _n->is_Load()) {
2175 _next_idx = MemNode::Address;
2176 _end_idx = n->req();
2177 _dep_next = dg.dep(_n)->in_head();
2178 } else if (_n->is_Mem()) {
2179 _next_idx = 0;
2180 _end_idx = 0;
2181 _dep_next = dg.dep(_n)->in_head();
2182 } else {
2183 _next_idx = 1;
2184 _end_idx = _n->req();
2185 _dep_next = NULL;
2186 }
2187 next();
2188 }
2190 //------------------------------next---------------------------
2191 void DepPreds::next() {
2192 if (_dep_next != NULL) {
2193 _current = _dep_next->pred()->node();
2194 _dep_next = _dep_next->next_in();
2195 } else if (_next_idx < _end_idx) {
2196 _current = _n->in(_next_idx++);
2197 } else {
2198 _done = true;
2199 }
2200 }
2202 // =========================== DepSuccs =========================
2203 // Iterator over successor edges in the dependence graph.
2205 //------------------------------DepSuccs---------------------------
2206 DepSuccs::DepSuccs(Node* n, DepGraph& dg) {
2207 _n = n;
2208 _done = false;
2209 if (_n->is_Load()) {
2210 _next_idx = 0;
2211 _end_idx = _n->outcnt();
2212 _dep_next = dg.dep(_n)->out_head();
2213 } else if (_n->is_Mem() || _n->is_Phi() && _n->bottom_type() == Type::MEMORY) {
2214 _next_idx = 0;
2215 _end_idx = 0;
2216 _dep_next = dg.dep(_n)->out_head();
2217 } else {
2218 _next_idx = 0;
2219 _end_idx = _n->outcnt();
2220 _dep_next = NULL;
2221 }
2222 next();
2223 }
2225 //-------------------------------next---------------------------
2226 void DepSuccs::next() {
2227 if (_dep_next != NULL) {
2228 _current = _dep_next->succ()->node();
2229 _dep_next = _dep_next->next_out();
2230 } else if (_next_idx < _end_idx) {
2231 _current = _n->raw_out(_next_idx++);
2232 } else {
2233 _done = true;
2234 }
2235 }