[llvm] [InstCombine] Simplify `(add/sub (sub/add) (sub/add))` irrelivant of use-count (PR #105866)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 23 12:53:09 PDT 2024


https://github.com/dtcxzyw requested changes to this pull request.

This patch causes clang to miscompile lief:
Reproducer:
```
static inline unsigned char mbedtls_ct_uchar_in_range_if(unsigned char low,
                                                         unsigned char high,
                                                         unsigned char c,
                                                         unsigned char t)
{
    const unsigned char co = (unsigned char) (c);
    const unsigned char to = (unsigned char) (t);

    /* low_mask is: 0 if low <= c, 0x...ff if low > c */
    unsigned low_mask = ((unsigned) co - low) >> 8;
    /* high_mask is: 0 if c <= high, 0x...ff if c > high */
    unsigned high_mask = ((unsigned) high - co) >> 8;

    return (unsigned char) (~(low_mask | high_mask)) & to;
}

unsigned char mbedtls_ct_base64_enc_char(unsigned char value)
{
    unsigned char digit = 0;
    /* For each range of values, if value is in that range, mask digit with
     * the corresponding value. Since value can only be in a single range,
     * only at most one masking will change digit. */
    digit |= mbedtls_ct_uchar_in_range_if(0, 25, value, 'A' + value);
    digit |= mbedtls_ct_uchar_in_range_if(26, 51, value, 'a' + value - 26);
    digit |= mbedtls_ct_uchar_in_range_if(52, 61, value, '0' + value - 52);
    digit |= mbedtls_ct_uchar_in_range_if(62, 62, value, '+');
    digit |= mbedtls_ct_uchar_in_range_if(63, 63, value, '/');
    return digit;
}

int main() {
    return mbedtls_ct_base64_enc_char(0);
}
```
It should map `0` to `65(ascii 'A')`, not `-1`.


https://github.com/llvm/llvm-project/pull/105866


More information about the llvm-commits mailing list