@@ -45,19 +45,18 @@ bool EVMPeephole::runOnMachineFunction(MachineFunction &MF) {
4545 return Changed;
4646}
4747
48- static bool isNegatadAndJumpedOn (const MachineBasicBlock &MBB,
48+ static bool isNegatedAndJumpedOn (const MachineBasicBlock &MBB,
4949 MachineBasicBlock::const_iterator I) {
5050 if (I == MBB.end () || I->getOpcode () != EVM::ISZERO_S)
5151 return false ;
5252 ++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;
53+ // When a conditional jump’s predicate is a (possibly nested) bitwise `or`,
54+ // both operands are eligible for folding. Currently we only fold the operand
55+ // computed last.
56+ // TODO: #887 Apply folding to all operands.
57+ while (I != MBB.end () && I->getOpcode () == EVM::OR_S)
58+ ++I;
59+ return I != MBB.end () && I->getOpcode () == EVM::PseudoJUMPI;
6160}
6261
6362bool EVMPeephole::optimizeConditionaJumps (MachineBasicBlock &MBB) const {
@@ -67,23 +66,23 @@ bool EVMPeephole::optimizeConditionaJumps(MachineBasicBlock &MBB) const {
6766 while (I != MBB.end ()) {
6867 // Fold ISZERO ISZERO to nothing, only if it's a predicate to JUMPI.
6968 if (I->getOpcode () == EVM::ISZERO_S &&
70- isNegatadAndJumpedOn (MBB, std::next (I))) {
69+ isNegatedAndJumpedOn (MBB, std::next (I))) {
7170 std::next (I)->eraseFromParent ();
7271 I->eraseFromParent ();
7372 return true ;
7473 }
7574
7675 // Fold EQ ISZERO to SUB, only if it's a predicate to JUMPI.
7776 if (I->getOpcode () == EVM::EQ_S &&
78- isNegatadAndJumpedOn (MBB, std::next (I))) {
77+ isNegatedAndJumpedOn (MBB, std::next (I))) {
7978 I->setDesc (TII->get (EVM::SUB_S));
8079 std::next (I)->eraseFromParent ();
8180 return true ;
8281 }
8382
8483 // Fold SUB ISZERO to EQ, only if it's a predicate to JUMPI.
8584 if (I->getOpcode () == EVM::SUB_S &&
86- isNegatadAndJumpedOn (MBB, std::next (I))) {
85+ isNegatedAndJumpedOn (MBB, std::next (I))) {
8786 I->setDesc (TII->get (EVM::EQ_S));
8887 std::next (I)->eraseFromParent ();
8988 return true ;
0 commit comments