From e047a4ab55e5899e159a0cf196b42cb34f6ad3f0 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 7 Sep 2022 11:40:30 +0200 Subject: [PATCH] [ConstantFold] Avoid unary ConstantExpr::get() Call ConstantFoldUnaryInstruction() instead, to only produce a result if it folds. --- llvm/lib/IR/ConstantFold.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index f0c41b3..8e7ecfc 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 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); -- 2.7.4