Skip to content

Commit 8185794

Browse files
authored
[LVI][CVP] Treat undef like a full range (#68190)
When converting to ConstantRange, we should treat undef like a full range. Fixes #68381.
1 parent 3542dd8 commit 8185794

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

llvm/lib/Analysis/LazyValueInfo.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ void LazyValueInfoImpl::intersectAssumeOrGuardBlockValueConstantRange(
801801

802802
static ConstantRange getConstantRangeOrFull(const ValueLatticeElement &Val,
803803
Type *Ty, const DataLayout &DL) {
804-
if (Val.isConstantRange())
804+
if (Val.isConstantRange(/*UndefAllowed*/ false))
805805
return Val.getConstantRange();
806806
return ConstantRange::getFull(DL.getTypeSizeInBits(Ty));
807807
}

llvm/test/Transforms/CorrelatedValuePropagation/merge-range-and-undef.ll

+37
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,40 @@ exit: ; CVP only simplifies based on ranges for non-local conditions.
382382
call void @use(i1 %t.1)
383383
ret i64 %res
384384
}
385+
386+
; Test case for PR68381.
387+
; Because of `undef`, we can only delete the second `and` instruction.
388+
define i32 @constant_range_and_undef_and(i1 %c0, i1 %c1, i8 %v1, i8 %v2) {
389+
; CHECK-LABEL: @constant_range_and_undef_and(
390+
; CHECK-NEXT: start:
391+
; CHECK-NEXT: br i1 [[C0:%.*]], label [[BB0:%.*]], label [[BB1:%.*]]
392+
; CHECK: bb0:
393+
; CHECK-NEXT: [[V1_I32:%.*]] = zext i8 [[V1:%.*]] to i32
394+
; CHECK-NEXT: br label [[BB1]]
395+
; CHECK: bb1:
396+
; CHECK-NEXT: [[X:%.*]] = phi i32 [ [[V1_I32]], [[BB0]] ], [ undef, [[START:%.*]] ]
397+
; CHECK-NEXT: br i1 [[C1:%.*]], label [[BB0]], label [[BB2:%.*]]
398+
; CHECK: bb2:
399+
; CHECK-NEXT: [[V2_I32:%.*]] = zext i8 [[V2:%.*]] to i32
400+
; CHECK-NEXT: [[Y:%.*]] = or i32 [[X]], [[V2_I32]]
401+
; CHECK-NEXT: [[Z:%.*]] = and i32 [[Y]], 255
402+
; CHECK-NEXT: ret i32 [[Z]]
403+
;
404+
start:
405+
br i1 %c0, label %bb0, label %bb1
406+
407+
bb0:
408+
%v1_i32 = zext i8 %v1 to i32
409+
br label %bb1
410+
411+
bb1:
412+
%x = phi i32 [ %v1_i32, %bb0 ], [ undef, %start ]
413+
br i1 %c1, label %bb0, label %bb2
414+
415+
bb2:
416+
%v2_i32 = zext i8 %v2 to i32
417+
%y = or i32 %x, %v2_i32
418+
%z = and i32 %y, 255
419+
%z1 = and i32 %z, 255
420+
ret i32 %z1
421+
}

0 commit comments

Comments
 (0)