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