[clang] [CIR] Recognize strlen in the IdiomRecognizer pass (PR #210400)
Andy Kaylor via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 17 15:01:16 PDT 2026
================
@@ -45,9 +45,41 @@ template <> bool signatureMatches<StdFindOp>(CallOp call) {
iterTy == call->getResult(0).getType();
}
-// Raises a direct cir.call to `TargetOp` when the callee carries the
-// matching identity tag.
-template <typename TargetOp> class StdRecognizer {
+// strlen takes a pointer to an 8 bit character and returns size_t, an unsigned
+// fundamental integer. A _BitInt is not a character type even at width 8.
+template <> bool signatureMatches<StrLenOp>(CallOp call) {
+ if (call.getNumOperands() != StrLenOp::getNumArgs() ||
+ call->getNumResults() != 1)
+ return false;
+ auto ptrTy = mlir::dyn_cast<cir::PointerType>(call.getOperand(0).getType());
+ return ptrTy && cir::isChar8Type(ptrTy.getPointee()) &&
+ cir::isFundamentalUIntType(call->getResult(0).getType());
+}
+
+// Returns true when the recorded no builtin state forbids treating the call
+// as the C library function `name`. The call carries a nobuiltin mark and
+// the caller a nobuiltins list, where an empty list disables them all.
+bool isNoBuiltin(CallOp call, llvm::StringRef name) {
+ if (call->hasAttr(cir::CIRDialect::getNoBuiltinAttrName()))
----------------
andykaylor wrote:
I don't think it applies to strlen, but in the general case we also need to verify that the call doesn't have the "builtin" attribute set. The front end sets both "nobuiltin" and "builtin" when a replaceable global allocation function is called but the `-fno-builtin` option was used.
https://github.com/llvm/llvm-project/pull/210400
More information about the cfe-commits
mailing list