[LLVMdev] LLVM 3.6 and ConstantExpr::replaceUsesOfWithOnConstant(...) strange behavior

Duncan P. N. Exon Smith dexonsmith at apple.com
Mon Mar 23 11:58:51 PDT 2015


> On 2015-Mar-23, at 08:31, Jobin, Gaël <gael.jobin at switzerlandmail.ch> wrote:
> 
> Le 20.03.2015 15:33, Duncan Exon Smith a écrit :
> 
> AFK right now, but IIRC, this function is supposed to be internal to replaceAllUsesWith() and it's not really supposed to be called directly.  Constants aren't supposed to change. 
> 
> Why can't you just call replaceAllUsesWith()?
> 
> -- dpnes
> Because it does not do what I want. Basically, my algorithm is interested in some GlobalVariable that are used by constant expression. My goal is to replace the GlobalVariable reference inside the constant expression by another GlobalVariable. If I use replaceAllUsesWith(...) on the constant expression, I should pass as argument a constant expression recreated from scratch. In the other hand, if I call replaceAllUsesWith(...) on the GlobalVariable "a" with the new GlobalVariable "b", it will replace all variables "a" with "b" in the whole module and I still need the "a" for my algorithm.

It seems strange to me that you need to replace constant expressions
but not other uses.  For example, you'd be translating this code:

    ; This will not change.
    %ptr = getelementptr [2 x i8]* @a, i32 0, i32 0
    store i8 7, i8* %ptr

    ; This will change.
    store i8 7, i8* getelementptr ([2 x i8]* @a, i32 0, i32 0)

into:

    ; This still points at @a.
    %ptr = getelementptr [2 x i8]* @a, i32 0, i32 0
    store i8 7, i8* %ptr

    ; This points at @b now.
    store i8 7, i8* getelementptr ([2 x i8]* @b, i32 0, i32 0)

I don't understand how this is useful.

> it will replace all variables "a" with "b" in the whole module and I still need the "a" for my algorithm.

Just to be sure: you know that this function affects the whole
module too, right?

> 
> The best solution would be the first but, during my research, I found the function replaceUsesOfWithOnConstant(...) that does exactly the job (with some improvment like the "in-place" replacement) and started using it.
> 
> But now, it doesn't work anymore in LLVM 3.6... So, I was interested in the change made between the two versions. And, apart for my algorithm, I think that maybe the new implementation was not very logical. Like I said, the replaceOperandsInPlace(...) seems designed to work with and without a given Use/NumUpdated argument (using an improvment for the first case). So, for me, it makes no sense to force using an Use argument for replaceUsesOfWithOnConstant(...) because it can handle both cases with minor changes.

I'm still skeptical that this functionality is generally useful.  I
think you should find a valid `Use` to send in here (it's surprising
you don't have one already; how do you know `@a` is an operand?).

If we were to add this in-tree, I suppose the right place would be a
non-virtual overload of `Constant::replaceUsesOfWithOnConstant()`
that takes two arguments, searches operands for a `Use`, and then
calls the `virtual` 3-arg version that already exists.



More information about the llvm-dev mailing list