1414#include " EVMMachineFunctionInfo.h"
1515#include " EVMSubtarget.h"
1616#include " MCTargetDesc/EVMMCTargetDesc.h"
17- #include " llvm/ADT/Statistic.h"
1817#include " llvm/CodeGen/MachineFunctionPass.h"
1918#include " llvm/CodeGen/MachineInstrBuilder.h"
20- #include " llvm/Support/CodeGen.h"
21- #include " llvm/Target/TargetMachine.h"
22- #include " llvm/Target/TargetOptions.h"
2319
2420using namespace llvm ;
2521
2622#define DEBUG_TYPE " evm-lower-jump-unless"
2723#define EVM_LOWER_JUMP_UNLESS_NAME " EVM Lower jump_unless"
2824
29- STATISTIC (NumPseudoJumpUnlessFolded, " Number of PseudoJUMP_UNLESS folded" );
30-
3125namespace {
3226class EVMLowerJumpUnless final : public MachineFunctionPass {
3327public:
@@ -72,51 +66,14 @@ static void lowerJumpUnless(MachineInstr &MI, const EVMInstrInfo *TII,
7266 .addReg (NewReg);
7367}
7468
75- // / Fold `<PrevMI> ; PseudoJUMP_UNLESS` into `PseudoJUMPI`.
76- // /
77- // / Supported `PrevMI` patterns and changes:
78- // / • `ISZERO_S` -> delete `ISZERO_S`
79- // / • `EQ_S` -> change to `SUB_S`
80- // / • `SUB_S` -> change to `EQ_S`
81- // /
82- // / Returns `true` if any fold was performed.
83- static bool tryFoldJumpUnless (MachineInstr &MI, const EVMInstrInfo *TII) {
84- auto I = MachineBasicBlock::iterator (&MI);
85- auto *PrevMI = I == MI.getParent ()->begin () ? nullptr : &*std::prev (I);
86- bool CanFold = PrevMI && (PrevMI->getOpcode () == EVM::ISZERO_S ||
87- PrevMI->getOpcode () == EVM::EQ_S ||
88- PrevMI->getOpcode () == EVM::SUB_S);
89-
90- if (!CanFold)
91- return false ;
92-
93- ++NumPseudoJumpUnlessFolded;
94-
95- if (PrevMI->getOpcode () == EVM::ISZERO_S)
96- PrevMI->eraseFromParent ();
97- else if (PrevMI->getOpcode () == EVM::EQ_S)
98- PrevMI->setDesc (TII->get (EVM::SUB_S));
99- else if (PrevMI->getOpcode () == EVM::SUB_S)
100- PrevMI->setDesc (TII->get (EVM::EQ_S));
101- return true ;
102- }
103-
104- // / Lower a `PseudoJUMP_UNLESS` to condition-setting + `PseudoJUMPI`.
105- // /
106- // / If `FoldJumps` is enabled and the local pattern allows it, an
107- // / optimisation in `tryFoldJumpUnless` removes the explicit `ISZERO_S`.
108- // / Otherwise the pseudo-op expands to:
109- // / ISZERO_S
110- // / PseudoJUMPI
69+ // Lower pseudo jump_unless into iszero and jumpi instructions. This pseudo
70+ // instruction can only be present in stackified functions.
11171static void lowerPseudoJumpUnless (MachineInstr &MI, const EVMInstrInfo *TII,
112- const bool IsStackified,
113- const bool FoldJumps) {
72+ const bool IsStackified) {
11473 assert (IsStackified && " Found pseudo jump_unless in non-stackified function" );
11574 assert (MI.getNumExplicitOperands () == 1 &&
11675 " Unexpected number of operands in pseudo jump_unless" );
117-
118- if (!FoldJumps || !tryFoldJumpUnless (MI, TII))
119- BuildMI (*MI.getParent (), MI, MI.getDebugLoc (), TII->get (EVM::ISZERO_S));
76+ BuildMI (*MI.getParent (), MI, MI.getDebugLoc (), TII->get (EVM::ISZERO_S));
12077 BuildMI (*MI.getParent (), MI, MI.getDebugLoc (), TII->get (EVM::PseudoJUMPI))
12178 .add (MI.getOperand (0 ));
12279}
@@ -127,32 +84,24 @@ bool EVMLowerJumpUnless::runOnMachineFunction(MachineFunction &MF) {
12784 << " ********** Function: " << MF.getName () << ' \n ' ;
12885 });
12986
130- CodeGenOptLevel OptLevel = MF.getTarget ().getOptLevel ();
13187 MachineRegisterInfo &MRI = MF.getRegInfo ();
13288 const auto *TII = MF.getSubtarget <EVMSubtarget>().getInstrInfo ();
13389 const bool IsStackified =
13490 MF.getInfo <EVMMachineFunctionInfo>()->getIsStackified ();
13591
13692 bool Changed = false ;
13793 for (MachineBasicBlock &MBB : MF) {
138- auto TermIt = MBB.getFirstInstrTerminator ();
139- if (TermIt == MBB.end ())
140- continue ;
141-
142- switch (TermIt->getOpcode ()) {
143- case EVM::PseudoJUMP_UNLESS:
144- lowerPseudoJumpUnless (*TermIt, TII, IsStackified,
145- OptLevel != CodeGenOptLevel::None);
146- break ;
147- case EVM::JUMP_UNLESS:
148- lowerJumpUnless (*TermIt, TII, IsStackified, MRI);
149- break ;
150- default :
151- continue ;
94+ for (auto &MI : make_early_inc_range (MBB)) {
95+ if (MI.getOpcode () == EVM::PseudoJUMP_UNLESS)
96+ lowerPseudoJumpUnless (MI, TII, IsStackified);
97+ else if (MI.getOpcode () == EVM::JUMP_UNLESS)
98+ lowerJumpUnless (MI, TII, IsStackified, MRI);
99+ else
100+ continue ;
101+
102+ MI.eraseFromParent ();
103+ Changed = true ;
152104 }
153-
154- TermIt->eraseFromParent ();
155- Changed = true ;
156105 }
157106 return Changed;
158107}
0 commit comments