Skip to content

Commit 1c9c087

Browse files
committed
Improved result when RHS is constant
1 parent 252b865 commit 1c9c087

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

llvm/lib/CodeGen/GlobalISel/GISelValueTracking.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,9 +1977,12 @@ unsigned GISelValueTracking::computeNumSignBits(Register R,
19771977
return TyBits;
19781978

19791979
// If the input is known to be positive (the sign bit is known clear),
1980-
// the output of the NEG has the same number of sign bits as the input.
1981-
if (Known2.isNonNegative())
1982-
return Src2NumSignBits;
1980+
// the output of the NEG has, at worst, the same number of sign bits as
1981+
// the input.
1982+
if (Known2.isNonNegative()) {
1983+
FirstAnswer = Src2NumSignBits;
1984+
break;
1985+
}
19831986

19841987
// Otherwise, we treat this like a SUB.
19851988
}

llvm/test/CodeGen/AArch64/GlobalISel/knownbits-sub.mir

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ body: |
3838
%2:_(s8) = G_SUB %0, %1
3939
...
4040
---
41+
name: CstNegFour
42+
body: |
43+
bb.1:
44+
; CHECK-LABEL: name: @CstNegFour
45+
; CHECK-NEXT: %0:_ KnownBits:00000000 SignBits:8
46+
; CHECK-NEXT: %1:_ KnownBits:00000100 SignBits:5
47+
; CHECK-NEXT: %2:_ KnownBits:11111100 SignBits:6
48+
%0:_(s8) = G_CONSTANT i8 0
49+
%1:_(s8) = G_CONSTANT i8 4
50+
%2:_(s8) = G_SUB %0, %1
51+
...
52+
---
4153
name: CstNeg
4254
body: |
4355
bb.1:

llvm/unittests/Target/AArch64/AArch64SelectionDAGTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ TEST_F(AArch64SelectionDAGTest, ComputeNumSignBits_SUB) {
204204
auto OpNegOne = DAG->getNode(ISD::SUB, Loc, IntVT, N0, N1);
205205
EXPECT_EQ(DAG->ComputeNumSignBits(OpNegOne), 8u);
206206

207+
// Neg 5
208+
// N0 = 00000000
209+
// N5 = 00000101
210+
auto OpNegFive = DAG->getNode(ISD::SUB, Loc, IntVT, N0, N5);
211+
EXPECT_EQ(DAG->ComputeNumSignBits(OpNegFive), 5u);
212+
207213
// Non negative
208214
// N0 = 00000000
209215
// Nsign3 = 000????0

0 commit comments

Comments
 (0)