Loading of immediates into registers


I have some constants that get loaded into a register, and then the register is copied to another register, and then used. I suspect this stems from PHI nodes lowering.I cannot avoid this right now, as the MI PHI’s will only hold registers, and not constants. Therefore this gets complicated, having to handle the introduced copies from the Eliminate PHIs pass, while the IR is not on SSA-form anymore.

Is there a simple way to handle this that I have missed?



It depends on what you mean by 'loaded'. If you have a cheap instruction that puts an immediate in a register, mark it as rematerializable and 'as cheap as a move'. Then the register allocator will replace all copies with rematerialized copies of the original instruction.

See MOV8ri and friends in X86InstrInfo.td.

If you mean a constant pool load, copies are preferred unless the value needs to be spilled. You should mark your load instructions as rematerializable as well.