-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[GlobalISel] Add Knownbits for G_LOAD/ZEXTLOAD/SEXTLOAD with range metadata #86431
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+93
−22
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…tadata Similar to llvm#80829 for GlobalISel.
@llvm/pr-subscribers-llvm-globalisel Author: David Green (davemgreen) ChangesSimilar to #80829 for GlobalISel. Full diff: https://github.com/llvm/llvm-project/pull/86431.diff 3 Files Affected:
diff --git a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
index 2e2cc9a95bd95c..101676510fab83 100644
--- a/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/GISelKnownBits.cpp
@@ -405,18 +405,23 @@ void GISelKnownBits::computeKnownBitsImpl(Register R, KnownBits &Known,
}
case TargetOpcode::G_LOAD: {
const MachineMemOperand *MMO = *MI.memoperands_begin();
- if (const MDNode *Ranges = MMO->getRanges()) {
- computeKnownBitsFromRangeMetadata(*Ranges, Known);
- }
-
+ KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
+ if (const MDNode *Ranges = MMO->getRanges())
+ computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
+ Known = KnownRange.anyext(Known.getBitWidth());
break;
}
+ case TargetOpcode::G_SEXTLOAD:
case TargetOpcode::G_ZEXTLOAD: {
if (DstTy.isVector())
break;
- // Everything above the retrieved bits is zero
- Known.Zero.setBitsFrom(
- (*MI.memoperands_begin())->getSizeInBits().getValue());
+ const MachineMemOperand *MMO = *MI.memoperands_begin();
+ KnownBits KnownRange(MMO->getMemoryType().getScalarSizeInBits());
+ if (const MDNode *Ranges = MMO->getRanges())
+ computeKnownBitsFromRangeMetadata(*Ranges, KnownRange);
+ Known = Opcode == TargetOpcode::G_SEXTLOAD
+ ? KnownRange.sext(Known.getBitWidth())
+ : KnownRange.zext(Known.getBitWidth());
break;
}
case TargetOpcode::G_ASHR: {
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
index f3659bc6956361..ef80eed8d18023 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownBitsTest.cpp
@@ -1100,6 +1100,25 @@ TEST_F(AMDGPUGISelMITest, TestIsKnownToBeAPowerOfTwo) {
EXPECT_TRUE(isKnownToBeAPowerOfTwo(CopyOrPow2, *MRI, &KB));
}
+static void AddRangeMetadata(LLVMContext &Context, MachineInstr *Load) {
+ IntegerType *Int8Ty = Type::getInt8Ty(Context);
+
+ // Value must be in [0, 2)
+ Metadata *LowAndHigh[] = {
+ ConstantAsMetadata::get(ConstantInt::get(Int8Ty, 0)),
+ ConstantAsMetadata::get(ConstantInt::get(Int8Ty, 2))};
+ auto NewMDNode = MDNode::get(Context, LowAndHigh);
+ const MachineMemOperand *OldMMO = *Load->memoperands_begin();
+ MachineMemOperand *NewMMO =
+ Load->getParent()->getParent()->getMachineMemOperand(
+ OldMMO->getPointerInfo(), OldMMO->getFlags(), OldMMO->getMemoryType(),
+ OldMMO->getAlign(), OldMMO->getAAInfo(), NewMDNode);
+ MachineIRBuilder MIB(*Load);
+ MIB.buildLoadInstr(Load->getOpcode(), Load->getOperand(0),
+ Load->getOperand(1), *NewMMO);
+ Load->eraseFromParent();
+}
+
TEST_F(AArch64GISelMITest, TestMetadata) {
StringRef MIRString = " %imp:_(p0) = G_IMPLICIT_DEF\n"
" %load:_(s8) = G_LOAD %imp(p0) :: (load (s8))\n"
@@ -1120,20 +1139,7 @@ TEST_F(AArch64GISelMITest, TestMetadata) {
MachineInstr *And = MRI->getVRegDef(SrcReg);
MachineInstr *Ext = MRI->getVRegDef(And->getOperand(1).getReg());
MachineInstr *Load = MRI->getVRegDef(Ext->getOperand(1).getReg());
- IntegerType *Int8Ty = Type::getInt8Ty(Context);
-
- // Value must be in [0, 2)
- Metadata *LowAndHigh[] = {
- ConstantAsMetadata::get(ConstantInt::get(Int8Ty, 0)),
- ConstantAsMetadata::get(ConstantInt::get(Int8Ty, 2))};
- auto NewMDNode = MDNode::get(Context, LowAndHigh);
- const MachineMemOperand *OldMMO = *Load->memoperands_begin();
- MachineMemOperand NewMMO(OldMMO->getPointerInfo(), OldMMO->getFlags(),
- OldMMO->getSizeInBits(), OldMMO->getAlign(),
- OldMMO->getAAInfo(), NewMDNode);
- MachineIRBuilder MIB(*Load);
- MIB.buildLoad(Load->getOperand(0), Load->getOperand(1), NewMMO);
- Load->eraseFromParent();
+ AddRangeMetadata(Context, Load);
GISelKnownBits Info(*MF);
KnownBits Res = Info.getKnownBits(And->getOperand(1).getReg());
@@ -1148,6 +1154,66 @@ TEST_F(AArch64GISelMITest, TestMetadata) {
EXPECT_EQ(Mask.getZExtValue(), Res.Zero.getZExtValue());
}
+TEST_F(AArch64GISelMITest, TestMetadataExt) {
+ StringRef MIRString = " %imp:_(p0) = G_IMPLICIT_DEF\n"
+ " %load:_(s32) = G_LOAD %imp(p0) :: (load (s8))\n"
+ " %copy:_(s32) = COPY %load(s32)\n";
+ setUp(MIRString);
+ if (!TM)
+ GTEST_SKIP();
+
+ Register CopyReg = Copies[Copies.size() - 1];
+ MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+ Register SrcReg = FinalCopy->getOperand(1).getReg();
+ MachineInstr *Load = MRI->getVRegDef(SrcReg);
+ AddRangeMetadata(Context, Load);
+
+ GISelKnownBits Info(*MF);
+ KnownBits Res = Info.getKnownBits(SrcReg);
+ EXPECT_TRUE(Res.One.isZero());
+ EXPECT_EQ(Res.Zero.getZExtValue(), 0xfeu);
+}
+
+TEST_F(AArch64GISelMITest, TestMetadataZExt) {
+ StringRef MIRString = " %imp:_(p0) = G_IMPLICIT_DEF\n"
+ " %load:_(s32) = G_ZEXTLOAD %imp(p0) :: (load (s8))\n"
+ " %copy:_(s32) = COPY %load(s32)\n";
+ setUp(MIRString);
+ if (!TM)
+ GTEST_SKIP();
+
+ Register CopyReg = Copies[Copies.size() - 1];
+ MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+ Register SrcReg = FinalCopy->getOperand(1).getReg();
+ MachineInstr *Load = MRI->getVRegDef(SrcReg);
+ AddRangeMetadata(Context, Load);
+
+ GISelKnownBits Info(*MF);
+ KnownBits Res = Info.getKnownBits(SrcReg);
+ EXPECT_TRUE(Res.One.isZero());
+ EXPECT_EQ(Res.Zero.getZExtValue(), 0xfffffffe);
+}
+
+TEST_F(AArch64GISelMITest, TestMetadataSExt) {
+ StringRef MIRString = " %imp:_(p0) = G_IMPLICIT_DEF\n"
+ " %load:_(s32) = G_SEXTLOAD %imp(p0) :: (load (s8))\n"
+ " %copy:_(s32) = COPY %load(s32)\n";
+ setUp(MIRString);
+ if (!TM)
+ GTEST_SKIP();
+
+ Register CopyReg = Copies[Copies.size() - 1];
+ MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+ Register SrcReg = FinalCopy->getOperand(1).getReg();
+ MachineInstr *Load = MRI->getVRegDef(SrcReg);
+ AddRangeMetadata(Context, Load);
+
+ GISelKnownBits Info(*MF);
+ KnownBits Res = Info.getKnownBits(SrcReg);
+ EXPECT_TRUE(Res.One.isZero());
+ EXPECT_EQ(Res.Zero.getZExtValue(), 0xfffffffe);
+}
+
TEST_F(AArch64GISelMITest, TestKnownBitsExt) {
StringRef MIRString = " %c1:_(s16) = G_CONSTANT i16 1\n"
" %x:_(s16) = G_IMPLICIT_DEF\n"
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp
index ab180cd1cc2726..dd6edd35a8468b 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownBitsVectorTest.cpp
@@ -1008,7 +1008,7 @@ TEST_F(AArch64GISelMITest, TestVectorMetadata) {
auto *NewMDNode = MDNode::get(Context, LowAndHigh);
const MachineMemOperand *OldMMO = *Load->memoperands_begin();
MachineMemOperand NewMMO(OldMMO->getPointerInfo(), OldMMO->getFlags(),
- OldMMO->getSizeInBits(), OldMMO->getAlign(),
+ OldMMO->getMemoryType(), OldMMO->getAlign(),
OldMMO->getAAInfo(), NewMDNode);
MachineIRBuilder MIB(*Load);
MIB.buildLoad(Load->getOperand(0), Load->getOperand(1), NewMMO);
|
✅ With the latest revision this PR passed the Python code formatter. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
arsenm
approved these changes
Mar 26, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Similar to #80829 for GlobalISel.