From 7fc0f10d118a0fac58bf588d4c4ecc4f6307bfdf Mon Sep 17 00:00:00 2001 From: Jay Foad Date: Fri, 11 Jun 2021 16:36:30 +0100 Subject: [PATCH] [IR] Simplify createReplacementInstr NFCI, although the test change shows that ConstantExpr::getAsInstruction is better than the old implementation of createReplacementInstr because it propagates things like the sdiv "exact" flag. Differential Revision: https://reviews.llvm.org/D104124 --- lib/IR/ReplaceConstant.cpp | 50 +++----------------------------------- 1 file changed, 3 insertions(+), 47 deletions(-) diff --git a/lib/IR/ReplaceConstant.cpp b/lib/IR/ReplaceConstant.cpp index 9382f820a7f..fd73a1a8e5a 100644 --- a/lib/IR/ReplaceConstant.cpp +++ b/lib/IR/ReplaceConstant.cpp @@ -20,53 +20,9 @@ namespace llvm { // Replace a constant expression by instructions with equivalent operations at // a specified location. Instruction *createReplacementInstr(ConstantExpr *CE, Instruction *Instr) { - IRBuilder Builder(Instr); - unsigned OpCode = CE->getOpcode(); - switch (OpCode) { - case Instruction::GetElementPtr: { - SmallVector CEOpVec(CE->operands()); - ArrayRef CEOps(CEOpVec); - return dyn_cast( - Builder.CreateInBoundsGEP(cast(CE)->getSourceElementType(), - CEOps[0], CEOps.slice(1))); - } - case Instruction::Add: - case Instruction::Sub: - case Instruction::Mul: - case Instruction::UDiv: - case Instruction::SDiv: - case Instruction::FDiv: - case Instruction::URem: - case Instruction::SRem: - case Instruction::FRem: - case Instruction::Shl: - case Instruction::LShr: - case Instruction::AShr: - case Instruction::And: - case Instruction::Or: - case Instruction::Xor: - return dyn_cast( - Builder.CreateBinOp((Instruction::BinaryOps)OpCode, CE->getOperand(0), - CE->getOperand(1), CE->getName())); - case Instruction::Trunc: - case Instruction::ZExt: - case Instruction::SExt: - case Instruction::FPToUI: - case Instruction::FPToSI: - case Instruction::UIToFP: - case Instruction::SIToFP: - case Instruction::FPTrunc: - case Instruction::FPExt: - case Instruction::PtrToInt: - case Instruction::IntToPtr: - case Instruction::BitCast: - case Instruction::AddrSpaceCast: - return dyn_cast( - Builder.CreateCast((Instruction::CastOps)OpCode, CE->getOperand(0), - CE->getType(), CE->getName())); - default: - llvm_unreachable("Unhandled constant expression!\n"); - } + auto *CEInstr = CE->getAsInstruction(); + CEInstr->insertBefore(Instr); + return CEInstr; } void convertConstantExprsToInstructions(Instruction *I, ConstantExpr *CE,