@@ -640,13 +640,51 @@ class SuperWord : public ResourceObj {
640640
641641// ------------------------------SWPointer---------------------------
642642// Information about an address for dependence checking and vector alignment
643+ //
644+ // We parse and represent pointers of the simple form:
645+ //
646+ // pointer = adr + offset + invar + scale * ConvI2L(iv)
647+ //
648+ // Where:
649+ //
650+ // adr: the base address of an array (base = adr)
651+ // OR
652+ // some address to off-heap memory (base = TOP)
653+ //
654+ // offset: a constant offset
655+ // invar: a runtime variable, which is invariant during the loop
656+ // scale: scaling factor
657+ // iv: loop induction variable
658+ //
659+ // But more precisely, we parse the composite-long-int form:
660+ //
661+ // pointer = adr + long_offset + long_invar + long_scale * ConvI2L(int_offset + inv_invar + int_scale * iv)
662+ //
663+ // pointer = adr + long_offset + long_invar + long_scale * ConvI2L(int_index)
664+ // int_index = int_offset + int_invar + int_scale * iv
665+ //
666+ // However, for aliasing and adjacency checks (e.g. SWPointer::cmp()) we always use the simple form to make
667+ // decisions. Hence, we must make sure to only create a "valid" SWPointer if the optimisations based on the
668+ // simple form produce the same result as the compound-long-int form would. Intuitively, this depends on
669+ // if the int_index overflows, but the precise conditions are given in SWPointer::is_safe_to_use_as_simple_form().
670+ //
671+ // ConvI2L(int_index) = ConvI2L(int_offset + int_invar + int_scale * iv)
672+ // = Convi2L(int_offset) + ConvI2L(int_invar) + ConvI2L(int_scale) * ConvI2L(iv)
673+ //
674+ // scale = long_scale * ConvI2L(int_scale)
675+ // offset = long_offset + long_scale * ConvI2L(int_offset)
676+ // invar = long_invar + long_scale * ConvI2L(int_invar)
677+ //
678+ // pointer = adr + offset + invar + scale * ConvI2L(iv)
679+ //
643680class SWPointer : public ArenaObj {
644681 protected:
645682 MemNode* _mem; // My memory reference node
646683 SuperWord* _slp; // SuperWord class
647684
648- Node* _base; // null if unsafe nonheap reference
649- Node* _adr; // address pointer
685+ // Components of the simple form:
686+ Node* _base; // Base address of an array OR null if some off-heap memory.
687+ Node* _adr; // Same as _base if an array pointer OR some off-heap memory pointer.
650688 int _scale; // multiplier for iv (in bytes), 0 if no loop iv
651689 int _offset; // constant offset (in bytes)
652690
@@ -657,6 +695,13 @@ class SWPointer : public ArenaObj {
657695 Node* _debug_invar_scale; // multiplier for invariant
658696#endif
659697
698+ // The int_index components of the compound-long-int form. Used to decide if it is safe to use the
699+ // simple form rather than the compound-long-int form that was parsed.
700+ bool _has_int_index_after_convI2L;
701+ int _int_index_after_convI2L_offset;
702+ Node* _int_index_after_convI2L_invar;
703+ int _int_index_after_convI2L_scale;
704+
660705 Node_Stack* _nstack; // stack used to record a swpointer trace of variants
661706 bool _analyze_only; // Used in loop unrolling only for swpointer trace
662707 uint _stack_idx; // Used in loop unrolling only for swpointer trace
@@ -675,6 +720,8 @@ class SWPointer : public ArenaObj {
675720 // Match: offset is (k [+/- invariant])
676721 bool offset_plus_k (Node* n, bool negate = false );
677722
723+ bool is_safe_to_use_as_simple_form (Node* base, Node* adr) const ;
724+
678725 public:
679726 enum CMP {
680727 Less = 1 ,
@@ -710,10 +757,43 @@ class SWPointer : public ArenaObj {
710757 return _invar == q._invar ;
711758 }
712759
760+ // We compute if and how two SWPointers can alias at runtime, i.e. if the two addressed regions of memory can
761+ // ever overlap. There are essentially 3 relevant return states:
762+ // - NotComparable: Synonymous to "unknown aliasing".
763+ // We have no information about how the two SWPointers can alias. They could overlap, refer
764+ // to another location in the same memory object, or point to a completely different object.
765+ // -> Memory edge required. Aliasing unlikely but possible.
766+ //
767+ // - Less / Greater: Synonymous to "never aliasing".
768+ // The two SWPointers may point into the same memory object, but be non-aliasing (i.e. we
769+ // know both address regions inside the same memory object, but these regions are non-
770+ // overlapping), or the SWPointers point to entirely different objects.
771+ // -> No memory edge required. Aliasing impossible.
772+ //
773+ // - Equal: Synonymous to "overlap, or point to different memory objects".
774+ // The two SWPointers either overlap on the same memory object, or point to two different
775+ // memory objects.
776+ // -> Memory edge required. Aliasing likely.
777+ //
778+ // In a future refactoring, we can simplify to two states:
779+ // - NeverAlias: instead of Less / Greater
780+ // - MayAlias: instead of Equal / NotComparable
781+ //
782+ // Two SWPointer are "comparable" (Less / Greater / Equal), iff all of these conditions apply:
783+ // 1) Both are valid, i.e. expressible in the compound-long-int or simple form.
784+ // 2) The adr are identical, or both are array bases of different arrays.
785+ // 3) They have identical scale.
786+ // 4) They have identical invar.
787+ // 5) The difference in offsets is limited: abs(offset0 - offset1) < 2^31.
713788 int cmp (SWPointer& q) {
714789 if (valid () && q.valid () &&
715790 (_adr == q._adr || (_base == _adr && q._base == q._adr )) &&
716791 _scale == q._scale && invar_equals (q)) {
792+ jlong difference = abs (java_subtract ((jlong)_offset, (jlong)q._offset ));
793+ jlong max_diff = (jlong)1 << 31 ;
794+ if (difference >= max_diff) {
795+ return NotComparable;
796+ }
717797 bool overlap = q._offset < _offset + memory_size () &&
718798 _offset < q._offset + q.memory_size ();
719799 return overlap ? Equal : (_offset < q._offset ? Less : Greater);
@@ -821,6 +901,12 @@ class SWPointer : public ArenaObj {
821901
822902 void maybe_add_to_invar (Node* new_invar, bool negate);
823903
904+ static bool try_AddI_no_overflow (int offset1, int offset2, int & result);
905+ static bool try_SubI_no_overflow (int offset1, int offset2, int & result);
906+ static bool try_AddSubI_no_overflow (int offset1, int offset2, bool is_sub, int & result);
907+ static bool try_LShiftI_no_overflow (int offset1, int offset2, int & result);
908+ static bool try_MulI_no_overflow (int offset1, int offset2, int & result);
909+
824910 Node* register_if_new (Node* n) const ;
825911};
826912
0 commit comments