Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions llvm/include/llvm/CodeGen/Register.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_CODEGEN_REGISTER_H

#include "llvm/MC/MCRegister.h"
#include "llvm/Support/MathExtras.h"
#include <cassert>

namespace llvm {
Expand All @@ -35,19 +36,23 @@ class Register {
// DenseMapInfo<unsigned> uses -1u and -2u.
static_assert(std::numeric_limits<decltype(Reg)>::max() >= 0xFFFFFFFF,
"Reg isn't large enough to hold full range.");
static constexpr unsigned FirstStackSlot = 1u << 30;
static_assert(FirstStackSlot >= MCRegister::LastPhysicalReg);
static constexpr unsigned MaxFrameIndexBitwidth = 30;
static constexpr unsigned StackSlotZero = 1u << MaxFrameIndexBitwidth;
static constexpr const unsigned StackSlotMask = StackSlotZero - 1;
static_assert(StackSlotZero >= MCRegister::LastPhysicalReg);
static constexpr unsigned VirtualRegFlag = 1u << 31;

/// Return true if this is a stack slot.
constexpr bool isStack() const {
return Register::FirstStackSlot <= Reg && Reg < Register::VirtualRegFlag;
return Register::StackSlotZero <= Reg && Reg < Register::VirtualRegFlag;
}

/// Convert a non-negative frame index to a stack slot register value.
static Register index2StackSlot(int FI) {
assert(FI >= 0 && "Cannot hold a negative frame index.");
return Register(FI + Register::FirstStackSlot);
assert(isInt<MaxFrameIndexBitwidth>(FI) &&
"Frame index must be at most 30 bits.");
unsigned FIMasked = FI & Register::StackSlotMask;
return Register(FIMasked | Register::StackSlotZero);
}

/// Return true if the specified register number is in
Expand Down Expand Up @@ -87,7 +92,7 @@ class Register {
/// Compute the frame index from a register value representing a stack slot.
int stackSlotIndex() const {
assert(isStack() && "Not a stack slot");
return static_cast<int>(Reg - Register::FirstStackSlot);
return SignExtend32<MaxFrameIndexBitwidth>(Reg & Register::StackSlotMask);
}

constexpr operator unsigned() const { return Reg; }
Expand Down
3 changes: 0 additions & 3 deletions llvm/lib/CodeGen/ReachingDefAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ void ReachingDefInfo::processDefs(MachineInstr *MI) {
for (auto &MO : MI->operands()) {
if (MO.isFI()) {
int FrameIndex = MO.getIndex();
assert(FrameIndex >= 0 && "Can't handle negative frame indicies yet!");
if (!isFIDef(*MI, FrameIndex, TII))
continue;
MBBFrameObjsReachingDefs[{MBBNumber, FrameIndex}].push_back(CurInstr);
Expand Down Expand Up @@ -302,8 +301,6 @@ void ReachingDefInfo::print(raw_ostream &OS) {
Register Reg;
if (MO.isFI()) {
int FrameIndex = MO.getIndex();
assert(FrameIndex >= 0 &&
"Can't handle negative frame indicies yet!");
Reg = Register::index2StackSlot(FrameIndex);
} else if (MO.isReg()) {
if (MO.isDef())
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ add_llvm_unittest(CodeGenTests
MachineOperandTest.cpp
MIR2VecTest.cpp
RegAllocScoreTest.cpp
RegisterTest.cpp
PassManagerTest.cpp
ScalableVectorMVTsTest.cpp
SchedBoundary.cpp
Expand Down
35 changes: 35 additions & 0 deletions llvm/unittests/CodeGen/RegisterTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===- RegisterTest.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/CodeGen/Register.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {
TEST(RegisterTest, Idx2StackSlot) {
EXPECT_EQ(Register::index2StackSlot(0), Register::StackSlotZero);
EXPECT_EQ(Register::index2StackSlot(1), Register::StackSlotZero | 1);
EXPECT_EQ(Register::index2StackSlot(-1),
Register::StackSlotZero | Register::StackSlotMask);
// check that we do not crash on the highest possible value of frame index.
EXPECT_NO_FATAL_FAILURE(Register::index2StackSlot((1 << 29) - 1));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use named constants for 29 here and the line below?

// check that we do not crash on the lowest possible value of frame index.
EXPECT_NO_FATAL_FAILURE(Register::index2StackSlot(-(1 << 29)));
Comment on lines +16 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EXPECT_EQ(Register::index2StackSlot(0), Register::StackSlotZero);
EXPECT_EQ(Register::index2StackSlot(1), Register::StackSlotZero | 1);
EXPECT_EQ(Register::index2StackSlot(-1),
Register::StackSlotZero | Register::StackSlotMask);
// check that we do not crash on the highest possible value of frame index.
EXPECT_NO_FATAL_FAILURE(Register::index2StackSlot((1 << 29) - 1));
// check that we do not crash on the lowest possible value of frame index.
EXPECT_NO_FATAL_FAILURE(Register::index2StackSlot(-(1 << 29)));
int MaxPowOf2 = 1 << 29;
EXPECT_EQ(Register::index2StackSlot(0), Register::StackSlotZero);
EXPECT_EQ(Register::index2StackSlot(1), Register::StackSlotZero | 1);
EXPECT_EQ(Register::index2StackSlot(-1),
Register::StackSlotZero | Register::StackSlotMask);
// check that we do not crash on the highest possible value of frame index.
EXPECT_NO_FATAL_FAILURE(Register::index2StackSlot(MaxPowOf2 - 1));
// check that we do not crash on the lowest possible value of frame index.
EXPECT_NO_FATAL_FAILURE(Register::index2StackSlot(-MaxPowOf2));

Or just MaxBits = 29 or something like that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should use Register::MaxFrameIndexBitwidth - 1 instead of putting 29 anywhere in the test.

}

TEST(RegisterTest, StackSlotIndex) {
int MaxPowOf2 = 1 << (Register::MaxFrameIndexBitwidth - 1);
std::vector<int> FIs = {0, 1 - 1, MaxPowOf2 - 1, -MaxPowOf2};

for (int FI : FIs) {
Register Reg = Register::index2StackSlot(FI);
EXPECT_EQ(Reg.stackSlotIndex(), FI);
}
}
} // end namespace