[llvm] [AMDGPU] Fold fneg into select source modifiers (PR #201426)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 9 02:55:27 PDT 2026


================
@@ -5249,12 +5255,20 @@ static unsigned inverseMinMax(unsigned Opc) {
 
 /// \return true if it's profitable to try to push an fneg into its source
 /// instruction.
+///
+/// This answers two related queries:
+///  - fneg(op(x)): \p N is the fneg, \p N0 is the source op. "Should the fneg
+///    be folded into op?"
+///  - op(fneg(x)): \p N is the op, \p N0 is a null SDValue(). Used to decide
+///    the inverse fold op(fneg) -> fneg(op): because that rewrite RAUWs all
+///    uses of op onto the new fneg, the op becomes single-use, so a null \p N0
+///    takes the single-use path. Pull the fneg out iff this returns false.
 bool AMDGPUTargetLowering::shouldFoldFNegIntoSrc(SDNode *N, SDValue N0) {
   // If the input has multiple uses and we can either fold the negate down, or
   // the other uses cannot, give up. This both prevents unprofitable
   // transformations and infinite loops: we won't repeatedly try to fold around
   // a negate that has no 'good' form.
-  if (N0.hasOneUse()) {
+  if (!N0 || N0.hasOneUse()) {
----------------
arsenm wrote:

I'd rather not weaken the API to allow null 

https://github.com/llvm/llvm-project/pull/201426


More information about the llvm-commits mailing list