summaryrefslogtreecommitdiff
path: root/0045-Transposed-SLP-Enable-Transposed-SLP.patch
blob: 6323e8d915b6e25cbc3b1e905930dabc5683da80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
From 639b5248cbab1806618545fc30215ed9d1a019e7 Mon Sep 17 00:00:00 2001
From: luohailing <luo_hailing@qq.com>
Date: Fri, 17 Jun 2022 22:38:55 +0800
Subject: [PATCH 11/12] [Transposed SLP] Enable Transposed SLP     Enable
 Transposed SLP when memory is uncontinual     with
 -ftree-slp-transpose-vectorize.

---
 gcc/common.opt                          |    4 +
 gcc/testsuite/gcc.dg/vect/transpose-1.c |   53 ++
 gcc/testsuite/gcc.dg/vect/transpose-2.c |   50 ++
 gcc/testsuite/gcc.dg/vect/transpose-3.c |   54 ++
 gcc/testsuite/gcc.dg/vect/transpose-4.c |   53 ++
 gcc/testsuite/gcc.dg/vect/transpose-5.c |   73 ++
 gcc/testsuite/gcc.dg/vect/transpose-6.c |   67 ++
 gcc/testsuite/gcc.dg/vect/transpose-7.c |   53 ++
 gcc/testsuite/gcc.dg/vect/transpose-8.c |   53 ++
 gcc/testsuite/gcc.dg/vect/vect.exp      |    7 +
 gcc/tree-vect-data-refs.c               |  236 +++++
 gcc/tree-vect-slp.c                     | 1090 ++++++++++++++++++++++-
 gcc/tree-vect-stmts.c                   |  763 +++++++++++++++-
 gcc/tree-vectorizer.h                   |   89 ++
 14 files changed, 2641 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-1.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-2.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-3.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-4.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-5.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-6.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-7.c
 create mode 100644 gcc/testsuite/gcc.dg/vect/transpose-8.c

diff --git a/gcc/common.opt b/gcc/common.opt
index 24834cf60..d38401b71 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -3049,6 +3049,10 @@ ftree-vect-analyze-slp-group
 Common Report Var(flag_tree_slp_group) Init(0)
 Disable SLP vectorization for reduction chain on tree.
 
+ftree-slp-transpose-vectorize
+Common Report Var(flag_tree_slp_transpose_vectorize) Optimization Init(0)
+Enable basic block vectorization (SLP) for transposed stores and loads on trees.
+
 fvect-cost-model=
 Common Joined RejectNegative Enum(vect_cost_model) Var(flag_vect_cost_model) Init(VECT_COST_MODEL_DEFAULT) Optimization
 -fvect-cost-model=[unlimited|dynamic|cheap]	Specifies the cost model for vectorization.
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-1.c b/gcc/testsuite/gcc.dg/vect/transpose-1.c
new file mode 100644
index 000000000..8237a8b9e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-1.c
@@ -0,0 +1,53 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-require-effective-target vect_int } */
+#include <stdio.h>
+#include <stdlib.h>
+#include "tree-vect.h"
+
+#define N 4
+#define M 256
+
+int foo (unsigned char *pix1, int i_pix1, unsigned char *pix2, int i_pix2)
+{
+  int i = 0;
+  int sum = 0;
+  unsigned c0[N], c1[N], c2[N], c3[N], c4[N], c5[N], c6[N], c7[N];
+  for (i = 0; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+      c2[i] = pix1[2] - pix2[2];
+      c3[i] = pix1[3] - pix2[3];
+      c4[i] = pix1[4] - pix2[4];
+      c5[i] = pix1[5] - pix2[5];
+      c6[i] = pix1[6] - pix2[6];
+      c7[i] = pix1[7] - pix2[7];
+    }
+  for (int i = 0; i < N; i++)
+    {
+      sum += c0[i] + c1[i] + c2[i] + c3[i] + c4[i] + c5[i] + c6[i] + c7[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned char input1[M];
+  unsigned char input2[M];
+  int i1 = 16;
+  int i2 = 8;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 2;
+	input2[i] = i;
+    }
+  int sum = foo (input1, i1, input2, i2);
+  if (sum != 1264)
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-2.c b/gcc/testsuite/gcc.dg/vect/transpose-2.c
new file mode 100644
index 000000000..b01a0410e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-2.c
@@ -0,0 +1,50 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-additional-options "-fno-tree-loop-vectorize" } */
+/* { dg-require-effective-target vect_int } */
+#include <stdio.h>
+#include <stdlib.h>
+#include "tree-vect.h"
+
+#define N 8
+#define M 256
+
+int foo (unsigned char *pix1, int i_pix1, unsigned char *pix2, int i_pix2)
+{
+  int i = 0;
+  int sum = 0;
+  unsigned short c0[N], c1[N], c2[N], c3[N], c4[N], c5[N], c6[N], c7[N];
+  for (i = 0; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+      c2[i] = pix1[2] - pix2[2];
+      c3[i] = pix1[3] - pix2[3];
+    }
+  for (int i = 0; i < N; i++)
+    {
+      sum += c0[i] + c1[i] + c2[i] + c3[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned char input1[M];
+  unsigned char input2[M];
+  int i1 = 5;
+  int i2 = 4;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 4;
+	input2[i] = i * 2;
+    }
+  int sum = foo (input1, i1, input2, i2);
+  if (sum != 1440)
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-3.c b/gcc/testsuite/gcc.dg/vect/transpose-3.c
new file mode 100644
index 000000000..529581c59
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-3.c
@@ -0,0 +1,54 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-additional-options "-fno-tree-loop-vectorize" } */
+/* { dg-require-effective-target vect_int } */
+#include <stdio.h>
+#include <stdlib.h>
+#include "tree-vect.h"
+
+#define N 4
+#define M 256
+
+int foo (unsigned short *pix1, int i_pix1, unsigned short *pix2, int i_pix2)
+{
+  int i = 0;
+  int sum = 0;
+  unsigned c0[N], c1[N], c2[N], c3[N], c4[N], c5[N], c6[N], c7[N];
+  for (i = 0; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+      c2[i] = pix1[2] - pix2[2];
+      c3[i] = pix1[3] - pix2[3];
+      c4[i] = pix1[4] - pix2[4];
+      c5[i] = pix1[5] - pix2[5];
+      c6[i] = pix1[6] - pix2[6];
+      c7[i] = pix1[7] - pix2[7];
+    }
+  for (int i = 0; i < N; i++)
+     {
+      sum += c0[i] + c1[i] + c2[i] + c3[i] + c4[i] + c5[i] + c6[i] + c7[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned short input1[M];
+  unsigned short input2[M];
+  int i1 = 8;
+  int i2 = 4;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 4;
+	input2[i] = i;
+    }
+  int sum = foo (input1, i1, input2, i2);
+  if (sum != 1680)
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-4.c b/gcc/testsuite/gcc.dg/vect/transpose-4.c
new file mode 100644
index 000000000..0b4adea9b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-4.c
@@ -0,0 +1,53 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-require-effective-target vect_int } */
+#include <stdio.h>
+#include <stdlib.h>
+#include "tree-vect.h"
+
+#define N 4
+#define M 256
+
+int foo (unsigned *pix1, int i_pix1, unsigned *pix2, int i_pix2)
+{
+  int i = 0;
+  int sum = 0;
+  unsigned c0[N], c1[N], c2[N], c3[N], c4[N], c5[N], c6[N], c7[N];
+  for (i = 0; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+      c2[i] = pix1[2] - pix2[2];
+      c3[i] = pix1[3] - pix2[3];
+      c4[i] = pix1[4] - pix2[4];
+      c5[i] = pix1[5] - pix2[5];
+      c6[i] = pix1[6] - pix2[6];
+      c7[i] = pix1[7] - pix2[7];
+    }
+  for (int i = 0; i < N; i++)
+     {
+      sum += c0[i] + c1[i] + c2[i] + c3[i] + c4[i] + c5[i] + c6[i] + c7[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned input1[M];
+  unsigned input2[M];
+  int i1 = 12;
+  int i2 = 6;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 7;
+	input2[i] = i * 3;
+    }
+  int sum = foo (input1, i1, input2, i2);
+  if (sum != 3616)
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-5.c b/gcc/testsuite/gcc.dg/vect/transpose-5.c
new file mode 100644
index 000000000..81a248840
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-5.c
@@ -0,0 +1,73 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-require-effective-target vect_int } */
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include "tree-vect.h"
+
+#define N 4
+#define M 256
+#define eps 1e-8
+
+double foo (unsigned char *pix1, int i_pix1, unsigned char *pix2, int i_pix2)
+{
+  unsigned a0[N];
+  unsigned a1[N];
+  unsigned a2[N];
+  unsigned a3[N];
+
+  int b0[N];
+  int b1[N];
+  int b2[N];
+  int b3[N];
+
+  for (int i = 0; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      a0[i] = (pix1[0] - pix2[0]) + ((pix1[4] + pix2[4]) << 16);
+      a1[i] = (pix1[1] - pix2[1]) + ((pix1[5] + pix2[5]) << 16);
+      a2[i] = (pix1[2] - pix2[2]) + ((pix1[6] + pix2[6]) << 16);
+      a3[i] = (pix1[3] - pix2[3]) + ((pix1[7] + pix2[7]) << 16);
+    }
+
+  for (int i = 0; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      b0[i] = (pix1[0] - pix2[0]) + (pix1[4] + pix2[4]);
+      b1[i] = (pix1[1] - pix2[1]) + (pix1[5] + pix2[5]);
+      b2[i] = (pix1[2] - pix2[2]) + (pix1[6] + pix2[6]);
+      b3[i] = (pix1[3] - pix2[3]) + (pix1[7] + pix2[7]);
+    }
+
+  double sum = 0;
+  for (int i = 0; i < N; i++)
+    {
+      sum += a0[i] + a1[i] + a2[i] + a3[i] + b0[i] + b1[i] + b2[i] + b3[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned char input1[M];
+  unsigned char input2[M];
+  int i1 = 8;
+  int i2 = 3;
+  unsigned char m = 2;
+  unsigned short n = 12;
+  float t = 3.0;
+  double k = 4.2;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 6;
+	input2[i] = i * 3;
+    }
+  double sum = foo (input1, i1, input2, i2);
+  if (fabs (sum - 78648144) > eps)
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
+/* { dg-final { scan-tree-dump-times "vectorizable_store for slp transpose" 2 "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-6.c b/gcc/testsuite/gcc.dg/vect/transpose-6.c
new file mode 100644
index 000000000..3e134ac02
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-6.c
@@ -0,0 +1,67 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-require-effective-target vect_int } */
+/* { dg-require-effective-target vect_float } */
+#include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include "tree-vect.h"
+
+#define N 4
+#define M 256
+#define eps 1e-8
+
+float foo (unsigned char *pix1, int i_pix1, unsigned char *pix2, int i_pix2)
+{
+  unsigned a0[N];
+  unsigned a1[N];
+  unsigned a2[N];
+  unsigned a3[N];
+
+  float c0[N];
+  float c1[N];
+  float c2[N];
+  float c3[N];
+
+  for (int i = 0; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      a0[i] = (pix1[0] - pix2[0]) + ((pix1[4] - pix2[4]) << 16);
+      a1[i] = (pix1[1] - pix2[1]) + ((pix1[5] - pix2[5]) << 16);
+      a2[i] = (pix1[2] - pix2[2]) + ((pix1[6] - pix2[6]) << 16);
+      a3[i] = (pix1[3] - pix2[3]) + ((pix1[7] - pix2[7]) << 16);
+
+      c0[i] = (pix1[0] * pix2[0]) + (pix1[4] * pix2[4]);
+      c1[i] = (pix1[1] * pix2[1]) + (pix1[5] * pix2[5]);
+      c2[i] = (pix1[2] * pix2[2]) + (pix1[6] * pix2[6]);
+      c3[i] = (pix1[3] * pix2[3]) + (pix1[7] * pix2[7]);
+    }
+
+  float sum = 0;
+  for (int i = 0; i < N; i++)
+    {
+      sum += a0[i] + a1[i] + a2[i] + a3[i] + c0[i] + c1[i] + c2[i] + c3[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned char input1[M];
+  unsigned char input2[M];
+  int i1 = 18;
+  int i2 = 6;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 4;
+	input2[i] = i * 2;
+    }
+  float sum = foo (input1, i1, input2, i2);
+  if (fabs (sum - 106041168) > eps) 
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
+/* { dg-final { scan-tree-dump-times "vectorizable_store for slp transpose" 2 "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-7.c b/gcc/testsuite/gcc.dg/vect/transpose-7.c
new file mode 100644
index 000000000..2074d9aa8
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-7.c
@@ -0,0 +1,53 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-additional-options "-fno-tree-loop-vectorize" } */
+/* { dg-require-effective-target vect_int } */
+#include <stdio.h>
+#include <stdlib.h>
+#include "tree-vect.h"
+
+#define N 16
+#define M 256
+
+int foo (unsigned char *pix1, int i_pix1, unsigned char *pix2, int i_pix2)
+{
+  int i = 0;
+  int sum = 0;
+  unsigned char c0[N], c1[N];
+  for (int i = 0; i < N/2; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+    }
+  for (int i = N/2; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+   }
+  for (int i = 0; i < N; i++)
+    {
+      sum += c0[i] + c1[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned char input1[M];
+  unsigned char input2[M];
+  int i1 = 6;
+  int i2 = 4;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 5;
+	input2[i] = i * 2;
+    }
+  int sum = foo (input1, i1, input2, i2);
+  if (sum != 3280)
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/transpose-8.c b/gcc/testsuite/gcc.dg/vect/transpose-8.c
new file mode 100644
index 000000000..a154f012a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/transpose-8.c
@@ -0,0 +1,53 @@
+/* { dg-do compile { target { aarch64*-*-linux* } } } */
+/* { dg-additional-options "-fno-tree-loop-vectorize" } */
+/* { dg-require-effective-target vect_int } */
+#include <stdio.h>
+#include <stdlib.h>
+#include "tree-vect.h"
+
+#define N 32
+#define M 256
+
+int foo (unsigned char *pix1, int i_pix1, unsigned char *pix2, int i_pix2)
+{
+  int i = 0;
+  int sum = 0;
+  unsigned char c0[N], c1[N];
+  for (int i = 0; i < N/2; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+    }
+  for (int i = N/2; i < N; i++, pix1 += i_pix1, pix2 += i_pix2)
+    {
+      c0[i] = pix1[0] - pix2[0];
+      c1[i] = pix1[1] - pix2[1];
+   }
+  for (int i = 0; i < N; i++)
+    {
+      sum += c0[i] + c1[i];
+    }
+  return sum;
+}
+
+int main (int argc, const char* argv[])
+{
+  unsigned char input1[M];
+  unsigned char input2[M];
+  int i1 = 6;
+  int i2 = 4;
+  check_vect ();
+  for (int i = 0; i < M; i++)
+    {
+	input1[i] = i * 5;
+	input2[i] = i * 2;
+    }
+  int sum = foo (input1, i1, input2, i2);
+  if (sum != 7584)
+    {
+      abort ();
+    }
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "vectorized using transposed version" "slp1" } } */
diff --git a/gcc/testsuite/gcc.dg/vect/vect.exp b/gcc/testsuite/gcc.dg/vect/vect.exp
index efe17ac6f..d92e1ba5b 100644
--- a/gcc/testsuite/gcc.dg/vect/vect.exp
+++ b/gcc/testsuite/gcc.dg/vect/vect.exp
@@ -114,6 +114,13 @@ et-dg-runtest dg-runtest [lsort \
 	[glob -nocomplain $srcdir/$subdir/no-vfa-*.\[cS\]]] \
 	"" $DEFAULT_VECTCFLAGS
 
+# -ftree-slp-transpose-vectorize SLP tests
+set VECT_SLP_CFLAGS $SAVED_VECT_SLP_CFLAGS
+lappend VECT_SLP_CFLAGS "-ftree-slp-transpose-vectorize"
+et-dg-runtest dg-runtest [lsort \
+	[glob -nocomplain $srcdir/$subdir/transpose-*.\[cS\]]] \
+	"" "-ftree-slp-transpose-vectorize -fdump-tree-slp-details -O3"
+
 # -ffast-math tests
 set DEFAULT_VECTCFLAGS $SAVED_DEFAULT_VECTCFLAGS
 lappend DEFAULT_VECTCFLAGS "-ffast-math"
diff --git a/gcc/tree-vect-data-refs.c b/gcc/tree-vect-data-refs.c
index fcc0726bd..d78b06455 100644
--- a/gcc/tree-vect-data-refs.c
+++ b/gcc/tree-vect-data-refs.c
@@ -2647,6 +2647,9 @@ vect_analyze_group_access_1 (dr_vec_info *dr_info)
       DR_GROUP_GAP (stmt_info) = groupsize - last_accessed_element;
 
       DR_GROUP_SIZE (stmt_info) = groupsize;
+
+      DR_GROUP_SLP_TRANSPOSE (stmt_info) = false;
+
       if (dump_enabled_p ())
 	{
 	  dump_printf_loc (MSG_NOTE, vect_location,
@@ -2676,6 +2679,20 @@ vect_analyze_group_access_1 (dr_vec_info *dr_info)
 			     DR_GROUP_GAP (stmt_info));
 	}
 
+      /* SLP: create an SLP data structure for every interleaving group of
+	 loads for further analysis in vect_analyse_slp.  */
+      if (DR_IS_READ (dr) && !slp_impossible)
+	{
+	  if (loop_vinfo)
+	    {
+	      LOOP_VINFO_GROUPED_LOADS (loop_vinfo).safe_push (stmt_info);
+	    }
+	  if (bb_vinfo)
+	    {
+	      BB_VINFO_GROUPED_LOADS (bb_vinfo).safe_push (stmt_info);
+	    }
+	}
+
       /* SLP: create an SLP data structure for every interleaving group of
 	 stores for further analysis in vect_analyse_slp.  */
       if (DR_IS_WRITE (dr) && !slp_impossible)
@@ -5413,6 +5430,225 @@ vect_permute_store_chain (vec<tree> dr_chain,
     }
 }
 
+/* Encoding the PERM_MASK_FIRST.  */
+
+static void
+vect_indices_encoding_first (tree vectype, unsigned int array_num,
+			     tree &perm_mask_high_first,
+			     tree &perm_mask_low_first)
+{
+  unsigned int nelt = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
+  vec_perm_builder sel (nelt, nelt, 1);
+  sel.quick_grow (nelt);
+  unsigned int group_num = nelt / array_num;
+  unsigned int index = 0;
+  unsigned int array = 0;
+  unsigned int group = 0;
+
+  /* The encoding has 1 pattern in the fisrt stage.  */
+  for (array = 0; array < array_num / 2; array++)
+    {
+      for (group = 0; group < group_num * 2; group++)
+	{
+	  sel[index++] = array + array_num * group;
+	}
+    }
+  vec_perm_indices indices (sel, 2, nelt);
+  perm_mask_high_first = vect_gen_perm_mask_checked (vectype, indices);
+
+  index = 0;
+  for (array = array_num / 2; array < array_num; array++)
+    {
+      for (group = 0; group < group_num * 2; group++)
+	{
+	  sel[index++] = array + array_num * group;
+	}
+    }
+  indices.new_vector (sel, 2, nelt);
+  perm_mask_low_first = vect_gen_perm_mask_checked (vectype, indices);
+}
+
+/* Encoding the PERM_MASK.  */
+
+static void
+vect_indices_encoding (tree vectype, unsigned int array_num,
+		       tree &perm_mask_high, tree &perm_mask_low)
+{
+  unsigned int nelt = TYPE_VECTOR_SUBPARTS (vectype).to_constant ();
+  vec_perm_builder sel (nelt, nelt, 1);
+  sel.quick_grow (nelt);
+  unsigned int group_num = nelt / array_num;
+  unsigned int index = 0;
+  unsigned int array = 0;
+  unsigned int group = 0;
+
+  /* The encoding has 2 patterns in the folllowing stages.  */
+  for (array = 0; array < array_num / 2; array++)
+    {
+      for (group = 0; group < group_num; group++)
+	{
+	  sel[index++] = group + group_num * array;
+	}
+      for (group = 0; group < group_num; group++)
+	{
+	  sel[index++] = nelt + group + group_num * array;
+	}
+    }
+  vec_perm_indices indices (sel, 2, nelt);
+  perm_mask_high = vect_gen_perm_mask_checked (vectype, indices);
+
+  index = 0;
+  for (array = array_num / 2; array < array_num; array++)
+    {
+      for (group = 0; group < group_num; group++)
+	{
+	  sel[index++] = group + group_num * array;
+	}
+      for (group = 0; group < group_num; group++)
+	{
+	  sel[index++] = nelt + group + group_num * array;
+	}
+    }
+  indices.new_vector (sel, 2, nelt);
+  perm_mask_low = vect_gen_perm_mask_checked (vectype, indices);
+}
+
+/* Function vect_transpose_store_chain.
+
+   Given a chain of interleaved stores in DR_CHAIN of LENGTH and ARRAY_NUM that
+   must be a power of 2.  Generate interleave_high/low stmts to reorder
+   the data correctly for the stores.  Return the final references for stores
+   in RESULT_CHAIN.  This function is similar to vect_permute_store_chain (),
+   we interleave the contents of the vectors in their order.
+
+   E.g., LENGTH is 4, the scalar type is short (i.e., VF is 8) and ARRAY_NUM
+   is 4.  That is, the input is 4 vectors each containing 8 elements.
+   And 2 (VF / ARRAY_NUM) of 8 elements come from the same array.  we interleave
+   the contents of the four vectors in their order.  We assign a number to each
+   element, the input sequence is:
+
+   1st vec:   0  1  2  3  4  5  6  7
+   2nd vec:   8  9 10 11 12 13 14 15
+   3rd vec:  16 17 18 19 20 21 22 23
+   4th vec:  24 25 26 27 28 29 30 31
+
+   The output sequence should be:
+
+   1st vec:   0  4  8 12 16 20 24 28
+   2nd vec:   1  5  9 13 17 21 25 29
+   3rd vec:   2  6 10 14 18 22 26 30
+   4th vec:   3  7 11 15 19 23 27 31
+
+   In our example,
+   We get 2 (VF / ARRAY_NUM) elements together in every vector.
+
+   I1:   0  4  1  5  2  6  3  7
+   I2:   8 12  9 13 10 14 11 15
+   I3:  16 20 17 21 18 22 19 23
+   I4:  24 28 25 29 26 30 27 31
+
+   Then, we use interleave_high/low instructions to create such output.
+   Every 2 (VF / ARRAY_NUM) elements are regarded as a whole.  The permutation
+   is done in log LENGTH stages.
+
+   I1: interleave_high (1st vec, 3rd vec)
+   I2: interleave_low (1st vec, 3rd vec)
+   I3: interleave_high (2nd vec, 4th vec)
+   I4: interleave_low (2nd vec, 4th vec)
+
+   The first stage of the sequence should be:
+
+   I1:   0  4 16 20  1  5 17 21
+   I2:   2  6 18 22  3  7 19 23
+   I3:   8 12 24 28  9 13 25 29
+   I4:  10 14 26 30 11 15 27 31
+
+   The following stage sequence should be, i.e. the final result is:
+
+   I1:   0  4  8 12 16 20 24 28
+   I2:   1  5  9 13 17 21 25 29
+   I3:   2  6 10 14 18 22 26 30
+   I4:   3  7 11 15 19 23 27 31.  */
+
+void
+vect_transpose_store_chain (vec<tree> dr_chain, unsigned int length,
+			    unsigned int array_num, stmt_vec_info stmt_info,
+			    gimple_stmt_iterator *gsi, vec<tree> *result_chain)
+{
+  gimple *perm_stmt = NULL;
+  tree vectype = STMT_VINFO_VECTYPE (stmt_info);
+  tree perm_mask_low_first = NULL;
+  tree perm_mask_high_first = NULL;
+  tree perm_mask_low = NULL;
+  tree perm_mask_high = NULL;
+  unsigned int log_length = exact_log2 (length);
+
+  /* Only power of 2 is supported.  */
+  gcc_assert (pow2p_hwi (length));
+
+  /* The encoding has 2 types, one for the grouped pattern in the fisrt stage,
+     another for the interleaved patterns in the following stages.  */
+  gcc_assert (array_num != 0);
+
+  /* Create grouped stmt (in the first stage):
+	group = nelt / array_num;
+	high_first = VEC_PERM_EXPR <vect1, vect2,
+		{0, array_num, 2*array_num, ..., (2*group-1)*array_num,
+		1, 1+array_num, 1+2*array_num, ..., 1+(2*group-1)*array_num,
+		...,
+		array_num/2-1, (array_num/2-1)+array_num, ...,
+		(array_num/2-1)+(2*group-1)*array_num}>
+	low_first = VEC_PERM_EXPR <vect1, vect2,
+		{array_num/2, array_num/2+array_num, array_num/2+2*array_num,
+		..., array_num/2+(2*group-1)*array_num,
+		array_num/2+1, array_num/2+1+array_num,
+		..., array_num/2+1+(2*group-1)*array_num,
+		...,
+		array_num-1, array_num-1+array_num,
+		..., array_num-1+(2*group-1)*array_num}>  */
+  vect_indices_encoding_first (vectype, array_num, perm_mask_high_first,
+			       perm_mask_low_first);
+
+  /* Create interleaving stmt (in the following stages):
+	high = VEC_PERM_EXPR <vect1, vect2, {0, 1, ..., group-1,
+		nelt, nelt+1, ..., nelt+group-1,
+		group, group+1, ..., 2*group-1,
+		nelt+group, nelt+group+1, ..., nelt+2*group-1,
+		...}>
+	low = VEC_PERM_EXPR <vect1, vect2,
+		{nelt/2, nelt/2+1, ..., nelt/2+group-1,
+		nelt*3/2, nelt*3/2+1, ..., nelt*3/2+group-1,
+		nelt/2+group, nelt/2+group+1, ..., nelt/2+2*group-1,
+		nelt*3/2+group, nelt*3/2+group+1, ..., nelt*3/2+2*group-1,
+		...}>  */
+  vect_indices_encoding (vectype, array_num, perm_mask_high, perm_mask_low);
+
+  for (unsigned int perm_time = 0; perm_time < log_length; perm_time++)
+    {
+      for (unsigned int index = 0; index < length / 2; index++)
+	{
+	  tree vect1 = dr_chain[index];
+	  tree vect2 = dr_chain[index + length / 2];
+
+	  tree high = make_temp_ssa_name (vectype, NULL, "vect_inter_high");
+	  perm_stmt = gimple_build_assign (high, VEC_PERM_EXPR, vect1, vect2,
+					   perm_time == 0 ? perm_mask_high_first
+							  : perm_mask_high);
+	  vect_finish_stmt_generation (stmt_info, perm_stmt, gsi);
+	  (*result_chain)[2 * index] = high;
+
+	  tree low = make_temp_ssa_name (vectype, NULL, "vect_inter_low");
+	  perm_stmt = gimple_build_assign (low, VEC_PERM_EXPR, vect1, vect2,
+					   perm_time == 0 ? perm_mask_low_first
+							  : perm_mask_low);
+	  vect_finish_stmt_generation (stmt_info, perm_stmt, gsi);
+	  (*result_chain)[2 * index+1] = low;
+	}
+      memcpy (dr_chain.address (), result_chain->address (),
+	      length * sizeof (tree));
+    }
+}
+
 /* Function vect_setup_realignment
 
    This function is called when vectorizing an unaligned load using
diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index 476b32370..d30463b96 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -2414,11 +2414,13 @@ vect_analyze_slp_instance (vec_info *vinfo,
 
   /* For basic block SLP, try to break the group up into multiples of the
      vector size.  */
+  bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
   unsigned HOST_WIDE_INT const_nunits;
   if (is_a <bb_vec_info> (vinfo)
       && STMT_VINFO_GROUPED_ACCESS (stmt_info)
       && DR_GROUP_FIRST_ELEMENT (stmt_info)
-      && nunits.is_constant (&const_nunits))
+      && nunits.is_constant (&const_nunits)
+      && !bb_vinfo->transposed)
     {
       /* We consider breaking the group only on VF boundaries from the existing
 	 start.  */
@@ -2455,6 +2457,898 @@ vect_analyze_slp_instance (vec_info *vinfo,
   return false;
 }
 
+static inline bool
+is_const_assign (stmt_vec_info store_elem)
+{
+  if (store_elem == NULL)
+    {
+      gcc_unreachable ();
+    }
+  gimple *stmt = store_elem->stmt;
+  gimple_rhs_class rhs_class = gimple_assign_rhs_class (stmt);
+  return rhs_class == GIMPLE_SINGLE_RHS
+	 && TREE_CONSTANT (gimple_assign_rhs1 (store_elem->stmt));
+}
+
+/* Push inits to INNERMOST_INITS and check const assign.  */
+
+static bool
+record_innermost (vec<tree> &innermost_inits,
+		  vec<tree> &innermost_offsets,
+		  stmt_vec_info stmt_vinfo)
+{
+  if (!stmt_vinfo)
+    {
+      return false;
+    }
+  stmt_vec_info next_info = stmt_vinfo;
+  while (next_info)
+    {
+      /* No need to vectorize constant assign in a transposed version.  */
+      if (is_const_assign (next_info))
+	{
+	  if (dump_enabled_p ())
+	    {
+	      dump_printf_loc (MSG_NOTE, vect_location,
+			      "no need to vectorize, store is const assign: %G",
+			      next_info->stmt);
+	    }
+	  return false;
+	}
+      innermost_inits.safe_push (STMT_VINFO_DR_INIT (next_info));
+      innermost_offsets.safe_push (STMT_VINFO_DR_OFFSET (next_info));
+      next_info = DR_GROUP_NEXT_ELEMENT (next_info);
+    }
+  return true;
+}
+
+/* Compare inits to INNERMOST_INITS, return FALSE if inits do not match
+   the first grouped_store.  And check const assign meanwhile.  */
+
+static bool
+compare_innermost (const vec<tree> &innermost_inits,
+		   const vec<tree> &innermost_offsets,
+		   stmt_vec_info stmt_vinfo)
+{
+  if (!stmt_vinfo || innermost_inits.length () != stmt_vinfo->size)
+    {
+      return false;
+    }
+  stmt_vec_info next_info = stmt_vinfo;
+  unsigned int i = 0;
+  while (next_info)
+    {
+      if (is_const_assign (next_info))
+	{
+	  if (dump_enabled_p ())
+	    {
+	      dump_printf_loc (MSG_NOTE, vect_location,
+			       "no need to vectorize, store is const "
+			       "assign: %G", next_info->stmt);
+	    }
+	  return false;
+	}
+      if (innermost_inits[i] != STMT_VINFO_DR_INIT (next_info)
+	  || innermost_offsets[i] != STMT_VINFO_DR_OFFSET (next_info))
+	{
+	  return false;
+	}
+      next_info = DR_GROUP_NEXT_ELEMENT (next_info);
+      i++;
+    }
+  return true;
+}
+
+/* Check if grouped stores are of same type.
+   input: t1/t2 = TREE_TYPE (gimple_assign_lhs (first_element->stmt))
+   output: 0 if same, 1 or -1 else.  */
+
+static int
+tree_type_cmp (const tree t1, const tree t2)
+{
+  gcc_checking_assert (t1 != NULL && t2 != NULL);
+  if (t1 != t2)
+    {
+      if (TREE_CODE (t1) != TREE_CODE (t2))
+	{
+	  return TREE_CODE (t1) > TREE_CODE (t2) ? 1 : -1;
+	}
+      if (TYPE_UNSIGNED (t1) != TYPE_UNSIGNED (t2))
+	{
+	  return TYPE_UNSIGNED (t1) > TYPE_UNSIGNED (t2) ? 1 : -1;
+	}
+      if (TYPE_PRECISION (t1) != TYPE_PRECISION (t2))
+	{
+	  return TYPE_PRECISION (t1) > TYPE_PRECISION (t2) ? 1 : -1;
+	}
+    }
+  return 0;
+}
+
+/* Check it if 2 grouped stores are of same type that
+   we can analyze them in a transpose group.  */
+static int
+check_same_store_type (stmt_vec_info grp1, stmt_vec_info grp2)
+{
+  if (grp1 == grp2)
+    {
+      return 0;
+    }
+  if (grp1->size != grp2->size)
+    {
+      return grp1->size > grp2->size ? 1 : -1;
+    }
+  tree lhs1 = gimple_assign_lhs (grp1->stmt);
+  tree lhs2 = gimple_assign_lhs (grp2->stmt);
+  if (TREE_CODE (lhs1) != TREE_CODE (lhs2))
+    {
+      return TREE_CODE (lhs1) > TREE_CODE (lhs2) ? 1 : -1;
+    }
+  tree grp_type1 = TREE_TYPE (gimple_assign_lhs (grp1->stmt));
+  tree grp_type2 = TREE_TYPE (gimple_assign_lhs (grp2->stmt));
+  int cmp = tree_type_cmp (grp_type1, grp_type2);
+  return cmp;
+}
+
+/* Sort grouped stores according to group_size and store_type.
+   output: 0 if same, 1 if grp1 > grp2, -1 otherwise.  */
+
+static int
+grouped_store_cmp (const void *grp1_, const void *grp2_)
+{
+  stmt_vec_info grp1 = *(stmt_vec_info *)const_cast<void *>(grp1_);
+  stmt_vec_info grp2 = *(stmt_vec_info *)const_cast<void *>(grp2_);
+  return check_same_store_type (grp1, grp2);
+}
+
+/* Transposing is based on permutation in registers.  Permutation requires
+   vector length being power of 2 and satisfying the vector mode.  */
+
+static inline bool
+check_filling_reg (stmt_vec_info current_element)
+{
+  if (current_element->size == 0)
+    {
+      return false;
+    }
+  /* If the gimple STMT was already vectorized in vect pass, it's unable to
+     conduct transpose analysis, skip it.  */
+  bool lhs_vectorized
+	= TREE_CODE (TREE_TYPE (gimple_get_lhs (current_element->stmt)))
+	  == VECTOR_TYPE;
+  bool rhs_vectorized
+	= TREE_CODE (TREE_TYPE (gimple_assign_rhs1 (current_element->stmt)))
+	  == VECTOR_TYPE;
+  if (lhs_vectorized || rhs_vectorized)
+    {
+      return false;
+    }
+  unsigned int store_precision
+    = TYPE_PRECISION (TREE_TYPE (gimple_get_lhs (current_element->stmt)));
+  auto_vector_modes vector_modes;
+  targetm.vectorize.autovectorize_vector_modes (&vector_modes, false);
+  unsigned min_mode_size = -1u;
+  for (unsigned i = 0; i < vector_modes.length (); i++)
+    {
+      unsigned mode_bit_size = (GET_MODE_BITSIZE (vector_modes[i])).coeffs[0];
+      min_mode_size = mode_bit_size < min_mode_size
+			? mode_bit_size : min_mode_size;
+    }
+  return store_precision != 0
+	 && pow2p_hwi (current_element->size)
+	 && (current_element->size * store_precision % min_mode_size == 0);
+}
+
+/* Check if previous groups are suitable to transpose, if not, set their
+   group number to -1, reduce grp_num and clear current_groups.
+   Otherwise, just clear current_groups.  */
+
+static void
+check_and_clear_groups (vec<stmt_vec_info> current_groups,
+			unsigned int &grp_num)
+{
+  stmt_vec_info first_element;
+  if (current_groups.length () == 1
+      || (current_groups.length () != 0
+	  && !pow2p_hwi (current_groups.length ())))
+    {
+      while (current_groups.length () != 0)
+	{
+	  first_element = current_groups.pop ();
+	  first_element->group_number = -1;
+	}
+      grp_num--;
+    }
+  else
+    {
+      while (current_groups.length ())
+	{
+	  current_groups.pop ();
+	}
+    }
+}
+
+
+/* Make sure that transpose slp vectorization is conducted only if grouped
+   stores are one dimension array ref.  */
+
+static bool
+is_store_one_dim_array (gimple *stmt)
+{
+  tree op = gimple_get_lhs (stmt);
+  if (TREE_CODE (op) != ARRAY_REF)
+    return false;
+  return TREE_OPERAND_LENGTH (op) > 0
+	 && TREE_OPERAND_LENGTH (TREE_OPERAND (op, 0)) == 0;
+}
+
+/* Set grouped_stores with similar MEM_REF to the same group and mark their
+   grp_num.  Groups with same grp_num consist the minimum unit to analyze
+   transpose.  Return num of such units.  */
+
+static unsigned
+vect_prepare_transpose (bb_vec_info bb_vinfo)
+{
+  stmt_vec_info current_element = NULL;
+  stmt_vec_info first_element = NULL;
+  unsigned int i = 0;
+  unsigned int grp_num = 0;
+  /* Use arrays to record MEM_REF data in different GROUPED_STORES.  */
+  auto_vec<tree> innermost_inits;
+  auto_vec<tree> innermost_offsets;
+
+  /* A set of stmt_vec_info with same store type.  Analyze them if their size
+     is suitable to transpose.  */
+  auto_vec<stmt_vec_info> current_groups;
+
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_stores, i, current_element)
+    {
+      /* Compare current grouped_store to the first one if first_element exists,
+	 push current_element to current_groups if they are similar on innermost
+	 behavior of MEM_REF.  */
+      if (first_element != NULL
+	  && !check_same_store_type (first_element, current_element)
+	  && compare_innermost (innermost_inits, innermost_offsets,
+				current_element))
+	{
+	  current_groups.safe_push (current_element);
+	  current_element->group_number = grp_num;
+	  /* If current_element is the last element in grouped_stores, continue
+	     will exit the loop and leave the last group unanalyzed.  */
+	  if (i == bb_vinfo->grouped_stores.length () - 1)
+	    {
+	      check_and_clear_groups (current_groups, grp_num);
+	    }
+	  continue;
+	}
+      check_and_clear_groups (current_groups, grp_num);
+      innermost_inits.release ();
+      innermost_offsets.release ();
+      /* Beginning of a new group to analyze whether they are able to consist
+	 a unit to conduct transpose analysis.  */
+      first_element = NULL;
+      if (is_store_one_dim_array (current_element->stmt)
+	  && check_filling_reg (current_element)
+	  && record_innermost (innermost_inits, innermost_offsets,
+			       current_element))
+	{
+	  first_element = current_element;
+	  current_groups.safe_push (current_element);
+	  current_element->group_number = ++grp_num;
+	  if (i == bb_vinfo->grouped_stores.length () - 1)
+	    {
+	      check_and_clear_groups (current_groups, grp_num);
+	    }
+	  continue;
+	}
+      current_element->group_number = -1;
+    }
+  return grp_num;
+}
+
+/* Return a flag to transpose grouped stores before building slp tree.
+   Add bool may_transpose in class vec_info.  */
+
+static bool
+vect_may_transpose (bb_vec_info bb_vinfo)
+{
+  if (targetm.vectorize.vec_perm_const == NULL)
+    {
+      return false;
+    }
+  if (bb_vinfo->grouped_stores.length () < 2)
+    {
+      return false;
+    }
+  DUMP_VECT_SCOPE ("analyze if grouped stores may transpose to slp");
+  /* Sort grouped_stores according to size and type for function
+     vect_prepare_transpose ().  */
+  bb_vinfo->grouped_stores.qsort (grouped_store_cmp);
+
+  int groups = vect_prepare_transpose (bb_vinfo);
+  BB_VINFO_TRANS_GROUPS (bb_vinfo) = groups;
+  if (dump_enabled_p ())
+      dump_printf_loc (MSG_NOTE, vect_location,
+		       "%d groups to analyze transposed slp.\n", groups);
+  return groups != 0;
+}
+
+/* Get the base address of STMT_INFO.  */
+
+static tree
+get_op_base_address (stmt_vec_info stmt_info)
+{
+  struct data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
+  tree op = DR_BASE_ADDRESS (dr);
+  while (TREE_OPERAND_LENGTH (op) > 0)
+    {
+      op = TREE_OPERAND (op, 0);
+    }
+  return op;
+}
+
+/* Compare the UID of the two stmt_info STMTINFO_A and STMTINFO_B.
+   Sorting them in ascending order.  */
+
+static int
+dr_group_cmp (const void *stmtinfo_a_, const void *stmtinfo_b_)
+{
+  stmt_vec_info stmtinfo_a
+	= *(stmt_vec_info *) const_cast<void *> (stmtinfo_a_);
+  stmt_vec_info stmtinfo_b
+	= *(stmt_vec_info *) const_cast<void *> (stmtinfo_b_);
+
+  /* Stabilize sort.  */
+  if (stmtinfo_a == stmtinfo_b)
+    {
+      return 0;
+    }
+  return gimple_uid (stmtinfo_a->stmt) < gimple_uid (stmtinfo_b->stmt) ? -1 : 1;
+}
+
+/* Find the first elements of the grouped loads which are required to merge.  */
+
+static void
+vect_slp_grouped_load_find (bb_vec_info bb_vinfo, vec<bool> &visited,
+			    vec<stmt_vec_info> &res)
+{
+  unsigned int i = 0;
+  stmt_vec_info merge_first_element = NULL;
+  stmt_vec_info first_element = NULL;
+  tree opa = NULL;
+  unsigned int grp_size_a = 0;
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_loads, i, first_element)
+    {
+      if (visited[i])
+	{
+	  continue;
+	}
+      if (!STMT_VINFO_GROUPED_ACCESS (first_element)
+	  || !pow2p_hwi (DR_GROUP_SIZE (first_element)))
+	{
+	  /* Non-conforming grouped load should be grouped separately.  */
+	  if (merge_first_element == NULL)
+	    {
+	      visited[i] = true;
+	      res.safe_push (first_element);
+	      return;
+	    }
+	}
+      if (merge_first_element == NULL)
+	{
+	  merge_first_element = first_element;
+	  opa = get_op_base_address (first_element);
+	  grp_size_a = DR_GROUP_SIZE (first_element);
+	  res.safe_push (first_element);
+	  visited[i] = true;
+	  continue;
+	}
+
+      /* If the two first elements are of the same base address and group size,
+	 these two grouped loads need to be merged.  */
+      tree opb = get_op_base_address (first_element);
+      unsigned int grp_size_b = DR_GROUP_SIZE (first_element);
+      if (opa == opb && grp_size_a == grp_size_b)
+	{
+	  res.safe_push (first_element);
+	  visited[i] = true;
+	}
+    }
+}
+
+/* Merge the grouped loads that are found from
+   vect_slp_grouped_load_find ().  */
+
+static stmt_vec_info
+vect_slp_grouped_load_merge (vec<stmt_vec_info> res)
+{
+  stmt_vec_info stmt_info = res[0];
+  if (res.length () == 1)
+    {
+      return stmt_info;
+    }
+  unsigned int i = 0;
+  unsigned int size = DR_GROUP_SIZE (res[0]);
+  unsigned int new_group_size = size * res.length ();
+  stmt_vec_info first_element = NULL;
+  stmt_vec_info merge_first_element = NULL;
+  stmt_vec_info last_element = NULL;
+  FOR_EACH_VEC_ELT (res, i, first_element)
+    {
+      if (merge_first_element == NULL)
+	{
+	  merge_first_element = first_element;
+	  last_element = merge_first_element;
+	  size = DR_GROUP_SIZE (merge_first_element);
+	}
+
+      if (last_element != first_element
+	  && !DR_GROUP_NEXT_ELEMENT (last_element))
+	{
+	  DR_GROUP_NEXT_ELEMENT (last_element) = first_element;
+	  /* Store the gap from the previous member of the group.  If there is
+	     no gap in the access, DR_GROUP_GAP is always 1.  */
+	  DR_GROUP_GAP_TRANS (first_element) = DR_GROUP_GAP (first_element);
+	  DR_GROUP_GAP (first_element) = 1;
+	}
+      for (stmt_info = first_element; stmt_info;
+	   stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
+	{
+	  DR_GROUP_FIRST_ELEMENT (stmt_info) = merge_first_element;
+	  DR_GROUP_SIZE_TRANS (stmt_info) = DR_GROUP_SIZE (stmt_info);
+	  DR_GROUP_SIZE (stmt_info) = new_group_size;
+	  last_element = stmt_info;
+	}
+    }
+  DR_GROUP_SIZE (merge_first_element) = new_group_size;
+  DR_GROUP_SLP_TRANSPOSE (merge_first_element) = true;
+  DR_GROUP_NEXT_ELEMENT (last_element) = NULL;
+  return merge_first_element;
+}
+
+/* Merge the grouped loads which have the same base address and group size.
+   For example, for grouped loads (opa_1, opa_2, opb_1, opb_2):
+     opa_1: a0->a1->a2->a3
+     opa_2: a8->a9->a10->a11
+     opb_1: b0->b1
+     opb_2: b16->b17
+   we can probably get two merged grouped loads:
+     opa: a0->a1->a2->a3->a8->a9->a10->a11
+     opb: b0->b1->b16->b17.  */
+
+static bool
+vect_merge_slp_grouped_loads (bb_vec_info bb_vinfo)
+{
+  if (bb_vinfo->grouped_loads.length () <= 0)
+    {
+      if (dump_enabled_p ())
+	{
+	  dump_printf_loc (MSG_NOTE, vect_location,
+			   "The number of grouped loads is 0.\n");
+	}
+      return false;
+    }
+  bb_vinfo->grouped_loads.qsort (dr_group_cmp);
+  auto_vec<bool> visited (bb_vinfo->grouped_loads.length ());
+  auto_vec<stmt_vec_info> grouped_loads_merge;
+  for (unsigned int i = 0; i < bb_vinfo->grouped_loads.length (); i++)
+    {
+      visited.safe_push (false);
+    }
+  while (1)
+    {
+      /* Find grouped loads which are required to merge.  */
+      auto_vec<stmt_vec_info> res;
+      vect_slp_grouped_load_find (bb_vinfo, visited, res);
+      if (res.is_empty ())
+	{
+	  break;
+	}
+      /* Merge the required grouped loads into one group.  */
+      grouped_loads_merge.safe_push (vect_slp_grouped_load_merge (res));
+    }
+  if (grouped_loads_merge.length () == bb_vinfo->grouped_loads.length ())
+    {
+      if (dump_enabled_p ())
+	{
+	  dump_printf_loc (MSG_NOTE, vect_location,
+			   "No grouped loads need to be merged.\n");
+	}
+      return false;
+    }
+  if (dump_enabled_p ())
+    {
+      dump_printf_loc (MSG_NOTE, vect_location,
+		       "Merging grouped loads successfully.\n");
+    }
+  BB_VINFO_GROUPED_LOADS (bb_vinfo).release ();
+  for (unsigned int i = 0; i < grouped_loads_merge.length (); i++)
+    {
+      BB_VINFO_GROUPED_LOADS (bb_vinfo).safe_push (grouped_loads_merge[i]);
+    }
+  return true;
+}
+
+/* Find the first elements of the grouped stores
+   which are required to transpose and merge.  */
+
+static void
+vect_slp_grouped_store_find (bb_vec_info bb_vinfo, vec<bool> &visited,
+			     vec<stmt_vec_info> &res)
+{
+  stmt_vec_info first_element = NULL;
+  stmt_vec_info merge_first_element = NULL;
+  unsigned int k = 0;
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_stores, k, first_element)
+    {
+      if (visited[k])
+	{
+	  continue;
+	}
+      /* Non-conforming grouped store should be grouped separately.  */
+      if (!STMT_VINFO_GROUPED_ACCESS (first_element)
+	  || first_element->group_number == -1)
+	{
+	  if (merge_first_element == NULL)
+	    {
+	      visited[k] = true;
+	      res.safe_push (first_element);
+	      return;
+	    }
+	}
+      if (first_element->group_number != -1
+	  && merge_first_element == NULL)
+	{
+	  merge_first_element = first_element;
+	}
+      if (merge_first_element->group_number == first_element->group_number)
+	{
+	  visited[k] = true;
+	  res.safe_push (first_element);
+	}
+    }
+}
+
+/* Transpose and merge the grouped stores that are found from
+   vect_slp_grouped_store_find ().  */
+
+static stmt_vec_info
+vect_slp_grouped_store_transform (vec<stmt_vec_info> res)
+{
+  stmt_vec_info stmt_info = res[0];
+  if (res.length () == 1)
+    {
+      return stmt_info;
+    }
+  stmt_vec_info rearrange_first_element = stmt_info;
+  stmt_vec_info last_element = rearrange_first_element;
+
+  unsigned int size = DR_GROUP_SIZE (rearrange_first_element);
+  unsigned int new_group_size = size * res.length ();
+  for (unsigned int i = 1; i < res.length (); i++)
+    {
+      /* Store the gap from the previous member of the group.  If there is no
+	 gap in the access, DR_GROUP_GAP is always 1.  */
+      DR_GROUP_GAP_TRANS (res[i]) = DR_GROUP_GAP (res[i]);
+      DR_GROUP_GAP (res[i]) = 1;
+    }
+  while (!res.is_empty ())
+    {
+      stmt_info = res[0];
+      res.ordered_remove (0);
+      if (DR_GROUP_NEXT_ELEMENT (stmt_info))
+	{
+	  res.safe_push (DR_GROUP_NEXT_ELEMENT (stmt_info));
+	}
+      DR_GROUP_FIRST_ELEMENT (stmt_info) = rearrange_first_element;
+      DR_GROUP_NEXT_ELEMENT (last_element) = stmt_info;
+      DR_GROUP_SIZE_TRANS (stmt_info) = DR_GROUP_SIZE (stmt_info);
+      DR_GROUP_SIZE (stmt_info) = new_group_size;
+      last_element = stmt_info;
+    }
+
+  DR_GROUP_SIZE (rearrange_first_element) = new_group_size;
+  DR_GROUP_SLP_TRANSPOSE (rearrange_first_element) = true;
+  DR_GROUP_NEXT_ELEMENT (last_element) = NULL;
+  return rearrange_first_element;
+}
+
+/* Save the STMT_INFO in the grouped stores to BB_VINFO_SCALAR_STORES for
+   transposing back grouped stores.  */
+
+static void
+get_scalar_stores (bb_vec_info bb_vinfo)
+{
+  unsigned int k = 0;
+  stmt_vec_info first_element = NULL;
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_stores, k, first_element)
+    {
+      /* Filter the grouped store which is unnecessary for transposing.  */
+      if (!STMT_VINFO_GROUPED_ACCESS (first_element)
+	  || first_element->group_number == -1)
+	{
+	  continue;
+	}
+      vec<stmt_vec_info> tmp_scalar_store;
+      tmp_scalar_store.create (DR_GROUP_SIZE (first_element));
+      for (stmt_vec_info stmt_info = first_element; stmt_info;
+	   stmt_info = DR_GROUP_NEXT_ELEMENT (stmt_info))
+	{
+	  tmp_scalar_store.safe_push (stmt_info);
+	}
+      BB_VINFO_SCALAR_STORES (bb_vinfo).safe_push (tmp_scalar_store);
+    }
+}
+
+/* Transpose and merge the grouped stores which have the same group number.
+   For example, for grouped stores (opa_0, opa_1, opa_2, opa_3):
+     opa_0: a00->a01->a02->a03
+     opa_1: a10->a11->a12->a13
+     opa_2: a20->a21->a22->a23
+     opa_2: a30->a31->a32->a33
+   we can probably get the merged grouped store:
+     opa: a00->a10->a20->a30
+	->a01->a11->a21->a31
+	->a02->a12->a22->a32
+	->a03->a13->a23->a33.  */
+
+static bool
+vect_transform_slp_grouped_stores (bb_vec_info bb_vinfo)
+{
+  if (bb_vinfo->grouped_stores.length () <= 0)
+    {
+      if (dump_enabled_p ())
+	{
+	  dump_printf_loc (MSG_NOTE, vect_location,
+			   "The number of grouped stores is 0.\n");
+	}
+      return false;
+    }
+
+  bb_vinfo->grouped_stores.qsort (dr_group_cmp);
+  auto_vec<stmt_vec_info> grouped_stores_merge;
+  auto_vec<bool> visited (bb_vinfo->grouped_stores.length ());
+  unsigned int i = 0;
+  for (i = 0; i < bb_vinfo->grouped_stores.length (); i++)
+    {
+      visited.safe_push (false);
+    }
+
+  /* Get scalar stores for the following transposition recovery.  */
+  get_scalar_stores (bb_vinfo);
+
+  while (1)
+    {
+      /* Find grouped stores which are required to transpose and merge.  */
+      auto_vec<stmt_vec_info> res;
+      vect_slp_grouped_store_find (bb_vinfo, visited, res);
+      if (res.is_empty ())
+	{
+	  break;
+	}
+      /* Transpose and merge the required grouped stores into one group.  */
+      grouped_stores_merge.safe_push (vect_slp_grouped_store_transform (res));
+    }
+
+  BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
+  for (i = 0; i < grouped_stores_merge.length (); i++)
+    {
+      BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (grouped_stores_merge[i]);
+    }
+
+  if (dump_enabled_p ())
+    {
+      dump_printf_loc (MSG_NOTE, vect_location,
+		       "Transposing grouped stores successfully.\n");
+    }
+  return true;
+}
+
+/* A helpful function of vect_transform_back_slp_grouped_stores ().  */
+
+static auto_vec<stmt_vec_info>
+vect_transform_back_slp_grouped_store (bb_vec_info bb_vinfo,
+				       stmt_vec_info first_stmt_info)
+{
+  auto_vec<stmt_vec_info> grouped_stores_split;
+  for (unsigned int i = 0; i < bb_vinfo->scalar_stores.length (); i++)
+    {
+      vec<stmt_vec_info> scalar_tmp = bb_vinfo->scalar_stores[i];
+      if (scalar_tmp.length () > 1
+	  && scalar_tmp[0]->group_number != first_stmt_info->group_number)
+	{
+	  continue;
+	}
+      stmt_vec_info cur_stmt_info = NULL;
+      stmt_vec_info cur_first_stmt_info = NULL;
+      stmt_vec_info last_stmt_info = NULL;
+      unsigned int k = 0;
+      FOR_EACH_VEC_ELT (scalar_tmp, k, cur_stmt_info)
+	{
+	  if (k == 0)
+	    {
+	      cur_first_stmt_info = cur_stmt_info;
+	      last_stmt_info = cur_stmt_info;
+	    }
+	  DR_GROUP_FIRST_ELEMENT (cur_stmt_info) = cur_first_stmt_info;
+	  DR_GROUP_NEXT_ELEMENT (last_stmt_info) = cur_stmt_info;
+	  last_stmt_info = cur_stmt_info;
+	}
+      DR_GROUP_SIZE (cur_first_stmt_info) = k;
+      DR_GROUP_NEXT_ELEMENT (last_stmt_info) = NULL;
+      if (first_stmt_info != cur_first_stmt_info)
+	{
+	  DR_GROUP_GAP (cur_first_stmt_info)
+		= DR_GROUP_GAP_TRANS (cur_first_stmt_info);
+	  DR_GROUP_SLP_TRANSPOSE (cur_first_stmt_info) = false;
+	  DR_GROUP_NUMBER (cur_first_stmt_info) = -1;
+	}
+      grouped_stores_split.safe_push (cur_first_stmt_info);
+    }
+  return grouped_stores_split;
+}
+
+/* Transform the grouped store back.  */
+
+void
+vect_transform_back_slp_grouped_stores (bb_vec_info bb_vinfo,
+					stmt_vec_info first_stmt_info)
+{
+  if (first_stmt_info->group_number == -1)
+    {
+      return;
+    }
+  /* Transform back.  */
+  auto_vec<stmt_vec_info> grouped_stores_split
+	= vect_transform_back_slp_grouped_store (bb_vinfo, first_stmt_info);
+
+  /* Add the remaining grouped stores to grouped_stores_split.  */
+  stmt_vec_info first_element = NULL;
+  unsigned int i = 0;
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_stores, i, first_element)
+    {
+      if (first_element->group_number != first_stmt_info->group_number)
+	{
+	  grouped_stores_split.safe_push (first_element);
+	}
+    }
+  DR_GROUP_SLP_TRANSPOSE (first_stmt_info) = false;
+  DR_GROUP_NUMBER (first_stmt_info) = -1;
+  BB_VINFO_GROUPED_STORES (bb_vinfo).release ();
+  for (i = 0; i < grouped_stores_split.length (); i++)
+    {
+      BB_VINFO_GROUPED_STORES (bb_vinfo).safe_push (grouped_stores_split[i]);
+    }
+}
+
+/* Function check_for_slp_vectype
+
+   Restriction for grouped stores by checking their vectype.
+   If the vectype of the grouped store is changed, it need transform back.
+   If all grouped stores need to be transformed back, return FALSE.  */
+
+static bool
+check_for_slp_vectype (bb_vec_info bb_vinfo)
+{
+  stmt_vec_info first_element = NULL;
+  unsigned int i = 0;
+  int count = 0;
+  auto_vec<stmt_vec_info> grouped_stores_check;
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_stores, i, first_element)
+    {
+      grouped_stores_check.safe_push (first_element);
+    }
+  FOR_EACH_VEC_ELT (grouped_stores_check, i, first_element)
+    {
+      if (STMT_VINFO_GROUPED_ACCESS (first_element)
+	  && first_element->group_number != -1)
+	{
+	  unsigned int group_size_b
+			= DR_GROUP_SIZE_TRANS (first_element);
+	  tree vectype = STMT_VINFO_VECTYPE (first_element);
+	  poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (vectype);
+	  if (nunits.to_constant () > group_size_b)
+	    {
+	      count++;
+	      /* If the vectype is changed, this grouped store need
+		 to be transformed back.  */
+	      vect_transform_back_slp_grouped_stores (bb_vinfo, first_element);
+	      if (dump_enabled_p ())
+		{
+		  dump_printf_loc (MSG_NOTE, vect_location,
+				   "No supported: only supported for"
+				   " group_size geq than nunits.\n");
+		}
+	    }
+	}
+    }
+  if (count == BB_VINFO_TRANS_GROUPS (bb_vinfo))
+    {
+      return false;
+    }
+  return true;
+}
+
+/* Function check_for_dr_alignment
+
+   Check the alignment of the slp instance loads.
+   Return FALSE if a load cannot be vectorized.  */
+
+static bool
+check_for_dr_alignment (slp_instance instance)
+{
+  slp_tree node = NULL;
+  unsigned int i = 0;
+  FOR_EACH_VEC_ELT (SLP_INSTANCE_LOADS (instance), i, node)
+    {
+      stmt_vec_info first_stmt_info = SLP_TREE_SCALAR_STMTS (node)[0];
+      dr_vec_info *first_dr_info = STMT_VINFO_DR_INFO (first_stmt_info);
+      enum dr_alignment_support supportable_dr_alignment
+	= vect_supportable_dr_alignment (first_dr_info, false);
+      if (supportable_dr_alignment == dr_explicit_realign_optimized
+	  || supportable_dr_alignment == dr_explicit_realign)
+	{
+	  return false;
+	}
+    }
+  return true;
+}
+
+/* Initialize slp_transpose flag before transposing.  */
+
+static void
+init_stmt_info_slp_transpose (bb_vec_info bb_vinfo)
+{
+  stmt_vec_info first_element = NULL;
+  unsigned int k = 0;
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_stores, k, first_element)
+    {
+      if (STMT_VINFO_GROUPED_ACCESS (first_element))
+	{
+	  DR_GROUP_SLP_TRANSPOSE (first_element) = false;
+	}
+    }
+  FOR_EACH_VEC_ELT (bb_vinfo->grouped_loads, k, first_element)
+    {
+      if (STMT_VINFO_GROUPED_ACCESS (first_element))
+	{
+	  DR_GROUP_SLP_TRANSPOSE (first_element) = false;
+	}
+    }
+}
+
+/* Analyze and transpose the stmts before building the SLP tree.  */
+
+static bool
+vect_analyze_transpose (bb_vec_info bb_vinfo)
+{
+  DUMP_VECT_SCOPE ("vect_analyze_transpose");
+
+  if (!vect_may_transpose (bb_vinfo))
+    {
+      return false;
+    }
+
+  /* For basic block SLP, try to merge the grouped stores and loads
+     into one group.  */
+  init_stmt_info_slp_transpose (bb_vinfo);
+  if (vect_transform_slp_grouped_stores (bb_vinfo)
+      && vect_merge_slp_grouped_loads (bb_vinfo))
+    {
+      if (dump_enabled_p ())
+	{
+	  dump_printf_loc (MSG_NOTE, vect_location,
+			   "Analysis succeeded with SLP transposed.\n");
+	}
+      return true;
+    }
+  if (dump_enabled_p ())
+    {
+      dump_printf_loc (MSG_NOTE, vect_location,
+		       "Analysis failed with SLP transposed.\n");
+    }
+  return false;
+}
 
 /* Check if there are stmts in the loop can be vectorized using SLP.  Build SLP
    trees of packed scalar stmts if SLP is possible.  */
@@ -3124,7 +4018,11 @@ vect_bb_vectorization_profitable_p (bb_vec_info bb_vinfo)
 
   vec_outside_cost = vec_prologue_cost + vec_epilogue_cost;
 
-  if (dump_enabled_p ())
+  BB_VINFO_VEC_INSIDE_COST (bb_vinfo) = vec_inside_cost;
+  BB_VINFO_VEC_OUTSIDE_COST (bb_vinfo) = vec_outside_cost;
+  BB_VINFO_SCALAR_COST (bb_vinfo) = scalar_cost;
+
+  if (!unlimited_cost_model (NULL) && dump_enabled_p ())
     {
       dump_printf_loc (MSG_NOTE, vect_location, "Cost model analysis: \n");
       dump_printf (MSG_NOTE, "  Vector inside of basic block cost: %d\n",
@@ -3239,6 +4137,22 @@ vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal)
 
   vect_pattern_recog (bb_vinfo);
 
+  /* Transpose grouped stores and loads for better vectorizable version.  */
+  if (bb_vinfo->transposed)
+    {
+      if (!vect_analyze_transpose (bb_vinfo))
+	{
+	  if (dump_enabled_p ())
+	    {
+	       dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+				"not vectorized: unhandled slp transposed in "
+				"basic block.\n");
+	    }
+	  return false;
+	}
+    }
+  bb_vinfo->before_slp = true;
+
   /* Check the SLP opportunities in the basic block, analyze and build SLP
      trees.  */
   if (!vect_analyze_slp (bb_vinfo, n_stmts))
@@ -3254,6 +4168,20 @@ vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal)
       return false;
     }
 
+  /* Check if the vectype is suitable for SLP transposed.  */
+  if (bb_vinfo->transposed && !check_for_slp_vectype (bb_vinfo))
+    {
+      if (dump_enabled_p ())
+	{
+	  dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+			   "Failed to SLP transposed in the basic block.\n");
+	  dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+			   "not vectorized: vectype is not suitable for "
+			   "SLP transposed in basic block.\n");
+	}
+      return false;
+    }
+
   vect_record_base_alignments (bb_vinfo);
 
   /* Analyze and verify the alignment of data references and the
@@ -3286,6 +4214,27 @@ vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal)
   if (! BB_VINFO_SLP_INSTANCES (bb_vinfo).length ())
     return false;
 
+  /* Check if the alignment is suitable for SLP transposed.  */
+  if (bb_vinfo->transposed)
+    {
+      for (i = 0; BB_VINFO_SLP_INSTANCES (bb_vinfo).iterate (i, &instance); i++)
+	{
+	  if (!check_for_dr_alignment (instance))
+	    {
+	      if (dump_enabled_p ())
+		{
+		  dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+				   "Failed to SLP transposed in the basic "
+				   "block.\n");
+		  dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+				   "not vectorized: alignment is not suitable "
+				   "for SLP transposed in basic block.\n");
+		}
+	      return false;
+	    }
+	}
+    }
+
   if (!vect_slp_analyze_operations (bb_vinfo))
     {
       if (dump_enabled_p ())
@@ -3311,6 +4260,83 @@ vect_slp_analyze_bb_1 (bb_vec_info bb_vinfo, int n_stmts, bool &fatal)
   return true;
 }
 
+static bool
+may_new_transpose_bbvinfo (bb_vec_info bb_vinfo_ori, bool res_ori)
+{
+  /* If the flag is false or the slp analysis is broken before
+     vect_analyze_slp, we don't try to analyze the transposed SLP version.  */
+  if (!flag_tree_slp_transpose_vectorize
+      || !BB_VINFO_BEFORE_SLP (bb_vinfo_ori))
+    {
+      return false;
+    }
+
+  /* If the original bb_vinfo can't be vectorized, try to new a bb_vinfo
+     of the transposed version.  */
+  if (!res_ori)
+    {
+      return true;
+    }
+
+  /* Caculate the cost of the original bb_vinfo.  */
+  if (unlimited_cost_model (NULL))
+    {
+      vect_bb_vectorization_profitable_p (bb_vinfo_ori);
+    }
+  /* If the vec cost and scalar cost are not much difference (here we set the
+     threshold to 4), we try to new a bb_vinfo of the transposed version.  */
+  if (BB_VINFO_SCALAR_COST (bb_vinfo_ori)
+      < 4 * (BB_VINFO_VEC_INSIDE_COST (bb_vinfo_ori)
+	     + BB_VINFO_VEC_OUTSIDE_COST (bb_vinfo_ori)))
+    {
+      return true;
+    }
+  return false;
+}
+
+static bool
+may_choose_transpose_bbvinfo (bb_vec_info bb_vinfo_trans, bool res_trans,
+			     bb_vec_info bb_vinfo_ori, bool res_ori)
+{
+  /* The original bb_vinfo is chosen if the transposed bb_vinfo
+     can't be vectorized.  */
+  if (!res_trans)
+    {
+      return false;
+    }
+  /* Caculate the cost of the transposed bb_vinfo.  */
+  if (unlimited_cost_model (NULL))
+    {
+      vect_bb_vectorization_profitable_p (bb_vinfo_trans);
+    }
+  int diff_bb_cost = -1;
+  int diff_bb_cost_trans = -1;
+  if (res_ori)
+    {
+      diff_bb_cost = BB_VINFO_SCALAR_COST (bb_vinfo_ori)
+		     - BB_VINFO_VEC_INSIDE_COST (bb_vinfo_ori)
+		     - BB_VINFO_VEC_OUTSIDE_COST (bb_vinfo_ori);
+    }
+  if (res_trans)
+    {
+      diff_bb_cost_trans = BB_VINFO_SCALAR_COST (bb_vinfo_trans)
+			   - BB_VINFO_VEC_INSIDE_COST (bb_vinfo_trans)
+			   - BB_VINFO_VEC_OUTSIDE_COST (bb_vinfo_trans);
+    }
+  /* The original bb_vinfo is chosen when one of the following conditions
+     is satisfied as follows:
+	1) The cost of original version is better transposed version.
+	2) The vec cost is similar to scalar cost in the transposed version.  */
+  if ((res_ori && res_trans && diff_bb_cost >= diff_bb_cost_trans)
+      || (res_trans && BB_VINFO_SCALAR_COST (bb_vinfo_trans)
+		       <= (BB_VINFO_VEC_INSIDE_COST (bb_vinfo_trans)
+			  + BB_VINFO_VEC_OUTSIDE_COST (bb_vinfo_trans))))
+    {
+      return false;
+    }
+  return true;
+}
+
 /* Subroutine of vect_slp_bb.  Try to vectorize the statements between
    REGION_BEGIN (inclusive) and REGION_END (exclusive), returning true
    on success.  The region has N_STMTS statements and has the datarefs
@@ -3323,6 +4349,7 @@ vect_slp_bb_region (gimple_stmt_iterator region_begin,
 		    unsigned int n_stmts)
 {
   bb_vec_info bb_vinfo;
+  bb_vec_info bb_vinfo_trans = NULL;
   auto_vector_modes vector_modes;
 
   /* Autodetect first vector size we try.  */
@@ -3337,6 +4364,10 @@ vect_slp_bb_region (gimple_stmt_iterator region_begin,
     {
       bool vectorized = false;
       bool fatal = false;
+      bool res_bb_vinfo_ori = false;
+      bool res_bb_vinfo_trans = false;
+
+      /* New a bb_vinfo of the original version.  */
       bb_vinfo = new _bb_vec_info (region_begin, region_end, &shared);
 
       bool first_time_p = shared.datarefs.is_empty ();
@@ -3346,8 +4377,57 @@ vect_slp_bb_region (gimple_stmt_iterator region_begin,
       else
 	bb_vinfo->shared->check_datarefs ();
       bb_vinfo->vector_mode = next_vector_mode;
+      bb_vinfo->transposed = false;
+      bb_vinfo->before_slp = false;
+
+      res_bb_vinfo_ori = vect_slp_analyze_bb_1 (bb_vinfo, n_stmts, fatal);
+      /* Analyze and new a transposed bb_vinfo.  */
+      if (may_new_transpose_bbvinfo (bb_vinfo, res_bb_vinfo_ori))
+	{
+	  bool fatal_trans = false;
+	  bb_vinfo_trans
+	    = new _bb_vec_info (region_begin, region_end, &shared);
+	  bool first_time_p = shared.datarefs.is_empty ();
+	  BB_VINFO_DATAREFS (bb_vinfo_trans) = datarefs;
+	  if (first_time_p)
+	    {
+	      bb_vinfo_trans->shared->save_datarefs ();
+	    }
+	  else
+	    {
+	      bb_vinfo_trans->shared->check_datarefs ();
+	    }
+	  bb_vinfo_trans->vector_mode = next_vector_mode;
+	  bb_vinfo_trans->transposed = true;
+	  bb_vinfo_trans->before_slp = false;
+
+	  res_bb_vinfo_trans
+	    = vect_slp_analyze_bb_1 (bb_vinfo_trans, n_stmts, fatal_trans);
+	  if (may_choose_transpose_bbvinfo (bb_vinfo_trans,
+					   res_bb_vinfo_trans,
+					   bb_vinfo, res_bb_vinfo_ori))
+	    {
+	      bb_vinfo = bb_vinfo_trans;
+	      fatal = fatal_trans;
+	      if (dump_enabled_p ())
+		{
+		  dump_printf_loc (MSG_NOTE, vect_location,
+				   "Basic block part vectorized "
+				   "using transposed version.\n");
+		}
+	    }
+	  else
+	    {
+	      if (dump_enabled_p ())
+		{
+		  dump_printf_loc (MSG_NOTE, vect_location,
+				   "Basic block part vectorized "
+				   "using original version.\n");
+		}
+	    }
+	}
 
-      if (vect_slp_analyze_bb_1 (bb_vinfo, n_stmts, fatal)
+      if ((res_bb_vinfo_ori || res_bb_vinfo_trans)
 	  && dbg_cnt (vect_slp))
 	{
 	  if (dump_enabled_p ())
@@ -3400,6 +4480,10 @@ vect_slp_bb_region (gimple_stmt_iterator region_begin,
 	  }
 
       delete bb_vinfo;
+      if (bb_vinfo_trans)
+	{
+	  bb_vinfo_trans = NULL;
+	}
 
       if (mode_i < vector_modes.length ()
 	  && VECTOR_MODE_P (autodetected_vector_mode)
diff --git a/gcc/tree-vect-stmts.c b/gcc/tree-vect-stmts.c
index 6418edb52..b872cfc8d 100644
--- a/gcc/tree-vect-stmts.c
+++ b/gcc/tree-vect-stmts.c
@@ -7329,6 +7329,153 @@ vectorizable_scan_store (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
   return true;
 }
 
+/* Function vect_permute_store_chains
+
+   Call function vect_permute_store_chain ().
+   Given a chain of interleaved stores in DR_CHAIN, generate
+   interleave_high/low stmts to reorder the data correctly.
+   Return the final references for stores in RESULT_CHAIN.  */
+
+static void
+vect_permute_store_chains (vec<tree> dr_chain, unsigned int num_each,
+			   stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
+			   vec<tree> *result_chain, unsigned int group)
+{
+  unsigned int k = 0;
+  unsigned int t = 0;
+
+  /* Divide vectors into GROUP parts.  And permute every NUM_EACH vectors
+     together.  */
+  for (k = 0; k < group; k++)
+    {
+      auto_vec<tree> dr_chain_transposed (num_each);
+      auto_vec<tree> result_chain_transposed (num_each);
+      for (t = k; t < dr_chain.length (); t = t + group)
+	{
+	  dr_chain_transposed.quick_push (dr_chain[t]);
+	}
+      vect_permute_store_chain (dr_chain_transposed, num_each, stmt_info,
+				gsi, &result_chain_transposed);
+      for (t = 0; t < num_each; t++)
+	{
+	  result_chain->quick_push (result_chain_transposed[t]);
+	}
+    }
+}
+
+/* Function transpose_oprnd_store
+
+    Calculate the transposed results from VEC_OPRNDS (VEC_STMT)
+    for vectorizable_store.  */
+
+static void
+transpose_oprnd_store (vec<tree>vec_oprnds, vec<tree> *result_chain,
+		       unsigned int vec_num, unsigned int const_nunits,
+		       unsigned int array_num, stmt_vec_info first_stmt_info,
+		       gimple_stmt_iterator *gsi)
+{
+  unsigned int group_for_transform = 0;
+  unsigned int num_each = 0;
+
+  /* Transpose back for vec_oprnds.  */
+  /* vec = {vec1, vec2, ...}  */
+  if (array_num < const_nunits
+      && const_nunits % array_num == 0)
+    {
+      vect_transpose_store_chain (vec_oprnds,
+				  vec_num, array_num,
+				  first_stmt_info,
+				  gsi, result_chain);
+    }
+   /* vec1 = {vec_part1}, vec2 = {vec_part2}, ...  */
+  else if (array_num >= const_nunits
+	   && array_num % const_nunits == 0)
+    {
+      group_for_transform = array_num / const_nunits;
+      num_each = vec_oprnds.length () / group_for_transform;
+      vect_permute_store_chains (vec_oprnds,
+				 num_each, first_stmt_info,
+				 gsi, result_chain,
+				 group_for_transform);
+    }
+  else
+    {
+      gcc_unreachable ();
+    }
+}
+
+static dr_vec_info *
+get_dr_info (stmt_vec_info stmt_info)
+{
+  dr_vec_info *dr_info = STMT_VINFO_DR_INFO (stmt_info);
+  if (dr_info->misalignment == DR_MISALIGNMENT_UNINITIALIZED)
+    {
+      SET_DR_MISALIGNMENT (dr_info, DR_MISALIGNMENT_UNKNOWN);
+    }
+  return dr_info;
+}
+
+static unsigned
+dr_align_vect_store (dr_vec_info *cur_first_dr_info,
+		     unsigned HOST_WIDE_INT &align)
+{
+  unsigned misalign = 0;
+  align = known_alignment (DR_TARGET_ALIGNMENT (cur_first_dr_info));
+  if (aligned_access_p (cur_first_dr_info))
+    {
+      return misalign;
+    }
+  else if (DR_MISALIGNMENT (cur_first_dr_info) == -1)
+    {
+      align = dr_alignment (vect_dr_behavior (cur_first_dr_info));
+    }
+  else
+    {
+      misalign = DR_MISALIGNMENT (cur_first_dr_info);
+    }
+  return misalign;
+}
+
+static stmt_vec_info
+add_new_stmt_vect_store (tree vectype, tree dataref_ptr, tree dataref_offset,
+			 tree ref_type, dr_vec_info *cur_first_dr_info,
+			 tree vec_oprnd, gimple_stmt_iterator *gsi,
+			 stmt_vec_info stmt_info)
+{
+  /* Data align.  */
+  unsigned HOST_WIDE_INT align;
+  unsigned misalign = dr_align_vect_store (cur_first_dr_info, align);
+
+  if (dataref_offset == NULL_TREE && TREE_CODE (dataref_ptr) == SSA_NAME)
+    {
+      set_ptr_info_alignment (get_ptr_info (dataref_ptr), align, misalign);
+    }
+
+  /* Get data_ref.  */
+  tree offset = dataref_offset ? dataref_offset : build_int_cst (ref_type, 0);
+  tree data_ref = fold_build2 (MEM_REF, vectype, dataref_ptr, offset);
+  if (aligned_access_p (cur_first_dr_info))
+    {
+      ;
+    }
+  else if (DR_MISALIGNMENT (cur_first_dr_info) == -1)
+    {
+      TREE_TYPE (data_ref) = build_aligned_type (TREE_TYPE (data_ref),
+						 align * BITS_PER_UNIT);
+    }
+  else
+    {
+      tree elem_type = TREE_TYPE (vectype);
+      TREE_TYPE (data_ref) = build_aligned_type (TREE_TYPE (data_ref),
+						 TYPE_ALIGN (elem_type));
+    }
+  /* Add new stmt.  */
+  vect_copy_ref_info (data_ref, DR_REF (cur_first_dr_info->dr));
+  gassign *new_stmt = gimple_build_assign (data_ref, vec_oprnd);
+  stmt_vec_info new_stmt_info
+		= vect_finish_stmt_generation (stmt_info, new_stmt, gsi);
+  return new_stmt_info;
+}
 
 /* Function vectorizable_store.
 
@@ -8208,6 +8355,16 @@ vectorizable_store (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
 	  else if (STMT_VINFO_GATHER_SCATTER_P (stmt_info))
 	    vect_get_gather_scatter_ops (loop, stmt_info, &gs_info,
 					 &dataref_ptr, &vec_offset);
+	  /* If the stmt_info need to be transposed recovery, dataref_ptr
+	     will be caculated later.  */
+	  else if (memory_access_type == VMAT_CONTIGUOUS
+		   && is_a <bb_vec_info> (vinfo)
+		   && STMT_VINFO_GROUPED_ACCESS (stmt_info)
+		   && DR_GROUP_SLP_TRANSPOSE (
+			DR_GROUP_FIRST_ELEMENT (stmt_info)))
+	    {
+	      dataref_ptr = NULL_TREE;
+	    }
 	  else
 	    dataref_ptr
 	      = vect_create_data_ref_ptr (first_stmt_info, aggr_type,
@@ -8299,6 +8456,75 @@ vectorizable_store (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
 	}
       else
 	{
+	  /* group_size: the size of group after transposing and merging.
+	     group_size_b: the size of group before transposing and merging,
+			 and only group_size_b >= const_nunits is supported.
+	     array_num: the number of arrays.
+	     const_nunits: TYPE_VECTOR_SUBPARTS (vectype).
+	     ncontinues: group_size_b / const_nunits, it means the number of
+			 times an array is stored in memory.  */
+	  if (slp && is_a <bb_vec_info> (vinfo)
+	      && STMT_VINFO_GROUPED_ACCESS (stmt_info)
+	      && DR_GROUP_SLP_TRANSPOSE (DR_GROUP_FIRST_ELEMENT (stmt_info)))
+	    {
+	      if (dump_enabled_p ())
+		{
+		  dump_printf_loc (MSG_NOTE, vect_location,
+				   "vectorizable_store for slp transpose.\n");
+		}
+	      /* Transpose back for grouped stores.  */
+	      vect_transform_back_slp_grouped_stores (bb_vinfo,
+						      first_stmt_info);
+
+	      result_chain.create (vec_oprnds.length ());
+	      unsigned int const_nunits = nunits.to_constant ();
+	      unsigned int group_size_b = DR_GROUP_SIZE_TRANS (first_stmt_info);
+	      unsigned int array_num = group_size / group_size_b;
+	      transpose_oprnd_store (vec_oprnds, &result_chain, vec_num,
+				     const_nunits, array_num,
+				     first_stmt_info, gsi);
+
+	      /* For every store group, not for every vec, because transposing
+	      and merging have changed the data reference access.  */
+	      gcc_assert (group_size_b >= const_nunits);
+	      unsigned int ncontinues = group_size_b / const_nunits;
+
+	      unsigned int k = 0;
+	      for (i = 0; i < array_num; i++)
+		{
+		  stmt_vec_info first_stmt_b;
+		  BB_VINFO_GROUPED_STORES (vinfo).iterate (i, &first_stmt_b);
+		  bool simd_lane_access_p
+			= STMT_VINFO_SIMD_LANE_ACCESS_P (first_stmt_b) != 0;
+		  tree ref_type = get_group_alias_ptr_type (first_stmt_b);
+		  dataref_ptr = vect_create_data_ref_ptr (
+				 first_stmt_b, aggr_type,
+				 simd_lane_access_p ? loop : NULL,
+				 offset, &dummy, gsi, &ptr_incr,
+				 simd_lane_access_p, NULL_TREE, bump);
+		  dr_vec_info *cur_first_dr_info = get_dr_info (first_stmt_b);
+		  for (unsigned int t = 0; t < ncontinues; t++)
+		    {
+		      vec_oprnd = result_chain[k];
+		      k++;
+		      if (t > 0)
+			{
+			  /* Bump the vector pointer.  */
+			  dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr,
+							 gsi, first_stmt_b,
+							 bump);
+			}
+		      new_stmt_info = add_new_stmt_vect_store (
+					vectype, dataref_ptr, dataref_offset,
+					ref_type, cur_first_dr_info, vec_oprnd,
+					gsi, first_stmt_b);
+		    }
+		}
+	      oprnds.release ();
+	      result_chain.release ();
+	      vec_oprnds.release ();
+	      return true;
+	    }
 	  new_stmt_info = NULL;
 	  if (grouped_store)
 	    {
@@ -8557,6 +8783,447 @@ hoist_defs_of_uses (stmt_vec_info stmt_info, class loop *loop)
   return true;
 }
 
+static tree
+calculate_new_type (tree vectype, unsigned int const_nunits,
+		    unsigned int group_size_b, unsigned int &nloads,
+		    unsigned int &ncontinues, tree &lvectype)
+{
+  tree ltype = TREE_TYPE (vectype);
+  /* nloads is the number of ARRAYs in a vector.
+     vectemp = {a[], b[], ...}  */
+  if (group_size_b < const_nunits)
+    {
+      tree ptype;
+      tree vtype
+	= vector_vector_composition_type (vectype,
+					  const_nunits / group_size_b,
+					  &ptype);
+      if (vtype != NULL_TREE)
+	{
+	  nloads = const_nunits / group_size_b;
+	  lvectype = vtype;
+	  ltype = ptype;
+	  ncontinues = 1;
+	}
+    }
+  /* ncontinues is the number of vectors from an ARRAY.
+     vectemp1 = {a[0], a[1], ...}
+     ...
+     vectempm = {a[k], a[k+1], ...}  */
+  else
+    {
+      nloads = 1;
+      ltype = vectype;
+      ncontinues = group_size_b / const_nunits;
+    }
+  ltype = build_aligned_type (ltype, TYPE_ALIGN (TREE_TYPE (vectype)));
+  return ltype;
+}
+
+static void
+generate_old_load_permutations (slp_tree slp_node, unsigned int group_size,
+				vec<unsigned> &old_load_permutation)
+{
+  /* Generate the old load permutations from the slp_node.  */
+  unsigned i = 0;
+  unsigned k = 0;
+
+  /* If SLP_NODE has load_permutation, we copy it to old_load_permutation.
+     Otherwise, we generate a permutation sequentially.  */
+  if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
+    {
+      FOR_EACH_VEC_ELT (SLP_TREE_LOAD_PERMUTATION (slp_node), i, k)
+	{
+	  old_load_permutation.safe_push (k);
+	}
+    }
+  else
+    {
+      for (unsigned i = 0; i < group_size; i++)
+	{
+	  old_load_permutation.safe_push (i);
+	}
+    }
+}
+
+static void
+generate_new_load_permutation_mapping (unsigned slp_node_length,
+				       vec<unsigned> &group_idx,
+				       const vec<unsigned> &load_permutation,
+				       unsigned int group_size_b,
+				       unsigned &new_group_size,
+				       vec<unsigned> &group_from)
+{
+  /* group_num_vec: only stores the group_loads IDs which are caculated from
+     load_permutation.  */
+  auto_vec<unsigned> group_num_vec;
+
+  /* Caculate which group_loads are the stmts in SLP_NODE from.  */
+  unsigned i = 0;
+  unsigned k = 0;
+  FOR_EACH_VEC_ELT (load_permutation, i, k)
+    {
+      unsigned int t0 = k / group_size_b;
+      if (!group_num_vec.contains (t0))
+	{
+	  group_num_vec.safe_push (t0);
+	}
+      group_from.safe_push (t0);
+    }
+  group_num_vec.qsort (cmp_for_group_num);
+  /* n_groups: the number of group_loads.  */
+  unsigned int n_groups = group_num_vec.length ();
+  new_group_size = n_groups * group_size_b;
+  for (i = 0; i < n_groups; i++)
+    {
+      group_idx.safe_push (group_num_vec[i] * group_size_b);
+    }
+  /* A new mapping from group_ind_vec to group_from.
+      For example:
+	Origin: group_from = {1,1,3,3,5,5,7,7};
+	After mapping: group_from = {0,0,1,1,2,2,2,2};  */
+  auto_vec<unsigned> group_ind_vec (n_groups);
+  for (k = 0; k < n_groups; k++)
+    {
+      group_ind_vec.safe_push (k);
+    }
+  for (i = 0; i < slp_node_length; i++)
+    {
+      for (k = 0; k < n_groups; k++)
+	{
+	  if (group_from[i] == group_num_vec[k])
+	    {
+	      group_from[i] = group_ind_vec[k];
+	      break;
+	    }
+	}
+    }
+}
+
+static void
+generate_new_load_permutation (vec<unsigned> &new_load_permutation,
+			       const vec<unsigned> &old_load_permutation,
+			       slp_tree slp_node, bool &this_load_permuted,
+			       const vec<unsigned> &group_from,
+			       unsigned int group_size_b)
+{
+  unsigned slp_node_length = SLP_TREE_SCALAR_STMTS (slp_node).length ();
+  /* Generate the new load permutation from the new mapping.  */
+  new_load_permutation.create (slp_node_length);
+  unsigned i = 0;
+  unsigned k = 0;
+  FOR_EACH_VEC_ELT (old_load_permutation, i, k)
+    {
+      /* t1 is the new permutation of k in the old permutation.
+	 t1 = base_address + offset:
+	 base_address = group_from[i] * group_size_b;
+	 offset = k % group_size_b.  */
+      unsigned int t1
+	= group_from[i] * group_size_b + k % group_size_b;
+      new_load_permutation.safe_push (t1);
+      if (t1 != k)
+	{
+	  this_load_permuted = true;
+	}
+    }
+}
+
+static bool
+is_slp_perm (bool slp_perm, bool this_load_permuted, poly_uint64 nunits,
+	     unsigned int group_size, stmt_vec_info first_stmt_info)
+{
+  /* Calculate the unrolling factor based on the smallest type.  */
+  poly_uint64 unrolling_factor
+    = exact_div (common_multiple (nunits, group_size), group_size);
+  /* The load requires permutation when unrolling exposes
+     a gap either because the group is larger than the SLP
+     group-size or because there is a gap between the groups.  */
+  if (!slp_perm && !this_load_permuted
+      && (known_eq (unrolling_factor, 1U)
+	  || (group_size == DR_GROUP_SIZE (first_stmt_info)
+	      && DR_GROUP_GAP (first_stmt_info) == 0)))
+    {
+      return false;
+    }
+  else
+    {
+      return true;
+    }
+}
+
+static void
+generate_load_permutation (slp_tree slp_node, unsigned &new_group_size,
+			   unsigned int group_size, unsigned int group_size_b,
+			   bool &this_load_permuted, vec<unsigned> &group_idx,
+			   vec<unsigned> &new_load_permutation)
+{
+  /* Generate the old load permutations from SLP_NODE.  */
+  vec<unsigned> old_load_permutation;
+  old_load_permutation.create (group_size);
+  generate_old_load_permutations (slp_node, group_size, old_load_permutation);
+
+  /* Caculate which group_loads are the stmts in SLP_NODE from.  */
+  unsigned slp_node_length = SLP_TREE_SCALAR_STMTS (slp_node).length ();
+  /* group_from: stores the group_loads ID for every stmt in SLP_NODE.  */
+  vec<unsigned> group_from;
+  group_from.create (slp_node_length);
+  generate_new_load_permutation_mapping (slp_node_length, group_idx,
+					 old_load_permutation,
+					 group_size_b, new_group_size,
+					 group_from);
+
+  /* Generate the new load permutation from the new mapping and caculate
+     this_load_permuted flag.  If this_load_permuted is true, we need execute
+     slp permutation by using new load permutation.  */
+  generate_new_load_permutation (new_load_permutation, old_load_permutation,
+				 slp_node, this_load_permuted, group_from,
+				 group_size_b);
+  old_load_permutation.release ();
+  group_from.release ();
+}
+
+static unsigned int
+dr_align_vect_load (dr_vec_info *cur_first_dr_info,
+		    unsigned HOST_WIDE_INT &align,
+		    enum dr_alignment_support alignment_support_scheme)
+{
+  unsigned int misalign = 0;
+
+  align = known_alignment (DR_TARGET_ALIGNMENT (cur_first_dr_info));
+  if (alignment_support_scheme == dr_aligned)
+    {
+      gcc_assert (aligned_access_p (cur_first_dr_info));
+    }
+  else if (DR_MISALIGNMENT (cur_first_dr_info) == -1)
+    {
+      align = dr_alignment (vect_dr_behavior (cur_first_dr_info));
+    }
+  else
+    {
+      misalign = DR_MISALIGNMENT (cur_first_dr_info);
+    }
+  return misalign;
+}
+
+static stmt_vec_info
+add_new_stmt_vect_load (tree vectype, tree dataref_ptr, tree dataref_offset,
+			tree ref_type, tree ltype, gassign *(&new_stmt),
+			dr_vec_info *cur_first_dr_info,
+			gimple_stmt_iterator *gsi, stmt_vec_info stmt_info)
+{
+  /* Data align.  */
+  enum dr_alignment_support alignment_support_scheme
+	= vect_supportable_dr_alignment (cur_first_dr_info, false);
+  unsigned HOST_WIDE_INT align;
+  unsigned int misalign = dr_align_vect_load (cur_first_dr_info, align,
+					      alignment_support_scheme);
+  if (dataref_offset == NULL_TREE && TREE_CODE (dataref_ptr) == SSA_NAME)
+    {
+      set_ptr_info_alignment (get_ptr_info (dataref_ptr), align, misalign);
+    }
+
+  /* Get data_ref.  */
+  tree offset = dataref_offset ? dataref_offset : build_int_cst (ref_type, 0);
+  tree data_ref = fold_build2 (MEM_REF, ltype, dataref_ptr, offset);
+  if (alignment_support_scheme == dr_aligned)
+    {
+      ;
+    }
+  else if (DR_MISALIGNMENT (cur_first_dr_info) == -1)
+    {
+      TREE_TYPE (data_ref)
+	= build_aligned_type (TREE_TYPE (data_ref), align * BITS_PER_UNIT);
+    }
+  else
+    {
+      tree elem_type = TREE_TYPE (vectype);
+      TREE_TYPE (data_ref)
+	= build_aligned_type (TREE_TYPE (data_ref), TYPE_ALIGN (elem_type));
+    }
+
+  /* Add new stmt.  */
+  vect_copy_ref_info (data_ref, DR_REF (cur_first_dr_info->dr));
+  new_stmt = gimple_build_assign (make_ssa_name (ltype), data_ref);
+  stmt_vec_info new_stmt_info
+	= vect_finish_stmt_generation (stmt_info, new_stmt, gsi);
+  return new_stmt_info;
+}
+
+static void
+push_new_stmt_to_dr_chain (bool slp_perm, stmt_vec_info new_stmt_info,
+			   vec<tree> &dr_chain, slp_tree slp_node)
+{
+    if (slp_perm)
+      {
+	dr_chain.quick_push (gimple_assign_lhs (new_stmt_info->stmt));
+      }
+    else
+      {
+	SLP_TREE_VEC_STMTS (slp_node).quick_push (new_stmt_info);
+      }
+}
+
+static stmt_vec_info
+get_first_stmt_info_before_transpose (stmt_vec_info first_stmt_info,
+				      unsigned int group_el,
+				      unsigned int group_size)
+{
+  stmt_vec_info last_stmt_info = first_stmt_info;
+  unsigned int count = 0;
+  gcc_assert (group_el < group_size);
+  while (count < group_el)
+    {
+      last_stmt_info = DR_GROUP_NEXT_ELEMENT (last_stmt_info);
+      count++;
+    }
+  return last_stmt_info;
+}
+
+static stmt_vec_info
+add_new_stmt_for_nloads_greater_than_one (tree lvectype, tree vectype,
+					  vec<constructor_elt, va_gc> *v,
+					  stmt_vec_info stmt_info,
+					  gimple_stmt_iterator *gsi)
+{
+  tree vec_inv = build_constructor (lvectype, v);
+  tree new_temp = vect_init_vector (stmt_info, vec_inv, lvectype, gsi);
+  vec_info *vinfo = stmt_info->vinfo;
+  stmt_vec_info new_stmt_info = vinfo->lookup_def (new_temp);
+  if (lvectype != vectype)
+    {
+      gassign *new_stmt = gimple_build_assign (make_ssa_name (vectype),
+					       VIEW_CONVERT_EXPR,
+					       build1 (VIEW_CONVERT_EXPR,
+						       vectype, new_temp));
+      new_stmt_info = vect_finish_stmt_generation (stmt_info, new_stmt, gsi);
+    }
+  return new_stmt_info;
+}
+
+/* Function new_vect_stmt_for_nloads.
+
+   New a VEC_STMT when nloads Arrays are merged into a vector.
+
+   ncopies is the number of vectors that need to be loaded from memmory.
+   nloads is the number of ARRAYs in a vector.
+   vectemp = {a[], b[], ...}  */
+
+static void
+new_vect_stmt_for_nloads (unsigned int ncopies, unsigned int nloads,
+			  vec<unsigned> group_idx, stmt_vec_info stmt_info,
+			  offset_info *offset_info, vectype_info *vectype_info,
+			  vect_memory_access_type memory_access_type,
+			  bool slp_perm, vec<tree>& dr_chain, slp_tree slp_node,
+			  gimple_stmt_iterator *gsi)
+{
+  vec<constructor_elt, va_gc> *v = NULL;
+  stmt_vec_info first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
+  unsigned int group_size = DR_GROUP_SIZE (first_stmt_info);
+  stmt_vec_info first_stmt_info_b = NULL;
+  stmt_vec_info new_stmt_info = NULL;
+  tree dataref_ptr = NULL_TREE;
+  tree dummy;
+  gimple *ptr_incr = NULL;
+  unsigned int n = 0;
+  for (unsigned int i = 0; i < ncopies; i++)
+    {
+      vec_alloc (v, nloads);
+      for (unsigned int t = 0; t < nloads; t++)
+	{
+	  first_stmt_info_b = get_first_stmt_info_before_transpose (
+				first_stmt_info, group_idx[n++], group_size);
+	  dr_vec_info* cur_first_dr_info = get_dr_info (first_stmt_info_b);
+	  tree bump = vect_get_data_ptr_increment (cur_first_dr_info,
+						   vectype_info->ltype,
+						   memory_access_type);
+	  bool simd_lane_access_p
+		= STMT_VINFO_SIMD_LANE_ACCESS_P (first_stmt_info_b) != 0;
+
+	  /* Create dataref_ptr which is point to init_address.  */
+	  dataref_ptr = vect_create_data_ref_ptr (
+			 first_stmt_info_b, vectype_info->ltype, NULL,
+			 offset_info->offset, &dummy, gsi, &ptr_incr,
+			 simd_lane_access_p, offset_info->byte_offset, bump);
+
+	  gassign *new_stmt = NULL;
+	  new_stmt_info = add_new_stmt_vect_load (
+			   vectype_info->vectype, dataref_ptr,
+			   offset_info->dataref_offset, vectype_info->ref_type,
+			   vectype_info->ltype, new_stmt, cur_first_dr_info,
+			   gsi, first_stmt_info_b);
+
+	  CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, gimple_assign_lhs (new_stmt));
+	}
+	new_stmt_info = add_new_stmt_for_nloads_greater_than_one (
+				 vectype_info->lvectype, vectype_info->vectype,
+				 v, first_stmt_info_b, gsi);
+	push_new_stmt_to_dr_chain (slp_perm, new_stmt_info,
+				   dr_chain, slp_node);
+    }
+}
+
+/* Function new_vect_stmt_for_ncontinues.
+
+   New a VEC_STMTs when an Array is divided into several vectors.
+
+   n_groups is the number of ARRAYs.
+   ncontinues is the number of vectors from an ARRAY.
+   vectemp1 = {a[0], a[1], ...}
+   ...
+   vectempm = {a[k], a[k+1], ...}  */
+
+static void
+new_vect_stmt_for_ncontinues (unsigned int ncontinues, vec<unsigned> group_idx,
+			      stmt_vec_info stmt_info, offset_info* offset_info,
+			      vectype_info* vectype_info,
+			      vect_memory_access_type memory_access_type,
+			      bool slp_perm, vec<tree>& dr_chain,
+			      slp_tree slp_node,
+			      gimple_stmt_iterator *gsi)
+{
+  stmt_vec_info first_stmt_info = DR_GROUP_FIRST_ELEMENT (stmt_info);
+  unsigned int group_size = DR_GROUP_SIZE (first_stmt_info);
+  stmt_vec_info new_stmt_info = NULL;
+  tree dataref_ptr = NULL_TREE;
+  tree dummy;
+  gimple *ptr_incr = NULL;
+  unsigned int n_groups = group_idx.length ();
+  for (unsigned int i = 0; i < n_groups; i++)
+    {
+      stmt_vec_info first_stmt_info_b = get_first_stmt_info_before_transpose (
+				first_stmt_info, group_idx[i], group_size);
+      dr_vec_info* cur_first_dr_info = get_dr_info (first_stmt_info_b);
+      tree bump = vect_get_data_ptr_increment (cur_first_dr_info,
+			vectype_info->ltype, memory_access_type);
+      bool simd_lane_access_p
+		= STMT_VINFO_SIMD_LANE_ACCESS_P (first_stmt_info_b) != 0;
+      for (unsigned int k = 0; k < ncontinues; k++)
+	{
+	  /* Create dataref_ptr which is point to init_address.  */
+	  if (k == 0)
+	    {
+	      dataref_ptr = vect_create_data_ref_ptr (
+			 first_stmt_info_b, vectype_info->ltype, NULL,
+			 offset_info->offset, &dummy, gsi, &ptr_incr,
+			 simd_lane_access_p, offset_info->byte_offset, bump);
+	    }
+	  else
+	    {
+	      dataref_ptr = bump_vector_ptr (dataref_ptr, ptr_incr,
+					  gsi, first_stmt_info_b, bump);
+	    }
+	  gassign *new_stmt = NULL;
+	  new_stmt_info = add_new_stmt_vect_load (
+			   vectype_info->vectype, dataref_ptr,
+			   offset_info->dataref_offset, vectype_info->ref_type,
+			   vectype_info->ltype, new_stmt, cur_first_dr_info,
+			   gsi, first_stmt_info_b);
+	  push_new_stmt_to_dr_chain (slp_perm, new_stmt_info,
+				     dr_chain, slp_node);
+	}
+    }
+}
+
 /* vectorizable_load.
 
    Check if STMT_INFO reads a non scalar data-ref (array/pointer/structure)
@@ -9364,6 +10031,9 @@ vectorizable_load (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
   tree vec_mask = NULL_TREE;
   prev_stmt_info = NULL;
   poly_uint64 group_elt = 0;
+  unsigned new_group_size = 0;
+  vec<unsigned> new_load_permutation;
+
   for (j = 0; j < ncopies; j++)
     {
       stmt_vec_info new_stmt_info = NULL;
@@ -9385,6 +10055,15 @@ vectorizable_load (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
 	      dataref_ptr = unshare_expr (DR_BASE_ADDRESS (first_dr_info->dr));
 	      dataref_offset = build_int_cst (ref_type, 0);
 	    }
+	  /* If the stmt_info need to be transposed recovery, dataref_ptr
+	     will be caculated later.  */
+	  else if (slp && is_a <bb_vec_info> (vinfo)
+		   && STMT_VINFO_GROUPED_ACCESS (stmt_info)
+		   && DR_GROUP_SLP_TRANSPOSE (
+			DR_GROUP_FIRST_ELEMENT (stmt_info)))
+	    {
+	      dataref_ptr = NULL_TREE;
+	    }
 	  else if (diff_first_stmt_info)
 	    {
 	      dataref_ptr
@@ -9501,6 +10180,63 @@ vectorizable_load (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
 	  /* Record that VEC_ARRAY is now dead.  */
 	  vect_clobber_variable (stmt_info, gsi, vec_array);
 	}
+      else if (slp && is_a <bb_vec_info> (vinfo)
+	       && STMT_VINFO_GROUPED_ACCESS (stmt_info)
+	       && DR_GROUP_SLP_TRANSPOSE (DR_GROUP_FIRST_ELEMENT (stmt_info)))
+	{
+	  if (dump_enabled_p ())
+	    {
+	      dump_printf_loc (MSG_NOTE, vect_location,
+			       "vectorizable_load for slp transpose.\n");
+	    }
+	  /* group_size: the size of group after merging.
+	     group_size_b: the size of group before merging.
+	     const_nunits: TYPE_VECTOR_SUBPARTS (vectype), it is the number of
+		elements in a vector.
+	     nloads: const_nunits / group_size_b or 1, it means the number
+		of ARRAYs in a vector.
+	     ncontinues: group_size_b / const_nunits or 1, it means the number
+		of vectors from an ARRAY.  */
+	  unsigned int group_size_b = DR_GROUP_SIZE_TRANS (first_stmt_info);
+	  unsigned int const_nunits = nunits.to_constant ();
+	  unsigned int nloads = const_nunits;
+	  unsigned int ncontinues = group_size_b;
+	  tree lvectype = vectype;
+	  tree ltype = calculate_new_type (vectype, const_nunits,
+					   group_size_b, nloads,
+					   ncontinues, lvectype);
+	  bool this_load_permuted = false;
+	  auto_vec<unsigned> group_idx;
+	  generate_load_permutation (slp_node, new_group_size, group_size,
+				     group_size_b, this_load_permuted,
+				     group_idx, new_load_permutation);
+	  slp_perm = is_slp_perm (slp_perm, this_load_permuted, nunits,
+  			  	  group_size, first_stmt_info);
+
+	  /* ncopies: the number of vectors that need to be loaded from
+		 memmory.  */
+	  unsigned int ncopies = new_group_size / const_nunits;
+	  offset_info offset_info = {offset, byte_offset, dataref_offset};
+	  vectype_info vectype_info = {vectype, ltype, lvectype, ref_type};
+	  if (slp_perm)
+	    {
+	       dr_chain.create (ncopies);
+	    }
+	  if (nloads > 1 && ncontinues == 1)
+	    {
+	      new_vect_stmt_for_nloads (ncopies, nloads, group_idx, stmt_info,
+					&offset_info, &vectype_info,
+					memory_access_type, slp_perm, dr_chain,
+					slp_node, gsi);
+	    }
+	  else
+	    {
+	      new_vect_stmt_for_ncontinues (ncontinues, group_idx, stmt_info,
+					    &offset_info, &vectype_info,
+					    memory_access_type, slp_perm,
+					    dr_chain, slp_node, gsi);
+	    }
+	}
       else
 	{
 	  for (i = 0; i < vec_num; i++)
@@ -9840,7 +10576,32 @@ vectorizable_load (stmt_vec_info stmt_info, gimple_stmt_iterator *gsi,
       if (slp && !slp_perm)
 	continue;
 
-      if (slp_perm)
+      /* Using the new load permutation to generate vector permute statements
+	 from a list of loads in DR_CHAIN.  */
+      if (slp && slp_perm && is_a <bb_vec_info> (vinfo)
+	  && STMT_VINFO_GROUPED_ACCESS (stmt_info)
+	  && DR_GROUP_SLP_TRANSPOSE (DR_GROUP_FIRST_ELEMENT (stmt_info)))
+	{
+	  unsigned n_perms;
+	  stmt_vec_info stmt_info_ = SLP_TREE_SCALAR_STMTS (slp_node)[0];
+	  unsigned int old_size = DR_GROUP_SIZE (stmt_info);
+	  DR_GROUP_SIZE (stmt_info_) = new_group_size;
+	  vec<unsigned> old_load_permutation
+			  = SLP_TREE_LOAD_PERMUTATION (slp_node);
+	  SLP_TREE_LOAD_PERMUTATION (slp_node) = new_load_permutation;
+	  bool perm_load_success = vect_transform_slp_perm_load (
+				     slp_node, dr_chain, gsi, vf,
+				     slp_node_instance, false, &n_perms);
+	  DR_GROUP_SIZE (stmt_info_) = old_size;
+	  SLP_TREE_LOAD_PERMUTATION (slp_node) = old_load_permutation;
+	  new_load_permutation.release ();
+	  if (!perm_load_success)
+	    {
+	      dr_chain.release ();
+	      return false;
+	    }
+	}
+      else if (slp_perm)
         {
 	  unsigned n_perms;
           if (!vect_transform_slp_perm_load (slp_node, dr_chain, gsi, vf,
diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
index f7becb34a..1c4a6c421 100644
--- a/gcc/tree-vectorizer.h
+++ b/gcc/tree-vectorizer.h
@@ -297,6 +297,21 @@ public:
   vec<ddr_p> ddrs;
 };
 
+/* Information about offset in vectorizable_load.  */
+struct offset_info {
+  tree offset;
+  tree byte_offset;
+  tree dataref_offset;
+};
+
+/* Information about vectype in vectorizable_load.  */
+struct vectype_info {
+  tree vectype;
+  tree ltype;
+  tree lvectype;
+  tree ref_type;
+};
+
 /* Vectorizer state common between loop and basic-block vectorization.  */
 class vec_info {
 public:
@@ -335,6 +350,14 @@ public:
      stmt in the chain.  */
   auto_vec<stmt_vec_info> grouped_stores;
 
+  /* All interleaving chains of loads, represented by the first
+     stmt in the chain.  */
+  auto_vec<stmt_vec_info> grouped_loads;
+
+  /* All interleaving chains of stores (before transposed), represented by all
+     stmt in the chain.  */
+  auto_vec<vec<stmt_vec_info> > scalar_stores;
+
   /* Cost data used by the target cost model.  */
   void *target_cost_data;
 
@@ -702,6 +725,8 @@ public:
 #define LOOP_VINFO_CHECK_NONZERO(L)        (L)->check_nonzero
 #define LOOP_VINFO_LOWER_BOUNDS(L)         (L)->lower_bounds
 #define LOOP_VINFO_GROUPED_STORES(L)       (L)->grouped_stores
+#define LOOP_VINFO_GROUPED_LOADS(L)	    (L)->grouped_loads
+#define LOOP_VINFO_SCALAR_STORES(L)	    (L)->scalar_stores
 #define LOOP_VINFO_SLP_INSTANCES(L)        (L)->slp_instances
 #define LOOP_VINFO_SLP_UNROLLING_FACTOR(L) (L)->slp_unrolling_factor
 #define LOOP_VINFO_REDUCTIONS(L)           (L)->reductions
@@ -764,6 +789,25 @@ public:
   basic_block bb;
   gimple_stmt_iterator region_begin;
   gimple_stmt_iterator region_end;
+
+  /* True, if bb_vinfo can goto vect_analyze_slp.  */
+  bool before_slp;
+
+  /* True, if bb_vinfo is a transposed version.  */
+  bool transposed;
+
+  /* The number of transposed groups.  */
+  int transposed_group;
+
+  /* The cost of the scalar iterations.  */
+  int scalar_cost;
+
+  /* The cost of the vector prologue and epilogue, including peeled
+     iterations and set-up code.  */
+  int vec_outside_cost;
+
+  /* The cost of the vector loop body.  */
+  int vec_inside_cost;
 } *bb_vec_info;
 
 #define BB_VINFO_BB(B)               (B)->bb
@@ -772,6 +816,14 @@ public:
 #define BB_VINFO_DATAREFS(B)         (B)->shared->datarefs
 #define BB_VINFO_DDRS(B)             (B)->shared->ddrs
 #define BB_VINFO_TARGET_COST_DATA(B) (B)->target_cost_data
+#define BB_VINFO_GROUPED_LOADS(B)    (B)->grouped_loads
+#define BB_VINFO_SCALAR_STORES(B)    (B)->scalar_stores
+#define BB_VINFO_VEC_OUTSIDE_COST(B) (B)->vec_outside_cost
+#define BB_VINFO_VEC_INSIDE_COST(B)  (B)->vec_inside_cost
+#define BB_VINFO_SCALAR_COST(B)      (B)->scalar_cost
+#define BB_VINFO_SLP_TRANSPOSED(B)   (B)->transposed
+#define BB_VINFO_BEFORE_SLP(B)       (B)->before_slp
+#define BB_VINFO_TRANS_GROUPS(B)     (B)->transposed_group
 
 static inline bb_vec_info
 vec_info_for_bb (basic_block bb)
@@ -1012,6 +1064,17 @@ public:
   stmt_vec_info next_element;
   /* The size of the group.  */
   unsigned int size;
+
+  /* The size of the group before transposed.  */
+  unsigned int size_before_transpose;
+
+  /* If true, the stmt_info is slp transposed.  */
+  bool slp_transpose;
+
+  /* Mark the group store number for rebuild interleaving chain
+     during transpose phase.  Value -1 represents unable to transpose.  */
+  int group_number;
+
   /* For stores, number of stores from this group seen. We vectorize the last
      one.  */
   unsigned int store_count;
@@ -1019,6 +1082,9 @@ public:
      is 1.  */
   unsigned int gap;
 
+  /* The gap before transposed.  */
+  unsigned int gap_before_transpose;
+
   /* The minimum negative dependence distance this stmt participates in
      or zero if none.  */
   unsigned int min_neg_dist;
@@ -1217,6 +1283,12 @@ STMT_VINFO_BB_VINFO (stmt_vec_info stmt_vinfo)
 #define STMT_VINFO_REDUC_VECTYPE_IN(S)  (S)->reduc_vectype_in
 #define STMT_VINFO_SLP_VECT_ONLY(S)     (S)->slp_vect_only_p
 
+#define DR_GROUP_SLP_TRANSPOSE(S) \
+  (gcc_checking_assert ((S)->dr_aux.dr), (S)->slp_transpose)
+#define DR_GROUP_SIZE_TRANS(S) \
+  (gcc_checking_assert ((S)->dr_aux.dr), (S)->size_before_transpose)
+#define DR_GROUP_NUMBER(S) \
+  (gcc_checking_assert ((S)->dr_aux.dr), (S)->group_number)
 #define DR_GROUP_FIRST_ELEMENT(S) \
   (gcc_checking_assert ((S)->dr_aux.dr), (S)->first_element)
 #define DR_GROUP_NEXT_ELEMENT(S) \
@@ -1227,6 +1299,8 @@ STMT_VINFO_BB_VINFO (stmt_vec_info stmt_vinfo)
   (gcc_checking_assert ((S)->dr_aux.dr), (S)->store_count)
 #define DR_GROUP_GAP(S) \
   (gcc_checking_assert ((S)->dr_aux.dr), (S)->gap)
+#define DR_GROUP_GAP_TRANS(S) \
+  (gcc_checking_assert ((S)->dr_aux.dr), (S)->gap_before_transpose)
 
 #define REDUC_GROUP_FIRST_ELEMENT(S) \
   (gcc_checking_assert (!(S)->dr_aux.dr), (S)->first_element)
@@ -1624,6 +1698,17 @@ vect_get_scalar_dr_size (dr_vec_info *dr_info)
   return tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr_info->dr))));
 }
 
+/* Compare two unsigned int A and B.
+   Sorting them in ascending order.  */
+
+static inline int
+cmp_for_group_num (const void *a_, const void *b_)
+{
+  unsigned int a = *(unsigned int *)const_cast<void *>(a_);
+  unsigned int b = *(unsigned int *)const_cast<void *>(b_);
+  return a < b ? -1 : 1;
+}
+
 /* Return true if LOOP_VINFO requires a runtime check for whether the
    vector loop is profitable.  */
 
@@ -1787,6 +1872,9 @@ extern bool vect_grouped_load_supported (tree, bool, unsigned HOST_WIDE_INT);
 extern bool vect_load_lanes_supported (tree, unsigned HOST_WIDE_INT, bool);
 extern void vect_permute_store_chain (vec<tree> ,unsigned int, stmt_vec_info,
                                     gimple_stmt_iterator *, vec<tree> *);
+extern void vect_transpose_store_chain (vec<tree>, unsigned int, unsigned int,
+					stmt_vec_info, gimple_stmt_iterator *,
+					vec<tree> *);
 extern tree vect_setup_realignment (stmt_vec_info, gimple_stmt_iterator *,
 				    tree *, enum dr_alignment_support, tree,
 	                            class loop **);
@@ -1849,6 +1937,7 @@ extern void vect_free_slp_instance (slp_instance, bool);
 extern bool vect_transform_slp_perm_load (slp_tree, vec<tree> ,
 					  gimple_stmt_iterator *, poly_uint64,
 					  slp_instance, bool, unsigned *);
+extern void vect_transform_back_slp_grouped_stores (bb_vec_info, stmt_vec_info);
 extern bool vect_slp_analyze_operations (vec_info *);
 extern void vect_schedule_slp (vec_info *);
 extern opt_result vect_analyze_slp (vec_info *, unsigned);
-- 
2.27.0.windows.1