xref: /llvm-project/llvm/test/CodeGen/RISCV/loop-strength-reduce-loop-invar.ll (revision 9122c5235ec85ce0c0ad337e862b006e7b349d84)
1923831ebSPhilip Reames; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2923831ebSPhilip Reames; RUN: llc < %s -mtriple=riscv32 -verify-machineinstrs | FileCheck %s -check-prefixes=RV32
3923831ebSPhilip Reames; RUN: llc < %s -mtriple=riscv64 -verify-machineinstrs | FileCheck %s -check-prefixes=RV64
4923831ebSPhilip Reames
5923831ebSPhilip Reames; Test case:
6923831ebSPhilip Reames; - `A[row]` is loop invariant and should be hoisted up to preheader
7923831ebSPhilip Reames; FIXME: RV32 is working as expected, but RV64 doesn't
8923831ebSPhilip Reames
9923831ebSPhilip Reames; The following LLVM IR simulates:
10923831ebSPhilip Reames; int A[16][16];
11923831ebSPhilip Reames; void test(int row, int N) {
12923831ebSPhilip Reames; 	for (int i=0; i<N; ++I) {
13923831ebSPhilip Reames; 		A[row][i+1] = 4;
14923831ebSPhilip Reames; 		A[row][i+2] = 5;
15923831ebSPhilip Reames; 	}
16923831ebSPhilip Reames; }
17923831ebSPhilip Reames
18923831ebSPhilip Reames; After LSR:
19923831ebSPhilip Reames; int A[16][16];
20923831ebSPhilip Reames; void test(int row, int N) {
21923831ebSPhilip Reames; 	for (int *ptr = A[row][2]; N>0; N--) {
22923831ebSPhilip Reames; 		*(ptr-1) = 4;
23923831ebSPhilip Reames; 		*(ptr) = 5;
24923831ebSPhilip Reames; 		++ptr;
25923831ebSPhilip Reames; 	}
26923831ebSPhilip Reames; }
27923831ebSPhilip Reames
288e451cdaSNikita Popov@A = internal global [16 x [16 x i32]] zeroinitializer, align 32		; <ptr> [#uses=2]
29923831ebSPhilip Reames
30923831ebSPhilip Reamesdefine void @test(i32 signext %row, i32 signext %N.in) nounwind {
31923831ebSPhilip Reames; RV32-LABEL: test:
32923831ebSPhilip Reames; RV32:       # %bb.0: # %entry
33923831ebSPhilip Reames; RV32-NEXT:    blez a1, .LBB0_3
34923831ebSPhilip Reames; RV32-NEXT:  # %bb.1: # %cond_true.preheader
35*9122c523SPengcheng Wang; RV32-NEXT:    slli a2, a0, 6
36*9122c523SPengcheng Wang; RV32-NEXT:    lui a3, %hi(A)
37*9122c523SPengcheng Wang; RV32-NEXT:    addi a3, a3, %lo(A)
38*9122c523SPengcheng Wang; RV32-NEXT:    li a0, 4
39*9122c523SPengcheng Wang; RV32-NEXT:    add a2, a2, a3
40*9122c523SPengcheng Wang; RV32-NEXT:    addi a2, a2, 8
41923831ebSPhilip Reames; RV32-NEXT:    li a3, 5
42923831ebSPhilip Reames; RV32-NEXT:  .LBB0_2: # %cond_true
43923831ebSPhilip Reames; RV32-NEXT:    # =>This Inner Loop Header: Depth=1
44*9122c523SPengcheng Wang; RV32-NEXT:    sw a0, -4(a2)
45*9122c523SPengcheng Wang; RV32-NEXT:    sw a3, 0(a2)
46923831ebSPhilip Reames; RV32-NEXT:    addi a1, a1, -1
47*9122c523SPengcheng Wang; RV32-NEXT:    addi a2, a2, 4
48923831ebSPhilip Reames; RV32-NEXT:    bnez a1, .LBB0_2
49923831ebSPhilip Reames; RV32-NEXT:  .LBB0_3: # %return
50923831ebSPhilip Reames; RV32-NEXT:    ret
51923831ebSPhilip Reames;
52923831ebSPhilip Reames; RV64-LABEL: test:
53923831ebSPhilip Reames; RV64:       # %bb.0: # %entry
54923831ebSPhilip Reames; RV64-NEXT:    blez a1, .LBB0_3
55923831ebSPhilip Reames; RV64-NEXT:  # %bb.1: # %cond_true.preheader
56*9122c523SPengcheng Wang; RV64-NEXT:    slli a3, a0, 6
57*9122c523SPengcheng Wang; RV64-NEXT:    lui a4, %hi(A)
58*9122c523SPengcheng Wang; RV64-NEXT:    addi a4, a4, %lo(A)
5925b65be4SPhilip Reames; RV64-NEXT:    addiw a1, a1, 2
60*9122c523SPengcheng Wang; RV64-NEXT:    li a0, 2
61*9122c523SPengcheng Wang; RV64-NEXT:    li a2, 4
62*9122c523SPengcheng Wang; RV64-NEXT:    add a3, a3, a4
63*9122c523SPengcheng Wang; RV64-NEXT:    addi a4, a3, 4
64974e2e69SCraig Topper; RV64-NEXT:    li a5, 5
65923831ebSPhilip Reames; RV64-NEXT:  .LBB0_2: # %cond_true
66923831ebSPhilip Reames; RV64-NEXT:    # =>This Inner Loop Header: Depth=1
67*9122c523SPengcheng Wang; RV64-NEXT:    sw a2, 0(a4)
68*9122c523SPengcheng Wang; RV64-NEXT:    slli a6, a0, 2
69*9122c523SPengcheng Wang; RV64-NEXT:    addiw a0, a0, 1
70*9122c523SPengcheng Wang; RV64-NEXT:    add a6, a3, a6
7125b65be4SPhilip Reames; RV64-NEXT:    sw a5, 0(a6)
72*9122c523SPengcheng Wang; RV64-NEXT:    addi a4, a4, 4
73*9122c523SPengcheng Wang; RV64-NEXT:    bne a0, a1, .LBB0_2
74923831ebSPhilip Reames; RV64-NEXT:  .LBB0_3: # %return
75923831ebSPhilip Reames; RV64-NEXT:    ret
76923831ebSPhilip Reamesentry:
77923831ebSPhilip Reames	%N = bitcast i32 %N.in to i32
78923831ebSPhilip Reames	%tmp5 = icmp sgt i32 %N.in, 0
79923831ebSPhilip Reames	br i1 %tmp5, label %cond_true, label %return
80923831ebSPhilip Reames
81923831ebSPhilip Reamescond_true:
82923831ebSPhilip Reames	%indvar = phi i32 [ 0, %entry ], [ %indvar.next, %cond_true ]
83923831ebSPhilip Reames	%tmp2 = add i32 %indvar, 1
848e451cdaSNikita Popov	%tmp = getelementptr [16 x [16 x i32]], ptr @A, i32 0, i32 %row, i32 %tmp2
858e451cdaSNikita Popov	store i32 4, ptr %tmp
86923831ebSPhilip Reames	%tmp5.upgrd.1 = add i32 %indvar, 2
878e451cdaSNikita Popov	%tmp7 = getelementptr [16 x [16 x i32]], ptr @A, i32 0, i32 %row, i32 %tmp5.upgrd.1
888e451cdaSNikita Popov	store i32 5, ptr %tmp7
89923831ebSPhilip Reames	%indvar.next = add i32 %indvar, 1
90923831ebSPhilip Reames	%exitcond = icmp eq i32 %indvar.next, %N
91923831ebSPhilip Reames	br i1 %exitcond, label %return, label %cond_true
92923831ebSPhilip Reames
93923831ebSPhilip Reamesreturn:
94923831ebSPhilip Reames	ret void
95923831ebSPhilip Reames}
96