Move compiler code out of method verifier.
We want to detect small methods for inlining at the end of
the method verification. Instead of adding more compiler
code to the runtime, we create a callback from the runtime
into the compiler, so that we can keep the code there.
Additionally, we move the compiler-related code that was
already in the method verifier to the compiler since it
doesn't really belong to the runtime in the first place.
Change-Id: I708ca13227c809e07917ff3879a89722017e83a9
diff --git a/runtime/verifier/method_verifier.cc b/runtime/verifier/method_verifier.cc
index 59ead89..f03cdcd 100644
--- a/runtime/verifier/method_verifier.cc
+++ b/runtime/verifier/method_verifier.cc
@@ -22,6 +22,7 @@
#include "base/mutex-inl.h"
#include "base/stringpiece.h"
#include "class_linker.h"
+#include "compiler_callbacks.h"
#include "dex_file-inl.h"
#include "dex_instruction-inl.h"
#include "dex_instruction_visitor.h"
@@ -110,7 +111,7 @@
*error = "Verifier rejected class " + PrettyDescriptor(klass) + failure_message;
if (Runtime::Current()->IsCompiler()) {
ClassReference ref(&dex_file, klass->GetDexClassDefIndex());
- AddRejectedClass(ref);
+ Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
}
return kHardFailure;
}
@@ -441,7 +442,12 @@
// Perform static instruction verification.
result = result && VerifyInstructions();
// Perform code-flow analysis and return.
- return result && VerifyCodeFlow();
+ result = result && VerifyCodeFlow();
+ // Compute information for compiler.
+ if (result && Runtime::Current()->IsCompiler()) {
+ result = Runtime::Current()->GetCompilerCallbacks()->MethodVerified(this);
+ }
+ return result;
}
std::ostream& MethodVerifier::Fail(VerifyError error) {
@@ -480,7 +486,7 @@
case VERIFY_ERROR_BAD_CLASS_HARD: {
if (Runtime::Current()->IsCompiler()) {
ClassReference ref(dex_file_, dex_file_->GetIndexForClassDef(*class_def_));
- AddRejectedClass(ref);
+ Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref);
}
have_pending_hard_failure_ = true;
break;
@@ -1063,38 +1069,6 @@
DCHECK_NE(failures_.size(), 0U);
return false;
}
-
- // Compute information for compiler.
- if (Runtime::Current()->IsCompiler()) {
- MethodReference ref(dex_file_, dex_method_idx_);
- bool compile = IsCandidateForCompilation(ref, method_access_flags_);
- if (compile) {
- /* Generate a register map and add it to the method. */
- const std::vector<uint8_t>* dex_gc_map = GenerateGcMap();
- if (dex_gc_map == NULL) {
- DCHECK_NE(failures_.size(), 0U);
- return false; // Not a real failure, but a failure to encode
- }
- if (kIsDebugBuild) {
- VerifyGcMap(*dex_gc_map);
- }
- verifier::MethodVerifier::SetDexGcMap(ref, dex_gc_map);
- }
-
- if (has_check_casts_) {
- MethodVerifier::MethodSafeCastSet* method_to_safe_casts = GenerateSafeCastSet();
- if (method_to_safe_casts != NULL) {
- SetSafeCastMap(ref, method_to_safe_casts);
- }
- }
-
- if (has_virtual_or_interface_invokes_) {
- MethodVerifier::PcToConcreteMethodMap* pc_to_concrete_method = GenerateDevirtMap();
- if (pc_to_concrete_method != NULL) {
- SetDevirtMap(ref, pc_to_concrete_method);
- }
- }
- }
return true;
}
@@ -3910,325 +3884,6 @@
return *declaring_class_;
}
-void MethodVerifier::ComputeGcMapSizes(size_t* gc_points, size_t* ref_bitmap_bits,
- size_t* log2_max_gc_pc) {
- size_t local_gc_points = 0;
- size_t max_insn = 0;
- size_t max_ref_reg = -1;
- for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
- if (insn_flags_[i].IsCompileTimeInfoPoint()) {
- local_gc_points++;
- max_insn = i;
- RegisterLine* line = reg_table_.GetLine(i);
- max_ref_reg = line->GetMaxNonZeroReferenceReg(max_ref_reg);
- }
- }
- *gc_points = local_gc_points;
- *ref_bitmap_bits = max_ref_reg + 1; // if max register is 0 we need 1 bit to encode (ie +1)
- size_t i = 0;
- while ((1U << i) <= max_insn) {
- i++;
- }
- *log2_max_gc_pc = i;
-}
-
-MethodVerifier::MethodSafeCastSet* MethodVerifier::GenerateSafeCastSet() {
- /*
- * Walks over the method code and adds any cast instructions in which
- * the type cast is implicit to a set, which is used in the code generation
- * to elide these casts.
- */
- if (!failure_messages_.empty()) {
- return NULL;
- }
- UniquePtr<MethodSafeCastSet> mscs;
- const Instruction* inst = Instruction::At(code_item_->insns_);
- const Instruction* end = Instruction::At(code_item_->insns_ +
- code_item_->insns_size_in_code_units_);
-
- for (; inst < end; inst = inst->Next()) {
- Instruction::Code code = inst->Opcode();
- if ((code == Instruction::CHECK_CAST) || (code == Instruction::APUT_OBJECT)) {
- uint32_t dex_pc = inst->GetDexPc(code_item_->insns_);
- RegisterLine* line = reg_table_.GetLine(dex_pc);
- bool is_safe_cast = false;
- if (code == Instruction::CHECK_CAST) {
- const RegType& reg_type(line->GetRegisterType(inst->VRegA_21c()));
- const RegType& cast_type = ResolveClassAndCheckAccess(inst->VRegB_21c());
- is_safe_cast = cast_type.IsStrictlyAssignableFrom(reg_type);
- } else {
- const RegType& array_type(line->GetRegisterType(inst->VRegB_23x()));
- // We only know its safe to assign to an array if the array type is precise. For example,
- // an Object[] can have any type of object stored in it, but it may also be assigned a
- // String[] in which case the stores need to be of Strings.
- if (array_type.IsPreciseReference()) {
- const RegType& value_type(line->GetRegisterType(inst->VRegA_23x()));
- const RegType& component_type(reg_types_.GetComponentType(array_type,
- class_loader_->get()));
- is_safe_cast = component_type.IsStrictlyAssignableFrom(value_type);
- }
- }
- if (is_safe_cast) {
- if (mscs.get() == NULL) {
- mscs.reset(new MethodSafeCastSet());
- }
- mscs->insert(dex_pc);
- }
- }
- }
- return mscs.release();
-}
-
-MethodVerifier::PcToConcreteMethodMap* MethodVerifier::GenerateDevirtMap() {
- // It is risky to rely on reg_types for sharpening in cases of soft
- // verification, we might end up sharpening to a wrong implementation. Just abort.
- if (!failure_messages_.empty()) {
- return NULL;
- }
-
- UniquePtr<PcToConcreteMethodMap> pc_to_concrete_method_map;
- const uint16_t* insns = code_item_->insns_;
- const Instruction* inst = Instruction::At(insns);
- const Instruction* end = Instruction::At(insns + code_item_->insns_size_in_code_units_);
-
- for (; inst < end; inst = inst->Next()) {
- bool is_virtual = (inst->Opcode() == Instruction::INVOKE_VIRTUAL) ||
- (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE);
- bool is_interface = (inst->Opcode() == Instruction::INVOKE_INTERFACE) ||
- (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
-
- if (!is_interface && !is_virtual) {
- continue;
- }
- // Get reg type for register holding the reference to the object that will be dispatched upon.
- uint32_t dex_pc = inst->GetDexPc(insns);
- RegisterLine* line = reg_table_.GetLine(dex_pc);
- bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE) ||
- (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
- const RegType&
- reg_type(line->GetRegisterType(is_range ? inst->VRegC_3rc() : inst->VRegC_35c()));
-
- if (!reg_type.HasClass()) {
- // We will compute devirtualization information only when we know the Class of the reg type.
- continue;
- }
- mirror::Class* reg_class = reg_type.GetClass();
- if (reg_class->IsInterface()) {
- // We can't devirtualize when the known type of the register is an interface.
- continue;
- }
- if (reg_class->IsAbstract() && !reg_class->IsArrayClass()) {
- // We can't devirtualize abstract classes except on arrays of abstract classes.
- continue;
- }
- mirror::ArtMethod* abstract_method = (*dex_cache_)->GetResolvedMethod(
- is_range ? inst->VRegB_3rc() : inst->VRegB_35c());
- if (abstract_method == NULL) {
- // If the method is not found in the cache this means that it was never found
- // by ResolveMethodAndCheckAccess() called when verifying invoke_*.
- continue;
- }
- // Find the concrete method.
- mirror::ArtMethod* concrete_method = NULL;
- if (is_interface) {
- concrete_method = reg_type.GetClass()->FindVirtualMethodForInterface(abstract_method);
- }
- if (is_virtual) {
- concrete_method = reg_type.GetClass()->FindVirtualMethodForVirtual(abstract_method);
- }
- if (concrete_method == NULL || concrete_method->IsAbstract()) {
- // In cases where concrete_method is not found, or is abstract, continue to the next invoke.
- continue;
- }
- if (reg_type.IsPreciseReference() || concrete_method->IsFinal() ||
- concrete_method->GetDeclaringClass()->IsFinal()) {
- // If we knew exactly the class being dispatched upon, or if the target method cannot be
- // overridden record the target to be used in the compiler driver.
- if (pc_to_concrete_method_map.get() == NULL) {
- pc_to_concrete_method_map.reset(new PcToConcreteMethodMap());
- }
- MethodReference concrete_ref(
- concrete_method->GetDeclaringClass()->GetDexCache()->GetDexFile(),
- concrete_method->GetDexMethodIndex());
- pc_to_concrete_method_map->Put(dex_pc, concrete_ref);
- }
- }
- return pc_to_concrete_method_map.release();
-}
-
-const std::vector<uint8_t>* MethodVerifier::GenerateGcMap() {
- size_t num_entries, ref_bitmap_bits, pc_bits;
- ComputeGcMapSizes(&num_entries, &ref_bitmap_bits, &pc_bits);
- // There's a single byte to encode the size of each bitmap
- if (ref_bitmap_bits >= (8 /* bits per byte */ * 8192 /* 13-bit size */ )) {
- // TODO: either a better GC map format or per method failures
- Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
- << ref_bitmap_bits << " registers";
- return NULL;
- }
- size_t ref_bitmap_bytes = (ref_bitmap_bits + 7) / 8;
- // There are 2 bytes to encode the number of entries
- if (num_entries >= 65536) {
- // TODO: either a better GC map format or per method failures
- Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
- << num_entries << " entries";
- return NULL;
- }
- size_t pc_bytes;
- RegisterMapFormat format;
- if (pc_bits <= 8) {
- format = kRegMapFormatCompact8;
- pc_bytes = 1;
- } else if (pc_bits <= 16) {
- format = kRegMapFormatCompact16;
- pc_bytes = 2;
- } else {
- // TODO: either a better GC map format or per method failures
- Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Cannot encode GC map for method with "
- << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
- return NULL;
- }
- size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
- std::vector<uint8_t>* table = new std::vector<uint8_t>;
- if (table == NULL) {
- Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Failed to encode GC map (size=" << table_size << ")";
- return NULL;
- }
- table->reserve(table_size);
- // Write table header
- table->push_back(format | ((ref_bitmap_bytes >> DexPcToReferenceMap::kRegMapFormatShift) &
- ~DexPcToReferenceMap::kRegMapFormatMask));
- table->push_back(ref_bitmap_bytes & 0xFF);
- table->push_back(num_entries & 0xFF);
- table->push_back((num_entries >> 8) & 0xFF);
- // Write table data
- for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
- if (insn_flags_[i].IsCompileTimeInfoPoint()) {
- table->push_back(i & 0xFF);
- if (pc_bytes == 2) {
- table->push_back((i >> 8) & 0xFF);
- }
- RegisterLine* line = reg_table_.GetLine(i);
- line->WriteReferenceBitMap(*table, ref_bitmap_bytes);
- }
- }
- DCHECK_EQ(table->size(), table_size);
- return table;
-}
-
-void MethodVerifier::VerifyGcMap(const std::vector<uint8_t>& data) {
- // Check that for every GC point there is a map entry, there aren't entries for non-GC points,
- // that the table data is well formed and all references are marked (or not) in the bitmap
- DexPcToReferenceMap map(&data[0]);
- DCHECK_EQ(data.size(), map.RawSize());
- size_t map_index = 0;
- for (size_t i = 0; i < code_item_->insns_size_in_code_units_; i++) {
- const uint8_t* reg_bitmap = map.FindBitMap(i, false);
- if (insn_flags_[i].IsCompileTimeInfoPoint()) {
- CHECK_LT(map_index, map.NumEntries());
- CHECK_EQ(map.GetDexPc(map_index), i);
- CHECK_EQ(map.GetBitMap(map_index), reg_bitmap);
- map_index++;
- RegisterLine* line = reg_table_.GetLine(i);
- for (size_t j = 0; j < code_item_->registers_size_; j++) {
- if (line->GetRegisterType(j).IsNonZeroReferenceTypes()) {
- CHECK_LT(j / 8, map.RegWidth());
- CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 1);
- } else if ((j / 8) < map.RegWidth()) {
- CHECK_EQ((reg_bitmap[j / 8] >> (j % 8)) & 1, 0);
- } else {
- // If a register doesn't contain a reference then the bitmap may be shorter than the line
- }
- }
- } else {
- CHECK(reg_bitmap == NULL);
- }
- }
-}
-
-void MethodVerifier::SetDexGcMap(MethodReference ref, const std::vector<uint8_t>* gc_map) {
- DCHECK(Runtime::Current()->IsCompiler());
- {
- WriterMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
- DexGcMapTable::iterator it = dex_gc_maps_->find(ref);
- if (it != dex_gc_maps_->end()) {
- delete it->second;
- dex_gc_maps_->erase(it);
- }
- dex_gc_maps_->Put(ref, gc_map);
- }
- DCHECK(GetDexGcMap(ref) != NULL);
-}
-
-
-void MethodVerifier::SetSafeCastMap(MethodReference ref, const MethodSafeCastSet* cast_set) {
- DCHECK(Runtime::Current()->IsCompiler());
- WriterMutexLock mu(Thread::Current(), *safecast_map_lock_);
- SafeCastMap::iterator it = safecast_map_->find(ref);
- if (it != safecast_map_->end()) {
- delete it->second;
- safecast_map_->erase(it);
- }
- safecast_map_->Put(ref, cast_set);
- DCHECK(safecast_map_->find(ref) != safecast_map_->end());
-}
-
-bool MethodVerifier::IsSafeCast(MethodReference ref, uint32_t pc) {
- DCHECK(Runtime::Current()->IsCompiler());
- ReaderMutexLock mu(Thread::Current(), *safecast_map_lock_);
- SafeCastMap::const_iterator it = safecast_map_->find(ref);
- if (it == safecast_map_->end()) {
- return false;
- }
-
- // Look up the cast address in the set of safe casts
- MethodVerifier::MethodSafeCastSet::const_iterator cast_it = it->second->find(pc);
- return cast_it != it->second->end();
-}
-
-const std::vector<uint8_t>* MethodVerifier::GetDexGcMap(MethodReference ref) {
- DCHECK(Runtime::Current()->IsCompiler());
- ReaderMutexLock mu(Thread::Current(), *dex_gc_maps_lock_);
- DexGcMapTable::const_iterator it = dex_gc_maps_->find(ref);
- CHECK(it != dex_gc_maps_->end())
- << "Didn't find GC map for: " << PrettyMethod(ref.dex_method_index, *ref.dex_file);
- CHECK(it->second != NULL);
- return it->second;
-}
-
-void MethodVerifier::SetDevirtMap(MethodReference ref,
- const PcToConcreteMethodMap* devirt_map) {
- DCHECK(Runtime::Current()->IsCompiler());
- WriterMutexLock mu(Thread::Current(), *devirt_maps_lock_);
- DevirtualizationMapTable::iterator it = devirt_maps_->find(ref);
- if (it != devirt_maps_->end()) {
- delete it->second;
- devirt_maps_->erase(it);
- }
-
- devirt_maps_->Put(ref, devirt_map);
- DCHECK(devirt_maps_->find(ref) != devirt_maps_->end());
-}
-
-const MethodReference* MethodVerifier::GetDevirtMap(const MethodReference& ref,
- uint32_t dex_pc) {
- DCHECK(Runtime::Current()->IsCompiler());
- ReaderMutexLock mu(Thread::Current(), *devirt_maps_lock_);
- DevirtualizationMapTable::const_iterator it = devirt_maps_->find(ref);
- if (it == devirt_maps_->end()) {
- return NULL;
- }
-
- // Look up the PC in the map, get the concrete method to execute and return its reference.
- MethodVerifier::PcToConcreteMethodMap::const_iterator pc_to_concrete_method
- = it->second->find(dex_pc);
- if (pc_to_concrete_method != it->second->end()) {
- return &(pc_to_concrete_method->second);
- } else {
- return NULL;
- }
-}
-
std::vector<int32_t> MethodVerifier::DescribeVRegs(uint32_t dex_pc) {
RegisterLine* line = reg_table_.GetLine(dex_pc);
std::vector<int32_t> result;
@@ -4273,120 +3928,14 @@
return result;
}
-bool MethodVerifier::IsCandidateForCompilation(MethodReference& method_ref,
- const uint32_t access_flags) {
-#ifdef ART_SEA_IR_MODE
- bool use_sea = Runtime::Current()->IsSeaIRMode();
- use_sea = use_sea && (std::string::npos != PrettyMethod(
- method_ref.dex_method_index, *(method_ref.dex_file)).find("fibonacci"));
- if (use_sea) return true;
-#endif
- // Don't compile class initializers, ever.
- if (((access_flags & kAccConstructor) != 0) && ((access_flags & kAccStatic) != 0)) {
- return false;
- }
- return (Runtime::Current()->GetCompilerFilter() != Runtime::kInterpretOnly);
-}
-
-ReaderWriterMutex* MethodVerifier::dex_gc_maps_lock_ = NULL;
-MethodVerifier::DexGcMapTable* MethodVerifier::dex_gc_maps_ = NULL;
-
-ReaderWriterMutex* MethodVerifier::safecast_map_lock_ = NULL;
-MethodVerifier::SafeCastMap* MethodVerifier::safecast_map_ = NULL;
-
-ReaderWriterMutex* MethodVerifier::devirt_maps_lock_ = NULL;
-MethodVerifier::DevirtualizationMapTable* MethodVerifier::devirt_maps_ = NULL;
-
-ReaderWriterMutex* MethodVerifier::rejected_classes_lock_ = NULL;
-MethodVerifier::RejectedClassesTable* MethodVerifier::rejected_classes_ = NULL;
-
void MethodVerifier::Init() {
- if (Runtime::Current()->IsCompiler()) {
- dex_gc_maps_lock_ = new ReaderWriterMutex("verifier GC maps lock");
- Thread* self = Thread::Current();
- {
- WriterMutexLock mu(self, *dex_gc_maps_lock_);
- dex_gc_maps_ = new MethodVerifier::DexGcMapTable;
- }
-
- safecast_map_lock_ = new ReaderWriterMutex("verifier Cast Elision lock");
- {
- WriterMutexLock mu(self, *safecast_map_lock_);
- safecast_map_ = new MethodVerifier::SafeCastMap();
- }
-
- devirt_maps_lock_ = new ReaderWriterMutex("verifier Devirtualization lock");
-
- {
- WriterMutexLock mu(self, *devirt_maps_lock_);
- devirt_maps_ = new MethodVerifier::DevirtualizationMapTable();
- }
-
- rejected_classes_lock_ = new ReaderWriterMutex("verifier rejected classes lock");
- {
- WriterMutexLock mu(self, *rejected_classes_lock_);
- rejected_classes_ = new MethodVerifier::RejectedClassesTable;
- }
- }
art::verifier::RegTypeCache::Init();
}
void MethodVerifier::Shutdown() {
- if (Runtime::Current()->IsCompiler()) {
- Thread* self = Thread::Current();
- {
- WriterMutexLock mu(self, *dex_gc_maps_lock_);
- STLDeleteValues(dex_gc_maps_);
- delete dex_gc_maps_;
- dex_gc_maps_ = NULL;
- }
- delete dex_gc_maps_lock_;
- dex_gc_maps_lock_ = NULL;
-
- {
- WriterMutexLock mu(self, *safecast_map_lock_);
- STLDeleteValues(safecast_map_);
- delete safecast_map_;
- safecast_map_ = NULL;
- }
- delete safecast_map_lock_;
- safecast_map_lock_ = NULL;
-
- {
- WriterMutexLock mu(self, *devirt_maps_lock_);
- STLDeleteValues(devirt_maps_);
- delete devirt_maps_;
- devirt_maps_ = NULL;
- }
- delete devirt_maps_lock_;
- devirt_maps_lock_ = NULL;
-
- {
- WriterMutexLock mu(self, *rejected_classes_lock_);
- delete rejected_classes_;
- rejected_classes_ = NULL;
- }
- delete rejected_classes_lock_;
- rejected_classes_lock_ = NULL;
- }
verifier::RegTypeCache::ShutDown();
}
-void MethodVerifier::AddRejectedClass(ClassReference ref) {
- DCHECK(Runtime::Current()->IsCompiler());
- {
- WriterMutexLock mu(Thread::Current(), *rejected_classes_lock_);
- rejected_classes_->insert(ref);
- }
- CHECK(IsClassRejected(ref));
-}
-
-bool MethodVerifier::IsClassRejected(ClassReference ref) {
- DCHECK(Runtime::Current()->IsCompiler());
- ReaderMutexLock mu(Thread::Current(), *rejected_classes_lock_);
- return (rejected_classes_->find(ref) != rejected_classes_->end());
-}
-
void MethodVerifier::VisitRoots(RootVisitor* visitor, void* arg) {
reg_types_.VisitRoots(visitor, arg);
}