1 //===- LowerWidenableCondition.cpp - Lower the guard intrinsic ---------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This pass lowers the llvm.widenable.condition intrinsic to default value
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
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/Transforms/Scalar.h"
26 static bool lowerWidenableCondition(Function
&F
) {
27 // Check if we can cheaply rule out the possibility of not having any work to
29 auto *WCDecl
= F
.getParent()->getFunction(
30 Intrinsic::getName(Intrinsic::experimental_widenable_condition
));
31 if (!WCDecl
|| WCDecl
->use_empty())
34 using namespace llvm::PatternMatch
;
35 SmallVector
<CallInst
*, 8> ToLower
;
36 // Traverse through the users of WCDecl.
37 // This is presumably cheaper than traversing all instructions in the
39 for (auto *U
: WCDecl
->users())
40 if (auto *CI
= dyn_cast
<CallInst
>(U
))
41 if (CI
->getFunction() == &F
)
42 ToLower
.push_back(CI
);
47 for (auto *CI
: ToLower
) {
48 CI
->replaceAllUsesWith(ConstantInt::getTrue(CI
->getContext()));
49 CI
->eraseFromParent();
54 PreservedAnalyses
LowerWidenableConditionPass::run(Function
&F
,
55 FunctionAnalysisManager
&AM
) {
56 if (lowerWidenableCondition(F
))
57 return PreservedAnalyses::none();
59 return PreservedAnalyses::all();