Hacker News new | past | comments | ask | show | jobs | submit login

> For instance, if two branches of a conditional expression both refer to the same value (represented by name $foo) then the code generator will only emit code to compute its value when it encounters the first reference. When the code generator encounters $foo for the second time in the other branch, no code will be emitted.

Isn't that what phi nodes were designed to fix?

I don't think so - phi nodes solve the "problem" of expressing a value in SSA one-definition form, when that value is control-flow dependent. But this is really an artifact of SSA form.

This seems like a much weirder problem, in which the optimizer is unaware of control flow and so will allow a value to reference an instruction that does not dominate it.

LLVM's IR verifier ensures that instructions dominate all of their uses, which simply means that all users of an instruction must provably execute after that instruction. This verification step seems to be what's missing.

Hi! Author here. I think you hit the nail on the head here. The optimizer is unaware of the control flow, because the control flow is added by the code generator.(And with it, the distinction between computing a value and referencing it). It's a control-flow-less IR, sort of.

I was thinking that this just needs the common value "promoted" outside of the conditional block but then it seemed that the different branches were probably modifying it so thought of phi nodes.

Anyways, seems to be some weird block scoping rules going on like with python.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact