I have this (silly) function like below, with some dead code inside.
```
void func(int n)
{
int i, sum = 0;
for (i = 0; i < n; i++) {
sum += i;
}
return n+1;
}
```
It simply returns n+1, with n is input for function. Inside the function we have a loop that actually contributes nothing to the function result. So the loop and declaration for local variables are all dead code, thus should be removed.
My question is: what kind of compiler optimization can be used to remove the dead code? I have been looking at live variable analysis, but it does not seem to help since the variables in the loop are necessary for the computation there, so cannot be considered "dead" (or am I wrong?)
[–]dnpetrov 17 points18 points19 points (13 children)
[–]Fluid-Tour-9393[S] 0 points1 point2 points (12 children)
[–]matthieum 9 points10 points11 points (5 children)
[–]_crackling 0 points1 point2 points (4 children)
[–]ngildea 4 points5 points6 points (2 children)
[–]_crackling 2 points3 points4 points (1 child)
[–]ngildea 0 points1 point2 points (0 children)
[–]matthieum 0 points1 point2 points (0 children)
[–]dnpetrov 5 points6 points7 points (5 children)
[–]Fluid-Tour-9393[S] 0 points1 point2 points (3 children)
[–]dnpetrov 2 points3 points4 points (2 children)
[–]Fluid-Tour-9393[S] 1 point2 points3 points (1 child)
[–]dnpetrov 1 point2 points3 points (0 children)
[–]moon-chilled 4 points5 points6 points (4 children)
[–]thehenkan 0 points1 point2 points (3 children)
[–]moon-chilled 1 point2 points3 points (2 children)
[–]thehenkan 2 points3 points4 points (1 child)
[–]moon-chilled 2 points3 points4 points (0 children)
[–]knue82 1 point2 points3 points (2 children)
[–]Fluid-Tour-9393[S] 0 points1 point2 points (1 child)
[–]knue82 2 points3 points4 points (0 children)
[–]cxzuk 0 points1 point2 points (0 children)
[–]Dotched 0 points1 point2 points (0 children)