From 41f0b6a78143776d673565cfa830849e3b468b8e Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Thu, 30 Jun 2022 10:44:08 +0200 Subject: [PATCH] [Evaluator] Use ConstantFoldInstOperands() For instructions that don't need any special handling, use ConstantFoldInstOperands(), rather than re-implementing individual cases. This is probably not NFC because it can handle cases the previous code missed (e.g. vector operations). --- llvm/lib/Transforms/Utils/Evaluator.cpp | 61 ++++--------------------- 1 file changed, 10 insertions(+), 51 deletions(-) diff --git a/llvm/lib/Transforms/Utils/Evaluator.cpp b/llvm/lib/Transforms/Utils/Evaluator.cpp index 0f08360968b2..aecf746c56a5 100644 --- a/llvm/lib/Transforms/Utils/Evaluator.cpp +++ b/llvm/lib/Transforms/Utils/Evaluator.cpp @@ -336,52 +336,6 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB, auto Res = MutatedMemory.try_emplace(GV, GV->getInitializer()); if (!Res.first->second.write(Val, Offset, DL)) return false; - } else if (BinaryOperator *BO = dyn_cast(CurInst)) { - InstResult = ConstantExpr::get(BO->getOpcode(), - getVal(BO->getOperand(0)), - getVal(BO->getOperand(1))); - LLVM_DEBUG(dbgs() << "Found a BinaryOperator! Simplifying: " - << *InstResult << "\n"); - } else if (CmpInst *CI = dyn_cast(CurInst)) { - InstResult = ConstantExpr::getCompare(CI->getPredicate(), - getVal(CI->getOperand(0)), - getVal(CI->getOperand(1))); - LLVM_DEBUG(dbgs() << "Found a CmpInst! Simplifying: " << *InstResult - << "\n"); - } else if (CastInst *CI = dyn_cast(CurInst)) { - InstResult = ConstantExpr::getCast(CI->getOpcode(), - getVal(CI->getOperand(0)), - CI->getType()); - LLVM_DEBUG(dbgs() << "Found a Cast! Simplifying: " << *InstResult - << "\n"); - } else if (SelectInst *SI = dyn_cast(CurInst)) { - InstResult = ConstantExpr::getSelect(getVal(SI->getOperand(0)), - getVal(SI->getOperand(1)), - getVal(SI->getOperand(2))); - LLVM_DEBUG(dbgs() << "Found a Select! Simplifying: " << *InstResult - << "\n"); - } else if (auto *EVI = dyn_cast(CurInst)) { - InstResult = ConstantFoldExtractValueInstruction( - getVal(EVI->getAggregateOperand()), EVI->getIndices()); - if (!InstResult) - return false; - LLVM_DEBUG(dbgs() << "Found an ExtractValueInst! Simplifying: " - << *InstResult << "\n"); - } else if (auto *IVI = dyn_cast(CurInst)) { - InstResult = ConstantExpr::getInsertValue( - getVal(IVI->getAggregateOperand()), - getVal(IVI->getInsertedValueOperand()), IVI->getIndices()); - LLVM_DEBUG(dbgs() << "Found an InsertValueInst! Simplifying: " - << *InstResult << "\n"); - } else if (GetElementPtrInst *GEP = dyn_cast(CurInst)) { - Constant *P = getVal(GEP->getOperand(0)); - SmallVector GEPOps; - for (Use &Op : llvm::drop_begin(GEP->operands())) - GEPOps.push_back(getVal(Op)); - InstResult = - ConstantExpr::getGetElementPtr(GEP->getSourceElementType(), P, GEPOps, - cast(GEP)->isInBounds()); - LLVM_DEBUG(dbgs() << "Found a GEP! Simplifying: " << *InstResult << "\n"); } else if (LoadInst *LI = dyn_cast(CurInst)) { if (!LI->isSimple()) { LLVM_DEBUG( @@ -628,11 +582,16 @@ bool Evaluator::EvaluateBlock(BasicBlock::iterator CurInst, BasicBlock *&NextBB, LLVM_DEBUG(dbgs() << "Successfully evaluated block.\n"); return true; } else { - // Did not know how to evaluate this! - LLVM_DEBUG( - dbgs() << "Failed to evaluate block due to unhandled instruction." - "\n"); - return false; + SmallVector Ops; + for (Value *Op : CurInst->operands()) + Ops.push_back(getVal(Op)); + InstResult = ConstantFoldInstOperands(&*CurInst, Ops, DL, TLI); + if (!InstResult) { + dbgs() << "Cannot fold instruction: " << *CurInst << "\n"; + return false; + } + dbgs() << "Folded instruction " << *CurInst << " to " << *InstResult + << "\n"; } if (!CurInst->use_empty()) {