[RISCV] Fix mgather -> riscv.masked.strided.load combine not extending indices (...
[llvm-project.git] / llvm / lib / Transforms / Scalar / LowerWidenableCondition.cpp
blob3c977b816a05067cf5708d58517fbc4414dcfb21
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.
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"
24 using namespace llvm;
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;
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);
44 if (ToLower.empty())
45 return false;
47 for (auto *CI : ToLower) {
48 CI->replaceAllUsesWith(ConstantInt::getTrue(CI->getContext()));
49 CI->eraseFromParent();
51 return true;
54 PreservedAnalyses LowerWidenableConditionPass::run(Function &F,
55 FunctionAnalysisManager &AM) {
56 if (lowerWidenableCondition(F))
57 return PreservedAnalyses::none();
59 return PreservedAnalyses::all();