< prev index next >

src/hotspot/share/ci/ciMethod.cpp

Print this page

        

@@ -460,10 +460,27 @@
 }
 #endif // COMPILER1
 
 
 // ------------------------------------------------------------------
+// ciMethod::saturated_add_int
+//
+// Update profile counters with saturating addition
+// Wil check and handle the overflow condition
+int ciMethod::saturated_add_int(int a, int b) {
+  jlong sum = a + b;
+  if (sum > max_jint) {
+    sum = max_jint;
+  } else if (sum < min_jint) {
+    sum = min_jint;
+  }
+
+  return (int)sum;
+}
+ 
+
+// ------------------------------------------------------------------
 // ciMethod::call_profile_at_bci
 //
 // Get the ciCallProfile for the invocation of this method.
 // Also reports receiver types for non-call type checks (if TypeProfileCasts).
 ciCallProfile ciMethod::call_profile_at_bci(int bci) {

@@ -500,13 +517,13 @@
           }
         }
         for (uint i = 0; i < call->row_limit(); i++) {
           ciKlass* receiver = call->receiver(i);
           if (receiver == NULL)  continue;
-          int rcount = call->receiver_count(i) + epsilon;
+          int rcount = saturated_add_int(call->receiver_count(i), epsilon);
           if (rcount == 0) rcount = 1; // Should be valid value
-          receivers_count_total += rcount;
+          receivers_count_total = saturated_add_int(receivers_count_total, rcount);
           // Add the receiver to result data.
           result.add_receiver(receiver, rcount);
           // If we extend profiling to record methods,
           // we will set result._method also.
         }

@@ -532,11 +549,11 @@
         // Make the count consistent if this is a call profile. If count is
         // zero or less, presume that this is a typecheck profile and
         // do nothing.  Otherwise, increase count to be the sum of all
         // receiver's counts.
         if (count >= 0) {
-          count += receivers_count_total;
+          count = saturated_add_int(count, receivers_count_total);
         }
       }
       result._count = count;
     }
   }
< prev index next >