This patch adds support for folding simple strnlen calls to SimplifyLibCalls.cpp. It extends the optimizeStringLength member function to accept the bound AKA maxlen argument to strnlen and attempt to fold calls to it analogously to strlen. To keep optimizeStringLength from growing unwieldy the patch factors out the string length logic into a new helper function, minStringLength, and adds most of the strnlen folding there.
The new minStringLength function can be called recursively to simplify some less than completely trivial calls with conditional expressions such as strnlen(x ? "123" : "12345" + i, n). In case one of the recursive calls fails, rather than creating and inserting the simplified instructions into the IR as it goes, the function returns a pointer to a function/lambda that does that. On success the caller then evaluates the lambda to create and insert the instructions (or constants) into the IR.
I have tested the patch by bootstrapping LLVM + Clang on x86_64-linux and running check-all.
If there are better solutions than the lambda approach above I'd be happy to learn about them and adjust the patch (and follow up on the question I posted about this on Discourse: https://discourse.llvm.org/t/61309).
The new minStringLength function can also be called to implement wcsnlen folding. I have done that in a separate patch that I'll submit if/when this is accepted.
It would be fine to just emit the umin unconditionally here and let it fold away if it happens to be constant, something like: