ART: Clean up arm64 kNumberOfXRegisters usage.
Avoid undefined behavior for arm64 stemming from 1u << 32 in
loops with upper bound kNumberOfXRegisters.
Create iterators for enumerating bits in an integer either
from high to low or from low to high and use them for
<arch>Context::FillCalleeSaves() on all architectures.
Refactor runtime/utils.{h,cc} by moving all bit-fiddling
functions to runtime/base/bit_utils.{h,cc} (together with
the new bit iterators) and all time-related functions to
runtime/base/time_utils.{h,cc}. Improve test coverage and
fix some corner cases for the bit-fiddling functions.
Bug: 13925192
Change-Id: I704884dab15b41ecf7a1c47d397ab1c3fc7ee0f7
diff --git a/runtime/arch/x86/context_x86.cc b/runtime/arch/x86/context_x86.cc
index 52a35dd..7a5d0c5 100644
--- a/runtime/arch/x86/context_x86.cc
+++ b/runtime/arch/x86/context_x86.cc
@@ -16,10 +16,9 @@
#include "context_x86.h"
+#include "base/bit_utils.h"
#include "mirror/art_method-inl.h"
#include "quick/quick_method_frame_info.h"
-#include "utils.h"
-
namespace art {
namespace x86 {
@@ -27,12 +26,8 @@
static constexpr uintptr_t gZero = 0;
void X86Context::Reset() {
- for (size_t i = 0; i < kNumberOfCpuRegisters; i++) {
- gprs_[i] = nullptr;
- }
- for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
- fprs_[i] = nullptr;
- }
+ std::fill_n(gprs_, arraysize(gprs_), nullptr);
+ std::fill_n(fprs_, arraysize(fprs_), nullptr);
gprs_[ESP] = &esp_;
// Initialize registers with easy to spot debug values.
esp_ = X86Context::kBadGprBase + ESP;
@@ -42,36 +37,29 @@
void X86Context::FillCalleeSaves(const StackVisitor& fr) {
mirror::ArtMethod* method = fr.GetMethod();
const QuickMethodFrameInfo frame_info = method->GetQuickFrameInfo();
- size_t spill_count = POPCOUNT(frame_info.CoreSpillMask());
- size_t fp_spill_count = POPCOUNT(frame_info.FpSpillMask());
- if (spill_count > 0) {
- // Lowest number spill is farthest away, walk registers and fill into context.
- int j = 2; // Offset j to skip return address spill.
- for (int i = 0; i < kNumberOfCpuRegisters; i++) {
- if (((frame_info.CoreSpillMask() >> i) & 1) != 0) {
- gprs_[i] = fr.CalleeSaveAddress(spill_count - j, frame_info.FrameSizeInBytes());
- j++;
- }
- }
+ int spill_pos = 0;
+
+ // Core registers come first, from the highest down to the lowest.
+ uint32_t core_regs =
+ frame_info.CoreSpillMask() & ~(static_cast<uint32_t>(-1) << kNumberOfCpuRegisters);
+ DCHECK_EQ(1, POPCOUNT(frame_info.CoreSpillMask() & ~core_regs)); // Return address spill.
+ for (uint32_t core_reg : HighToLowBits(core_regs)) {
+ gprs_[core_reg] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes());
+ ++spill_pos;
}
- if (fp_spill_count > 0) {
- // Lowest number spill is farthest away, walk registers and fill into context.
- size_t j = 2; // Offset j to skip return address spill.
- size_t fp_spill_size_in_words = fp_spill_count * 2;
- for (size_t i = 0; i < kNumberOfFloatRegisters; ++i) {
- if (((frame_info.FpSpillMask() >> i) & 1) != 0) {
- // There are 2 pieces to each XMM register, to match VR size.
- fprs_[2*i] = reinterpret_cast<uint32_t*>(
- fr.CalleeSaveAddress(spill_count + fp_spill_size_in_words - j,
- frame_info.FrameSizeInBytes()));
- fprs_[2*i+1] = reinterpret_cast<uint32_t*>(
- fr.CalleeSaveAddress(spill_count + fp_spill_size_in_words - j - 1,
- frame_info.FrameSizeInBytes()));
- // Two void* per XMM register.
- j += 2;
- }
- }
+ DCHECK_EQ(spill_pos, POPCOUNT(frame_info.CoreSpillMask()) - 1);
+
+ // FP registers come second, from the highest down to the lowest.
+ uint32_t fp_regs = frame_info.FpSpillMask();
+ DCHECK_EQ(0u, fp_regs & (static_cast<uint32_t>(-1) << kNumberOfFloatRegisters));
+ for (uint32_t fp_reg : HighToLowBits(fp_regs)) {
+ // Two void* per XMM register.
+ fprs_[2 * fp_reg] = fr.CalleeSaveAddress(spill_pos + 1, frame_info.FrameSizeInBytes());
+ fprs_[2 * fp_reg + 1] = fr.CalleeSaveAddress(spill_pos, frame_info.FrameSizeInBytes());
+ spill_pos += 2;
}
+ DCHECK_EQ(spill_pos,
+ POPCOUNT(frame_info.CoreSpillMask()) - 1 + 2 * POPCOUNT(frame_info.FpSpillMask()));
}
void X86Context::SmashCallerSaves() {
diff --git a/runtime/arch/x86/quick_method_frame_info_x86.h b/runtime/arch/x86/quick_method_frame_info_x86.h
index 9bba531..ed1d860 100644
--- a/runtime/arch/x86/quick_method_frame_info_x86.h
+++ b/runtime/arch/x86/quick_method_frame_info_x86.h
@@ -17,6 +17,7 @@
#ifndef ART_RUNTIME_ARCH_X86_QUICK_METHOD_FRAME_INFO_X86_H_
#define ART_RUNTIME_ARCH_X86_QUICK_METHOD_FRAME_INFO_X86_H_
+#include "base/bit_utils.h"
#include "quick/quick_method_frame_info.h"
#include "registers_x86.h"
#include "runtime.h" // for Runtime::CalleeSaveType.