[LLVMdev] replacing a global variable by a constant

Matthijs Kooijman matthijs at stdin.nl
Fri Dec 5 07:16:13 PST 2008


Hi Ralf,

> I am trying to replace a global variable with a constant.
You should be able to replace the initializer for the global with your
constant, mark it as having internal linkage and then run some optimization
pass over it to do the rest (not sure which one it was again, probably some
constant propagation or global value optimization).

That pass would then probably do some similar iterating as you've described,
but at least you won't have to maintain that part of code :-)

> I did manage to do it, but somehow it appears to be fairly bruteforce by
> just iterating over all functions/bblocks/instructions and filtering for
>  those that load the variable and replacing the instruction with
> Instruction::replaceAllUsesWith().
> The more intuitive way of iterating over the uses of the variable did
> not work out as I have to replace the whole instruction instead of just
> the operand (as GlobalVariable::replaceAllUsesWith() does) in order to
> prevent loading from the memory address of the constant instead of
> loading the constant :p. Basically what seems to be missing is some
> method that returns the parent-instruction of a Use.
You can use Use::getUser(), which returns a User (from which Instruction and
LoadInst derive). Note that you will have to accoutn for other uses than Loads
(such as the initializers of other globals), but in those cases your
transformation can probably not proceed anyway.

> Does an invokation of a JIT through runFunction() perform any
> optimization(s) by itself? 
I don't thinks so, not sure.

> Is there a way to display the generated code?
No clue :-)

Gr.

Matthijs
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20081205/5044fb1c/attachment.sig>


More information about the llvm-dev mailing list