diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp --- a/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp +++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.cpp @@ -2011,6 +2011,13 @@ MachineBasicBlock &MBB, MachineFunction &MF, const outliner::OutlinedFunction &OF) const { + // FIXME: Add an empty successor so that MCP won't delete any copy + // instructions where a register is set that is intended to be live-out, since + // MCP assumes that all the defs are live-out conservatively. + MachineBasicBlock &EmptyMBB = *MF.CreateMachineBasicBlock(); + MF.insert(MF.end(), &EmptyMBB); + MBB.addSuccessorWithoutProb(&EmptyMBB); + // Strip out any CFI instructions bool Changed = true; while (Changed) { diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp --- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp +++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp @@ -337,19 +337,16 @@ void RISCVPassConfig::addPreEmitPass() { addPass(&BranchRelaxationPassID); addPass(createRISCVMakeCompressibleOptPass()); - - // TODO: It would potentially be better to schedule copy propagation after - // expanding pseudos (in addPreEmitPass2). However, performing copy - // propagation after the machine outliner (which runs after addPreEmitPass) - // currently leads to incorrect code-gen, where copies to registers within - // outlined functions are removed erroneously. - if (TM->getOptLevel() >= CodeGenOpt::Default && EnableRISCVCopyPropagation) - addPass(createMachineCopyPropagationPass(true)); } void RISCVPassConfig::addPreEmitPass2() { addPass(createRISCVExpandPseudoPass()); + // Do the copy propagation after expanding pseudos because we may produce some + // MVs when expanding. + if (TM->getOptLevel() >= CodeGenOpt::Default && EnableRISCVCopyPropagation) + addPass(createMachineCopyPropagationPass(true)); + // Schedule the expansion of AMOs at the last possible moment, avoiding the // possibility for other passes to break the requirements for forward // progress in the LR/SC block. diff --git a/llvm/test/CodeGen/RISCV/O3-pipeline.ll b/llvm/test/CodeGen/RISCV/O3-pipeline.ll --- a/llvm/test/CodeGen/RISCV/O3-pipeline.ll +++ b/llvm/test/CodeGen/RISCV/O3-pipeline.ll @@ -166,7 +166,6 @@ ; CHECK-NEXT: Implement the 'patchable-function' attribute ; CHECK-NEXT: Branch relaxation pass ; CHECK-NEXT: RISCV Make Compressible -; CHECK-NEXT: Machine Copy Propagation Pass ; CHECK-NEXT: Contiguously Lay Out Funclets ; CHECK-NEXT: StackMap Liveness Analysis ; CHECK-NEXT: Live DEBUG_VALUE analysis @@ -177,6 +176,7 @@ ; CHECK-NEXT: Machine Optimization Remark Emitter ; CHECK-NEXT: Stack Frame Layout Analysis ; CHECK-NEXT: RISCV pseudo instruction expansion pass +; CHECK-NEXT: Machine Copy Propagation Pass ; CHECK-NEXT: RISCV atomic pseudo instruction expansion pass ; CHECK-NEXT: Lazy Machine Block Frequency Analysis ; CHECK-NEXT: Machine Optimization Remark Emitter diff --git a/llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll b/llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll --- a/llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll +++ b/llvm/test/CodeGen/RISCV/machine-outliner-and-machine-copy-propagation.ll @@ -196,3 +196,4 @@ ; RV64I-NEXT: lui a0, 524288 ; RV64I-NEXT: mv a1, s0 ; RV64I-NEXT: jr t0 +; RV64I-NEXT: # %bb.1: