33 #define DEBUG_TYPE "lower-expect-intrinsic"
36 "Number of 'expect' intrinsic instructions handled");
52 cl::desc(
"Weight of the branch likely to be taken (default = 2000)"));
55 cl::desc(
"Weight of the branch unlikely to be taken (default = 1)"));
113 while (!isa<PHINode>(V)) {
114 if (
ZExtInst *ZExt = dyn_cast<ZExtInst>(V)) {
115 V = ZExt->getOperand(0);
120 if (
SExtInst *SExt = dyn_cast<SExtInst>(V)) {
121 V = SExt->getOperand(0);
127 if (!BinOp || BinOp->
getOpcode() != Instruction::Xor)
139 auto ApplyOperations = [&](
const APInt &
Value) {
142 switch (
Op->getOpcode()) {
143 case Instruction::Xor:
144 Result ^= cast<ConstantInt>(
Op->getOperand(1))->getValue();
146 case Instruction::ZExt:
147 Result = Result.
zext(
Op->getType()->getIntegerBitWidth());
149 case Instruction::SExt:
150 Result = Result.
sext(
Op->getType()->getIntegerBitWidth());
163 auto GetDomConditional = [&](
unsigned i) ->
BranchInst * {
180 for (
unsigned i = 0, e = PhiDef->getNumIncomingValues(); i != e; ++i) {
182 Value *PhiOpnd = PhiDef->getIncomingValue(i);
190 if (ExpectedPhiValue == ApplyOperations(CI->
getValue()))
207 auto *OpndIncomingBB = PhiDef->getIncomingBlock(i);
208 auto IsOpndComingFromSuccessor = [&](
BasicBlock *Succ) {
209 if (OpndIncomingBB == Succ)
225 else if (IsOpndComingFromSuccessor(BI->
getSuccessor(0)))
258 if (!CmpConstOperand)
266 uint64_t ValueComparedTo = 0;
267 if (CmpConstOperand) {
285 if ((ExpectedValue->
getZExtValue() == ValueComparedTo) ==
296 BSI.setCondition(ArgValue);
304 return handleBrSelExpect<BranchInst>(BI);
308 bool Changed =
false;
312 if (
BranchInst *BI = dyn_cast<BranchInst>(BB.getTerminator())) {
314 ExpectIntrinsicsHandled++;
315 }
else if (
SwitchInst *
SI = dyn_cast<SwitchInst>(BB.getTerminator())) {
317 ExpectIntrinsicsHandled++;
323 for (
auto BI = BB.rbegin(), BE = BB.rend(); BI != BE;) {
329 ExpectIntrinsicsHandled++;
379 "Lower 'expect' Intrinsics",
false,
false)
382 return new LowerExpectIntrinsic();
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
INITIALIZE_PASS(LowerExpectIntrinsic,"lower-expect","Lower 'expect' Intrinsics", false, false) FunctionPass *llvm
unsigned getBitWidth() const
getBitWidth - Return the bitwidth of this constant.
This class represents zero extension of integer types.
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
This class represents a function call, abstracting a target machine's calling convention.
unsigned getCaseIndex() const
Returns number of current case.
const Function * getParent() const
Return the enclosing method, or null if none.
This class represents a sign extension of integer types.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Run the pass over the function.
bool isUnconditional() const
This class represents the LLVM 'select' instruction.
const APInt & getValue() const
Return the constant as an APInt value reference.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
static bool lowerExpectIntrinsic(Function &F)
BasicBlock * getSuccessor(unsigned i) const
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
static cl::opt< uint32_t > UnlikelyBranchWeight("unlikely-branch-weight", cl::Hidden, cl::init(1), cl::desc("Weight of the branch unlikely to be taken (default = 1)"))
initializer< Ty > init(const Ty &Val)
A set of analyses that are preserved following a run of a transformation pass.
void initializeLowerExpectIntrinsicPass(PassRegistry &)
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
APInt sext(unsigned width) const
Sign extend to a new width.
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
Predicate getPredicate() const
Return the predicate for this instruction.
static cl::opt< uint32_t > LikelyBranchWeight("likely-branch-weight", cl::Hidden, cl::init(2000), cl::desc("Weight of the branch likely to be taken (default = 2000)"))
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
LLVMContext & getContext() const
All values hold a context through their type.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
bool isConditional() const
BinaryOps getOpcode() const
static bool handleBrSelExpect(BrSelInst &BSI)
This is the shared class of boolean and integer constants.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Function * getCalledFunction() const
Return the function called, or null if this is an indirect function invocation.
CaseIt findCaseValue(const ConstantInt *C)
Search all of the case values for the specified constant.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
void setOperand(unsigned i, Value *Val)
Value * getArgOperand(unsigned i) const
getArgOperand/setArgOperand - Return/set the i-th call argument.
Class for arbitrary precision integers.
static void handlePhiDef(CallInst *Expect)
Handler for PHINodes that define the value argument to an .expect call.
static bool handleBranchExpect(BranchInst &BI)
Value * getCondition() const
llvm_unreachable("Unknown RegBank!")
void setCondition(Value *V)
The header file for the LowerExpectIntrinsic pass as used by the new pass manager.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
CaseIt case_default()
Returns an iterator that points to the default case.
unsigned getNumCases() const
Return the number of 'cases' in this switch instruction, excluding the default case.
LLVM Value Representation.
APInt zext(unsigned width) const
Zero extend to a new width.
A container for analyses that lazily runs them and caches their results.
const TerminatorInst * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static bool handleSwitchExpect(SwitchInst &SI)
const BasicBlock * getParent() const
FunctionPass * createLowerExpectIntrinsicPass()