Skip to content

Commit e656a99

Browse files
committed
!fixup address latest comments, thanks!
1 parent c9c8024 commit e656a99

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes,
3939
llvm::Module &M, const CodeGenOptions &CGO,
4040
const LangOptions &Features)
4141
: Context(Ctx), CGTypes(CGTypes), Module(M), CodeGenOpts(CGO),
42-
Features(Features), MDHelper(M.getContext()), Root(nullptr),
43-
Char(nullptr) {}
42+
Features(Features),
43+
MangleCtx(ItaniumMangleContext::create(Ctx, Ctx.getDiagnostics())),
44+
MDHelper(M.getContext()), Root(nullptr), Char(nullptr) {}
4445

4546
CodeGenTBAA::~CodeGenTBAA() {
4647
}
@@ -202,14 +203,6 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
202203
// Other qualifiers could theoretically be distinguished, especially if
203204
// they involve a significant representation difference. We don't
204205
// currently do so, however.
205-
//
206-
// Computing the pointee type string recursively is implicitly more
207-
// forgiving than the standards require. Effectively, we are turning
208-
// the question "are these types compatible/similar" into "are
209-
// accesses to these types allowed to alias". In both C and C++,
210-
// the latter question has special carve-outs for signedness
211-
// mismatches that only apply at the top level. As a result, we are
212-
// allowing e.g. `int *` l-values to access `unsigned *` objects.
213206
if (Ty->isPointerType() || Ty->isReferenceType()) {
214207
llvm::MDNode *AnyPtr = createScalarTypeNode("any pointer", getChar(), Size);
215208
if (!CodeGenOpts.PointerTBAA)
@@ -221,7 +214,15 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
221214
PtrDepth++;
222215
Ty = Ty->getPointeeType().getTypePtr();
223216
} while (Ty->isPointerType());
224-
217+
Ty = Context.getBaseElementType(QualType(Ty, 0)).getTypePtr();
218+
assert(!isa<VariableArrayType>(Ty));
219+
// When the underlying type is a builtin type, we compute the pointee type
220+
// string recursively, which is implicitly more forgiving than the standards
221+
// require. Effectively, we are turning the question "are these types
222+
// compatible/similar" into "are accesses to these types allowed to alias".
223+
// In both C and C++, the latter question has special carve-outs for
224+
// signedness mismatches that only apply at the top level. As a result, we
225+
// are allowing e.g. `int *` l-values to access `unsigned *` objects.
225226
SmallString<256> TyName;
226227
if (isa<BuiltinType>(Ty)) {
227228
llvm::MDNode *ScalarMD = getTypeInfoHelper(Ty);
@@ -230,18 +231,17 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
230231
ScalarMD->getOperand(CodeGenOpts.NewStructPathTBAA ? 2 : 0))
231232
->getString();
232233
TyName = Name;
233-
} else if (!isa<VariableArrayType>(Ty)) {
234+
} else {
234235
// For non-builtin types use the mangled name of the canonical type.
235236
llvm::raw_svector_ostream TyOut(TyName);
236-
Context.createMangleContext()->mangleCanonicalTypeName(QualType(Ty, 0),
237-
TyOut);
237+
MangleCtx->mangleCanonicalTypeName(QualType(Ty, 0), TyOut);
238238
}
239239

240-
SmallString<256> OutName("p");
241-
OutName += std::to_string(PtrDepth);
242-
OutName += " ";
243-
OutName += TyName;
244-
return createScalarTypeNode(OutName, AnyPtr, Size);
240+
SmallString<256> OutName("p");
241+
OutName += std::to_string(PtrDepth);
242+
OutName += " ";
243+
OutName += TyName;
244+
return createScalarTypeNode(OutName, AnyPtr, Size);
245245
}
246246

247247
// Accesses to arrays are accesses to objects of their element types.

clang/lib/CodeGen/CodeGenTBAA.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ namespace clang {
2424
class ASTContext;
2525
class CodeGenOptions;
2626
class LangOptions;
27+
class MangleContext;
2728
class QualType;
2829
class Type;
2930

@@ -119,6 +120,7 @@ class CodeGenTBAA {
119120
llvm::Module &Module;
120121
const CodeGenOptions &CodeGenOpts;
121122
const LangOptions &Features;
123+
MangleContext *MangleCtx;
122124

123125
// MDHelper - Helper for creating metadata.
124126
llvm::MDBuilder MDHelper;

clang/test/CodeGen/tbaa-pointers.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,32 @@ void p2struct2(struct S2 *ptr) {
160160
ptr->s = 0;
161161
}
162162

163+
164+
void vla1(int n, int ptr[][n], int idx) {
165+
// COMMON-LABEL: define void @vla1(
166+
// COMMON-SAME: i32 noundef [[N:%.+]], ptr noundef [[PTR:%.+]], i32 noundef [[IDX:%.+]])
167+
// COMMON: [[N_ADDR:%.+]] = alloca i32, align 4
168+
// COMMON-NEXT: [[PTR_ADDR:%.+]] = alloca ptr, align 8
169+
// COMMON-NEXT: [[IDX_ADDR:%.+]] = alloca i32, align 4
170+
// COMMON-NEXT: store i32 [[N]], ptr [[N_ADDR]], align 4, !tbaa [[INT_TY:!.+]]
171+
// ENABLED-NEXT: store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa [[P1INT0:!.+]]
172+
// DEFAULT-NEXT: store ptr [[PTR]], ptr [[PTR_ADDR]], align 8, !tbaa [[ANYPTR]]
173+
// COMMON-NEXT: store i32 [[IDX]], ptr [[IDX_ADDR]], align 4, !tbaa [[INT_TY]]
174+
// COMMON-NEXT: [[L:%.+]] = load i32, ptr [[N_ADDR]], align 4, !tbaa [[INT_TY]]
175+
// COMMON-NEXT: [[L_EXT:%.+]] = zext i32 [[L]] to i64
176+
// ENABLED-NEXT: [[L_PTR:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, !tbaa [[P1INT0]]
177+
// DEFAULT-NEXT: [[L_PTR:%.+]] = load ptr, ptr [[PTR_ADDR]], align 8, !tbaa [[ANYPTR]]
178+
// COMMON-NEXT: [[L_IDX:%.+]] = load i32, ptr [[IDX_ADDR]], align 4, !tbaa [[INT_TY]]
179+
// COMMON-NEXT: [[IDX_EXT:%.+]] = sext i32 [[L_IDX]] to i64
180+
// COMMON-NEXT: [[MUL:%.+]] = mul nsw i64 [[IDX_EXT]], [[L_EXT]]
181+
// COMMON-NEXT: [[GEP1:%.+]] = getelementptr inbounds i32, ptr [[L_PTR]], i64 [[MUL]]
182+
// COMMON-NEXT: [[GEP2:%.+]] = getelementptr inbounds i32, ptr [[GEP1]], i64 0
183+
// COMMON-NEXT: store i32 0, ptr [[GEP2]], align 4, !tbaa [[INT_TAG:!.+]]
184+
// ENABLED-NEXT: ret void
185+
186+
ptr[idx][0] = 0;
187+
}
188+
163189
// ENABLED: [[P2INT_0]] = !{[[P2INT:!.+]], [[P2INT]], i64 0}
164190
// ENABLED: [[P2INT]] = !{!"p2 int", [[ANY_POINTER:!.+]], i64 0}
165191
// DEFAULT: [[ANYPTR]] = !{[[ANY_POINTER:!.+]], [[ANY_POINTER]], i64 0}
@@ -189,3 +215,5 @@ void p2struct2(struct S2 *ptr) {
189215
// ENABLED: [[S2_TY]] = !{!"S2", [[P1S1]], i64 0}
190216
// DEFAULT: [[S2_S_TAG]] = !{[[S2_TY:!.+]], [[ANY_POINTER]], i64 0}
191217
// DEFAULT: [[S2_TY]] = !{!"S2", [[ANY_POINTER]], i64 0}
218+
// COMMON: [[INT_TAG]] = !{[[INT_TY:!.+]], [[INT_TY]], i64 0}
219+
// COMMON: [[INT_TY]] = !{!"int", [[CHAR]], i64 0}

0 commit comments

Comments
 (0)