unreachable branch in codegen for switch statement
Lukas Stadler
lukas.stadler at jku.at
Wed Nov 6 03:07:42 PST 2013
The problem in this particular case is that a switch with a default target is not recognized as a non-fall-through instruction in LIRGenerator.endsWithJump().
I’ve just pushed some changes that get rid of this unnecessary jump, by taking into account the (somewhat ill-named) FallThroughOps.
There’s still a problem left - occasionally, the a SwitchOp emits useless jumps to the next instruction, because the control flow optimization logic doesn’t know how to optimize blocks ending in switches.
Getting rid of this will require a bit more refactoring.
- Lukas
On 06 Nov 2013, at 3:22, Venkatachalam, Vasanth <Vasanth.Venkatachalam at amd.com> wrote:
> Doug or others-
>
> When I ran the test case below through the AMD64 backend, I noticed an unreachable branch in the code generated below. Namely, the jmp instruction at 0x00007facc8145a60. It appears this is coming from the call to emitJump( ) in LIRGenerator.doBlock( ), which appends a jump instruction to the end of each basic block, which may later get optimized away by the reg. allocator. Is there a reason why this jmp isn't being optimized away? Is there any way around this?
> I tried -G:+RemoveNeverExecutedCode but this isn't making a difference.
>
> I'm hitting the same issue when I generate HSAIL code for this switch statement, but I get an assembler error due to this unreachable branch.
>
> public int switchTest(int n) {
> switch (n) {
> case 0:
> return 0;
> case 1:
> return 1;
> case 2:
> return 2;
> default:
> return 3;
> }
> }
>
> 0x00007facc8145a40: cmp edx,0x0
> 0x00007facc8145a43: je 0x00007facc8145a6b
> 0x00007facc8145a49: cmp edx,0x1
> 0x00007facc8145a4c: je 0x00007facc8145a72
> 0x00007facc8145a52: cmp edx,0x2
> 0x00007facc8145a55: je 0x00007facc8145a79
> 0x00007facc8145a5b: jmp 0x00007facc8145a65
> 0x00007facc8145a60: jmp 0x00007facc8145a6b //unreachable code
> 0x00007facc8145a65: mov eax,0x3
> 0x00007facc8145a6a: ret
> 0x00007facc8145a6b: mov eax,0x0 //case 0
> 0x00007facc8145a70: jmp 0x00007facc8145a6a
> 0x00007facc8145a72: mov eax,0x1 //case 1
> 0x00007facc8145a77: jmp 0x00007facc8145a6a
> 0x00007facc8145a79: mov eax,0x2 //case 2
> 0x00007facc8145a7e: jmp 0x00007facc8145a6a
More information about the graal-dev
mailing list