llvm.org GIT mirror llvm / d4f6d11
[PM] Port LowerExpectIntrinsic to the new pass manager. This just lifts the logic into a static helper function, sinks the legacy pass to be a trivial wrapper of that helper fuction, and adds a trivial wrapper for the new PM as well. Not much to see here. I switched a test case to run in both modes, but we have to strip the dead prototypes separately as that pass isn't in the new pass manager (yet). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@226999 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 4 years ago
5 changed file(s) with 71 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
0 //===- LowerExpectIntrinsic.h - LowerExpectIntrinsic pass -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 ///
10 /// The header file for the LowerExpectIntrinsic pass as used by the new pass
11 /// manager.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_SCALAR_LOWEREXPECTINTRINSIC_H
16 #define LLVM_TRANSFORMS_SCALAR_LOWEREXPECTINTRINSIC_H
17
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/PassManager.h"
20
21 namespace llvm {
22
23 class LowerExpectIntrinsicPass {
24 public:
25 static StringRef name() { return "LowerExpectIntrinsicPass"; }
26
27 /// \brief Run the pass over the function.
28 ///
29 /// This will lower all of th expect intrinsic calls in this function into
30 /// branch weight metadata. That metadata will subsequently feed the analysis
31 /// of the probabilities and frequencies of the CFG. After running this pass,
32 /// no more expect intrinsics remain, allowing the rest of the optimizer to
33 /// ignore them.
34 PreservedAnalyses run(Function &F);
35 };
36
37 }
38
39 #endif
1010 //
1111 //===----------------------------------------------------------------------===//
1212
13 #include "llvm/Transforms/Scalar.h"
13 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
1414 #include "llvm/ADT/SmallVector.h"
1515 #include "llvm/ADT/Statistic.h"
1616 #include "llvm/IR/BasicBlock.h"
2424 #include "llvm/Pass.h"
2525 #include "llvm/Support/CommandLine.h"
2626 #include "llvm/Support/Debug.h"
27 #include "llvm/Transforms/Scalar.h"
2728
2829 using namespace llvm;
2930
3839 static cl::opt
3940 UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(4),
4041 cl::desc("Weight of the branch unlikely to be taken (default = 4)"));
41
42 namespace {
43 /// \brief Legacy pass for lowering expect intrinsics out of the IR.
44 ///
45 /// When this pass is run over a function it uses expect intrinsics which feed
46 /// branches and switches to provide branch weight metadata for those
47 /// terminators. It then removes the expect intrinsics from the IR so the rest
48 /// of the optimizer can ignore them.
49 class LowerExpectIntrinsic : public FunctionPass {
50 public:
51 static char ID;
52 LowerExpectIntrinsic() : FunctionPass(ID) {
53 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
54 }
55
56 bool runOnFunction(Function &F) override;
57 };
58 }
5942
6043 static bool handleSwitchExpect(SwitchInst &SI) {
6144 CallInst *CI = dyn_cast(SI.getCondition());
142125 return true;
143126 }
144127
145 bool LowerExpectIntrinsic::runOnFunction(Function &F) {
128 static bool lowerExpectIntrinsic(Function &F) {
146129 bool Changed = false;
147130
148131 for (BasicBlock &BB : F) {
174157 return Changed;
175158 }
176159
160 PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F) {
161 if (lowerExpectIntrinsic(F))
162 return PreservedAnalyses::none();
163
164 return PreservedAnalyses::all();
165 }
166
167 namespace {
168 /// \brief Legacy pass for lowering expect intrinsics out of the IR.
169 ///
170 /// When this pass is run over a function it uses expect intrinsics which feed
171 /// branches and switches to provide branch weight metadata for those
172 /// terminators. It then removes the expect intrinsics from the IR so the rest
173 /// of the optimizer can ignore them.
174 class LowerExpectIntrinsic : public FunctionPass {
175 public:
176 static char ID;
177 LowerExpectIntrinsic() : FunctionPass(ID) {
178 initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
179 }
180
181 bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); }
182 };
183 }
184
177185 char LowerExpectIntrinsic::ID = 0;
178186 INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
179187 "Lower 'expect' Intrinsics", false, false)
0 ; RUN: opt -lower-expect -strip-dead-prototypes -S -o - < %s | FileCheck %s
1 ; RUN: opt -S -passes=lower-expect < %s | opt -strip-dead-prototypes -S | FileCheck %s
12
23 ; CHECK-LABEL: @test1(
34 define i32 @test1(i32 %x) nounwind uwtable ssp {
6262 FUNCTION_PASS("instcombine", InstCombinePass())
6363 FUNCTION_PASS("invalidate", InvalidateAllAnalysesPass())
6464 FUNCTION_PASS("no-op-function", NoOpFunctionPass())
65 FUNCTION_PASS("lower-expect", LowerExpectIntrinsicPass())
6566 FUNCTION_PASS("print", PrintFunctionPass(dbgs()))
6667 FUNCTION_PASS("print", AssumptionPrinterPass(dbgs()))
6768 FUNCTION_PASS("print", DominatorTreePrinterPass(dbgs()))
2525 #include "llvm/IR/Verifier.h"
2626 #include "llvm/Support/Debug.h"
2727 #include "llvm/Transforms/InstCombine/InstCombine.h"
28 #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
2829
2930 using namespace llvm;
3031