Make VERIFY_HEAP-enabled verification depend on --verify-heap

There were a few places where we would do such verification even
without --verify-heap. The CL changes these to be in line with all
the rest.

Change-Id: Ia43708104c7d7818dc8d41d645a84f9b5e7446a8
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2336796
Auto-Submit: Georg Neis <neis@chromium.org>
Reviewed-by: Jakob Gruber <jgruber@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Reviewed-by: Hannes Payer <hpayer@chromium.org>
Commit-Queue: Hannes Payer <hpayer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69269}
diff --git a/src/heap/factory-base.cc b/src/heap/factory-base.cc
index 5dd88f9..433c655 100644
--- a/src/heap/factory-base.cc
+++ b/src/heap/factory-base.cc
@@ -334,7 +334,7 @@
   shared->set_kind(kind);
 
 #ifdef VERIFY_HEAP
-  shared->SharedFunctionInfoVerify(isolate());
+  if (FLAG_verify_heap) shared->SharedFunctionInfoVerify(isolate());
 #endif  // VERIFY_HEAP
   return shared;
 }
@@ -644,7 +644,7 @@
   shared->Init(read_only_roots(), unique_id);
 
 #ifdef VERIFY_HEAP
-  shared->SharedFunctionInfoVerify(isolate());
+  if (FLAG_verify_heap) shared->SharedFunctionInfoVerify(isolate());
 #endif  // VERIFY_HEAP
   return shared;
 }
diff --git a/src/heap/mark-compact.cc b/src/heap/mark-compact.cc
index 988efff..6d1e754 100644
--- a/src/heap/mark-compact.cc
+++ b/src/heap/mark-compact.cc
@@ -1364,7 +1364,7 @@
   inline bool TryEvacuateObject(AllocationSpace target_space, HeapObject object,
                                 int size, HeapObject* target_object) {
 #ifdef VERIFY_HEAP
-    if (AbortCompactionForTesting(object)) return false;
+    if (FLAG_verify_heap && AbortCompactionForTesting(object)) return false;
 #endif  // VERIFY_HEAP
     AllocationAlignment alignment = HeapObject::RequiredAlignment(object.map());
     AllocationResult allocation = local_allocator_->Allocate(
@@ -2448,12 +2448,13 @@
     for (InternalIndex i : table.IterateEntries()) {
       HeapObject key = HeapObject::cast(table.KeyAt(i));
 #ifdef VERIFY_HEAP
-      Object value = table.ValueAt(i);
-
-      if (value.IsHeapObject()) {
-        CHECK_IMPLIES(
-            non_atomic_marking_state()->IsBlackOrGrey(key),
-            non_atomic_marking_state()->IsBlackOrGrey(HeapObject::cast(value)));
+      if (FLAG_verify_heap) {
+        Object value = table.ValueAt(i);
+        if (value.IsHeapObject()) {
+          CHECK_IMPLIES(non_atomic_marking_state()->IsBlackOrGrey(key),
+                        non_atomic_marking_state()->IsBlackOrGrey(
+                            HeapObject::cast(value)));
+        }
       }
 #endif
       if (!non_atomic_marking_state()->IsBlackOrGrey(key)) {
diff --git a/src/objects/objects-inl.h b/src/objects/objects-inl.h
index 42db63d..59f2e77 100644
--- a/src/objects/objects-inl.h
+++ b/src/objects/objects-inl.h
@@ -699,11 +699,11 @@
 DEF_GETTER(HeapObject, map, Map) { return map_word(isolate).ToMap(); }
 
 void HeapObject::set_map(Map value) {
-  if (!value.is_null()) {
 #ifdef VERIFY_HEAP
+  if (FLAG_verify_heap && !value.is_null()) {
     GetHeapFromWritableObject(*this)->VerifyObjectLayoutChange(*this, value);
-#endif
   }
+#endif
   set_map_word(MapWord::FromMap(value));
 #ifndef V8_DISABLE_WRITE_BARRIERS
   if (!value.is_null()) {
@@ -719,11 +719,11 @@
 }
 
 void HeapObject::synchronized_set_map(Map value) {
-  if (!value.is_null()) {
 #ifdef VERIFY_HEAP
+  if (FLAG_verify_heap && !value.is_null()) {
     GetHeapFromWritableObject(*this)->VerifyObjectLayoutChange(*this, value);
-#endif
   }
+#endif
   synchronized_set_map_word(MapWord::FromMap(value));
 #ifndef V8_DISABLE_WRITE_BARRIERS
   if (!value.is_null()) {
@@ -736,11 +736,11 @@
 
 // Unsafe accessor omitting write barrier.
 void HeapObject::set_map_no_write_barrier(Map value) {
-  if (!value.is_null()) {
 #ifdef VERIFY_HEAP
+  if (FLAG_verify_heap && !value.is_null()) {
     GetHeapFromWritableObject(*this)->VerifyObjectLayoutChange(*this, value);
-#endif
   }
+#endif
   set_map_word(MapWord::FromMap(value));
 }
 
diff --git a/src/objects/shared-function-info.h b/src/objects/shared-function-info.h
index bf20a3e..be1ace2 100644
--- a/src/objects/shared-function-info.h
+++ b/src/objects/shared-function-info.h
@@ -606,7 +606,9 @@
   // Dispatched behavior.
   DECL_PRINTER(SharedFunctionInfo)
   DECL_VERIFIER(SharedFunctionInfo)
+#ifdef VERIFY_HEAP
   void SharedFunctionInfoVerify(OffThreadIsolate* isolate);
+#endif
 #ifdef OBJECT_PRINT
   void PrintSourceCode(std::ostream& os);
 #endif
@@ -656,7 +658,9 @@
   inline bool needs_home_object() const;
 
  private:
+#ifdef VERIFY_HEAP
   void SharedFunctionInfoVerify(ReadOnlyRoots roots);
+#endif
 
   // [name_or_scope_info]: Function name string, kNoSharedNameSentinel or
   // ScopeInfo.
diff --git a/src/snapshot/snapshot.cc b/src/snapshot/snapshot.cc
index fd08666..f99b9df1 100644
--- a/src/snapshot/snapshot.cc
+++ b/src/snapshot/snapshot.cc
@@ -312,7 +312,7 @@
     CHECK(new_native_context->IsNativeContext());
 
 #ifdef VERIFY_HEAP
-    new_isolate->heap()->Verify();
+    if (FLAG_verify_heap) new_isolate->heap()->Verify();
 #endif  // VERIFY_HEAP
   }
   new_isolate->Exit();