llvm.org GIT mirror llvm / 35115f9
Reapply 54786. Add overflow and number of mantissa bits checks. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@54821 91177308-0d34-0410-b5e6-96231b3b80d8 Devang Patel 11 years ago
2 changed file(s) with 217 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
4444 STATISTIC(NumInserted, "Number of PHIs inserted");
4545 STATISTIC(NumVariable, "Number of PHIs with variable strides");
4646 STATISTIC(NumEliminated , "Number of strides eliminated");
47 STATISTIC(NumShadow , "Number of Shadow IVs optimized");
4748
4849 namespace {
4950
176177 IVStrideUse* &CondUse,
177178 const SCEVHandle* &CondStride);
178179 void OptimizeIndvars(Loop *L);
180
181 /// OptimizeShadowIV - If IV is used in a int-to-float cast
182 /// inside the loop then try to eliminate the cast opeation.
183 void OptimizeShadowIV(Loop *L, ICmpInst *Cond,
184 const SCEVHandle *&CondStride);
179185 bool FindIVUserForCond(ICmpInst *Cond, IVStrideUse *&CondUse,
180 const SCEVHandle *&CondStride);
186 const SCEVHandle *&CondStride);
181187 bool RequiresTypeConversion(const Type *Ty, const Type *NewTy);
182188 unsigned CheckForIVReuse(bool, bool, const SCEVHandle&,
183189 IVExpr&, const Type*,
16881694 return Cond;
16891695 }
16901696
1697 /// OptimizeShadowIV - If IV is used in a int-to-float cast
1698 /// inside the loop then try to eliminate the cast opeation.
1699 void LoopStrengthReduce::OptimizeShadowIV(Loop *L, ICmpInst *Cond,
1700 const SCEVHandle *&CondStride) {
1701
1702 const SCEVConstant *SC = dyn_cast(*CondStride);
1703 if (!SC) return;
1704
1705 SCEVHandle IterationCount = SE->getIterationCount(L);
1706 if (isa(IterationCount))
1707 return;
1708
1709 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e;
1710 ++Stride) {
1711 std::map::iterator SI =
1712 IVUsesByStride.find(StrideOrder[Stride]);
1713 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1714
1715 for (std::vector::iterator UI = SI->second.Users.begin(),
1716 E = SI->second.Users.end(); UI != E; /* empty */) {
1717 std::vector::iterator CandidateUI = UI;
1718 UI++;
1719 Instruction *ShadowUse = CandidateUI->User;
1720 const Type *DestTy = NULL;
1721
1722 /* If shadow use is a int->float cast then insert a second IV
1723 to elminate this cast.
1724
1725 for (unsigned i = 0; i < n; ++i)
1726 foo((double)i);
1727
1728 is trnasformed into
1729
1730 double d = 0.0;
1731 for (unsigned i = 0; i < n; ++i, ++d)
1732 foo(d);
1733 */
1734 UIToFPInst *UCast = dyn_cast(CandidateUI->User);
1735 if (UCast)
1736 DestTy = UCast->getDestTy();
1737 else {
1738 SIToFPInst *SCast = dyn_cast(CandidateUI->User);
1739 if (!SCast) continue;
1740 DestTy = SCast->getDestTy();
1741 }
1742
1743 PHINode *PH = dyn_cast(ShadowUse->getOperand(0));
1744 if (!PH) continue;
1745 if (PH->getNumIncomingValues() != 2) continue;
1746
1747 const Type *SrcTy = PH->getType();
1748 int Mantissa = DestTy->getFPMantissaWidth();
1749 if (Mantissa == -1) continue;
1750 if ((int)TD->getTypeSizeInBits(SrcTy) > Mantissa)
1751 continue;
1752
1753 unsigned Entry, Latch;
1754 if (PH->getIncomingBlock(0) == L->getLoopPreheader()) {
1755 Entry = 0;
1756 Latch = 1;
1757 } else {
1758 Entry = 1;
1759 Latch = 0;
1760 }
1761
1762 ConstantInt *Init = dyn_cast(PH->getIncomingValue(Entry));
1763 if (!Init) continue;
1764 ConstantFP *NewInit = ConstantFP::get(DestTy, Init->getZExtValue());
1765
1766 BinaryOperator *Incr =
1767 dyn_cast(PH->getIncomingValue(Latch));
1768 if (!Incr) continue;
1769 if (Incr->getOpcode() != Instruction::Add
1770 && Incr->getOpcode() != Instruction::Sub)
1771 continue;
1772
1773 /* Initialize new IV, double d = 0.0 in above example. */
1774 ConstantInt *C = NULL;
1775 if (Incr->getOperand(0) == PH)
1776 C = dyn_cast(Incr->getOperand(1));
1777 else if (Incr->getOperand(1) == PH)
1778 C = dyn_cast(Incr->getOperand(0));
1779 else
1780 continue;
1781
1782 if (!C) continue;
1783
1784 /* Add new PHINode. */
1785 PHINode *NewPH = PHINode::Create(DestTy, "IV.S.", PH);
1786
1787 /* create new icnrement. '++d' in above example. */
1788 ConstantFP *CFP = ConstantFP::get(DestTy, C->getZExtValue());
1789 BinaryOperator *NewIncr =
1790 BinaryOperator::Create(Incr->getOpcode(),
1791 NewPH, CFP, "IV.S.next.", Incr);
1792
1793 NewPH->addIncoming(NewInit, PH->getIncomingBlock(Entry));
1794 NewPH->addIncoming(NewIncr, PH->getIncomingBlock(Latch));
1795
1796 /* Remove cast operation */
1797 ShadowUse->replaceAllUsesWith(NewPH);
1798 ShadowUse->eraseFromParent();
1799 SI->second.Users.erase(CandidateUI);
1800 NumShadow++;
1801 break;
1802 }
1803 }
1804 }
1805
16911806 // OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
16921807 // uses in the loop, look to see if we can eliminate some, in favor of using
16931808 // common indvars for the different uses.
17141829
17151830 if (!FindIVUserForCond(Cond, CondUse, CondStride))
17161831 return; // setcc doesn't use the IV.
1832
1833 OptimizeShadowIV(L, Cond, CondStride);
17171834
17181835 // If possible, change stride and operands of the compare instruction to
17191836 // eliminate one stride.
0 ; RUN: llvm-as < %s | opt -loop-reduce | llvm-dis | grep "phi double" | count 1
1
2 define void @foobar(i32 %n) nounwind {
3 entry:
4 icmp eq i32 %n, 0 ; :0 [#uses=2]
5 br i1 %0, label %return, label %bb.nph
6
7 bb.nph: ; preds = %entry
8 %umax = select i1 %0, i32 1, i32 %n ; [#uses=1]
9 br label %bb
10
11 bb: ; preds = %bb, %bb.nph
12 %i.03 = phi i32 [ 0, %bb.nph ], [ %indvar.next, %bb ] ; [#uses=3]
13 tail call void @bar( i32 %i.03 ) nounwind
14 uitofp i32 %i.03 to double ; :1 [#uses=1]
15 tail call void @foo( double %1 ) nounwind
16 %indvar.next = add i32 %i.03, 1 ; [#uses=2]
17 %exitcond = icmp eq i32 %indvar.next, %umax ; [#uses=1]
18 br i1 %exitcond, label %return, label %bb
19
20 return: ; preds = %bb, %entry
21 ret void
22 }
23
24 ; Unable to eliminate cast because the mantissa bits for double are not enough
25 ; to hold all of i64 IV bits.
26 define void @foobar2(i64 %n) nounwind {
27 entry:
28 icmp eq i64 %n, 0 ; :0 [#uses=2]
29 br i1 %0, label %return, label %bb.nph
30
31 bb.nph: ; preds = %entry
32 %umax = select i1 %0, i64 1, i64 %n ; [#uses=1]
33 br label %bb
34
35 bb: ; preds = %bb, %bb.nph
36 %i.03 = phi i64 [ 0, %bb.nph ], [ %indvar.next, %bb ] ; [#uses=3]
37 trunc i64 %i.03 to i32 ; :1 [#uses=1]
38 tail call void @bar( i32 %1 ) nounwind
39 uitofp i64 %i.03 to double ; :2 [#uses=1]
40 tail call void @foo( double %2 ) nounwind
41 %indvar.next = add i64 %i.03, 1 ; [#uses=2]
42 %exitcond = icmp eq i64 %indvar.next, %umax ; [#uses=1]
43 br i1 %exitcond, label %return, label %bb
44
45 return: ; preds = %bb, %entry
46 ret void
47 }
48
49 ; Unable to eliminate cast due to potentional overflow.
50 define void @foobar3() nounwind {
51 entry:
52 tail call i32 (...)* @nn( ) nounwind ; :0 [#uses=1]
53 icmp eq i32 %0, 0 ; :1 [#uses=1]
54 br i1 %1, label %return, label %bb
55
56 bb: ; preds = %bb, %entry
57 %i.03 = phi i32 [ 0, %entry ], [ %3, %bb ] ; [#uses=3]
58 tail call void @bar( i32 %i.03 ) nounwind
59 uitofp i32 %i.03 to double ; :2 [#uses=1]
60 tail call void @foo( double %2 ) nounwind
61 add i32 %i.03, 1 ; :3 [#uses=2]
62 tail call i32 (...)* @nn( ) nounwind ; :4 [#uses=1]
63 icmp ugt i32 %4, %3 ; :5 [#uses=1]
64 br i1 %5, label %bb, label %return
65
66 return: ; preds = %bb, %entry
67 ret void
68 }
69
70 ; Unable to eliminate cast due to overflow.
71 define void @foobar4() nounwind {
72 entry:
73 br label %bb.nph
74
75 bb.nph: ; preds = %entry
76 br label %bb
77
78 bb: ; preds = %bb, %bb.nph
79 %i.03 = phi i8 [ 0, %bb.nph ], [ %indvar.next, %bb ] ; [#uses=3]
80 %tmp2 = sext i8 %i.03 to i32 ; :0 [#uses=1]
81 tail call void @bar( i32 %tmp2 ) nounwind
82 %tmp3 = uitofp i8 %i.03 to double ; :1 [#uses=1]
83 tail call void @foo( double %tmp3 ) nounwind
84 %indvar.next = add i8 %i.03, 1 ; [#uses=2]
85 %tmp = sext i8 %indvar.next to i32
86 %exitcond = icmp eq i32 %tmp, 32767 ; [#uses=1]
87 br i1 %exitcond, label %return, label %bb
88
89 return: ; preds = %bb, %entry
90 ret void
91 }
92
93 declare void @bar(i32)
94
95 declare void @foo(double)
96
97 declare i32 @nn(...)
98