llvm.org GIT mirror llvm / 9c52409
[GVN] Fold constant expression in GVN. Fix for PR 28418. opt never finishes compiling a test when -gvn option is passed. The problem is caused by the fact that GVN fails to fold a constant expression. Differential Revision: https://reviews.llvm.org/D22185 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275483 91177308-0d34-0410-b5e6-96231b3b80d8 Ekaterina Romanova 4 years ago
2 changed file(s) with 138 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
724724 assert(CanCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL) &&
725725 "precondition violation - materialization can't fail");
726726
727 if (auto *CExpr = dyn_cast(StoredVal))
728 StoredVal = ConstantFoldConstantExpression(CExpr, DL);
729
727730 // If this is already the right type, just return it.
728731 Type *StoredValTy = StoredVal->getType();
729732
734737 if (StoredValSize == LoadedValSize) {
735738 // Pointer to Pointer -> use bitcast.
736739 if (StoredValTy->getScalarType()->isPointerTy() &&
737 LoadedTy->getScalarType()->isPointerTy())
738 return IRB.CreateBitCast(StoredVal, LoadedTy);
739
740 // Convert source pointers to integers, which can be bitcast.
741 if (StoredValTy->getScalarType()->isPointerTy()) {
742 StoredValTy = DL.getIntPtrType(StoredValTy);
743 StoredVal = IRB.CreatePtrToInt(StoredVal, StoredValTy);
744 }
745
746 Type *TypeToCastTo = LoadedTy;
747 if (TypeToCastTo->getScalarType()->isPointerTy())
748 TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
749
750 if (StoredValTy != TypeToCastTo)
751 StoredVal = IRB.CreateBitCast(StoredVal, TypeToCastTo);
752
753 // Cast to pointer if the load needs a pointer type.
754 if (LoadedTy->getScalarType()->isPointerTy())
755 StoredVal = IRB.CreateIntToPtr(StoredVal, LoadedTy);
740 LoadedTy->getScalarType()->isPointerTy()) {
741 StoredVal = IRB.CreateBitCast(StoredVal, LoadedTy);
742 } else {
743 // Convert source pointers to integers, which can be bitcast.
744 if (StoredValTy->getScalarType()->isPointerTy()) {
745 StoredValTy = DL.getIntPtrType(StoredValTy);
746 StoredVal = IRB.CreatePtrToInt(StoredVal, StoredValTy);
747 }
748
749 Type *TypeToCastTo = LoadedTy;
750 if (TypeToCastTo->getScalarType()->isPointerTy())
751 TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
752
753 if (StoredValTy != TypeToCastTo)
754 StoredVal = IRB.CreateBitCast(StoredVal, TypeToCastTo);
755
756 // Cast to pointer if the load needs a pointer type.
757 if (LoadedTy->getScalarType()->isPointerTy())
758 StoredVal = IRB.CreateIntToPtr(StoredVal, LoadedTy);
759 }
760
761 if (auto *CExpr = dyn_cast(StoredVal))
762 StoredVal = ConstantFoldConstantExpression(CExpr, DL);
756763
757764 return StoredVal;
758765 }
787794 Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadedValSize);
788795 StoredVal = IRB.CreateTrunc(StoredVal, NewIntTy, "trunc");
789796
790 if (LoadedTy == NewIntTy)
791 return StoredVal;
792
793 // If the result is a pointer, inttoptr.
794 if (LoadedTy->getScalarType()->isPointerTy())
795 return IRB.CreateIntToPtr(StoredVal, LoadedTy, "inttoptr");
796
797 // Otherwise, bitcast.
798 return IRB.CreateBitCast(StoredVal, LoadedTy, "bitcast");
797 if (LoadedTy != NewIntTy) {
798 // If the result is a pointer, inttoptr.
799 if (LoadedTy->getScalarType()->isPointerTy())
800 StoredVal = IRB.CreateIntToPtr(StoredVal, LoadedTy, "inttoptr");
801 else
802 // Otherwise, bitcast.
803 StoredVal = IRB.CreateBitCast(StoredVal, LoadedTy, "bitcast");
804 }
805
806 if (auto *CExpr = dyn_cast(StoredVal))
807 StoredVal = ConstantFoldConstantExpression(CExpr, DL);
808
809 return StoredVal;
799810 }
800811
801812 /// This function is called when we have a
0 ; GVN failed to do constant expression folding and expanded
1 ; them unfolded in many places, producing exponentially large const
2 ; expressions. As a result, the compilation never fisished.
3 ; This test checks that we are folding constant expression
4 ; PR 28418
5 ; RUN: opt -gvn -S < %s | FileCheck %s
6
7 %2 = type { i32, i32, i32, i32, i32 }
8 define i32 @_Z16vector3util_mainv(i32 %x, i32 %y) {
9 %tmp1 = alloca %2, align 4
10 %tmp114 = getelementptr inbounds %2, %2* %tmp1, i64 0, i32 1
11 %tmp115 = bitcast i32* %tmp114 to <4 x i32>*
12 store <4 x i32> , <4 x i32>* %tmp115, align 4
13 %tmp1683 = getelementptr inbounds %2, %2* %tmp1, i64 0, i32 1
14 %tmp1688 = load i32, i32* %tmp1683, align 4
15 %tmp1693 = shl i32 %tmp1688, 5
16 %tmp1694 = xor i32 %tmp1693, %tmp1688
17 %tmp1695 = lshr i32 %tmp1694, 7
18 %tmp1696 = xor i32 %tmp1695, %tmp1694
19 %tmp1697 = shl i32 %tmp1696, 22
20 %tmp1698 = xor i32 %tmp1697, %tmp1696
21 %tmp1707 = shl i32 %tmp1698, 5
22 %tmp1708 = xor i32 %tmp1707, %tmp1698
23 %tmp1709 = lshr i32 %tmp1708, 7
24 %tmp1710 = xor i32 %tmp1709, %tmp1708
25 %tmp1711 = shl i32 %tmp1710, 22
26 %tmp1712 = xor i32 %tmp1711, %tmp1710
27 %tmp1721 = shl i32 %tmp1712, 5
28 %tmp1722 = xor i32 %tmp1721, %tmp1712
29 %tmp1723 = lshr i32 %tmp1722, 7
30 %tmp1724 = xor i32 %tmp1723, %tmp1722
31 %tmp1725 = shl i32 %tmp1724, 22
32 %tmp1726 = xor i32 %tmp1725, %tmp1724
33 %tmp1735 = shl i32 %tmp1726, 5
34 %tmp1736 = xor i32 %tmp1735, %tmp1726
35 %tmp1737 = lshr i32 %tmp1736, 7
36 %tmp1738 = xor i32 %tmp1737, %tmp1736
37 %tmp1739 = shl i32 %tmp1738, 22
38 %tmp1740 = xor i32 %tmp1739, %tmp1738
39 store i32 %tmp1740, i32* %tmp1683, align 4
40 ; CHECK: store i32 310393545, i32* %tmp114, align 4
41 %tmp1756 = getelementptr inbounds %2, %2* %tmp1, i64 0, i32 1
42 %tmp1761 = load i32, i32* %tmp1756, align 4
43 %tmp1766 = shl i32 %tmp1761, 5
44 %tmp1767 = xor i32 %tmp1766, %tmp1761
45 %tmp1768 = lshr i32 %tmp1767, 7
46 %tmp1769 = xor i32 %tmp1768, %tmp1767
47 %tmp1770 = shl i32 %tmp1769, 22
48 %tmp1771 = xor i32 %tmp1770, %tmp1769
49 %tmp1780 = shl i32 %tmp1771, 5
50 %tmp1781 = xor i32 %tmp1780, %tmp1771
51 %tmp1782 = lshr i32 %tmp1781, 7
52 %tmp1783 = xor i32 %tmp1782, %tmp1781
53 %tmp1784 = shl i32 %tmp1783, 22
54 %tmp1785 = xor i32 %tmp1784, %tmp1783
55 %tmp1794 = shl i32 %tmp1785, 5
56 %tmp1795 = xor i32 %tmp1794, %tmp1785
57 %tmp1796 = lshr i32 %tmp1795, 7
58 %tmp1797 = xor i32 %tmp1796, %tmp1795
59 %tmp1798 = shl i32 %tmp1797, 22
60 %tmp1799 = xor i32 %tmp1798, %tmp1797
61 %tmp1808 = shl i32 %tmp1799, 5
62 %tmp1809 = xor i32 %tmp1808, %tmp1799
63 %tmp1810 = lshr i32 %tmp1809, 7
64 %tmp1811 = xor i32 %tmp1810, %tmp1809
65 %tmp1812 = shl i32 %tmp1811, 22
66 %tmp1813 = xor i32 %tmp1812, %tmp1811
67 store i32 %tmp1813, i32* %tmp1756, align 4
68 ; CHECK: store i32 -383584258, i32* %tmp114, align 4
69 %tmp2645 = getelementptr inbounds %2, %2* %tmp1, i64 0, i32 1
70 %tmp2650 = load i32, i32* %tmp2645, align 4
71 %tmp2655 = shl i32 %tmp2650, 5
72 %tmp2656 = xor i32 %tmp2655, %tmp2650
73 %tmp2657 = lshr i32 %tmp2656, 7
74 %tmp2658 = xor i32 %tmp2657, %tmp2656
75 %tmp2659 = shl i32 %tmp2658, 22
76 %tmp2660 = xor i32 %tmp2659, %tmp2658
77 %tmp2669 = shl i32 %tmp2660, 5
78 %tmp2670 = xor i32 %tmp2669, %tmp2660
79 %tmp2671 = lshr i32 %tmp2670, 7
80 %tmp2672 = xor i32 %tmp2671, %tmp2670
81 %tmp2673 = shl i32 %tmp2672, 22
82 %tmp2674 = xor i32 %tmp2673, %tmp2672
83 %tmp2683 = shl i32 %tmp2674, 5
84 %tmp2684 = xor i32 %tmp2683, %tmp2674
85 %tmp2685 = lshr i32 %tmp2684, 7
86 %tmp2686 = xor i32 %tmp2685, %tmp2684
87 %tmp2687 = shl i32 %tmp2686, 22
88 %tmp2688 = xor i32 %tmp2687, %tmp2686
89 %tmp2697 = shl i32 %tmp2688, 5
90 %tmp2698 = xor i32 %tmp2697, %tmp2688
91 %tmp2699 = lshr i32 %tmp2698, 7
92 %tmp2700 = xor i32 %tmp2699, %tmp2698
93 %tmp2701 = shl i32 %tmp2700, 22
94 %tmp2702 = xor i32 %tmp2701, %tmp2700
95 store i32 %tmp2702, i32* %tmp2645, align 4
96 ; CHECK: store i32 -57163022, i32* %tmp114, align 4
97 ret i32 0
98 }