aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantFold.cpp
diff options
context:
space:
mode:
authorNikita Popov <npopov@redhat.com>2022-09-07 11:40:30 +0200
committerNikita Popov <npopov@redhat.com>2022-09-07 12:00:26 +0200
commite047a4ab55e5899e159a0cf196b42cb34f6ad3f0 (patch)
tree2c04f49d660a2806878131bd704c6274a3cd54d8 /llvm/lib/IR/ConstantFold.cpp
parentb285d708a7703d491282318dacf06679479ae8e4 (diff)
downloadllvm-e047a4ab55e5899e159a0cf196b42cb34f6ad3f0.zip
llvm-e047a4ab55e5899e159a0cf196b42cb34f6ad3f0.tar.gz
llvm-e047a4ab55e5899e159a0cf196b42cb34f6ad3f0.tar.bz2
[ConstantFold] Avoid unary ConstantExpr::get()
Call ConstantFoldUnaryInstruction() instead, to only produce a result if it folds.
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r--llvm/lib/IR/ConstantFold.cpp13
1 files 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<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);