[PATCH] D34822: [LVI] Constant-propagate a zero extension of the switch condition value through case edges

Hiroshi Yamauchi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 29 11:12:16 PDT 2017


yamauchi created this revision.

LazyValueInfo currently computes the constant value of the switch condition through case edges, which allows the constant value to be propagated through the case edges.

But we have seen a case where a zero-extended value of the switch condition is used past case edges for which the constant propagation doesn't occur.

This patch adds a small logic to handle such a case in getEdgeValueLocal().

This is motivated by the Python 2.7 eval loop in PyEval_EvalFrameEx() where the lack of the constant propagation causes longer live ranges and more spill code than necessary.

With this patch, we see that the code size of PyEval_EvalFrameEx() decreases by ~5.4% and a performance test improves by ~4.6%.


https://reviews.llvm.org/D34822

Files:
  lib/Analysis/LazyValueInfo.cpp


Index: lib/Analysis/LazyValueInfo.cpp
===================================================================
--- lib/Analysis/LazyValueInfo.cpp
+++ lib/Analysis/LazyValueInfo.cpp
@@ -1390,15 +1390,25 @@
   // If the edge was formed by a switch on the value, then we may know exactly
   // what it is.
   if (SwitchInst *SI = dyn_cast<SwitchInst>(BBFrom->getTerminator())) {
-    if (SI->getCondition() != Val)
+    Value* Condition = SI->getCondition();
+    bool ZeroExtendedCondition = false;
+    if (isa<ZExtInst>(Val) &&
+        cast<ZExtInst>(Val)->getOperand(0) == Condition) {
+      // Val is an zero extention of the switch condition value.
+      ZeroExtendedCondition = true;
+    } else if (Condition != Val) {
       return false;
-
+    }
     bool DefaultCase = SI->getDefaultDest() == BBTo;
     unsigned BitWidth = Val->getType()->getIntegerBitWidth();
     ConstantRange EdgesVals(BitWidth, DefaultCase/*isFullSet*/);
 
     for (auto Case : SI->cases()) {
-      ConstantRange EdgeVal(Case.getCaseValue()->getValue());
+      APInt CaseValue = Case.getCaseValue()->getValue();
+      if (ZeroExtendedCondition) {
+        CaseValue = CaseValue.zext(BitWidth);
+      }
+      ConstantRange EdgeVal(CaseValue);
       if (DefaultCase) {
         // It is possible that the default destination is the destination of
         // some cases. There is no need to perform difference for those cases.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34822.104681.patch
Type: text/x-patch
Size: 1412 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170629/7f7a45a3/attachment.bin>


More information about the llvm-commits mailing list