llvm.org GIT mirror llvm / a942d77
[Verifier] Add verification for TBAA metadata Summary: This change adds some verification in the IR verifier around struct path TBAA metadata. Other than some basic sanity checks (e.g. we get constant integers where we expect constant integers), this checks: - That by the time an struct access tuple `(base-type, offset)` is "reduced" to a scalar base type, the offset is `0`. For instance, in C++ you can't start from, say `("struct-a", 16)`, and end up with `("int", 4)` -- by the time the base type is `"int"`, the offset better be zero. In particular, a variant of this invariant is needed for `llvm::getMostGenericTBAA` to be correct. - That there are no cycles in a struct path. - That struct type nodes have their offsets listed in an ascending order. - That when generating the struct access path, you eventually reach the access type listed in the tbaa tag node. Reviewers: dexonsmith, chandlerc, reames, mehdi_amini, manmanren Subscribers: mcrosier, llvm-commits Differential Revision: https://reviews.llvm.org/D26438 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@289402 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
36 changed file(s) with 454 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
186186 *OS << *C;
187187 }
188188
189 void Write(const APInt *AI) {
190 if (!AI)
191 return;
192 *OS << *AI << '\n';
193 }
194
195 void Write(const unsigned i) { *OS << i << '\n'; }
196
189197 template void Write(ArrayRef Vs) {
190198 for (const T &V : Vs)
191199 Write(V);
283291 // twice, if they have multiple operands. In particular for very large
284292 // constant expressions, we can arrive at a particular user many times.
285293 SmallPtrSet GlobalValueVisited;
294
295 /// Cache of TBAA base nodes that have already been visited. This cachce maps
296 /// a node that has been visited to a pair (IsInvalid, BitWidth) where
297 ///
298 /// \c IsInvalid is true iff the node is invalid.
299 /// \c BitWidth, if non-zero, is the bitwidth of the integer used to denoting
300 /// the offset of the access. If zero, only a zero offset is allowed.
301 ///
302 /// \c BitWidth has no meaning if \c IsInvalid is true.
303 typedef std::pair TBAABaseNodeSummary;
304 DenseMap TBAABaseNodes;
286305
287306 void checkAtomicMemAccessSize(Type *Ty, const Instruction *I);
288307
391410 void visitRangeMetadata(Instruction &I, MDNode *Range, Type *Ty);
392411 void visitDereferenceableMetadata(Instruction &I, MDNode *MD);
393412 void visitTBAAMetadata(Instruction &I, MDNode *MD);
413
414 /// \name Helper functions used by \c visitTBAAMetadata.
415 /// @{
416 MDNode *getFieldNodeFromTBAABaseNode(Instruction &I, MDNode *BaseNode,
417 APInt &Offset);
418 TBAABaseNodeSummary verifyTBAABaseNode(Instruction &I, MDNode *BaseNode);
419 TBAABaseNodeSummary verifyTBAABaseNodeImpl(Instruction &I, MDNode *BaseNode);
420 /// @}
394421
395422 template bool isValidMetadataArray(const MDTuple &N);
396423 #define HANDLE_SPECIALIZED_MDNODE_LEAF(CLASS) void visit##CLASS(const CLASS &N);
36563683 "dereferenceable_or_null metadata value must be an i64!", &I);
36573684 }
36583685
3686 /// Verify that \p BaseNode can be used as the "base type" in the struct-path
3687 /// TBAA scheme. This means \p BaseNode is either a scalar node, or a
3688 /// struct-type node describing an aggregate data structure (like a struct).
3689 Verifier::TBAABaseNodeSummary Verifier::verifyTBAABaseNode(Instruction &I,
3690 MDNode *BaseNode) {
3691 if (BaseNode->getNumOperands() < 2) {
3692 CheckFailed("Base nodes must have at least two operands", &I, BaseNode);
3693 return {true, ~0u};
3694 }
3695
3696 auto Itr = TBAABaseNodes.find(BaseNode);
3697 if (Itr != TBAABaseNodes.end())
3698 return Itr->second;
3699
3700 auto Result = verifyTBAABaseNodeImpl(I, BaseNode);
3701 auto InsertResult = TBAABaseNodes.insert({BaseNode, Result});
3702 (void)InsertResult;
3703 assert(InsertResult.second && "We just checked!");
3704 return Result;
3705 }
3706
3707 Verifier::TBAABaseNodeSummary
3708 Verifier::verifyTBAABaseNodeImpl(Instruction &I, MDNode *BaseNode) {
3709 const Verifier::TBAABaseNodeSummary InvalidNode = {true, ~0u};
3710
3711 if (BaseNode->getNumOperands() == 2) {
3712 // This is a scalar base node.
3713 if (!BaseNode->getOperand(0) || !BaseNode->getOperand(1)) {
3714 CheckFailed("Null operands in scalar type nodes!", &I, BaseNode);
3715 return InvalidNode;
3716 }
3717 if (!isa(BaseNode->getOperand(1))) {
3718 CheckFailed("Invalid parent operand in scalar TBAA node", &I, BaseNode);
3719 return InvalidNode;
3720 }
3721 if (!isa(BaseNode->getOperand(0))) {
3722 CheckFailed("Invalid name operand in scalar TBAA node", &I, BaseNode);
3723 return InvalidNode;
3724 }
3725
3726 // Scalar nodes can only be accessed at offset 0.
3727 return {false, 0};
3728 }
3729
3730 if (BaseNode->getNumOperands() % 2 != 1) {
3731 CheckFailed("Struct tag nodes must have an odd number of operands!",
3732 BaseNode);
3733 return InvalidNode;
3734 }
3735
3736 bool Failed = false;
3737
3738 Optional PrevOffset;
3739 unsigned BitWidth = ~0u;
3740
3741 // We've already checked that BaseNode is not a degenerate root node with one
3742 // operand in \c verifyTBAABaseNode, so this loop should run at least once.
3743 for (unsigned Idx = 1; Idx < BaseNode->getNumOperands(); Idx += 2) {
3744 const MDOperand &FieldTy = BaseNode->getOperand(Idx);
3745 const MDOperand &FieldOffset = BaseNode->getOperand(Idx + 1);
3746 if (!isa(FieldTy)) {
3747 CheckFailed("Incorrect field entry in struct type node!", &I, BaseNode);
3748 Failed = true;
3749 continue;
3750 }
3751
3752 auto *OffsetEntryCI =
3753 mdconst::dyn_extract_or_null(FieldOffset);
3754 if (!OffsetEntryCI) {
3755 CheckFailed("Offset entries must be constants!", &I, BaseNode);
3756 Failed = true;
3757 continue;
3758 }
3759
3760 if (BitWidth == ~0u)
3761 BitWidth = OffsetEntryCI->getBitWidth();
3762
3763 if (OffsetEntryCI->getBitWidth() != BitWidth) {
3764 CheckFailed(
3765 "Bitwidth between the offsets and struct type entries must match", &I,
3766 BaseNode);
3767 Failed = true;
3768 continue;
3769 }
3770
3771 // NB! As far as I can tell, we generate a non-strictly increasing offset
3772 // sequence only from structs that have zero size bit fields. When
3773 // recursing into a contained struct in \c getFieldNodeFromTBAABaseNode we
3774 // pick the field lexically the latest in struct type metadata node. This
3775 // mirrors the actual behavior of the alias analysis implementation.
3776 bool IsAscending =
3777 !PrevOffset || PrevOffset->ule(OffsetEntryCI->getValue());
3778
3779 if (!IsAscending) {
3780 CheckFailed("Offsets must be increasing!", &I, BaseNode);
3781 Failed = true;
3782 }
3783
3784 PrevOffset = OffsetEntryCI->getValue();
3785 }
3786
3787 return Failed ? InvalidNode : Verifier::TBAABaseNodeSummary(false, BitWidth);
3788 }
3789
3790 static bool IsRootTBAANode(const MDNode *MD) {
3791 return MD->getNumOperands() < 2;
3792 }
3793
3794 static bool IsScalarTBAANodeImpl(const MDNode *MD,
3795 SmallPtrSetImpl &Visited) {
3796 if (MD->getNumOperands() == 2)
3797 return true;
3798
3799 if (MD->getNumOperands() != 3)
3800 return false;
3801
3802 auto *Offset = mdconst::dyn_extract(MD->getOperand(2));
3803 if (!(Offset && Offset->isZero() && isa(MD->getOperand(0))))
3804 return false;
3805
3806 auto *Parent = dyn_cast(MD->getOperand(1));
3807 return Visited.insert(Parent).second &&
3808 (IsRootTBAANode(Parent) || IsScalarTBAANodeImpl(Parent, Visited));
3809 }
3810
3811 static bool IsScalarTBAANode(const MDNode *MD) {
3812 SmallPtrSet Visited;
3813 return IsScalarTBAANodeImpl(MD, Visited);
3814 }
3815
3816 /// Returns the field node at the offset \p Offset in \p BaseNode. Update \p
3817 /// Offset in place to be the offset within the field node returned.
3818 ///
3819 /// We assume we've okayed \p BaseNode via \c verifyTBAABaseNode.
3820 MDNode *Verifier::getFieldNodeFromTBAABaseNode(Instruction &I, MDNode *BaseNode,
3821 APInt &Offset) {
3822 assert(BaseNode->getNumOperands() >= 2 && "Invalid base node!");
3823
3824 // Scalar nodes have only one possible "field" -- their parent in the access
3825 // hierarchy. Offset must be zero at this point, but our caller is supposed
3826 // to Assert that.
3827 if (BaseNode->getNumOperands() == 2)
3828 return cast(BaseNode->getOperand(1));
3829
3830 for (unsigned Idx = 1; Idx < BaseNode->getNumOperands(); Idx += 2) {
3831 auto *OffsetEntryCI =
3832 mdconst::extract(BaseNode->getOperand(Idx + 1));
3833 if (OffsetEntryCI->getValue().ugt(Offset)) {
3834 if (Idx == 1) {
3835 CheckFailed("Could not find TBAA parent in struct type node", &I,
3836 BaseNode, &Offset);
3837 return nullptr;
3838 }
3839
3840 auto *PrevOffsetEntryCI =
3841 mdconst::extract(BaseNode->getOperand(Idx - 1));
3842 Offset -= PrevOffsetEntryCI->getValue();
3843 return cast(BaseNode->getOperand(Idx - 2));
3844 }
3845 }
3846
3847 auto *LastOffsetEntryCI = mdconst::extract(
3848 BaseNode->getOperand(BaseNode->getNumOperands() - 1));
3849
3850 Offset -= LastOffsetEntryCI->getValue();
3851 return cast(BaseNode->getOperand(BaseNode->getNumOperands() - 2));
3852 }
3853
36593854 void Verifier::visitTBAAMetadata(Instruction &I, MDNode *MD) {
36603855 bool IsStructPathTBAA =
36613856 isa(MD->getOperand(0)) && MD->getNumOperands() >= 3;
36633858 Assert(IsStructPathTBAA,
36643859 "Old-style TBAA is no longer allowed, use struct-path TBAA instead",
36653860 &I);
3861
3862 Assert(MD->getNumOperands() < 5,
3863 "Struct tag metadata must have either 3 or 4 operands", &I, MD);
3864
3865 MDNode *BaseNode = dyn_cast_or_null(MD->getOperand(0));
3866 MDNode *AccessType = dyn_cast_or_null(MD->getOperand(1));
3867
3868 if (MD->getNumOperands() == 4) {
3869 auto *IsImmutableCI =
3870 mdconst::dyn_extract_or_null(MD->getOperand(3));
3871 Assert(IsImmutableCI,
3872 "Immutability tag on struct tag metadata must be a constant", &I,
3873 MD);
3874 Assert(IsImmutableCI->isZero() || IsImmutableCI->isOne(),
3875 "Immutability part of the struct tag metadata must be either 0 or 1",
3876 &I, MD);
3877 }
3878
3879 Assert(BaseNode && AccessType,
3880 "Malformed struct tag metadata: base and access-type "
3881 "should be non-null and point to Metadata nodes",
3882 &I, MD, BaseNode, AccessType);
3883
3884 Assert(IsScalarTBAANode(AccessType), "Access type node must be scalar", &I,
3885 MD, AccessType);
3886
3887 auto *OffsetCI = mdconst::dyn_extract_or_null(MD->getOperand(2));
3888 Assert(OffsetCI, "Offset must be constant integer", &I, MD);
3889
3890 APInt Offset = OffsetCI->getValue();
3891 bool SeenAccessTypeInPath = false;
3892
3893 SmallPtrSet StructPath;
3894
3895 for (/* empty */; BaseNode && !IsRootTBAANode(BaseNode);
3896 BaseNode = getFieldNodeFromTBAABaseNode(I, BaseNode, Offset)) {
3897 if (!StructPath.insert(BaseNode).second) {
3898 CheckFailed("Cycle detected in struct path", &I, MD);
3899 return;
3900 }
3901
3902 bool Invalid;
3903 unsigned BaseNodeBitWidth;
3904 std::tie(Invalid, BaseNodeBitWidth) = verifyTBAABaseNode(I, BaseNode);
3905
3906 // If the base node is invalid in itself, then we've already printed all the
3907 // errors we wanted to print.
3908 if (Invalid)
3909 return;
3910
3911 SeenAccessTypeInPath |= BaseNode == AccessType;
3912
3913 if (IsScalarTBAANode(BaseNode) || BaseNode == AccessType)
3914 Assert(Offset == 0, "Offset not zero at the point of scalar access", &I,
3915 MD, &Offset);
3916
3917 Assert(BaseNodeBitWidth == Offset.getBitWidth() ||
3918 (BaseNodeBitWidth == 0 && Offset == 0),
3919 "Access bit-width not the same as description bit-width", &I, MD,
3920 BaseNodeBitWidth, Offset.getBitWidth());
3921 }
3922
3923 Assert(SeenAccessTypeInPath, "Did not see access type in access path!", &I,
3924 MD);
36663925 }
36673926
36683927 /// verifyInstruction - Verify that an instruction is well formed.
38004059 if (MDNode *MD = I.getMetadata(LLVMContext::MD_dereferenceable_or_null))
38014060 visitDereferenceableMetadata(I, MD);
38024061
3803 if (MDNode *MD = I.getMetadata(LLVMContext::MD_tbaa))
3804 visitTBAAMetadata(I, MD);
4062 if (MDNode *TBAA = I.getMetadata(LLVMContext::MD_tbaa)) {
4063 Assert(isa(I) || isa(I) || isa(I) ||
4064 isa(I),
4065 "TBAA is only for loads, stores and calls!", &I);
4066 visitTBAAMetadata(I, TBAA);
4067 }
38054068
38064069 if (MDNode *AlignMD = I.getMetadata(LLVMContext::MD_align)) {
38074070 Assert(I.getType()->isPointerTy(), "align applies only to pointer types",
3030
3131 !0 = !{!4, !4, i64 0}
3232 !1 = !{!"omnipotent char", !2}
33 !2 = !{!"Simple C/C++ TBAA", null}
33 !2 = !{!"Simple C/C++ TBAA"}
3434 !3 = !{!5, !5, i64 0}
3535 !4 = !{!"double", !1}
3636 !5 = !{!"int", !1}
3232
3333 !0 = !{!4, !4, i64 0}
3434 !1 = !{!"omnipotent char", !2}
35 !2 = !{!"Simple C/C++ TBAA", null}
35 !2 = !{!"Simple C/C++ TBAA"}
3636 !3 = !{!5, !5, i64 0}
3737 !4 = !{!"double", !1}
3838 !5 = !{!"int", !1}
6262 !7 = !{ !"foo", !0 }
6363 !8 = !{ !"bar", !0 }
6464 !9 = !{ !"foo", !0 }
65 !10 = !{ !"bar", !"different" }
65 !10 = !{ !"bar", !12 }
6666 !11 = !{ !"qux", !0}
67 !12 = !{!"different"}
0 ; RUN: not opt -instcombine < %s 2>&1 | FileCheck %s
1 ; CHECK: Cycle found in TBAA metadata.
1 ; CHECK: Access type node must be scalar
22
33 define void @test6(i32* %gi) #0 {
44 entry:
6767 !7 = !{ !"foo", !0 }
6868 !8 = !{ !"bar", !0 }
6969 !9 = !{ !"foo", !0 }
70 !10 = !{ !"bar", !"different" }
70 !10 = !{ !"bar", !12}
7171 !11 = !{ !"qux", !0}
72 !12 = !{!"different"}
126126 ; CHECK: [[TYPE_LL]] = !{!"long long", {{!.*}}}
127127 !0 = !{!6, !6, i64 0}
128128 !1 = !{!"omnipotent char", !2}
129 !2 = !{!"Simple C/C++ TBAA", null}
129 !2 = !{!"Simple C/C++ TBAA"}
130130 !3 = !{!7, !7, i64 0}
131131 !4 = !{!8, !8, i64 0}
132132 !5 = !{!9, !9, i64 0}
9090 !4 = !{!8, !8, i64 0}
9191 !5 = !{!"red", !0}
9292 !6 = !{!"blu", !0}
93 !7 = !{!"outer space"}
93 !7 = !{!"outer space", !9}
9494 !8 = !{!"brick red", !5}
95 !9 = !{!"observable universe"}
2525 ; CHECK: attributes #1 = { argmemonly nounwind }
2626 ; CHECK: attributes [[NUW]] = { nounwind }
2727
28 !0 = !{!"tbaa root", null}
28 !0 = !{!"tbaa root"}
2929 !1 = !{!3, !3, i64 0}
3030 !2 = !{!4, !4, i64 0}
3131 !3 = !{!"A", !0}
2828 ret void
2929 }
3030
31 !0 = !{!"root", null}
31 !0 = !{!"root"}
3232 !1 = !{!6, !6, i64 0}
3333 !2 = !{!7, !7, i64 0}
3434
5757
5858 !3 = !{!"pointer", !8}
5959 !4 = !{!8, !8, i64 0}
60 !5 = !{!9, !9, i64 0}
60 !5 = !{!10, !10, i64 0}
6161 !6 = !{!"pointer", !0}
6262 !7 = !{!"double", !0}
6363 !8 = !{!"char", !9}
64 !9 = !{!"root", null}
64 !9 = !{!"root"}
65 !10 = !{!"scalar-type", !9}
1919
2020 ; CHECK: [[TAGA]] = !{[[TYPEA:!.*]], [[TYPEA]], i64 0}
2121 ; CHECK: [[TYPEA]] = !{!"A", !{{.*}}}
22 !0 = !{!"tbaa root", null}
22 !0 = !{!"tbaa root"}
2323 !1 = !{!3, !3, i64 0}
2424 !2 = !{!4, !4, i64 0}
2525 !3 = !{!"A", !0}
4848
4949 attributes #0 = { nounwind readonly }
5050
51 !0 = !{!"const", null, i32 1}
51 !0 = !{!"const", !1, i32 1}
52 !1 = !{!"tbaa root"}
411411 attributes #2 = { nounwind readonly }
412412
413413 !0 = !{!1, !1, i64 0, i32 1}
414 !1 = !{!"const", null}
414 !1 = !{!"const", !3}
415415 !2 = !{!1, !1, i64 0}
416 !3 = !{!"tbaa root"}
4848 attributes #1 = { nounwind readnone }
4949
5050 !0 = !{!1, !1, i64 0, i32 1}
51 !1 = !{!"const", null}
51 !1 = !{!"const", !2}
52 !2 = !{!"tbaa root"}
5656 attributes #1 = { nounwind readnone }
5757
5858 !0 = !{!1, !1, i64 0, i32 1}
59 !1 = !{!"const", null}
59 !1 = !{!"const", !2}
60 !2 = !{!"tbaa root"}
16181618 attributes #3 = { nounwind readonly }
16191619
16201620 !0 = !{!1, !1, i64 0, i32 1}
1621 !1 = !{!"const", null}
1621 !1 = !{!"const", !2}
1622 !2 = !{!"tbaa root"}
4141 attributes #1 = { nounwind readnone }
4242
4343 !0 = !{!1, !1, i64 0, i32 1}
44 !1 = !{!"const", null}
44 !1 = !{!"const", !3}
4545 !2 = !{!1, !1, i64 0}
46 !3 = !{!"tbaa root"}
496496 attributes #1 = { nounwind readnone }
497497
498498 !0 = !{!1, !1, i64 0, i32 1}
499 !1 = !{!"const", null}
499 !1 = !{!"const", !2}
500 !2 = !{!"tbaa root"}
8181 attributes #2 = { nounwind readnone }
8282
8383 !0 = !{!1, !1, i64 0, i32 1}
84 !1 = !{!"const", null}
84 !1 = !{!"const", !2}
85 !2 = !{!"tbaa root"}
8282
8383 !0 = !{!"any pointer", !1}
8484 !1 = !{!"omnipotent char", !2}
85 !2 = !{!"Simple C/C++ TBAA", null}
85 !2 = !{!"Simple C/C++ TBAA"}
8686 !3 = !{!"bool", !1}
8787 !4 = !{!"int", !1}
6565
6666 !0 = !{!5, !5, i64 0}
6767 !1 = !{!"omnipotent char", !2}
68 !2 = !{!"Simple C/C++ TBAA", null}
68 !2 = !{!"Simple C/C++ TBAA"}
6969 !3 = !{!6, !6, i64 0}
7070 !4 = !{i32 156132, i32 156164, i32 156205, i32 156238, i32 156282, i32 156332, i32 156370, i32 156408, i32 156447, i32 156486, i32 156536, i32 156574, i32 156612, i32 156651, i32 156690, i32 156740, i32 156778, i32 156816, i32 156855, i32 156894, i32 156944, i32 156982, i32 157020, i32 157059, i32 157098, i32 157148, i32 157186, i32 157224, i32 157263, i32 157302, i32 157352, i32 157390, i32 157428, i32 157467, i32 157506, i32 157556, i32 157594, i32 157632, i32 157671, i32 157710, i32 157760, i32 157798, i32 157836, i32 157875, i32 157914, i32 157952, i32 157996, i32 158046, i32 158099, i32 158140, i32 158179, i32 158218, i32 158268, i32 158321, i32 158362, i32 158401, i32 158440, i32 158490, i32 158543, i32 158584, i32 158623, i32 158662, i32 158712, i32 158765, i32 158806, i32 158845, i32 158884, i32 158922, i32 158963, i32 158996, i32 159029, i32 159062, i32 159109, i32 159154, i32 159199, i32 159243, i32 159286, i32 159329, i32 159375, i32 159422, i32 159478, i32 159522, i32 159566}
7171 !5 = !{!"any pointer", !1}
5353 ; CHECK: = load
5454 ; CHECK: ret void
5555
56 !0 = !{!"Simple C/C++ TBAA", null}
56 !0 = !{!"Simple C/C++ TBAA"}
5757 !1 = !{!"vtable pointer", !0}
5858 !2 = !{!1, !1, i64 0}
88 ret i8 %0
99 }
1010 !0 = !{!2, !2, i64 0}
11 !1 = !{!"Simple C/C++ TBAA", null}
11 !1 = !{!"Simple C/C++ TBAA"}
1212 !2 = !{!"vtable pointer", !1}
3535 }
3636
3737 !0 = !{!2, !2, i64 0}
38 !1 = !{!"Simple C/C++ TBAA", null}
38 !1 = !{!"Simple C/C++ TBAA"}
3939 !2 = !{!"vtable pointer", !1}
2626
2727 !0 = !{!3, !3, i64 0}
2828 !1 = !{!"omnipotent char", !2}
29 !2 = !{!"Simple C/C++ TBAA", null}
29 !2 = !{!"Simple C/C++ TBAA"}
3030 !3 = !{!"short", !1}
112112 ; CHECK: [[TAGA]] = !{[[TYPEA]], [[TYPEA]], i64 0}
113113 !0 = !{!5, !5, i64 0}
114114 !1 = !{!6, !6, i64 0}
115 !2 = !{!"tbaa root", null}
115 !2 = !{!"tbaa root"}
116116 !3 = !{!7, !7, i64 0}
117 !4 = !{!8, !8, i64 0}
117 !4 = !{!11, !11, i64 0}
118118 !5 = !{!"C", !6}
119119 !6 = !{!"A", !2}
120120 !7 = !{!"B", !6}
121 !8 = !{!"another root", null}
121 !8 = !{!"another root"}
122 !11 = !{!"scalar type", !8}
122123
123124
124125 ;; A TBAA structure who's only point is to have a constant location
2525 !5 = !{!3}
2626 !6 = !{!4}
2727 !7 = !{ !"tbaa root" }
28 !8 = !{ !7, !7, i64 0 }
28 !8 = !{ !9, !9, i64 0 }
29 !9 = !{ !"scalar type", !7}
1414 define i32 @test_load_cast_combine_noalias(float* %ptr) {
1515 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves no-alias metadata.
1616 ; CHECK-LABEL: @test_load_cast_combine_noalias(
17 ; CHECK: load i32, i32* %{{.*}}, !alias.scope !2, !noalias !1
17 ; CHECK: load i32, i32* %{{.*}}, !alias.scope !3, !noalias !4
1818 entry:
19 %l = load float, float* %ptr, !alias.scope !2, !noalias !1
19 %l = load float, float* %ptr, !alias.scope !3, !noalias !4
2020 %c = bitcast float %l to i32
2121 ret i32 %c
2222 }
3030 ; CHECK-NOT: !range
3131 ; CHECK: ret float
3232 entry:
33 %l = load i32, i32* %ptr, !range !6
33 %l = load i32, i32* %ptr, !range !5
3434 %c = bitcast i32 %l to float
3535 ret float %c
3636 }
3838 define i32 @test_load_cast_combine_invariant(float* %ptr) {
3939 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves invariant metadata.
4040 ; CHECK-LABEL: @test_load_cast_combine_invariant(
41 ; CHECK: load i32, i32* %{{.*}}, !invariant.load !3
41 ; CHECK: load i32, i32* %{{.*}}, !invariant.load !5
4242 entry:
43 %l = load float, float* %ptr, !invariant.load !3
43 %l = load float, float* %ptr, !invariant.load !6
4444 %c = bitcast float %l to i32
4545 ret i32 %c
4646 }
4949 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves nontemporal
5050 ; metadata.
5151 ; CHECK-LABEL: @test_load_cast_combine_nontemporal(
52 ; CHECK: load i32, i32* %{{.*}}, !nontemporal !4
52 ; CHECK: load i32, i32* %{{.*}}, !nontemporal !6
5353 entry:
54 %l = load float, float* %ptr, !nontemporal !4
54 %l = load float, float* %ptr, !nontemporal !7
5555 %c = bitcast float %l to i32
5656 ret i32 %c
5757 }
6060 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves align
6161 ; metadata.
6262 ; CHECK-LABEL: @test_load_cast_combine_align(
63 ; CHECK: load i8*, i8** %{{.*}}, !align !5
63 ; CHECK: load i8*, i8** %{{.*}}, !align !7
6464 entry:
65 %l = load i32*, i32** %ptr, !align !5
65 %l = load i32*, i32** %ptr, !align !8
6666 %c = bitcast i32* %l to i8*
6767 ret i8* %c
6868 }
7171 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves dereferenceable
7272 ; metadata.
7373 ; CHECK-LABEL: @test_load_cast_combine_deref(
74 ; CHECK: load i8*, i8** %{{.*}}, !dereferenceable !5
74 ; CHECK: load i8*, i8** %{{.*}}, !dereferenceable !7
7575 entry:
76 %l = load i32*, i32** %ptr, !dereferenceable !5
76 %l = load i32*, i32** %ptr, !dereferenceable !8
7777 %c = bitcast i32* %l to i8*
7878 ret i8* %c
7979 }
8282 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves
8383 ; dereferenceable_or_null metadata.
8484 ; CHECK-LABEL: @test_load_cast_combine_deref_or_null(
85 ; CHECK: load i8*, i8** %{{.*}}, !dereferenceable_or_null !5
85 ; CHECK: load i8*, i8** %{{.*}}, !dereferenceable_or_null !7
8686 entry:
87 %l = load i32*, i32** %ptr, !dereferenceable_or_null !5
87 %l = load i32*, i32** %ptr, !dereferenceable_or_null !8
8888 %c = bitcast i32* %l to i8*
8989 ret i8* %c
9090 }
9393 ; Ensure (cast (load (...))) -> (load (cast (...))) preserves loop access
9494 ; metadata.
9595 ; CHECK-LABEL: @test_load_cast_combine_loop(
96 ; CHECK: load i32, i32* %{{.*}}, !llvm.mem.parallel_loop_access !1
96 ; CHECK: load i32, i32* %{{.*}}, !llvm.mem.parallel_loop_access !4
9797 entry:
9898 br label %loop
9999
101101 %i = phi i32 [ 0, %entry ], [ %i.next, %loop ]
102102 %src.gep = getelementptr inbounds float, float* %src, i32 %i
103103 %dst.gep = getelementptr inbounds i32, i32* %dst, i32 %i
104 %l = load float, float* %src.gep, !llvm.mem.parallel_loop_access !1
104 %l = load float, float* %src.gep, !llvm.mem.parallel_loop_access !4
105105 %c = bitcast float %l to i32
106106 store i32 %c, i32* %dst.gep
107107 %i.next = add i32 %i, 1
129129 ; CHECK-NOT: !nonnull
130130 ; CHECK: store i64 %[[V]], i64*
131131 entry:
132 %p = load float*, float** %ptr, !nonnull !3
132 %p = load float*, float** %ptr, !nonnull !6
133133 %gep = getelementptr float*, float** %ptr, i32 42
134134 store float* %p, float** %gep
135135 ret void
137137
138138 ; This is the metadata tuple that we reference above:
139139 ; CHECK: ![[MD]] = !{i64 1, i64 0}
140 !0 = !{ !1, !1, i64 0 }
141 !1 = !{ !1 }
142 !2 = !{ !2, !1 }
143 !3 = !{ }
144 !4 = !{ i32 1 }
145 !5 = !{ i64 8 }
146 !6 = !{ i32 0, i32 42 }
140 !0 = !{!1, !1, i64 0}
141 !1 = !{!"scalar type", !2}
142 !2 = !{!"root"}
143 !3 = distinct !{!3, !4}
144 !4 = distinct !{!4}
145 !5 = !{i32 0, i32 42}
146 !6 = !{}
147 !7 = !{i32 1}
148 !8 = !{i64 8}
1313 }
1414
1515 !0 = !{!1, !1, i64 0}
16 !1 = !{!"load_tbaa"}
16 !1 = !{!"scalar type", !2}
17 !2 = !{!"load_tbaa"}
303303
304304 !0 = !{!3, !3, i64 0}
305305 !1 = !{!"omnipotent char", !2}
306 !2 = !{!"Simple C/C++ TBAA", null}
306 !2 = !{!"Simple C/C++ TBAA"}
307307 !3 = !{!"int", !1}
308308 !4 = !{ i32 0, i32 1 }
309309 !5 = !{ i32 8, i32 10 }
3131
3232 !0 = !{!5, !5, i64 0}
3333 !1 = !{!"omnipotent char", !2}
34 !2 = !{!"Simple C/C++ TBAA", null}
34 !2 = !{!"Simple C/C++ TBAA"}
3535 !3 = !{!"short", !1}
3636 !4 = !{!6, !6, i64 0}
3737 !5 = !{!"any pointer", !1}
3131 !6 = !{!"omnipotent char", !7, i64 0}
3232 !7 = !{!"Simple C/C++ TBAA"}
3333 !8 = !{!9, !5, i64 0}
34 !9 = !{!5, i64 0, !10, i64 4}
34 !9 = !{!"some struct", !5, i64 0, !10, i64 4}
3535 !10 = !{!"int", !6, i64 0}
3636 !11 = !{!9, !10, i64 4}
None ; RUN: opt < %s -basicaa -slp-vectorizer -S -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7
0 ; RUN: opt < %s -basicaa -disable-verify -slp-vectorizer -S -mtriple=x86_64-apple-macosx10.8.0 -mcpu=corei7
11
22 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
33 target triple = "x86_64-apple-darwin13.3.0"
105105 %tmp1 = icmp eq i8 %tmp, 0
106106 br i1 %tmp1, label %bb2, label %bb1
107107 bb1:
108 %tmp3 = load i8, i8* %r, align 1, !range !2, !tbaa !1, !dbg !5
108 %tmp3 = load i8, i8* %r, align 1, !range !2, !tbaa !10, !dbg !5
109109 %tmp4 = icmp eq i8 %tmp3, 1
110110 br i1 %tmp4, label %bb2, label %bb3
111111 bb2:
119119 !llvm.dbg.cu = !{!3}
120120 !llvm.module.flags = !{!8, !9}
121121
122 !0 = !{!1, !1, i64 0}
122 !0 = !{!10, !10, i64 0}
123123 !1 = !{!"foo"}
124124 !2 = !{i8 0, i8 2}
125125 !3 = distinct !DICompileUnit(language: DW_LANG_C99, file: !7, producer: "clang", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !4)
129129 !7 = !DIFile(filename: "foo.c", directory: "/")
130130 !8 = !{i32 2, !"Dwarf Version", i32 2}
131131 !9 = !{i32 2, !"Debug Info Version", i32 3}
132 !10 = !{!"scalar type", !1}
3131 !5 = !DICompositeType(tag: DW_TAG_class_type, scope: !4, file: !1, line: 25, size: 8, align: 8)
3232 !6 = !DIExpression()
3333 !7 = !DILocation(line: 25, column: 8, scope: !4)
34 !8 = !{!9, !9, i64 0}
34 !8 = !{!10, !10, i64 0}
3535 !9 = !{i64 0}
36 !10 = !{!"scalar type", !9}
0 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
1
2 define void @f_0(i32* %ptr) {
3 ; This part checks for the easy syntactic verifier rules.
4
5 ; CHECK: Struct tag metadata must have either 3 or 4 operands
6 ; CHECK-NEXT: store i32 0, i32* %ptr, !tbaa !{{[0-9]+}}
7
8 ; CHECK: Immutability tag on struct tag metadata must be a constant
9 ; CHECK-NEXT: store i32 1, i32* %ptr, !tbaa !{{[0-9]+}}
10
11 ; CHECK: Immutability part of the struct tag metadata must be either 0 or 1
12 ; CHECK-NEXT: store i32 2, i32* %ptr, !tbaa !{{[0-9]+}}
13
14 ; CHECK: Offset must be constant integer
15 ; CHECK-NEXT: store i32 3, i32* %ptr, !tbaa !{{[0-9]+}}
16
17 ; CHECK: Malformed struct tag metadata: base and access-type should be non-null and point to Metadata nodes
18 ; CHECK-NEXT: store i32 4, i32* %ptr, !tbaa !{{[0-9]+}}
19
20 ; CHECK: Access type node must be scalar
21 ; CHECK-NEXT: store i32 5, i32* %ptr, !tbaa !{{[0-9]+}}
22
23 ; CHECK: Access bit-width not the same as description bit-width
24 ; CHECK-NEXT: store i32 6, i32* %ptr, !tbaa !{{[0-9]+}}
25
26 ; CHECK: Access type node must be scalar
27 ; CHECK-NEXT: store i32 7, i32* %ptr, !tbaa !{{[0-9]+}}
28
29 store i32 0, i32* %ptr, !tbaa !{!3, !2, i64 40, i64 0, i64 1, i64 2}
30 store i32 1, i32* %ptr, !tbaa !{!3, !2, i64 40, !"immutable"}
31 store i32 2, i32* %ptr, !tbaa !{!3, !2, i64 40, i64 4}
32 store i32 3, i32* %ptr, !tbaa !{!3, !2, !"40", i64 0}
33 store i32 4, i32* %ptr, !tbaa !{!3, null, !"40", i64 0}
34 store i32 5, i32* %ptr, !tbaa !{!3, !3, !"40", i64 0}
35 store i32 6, i32* %ptr, !tbaa !{!3, !2, i32 40, i64 0}
36 store i32 7, i32* %ptr, !tbaa !{!3, !12, i32 40, i64 0}
37 ret void
38 }
39
40
41 define void @f_1(i32* %ptr) {
42 ; This part checks for more semantic verifier rules.
43
44 ; CHECK: Cycle detected in struct path
45 ; CHECK-NEXT: store i32 0, i32* %ptr, !tbaa !{{[0-9]+}}
46
47 ; CHECK: Offset not zero at the point of scalar access
48 ; CHECK-NEXT: store i32 1, i32* %ptr, !tbaa !{{[0-9]+}}
49
50 ; CHECK: Offset not zero at the point of scalar access
51 ; CHECK-NEXT: store i32 2, i32* %ptr, !tbaa !{{[0-9]+}}
52
53 ; CHECK: Could not find TBAA parent in struct type node
54 ; CHECK-NEXT: store i32 3, i32* %ptr, !tbaa !{{[0-9]+}}
55
56 ; CHECK: Did not see access type in access path!
57 ; CHECK-NEXT: store i32 3, i32* %ptr, !tbaa !{{[0-9]+}}
58
59 ; CHECK: Invalid parent operand in scalar TBAA node
60 ; CHECK-NEXT: store i32 4, i32* %ptr, !tbaa !{{[0-9]+}}
61
62 ; CHECK: Invalid name operand in scalar TBAA node
63 ; CHECK-NEXT: store i32 5, i32* %ptr, !tbaa !{{[0-9]+}}
64
65 ; CHECK: Null operands in scalar type nodes!
66 ; CHECK-NEXT: store i32 6, i32* %ptr, !tbaa !{{[0-9]+}}
67
68 ; CHECK: Struct tag nodes must have an odd number of operands!
69 ; CHECK-NEXT:!{{[0-9]+}} = !{!"bad-struct-type-0", !{{[0-9]+}}, i64 40, !{{[0-9]+}}}
70
71 ; CHECK: Incorrect field entry in struct type node!
72 ; CHECK-NEXT: store i32 8, i32* %ptr, !tbaa !{{[0-9]+}}
73
74 ; CHECK: Bitwidth between the offsets and struct type entries must match
75 ; CHECK-NEXT: store i32 9, i32* %ptr, !tbaa !{{[0-9]+}}
76
77 ; CHECK: Offsets must be increasing!
78 ; CHECK-NEXT: store i32 10, i32* %ptr, !tbaa !{{[0-9]+}}
79
80 store i32 0, i32* %ptr, !tbaa !{!4, !2, i64 40}
81 store i32 1, i32* %ptr, !tbaa !{!3, !2, i64 45}
82 store i32 2, i32* %ptr, !tbaa !{!3, !2, i64 45}
83 store i32 3, i32* %ptr, !tbaa !{!3, !2, i64 10}
84 store i32 4, i32* %ptr, !tbaa !{!5, !5, i64 0}
85 store i32 5, i32* %ptr, !tbaa !{!6, !6, i64 0}
86 store i32 6, i32* %ptr, !tbaa !{!7, !7, i64 0}
87 store i32 7, i32* %ptr, !tbaa !{!8, !1, i64 40}
88 store i32 8, i32* %ptr, !tbaa !{!9, !1, i64 40}
89 store i32 9, i32* %ptr, !tbaa !{!10, !1, i64 40}
90 store i32 10, i32* %ptr, !tbaa !{!11, !1, i64 40}
91 ret void
92 }
93
94
95
96 !0 = !{!"root"}
97 !1 = !{!"scalar-a", !0}
98 !2 = !{!"scalar-b", !0}
99 !3 = !{!"struct-a", !2, i64 20, !1, i64 40}
100 !4 = distinct !{!"self-recursive-struct", !2, i64 20, !4, i64 40}
101 !5 = !{!"bad-scalar-0", i64 40}
102 !6 = !{i64 42, !0}
103 !7 = !{!"bad-scalar-1", null}
104 !8 = !{!"bad-struct-type-0", !1, i64 40, !1}
105 !9 = !{!"bad-struct-type-1", !1, i64 40, i64 56, !1}
106 !10 = !{!"bad-struct-type-2", !1, i64 40, !1, i32 56}
107 !11 = !{!"bad-struct-type-2", !1, i64 80, !1, i64 56}
108 !12 = !{!"bad-scalar-2", !3, i64 0}