annotate lib/Transforms/Scalar/DivRemPairs.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- DivRemPairs.cpp - Hoist/decompose division and remainder -*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // This pass hoists and/or decomposes integer division and remainder
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 // instructions to enable CFG improvements and better codegen.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #include "llvm/Transforms/Scalar/DivRemPairs.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include "llvm/ADT/Statistic.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "llvm/Analysis/GlobalsModRef.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/Analysis/TargetTransformInfo.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/IR/Dominators.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/IR/Function.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/Pass.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #include "llvm/Transforms/Scalar.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 #include "llvm/Transforms/Utils/BypassSlowDivision.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 #define DEBUG_TYPE "div-rem-pairs"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 STATISTIC(NumPairs, "Number of div/rem pairs");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 STATISTIC(NumHoisted, "Number of instructions hoisted");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 STATISTIC(NumDecomposed, "Number of instructions decomposed");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 /// Find matching pairs of integer div/rem ops (they have the same numerator,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 /// denominator, and signedness). If they exist in different basic blocks, bring
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 /// them together by hoisting or replace the common division operation that is
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 /// implicit in the remainder:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 /// X % Y <--> X - ((X / Y) * Y).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 /// We can largely ignore the normal safety and cost constraints on speculation
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 /// of these ops when we find a matching pair. This is because we are already
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 /// guaranteed that any exceptions and most cost are already incurred by the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 /// first member of the pair.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 /// Note: This transform could be an oddball enhancement to EarlyCSE, GVN, or
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 /// SimplifyCFG, but it's split off on its own because it's different enough
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 /// that it doesn't quite match the stated objectives of those passes.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 static bool optimizeDivRem(Function &F, const TargetTransformInfo &TTI,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 const DominatorTree &DT) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 bool Changed = false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 // Insert all divide and remainder instructions into maps keyed by their
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 // operands and opcode (signed or unsigned).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 DenseMap<DivRemMapKey, Instruction *> DivMap, RemMap;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 for (auto &BB : F) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 for (auto &I : BB) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 if (I.getOpcode() == Instruction::SDiv)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 DivMap[DivRemMapKey(true, I.getOperand(0), I.getOperand(1))] = &I;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 else if (I.getOpcode() == Instruction::UDiv)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 DivMap[DivRemMapKey(false, I.getOperand(0), I.getOperand(1))] = &I;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 else if (I.getOpcode() == Instruction::SRem)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 RemMap[DivRemMapKey(true, I.getOperand(0), I.getOperand(1))] = &I;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 else if (I.getOpcode() == Instruction::URem)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 RemMap[DivRemMapKey(false, I.getOperand(0), I.getOperand(1))] = &I;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 // We can iterate over either map because we are only looking for matched
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 // pairs. Choose remainders for efficiency because they are usually even more
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 // rare than division.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 for (auto &RemPair : RemMap) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 // Find the matching division instruction from the division map.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 Instruction *DivInst = DivMap[RemPair.getFirst()];
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 if (!DivInst)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 // We have a matching pair of div/rem instructions. If one dominates the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 // other, hoist and/or replace one.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 NumPairs++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 Instruction *RemInst = RemPair.getSecond();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 bool IsSigned = DivInst->getOpcode() == Instruction::SDiv;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 bool HasDivRemOp = TTI.hasDivRemOp(DivInst->getType(), IsSigned);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 // If the target supports div+rem and the instructions are in the same block
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 // already, there's nothing to do. The backend should handle this. If the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 // target does not support div+rem, then we will decompose the rem.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 if (HasDivRemOp && RemInst->getParent() == DivInst->getParent())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 bool DivDominates = DT.dominates(DivInst, RemInst);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 if (!DivDominates && !DT.dominates(RemInst, DivInst))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 continue;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 if (HasDivRemOp) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 // The target has a single div/rem operation. Hoist the lower instruction
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 // to make the matched pair visible to the backend.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 if (DivDominates)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 RemInst->moveAfter(DivInst);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 DivInst->moveAfter(RemInst);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 NumHoisted++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 } else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 // The target does not have a single div/rem operation. Decompose the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 // remainder calculation as:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 // X % Y --> X - ((X / Y) * Y).
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 Value *X = RemInst->getOperand(0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 Value *Y = RemInst->getOperand(1);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 Instruction *Mul = BinaryOperator::CreateMul(DivInst, Y);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 Instruction *Sub = BinaryOperator::CreateSub(X, Mul);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 // If the remainder dominates, then hoist the division up to that block:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 // bb1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 // %rem = srem %x, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 // bb2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 // %div = sdiv %x, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 // -->
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 // bb1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 // %div = sdiv %x, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117 // %mul = mul %div, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 // %rem = sub %x, %mul
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 // If the division dominates, it's already in the right place. The mul+sub
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 // will be in a different block because we don't assume that they are
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 // cheap to speculatively execute:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 // bb1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 // %div = sdiv %x, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 // bb2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 // %rem = srem %x, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 // -->
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 // bb1:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 // %div = sdiv %x, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 // bb2:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 // %mul = mul %div, %y
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 // %rem = sub %x, %mul
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 // If the div and rem are in the same block, we do the same transform,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 // but any code movement would be within the same block.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 if (!DivDominates)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 DivInst->moveBefore(RemInst);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 Mul->insertAfter(RemInst);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 Sub->insertAfter(Mul);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 // Now kill the explicit remainder. We have replaced it with:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 // (sub X, (mul (div X, Y), Y)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 RemInst->replaceAllUsesWith(Sub);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 RemInst->eraseFromParent();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 NumDecomposed++;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 Changed = true;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 return Changed;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 // Pass manager boilerplate below here.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 struct DivRemPairsLegacyPass : public FunctionPass {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 static char ID;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 DivRemPairsLegacyPass() : FunctionPass(ID) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 initializeDivRemPairsLegacyPassPass(*PassRegistry::getPassRegistry());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 void getAnalysisUsage(AnalysisUsage &AU) const override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 AU.addRequired<DominatorTreeWrapperPass>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 AU.addRequired<TargetTransformInfoWrapperPass>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 AU.setPreservesCFG();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 AU.addPreserved<DominatorTreeWrapperPass>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 AU.addPreserved<GlobalsAAWrapperPass>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 FunctionPass::getAnalysisUsage(AU);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 bool runOnFunction(Function &F) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 if (skipFunction(F))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 return false;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 return optimizeDivRem(F, TTI, DT);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 char DivRemPairsLegacyPass::ID = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 INITIALIZE_PASS_BEGIN(DivRemPairsLegacyPass, "div-rem-pairs",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 "Hoist/decompose integer division and remainder", false,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 false)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 INITIALIZE_PASS_END(DivRemPairsLegacyPass, "div-rem-pairs",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 "Hoist/decompose integer division and remainder", false,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 false)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 FunctionPass *llvm::createDivRemPairsPass() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 return new DivRemPairsLegacyPass();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 PreservedAnalyses DivRemPairsPass::run(Function &F,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 FunctionAnalysisManager &FAM) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 TargetTransformInfo &TTI = FAM.getResult<TargetIRAnalysis>(F);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 if (!optimizeDivRem(F, TTI, DT))
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 return PreservedAnalyses::all();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 // TODO: This pass just hoists/replaces math ops - all analyses are preserved?
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 PreservedAnalyses PA;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 PA.preserveSet<CFGAnalyses>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 PA.preserve<GlobalsAA>();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 return PA;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 }