<div dir="ltr"><br><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Jul 24, 2014 at 11:49 AM, Hans Wennborg <span dir="ltr"><<a href="mailto:hans@chromium.org" target="_blank">hans@chromium.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On Wed, Jul 23, 2014 at 4:13 PM, Manman Ren <<a href="mailto:manman.ren@gmail.com" target="_blank">manman.ren@gmail.com</a>> wrote:<br>


> Author: mren<br>
> Date: Wed Jul 23 18:13:23 2014<br>
> New Revision: 213815<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=213815&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=213815&view=rev</a><br>
> Log:<br>
> SimplifyCFG: fix a bug in switch to table conversion<br>
><br>
> We use gep to access the global array "switch.table", and the table index<br>
> should be treated as unsigned. When the highest bit is 1, this commit<br>
> zero-extends the index to an integer type with larger size.<br>
<br>
</div>Thanks for catching and fixing this! Maybe it should get merged to the<br>
3.5 branch?<br>
<br>
The patch looks good; I only have a few suggestions inline below.<br>
<div><br>
> -Value *SwitchLookupTable::BuildLookup(Value *Index, IRBuilder<> &Builder) {<br>
> +Value *SwitchLookupTable::BuildLookup(Value *Index, uint64_t TableSize,<br>
> +                                      IRBuilder<> &Builder) {<br>
<br>
</div>It feels wrong having to pass the TableSize here. SwitchLookupTable<br>
should know its own size.<br>
<div><br>
>    switch (Kind) {<br>
>      case SingleValueKind:<br>
>        return SingleValue;<br>
> @@ -3624,6 +3625,14 @@ Value *SwitchLookupTable::BuildLookup(Va<br>
>                                   "switch.masked");<br>
>      }<br>
>      case ArrayKind: {<br>
<br>
</div>We can get the table size from the Array member:<br>
<br>
uint64_t TableSize =<br>
cast<Constant>(Array->getInitializer())->getType()->getArrayNumElements();</blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
(Or we could even store it as a new ArraySize member when we construct<br>
the Array in the SwitchLookupTable constructor.)<br>
<div><br>
> +      // Make sure the table index will not overflow when treated as signed.<br>
> +      if (IntegerType *IT = dyn_cast<IntegerType>(Index->getType()))<br>
<br>
</div>The index is always of integer type, so no need for dyn_cast and if here.<br></blockquote><div><br></div><div>From the language ref, it should be integer type, the verifier does not verify that, and I just found out that the parser does check that.</div>

<div><br></div><div>In r213895.</div><div><br></div><div>Thanks,</div><div>Manman</div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Cheers,<br>
Hans<br>
</blockquote></div><br></div></div>