LowerWidenableCondition.cpp 2.7 KB

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