[llvm] c9e7049 - [JumpThreading] Look through freeze in getPredicateAt() fold

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed May 18 03:10:07 PDT 2022


Author: Nikita Popov
Date: 2022-05-18T12:09:59+02:00
New Revision: c9e7049754ac94a952de8ac6962c36f844e43b3c

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

LOG: [JumpThreading] Look through freeze in getPredicateAt() fold

This code is valid for any icmp, so we can safely look through a
freeze when trying to find one.

A caveat here is that replaceFoldableUses() may not end up replacing
any uses in this case. It might make sense to use the freeze as the
context instruction (rather than the terminator) if there is a
freeze, to ensure that it always gets folded. This would require
some changes to how replaceFoldedUses() works though, as it
currently assumes that the value is valid at the end of the block.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/JumpThreading.cpp
    llvm/test/Transforms/JumpThreading/freeze.ll
    llvm/test/Transforms/JumpThreading/select-unfold-freeze.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 3bf9c4d6438b..06dd9ff0a75a 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -489,14 +489,15 @@ bool JumpThreadingPass::runImpl(Function &F, TargetLibraryInfo *TLI_,
 // at the end of block. RAUW unconditionally replaces all uses
 // including the guards/assumes themselves and the uses before the
 // guard/assume.
-static void replaceFoldableUses(Instruction *Cond, Value *ToVal,
+static bool replaceFoldableUses(Instruction *Cond, Value *ToVal,
                                 BasicBlock *KnownAtEndOfBB) {
+  bool Changed = false;
   assert(Cond->getType() == ToVal->getType());
   // We can unconditionally replace all uses in non-local blocks (i.e. uses
   // strictly dominated by BB), since LVI information is true from the
   // terminator of BB.
   if (Cond->getParent() == KnownAtEndOfBB)
-    replaceNonLocalUsesWith(Cond, ToVal);
+    Changed |= replaceNonLocalUsesWith(Cond, ToVal);
   for (Instruction &I : reverse(*KnownAtEndOfBB)) {
     // Reached the Cond whose uses we are trying to replace, so there are no
     // more uses.
@@ -506,10 +507,13 @@ static void replaceFoldableUses(Instruction *Cond, Value *ToVal,
     // of BB, where we know Cond is ToVal.
     if (!isGuaranteedToTransferExecutionToSuccessor(&I))
       break;
-    I.replaceUsesOfWith(Cond, ToVal);
+    Changed |= I.replaceUsesOfWith(Cond, ToVal);
   }
-  if (Cond->use_empty() && !Cond->mayHaveSideEffects())
+  if (Cond->use_empty() && !Cond->mayHaveSideEffects()) {
     Cond->eraseFromParent();
+    Changed = true;
+  }
+  return Changed;
 }
 
 /// Return the cost of duplicating a piece of this block from first non-phi
@@ -1139,7 +1143,7 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
   if (auto *FI = dyn_cast<FreezeInst>(CondInst))
     CondWithoutFreeze = FI->getOperand(0);
 
-  if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) {
+  if (CmpInst *CondCmp = dyn_cast<CmpInst>(CondWithoutFreeze)) {
     // If we're branching on a conditional, LVI might be able to determine
     // it's value at the branch instruction.  We only handle comparisons
     // against a constant at this time.
@@ -1159,8 +1163,8 @@ bool JumpThreadingPass::processBlock(BasicBlock *BB) {
         auto *CI = Ret == LazyValueInfo::True ?
           ConstantInt::getTrue(CondCmp->getType()) :
           ConstantInt::getFalse(CondCmp->getType());
-        replaceFoldableUses(CondCmp, CI, BB);
-        return true;
+        if (replaceFoldableUses(CondCmp, CI, BB))
+          return true;
       }
 
       // We did not manage to simplify this branch, try to see whether

diff  --git a/llvm/test/Transforms/JumpThreading/freeze.ll b/llvm/test/Transforms/JumpThreading/freeze.ll
index 5df03c1790a2..8bbadcbe2b46 100644
--- a/llvm/test/Transforms/JumpThreading/freeze.ll
+++ b/llvm/test/Transforms/JumpThreading/freeze.ll
@@ -198,7 +198,6 @@ define i32 @freeze_known_predicate(i1 %cond) {
 ; CHECK-NEXT:    br label [[ELSE2]]
 ; CHECK:       else2:
 ; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 1, [[IF]] ], [ 2, [[ENTRY:%.*]] ]
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[PHI]], 0
 ; CHECK-NEXT:    ret i32 1
 ;
 entry:

diff  --git a/llvm/test/Transforms/JumpThreading/select-unfold-freeze.ll b/llvm/test/Transforms/JumpThreading/select-unfold-freeze.ll
index 12288fc27262..5f6f5ddcd7cf 100644
--- a/llvm/test/Transforms/JumpThreading/select-unfold-freeze.ll
+++ b/llvm/test/Transforms/JumpThreading/select-unfold-freeze.ll
@@ -171,27 +171,23 @@ define i32 @unfold5(i32 %u, i32 %v, i32 %w, i32 %x, i32 %y, i32 %z, i32 %j) noun
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[ADD3:%.*]] = add nsw i32 [[J:%.*]], 2
 ; CHECK-NEXT:    [[CMP_I:%.*]] = icmp slt i32 [[U:%.*]], [[V:%.*]]
-; CHECK-NEXT:    br i1 [[CMP_I]], label [[DOTEXIT_THREAD:%.*]], label [[COND_FALSE_I:%.*]]
+; CHECK-NEXT:    br i1 [[CMP_I]], label [[DOTEXIT:%.*]], label [[COND_FALSE_I:%.*]]
 ; CHECK:       cond.false.i:
 ; CHECK-NEXT:    [[CMP4_I:%.*]] = icmp sgt i32 [[U]], [[V]]
-; CHECK-NEXT:    br i1 [[CMP4_I]], label [[DOTEXIT_THREAD]], label [[COND_FALSE_6_I:%.*]]
+; CHECK-NEXT:    br i1 [[CMP4_I]], label [[DOTEXIT]], label [[COND_FALSE_6_I:%.*]]
 ; CHECK:       cond.false.6.i:
 ; CHECK-NEXT:    [[CMP8_I:%.*]] = icmp slt i32 [[W:%.*]], [[X:%.*]]
-; CHECK-NEXT:    br i1 [[CMP8_I]], label [[DOTEXIT_THREAD]], label [[COND_FALSE_10_I:%.*]]
+; CHECK-NEXT:    br i1 [[CMP8_I]], label [[DOTEXIT]], label [[COND_FALSE_10_I:%.*]]
 ; CHECK:       cond.false.10.i:
 ; CHECK-NEXT:    [[CMP13_I:%.*]] = icmp sgt i32 [[W]], [[X]]
-; CHECK-NEXT:    br i1 [[CMP13_I]], label [[TMP0:%.*]], label [[COND_FALSE_15_I:%.*]]
+; CHECK-NEXT:    br i1 [[CMP13_I]], label [[DOTEXIT]], label [[COND_FALSE_15_I:%.*]]
 ; CHECK:       cond.false.15.i:
 ; CHECK-NEXT:    [[CMP19_I:%.*]] = icmp sge i32 [[Y:%.*]], [[Z:%.*]]
 ; CHECK-NEXT:    [[CONV:%.*]] = zext i1 [[CMP19_I]] to i32
-; CHECK-NEXT:    br label [[DOTEXIT_THREAD]]
-; CHECK:       0:
-; CHECK-NEXT:    [[COND23_I:%.*]] = phi i32 [ 7, [[COND_FALSE_10_I]] ]
-; CHECK-NEXT:    [[LNOT_I18:%.*]] = icmp sgt i32 [[COND23_I]], 5
-; CHECK-NEXT:    br label [[DOTEXIT_THREAD]]
-; CHECK:       .exit.thread:
-; CHECK-NEXT:    [[TMP1:%.*]] = phi i32 [ [[J]], [[TMP0]] ], [ [[CONV]], [[COND_FALSE_15_I]] ], [ 1, [[COND_FALSE_6_I]] ], [ 3, [[COND_FALSE_I]] ], [ 2, [[ENTRY:%.*]] ]
-; CHECK-NEXT:    ret i32 [[TMP1]]
+; CHECK-NEXT:    br label [[DOTEXIT]]
+; CHECK:       .exit:
+; CHECK-NEXT:    [[TMP0:%.*]] = phi i32 [ [[J]], [[COND_FALSE_10_I]] ], [ [[CONV]], [[COND_FALSE_15_I]] ], [ 1, [[COND_FALSE_6_I]] ], [ 3, [[COND_FALSE_I]] ], [ 2, [[ENTRY:%.*]] ]
+; CHECK-NEXT:    ret i32 [[TMP0]]
 ;
 entry:
   %add3 = add nsw i32 %j, 2


        


More information about the llvm-commits mailing list