llvm.org GIT mirror llvm / 7c48f0a
[Analysis] Generate more precise TBAA tags when one access encloses the other There are cases when two tags with different base types denote accesses to the same direct or indirect member of a structure type. Currently, merging of such tags results in a tag that represents an access to an object that has the type of that member. This patch changes this so that if one of the accesses encloses the other, then the generic tag is the one of the enclosed access. Differential Revision: https://reviews.llvm.org/D39557 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@321019 91177308-0d34-0410-b5e6-96231b3b80d8 Ivan A. Kosarev 1 year, 9 months ago
3 changed file(s) with 104 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
543543 TBAAStructTagNode TagA(A), TagB(B);
544544 const MDNode *CommonType = getLeastCommonType(TagA.getAccessType(),
545545 TagB.getAccessType());
546
547 // TODO: We need to check if AccessType of TagA encloses AccessType of
548 // TagB to support aggregate AccessType. If yes, return true.
549
550 // Climb the type DAG from base type of A to see if we reach base type of B.
551 uint64_t OffsetA;
552 if (findAccessType(TagA, TagB.getBaseType(), OffsetA)) {
553 bool SameMemberAccess = OffsetA == TagB.getOffset();
554 if (GenericTag)
555 *GenericTag = SameMemberAccess ? TagB.getNode() :
556 createAccessTag(CommonType);
557 return SameMemberAccess;
558 }
559
560 // Climb the type DAG from base type of B to see if we reach base type of A.
561 uint64_t OffsetB;
562 if (findAccessType(TagB, TagA.getBaseType(), OffsetB)) {
563 bool SameMemberAccess = OffsetB == TagA.getOffset();
564 if (GenericTag)
565 *GenericTag = SameMemberAccess ? TagA.getNode() :
566 createAccessTag(CommonType);
567 return SameMemberAccess;
568 }
569
546570 if (GenericTag)
547571 *GenericTag = createAccessTag(CommonType);
548
549 // TODO: We need to check if AccessType of TagA encloses AccessType of
550 // TagB to support aggregate AccessType. If yes, return true.
551
552 // Climb the type DAG from base type of A to see if we reach base type of B.
553 uint64_t OffsetA;
554 if (findAccessType(TagA, TagB.getBaseType(), OffsetA))
555 return OffsetA == TagB.getOffset();
556
557 // Climb the type DAG from base type of B to see if we reach base type of A.
558 uint64_t OffsetB;
559 if (findAccessType(TagB, TagA.getBaseType(), OffsetB))
560 return OffsetB == TagA.getOffset();
561572
562573 // If the final access types have different roots, they're part of different
563574 // potentially unrelated type systems, so we must be conservative.
0 ; RUN: opt -tbaa -basicaa -gvn -S < %s | FileCheck %s
11
22 define i32 @test1(i8* %p, i8* %q) {
3 ; CHECK: @test1(i8* %p, i8* %q)
3 ; CHECK-LABEL: @test1(i8* %p, i8* %q)
44 ; CHECK: call i32 @foo(i8* %p)
55 ; CHECK-NOT: tbaa
66 ; CHECK: %c = add i32 %a, %a
1111 }
1212
1313 define i32 @test2(i8* %p, i8* %q) {
14 ; CHECK: @test2(i8* %p, i8* %q)
14 ; CHECK-LABEL: @test2(i8* %p, i8* %q)
1515 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGC:!.*]]
1616 ; CHECK: %c = add i32 %a, %a
1717 %a = call i32 @foo(i8* %p), !tbaa !0
2121 }
2222
2323 define i32 @test3(i8* %p, i8* %q) {
24 ; CHECK: @test3(i8* %p, i8* %q)
24 ; CHECK-LABEL: @test3(i8* %p, i8* %q)
2525 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGB:!.*]]
2626 ; CHECK: %c = add i32 %a, %a
2727 %a = call i32 @foo(i8* %p), !tbaa !3
3131 }
3232
3333 define i32 @test4(i8* %p, i8* %q) {
34 ; CHECK: @test4(i8* %p, i8* %q)
34 ; CHECK-LABEL: @test4(i8* %p, i8* %q)
3535 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA:!.*]]
3636 ; CHECK: %c = add i32 %a, %a
3737 %a = call i32 @foo(i8* %p), !tbaa !1
4141 }
4242
4343 define i32 @test5(i8* %p, i8* %q) {
44 ; CHECK: @test5(i8* %p, i8* %q)
45 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA:!.*]]
44 ; CHECK-LABEL: @test5(i8* %p, i8* %q)
45 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA]]
4646 ; CHECK: %c = add i32 %a, %a
4747 %a = call i32 @foo(i8* %p), !tbaa !0
4848 %b = call i32 @foo(i8* %p), !tbaa !1
5151 }
5252
5353 define i32 @test6(i8* %p, i8* %q) {
54 ; CHECK: @test6(i8* %p, i8* %q)
55 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA:!.*]]
54 ; CHECK-LABEL: @test6(i8* %p, i8* %q)
55 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA]]
5656 ; CHECK: %c = add i32 %a, %a
5757 %a = call i32 @foo(i8* %p), !tbaa !0
5858 %b = call i32 @foo(i8* %p), !tbaa !3
6161 }
6262
6363 define i32 @test7(i8* %p, i8* %q) {
64 ; CHECK: @test7(i8* %p, i8* %q)
64 ; CHECK-LABEL: @test7(i8* %p, i8* %q)
6565 ; CHECK: call i32 @foo(i8* %p)
6666 ; CHECK-NOT: tbaa
6767 ; CHECK: %c = add i32 %a, %a
7171 ret i32 %c
7272 }
7373
74
75
7674 define i32 @test8(i32* %p, i32* %q) {
77 ; CHECK-LABEL: test8
75 ; CHECK-LABEL: @test8
7876 ; CHECK-NEXT: store i32 15, i32* %p
7977 ; CHECK-NEXT: ret i32 0
8078 ; Since we know the location is invariant, we can forward the
8684 %c = sub i32 %a, %b
8785 ret i32 %c
8886 }
87
8988 define i32 @test9(i32* %p, i32* %q) {
90 ; CHECK-LABEL: test9
89 ; CHECK-LABEL: @test9
9190 ; CHECK-NEXT: call void @clobber()
9291 ; CHECK-NEXT: ret i32 0
9392 ; Since we know the location is invariant, we can forward the
10099 ret i32 %c
101100 }
102101
102 define i32 @test10(i8* %p, i8* %q) {
103 ; If one access encloses the other, then the merged access is the enclosed one
104 ; and not just the common final access type.
105 ; CHECK-LABEL: @test10
106 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAG_X_i:!.*]]
107 ; CHECK: %c = add i32 %a, %a
108 %a = call i32 @foo(i8* %p), !tbaa !15 ; TAG_X_i
109 %b = call i32 @foo(i8* %p), !tbaa !19 ; TAG_Y_x_i
110 %c = add i32 %a, %b
111 ret i32 %c
112 }
103113
104114 declare void @clobber()
105115 declare i32 @foo(i8*) readonly
106116
107 ; CHECK: [[TAGC]] = !{[[TYPEC:!.*]], [[TYPEC]], i64 0}
108 ; CHECK: [[TYPEC]] = !{!"C", [[TYPEA:!.*]]}
109 ; CHECK: [[TYPEA]] = !{!"A", !{{.*}}}
110 ; CHECK: [[TAGB]] = !{[[TYPEB:!.*]], [[TYPEB]], i64 0}
111 ; CHECK: [[TYPEB]] = !{!"B", [[TYPEA]]}
112 ; CHECK: [[TAGA]] = !{[[TYPEA]], [[TYPEA]], i64 0}
117 ; CHECK-DAG: [[TAGC]] = !{[[TYPEC:!.*]], [[TYPEC]], i64 0}
118 ; CHECK-DAG: [[TYPEC]] = !{!"C", [[TYPEA:!.*]]}
119 ; CHECK-DAG: [[TYPEA]] = !{!"A", !{{.*}}}
120 ; CHECK-DAG: [[TAGB]] = !{[[TYPEB:!.*]], [[TYPEB]], i64 0}
121 ; CHECK-DAG: [[TYPEB]] = !{!"B", [[TYPEA]]}
122 ; CHECK-DAG: [[TAGA]] = !{[[TYPEA]], [[TYPEA]], i64 0}
113123 !0 = !{!5, !5, i64 0}
114124 !1 = !{!6, !6, i64 0}
115125 !2 = !{!"tbaa root"}
121131 !8 = !{!"another root"}
122132 !11 = !{!"scalar type", !8}
123133
134 ; CHECK-DAG: [[TAG_X_i]] = !{[[TYPE_X:!.*]], [[TYPE_int:!.*]], i64 0}
135 ; CHECK-DAG: [[TYPE_X:!.*]] = !{!"struct X", [[TYPE_int]], i64 0}
136 ; CHECK-DAG: [[TYPE_int]] = !{!"int", {{!.*}}, i64 0}
137 !15 = !{!16, !17, i64 0} ; TAG_X_i
138 !16 = !{!"struct X", !17, i64 0} ; struct X { int i; };
139 !17 = !{!"int", !18, i64 0}
140 !18 = !{!"char", !2, i64 0}
124141
125 ;; A TBAA structure who's only point is to have a constant location
142 !19 = !{!20, !17, i64 0} ; TAG_Y_x_i
143 !20 = !{!"struct Y", !16, i64 0} ; struct Y { struct X x; };
144
145 ; A TBAA structure who's only point is to have a constant location.
126146 !9 = !{!"yet another root"}
127147 !10 = !{!"node", !9, i64 1}
128
0 ; RUN: opt -tbaa -basicaa -newgvn -S < %s | FileCheck %s
11
22 define i32 @test1(i8* %p, i8* %q) {
3 ; CHECK: @test1(i8* %p, i8* %q)
3 ; CHECK-LABEL: @test1(i8* %p, i8* %q)
44 ; CHECK: call i32 @foo(i8* %p)
55 ; CHECK-NOT: tbaa
66 ; CHECK: %c = add i32 %a, %a
1111 }
1212
1313 define i32 @test2(i8* %p, i8* %q) {
14 ; CHECK: @test2(i8* %p, i8* %q)
14 ; CHECK-LABEL: @test2(i8* %p, i8* %q)
1515 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGC:!.*]]
1616 ; CHECK: %c = add i32 %a, %a
1717 %a = call i32 @foo(i8* %p), !tbaa !0
2121 }
2222
2323 define i32 @test3(i8* %p, i8* %q) {
24 ; CHECK: @test3(i8* %p, i8* %q)
24 ; CHECK-LABEL: @test3(i8* %p, i8* %q)
2525 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGB:!.*]]
2626 ; CHECK: %c = add i32 %a, %a
2727 %a = call i32 @foo(i8* %p), !tbaa !3
3131 }
3232
3333 define i32 @test4(i8* %p, i8* %q) {
34 ; CHECK: @test4(i8* %p, i8* %q)
34 ; CHECK-LABEL: @test4(i8* %p, i8* %q)
3535 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA:!.*]]
3636 ; CHECK: %c = add i32 %a, %a
3737 %a = call i32 @foo(i8* %p), !tbaa !1
4141 }
4242
4343 define i32 @test5(i8* %p, i8* %q) {
44 ; CHECK: @test5(i8* %p, i8* %q)
45 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA:!.*]]
44 ; CHECK-LABEL: @test5(i8* %p, i8* %q)
45 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA]]
4646 ; CHECK: %c = add i32 %a, %a
4747 %a = call i32 @foo(i8* %p), !tbaa !0
4848 %b = call i32 @foo(i8* %p), !tbaa !1
5151 }
5252
5353 define i32 @test6(i8* %p, i8* %q) {
54 ; CHECK: @test6(i8* %p, i8* %q)
55 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA:!.*]]
54 ; CHECK-LABEL: @test6(i8* %p, i8* %q)
55 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAGA]]
5656 ; CHECK: %c = add i32 %a, %a
5757 %a = call i32 @foo(i8* %p), !tbaa !0
5858 %b = call i32 @foo(i8* %p), !tbaa !3
6161 }
6262
6363 define i32 @test7(i8* %p, i8* %q) {
64 ; CHECK: @test7(i8* %p, i8* %q)
64 ; CHECK-LABEL: @test7(i8* %p, i8* %q)
6565 ; CHECK: call i32 @foo(i8* %p)
6666 ; CHECK-NOT: tbaa
6767 ; CHECK: %c = add i32 %a, %a
7171 ret i32 %c
7272 }
7373
74
75
7674 define i32 @test8(i32* %p, i32* %q) {
77 ; CHECK-LABEL: test8
75 ; CHECK-LABEL: @test8
7876 ; CHECK-NEXT: store i32 15, i32* %p
7977 ; CHECK-NEXT: ret i32 0
8078 ; Since we know the location is invariant, we can forward the
8684 %c = sub i32 %a, %b
8785 ret i32 %c
8886 }
87
8988 define i32 @test9(i32* %p, i32* %q) {
90 ; CHECK-LABEL: test9
89 ; CHECK-LABEL: @test9
9190 ; CHECK-NEXT: call void @clobber()
9291 ; CHECK-NEXT: ret i32 0
9392 ; Since we know the location is invariant, we can forward the
10099 ret i32 %c
101100 }
102101
102 define i32 @test10(i8* %p, i8* %q) {
103 ; If one access encloses the other, then the merged access is the enclosed one
104 ; and not just the common final access type.
105 ; CHECK-LABEL: @test10
106 ; CHECK: call i32 @foo(i8* %p), !tbaa [[TAG_X_i:!.*]]
107 ; CHECK: %c = add i32 %a, %a
108 %a = call i32 @foo(i8* %p), !tbaa !15 ; TAG_X_i
109 %b = call i32 @foo(i8* %p), !tbaa !19 ; TAG_Y_x_i
110 %c = add i32 %a, %b
111 ret i32 %c
112 }
103113
104114 declare void @clobber()
105115 declare i32 @foo(i8*) readonly
106116
107 ; CHECK: [[TAGC]] = !{[[TYPEC:!.*]], [[TYPEC]], i64 0}
108 ; CHECK: [[TYPEC]] = !{!"C", [[TYPEA:!.*]]}
109 ; CHECK: [[TYPEA]] = !{!"A", !{{.*}}}
110 ; CHECK: [[TAGB]] = !{[[TYPEB:!.*]], [[TYPEB]], i64 0}
111 ; CHECK: [[TYPEB]] = !{!"B", [[TYPEA]]}
112 ; CHECK: [[TAGA]] = !{[[TYPEA]], [[TYPEA]], i64 0}
117 ; CHECK-DAG: [[TAGC]] = !{[[TYPEC:!.*]], [[TYPEC]], i64 0}
118 ; CHECK-DAG: [[TYPEC]] = !{!"C", [[TYPEA:!.*]]}
119 ; CHECK-DAG: [[TYPEA]] = !{!"A", !{{.*}}}
120 ; CHECK-DAG: [[TAGB]] = !{[[TYPEB:!.*]], [[TYPEB]], i64 0}
121 ; CHECK-DAG: [[TYPEB]] = !{!"B", [[TYPEA]]}
122 ; CHECK-DAG: [[TAGA]] = !{[[TYPEA]], [[TYPEA]], i64 0}
113123 !0 = !{!5, !5, i64 0}
114124 !1 = !{!6, !6, i64 0}
115125 !2 = !{!"tbaa root"}
121131 !8 = !{!"another root"}
122132 !11 = !{!"scalar type", !8}
123133
134 ; CHECK-DAG: [[TAG_X_i]] = !{[[TYPE_X:!.*]], [[TYPE_int:!.*]], i64 0}
135 ; CHECK-DAG: [[TYPE_X:!.*]] = !{!"struct X", [[TYPE_int]], i64 0}
136 ; CHECK-DAG: [[TYPE_int]] = !{!"int", {{!.*}}, i64 0}
137 !15 = !{!16, !17, i64 0} ; TAG_X_i
138 !16 = !{!"struct X", !17, i64 0} ; struct X { int i; };
139 !17 = !{!"int", !18, i64 0}
140 !18 = !{!"char", !2, i64 0}
124141
125 ;; A TBAA structure who's only point is to have a constant location
142 !19 = !{!20, !17, i64 0} ; TAG_Y_x_i
143 !20 = !{!"struct Y", !16, i64 0} ; struct Y { struct X x; };
144
145 ; A TBAA structure who's only point is to have a constant location.
126146 !9 = !{!"yet another root"}
127147 !10 = !{!"node", !9, i64 1}
128