[llvm-dev] [IR question] Switching on pointers

陳韋任 via llvm-dev llvm-dev at lists.llvm.org
Tue May 16 04:48:47 PDT 2017


Just a guess. Maybe you can write C code mimic what you want to achieve,
then compile it to LLVM IR.
See what LLVM IR generated by FE.

Regards,
chenwj

2017-05-16 0:29 GMT+08:00 Benoit Vey via llvm-dev <llvm-dev at lists.llvm.org>:

> Hi.
>
> 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.
>
> The "ideal switch" I'd like to have would look something like this (with
> way more cases):
>
>     ; ModuleID = 'module'
>     source_filename = "module"
>
>     @var0 = global i32 0
>     @var1 = global i32 0
>
>     define i32 @func(i32*) {
>     entry:
>       switch i32* %0, label %unreachable [
>         i32* @var0, label %var0
>         i32* @var1, label %var1
>       ]
>
>     var0:                                             ; preds = %entry
>       br label %post
>
>     var1:                                             ; preds = %entry
>       br label %post
>
>     unreachable:                                      ; preds = %entry
>       unreachable
>
>     post:                                             ; preds = %var1,
> %var0
>       %1 = phi i32 [ 0, %var0 ], [ 1, %var1 ]
>       ret i32 %1
>     }
>
> This example is impossible because a `switch` cannot have pointer
> operands. So I tried with `ptrtoint`, turning the `switch` into this:
>
>       %1 = ptrtoint i32* %0 to i64
>       switch i64 %1, label %unreachable [
>         i64 ptrtoint (i32* @var0 to i64), label %var0
>         i64 ptrtoint (i32* @var1 to i64), label %var1
>       ]
>
> 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.
>
> At that point, I don't really know how to proceed. My questions are:
>
> - Why is `switch` restricted to integers?
> - What is the result of a constant `ptrtoint` and why isn't it a constant
> integer?
> - Is there a way to switch on pointers despite these restrictions?
>
> Thanks.
>
> ----------------------------------------------------------------
> This message was sent using IMP, the Internet Messaging Program.
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>



-- 
Wei-Ren Chen (陳韋任)
Homepage: https://people.cs.nctu.edu.tw/~chenwj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170516/cdee8cf1/attachment.html>


More information about the llvm-dev mailing list