[LLVMdev] creating a constant with the address of another constant

Chris Lattner sabre at nondot.org
Mon Jul 31 20:22:23 PDT 2006


On Mon, 31 Jul 2006, [UTF-8] Rafael Esp?ndola wrote:
> In ARM, the conventional way of setting a register to a 32 bit
> constant is to use a load:
> ---------------------------------
> str:
> .asciz  "Hello World"
>
> .text
> main:
>    ...
>    ldr r0, .L3
>    ....
> .L3:
>       .word   str
> -----------------------------------
>
> To implement this, LowerGlobalAddress must add an element to the
> constant pool (.L3 in the example). How can I implement this?

The traditional way you do this is with the MachineConstantPool class 
(reachable from the current MachineFunction).  You can add it to the 
constant pool two ways.

If you aren't in the lowering phase, you can directly manipulate the 
constant pool.  To plop the address of the global into the constant pool, 
you'd do this, for example:

MachineConstantPool &MCP = ..
GlobalValue *GV = ...
unsigned CPI = MCP.getConstantPoolIndex(GV, /*alignment*/4);


If you *are* in the context of lowering (which is more likely), you can 
put it into the constantpool and create a SDNode by using:

SDOperand CPAddr = DAG.getConstantPool(GV, /*pointer type*/ MVT::i32, /*alignment*/ 4);

If it helps, there are examples around that use it, e.g. 
X86TargetLowering::LowerFABS.

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/



More information about the llvm-dev mailing list