[llvm] a6d4b41 - [ConstantFold] Supports compares in ConstantFoldInstOperands()
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 30 02:05:32 PDT 2022
Author: Nikita Popov
Date: 2022-06-30T11:05:24+02:00
New Revision: a6d4b4138ffcffabfacef12d1d903a44882ec933
URL: https://github.com/llvm/llvm-project/commit/a6d4b4138ffcffabfacef12d1d903a44882ec933
DIFF: https://github.com/llvm/llvm-project/commit/a6d4b4138ffcffabfacef12d1d903a44882ec933.diff
LOG: [ConstantFold] Supports compares in ConstantFoldInstOperands()
Support compares in ConstantFoldInstOperands(), instead of
forcing the use of ConstantFoldCompareInstOperands(). Also handle
insertvalue (extractvalue was already handled).
This removes a footgun, where many uses of ConstantFoldInstOperands()
need a separate check for compares beforehand. It's particularly
insidious if called on a constant expression, because it doesn't
fail in that case, but will just not do DL-dependent folding.
Added:
Modified:
llvm/lib/Analysis/ConstantFolding.cpp
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/lib/Analysis/ScalarEvolution.cpp
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 7a972c906b8c9..f5da9637efea5 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -1061,13 +1061,19 @@ Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
GEP->getInRangeIndex());
}
- if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE))
+ if (auto *CE = dyn_cast<ConstantExpr>(InstOrCE)) {
+ if (CE->isCompare())
+ return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
+ DL, TLI);
return CE->getWithOperands(Ops);
+ }
switch (Opcode) {
default: return nullptr;
case Instruction::ICmp:
- case Instruction::FCmp: llvm_unreachable("Invalid for compares");
+ case Instruction::FCmp:
+ return ConstantFoldCompareInstOperands(
+ cast<CmpInst>(InstOrCE)->getPredicate(), Ops[0], Ops[1], DL, TLI);
case Instruction::Freeze:
return isGuaranteedNotToBeUndefOrPoison(Ops[0]) ? Ops[0] : nullptr;
case Instruction::Call:
@@ -1086,6 +1092,9 @@ Constant *ConstantFoldInstOperandsImpl(const Value *InstOrCE, unsigned Opcode,
Ops[0], cast<ExtractValueInst>(InstOrCE)->getIndices());
case Instruction::InsertElement:
return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]);
+ case Instruction::InsertValue:
+ return ConstantExpr::getInsertValue(
+ Ops[0], Ops[1], cast<InsertValueInst>(InstOrCE)->getIndices());
case Instruction::ShuffleVector:
return ConstantExpr::getShuffleVector(
Ops[0], Ops[1], cast<ShuffleVectorInst>(InstOrCE)->getShuffleMask());
@@ -1125,13 +1134,8 @@ ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL,
Ops.push_back(NewC);
}
- if (auto *CE = dyn_cast<ConstantExpr>(C)) {
- if (CE->isCompare())
- return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1],
- DL, TLI);
-
+ if (auto *CE = dyn_cast<ConstantExpr>(C))
return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI);
- }
assert(isa<ConstantVector>(C));
return ConstantVector::get(Ops);
@@ -1184,22 +1188,12 @@ Constant *llvm::ConstantFoldInstruction(Instruction *I, const DataLayout &DL,
Ops.push_back(Op);
}
- if (const auto *CI = dyn_cast<CmpInst>(I))
- return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1],
- DL, TLI);
-
if (const auto *LI = dyn_cast<LoadInst>(I)) {
if (LI->isVolatile())
return nullptr;
return ConstantFoldLoadFromConstPtr(Ops[0], LI->getType(), DL);
}
- if (auto *IVI = dyn_cast<InsertValueInst>(I))
- return ConstantExpr::getInsertValue(Ops[0], Ops[1], IVI->getIndices());
-
- if (auto *EVI = dyn_cast<ExtractValueInst>(I))
- return ConstantFoldExtractValueInstruction(Ops[0], EVI->getIndices());
-
return ConstantFoldInstOperands(I, Ops, DL, TLI);
}
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index f41ea9a79c635..a0e986422d4ee 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4209,10 +4209,6 @@ static Value *simplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp,
if (!AllowRefinement && canCreatePoison(cast<Operator>(I)))
return nullptr;
- if (CmpInst *C = dyn_cast<CmpInst>(I))
- return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0],
- ConstOps[1], Q.DL, Q.TLI);
-
if (LoadInst *LI = dyn_cast<LoadInst>(I))
if (!LI->isVolatile())
return ConstantFoldLoadFromConstPtr(ConstOps[0], LI->getType(), Q.DL);
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index dc48b256cbf4c..1ab67bef4bc80 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -9237,9 +9237,6 @@ static Constant *EvaluateExpression(Value *V, const Loop *L,
Operands[i] = C;
}
- if (CmpInst *CI = dyn_cast<CmpInst>(I))
- return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
- Operands[1], DL, TLI);
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
if (!LI->isVolatile())
return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
@@ -9663,10 +9660,7 @@ const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
if (MadeImprovement) {
Constant *C = nullptr;
const DataLayout &DL = getDataLayout();
- if (const CmpInst *CI = dyn_cast<CmpInst>(I))
- C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
- Operands[1], DL, &TLI);
- else if (const LoadInst *Load = dyn_cast<LoadInst>(I)) {
+ if (const LoadInst *Load = dyn_cast<LoadInst>(I)) {
if (!Load->isVolatile())
C = ConstantFoldLoadFromConstPtr(Operands[0], Load->getType(),
DL);
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 399fc3baf7aa4..567b866f77778 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -5550,11 +5550,6 @@ ConstantFold(Instruction *I, const DataLayout &DL,
return nullptr;
}
- if (CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
- return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0],
- COps[1], DL);
- }
-
return ConstantFoldInstOperands(I, COps, DL);
}
More information about the llvm-commits
mailing list