<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On 16 May 2017 at 12:48, 陳韋任 via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif">Just a guess. Maybe you can write C code mimic what you want to achieve, then compile it to LLVM IR.</div><div style="font-family:arial,helvetica,sans-serif">See what LLVM IR generated by FE.</div></div></blockquote><div><br></div><div>Probably won't work, since both the type of x in switch(x) and case-labels must be integral types (not pointers).<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div style="font-family:arial,helvetica,sans-serif"><br></div><div style="font-family:arial,helvetica,sans-serif">Regards,</div><div style="font-family:arial,helvetica,sans-serif">chenwj</div></div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">2017-05-16 0:29 GMT+08:00 Benoit Vey via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi.<br>
<br>
First of all, some context. I'm trying to implement a new functionality in an LLVM-based compiler and I need to take various actions based on the value of a given pointer, the possible values being the addresses of various global constants. I tried to use a `switch` instruction but I encountered several problems.<br>
<br>
The "ideal switch" I'd like to have would look something like this (with way more cases):<br>
<br>
; ModuleID = 'module'<br>
source_filename = "module"<br>
<br>
@var0 = global i32 0<br>
@var1 = global i32 0<br>
<br>
define i32 @func(i32*) {<br>
entry:<br>
switch i32* %0, label %unreachable [<br>
i32* @var0, label %var0<br>
i32* @var1, label %var1<br>
]<br>
<br>
var0: ; preds = %entry<br>
br label %post<br>
<br>
var1: ; preds = %entry<br>
br label %post<br>
<br>
unreachable: ; preds = %entry<br>
unreachable<br>
<br>
post: ; preds = %var1, %var0<br>
%1 = phi i32 [ 0, %var0 ], [ 1, %var1 ]<br>
ret i32 %1<br>
}<br>
<br>
This example is impossible because a `switch` cannot have pointer operands. So I tried with `ptrtoint`, turning the `switch` into this:<br>
<br>
%1 = ptrtoint i32* %0 to i64<br>
switch i64 %1, label %unreachable [<br>
i64 ptrtoint (i32* @var0 to i64), label %var0<br>
i64 ptrtoint (i32* @var1 to i64), label %var1<br>
]<br>
<br>
I'm a bit baffled by that one. According to the documentation the address of a global value is a constant, yet this `switch` is impossible because the result of the `ptrtoint` isn't a constant integer.<br>
<br>
At that point, I don't really know how to proceed. My questions are:<br>
<br>
- Why is `switch` restricted to integers?<br></blockquote></div></div></div></div></blockquote><div><br></div><div>Same reason C only supports it? <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_extra"><div><div class="h5"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- What is the result of a constant `ptrtoint` and why isn't it a constant integer?<br></blockquote></div></div></div></div></blockquote><div><br></div><div>Well, a POINTER isn't a true constant either, so I'm not entirely sure your original value is indeed, truly, a constant. But perhaps splitting hairs.<br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_extra"><div><div class="h5"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
- Is there a way to switch on pointers despite these restrictions?<br></blockquote></div></div></div></div></blockquote><div><br></div><div>I'm not sure what you are ACTUALLY trying to solve. The subject seems a bit XY-question - you want to achieve X, you think Y is the way to do that, so you ask how to do Y - if you explain "I want to do X, how should I do that?", maybe someone can come up with a good answer? <br><br>--<br></div><div>Mats<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_extra"><div><div class="h5"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Thanks.<br>
<br>
------------------------------<wbr>------------------------------<wbr>----<br>
This message was sent using IMP, the Internet Messaging Program.<br>
<br>
<br>
______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
</blockquote></div><br><br clear="all"><div><br></div></div></div><span class="HOEnZb"><font color="#888888">-- <br><div class="m_-6588936642367253489gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Wei-Ren Chen (陳韋任)<br>Homepage: <a href="https://people.cs.nctu.edu.tw/~chenwj" target="_blank">https://people.cs.nctu.edu.tw/<wbr>~chenwj</a></div></div></div>
</font></span></div>
<br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div></div>