LLVM 19.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_NAMES
33#include "llvm/Analysis/VecFuncs.def"
34};
35
36class PPCLowerMASSVEntries : public ModulePass {
37public:
38 static char ID;
39
40 PPCLowerMASSVEntries() : ModulePass(ID) {}
41
42 bool runOnModule(Module &M) override;
43
44 StringRef getPassName() const override { return "PPC Lower MASS Entries"; }
45
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
48 }
49
50private:
51 static bool isMASSVFunc(StringRef Name);
52 static StringRef getCPUSuffix(const PPCSubtarget *Subtarget);
53 static std::string createMASSVFuncName(Function &Func,
54 const PPCSubtarget *Subtarget);
55 bool handlePowSpecialCases(CallInst *CI, Function &Func, Module &M);
56 bool lowerMASSVCall(CallInst *CI, Function &Func, Module &M,
57 const PPCSubtarget *Subtarget);
58};
59
60} // namespace
61
62/// Checks if the specified function name represents an entry in the MASSV
63/// library.
64bool PPCLowerMASSVEntries::isMASSVFunc(StringRef Name) {
65 return llvm::is_contained(MASSVFuncs, Name);
66}
67
68// FIXME:
69/// Returns a string corresponding to the specified PowerPC subtarget. e.g.:
70/// "_P8" for Power8, "_P9" for Power9. The string is used as a suffix while
71/// generating subtarget-specific MASSV library functions. Current support
72/// includes minimum subtarget Power8 for Linux and Power7 for AIX.
73StringRef PPCLowerMASSVEntries::getCPUSuffix(const PPCSubtarget *Subtarget) {
74 // Assume generic when Subtarget is unavailable.
75 if (!Subtarget)
76 return "";
77 // TODO: add _P10 enties to Linux MASS lib and remove the check for AIX
78 if (Subtarget->isAIXABI() && Subtarget->hasP10Vector())
79 return "_P10";
80 if (Subtarget->hasP9Vector())
81 return "_P9";
82 if (Subtarget->hasP8Vector())
83 return "_P8";
84 if (Subtarget->isAIXABI())
85 return "_P7";
86
88 "Mininum subtarget for -vector-library=MASSV option is Power8 on Linux "
89 "and Power7 on AIX when vectorization is not disabled.");
90}
91
92/// Creates PowerPC subtarget-specific name corresponding to the specified
93/// generic MASSV function, and the PowerPC subtarget.
94std::string
95PPCLowerMASSVEntries::createMASSVFuncName(Function &Func,
96 const PPCSubtarget *Subtarget) {
97 StringRef Suffix = getCPUSuffix(Subtarget);
98 auto GenericName = Func.getName().str();
99 std::string MASSVEntryName = GenericName + Suffix.str();
100 return MASSVEntryName;
101}
102
103/// If there are proper fast-math flags, this function creates llvm.pow
104/// intrinsics when the exponent is 0.25 or 0.75.
105bool PPCLowerMASSVEntries::handlePowSpecialCases(CallInst *CI, Function &Func,
106 Module &M) {
107 if (Func.getName() != "__powf4" && Func.getName() != "__powd2")
108 return false;
109
110 if (Constant *Exp = dyn_cast<Constant>(CI->getArgOperand(1)))
111 if (ConstantFP *CFP = dyn_cast_or_null<ConstantFP>(Exp->getSplatValue())) {
112 // If the argument is 0.75 or 0.25 it is cheaper to turn it into pow
113 // intrinsic so that it could be optimzed as sequence of sqrt's.
114 if (!CI->hasNoInfs() || !CI->hasApproxFunc())
115 return false;
116
117 if (!CFP->isExactlyValue(0.75) && !CFP->isExactlyValue(0.25))
118 return false;
119
120 if (CFP->isExactlyValue(0.25) && !CI->hasNoSignedZeros())
121 return false;
122
124 Intrinsic::getDeclaration(&M, Intrinsic::pow, CI->getType()));
125 return true;
126 }
127
128 return false;
129}
130
131/// Lowers generic MASSV entries to PowerPC subtarget-specific MASSV entries.
132/// e.g.: __sind2_massv --> __sind2_P9 for a Power9 subtarget.
133/// Both function prototypes and their callsites are updated during lowering.
134bool PPCLowerMASSVEntries::lowerMASSVCall(CallInst *CI, Function &Func,
135 Module &M,
136 const PPCSubtarget *Subtarget) {
137 if (CI->use_empty())
138 return false;
139
140 // Handling pow(x, 0.25), pow(x, 0.75), powf(x, 0.25), powf(x, 0.75)
141 if (handlePowSpecialCases(CI, Func, M))
142 return true;
143
144 std::string MASSVEntryName = createMASSVFuncName(Func, Subtarget);
145 FunctionCallee FCache = M.getOrInsertFunction(
146 MASSVEntryName, Func.getFunctionType(), Func.getAttributes());
147
148 CI->setCalledFunction(FCache);
149
150 return true;
151}
152
153bool PPCLowerMASSVEntries::runOnModule(Module &M) {
154 bool Changed = false;
155
156 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
157 if (!TPC)
158 return Changed;
159
160 auto &TM = TPC->getTM<PPCTargetMachine>();
161 const PPCSubtarget *Subtarget;
162
163 for (Function &Func : M) {
164 if (!Func.isDeclaration())
165 continue;
166
167 if (!isMASSVFunc(Func.getName()))
168 continue;
169
170 // Call to lowerMASSVCall() invalidates the iterator over users upon
171 // replacing the users. Precomputing the current list of users allows us to
172 // replace all the call sites.
173 SmallVector<User *, 4> MASSVUsers(Func.users());
174
175 for (auto *User : MASSVUsers) {
176 auto *CI = dyn_cast<CallInst>(User);
177 if (!CI)
178 continue;
179
180 Subtarget = &TM.getSubtarget<PPCSubtarget>(*CI->getParent()->getParent());
181 Changed |= lowerMASSVCall(CI, Func, M, Subtarget);
182 }
183 }
184
185 return Changed;
186}
187
188char PPCLowerMASSVEntries::ID = 0;
189
190char &llvm::PPCLowerMASSVEntriesID = PPCLowerMASSVEntries::ID;
191
192INITIALIZE_PASS(PPCLowerMASSVEntries, DEBUG_TYPE, "Lower MASSV entries", false,
193 false)
194
196 return new PPCLowerMASSVEntries();
197}
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()
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:206
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1687
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
Definition: InstrTypes.h:1781
This class represents a function call, abstracting a target machine's calling convention.
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
This is an important base class in LLVM.
Definition: Constant.h:41
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.
const BasicBlock * getParent() const
Definition: Instruction.h:152
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:214
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:1209
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:222
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
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:1465
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:156
char & PPCLowerMASSVEntriesID
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879