From 9d928c223fce575edb67273823f6d76ae05166f2 Mon Sep 17 00:00:00 2001 From: Bob Wilson Date: Thu, 7 May 2009 23:47:03 +0000 Subject: [PATCH] Fix pr4100. Do not remove no-op copies when they are dead. The register scavenger gets confused about register liveness if it doesn't see them. I'm not thrilled with this solution, but it only comes up when there are dead copies in the code, which is something that hopefully doesn't happen much. Here is what happens in pr4100: As shown in the following excerpt from the debug output of llc, the source of a move gets reloaded from the stack, inserting a new load instruction before the move. Since that source operand is a kill, the physical register is free to be reused for the destination of the move. The move ends up being a no-op, copying R3 to R3, so it is deleted. But, it leaves behind the load to reload %reg1028 into R3, and that load is not updated to show that it's destination operand (R3) is dead. The scavenger gets confused by that load because it thinks that R3 is live. Starting RegAlloc of: %reg1025 = MOVr %reg1028, 14, %reg0, %reg0 Regs have values: Reloading %reg1028 into R3 Last use of R3[%reg1028], removing it from live set Assigning R3 to %reg1025 Register R3 [%reg1025] is never used, removing it from live set Alternative solutions might be either marking the load as dead, or zapping the load along with the no-op copy. I couldn't see an easy way to do either of those, though. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@71196 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/RegAllocLocal.cpp | 6 ++++-- test/CodeGen/ARM/2009-05-07-RegAllocLocal.ll | 12 ++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 test/CodeGen/ARM/2009-05-07-RegAllocLocal.ll diff --git a/lib/CodeGen/RegAllocLocal.cpp b/lib/CodeGen/RegAllocLocal.cpp index 5ebecc3043e..e1cc20cf4fb 100644 --- a/lib/CodeGen/RegAllocLocal.cpp +++ b/lib/CodeGen/RegAllocLocal.cpp @@ -981,10 +981,12 @@ void RALocal::AllocateBasicBlock(MachineBasicBlock &MBB) { } } - // Finally, if this is a noop copy instruction, zap it. + // Finally, if this is a noop copy instruction, zap it. (Except that if + // the copy is dead, it must be kept to avoid messing up liveness info for + // the register scavenger. See pr4100.) unsigned SrcReg, DstReg, SrcSubReg, DstSubReg; if (TII->isMoveInstr(*MI, SrcReg, DstReg, SrcSubReg, DstSubReg) && - SrcReg == DstReg) + SrcReg == DstReg && DeadDefs.empty()) MBB.erase(MI); } diff --git a/test/CodeGen/ARM/2009-05-07-RegAllocLocal.ll b/test/CodeGen/ARM/2009-05-07-RegAllocLocal.ll new file mode 100644 index 00000000000..d03b7ce8753 --- /dev/null +++ b/test/CodeGen/ARM/2009-05-07-RegAllocLocal.ll @@ -0,0 +1,12 @@ +; RUN: llvm-as < %s | llc -mtriple=armv5-unknown-linux-gnueabi -O0 -regalloc=local +; PR4100 +@.str = external constant [30 x i8] ; <[30 x i8]*> [#uses=1] + +define i16 @fn16(i16 %arg0.0, <2 x i16> %arg1, i16 %arg2.0) nounwind { +entry: + store <2 x i16> %arg1, <2 x i16>* null + %0 = call i32 (i8*, ...)* @printf(i8* getelementptr ([30 x i8]* @.str, i32 0, i32 0), i32 0) nounwind ; [#uses=0] + ret i16 0 +} + +declare i32 @printf(i8*, ...) nounwind