0
$\begingroup$

I was reading the LICM section of the book Advanced Compiler Design and Implementation and found me questioning why the expression d = -c wasn't tagged as invariant.

It isn't hard to see that c = 2 is invariant given its operand is a constant. Then, since d = -c is taking an invariant operand, then d also should be invariant.

What point am I missing here?

LICM

$\endgroup$
1
  • 2
    $\begingroup$ run the loop for a lesser iterations $(i>3)$ and you'll see. $\endgroup$
    – Rinkesh P
    Commented May 1, 2023 at 4:00

1 Answer 1

1
$\begingroup$

The comment by Rinkesh is correct. The expression $-c$ is loop-invariant (and can be constant-propagated to the value $-2$), but the assignment $d \leftarrow -c$ is not because the assignment in block B4 might execute instead.

If you're still not convinced, try converting it to static single assignment (SSA) form.

$\endgroup$
1
  • $\begingroup$ Oh, I get it, I thought the right-side expression was what mattered, but I can't move the assignment to d upside when d might get another assignment. $\endgroup$ Commented May 1, 2023 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.