[PATCH] D106779: [SimplifyCFG] Make ValueEqaulityComparison freeze-aware.

Hyeongyu Kim via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 25 20:44:40 PDT 2021


hyeongyukim created this revision.
Herald added a subscriber: hiraditya.
hyeongyukim requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

    cond1.fr = freeze(%v != 0)
    cond2.fr = freeze(%v == 0)
    br i1 %cond1.fr, label %body1, label %split
  
  split:
    br %cond2.fr, label %body2, label %body3

`%cond2.fr` is always true in the `split` block.
But currently, this analysis is not performed when conditions are frozen.
This problem can be solved by modifying `isValueEqualityComparison` and `isValueEqualityComparison`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106779

Files:
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/branch-fold.ll


Index: llvm/test/Transforms/SimplifyCFG/branch-fold.ll
===================================================================
--- llvm/test/Transforms/SimplifyCFG/branch-fold.ll
+++ llvm/test/Transforms/SimplifyCFG/branch-fold.ll
@@ -114,3 +114,48 @@
   call void @foo()
   ret void
 }
+
+declare void @foo1()
+declare void @foo2()
+declare void @foo3()
+; Fold even if the condition is frozen.
+define void @test4(i32 %v) {
+; CHECK-LABEL: @test4(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[COND1:%.*]] = icmp ne i32 [[V:%.*]], 0
+; CHECK-NEXT:    [[COND1_FR:%.*]] = freeze i1 [[COND1]]
+; CHECK-NEXT:    br i1 [[COND1_FR]], label [[BODY1:%.*]], label [[BODY2:%.*]]
+; CHECK:       body1:
+; CHECK-NEXT:    call void @foo1()
+; CHECK-NEXT:    br label [[END:%.*]]
+; CHECK:       body2:
+; CHECK-NEXT:    call void @foo2()
+; CHECK-NEXT:    br label [[END]]
+; CHECK:       end:
+; CHECK-NEXT:    ret void
+;
+entry:
+  %cond1 = icmp ne i32 %v, 0
+  %cond2 = icmp eq i32 %v, 0
+  %cond1.fr = freeze i1 %cond1
+  br i1 %cond1.fr, label %body1, label %split
+
+split:
+  %cond2.fr = freeze i1 %cond2
+  br i1 %cond2.fr, label %body2, label %body3
+
+body1:
+  call void @foo1()
+  br label %end
+
+body2:
+  call void @foo2()
+  br label %end
+
+body3:
+  call void @foo3()
+  br label %end
+
+end:
+  ret void
+}
Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -735,12 +735,18 @@
     // predecessors unless there is only one predecessor.
     if (!SI->getParent()->hasNPredecessorsOrMore(128 / SI->getNumSuccessors()))
       CV = SI->getCondition();
-  } else if (BranchInst *BI = dyn_cast<BranchInst>(TI))
-    if (BI->isConditional() && BI->getCondition()->hasOneUse())
-      if (ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
-        if (ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL))
-          CV = ICI->getOperand(0);
-      }
+  } else if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
+    if (BI->isConditional() && BI->getCondition()->hasOneUse()) {
+      ICmpInst *ICI = nullptr;
+      if (FreezeInst *FR = dyn_cast<FreezeInst>(BI->getCondition()))
+        ICI = dyn_cast<ICmpInst>(FR->getOperand(0));
+      else
+        ICI = dyn_cast<ICmpInst>(BI->getCondition());
+
+      if (ICI && ICI->isEquality() && GetConstantInt(ICI->getOperand(1), DL))
+        CV = ICI->getOperand(0);
+    }
+  }
 
   // Unwrap any lossless ptrtoint cast.
   if (CV) {
@@ -766,7 +772,11 @@
   }
 
   BranchInst *BI = cast<BranchInst>(TI);
-  ICmpInst *ICI = cast<ICmpInst>(BI->getCondition());
+  ICmpInst *ICI;
+  if (FreezeInst *FR = dyn_cast<FreezeInst>(BI->getCondition()))
+    ICI = dyn_cast<ICmpInst>(FR->getOperand(0));
+  else
+    ICI = dyn_cast<ICmpInst>(BI->getCondition());
   BasicBlock *Succ = BI->getSuccessor(ICI->getPredicate() == ICmpInst::ICMP_NE);
   Cases.push_back(ValueEqualityComparisonCase(
       GetConstantInt(ICI->getOperand(1), DL), Succ));


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106779.361574.patch
Type: text/x-patch
Size: 3058 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210726/e63b9390/attachment.bin>


More information about the llvm-commits mailing list