[clang] [clang][codegen] Don't mark "int" TBAA on FP libcalls with indirect args (PR #108853)

Matt Arsenault via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 18 02:27:13 PDT 2024


================
@@ -690,23 +690,46 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
                               const CallExpr *E, llvm::Constant *calleeValue) {
   CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
   CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
+  llvm::CallBase *callOrInvoke = nullptr;
+  CGFunctionInfo const *FnInfo = nullptr;
   RValue Call =
-      CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
+      CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot(),
+                   /*Chain=*/nullptr, &callOrInvoke, &FnInfo);
 
   if (unsigned BuiltinID = FD->getBuiltinID()) {
     // Check whether a FP math builtin function, such as BI__builtin_expf
     ASTContext &Context = CGF.getContext();
     bool ConstWithoutErrnoAndExceptions =
         Context.BuiltinInfo.isConstWithoutErrnoAndExceptions(BuiltinID);
+
+    auto isDirectOrIgnore = [&](ABIArgInfo const &info) {
+      // For a non-aggregate types direct/extend means the type will be used
+      // directly (or a sign/zero extension of it) on the call (not a
+      // input/output pointer).
+      return info.isDirect() || info.isExtend() || info.isIgnore();
+    };
+
+    // Before annotating this libcall with "int" TBAA metadata check all
+    // arguments/results are passed directly. On some targets, types such as
+    // "long double" are passed indirectly via a pointer, and annotating the
+    // call with "int" TBAA metadata will lead to set up for those arguments
+    // being incorrectly optimized out.
+    bool ReturnAndAllArgumentsDirect =
+        isDirectOrIgnore(FnInfo->getReturnInfo()) &&
+        llvm::all_of(FnInfo->arguments(),
+                     [&](CGFunctionInfoArgInfo const &ArgInfo) {
+                       return isDirectOrIgnore(ArgInfo.info);
+                     });
----------------
arsenm wrote:

Make a predicate function and call at the end of the if expression below 

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


More information about the cfe-commits mailing list