On PPC, global variable access is done through the table of contents (TOC) which is always in register X2. The ABI reserves this register in any functions that have calls or access global variables. Furthermore, a call through a function pointer involves saving, changing and restoring the register around the call.
Currently MachineLICM is not able to hoist instructions used to get the TOC-based addresses out of a loop if there is a virtual function call within that loop. This is because X2 is (visibly) saved/restored by the caller around the call. MachineLICM sees the use of the X2 register but since it is NOT a constant physical register in this machine function, it deemed it unsafe and does not hoist it out of the loop. Of course, overriding isConstantPhysicalRegister() for X2 is not valid since there are visible defs of the register.
What is needed is a way to communicate to MachineLICM that even though the register isn't constant in the function, it is preserved between uses - i.e. ensure that any clobber of X2 will be followed by a restore before the next use. This patch adds such a query to the MachineRegisterInfo/TargetRegisterInfo.
SPEC perf run showed negligible differences in speedups.