@@ -807,24 +807,26 @@ void VPointer::maybe_add_to_invar(Node* new_invar, bool negate) {
807807 _invar = register_if_new (add);
808808}
809809
810+ // We use two comparisons, because a subtraction could underflow.
811+ #define RETURN_CMP_VALUE_IF_NOT_EQUAL (a, b ) \
812+ if (a < b) { return -1 ; } \
813+ if (a > b) { return 1 ; }
814+
810815// To be in the same group, two VPointers must be the same,
811816// except for the offset.
812817int VPointer::cmp_for_sort_by_group (const VPointer** p1, const VPointer** p2) {
813818 const VPointer* a = *p1;
814819 const VPointer* b = *p2;
815820
816- int cmp_base = a->base ()->_idx - b->base ()->_idx ;
817- if (cmp_base != 0 ) { return cmp_base; }
818-
819- int cmp_opcode = a->mem ()->Opcode () - b->mem ()->Opcode ();
820- if (cmp_opcode != 0 ) { return cmp_opcode; }
821+ RETURN_CMP_VALUE_IF_NOT_EQUAL (a->base ()->_idx , b->base ()->_idx );
822+ RETURN_CMP_VALUE_IF_NOT_EQUAL (a->mem ()->Opcode (), b->mem ()->Opcode ());
823+ RETURN_CMP_VALUE_IF_NOT_EQUAL (a->scale_in_bytes (), b->scale_in_bytes ());
821824
822- int cmp_scale = a->scale_in_bytes () - b->scale_in_bytes ();
823- if (cmp_scale != 0 ) { return cmp_scale; }
825+ int a_inva_idx = a->invar () == nullptr ? 0 : a->invar ()->_idx ;
826+ int b_inva_idx = b->invar () == nullptr ? 0 : b->invar ()->_idx ;
827+ RETURN_CMP_VALUE_IF_NOT_EQUAL (a_inva_idx, b_inva_idx);
824828
825- int cmp_invar = (a->invar () == nullptr ? 0 : a->invar ()->_idx ) -
826- (b->invar () == nullptr ? 0 : b->invar ()->_idx );
827- return cmp_invar;
829+ return 0 ; // equal
828830}
829831
830832// We compare by group, then by offset, and finally by node idx.
@@ -835,10 +837,9 @@ int VPointer::cmp_for_sort(const VPointer** p1, const VPointer** p2) {
835837 const VPointer* a = *p1;
836838 const VPointer* b = *p2;
837839
838- int cmp_offset = a->offset_in_bytes () - b->offset_in_bytes ();
839- if (cmp_offset != 0 ) { return cmp_offset; }
840-
841- return a->mem ()->_idx - b->mem ()->_idx ;
840+ RETURN_CMP_VALUE_IF_NOT_EQUAL (a->offset_in_bytes (), b->offset_in_bytes ());
841+ RETURN_CMP_VALUE_IF_NOT_EQUAL (a->mem ()->_idx , b->mem ()->_idx );
842+ return 0 ; // equal
842843}
843844
844845#ifndef PRODUCT
0 commit comments