From 03ea86c8555a143b94e1cfd178fc49891dee14d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 13 May 2015 13:10:22 +0200 Subject: [PATCH] Eliminate dead instructions replaced in AP arithmetic lowering. --- evmjit/libevmjit/Optimizer.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/evmjit/libevmjit/Optimizer.cpp b/evmjit/libevmjit/Optimizer.cpp index 7bd7f252c..982f1cefe 100644 --- a/evmjit/libevmjit/Optimizer.cpp +++ b/evmjit/libevmjit/Optimizer.cpp @@ -54,25 +54,30 @@ bool LowerEVMPass::runOnBasicBlock(llvm::BasicBlock& _bb) { auto modified = false; auto module = _bb.getParent()->getParent(); - for (auto&& inst : _bb) + for (auto it = _bb.begin(); it != _bb.end(); ) { - if (inst.getOpcode() == llvm::Instruction::Mul) + auto& inst = *it++; + llvm::Function* func = nullptr; + if (inst.getType() == Type::Word) { - if (inst.getType() == Type::Word) + switch (inst.getOpcode()) { - auto call = llvm::CallInst::Create(Arith256::getMulFunc(*module), {inst.getOperand(0), inst.getOperand(1)}, "", &inst); - inst.replaceAllUsesWith(call); - modified = true; + case llvm::Instruction::Mul: + func = Arith256::getMulFunc(*module); + break; + + case llvm::Instruction::UDiv: + func = Arith256::getUDiv256Func(*module); + break; } } - else if (inst.getOpcode() == llvm::Instruction::UDiv) + + if (func) { - if (inst.getType() == Type::Word) - { - auto call = llvm::CallInst::Create(Arith256::getUDiv256Func(*module), {inst.getOperand(0), inst.getOperand(1)}, "", &inst); - inst.replaceAllUsesWith(call); - modified = true; - } + auto call = llvm::CallInst::Create(func, {inst.getOperand(0), inst.getOperand(1)}, "", &inst); + inst.replaceAllUsesWith(call); + inst.eraseFromParent(); + modified = true; } } return modified; @@ -90,7 +95,6 @@ bool prepare(llvm::Module& _module) auto pm = llvm::legacy::PassManager{}; pm.add(llvm::createDeadCodeEliminationPass()); pm.add(new LowerEVMPass{}); - pm.add(llvm::createDeadInstEliminationPass()); // Remove leftovers return pm.run(_module); }