array_element_access.carbon 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // AUTOUPDATE
  6. // TIP: To test this file alone, run:
  7. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/lower/testdata/index/array_element_access.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/lower/testdata/index/array_element_access.carbon
  10. fn A() -> (i32, i32) { return (1, 2); }
  11. fn B() -> [i32; 2] { return (1, 2); }
  12. fn Run() {
  13. var a: [i32; 2] = A();
  14. var b: i32 = 1;
  15. var c: i32 = a[b];
  16. var d: i32 = B()[1];
  17. }
  18. // CHECK:STDOUT: ; ModuleID = 'array_element_access.carbon'
  19. // CHECK:STDOUT: source_filename = "array_element_access.carbon"
  20. // CHECK:STDOUT:
  21. // CHECK:STDOUT: @tuple.loc10_37 = internal constant { i32, i32 } { i32 1, i32 2 }
  22. // CHECK:STDOUT: @array.loc12_35 = internal constant [2 x i32] [i32 1, i32 2]
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: define void @_CA.Main(ptr sret({ i32, i32 }) %return) !dbg !4 {
  25. // CHECK:STDOUT: entry:
  26. // CHECK:STDOUT: %.loc10_36.2.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 0, !dbg !7
  27. // CHECK:STDOUT: %.loc10_36.4.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %return, i32 0, i32 1, !dbg !7
  28. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %return, ptr align 4 @tuple.loc10_37, i64 8, i1 false), !dbg !8
  29. // CHECK:STDOUT: ret void, !dbg !8
  30. // CHECK:STDOUT: }
  31. // CHECK:STDOUT:
  32. // CHECK:STDOUT: define void @_CB.Main(ptr sret([2 x i32]) %return) !dbg !9 {
  33. // CHECK:STDOUT: entry:
  34. // CHECK:STDOUT: %.loc12_34.3.array.index = getelementptr inbounds [2 x i32], ptr %return, i32 0, i32 0, !dbg !10
  35. // CHECK:STDOUT: %.loc12_34.6.array.index = getelementptr inbounds [2 x i32], ptr %return, i32 0, i32 1, !dbg !10
  36. // CHECK:STDOUT: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %return, ptr align 4 @array.loc12_35, i64 8, i1 false), !dbg !11
  37. // CHECK:STDOUT: ret void, !dbg !11
  38. // CHECK:STDOUT: }
  39. // CHECK:STDOUT:
  40. // CHECK:STDOUT: define void @main() !dbg !12 {
  41. // CHECK:STDOUT: entry:
  42. // CHECK:STDOUT: %a.var = alloca [2 x i32], align 4, !dbg !13
  43. // CHECK:STDOUT: %.loc15_22.1.temp = alloca { i32, i32 }, align 8, !dbg !14
  44. // CHECK:STDOUT: call void @_CA.Main(ptr %.loc15_22.1.temp), !dbg !14
  45. // CHECK:STDOUT: %.loc15_22.3.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %.loc15_22.1.temp, i32 0, i32 0, !dbg !14
  46. // CHECK:STDOUT: %.loc15_22.4 = load i32, ptr %.loc15_22.3.tuple.elem, align 4, !dbg !14
  47. // CHECK:STDOUT: %.loc15_22.6.array.index = getelementptr inbounds [2 x i32], ptr %a.var, i32 0, i32 0, !dbg !14
  48. // CHECK:STDOUT: store i32 %.loc15_22.4, ptr %.loc15_22.6.array.index, align 4, !dbg !14
  49. // CHECK:STDOUT: %.loc15_22.8.tuple.elem = getelementptr inbounds nuw { i32, i32 }, ptr %.loc15_22.1.temp, i32 0, i32 1, !dbg !14
  50. // CHECK:STDOUT: %.loc15_22.9 = load i32, ptr %.loc15_22.8.tuple.elem, align 4, !dbg !14
  51. // CHECK:STDOUT: %.loc15_22.11.array.index = getelementptr inbounds [2 x i32], ptr %a.var, i32 0, i32 1, !dbg !14
  52. // CHECK:STDOUT: store i32 %.loc15_22.9, ptr %.loc15_22.11.array.index, align 4, !dbg !14
  53. // CHECK:STDOUT: %b.var = alloca i32, align 4, !dbg !15
  54. // CHECK:STDOUT: store i32 1, ptr %b.var, align 4, !dbg !16
  55. // CHECK:STDOUT: %c.var = alloca i32, align 4, !dbg !17
  56. // CHECK:STDOUT: %.loc17_18 = load i32, ptr %b.var, align 4, !dbg !18
  57. // CHECK:STDOUT: %.loc17_19.1.array.index = getelementptr inbounds [2 x i32], ptr %a.var, i32 0, i32 %.loc17_18, !dbg !19
  58. // CHECK:STDOUT: %.loc17_19.2 = load i32, ptr %.loc17_19.1.array.index, align 4, !dbg !19
  59. // CHECK:STDOUT: store i32 %.loc17_19.2, ptr %c.var, align 4, !dbg !20
  60. // CHECK:STDOUT: %d.var = alloca i32, align 4, !dbg !21
  61. // CHECK:STDOUT: %.loc18_17.1.temp = alloca [2 x i32], align 4, !dbg !22
  62. // CHECK:STDOUT: call void @_CB.Main(ptr %.loc18_17.1.temp), !dbg !22
  63. // CHECK:STDOUT: %.loc18_21.1.array.index = getelementptr inbounds [2 x i32], ptr %.loc18_17.1.temp, i32 0, i32 1, !dbg !22
  64. // CHECK:STDOUT: %.loc18_21.2 = load i32, ptr %.loc18_21.1.array.index, align 4, !dbg !22
  65. // CHECK:STDOUT: store i32 %.loc18_21.2, ptr %d.var, align 4, !dbg !23
  66. // CHECK:STDOUT: ret void, !dbg !24
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: ; Function Attrs: nocallback nofree nounwind willreturn memory(argmem: readwrite)
  70. // CHECK:STDOUT: declare void @llvm.memcpy.p0.p0.i64(ptr noalias nocapture writeonly, ptr noalias nocapture readonly, i64, i1 immarg) #0
  71. // CHECK:STDOUT:
  72. // CHECK:STDOUT: ; uselistorder directives
  73. // CHECK:STDOUT: uselistorder ptr @llvm.memcpy.p0.p0.i64, { 1, 0 }
  74. // CHECK:STDOUT:
  75. // CHECK:STDOUT: attributes #0 = { nocallback nofree nounwind willreturn memory(argmem: readwrite) }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: !llvm.module.flags = !{!0, !1}
  78. // CHECK:STDOUT: !llvm.dbg.cu = !{!2}
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: !0 = !{i32 7, !"Dwarf Version", i32 5}
  81. // CHECK:STDOUT: !1 = !{i32 2, !"Debug Info Version", i32 3}
  82. // CHECK:STDOUT: !2 = distinct !DICompileUnit(language: DW_LANG_C, file: !3, producer: "carbon", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug)
  83. // CHECK:STDOUT: !3 = !DIFile(filename: "array_element_access.carbon", directory: "")
  84. // CHECK:STDOUT: !4 = distinct !DISubprogram(name: "A", linkageName: "_CA.Main", scope: null, file: !3, line: 10, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  85. // CHECK:STDOUT: !5 = !DISubroutineType(types: !6)
  86. // CHECK:STDOUT: !6 = !{}
  87. // CHECK:STDOUT: !7 = !DILocation(line: 10, column: 31, scope: !4)
  88. // CHECK:STDOUT: !8 = !DILocation(line: 10, column: 24, scope: !4)
  89. // CHECK:STDOUT: !9 = distinct !DISubprogram(name: "B", linkageName: "_CB.Main", scope: null, file: !3, line: 12, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  90. // CHECK:STDOUT: !10 = !DILocation(line: 12, column: 29, scope: !9)
  91. // CHECK:STDOUT: !11 = !DILocation(line: 12, column: 22, scope: !9)
  92. // CHECK:STDOUT: !12 = distinct !DISubprogram(name: "Run", linkageName: "main", scope: null, file: !3, line: 14, type: !5, spFlags: DISPFlagDefinition, unit: !2)
  93. // CHECK:STDOUT: !13 = !DILocation(line: 15, column: 7, scope: !12)
  94. // CHECK:STDOUT: !14 = !DILocation(line: 15, column: 21, scope: !12)
  95. // CHECK:STDOUT: !15 = !DILocation(line: 16, column: 7, scope: !12)
  96. // CHECK:STDOUT: !16 = !DILocation(line: 16, column: 3, scope: !12)
  97. // CHECK:STDOUT: !17 = !DILocation(line: 17, column: 7, scope: !12)
  98. // CHECK:STDOUT: !18 = !DILocation(line: 17, column: 18, scope: !12)
  99. // CHECK:STDOUT: !19 = !DILocation(line: 17, column: 16, scope: !12)
  100. // CHECK:STDOUT: !20 = !DILocation(line: 17, column: 3, scope: !12)
  101. // CHECK:STDOUT: !21 = !DILocation(line: 18, column: 7, scope: !12)
  102. // CHECK:STDOUT: !22 = !DILocation(line: 18, column: 16, scope: !12)
  103. // CHECK:STDOUT: !23 = !DILocation(line: 18, column: 3, scope: !12)
  104. // CHECK:STDOUT: !24 = !DILocation(line: 14, column: 1, scope: !12)