[llvm] [ValueTracking] Compute known bits from recursive select/phi (PR #113707)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 26 08:28:51 PDT 2024
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/113707
>From 5f0ad3232bf1ebab020284895b61c5b8dea6f4d1 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 26 Oct 2024 00:16:13 +0800
Subject: [PATCH 1/6] [InstCombine] Add pre-commit tests. NFC.
---
.../InstCombine/known-phi-recurse.ll | 118 ++++++++++++++++++
1 file changed, 118 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/known-phi-recurse.ll b/llvm/test/Transforms/InstCombine/known-phi-recurse.ll
index c2007d16ae93be..7604f3f0c17849 100644
--- a/llvm/test/Transforms/InstCombine/known-phi-recurse.ll
+++ b/llvm/test/Transforms/InstCombine/known-phi-recurse.ll
@@ -142,3 +142,121 @@ end:
ret i32 %res
}
+define i32 @knownbits_phi_select_test1(ptr %p1, ptr %p2, i8 %x) {
+; CHECK-LABEL: @knownbits_phi_select_test1(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[LOOP:%.*]]
+; CHECK: loop:
+; CHECK-NEXT: [[INDVAR1:%.*]] = phi i8 [ [[LOAD2:%.*]], [[BB2:%.*]] ], [ [[X:%.*]], [[ENTRY:%.*]] ]
+; CHECK-NEXT: [[INDVAR3:%.*]] = phi ptr [ [[INDVAR3_NEXT:%.*]], [[BB2]] ], [ [[P1:%.*]], [[ENTRY]] ]
+; CHECK-NEXT: [[INDVAR4:%.*]] = phi i32 [ [[INDVAR4_NEXT:%.*]], [[BB2]] ], [ 0, [[ENTRY]] ]
+; CHECK-NEXT: [[INDVAR5:%.*]] = phi i32 [ [[INDVAR5_NEXT:%.*]], [[BB2]] ], [ 0, [[ENTRY]] ]
+; CHECK-NEXT: switch i8 [[INDVAR1]], label [[DEFAULT:%.*]] [
+; CHECK-NEXT: i8 0, label [[EXIT:%.*]]
+; CHECK-NEXT: i8 59, label [[BB1:%.*]]
+; CHECK-NEXT: i8 35, label [[BB1]]
+; CHECK-NEXT: ]
+; CHECK: default:
+; CHECK-NEXT: [[EXT:%.*]] = sext i8 [[INDVAR1]] to i64
+; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i16, ptr [[P2:%.*]], i64 [[EXT]]
+; CHECK-NEXT: [[LOAD1:%.*]] = load i16, ptr [[GEP1]], align 2
+; CHECK-NEXT: [[MASK:%.*]] = and i16 [[LOAD1]], 8192
+; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i16 [[MASK]], 0
+; CHECK-NEXT: br i1 [[CMP1]], label [[BB2]], label [[BB1]]
+; CHECK: bb1:
+; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[INDVAR4]], 0
+; CHECK-NEXT: [[CMP3:%.*]] = icmp ne i32 [[INDVAR5]], 0
+; CHECK-NEXT: [[OR_COND:%.*]] = select i1 [[CMP2]], i1 true, i1 [[CMP3]]
+; CHECK-NEXT: br i1 [[OR_COND]], label [[BB2]], label [[EXIT]]
+; CHECK: bb2:
+; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i8 [[INDVAR1]], 39
+; CHECK-NEXT: [[CMP5:%.*]] = icmp eq i32 [[INDVAR4]], 0
+; CHECK-NEXT: [[EXT2:%.*]] = zext i1 [[CMP5]] to i32
+; CHECK-NEXT: [[INDVAR4_NEXT]] = select i1 [[CMP4]], i32 [[EXT2]], i32 [[INDVAR4]]
+; CHECK-NEXT: [[CMP6:%.*]] = icmp eq i8 [[INDVAR1]], 34
+; CHECK-NEXT: [[CMP7:%.*]] = icmp eq i32 [[INDVAR5]], 0
+; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[CMP7]] to i32
+; CHECK-NEXT: [[INDVAR5_NEXT]] = select i1 [[CMP6]], i32 [[EXT3]], i32 [[INDVAR5]]
+; CHECK-NEXT: [[INDVAR3_NEXT]] = getelementptr inbounds i8, ptr [[INDVAR3]], i64 1
+; CHECK-NEXT: [[LOAD2]] = load i8, ptr [[INDVAR3_NEXT]], align 1
+; CHECK-NEXT: br label [[LOOP]]
+; CHECK: exit:
+; CHECK-NEXT: ret i32 [[INDVAR5]]
+;
+entry:
+ br label %loop
+
+loop:
+ %indvar1 = phi i8 [ %load2, %bb2 ], [ %x, %entry ]
+ %indvar2 = phi i64 [ %indvar2.next, %bb2 ], [ 0, %entry ]
+ %indvar3 = phi ptr [ %indvar3.next, %bb2 ], [ %p1, %entry ]
+ %indvar4 = phi i32 [ %indvar4.next, %bb2 ], [ 0, %entry ]
+ %indvar5 = phi i32 [ %indvar5.next, %bb2 ], [ 0, %entry ]
+ switch i8 %indvar1, label %default [
+ i8 0, label %exit
+ i8 59, label %bb1
+ i8 35, label %bb1
+ ]
+
+default:
+ %ext = sext i8 %indvar1 to i64
+ %gep1 = getelementptr inbounds i16, ptr %p2, i64 %ext
+ %load1 = load i16, ptr %gep1, align 2
+ %mask = and i16 %load1, 8192
+ %cmp1 = icmp eq i16 %mask, 0
+ br i1 %cmp1, label %bb2, label %bb1
+
+bb1:
+ %cmp2 = icmp ne i32 %indvar4, 0
+ %cmp3 = icmp ne i32 %indvar5, 0
+ %or.cond = select i1 %cmp2, i1 true, i1 %cmp3
+ br i1 %or.cond, label %bb2, label %exit
+
+bb2:
+ %cmp4 = icmp eq i8 %indvar1, 39
+ %cmp5 = icmp eq i32 %indvar4, 0
+ %ext2 = zext i1 %cmp5 to i32
+ %indvar4.next = select i1 %cmp4, i32 %ext2, i32 %indvar4
+ %cmp6 = icmp eq i8 %indvar1, 34
+ %cmp7 = icmp eq i32 %indvar5, 0
+ %ext3 = zext i1 %cmp7 to i32
+ %indvar5.next = select i1 %cmp6, i32 %ext3, i32 %indvar5
+ %indvar3.next = getelementptr inbounds i8, ptr %indvar3, i64 1
+ %indvar2.next = add i64 %indvar2, 1
+ %load2 = load i8, ptr %indvar3.next, align 1
+ br label %loop
+
+exit:
+ ret i32 %indvar5
+}
+
+define i8 @knownbits_phi_select_test2() {
+; CHECK-LABEL: @knownbits_phi_select_test2(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: br label [[LOOP:%.*]]
+; CHECK: loop:
+; CHECK-NEXT: [[INDVAR:%.*]] = phi i8 [ 0, [[ENTRY:%.*]] ], [ [[CONTAIN:%.*]], [[LOOP]] ]
+; CHECK-NEXT: [[COND0:%.*]] = call i1 @cond()
+; CHECK-NEXT: [[CONTAIN]] = select i1 [[COND0]], i8 1, i8 [[INDVAR]]
+; CHECK-NEXT: [[COND1:%.*]] = call i1 @cond()
+; CHECK-NEXT: br i1 [[COND1]], label [[EXIT:%.*]], label [[LOOP]]
+; CHECK: exit:
+; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[CONTAIN]], 1
+; CHECK-NEXT: ret i8 [[TMP0]]
+;
+entry:
+ br label %loop
+
+loop:
+ %indvar = phi i8 [ 0, %entry ], [ %contain, %loop ]
+ %cond0 = call i1 @cond()
+ %contain = select i1 %cond0, i8 1, i8 %indvar
+ %cond1 = call i1 @cond()
+ br i1 %cond1, label %exit, label %loop
+
+exit:
+ %bool = and i8 %contain, 1
+ ret i8 %bool
+}
+
+declare i1 @cond()
>From 015584b9b69e3f06e77f59f138cda5ba21b2bca3 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 26 Oct 2024 00:23:35 +0800
Subject: [PATCH 2/6] [ValueTracking] Compute known bits from recursive
select/phi
---
llvm/lib/Analysis/ValueTracking.cpp | 7 +++++++
.../Transforms/InstCombine/known-phi-recurse.ll | 13 +++++--------
2 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e9ed8b3c862b55..e443488a56b3cb 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1566,6 +1566,13 @@ static void computeKnownBitsFromOperator(const Operator *I,
// Skip direct self references.
if (IncValue == P) continue;
+ // If the Use is a select of this phi, use the knownbit of the other
+ // operand to break the recursion.
+ Value *V;
+ if (match(IncValue, m_Select(m_Value(), m_Specific(P), m_Value(V))) ||
+ match(IncValue, m_Select(m_Value(), m_Value(V), m_Specific(P))))
+ IncValue = V;
+
// Change the context instruction to the "edge" that flows into the
// phi. This is important because that is where the value is actually
// "evaluated" even though it is used later somewhere else. (see also
diff --git a/llvm/test/Transforms/InstCombine/known-phi-recurse.ll b/llvm/test/Transforms/InstCombine/known-phi-recurse.ll
index 7604f3f0c17849..fd3728324b8ea8 100644
--- a/llvm/test/Transforms/InstCombine/known-phi-recurse.ll
+++ b/llvm/test/Transforms/InstCombine/known-phi-recurse.ll
@@ -170,13 +170,11 @@ define i32 @knownbits_phi_select_test1(ptr %p1, ptr %p2, i8 %x) {
; CHECK-NEXT: br i1 [[OR_COND]], label [[BB2]], label [[EXIT]]
; CHECK: bb2:
; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i8 [[INDVAR1]], 39
-; CHECK-NEXT: [[CMP5:%.*]] = icmp eq i32 [[INDVAR4]], 0
-; CHECK-NEXT: [[EXT2:%.*]] = zext i1 [[CMP5]] to i32
-; CHECK-NEXT: [[INDVAR4_NEXT]] = select i1 [[CMP4]], i32 [[EXT2]], i32 [[INDVAR4]]
+; CHECK-NEXT: [[EXT2:%.*]] = zext i1 [[CMP4]] to i32
+; CHECK-NEXT: [[INDVAR4_NEXT]] = xor i32 [[INDVAR4]], [[EXT2]]
; CHECK-NEXT: [[CMP6:%.*]] = icmp eq i8 [[INDVAR1]], 34
-; CHECK-NEXT: [[CMP7:%.*]] = icmp eq i32 [[INDVAR5]], 0
-; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[CMP7]] to i32
-; CHECK-NEXT: [[INDVAR5_NEXT]] = select i1 [[CMP6]], i32 [[EXT3]], i32 [[INDVAR5]]
+; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[CMP6]] to i32
+; CHECK-NEXT: [[INDVAR5_NEXT]] = xor i32 [[INDVAR5]], [[EXT3]]
; CHECK-NEXT: [[INDVAR3_NEXT]] = getelementptr inbounds i8, ptr [[INDVAR3]], i64 1
; CHECK-NEXT: [[LOAD2]] = load i8, ptr [[INDVAR3_NEXT]], align 1
; CHECK-NEXT: br label [[LOOP]]
@@ -241,8 +239,7 @@ define i8 @knownbits_phi_select_test2() {
; CHECK-NEXT: [[COND1:%.*]] = call i1 @cond()
; CHECK-NEXT: br i1 [[COND1]], label [[EXIT:%.*]], label [[LOOP]]
; CHECK: exit:
-; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[CONTAIN]], 1
-; CHECK-NEXT: ret i8 [[TMP0]]
+; CHECK-NEXT: ret i8 [[CONTAIN]]
;
entry:
br label %loop
>From b66b3ca6808e0485f349009447584effc27488d2 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 26 Oct 2024 16:53:57 +0800
Subject: [PATCH 3/6] [ValueTracking] Increase recursion depth. NFC.
---
llvm/lib/Analysis/ValueTracking.cpp | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index e443488a56b3cb..d04884a2464bcd 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1568,10 +1568,13 @@ static void computeKnownBitsFromOperator(const Operator *I,
// If the Use is a select of this phi, use the knownbit of the other
// operand to break the recursion.
+ unsigned IncDepth = MaxAnalysisRecursionDepth - 1;
Value *V;
if (match(IncValue, m_Select(m_Value(), m_Specific(P), m_Value(V))) ||
- match(IncValue, m_Select(m_Value(), m_Value(V), m_Specific(P))))
+ match(IncValue, m_Select(m_Value(), m_Value(V), m_Specific(P)))) {
IncValue = V;
+ IncDepth = Depth + 1;
+ }
// Change the context instruction to the "edge" that flows into the
// phi. This is important because that is where the value is actually
@@ -1586,8 +1589,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
// want to waste time spinning around in loops.
// TODO: See if we can base recursion limiter on number of incoming phi
// edges so we don't overly clamp analysis.
- computeKnownBits(IncValue, DemandedElts, Known2,
- MaxAnalysisRecursionDepth - 1, RecQ);
+ computeKnownBits(IncValue, DemandedElts, Known2, IncDepth, RecQ);
// See if we can further use a conditional branch into the phi
// to help us determine the range of the value.
>From d990febe06a684509c77df642ce986f65b0faed9 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 26 Oct 2024 20:06:36 +0800
Subject: [PATCH 4/6] [ValueTracking] Adjust comments. NFC.
---
llvm/lib/Analysis/ValueTracking.cpp | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index d04884a2464bcd..bff2acb0f33c16 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1566,9 +1566,14 @@ static void computeKnownBitsFromOperator(const Operator *I,
// Skip direct self references.
if (IncValue == P) continue;
+ // Recurse, but cap the recursion to one level, because we don't
+ // want to waste time spinning around in loops.
+ // TODO: See if we can base recursion limiter on number of incoming phi
+ // edges so we don't overly clamp analysis.
+ unsigned IncDepth = MaxAnalysisRecursionDepth - 1;
+
// If the Use is a select of this phi, use the knownbit of the other
// operand to break the recursion.
- unsigned IncDepth = MaxAnalysisRecursionDepth - 1;
Value *V;
if (match(IncValue, m_Select(m_Value(), m_Specific(P), m_Value(V))) ||
match(IncValue, m_Select(m_Value(), m_Value(V), m_Specific(P)))) {
@@ -1584,11 +1589,6 @@ static void computeKnownBitsFromOperator(const Operator *I,
RecQ.CxtI = P->getIncomingBlock(u)->getTerminator();
Known2 = KnownBits(BitWidth);
-
- // Recurse, but cap the recursion to one level, because we don't
- // want to waste time spinning around in loops.
- // TODO: See if we can base recursion limiter on number of incoming phi
- // edges so we don't overly clamp analysis.
computeKnownBits(IncValue, DemandedElts, Known2, IncDepth, RecQ);
// See if we can further use a conditional branch into the phi
>From 5751edbea615bf41c0aa86f4366afcf04c2373cc Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 26 Oct 2024 20:36:39 +0800
Subject: [PATCH 5/6] [ValueTracking] Adjust known bits from select's condition
---
llvm/lib/Analysis/ValueTracking.cpp | 13 +++++--
.../X86/2012-01-10-UndefExceptionEdge.ll | 39 ++++++++-----------
.../Transforms/InstCombine/cast-mul-select.ll | 20 +++-------
3 files changed, 31 insertions(+), 41 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index bff2acb0f33c16..6baecdb63456f6 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1574,11 +1574,13 @@ static void computeKnownBitsFromOperator(const Operator *I,
// If the Use is a select of this phi, use the knownbit of the other
// operand to break the recursion.
- Value *V;
- if (match(IncValue, m_Select(m_Value(), m_Specific(P), m_Value(V))) ||
- match(IncValue, m_Select(m_Value(), m_Value(V), m_Specific(P)))) {
- IncValue = V;
+ Value *IncCond = nullptr;
+ bool InvertIncCond = false;
+ if (auto *SI = dyn_cast<SelectInst>(IncValue)) {
+ InvertIncCond = SI->getTrueValue() == P;
+ IncValue = InvertIncCond ? SI->getFalseValue() : SI->getTrueValue();
IncDepth = Depth + 1;
+ IncCond = SI->getCondition();
}
// Change the context instruction to the "edge" that flows into the
@@ -1590,6 +1592,9 @@ static void computeKnownBitsFromOperator(const Operator *I,
Known2 = KnownBits(BitWidth);
computeKnownBits(IncValue, DemandedElts, Known2, IncDepth, RecQ);
+ if (IncCond)
+ adjustKnownBitsForSelectArm(Known2, IncCond, IncValue, InvertIncCond,
+ Depth, RecQ);
// See if we can further use a conditional branch into the phi
// to help us determine the range of the value.
diff --git a/llvm/test/CodeGen/X86/2012-01-10-UndefExceptionEdge.ll b/llvm/test/CodeGen/X86/2012-01-10-UndefExceptionEdge.ll
index 1962ddebc2115e..01a8111ebe0960 100644
--- a/llvm/test/CodeGen/X86/2012-01-10-UndefExceptionEdge.ll
+++ b/llvm/test/CodeGen/X86/2012-01-10-UndefExceptionEdge.ll
@@ -29,32 +29,29 @@ define void @f(ptr nocapture %arg, ptr nocapture %arg1, ptr nocapture %arg2, ptr
; CHECK-NEXT: pushl %ebx
; CHECK-NEXT: pushl %edi
; CHECK-NEXT: pushl %esi
-; CHECK-NEXT: subl $28, %esp
+; CHECK-NEXT: subl $12, %esp
; CHECK-NEXT: .cfi_offset %esi, -20
; CHECK-NEXT: .cfi_offset %edi, -16
; CHECK-NEXT: .cfi_offset %ebx, -12
; CHECK-NEXT: xorl %eax, %eax
-; CHECK-NEXT: xorl %edi, %edi
; CHECK-NEXT: testb %al, %al
; CHECK-NEXT: Ltmp0:
-; CHECK-NEXT: ## implicit-def: $ebx
+; CHECK-NEXT: ## implicit-def: $edi
; CHECK-NEXT: calll __Znam
; CHECK-NEXT: Ltmp1:
; CHECK-NEXT: ## %bb.1: ## %bb11
; CHECK-NEXT: movl %eax, %esi
-; CHECK-NEXT: movb $1, %al
-; CHECK-NEXT: testb %al, %al
+; CHECK-NEXT: movb $1, %bl
+; CHECK-NEXT: testb %bl, %bl
; CHECK-NEXT: jne LBB0_2
; CHECK-NEXT: ## %bb.7: ## %bb31
-; CHECK-NEXT: ## implicit-def: $eax
-; CHECK-NEXT: ## kill: killed $eax
+; CHECK-NEXT: ## implicit-def: $edi
; CHECK-NEXT: LBB0_8: ## %bb38
; CHECK-NEXT: ## =>This Loop Header: Depth=1
; CHECK-NEXT: ## Child Loop BB0_13 Depth 2
; CHECK-NEXT: ## Child Loop BB0_16 Depth 3
; CHECK-NEXT: ## Child Loop BB0_21 Depth 2
-; CHECK-NEXT: movb $1, %al
-; CHECK-NEXT: testb %al, %al
+; CHECK-NEXT: testb %bl, %bl
; CHECK-NEXT: jne LBB0_9
; CHECK-NEXT: ## %bb.10: ## %bb41
; CHECK-NEXT: ## in Loop: Header=BB0_8 Depth=1
@@ -78,8 +75,7 @@ define void @f(ptr nocapture %arg, ptr nocapture %arg1, ptr nocapture %arg2, ptr
; CHECK-NEXT: ## Parent Loop BB0_8 Depth=1
; CHECK-NEXT: ## => This Loop Header: Depth=2
; CHECK-NEXT: ## Child Loop BB0_16 Depth 3
-; CHECK-NEXT: movb $1, %cl
-; CHECK-NEXT: testb %cl, %cl
+; CHECK-NEXT: testb %bl, %bl
; CHECK-NEXT: jne LBB0_19
; CHECK-NEXT: ## %bb.14: ## %bb48
; CHECK-NEXT: ## in Loop: Header=BB0_13 Depth=2
@@ -88,14 +84,13 @@ define void @f(ptr nocapture %arg, ptr nocapture %arg1, ptr nocapture %arg2, ptr
; CHECK-NEXT: ## in Loop: Header=BB0_13 Depth=2
; CHECK-NEXT: xorl %ecx, %ecx
; CHECK-NEXT: movl %esi, %edx
-; CHECK-NEXT: movl %edi, %ebx
; CHECK-NEXT: LBB0_16: ## %bb49
; CHECK-NEXT: ## Parent Loop BB0_8 Depth=1
; CHECK-NEXT: ## Parent Loop BB0_13 Depth=2
; CHECK-NEXT: ## => This Inner Loop Header: Depth=3
; CHECK-NEXT: incl %ecx
; CHECK-NEXT: addl $4, %edx
-; CHECK-NEXT: decl %ebx
+; CHECK-NEXT: testb %bl, %bl
; CHECK-NEXT: jne LBB0_16
; CHECK-NEXT: LBB0_17: ## %bb57
; CHECK-NEXT: ## in Loop: Header=BB0_13 Depth=2
@@ -107,33 +102,31 @@ define void @f(ptr nocapture %arg, ptr nocapture %arg1, ptr nocapture %arg2, ptr
; CHECK-NEXT: movl %eax, {{[0-9]+}}(%esp)
; CHECK-NEXT: movl $0, (%esp)
; CHECK-NEXT: calll ___bzero
-; CHECK-NEXT: movb $1, %al
-; CHECK-NEXT: testb %al, %al
+; CHECK-NEXT: testb %bl, %bl
; CHECK-NEXT: jne LBB0_22
; CHECK-NEXT: ## %bb.20: ## %bb61.preheader
; CHECK-NEXT: ## in Loop: Header=BB0_8 Depth=1
; CHECK-NEXT: movl %esi, %eax
-; CHECK-NEXT: movl %edi, %ecx
; CHECK-NEXT: LBB0_21: ## %bb61
; CHECK-NEXT: ## Parent Loop BB0_8 Depth=1
; CHECK-NEXT: ## => This Inner Loop Header: Depth=2
; CHECK-NEXT: movl $0, (%eax)
; CHECK-NEXT: addl $4, %eax
-; CHECK-NEXT: decl %ecx
+; CHECK-NEXT: testb %bl, %bl
; CHECK-NEXT: jne LBB0_21
; CHECK-NEXT: LBB0_22: ## %bb67
; CHECK-NEXT: ## in Loop: Header=BB0_8 Depth=1
-; CHECK-NEXT: decl {{[-0-9]+}}(%e{{[sb]}}p) ## 4-byte Folded Spill
+; CHECK-NEXT: decl %edi
; CHECK-NEXT: jmp LBB0_8
; CHECK-NEXT: LBB0_18: ## %bb43
; CHECK-NEXT: Ltmp5:
-; CHECK-NEXT: movl %esi, %ebx
+; CHECK-NEXT: movl %esi, %edi
; CHECK-NEXT: calll _OnOverFlow
; CHECK-NEXT: Ltmp6:
; CHECK-NEXT: jmp LBB0_3
; CHECK-NEXT: LBB0_2: ## %bb29
; CHECK-NEXT: Ltmp7:
-; CHECK-NEXT: movl %esi, %ebx
+; CHECK-NEXT: movl %esi, %edi
; CHECK-NEXT: calll _OnOverFlow
; CHECK-NEXT: Ltmp8:
; CHECK-NEXT: LBB0_3: ## %bb30
@@ -141,10 +134,10 @@ define void @f(ptr nocapture %arg, ptr nocapture %arg1, ptr nocapture %arg2, ptr
; CHECK-NEXT: LBB0_4: ## %bb20.loopexit
; CHECK-NEXT: Ltmp4:
; CHECK-NEXT: LBB0_9:
-; CHECK-NEXT: movl %esi, %ebx
+; CHECK-NEXT: movl %esi, %edi
; CHECK-NEXT: LBB0_6: ## %bb23
-; CHECK-NEXT: testl %ebx, %ebx
-; CHECK-NEXT: addl $28, %esp
+; CHECK-NEXT: testl %edi, %edi
+; CHECK-NEXT: addl $12, %esp
; CHECK-NEXT: popl %esi
; CHECK-NEXT: popl %edi
; CHECK-NEXT: popl %ebx
diff --git a/llvm/test/Transforms/InstCombine/cast-mul-select.ll b/llvm/test/Transforms/InstCombine/cast-mul-select.ll
index 6eb3a8c0a2049b..0c3a80dd247e95 100644
--- a/llvm/test/Transforms/InstCombine/cast-mul-select.ll
+++ b/llvm/test/Transforms/InstCombine/cast-mul-select.ll
@@ -182,8 +182,6 @@ define void @PR36225(i32 %a, i32 %b, i1 %c1, i3 %v1, i3 %v2) {
; CHECK: while.body:
; CHECK-NEXT: br i1 [[C1:%.*]], label [[FOR_BODY3_US:%.*]], label [[FOR_BODY3:%.*]]
; CHECK: for.body3.us:
-; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[B:%.*]], 0
-; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i8 0, i8 4
; CHECK-NEXT: switch i3 [[V1:%.*]], label [[EXIT:%.*]] [
; CHECK-NEXT: i3 0, label [[FOR_END:%.*]]
; CHECK-NEXT: i3 -1, label [[FOR_END]]
@@ -194,9 +192,7 @@ define void @PR36225(i32 %a, i32 %b, i1 %c1, i3 %v1, i3 %v2) {
; CHECK-NEXT: i3 -1, label [[FOR_END]]
; CHECK-NEXT: ]
; CHECK: for.end:
-; CHECK-NEXT: [[H:%.*]] = phi i8 [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ 0, [[FOR_BODY3]] ], [ 0, [[FOR_BODY3]] ]
-; CHECK-NEXT: [[CONV:%.*]] = zext nneg i8 [[H]] to i32
-; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], [[CONV]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], 0
; CHECK-NEXT: br i1 [[CMP]], label [[EXIT]], label [[EXIT2:%.*]]
; CHECK: exit2:
; CHECK-NEXT: unreachable
@@ -210,10 +206,8 @@ define void @PR36225(i32 %a, i32 %b, i1 %c1, i3 %v1, i3 %v2) {
; DBGINFO-NEXT: #dbg_value(i32 [[B:%.*]], [[META89:![0-9]+]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), [[META95:![0-9]+]])
; DBGINFO-NEXT: br i1 [[C1:%.*]], label [[FOR_BODY3_US:%.*]], label [[FOR_BODY3:%.*]], !dbg [[DBG96:![0-9]+]]
; DBGINFO: for.body3.us:
-; DBGINFO-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[B]], 0, !dbg [[META95]]
-; DBGINFO-NEXT: #dbg_value(i1 [[TOBOOL]], [[META89]], !DIExpression(), [[META95]])
-; DBGINFO-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[TOBOOL]], i8 0, i8 4, !dbg [[DBG97:![0-9]+]]
-; DBGINFO-NEXT: #dbg_value(i8 [[SPEC_SELECT]], [[META90:![0-9]+]], !DIExpression(), [[DBG97]])
+; DBGINFO-NEXT: #dbg_value(i32 [[B]], [[META89]], !DIExpression(DW_OP_lit0, DW_OP_eq, DW_OP_stack_value), [[META95]])
+; DBGINFO-NEXT: #dbg_value(i8 poison, [[META90:![0-9]+]], !DIExpression(), [[META97:![0-9]+]])
; DBGINFO-NEXT: switch i3 [[V1:%.*]], label [[EXIT:%.*]] [
; DBGINFO-NEXT: i3 0, label [[FOR_END:%.*]]
; DBGINFO-NEXT: i3 -1, label [[FOR_END]]
@@ -224,11 +218,9 @@ define void @PR36225(i32 %a, i32 %b, i1 %c1, i3 %v1, i3 %v2) {
; DBGINFO-NEXT: i3 -1, label [[FOR_END]]
; DBGINFO-NEXT: ], !dbg [[DBG99:![0-9]+]]
; DBGINFO: for.end:
-; DBGINFO-NEXT: [[H:%.*]] = phi i8 [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ [[SPEC_SELECT]], [[FOR_BODY3_US]] ], [ 0, [[FOR_BODY3]] ], [ 0, [[FOR_BODY3]] ], !dbg [[DBG100:![0-9]+]]
-; DBGINFO-NEXT: #dbg_value(i8 [[H]], [[META91:![0-9]+]], !DIExpression(), [[DBG100]])
-; DBGINFO-NEXT: [[CONV:%.*]] = zext nneg i8 [[H]] to i32, !dbg [[DBG101:![0-9]+]]
-; DBGINFO-NEXT: #dbg_value(i32 [[CONV]], [[META92:![0-9]+]], !DIExpression(), [[DBG101]])
-; DBGINFO-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], [[CONV]], !dbg [[DBG102:![0-9]+]]
+; DBGINFO-NEXT: #dbg_value(i8 poison, [[META91:![0-9]+]], !DIExpression(), [[META100:![0-9]+]])
+; DBGINFO-NEXT: #dbg_value(i8 0, [[META92:![0-9]+]], !DIExpression(DW_OP_LLVM_convert, 8, DW_ATE_unsigned, DW_OP_LLVM_convert, 32, DW_ATE_unsigned, DW_OP_stack_value), [[META101:![0-9]+]])
+; DBGINFO-NEXT: [[CMP:%.*]] = icmp sgt i32 [[A:%.*]], 0, !dbg [[DBG102:![0-9]+]]
; DBGINFO-NEXT: #dbg_value(i1 [[CMP]], [[META93:![0-9]+]], !DIExpression(), [[DBG102]])
; DBGINFO-NEXT: br i1 [[CMP]], label [[EXIT]], label [[EXIT2:%.*]], !dbg [[DBG103:![0-9]+]]
; DBGINFO: exit2:
>From a5567a2119cf783d482c9152ac958fb841f87d91 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sat, 26 Oct 2024 23:28:23 +0800
Subject: [PATCH 6/6] [ValueTracking] Fix depth
---
llvm/lib/Analysis/ValueTracking.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 6baecdb63456f6..51e1a0c2b53de0 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -1594,7 +1594,7 @@ static void computeKnownBitsFromOperator(const Operator *I,
computeKnownBits(IncValue, DemandedElts, Known2, IncDepth, RecQ);
if (IncCond)
adjustKnownBitsForSelectArm(Known2, IncCond, IncValue, InvertIncCond,
- Depth, RecQ);
+ IncDepth, RecQ);
// See if we can further use a conditional branch into the phi
// to help us determine the range of the value.
More information about the llvm-commits
mailing list