Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Scalar/LowerExpectIntrinsic.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- LowerExpectIntrinsic.cpp - Lower expect 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 'expect' intrinsic to LLVM metadata.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
14
#include "llvm/ADT/SmallVector.h"
15
#include "llvm/ADT/Statistic.h"
16
#include "llvm/ADT/iterator_range.h"
17
#include "llvm/IR/BasicBlock.h"
18
#include "llvm/IR/Constants.h"
19
#include "llvm/IR/Function.h"
20
#include "llvm/IR/Instructions.h"
21
#include "llvm/IR/Intrinsics.h"
22
#include "llvm/IR/LLVMContext.h"
23
#include "llvm/IR/MDBuilder.h"
24
#include "llvm/IR/Metadata.h"
25
#include "llvm/Pass.h"
26
#include "llvm/Support/CommandLine.h"
27
#include "llvm/Support/Debug.h"
28
#include "llvm/Transforms/Scalar.h"
29
30
using namespace llvm;
31
32
#define DEBUG_TYPE "lower-expect-intrinsic"
33
34
STATISTIC(ExpectIntrinsicsHandled,
35
          "Number of 'expect' intrinsic instructions handled");
36
37
// These default values are chosen to represent an extremely skewed outcome for
38
// a condition, but they leave some room for interpretation by later passes.
39
//
40
// If the documentation for __builtin_expect() was made explicit that it should
41
// only be used in extreme cases, we could make this ratio higher. As it stands,
42
// programmers may be using __builtin_expect() / llvm.expect to annotate that a
43
// branch is likely or unlikely to be taken.
44
//
45
// There is a known dependency on this ratio in CodeGenPrepare when transforming
46
// 'select' instructions. It may be worthwhile to hoist these values to some
47
// shared space, so they can be used directly by other passes.
48
49
static cl::opt<uint32_t> LikelyBranchWeight(
50
    "likely-branch-weight", cl::Hidden, cl::init(2000),
51
    cl::desc("Weight of the branch likely to be taken (default = 2000)"));
52
static cl::opt<uint32_t> UnlikelyBranchWeight(
53
    "unlikely-branch-weight", cl::Hidden, cl::init(1),
54
    cl::desc("Weight of the branch unlikely to be taken (default = 1)"));
55
56
13.3k
static bool handleSwitchExpect(SwitchInst &SI) {
57
13.3k
  CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
58
13.3k
  if (!CI)
59
12.9k
    return false;
60
469
61
469
  Function *Fn = CI->getCalledFunction();
62
469
  if (!Fn || 
Fn->getIntrinsicID() != Intrinsic::expect450
)
63
465
    return false;
64
4
65
4
  Value *ArgValue = CI->getArgOperand(0);
66
4
  ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
67
4
  if (!ExpectedValue)
68
0
    return false;
69
4
70
4
  SwitchInst::CaseHandle Case = *SI.findCaseValue(ExpectedValue);
71
4
  unsigned n = SI.getNumCases(); // +1 for default case.
72
4
  SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
73
4
74
4
  if (Case == *SI.case_default())
75
2
    Weights[0] = LikelyBranchWeight;
76
2
  else
77
2
    Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
78
4
79
4
  SI.setMetadata(LLVMContext::MD_prof,
80
4
                 MDBuilder(CI->getContext()).createBranchWeights(Weights));
81
4
82
4
  SI.setCondition(ArgValue);
83
4
  return true;
84
4
}
85
86
/// Handler for PHINodes that define the value argument to an
87
/// @llvm.expect call.
88
///
89
/// If the operand of the phi has a constant value and it 'contradicts'
90
/// with the expected value of phi def, then the corresponding incoming
91
/// edge of the phi is unlikely to be taken. Using that information,
92
/// the branch probability info for the originating branch can be inferred.
93
21.4k
static void handlePhiDef(CallInst *Expect) {
94
21.4k
  Value &Arg = *Expect->getArgOperand(0);
95
21.4k
  ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(Expect->getArgOperand(1));
96
21.4k
  if (!ExpectedValue)
97
2
    return;
98
21.4k
  const APInt &ExpectedPhiValue = ExpectedValue->getValue();
99
21.4k
100
21.4k
  // Walk up in backward a list of instructions that
101
21.4k
  // have 'copy' semantics by 'stripping' the copies
102
21.4k
  // until a PHI node or an instruction of unknown kind
103
21.4k
  // is reached. Negation via xor is also handled.
104
21.4k
  //
105
21.4k
  //       C = PHI(...);
106
21.4k
  //       B = C;
107
21.4k
  //       A = B;
108
21.4k
  //       D = __builtin_expect(A, 0);
109
21.4k
  //
110
21.4k
  Value *V = &Arg;
111
21.4k
  SmallVector<Instruction *, 4> Operations;
112
55.3k
  while (!isa<PHINode>(V)) {
113
54.6k
    if (ZExtInst *ZExt = dyn_cast<ZExtInst>(V)) {
114
18.5k
      V = ZExt->getOperand(0);
115
18.5k
      Operations.push_back(ZExt);
116
18.5k
      continue;
117
18.5k
    }
118
36.1k
119
36.1k
    if (SExtInst *SExt = dyn_cast<SExtInst>(V)) {
120
48
      V = SExt->getOperand(0);
121
48
      Operations.push_back(SExt);
122
48
      continue;
123
48
    }
124
36.1k
125
36.1k
    BinaryOperator *BinOp = dyn_cast<BinaryOperator>(V);
126
36.1k
    if (!BinOp || 
BinOp->getOpcode() != Instruction::Xor15.3k
)
127
20.7k
      return;
128
15.3k
129
15.3k
    ConstantInt *CInt = dyn_cast<ConstantInt>(BinOp->getOperand(1));
130
15.3k
    if (!CInt)
131
0
      return;
132
15.3k
133
15.3k
    V = BinOp->getOperand(0);
134
15.3k
    Operations.push_back(BinOp);
135
15.3k
  }
136
21.4k
137
21.4k
  // Executes the recorded operations on input 'Value'.
138
21.4k
  
auto ApplyOperations = [&](const APInt &Value) 703
{
139
731
    APInt Result = Value;
140
886
    for (auto Op : llvm::reverse(Operations)) {
141
886
      switch (Op->getOpcode()) {
142
886
      case Instruction::Xor:
143
143
        Result ^= cast<ConstantInt>(Op->getOperand(1))->getValue();
144
143
        break;
145
886
      case Instruction::ZExt:
146
703
        Result = Result.zext(Op->getType()->getIntegerBitWidth());
147
703
        break;
148
886
      case Instruction::SExt:
149
40
        Result = Result.sext(Op->getType()->getIntegerBitWidth());
150
40
        break;
151
886
      default:
152
0
        llvm_unreachable("Unexpected operation");
153
886
      }
154
886
    }
155
731
    return Result;
156
731
  };
157
703
158
703
  auto *PhiDef = dyn_cast<PHINode>(V);
159
703
160
703
  // Get the first dominating conditional branch of the operand
161
703
  // i's incoming block.
162
703
  auto GetDomConditional = [&](unsigned i) -> BranchInst * {
163
594
    BasicBlock *BB = PhiDef->getIncomingBlock(i);
164
594
    BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
165
594
    if (BI && BI->isConditional())
166
592
      return BI;
167
2
    BB = BB->getSinglePredecessor();
168
2
    if (!BB)
169
0
      return nullptr;
170
2
    BI = dyn_cast<BranchInst>(BB->getTerminator());
171
2
    if (!BI || BI->isUnconditional())
172
0
      return nullptr;
173
2
    return BI;
174
2
  };
175
703
176
703
  // Now walk through all Phi operands to find phi oprerands with values
177
703
  // conflicting with the expected phi output value. Any such operand
178
703
  // indicates the incoming edge to that operand is unlikely.
179
2.13k
  for (unsigned i = 0, e = PhiDef->getNumIncomingValues(); i != e; 
++i1.43k
) {
180
1.43k
181
1.43k
    Value *PhiOpnd = PhiDef->getIncomingValue(i);
182
1.43k
    ConstantInt *CI = dyn_cast<ConstantInt>(PhiOpnd);
183
1.43k
    if (!CI)
184
703
      continue;
185
731
186
731
    // Not an interesting case when IsUnlikely is false -- we can not infer
187
731
    // anything useful when the operand value matches the expected phi
188
731
    // output.
189
731
    if (ExpectedPhiValue == ApplyOperations(CI->getValue()))
190
137
      continue;
191
594
192
594
    BranchInst *BI = GetDomConditional(i);
193
594
    if (!BI)
194
0
      continue;
195
594
196
594
    MDBuilder MDB(PhiDef->getContext());
197
594
198
594
    // There are two situations in which an operand of the PhiDef comes
199
594
    // from a given successor of a branch instruction BI.
200
594
    // 1) When the incoming block of the operand is the successor block;
201
594
    // 2) When the incoming block is BI's enclosing block and the
202
594
    // successor is the PhiDef's enclosing block.
203
594
    //
204
594
    // Returns true if the operand which comes from OpndIncomingBB
205
594
    // comes from outgoing edge of BI that leads to Succ block.
206
594
    auto *OpndIncomingBB = PhiDef->getIncomingBlock(i);
207
1.14k
    auto IsOpndComingFromSuccessor = [&](BasicBlock *Succ) {
208
1.14k
      if (OpndIncomingBB == Succ)
209
2
        // If this successor is the incoming block for this
210
2
        // Phi operand, then this successor does lead to the Phi.
211
2
        return true;
212
1.13k
      if (OpndIncomingBB == BI->getParent() && 
Succ == PhiDef->getParent()1.13k
)
213
592
        // Otherwise, if the edge is directly from the branch
214
592
        // to the Phi, this successor is the one feeding this
215
592
        // Phi operand.
216
592
        return true;
217
546
      return false;
218
546
    };
219
594
220
594
    if (IsOpndComingFromSuccessor(BI->getSuccessor(1)))
221
48
      BI->setMetadata(
222
48
          LLVMContext::MD_prof,
223
48
          MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight));
224
546
    else if (IsOpndComingFromSuccessor(BI->getSuccessor(0)))
225
546
      BI->setMetadata(
226
546
          LLVMContext::MD_prof,
227
546
          MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight));
228
594
  }
229
703
}
230
231
// Handle both BranchInst and SelectInst.
232
1.03M
template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) {
233
1.03M
234
1.03M
  // Handle non-optimized IR code like:
235
1.03M
  //   %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
236
1.03M
  //   %tobool = icmp ne i64 %expval, 0
237
1.03M
  //   br i1 %tobool, label %if.then, label %if.end
238
1.03M
  //
239
1.03M
  // Or the following simpler case:
240
1.03M
  //   %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
241
1.03M
  //   br i1 %expval, label %if.then, label %if.end
242
1.03M
243
1.03M
  CallInst *CI;
244
1.03M
245
1.03M
  ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition());
246
1.03M
  CmpInst::Predicate Predicate;
247
1.03M
  ConstantInt *CmpConstOperand = nullptr;
248
1.03M
  if (!CmpI) {
249
163k
    CI = dyn_cast<CallInst>(BSI.getCondition());
250
163k
    Predicate = CmpInst::ICMP_NE;
251
869k
  } else {
252
869k
    Predicate = CmpI->getPredicate();
253
869k
    if (Predicate != CmpInst::ICMP_NE && 
Predicate != CmpInst::ICMP_EQ678k
)
254
264k
      return false;
255
605k
256
605k
    CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1));
257
605k
    if (!CmpConstOperand)
258
299k
      return false;
259
306k
    CI = dyn_cast<CallInst>(CmpI->getOperand(0));
260
306k
  }
261
1.03M
262
1.03M
  
if (469k
!CI469k
)
263
343k
    return false;
264
126k
265
126k
  uint64_t ValueComparedTo = 0;
266
126k
  if (CmpConstOperand) {
267
82.0k
    if (CmpConstOperand->getBitWidth() > 64)
268
0
      return false;
269
82.0k
    ValueComparedTo = CmpConstOperand->getZExtValue();
270
82.0k
  }
271
126k
272
126k
  Function *Fn = CI->getCalledFunction();
273
126k
  if (!Fn || 
Fn->getIntrinsicID() != Intrinsic::expect112k
)
274
105k
    return false;
275
21.4k
276
21.4k
  Value *ArgValue = CI->getArgOperand(0);
277
21.4k
  ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
278
21.4k
  if (!ExpectedValue)
279
0
    return false;
280
21.4k
281
21.4k
  MDBuilder MDB(CI->getContext());
282
21.4k
  MDNode *Node;
283
21.4k
284
21.4k
  if ((ExpectedValue->getZExtValue() == ValueComparedTo) ==
285
21.4k
      (Predicate == CmpInst::ICMP_EQ))
286
532
    Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
287
20.8k
  else
288
20.8k
    Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
289
21.4k
290
21.4k
  BSI.setMetadata(LLVMContext::MD_prof, Node);
291
21.4k
292
21.4k
  if (CmpI)
293
21.4k
    CmpI->setOperand(0, ArgValue);
294
2
  else
295
2
    BSI.setCondition(ArgValue);
296
21.4k
  return true;
297
21.4k
}
LowerExpectIntrinsic.cpp:bool handleBrSelExpect<llvm::BranchInst>(llvm::BranchInst&)
Line
Count
Source
232
975k
template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) {
233
975k
234
975k
  // Handle non-optimized IR code like:
235
975k
  //   %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
236
975k
  //   %tobool = icmp ne i64 %expval, 0
237
975k
  //   br i1 %tobool, label %if.then, label %if.end
238
975k
  //
239
975k
  // Or the following simpler case:
240
975k
  //   %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
241
975k
  //   br i1 %expval, label %if.then, label %if.end
242
975k
243
975k
  CallInst *CI;
244
975k
245
975k
  ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition());
246
975k
  CmpInst::Predicate Predicate;
247
975k
  ConstantInt *CmpConstOperand = nullptr;
248
975k
  if (!CmpI) {
249
156k
    CI = dyn_cast<CallInst>(BSI.getCondition());
250
156k
    Predicate = CmpInst::ICMP_NE;
251
819k
  } else {
252
819k
    Predicate = CmpI->getPredicate();
253
819k
    if (Predicate != CmpInst::ICMP_NE && 
Predicate != CmpInst::ICMP_EQ629k
)
254
231k
      return false;
255
588k
256
588k
    CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1));
257
588k
    if (!CmpConstOperand)
258
297k
      return false;
259
290k
    CI = dyn_cast<CallInst>(CmpI->getOperand(0));
260
290k
  }
261
975k
262
975k
  
if (447k
!CI447k
)
263
328k
    return false;
264
118k
265
118k
  uint64_t ValueComparedTo = 0;
266
118k
  if (CmpConstOperand) {
267
74.8k
    if (CmpConstOperand->getBitWidth() > 64)
268
0
      return false;
269
74.8k
    ValueComparedTo = CmpConstOperand->getZExtValue();
270
74.8k
  }
271
118k
272
118k
  Function *Fn = CI->getCalledFunction();
273
118k
  if (!Fn || 
Fn->getIntrinsicID() != Intrinsic::expect111k
)
274
97.3k
    return false;
275
21.4k
276
21.4k
  Value *ArgValue = CI->getArgOperand(0);
277
21.4k
  ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
278
21.4k
  if (!ExpectedValue)
279
0
    return false;
280
21.4k
281
21.4k
  MDBuilder MDB(CI->getContext());
282
21.4k
  MDNode *Node;
283
21.4k
284
21.4k
  if ((ExpectedValue->getZExtValue() == ValueComparedTo) ==
285
21.4k
      (Predicate == CmpInst::ICMP_EQ))
286
532
    Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
287
20.8k
  else
288
20.8k
    Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
289
21.4k
290
21.4k
  BSI.setMetadata(LLVMContext::MD_prof, Node);
291
21.4k
292
21.4k
  if (CmpI)
293
21.4k
    CmpI->setOperand(0, ArgValue);
294
2
  else
295
2
    BSI.setCondition(ArgValue);
296
21.4k
  return true;
297
21.4k
}
LowerExpectIntrinsic.cpp:bool handleBrSelExpect<llvm::SelectInst>(llvm::SelectInst&)
Line
Count
Source
232
57.6k
template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) {
233
57.6k
234
57.6k
  // Handle non-optimized IR code like:
235
57.6k
  //   %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
236
57.6k
  //   %tobool = icmp ne i64 %expval, 0
237
57.6k
  //   br i1 %tobool, label %if.then, label %if.end
238
57.6k
  //
239
57.6k
  // Or the following simpler case:
240
57.6k
  //   %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
241
57.6k
  //   br i1 %expval, label %if.then, label %if.end
242
57.6k
243
57.6k
  CallInst *CI;
244
57.6k
245
57.6k
  ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition());
246
57.6k
  CmpInst::Predicate Predicate;
247
57.6k
  ConstantInt *CmpConstOperand = nullptr;
248
57.6k
  if (!CmpI) {
249
7.01k
    CI = dyn_cast<CallInst>(BSI.getCondition());
250
7.01k
    Predicate = CmpInst::ICMP_NE;
251
50.6k
  } else {
252
50.6k
    Predicate = CmpI->getPredicate();
253
50.6k
    if (Predicate != CmpInst::ICMP_NE && 
Predicate != CmpInst::ICMP_EQ48.7k
)
254
33.2k
      return false;
255
17.3k
256
17.3k
    CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1));
257
17.3k
    if (!CmpConstOperand)
258
2.03k
      return false;
259
15.3k
    CI = dyn_cast<CallInst>(CmpI->getOperand(0));
260
15.3k
  }
261
57.6k
262
57.6k
  
if (22.3k
!CI22.3k
)
263
14.5k
    return false;
264
7.81k
265
7.81k
  uint64_t ValueComparedTo = 0;
266
7.81k
  if (CmpConstOperand) {
267
7.19k
    if (CmpConstOperand->getBitWidth() > 64)
268
0
      return false;
269
7.19k
    ValueComparedTo = CmpConstOperand->getZExtValue();
270
7.19k
  }
271
7.81k
272
7.81k
  Function *Fn = CI->getCalledFunction();
273
7.81k
  if (!Fn || 
Fn->getIntrinsicID() != Intrinsic::expect797
)
274
7.80k
    return false;
275
2
276
2
  Value *ArgValue = CI->getArgOperand(0);
277
2
  ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
278
2
  if (!ExpectedValue)
279
0
    return false;
280
2
281
2
  MDBuilder MDB(CI->getContext());
282
2
  MDNode *Node;
283
2
284
2
  if ((ExpectedValue->getZExtValue() == ValueComparedTo) ==
285
2
      (Predicate == CmpInst::ICMP_EQ))
286
0
    Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
287
2
  else
288
2
    Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
289
2
290
2
  BSI.setMetadata(LLVMContext::MD_prof, Node);
291
2
292
2
  if (CmpI)
293
2
    CmpI->setOperand(0, ArgValue);
294
0
  else
295
0
    BSI.setCondition(ArgValue);
296
2
  return true;
297
2
}
298
299
1.75M
static bool handleBranchExpect(BranchInst &BI) {
300
1.75M
  if (BI.isUnconditional())
301
779k
    return false;
302
975k
303
975k
  return handleBrSelExpect<BranchInst>(BI);
304
975k
}
305
306
589k
static bool lowerExpectIntrinsic(Function &F) {
307
589k
  bool Changed = false;
308
589k
309
2.42M
  for (BasicBlock &BB : F) {
310
2.42M
    // Create "block_weights" metadata.
311
2.42M
    if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
312
1.75M
      if (handleBranchExpect(*BI))
313
21.4k
        ExpectIntrinsicsHandled++;
314
1.75M
    } else 
if (SwitchInst *665k
SI665k
= dyn_cast<SwitchInst>(BB.getTerminator())) {
315
13.3k
      if (handleSwitchExpect(*SI))
316
4
        ExpectIntrinsicsHandled++;
317
13.3k
    }
318
2.42M
319
2.42M
    // Remove llvm.expect intrinsics. Iterate backwards in order
320
2.42M
    // to process select instructions before the intrinsic gets
321
2.42M
    // removed.
322
15.5M
    for (auto BI = BB.rbegin(), BE = BB.rend(); BI != BE;) {
323
13.0M
      Instruction *Inst = &*BI++;
324
13.0M
      CallInst *CI = dyn_cast<CallInst>(Inst);
325
13.0M
      if (!CI) {
326
10.8M
        if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) {
327
57.6k
          if (handleBrSelExpect(*SI))
328
2
            ExpectIntrinsicsHandled++;
329
57.6k
        }
330
10.8M
        continue;
331
10.8M
      }
332
2.20M
333
2.20M
      Function *Fn = CI->getCalledFunction();
334
2.20M
      if (Fn && 
Fn->getIntrinsicID() == Intrinsic::expect2.14M
) {
335
21.4k
        // Before erasing the llvm.expect, walk backward to find
336
21.4k
        // phi that define llvm.expect's first arg, and
337
21.4k
        // infer branch probability:
338
21.4k
        handlePhiDef(CI);
339
21.4k
        Value *Exp = CI->getArgOperand(0);
340
21.4k
        CI->replaceAllUsesWith(Exp);
341
21.4k
        CI->eraseFromParent();
342
21.4k
        Changed = true;
343
21.4k
      }
344
2.20M
    }
345
2.42M
  }
346
589k
347
589k
  return Changed;
348
589k
}
349
350
PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F,
351
1.19k
                                                FunctionAnalysisManager &) {
352
1.19k
  if (lowerExpectIntrinsic(F))
353
28
    return PreservedAnalyses::none();
354
1.16k
355
1.16k
  return PreservedAnalyses::all();
356
1.16k
}
357
358
namespace {
359
/// Legacy pass for lowering expect intrinsics out of the IR.
360
///
361
/// When this pass is run over a function it uses expect intrinsics which feed
362
/// branches and switches to provide branch weight metadata for those
363
/// terminators. It then removes the expect intrinsics from the IR so the rest
364
/// of the optimizer can ignore them.
365
class LowerExpectIntrinsic : public FunctionPass {
366
public:
367
  static char ID;
368
12.6k
  LowerExpectIntrinsic() : FunctionPass(ID) {
369
12.6k
    initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
370
12.6k
  }
371
372
588k
  bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); }
373
};
374
}
375
376
char LowerExpectIntrinsic::ID = 0;
377
INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
378
                "Lower 'expect' Intrinsics", false, false)
379
380
12.6k
FunctionPass *llvm::createLowerExpectIntrinsicPass() {
381
12.6k
  return new LowerExpectIntrinsic();
382
12.6k
}