1*a686c60cSRoman Lebedev //===- DivRemPairs.cpp - Hoist/[dr]ecompose division and remainder --------===// 26fd4391dSSanjay Patel // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 66fd4391dSSanjay Patel // 76fd4391dSSanjay Patel //===----------------------------------------------------------------------===// 86fd4391dSSanjay Patel // 9*a686c60cSRoman Lebedev // This pass hoists and/or decomposes/recomposes integer division and remainder 106fd4391dSSanjay Patel // instructions to enable CFG improvements and better codegen. 116fd4391dSSanjay Patel // 126fd4391dSSanjay Patel //===----------------------------------------------------------------------===// 136fd4391dSSanjay Patel 146fd4391dSSanjay Patel #include "llvm/Transforms/Scalar/DivRemPairs.h" 152af5f3c1SGeoff Berry #include "llvm/ADT/DenseMap.h" 162af5f3c1SGeoff Berry #include "llvm/ADT/MapVector.h" 176fd4391dSSanjay Patel #include "llvm/ADT/Statistic.h" 186fd4391dSSanjay Patel #include "llvm/Analysis/GlobalsModRef.h" 196fd4391dSSanjay Patel #include "llvm/Analysis/TargetTransformInfo.h" 206fd4391dSSanjay Patel #include "llvm/IR/Dominators.h" 216fd4391dSSanjay Patel #include "llvm/IR/Function.h" 22*a686c60cSRoman Lebedev #include "llvm/IR/PatternMatch.h" 236fd4391dSSanjay Patel #include "llvm/Pass.h" 24213d1d23SGeorge Burgess IV #include "llvm/Support/DebugCounter.h" 256fd4391dSSanjay Patel #include "llvm/Transforms/Scalar.h" 266fd4391dSSanjay Patel #include "llvm/Transforms/Utils/BypassSlowDivision.h" 275f616901SRoman Lebedev 286fd4391dSSanjay Patel using namespace llvm; 29*a686c60cSRoman Lebedev using namespace llvm::PatternMatch; 306fd4391dSSanjay Patel 316fd4391dSSanjay Patel #define DEBUG_TYPE "div-rem-pairs" 326fd4391dSSanjay Patel STATISTIC(NumPairs, "Number of div/rem pairs"); 33*a686c60cSRoman Lebedev STATISTIC(NumRecomposed, "Number of instructions recomposed"); 346fd4391dSSanjay Patel STATISTIC(NumHoisted, "Number of instructions hoisted"); 356fd4391dSSanjay Patel STATISTIC(NumDecomposed, "Number of instructions decomposed"); 36213d1d23SGeorge Burgess IV DEBUG_COUNTER(DRPCounter, "div-rem-pairs-transform", 37213d1d23SGeorge Burgess IV "Controls transformations in div-rem-pairs pass"); 386fd4391dSSanjay Patel 39*a686c60cSRoman Lebedev namespace { 40*a686c60cSRoman Lebedev struct ExpandedMatch { 41*a686c60cSRoman Lebedev DivRemMapKey Key; 42*a686c60cSRoman Lebedev Instruction *Value; 43*a686c60cSRoman Lebedev }; 44*a686c60cSRoman Lebedev } // namespace 45*a686c60cSRoman Lebedev 46*a686c60cSRoman Lebedev /// See if we can match: (which is the form we expand into) 47*a686c60cSRoman Lebedev /// X - ((X ?/ Y) * Y) 48*a686c60cSRoman Lebedev /// which is equivalent to: 49*a686c60cSRoman Lebedev /// X ?% Y 50*a686c60cSRoman Lebedev static llvm::Optional<ExpandedMatch> matchExpandedRem(Instruction &I) { 51*a686c60cSRoman Lebedev Value *Dividend, *XroundedDownToMultipleOfY; 52*a686c60cSRoman Lebedev if (!match(&I, m_Sub(m_Value(Dividend), m_Value(XroundedDownToMultipleOfY)))) 53*a686c60cSRoman Lebedev return llvm::None; 54*a686c60cSRoman Lebedev 55*a686c60cSRoman Lebedev Value *Divisor; 56*a686c60cSRoman Lebedev Instruction *Div; 57*a686c60cSRoman Lebedev // Look for ((X / Y) * Y) 58*a686c60cSRoman Lebedev if (!match( 59*a686c60cSRoman Lebedev XroundedDownToMultipleOfY, 60*a686c60cSRoman Lebedev m_c_Mul(m_CombineAnd(m_IDiv(m_Specific(Dividend), m_Value(Divisor)), 61*a686c60cSRoman Lebedev m_Instruction(Div)), 62*a686c60cSRoman Lebedev m_Deferred(Divisor)))) 63*a686c60cSRoman Lebedev return llvm::None; 64*a686c60cSRoman Lebedev 65*a686c60cSRoman Lebedev ExpandedMatch M; 66*a686c60cSRoman Lebedev M.Key.SignedOp = Div->getOpcode() == Instruction::SDiv; 67*a686c60cSRoman Lebedev M.Key.Dividend = Dividend; 68*a686c60cSRoman Lebedev M.Key.Divisor = Divisor; 69*a686c60cSRoman Lebedev M.Value = &I; 70*a686c60cSRoman Lebedev return M; 71*a686c60cSRoman Lebedev } 72*a686c60cSRoman Lebedev 735f616901SRoman Lebedev /// A thin wrapper to store two values that we matched as div-rem pair. 745f616901SRoman Lebedev /// We want this extra indirection to avoid dealing with RAUW'ing the map keys. 755f616901SRoman Lebedev struct DivRemPairWorklistEntry { 765f616901SRoman Lebedev /// The actual udiv/sdiv instruction. Source of truth. 775f616901SRoman Lebedev AssertingVH<Instruction> DivInst; 786fd4391dSSanjay Patel 795f616901SRoman Lebedev /// The instruction that we have matched as a remainder instruction. 805f616901SRoman Lebedev /// Should only be used as Value, don't introspect it. 815f616901SRoman Lebedev AssertingVH<Instruction> RemInst; 825f616901SRoman Lebedev 835f616901SRoman Lebedev DivRemPairWorklistEntry(Instruction *DivInst_, Instruction *RemInst_) 845f616901SRoman Lebedev : DivInst(DivInst_), RemInst(RemInst_) { 855f616901SRoman Lebedev assert((DivInst->getOpcode() == Instruction::UDiv || 865f616901SRoman Lebedev DivInst->getOpcode() == Instruction::SDiv) && 875f616901SRoman Lebedev "Not a division."); 885f616901SRoman Lebedev assert(DivInst->getType() == RemInst->getType() && "Types should match."); 895f616901SRoman Lebedev // We can't check anything else about remainder instruction, 905f616901SRoman Lebedev // it's not strictly required to be a urem/srem. 915f616901SRoman Lebedev } 925f616901SRoman Lebedev 935f616901SRoman Lebedev /// The type for this pair, identical for both the div and rem. 945f616901SRoman Lebedev Type *getType() const { return DivInst->getType(); } 955f616901SRoman Lebedev 965f616901SRoman Lebedev /// Is this pair signed or unsigned? 975f616901SRoman Lebedev bool isSigned() const { return DivInst->getOpcode() == Instruction::SDiv; } 985f616901SRoman Lebedev 995f616901SRoman Lebedev /// In this pair, what are the divident and divisor? 1005f616901SRoman Lebedev Value *getDividend() const { return DivInst->getOperand(0); } 1015f616901SRoman Lebedev Value *getDivisor() const { return DivInst->getOperand(1); } 102*a686c60cSRoman Lebedev 103*a686c60cSRoman Lebedev bool isRemExpanded() const { 104*a686c60cSRoman Lebedev switch (RemInst->getOpcode()) { 105*a686c60cSRoman Lebedev case Instruction::SRem: 106*a686c60cSRoman Lebedev case Instruction::URem: 107*a686c60cSRoman Lebedev return false; // single 'rem' instruction - unexpanded form. 108*a686c60cSRoman Lebedev default: 109*a686c60cSRoman Lebedev return true; // anything else means we have remainder in expanded form. 110*a686c60cSRoman Lebedev } 111*a686c60cSRoman Lebedev } 1125f616901SRoman Lebedev }; 1135f616901SRoman Lebedev using DivRemWorklistTy = SmallVector<DivRemPairWorklistEntry, 4>; 1145f616901SRoman Lebedev 1155f616901SRoman Lebedev /// Find matching pairs of integer div/rem ops (they have the same numerator, 1165f616901SRoman Lebedev /// denominator, and signedness). Place those pairs into a worklist for further 1175f616901SRoman Lebedev /// processing. This indirection is needed because we have to use TrackingVH<> 1185f616901SRoman Lebedev /// because we will be doing RAUW, and if one of the rem instructions we change 1195f616901SRoman Lebedev /// happens to be an input to another div/rem in the maps, we'd have problems. 1205f616901SRoman Lebedev static DivRemWorklistTy getWorklist(Function &F) { 1216fd4391dSSanjay Patel // Insert all divide and remainder instructions into maps keyed by their 1226fd4391dSSanjay Patel // operands and opcode (signed or unsigned). 1232af5f3c1SGeoff Berry DenseMap<DivRemMapKey, Instruction *> DivMap; 1242af5f3c1SGeoff Berry // Use a MapVector for RemMap so that instructions are moved/inserted in a 1252af5f3c1SGeoff Berry // deterministic order. 1268e0cf076SRoman Lebedev MapVector<DivRemMapKey, Instruction *> RemMap; 1276fd4391dSSanjay Patel for (auto &BB : F) { 1286fd4391dSSanjay Patel for (auto &I : BB) { 1296fd4391dSSanjay Patel if (I.getOpcode() == Instruction::SDiv) 1306fd4391dSSanjay Patel DivMap[DivRemMapKey(true, I.getOperand(0), I.getOperand(1))] = &I; 1316fd4391dSSanjay Patel else if (I.getOpcode() == Instruction::UDiv) 1326fd4391dSSanjay Patel DivMap[DivRemMapKey(false, I.getOperand(0), I.getOperand(1))] = &I; 1336fd4391dSSanjay Patel else if (I.getOpcode() == Instruction::SRem) 1348e0cf076SRoman Lebedev RemMap[DivRemMapKey(true, I.getOperand(0), I.getOperand(1))] = &I; 1356fd4391dSSanjay Patel else if (I.getOpcode() == Instruction::URem) 1368e0cf076SRoman Lebedev RemMap[DivRemMapKey(false, I.getOperand(0), I.getOperand(1))] = &I; 137*a686c60cSRoman Lebedev else if (auto Match = matchExpandedRem(I)) 138*a686c60cSRoman Lebedev RemMap[Match->Key] = Match->Value; 1396fd4391dSSanjay Patel } 1406fd4391dSSanjay Patel } 1416fd4391dSSanjay Patel 1425f616901SRoman Lebedev // We'll accumulate the matching pairs of div-rem instructions here. 1435f616901SRoman Lebedev DivRemWorklistTy Worklist; 1445f616901SRoman Lebedev 1456fd4391dSSanjay Patel // We can iterate over either map because we are only looking for matched 1466fd4391dSSanjay Patel // pairs. Choose remainders for efficiency because they are usually even more 1476fd4391dSSanjay Patel // rare than division. 1486fd4391dSSanjay Patel for (auto &RemPair : RemMap) { 1496fd4391dSSanjay Patel // Find the matching division instruction from the division map. 1502af5f3c1SGeoff Berry Instruction *DivInst = DivMap[RemPair.first]; 1516fd4391dSSanjay Patel if (!DivInst) 1526fd4391dSSanjay Patel continue; 1536fd4391dSSanjay Patel 1545f616901SRoman Lebedev // We have a matching pair of div/rem instructions. 1556fd4391dSSanjay Patel NumPairs++; 1568e0cf076SRoman Lebedev Instruction *RemInst = RemPair.second; 1575f616901SRoman Lebedev 1585f616901SRoman Lebedev // Place it in the worklist. 1595f616901SRoman Lebedev Worklist.emplace_back(DivInst, RemInst); 1605f616901SRoman Lebedev } 1615f616901SRoman Lebedev 1625f616901SRoman Lebedev return Worklist; 1635f616901SRoman Lebedev } 1645f616901SRoman Lebedev 1655f616901SRoman Lebedev /// Find matching pairs of integer div/rem ops (they have the same numerator, 1665f616901SRoman Lebedev /// denominator, and signedness). If they exist in different basic blocks, bring 1675f616901SRoman Lebedev /// them together by hoisting or replace the common division operation that is 1685f616901SRoman Lebedev /// implicit in the remainder: 1695f616901SRoman Lebedev /// X % Y <--> X - ((X / Y) * Y). 1705f616901SRoman Lebedev /// 1715f616901SRoman Lebedev /// We can largely ignore the normal safety and cost constraints on speculation 1725f616901SRoman Lebedev /// of these ops when we find a matching pair. This is because we are already 1735f616901SRoman Lebedev /// guaranteed that any exceptions and most cost are already incurred by the 1745f616901SRoman Lebedev /// first member of the pair. 1755f616901SRoman Lebedev /// 1765f616901SRoman Lebedev /// Note: This transform could be an oddball enhancement to EarlyCSE, GVN, or 1775f616901SRoman Lebedev /// SimplifyCFG, but it's split off on its own because it's different enough 1785f616901SRoman Lebedev /// that it doesn't quite match the stated objectives of those passes. 1795f616901SRoman Lebedev static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI, 1805f616901SRoman Lebedev const DominatorTree &DT) { 1815f616901SRoman Lebedev bool Changed = false; 1825f616901SRoman Lebedev 1835f616901SRoman Lebedev // Get the matching pairs of div-rem instructions. We want this extra 1845f616901SRoman Lebedev // indirection to avoid dealing with having to RAUW the keys of the maps. 1855f616901SRoman Lebedev DivRemWorklistTy Worklist = getWorklist(F); 1865f616901SRoman Lebedev 1875f616901SRoman Lebedev // Process each entry in the worklist. 1885f616901SRoman Lebedev for (DivRemPairWorklistEntry &E : Worklist) { 189*a686c60cSRoman Lebedev if (!DebugCounter::shouldExecute(DRPCounter)) 190*a686c60cSRoman Lebedev continue; 191*a686c60cSRoman Lebedev 1925f616901SRoman Lebedev bool HasDivRemOp = TTI.hasDivRemOp(E.getType(), E.isSigned()); 1935f616901SRoman Lebedev 1945f616901SRoman Lebedev auto &DivInst = E.DivInst; 1955f616901SRoman Lebedev auto &RemInst = E.RemInst; 1966fd4391dSSanjay Patel 197*a686c60cSRoman Lebedev const bool RemOriginallyWasInExpandedForm = E.isRemExpanded(); 198*a686c60cSRoman Lebedev 199*a686c60cSRoman Lebedev if (HasDivRemOp && E.isRemExpanded()) { 200*a686c60cSRoman Lebedev // The target supports div+rem but the rem is expanded. 201*a686c60cSRoman Lebedev // We should recompose it first. 202*a686c60cSRoman Lebedev Value *X = E.getDividend(); 203*a686c60cSRoman Lebedev Value *Y = E.getDivisor(); 204*a686c60cSRoman Lebedev Instruction *RealRem = E.isSigned() ? BinaryOperator::CreateSRem(X, Y) 205*a686c60cSRoman Lebedev : BinaryOperator::CreateURem(X, Y); 206*a686c60cSRoman Lebedev // Note that we place it right next to the original expanded instruction, 207*a686c60cSRoman Lebedev // and letting further handling to move it if needed. 208*a686c60cSRoman Lebedev RealRem->setName(RemInst->getName() + ".recomposed"); 209*a686c60cSRoman Lebedev RealRem->insertAfter(RemInst); 210*a686c60cSRoman Lebedev Instruction *OrigRemInst = RemInst; 211*a686c60cSRoman Lebedev // Update AssertingVH<> with new instruction so it doesn't assert. 212*a686c60cSRoman Lebedev RemInst = RealRem; 213*a686c60cSRoman Lebedev // And replace the original instruction with the new one. 214*a686c60cSRoman Lebedev OrigRemInst->replaceAllUsesWith(RealRem); 215*a686c60cSRoman Lebedev OrigRemInst->eraseFromParent(); 216*a686c60cSRoman Lebedev NumRecomposed++; 217*a686c60cSRoman Lebedev // Note that we have left ((X / Y) * Y) around. 218*a686c60cSRoman Lebedev // If it had other uses we could rewrite it as X - X % Y 219*a686c60cSRoman Lebedev } 220*a686c60cSRoman Lebedev 221*a686c60cSRoman Lebedev assert((!E.isRemExpanded() || !HasDivRemOp) && 222*a686c60cSRoman Lebedev "*If* the target supports div-rem, then by now the RemInst *is* " 223*a686c60cSRoman Lebedev "Instruction::[US]Rem."); 224*a686c60cSRoman Lebedev 2256fd4391dSSanjay Patel // If the target supports div+rem and the instructions are in the same block 2266fd4391dSSanjay Patel // already, there's nothing to do. The backend should handle this. If the 2276fd4391dSSanjay Patel // target does not support div+rem, then we will decompose the rem. 2286fd4391dSSanjay Patel if (HasDivRemOp && RemInst->getParent() == DivInst->getParent()) 2296fd4391dSSanjay Patel continue; 2306fd4391dSSanjay Patel 2316fd4391dSSanjay Patel bool DivDominates = DT.dominates(DivInst, RemInst); 232*a686c60cSRoman Lebedev if (!DivDominates && !DT.dominates(RemInst, DivInst)) { 233*a686c60cSRoman Lebedev // We have matching div-rem pair, but they are in two different blocks, 234*a686c60cSRoman Lebedev // neither of which dominates one another. 235*a686c60cSRoman Lebedev assert(!RemOriginallyWasInExpandedForm && 236*a686c60cSRoman Lebedev "Won't happen for expanded-form rem."); 237*a686c60cSRoman Lebedev // FIXME: We could hoist both ops to the common predecessor block? 2386fd4391dSSanjay Patel continue; 239*a686c60cSRoman Lebedev } 2406fd4391dSSanjay Patel 241*a686c60cSRoman Lebedev // The target does not have a single div/rem operation, 242*a686c60cSRoman Lebedev // and the rem is already in expanded form. Nothing to do. 243*a686c60cSRoman Lebedev if (!HasDivRemOp && E.isRemExpanded()) 244213d1d23SGeorge Burgess IV continue; 245213d1d23SGeorge Burgess IV 2466fd4391dSSanjay Patel if (HasDivRemOp) { 2476fd4391dSSanjay Patel // The target has a single div/rem operation. Hoist the lower instruction 2486fd4391dSSanjay Patel // to make the matched pair visible to the backend. 2496fd4391dSSanjay Patel if (DivDominates) 2506fd4391dSSanjay Patel RemInst->moveAfter(DivInst); 2516fd4391dSSanjay Patel else 2526fd4391dSSanjay Patel DivInst->moveAfter(RemInst); 2536fd4391dSSanjay Patel NumHoisted++; 2546fd4391dSSanjay Patel } else { 255*a686c60cSRoman Lebedev // The target does not have a single div/rem operation, 256*a686c60cSRoman Lebedev // and the rem is *not* in a already-expanded form. 257*a686c60cSRoman Lebedev // Decompose the remainder calculation as: 2586fd4391dSSanjay Patel // X % Y --> X - ((X / Y) * Y). 259*a686c60cSRoman Lebedev 260*a686c60cSRoman Lebedev assert(!RemOriginallyWasInExpandedForm && 261*a686c60cSRoman Lebedev "We should not be expanding if the rem was in expanded form to " 262*a686c60cSRoman Lebedev "begin with."); 263*a686c60cSRoman Lebedev 2645f616901SRoman Lebedev Value *X = E.getDividend(); 2655f616901SRoman Lebedev Value *Y = E.getDivisor(); 2666fd4391dSSanjay Patel Instruction *Mul = BinaryOperator::CreateMul(DivInst, Y); 2676fd4391dSSanjay Patel Instruction *Sub = BinaryOperator::CreateSub(X, Mul); 2686fd4391dSSanjay Patel 2696fd4391dSSanjay Patel // If the remainder dominates, then hoist the division up to that block: 2706fd4391dSSanjay Patel // 2716fd4391dSSanjay Patel // bb1: 2726fd4391dSSanjay Patel // %rem = srem %x, %y 2736fd4391dSSanjay Patel // bb2: 2746fd4391dSSanjay Patel // %div = sdiv %x, %y 2756fd4391dSSanjay Patel // --> 2766fd4391dSSanjay Patel // bb1: 2776fd4391dSSanjay Patel // %div = sdiv %x, %y 2786fd4391dSSanjay Patel // %mul = mul %div, %y 2796fd4391dSSanjay Patel // %rem = sub %x, %mul 2806fd4391dSSanjay Patel // 2816fd4391dSSanjay Patel // If the division dominates, it's already in the right place. The mul+sub 2826fd4391dSSanjay Patel // will be in a different block because we don't assume that they are 2836fd4391dSSanjay Patel // cheap to speculatively execute: 2846fd4391dSSanjay Patel // 2856fd4391dSSanjay Patel // bb1: 2866fd4391dSSanjay Patel // %div = sdiv %x, %y 2876fd4391dSSanjay Patel // bb2: 2886fd4391dSSanjay Patel // %rem = srem %x, %y 2896fd4391dSSanjay Patel // --> 2906fd4391dSSanjay Patel // bb1: 2916fd4391dSSanjay Patel // %div = sdiv %x, %y 2926fd4391dSSanjay Patel // bb2: 2936fd4391dSSanjay Patel // %mul = mul %div, %y 2946fd4391dSSanjay Patel // %rem = sub %x, %mul 2956fd4391dSSanjay Patel // 2966fd4391dSSanjay Patel // If the div and rem are in the same block, we do the same transform, 2976fd4391dSSanjay Patel // but any code movement would be within the same block. 2986fd4391dSSanjay Patel 2996fd4391dSSanjay Patel if (!DivDominates) 3006fd4391dSSanjay Patel DivInst->moveBefore(RemInst); 3016fd4391dSSanjay Patel Mul->insertAfter(RemInst); 3026fd4391dSSanjay Patel Sub->insertAfter(Mul); 3036fd4391dSSanjay Patel 3046fd4391dSSanjay Patel // Now kill the explicit remainder. We have replaced it with: 3056fd4391dSSanjay Patel // (sub X, (mul (div X, Y), Y) 3065f616901SRoman Lebedev Sub->setName(RemInst->getName() + ".decomposed"); 3075f616901SRoman Lebedev Instruction *OrigRemInst = RemInst; 3085f616901SRoman Lebedev // Update AssertingVH<> with new instruction so it doesn't assert. 3095f616901SRoman Lebedev RemInst = Sub; 3105f616901SRoman Lebedev // And replace the original instruction with the new one. 3115f616901SRoman Lebedev OrigRemInst->replaceAllUsesWith(Sub); 3125f616901SRoman Lebedev OrigRemInst->eraseFromParent(); 3136fd4391dSSanjay Patel NumDecomposed++; 3146fd4391dSSanjay Patel } 3156fd4391dSSanjay Patel Changed = true; 3166fd4391dSSanjay Patel } 3176fd4391dSSanjay Patel 3186fd4391dSSanjay Patel return Changed; 3196fd4391dSSanjay Patel } 3206fd4391dSSanjay Patel 3216fd4391dSSanjay Patel // Pass manager boilerplate below here. 3226fd4391dSSanjay Patel 3236fd4391dSSanjay Patel namespace { 3246fd4391dSSanjay Patel struct DivRemPairsLegacyPass : public FunctionPass { 3256fd4391dSSanjay Patel static char ID; 3266fd4391dSSanjay Patel DivRemPairsLegacyPass() : FunctionPass(ID) { 3276fd4391dSSanjay Patel initializeDivRemPairsLegacyPassPass(*PassRegistry::getPassRegistry()); 3286fd4391dSSanjay Patel } 3296fd4391dSSanjay Patel 3306fd4391dSSanjay Patel void getAnalysisUsage(AnalysisUsage &AU) const override { 3316fd4391dSSanjay Patel AU.addRequired<DominatorTreeWrapperPass>(); 3326fd4391dSSanjay Patel AU.addRequired<TargetTransformInfoWrapperPass>(); 3336fd4391dSSanjay Patel AU.setPreservesCFG(); 3346fd4391dSSanjay Patel AU.addPreserved<DominatorTreeWrapperPass>(); 3356fd4391dSSanjay Patel AU.addPreserved<GlobalsAAWrapperPass>(); 3366fd4391dSSanjay Patel FunctionPass::getAnalysisUsage(AU); 3376fd4391dSSanjay Patel } 3386fd4391dSSanjay Patel 3396fd4391dSSanjay Patel bool runOnFunction(Function &F) override { 3406fd4391dSSanjay Patel if (skipFunction(F)) 3416fd4391dSSanjay Patel return false; 3426fd4391dSSanjay Patel auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 3436fd4391dSSanjay Patel auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 3446fd4391dSSanjay Patel return optimizeDivRem(F, TTI, DT); 3456fd4391dSSanjay Patel } 3466fd4391dSSanjay Patel }; 3475f616901SRoman Lebedev } // namespace 3486fd4391dSSanjay Patel 3496fd4391dSSanjay Patel char DivRemPairsLegacyPass::ID = 0; 3506fd4391dSSanjay Patel INITIALIZE_PASS_BEGIN(DivRemPairsLegacyPass, "div-rem-pairs", 3516fd4391dSSanjay Patel "Hoist/decompose integer division and remainder", false, 3526fd4391dSSanjay Patel false) 3536fd4391dSSanjay Patel INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 3546fd4391dSSanjay Patel INITIALIZE_PASS_END(DivRemPairsLegacyPass, "div-rem-pairs", 3556fd4391dSSanjay Patel "Hoist/decompose integer division and remainder", false, 3566fd4391dSSanjay Patel false) 3576fd4391dSSanjay Patel FunctionPass *llvm::createDivRemPairsPass() { 3586fd4391dSSanjay Patel return new DivRemPairsLegacyPass(); 3596fd4391dSSanjay Patel } 3606fd4391dSSanjay Patel 3616fd4391dSSanjay Patel PreservedAnalyses DivRemPairsPass::run(Function &F, 3626fd4391dSSanjay Patel FunctionAnalysisManager &FAM) { 3636fd4391dSSanjay Patel TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F); 3646fd4391dSSanjay Patel DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F); 3656fd4391dSSanjay Patel if (!optimizeDivRem(F, TTI, DT)) 3666fd4391dSSanjay Patel return PreservedAnalyses::all(); 3676fd4391dSSanjay Patel // TODO: This pass just hoists/replaces math ops - all analyses are preserved? 3686fd4391dSSanjay Patel PreservedAnalyses PA; 3696fd4391dSSanjay Patel PA.preserveSet<CFGAnalyses>(); 3706fd4391dSSanjay Patel PA.preserve<GlobalsAA>(); 3716fd4391dSSanjay Patel return PA; 3726fd4391dSSanjay Patel } 373