LLVM 20.0.0git
PPCLowerMASSVEntries.cpp
Go to the documentation of this file.
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"
23#include "llvm/IR/Module.h"
24
25#define DEBUG_TYPE "ppc-lower-massv-entries"
26
27using namespace llvm;
28
29namespace {
30
31static StringRef MASSVFuncs[] = {
32#define TLI_DEFINE_MASSV_VECFUNCS
33#define TLI_DEFINE_VECFUNC(SCAL, VEC, VF, VABI_PREFIX) VEC,
34#include "llvm/Analysis/VecFuncs.def"
35#undef TLI_DEFINE_MASSV_VECFUNCS
36};
37
38class PPCLowerMASSVEntries : public ModulePass {
39public:
40 static char ID;
41
42 PPCLowerMASSVEntries() : ModulePass(ID) {}
43
44 bool runOnModule(Module &M) override;
45
46 StringRef getPassName() const override { return "PPC Lower MASS Entries"; }
47
48 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 }
51
52private:
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 handlePowSpecialCases(CallInst *CI, Function &Func, Module &M);
58 bool lowerMASSVCall(CallInst *CI, Function &Func, Module &M,
59 const PPCSubtarget *Subtarget);
60};
61
62} // namespace
63
64/// Checks if the specified function name represents an entry in the MASSV
65/// library.
66bool PPCLowerMASSVEntries::isMASSVFunc(StringRef Name) {
67 return llvm::is_contained(MASSVFuncs, Name);
68}
69
70// FIXME:
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 minimum subtarget Power8 for Linux and Power7 for AIX.
75StringRef PPCLowerMASSVEntries::getCPUSuffix(const PPCSubtarget *Subtarget) {
76 // Assume generic when Subtarget is unavailable.
77 if (!Subtarget)
78 return "";
79 // TODO: add _P10 enties to Linux MASS lib and remove the check for AIX
80 if (Subtarget->isAIXABI() && Subtarget->hasP10Vector())
81 return "_P10";
82 if (Subtarget->hasP9Vector())
83 return "_P9";
84 if (Subtarget->hasP8Vector())
85 return "_P8";
86 if (Subtarget->isAIXABI())
87 return "_P7";
88
90 "Mininum subtarget for -vector-library=MASSV option is Power8 on Linux "
91 "and Power7 on AIX when vectorization is not disabled.");
92}
93
94/// Creates PowerPC subtarget-specific name corresponding to the specified
95/// generic MASSV function, and the PowerPC subtarget.
96std::string
97PPCLowerMASSVEntries::createMASSVFuncName(Function &Func,
98 const PPCSubtarget *Subtarget) {
99 StringRef Suffix = getCPUSuffix(Subtarget);
100 auto GenericName = Func.getName().str();
101 std::string MASSVEntryName = GenericName + Suffix.str();
102 return MASSVEntryName;
103}
104
105/// If there are proper fast-math flags, this function creates llvm.pow
106/// intrinsics when the exponent is 0.25 or 0.75.
107bool PPCLowerMASSVEntries::handlePowSpecialCases(CallInst *CI, Function &Func,
108 Module &M) {
109 if (Func.getName() != "__powf4" && Func.getName() != "__powd2")
110 return false;
111
112 if (Constant *Exp = dyn_cast<Constant>(CI->getArgOperand(1)))
113 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(Exp->getSplatValue())) {
114 // If the argument is 0.75 or 0.25 it is cheaper to turn it into pow
115 // intrinsic so that it could be optimzed as sequence of sqrt's.
116 if (!CI->hasNoInfs() || !CI->hasApproxFunc())
117 return false;
118
119 if (!CFP->isExactlyValue(0.75) && !CFP->isExactlyValue(0.25))
120 return false;
121
122 if (CFP->isExactlyValue(0.25) && !CI->hasNoSignedZeros())
123 return false;
124
126 Intrinsic::getDeclaration(&M, Intrinsic::pow, CI->getType()));
127 return true;
128 }
129
130 return false;
131}
132
133/// Lowers generic MASSV entries to PowerPC subtarget-specific MASSV entries.
134/// e.g.: __sind2_massv --> __sind2_P9 for a Power9 subtarget.
135/// Both function prototypes and their callsites are updated during lowering.
136bool PPCLowerMASSVEntries::lowerMASSVCall(CallInst *CI, Function &Func,
137 Module &M,
138 const PPCSubtarget *Subtarget) {
139 if (CI->use_empty())
140 return false;
141
142 // Handling pow(x, 0.25), pow(x, 0.75), powf(x, 0.25), powf(x, 0.75)
143 if (handlePowSpecialCases(CI, Func, M))
144 return true;
145
146 std::string MASSVEntryName = createMASSVFuncName(Func, Subtarget);
147 FunctionCallee FCache = M.getOrInsertFunction(
148 MASSVEntryName, Func.getFunctionType(), Func.getAttributes());
149
150 CI->setCalledFunction(FCache);
151
152 return true;
153}
154
155bool PPCLowerMASSVEntries::runOnModule(Module &M) {
156 bool Changed = false;
157
158 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
159 if (!TPC)
160 return Changed;
161
162 auto &TM = TPC->getTM<PPCTargetMachine>();
163 const PPCSubtarget *Subtarget;
164
165 for (Function &Func : M) {
166 if (!Func.isDeclaration())
167 continue;
168
169 if (!isMASSVFunc(Func.getName()))
170 continue;
171
172 // Call to lowerMASSVCall() invalidates the iterator over users upon
173 // replacing the users. Precomputing the current list of users allows us to
174 // replace all the call sites.
175 SmallVector<User *, 4> MASSVUsers(Func.users());
176
177 for (auto *User : MASSVUsers) {
178 auto *CI = dyn_cast<CallInst>(User);
179 if (!CI)
180 continue;
181
182 Subtarget = &TM.getSubtarget<PPCSubtarget>(*CI->getParent()->getParent());
183 Changed |= lowerMASSVCall(CI, Func, M, Subtarget);
184 }
185 }
186
187 return Changed;
188}
189
190char PPCLowerMASSVEntries::ID = 0;
191
192char &llvm::PPCLowerMASSVEntriesID = PPCLowerMASSVEntries::ID;
193
194INITIALIZE_PASS(PPCLowerMASSVEntries, DEBUG_TYPE, "Lower MASSV entries", false,
195 false)
196
198 return new PPCLowerMASSVEntries();
199}
std::string Name
Module.h This file contains the declarations for the Module class.
#define DEBUG_TYPE
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file contains some templates that are useful if you are working with the STL at all.
Target-Independent Code Generator Pass Configuration Options pass.
This pass exposes codegen information to IR-level passes.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1410
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1504
This class represents a function call, abstracting a target machine's calling convention.
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:269
This is an important base class in LLVM.
Definition: Constant.h:42
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:168
bool hasNoInfs() const LLVM_READONLY
Determine whether the no-infs flag is set.
bool hasNoSignedZeros() const LLVM_READONLY
Determine whether the no-signed-zeros flag is set.
bool hasApproxFunc() const LLVM_READONLY
Determine whether the approximate-math-functions flag is set.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
bool isAIXABI() const
Definition: PPCSubtarget.h:215
Common code between 32-bit and 64-bit PowerPC targets.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1210
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:215
Wrapper pass for TargetTransformInfo.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
bool use_empty() const
Definition: Value.h:344
const ParentTy * getParent() const
Definition: ilist_node.h:32
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1539
NodeAddr< FuncNode * > Func
Definition: RDFGraph.h:393
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
ModulePass * createPPCLowerMASSVEntriesPass()
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
char & PPCLowerMASSVEntriesID
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879