1 //===-- PPCLowerMASSVEntries.cpp ------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements lowering of MASSV (SIMD) entries for specific PowerPC
11 // Following is an example of a conversion specific to Power9 subtarget:
12 // __sind2_massv ---> __sind2_P9
14 //===----------------------------------------------------------------------===//
17 #include "PPCSubtarget.h"
18 #include "PPCTargetMachine.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
20 #include "llvm/CodeGen/TargetPassConfig.h"
21 #include "llvm/IR/Instructions.h"
22 #include "llvm/IR/Module.h"
24 #define DEBUG_TYPE "ppc-lower-massv-entries"
30 // Length of the suffix "massv", which is specific to IBM MASSV library entries.
31 const unsigned MASSVSuffixLength = 5;
33 static StringRef MASSVFuncs[] = {
34 #define TLI_DEFINE_MASSV_VECFUNCS_NAMES
35 #include "llvm/Analysis/VecFuncs.def"
38 class PPCLowerMASSVEntries : public ModulePass {
42 PPCLowerMASSVEntries() : ModulePass(ID) {}
44 bool runOnModule(Module &M) override;
46 StringRef getPassName() const override { return "PPC Lower MASS Entries"; }
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
49 AU.addRequired<TargetTransformInfoWrapperPass>();
53 static bool isMASSVFunc(StringRef Name);
54 static StringRef getCPUSuffix(const PPCSubtarget *Subtarget);
55 static std::string createMASSVFuncName(Function &Func,
56 const PPCSubtarget *Subtarget);
57 bool lowerMASSVCall(CallInst *CI, Function &Func, Module &M,
58 const PPCSubtarget *Subtarget);
63 /// Checks if the specified function name represents an entry in the MASSV
65 bool PPCLowerMASSVEntries::isMASSVFunc(StringRef Name) {
66 auto Iter = std::find(std::begin(MASSVFuncs), std::end(MASSVFuncs), Name);
67 return Iter != std::end(MASSVFuncs);
71 /// Returns a string corresponding to the specified PowerPC subtarget. e.g.:
72 /// "P8" for Power8, "P9" for Power9. The string is used as a suffix while
73 /// generating subtarget-specific MASSV library functions. Current support
74 /// includes Power8 and Power9 subtargets.
75 StringRef PPCLowerMASSVEntries::getCPUSuffix(const PPCSubtarget *Subtarget) {
76 // Assume Power8 when Subtarget is unavailable.
79 if (Subtarget->hasP9Vector())
81 if (Subtarget->hasP8Vector())
84 report_fatal_error("Unsupported Subtarget: MASSV is supported only on "
85 "Power8 and Power9 subtargets.");
88 /// Creates PowerPC subtarget-specific name corresponding to the specified
89 /// generic MASSV function, and the PowerPC subtarget.
91 PPCLowerMASSVEntries::createMASSVFuncName(Function &Func,
92 const PPCSubtarget *Subtarget) {
93 StringRef Suffix = getCPUSuffix(Subtarget);
94 auto GenericName = Func.getName().drop_back(MASSVSuffixLength).str();
95 std::string MASSVEntryName = GenericName + Suffix.str();
96 return MASSVEntryName;
99 /// Lowers generic MASSV entries to PowerPC subtarget-specific MASSV entries.
100 /// e.g.: __sind2_massv --> __sind2_P9 for a Power9 subtarget.
101 /// Both function prototypes and their callsites are updated during lowering.
102 bool PPCLowerMASSVEntries::lowerMASSVCall(CallInst *CI, Function &Func,
104 const PPCSubtarget *Subtarget) {
108 std::string MASSVEntryName = createMASSVFuncName(Func, Subtarget);
109 FunctionCallee FCache = M.getOrInsertFunction(
110 MASSVEntryName, Func.getFunctionType(), Func.getAttributes());
113 CI->setCalledFunction(FCache);
118 bool PPCLowerMASSVEntries::runOnModule(Module &M) {
119 bool Changed = false;
121 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
125 auto &TM = TPC->getTM<PPCTargetMachine>();
126 const PPCSubtarget *Subtarget;
128 for (Function &Func : M) {
129 if (!Func.isDeclaration())
132 if (!isMASSVFunc(Func.getName()))
135 // Call to lowerMASSVCall() invalidates the iterator over users upon
136 // replacing the users. Precomputing the current list of users allows us to
137 // replace all the call sites.
138 SmallVector<User *, 4> MASSVUsers;
139 for (auto *User: Func.users())
140 MASSVUsers.push_back(User);
142 for (auto *User : MASSVUsers) {
143 auto *CI = dyn_cast<CallInst>(User);
147 Subtarget = &TM.getSubtarget<PPCSubtarget>(*CI->getParent()->getParent());
148 Changed |= lowerMASSVCall(CI, Func, M, Subtarget);
155 char PPCLowerMASSVEntries::ID = 0;
157 char &llvm::PPCLowerMASSVEntriesID = PPCLowerMASSVEntries::ID;
159 INITIALIZE_PASS(PPCLowerMASSVEntries, DEBUG_TYPE, "Lower MASSV entries", false,
162 ModulePass *llvm::createPPCLowerMASSVEntriesPass() {
163 return new PPCLowerMASSVEntries();