xref: /llvm-project/flang/test/Analysis/AliasAnalysis/alias-analysis-4.fir (revision c3f060b026a1e7f4676038fffce5ee2bea1292ac)
1// check that hlfir.designate can be followed by alias analysis
2
3// use --mlir-disable-threading so that the AA queries are serialised
4// as well as its diagnostic output.
5// RUN: fir-opt %s --test-fir-alias-analysis -split-input-file --mlir-disable-threading 2>&1 | FileCheck %s
6
7// designate for a derived type component:
8// module m
9//   type t
10//     real :: array(42)
11//   end type t
12//   type (t) :: glbl
13// contains
14//   subroutine test(arg)
15//     real :: arg(42)
16//     glbl%array = arg
17//   end subroutine test
18// end module m
19
20// A global can't alias with a dummy argument
21// CHECK: arg#0 <-> glbl%array#0: NoAlias
22
23module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "aarch64-unknown-linux-gnu"} {
24  fir.global @_QMmEglbl : !fir.type<_QMmTt{array:!fir.array<42xf32>}> {
25    %0 = fir.undefined !fir.type<_QMmTt{array:!fir.array<42xf32>}>
26    fir.has_value %0 : !fir.type<_QMmTt{array:!fir.array<42xf32>}>
27  }
28  func.func @_QMmPtest(%arg0: !fir.ref<!fir.array<42xf32>> {fir.bindc_name = "arg"}) {
29    %c42 = arith.constant 42 : index
30    %0 = fir.address_of(@_QMmEglbl) : !fir.ref<!fir.type<_QMmTt{array:!fir.array<42xf32>}>>
31    %1:2 = hlfir.declare %0 {uniq_name = "_QMmEglbl"} : (!fir.ref<!fir.type<_QMmTt{array:!fir.array<42xf32>}>>) -> (!fir.ref<!fir.type<_QMmTt{array:!fir.array<42xf32>}>>, !fir.ref<!fir.type<_QMmTt{array:!fir.array<42xf32>}>>)
32    %2 = fir.shape %c42 : (index) -> !fir.shape<1>
33    %3:2 = hlfir.declare %arg0(%2) {uniq_name = "_QMmFtestEarg", test.ptr = "arg"} : (!fir.ref<!fir.array<42xf32>>, !fir.shape<1>) -> (!fir.ref<!fir.array<42xf32>>, !fir.ref<!fir.array<42xf32>>)
34    %4 = hlfir.designate %1#0{"array"} shape %2 {test.ptr = "glbl%array"} : (!fir.ref<!fir.type<_QMmTt{array:!fir.array<42xf32>}>>, !fir.shape<1>) -> !fir.ref<!fir.array<42xf32>>
35    hlfir.assign %3#0 to %4 : !fir.ref<!fir.array<42xf32>>, !fir.ref<!fir.array<42xf32>>
36    return
37  }
38}
39
40// -----
41
42// designate for an array element
43
44// two dummy arguments don't alias
45// CHECK: array0#0 <-> array1#0: NoAlias
46
47func.func @array_element(%arg0: !fir.ref<!fir.array<2x42xi32>>, %arg1: !fir.ref<!fir.array<2x42xi32>>) {
48    %c0 = arith.constant 0 : index
49    %c1 = arith.constant 1 : index
50    %c41 = arith.constant 41 : index
51    %c42 = arith.constant 42 : index
52    %shape = fir.shape %c42 : (index) -> !fir.shape<1>
53    %array0 = hlfir.designate %arg0 (%c0, %c0:%c41:%c1) shape %shape {test.ptr = "array0"} : (!fir.ref<!fir.array<2x42xi32>>, index, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
54    %array1 = hlfir.designate %arg1 (%c1, %c0:%c41:%c1) shape %shape {test.ptr = "array1"} : (!fir.ref<!fir.array<2x42xi32>>, index, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
55    return
56}
57
58// -----
59
60// FIXME: designate doesn't understand non-overlappning array indices
61// make sure that we say MayAlias and not MustAlias until array indexes are understood
62// CHECK: array2#0 <-> array3#0: MayAlias
63
64func.func @array_element_same_source(%arg0: !fir.ref<!fir.array<2x42xi32>>) {
65    %c0 = arith.constant 0 : index
66    %c1 = arith.constant 1 : index
67    %c41 = arith.constant 41 : index
68    %c42 = arith.constant 42 : index
69    %shape = fir.shape %c42 : (index) -> !fir.shape<1>
70    %array2 = hlfir.designate %arg0 (%c0, %c0:%c41:%c1) shape %shape {test.ptr = "array2"} : (!fir.ref<!fir.array<2x42xi32>>, index, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
71    %array3 = hlfir.designate %arg0 (%c1, %c0:%c41:%c1) shape %shape {test.ptr = "array3"} : (!fir.ref<!fir.array<2x42xi32>>, index, index, index, index, !fir.shape<1>) -> !fir.box<!fir.array<?xi32>>
72    return
73}