LowerWidenableCondition.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===- LowerWidenableCondition.cpp - Lower the guard intrinsic ---------------===//
  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 pass lowers the llvm.widenable.condition intrinsic to default value
  10. // which is i1 true.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
  14. #include "llvm/ADT/SmallVector.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/InstIterator.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/IR/Intrinsics.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/IR/PatternMatch.h"
  21. #include "llvm/InitializePasses.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/Transforms/Scalar.h"
  24. using namespace llvm;
  25. namespace {
  26. struct LowerWidenableConditionLegacyPass : public FunctionPass {
  27. static char ID;
  28. LowerWidenableConditionLegacyPass() : FunctionPass(ID) {
  29. initializeLowerWidenableConditionLegacyPassPass(
  30. *PassRegistry::getPassRegistry());
  31. }
  32. bool runOnFunction(Function &F) override;
  33. };
  34. }
  35. static bool lowerWidenableCondition(Function &F) {
  36. // Check if we can cheaply rule out the possibility of not having any work to
  37. // do.
  38. auto *WCDecl = F.getParent()->getFunction(
  39. Intrinsic::getName(Intrinsic::experimental_widenable_condition));
  40. if (!WCDecl || WCDecl->use_empty())
  41. return false;
  42. using namespace llvm::PatternMatch;
  43. SmallVector<CallInst *, 8> ToLower;
  44. // Traverse through the users of WCDecl.
  45. // This is presumably cheaper than traversing all instructions in the
  46. // function.
  47. for (auto *U : WCDecl->users())
  48. if (auto *CI = dyn_cast<CallInst>(U))
  49. if (CI->getFunction() == &F)
  50. ToLower.push_back(CI);
  51. if (ToLower.empty())
  52. return false;
  53. for (auto *CI : ToLower) {
  54. CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getContext()));
  55. CI->eraseFromParent();
  56. }
  57. return true;
  58. }
  59. bool LowerWidenableConditionLegacyPass::runOnFunction(Function &F) {
  60. return lowerWidenableCondition(F);
  61. }
  62. char LowerWidenableConditionLegacyPass::ID = 0;
  63. INITIALIZE_PASS(LowerWidenableConditionLegacyPass, "lower-widenable-condition",
  64. "Lower the widenable condition to default true value", false,
  65. false)
  66. Pass *llvm::createLowerWidenableConditionPass() {
  67. return new LowerWidenableConditionLegacyPass();
  68. }
  69. PreservedAnalyses LowerWidenableConditionPass::run(Function &F,
  70. FunctionAnalysisManager &AM) {
  71. if (lowerWidenableCondition(F))
  72. return PreservedAnalyses::none();
  73. return PreservedAnalyses::all();
  74. }