[PATCH] Simplify switch table lookup if a linear mapping is possible

Erik Eckstein eeckstein at apple.com
Thu Nov 13 07:26:43 PST 2014


Hi Hans,

this patch adds 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:                                    ; preds = %entry
  %switch.offset = add i32 %x, 10
  ret i32 %switch.offset

return:                                           ; preds = %entry
  ret i32 0
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: llvm-switch-linearmap.patch
Type: application/octet-stream
Size: 6260 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141113/cec1bf92/attachment.obj>
-------------- next part --------------


Can you (or anyone else) please review it.

Thanks,
Erik



More information about the llvm-commits mailing list