Skip to content

Commit

Permalink
[LoopUnswitch] Simplify branch condition if it is select with constan…
Browse files Browse the repository at this point in the history
…t operands

This fixes the miscompilation reported in https://reviews.llvm.org/rG5bb38e84d3d0#986154 .

`select _, true, false` matches both m_LogicalAnd and m_LogicalOr, making later
transformations confused.
Simplify the branch condition to not have the form.
  • Loading branch information
aqjune committed Mar 30, 2021
1 parent f71ed5d commit 6b4b1dc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
Expand Up @@ -2645,6 +2645,13 @@ unswitchBestCondition(Loop &L, DominatorTree &DT, LoopInfo &LI,
BI->getSuccessor(0) == BI->getSuccessor(1))
continue;

// If BI's condition is 'select _, true, false', simplify it to confuse
// matchers
Value *Cond = BI->getCondition(), *CondNext;
while (match(Cond, m_Select(m_Value(CondNext), m_One(), m_Zero())))
Cond = CondNext;
BI->setCondition(Cond);

if (L.isLoopInvariant(BI->getCondition())) {
UnswitchCandidates.push_back({BI, {BI->getCondition()}});
continue;
Expand Down
Expand Up @@ -4248,7 +4248,7 @@ loop_begin:
; CHECK-NEXT: %[[V2:.*]] = load i1, i1* %ptr2
; CHECK-NEXT: %[[AND1:.*]] = select i1 %[[V1]], i1 true, i1 false
; CHECK-NEXT: %[[AND2:.*]] = select i1 %[[AND1]], i1 true, i1 false
; CHECK-NEXT: br i1 %[[AND2]], label %loop_a, label %loop_b
; CHECK-NEXT: br i1 %[[V1]], label %loop_a, label %loop_b

loop_a:
call i32 @a()
Expand Down Expand Up @@ -4326,7 +4326,7 @@ loop_begin:
; CHECK-NEXT: %[[V2:.*]] = load i1, i1* %ptr2
; CHECK-NEXT: %[[AND1:.*]] = select i1 %[[V1]], i1 true, i1 false
; CHECK-NEXT: %[[AND2:.*]] = select i1 %[[AND1]], i1 true, i1 false
; CHECK-NEXT: br i1 %[[AND2]], label %loop_b, label %loop_a
; CHECK-NEXT: br i1 %[[V1]], label %loop_b, label %loop_a

loop_a:
call i32 @a()
Expand Down

0 comments on commit 6b4b1dc

Please sign in to comment.