3 ms·
Personally, I'd expect a compiler to take it one step further than what was suggested here. For example, clang or gcc (under -O1) will precompute all of the ma
by sharth 13y ago
Personally, I'd expect a compiler to take it one step further than what was suggested here.
For example, clang or gcc (under -O1) will precompute all of the math in that for-loop, and the entire function just becomes the printf with a known value.
define i32 @main() nounwind uwtable ssp {
%1 = tail call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([6 x i8]* @.str, i64 0, i64 0), i64 500000000500000000) nounwind
ret i32 0
}
- CurtHagenlocher 13y agoIf you read all the way into the comments, you'll see that this does happen -- but only for values of n that are quite small. And this is a side effect of other optimizations -- not because of anything specific to this closed form.
- sharth 13y agoYeah, I was just now skimming through the comments as well and I saw that. It looks like their constant propagation logic doesn't work on loops at all, and it's just that their loop unroller stage (which runs before it) will unroll loops of less than 10 iterations.
- DannyBee 13y agoYes, which is kinda sad. both GCC and LLVM do it as a result of computing scalar evolutions for the variables, and then instantiating the final values after the loop. There are certain calculations both will bail on due to solving cost (rather than inability to solve), but otherwise ...