[PATCH] D75385: [TargetLowering] Avoid infinite iteration on setcc fold

David Greene via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 28 14:00:28 PST 2020


greened created this revision.
greened added reviewers: spatel, RKSimon, craig.topper.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
greened added a comment.

This appeared using a quite old version of LLVM using an out-of-tree target and I'm having trouble getting a valid testcase out that runs with the latest LLVM/bugpoint.  The code in question is exacly the same in the old and current LLVM so the bug still exists currently but I haven't yet been able to generate a testcase.  Thought I would at least put this up for review.  I'm not entirely confident current master could get into this situation but it seems wise to guard against it.


When folding (X & Y) == Y to (~X & Y) == 0, we should only do the fold if the computed zero value is not Y.  Otherwise, the resulting compare will match the same pattern ad infinitum.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75385

Files:
  llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp


Index: llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
===================================================================
--- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -2968,6 +2968,12 @@
     if (YConst && YConst->isNullValue())
       return SDValue();
 
+    // Bail out if Y is already equivalent to Zero.  Otherwise we will
+    // infinitely recurse as (X' & Y) == Y (where X' is ~X) still matches the
+    // pattern (X & Y) == Y, where X = X'.
+    if (Y == Zero)
+      return SDValue();
+
     // Transform this into: ~X & Y == 0.
     SDValue NotX = DAG.getNOT(SDLoc(X), X, OpVT);
     SDValue NewAnd = DAG.getNode(ISD::AND, SDLoc(N0), OpVT, NotX, Y);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75385.247363.patch
Type: text/x-patch
Size: 728 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200228/66382820/attachment.bin>


More information about the llvm-commits mailing list