Optimize switch lookup tables with linear mapping.
authorErik Eckstein <eeckstein@apple.com>
Mon, 17 Nov 2014 09:13:57 +0000 (09:13 +0000)
committerErik Eckstein <eeckstein@apple.com>
Mon, 17 Nov 2014 09:13:57 +0000 (09:13 +0000)
commit105374fe5efa11c978da69417481d5a92f4890a1
tree70deffa59a94c97af6b6692e0ec593701cb2788a
parenta61a19037a9dbf43800e2bbdda250f86625aa59e
Optimize switch lookup tables with linear mapping.

This is a simple optimization for switch table lookup:
It computes the output value directly with an (optional) mul and add if there is a linear mapping between index and output.
Example:

int f1(int x) {
  switch (x) {
    case 0: return 10;
    case 1: return 11;
    case 2: return 12;
    case 3: return 13;
  }
  return 0;
}

generates:

define i32 @f1(i32 %x) #0 {
entry:
  %0 = icmp ult i32 %x, 4
  br i1 %0, label %switch.lookup, label %return

switch.lookup:
  %switch.offset = add i32 %x, 10
  ret i32 %switch.offset

return:
  ret i32 0
}

llvm-svn: 222121
llvm/lib/Transforms/Utils/SimplifyCFG.cpp
llvm/test/Transforms/SimplifyCFG/X86/switch_to_lookup_table.ll