SimplifyCFG: fix a bug in switch to table conversion
authorManman Ren <manman.ren@gmail.com>
Wed, 23 Jul 2014 23:13:23 +0000 (23:13 +0000)
committerManman Ren <manman.ren@gmail.com>
Wed, 23 Jul 2014 23:13:23 +0000 (23:13 +0000)
commit4cae9cb0345162984c45da7769c5bfd2c62f6ff3
tree46057051113a215177621458e63c0796854e9da5
parent48b8c128df059823e15f4b030db23664713dc442
SimplifyCFG: fix a bug in switch to table conversion

We use gep to access the global array "switch.table", and the table index
should be treated as unsigned. When the highest bit is 1, this commit
zero-extends the index to an integer type with larger size.

For a switch on i2, we used to generate:
%switch.tableidx = sub i2 %0, -2
getelementptr inbounds [4 x i64]* @switch.table, i32 0, i2 %switch.tableidx

It is incorrect when %switch.tableidx is 2 or 3. The fix is to generate
%switch.tableidx = sub i2 %0, -2
%switch.tableidx.zext = zext i2 %switch.tableidx to i3
getelementptr inbounds [4 x i64]* @switch.table, i32 0, i3 %switch.tableidx.zext

rdar://17735071

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213815 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Transforms/Utils/SimplifyCFG.cpp
test/Transforms/SimplifyCFG/switch-table-bug.ll [new file with mode: 0644]