11c466477SZhaoshi Zheng; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
21c466477SZhaoshi Zheng; RUN: llc -mtriple=riscv32 -mattr=+reserve-x18 -verify-machineinstrs < %s \
31c466477SZhaoshi Zheng; RUN:   | FileCheck %s --check-prefix=RV32
41c466477SZhaoshi Zheng; RUN: llc -mtriple=riscv64 -mattr=+reserve-x18 -verify-machineinstrs < %s \
51c466477SZhaoshi Zheng; RUN:   | FileCheck %s --check-prefix=RV64
61c466477SZhaoshi Zheng
71c466477SZhaoshi Zhengdefine void @f1() shadowcallstack {
81c466477SZhaoshi Zheng; RV32-LABEL: f1:
91c466477SZhaoshi Zheng; RV32:       # %bb.0:
101c466477SZhaoshi Zheng; RV32-NEXT:    ret
111c466477SZhaoshi Zheng;
121c466477SZhaoshi Zheng; RV64-LABEL: f1:
131c466477SZhaoshi Zheng; RV64:       # %bb.0:
141c466477SZhaoshi Zheng; RV64-NEXT:    ret
151c466477SZhaoshi Zheng  ret void
161c466477SZhaoshi Zheng}
171c466477SZhaoshi Zheng
181c466477SZhaoshi Zhengdeclare void @foo()
191c466477SZhaoshi Zheng
201c466477SZhaoshi Zhengdefine void @f2() shadowcallstack {
211c466477SZhaoshi Zheng; RV32-LABEL: f2:
221c466477SZhaoshi Zheng; RV32:       # %bb.0:
23e28b6a60SMichael Munday; RV32-NEXT:    tail foo@plt
241c466477SZhaoshi Zheng;
251c466477SZhaoshi Zheng; RV64-LABEL: f2:
261c466477SZhaoshi Zheng; RV64:       # %bb.0:
27e28b6a60SMichael Munday; RV64-NEXT:    tail foo@plt
281c466477SZhaoshi Zheng  tail call void @foo()
291c466477SZhaoshi Zheng  ret void
301c466477SZhaoshi Zheng}
311c466477SZhaoshi Zheng
321c466477SZhaoshi Zhengdeclare i32 @bar()
331c466477SZhaoshi Zheng
341c466477SZhaoshi Zhengdefine i32 @f3() shadowcallstack {
351c466477SZhaoshi Zheng; RV32-LABEL: f3:
361c466477SZhaoshi Zheng; RV32:       # %bb.0:
371c466477SZhaoshi Zheng; RV32-NEXT:    sw ra, 0(s2)
381c466477SZhaoshi Zheng; RV32-NEXT:    addi s2, s2, 4
391c466477SZhaoshi Zheng; RV32-NEXT:    addi sp, sp, -16
401c466477SZhaoshi Zheng; RV32-NEXT:    .cfi_def_cfa_offset 16
41e28b6a60SMichael Munday; RV32-NEXT:    sw ra, 12(sp) # 4-byte Folded Spill
421c466477SZhaoshi Zheng; RV32-NEXT:    .cfi_offset ra, -4
43e28b6a60SMichael Munday; RV32-NEXT:    call bar@plt
44e28b6a60SMichael Munday; RV32-NEXT:    lw ra, 12(sp) # 4-byte Folded Reload
451c466477SZhaoshi Zheng; RV32-NEXT:    addi sp, sp, 16
461c466477SZhaoshi Zheng; RV32-NEXT:    lw ra, -4(s2)
471c466477SZhaoshi Zheng; RV32-NEXT:    addi s2, s2, -4
481c466477SZhaoshi Zheng; RV32-NEXT:    ret
491c466477SZhaoshi Zheng;
501c466477SZhaoshi Zheng; RV64-LABEL: f3:
511c466477SZhaoshi Zheng; RV64:       # %bb.0:
521c466477SZhaoshi Zheng; RV64-NEXT:    sd ra, 0(s2)
531c466477SZhaoshi Zheng; RV64-NEXT:    addi s2, s2, 8
541c466477SZhaoshi Zheng; RV64-NEXT:    addi sp, sp, -16
551c466477SZhaoshi Zheng; RV64-NEXT:    .cfi_def_cfa_offset 16
56e28b6a60SMichael Munday; RV64-NEXT:    sd ra, 8(sp) # 8-byte Folded Spill
571c466477SZhaoshi Zheng; RV64-NEXT:    .cfi_offset ra, -8
58e28b6a60SMichael Munday; RV64-NEXT:    call bar@plt
59e28b6a60SMichael Munday; RV64-NEXT:    ld ra, 8(sp) # 8-byte Folded Reload
601c466477SZhaoshi Zheng; RV64-NEXT:    addi sp, sp, 16
611c466477SZhaoshi Zheng; RV64-NEXT:    ld ra, -8(s2)
621c466477SZhaoshi Zheng; RV64-NEXT:    addi s2, s2, -8
631c466477SZhaoshi Zheng; RV64-NEXT:    ret
641c466477SZhaoshi Zheng  %res = call i32 @bar()
651c466477SZhaoshi Zheng  %res1 = add i32 %res, 1
661c466477SZhaoshi Zheng  ret i32 %res
671c466477SZhaoshi Zheng}
681c466477SZhaoshi Zheng
691c466477SZhaoshi Zhengdefine i32 @f4() shadowcallstack {
701c466477SZhaoshi Zheng; RV32-LABEL: f4:
711c466477SZhaoshi Zheng; RV32:       # %bb.0:
721c466477SZhaoshi Zheng; RV32-NEXT:    sw ra, 0(s2)
731c466477SZhaoshi Zheng; RV32-NEXT:    addi s2, s2, 4
741c466477SZhaoshi Zheng; RV32-NEXT:    addi sp, sp, -16
751c466477SZhaoshi Zheng; RV32-NEXT:    .cfi_def_cfa_offset 16
76e28b6a60SMichael Munday; RV32-NEXT:    sw ra, 12(sp) # 4-byte Folded Spill
77e28b6a60SMichael Munday; RV32-NEXT:    sw s0, 8(sp) # 4-byte Folded Spill
78e28b6a60SMichael Munday; RV32-NEXT:    sw s1, 4(sp) # 4-byte Folded Spill
79e28b6a60SMichael Munday; RV32-NEXT:    sw s3, 0(sp) # 4-byte Folded Spill
801c466477SZhaoshi Zheng; RV32-NEXT:    .cfi_offset ra, -4
811c466477SZhaoshi Zheng; RV32-NEXT:    .cfi_offset s0, -8
821c466477SZhaoshi Zheng; RV32-NEXT:    .cfi_offset s1, -12
831c466477SZhaoshi Zheng; RV32-NEXT:    .cfi_offset s3, -16
84e28b6a60SMichael Munday; RV32-NEXT:    call bar@plt
85*8def89b5Swangpc; RV32-NEXT:    mv s0, a0
86e28b6a60SMichael Munday; RV32-NEXT:    call bar@plt
871c466477SZhaoshi Zheng; RV32-NEXT:    mv s1, a0
88e28b6a60SMichael Munday; RV32-NEXT:    call bar@plt
89*8def89b5Swangpc; RV32-NEXT:    mv s3, a0
90e28b6a60SMichael Munday; RV32-NEXT:    call bar@plt
91*8def89b5Swangpc; RV32-NEXT:    add a1, s0, s1
92*8def89b5Swangpc; RV32-NEXT:    add a0, s3, a0
931c466477SZhaoshi Zheng; RV32-NEXT:    add a0, a1, a0
94e28b6a60SMichael Munday; RV32-NEXT:    lw ra, 12(sp) # 4-byte Folded Reload
95137d3474SHsiangkai Wang; RV32-NEXT:    lw s0, 8(sp) # 4-byte Folded Reload
96137d3474SHsiangkai Wang; RV32-NEXT:    lw s1, 4(sp) # 4-byte Folded Reload
97137d3474SHsiangkai Wang; RV32-NEXT:    lw s3, 0(sp) # 4-byte Folded Reload
981c466477SZhaoshi Zheng; RV32-NEXT:    addi sp, sp, 16
991c466477SZhaoshi Zheng; RV32-NEXT:    lw ra, -4(s2)
1001c466477SZhaoshi Zheng; RV32-NEXT:    addi s2, s2, -4
1011c466477SZhaoshi Zheng; RV32-NEXT:    ret
1021c466477SZhaoshi Zheng;
1031c466477SZhaoshi Zheng; RV64-LABEL: f4:
1041c466477SZhaoshi Zheng; RV64:       # %bb.0:
1051c466477SZhaoshi Zheng; RV64-NEXT:    sd ra, 0(s2)
1061c466477SZhaoshi Zheng; RV64-NEXT:    addi s2, s2, 8
1071c466477SZhaoshi Zheng; RV64-NEXT:    addi sp, sp, -32
1081c466477SZhaoshi Zheng; RV64-NEXT:    .cfi_def_cfa_offset 32
109e28b6a60SMichael Munday; RV64-NEXT:    sd ra, 24(sp) # 8-byte Folded Spill
110e28b6a60SMichael Munday; RV64-NEXT:    sd s0, 16(sp) # 8-byte Folded Spill
111e28b6a60SMichael Munday; RV64-NEXT:    sd s1, 8(sp) # 8-byte Folded Spill
112e28b6a60SMichael Munday; RV64-NEXT:    sd s3, 0(sp) # 8-byte Folded Spill
1131c466477SZhaoshi Zheng; RV64-NEXT:    .cfi_offset ra, -8
1141c466477SZhaoshi Zheng; RV64-NEXT:    .cfi_offset s0, -16
1151c466477SZhaoshi Zheng; RV64-NEXT:    .cfi_offset s1, -24
1161c466477SZhaoshi Zheng; RV64-NEXT:    .cfi_offset s3, -32
117e28b6a60SMichael Munday; RV64-NEXT:    call bar@plt
118*8def89b5Swangpc; RV64-NEXT:    mv s0, a0
119e28b6a60SMichael Munday; RV64-NEXT:    call bar@plt
1201c466477SZhaoshi Zheng; RV64-NEXT:    mv s1, a0
121e28b6a60SMichael Munday; RV64-NEXT:    call bar@plt
122*8def89b5Swangpc; RV64-NEXT:    mv s3, a0
123e28b6a60SMichael Munday; RV64-NEXT:    call bar@plt
124*8def89b5Swangpc; RV64-NEXT:    addw a1, s0, s1
125*8def89b5Swangpc; RV64-NEXT:    addw a0, s3, a0
1261c466477SZhaoshi Zheng; RV64-NEXT:    addw a0, a1, a0
127e28b6a60SMichael Munday; RV64-NEXT:    ld ra, 24(sp) # 8-byte Folded Reload
128137d3474SHsiangkai Wang; RV64-NEXT:    ld s0, 16(sp) # 8-byte Folded Reload
129137d3474SHsiangkai Wang; RV64-NEXT:    ld s1, 8(sp) # 8-byte Folded Reload
130137d3474SHsiangkai Wang; RV64-NEXT:    ld s3, 0(sp) # 8-byte Folded Reload
1311c466477SZhaoshi Zheng; RV64-NEXT:    addi sp, sp, 32
1321c466477SZhaoshi Zheng; RV64-NEXT:    ld ra, -8(s2)
1331c466477SZhaoshi Zheng; RV64-NEXT:    addi s2, s2, -8
1341c466477SZhaoshi Zheng; RV64-NEXT:    ret
1351c466477SZhaoshi Zheng  %res1 = call i32 @bar()
1361c466477SZhaoshi Zheng  %res2 = call i32 @bar()
1371c466477SZhaoshi Zheng  %res3 = call i32 @bar()
1381c466477SZhaoshi Zheng  %res4 = call i32 @bar()
1391c466477SZhaoshi Zheng  %res12 = add i32 %res1, %res2
1401c466477SZhaoshi Zheng  %res34 = add i32 %res3, %res4
1411c466477SZhaoshi Zheng  %res1234 = add i32 %res12, %res34
1421c466477SZhaoshi Zheng  ret i32 %res1234
1431c466477SZhaoshi Zheng}
1441c466477SZhaoshi Zheng
1451c466477SZhaoshi Zhengdefine i32 @f5() shadowcallstack nounwind {
1461c466477SZhaoshi Zheng; RV32-LABEL: f5:
1471c466477SZhaoshi Zheng; RV32:       # %bb.0:
1481c466477SZhaoshi Zheng; RV32-NEXT:    sw ra, 0(s2)
1491c466477SZhaoshi Zheng; RV32-NEXT:    addi s2, s2, 4
1501c466477SZhaoshi Zheng; RV32-NEXT:    addi sp, sp, -16
151e28b6a60SMichael Munday; RV32-NEXT:    sw ra, 12(sp) # 4-byte Folded Spill
152e28b6a60SMichael Munday; RV32-NEXT:    call bar@plt
153e28b6a60SMichael Munday; RV32-NEXT:    lw ra, 12(sp) # 4-byte Folded Reload
1541c466477SZhaoshi Zheng; RV32-NEXT:    addi sp, sp, 16
1551c466477SZhaoshi Zheng; RV32-NEXT:    lw ra, -4(s2)
1561c466477SZhaoshi Zheng; RV32-NEXT:    addi s2, s2, -4
1571c466477SZhaoshi Zheng; RV32-NEXT:    ret
1581c466477SZhaoshi Zheng;
1591c466477SZhaoshi Zheng; RV64-LABEL: f5:
1601c466477SZhaoshi Zheng; RV64:       # %bb.0:
1611c466477SZhaoshi Zheng; RV64-NEXT:    sd ra, 0(s2)
1621c466477SZhaoshi Zheng; RV64-NEXT:    addi s2, s2, 8
1631c466477SZhaoshi Zheng; RV64-NEXT:    addi sp, sp, -16
164e28b6a60SMichael Munday; RV64-NEXT:    sd ra, 8(sp) # 8-byte Folded Spill
165e28b6a60SMichael Munday; RV64-NEXT:    call bar@plt
166e28b6a60SMichael Munday; RV64-NEXT:    ld ra, 8(sp) # 8-byte Folded Reload
1671c466477SZhaoshi Zheng; RV64-NEXT:    addi sp, sp, 16
1681c466477SZhaoshi Zheng; RV64-NEXT:    ld ra, -8(s2)
1691c466477SZhaoshi Zheng; RV64-NEXT:    addi s2, s2, -8
1701c466477SZhaoshi Zheng; RV64-NEXT:    ret
1711c466477SZhaoshi Zheng  %res = call i32 @bar()
1721c466477SZhaoshi Zheng  %res1 = add i32 %res, 1
1731c466477SZhaoshi Zheng  ret i32 %res
1741c466477SZhaoshi Zheng}
175