Skip to content

Commit 015584b

Browse files
committed
[ValueTracking] Compute known bits from recursive select/phi
1 parent 5f0ad32 commit 015584b

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,13 @@ static void computeKnownBitsFromOperator(const Operator *I,
15661566
// Skip direct self references.
15671567
if (IncValue == P) continue;
15681568

1569+
// If the Use is a select of this phi, use the knownbit of the other
1570+
// operand to break the recursion.
1571+
Value *V;
1572+
if (match(IncValue, m_Select(m_Value(), m_Specific(P), m_Value(V))) ||
1573+
match(IncValue, m_Select(m_Value(), m_Value(V), m_Specific(P))))
1574+
IncValue = V;
1575+
15691576
// Change the context instruction to the "edge" that flows into the
15701577
// phi. This is important because that is where the value is actually
15711578
// "evaluated" even though it is used later somewhere else. (see also

llvm/test/Transforms/InstCombine/known-phi-recurse.ll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,11 @@ define i32 @knownbits_phi_select_test1(ptr %p1, ptr %p2, i8 %x) {
170170
; CHECK-NEXT: br i1 [[OR_COND]], label [[BB2]], label [[EXIT]]
171171
; CHECK: bb2:
172172
; CHECK-NEXT: [[CMP4:%.*]] = icmp eq i8 [[INDVAR1]], 39
173-
; CHECK-NEXT: [[CMP5:%.*]] = icmp eq i32 [[INDVAR4]], 0
174-
; CHECK-NEXT: [[EXT2:%.*]] = zext i1 [[CMP5]] to i32
175-
; CHECK-NEXT: [[INDVAR4_NEXT]] = select i1 [[CMP4]], i32 [[EXT2]], i32 [[INDVAR4]]
173+
; CHECK-NEXT: [[EXT2:%.*]] = zext i1 [[CMP4]] to i32
174+
; CHECK-NEXT: [[INDVAR4_NEXT]] = xor i32 [[INDVAR4]], [[EXT2]]
176175
; CHECK-NEXT: [[CMP6:%.*]] = icmp eq i8 [[INDVAR1]], 34
177-
; CHECK-NEXT: [[CMP7:%.*]] = icmp eq i32 [[INDVAR5]], 0
178-
; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[CMP7]] to i32
179-
; CHECK-NEXT: [[INDVAR5_NEXT]] = select i1 [[CMP6]], i32 [[EXT3]], i32 [[INDVAR5]]
176+
; CHECK-NEXT: [[EXT3:%.*]] = zext i1 [[CMP6]] to i32
177+
; CHECK-NEXT: [[INDVAR5_NEXT]] = xor i32 [[INDVAR5]], [[EXT3]]
180178
; CHECK-NEXT: [[INDVAR3_NEXT]] = getelementptr inbounds i8, ptr [[INDVAR3]], i64 1
181179
; CHECK-NEXT: [[LOAD2]] = load i8, ptr [[INDVAR3_NEXT]], align 1
182180
; CHECK-NEXT: br label [[LOOP]]
@@ -241,8 +239,7 @@ define i8 @knownbits_phi_select_test2() {
241239
; CHECK-NEXT: [[COND1:%.*]] = call i1 @cond()
242240
; CHECK-NEXT: br i1 [[COND1]], label [[EXIT:%.*]], label [[LOOP]]
243241
; CHECK: exit:
244-
; CHECK-NEXT: [[TMP0:%.*]] = and i8 [[CONTAIN]], 1
245-
; CHECK-NEXT: ret i8 [[TMP0]]
242+
; CHECK-NEXT: ret i8 [[CONTAIN]]
246243
;
247244
entry:
248245
br label %loop

0 commit comments

Comments
 (0)