Skip to content

Commit 7b35676

Browse files
committed
[InstCombine] Fold assume(false) to non-terminator unreachable
assume(false) is immediate UB, so fold it to (non-terminator) unreachable.
1 parent 93407f7 commit 7b35676

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,6 +2773,12 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
27732773
if (Known.isAllOnes() && isAssumeWithEmptyBundle(cast<AssumeInst>(*II)))
27742774
return eraseInstFromFunction(*II);
27752775

2776+
// assume(false) is unreachable.
2777+
if (match(IIOperand, m_CombineOr(m_Zero(), m_Undef()))) {
2778+
CreateNonTerminatorUnreachable(II);
2779+
return eraseInstFromFunction(*II);
2780+
}
2781+
27762782
// Update the cache of affected values for this assumption (we might be
27772783
// here because we just simplified the condition).
27782784
AC.updateAffectedValues(cast<AssumeInst>(II));

llvm/test/Transforms/InstCombine/assume.ll

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt < %s -passes=instcombine -S -instcombine-infinite-loop-threshold=3 | FileCheck --check-prefixes=CHECK,DEFAULT %s
3-
; RUN: opt < %s -passes=instcombine --enable-knowledge-retention -S -instcombine-infinite-loop-threshold=3 | FileCheck --check-prefixes=CHECK,BUNDLES %s
2+
; RUN: opt < %s -passes=instcombine -S -instcombine-infinite-loop-threshold=2 | FileCheck --check-prefixes=CHECK,DEFAULT %s
3+
; RUN: opt < %s -passes=instcombine --enable-knowledge-retention -S -instcombine-infinite-loop-threshold=2 | FileCheck --check-prefixes=CHECK,BUNDLES %s
44

55
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
66
target triple = "x86_64-unknown-linux-gnu"
@@ -383,8 +383,8 @@ define i1 @nonnull5(ptr %a) {
383383

384384
define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
385385
; CHECK-LABEL: @assumption_conflicts_with_known_bits(
386-
; CHECK-NEXT: tail call void @llvm.assume(i1 false)
387-
; CHECK-NEXT: ret i32 0
386+
; CHECK-NEXT: store i1 true, ptr poison, align 1
387+
; CHECK-NEXT: ret i32 poison
388388
;
389389
%and1 = and i32 %b, 3
390390
%B1 = lshr i32 %and1, %and1
@@ -403,12 +403,8 @@ define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
403403

404404
define void @debug_interference(i8 %x) {
405405
; CHECK-LABEL: @debug_interference(
406-
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[X:%.*]], 0
407406
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata [[META7:![0-9]+]], metadata !DIExpression()), !dbg [[DBG9:![0-9]+]]
408-
; CHECK-NEXT: tail call void @llvm.assume(i1 false)
409-
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata [[META7]], metadata !DIExpression()), !dbg [[DBG9]]
410-
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata [[META7]], metadata !DIExpression()), !dbg [[DBG9]]
411-
; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP2]])
407+
; CHECK-NEXT: store i1 true, ptr poison, align 1
412408
; CHECK-NEXT: ret void
413409
;
414410
%cmp1 = icmp eq i8 %x, 0
@@ -575,10 +571,8 @@ define void @always_true_assumption() {
575571

576572
define i64 @PR31809() {
577573
; CHECK-LABEL: @PR31809(
578-
; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
579-
; CHECK-NEXT: [[T1:%.*]] = ptrtoint ptr [[A]] to i64
580-
; CHECK-NEXT: call void @llvm.assume(i1 false)
581-
; CHECK-NEXT: ret i64 [[T1]]
574+
; CHECK-NEXT: store i1 true, ptr poison, align 1
575+
; CHECK-NEXT: ret i64 poison
582576
;
583577
%a = alloca i32
584578
%t1 = ptrtoint ptr %a to i64
@@ -592,10 +586,8 @@ define i64 @PR31809() {
592586

593587
define i8 @conflicting_assumptions(i8 %x){
594588
; CHECK-LABEL: @conflicting_assumptions(
595-
; CHECK-NEXT: call void @llvm.assume(i1 false)
596-
; CHECK-NEXT: [[COND2:%.*]] = icmp eq i8 [[X:%.*]], 4
597-
; CHECK-NEXT: call void @llvm.assume(i1 [[COND2]])
598-
; CHECK-NEXT: ret i8 5
589+
; CHECK-NEXT: store i1 true, ptr poison, align 1
590+
; CHECK-NEXT: ret i8 poison
599591
;
600592
%add = add i8 %x, 1
601593
%cond1 = icmp eq i8 %x, 3

llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
define i32 @test_out_of_bounds(i32 %a, i1 %x, i1 %y) {
66
; CHECK-LABEL: @test_out_of_bounds(
77
; CHECK-NEXT: entry:
8-
; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], 3
9-
; CHECK-NEXT: tail call void @llvm.assume(i1 poison)
10-
; CHECK-NEXT: ret i32 [[AND1]]
8+
; CHECK-NEXT: store i1 true, ptr poison, align 1
9+
; CHECK-NEXT: ret i32 poison
1110
;
1211
entry:
1312
%and1 = and i32 %a, 3
@@ -19,9 +18,8 @@ entry:
1918

2019
define i128 @test_non64bit(i128 %a) {
2120
; CHECK-LABEL: @test_non64bit(
22-
; CHECK-NEXT: [[AND1:%.*]] = and i128 [[A:%.*]], 3
23-
; CHECK-NEXT: tail call void @llvm.assume(i1 poison)
24-
; CHECK-NEXT: ret i128 [[AND1]]
21+
; CHECK-NEXT: store i1 true, ptr poison, align 1
22+
; CHECK-NEXT: ret i128 poison
2523
;
2624
%and1 = and i128 %a, 3
2725
%B = lshr i128 %and1, -1

0 commit comments

Comments
 (0)