<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;">r193068.<div><br><div style=""><div>On Oct 20, 2013, at 8:43 PM, 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=windows-1252"><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">LGTM!  Thanks Michael,<div><br></div><div>-Chris</div><div><br><div><div>On Oct 20, 2013, at 1:51 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-family: Helvetica; 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; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">(Attached wrong patch)<div><br></div><div></div><span><0001-Fix-the-predecessor-removal-logic-in-r193045.patch></span><div></div><div><br></div><div>Michael</div><div><br><div><div>On Oct 20, 2013, at 1:23 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-family: Helvetica; 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;"><div><br class="Apple-interchange-newline">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"><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">Without this logic in the loop.</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><span><0001-Teach-simplify-cfg-how-to-correctly-create-covered-l.patch></span><div style="font-family: Helvetica; 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;"><div></div><div><br></div><div>Michael</div><br><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><br></div><div>-Chris</div></div><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;">    Succ->removePredecessor(SI->getParent());<br>  }<br>  SI->eraseFromParent();<br><br>Added: llvm/trunk/test/Transforms/SimplifyCFG/CoveredLookupTable.ll<br>URL:<span class="Apple-converted-space"> </span><a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/CoveredLookupTable.ll?rev=193045&view=auto">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SimplifyCFG/CoveredLookupTable.ll?rev=193045&view=auto</a><br>==============================================================================<br>--- llvm/trunk/test/Transforms/SimplifyCFG/CoveredLookupTable.ll (added)<br>+++ llvm/trunk/test/Transforms/SimplifyCFG/CoveredLookupTable.ll Sun Oct 20 02:04:37 2013<br>@@ -0,0 +1,48 @@<br>+; RUN: opt -simplifycfg -S %s | FileCheck %s<br>+;<span class="Apple-converted-space"> </span><a href="rdar://15268442">rdar://15268442</a><br>+<br>+target datalayout = "e-p:64:64:64-S128-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f16:16:16-f32:32:32-f64:64:64-f128:128:128-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"<br>+target triple = "x86_64-apple-darwin12.0.0"<br>+<br>+; CHECK-LABEL: define i3 @coveredswitch_test(<br>+; CHECK: entry:<br>+; CHECK-NEXT: sub i3 %input, -4<br>+; CHECK-NEXT: zext i3 %switch.tableidx to i24<br>+; CHECK-NEXT: mul i24 %switch.cast, 3<br>+; CHECK-NEXT: lshr i24 7507338, %switch.shiftamt<br>+; CHECK-NEXT: trunc i24 %switch.downshift to i3<br>+; CHECK-NEXT: ret i3 %switch.masked<br>+<br>+define i3 @coveredswitch_test(i3 %input) {<br>+entry:<br>+  switch i3 %input, label %bb8 [<br>+    i3 0, label %bb7<br>+    i3 1, label %bb<br>+    i3 2, label %bb3<br>+    i3 3, label %bb4<br>+    i3 4, label %bb5<br>+    i3 5, label %bb6<br>+  ]<br>+<br>+bb:                                               ; preds = %entry<br>+  br label %bb8<br>+<br>+bb3:                                              ; preds = %entry<br>+  br label %bb8<br>+<br>+bb4:                                              ; preds = %entry<br>+  br label %bb8<br>+<br>+bb5:                                              ; preds = %entry<br>+  br label %bb8<br>+<br>+bb6:                                              ; preds = %entry<br>+  br label %bb8<br>+<br>+bb7:                                              ; preds = %entry<br>+  br label %bb8<br>+<br>+bb8:                                              ; preds = %bb7, %bb6, %bb5, %bb4, %bb3, %bb, %entry<br>+  %result = phi i3 [ 0, %bb7 ], [ 1, %bb6 ], [ 2, %bb5 ], [ 3, %bb4 ], [ 4, %bb3 ], [ 5, %bb ], [ 6, %entry ]<br>+  ret i3 %result<br>+}<br><br><br>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></div></blockquote></div><br></div></blockquote></div><br style="font-family: Helvetica; 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;"><span style="font-family: Helvetica; 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; float: none; display: inline !important;">_______________________________________________</span><br style="font-family: Helvetica; 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;"><span style="font-family: Helvetica; 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; 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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 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; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></blockquote></div></div></div></blockquote></div><br></div></div></blockquote></div><br></div></body></html>