@@ -807,24 +807,26 @@ void VPointer::maybe_add_to_invar(Node* new_invar, bool negate) {
807
807
_invar = register_if_new (add);
808
808
}
809
809
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
+
810
815
// To be in the same group, two VPointers must be the same,
811
816
// except for the offset.
812
817
int VPointer::cmp_for_sort_by_group (const VPointer** p1, const VPointer** p2) {
813
818
const VPointer* a = *p1;
814
819
const VPointer* b = *p2;
815
820
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 ());
821
824
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);
824
828
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
828
830
}
829
831
830
832
// 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) {
835
837
const VPointer* a = *p1;
836
838
const VPointer* b = *p2;
837
839
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
842
843
}
843
844
844
845
#ifndef PRODUCT
0 commit comments