Max Kazantsev | f392bc8 | 2019-01-31 09:10:17 +0000 | [diff] [blame^] | 1 | //===- LowerWidenableCondition.cpp - Lower the guard intrinsic ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass lowers the llvm.widenable.condition intrinsic to default value |
| 11 | // which is i1 true. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/Transforms/Scalar/LowerWidenableCondition.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/Analysis/GuardUtils.h" |
| 18 | #include "llvm/IR/BasicBlock.h" |
| 19 | #include "llvm/IR/Function.h" |
| 20 | #include "llvm/IR/InstIterator.h" |
| 21 | #include "llvm/IR/Instructions.h" |
| 22 | #include "llvm/IR/Intrinsics.h" |
| 23 | #include "llvm/IR/Module.h" |
| 24 | #include "llvm/IR/PatternMatch.h" |
| 25 | #include "llvm/Pass.h" |
| 26 | #include "llvm/Transforms/Scalar.h" |
| 27 | #include "llvm/Transforms/Utils/GuardUtils.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
| 32 | struct LowerWidenableConditionLegacyPass : public FunctionPass { |
| 33 | static char ID; |
| 34 | LowerWidenableConditionLegacyPass() : FunctionPass(ID) { |
| 35 | initializeLowerWidenableConditionLegacyPassPass( |
| 36 | *PassRegistry::getPassRegistry()); |
| 37 | } |
| 38 | |
| 39 | bool runOnFunction(Function &F) override; |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | static bool lowerWidenableCondition(Function &F) { |
| 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; |
| 52 | SmallVector<CallInst *, 8> ToLower; |
| 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 | |
| 67 | bool LowerWidenableConditionLegacyPass::runOnFunction(Function &F) { |
| 68 | return lowerWidenableCondition(F); |
| 69 | } |
| 70 | |
| 71 | char LowerWidenableConditionLegacyPass::ID = 0; |
| 72 | INITIALIZE_PASS(LowerWidenableConditionLegacyPass, "lower-widenable-condition", |
| 73 | "Lower the widenable condition to default true value", false, |
| 74 | false) |
| 75 | |
| 76 | Pass *llvm::createLowerWidenableConditionPass() { |
| 77 | return new LowerWidenableConditionLegacyPass(); |
| 78 | } |
| 79 | |
| 80 | PreservedAnalyses LowerWidenableConditionPass::run(Function &F, |
| 81 | FunctionAnalysisManager &AM) { |
| 82 | if (lowerWidenableCondition(F)) |
| 83 | return PreservedAnalyses::none(); |
| 84 | |
| 85 | return PreservedAnalyses::all(); |
| 86 | } |