Index: lib/CodeGen/MachineSink.cpp =================================================================== --- lib/CodeGen/MachineSink.cpp +++ lib/CodeGen/MachineSink.cpp @@ -984,12 +984,12 @@ static MachineBasicBlock * getSingleLiveInSuccBB(MachineBasicBlock &CurBB, - ArrayRef SinkableBBs, unsigned Reg, - const TargetRegisterInfo *TRI) { + const SmallPtrSetImpl &SinkableBBs, + unsigned Reg, const TargetRegisterInfo *TRI) { // Try to find a single sinkable successor in which Reg is live-in. MachineBasicBlock *BB = nullptr; for (auto *SI : SinkableBBs) { - if (SI->isLiveIn(Reg)) { + if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) { // If BB is set here, Reg is live-in to at least two sinkable successors, // so quit. if (BB) @@ -1003,17 +1003,17 @@ // Check if any register aliased with Reg is live-in in other successors. for (auto *SI : CurBB.successors()) { - if (SI == BB) - continue; - if (aliasWithRegsInLiveIn(*SI, Reg, TRI)) + if (!SinkableBBs.count(SI) && aliasWithRegsInLiveIn(*SI, Reg, TRI)) return nullptr; } return BB; } -static MachineBasicBlock *getSingleLiveInSuccBB( - MachineBasicBlock &CurBB, ArrayRef SinkableBBs, - ArrayRef DefedRegsInCopy, const TargetRegisterInfo *TRI) { +static MachineBasicBlock * +getSingleLiveInSuccBB(MachineBasicBlock &CurBB, + const SmallPtrSetImpl &SinkableBBs, + ArrayRef DefedRegsInCopy, + const TargetRegisterInfo *TRI) { MachineBasicBlock *SingleBB = nullptr; for (auto DefReg : DefedRegsInCopy) { MachineBasicBlock *BB = @@ -1096,13 +1096,13 @@ MachineFunction &MF, const TargetRegisterInfo *TRI, const TargetInstrInfo *TII) { - SmallVector SinkableBBs; + SmallPtrSet SinkableBBs; // FIXME: For now, we sink only to a successor which has a single predecessor // so that we can directly sink COPY instructions to the successor without // adding any new block or branch instruction. for (MachineBasicBlock *SI : CurBB.successors()) if (!SI->livein_empty() && SI->pred_size() == 1) - SinkableBBs.push_back(SI); + SinkableBBs.insert(SI); if (SinkableBBs.empty()) return false; Index: test/CodeGen/AArch64/post-ra-machine-sink.mir =================================================================== --- test/CodeGen/AArch64/post-ra-machine-sink.mir +++ test/CodeGen/AArch64/post-ra-machine-sink.mir @@ -203,8 +203,43 @@ liveins: $w0, $w19 $w0 = ADDWrr $w0, $w19 RET $x0 +... + --- +# Sink w19 to %bb.3 through %bb.2. +# CHECK-LABEL: name: sinkcopy8 +# CHECK-LABEL: bb.0: +# CHECK-NOT: renamable $w19 = COPY $w0, implicit-def $x19 +# CHECK-LABEL: bb.2: +# CHECK: $w1 = ADDWrr $w1, $w0, implicit $x0 +# CHECK-LABEL: bb.3: +# CHECK: liveins: $x1, $w0 +# CHECK: renamable $w19 = COPY killed $w0, implicit-def $x19 +name: sinkcopy8 +tracksRegLiveness: true +body: | + bb.0: + liveins: $w0, $x1 + $w1 = SUBSWri $w1, 1, 0, implicit-def $nzcv + renamable $w19 = COPY $w0, implicit-def $x19 + Bcc 11, %bb.2, implicit $nzcv + bb.1: + liveins: $x0 + $w19 = COPY $wzr + RET $x0 + + bb.2: + liveins: $w0, $x1, $x19 + $w1 = ADDWrr $w1, $w0, implicit killed $x0 + + bb.3: + liveins: $x1, $x19 + $x0 = ADDXrr $x1, $x19 + RET $x0 +... + +--- # Don't sink w19 as w0 is defined in bb.0. # CHECK-LABEL: name: donotsinkcopy1 # CHECK-LABEL: bb.0: