Question

I've been looking into IR code which is specified using SSA- especially, generating LLVM IR in this form. However, I'm confused about whether or not this can be effective when presented with a type which has non-trivial copy semantics. For example,

void f() {
    std::string s = "Too long for you, short string optimization!";
    std::string s1 = s + " Also, goodbye SSA.";
    some_other_function(s1);
}

In this SSA form, at least at the most obvious level, this results in a nasty mess of copies (even for C++). Can optimizers such as LLVM's actually optimize this case accurately? Is SSA viable for use even for types with non-trivial copy/assignment/etc semantics?

Edit: The question is that if I use an LLVM SSA register to represent a complex type (in this case, std:string), here represented by manually making it SSA, can LLVM automatically translate this into a mutating += call in the underlying assembly in the general case and avoid a nasty copy?

Was it helpful?

Solution

SSA means single static assignment. It's a way of dealing with value semantics as applied to registers. Each object is the result of exactly one machine instruction.

LLVM provides a generic "move" instruction, which is useful because there are many instructions across the spectrum of architectures that move 8, 32, N bytes. It also provides structured datatypes and arrays, because it is useful to hoist such things to registers, and they can be used to represent wacky high-level machine constructs. The intent is not to model OOP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top