1 //===-- PPCLowerMASSVEntries.cpp ------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements lowering of MASSV (SIMD) entries for specific PowerPC
10 // subtargets.
11 // Following is an example of a conversion specific to Power9 subtarget:
12 // __sind2_massv ---> __sind2_P9
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "PPC.h"
17 #include "PPCSubtarget.h"
18 #include "PPCTargetMachine.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Analysis/TargetTransformInfo.h"
21 #include "llvm/CodeGen/TargetPassConfig.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 
25 #define DEBUG_TYPE "ppc-lower-massv-entries"
26 
27 using namespace llvm;
28 
29 namespace {
30 
31 // Length of the suffix "massv", which is specific to IBM MASSV library entries.
32 const unsigned MASSVSuffixLength = 5;
33 
34 static StringRef MASSVFuncs[] = {
35 #define TLI_DEFINE_MASSV_VECFUNCS_NAMES
36 #include "llvm/Analysis/VecFuncs.def"
37 };
38 
39 class PPCLowerMASSVEntries : public ModulePass {
40 public:
41   static char ID;
42 
PPCLowerMASSVEntries()43   PPCLowerMASSVEntries() : ModulePass(ID) {}
44 
45   bool runOnModule(Module &M) override;
46 
getPassName() const47   StringRef getPassName() const override { return "PPC Lower MASS Entries"; }
48 
getAnalysisUsage(AnalysisUsage & AU) const49   void getAnalysisUsage(AnalysisUsage &AU) const override {
50     AU.addRequired<TargetTransformInfoWrapperPass>();
51   }
52 
53 private:
54   static bool isMASSVFunc(StringRef Name);
55   static StringRef getCPUSuffix(const PPCSubtarget *Subtarget);
56   static std::string createMASSVFuncName(Function &Func,
57                                          const PPCSubtarget *Subtarget);
58   bool handlePowSpecialCases(CallInst *CI, Function &Func, Module &M);
59   bool lowerMASSVCall(CallInst *CI, Function &Func, Module &M,
60                       const PPCSubtarget *Subtarget);
61 };
62 
63 } // namespace
64 
65 /// Checks if the specified function name represents an entry in the MASSV
66 /// library.
isMASSVFunc(StringRef Name)67 bool PPCLowerMASSVEntries::isMASSVFunc(StringRef Name) {
68   return llvm::is_contained(MASSVFuncs, Name);
69 }
70 
71 // FIXME:
72 /// Returns a string corresponding to the specified PowerPC subtarget. e.g.:
73 /// "P8" for Power8, "P9" for Power9. The string is used as a suffix while
74 /// generating subtarget-specific MASSV library functions. Current support
75 /// includes  Power8 and Power9 subtargets.
getCPUSuffix(const PPCSubtarget * Subtarget)76 StringRef PPCLowerMASSVEntries::getCPUSuffix(const PPCSubtarget *Subtarget) {
77   // Assume Power8 when Subtarget is unavailable.
78   if (!Subtarget)
79     return "P8";
80   if (Subtarget->hasP9Vector())
81     return "P9";
82   if (Subtarget->hasP8Vector())
83     return "P8";
84 
85   report_fatal_error("Unsupported Subtarget: MASSV is supported only on "
86                      "Power8 and Power9 subtargets.");
87 }
88 
89 /// Creates PowerPC subtarget-specific name corresponding to the specified
90 /// generic MASSV function, and the PowerPC subtarget.
91 std::string
createMASSVFuncName(Function & Func,const PPCSubtarget * Subtarget)92 PPCLowerMASSVEntries::createMASSVFuncName(Function &Func,
93                                           const PPCSubtarget *Subtarget) {
94   StringRef Suffix = getCPUSuffix(Subtarget);
95   auto GenericName = Func.getName().drop_back(MASSVSuffixLength).str();
96   std::string MASSVEntryName = GenericName + Suffix.str();
97   return MASSVEntryName;
98 }
99 
100 /// If there are proper fast-math flags, this function creates llvm.pow
101 /// intrinsics when the exponent is 0.25 or 0.75.
handlePowSpecialCases(CallInst * CI,Function & Func,Module & M)102 bool PPCLowerMASSVEntries::handlePowSpecialCases(CallInst *CI, Function &Func,
103                                                  Module &M) {
104   if (Func.getName() != "__powf4_massv" && Func.getName() != "__powd2_massv")
105     return false;
106 
107   if (Constant *Exp = dyn_cast<Constant>(CI->getArgOperand(1)))
108     if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(Exp->getSplatValue())) {
109       // If the argument is 0.75 or 0.25 it is cheaper to turn it into pow
110       // intrinsic so that it could be optimzed as sequence of sqrt's.
111       if (!CI->hasNoInfs() || !CI->hasApproxFunc())
112         return false;
113 
114       if (!CFP->isExactlyValue(0.75) && !CFP->isExactlyValue(0.25))
115         return false;
116 
117       if (CFP->isExactlyValue(0.25) && !CI->hasNoSignedZeros())
118         return false;
119 
120       CI->setCalledFunction(
121           Intrinsic::getDeclaration(&M, Intrinsic::pow, CI->getType()));
122       return true;
123     }
124 
125   return false;
126 }
127 
128 /// Lowers generic MASSV entries to PowerPC subtarget-specific MASSV entries.
129 /// e.g.: __sind2_massv --> __sind2_P9 for a Power9 subtarget.
130 /// Both function prototypes and their callsites are updated during lowering.
lowerMASSVCall(CallInst * CI,Function & Func,Module & M,const PPCSubtarget * Subtarget)131 bool PPCLowerMASSVEntries::lowerMASSVCall(CallInst *CI, Function &Func,
132                                           Module &M,
133                                           const PPCSubtarget *Subtarget) {
134   if (CI->use_empty())
135     return false;
136 
137   // Handling pow(x, 0.25), pow(x, 0.75), powf(x, 0.25), powf(x, 0.75)
138   if (handlePowSpecialCases(CI, Func, M))
139     return true;
140 
141   std::string MASSVEntryName = createMASSVFuncName(Func, Subtarget);
142   FunctionCallee FCache = M.getOrInsertFunction(
143       MASSVEntryName, Func.getFunctionType(), Func.getAttributes());
144 
145   CI->setCalledFunction(FCache);
146 
147   return true;
148 }
149 
runOnModule(Module & M)150 bool PPCLowerMASSVEntries::runOnModule(Module &M) {
151   bool Changed = false;
152 
153   auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
154   if (!TPC)
155     return Changed;
156 
157   auto &TM = TPC->getTM<PPCTargetMachine>();
158   const PPCSubtarget *Subtarget;
159 
160   for (Function &Func : M) {
161     if (!Func.isDeclaration())
162       continue;
163 
164     if (!isMASSVFunc(Func.getName()))
165       continue;
166 
167     // Call to lowerMASSVCall() invalidates the iterator over users upon
168     // replacing the users. Precomputing the current list of users allows us to
169     // replace all the call sites.
170     SmallVector<User *, 4> MASSVUsers;
171     for (auto *User: Func.users())
172       MASSVUsers.push_back(User);
173 
174     for (auto *User : MASSVUsers) {
175       auto *CI = dyn_cast<CallInst>(User);
176       if (!CI)
177         continue;
178 
179       Subtarget = &TM.getSubtarget<PPCSubtarget>(*CI->getParent()->getParent());
180       Changed |= lowerMASSVCall(CI, Func, M, Subtarget);
181     }
182   }
183 
184   return Changed;
185 }
186 
187 char PPCLowerMASSVEntries::ID = 0;
188 
189 char &llvm::PPCLowerMASSVEntriesID = PPCLowerMASSVEntries::ID;
190 
191 INITIALIZE_PASS(PPCLowerMASSVEntries, DEBUG_TYPE, "Lower MASSV entries", false,
192                 false)
193 
createPPCLowerMASSVEntriesPass()194 ModulePass *llvm::createPPCLowerMASSVEntriesPass() {
195   return new PPCLowerMASSVEntries();
196 }
197