llvm.org GIT mirror llvm / e5f7687
When determining a canonical insert position, don't climb deeper into adjacent loops. Also, ensure that the insert position is dominated by the loop latch of any loop in the post-inc set which has a latch. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@100906 91177308-0d34-0410-b5e6-96231b3b80d8 Dan Gohman 9 years ago
2 changed file(s) with 104 addition(s) and 34 deletion(s). Raw diff Collapse all Expand all
11561156 IVUsers &IU;
11571157 ScalarEvolution &SE;
11581158 DominatorTree &DT;
1159 LoopInfo &LI;
11591160 const TargetLowering *const TLI;
11601161 Loop *const L;
11611162 bool Changed;
12351236 DenseSet &VisitedRegs) const;
12361237 void Solve(SmallVectorImpl &Solution) const;
12371238
1238 BasicBlock::iterator AdjustInputPositionForExpand(BasicBlock::iterator IP,
1239 const LSRFixup &LF,
1240 const LSRUse &LU) const;
1239 BasicBlock::iterator
1240 HoistInsertPosition(BasicBlock::iterator IP,
1241 const SmallVectorImpl &Inputs) const;
1242 BasicBlock::iterator AdjustInsertPositionForExpand(BasicBlock::iterator IP,
1243 const LSRFixup &LF,
1244 const LSRUse &LU) const;
12411245
12421246 Value *Expand(const LSRFixup &LF,
12431247 const Formula &F,
28122816 return Node->getBlock();
28132817 }
28142818
2815 /// AdjustInputPositionForExpand - Determine an input position which will be
2819 /// HoistInsertPosition - Helper for AdjustInsertPositionForExpand. Climb up
2820 /// the dominator tree far as we can go while still being dominated by the
2821 /// input positions. This helps canonicalize the insert position, which
2822 /// encourages sharing.
2823 BasicBlock::iterator
2824 LSRInstance::HoistInsertPosition(BasicBlock::iterator IP,
2825 const SmallVectorImpl &Inputs)
2826 const {
2827 for (;;) {
2828 const Loop *IPLoop = LI.getLoopFor(IP->getParent());
2829 unsigned IPLoopDepth = IPLoop ? IPLoop->getLoopDepth() : 0;
2830
2831 BasicBlock *IDom;
2832 for (BasicBlock *Rung = IP->getParent(); ; Rung = IDom) {
2833 IDom = getImmediateDominator(Rung, DT);
2834 if (!IDom) return IP;
2835
2836 // Don't climb into a loop though.
2837 const Loop *IDomLoop = LI.getLoopFor(IDom);
2838 unsigned IDomDepth = IDomLoop ? IDomLoop->getLoopDepth() : 0;
2839 if (IDomDepth <= IPLoopDepth &&
2840 (IDomDepth != IPLoopDepth || IDomLoop == IPLoop))
2841 break;
2842 }
2843
2844 bool AllDominate = true;
2845 Instruction *BetterPos = 0;
2846 Instruction *Tentative = IDom->getTerminator();
2847 for (SmallVectorImpl::const_iterator I = Inputs.begin(),
2848 E = Inputs.end(); I != E; ++I) {
2849 Instruction *Inst = *I;
2850 if (Inst == Tentative || !DT.dominates(Inst, Tentative)) {
2851 AllDominate = false;
2852 break;
2853 }
2854 // Attempt to find an insert position in the middle of the block,
2855 // instead of at the end, so that it can be used for other expansions.
2856 if (IDom == Inst->getParent() &&
2857 (!BetterPos || DT.dominates(BetterPos, Inst)))
2858 BetterPos = next(BasicBlock::iterator(Inst));
2859 }
2860 if (!AllDominate)
2861 break;
2862 if (BetterPos)
2863 IP = BetterPos;
2864 else
2865 IP = Tentative;
2866 }
2867
2868 return IP;
2869 }
2870
2871 /// AdjustInsertPositionForExpand - Determine an input position which will be
28162872 /// dominated by the operands and which will dominate the result.
28172873 BasicBlock::iterator
2818 LSRInstance::AdjustInputPositionForExpand(BasicBlock::iterator IP,
2819 const LSRFixup &LF,
2820 const LSRUse &LU) const {
2874 LSRInstance::AdjustInsertPositionForExpand(BasicBlock::iterator IP,
2875 const LSRFixup &LF,
2876 const LSRUse &LU) const {
28212877 // Collect some instructions which must be dominated by the
28222878 // expanding replacement. These must be dominated by any operands that
28232879 // will be required in the expansion.
28412897 const Loop *PIL = *I;
28422898 if (PIL == L) continue;
28432899
2900 // Be dominated by the loop exit.
28442901 SmallVector ExitingBlocks;
28452902 PIL->getExitingBlocks(ExitingBlocks);
28462903 if (!ExitingBlocks.empty()) {
28492906 BB = DT.findNearestCommonDominator(BB, ExitingBlocks[i]);
28502907 Inputs.push_back(BB->getTerminator());
28512908 }
2909
2910 // Be dominated by the loop latch, if it's unique.
2911 if (BasicBlock *Latch = PIL->getLoopLatch())
2912 Inputs.push_back(prior(BasicBlock::iterator(Latch->getTerminator())));
28522913 }
28532914
28542915 // Then, climb up the immediate dominator tree as far as we can go while
28552916 // still being dominated by the input positions.
2856 for (;;) {
2857 bool AllDominate = true;
2858 Instruction *BetterPos = 0;
2859 BasicBlock *IDom = getImmediateDominator(IP->getParent(), DT);
2860 if (!IDom) break;
2861 Instruction *Tentative = IDom->getTerminator();
2862 for (SmallVectorImpl::const_iterator I = Inputs.begin(),
2863 E = Inputs.end(); I != E; ++I) {
2864 Instruction *Inst = *I;
2865 if (Inst == Tentative || !DT.dominates(Inst, Tentative)) {
2866 AllDominate = false;
2867 break;
2868 }
2869 if (IDom == Inst->getParent() &&
2870 (!BetterPos || DT.dominates(BetterPos, Inst)))
2871 BetterPos = next(BasicBlock::iterator(Inst));
2872 }
2873 if (!AllDominate)
2874 break;
2875 if (BetterPos)
2876 IP = BetterPos;
2877 else
2878 IP = Tentative;
2879 }
2917 IP = HoistInsertPosition(IP, Inputs);
28802918
28812919 // Don't insert instructions before PHI nodes.
28822920 while (isa(IP)) ++IP;
28962934
28972935 // Determine an input position which will be dominated by the operands and
28982936 // which will dominate the result.
2899 IP = AdjustInputPositionForExpand(IP, LF, LU);
2937 IP = AdjustInsertPositionForExpand(IP, LF, LU);
29002938
29012939 // Inform the Rewriter if we have a post-increment use, so that it can
29022940 // perform an advantageous expansion.
31743212 : IU(P->getAnalysis()),
31753213 SE(P->getAnalysis()),
31763214 DT(P->getAnalysis()),
3215 LI(P->getAnalysis()),
31773216 TLI(tli), L(l), Changed(false), IVIncInsertPos(0) {
31783217
31793218 // If LoopSimplify form is not available, stay out of trouble.
33303369 // We split critical edges, so we change the CFG. However, we do update
33313370 // many analyses if they are around.
33323371 AU.addPreservedID(LoopSimplifyID);
3372 AU.addPreserved("domfrontier");
3373
3374 AU.addRequired();
33333375 AU.addPreserved();
3334 AU.addPreserved("domfrontier");
3335
33363376 AU.addRequiredID(LoopSimplifyID);
33373377 AU.addRequired();
33383378 AU.addPreserved();
3434 store i8 undef, i8* %t9
3535 br label %bb14
3636 }
37
38 define fastcc void @TransformLine() nounwind {
39 bb:
40 br label %loop0
41
42 loop0: ; preds = %loop0, %bb
43 %i0 = phi i32 [ %i0.next, %loop0 ], [ 0, %bb ] ; [#uses=2]
44 %i0.next = add i32 %i0, 1 ; [#uses=1]
45 br i1 false, label %loop0, label %bb0
46
47 bb0: ; preds = %loop0
48 br label %loop1
49
50 loop1: ; preds = %bb5, %bb0
51 %i1 = phi i32 [ 0, %bb0 ], [ %i1.next, %bb5 ] ; [#uses=4]
52 %t0 = add i32 %i0, %i1 ; [#uses=1]
53 br i1 false, label %bb2, label %bb6
54
55 bb2: ; preds = %loop1
56 br i1 true, label %bb6, label %bb5
57
58 bb5: ; preds = %bb2
59 %i1.next = add i32 %i1, 1 ; [#uses=1]
60 br i1 true, label %bb6, label %loop1
61
62 bb6: ; preds = %bb5, %bb2, %loop1
63 %p8 = phi i32 [ %t0, %bb5 ], [ undef, %loop1 ], [ undef, %bb2 ] ; [#uses=0]
64 %p9 = phi i32 [ undef, %bb5 ], [ %i1, %loop1 ], [ %i1, %bb2 ] ; [#uses=0]
65 unreachable
66 }