[llvm-commits] [LLVM, SwitchInst, case ranges] Auxiliary patch #1

Chris Lattner clattner at apple.com
Thu Jan 5 10:27:57 PST 2012


Hi Stephan,

I'm sorry for being distracted and not getting back to you about this earlier.

On Jan 5, 2012, at 9:43 AM, Stepan Dyatkovskiy wrote:
> Due to case-ranges feature (PR1255), we want to replace switch cases type: from ConstantInt to APInt. Of course it may be some new type, call it APIntEx. We need "less" comparison for this type, since we need to sort it sometimes. And it also should support initialized property.  I think that two this features will be useful for APInt itself. But of course it is possible to create new type. So, your suggestions. APIntEx for case values?


Lets discuss the design of this feature before we finalize on an implementation.  PR1255 is really about two different things: 1) implementing support for GCC-style case ranges which may make Fortran and Ada support better.  2) making it easier to canonicalize switches in the optimizer, eliminating the critical edge problem.  I'm personally mostly interested in #2 :), but I'll admit that it makes things a bit more complex. 

If we were only interested in solving #1, we'd want to have a [start,end] range value for each edge out of a switch.  However, this isn't enough to solve #2, because we'd really like to be able to canonicalize switches that have arbitrarily different values going to the same block (assuming all phi values match).  For example, I'd really like to have one CFG edge for:

switch (x)
case 4:
case 17:
case 97:

just like we should only have one edge for:

switch (x)
case 4...1000:

Doing this would allow simplifycfg to build this canonical form, and reduce some bad cases we get with switches.

If you're willing to agree that this is important, then we have to figure out how to represent this.  Despite the pushback I gave in PR1255, perhaps representing these things as constants really is the best way to go (but SwitchInst should have good accessors to dig into this).

>From a syntax perspective, I think that something like this would make sense (though it would be better if the default label could also be merged into one of the cases):

switch i32 %val, label %otherwise
   [ i32 0, label %onzero
     i32 4 .. 1000, label %onrange
     i32 1, i32 1004, i32 4000 ... 5000, label %onstuff ]

Given a structure like this, I revise my stance and think that it would be easiest to represent this with a ConstantArray, and maybe even represent the ranges as ConstantVectors or something.

What do you think?

-Chris



More information about the llvm-commits mailing list