blob: 9b4319d40cd00e712c8565fb184555594e2c8743 [file] [log] [blame]
Devang Patelbc5fe632007-08-07 00:25:56 +00001//===- LoopIndexSplit.cpp - Loop Index Splitting Pass ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Devang Patel and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements Loop Index Splitting Pass.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "loop-index-split"
15
Devang Patelbc5fe632007-08-07 00:25:56 +000016#include "llvm/Transforms/Scalar.h"
17#include "llvm/Analysis/LoopPass.h"
18#include "llvm/Analysis/ScalarEvolutionExpander.h"
Devang Patel95fd7172007-08-08 21:39:47 +000019#include "llvm/Analysis/Dominators.h"
Devang Patel901f67e2007-08-10 18:07:13 +000020#include "llvm/Transforms/Utils/BasicBlockUtils.h"
21#include "llvm/Transforms/Utils/Cloning.h"
Devang Patelbc5fe632007-08-07 00:25:56 +000022#include "llvm/Support/Compiler.h"
Devang Patelf4277122007-08-15 03:31:47 +000023#include "llvm/ADT/DepthFirstIterator.h"
Devang Patelbc5fe632007-08-07 00:25:56 +000024#include "llvm/ADT/Statistic.h"
25
26using namespace llvm;
27
28STATISTIC(NumIndexSplit, "Number of loops index split");
29
30namespace {
31
32 class VISIBILITY_HIDDEN LoopIndexSplit : public LoopPass {
33
34 public:
35 static char ID; // Pass ID, replacement for typeid
36 LoopIndexSplit() : LoopPass((intptr_t)&ID) {}
37
38 // Index split Loop L. Return true if loop is split.
39 bool runOnLoop(Loop *L, LPPassManager &LPM);
40
41 void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<ScalarEvolution>();
43 AU.addPreserved<ScalarEvolution>();
44 AU.addRequiredID(LCSSAID);
45 AU.addPreservedID(LCSSAID);
Devang Patel901f67e2007-08-10 18:07:13 +000046 AU.addRequired<LoopInfo>();
Devang Patelbc5fe632007-08-07 00:25:56 +000047 AU.addPreserved<LoopInfo>();
48 AU.addRequiredID(LoopSimplifyID);
49 AU.addPreservedID(LoopSimplifyID);
Devang Patel0aaeb172007-08-08 22:25:28 +000050 AU.addRequired<DominatorTree>();
Devang Patelf4277122007-08-15 03:31:47 +000051 AU.addRequired<DominanceFrontier>();
Devang Patel95fd7172007-08-08 21:39:47 +000052 AU.addPreserved<DominatorTree>();
53 AU.addPreserved<DominanceFrontier>();
Devang Patelbc5fe632007-08-07 00:25:56 +000054 }
55
56 private:
Devang Patelc8dadbf2007-08-08 21:02:17 +000057
58 class SplitInfo {
59 public:
Devang Patel61571ca2007-08-10 00:33:50 +000060 SplitInfo() : SplitValue(NULL), SplitCondition(NULL) {}
Devang Patel2545f7b2007-08-09 01:39:01 +000061
Devang Patelc8dadbf2007-08-08 21:02:17 +000062 // Induction variable's range is split at this value.
63 Value *SplitValue;
64
Devang Patelc8dadbf2007-08-08 21:02:17 +000065 // This compare instruction compares IndVar against SplitValue.
66 ICmpInst *SplitCondition;
67
Devang Patel31696332007-08-08 21:18:27 +000068 // Clear split info.
69 void clear() {
Devang Patel31696332007-08-08 21:18:27 +000070 SplitValue = NULL;
Devang Patel31696332007-08-08 21:18:27 +000071 SplitCondition = NULL;
Devang Patel31696332007-08-08 21:18:27 +000072 }
Devang Patel2545f7b2007-08-09 01:39:01 +000073
Devang Patelc8dadbf2007-08-08 21:02:17 +000074 };
Devang Patel61571ca2007-08-10 00:33:50 +000075
Devang Patelc8dadbf2007-08-08 21:02:17 +000076 private:
Devang Patelbc5fe632007-08-07 00:25:56 +000077 /// Find condition inside a loop that is suitable candidate for index split.
78 void findSplitCondition();
79
Devang Patel61571ca2007-08-10 00:33:50 +000080 /// Find loop's exit condition.
81 void findLoopConditionals();
82
83 /// Return induction variable associated with value V.
84 void findIndVar(Value *V, Loop *L);
85
Devang Patelbc5fe632007-08-07 00:25:56 +000086 /// processOneIterationLoop - Current loop L contains compare instruction
87 /// that compares induction variable, IndVar, agains loop invariant. If
88 /// entire (i.e. meaningful) loop body is dominated by this compare
89 /// instruction then loop body is executed only for one iteration. In
90 /// such case eliminate loop structure surrounding this loop body. For
Devang Patel901f67e2007-08-10 18:07:13 +000091 bool processOneIterationLoop(SplitInfo &SD);
Devang Patelbc5fe632007-08-07 00:25:56 +000092
Devang Patel0aaeb172007-08-08 22:25:28 +000093 /// If loop header includes loop variant instruction operands then
94 /// this loop may not be eliminated.
Devang Patelc8dadbf2007-08-08 21:02:17 +000095 bool safeHeader(SplitInfo &SD, BasicBlock *BB);
Devang Patelbc5fe632007-08-07 00:25:56 +000096
Devang Patel0aaeb172007-08-08 22:25:28 +000097 /// If Exit block includes loop variant instructions then this
98 /// loop may not be eliminated.
Devang Patelc8dadbf2007-08-08 21:02:17 +000099 bool safeExitBlock(SplitInfo &SD, BasicBlock *BB);
Devang Patelbc5fe632007-08-07 00:25:56 +0000100
Devang Patel60a94c72007-08-14 18:35:57 +0000101 /// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
102 /// This routine is used to remove split condition's dead branch, dominated by
103 /// DeadBB. LiveBB dominates split conidition's other branch.
104 void removeBlocks(BasicBlock *DeadBB, Loop *LP, BasicBlock *LiveBB);
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000105
Devang Patel0aaeb172007-08-08 22:25:28 +0000106 /// Find cost of spliting loop L.
107 unsigned findSplitCost(Loop *L, SplitInfo &SD);
Devang Patelc8dadbf2007-08-08 21:02:17 +0000108 bool splitLoop(SplitInfo &SD);
Devang Patelbc5fe632007-08-07 00:25:56 +0000109
Devang Patel61571ca2007-08-10 00:33:50 +0000110 void initialize() {
111 IndVar = NULL;
112 IndVarIncrement = NULL;
113 ExitCondition = NULL;
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000114 StartValue = NULL;
115 ExitValueNum = 0;
116 SplitData.clear();
Devang Patel61571ca2007-08-10 00:33:50 +0000117 }
118
Devang Patelbc5fe632007-08-07 00:25:56 +0000119 private:
120
121 // Current Loop.
122 Loop *L;
Devang Patel901f67e2007-08-10 18:07:13 +0000123 LPPassManager *LPM;
124 LoopInfo *LI;
Devang Patelbc5fe632007-08-07 00:25:56 +0000125 ScalarEvolution *SE;
Devang Patel0aaeb172007-08-08 22:25:28 +0000126 DominatorTree *DT;
Devang Patelb7639612007-08-13 22:13:24 +0000127 DominanceFrontier *DF;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000128 SmallVector<SplitInfo, 4> SplitData;
Devang Patel61571ca2007-08-10 00:33:50 +0000129
130 // Induction variable whose range is being split by this transformation.
131 PHINode *IndVar;
132 Instruction *IndVarIncrement;
133
134 // Loop exit condition.
135 ICmpInst *ExitCondition;
136
137 // Induction variable's initial value.
138 Value *StartValue;
139
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000140 // Induction variable's final loop exit value operand number in exit condition..
141 unsigned ExitValueNum;
Devang Patelbc5fe632007-08-07 00:25:56 +0000142 };
143
144 char LoopIndexSplit::ID = 0;
145 RegisterPass<LoopIndexSplit> X ("loop-index-split", "Index Split Loops");
146}
147
148LoopPass *llvm::createLoopIndexSplitPass() {
149 return new LoopIndexSplit();
150}
151
152// Index split Loop L. Return true if loop is split.
Devang Patel901f67e2007-08-10 18:07:13 +0000153bool LoopIndexSplit::runOnLoop(Loop *IncomingLoop, LPPassManager &LPM_Ref) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000154 bool Changed = false;
155 L = IncomingLoop;
Devang Patel901f67e2007-08-10 18:07:13 +0000156 LPM = &LPM_Ref;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000157
Devang Patel81fcdfb2007-08-15 02:14:55 +0000158 // FIXME - Nested loops make dominator info updates tricky.
Devang Patel79276b32007-08-14 23:53:57 +0000159 if (!L->getSubLoops().empty())
160 return false;
161
Devang Patelbc5fe632007-08-07 00:25:56 +0000162 SE = &getAnalysis<ScalarEvolution>();
Devang Patel0aaeb172007-08-08 22:25:28 +0000163 DT = &getAnalysis<DominatorTree>();
Devang Patel901f67e2007-08-10 18:07:13 +0000164 LI = &getAnalysis<LoopInfo>();
Devang Patel2190f172007-08-15 03:34:53 +0000165 DF = &getAnalysis<DominanceFrontier>();
Devang Patelbc5fe632007-08-07 00:25:56 +0000166
Devang Patel61571ca2007-08-10 00:33:50 +0000167 initialize();
168
169 findLoopConditionals();
170
171 if (!ExitCondition)
172 return false;
173
Devang Patelbc5fe632007-08-07 00:25:56 +0000174 findSplitCondition();
175
Devang Patelc8dadbf2007-08-08 21:02:17 +0000176 if (SplitData.empty())
Devang Patelbc5fe632007-08-07 00:25:56 +0000177 return false;
178
Devang Patelc8dadbf2007-08-08 21:02:17 +0000179 // First see if it is possible to eliminate loop itself or not.
180 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
181 E = SplitData.end(); SI != E; ++SI) {
182 SplitInfo &SD = *SI;
183 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ) {
Devang Patel901f67e2007-08-10 18:07:13 +0000184 Changed = processOneIterationLoop(SD);
Devang Patelc8dadbf2007-08-08 21:02:17 +0000185 if (Changed) {
186 ++NumIndexSplit;
187 // If is loop is eliminated then nothing else to do here.
188 return Changed;
189 }
190 }
191 }
192
Devang Patel0aaeb172007-08-08 22:25:28 +0000193 unsigned MaxCost = 99;
194 unsigned Index = 0;
195 unsigned MostProfitableSDIndex = 0;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000196 for (SmallVector<SplitInfo, 4>::iterator SI = SplitData.begin(),
Devang Patel0aaeb172007-08-08 22:25:28 +0000197 E = SplitData.end(); SI != E; ++SI, ++Index) {
198 SplitInfo SD = *SI;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000199
200 // ICM_EQs are already handled above.
Devang Patel0aaeb172007-08-08 22:25:28 +0000201 if (SD.SplitCondition->getPredicate() == ICmpInst::ICMP_EQ)
Devang Patelc8dadbf2007-08-08 21:02:17 +0000202 continue;
Devang Patel0aaeb172007-08-08 22:25:28 +0000203
204 unsigned Cost = findSplitCost(L, SD);
205 if (Cost < MaxCost)
206 MostProfitableSDIndex = Index;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000207 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000208
Devang Patel0aaeb172007-08-08 22:25:28 +0000209 // Split most profitiable condition.
210 Changed = splitLoop(SplitData[MostProfitableSDIndex]);
211
Devang Patelbc5fe632007-08-07 00:25:56 +0000212 if (Changed)
213 ++NumIndexSplit;
Devang Patelc8dadbf2007-08-08 21:02:17 +0000214
Devang Patelbc5fe632007-08-07 00:25:56 +0000215 return Changed;
216}
217
Devang Patel2545f7b2007-08-09 01:39:01 +0000218/// Return true if V is a induction variable or induction variable's
219/// increment for loop L.
Devang Patel61571ca2007-08-10 00:33:50 +0000220void LoopIndexSplit::findIndVar(Value *V, Loop *L) {
Devang Patel2545f7b2007-08-09 01:39:01 +0000221
222 Instruction *I = dyn_cast<Instruction>(V);
223 if (!I)
Devang Patel61571ca2007-08-10 00:33:50 +0000224 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000225
226 // Check if I is a phi node from loop header or not.
227 if (PHINode *PN = dyn_cast<PHINode>(V)) {
228 if (PN->getParent() == L->getHeader()) {
Devang Patel61571ca2007-08-10 00:33:50 +0000229 IndVar = PN;
230 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000231 }
232 }
233
234 // Check if I is a add instruction whose one operand is
235 // phi node from loop header and second operand is constant.
236 if (I->getOpcode() != Instruction::Add)
Devang Patel61571ca2007-08-10 00:33:50 +0000237 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000238
239 Value *Op0 = I->getOperand(0);
240 Value *Op1 = I->getOperand(1);
241
242 if (PHINode *PN = dyn_cast<PHINode>(Op0)) {
243 if (PN->getParent() == L->getHeader()
244 && isa<ConstantInt>(Op1)) {
245 IndVar = PN;
246 IndVarIncrement = I;
Devang Patel61571ca2007-08-10 00:33:50 +0000247 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000248 }
249 }
250
251 if (PHINode *PN = dyn_cast<PHINode>(Op1)) {
252 if (PN->getParent() == L->getHeader()
253 && isa<ConstantInt>(Op0)) {
254 IndVar = PN;
255 IndVarIncrement = I;
Devang Patel61571ca2007-08-10 00:33:50 +0000256 return;
Devang Patel2545f7b2007-08-09 01:39:01 +0000257 }
258 }
259
Devang Patel61571ca2007-08-10 00:33:50 +0000260 return;
261}
262
263// Find loop's exit condition and associated induction variable.
264void LoopIndexSplit::findLoopConditionals() {
265
266 BasicBlock *ExitBlock = NULL;
267
268 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
269 I != E; ++I) {
270 BasicBlock *BB = *I;
271 if (!L->isLoopExit(BB))
272 continue;
273 if (ExitBlock)
274 return;
275 ExitBlock = BB;
276 }
277
278 if (!ExitBlock)
279 return;
280
281 // If exit block's terminator is conditional branch inst then we have found
282 // exit condition.
283 BranchInst *BR = dyn_cast<BranchInst>(ExitBlock->getTerminator());
284 if (!BR || BR->isUnconditional())
285 return;
286
287 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
288 if (!CI)
289 return;
290
291 ExitCondition = CI;
292
293 // Exit condition's one operand is loop invariant exit value and second
294 // operand is SCEVAddRecExpr based on induction variable.
295 Value *V0 = CI->getOperand(0);
296 Value *V1 = CI->getOperand(1);
297
298 SCEVHandle SH0 = SE->getSCEV(V0);
299 SCEVHandle SH1 = SE->getSCEV(V1);
300
301 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000302 ExitValueNum = 0;
Devang Patel61571ca2007-08-10 00:33:50 +0000303 findIndVar(V1, L);
304 }
305 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000306 ExitValueNum = 1;
Devang Patel61571ca2007-08-10 00:33:50 +0000307 findIndVar(V0, L);
308 }
309
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000310 if (!IndVar)
Devang Patel61571ca2007-08-10 00:33:50 +0000311 ExitCondition = NULL;
312 else if (IndVar) {
313 BasicBlock *Preheader = L->getLoopPreheader();
314 StartValue = IndVar->getIncomingValueForBlock(Preheader);
315 }
Devang Patel2545f7b2007-08-09 01:39:01 +0000316}
317
Devang Patelbc5fe632007-08-07 00:25:56 +0000318/// Find condition inside a loop that is suitable candidate for index split.
319void LoopIndexSplit::findSplitCondition() {
320
Devang Patelc8dadbf2007-08-08 21:02:17 +0000321 SplitInfo SD;
Devang Patel2545f7b2007-08-09 01:39:01 +0000322 // Check all basic block's terminators.
Devang Patelbc5fe632007-08-07 00:25:56 +0000323
Devang Patel2545f7b2007-08-09 01:39:01 +0000324 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
325 I != E; ++I) {
326 BasicBlock *BB = *I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000327
Devang Patel2545f7b2007-08-09 01:39:01 +0000328 // If this basic block does not terminate in a conditional branch
329 // then terminator is not a suitable split condition.
330 BranchInst *BR = dyn_cast<BranchInst>(BB->getTerminator());
331 if (!BR)
332 continue;
333
334 if (BR->isUnconditional())
Devang Patelbc5fe632007-08-07 00:25:56 +0000335 continue;
336
Devang Patel2545f7b2007-08-09 01:39:01 +0000337 ICmpInst *CI = dyn_cast<ICmpInst>(BR->getCondition());
Devang Patel61571ca2007-08-10 00:33:50 +0000338 if (!CI || CI == ExitCondition)
Devang Patel2545f7b2007-08-09 01:39:01 +0000339 return;
Devang Patelbc5fe632007-08-07 00:25:56 +0000340
Devang Patel2545f7b2007-08-09 01:39:01 +0000341 // If one operand is loop invariant and second operand is SCEVAddRecExpr
342 // based on induction variable then CI is a candidate split condition.
343 Value *V0 = CI->getOperand(0);
344 Value *V1 = CI->getOperand(1);
345
346 SCEVHandle SH0 = SE->getSCEV(V0);
347 SCEVHandle SH1 = SE->getSCEV(V1);
348
349 if (SH0->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH1)) {
350 SD.SplitValue = V0;
351 SD.SplitCondition = CI;
Devang Patel61571ca2007-08-10 00:33:50 +0000352 if (PHINode *PN = dyn_cast<PHINode>(V1)) {
353 if (PN == IndVar)
354 SplitData.push_back(SD);
355 }
356 else if (Instruction *Insn = dyn_cast<Instruction>(V1)) {
357 if (IndVarIncrement && IndVarIncrement == Insn)
358 SplitData.push_back(SD);
359 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000360 }
Devang Patel2545f7b2007-08-09 01:39:01 +0000361 else if (SH1->isLoopInvariant(L) && isa<SCEVAddRecExpr>(SH0)) {
362 SD.SplitValue = V1;
363 SD.SplitCondition = CI;
Devang Patel61571ca2007-08-10 00:33:50 +0000364 if (PHINode *PN = dyn_cast<PHINode>(V0)) {
365 if (PN == IndVar)
366 SplitData.push_back(SD);
367 }
368 else if (Instruction *Insn = dyn_cast<Instruction>(V0)) {
369 if (IndVarIncrement && IndVarIncrement == Insn)
370 SplitData.push_back(SD);
371 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000372 }
373 }
374}
375
376/// processOneIterationLoop - Current loop L contains compare instruction
377/// that compares induction variable, IndVar, against loop invariant. If
378/// entire (i.e. meaningful) loop body is dominated by this compare
379/// instruction then loop body is executed only once. In such case eliminate
380/// loop structure surrounding this loop body. For example,
381/// for (int i = start; i < end; ++i) {
382/// if ( i == somevalue) {
383/// loop_body
384/// }
385/// }
386/// can be transformed into
387/// if (somevalue >= start && somevalue < end) {
388/// i = somevalue;
389/// loop_body
390/// }
Devang Patel901f67e2007-08-10 18:07:13 +0000391bool LoopIndexSplit::processOneIterationLoop(SplitInfo &SD) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000392
393 BasicBlock *Header = L->getHeader();
394
395 // First of all, check if SplitCondition dominates entire loop body
396 // or not.
397
398 // If SplitCondition is not in loop header then this loop is not suitable
399 // for this transformation.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000400 if (SD.SplitCondition->getParent() != Header)
Devang Patelbc5fe632007-08-07 00:25:56 +0000401 return false;
402
Devang Patelbc5fe632007-08-07 00:25:56 +0000403 // If loop header includes loop variant instruction operands then
404 // this loop may not be eliminated.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000405 if (!safeHeader(SD, Header))
Devang Patelbc5fe632007-08-07 00:25:56 +0000406 return false;
407
408 // If Exit block includes loop variant instructions then this
409 // loop may not be eliminated.
Devang Patelbfa5eba2007-08-10 00:59:03 +0000410 if (!safeExitBlock(SD, ExitCondition->getParent()))
Devang Patelbc5fe632007-08-07 00:25:56 +0000411 return false;
412
Devang Patel2bcb5012007-08-08 01:51:27 +0000413 // Update CFG.
414
415 // As a first step to break this loop, remove Latch to Header edge.
Devang Patelbc5fe632007-08-07 00:25:56 +0000416 BasicBlock *Latch = L->getLoopLatch();
Devang Patel2bcb5012007-08-08 01:51:27 +0000417 BasicBlock *LatchSucc = NULL;
418 BranchInst *BR = dyn_cast<BranchInst>(Latch->getTerminator());
419 if (!BR)
420 return false;
421 Header->removePredecessor(Latch);
422 for (succ_iterator SI = succ_begin(Latch), E = succ_end(Latch);
423 SI != E; ++SI) {
424 if (Header != *SI)
425 LatchSucc = *SI;
426 }
427 BR->setUnconditionalDest(LatchSucc);
428
Devang Patelbc5fe632007-08-07 00:25:56 +0000429 Instruction *Terminator = Header->getTerminator();
Devang Patel59e0c062007-08-14 01:30:57 +0000430 Value *ExitValue = ExitCondition->getOperand(ExitValueNum);
Devang Patelbc5fe632007-08-07 00:25:56 +0000431
Devang Patelbc5fe632007-08-07 00:25:56 +0000432 // Replace split condition in header.
433 // Transform
434 // SplitCondition : icmp eq i32 IndVar, SplitValue
435 // into
436 // c1 = icmp uge i32 SplitValue, StartValue
437 // c2 = icmp ult i32 vSplitValue, ExitValue
438 // and i32 c1, c2
Devang Patel61571ca2007-08-10 00:33:50 +0000439 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patelbc5fe632007-08-07 00:25:56 +0000440 Instruction *C1 = new ICmpInst(SignedPredicate ?
441 ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE,
Devang Patelc8dadbf2007-08-08 21:02:17 +0000442 SD.SplitValue, StartValue, "lisplit",
443 Terminator);
Devang Patelbc5fe632007-08-07 00:25:56 +0000444 Instruction *C2 = new ICmpInst(SignedPredicate ?
445 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patel59e0c062007-08-14 01:30:57 +0000446 SD.SplitValue, ExitValue, "lisplit",
Devang Patelc8dadbf2007-08-08 21:02:17 +0000447 Terminator);
448 Instruction *NSplitCond = BinaryOperator::createAnd(C1, C2, "lisplit",
449 Terminator);
450 SD.SplitCondition->replaceAllUsesWith(NSplitCond);
451 SD.SplitCondition->eraseFromParent();
Devang Patelbc5fe632007-08-07 00:25:56 +0000452
Devang Patelbc5fe632007-08-07 00:25:56 +0000453 // Now, clear latch block. Remove instructions that are responsible
454 // to increment induction variable.
455 Instruction *LTerminator = Latch->getTerminator();
456 for (BasicBlock::iterator LB = Latch->begin(), LE = Latch->end();
457 LB != LE; ) {
458 Instruction *I = LB;
459 ++LB;
460 if (isa<PHINode>(I) || I == LTerminator)
461 continue;
462
Devang Patel59e0c062007-08-14 01:30:57 +0000463 if (I == IndVarIncrement)
464 I->replaceAllUsesWith(ExitValue);
465 else
466 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Devang Patel0d75c292007-08-07 17:45:35 +0000467 I->eraseFromParent();
Devang Patelbc5fe632007-08-07 00:25:56 +0000468 }
469
Devang Patel901f67e2007-08-10 18:07:13 +0000470 LPM->deleteLoopFromQueue(L);
Devang Patel95fd7172007-08-08 21:39:47 +0000471
472 // Update Dominator Info.
473 // Only CFG change done is to remove Latch to Header edge. This
474 // does not change dominator tree because Latch did not dominate
475 // Header.
Devang Patelb7639612007-08-13 22:13:24 +0000476 if (DF) {
Devang Patel95fd7172007-08-08 21:39:47 +0000477 DominanceFrontier::iterator HeaderDF = DF->find(Header);
478 if (HeaderDF != DF->end())
479 DF->removeFromFrontier(HeaderDF, Header);
480
481 DominanceFrontier::iterator LatchDF = DF->find(Latch);
482 if (LatchDF != DF->end())
483 DF->removeFromFrontier(LatchDF, Header);
484 }
Devang Patelbc5fe632007-08-07 00:25:56 +0000485 return true;
486}
487
488// If loop header includes loop variant instruction operands then
489// this loop can not be eliminated. This is used by processOneIterationLoop().
Devang Patelc8dadbf2007-08-08 21:02:17 +0000490bool LoopIndexSplit::safeHeader(SplitInfo &SD, BasicBlock *Header) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000491
492 Instruction *Terminator = Header->getTerminator();
493 for(BasicBlock::iterator BI = Header->begin(), BE = Header->end();
494 BI != BE; ++BI) {
495 Instruction *I = BI;
496
Devang Patel59e0c062007-08-14 01:30:57 +0000497 // PHI Nodes are OK.
Devang Patelbc5fe632007-08-07 00:25:56 +0000498 if (isa<PHINode>(I))
499 continue;
500
501 // SplitCondition itself is OK.
Devang Patelc8dadbf2007-08-08 21:02:17 +0000502 if (I == SD.SplitCondition)
Devang Patel2bcb5012007-08-08 01:51:27 +0000503 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000504
Devang Patel2545f7b2007-08-09 01:39:01 +0000505 // Induction variable is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000506 if (I == IndVar)
Devang Patel2545f7b2007-08-09 01:39:01 +0000507 continue;
508
509 // Induction variable increment is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000510 if (I == IndVarIncrement)
Devang Patel2545f7b2007-08-09 01:39:01 +0000511 continue;
512
Devang Patelbc5fe632007-08-07 00:25:56 +0000513 // Terminator is also harmless.
514 if (I == Terminator)
515 continue;
516
517 // Otherwise we have a instruction that may not be safe.
518 return false;
519 }
520
521 return true;
522}
523
524// If Exit block includes loop variant instructions then this
525// loop may not be eliminated. This is used by processOneIterationLoop().
Devang Patelc8dadbf2007-08-08 21:02:17 +0000526bool LoopIndexSplit::safeExitBlock(SplitInfo &SD, BasicBlock *ExitBlock) {
Devang Patelbc5fe632007-08-07 00:25:56 +0000527
Devang Patelbc5fe632007-08-07 00:25:56 +0000528 for (BasicBlock::iterator BI = ExitBlock->begin(), BE = ExitBlock->end();
529 BI != BE; ++BI) {
530 Instruction *I = BI;
531
Devang Patel59e0c062007-08-14 01:30:57 +0000532 // PHI Nodes are OK.
Devang Patelbc5fe632007-08-07 00:25:56 +0000533 if (isa<PHINode>(I))
534 continue;
535
Devang Patel2545f7b2007-08-09 01:39:01 +0000536 // Induction variable increment is OK.
Devang Patel61571ca2007-08-10 00:33:50 +0000537 if (IndVarIncrement && IndVarIncrement == I)
Devang Patel2545f7b2007-08-09 01:39:01 +0000538 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000539
Devang Patel2545f7b2007-08-09 01:39:01 +0000540 // Check if I is induction variable increment instruction.
Devang Patel61571ca2007-08-10 00:33:50 +0000541 if (!IndVarIncrement && I->getOpcode() == Instruction::Add) {
Devang Patel2545f7b2007-08-09 01:39:01 +0000542
543 Value *Op0 = I->getOperand(0);
544 Value *Op1 = I->getOperand(1);
Devang Patelbc5fe632007-08-07 00:25:56 +0000545 PHINode *PN = NULL;
546 ConstantInt *CI = NULL;
547
548 if ((PN = dyn_cast<PHINode>(Op0))) {
549 if ((CI = dyn_cast<ConstantInt>(Op1)))
Devang Patel61571ca2007-08-10 00:33:50 +0000550 IndVarIncrement = I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000551 } else
552 if ((PN = dyn_cast<PHINode>(Op1))) {
553 if ((CI = dyn_cast<ConstantInt>(Op0)))
Devang Patel61571ca2007-08-10 00:33:50 +0000554 IndVarIncrement = I;
Devang Patelbc5fe632007-08-07 00:25:56 +0000555 }
556
Devang Patel61571ca2007-08-10 00:33:50 +0000557 if (IndVarIncrement && PN == IndVar && CI->isOne())
Devang Patelbc5fe632007-08-07 00:25:56 +0000558 continue;
559 }
Devang Patel2bcb5012007-08-08 01:51:27 +0000560
Devang Patelbc5fe632007-08-07 00:25:56 +0000561 // I is an Exit condition if next instruction is block terminator.
562 // Exit condition is OK if it compares loop invariant exit value,
563 // which is checked below.
Devang Patel3719d4f2007-08-07 23:17:52 +0000564 else if (ICmpInst *EC = dyn_cast<ICmpInst>(I)) {
Devang Patel61571ca2007-08-10 00:33:50 +0000565 if (EC == ExitCondition)
Devang Patel2bcb5012007-08-08 01:51:27 +0000566 continue;
Devang Patelbc5fe632007-08-07 00:25:56 +0000567 }
568
Devang Patel61571ca2007-08-10 00:33:50 +0000569 if (I == ExitBlock->getTerminator())
570 continue;
571
Devang Patelbc5fe632007-08-07 00:25:56 +0000572 // Otherwise we have instruction that may not be safe.
573 return false;
574 }
575
Devang Patelbc5fe632007-08-07 00:25:56 +0000576 // We could not find any reason to consider ExitBlock unsafe.
577 return true;
578}
579
Devang Patel0aaeb172007-08-08 22:25:28 +0000580/// Find cost of spliting loop L. Cost is measured in terms of size growth.
581/// Size is growth is calculated based on amount of code duplicated in second
582/// loop.
583unsigned LoopIndexSplit::findSplitCost(Loop *L, SplitInfo &SD) {
584
585 unsigned Cost = 0;
586 BasicBlock *SDBlock = SD.SplitCondition->getParent();
587 for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
588 I != E; ++I) {
589 BasicBlock *BB = *I;
590 // If a block is not dominated by split condition block then
591 // it must be duplicated in both loops.
592 if (!DT->dominates(SDBlock, BB))
593 Cost += BB->size();
594 }
595
596 return Cost;
597}
598
Devang Patel60a94c72007-08-14 18:35:57 +0000599/// removeBlocks - Remove basic block DeadBB and all blocks dominated by DeadBB.
600/// This routine is used to remove split condition's dead branch, dominated by
601/// DeadBB. LiveBB dominates split conidition's other branch.
602void LoopIndexSplit::removeBlocks(BasicBlock *DeadBB, Loop *LP,
603 BasicBlock *LiveBB) {
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000604
Devang Patelf4277122007-08-15 03:31:47 +0000605 // First update DeadBB's dominance frontier.
Devang Patel9cee7a02007-08-17 21:59:16 +0000606 SmallVector<BasicBlock *, 8> FrontierBBs;
Devang Patelf4277122007-08-15 03:31:47 +0000607 DominanceFrontier::iterator DeadBBDF = DF->find(DeadBB);
608 if (DeadBBDF != DF->end()) {
609 SmallVector<BasicBlock *, 8> PredBlocks;
610
611 DominanceFrontier::DomSetType DeadBBSet = DeadBBDF->second;
612 for (DominanceFrontier::DomSetType::iterator DeadBBSetI = DeadBBSet.begin(),
613 DeadBBSetE = DeadBBSet.end(); DeadBBSetI != DeadBBSetE; ++DeadBBSetI) {
614 BasicBlock *FrontierBB = *DeadBBSetI;
Devang Patel9cee7a02007-08-17 21:59:16 +0000615 FrontierBBs.push_back(FrontierBB);
616
Devang Patelf4277122007-08-15 03:31:47 +0000617 // Rremove any PHI incoming edge from blocks dominated by DeadBB.
618 PredBlocks.clear();
619 for(pred_iterator PI = pred_begin(FrontierBB), PE = pred_end(FrontierBB);
620 PI != PE; ++PI) {
621 BasicBlock *P = *PI;
622 if (P == DeadBB || DT->dominates(DeadBB, P))
623 PredBlocks.push_back(P);
Devang Patelb7639612007-08-13 22:13:24 +0000624 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000625
626 BasicBlock *NewDominator = NULL;
Devang Patelf4277122007-08-15 03:31:47 +0000627 for(BasicBlock::iterator FBI = FrontierBB->begin(), FBE = FrontierBB->end();
628 FBI != FBE; ++FBI) {
629 if (PHINode *PN = dyn_cast<PHINode>(FBI)) {
630 for(SmallVector<BasicBlock *, 8>::iterator PI = PredBlocks.begin(),
631 PE = PredBlocks.end(); PI != PE; ++PI) {
632 BasicBlock *P = *PI;
633 PN->removeIncomingValue(P);
634 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000635 // If we have not identified new dominator then see if we can identify
636 // one based on remaining incoming PHINode values.
637 if (NewDominator == NULL && PN->getNumIncomingValues() == 1)
638 NewDominator = PN->getIncomingBlock(0);
Devang Patelf4277122007-08-15 03:31:47 +0000639 }
640 else
641 break;
Devang Patel9cee7a02007-08-17 21:59:16 +0000642 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000643 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000644 }
Devang Patelf4277122007-08-15 03:31:47 +0000645
646 // Now remove DeadBB and all nodes dominated by DeadBB in df order.
647 SmallVector<BasicBlock *, 32> WorkList;
648 DomTreeNode *DN = DT->getNode(DeadBB);
649 for (df_iterator<DomTreeNode*> DI = df_begin(DN),
650 E = df_end(DN); DI != E; ++DI) {
651 BasicBlock *BB = DI->getBlock();
652 WorkList.push_back(BB);
Devang Patel9cee7a02007-08-17 21:59:16 +0000653 BB->replaceAllUsesWith(UndefValue::get(Type::LabelTy));
Devang Patelf4277122007-08-15 03:31:47 +0000654 }
655
656 while (!WorkList.empty()) {
657 BasicBlock *BB = WorkList.back(); WorkList.pop_back();
658 for(BasicBlock::iterator BBI = BB->begin(), BBE = BB->end();
659 BBI != BBE; ++BBI) {
660 Instruction *I = BBI;
661 I->replaceAllUsesWith(UndefValue::get(I->getType()));
662 I->eraseFromParent();
663 }
664 LPM->deleteSimpleAnalysisValue(BB, LP);
665 DT->eraseNode(BB);
666 DF->removeBlock(BB);
667 LI->removeBlock(BB);
668 BB->eraseFromParent();
669 }
Devang Patel9cee7a02007-08-17 21:59:16 +0000670
671 // Update Frontier BBs' dominator info.
672 while (!FrontierBBs.empty()) {
673 BasicBlock *FBB = FrontierBBs.back(); FrontierBBs.pop_back();
674 BasicBlock *NewDominator = FBB->getSinglePredecessor();
675 if (!NewDominator) {
676 pred_iterator PI = pred_begin(FBB), PE = pred_end(FBB);
677 NewDominator = *PI;
678 ++PI;
679 if (NewDominator != LiveBB) {
680 for(; PI != PE; ++PI) {
681 BasicBlock *P = *PI;
682 if (P == LiveBB) {
683 NewDominator = LiveBB;
684 break;
685 }
686 NewDominator = DT->findNearestCommonDominator(NewDominator, P);
687 }
688 }
689 }
690 assert (NewDominator && "Unable to fix dominator info.");
691 DT->changeImmediateDominator(FBB, NewDominator);
692 DF->changeImmediateDominator(FBB, NewDominator, DT);
693 }
694
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000695}
696
Devang Patelc8dadbf2007-08-08 21:02:17 +0000697bool LoopIndexSplit::splitLoop(SplitInfo &SD) {
Devang Patelf824fb42007-08-10 00:53:35 +0000698
699 BasicBlock *Preheader = L->getLoopPreheader();
Devang Patel81fcdfb2007-08-15 02:14:55 +0000700 BasicBlock *SplitBlock = SD.SplitCondition->getParent();
701 BasicBlock *Latch = L->getLoopLatch();
702 BasicBlock *Header = L->getHeader();
703 BranchInst *SplitTerminator = cast<BranchInst>(SplitBlock->getTerminator());
704
705 // FIXME - Unable to handle triange loops at the moment.
706 // In triangle loop, split condition is in header and one of the
707 // the split destination is loop latch. If split condition is EQ
708 // then such loops are already handle in processOneIterationLoop().
709 if (Header == SplitBlock
710 && (Latch == SplitTerminator->getSuccessor(0)
711 || Latch == SplitTerminator->getSuccessor(1)))
712 return false;
Devang Patelf824fb42007-08-10 00:53:35 +0000713
Devang Patel9cba64e2007-08-18 00:00:32 +0000714 // If one of the split condition branch is post dominating other then loop
715 // index split is not appropriate.
Devang Patel9cee7a02007-08-17 21:59:16 +0000716 BasicBlock *Succ0 = SplitTerminator->getSuccessor(0);
717 BasicBlock *Succ1 = SplitTerminator->getSuccessor(1);
718 if (DT->dominates(Succ0, Latch) || DT->dominates(Succ1, Latch))
719 return false;
720
Devang Patel9cba64e2007-08-18 00:00:32 +0000721 // If one of the split condition branch is a predecessor of the other
722 // split condition branch head then do not split loop on this condition.
723 for(pred_iterator PI = pred_begin(Succ0), PE = pred_end(Succ0); PI != PE; ++PI)
724 if (Succ1 == *PI)
725 return false;
726 for(pred_iterator PI = pred_begin(Succ1), PE = pred_end(Succ1); PI != PE; ++PI)
727 if (Succ0 == *PI)
728 return false;
729
Devang Patel61571ca2007-08-10 00:33:50 +0000730 // True loop is original loop. False loop is cloned loop.
Devang Patelf824fb42007-08-10 00:53:35 +0000731
732 bool SignedPredicate = ExitCondition->isSignedPredicate();
Devang Patel61571ca2007-08-10 00:33:50 +0000733 //[*] Calculate True loop's new Exit Value in loop preheader.
Devang Patelf824fb42007-08-10 00:53:35 +0000734 // TLExitValue = min(SplitValue, ExitValue)
Devang Patel61571ca2007-08-10 00:33:50 +0000735 //[*] Calculate False loop's new Start Value in loop preheader.
Devang Patelf824fb42007-08-10 00:53:35 +0000736 // FLStartValue = min(SplitValue, TrueLoop.StartValue)
737 Value *TLExitValue = NULL;
738 Value *FLStartValue = NULL;
739 if (isa<ConstantInt>(SD.SplitValue)) {
740 TLExitValue = SD.SplitValue;
741 FLStartValue = SD.SplitValue;
742 }
743 else {
744 Value *C1 = new ICmpInst(SignedPredicate ?
745 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000746 SD.SplitValue,
747 ExitCondition->getOperand(ExitValueNum),
748 "lsplit.ev",
Devang Patelf824fb42007-08-10 00:53:35 +0000749 Preheader->getTerminator());
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000750 TLExitValue = new SelectInst(C1, SD.SplitValue,
751 ExitCondition->getOperand(ExitValueNum),
Devang Patelf824fb42007-08-10 00:53:35 +0000752 "lsplit.ev", Preheader->getTerminator());
753
754 Value *C2 = new ICmpInst(SignedPredicate ?
755 ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT,
756 SD.SplitValue, StartValue, "lsplit.sv",
757 Preheader->getTerminator());
758 FLStartValue = new SelectInst(C2, SD.SplitValue, StartValue,
759 "lsplit.sv", Preheader->getTerminator());
760 }
Devang Patel901f67e2007-08-10 18:07:13 +0000761
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000762 //[*] Clone loop. Avoid true destination of split condition and
763 // the blocks dominated by true destination.
764 DenseMap<const Value *, Value *> ValueMap;
765 Loop *FalseLoop = CloneLoop(L, LPM, LI, ValueMap, this);
766 BasicBlock *FalseHeader = FalseLoop->getHeader();
767
768 //[*] True loop's exit edge enters False loop.
769 PHINode *IndVarClone = cast<PHINode>(ValueMap[IndVar]);
Devang Patel901f67e2007-08-10 18:07:13 +0000770 BasicBlock *ExitBlock = ExitCondition->getParent();
771 BranchInst *ExitInsn = dyn_cast<BranchInst>(ExitBlock->getTerminator());
772 assert (ExitInsn && "Unable to find suitable loop exit branch");
773 BasicBlock *ExitDest = ExitInsn->getSuccessor(1);
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000774
Devang Patel59e0c062007-08-14 01:30:57 +0000775 if (L->contains(ExitDest)) {
776 ExitDest = ExitInsn->getSuccessor(0);
777 ExitInsn->setSuccessor(0, FalseHeader);
778 } else
779 ExitInsn->setSuccessor(1, FalseHeader);
780
781 // Collect inverse map of Header PHINodes.
782 DenseMap<Value *, Value *> InverseMap;
783 for (BasicBlock::iterator BI = L->getHeader()->begin(),
784 BE = L->getHeader()->end(); BI != BE; ++BI) {
785 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
786 PHINode *PNClone = cast<PHINode>(ValueMap[PN]);
787 InverseMap[PNClone] = PN;
788 } else
789 break;
790 }
791
792 // Update False loop's header
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000793 for (BasicBlock::iterator BI = FalseHeader->begin(), BE = FalseHeader->end();
794 BI != BE; ++BI) {
795 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
796 PN->removeIncomingValue(Preheader);
797 if (PN == IndVarClone)
798 PN->addIncoming(FLStartValue, ExitBlock);
Devang Patel59e0c062007-08-14 01:30:57 +0000799 else {
800 PHINode *OrigPN = cast<PHINode>(InverseMap[PN]);
801 Value *V2 = OrigPN->getIncomingValueForBlock(ExitBlock);
802 PN->addIncoming(V2, ExitBlock);
803 }
804 } else
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000805 break;
806 }
807
Devang Patel59e0c062007-08-14 01:30:57 +0000808 // Update ExitDest. Now it's predecessor is False loop's exit block.
809 BasicBlock *ExitBlockClone = cast<BasicBlock>(ValueMap[ExitBlock]);
810 for (BasicBlock::iterator BI = ExitDest->begin(), BE = ExitDest->end();
811 BI != BE; ++BI) {
812 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
813 PN->addIncoming(ValueMap[PN->getIncomingValueForBlock(ExitBlock)], ExitBlockClone);
814 PN->removeIncomingValue(ExitBlock);
815 } else
816 break;
817 }
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000818
Devang Patelb7639612007-08-13 22:13:24 +0000819 if (DT) {
820 DT->changeImmediateDominator(FalseHeader, ExitBlock);
821 DT->changeImmediateDominator(ExitDest, cast<BasicBlock>(ValueMap[ExitBlock]));
822 }
823
Devang Patel901f67e2007-08-10 18:07:13 +0000824 assert (!L->contains(ExitDest) && " Unable to find exit edge destination");
Devang Patel901f67e2007-08-10 18:07:13 +0000825
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000826 //[*] Split Exit Edge.
827 SplitEdge(ExitBlock, FalseHeader, this);
Devang Patel901f67e2007-08-10 18:07:13 +0000828
Devang Patel61571ca2007-08-10 00:33:50 +0000829 //[*] Eliminate split condition's false branch from True loop.
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000830 BranchInst *BR = cast<BranchInst>(SplitBlock->getTerminator());
831 BasicBlock *FBB = BR->getSuccessor(1);
832 BR->setUnconditionalDest(BR->getSuccessor(0));
Devang Patel60a94c72007-08-14 18:35:57 +0000833 removeBlocks(FBB, L, BR->getSuccessor(0));
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000834
835 //[*] Update True loop's exit value using new exit value.
836 ExitCondition->setOperand(ExitValueNum, TLExitValue);
837
838 //[*] Eliminate split condition's true branch in False loop CFG.
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000839 BasicBlock *FSplitBlock = cast<BasicBlock>(ValueMap[SplitBlock]);
840 BranchInst *FBR = cast<BranchInst>(FSplitBlock->getTerminator());
841 BasicBlock *TBB = FBR->getSuccessor(0);
842 FBR->setUnconditionalDest(FBR->getSuccessor(1));
Devang Patel60a94c72007-08-14 18:35:57 +0000843 removeBlocks(TBB, FalseLoop, cast<BasicBlock>(FBR->getSuccessor(0)));
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000844
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000845 return true;
Devang Patelbc5fe632007-08-07 00:25:56 +0000846}
Devang Patel6a2d6ef2007-08-12 07:02:51 +0000847