[LLVMdev] First-class aggregate semantics

Dustin Laurence dllaurence at dslextreme.com
Thu Jan 7 19:48:24 PST 2010


On 01/07/2010 06:03 PM, Alastair Lynn wrote:
> 
> You'll probably need to use insertvalue to construct your return value.

Ah ha!

The fact is I didn't really understand the significance of this part
when I read it, and so didn't remember it when I needed it.  OK, so I
have tested it and I can now build up a struct like this

    %s1 = insertvalue {i32, i32} {i32 0, i32 0}, i32 1, 0 ; s1 = {1,0}
    %s2 = insertvalue {i32, i32} %s1, i32 2, 1            ; %s2 == {1,2}

which reminds me of another thing I never understood.  I can't make my
code (slightly) more readable by changing that to something like

    %s0 = {i32 0, i32 0}
    %s1 = insertvalue {i32, i32} %s0, i32 1, 0     ; s1 = {1,0}
    %s2 = insertvalue {i32, i32} %s1, i32 2, 1     ; %s2 == {1,2}

because LLVM will complain that it "expected instruction opcode" at the
assignment to %s0.  If there is a general way to give names to constants
in that way I didn't find it.  In fact, I think I tended not to use
temporaries like I would variables precisely because when I tried the
second alternative as the natural way to hand-code it and it didn't
work, I didn't think how to phrase it so only the results of operations
get named.

Help me understand the underlying logic--why can one only name the
results of operations?  I realize that the local temporaries are
notionally register variables for a machine with an infinite number of
registers, but my very dim memory of real assembly was that I not only
could load constants into registers but had to do so.  What part of the
picture am I missing here?

You need an IR tutorial.  Or, to speak correctly, *I* need a tutorial.
:-)  But I'm learning....

Dustin



More information about the llvm-dev mailing list