[llvm] [SelectionDAG] Do not build illegal nodes with users (PR #108573)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 16 01:33:35 PDT 2024


https://github.com/ErikHogeman updated https://github.com/llvm/llvm-project/pull/108573

>From 2677cef592e8fb2000c5b2971e5f3fd5e390a6cb Mon Sep 17 00:00:00 2001
From: Erik Hogeman <erik.hogeman at arm.com>
Date: Fri, 13 Sep 2024 15:49:56 +0200
Subject: [PATCH] [SelectionDAG] Do not build illegal nodes with users

When we build a node with illegal type which has a
user, it's possible that it can end up being processed
by the DAG combiner later before it's removed, which
can trigger an assert expecting the types to be
legalized already.
---
 llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 29505f444b7650..44ec6f7cab145a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -6799,14 +6799,17 @@ SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
     // Constant fold the scalar operands.
     SDValue ScalarResult = getNode(Opcode, DL, SVT, ScalarOps, Flags);
 
-    // Legalize the (integer) scalar constant if necessary.
-    if (LegalSVT != SVT)
-      ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
-
     // Scalar folding only succeeded if the result is a constant or UNDEF.
     if (!ScalarResult.isUndef() && ScalarResult.getOpcode() != ISD::Constant &&
         ScalarResult.getOpcode() != ISD::ConstantFP)
       return SDValue();
+
+    // Legalize the (integer) scalar constant if necessary. We only do
+    // this once we know the folding succeeded, since otherwise we would
+    // get a node with illegal type which has a user.
+    if (LegalSVT != SVT)
+      ScalarResult = getNode(ExtendCode, DL, LegalSVT, ScalarResult);
+
     ScalarResults.push_back(ScalarResult);
   }
 



More information about the llvm-commits mailing list