[llvm-branch-commits] [llvm] [DA] Introduce OverflowSafeSignedAPInt to prevent potential overflow (PR #171991)
Ryotaro Kasuga via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Dec 16 03:25:02 PST 2025
kasuga-fj wrote:
> APInt has several routines that deal determine overflow, e.g. sadd_ov, ssub_ov, etc. So, as a very minimum, if we need a convenience wrapper, we should first use those routines?
This is exactly a wrapper for those routines. I defined the wrapper for readability. For example, we have the following piece of code which may overflow:
```cpp
LowerDistance = (TY - TX) + (TA - TB) * TL;
```
If we use `sadd_ov`, `ssub_ov` and `smul_ov` directly, the code will become:
```cpp
bool Overflow;
APInt Tmp0 = TY.ssub_ov(TX, Overflow);
if (Overflow) ...;
APInt Tmp1 = TA.ssub_ov(TB, Overflow);
if (Overflow) ...;
APInt Tmp2 = Tmp1.smul_ov(TL, Overflow);
if (Overflow) ...;
LowerDistance = Tmp0.sadd_ov(Tmp2, Overflow);
if (Overflow) ...;
```
I think it's a bit ugly and hard to read/maintain. Using a wrapper allows us to keep the code clean and readable.
https://github.com/llvm/llvm-project/pull/171991
More information about the llvm-branch-commits
mailing list