This patch prevents instructions using $gp from being placed between a jalr and the instruction that restores the clobbered $gp (please see the following example). <br><br>The current implementation creates load nodes that restore $gp when call instructions are lowered in MipsISelLowering::LowerCall. However, it does not add chains or glues that are needed to make sure load/store nodes that have $gp as an operand are not scheduled between JALR nodes and nodes that restore $gp after the call.<br>
<br>This patch runs a pre-regalloc pass that emits $gp restore instructions instead.<br><br>- bitcode<br>@p = external global i32<br>...<br>
tail call void (...)* @f1() nounwind<br>%tmp = load i32* @p, align 4, !tbaa !0<br><br>- currently produces following code:<br>...<br>lw  $25, %call16(f1)($gp)<br>jalr  $25                        # calls f1. $gp is clobbered.<br>

nop<br>lw  $3, 16($sp)              # load $gp from stack.  <br>lw  $2, %got(p)($gp)      # this instr uses a clobbered $gp.<br>addu  $gp, $zero, $3     # $gp is restored here.<br>...<br><br>- produces following code after patch is applied.<br>

...<br>lw  $25, %call16(f1)($gp)<br>lw  $16, %got(p)($gp)  # $gp has correct value here.<br>jalr  $25                     # calls f1. clobbers $gp.<br>nop<br>lw  $gp, 16($sp)          # $gp restored.<br>...<br><br>