[PATCH] D124888: Summary: Add support to deduce a PHI node being a power of 2 if each incoming value is a power of 2.
William Junda Huang via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue May 3 15:45:38 PDT 2022
huangjd created this revision.
huangjd added reviewers: davidxl, Carrot, nikic, spatel, reames.
Herald added a subscriber: hiraditya.
Herald added a project: All.
huangjd requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D124888
Files:
llvm/lib/Analysis/ValueTracking.cpp
llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
Index: llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
===================================================================
--- llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
+++ llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
@@ -19,7 +19,8 @@
; CHECK-NEXT: br label [[COND_END]]
; CHECK: cond.end:
; CHECK-NEXT: [[PHI1:%.*]] = phi i64 [ 4096, [[ENTRY:%.*]] ], [ [[PHI]], [[COND_TRUE_END]] ]
-; CHECK-NEXT: [[UREM:%.*]] = urem i64 [[SIZE:%.*]], [[PHI1]]
+; CHECK-NEXT: [[TMP0:%.*]] = add nsw i64 [[PHI1]], -1
+; CHECK-NEXT: [[UREM:%.*]] = and i64 [[TMP0]], [[SIZE:%.*]]
; CHECK-NEXT: ret i64 [[UREM]]
;
entry:
@@ -56,7 +57,8 @@
; CHECK-NEXT: br label [[COND_END]]
; CHECK: cond.end:
; CHECK-NEXT: [[PHI:%.*]] = phi i64 [ [[SELECT]], [[COND_FALSE]] ], [ [[TMP1]], [[COND_TRUE]] ], [ [[PHI]], [[COND_END]] ]
-; CHECK-NEXT: [[UREM:%.*]] = urem i64 [[SIZE:%.*]], [[PHI]]
+; CHECK-NEXT: [[TMP2:%.*]] = add i64 [[PHI]], -1
+; CHECK-NEXT: [[UREM:%.*]] = and i64 [[TMP2]], [[SIZE:%.*]]
; CHECK-NEXT: [[CMP2:%.*]] = icmp ult i64 [[UREM]], 10
; CHECK-NEXT: br i1 [[CMP2]], label [[COND_END]], label [[END:%.*]]
; CHECK: end:
Index: llvm/lib/Analysis/ValueTracking.cpp
===================================================================
--- llvm/lib/Analysis/ValueTracking.cpp
+++ llvm/lib/Analysis/ValueTracking.cpp
@@ -2128,6 +2128,20 @@
}
}
+ // A PHI node is power of two if all incoming values are power of two.
+ if (const PHINode *PN = dyn_cast<PHINode>(V)) {
+ unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
+ Query RecQ = Q;
+
+ // Recursively check all incoming values
+ return llvm::all_of(PN->operands(), [&](const Use &U) {
+ if (U.get() == PN)
+ return true;
+ RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
+ return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
+ });
+ }
+
// An exact divide or right shift can only shift off zero bits, so the result
// is a power of two only if the first operand is a power of two and not
// copying a sign bit (sdiv int_min, 2).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D124888.426872.patch
Type: text/x-patch
Size: 2174 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220503/2fba7098/attachment.bin>
More information about the llvm-commits
mailing list