[llvm] 7b35676 - [InstCombine] Fold assume(false) to non-terminator unreachable
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 22 07:44:13 PDT 2023
Author: Nikita Popov
Date: 2023-06-22T16:44:05+02:00
New Revision: 7b356769fc724c07e1a8be1beb6243474da9aaff
URL: https://github.com/llvm/llvm-project/commit/7b356769fc724c07e1a8be1beb6243474da9aaff
DIFF: https://github.com/llvm/llvm-project/commit/7b356769fc724c07e1a8be1beb6243474da9aaff.diff
LOG: [InstCombine] Fold assume(false) to non-terminator unreachable
assume(false) is immediate UB, so fold it to (non-terminator)
unreachable.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/assume.ll
llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index c30c56a418ca5..ea35e7934f016 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2773,6 +2773,12 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
if (Known.isAllOnes() && isAssumeWithEmptyBundle(cast<AssumeInst>(*II)))
return eraseInstFromFunction(*II);
+ // assume(false) is unreachable.
+ if (match(IIOperand, m_CombineOr(m_Zero(), m_Undef()))) {
+ CreateNonTerminatorUnreachable(II);
+ return eraseInstFromFunction(*II);
+ }
+
// Update the cache of affected values for this assumption (we might be
// here because we just simplified the condition).
AC.updateAffectedValues(cast<AssumeInst>(II));
diff --git a/llvm/test/Transforms/InstCombine/assume.ll b/llvm/test/Transforms/InstCombine/assume.ll
index 48095cf8b13b1..d11928f62ce78 100644
--- a/llvm/test/Transforms/InstCombine/assume.ll
+++ b/llvm/test/Transforms/InstCombine/assume.ll
@@ -1,6 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt < %s -passes=instcombine -S -instcombine-infinite-loop-threshold=3 | FileCheck --check-prefixes=CHECK,DEFAULT %s
-; RUN: opt < %s -passes=instcombine --enable-knowledge-retention -S -instcombine-infinite-loop-threshold=3 | FileCheck --check-prefixes=CHECK,BUNDLES %s
+; RUN: opt < %s -passes=instcombine -S -instcombine-infinite-loop-threshold=2 | FileCheck --check-prefixes=CHECK,DEFAULT %s
+; RUN: opt < %s -passes=instcombine --enable-knowledge-retention -S -instcombine-infinite-loop-threshold=2 | FileCheck --check-prefixes=CHECK,BUNDLES %s
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
@@ -383,8 +383,8 @@ define i1 @nonnull5(ptr %a) {
define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
; CHECK-LABEL: @assumption_conflicts_with_known_bits(
-; CHECK-NEXT: tail call void @llvm.assume(i1 false)
-; CHECK-NEXT: ret i32 0
+; CHECK-NEXT: store i1 true, ptr poison, align 1
+; CHECK-NEXT: ret i32 poison
;
%and1 = and i32 %b, 3
%B1 = lshr i32 %and1, %and1
@@ -403,12 +403,8 @@ define i32 @assumption_conflicts_with_known_bits(i32 %a, i32 %b) {
define void @debug_interference(i8 %x) {
; CHECK-LABEL: @debug_interference(
-; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i8 [[X:%.*]], 0
; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata [[META7:![0-9]+]], metadata !DIExpression()), !dbg [[DBG9:![0-9]+]]
-; CHECK-NEXT: tail call void @llvm.assume(i1 false)
-; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata [[META7]], metadata !DIExpression()), !dbg [[DBG9]]
-; CHECK-NEXT: tail call void @llvm.dbg.value(metadata i32 5, metadata [[META7]], metadata !DIExpression()), !dbg [[DBG9]]
-; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP2]])
+; CHECK-NEXT: store i1 true, ptr poison, align 1
; CHECK-NEXT: ret void
;
%cmp1 = icmp eq i8 %x, 0
@@ -575,10 +571,8 @@ define void @always_true_assumption() {
define i64 @PR31809() {
; CHECK-LABEL: @PR31809(
-; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
-; CHECK-NEXT: [[T1:%.*]] = ptrtoint ptr [[A]] to i64
-; CHECK-NEXT: call void @llvm.assume(i1 false)
-; CHECK-NEXT: ret i64 [[T1]]
+; CHECK-NEXT: store i1 true, ptr poison, align 1
+; CHECK-NEXT: ret i64 poison
;
%a = alloca i32
%t1 = ptrtoint ptr %a to i64
@@ -592,10 +586,8 @@ define i64 @PR31809() {
define i8 @conflicting_assumptions(i8 %x){
; CHECK-LABEL: @conflicting_assumptions(
-; CHECK-NEXT: call void @llvm.assume(i1 false)
-; CHECK-NEXT: [[COND2:%.*]] = icmp eq i8 [[X:%.*]], 4
-; CHECK-NEXT: call void @llvm.assume(i1 [[COND2]])
-; CHECK-NEXT: ret i8 5
+; CHECK-NEXT: store i1 true, ptr poison, align 1
+; CHECK-NEXT: ret i8 poison
;
%add = add i8 %x, 1
%cond1 = icmp eq i8 %x, 3
diff --git a/llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll b/llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
index 3b01d5186d33f..d7c2bc7b2ae20 100644
--- a/llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
+++ b/llvm/test/Transforms/InstCombine/out-of-bounds-indexes.ll
@@ -5,9 +5,8 @@
define i32 @test_out_of_bounds(i32 %a, i1 %x, i1 %y) {
; CHECK-LABEL: @test_out_of_bounds(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[AND1:%.*]] = and i32 [[A:%.*]], 3
-; CHECK-NEXT: tail call void @llvm.assume(i1 poison)
-; CHECK-NEXT: ret i32 [[AND1]]
+; CHECK-NEXT: store i1 true, ptr poison, align 1
+; CHECK-NEXT: ret i32 poison
;
entry:
%and1 = and i32 %a, 3
@@ -19,9 +18,8 @@ entry:
define i128 @test_non64bit(i128 %a) {
; CHECK-LABEL: @test_non64bit(
-; CHECK-NEXT: [[AND1:%.*]] = and i128 [[A:%.*]], 3
-; CHECK-NEXT: tail call void @llvm.assume(i1 poison)
-; CHECK-NEXT: ret i128 [[AND1]]
+; CHECK-NEXT: store i1 true, ptr poison, align 1
+; CHECK-NEXT: ret i128 poison
;
%and1 = and i128 %a, 3
%B = lshr i128 %and1, -1
More information about the llvm-commits
mailing list