[llvm] 6f2deab - [SelectionDAG][NFC] Add function for `peekThroughFreeze` (#195324)
via llvm-commits
llvm-commits at lists.llvm.org
Sun May 3 09:26:06 PDT 2026
Author: Henry Jiang
Date: 2026-05-03T09:26:01-07:00
New Revision: 6f2deab213fb6c72d2f41a4f846bcf57cb652313
URL: https://github.com/llvm/llvm-project/commit/6f2deab213fb6c72d2f41a4f846bcf57cb652313
DIFF: https://github.com/llvm/llvm-project/commit/6f2deab213fb6c72d2f41a4f846bcf57cb652313.diff
LOG: [SelectionDAG][NFC] Add function for `peekThroughFreeze` (#195324)
There are a few callsites in SelectionDAG and DAGCombiner where it is
necessary to look through an `ISD::FREEZE` to unblock some optimization
and folds. This patch introduces `peekThroughFreeze` and
`peekThroughOneUseFreeze` utility functions to cleanup the repeated sites.
Added:
Modified:
llvm/include/llvm/CodeGen/SelectionDAGNodes.h
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
llvm/lib/Target/X86/X86ISelLowering.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/CodeGen/SelectionDAGNodes.h b/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
index 2d5e92381ef64..7bc07f257de4d 100644
--- a/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
+++ b/llvm/include/llvm/CodeGen/SelectionDAGNodes.h
@@ -1977,6 +1977,22 @@ LLVM_ABI SDValue peekThroughInsertVectorElt(SDValue V,
/// If \p V is not a truncation, it is returned as-is.
LLVM_ABI SDValue peekThroughTruncates(SDValue V);
+/// Return the non-frozen source operand of \p V if it exists.
+/// If \p V is not a freeze, it is returned as-is.
+inline SDValue peekThroughFreeze(SDValue V) {
+ if (V.getOpcode() == ISD::FREEZE)
+ return V.getOperand(0);
+ return V;
+}
+
+/// Return the non-frozen source operand of \p V if it exists and \p V has
+/// a single use. If \p V is not a single-use freeze, it is returned as-is.
+inline SDValue peekThroughOneUseFreeze(SDValue V) {
+ if (V.getOpcode() == ISD::FREEZE && V.hasOneUse())
+ return V.getOperand(0);
+ return V;
+}
+
/// Returns true if \p V is a bitwise not operation. Assumes that an all ones
/// constant is canonicalized to be operand 1.
LLVM_ABI bool isBitwiseNot(SDValue V, bool AllowUndefs = false);
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 75539da583342..72498d3d2bfff 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -7980,7 +7980,7 @@ SDValue DAGCombiner::visitAND(SDNode *N) {
// fold (and (freeze (load x)), 255) -> (freeze (zextload x, i8))
// fold (and (freeze (extload x, i16)), 255) -> (freeze (zextload x, i8))
if (N1C && !VT.isVector()) {
- SDValue Inner = N0.getOpcode() == ISD::FREEZE ? N0.getOperand(0) : N0;
+ SDValue Inner = peekThroughFreeze(N0);
if (Inner.getOpcode() == ISD::LOAD)
if (SDValue Res = reduceLoadWidth(N))
return Res;
diff --git a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
index 83d02fa31b577..edd372a9db344 100644
--- a/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
+++ b/llvm/lib/Target/NVPTX/NVPTXISelLowering.cpp
@@ -6659,9 +6659,7 @@ static SDValue PerformSETCCCombine(SDNode *N,
static SDValue PerformEXTRACTCombine(SDNode *N,
TargetLowering::DAGCombinerInfo &DCI) {
- SDValue Vector = N->getOperand(0);
- if (Vector->getOpcode() == ISD::FREEZE)
- Vector = Vector->getOperand(0);
+ SDValue Vector = peekThroughFreeze(N->getOperand(0));
SDLoc DL(N);
EVT VectorVT = Vector.getValueType();
if (Vector->getOpcode() == ISD::LOAD && VectorVT.isSimple() &&
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
index 396d2f21ff16a..dda91ac19b44a 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
@@ -1052,9 +1052,7 @@ bool WebAssemblyTargetLowering::isIntDivCheap(EVT VT,
bool WebAssemblyTargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const {
EVT ExtT = ExtVal.getValueType();
- SDValue N0 = ExtVal->getOperand(0);
- if (N0.getOpcode() == ISD::FREEZE)
- N0 = N0.getOperand(0);
+ SDValue N0 = peekThroughFreeze(ExtVal->getOperand(0));
auto *Load = dyn_cast<LoadSDNode>(N0);
if (!Load)
return false;
diff --git a/llvm/lib/Target/X86/X86ISelLowering.cpp b/llvm/lib/Target/X86/X86ISelLowering.cpp
index 29daeb5c23cc8..5a1171b2b4ee6 100644
--- a/llvm/lib/Target/X86/X86ISelLowering.cpp
+++ b/llvm/lib/Target/X86/X86ISelLowering.cpp
@@ -9704,16 +9704,11 @@ LowerBUILD_VECTORAsVariablePermute(SDValue V, const SDLoc &DL,
const X86Subtarget &Subtarget) {
SDValue SrcVec, IndicesVec;
- auto PeekThroughFreeze = [](SDValue N) {
- if (N->getOpcode() == ISD::FREEZE && N.hasOneUse())
- return N->getOperand(0);
- return N;
- };
// Check for a match of the permute source vector and permute index elements.
// This is done by checking that the i-th build_vector operand is of the form:
// (extract_elt SrcVec, (extract_elt IndicesVec, i)).
for (unsigned Idx = 0, E = V.getNumOperands(); Idx != E; ++Idx) {
- SDValue Op = PeekThroughFreeze(V.getOperand(Idx));
+ SDValue Op = peekThroughOneUseFreeze(V.getOperand(Idx));
if (Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
return SDValue();
More information about the llvm-commits
mailing list