[LLVMdev] Assignment of large objects, optimization?

Duncan Sands baldrick at free.fr
Mon Mar 12 08:24:50 PDT 2012


Hi Patrik,

> My fronted generates (bad) code, which I see that LLVM is unable to optimize.
> For example, code similar to:
> %a = type [32 x i16]
> declare void @set_obj(%a*)
> declare void @use_obj(%a*)
> define void @foo() {
> entry:
> %a1 = alloca %a
> %a2 = alloca %a
> call void @set_obj(%a* %a2)
> %a3 = load %a* %a2
> store %a %a3, %a* %a1
> call void @use_obj(%a* %a1)
> ret void
> }
> (Or with load/store replaced with memcpy).
> In C pseudo-code this is similar to:
> a a1;
> a a2 = set_obj();
> a1 = a2;
> use_obj(a1);
> and the corresponding LLVM IR in foo() can be simplified to:
> %a1 = alloca %a
> call void @set_obj(%a* %a1)
> call void @use_obj(%a* %a1)

no it can't.  That's because set_obj may have remembered the address passed to
it, for example by storing it in a global variable.  Then use_obj might compare
the address passed to it with the address that set_obj stashes away, and make
decisions based on whether they compare equal or not.

> Is it unreasonable to expect LLVM to do this kind of simplifications?

Try adding the nocapture attribute to the argument of set_obj.

> On a side note: Why isn't there an assignment operator in the LLVM IR? Other
> compilers I have seen have some kind of assignment operator in the IR.

That's because LLVM IR is always in SSA form.  SSA form makes assignments
pointless.  For example, suppose you could write
   %x := %y
(assignment).  Thanks to SSA form, you know that %x can only get a value
once, and thus %y is that value: %x is equal to %y throughout the function.
But then what's the point of %x?  You might as well just use %y wherever
you see %x.

Ciao, Duncan.



More information about the llvm-dev mailing list