[LLVMdev] Quirk in switch lowering

Jasper Neumann jasper.neumann at web.de
Tue Dec 24 14:42:30 PST 2013


Hello all,

in SelectionDAGBuilder::handleBTSplitSwitchCase 
(lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp) seems to be a quirk 
which can effectively create an else-if chain from a very sparse switch 
statement instead of a balanced binary tree.

A possible solution is to weight an isolated single switch label with 0. 
To do this one can replace the assignments to LDensity und RDensity by:
     volatile double LDensity =
        (double)(LSize-1).roundToDouble() /
                            (LEnd - First + 1ULL).roundToDouble();
     volatile double RDensity =
       (double)(RSize-1).roundToDouble() /
                            (Last - RBegin + 1ULL).roundToDouble();

I replaced ?Size by (?Size-1).

Here is a more or less stupid test program (translate with "clang -O3 -S 
test.cpp" and then look at test.s):

int main(int argc, char **argv) {
   switch (argc) {
     case 100: return 1;
     case 200: return 2;
     case 300: return 3;
     case 400: return 4;
     case 500: return 5;
     case 600: return 6;
     case 700: return 7;
     case 800: return 8;
     case 900: return 9;
     default: return 0;
     }
   }

Best regards
Jasper Neumann



More information about the llvm-dev mailing list