[llvm] [TLI] replace-with-veclib works with FRem Instruction. (PR #76166)

Alexandros Lamprineas via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 3 05:53:27 PST 2024


================
@@ -162,27 +170,35 @@ static bool replaceWithCallToVeclib(const TargetLibraryInfo &TLI,
   if (!VectorFTy)
     return false;
 
-  Function *FuncToReplace = CallToReplace.getCalledFunction();
-  Function *TLIFunc = getTLIFunction(CallToReplace.getModule(), VectorFTy,
+  Function *FuncToReplace = CI ? CI->getCalledFunction() : nullptr;
+  Function *TLIFunc = getTLIFunction(I.getModule(), VectorFTy,
                                      VD->getVectorFnName(), FuncToReplace);
-  replaceWithTLIFunction(CallToReplace, *OptInfo, TLIFunc);
-
-  LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Replaced call to `"
-                    << FuncToReplace->getName() << "` with call to `"
-                    << TLIFunc->getName() << "`.\n");
+  replaceWithTLIFunction(I, *OptInfo, TLIFunc);
+  LLVM_DEBUG(dbgs() << DEBUG_TYPE << ": Replaced call to `" << ScalarName
+                    << "` with call to `" << TLIFunc->getName() << "`.\n");
   ++NumCallsReplaced;
   return true;
 }
 
+/// Supported instructions \p I are either frem or CallInsts to intrinsics.
+static bool isSupportedInstruction(Instruction *I) {
+  if (auto *CI = dyn_cast<CallInst>(I)) {
+    if (CI->getCalledFunction() &&
+        CI->getCalledFunction()->getIntrinsicID() != Intrinsic::not_intrinsic)
+      return true;
+  } else if (I->getOpcode() == Instruction::FRem && I->getType()->isVectorTy())
+    return true;
+
+  return false;
+}
+
 static bool runImpl(const TargetLibraryInfo &TLI, Function &F) {
   bool Changed = false;
-  SmallVector<CallInst *> ReplacedCalls;
+  SmallVector<Instruction *> ReplacedCalls;
   for (auto &I : instructions(F)) {
-    if (auto *CI = dyn_cast<CallInst>(&I)) {
-      if (replaceWithCallToVeclib(TLI, *CI)) {
-        ReplacedCalls.push_back(CI);
-        Changed = true;
-      }
+    if (isSupportedInstruction(&I) && replaceWithCallToVeclib(TLI, I)) {
----------------
labrinea wrote:

Alternative:
```
if (!isSupportedInstruction(&I))
  continue;
if (replaceWithCallToVeclib(TLI, *CI)) {
  ...
}
```

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


More information about the llvm-commits mailing list