|
| 1 | + |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// Pre-emission peephole optimizations. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#include "EVM.h" |
| 14 | +#include "MCTargetDesc/EVMMCTargetDesc.h" |
| 15 | +#include "llvm/CodeGen/MachineBasicBlock.h" |
| 16 | +#include "llvm/CodeGen/MachineFunctionPass.h" |
| 17 | +#include "llvm/CodeGen/MachineInstr.h" |
| 18 | +#include "llvm/CodeGen/MachineInstrBuilder.h" |
| 19 | +#include "llvm/CodeGen/TargetInstrInfo.h" |
| 20 | +#include "llvm/CodeGen/TargetSubtargetInfo.h" |
| 21 | + |
| 22 | +#define DEBUG_TYPE "evm-peephole" |
| 23 | +#define EVM_PEEPHOLE "EVM Peephole" |
| 24 | + |
| 25 | +using namespace llvm; |
| 26 | + |
| 27 | +namespace { |
| 28 | +/// Perform foldings on stack-form MIR before emission. |
| 29 | +class EVMPeephole final : public MachineFunctionPass { |
| 30 | +public: |
| 31 | + static char ID; |
| 32 | + EVMPeephole() : MachineFunctionPass(ID) {} |
| 33 | + |
| 34 | + StringRef getPassName() const override { return EVM_PEEPHOLE; } |
| 35 | + bool runOnMachineFunction(MachineFunction &MF) override; |
| 36 | + bool optimizeConditionaJumps(MachineBasicBlock &MBB) const; |
| 37 | +}; |
| 38 | +} // namespace |
| 39 | + |
| 40 | +bool EVMPeephole::runOnMachineFunction(MachineFunction &MF) { |
| 41 | + bool Changed = false; |
| 42 | + for (MachineBasicBlock &MBB : MF) { |
| 43 | + Changed |= optimizeConditionaJumps(MBB); |
| 44 | + } |
| 45 | + return Changed; |
| 46 | +} |
| 47 | + |
| 48 | +static bool isNegatadAndJumpedOn(const MachineBasicBlock &MBB, |
| 49 | + MachineBasicBlock::const_iterator I) { |
| 50 | + if (I == MBB.end() || I->getOpcode() != EVM::ISZERO_S) |
| 51 | + return false; |
| 52 | + ++I; |
| 53 | + if (I == MBB.end()) |
| 54 | + return false; |
| 55 | + if (I->getOpcode() == EVM::PseudoJUMPI) |
| 56 | + return true; |
| 57 | + if (I->getOpcode() != EVM::PUSH4_S) |
| 58 | + return false; |
| 59 | + ++I; |
| 60 | + return I != MBB.end() && I->getOpcode() == EVM::JUMPI; |
| 61 | +} |
| 62 | + |
| 63 | +bool EVMPeephole::optimizeConditionaJumps(MachineBasicBlock &MBB) const { |
| 64 | + MachineBasicBlock::iterator I = MBB.begin(); |
| 65 | + const TargetInstrInfo *TII = MBB.getParent()->getSubtarget().getInstrInfo(); |
| 66 | + |
| 67 | + while (I != MBB.end()) { |
| 68 | + // Fold ISZERO ISZERO to nothing, only if it's a predicate to JUMPI. |
| 69 | + if (I->getOpcode() == EVM::ISZERO_S && |
| 70 | + isNegatadAndJumpedOn(MBB, std::next(I))) { |
| 71 | + std::next(I)->eraseFromParent(); |
| 72 | + I->eraseFromParent(); |
| 73 | + return true; |
| 74 | + } |
| 75 | + |
| 76 | + // Fold EQ ISZERO to SUB, only if it's a predicate to JUMPI. |
| 77 | + if (I->getOpcode() == EVM::EQ_S && |
| 78 | + isNegatadAndJumpedOn(MBB, std::next(I))) { |
| 79 | + I->setDesc(TII->get(EVM::SUB_S)); |
| 80 | + std::next(I)->eraseFromParent(); |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + // Fold SUB ISZERO to EQ, only if it's a predicate to JUMPI. |
| 85 | + if (I->getOpcode() == EVM::SUB_S && |
| 86 | + isNegatadAndJumpedOn(MBB, std::next(I))) { |
| 87 | + I->setDesc(TII->get(EVM::EQ_S)); |
| 88 | + std::next(I)->eraseFromParent(); |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + ++I; |
| 93 | + } |
| 94 | + return false; |
| 95 | +} |
| 96 | + |
| 97 | +char EVMPeephole::ID = 0; |
| 98 | + |
| 99 | +INITIALIZE_PASS(EVMPeephole, DEBUG_TYPE, EVM_PEEPHOLE, false, false) |
| 100 | + |
| 101 | +FunctionPass *llvm::createEVMPeepholePass() { return new EVMPeephole(); } |
0 commit comments