xref: /llvm-project/llvm/lib/Transforms/Scalar/LowerWidenableCondition.cpp (revision b02e5bc5b1be9d94689ebe1cf1244b7da540fb19)
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/Instructions.h"
18 #include "llvm/IR/Intrinsics.h"
19 #include "llvm/IR/PatternMatch.h"
20 #include "llvm/Transforms/Scalar.h"
21 
22 using namespace llvm;
23 
24 static bool lowerWidenableCondition(Function &F) {
25   // Check if we can cheaply rule out the possibility of not having any work to
26   // do.
27   auto *WCDecl = Intrinsic::getDeclarationIfExists(
28       F.getParent(), Intrinsic::experimental_widenable_condition);
29   if (!WCDecl || WCDecl->use_empty())
30     return false;
31 
32   using namespace llvm::PatternMatch;
33   SmallVector<CallInst *, 8> ToLower;
34   // Traverse through the users of WCDecl.
35   // This is presumably cheaper than traversing all instructions in the
36   // function.
37   for (auto *U : WCDecl->users())
38     if (auto *CI = dyn_cast<CallInst>(U))
39       if (CI->getFunction() == &F)
40         ToLower.push_back(CI);
41 
42   if (ToLower.empty())
43     return false;
44 
45   for (auto *CI : ToLower) {
46     CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getContext()));
47     CI->eraseFromParent();
48   }
49   return true;
50 }
51 
52 PreservedAnalyses LowerWidenableConditionPass::run(Function &F,
53                                                FunctionAnalysisManager &AM) {
54   if (lowerWidenableCondition(F))
55     return PreservedAnalyses::none();
56 
57   return PreservedAnalyses::all();
58 }
59