LLVM  15.0.0git
LowerWidenableCondition.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/InstIterator.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/Intrinsics.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/PatternMatch.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Transforms/Scalar.h"
25 
26 using namespace llvm;
27 
28 namespace {
29 struct LowerWidenableConditionLegacyPass : public FunctionPass {
30  static char ID;
31  LowerWidenableConditionLegacyPass() : FunctionPass(ID) {
34  }
35 
36  bool runOnFunction(Function &F) override;
37 };
38 }
39 
41  // Check if we can cheaply rule out the possibility of not having any work to
42  // do.
43  auto *WCDecl = F.getParent()->getFunction(
44  Intrinsic::getName(Intrinsic::experimental_widenable_condition));
45  if (!WCDecl || WCDecl->use_empty())
46  return false;
47 
48  using namespace llvm::PatternMatch;
50  // Traverse through the users of WCDecl.
51  // This is presumably cheaper than traversing all instructions in the
52  // function.
53  for (auto *U : WCDecl->users())
54  if (auto *CI = dyn_cast<CallInst>(U))
55  if (CI->getFunction() == &F)
56  ToLower.push_back(CI);
57 
58  if (ToLower.empty())
59  return false;
60 
61  for (auto *CI : ToLower) {
62  CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getContext()));
63  CI->eraseFromParent();
64  }
65  return true;
66 }
67 
69  return lowerWidenableCondition(F);
70 }
71 
73 INITIALIZE_PASS(LowerWidenableConditionLegacyPass, "lower-widenable-condition",
74  "Lower the widenable condition to default true value", false,
75  false)
76 
78  return new LowerWidenableConditionLegacyPass();
79 }
80 
84  return PreservedAnalyses::none();
85 
86  return PreservedAnalyses::all();
87 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
LowerWidenableCondition.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
Scalar.h
InstIterator.h
llvm::Function
Definition: Function.h:60
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::Intrinsic::getName
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
Definition: Function.cpp:879
llvm::PreservedAnalyses::none
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: PassManager.h:155
Module.h
INITIALIZE_PASS
INITIALIZE_PASS(LowerWidenableConditionLegacyPass, "lower-widenable-condition", "Lower the widenable condition to default true value", false, false) Pass *llvm
Definition: LowerWidenableCondition.cpp:73
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
Intrinsics.h
PatternMatch.h
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
lowerWidenableCondition
static bool lowerWidenableCondition(Function &F)
Definition: LowerWidenableCondition.cpp:40
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::initializeLowerWidenableConditionLegacyPassPass
void initializeLowerWidenableConditionLegacyPassPass(PassRegistry &)
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:867
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
Function.h
llvm::LowerWidenableConditionPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LowerWidenableCondition.cpp:81
llvm::Pass
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
Instructions.h
SmallVector.h
llvm::PatternMatch
Definition: PatternMatch.h:47
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::createLowerWidenableConditionPass
Pass * createLowerWidenableConditionPass()
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37