Arnaud Allard de Grandmaison
2010-Jun-03 13:33 UTC
[LLVMdev] Unused argument registers can not be reused ?
While migrating my codebase from llvm-2.6 to llvm-2.7, I found a different behaviour in the register allocation. I have been able to reproduce it using the msp430 backend, with the 2.7 release as well as the svn head. For the msp430, the first four parameters of a function are passed thru registers. What I observe is that if those parameters are not used inside the function, those registers can not be used. For example, with the following C code:> cat test_unused_regs.cshort a = 2; short b = 32; short r = 1024; void test(short w, short x, short y, short z) { r -= a+b; } I get the following assembly code for the test function after running thru clang:> clang -ccc-host-triple msp430-unknown-unknown -O2 -fomit-frame-pointer -S -o test_unused_regs.s test_unused_regs.c > cat test_unused_regs.s... test: push.w r11 mov.w &b, r11 add.w &a, r11 sub.w r11, &r pop.w r11 ret ... Instead of using r11, llvm should have used r12/r13/r14 or r15. This was my expectation, and the behaviour with llvm-2.6. This would have spared saving/restoring r11. Although this is not a functionnal regression, this looks to me like a performance regression, unless the calling convention has changed (i.e. the arguments can not be clobbered anymore). I have not yet understood why, but the liveIntervals analysis dump looks dubious to me (R12W,R13W,R14W and R15W should be dead/killed livein registers) :> llc -march=msp430 -debug-only=liveintervals -o test_unused_regs.s test_unused_regs.ll********** COMPUTING LIVE INTERVALS ********** ********** Function: test BB#0: # derived from entry livein register: R15W live through +[0,40:0) livein register: R15B dead +[0,3:0) livein register: R14W live through +[0,40:0) livein register: R14B dead +[0,3:0) livein register: R13W live through +[0,40:0) livein register: R13B dead +[0,3:0) livein register: R12W live through +[0,40:0) livein register: R12B dead +[0,3:0) 4 %reg1028<def> = MOV16rm %reg0, <ga:@b>; mem:LD2[@b] register: %reg1028 +[6,14:0) 12 %reg1029<def> = MOV16rr %reg1028<kill> register: %reg1029 +[14,30:0) 20 %reg1029<def> = ADD16rm %reg1029, %reg0, <ga:@a>, %SRW<imp-def>; mem:LD2[@a] register: %reg1029 replace range with [14,22:1) RESULT: %reg1029,0.000000e+00 = [14,22:1)[22,30:0) 0 at 22-(30) 1 at 14-(22) 28 SUB16mr %reg0, <ga:@r>, %reg1029<kill>, %SRW<imp-def>; mem:ST2[@r] LD2[@r] 40 RET Any help or hints would be much welcome ! Best regards, -- Arnaud de Grandmaison -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100603/1db8a188/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: test_unused_regs.ll Type: application/octet-stream Size: 909 bytes Desc: test_unused_regs.ll URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100603/1db8a188/attachment.obj> -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: test_unused_regs.c URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100603/1db8a188/attachment.c>
Jakob Stoklund Olesen
2010-Jun-03 17:39 UTC
[LLVMdev] Unused argument registers can not be reused ?
On Jun 3, 2010, at 6:33 AM, Arnaud Allard de Grandmaison wrote:> While migrating my codebase from llvm-2.6 to llvm-2.7, I found a different behaviour in the register allocation. I have been able to reproduce it using the msp430 backend, with the 2.7 release as well as the svn head. > > For the msp430, the first four parameters of a function are passed thru registers. What I observe is that if those parameters are not used inside the function, those registers can not be used.It looks like you found a bug, R12-R15 should be usable inside the function. Please file a PR against the code generator. I think that unused arguments shouldn't have been marked live in at all. Thanks, /jakob
Arnaud Allard de Grandmaison
2010-Jun-03 20:01 UTC
[LLVMdev] FW: Unused argument registers can not be reused ?
Oops. Forgot the mailing list in the reply. -- Arnaud de Grandmaison ________________________________________ From: Arnaud Allard de Grandmaison Sent: Thursday, June 03, 2010 9:57 PM To: Jakob Stoklund Olesen Subject: RE: [LLVMdev] Unused argument registers can not be reused ? I have finally been able to make some progress : the bug has been introduced/revealed by svn commit #95493, which enabled DeadMachineInstructionElimPass for all targets. Disabling this pass makes llvm-2.7 behave as expected. What we can see when dumping the test function body at the path entry & exit is : # Machine code for function test: Function Live Ins: %R15W in reg%1024, %R14W in reg%1025, %R13W in reg%1026, %R12W in reg%1027 BB#0: derived from LLVM BB %entry Live Ins: %R15W %R14W %R13W %R12W %reg1027<def> = MOV16rr %R12W %reg1026<def> = MOV16rr %R13W %reg1025<def> = MOV16rr %R14W %reg1024<def> = MOV16rr %R15W %reg1028<def> = MOV16rm %reg0, <ga:@b>; mem:LD2[@b] %reg1029<def> = ADD16rm %reg1028, %reg0, <ga:@a>, %SRW<imp-def,dead>; mem:LD2[@a] SUB16mr %reg0, <ga:@r>, %reg1029, %SRW<imp-def,dead>; mem:ST2[@r] LD2[@r] RET # End machine code for function test. # Machine code for function test: Function Live Ins: %R15W in reg%1024, %R14W in reg%1025, %R13W in reg%1026, %R12W in reg%1027 BB#0: derived from LLVM BB %entry Live Ins: %R15W %R14W %R13W %R12W %reg1028<def> = MOV16rm %reg0, <ga:@b>; mem:LD2[@b] %reg1029<def> = ADD16rm %reg1028, %reg0, <ga:@a>, %SRW<imp-def,dead>; mem:LD2[@a] SUB16mr %reg0, <ga:@r>, %reg1029, %SRW<imp-def,dead>; mem:ST2[@r] LD2[@r] RET # End machine code for function test. This points to 2 different ways of fixing the issue : 1- Fix the backends to not lower unused arguments, or at least, not mark them as liveins --- if possible. 2- Fix the pass I will fill a bug tomorrow for this tomorrow. Best regards, -- Arnaud de Grandmaison ________________________________________ From: Jakob Stoklund Olesen [stoklund at 2pi.dk] Sent: Thursday, June 03, 2010 7:39 PM To: Arnaud Allard de Grandmaison Cc: llvmdev at cs.uiuc.edu Subject: Re: [LLVMdev] Unused argument registers can not be reused ? On Jun 3, 2010, at 6:33 AM, Arnaud Allard de Grandmaison wrote:> While migrating my codebase from llvm-2.6 to llvm-2.7, I found a different behaviour in the register allocation. I have been able to reproduce it using the msp430 backend, with the 2.7 release as well as the svn head. > > For the msp430, the first four parameters of a function are passed thru registers. What I observe is that if those parameters are not used inside the function, those registers can not be used.It looks like you found a bug, R12-R15 should be usable inside the function. Please file a PR against the code generator. I think that unused arguments shouldn't have been marked live in at all. Thanks, /jakob