Issue 174813
Summary LLVM does not propagate sign information out of trivial loop
Labels new issue
Assignees
Reporter TiborGY
    See https://godbolt.org/z/xnrGGvdYK

Consider this code:
```
double CompareDistmats(double* distmat1, double* distmat2){
    double RMSD = 0.0;
    for (int i=0; i<1; i++){
		RMSD += (distmat1[i]-distmat2[i]) * (distmat1[i]-distmat2[i]);
    }
    return std::sqrt(RMSD);
}
```
Ideally the sqrt should become 1 HW instruction. However, due to...history, sqrt needs to set ERRNO if the argument is out of range (smaller than -0.0), therefore LLVM inserts a branch to the library function to handle that case correctly, incurring a performance and code size penalty (left panel on CE).
This is of course redundant if the argument can never be negative, and LLVM already optimizes this away if [[assume]] is used (middle panel on CE).
The compiler can sometimes deduce this non-negativity on its own, for example here if the loop is removed (right panel on CE), but a trivial loop like this is sufficient to break the analysis and result in the branch getting inserted.

LLVM should be able to propagate such non-negativity information out of simple loops like this.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to