I think it might be useful to separate what is possible versus what is
done for practicality.
I believe that there is no technical reason for mem2reg to restrict
itself to allocas within the entry block. It should be possible to
promote allocas not within the entry block into SSA registers provided
that they meet certain restrictions. Off the top of my head, those
restrictions are ensuring that the alloca is not in a loop and ensuring
that the returned pointer doesn't escape the function. I suspect that
allocas that don't dominate all of the basic blocks in the function
might require special handling.
My guess is that mem2reg limits itself to allocas within the entry block
because that is where nearly all allocas that can be converted reside,
and there's little benefit (at least for C/C++) in trying to promote
allocas that aren't in the entry block. Mem2reg was originally built so
that front-ends (namely the original llvm-gcc) wouldn't have to do SSA
construction; since llvm-gcc put its allocas in the entry block, there
probably wasn't a need to promote other allocas.
If you want to promote allocas outside the entry block, you can probably
implement an algorithm to do it. I think mem2reg doesn't do it because
it hasn't been worth the trouble.
-- John T.