LLVM  14.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"
17 #include "llvm/IR/BasicBlock.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/InstIterator.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/PatternMatch.h"
24 #include "llvm/InitializePasses.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Transforms/Scalar.h"
28 
29 using namespace llvm;
30 
31 namespace {
32 struct LowerWidenableConditionLegacyPass : public FunctionPass {
33  static char ID;
34  LowerWidenableConditionLegacyPass() : FunctionPass(ID) {
37  }
38 
39  bool runOnFunction(Function &F) override;
40 };
41 }
42 
44  // Check if we can cheaply rule out the possibility of not having any work to
45  // do.
46  auto *WCDecl = F.getParent()->getFunction(
47  Intrinsic::getName(Intrinsic::experimental_widenable_condition));
48  if (!WCDecl || WCDecl->use_empty())
49  return false;
50 
51  using namespace llvm::PatternMatch;
53  for (auto &I : instructions(F))
54  if (match(&I, m_Intrinsic<Intrinsic::experimental_widenable_condition>()))
55  ToLower.push_back(cast<CallInst>(&I));
56 
57  if (ToLower.empty())
58  return false;
59 
60  for (auto *CI : ToLower) {
61  CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getContext()));
62  CI->eraseFromParent();
63  }
64  return true;
65 }
66 
68  return lowerWidenableCondition(F);
69 }
70 
72 INITIALIZE_PASS(LowerWidenableConditionLegacyPass, "lower-widenable-condition",
73  "Lower the widenable condition to default true value", false,
74  false)
75 
77  return new LowerWidenableConditionLegacyPass();
78 }
79 
83  return PreservedAnalyses::none();
84 
85  return PreservedAnalyses::all();
86 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
LowerWidenableCondition.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
Scalar.h
InstIterator.h
llvm::Function
Definition: Function.h:62
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:1168
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:158
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:72
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
Intrinsics.h
GuardUtils.h
PatternMatch.h
BasicBlock.h
llvm::instructions
inst_range instructions(Function *F)
Definition: InstIterator.h:133
lowerWidenableCondition
static bool lowerWidenableCondition(Function &F)
Definition: LowerWidenableCondition.cpp:43
I
#define I(x, y, z)
Definition: MD5.cpp:59
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:873
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
Function.h
llvm::LowerWidenableConditionPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: LowerWidenableCondition.cpp:80
GuardUtils.h
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:44
llvm::createLowerWidenableConditionPass
Pass * createLowerWidenableConditionPass()
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
InitializePasses.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38