31#define DEBUG_TYPE "lower-expect-intrinsic"
34 "Number of 'expect' intrinsic instructions handled");
48 if (IntrinsicID == Intrinsic::expect) {
50 return std::make_tuple(Opts.likely_branch_weight,
51 Opts.unlikely_branch_weight);
55 "expect with probability must have 3 arguments");
57 double TrueProb = Confidence->getValueAPF().convertToDouble();
58 assert((TrueProb >= 0.0 && TrueProb <= 1.0) &&
59 "probability value must be in the range [0.0, 1.0]");
60 double FalseProb = (1.0 - TrueProb) / (BranchCount - 1);
61 uint32_t LikelyBW = ceil((TrueProb * (
double)(INT32_MAX - 1)) + 1.0);
62 uint32_t UnlikelyBW = ceil((FalseProb * (
double)(INT32_MAX - 1)) + 1.0);
63 return std::make_tuple(LikelyBW, UnlikelyBW);
83 unsigned n =
SI.getNumCases();
84 uint32_t LikelyBranchWeightVal, UnlikelyBranchWeightVal;
85 std::tie(LikelyBranchWeightVal, UnlikelyBranchWeightVal) =
91 Weights[Index] = LikelyBranchWeightVal;
95 SI.setCondition(ArgValue);
113 bool ExpectedValueIsLikely =
true;
121 double TrueProb = Confidence->getValueAPF().convertToDouble();
122 ExpectedValueIsLikely = (TrueProb > 0.5);
139 V = ZExt->getOperand(0);
145 V = SExt->getOperand(0);
151 if (!BinOp || BinOp->
getOpcode() != Instruction::Xor)
163 auto ApplyOperations = [&](
const APInt &
Value) {
166 switch (
Op->getOpcode()) {
167 case Instruction::Xor:
170 case Instruction::ZExt:
171 Result = Result.zext(
Op->getType()->getIntegerBitWidth());
173 case Instruction::SExt:
174 Result = Result.sext(
Op->getType()->getIntegerBitWidth());
187 auto GetDomConditional = [&](
unsigned i) ->
CondBrInst * {
200 for (
unsigned i = 0, e = PhiDef->getNumIncomingValues(); i != e; ++i) {
202 Value *PhiOpnd = PhiDef->getIncomingValue(i);
212 const APInt &CurrentPhiValue = ApplyOperations(CI->
getValue());
213 if (ExpectedValueIsLikely == (ExpectedPhiValue == CurrentPhiValue))
230 auto *OpndIncomingBB = PhiDef->getIncomingBlock(i);
231 auto IsOpndComingFromSuccessor = [&](
BasicBlock *Succ) {
232 if (OpndIncomingBB == Succ)
236 if (OpndIncomingBB == BI->
getParent() && Succ == PhiDef->getParent())
243 uint32_t LikelyBranchWeightVal, UnlikelyBranchWeightVal;
244 std::tie(LikelyBranchWeightVal, UnlikelyBranchWeightVal) =
getBranchWeight(
246 if (!ExpectedValueIsLikely)
247 std::swap(LikelyBranchWeightVal, UnlikelyBranchWeightVal);
252 UnlikelyBranchWeightVal,
254 else if (IsOpndComingFromSuccessor(BI->
getSuccessor(0)))
257 LikelyBranchWeightVal,
263template <
class BrSelInst>
289 if (!CmpConstOperand)
298 if (CmpConstOperand) {
317 uint32_t LikelyBranchWeightVal, UnlikelyBranchWeightVal;
318 std::tie(LikelyBranchWeightVal, UnlikelyBranchWeightVal) =
322 if ((ExpectedValue->
getZExtValue() == ValueComparedTo) ==
325 LikelyBranchWeightVal, UnlikelyBranchWeightVal,
true);
326 ExpectedWeights = {LikelyBranchWeightVal, UnlikelyBranchWeightVal};
329 LikelyBranchWeightVal,
true);
330 ExpectedWeights = {UnlikelyBranchWeightVal, LikelyBranchWeightVal};
336 BSI.setCondition(ArgValue);
340 BSI.setMetadata(LLVMContext::MD_prof,
Node);
353 ExpectIntrinsicsHandled++;
356 ExpectIntrinsicsHandled++;
367 ExpectIntrinsicsHandled++;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static void handlePhiDef(const ScalarOptions &Opts, CallInst *Expect)
Handler for PHINodes that define the value argument to an @llvm.expect call.
static bool handleBrSelExpect(const ScalarOptions &Opts, BrSelInst &BSI)
static bool handleSwitchExpect(const ScalarOptions &Opts, SwitchInst &SI)
static bool lowerExpectIntrinsic(Function &F)
static std::tuple< uint32_t, uint32_t > getBranchWeight(const ScalarOptions &Opts, Intrinsic::ID IntrinsicID, CallInst *CI, int BranchCount)
The header file for the LowerExpectIntrinsic pass as used by the new pass manager.
This file contains the declarations for profiling metadata utility functions.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Class for arbitrary precision integers.
LLVM Basic Block Representation.
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
BinaryOps getOpcode() const
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Value * getArgOperand(unsigned i) const
This class represents a function call, abstracting a target machine's calling convention.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Predicate getPredicate() const
Return the predicate for this instruction.
Conditional Branch instruction.
BasicBlock * getSuccessor(unsigned i) const
This is the shared class of boolean and integer constants.
unsigned getBitWidth() const
getBitWidth - Return the scalar bitwidth of this constant.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
const APInt & getValue() const
Return the constant as an APInt value reference.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
This instruction compares its operands according to the predicate given to the constructor.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
LLVM_ABI MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
This class represents a sign extension of integer types.
This class represents the LLVM 'select' instruction.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
unsigned getCaseIndex() const
Returns number of current case.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
unsigned getNumOperands() const
LLVM Value Representation.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVMContext & getContext() const
All values hold a context through their type.
This class represents zero extension of integer types.
const ParentTy * getParent() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI void checkExpectAnnotations(const Instruction &I, ArrayRef< uint32_t > ExistingWeights, bool IsFrontend)
checkExpectAnnotations - compares PGO counters to the thresholds used for llvm.expect and warns if th...
LLVM_ABI void checkFrontendInstrumentation(const Instruction &I, ArrayRef< uint32_t > ExpectedWeights)
checkFrontendInstrumentation - compares PGO counters to the thresholds used for llvm....
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
const ScalarOptions & getScalarOptions(const LLVMContext &C)
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected, bool ElideAllZero=false)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
auto reverse(ContainerTy &&C)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Run the pass over the function.