Skip to content

Commit c4ff0a6

Browse files
[TLI] Add getLibFunc that accepts an Opcode and scalar Type. (#75919)
It sets a LibFunc similarly with the other two getLibFunc methods. Currently, it supports only the FRem Instruction. Add tests for FRem.
1 parent f568763 commit c4ff0a6

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

llvm/include/llvm/Analysis/TargetLibraryInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ class TargetLibraryInfoImpl {
156156
/// FDecl is assumed to have a parent Module when using this function.
157157
bool getLibFunc(const Function &FDecl, LibFunc &F) const;
158158

159+
/// Searches for a function name using an Instruction \p Opcode.
160+
/// Currently, only the frem instruction is supported.
161+
bool getLibFunc(unsigned int Opcode, Type *Ty, LibFunc &F) const;
162+
159163
/// Forces a function to be marked as unavailable.
160164
void setUnavailable(LibFunc F) {
161165
setState(F, Unavailable);
@@ -360,6 +364,12 @@ class TargetLibraryInfo {
360364
getLibFunc(*(CB.getCalledFunction()), F);
361365
}
362366

367+
/// Searches for a function name using an Instruction \p Opcode.
368+
/// Currently, only the frem instruction is supported.
369+
bool getLibFunc(unsigned int Opcode, Type *Ty, LibFunc &F) const {
370+
return Impl->getLibFunc(Opcode, Ty, F);
371+
}
372+
363373
/// Disables all builtins.
364374
///
365375
/// This can be used for options like -fno-builtin.

llvm/lib/Analysis/TargetLibraryInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,6 +1149,16 @@ bool TargetLibraryInfoImpl::getLibFunc(const Function &FDecl,
11491149
return isValidProtoForLibFunc(*FDecl.getFunctionType(), F, *M);
11501150
}
11511151

1152+
bool TargetLibraryInfoImpl::getLibFunc(unsigned int Opcode, Type *Ty,
1153+
LibFunc &F) const {
1154+
// Must be a frem instruction with float or double arguments.
1155+
if (Opcode != Instruction::FRem || (!Ty->isDoubleTy() && !Ty->isFloatTy()))
1156+
return false;
1157+
1158+
F = Ty->isDoubleTy() ? LibFunc_fmod : LibFunc_fmodf;
1159+
return true;
1160+
}
1161+
11521162
void TargetLibraryInfoImpl::disableAllFunctions() {
11531163
memset(AvailableArray, 0, sizeof(AvailableArray));
11541164
}

llvm/unittests/Analysis/TargetLibraryInfoTest.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,3 +621,38 @@ TEST_F(TargetLibraryInfoTest, ValidProto) {
621621
EXPECT_TRUE(isLibFunc(F, LF));
622622
}
623623
}
624+
625+
namespace {
626+
627+
/// Creates TLI for AArch64 and uses it to get the LibFunc names for the given
628+
/// Instruction opcode and Type.
629+
class TLITestAarch64 : public ::testing::Test {
630+
private:
631+
const Triple TargetTriple;
632+
633+
protected:
634+
LLVMContext Ctx;
635+
std::unique_ptr<TargetLibraryInfoImpl> TLII;
636+
std::unique_ptr<TargetLibraryInfo> TLI;
637+
638+
/// Create TLI for AArch64
639+
TLITestAarch64() : TargetTriple(Triple("aarch64-unknown-linux-gnu")) {
640+
TLII = std::make_unique<TargetLibraryInfoImpl>(
641+
TargetLibraryInfoImpl(TargetTriple));
642+
TLI = std::make_unique<TargetLibraryInfo>(TargetLibraryInfo(*TLII));
643+
}
644+
645+
/// Returns the TLI function name for the given \p Opcode and type \p Ty.
646+
StringRef getScalarName(unsigned int Opcode, Type *Ty) {
647+
LibFunc Func;
648+
if (!TLI->getLibFunc(Opcode, Ty, Func))
649+
return "";
650+
return TLI->getName(Func);
651+
}
652+
};
653+
} // end anonymous namespace
654+
655+
TEST_F(TLITestAarch64, TestFrem) {
656+
EXPECT_EQ(getScalarName(Instruction::FRem, Type::getDoubleTy(Ctx)), "fmod");
657+
EXPECT_EQ(getScalarName(Instruction::FRem, Type::getFloatTy(Ctx)), "fmodf");
658+
}

0 commit comments

Comments
 (0)