diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/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);