[llvm] 6b4b1dc - [LoopUnswitch] Simplify branch condition if it is select with constant operands

Juneyoung Lee via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 30 04:09:55 PDT 2021


Author: Juneyoung Lee
Date: 2021-03-30T20:09:42+09:00
New Revision: 6b4b1dc6ec6f0bf0a1bb414fbe751ccab99d41a0

URL: https://github.com/llvm/llvm-project/commit/6b4b1dc6ec6f0bf0a1bb414fbe751ccab99d41a0
DIFF: https://github.com/llvm/llvm-project/commit/6b4b1dc6ec6f0bf0a1bb414fbe751ccab99d41a0.diff

LOG: [LoopUnswitch] Simplify branch condition if it is select with constant 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.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
    llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
index cf77cf70e323..2e1c0dee32dc 100644
--- a/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
+++ b/llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
@@ -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;

diff  --git a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
index bfbe3e65daa6..58950835d585 100644
--- a/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
+++ b/llvm/test/Transforms/SimpleLoopUnswitch/nontrivial-unswitch.ll
@@ -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()
@@ -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()


        


More information about the llvm-commits mailing list