<div dir="ltr">Thanks Jim :)<div><br></div><div>Answers inlined</div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-06-12 18:39 GMT+01:00 Jim Grosbach <span dir="ltr"><<a href="mailto:grosbach@apple.com" target="_blank">grosbach@apple.com</a>></span>:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word">Hi Marcello,<div><br></div><div>This is awesome. I’ve been wanting improvements like this for a long time now, but have never gotten around to doing it myself. My procrastination^Wpatience is paying off!</div>

<div><br></div><div>When running on the LLVM test suite (including externals, preferably), how often does this optimization fire?</div><div><br></div><div>A few detail comments below. Nothing major.</div><div><br></div><div>

General style nitpick: make sure comments are full sentences and end in a period.</div><div><br></div><div><br></div></div></blockquote><div><br></div><div>Thank you for the suggestion Jim!</div><div>About the number of triggers of the optimization in the standard LLVM tests the switch lowering is not run in a lot of tests (8 tests in total I believe). <br>

</div><div>When it ran though the the optimization was triggered 3 times out of 8.</div><div><br></div><div>In externals where was used much more I saw it triggering about 15%.</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div style="word-wrap:break-word"><div></div><div><blockquote type="cite">+// LowerBound and UpperBound are used to keep track of the bounds for Val       <br>+// that have being already checked by a block emitted by one of the previous    <br>

+// calls to switchConvert in the call stack.   </blockquote><br></div><div>s/have being already/have already been/</div><div><br></div></div></blockquote><div><br></div><div>Fixed :P </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div style="word-wrap:break-word"><div></div><div><blockquote type="cite"><div>+    // Check if the Case Range is perfectly squeezed in between</div><div>+    // already checked Upper and Lower bounds. If it is then we can avoid</div>

<div>+    // emitting the code that checks if the value actually falls in the range</div><div>+    // because the bounds already tell us so</div><div>+    if (LowerBound != nullptr && UpperBound != nullptr &&</div>

<div>+        Begin->Low == LowerBound && Begin->High == UpperBound) {</div><div>+      return Begin->BB;</div><div>+    }</div></blockquote><div><br></div><div>Can Begin->Low or Begin->High ever be null? If not, the explicit nullptr checks can be removed as they’re implicit. If they can be null, that seems a bit odd for the algorithm.</div>

<div><br></div></div></div></blockquote><div> </div><div>Oh, good spot here! Low and High actually cannot be null. Thanks. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word">
<div><div></div><div><blockquote type="cite">+  // NewLowerBound here should never be the integer value minimal.</blockquote><br></div><div>I don’t quite follow this comment.</div><div><br></div></div></div></blockquote>
<div>
<br></div><div>Yeah, it could have been more explanatory ... :P</div><div>What I mean here is that the NewLowerBound variable cannot have assigned the smallest value an integer value of the type the switch is evaluating can encode (like -128 for an i8 for example) and that is safe to do -1 without overflowing.</div>

<div>The reason for that is that NewLowerBound is never computed from the case with the lowest value, so if there is at least one case with a lower value of the one we are computing that value from it means it cannot have assigned the lowest available value and we can subtract at least one from it.</div>

<div><br></div><div>A little bit convoluted but I hope it explained it ... </div><div><br></div><div>I added a more descriptive comment and also some extra comments related to that.</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div style="word-wrap:break-word"><div><div></div><div><blockquote type="cite">+  // Optimize the case where Default is an unreachable block                    <br>+  if (DefaultIsUnreachable) {                                                   <br>

+    CaseItr LastCase = Cases.begin() + Cases.size() - 1;                        <br>+    UpperBound = cast<ConstantInt>(LastCase->High);                             <br>+    LowerBound = cast<ConstantInt>(Cases.begin()->Low);                         <br>

+  }</blockquote></div><div><br></div><div>While accurate, the comment is a bit terse. It would be helpful to explain how we’re optimizing it, not just that we are. I.e., explain why the following code results in an optimization.</div>

<div><br></div><br></div></div></blockquote><div><br></div><div>Tried to make it a little bit more descriptive :) </div><div><br></div><div>Marcello</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div style="word-wrap:break-word"><div><div><blockquote type="cite"><div><div><div>On Jun 11, 2014, at 4:22 PM, Marcello Maggioni <<a href="mailto:hayarms@gmail.com" target="_blank">hayarms@gmail.com</a>> wrote:</div>

<br></div></div><div><div><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">

<div>I'm updating the patch to also remove the default basic block if it ends up being dead after switch lowering (no predecessors)<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Marcello<br><br>

<div class="gmail_quote">2014-06-11 20:34 GMT+01:00 Marcello Maggioni<span> </span><span dir="ltr"><<a href="mailto:hayarms@gmail.com" target="_blank">hayarms@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">

<div dir="ltr">Joerg,<div><br></div><div>I addressed your suggestion in this patch.</div><div>It was quite easy to add and can be useful in general, so thanks!</div><div><br></div><div>I also added a test that tests this kind of optimization being applied.</div>

<div><br></div><div>Marcello</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-06-11 18:37 GMT+01:00 Joerg Sonnenberger<span> </span><span dir="ltr"><<a href="mailto:joerg@britannica.bec.de" target="_blank">joerg@britannica.bec.de</a>></span>:<div>

<div><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div>On Wed, Jun 11, 2014 at 05:28:05PM +0100, Marcello Maggioni wrote:<br>

> Yeah, it is suboptimal, it doesn't take into consideration the fact that<br>> the default is unreachable.<br>><br>> I'll a look at it later to see if it is easy to also take into<br>> consideration this case and add it to the patch + test for the condition.<br>

<br></div>Thanks. Please don't take this as hold up for pushing the original<br>patch, it can just as well be a follow-up commit.<br><div><br>Joerg<br>_______________________________________________<br>llvm-commits mailing list<br>

<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>

</div></blockquote></div></div></div><br></div></blockquote></div><br></div></div></div></div><span><optimized_lower_switch_v5.patch></span><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">_______________________________________________</span><div>

<br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">

<span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">llvm-commits mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">

<a href="mailto:llvm-commits@cs.uiuc.edu" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">llvm-commits@cs.uiuc.edu</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">

<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></div>

</div></blockquote></div><br></div></div></blockquote></div><br></div></div>