[llvm] f42d926 - [Reassociate] Avoid ConstantExpr::getFNeg() (NFCI)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 7 01:48:19 PDT 2022


Author: Nikita Popov
Date: 2022-09-07T10:48:08+02:00
New Revision: f42d92611da60f3613af3ad4e84670380cb7d67d

URL: https://github.com/llvm/llvm-project/commit/f42d92611da60f3613af3ad4e84670380cb7d67d
DIFF: https://github.com/llvm/llvm-project/commit/f42d92611da60f3613af3ad4e84670380cb7d67d.diff

LOG: [Reassociate] Avoid ConstantExpr::getFNeg() (NFCI)

Use ConstantFoldUnaryOpOperand() instead. Also make the code below
robust against non-instruction users, just in case it doesn't fold.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/Reassociate.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index 3bd1234bb9083..1b11ec67f2320 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -833,9 +833,14 @@ void ReassociatePass::RewriteExprTree(BinaryOperator *I,
 /// additional opportunities have been exposed.
 static Value *NegateValue(Value *V, Instruction *BI,
                           ReassociatePass::OrderedSet &ToRedo) {
-  if (auto *C = dyn_cast<Constant>(V))
-    return C->getType()->isFPOrFPVectorTy() ? ConstantExpr::getFNeg(C) :
-                                              ConstantExpr::getNeg(C);
+  if (auto *C = dyn_cast<Constant>(V)) {
+    const DataLayout &DL = BI->getModule()->getDataLayout();
+    Constant *Res = C->getType()->isFPOrFPVectorTy()
+                        ? ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)
+                        : ConstantExpr::getNeg(C);
+    if (Res)
+      return Res;
+  }
 
   // We are trying to expose opportunity for reassociation.  One of the things
   // that we want to do to achieve this is to push a negation as deep into an
@@ -880,10 +885,11 @@ static Value *NegateValue(Value *V, Instruction *BI,
     // this use.  We do this by moving it to the entry block (if it is a
     // non-instruction value) or right after the definition.  These negates will
     // be zapped by reassociate later, so we don't need much finesse here.
-    Instruction *TheNeg = cast<Instruction>(U);
+    Instruction *TheNeg = dyn_cast<Instruction>(U);
 
     // Verify that the negate is in this function, V might be a constant expr.
-    if (TheNeg->getParent()->getParent() != BI->getParent()->getParent())
+    if (!TheNeg ||
+        TheNeg->getParent()->getParent() != BI->getParent()->getParent())
       continue;
 
     Instruction *InsertPt;


        


More information about the llvm-commits mailing list