[llvm] e047a4a - [ConstantFold] Avoid unary ConstantExpr::get()

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 7 03:00:37 PDT 2022


Author: Nikita Popov
Date: 2022-09-07T12:00:26+02:00
New Revision: e047a4ab55e5899e159a0cf196b42cb34f6ad3f0

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

LOG: [ConstantFold] Avoid unary ConstantExpr::get()

Call ConstantFoldUnaryInstruction() instead, to only produce a
result if it folds.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index f0c41b3232525..8e7ecfc0e563d 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -848,18 +848,19 @@ Constant *llvm::ConstantFoldUnaryInstruction(unsigned Opcode, Constant *C) {
 
     Type *Ty = IntegerType::get(VTy->getContext(), 32);
     // Fast path for splatted constants.
-    if (Constant *Splat = C->getSplatValue()) {
-      Constant *Elt = ConstantExpr::get(Opcode, Splat);
-      return ConstantVector::getSplat(VTy->getElementCount(), Elt);
-    }
+    if (Constant *Splat = C->getSplatValue())
+      if (Constant *Elt = ConstantFoldUnaryInstruction(Opcode, Splat))
+        return ConstantVector::getSplat(VTy->getElementCount(), Elt);
 
     // Fold each element and create a vector constant from those constants.
     SmallVector<Constant *, 16> Result;
     for (unsigned i = 0, e = VTy->getNumElements(); i != e; ++i) {
       Constant *ExtractIdx = ConstantInt::get(Ty, i);
       Constant *Elt = ConstantExpr::getExtractElement(C, ExtractIdx);
-
-      Result.push_back(ConstantExpr::get(Opcode, Elt));
+      Constant *Res = ConstantFoldUnaryInstruction(Opcode, Elt);
+      if (!Res)
+        return nullptr;
+      Result.push_back(Res);
     }
 
     return ConstantVector::get(Result);


        


More information about the llvm-commits mailing list