Missing pattern for abs?

Not sure clang is the right spot, though it seems like it is, I have a pattern for absolute value that isn’t being translated to llvm.abs intrinsic for llvm:

int s = ((a>>15)&0x10001)*0xffff;
return (a+s)^s;

Is this something commonly being done in clang? If clang is the right spot for this AST match, can you point me to the right place?


I think this kind of thing is done in an LLVM pass generally. Though I’m not sure it’s worth adding. You can just use std::abs() or __builtin_abs, which is way more expressive and basically impossible for the compiler to get wrong.


I can’t change the source but thanks for the answer. I suppose the AST doesn’t generate llvm intrinsics from expressions?

I’m not a clang expert, but AFAIK generally not. There are a few cases, like zero-initialized arrays that get transformed into intrinsics, but most of the time it’s left to the middle/back-end to generate good code. That part of the compiler is much better at generating good code than the front-end (by design).

1 Like