LLVM  10.0.0svn
AMDGPULibFunc.h
Go to the documentation of this file.
1 //===-- AMDGPULibFunc.h ----------------------------------------*- C++ -*--===//
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 #ifndef _AMDGPU_LIBFUNC_H_
10 #define _AMDGPU_LIBFUNC_H_
11 
12 #include "llvm/ADT/StringRef.h"
13 
14 namespace llvm {
15 
16 class FunctionType;
17 class Function;
18 class Module;
19 
21 public:
22  enum EFuncId {
24 
25  // IMPORTANT: enums below should go in ascending by 1 value order
26  // because they are used as indexes in the mangling rules table.
27  // don't use explicit value assignment.
28  //
29  // There are two types of library functions: those with mangled
30  // name and those with unmangled name. The enums for the library
31  // functions with mangled name are defined before enums for the
32  // library functions with unmangled name. The enum for the last
33  // library function with mangled name is EI_LAST_MANGLED.
34  //
35  // Library functions with mangled name.
234  EI_RCBRT, /* The last library function with mangled name */
235 
236  // Library functions with unmangled name.
241 
243  };
244 
245  enum ENamePrefix {
249  };
250 
251  enum EType {
252  B8 = 1,
253  B16 = 2,
254  B32 = 3,
255  B64 = 4,
257  FLOAT = 0x10,
258  INT = 0x20,
259  UINT = 0x30,
261  U8 = UINT | B8,
262  U16 = UINT | B16,
263  U32 = UINT | B32,
264  U64 = UINT | B64,
265  I8 = INT | B8,
266  I16 = INT | B16,
267  I32 = INT | B32,
268  I64 = INT | B64,
269  F16 = FLOAT | B16,
270  F32 = FLOAT | B32,
271  F64 = FLOAT | B64,
272  IMG1DA = 0x80,
281  };
282 
283  enum EPtrKind {
284  BYVALUE = 0,
285  ADDR_SPACE = 0xF, // Address space takes value 0x1 ~ 0xF.
286  CONST = 0x10,
287  VOLATILE = 0x20
288  };
289 
290  struct Param {
291  unsigned char ArgType;
292  unsigned char VectorSize;
293  unsigned char PtrKind;
294 
295  unsigned char Reserved;
296 
297  void reset() {
298  ArgType = 0;
299  VectorSize = 1;
300  PtrKind = 0;
301  }
302  Param() { reset(); }
303 
304  template <typename Stream>
305  void mangleItanium(Stream& os);
306  };
307  static bool isMangled(EFuncId Id) {
308  return static_cast<unsigned>(Id) <= static_cast<unsigned>(EI_LAST_MANGLED);
309  }
310 
311  static unsigned getEPtrKindFromAddrSpace(unsigned AS) {
312  assert(((AS + 1) & ~ADDR_SPACE) == 0);
313  return AS + 1;
314  }
315 
316  static unsigned getAddrSpaceFromEPtrKind(unsigned Kind) {
317  Kind = Kind & ADDR_SPACE;
318  assert(Kind >= 1);
319  return Kind - 1;
320  }
321 };
322 
324 public:
326  virtual ~AMDGPULibFuncImpl() {}
327 
328  /// Get unmangled name for mangled library function and name for unmangled
329  /// library function.
330  virtual std::string getName() const = 0;
331  virtual unsigned getNumArgs() const = 0;
332  EFuncId getId() const { return FuncId; }
333  ENamePrefix getPrefix() const { return FKind; }
334 
336 
337  void setId(EFuncId id) { FuncId = id; }
338  virtual bool parseFuncName(StringRef &mangledName) = 0;
339 
340  /// \return The mangled function name for mangled library functions
341  /// and unmangled function name for unmangled library functions.
342  virtual std::string mangle() const = 0;
343 
344  void setName(StringRef N) { Name = N; }
345  void setPrefix(ENamePrefix pfx) { FKind = pfx; }
346 
347  virtual FunctionType *getFunctionType(Module &M) const = 0;
348 
349 protected:
351  std::string Name;
353 };
354 
355 /// Wrapper class for AMDGPULIbFuncImpl
357 public:
358  explicit AMDGPULibFunc() : Impl(std::unique_ptr<AMDGPULibFuncImpl>()) {}
359  AMDGPULibFunc(const AMDGPULibFunc &F);
360  /// Clone a mangled library func with the Id \p Id and argument info from \p
361  /// CopyFrom.
362  explicit AMDGPULibFunc(EFuncId Id, const AMDGPULibFunc &CopyFrom);
363  /// Construct an unmangled library function on the fly.
364  explicit AMDGPULibFunc(StringRef FName, FunctionType *FT);
365 
366  AMDGPULibFunc &operator=(const AMDGPULibFunc &F);
367 
368  /// Get unmangled name for mangled library function and name for unmangled
369  /// library function.
370  std::string getName() const { return Impl->getName(); }
371  unsigned getNumArgs() const { return Impl->getNumArgs(); }
372  EFuncId getId() const { return Impl->getId(); }
373  ENamePrefix getPrefix() const { return Impl->getPrefix(); }
374  /// Get leading parameters for mangled lib functions.
375  Param *getLeads();
376  const Param *getLeads() const;
377 
378  bool isMangled() const { return Impl->isMangled(); }
379  void setId(EFuncId Id) { Impl->setId(Id); }
380  bool parseFuncName(StringRef &MangledName) {
381  return Impl->parseFuncName(MangledName);
382  }
383 
384  /// \return The mangled function name for mangled library functions
385  /// and unmangled function name for unmangled library functions.
386  std::string mangle() const { return Impl->mangle(); }
387 
388  void setName(StringRef N) { Impl->setName(N); }
389  void setPrefix(ENamePrefix PFX) { Impl->setPrefix(PFX); }
390 
392  return Impl->getFunctionType(M);
393  }
394  static Function *getFunction(llvm::Module *M, const AMDGPULibFunc &fInfo);
395 
396  static FunctionCallee getOrInsertFunction(llvm::Module *M,
397  const AMDGPULibFunc &fInfo);
398  static bool parse(StringRef MangledName, AMDGPULibFunc &Ptr);
399 
400 private:
401  /// Initialize as a mangled library function.
402  void initMangled();
403  std::unique_ptr<AMDGPULibFuncImpl> Impl;
404 };
405 
407 public:
408  Param Leads[2];
409 
410  explicit AMDGPUMangledLibFunc();
411  explicit AMDGPUMangledLibFunc(EFuncId id,
412  const AMDGPUMangledLibFunc &copyFrom);
413 
414  std::string getName() const override;
415  unsigned getNumArgs() const override;
416  FunctionType *getFunctionType(Module &M) const override;
417  static StringRef getUnmangledName(StringRef MangledName);
418 
419  bool parseFuncName(StringRef &mangledName) override;
420 
421  // Methods for support type inquiry through isa, cast, and dyn_cast:
422  static bool classof(const AMDGPULibFuncImpl *F) { return F->isMangled(); }
423 
424  std::string mangle() const override;
425 
426 private:
427  std::string mangleNameItanium() const;
428 
429  std::string mangleName(StringRef Name) const;
430  bool parseUnmangledName(StringRef MangledName);
431 
432  template <typename Stream> void writeName(Stream &OS) const;
433 };
434 
436  FunctionType *FuncTy;
437 
438 public:
439  explicit AMDGPUUnmangledLibFunc();
441  Name = FName;
442  FuncTy = FT;
443  }
444  std::string getName() const override { return Name; }
445  unsigned getNumArgs() const override;
446  FunctionType *getFunctionType(Module &M) const override { return FuncTy; }
447 
448  bool parseFuncName(StringRef &Name) override;
449 
450  // Methods for support type inquiry through isa, cast, and dyn_cast:
451  static bool classof(const AMDGPULibFuncImpl *F) { return !F->isMangled(); }
452 
453  std::string mangle() const override { return Name; }
454 
455  void setFunctionType(FunctionType *FT) { FuncTy = FT; }
456 };
457 }
458 #endif // _AMDGPU_LIBFUNC_H_
Profile::FuncID FuncId
Definition: Profile.cpp:321
EFuncId getId() const
This class represents lattice values for constants.
Definition: AllocatorList.h:23
ENamePrefix getPrefix() const
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:66
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
AMDGPUUnmangledLibFunc(StringRef FName, FunctionType *FT)
void setId(EFuncId id)
F(f)
void setPrefix(ENamePrefix PFX)
std::string getName() const
Get unmangled name for mangled library function and name for unmangled library function.
Definition: BitVector.h:937
static bool isMangled(EFuncId Id)
static StringRef getName(Value *V)
unsigned getNumArgs() const
void setId(EFuncId Id)
std::string getName() const override
Get unmangled name for mangled library function and name for unmangled library function.
Class to represent function types.
Definition: DerivedTypes.h:108
static Function * getFunction(Constant *C)
Definition: Evaluator.cpp:258
static bool classof(const AMDGPULibFuncImpl *F)
void setName(StringRef N)
bool isMangled() const
Wrapper class for AMDGPULIbFuncImpl.
static unsigned getEPtrKindFromAddrSpace(unsigned AS)
bool parseFuncName(StringRef &MangledName)
EFuncId getId() const
void setFunctionType(FunctionType *FT)
void mangleItanium(Stream &os)
FunctionType * getFunctionType(Module &M) const override
static unsigned getAddrSpaceFromEPtrKind(unsigned Kind)
void setPrefix(ENamePrefix pfx)
ENamePrefix getPrefix() const
std::string mangle() const override
#define N
void setName(StringRef N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::string mangle() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static llvm::Error parse(DataExtractor &Data, uint64_t BaseAddr, LineEntryCallback const &Callback)
Definition: LineTable.cpp:54
static bool classof(const AMDGPULibFuncImpl *F)
FunctionType * getFunctionType(Module &M) const