eliminate phi nodes, reduce unnecessary loads / stores , reg2mem, mem2reg


I work on a pass that requires as input LLVM code without any phi nodes. For this, I use the reg2mem pass which produces suitable code, the pass runs correctly, but I obtain a significant performance decrease. I expect that this is because there are more reads / writes to memory after running the reg2mem pass.
How can I optimize the code, without inserting any phi nodes? Or is there a better solution to eliminate the phi nodes in the first place?

The solution that comes to my mind is to run a custom version of the mem2reg pass. That would mean, after running the reg2mem pass, to eliminate unnecessary load / store instructions, if this can be done without inserting any phi nodes.

Do you think this can be achieved and is it the right way to go?

Thank you,

Generally, no. For variables with multiple assignments, you can
either have loads and stores to allocas, or virtual registers with phi
nodes. This is a feature of LLVM IR, there is no non-SSA form using
virtual registers.

Is it possible for you to run reg2mem, your pass, and then mem2reg
again? That's the intended usage of reg2mem.