PPCGenScalarMASSEntries.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //===-- PPCGenScalarMASSEntries.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 transformation converts standard math functions into their
  10. // corresponding MASS (scalar) entries for PowerPC targets.
  11. // Following are examples of such conversion:
  12. // tanh ---> __xl_tanh_finite
  13. // Such lowering is legal under the fast-math option.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "PPC.h"
  17. #include "PPCSubtarget.h"
  18. #include "PPCTargetMachine.h"
  19. #include "llvm/Analysis/TargetTransformInfo.h"
  20. #include "llvm/CodeGen/TargetPassConfig.h"
  21. #include "llvm/IR/Instructions.h"
  22. #include "llvm/IR/Module.h"
  23. #define DEBUG_TYPE "ppc-gen-scalar-mass"
  24. using namespace llvm;
  25. namespace {
  26. class PPCGenScalarMASSEntries : public ModulePass {
  27. public:
  28. static char ID;
  29. PPCGenScalarMASSEntries() : ModulePass(ID) {
  30. ScalarMASSFuncs = {
  31. #define TLI_DEFINE_SCALAR_MASS_FUNCS
  32. #include "llvm/Analysis/ScalarFuncs.def"
  33. };
  34. }
  35. bool runOnModule(Module &M) override;
  36. StringRef getPassName() const override {
  37. return "PPC Generate Scalar MASS Entries";
  38. }
  39. void getAnalysisUsage(AnalysisUsage &AU) const override {
  40. AU.addRequired<TargetTransformInfoWrapperPass>();
  41. }
  42. private:
  43. std::map<StringRef, StringRef> ScalarMASSFuncs;
  44. bool isCandidateSafeToLower(const CallInst &CI) const;
  45. bool isFiniteCallSafe(const CallInst &CI) const;
  46. bool createScalarMASSCall(StringRef MASSEntry, CallInst &CI,
  47. Function &Func) const;
  48. };
  49. } // namespace
  50. // Returns true if 'afn' flag exists on the call instruction with the math
  51. // function
  52. bool PPCGenScalarMASSEntries::isCandidateSafeToLower(const CallInst &CI) const {
  53. // skip functions with no scalar or vector FP type (like cosisin)
  54. if (!isa<FPMathOperator>(CI))
  55. return false;
  56. return CI.hasApproxFunc();
  57. }
  58. // Returns true if 'nnan', 'ninf' and 'nsz' flags exist on the call instruction
  59. // with the math function
  60. bool PPCGenScalarMASSEntries::isFiniteCallSafe(const CallInst &CI) const {
  61. // skip functions with no scalar or vector FP type (like cosisin)
  62. if (!isa<FPMathOperator>(CI))
  63. return false;
  64. // FIXME: no-errno and trapping-math need to be set for MASS converstion
  65. // but they don't have IR representation.
  66. return CI.hasNoNaNs() && CI.hasNoInfs() && CI.hasNoSignedZeros();
  67. }
  68. /// Lowers scalar math functions to scalar MASS functions.
  69. /// e.g.: tanh --> __xl_tanh_finite or __xl_tanh
  70. /// Both function prototype and its callsite is updated during lowering.
  71. bool PPCGenScalarMASSEntries::createScalarMASSCall(StringRef MASSEntry,
  72. CallInst &CI,
  73. Function &Func) const {
  74. if (CI.use_empty())
  75. return false;
  76. Module *M = Func.getParent();
  77. assert(M && "Expecting a valid Module");
  78. std::string MASSEntryStr = MASSEntry.str();
  79. if (isFiniteCallSafe(CI))
  80. MASSEntryStr += "_finite";
  81. FunctionCallee FCache = M->getOrInsertFunction(
  82. MASSEntryStr, Func.getFunctionType(), Func.getAttributes());
  83. CI.setCalledFunction(FCache);
  84. return true;
  85. }
  86. bool PPCGenScalarMASSEntries::runOnModule(Module &M) {
  87. bool Changed = false;
  88. auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
  89. if (!TPC || skipModule(M))
  90. return false;
  91. for (Function &Func : M) {
  92. if (!Func.isDeclaration())
  93. continue;
  94. auto Iter = ScalarMASSFuncs.find(Func.getName());
  95. if (Iter == ScalarMASSFuncs.end())
  96. continue;
  97. // The call to createScalarMASSCall() invalidates the iterator over users
  98. // upon replacing the users. Precomputing the current list of users allows
  99. // us to replace all the call sites.
  100. SmallVector<User *, 4> TheUsers;
  101. for (auto *User : Func.users())
  102. TheUsers.push_back(User);
  103. for (auto *User : TheUsers)
  104. if (auto *CI = dyn_cast_or_null<CallInst>(User)) {
  105. if (isCandidateSafeToLower(*CI))
  106. Changed |= createScalarMASSCall(Iter->second, *CI, Func);
  107. }
  108. }
  109. return Changed;
  110. }
  111. char PPCGenScalarMASSEntries::ID = 0;
  112. char &llvm::PPCGenScalarMASSEntriesID = PPCGenScalarMASSEntries::ID;
  113. INITIALIZE_PASS(PPCGenScalarMASSEntries, DEBUG_TYPE,
  114. "Generate Scalar MASS entries", false, false)
  115. ModulePass *llvm::createPPCGenScalarMASSEntriesPass() {
  116. return new PPCGenScalarMASSEntries();
  117. }