Skip to content

Commit 2c094ac

Browse files
authored
[InstCombine] Drop range attributes in foldBitCeil (#116641)
Closes #112076
1 parent 8c56dd3 commit 2c094ac

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,7 +3466,8 @@ static bool isSafeToRemoveBitCeilSelect(ICmpInst::Predicate Pred, Value *Cond0,
34663466
// Note that the select is optimized away while the shift count is masked with
34673467
// 31. We handle some variations of the input operand like std::bit_ceil(X +
34683468
// 1).
3469-
static Instruction *foldBitCeil(SelectInst &SI, IRBuilderBase &Builder) {
3469+
static Instruction *foldBitCeil(SelectInst &SI, IRBuilderBase &Builder,
3470+
InstCombinerImpl &IC) {
34703471
Type *SelType = SI.getType();
34713472
unsigned BitWidth = SelType->getScalarSizeInBits();
34723473

@@ -3501,6 +3502,10 @@ static Instruction *foldBitCeil(SelectInst &SI, IRBuilderBase &Builder) {
35013502
// single hardware instruction as opposed to BitWidth - CTLZ, where BitWidth
35023503
// is an integer constant. Masking with BitWidth-1 comes free on some
35033504
// hardware as part of the shift instruction.
3505+
3506+
// Drop range attributes and re-infer them in the next iteration.
3507+
cast<Instruction>(Ctlz)->dropPoisonGeneratingAnnotations();
3508+
IC.addToWorklist(cast<Instruction>(Ctlz));
35043509
Value *Neg = Builder.CreateNeg(Ctlz);
35053510
Value *Masked =
35063511
Builder.CreateAnd(Neg, ConstantInt::get(SelType, BitWidth - 1));
@@ -4148,7 +4153,7 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
41484153
if (sinkNotIntoOtherHandOfLogicalOp(SI))
41494154
return &SI;
41504155

4151-
if (Instruction *I = foldBitCeil(SI, Builder))
4156+
if (Instruction *I = foldBitCeil(SI, Builder, *this))
41524157
return I;
41534158

41544159
if (Instruction *I = foldSelectToCmp(SI))

llvm/test/Transforms/InstCombine/bit_ceil.ll

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,23 @@ define i32 @pr91691_keep_nsw(i32 %0) {
320320
ret i32 %7
321321
}
322322

323+
define i32 @test_drop_range_attr(i32 %x) {
324+
; CHECK-LABEL: @test_drop_range_attr(
325+
; CHECK-NEXT: [[CTLZ:%.*]] = call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false)
326+
; CHECK-NEXT: [[TMP1:%.*]] = sub nsw i32 0, [[CTLZ]]
327+
; CHECK-NEXT: [[TMP2:%.*]] = and i32 [[TMP1]], 31
328+
; CHECK-NEXT: [[SEL:%.*]] = shl nuw i32 1, [[TMP2]]
329+
; CHECK-NEXT: ret i32 [[SEL]]
330+
;
331+
%ctlz = call range(i32 1, 33) i32 @llvm.ctlz.i32(i32 %x, i1 false)
332+
%sub = sub i32 32, %ctlz
333+
%shl = shl i32 1, %sub
334+
%dec = add i32 %x, -1
335+
%ult = icmp ult i32 %dec, -2
336+
%sel = select i1 %ult, i32 %shl, i32 1
337+
ret i32 %sel
338+
}
339+
323340
declare i32 @llvm.ctlz.i32(i32, i1 immarg)
324341
declare i64 @llvm.ctlz.i64(i64, i1 immarg)
325342
declare <4 x i32> @llvm.ctlz.v4i32(<4 x i32>, i1)

0 commit comments

Comments
 (0)