172793727SChandler Carruth //===- LowerExpectIntrinsic.cpp - Lower expect intrinsic ------------------===//
272793727SChandler Carruth //
372793727SChandler Carruth //                     The LLVM Compiler Infrastructure
472793727SChandler Carruth //
572793727SChandler Carruth // This file is distributed under the University of Illinois Open Source
672793727SChandler Carruth // License. See LICENSE.TXT for details.
772793727SChandler Carruth //
872793727SChandler Carruth //===----------------------------------------------------------------------===//
972793727SChandler Carruth //
1072793727SChandler Carruth // This pass lowers the 'expect' intrinsic to LLVM metadata.
1172793727SChandler Carruth //
1272793727SChandler Carruth //===----------------------------------------------------------------------===//
1372793727SChandler Carruth 
1443e590e5SChandler Carruth #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
153f5e7b1fSChandler Carruth #include "llvm/ADT/SmallVector.h"
1672793727SChandler Carruth #include "llvm/ADT/Statistic.h"
17*621e8dcfSXinliang David Li #include "llvm/ADT/iterator_range.h"
1872793727SChandler Carruth #include "llvm/IR/BasicBlock.h"
1972793727SChandler Carruth #include "llvm/IR/Constants.h"
2072793727SChandler Carruth #include "llvm/IR/Function.h"
2172793727SChandler Carruth #include "llvm/IR/Instructions.h"
2272793727SChandler Carruth #include "llvm/IR/Intrinsics.h"
2372793727SChandler Carruth #include "llvm/IR/LLVMContext.h"
2472793727SChandler Carruth #include "llvm/IR/MDBuilder.h"
2572793727SChandler Carruth #include "llvm/IR/Metadata.h"
2672793727SChandler Carruth #include "llvm/Pass.h"
2772793727SChandler Carruth #include "llvm/Support/CommandLine.h"
2872793727SChandler Carruth #include "llvm/Support/Debug.h"
2943e590e5SChandler Carruth #include "llvm/Transforms/Scalar.h"
3072793727SChandler Carruth 
3172793727SChandler Carruth using namespace llvm;
3272793727SChandler Carruth 
3372793727SChandler Carruth #define DEBUG_TYPE "lower-expect-intrinsic"
3472793727SChandler Carruth 
356eb60eb5SChandler Carruth STATISTIC(ExpectIntrinsicsHandled,
366eb60eb5SChandler Carruth           "Number of 'expect' intrinsic instructions handled");
3772793727SChandler Carruth 
38d2d2aa52SSanjay Patel // These default values are chosen to represent an extremely skewed outcome for
39d2d2aa52SSanjay Patel // a condition, but they leave some room for interpretation by later passes.
40d2d2aa52SSanjay Patel //
41d2d2aa52SSanjay Patel // If the documentation for __builtin_expect() was made explicit that it should
42d2d2aa52SSanjay Patel // only be used in extreme cases, we could make this ratio higher. As it stands,
43d2d2aa52SSanjay Patel // programmers may be using __builtin_expect() / llvm.expect to annotate that a
44d2d2aa52SSanjay Patel // branch is likely or unlikely to be taken.
45d2d2aa52SSanjay Patel //
46d2d2aa52SSanjay Patel // There is a known dependency on this ratio in CodeGenPrepare when transforming
47d2d2aa52SSanjay Patel // 'select' instructions. It may be worthwhile to hoist these values to some
48d2d2aa52SSanjay Patel // shared space, so they can be used directly by other passes.
49d2d2aa52SSanjay Patel 
50d2d2aa52SSanjay Patel static cl::opt<uint32_t> LikelyBranchWeight(
51d2d2aa52SSanjay Patel     "likely-branch-weight", cl::Hidden, cl::init(2000),
52d2d2aa52SSanjay Patel     cl::desc("Weight of the branch likely to be taken (default = 2000)"));
53d2d2aa52SSanjay Patel static cl::opt<uint32_t> UnlikelyBranchWeight(
54d2d2aa52SSanjay Patel     "unlikely-branch-weight", cl::Hidden, cl::init(1),
55d2d2aa52SSanjay Patel     cl::desc("Weight of the branch unlikely to be taken (default = 1)"));
5672793727SChandler Carruth 
570012c778SChandler Carruth static bool handleSwitchExpect(SwitchInst &SI) {
580012c778SChandler Carruth   CallInst *CI = dyn_cast<CallInst>(SI.getCondition());
5972793727SChandler Carruth   if (!CI)
6072793727SChandler Carruth     return false;
6172793727SChandler Carruth 
6272793727SChandler Carruth   Function *Fn = CI->getCalledFunction();
6372793727SChandler Carruth   if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
6472793727SChandler Carruth     return false;
6572793727SChandler Carruth 
6672793727SChandler Carruth   Value *ArgValue = CI->getArgOperand(0);
6772793727SChandler Carruth   ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
6872793727SChandler Carruth   if (!ExpectedValue)
6972793727SChandler Carruth     return false;
7072793727SChandler Carruth 
71927d8e61SChandler Carruth   SwitchInst::CaseHandle Case = *SI.findCaseValue(ExpectedValue);
720012c778SChandler Carruth   unsigned n = SI.getNumCases(); // +1 for default case.
733f5e7b1fSChandler Carruth   SmallVector<uint32_t, 16> Weights(n + 1, UnlikelyBranchWeight);
7472793727SChandler Carruth 
75927d8e61SChandler Carruth   if (Case == *SI.case_default())
763f5e7b1fSChandler Carruth     Weights[0] = LikelyBranchWeight;
773f5e7b1fSChandler Carruth   else
783f5e7b1fSChandler Carruth     Weights[Case.getCaseIndex() + 1] = LikelyBranchWeight;
7972793727SChandler Carruth 
800012c778SChandler Carruth   SI.setMetadata(LLVMContext::MD_prof,
8172793727SChandler Carruth                  MDBuilder(CI->getContext()).createBranchWeights(Weights));
8272793727SChandler Carruth 
830012c778SChandler Carruth   SI.setCondition(ArgValue);
8472793727SChandler Carruth   return true;
8572793727SChandler Carruth }
8672793727SChandler Carruth 
87*621e8dcfSXinliang David Li /// Handler for PHINodes that define the value argument to an
88*621e8dcfSXinliang David Li /// @llvm.expect call.
89*621e8dcfSXinliang David Li ///
90*621e8dcfSXinliang David Li /// If the operand of the phi has a constant value and it 'contradicts'
91*621e8dcfSXinliang David Li /// with the expected value of phi def, then the corresponding incoming
92*621e8dcfSXinliang David Li /// edge of the phi is unlikely to be taken. Using that information,
93*621e8dcfSXinliang David Li /// the branch probability info for the originating branch can be inferred.
94*621e8dcfSXinliang David Li static void handlePhiDef(CallInst *Expect) {
95*621e8dcfSXinliang David Li   Value &Arg = *Expect->getArgOperand(0);
96*621e8dcfSXinliang David Li   ConstantInt *ExpectedValue = cast<ConstantInt>(Expect->getArgOperand(1));
97*621e8dcfSXinliang David Li   const APInt &ExpectedPhiValue = ExpectedValue->getValue();
98*621e8dcfSXinliang David Li 
99*621e8dcfSXinliang David Li   // Walk up in backward a list of instructions that
100*621e8dcfSXinliang David Li   // have 'copy' semantics by 'stripping' the copies
101*621e8dcfSXinliang David Li   // until a PHI node or an instruction of unknown kind
102*621e8dcfSXinliang David Li   // is reached. Negation via xor is also handled.
103*621e8dcfSXinliang David Li   //
104*621e8dcfSXinliang David Li   //       C = PHI(...);
105*621e8dcfSXinliang David Li   //       B = C;
106*621e8dcfSXinliang David Li   //       A = B;
107*621e8dcfSXinliang David Li   //       D = __builtin_expect(A, 0);
108*621e8dcfSXinliang David Li   //
109*621e8dcfSXinliang David Li   Value *V = &Arg;
110*621e8dcfSXinliang David Li   SmallVector<Instruction *, 4> Operations;
111*621e8dcfSXinliang David Li   while (!isa<PHINode>(V)) {
112*621e8dcfSXinliang David Li     if (ZExtInst *ZExt = dyn_cast<ZExtInst>(V)) {
113*621e8dcfSXinliang David Li       V = ZExt->getOperand(0);
114*621e8dcfSXinliang David Li       Operations.push_back(ZExt);
115*621e8dcfSXinliang David Li       continue;
116*621e8dcfSXinliang David Li     }
117*621e8dcfSXinliang David Li 
118*621e8dcfSXinliang David Li     if (SExtInst *SExt = dyn_cast<SExtInst>(V)) {
119*621e8dcfSXinliang David Li       V = SExt->getOperand(0);
120*621e8dcfSXinliang David Li       Operations.push_back(SExt);
121*621e8dcfSXinliang David Li       continue;
122*621e8dcfSXinliang David Li     }
123*621e8dcfSXinliang David Li 
124*621e8dcfSXinliang David Li     BinaryOperator *BinOp = dyn_cast<BinaryOperator>(V);
125*621e8dcfSXinliang David Li     if (!BinOp || BinOp->getOpcode() != Instruction::Xor)
126*621e8dcfSXinliang David Li       return;
127*621e8dcfSXinliang David Li 
128*621e8dcfSXinliang David Li     ConstantInt *CInt = dyn_cast<ConstantInt>(BinOp->getOperand(1));
129*621e8dcfSXinliang David Li     if (!CInt)
130*621e8dcfSXinliang David Li       return;
131*621e8dcfSXinliang David Li 
132*621e8dcfSXinliang David Li     V = BinOp->getOperand(0);
133*621e8dcfSXinliang David Li     Operations.push_back(BinOp);
134*621e8dcfSXinliang David Li   }
135*621e8dcfSXinliang David Li 
136*621e8dcfSXinliang David Li   // Executes the recorded operations on input 'Value'.
137*621e8dcfSXinliang David Li   auto ApplyOperations = [&](const APInt &Value) {
138*621e8dcfSXinliang David Li     APInt Result = Value;
139*621e8dcfSXinliang David Li     for (auto Op : llvm::reverse(Operations)) {
140*621e8dcfSXinliang David Li       switch (Op->getOpcode()) {
141*621e8dcfSXinliang David Li       case Instruction::Xor:
142*621e8dcfSXinliang David Li         Result ^= cast<ConstantInt>(Op->getOperand(1))->getValue();
143*621e8dcfSXinliang David Li         break;
144*621e8dcfSXinliang David Li       case Instruction::ZExt:
145*621e8dcfSXinliang David Li         Result = Result.zext(Op->getType()->getIntegerBitWidth());
146*621e8dcfSXinliang David Li         break;
147*621e8dcfSXinliang David Li       case Instruction::SExt:
148*621e8dcfSXinliang David Li         Result = Result.sext(Op->getType()->getIntegerBitWidth());
149*621e8dcfSXinliang David Li         break;
150*621e8dcfSXinliang David Li       default:
151*621e8dcfSXinliang David Li         llvm_unreachable("Unexpected operation");
152*621e8dcfSXinliang David Li       }
153*621e8dcfSXinliang David Li     }
154*621e8dcfSXinliang David Li     return Result;
155*621e8dcfSXinliang David Li   };
156*621e8dcfSXinliang David Li 
157*621e8dcfSXinliang David Li   auto *PhiDef = dyn_cast<PHINode>(V);
158*621e8dcfSXinliang David Li 
159*621e8dcfSXinliang David Li   // Get the first dominating conditional branch of the operand
160*621e8dcfSXinliang David Li   // i's incoming block.
161*621e8dcfSXinliang David Li   auto GetDomConditional = [&](unsigned i) -> BranchInst * {
162*621e8dcfSXinliang David Li     BasicBlock *BB = PhiDef->getIncomingBlock(i);
163*621e8dcfSXinliang David Li     BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
164*621e8dcfSXinliang David Li     if (BI && BI->isConditional())
165*621e8dcfSXinliang David Li       return BI;
166*621e8dcfSXinliang David Li     BB = BB->getSinglePredecessor();
167*621e8dcfSXinliang David Li     if (!BB)
168*621e8dcfSXinliang David Li       return nullptr;
169*621e8dcfSXinliang David Li     BI = dyn_cast<BranchInst>(BB->getTerminator());
170*621e8dcfSXinliang David Li     if (!BI || BI->isUnconditional())
171*621e8dcfSXinliang David Li       return nullptr;
172*621e8dcfSXinliang David Li     return BI;
173*621e8dcfSXinliang David Li   };
174*621e8dcfSXinliang David Li 
175*621e8dcfSXinliang David Li   // Now walk through all Phi operands to find phi oprerands with values
176*621e8dcfSXinliang David Li   // conflicting with the expected phi output value. Any such operand
177*621e8dcfSXinliang David Li   // indicates the incoming edge to that operand is unlikely.
178*621e8dcfSXinliang David Li   for (unsigned i = 0, e = PhiDef->getNumIncomingValues(); i != e; ++i) {
179*621e8dcfSXinliang David Li 
180*621e8dcfSXinliang David Li     Value *PhiOpnd = PhiDef->getIncomingValue(i);
181*621e8dcfSXinliang David Li     ConstantInt *CI = dyn_cast<ConstantInt>(PhiOpnd);
182*621e8dcfSXinliang David Li     if (!CI)
183*621e8dcfSXinliang David Li       continue;
184*621e8dcfSXinliang David Li 
185*621e8dcfSXinliang David Li     // Not an interesting case when IsUnlikely is false -- we can not infer
186*621e8dcfSXinliang David Li     // anything useful when the operand value matches the expected phi
187*621e8dcfSXinliang David Li     // output.
188*621e8dcfSXinliang David Li     if (ExpectedPhiValue == ApplyOperations(CI->getValue()))
189*621e8dcfSXinliang David Li       continue;
190*621e8dcfSXinliang David Li 
191*621e8dcfSXinliang David Li     BranchInst *BI = GetDomConditional(i);
192*621e8dcfSXinliang David Li     if (!BI)
193*621e8dcfSXinliang David Li       continue;
194*621e8dcfSXinliang David Li 
195*621e8dcfSXinliang David Li     MDBuilder MDB(PhiDef->getContext());
196*621e8dcfSXinliang David Li 
197*621e8dcfSXinliang David Li     // There are two situations in which an operand of the PhiDef comes
198*621e8dcfSXinliang David Li     // from a given successor of a branch instruction BI.
199*621e8dcfSXinliang David Li     // 1) When the incoming block of the operand is the successor block;
200*621e8dcfSXinliang David Li     // 2) When the incoming block is BI's enclosing block and the
201*621e8dcfSXinliang David Li     // successor is the PhiDef's enclosing block.
202*621e8dcfSXinliang David Li     //
203*621e8dcfSXinliang David Li     // Returns true if the operand which comes from OpndIncomingBB
204*621e8dcfSXinliang David Li     // comes from outgoing edge of BI that leads to Succ block.
205*621e8dcfSXinliang David Li     auto *OpndIncomingBB = PhiDef->getIncomingBlock(i);
206*621e8dcfSXinliang David Li     auto IsOpndComingFromSuccessor = [&](BasicBlock *Succ) {
207*621e8dcfSXinliang David Li       if (OpndIncomingBB == Succ)
208*621e8dcfSXinliang David Li         // If this successor is the incoming block for this
209*621e8dcfSXinliang David Li         // Phi operand, then this successor does lead to the Phi.
210*621e8dcfSXinliang David Li         return true;
211*621e8dcfSXinliang David Li       if (OpndIncomingBB == BI->getParent() && Succ == PhiDef->getParent())
212*621e8dcfSXinliang David Li         // Otherwise, if the edge is directly from the branch
213*621e8dcfSXinliang David Li         // to the Phi, this successor is the one feeding this
214*621e8dcfSXinliang David Li         // Phi operand.
215*621e8dcfSXinliang David Li         return true;
216*621e8dcfSXinliang David Li       return false;
217*621e8dcfSXinliang David Li     };
218*621e8dcfSXinliang David Li 
219*621e8dcfSXinliang David Li     if (IsOpndComingFromSuccessor(BI->getSuccessor(1)))
220*621e8dcfSXinliang David Li       BI->setMetadata(
221*621e8dcfSXinliang David Li           LLVMContext::MD_prof,
222*621e8dcfSXinliang David Li           MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight));
223*621e8dcfSXinliang David Li     else if (IsOpndComingFromSuccessor(BI->getSuccessor(0)))
224*621e8dcfSXinliang David Li       BI->setMetadata(
225*621e8dcfSXinliang David Li           LLVMContext::MD_prof,
226*621e8dcfSXinliang David Li           MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight));
227*621e8dcfSXinliang David Li   }
228*621e8dcfSXinliang David Li }
229*621e8dcfSXinliang David Li 
23040afd5c9SXinliang David Li // Handle both BranchInst and SelectInst.
23140afd5c9SXinliang David Li template <class BrSelInst> static bool handleBrSelExpect(BrSelInst &BSI) {
23272793727SChandler Carruth 
23372793727SChandler Carruth   // Handle non-optimized IR code like:
23472793727SChandler Carruth   //   %expval = call i64 @llvm.expect.i64(i64 %conv1, i64 1)
23572793727SChandler Carruth   //   %tobool = icmp ne i64 %expval, 0
23672793727SChandler Carruth   //   br i1 %tobool, label %if.then, label %if.end
23772793727SChandler Carruth   //
23872793727SChandler Carruth   // Or the following simpler case:
23972793727SChandler Carruth   //   %expval = call i1 @llvm.expect.i1(i1 %cmp, i1 1)
24072793727SChandler Carruth   //   br i1 %expval, label %if.then, label %if.end
24172793727SChandler Carruth 
24272793727SChandler Carruth   CallInst *CI;
24372793727SChandler Carruth 
24440afd5c9SXinliang David Li   ICmpInst *CmpI = dyn_cast<ICmpInst>(BSI.getCondition());
245ee8d6acbSXinliang David Li   CmpInst::Predicate Predicate;
246d6cfba2aSXinliang David Li   ConstantInt *CmpConstOperand = nullptr;
24772793727SChandler Carruth   if (!CmpI) {
24840afd5c9SXinliang David Li     CI = dyn_cast<CallInst>(BSI.getCondition());
249ee8d6acbSXinliang David Li     Predicate = CmpInst::ICMP_NE;
25072793727SChandler Carruth   } else {
251ee8d6acbSXinliang David Li     Predicate = CmpI->getPredicate();
252ee8d6acbSXinliang David Li     if (Predicate != CmpInst::ICMP_NE && Predicate != CmpInst::ICMP_EQ)
25372793727SChandler Carruth       return false;
254d6cfba2aSXinliang David Li 
255d6cfba2aSXinliang David Li     CmpConstOperand = dyn_cast<ConstantInt>(CmpI->getOperand(1));
256ee8d6acbSXinliang David Li     if (!CmpConstOperand)
257ee8d6acbSXinliang David Li       return false;
25872793727SChandler Carruth     CI = dyn_cast<CallInst>(CmpI->getOperand(0));
25972793727SChandler Carruth   }
26072793727SChandler Carruth 
26172793727SChandler Carruth   if (!CI)
26272793727SChandler Carruth     return false;
26372793727SChandler Carruth 
264d6cfba2aSXinliang David Li   uint64_t ValueComparedTo = 0;
265d6cfba2aSXinliang David Li   if (CmpConstOperand) {
266d6cfba2aSXinliang David Li     if (CmpConstOperand->getBitWidth() > 64)
267d6cfba2aSXinliang David Li       return false;
268d6cfba2aSXinliang David Li     ValueComparedTo = CmpConstOperand->getZExtValue();
269d6cfba2aSXinliang David Li   }
270d6cfba2aSXinliang David Li 
27172793727SChandler Carruth   Function *Fn = CI->getCalledFunction();
27272793727SChandler Carruth   if (!Fn || Fn->getIntrinsicID() != Intrinsic::expect)
27372793727SChandler Carruth     return false;
27472793727SChandler Carruth 
27572793727SChandler Carruth   Value *ArgValue = CI->getArgOperand(0);
27672793727SChandler Carruth   ConstantInt *ExpectedValue = dyn_cast<ConstantInt>(CI->getArgOperand(1));
27772793727SChandler Carruth   if (!ExpectedValue)
27872793727SChandler Carruth     return false;
27972793727SChandler Carruth 
28072793727SChandler Carruth   MDBuilder MDB(CI->getContext());
28172793727SChandler Carruth   MDNode *Node;
28272793727SChandler Carruth 
283ee8d6acbSXinliang David Li   if ((ExpectedValue->getZExtValue() == ValueComparedTo) ==
284ee8d6acbSXinliang David Li       (Predicate == CmpInst::ICMP_EQ))
28572793727SChandler Carruth     Node = MDB.createBranchWeights(LikelyBranchWeight, UnlikelyBranchWeight);
28672793727SChandler Carruth   else
28772793727SChandler Carruth     Node = MDB.createBranchWeights(UnlikelyBranchWeight, LikelyBranchWeight);
28872793727SChandler Carruth 
28940afd5c9SXinliang David Li   BSI.setMetadata(LLVMContext::MD_prof, Node);
29072793727SChandler Carruth 
29172793727SChandler Carruth   if (CmpI)
29272793727SChandler Carruth     CmpI->setOperand(0, ArgValue);
29372793727SChandler Carruth   else
29440afd5c9SXinliang David Li     BSI.setCondition(ArgValue);
29572793727SChandler Carruth   return true;
29672793727SChandler Carruth }
29772793727SChandler Carruth 
29840afd5c9SXinliang David Li static bool handleBranchExpect(BranchInst &BI) {
29940afd5c9SXinliang David Li   if (BI.isUnconditional())
30040afd5c9SXinliang David Li     return false;
30140afd5c9SXinliang David Li 
30240afd5c9SXinliang David Li   return handleBrSelExpect<BranchInst>(BI);
30340afd5c9SXinliang David Li }
30440afd5c9SXinliang David Li 
30543e590e5SChandler Carruth static bool lowerExpectIntrinsic(Function &F) {
306c3bf5bd8SChandler Carruth   bool Changed = false;
307c3bf5bd8SChandler Carruth 
308d12741e0SChandler Carruth   for (BasicBlock &BB : F) {
30972793727SChandler Carruth     // Create "block_weights" metadata.
310d12741e0SChandler Carruth     if (BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
3110012c778SChandler Carruth       if (handleBranchExpect(*BI))
3126eb60eb5SChandler Carruth         ExpectIntrinsicsHandled++;
313d12741e0SChandler Carruth     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
3140012c778SChandler Carruth       if (handleSwitchExpect(*SI))
3156eb60eb5SChandler Carruth         ExpectIntrinsicsHandled++;
31672793727SChandler Carruth     }
31772793727SChandler Carruth 
31840afd5c9SXinliang David Li     // Remove llvm.expect intrinsics. Iterate backwards in order
31940afd5c9SXinliang David Li     // to process select instructions before the intrinsic gets
32040afd5c9SXinliang David Li     // removed.
32140afd5c9SXinliang David Li     for (auto BI = BB.rbegin(), BE = BB.rend(); BI != BE;) {
32240afd5c9SXinliang David Li       Instruction *Inst = &*BI++;
32340afd5c9SXinliang David Li       CallInst *CI = dyn_cast<CallInst>(Inst);
32440afd5c9SXinliang David Li       if (!CI) {
32540afd5c9SXinliang David Li         if (SelectInst *SI = dyn_cast<SelectInst>(Inst)) {
32640afd5c9SXinliang David Li           if (handleBrSelExpect(*SI))
32740afd5c9SXinliang David Li             ExpectIntrinsicsHandled++;
32840afd5c9SXinliang David Li         }
32972793727SChandler Carruth         continue;
33040afd5c9SXinliang David Li       }
33172793727SChandler Carruth 
33272793727SChandler Carruth       Function *Fn = CI->getCalledFunction();
33372793727SChandler Carruth       if (Fn && Fn->getIntrinsicID() == Intrinsic::expect) {
334*621e8dcfSXinliang David Li         // Before erasing the llvm.expect, walk backward to find
335*621e8dcfSXinliang David Li         // phi that define llvm.expect's first arg, and
336*621e8dcfSXinliang David Li         // infer branch probability:
337*621e8dcfSXinliang David Li         handlePhiDef(CI);
33872793727SChandler Carruth         Value *Exp = CI->getArgOperand(0);
33972793727SChandler Carruth         CI->replaceAllUsesWith(Exp);
34072793727SChandler Carruth         CI->eraseFromParent();
341c3bf5bd8SChandler Carruth         Changed = true;
34272793727SChandler Carruth       }
34372793727SChandler Carruth     }
34472793727SChandler Carruth   }
34572793727SChandler Carruth 
346c3bf5bd8SChandler Carruth   return Changed;
34772793727SChandler Carruth }
34872793727SChandler Carruth 
349164a2aa6SChandler Carruth PreservedAnalyses LowerExpectIntrinsicPass::run(Function &F,
350164a2aa6SChandler Carruth                                                 FunctionAnalysisManager &) {
35143e590e5SChandler Carruth   if (lowerExpectIntrinsic(F))
35243e590e5SChandler Carruth     return PreservedAnalyses::none();
35343e590e5SChandler Carruth 
35443e590e5SChandler Carruth   return PreservedAnalyses::all();
35543e590e5SChandler Carruth }
35643e590e5SChandler Carruth 
35743e590e5SChandler Carruth namespace {
35843e590e5SChandler Carruth /// \brief Legacy pass for lowering expect intrinsics out of the IR.
35943e590e5SChandler Carruth ///
36043e590e5SChandler Carruth /// When this pass is run over a function it uses expect intrinsics which feed
36143e590e5SChandler Carruth /// branches and switches to provide branch weight metadata for those
36243e590e5SChandler Carruth /// terminators. It then removes the expect intrinsics from the IR so the rest
36343e590e5SChandler Carruth /// of the optimizer can ignore them.
36443e590e5SChandler Carruth class LowerExpectIntrinsic : public FunctionPass {
36543e590e5SChandler Carruth public:
36643e590e5SChandler Carruth   static char ID;
36743e590e5SChandler Carruth   LowerExpectIntrinsic() : FunctionPass(ID) {
36843e590e5SChandler Carruth     initializeLowerExpectIntrinsicPass(*PassRegistry::getPassRegistry());
36943e590e5SChandler Carruth   }
37043e590e5SChandler Carruth 
37143e590e5SChandler Carruth   bool runOnFunction(Function &F) override { return lowerExpectIntrinsic(F); }
37243e590e5SChandler Carruth };
373f00654e3SAlexander Kornienko }
37443e590e5SChandler Carruth 
37572793727SChandler Carruth char LowerExpectIntrinsic::ID = 0;
3760ea746bfSChandler Carruth INITIALIZE_PASS(LowerExpectIntrinsic, "lower-expect",
3770ea746bfSChandler Carruth                 "Lower 'expect' Intrinsics", false, false)
37872793727SChandler Carruth 
37972793727SChandler Carruth FunctionPass *llvm::createLowerExpectIntrinsicPass() {
38072793727SChandler Carruth   return new LowerExpectIntrinsic();
38172793727SChandler Carruth }
382