From a6d4b4138ffcffabfacef12d1d903a44882ec933 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 30 Jun 2022 11:02:36 +0200 Subject: [PATCH] [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. --- llvm/lib/Analysis/ConstantFolding.cpp | 30 ++++++++++++------------------ llvm/lib/Analysis/InstructionSimplify.cpp | 4 ---- llvm/lib/Analysis/ScalarEvolution.cpp | 8 +------- llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 5 ----- 4 files changed, 13 insertions(+), 34 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 7a972c9..f5da963 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(InstOrCE)) + if (auto *CE = dyn_cast(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(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(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(InstOrCE)->getIndices()); case Instruction::ShuffleVector: return ConstantExpr::getShuffleVector( Ops[0], Ops[1], cast(InstOrCE)->getShuffleMask()); @@ -1125,13 +1134,8 @@ ConstantFoldConstantImpl(const Constant *C, const DataLayout &DL, Ops.push_back(NewC); } - if (auto *CE = dyn_cast(C)) { - if (CE->isCompare()) - return ConstantFoldCompareInstOperands(CE->getPredicate(), Ops[0], Ops[1], - DL, TLI); - + if (auto *CE = dyn_cast(C)) return ConstantFoldInstOperandsImpl(CE, CE->getOpcode(), Ops, DL, TLI); - } assert(isa(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(I)) - return ConstantFoldCompareInstOperands(CI->getPredicate(), Ops[0], Ops[1], - DL, TLI); - if (const auto *LI = dyn_cast(I)) { if (LI->isVolatile()) return nullptr; return ConstantFoldLoadFromConstPtr(Ops[0], LI->getType(), DL); } - if (auto *IVI = dyn_cast(I)) - return ConstantExpr::getInsertValue(Ops[0], Ops[1], IVI->getIndices()); - - if (auto *EVI = dyn_cast(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 f41ea9a..a0e9864 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(I))) return nullptr; - if (CmpInst *C = dyn_cast(I)) - return ConstantFoldCompareInstOperands(C->getPredicate(), ConstOps[0], - ConstOps[1], Q.DL, Q.TLI); - if (LoadInst *LI = dyn_cast(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 dc48b256..1ab67be 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(I)) - return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], - Operands[1], DL, TLI); if (LoadInst *LI = dyn_cast(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(I)) - C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0], - Operands[1], DL, &TLI); - else if (const LoadInst *Load = dyn_cast(I)) { + if (const LoadInst *Load = dyn_cast(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 399fc3b..567b866 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(I)) { - return ConstantFoldCompareInstOperands(Cmp->getPredicate(), COps[0], - COps[1], DL); - } - return ConstantFoldInstOperands(I, COps, DL); } -- 2.7.4