In the following program, clang is able to fully remove the second if statement. Instead it creates a select using a[0] to select between constant 1 and constant 2. The final load of a[1] is completely removed.
int func(int* a)
{
if (a[0])
return 1;
if (a)
return 2;
return a[1];
}
When we load from a[0], we can assume that a is not a nullptr, and LLVM is able to use this assumption to determine that the "else"-branch of the second if is unreachable. We could also do this. For reference, the relevant RVSDG regions of f are shown here:
In the same vein, clang is able to convert the below code into a single ret 2:
int func(int* a)
{
if (a)
return 2;
return a[0];
}
In the following program, clang is able to fully remove the second
ifstatement. Instead it creates a select usinga[0]to select between constant 1 and constant 2. The final load ofa[1]is completely removed.When we load from
a[0], we can assume thatais not a nullptr, and LLVM is able to use this assumption to determine that the "else"-branch of the secondifis unreachable. We could also do this. For reference, the relevant RVSDG regions offare shown here:In the same vein, clang is able to convert the below code into a single
ret 2: