xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/Scalar/LowerWidenableCondition.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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 
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"
23 
24 using namespace llvm;
25 
26 static bool lowerWidenableCondition(Function &F) {
27   // Check if we can cheaply rule out the possibility of not having any work to
28   // do.
29   auto *WCDecl = F.getParent()->getFunction(
30       Intrinsic::getName(Intrinsic::experimental_widenable_condition));
31   if (!WCDecl || WCDecl->use_empty())
32     return false;
33 
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
38   // function.
39   for (auto *U : WCDecl->users())
40     if (auto *CI = dyn_cast<CallInst>(U))
41       if (CI->getFunction() == &F)
42         ToLower.push_back(CI);
43 
44   if (ToLower.empty())
45     return false;
46 
47   for (auto *CI : ToLower) {
48     CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getContext()));
49     CI->eraseFromParent();
50   }
51   return true;
52 }
53 
54 PreservedAnalyses LowerWidenableConditionPass::run(Function &F,
55                                                FunctionAnalysisManager &AM) {
56   if (lowerWidenableCondition(F))
57     return PreservedAnalyses::none();
58 
59   return PreservedAnalyses::all();
60 }
61