3 ms·
Could you give an example of an alloca mem2reg would eliminate?
by slavik81 5y ago
Could you give an example of an alloca mem2reg would eliminate?
- dragontamer 5y agoI don't think that would be helpful in learning what's going on. Instead, I'll point you out to the keyword "Dominator Frontier", and "SSA Construction Algorithm". https://en.wikipedia.org/wiki/Static_single_assignment_form#Computing_minimal_SSA_using_dominance_frontiers https://en.wikipedia.org/wiki/Static_single_assignment_form#... You should read that only after you understand SSA and Phi functions in particular. Once you know how Phi works, the next question is the algorithm for how they're constructed. https://en.wikipedia.org/wiki/Dominator_(graph_theory) https://en.wikipedia.org/wiki/Dominator_(graph_theory) This page may also help.
- NobodyNada 5y agoThe article has an example: in the unoptimized IR generated by the frontend, all the variables are stored in memory on the stack and accessed through pointers stored in registers, whereas after the mem2reg pass variables are stored directly in registers. Memory is mutable whereas SSA registers are immutable, so the compiler frontend just put all the C variables in memory to express the semantics of the C program in the simplest way possible. The mem2reg pass then moved all the variables into SSA registers, by simply creating a new register for each assignment and adding phi nodes to track the flow of variables between basic blocks (like the individual iterations of the loop body).
- slavik81 5y agoYou've very concisely explained the bits that were unclear to me. Thank you.