Skip to content

Commit 4fbabd6

Browse files
committed
fixed or-->trunc->shl error, by updating the GetShiftedRange, needs more checks
1 parent 3a4d65e commit 4fbabd6

File tree

3 files changed

+9
-26
lines changed

3 files changed

+9
-26
lines changed

llvm/lib/Analysis/DemandedBits.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,12 @@ void DemandedBits::determineLiveOperandBits(
7878
};
7979
auto GetShiftedRange = [&](unsigned const Min, unsigned const Max,
8080
bool ShiftLeft) {
81-
APInt Range = APInt::getLowBitsSet(BitWidth, Max + 1) &
82-
~APInt::getLowBitsSet(BitWidth, Min);
8381
using ShiftFn = APInt (APInt::*)(unsigned) const;
8482
auto Shift = ShiftLeft ? static_cast<ShiftFn>(&APInt::shl)
8583
: static_cast<ShiftFn>(&APInt::lshr);
8684
AB = APInt::getZero(BitWidth);
87-
APInt Bits = AOut;
88-
while (Bits != 0) {
89-
unsigned I = Bits.countr_zero();
90-
AB |= (Range.*Shift)(I);
91-
Bits.clearBit(I);
85+
for (unsigned ShiftAmount = Min; ShiftAmount <= Max; ++ShiftAmount) {
86+
AB |= (AOut.*Shift)(ShiftAmount);
9287
}
9388
};
9489

@@ -203,7 +198,7 @@ void DemandedBits::determineLiveOperandBits(
203198
unsigned Min = Known.getMinValue().getLimitedValue(BitWidth - 1);
204199
unsigned Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
205200
// similar to Lshr case
206-
GetShiftedRange(Min, Max, false);
201+
GetShiftedRange(Min, Max, /*ShiftLeft=*/false);
207202
const auto *S = cast<ShlOperator>(UserI);
208203
if (S->hasNoSignedWrap())
209204
AB |= APInt::getHighBitsSet(BitWidth, Max + 1);
@@ -229,13 +224,12 @@ void DemandedBits::determineLiveOperandBits(
229224
unsigned Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
230225
// Suppose AOut == 0b0000 1001
231226
// [min, max] = [1, 3]
232-
// we create the range 0b0000 1110
233-
// shift by 1 we get 0b0001 1100
234-
// shift by 2 we get 0b0011 1000
235-
// shift by 3 we get 0b0111 0000
227+
// shift by 1 we get 0b0001 00100
228+
// shift by 2 we get 0b0010 0100
229+
// shift by 3 we get 0b0100 1000
236230
// we take the or for every shift to cover all the positions.
237231
//
238-
GetShiftedRange(Min, Max, true);
232+
GetShiftedRange(Min, Max, /*ShiftLeft=*/true);
239233
if (cast<LShrOperator>(UserI)->isExact())
240234
AB |= APInt::getLowBitsSet(BitWidth, Max);
241235
}
@@ -262,7 +256,7 @@ void DemandedBits::determineLiveOperandBits(
262256
ComputeKnownBits(BitWidth, UserI->getOperand(1), nullptr);
263257
unsigned Min = Known.getMinValue().getLimitedValue(BitWidth - 1);
264258
unsigned Max = Known.getMaxValue().getLimitedValue(BitWidth - 1);
265-
GetShiftedRange(Min, Max, true);
259+
GetShiftedRange(Min, Max, /*ShiftLeft=*/true);
266260
if (Max) {
267261
// Suppose AOut = 0011 1100
268262
// [min, max] = [1, 3]

llvm/test/Analysis/DemandedBits/ashr.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ define i8 @test_ashr_var_amount(i32 %a, i32 %b){
8989
ret i8 %5
9090
}
9191

92+
define i8 @test_ashr_var_amount_nsw(i32 %a, i32 %b){
9293
; CHECK-LABEL 'test_ashr_var_amount_nsw'
9394
; CHECK-DAG: DemandedBits: 0xff for %5 = trunc i32 %4 to i8
9495
; CHECK-DAG: DemandedBits: 0xff for %4 in %5 = trunc i32 %4 to i8

llvm/test/Analysis/DemandedBits/lshr.ll

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,6 @@ define i8 @test_lshr(i32 %a, i32 %b) {
6363
%lshr.t = trunc i32 %lshr to i8
6464
ret i8 %lshr.t
6565
}
66-
DemandedBits: 0xff for %4 = lshr i32 %1, %3
67-
DemandedBits: 0xffffffff for %1 in %4 = lshr i32 %1, %3
68-
DemandedBits: 0xffffffff for %3 in %4 = lshr i32 %1, %3
69-
DemandedBits: 0xff for %5 = trunc i32 %4 to i8
70-
DemandedBits: 0xff for %4 in %5 = trunc i32 %4 to i8
71-
DemandedBits: 0xffffffff for %3 = zext i8 %2 to i32
72-
DemandedBits: 0xff for %2 in %3 = zext i8 %2 to i32
73-
DemandedBits: 0xffffffff for %1 = add nsw i32 %a, %b
74-
DemandedBits: 0xffffffff for %a in %1 = add nsw i32 %a, %b
75-
DemandedBits: 0xffffffff for %b in %1 = add nsw i32 %a, %b
76-
DemandedBits: 0xff for %2 = trunc i32 %1 to i8
77-
DemandedBits: 0xff for %1 in %2 = trunc i32 %1 to i8
7866

7967
define i8 @test_lshr_var_amount(i32 %a, i32 %b){
8068
; CHECK-LABEL: 'test_lshr_var_amount'

0 commit comments

Comments
 (0)