blob: 5342f2ddcb6b11abd47604b2aa5d3f78467ff565 [file] [log] [blame]
Max Kazantsevf392bc82019-01-31 09:10:17 +00001//===- LowerWidenableCondition.cpp - Lower the guard intrinsic ---------------===//
2//
Chandler Carruth1f555032019-02-11 08:25:56 +00003// 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
Max Kazantsevf392bc82019-01-31 09:10:17 +00006//
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/Analysis/GuardUtils.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/Pass.h"
25#include "llvm/Transforms/Scalar.h"
26#include "llvm/Transforms/Utils/GuardUtils.h"
27
28using namespace llvm;
29
30namespace {
31struct LowerWidenableConditionLegacyPass : public FunctionPass {
32 static char ID;
33 LowerWidenableConditionLegacyPass() : FunctionPass(ID) {
34 initializeLowerWidenableConditionLegacyPassPass(
35 *PassRegistry::getPassRegistry());
36 }
37
38 bool runOnFunction(Function &F) override;
39};
40}
41
42static bool lowerWidenableCondition(Function &F) {
43 // Check if we can cheaply rule out the possibility of not having any work to
44 // do.
45 auto *WCDecl = F.getParent()->getFunction(
46 Intrinsic::getName(Intrinsic::experimental_widenable_condition));
47 if (!WCDecl || WCDecl->use_empty())
48 return false;
49
50 using namespace llvm::PatternMatch;
51 SmallVector<CallInst *, 8> ToLower;
52 for (auto &I : instructions(F))
53 if (match(&I, m_Intrinsic<Intrinsic::experimental_widenable_condition>()))
54 ToLower.push_back(cast<CallInst>(&I));
55
56 if (ToLower.empty())
57 return false;
58
59 for (auto *CI : ToLower) {
60 CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getContext()));
61 CI->eraseFromParent();
62 }
63 return true;
64}
65
66bool LowerWidenableConditionLegacyPass::runOnFunction(Function &F) {
67 return lowerWidenableCondition(F);
68}
69
70char LowerWidenableConditionLegacyPass::ID = 0;
71INITIALIZE_PASS(LowerWidenableConditionLegacyPass, "lower-widenable-condition",
72 "Lower the widenable condition to default true value", false,
73 false)
74
75Pass *llvm::createLowerWidenableConditionPass() {
76 return new LowerWidenableConditionLegacyPass();
77}
78
79PreservedAnalyses LowerWidenableConditionPass::run(Function &F,
80 FunctionAnalysisManager &AM) {
81 if (lowerWidenableCondition(F))
82 return PreservedAnalyses::none();
83
84 return PreservedAnalyses::all();
85}