[LLVMdev] Replacing uses within a function

Carlos Sánchez de La Lama carlos.delalama at urjc.es
Fri Oct 21 10:03:45 PDT 2011

Hi John,

> > store i32 0, i32* getelementptr inbounds ([3 x i32]* @value1, i32 0, i32
> > 0)
> The issue here is that the store instruction is using a constant 
> expression (ConstExpr) which is using the value that you wish to 
> replace.  The GEP that you see here is not a GEP instruction but a GEP 
> constant expression (http://llvm.org/docs/LangRef.html#constantexprs).

Ok, got the point.

> First, you could use something like the BreakConstantGEP pass from 
> SAFECode to convert constant expression GEPs into GEP instructions.  
> Then your old approach will work properly.  However, the speed of the 
> code may suffer because GEP constant expressions are replaced with 
> constant values during code generation while GEP instructions most 
> likely will not.  You could, of course, modify the code to selectively 
> convert GEP constant expressions used within the function that you care 
> about.

As you say this looks like some de-optimization, I prefer constants to
remain constants for more efficient code.

> A second approach is to simply search for constant expressions that are 
> used within the function you care about and replace them with a new 
> constant expression that uses your new value.  I think constants are 
> unique in LLVM, so you can't just call 
> ConstantExpr::replaceUsesOfWith(); you'll need to create a new constant 
> expression that is identical to the old one but using the new value, and 
> then replace the specific use of the old constant expression with the 
> new one that you've created.

Actually I have found ConstantExpr::replaceUsesOfWithOnConstant on the
API, which does the trick. Now my only problem is how to find all the
ConstantExprs within a function (as I guess ConstantExprs can have more
ConstantExprs as operands) but that is doable.  

You made it pretty clear, thank you :)


More information about the llvm-dev mailing list