Index: llvm/lib/CodeGen/MachineSSAUpdater.cpp =================================================================== --- llvm/lib/CodeGen/MachineSSAUpdater.cpp +++ llvm/lib/CodeGen/MachineSSAUpdater.cpp @@ -152,6 +152,14 @@ if (!HasValueForBlock(BB)) return GetValueAtEndOfBlockInternal(BB, ExistingValueOnly); + // Ok, we have already got a value for this block. If it is out of our block + // or it is a phi - we can re-use it as it will be defined in the middle block + // as well. + Register defR = getAvailableVals(AV).lookup(BB); + if (auto I = MRI->getVRegDef(defR)) + if (I->isPHI() || I->getParent() != BB) + return defR; + // If there are no predecessors, just return undef. if (BB->pred_empty()) { // If we cannot insert new instructions, just return $noreg. Index: llvm/lib/Transforms/Utils/SSAUpdater.cpp =================================================================== --- llvm/lib/Transforms/Utils/SSAUpdater.cpp +++ llvm/lib/Transforms/Utils/SSAUpdater.cpp @@ -100,6 +100,14 @@ if (!HasValueForBlock(BB)) return GetValueAtEndOfBlock(BB); + // Ok, we have already got a value for this block. If it is out of our block + // or it is a phi - we can re-use it as it will be defined in the middle block + // as well. + Value *defV = FindValueForBlock(BB); + if (auto I = dyn_cast(defV)) + if (isa(I) || I->getParent() != BB) + return defV; + // Otherwise, we have the hard case. Get the live-in values for each // predecessor. SmallVector, 8> PredValues; Index: llvm/test/CodeGen/AArch64/tail-dup-redundant-phi.mir =================================================================== --- llvm/test/CodeGen/AArch64/tail-dup-redundant-phi.mir +++ llvm/test/CodeGen/AArch64/tail-dup-redundant-phi.mir @@ -343,7 +343,7 @@ ; CHECK-NEXT: ADJCALLSTACKDOWN 0, 0, implicit-def dead $sp, implicit $sp ; CHECK-NEXT: [[MOVi32imm5:%[0-9]+]]:gpr32 = MOVi32imm 10 ; CHECK-NEXT: $w0 = COPY [[MOVi32imm5]] - ; CHECK-NEXT: [[STATEPOINT2:%[0-9]+]]:gpr64all, [[STATEPOINT3:%[0-9]+]]:gpr64all = STATEPOINT 2882400000, 0, 1, @wombat, $w0, 2, 0, 2, 0, 2, 3, [[PHI3]], [[PHI2]], [[PHI]], 2, 2, [[PHI1]](tied-def 0), [[PHI]](tied-def 1), 2, 0, 2, 2, 0, 0, 1, 1, csr_aarch64_aapcs, implicit-def $sp, implicit-def dead early-clobber $lr + ; CHECK-NEXT: [[STATEPOINT2:%[0-9]+]]:gpr64all, [[STATEPOINT3:%[0-9]+]]:gpr64all = STATEPOINT 2882400000, 0, 1, @wombat, $w0, 2, 0, 2, 0, 2, 3, [[PHI3]], [[PHI2]], [[PHI2]], 2, 2, [[PHI3]](tied-def 0), [[PHI2]](tied-def 1), 2, 0, 2, 2, 0, 0, 1, 1, csr_aarch64_aapcs, implicit-def $sp, implicit-def dead early-clobber $lr ; CHECK-NEXT: ADJCALLSTACKUP 0, 0, implicit-def dead $sp, implicit $sp ; CHECK-NEXT: {{ $}} ; CHECK-NEXT: bb.18.bb51: