OSDN Git Service

Revert "Revert "Check if we require barrier if we did not resolve classes""
authorMathieu Chartier <mathieuc@google.com>
Thu, 7 Apr 2016 17:52:52 +0000 (10:52 -0700)
committerMathieu Chartier <mathieuc@google.com>
Thu, 7 Apr 2016 17:52:52 +0000 (10:52 -0700)
This reverts commit a7ab4997f7263439561093ffbc7dea29181a47c5.

compiler/driver/compiler_driver.cc
compiler/driver/compiler_driver.h

index 22e35ad..5fe81c7 100644 (file)
@@ -358,6 +358,7 @@ CompilerDriver::CompilerDriver(
       instruction_set_(instruction_set),
       instruction_set_features_(instruction_set_features),
       no_barrier_constructor_classes_lock_("freezing constructor lock"),
+      resolved_classes_(false),
       compiled_classes_lock_("compiled classes lock"),
       compiled_methods_lock_("compiled method lock"),
       compiled_methods_(MethodTable::key_compare()),
@@ -712,6 +713,8 @@ void CompilerDriver::Resolve(jobject class_loader,
                    resolve_thread_count,
                    timings);
   }
+
+  resolved_classes_ = true;
 }
 
 // Resolve const-strings in the code. Done to have deterministic allocation behavior. Right now
@@ -2006,6 +2009,28 @@ static void CheckAndClearResolveException(Thread* self)
   self->ClearException();
 }
 
+bool CompilerDriver::RequiresConstructorBarrier(const DexFile& dex_file,
+                                                uint16_t class_def_idx) const {
+  const DexFile::ClassDef& class_def = dex_file.GetClassDef(class_def_idx);
+  const uint8_t* class_data = dex_file.GetClassData(class_def);
+  if (class_data == nullptr) {
+    // Empty class such as a marker interface.
+    return false;
+  }
+  ClassDataItemIterator it(dex_file, class_data);
+  while (it.HasNextStaticField()) {
+    it.Next();
+  }
+  // We require a constructor barrier if there are final instance fields.
+  while (it.HasNextInstanceField()) {
+    if (it.MemberIsFinal()) {
+      return true;
+    }
+    it.Next();
+  }
+  return false;
+}
+
 class ResolveClassFieldsAndMethodsVisitor : public CompilationVisitor {
  public:
   explicit ResolveClassFieldsAndMethodsVisitor(const ParallelCompilationManager* manager)
@@ -2779,8 +2804,11 @@ void CompilerDriver::AddRequiresNoConstructorBarrier(Thread* self,
 bool CompilerDriver::RequiresConstructorBarrier(Thread* self,
                                                 const DexFile* dex_file,
                                                 uint16_t class_def_index) const {
-  ReaderMutexLock mu(self, no_barrier_constructor_classes_lock_);
-  return no_barrier_constructor_classes_.count(ClassReference(dex_file, class_def_index)) == 0;
+  if (resolved_classes_) {
+    ReaderMutexLock mu(self, no_barrier_constructor_classes_lock_);
+    return no_barrier_constructor_classes_.count(ClassReference(dex_file, class_def_index)) == 0;
+  }
+  return RequiresConstructorBarrier(*dex_file, class_def_index);
 }
 
 std::string CompilerDriver::GetMemoryUsageString(bool extended) const {
index 98e3d89..0ed0bb6 100644 (file)
@@ -619,6 +619,8 @@ class CompilerDriver {
   void FreeThreadPools();
   void CheckThreadPools();
 
+  bool RequiresConstructorBarrier(const DexFile& dex_file, uint16_t class_def_idx) const;
+
   const CompilerOptions* const compiler_options_;
   VerificationResults* const verification_results_;
   DexFileToMethodInlinerMap* const method_inliner_map_;
@@ -629,10 +631,14 @@ class CompilerDriver {
   const InstructionSet instruction_set_;
   const InstructionSetFeatures* const instruction_set_features_;
 
-  // All class references that do not require constructor barriers
+  // All class references that do not require constructor barriers. Only filled in if
+  // resolved_classes_ is true.
   mutable ReaderWriterMutex no_barrier_constructor_classes_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
   std::set<ClassReference> no_barrier_constructor_classes_
       GUARDED_BY(no_barrier_constructor_classes_lock_);
+  // resolved_classes_ is true if we performed the resolve phase and filled in
+  // no_barrier_constructor_classes_.
+  bool resolved_classes_;
 
   typedef SafeMap<const ClassReference, CompiledClass*> ClassTable;
   // All class references that this compiler has compiled.