32 #define DEBUG_TYPE "div-rem-pairs" 33 STATISTIC(NumPairs,
"Number of div/rem pairs");
34 STATISTIC(NumRecomposed,
"Number of instructions recomposed");
35 STATISTIC(NumHoisted,
"Number of instructions hoisted");
36 STATISTIC(NumDecomposed,
"Number of instructions decomposed");
38 "Controls transformations in div-rem-pairs pass");
41 struct ExpandedMatch {
52 Value *Dividend, *XroundedDownToMultipleOfY;
60 XroundedDownToMultipleOfY,
67 M.Key.SignedOp = Div->
getOpcode() == Instruction::SDiv;
68 M.Key.Dividend = Dividend;
69 M.Key.Divisor = Divisor;
85 : DivInst(DivInst_), RemInst(RemInst_) {
87 DivInst->
getOpcode() == Instruction::SDiv) &&
106 case Instruction::SRem:
107 case Instruction::URem:
130 if (
I.getOpcode() == Instruction::SDiv)
132 else if (
I.getOpcode() == Instruction::UDiv)
134 else if (
I.getOpcode() == Instruction::SRem)
136 else if (
I.getOpcode() == Instruction::URem)
149 for (
auto &RemPair : RemMap) {
182 bool Changed =
false;
193 bool HasDivRemOp = TTI.
hasDivRemOp(
E.getType(),
E.isSigned());
195 auto &DivInst =
E.DivInst;
196 auto &RemInst =
E.RemInst;
198 const bool RemOriginallyWasInExpandedForm =
E.isRemExpanded();
199 (void)RemOriginallyWasInExpandedForm;
201 if (HasDivRemOp &&
E.isRemExpanded()) {
206 Instruction *RealRem =
E.isSigned() ? BinaryOperator::CreateSRem(X, Y)
207 : BinaryOperator::CreateURem(X, Y);
210 RealRem->
setName(RemInst->getName() +
".recomposed");
223 assert((!
E.isRemExpanded() || !HasDivRemOp) &&
224 "*If* the target supports div-rem, then by now the RemInst *is* " 225 "Instruction::[US]Rem.");
230 if (HasDivRemOp && RemInst->getParent() == DivInst->getParent())
233 bool DivDominates = DT.
dominates(DivInst, RemInst);
234 if (!DivDominates && !DT.
dominates(RemInst, DivInst)) {
243 if (!HasDivRemOp &&
E.isRemExpanded())
250 RemInst->moveAfter(DivInst);
252 DivInst->moveAfter(RemInst);
260 assert(!RemOriginallyWasInExpandedForm &&
261 "We should not be expanding if the rem was in expanded form to " 267 Instruction *Sub = BinaryOperator::CreateSub(X, Mul);
300 DivInst->moveBefore(RemInst);
306 Sub->
setName(RemInst->getName() +
".decomposed");
342 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
343 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
351 "Hoist/decompose integer division and remainder",
false,
358 return new DivRemPairsLegacyPass();
Legacy wrapper pass to provide the GlobalsAAResult object.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
reference emplace_back(ArgTypes &&... Args)
Type * getType() const
The type for this pair, identical for both the div and rem.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI, const DominatorTree &DT)
Find matching pairs of integer div/rem ops (they have the same numerator, denominator, and signedness).
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents lattice values for constants.
This is the interface for a simple mod/ref and alias analysis over globals.
Analysis pass providing the TargetTransformInfo.
void initializeDivRemPairsLegacyPassPass(PassRegistry &)
This class implements a map that also provides access to all stored values in a deterministic order...
STATISTIC(NumFunctions, "Total number of functions")
Analysis pass which computes a DominatorTree.
INITIALIZE_PASS_BEGIN(DivRemPairsLegacyPass, "div-rem-pairs", "Hoist/decompose integer division and remainder", false, false) INITIALIZE_PASS_END(DivRemPairsLegacyPass
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool match(Val *V, const Pattern &P)
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
AssertingVH< Instruction > RemInst
The instruction that we have matched as a remainder instruction.
void setName(const Twine &Name)
Change the name of the value.
Value * getDivisor() const
This file provides an implementation of debug counters.
div rem Hoist decompose integer division and remainder
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Type * getType() const
All values are typed, get the type of this value.
A thin wrapper to store two values that we matched as div-rem pair.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Value * getOperand(unsigned i) const
static bool runOnFunction(Function &F, bool PostInlining)
A set of analyses that are preserved following a run of a transformation pass.
AssertingVH< Instruction > DivInst
The actual udiv/sdiv instruction. Source of truth.
The instances of the Type class are immutable: once they are created, they are never changed...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static DivRemWorklistTy getWorklist(Function &F)
Find matching pairs of integer div/rem ops (they have the same numerator, denominator, and signedness).
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
FunctionPass * createDivRemPairsPass()
Represent the analysis usage information of a pass.
Analysis pass providing a never-invalidated alias analysis result.
FunctionPass class - This class is used to implement most global optimizations.
static bool shouldExecute(unsigned CounterName)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
bool isRemExpanded() const
deferredval_ty< Value > m_Deferred(Value *const &V)
A commutative-friendly version of m_Specific().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
static llvm::Optional< ExpandedMatch > matchExpandedRem(Instruction &I)
See if we can match: (which is the form we expand into) X - ((X ?/ Y) * Y) which is equivalent to: X ...
DivRemPairWorklistEntry(Instruction *DivInst_, Instruction *RemInst_)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
DEBUG_COUNTER(DRPCounter, "div-rem-pairs-transform", "Controls transformations in div-rem-pairs pass")
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Value * getDividend() const
In this pair, what are the divident and divisor?
void preserveSet()
Mark an analysis set as preserved.
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
void preserve()
Mark an analysis as preserved.
bool isSigned() const
Is this pair signed or unsigned?
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
static BinaryOperator * CreateMul(Value *S1, Value *S2, const Twine &Name, Instruction *InsertBefore, Value *FlagsOp)
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.