Skip to content

Commit 1a40063

Browse files
committed
CodeGenTBAA/CGExpr: fix cases with may_alias arrays
1 parent 52cb263 commit 1a40063

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4524,11 +4524,8 @@ LValue CodeGenFunction::EmitArraySubscriptExpr(const ArraySubscriptExpr *E,
45244524
// If the array lvalue had no base type, there is no point trying to
45254525
// generate one, since an array itself is not a valid base type.
45264526

4527-
// The access size must be updated to the size of an individual element.
4528-
// We also reset the access type using the original element type: in case
4529-
// of type decoration on the element type they may have been lost when
4530-
// the array type was canonicalized.
4531-
EltTBAAInfo.AccessType = CGM.getTBAATypeInfo(E->getType());
4527+
// We also retain the access type from the base lvalue, but the access
4528+
// size must be updated to the size of an individual element.
45324529
EltTBAAInfo.Size =
45334530
getContext().getTypeSizeInChars(E->getType()).getQuantity();
45344531
}

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ static bool TypeHasMayAlias(QualType QTy) {
130130
return true;
131131
QTy = TT->desugar();
132132
}
133+
134+
// Also consider an array type as may_alias when its element type (at
135+
// any level) is marked as such.
136+
if (auto *ArrayTy = QTy->getAsArrayTypeUnsafe())
137+
if (TypeHasMayAlias(ArrayTy->getElementType()))
138+
return true;
139+
133140
return false;
134141
}
135142

clang/test/CodeGen/tbaa-array.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ struct C { int i; int x[3]; };
1313
struct D { int n; int arr[]; }; // flexible array member
1414
extern int AA[]; // incomplete array type
1515

16+
typedef int __attribute__((may_alias)) aliasing_int;
17+
typedef int __attribute__((may_alias)) aliasing_array[10];
18+
struct E {
19+
aliasing_int x[4]; aliasing_array y;
20+
};
21+
1622
int foo(B *b) {
1723
// CHECK-LABEL: _Z3fooP1B
1824
// CHECK: load i32, {{.*}}, !tbaa [[TAG_A_i:!.*]]
@@ -54,6 +60,18 @@ int bar5(int j) {
5460
return AA[2] + AA[j];
5561
}
5662

63+
int bar6(E *e, int j) {
64+
// CHECK-NEW-LABEL: _Z4bar6P1Ei
65+
// CHECK-NEW: load i32, {{.*}}, !tbaa [[TAG_E_x:!.*]]
66+
return e->x[j];
67+
}
68+
69+
int bar7(E *e, int j) {
70+
// CHECK-NEW-LABEL: _Z4bar7P1Ei
71+
// CHECK-NEW: load i32, {{.*}}, !tbaa [[TAG_E_y:!.*]]
72+
return e->y[j];
73+
}
74+
5775
// CHECK-DAG: [[TAG_A_i]] = !{[[TYPE_A:!.*]], [[TYPE_int:!.*]], i64 0}
5876
// CHECK-DAG: [[TYPE_A]] = !{!"_ZTS1A", !{{.*}}, i64 0}
5977
// CHECK-DAG: [[TYPE_int]] = !{!"int", !{{.*}}, i64 0}
@@ -64,5 +82,8 @@ int bar5(int j) {
6482
// CHECK-NEW-DAG: [[TYPE_A:!.*]] = !{[[TYPE_char]], i64 4, !"_ZTS1A", [[TYPE_int]], i64 0, i64 4}
6583
// CHECK-NEW-DAG: [[TAG_A_i]] = !{[[TYPE_A]], [[TYPE_int]], i64 0, i64 4}
6684
// CHECK-NEW-DAG: [[TYPE_C:!.*]] = !{[[TYPE_char]], i64 16, !"_ZTS1C", [[TYPE_int]], i64 0, i64 4, [[TYPE_int]], i64 4, i64 12}
67-
// CHECK-NEW-DAG: [[TAG_C_i]] = !{[[TYPE_C:!.*]], [[TYPE_int:!.*]], i64 0, i64 4}
68-
// CHECK-NEW-DAG: [[TAG_C_x]] = !{[[TYPE_C:!.*]], [[TYPE_int:!.*]], i64 4, i64 4}
85+
// CHECK-NEW-DAG: [[TAG_C_i]] = !{[[TYPE_C]], [[TYPE_int]], i64 0, i64 4}
86+
// CHECK-NEW-DAG: [[TAG_C_x]] = !{[[TYPE_C]], [[TYPE_int]], i64 4, i64 4}
87+
// CHECK-NEW-DAG: [[TYPE_E:!.*]] = !{[[TYPE_char]], i64 56, !"_ZTS1E", [[TYPE_char]], i64 0, i64 16, [[TYPE_char]], i64 16, i64 40}
88+
// CHECK-NEW-DAG: [[TAG_E_x]] = !{[[TYPE_E]], [[TYPE_char]], i64 0, i64 4}
89+
// CHECK-NEW-DAG: [[TAG_E_y]] = !{[[TYPE_E]], [[TYPE_char]], i64 16, i64 4}

0 commit comments

Comments
 (0)