3 ms·
In what world the return value doesn't depends on 'a' in this code? if (num < a) return num * num; /*else*/ return num; A control dependency is still a
by gpderetta 2y ago
In what world the return value doesn't depends on 'a' in this code?
if (num < a) return num * num;
/*else*/ return num;
A control dependency is still a dependency
- jmillikin 2y ago`a = num; a += 1; if (num < a)` is the same as `if (num < (num + 1))`, which for unsigned integer addition can be rewritten as `if (num != UINT_MAX)`. So there's no need to actually compute `a+1`, the comparison is against a constant. If the code returns `num * a` then the value of `a` is now necessary, and must be computed before the function returns. For signed integer addition the compiler is allowed to assume that `(num < (num + 1))` is true, so the comparison can be removed entirely.
- eru 2y ago> For signed integer addition the compiler is allowed to assume that `(num < (num + 1))` is true, so the comparison can be removed entirely. That's not directly what the compiler assumes. The direct problem is in 'a + 1' having undefined behaviour, and that transitively allows the assumption on the comparison that you mentioned. This was an example where 'a + 1' doesn't compile to an add instruction.