Clear DexCache MethodTypes and CallSites in ImageWriter.

And clean up the rest of ImageWriter::ClearDexCache().

Test: m
Change-Id: I975ce128d1ef7a14e972a17c70f1dee2b55914f6
diff --git a/dex2oat/linker/image_writer.cc b/dex2oat/linker/image_writer.cc
index ff4c7b2..fbdffa8 100644
--- a/dex2oat/linker/image_writer.cc
+++ b/dex2oat/linker/image_writer.cc
@@ -1243,36 +1243,43 @@
   // Clear methods.
   mirror::MethodDexCacheType* resolved_methods = dex_cache->GetResolvedMethods();
   for (size_t slot_idx = 0, num = dex_cache->NumResolvedMethods(); slot_idx != num; ++slot_idx) {
-    auto pair =
-        mirror::DexCache::GetNativePairPtrSize(resolved_methods, slot_idx, target_ptr_size_);
-    if (pair.object != nullptr) {
-      dex_cache->ClearResolvedMethod(pair.index, target_ptr_size_);
-    }
+    mirror::MethodDexCachePair invalid(nullptr,
+                                       mirror::MethodDexCachePair::InvalidIndexForSlot(slot_idx));
+    mirror::DexCache::SetNativePairPtrSize(resolved_methods, slot_idx, invalid, target_ptr_size_);
   }
   // Clear fields.
   mirror::FieldDexCacheType* resolved_fields = dex_cache->GetResolvedFields();
   for (size_t slot_idx = 0, num = dex_cache->NumResolvedFields(); slot_idx != num; ++slot_idx) {
-    auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_fields, slot_idx, target_ptr_size_);
-    if (pair.object != nullptr) {
-      dex_cache->ClearResolvedField(pair.index, target_ptr_size_);
-    }
+    mirror::FieldDexCachePair invalid(nullptr,
+                                      mirror::FieldDexCachePair::InvalidIndexForSlot(slot_idx));
+    mirror::DexCache::SetNativePairPtrSize(resolved_fields, slot_idx, invalid, target_ptr_size_);
   }
   // Clear types.
+  mirror::TypeDexCacheType* resolved_types = dex_cache->GetResolvedTypes();
   for (size_t slot_idx = 0, num = dex_cache->NumResolvedTypes(); slot_idx != num; ++slot_idx) {
-    mirror::TypeDexCachePair pair =
-        dex_cache->GetResolvedTypes()[slot_idx].load(std::memory_order_relaxed);
-    if (!pair.object.IsNull()) {
-      dex_cache->ClearResolvedType(dex::TypeIndex(pair.index));
-    }
+    mirror::TypeDexCachePair invalid(nullptr,
+                                     mirror::TypeDexCachePair::InvalidIndexForSlot(slot_idx));
+    resolved_types[slot_idx].store(invalid, std::memory_order_relaxed);
   }
   // Clear strings.
+  mirror::StringDexCacheType* resolved_strings = dex_cache->GetStrings();
   for (size_t slot_idx = 0, num = dex_cache->NumStrings(); slot_idx != num; ++slot_idx) {
-    mirror::StringDexCachePair pair =
-        dex_cache->GetStrings()[slot_idx].load(std::memory_order_relaxed);
-    if (!pair.object.IsNull()) {
-      dex_cache->ClearString(dex::StringIndex(pair.index));
-    }
+    mirror::StringDexCachePair invalid(nullptr,
+                                       mirror::StringDexCachePair::InvalidIndexForSlot(slot_idx));
+    resolved_strings[slot_idx].store(invalid, std::memory_order_relaxed);
   }
+  // Clear method types.
+  mirror::MethodTypeDexCacheType* resolved_method_types = dex_cache->GetResolvedMethodTypes();
+  size_t num_resolved_method_types = dex_cache->NumResolvedMethodTypes();
+  for (size_t slot_idx = 0; slot_idx != num_resolved_method_types; ++slot_idx) {
+    mirror::MethodTypeDexCachePair invalid(
+        nullptr, mirror::MethodTypeDexCachePair::InvalidIndexForSlot(slot_idx));
+    resolved_method_types[slot_idx].store(invalid, std::memory_order_relaxed);
+  }
+  // Clear call sites.
+  std::fill_n(dex_cache->GetResolvedCallSites(),
+              dex_cache->NumResolvedCallSites(),
+              GcRoot<mirror::CallSite>(nullptr));
 }
 
 void ImageWriter::PreloadDexCache(ObjPtr<mirror::DexCache> dex_cache,
diff --git a/runtime/mirror/dex_cache-inl.h b/runtime/mirror/dex_cache-inl.h
index d5e1362..010c5a5 100644
--- a/runtime/mirror/dex_cache-inl.h
+++ b/runtime/mirror/dex_cache-inl.h
@@ -257,18 +257,6 @@
   SetNativePairPtrSize(GetResolvedFields(), FieldSlotIndex(field_idx), pair, ptr_size);
 }
 
-inline void DexCache::ClearResolvedField(uint32_t field_idx, PointerSize ptr_size) {
-  DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
-  uint32_t slot_idx = FieldSlotIndex(field_idx);
-  auto* resolved_fields = GetResolvedFields();
-  // This is racy but should only be called from the single-threaded ImageWriter.
-  DCHECK(Runtime::Current()->IsAotCompiler());
-  if (GetNativePairPtrSize(resolved_fields, slot_idx, ptr_size).index == field_idx) {
-    FieldDexCachePair cleared(nullptr, FieldDexCachePair::InvalidIndexForSlot(slot_idx));
-    SetNativePairPtrSize(resolved_fields, slot_idx, cleared, ptr_size);
-  }
-}
-
 inline uint32_t DexCache::MethodSlotIndex(uint32_t method_idx) {
   DCHECK_LT(method_idx, GetDexFile()->NumMethodIds());
   const uint32_t slot_idx = method_idx % kDexCacheMethodCacheSize;
@@ -291,18 +279,6 @@
   SetNativePairPtrSize(GetResolvedMethods(), MethodSlotIndex(method_idx), pair, ptr_size);
 }
 
-inline void DexCache::ClearResolvedMethod(uint32_t method_idx, PointerSize ptr_size) {
-  DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), ptr_size);
-  uint32_t slot_idx = MethodSlotIndex(method_idx);
-  auto* resolved_methods = GetResolvedMethods();
-  // This is racy but should only be called from the single-threaded ImageWriter.
-  DCHECK(Runtime::Current()->IsAotCompiler());
-  if (GetNativePairPtrSize(resolved_methods, slot_idx, ptr_size).index == method_idx) {
-    MethodDexCachePair cleared(nullptr, MethodDexCachePair::InvalidIndexForSlot(slot_idx));
-    SetNativePairPtrSize(resolved_methods, slot_idx, cleared, ptr_size);
-  }
-}
-
 template <typename T>
 NativeDexCachePair<T> DexCache::GetNativePairPtrSize(std::atomic<NativeDexCachePair<T>>* pair_array,
                                                      size_t idx,
diff --git a/runtime/mirror/dex_cache.h b/runtime/mirror/dex_cache.h
index 6067c76..80cca4e 100644
--- a/runtime/mirror/dex_cache.h
+++ b/runtime/mirror/dex_cache.h
@@ -305,8 +305,6 @@
                                        ArtMethod* resolved,
                                        PointerSize ptr_size)
       REQUIRES_SHARED(Locks::mutator_lock_);
-  ALWAYS_INLINE void ClearResolvedMethod(uint32_t method_idx, PointerSize ptr_size)
-      REQUIRES_SHARED(Locks::mutator_lock_);
 
   // Pointer sized variant, used for patching.
   ALWAYS_INLINE ArtField* GetResolvedField(uint32_t idx, PointerSize ptr_size)
@@ -315,8 +313,6 @@
   // Pointer sized variant, used for patching.
   ALWAYS_INLINE void SetResolvedField(uint32_t idx, ArtField* field, PointerSize ptr_size)
       REQUIRES_SHARED(Locks::mutator_lock_);
-  ALWAYS_INLINE void ClearResolvedField(uint32_t idx, PointerSize ptr_size)
-      REQUIRES_SHARED(Locks::mutator_lock_);
 
   MethodType* GetResolvedMethodType(dex::ProtoIndex proto_idx) REQUIRES_SHARED(Locks::mutator_lock_);