[PATCH] D75401: [InstSimplify] Remove freeze if its operand is used as a branch condition
Juneyoung Lee via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 28 22:52:38 PST 2020
aqjune created this revision.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
aqjune added reviewers: xbolva00, spatel, lebedev.ri, reames.
fr = freeze(c)
br i1 c, BB1, BB2:
BB1:
use1(fr)
BB2:
use2(fr)
In BB1 and BB2, fr can be replaced with c because if c was undef or poison the branch would have triggered UB.
Checked with Alive2
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D75401
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/freeze.ll
Index: llvm/test/Transforms/InstSimplify/freeze.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/freeze.ll
+++ llvm/test/Transforms/InstSimplify/freeze.ll
@@ -18,3 +18,19 @@
%x = freeze i32 10
ret i32 %x
}
+
+define i1 @brcond(i1 %c) {
+; CHECK-LABEL: @brcond(
+; CHECK-NEXT: br i1 [[C:%.*]], label [[A:%.*]], label [[B:%.*]]
+; CHECK: A:
+; CHECK-NEXT: ret i1 [[C]]
+; CHECK: B:
+; CHECK-NEXT: ret i1 [[C]]
+;
+ %f = freeze i1 %c
+ br i1 %c, label %A, label %B
+A:
+ ret i1 %f
+B:
+ ret i1 %f
+}
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5361,16 +5361,29 @@
}
/// Given operands for a Freeze, see if we can fold the result.
-static Value *SimplifyFreezeInst(Value *Op0) {
+static Value *SimplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
// Use a utility function defined in ValueTracking.
if (llvm::isGuaranteedNotToBeUndefOrPoison(Op0))
return Op0;
+
+ if (Q.CxtI) {
+ // Given freeze(Op0), if Op0 is ever used as a branch condition, Op0 cannot
+ // be undef or poison.
+ // v = freeze(cond)
+ // br cond, BB1, BB2
+ // BB1:
+ // use(v) -> use(cond)
+ auto BI = dyn_cast<BranchInst>(Q.CxtI->getParent()->getTerminator());
+ if (BI && BI->isConditional() && BI->getCondition() == Op0)
+ return Op0;
+ }
+
// We have room for improvement.
return nullptr;
}
Value *llvm::SimplifyFreezeInst(Value *Op0, const SimplifyQuery &Q) {
- return ::SimplifyFreezeInst(Op0);
+ return ::SimplifyFreezeInst(Op0, Q);
}
/// See if we can compute a simplified version of this instruction.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75401.247421.patch
Type: text/x-patch
Size: 1810 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200229/01e675f8/attachment.bin>
More information about the llvm-commits
mailing list