<div dir="ltr">Hi!<br><div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/7/18 Erik Rantapaa <span dir="ltr">&lt;<a href="mailto:erantapaa@yahoo.com" target="_blank">erantapaa@yahoo.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
I&#39;ve been looking at the assembly code that ghc generates for simple pattern matching for situations like:<br>
<br>
foo :: Int -&gt; Int<br>
foo 1 = 3<br>
foo 2 = 10<br>
foo 3 = 2<br>
foo 4 = 42<br>
-- ... other random assignments<br>
<br>
and I noticed that it doesn&#39;t seem to generate a jump table (or even a table lookup)  like, for instance, gcc would for this switch statement:<br>
<br>
int foo(int x) {<br>
  switch (c) {<br>
    case 1: return 3;<br>
    case 2: return 10;<br>
    case 3: return 2;<br>
    case 4: return 42;<br>
    // ... etc.<br>
  }<br>
}<br>
<br>
Under -O3 ghc seems to produce a binary-search of the cases.<br>
<br>
Questions: Would generating a jump/lookup table for pattern matches like this (i.e. with a suitable density of cases) be beneficial? And, if so, would it be difficult to add to ghc? And would it be a good first project for someone who wants to get into the ghc code base (perhaps with some mentoring from the community?)<br>
</blockquote><div><br></div><div>I was just digging around in the native code generator so I have a few leads for you.<br> <br></div><div>GHC can already generate jump tables, look at genSwitch in compiler/nativeGen/X86/CodeGen.hs, and it is what a CmmSwitch gets compiled into. Follow that into the codeGen and you see CmmSwitch is only created in emitSwitch in StgCmmExpr.hs. You can follow that farther and see when that is invoked. <br>
</div><div><br></div><div>I have no clue if it&#39;s a worthwhile thing to do. Try it and measure the impact.<br><br></div><div>Niklas<br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</blockquote></div><br></div></div></div>