Clean up tracking new roots, do not track them for CC.

This addresses comments from
    https://android-review.googlesource.com/321552

Test: ART_USE_READ_BARRIER=false testrunner.py -b --host
Test: ART_USE_READ_BARRIER=true testrunner.py -b --host
Bug: 30627598
Change-Id: I9740e599fe8170201a3b5f10113bbeb0dee500fe
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index d02cf17..1d95615 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -1908,6 +1908,13 @@
   const bool tracing_enabled = Trace::IsTracingEnabled();
   Thread* const self = Thread::Current();
   WriterMutexLock mu(self, *Locks::classlinker_classes_lock_);
+  if (kUseReadBarrier) {
+    // We do not track new roots for CC.
+    DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots |
+                          kVisitRootFlagClearRootLog |
+                          kVisitRootFlagStartLoggingNewRoots |
+                          kVisitRootFlagStopLoggingNewRoots));
+  }
   if ((flags & kVisitRootFlagAllRoots) != 0) {
     // Argument for how root visiting deals with ArtField and ArtMethod roots.
     // There is 3 GC cases to handle:
@@ -1937,7 +1944,7 @@
         root.VisitRoot(visitor, RootInfo(kRootVMInternal));
       }
     }
-  } else if ((flags & kVisitRootFlagNewRoots) != 0) {
+  } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) {
     for (auto& root : new_class_roots_) {
       ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>();
       root.VisitRoot(visitor, RootInfo(kRootStickyClass));
@@ -1958,13 +1965,13 @@
       }
     }
   }
-  if ((flags & kVisitRootFlagClearRootLog) != 0) {
+  if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) {
     new_class_roots_.clear();
     new_bss_roots_boot_oat_files_.clear();
   }
-  if ((flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
+  if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) {
     log_new_roots_ = true;
-  } else if ((flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
+  } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) {
     log_new_roots_ = false;
   }
   // We deliberately ignore the class roots in the image since we
@@ -3757,10 +3764,14 @@
 }
 
 void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) {
-  WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
-  DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation();
-  if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) {
-    new_bss_roots_boot_oat_files_.push_back(oat_file);
+  if (!kUseReadBarrier) {
+    WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_);
+    DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation();
+    if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) {
+      new_bss_roots_boot_oat_files_.push_back(oat_file);
+    }
+  } else {
+    LOG(FATAL) << "UNREACHABLE";
   }
 }
 
diff --git a/runtime/entrypoints/quick/quick_dexcache_entrypoints.cc b/runtime/entrypoints/quick/quick_dexcache_entrypoints.cc
index 699cf91..47c6b51 100644
--- a/runtime/entrypoints/quick/quick_dexcache_entrypoints.cc
+++ b/runtime/entrypoints/quick/quick_dexcache_entrypoints.cc
@@ -32,23 +32,33 @@
 namespace art {
 
 static inline void BssWriteBarrier(ArtMethod* outer_method) REQUIRES_SHARED(Locks::mutator_lock_) {
-  // For AOT code, we need a write barrier for the class loader that holds
-  // the GC roots in the .bss.
-  const DexFile* dex_file = outer_method->GetDexFile();
+  // For non-CC AOT code, we need a write barrier for the class loader that holds the
+  // GC roots in the .bss. For CC, we do not need to do anything because the roots
+  // we're storing are all referencing to-space and do not need to be re-visited.
+  // However, we do the DCHECK() for the registration of oat files with .bss sections.
+  const DexFile* dex_file =
+      (kUseReadBarrier && !kIsDebugBuild) ? nullptr : outer_method->GetDexFile();
   if (dex_file != nullptr &&
       dex_file->GetOatDexFile() != nullptr &&
       !dex_file->GetOatDexFile()->GetOatFile()->GetBssGcRoots().empty()) {
-    mirror::ClassLoader* class_loader = outer_method->GetClassLoader();
-    if (class_loader != nullptr) {
-      DCHECK(!class_loader->GetClassTable()->InsertOatFile(dex_file->GetOatDexFile()->GetOatFile()))
+    ObjPtr<mirror::ClassLoader> class_loader = outer_method->GetClassLoader();
+    if (kIsDebugBuild) {
+      ClassTable* class_table =
+          Runtime::Current()->GetClassLinker()->ClassTableForClassLoader(class_loader);
+      CHECK(class_table != nullptr &&
+            !class_table->InsertOatFile(dex_file->GetOatDexFile()->GetOatFile()))
           << "Oat file with .bss GC roots was not registered in class table: "
           << dex_file->GetOatDexFile()->GetOatFile()->GetLocation();
-      // Note that we emit the barrier before the compiled code stores the String or Class
-      // as a GC root. This is OK as there is no suspend point point in between.
-      Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader);
-    } else {
-      Runtime::Current()->GetClassLinker()->WriteBarrierForBootOatFileBssRoots(
-          dex_file->GetOatDexFile()->GetOatFile());
+    }
+    if (!kUseReadBarrier) {
+      if (class_loader != nullptr) {
+        // Note that we emit the barrier before the compiled code stores the String or Class
+        // as a GC root. This is OK as there is no suspend point point in between.
+        Runtime::Current()->GetHeap()->WriteBarrierEveryFieldOf(class_loader);
+      } else {
+        Runtime::Current()->GetClassLinker()->WriteBarrierForBootOatFileBssRoots(
+            dex_file->GetOatDexFile()->GetOatFile());
+      }
     }
   }
 }