[llvm-branch-commits] [llvm] b89a2ac - PreISelIntrinsicLowering: Check RuntimeLibcalls instead of TLI for memory functions

Tobias Hieta via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Aug 21 01:08:13 PDT 2023


Author: Matt Arsenault
Date: 2023-08-21T09:53:10+02:00
New Revision: b89a2ac64f641e8d38fcb15c626b0a9707013bba

URL: https://github.com/llvm/llvm-project/commit/b89a2ac64f641e8d38fcb15c626b0a9707013bba
DIFF: https://github.com/llvm/llvm-project/commit/b89a2ac64f641e8d38fcb15c626b0a9707013bba.diff

LOG: PreISelIntrinsicLowering: Check RuntimeLibcalls instead of TLI for memory functions

We need a better mechanism for expressing which calls you are allowed
to emit and which calls are recognized. This should be applied to the
17 branch.

(cherry picked from commit c8cac156135de8e211b800ea5abe9666803b2875)

Added: 
    llvm/test/CodeGen/ARM/no-expand-memcpy-no-builtins.ll
    llvm/test/Transforms/PreISelIntrinsicLowering/X86/lit.local.cfg
    llvm/test/Transforms/PreISelIntrinsicLowering/X86/load-relative.ll
    llvm/test/Transforms/PreISelIntrinsicLowering/X86/objc-arc.ll

Modified: 
    llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
    llvm/include/llvm/CodeGen/PreISelIntrinsicLowering.h
    llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp

Removed: 
    llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll
    llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll


################################################################################
diff  --git a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
index ab1219328a5d49..b77bcdb8902401 100644
--- a/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
+++ b/llvm/include/llvm/CodeGen/CodeGenPassBuilder.h
@@ -579,7 +579,7 @@ void CodeGenPassBuilder<Derived>::addISelPasses(AddIRPass &addPass) const {
   if (TM.useEmulatedTLS())
     addPass(LowerEmuTLSPass());
 
-  addPass(PreISelIntrinsicLoweringPass());
+  addPass(PreISelIntrinsicLoweringPass(TM));
 
   derived().addIRPasses(addPass);
   derived().addCodeGenPrepare(addPass);

diff  --git a/llvm/include/llvm/CodeGen/PreISelIntrinsicLowering.h b/llvm/include/llvm/CodeGen/PreISelIntrinsicLowering.h
index 73d7d779e55b67..aa6a0e6935b33b 100644
--- a/llvm/include/llvm/CodeGen/PreISelIntrinsicLowering.h
+++ b/llvm/include/llvm/CodeGen/PreISelIntrinsicLowering.h
@@ -18,9 +18,13 @@
 namespace llvm {
 
 class Module;
+class TargetMachine;
 
 struct PreISelIntrinsicLoweringPass
     : PassInfoMixin<PreISelIntrinsicLoweringPass> {
+  const TargetMachine &TM;
+
+  PreISelIntrinsicLoweringPass(const TargetMachine &TM) : TM(TM) {}
   PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
 };
 

diff  --git a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
index 3448c56e49948b..d62cbb02aee452 100644
--- a/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
+++ b/llvm/lib/CodeGen/PreISelIntrinsicLowering.cpp
@@ -14,9 +14,10 @@
 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
 #include "llvm/Analysis/ObjCARCInstKind.h"
 #include "llvm/Analysis/ObjCARCUtil.h"
-#include "llvm/Analysis/TargetLibraryInfo.h"
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/Passes.h"
+#include "llvm/CodeGen/TargetLowering.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/IRBuilder.h"
 #include "llvm/IR/Instructions.h"
@@ -26,6 +27,7 @@
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Target/TargetMachine.h"
 #include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
 
 using namespace llvm;
@@ -41,19 +43,19 @@ static cl::opt<int64_t> MemIntrinsicExpandSizeThresholdOpt(
 namespace {
 
 struct PreISelIntrinsicLowering {
+  const TargetMachine &TM;
   const function_ref<TargetTransformInfo &(Function &)> LookupTTI;
-  const function_ref<TargetLibraryInfo &(Function &)> LookupLibInfo;
 
   /// If this is true, assume it's preferably to leave memory intrinsic calls
   /// for replacement with a library call later. Otherwise this depends on
-  /// TargetLibraryInfo availability of the corresponding function.
+  /// TargetLoweringInfo availability of the corresponding function.
   const bool UseMemIntrinsicLibFunc;
 
   explicit PreISelIntrinsicLowering(
+      const TargetMachine &TM_,
       function_ref<TargetTransformInfo &(Function &)> LookupTTI_,
-      function_ref<TargetLibraryInfo &(Function &)> LookupLibInfo_,
       bool UseMemIntrinsicLibFunc_ = true)
-      : LookupTTI(LookupTTI_), LookupLibInfo(LookupLibInfo_),
+      : TM(TM_), LookupTTI(LookupTTI_),
         UseMemIntrinsicLibFunc(UseMemIntrinsicLibFunc_) {}
 
   static bool shouldExpandMemIntrinsicWithSize(Value *Size,
@@ -187,9 +189,15 @@ bool PreISelIntrinsicLowering::shouldExpandMemIntrinsicWithSize(
   return SizeVal > Threshold || Threshold == 0;
 }
 
+static bool canEmitLibcall(const TargetLowering &TLI, RTLIB::Libcall LC) {
+  // TODO: Should this consider the address space of the memcpy?
+  return TLI.getLibcallName(LC) != nullptr;
+}
+
 // TODO: Handle atomic memcpy and memcpy.inline
 // TODO: Pass ScalarEvolution
 bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
+  const TargetLowering *TLI = TM.getSubtargetImpl(F)->getTargetLowering();
   Intrinsic::ID ID = F.getIntrinsicID();
   bool Changed = false;
 
@@ -202,10 +210,10 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
       Function *ParentFunc = Memcpy->getFunction();
       const TargetTransformInfo &TTI = LookupTTI(*ParentFunc);
       if (shouldExpandMemIntrinsicWithSize(Memcpy->getLength(), TTI)) {
-        if (UseMemIntrinsicLibFunc &&
-            LookupLibInfo(*ParentFunc).has(LibFunc_memcpy))
+        if (UseMemIntrinsicLibFunc && canEmitLibcall(*TLI, RTLIB::MEMCPY))
           break;
 
+        // TODO: For optsize, emit the loop into a separate function
         expandMemCpyAsLoop(Memcpy, TTI);
         Changed = true;
         Memcpy->eraseFromParent();
@@ -218,8 +226,7 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
       Function *ParentFunc = Memmove->getFunction();
       const TargetTransformInfo &TTI = LookupTTI(*ParentFunc);
       if (shouldExpandMemIntrinsicWithSize(Memmove->getLength(), TTI)) {
-        if (UseMemIntrinsicLibFunc &&
-            LookupLibInfo(*ParentFunc).has(LibFunc_memmove))
+        if (UseMemIntrinsicLibFunc && canEmitLibcall(*TLI, RTLIB::MEMMOVE))
           break;
 
         if (expandMemMoveAsLoop(Memmove, TTI)) {
@@ -235,8 +242,7 @@ bool PreISelIntrinsicLowering::expandMemIntrinsicUses(Function &F) const {
       Function *ParentFunc = Memset->getFunction();
       const TargetTransformInfo &TTI = LookupTTI(*ParentFunc);
       if (shouldExpandMemIntrinsicWithSize(Memset->getLength(), TTI)) {
-        if (UseMemIntrinsicLibFunc &&
-            LookupLibInfo(*Memset->getFunction()).has(LibFunc_memset))
+        if (UseMemIntrinsicLibFunc && canEmitLibcall(*TLI, RTLIB::MEMSET))
           break;
 
         expandMemSetAsLoop(Memset);
@@ -357,8 +363,8 @@ class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
   PreISelIntrinsicLoweringLegacyPass() : ModulePass(ID) {}
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.addRequired<TargetLibraryInfoWrapperPass>();
     AU.addRequired<TargetTransformInfoWrapperPass>();
+    AU.addRequired<TargetPassConfig>();
   }
 
   bool runOnModule(Module &M) override {
@@ -366,11 +372,8 @@ class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
       return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
     };
 
-    auto LookupTLI = [this](Function &F) -> TargetLibraryInfo & {
-      return this->getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
-    };
-
-    PreISelIntrinsicLowering Lowering(LookupTTI, LookupTLI);
+    const auto &TM = getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
+    PreISelIntrinsicLowering Lowering(TM, LookupTTI);
     return Lowering.lowerIntrinsics(M);
   }
 };
@@ -379,27 +382,28 @@ class PreISelIntrinsicLoweringLegacyPass : public ModulePass {
 
 char PreISelIntrinsicLoweringLegacyPass::ID;
 
-INITIALIZE_PASS(PreISelIntrinsicLoweringLegacyPass,
-                "pre-isel-intrinsic-lowering", "Pre-ISel Intrinsic Lowering",
-                false, false)
+INITIALIZE_PASS_BEGIN(PreISelIntrinsicLoweringLegacyPass,
+                      "pre-isel-intrinsic-lowering",
+                      "Pre-ISel Intrinsic Lowering", false, false)
+INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
+INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
+INITIALIZE_PASS_END(PreISelIntrinsicLoweringLegacyPass,
+                    "pre-isel-intrinsic-lowering",
+                    "Pre-ISel Intrinsic Lowering", false, false)
 
 ModulePass *llvm::createPreISelIntrinsicLoweringPass() {
-  return new PreISelIntrinsicLoweringLegacyPass;
+  return new PreISelIntrinsicLoweringLegacyPass();
 }
 
 PreservedAnalyses PreISelIntrinsicLoweringPass::run(Module &M,
                                                     ModuleAnalysisManager &AM) {
   auto &FAM = AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
 
-  auto LookupTLI = [&FAM](Function &F) -> TargetLibraryInfo & {
-    return FAM.getResult<TargetLibraryAnalysis>(F);
-  };
-
   auto LookupTTI = [&FAM](Function &F) -> TargetTransformInfo & {
     return FAM.getResult<TargetIRAnalysis>(F);
   };
 
-  PreISelIntrinsicLowering Lowering(LookupTTI, LookupTLI);
+  PreISelIntrinsicLowering Lowering(TM, LookupTTI);
   if (!Lowering.lowerIntrinsics(M))
     return PreservedAnalyses::all();
   else

diff  --git a/llvm/test/CodeGen/ARM/no-expand-memcpy-no-builtins.ll b/llvm/test/CodeGen/ARM/no-expand-memcpy-no-builtins.ll
new file mode 100644
index 00000000000000..0b2c96d93ba430
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/no-expand-memcpy-no-builtins.ll
@@ -0,0 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 2
+; RUN: llc -mtriple=thumbv7em-apple-unknown-macho < %s | FileCheck %s
+
+target datalayout = "e-m:o-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
+
+declare void @llvm.memcpy.p0.p0.i32(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i32, i1 immarg) #0
+declare void @llvm.memmove.p0.p0.i32(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i32, i1 immarg) #0
+declare void @llvm.memset.p0.i32(ptr noalias nocapture writeonly, i8, i32, i1 immarg) #0
+
+; Check we don't expand memcpy to a loop when the caller
+; even if we have no-builtins attached.
+
+; CHECK: bl _memcpy
+define arm_aapcs_vfpcc void @test_memcpy(ptr %p1, ptr %p2) #1 {
+  call void @llvm.memcpy.p0.p0.i32(ptr %p1, ptr %p2, i32 128, i1 false)
+  ret void
+}
+
+; CHECK: bl _memmove
+define arm_aapcs_vfpcc void @test_memmove(ptr %p1, ptr %p2) #1 {
+  call void @llvm.memmove.p0.p0.i32(ptr %p1, ptr %p2, i32 128, i1 false)
+  ret void
+}
+
+; CHECK: bl _memset
+define arm_aapcs_vfpcc void @test_memset(ptr %p1) #1 {
+  call void @llvm.memset.p0.i32(ptr %p1, i8 0, i32 128, i1 false)
+  ret void
+}
+
+attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
+attributes #1 = { "no-builtins" }

diff  --git a/llvm/test/Transforms/PreISelIntrinsicLowering/X86/lit.local.cfg b/llvm/test/Transforms/PreISelIntrinsicLowering/X86/lit.local.cfg
new file mode 100644
index 00000000000000..42bf50dcc13c35
--- /dev/null
+++ b/llvm/test/Transforms/PreISelIntrinsicLowering/X86/lit.local.cfg
@@ -0,0 +1,2 @@
+if not "X86" in config.root.targets:
+    config.unsupported = True

diff  --git a/llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll b/llvm/test/Transforms/PreISelIntrinsicLowering/X86/load-relative.ll
similarity index 89%
rename from llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll
rename to llvm/test/Transforms/PreISelIntrinsicLowering/X86/load-relative.ll
index b9e03111d021ac..805d61607a7ed3 100644
--- a/llvm/test/Transforms/PreISelIntrinsicLowering/load-relative.ll
+++ b/llvm/test/Transforms/PreISelIntrinsicLowering/X86/load-relative.ll
@@ -1,4 +1,4 @@
-; RUN: opt -pre-isel-intrinsic-lowering -S -o - %s | FileCheck %s
+; RUN: opt -mtriple=x86_64-pc-linux-gnu -pre-isel-intrinsic-lowering -S -o - %s | FileCheck %s
 
 ; CHECK: define ptr @foo32(ptr [[P:%.*]], i32 [[O:%.*]])
 define ptr @foo32(ptr %p, i32 %o) {

diff  --git a/llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll b/llvm/test/Transforms/PreISelIntrinsicLowering/X86/objc-arc.ll
similarity index 99%
rename from llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll
rename to llvm/test/Transforms/PreISelIntrinsicLowering/X86/objc-arc.ll
index 41420dcec85d0b..b36737f12e4771 100644
--- a/llvm/test/Transforms/PreISelIntrinsicLowering/objc-arc.ll
+++ b/llvm/test/Transforms/PreISelIntrinsicLowering/X86/objc-arc.ll
@@ -1,4 +1,4 @@
-; RUN: opt -pre-isel-intrinsic-lowering -S -o - %s | FileCheck %s
+; RUN: opt -mtriple=x86_64-pc-linux-gnu -pre-isel-intrinsic-lowering -S -o - %s | FileCheck %s
 
 ; Make sure calls to the objc intrinsics are translated to calls in to the
 ; runtime


        


More information about the llvm-branch-commits mailing list