<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Oct 20, 2013, at 12:47 AM, Chris Lattner <<a href="mailto:clattner@apple.com">clattner@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>On Oct 20, 2013, at 12:04 AM, Michael Gottesman <<a href="mailto:mgottesman@apple.com">mgottesman@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">Author: mgottesman<br>Date: Sun Oct 20 02:04:37 2013<br>New Revision: 193045<br><br>URL:<span class="Apple-converted-space"> </span><a href="http://llvm.org/viewvc/llvm-project?rev=193045&view=rev">http://llvm.org/viewvc/llvm-project?rev=193045&view=rev</a><br>Log:<br>Teach simplify-cfg how to correctly create covered lookup tables for switches on iN with N >= 3.<br></div></blockquote><div dir="auto"><br></div>Thanks Michael.  More specifically, this is handling "fully-covered" switches.  A couple of comments:</div></div></blockquote><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div dir="auto"><br></div><div dir="auto"><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">+  // Compute the maximum table size representable by the integer type we are<br>+  // switching upon.<br>+  const unsigned CaseSize = MinCaseVal->getType()->getPrimitiveSizeInBits();</div></blockquote><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">+  const uint64_t MaxTableSize = CaseSize > 63? UINT64_MAX : 1ULL << CaseSize;<br></div></blockquote><div dir="auto"><br></div><div dir="auto">We don't generally mark local variables const like this.  Please don’t.</div></div></div></blockquote><div><br></div><div>This was a last minute thing that crept in. Its gone.</div><br><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div dir="auto"><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">+  assert(MaxTableSize >= TableSize &&<br>+         "It is impossible for a switch to have more entries than the max "<br>+         "representable value of its input integer type's size.");<br>+<br>+  // If we have a covered lookup table, unconditionally branch to the lookup table<br>+  // BB. Otherwise, check if the condition value is within the case range. If it<br>+  // is so, branch to the new BB. Otherwise branch to SI's default destination.<br>+  const bool GeneratingCoveredLookupTable = MaxTableSize == TableSize;<br></div></blockquote><div dir="auto"><br></div><div dir="auto">This is a "fully covered" table.</div></div></div></blockquote><div><br></div><div>Ok.</div><br><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div dir="auto"><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">+  if (GeneratingCoveredLookupTable) {<br>+    Builder.CreateBr(LookupBB);<br>+  } else {<br>+    Value *Cmp = Builder.CreateICmpULT(TableIndex, ConstantInt::get(<br>+                                         MinCaseVal->getType(), TableSize));<br>+    Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());<br>+  }<br><br>  // Populate the BB that does the lookups.<br>  Builder.SetInsertPoint(LookupBB);<br>@@ -3772,7 +3788,13 @@ static bool SwitchToLookupTable(SwitchIn<br>  // Remove the switch.<br>  for (unsigned i = 0; i < SI->getNumSuccessors(); ++i) {<br></div></blockquote><div dir="auto"><br></div><div dir="auto">This is not a problem in your patch, but there is no reason to evaluate getNumSuccessors() each time through the loop.</div></div></div></blockquote><div><br></div><div>Sounds good.</div><br><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div dir="auto"><br><blockquote type="cite"><div style="font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">    BasicBlock *Succ = SI->getSuccessor(i);<br>-    if (Succ == SI->getDefaultDest()) continue;<br>+<br>+    // If we are not generating a covered lookup table, we will have a<br>+    // conditional branch from SI's parent BB to SI's default destination if our<br>+    // input value lies outside of our case range. Thus in that case leave the<br>+    // default destination BB as a predecessor of SI's parent BB.<br>+    if (Succ == SI->getDefaultDest() && !GeneratingCoveredLookupTable)<br>+      continue;<br></div></blockquote><div dir="auto"><br></div><div dir="auto">This doesn't seem like the right check.  If there is a switch whose default destination is "BB" and there are explicit edges to the same block, this will incorrectly remove multiple predecessors.  The right patch for this is simply:</div><div dir="auto"><br></div><div dir="auto"><div>  if (GeneratingCoveredLookupTable) {<br>    Builder.CreateBr(LookupBB);</div><div>    SI->getDefaultDest()->removePredecessor(SI->getParent());</div><div>  } else {<br>    Value *Cmp = Builder.CreateICmpULT(TableIndex, ConstantInt::get(<br>                                         MinCaseVal->getType(), TableSize));<br>    Builder.CreateCondBr(Cmp, LookupBB, SI->getDefaultDest());<br>  }<br></div><div> </div></div></div></div></blockquote><blockquote type="cite"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div dir="auto"><div dir="auto"><div>Without this logic in the loop.</div></div></div></div></blockquote><div><br></div><div>Ok you are right. Removing a successor which is no longer a successor is not a good thing to do = /. (*DOH*)</div><div><br></div><div>How does the attached patch look?</div><div><br></div><div></div></div></body></html>