1 //===- llvm/unittest/IR/AttributesTest.cpp - Attributes unit tests --------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/IR/Attributes.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "gtest/gtest.h"
13 using namespace llvm;
14
15 namespace {
16
TEST(Attributes,Uniquing)17 TEST(Attributes, Uniquing) {
18 LLVMContext C;
19
20 Attribute AttrA = Attribute::get(C, Attribute::AlwaysInline);
21 Attribute AttrB = Attribute::get(C, Attribute::AlwaysInline);
22 EXPECT_EQ(AttrA, AttrB);
23
24 AttributeList ASs[] = {AttributeList::get(C, 1, Attribute::ZExt),
25 AttributeList::get(C, 2, Attribute::SExt)};
26
27 AttributeList SetA = AttributeList::get(C, ASs);
28 AttributeList SetB = AttributeList::get(C, ASs);
29 EXPECT_EQ(SetA, SetB);
30 }
31
TEST(Attributes,Ordering)32 TEST(Attributes, Ordering) {
33 LLVMContext C;
34
35 Attribute Align4 = Attribute::get(C, Attribute::Alignment, 4);
36 Attribute Align5 = Attribute::get(C, Attribute::Alignment, 5);
37 Attribute Deref4 = Attribute::get(C, Attribute::Dereferenceable, 4);
38 Attribute Deref5 = Attribute::get(C, Attribute::Dereferenceable, 5);
39 EXPECT_TRUE(Align4 < Align5);
40 EXPECT_TRUE(Align4 < Deref4);
41 EXPECT_TRUE(Align4 < Deref5);
42 EXPECT_TRUE(Align5 < Deref4);
43
44 AttributeList ASs[] = {AttributeList::get(C, 2, Attribute::ZExt),
45 AttributeList::get(C, 1, Attribute::SExt)};
46
47 AttributeList SetA = AttributeList::get(C, ASs);
48 AttributeList SetB = SetA.removeAttributes(C, 1, ASs[1].getAttributes(1));
49 EXPECT_NE(SetA, SetB);
50 }
51
TEST(Attributes,AddAttributes)52 TEST(Attributes, AddAttributes) {
53 LLVMContext C;
54 AttributeList AL;
55 AttrBuilder B;
56 B.addAttribute(Attribute::NoReturn);
57 AL = AL.addAttributes(C, AttributeList::FunctionIndex, AttributeSet::get(C, B));
58 EXPECT_TRUE(AL.hasFnAttribute(Attribute::NoReturn));
59 B.clear();
60 B.addAttribute(Attribute::SExt);
61 AL = AL.addAttributes(C, AttributeList::ReturnIndex, B);
62 EXPECT_TRUE(AL.hasAttribute(AttributeList::ReturnIndex, Attribute::SExt));
63 EXPECT_TRUE(AL.hasFnAttribute(Attribute::NoReturn));
64 }
65
TEST(Attributes,RemoveAlign)66 TEST(Attributes, RemoveAlign) {
67 LLVMContext C;
68
69 Attribute AlignAttr = Attribute::getWithAlignment(C, 8);
70 Attribute StackAlignAttr = Attribute::getWithStackAlignment(C, 32);
71 AttrBuilder B_align_readonly;
72 B_align_readonly.addAttribute(AlignAttr);
73 B_align_readonly.addAttribute(Attribute::ReadOnly);
74 AttrBuilder B_align;
75 B_align.addAttribute(AlignAttr);
76 AttrBuilder B_stackalign_optnone;
77 B_stackalign_optnone.addAttribute(StackAlignAttr);
78 B_stackalign_optnone.addAttribute(Attribute::OptimizeNone);
79 AttrBuilder B_stackalign;
80 B_stackalign.addAttribute(StackAlignAttr);
81
82 AttributeSet AS = AttributeSet::get(C, B_align_readonly);
83 EXPECT_TRUE(AS.getAlignment() == 8);
84 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
85 AS = AS.removeAttribute(C, Attribute::Alignment);
86 EXPECT_FALSE(AS.hasAttribute(Attribute::Alignment));
87 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
88 AS = AttributeSet::get(C, B_align_readonly);
89 AS = AS.removeAttributes(C, B_align);
90 EXPECT_TRUE(AS.getAlignment() == 0);
91 EXPECT_TRUE(AS.hasAttribute(Attribute::ReadOnly));
92
93 AttributeList AL;
94 AL = AL.addParamAttributes(C, 0, B_align_readonly);
95 AL = AL.addAttributes(C, 0, B_stackalign_optnone);
96 EXPECT_TRUE(AL.hasAttributes(0));
97 EXPECT_TRUE(AL.hasAttribute(0, Attribute::StackAlignment));
98 EXPECT_TRUE(AL.hasAttribute(0, Attribute::OptimizeNone));
99 EXPECT_TRUE(AL.getStackAlignment(0) == 32);
100 EXPECT_TRUE(AL.hasParamAttrs(0));
101 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::Alignment));
102 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
103 EXPECT_TRUE(AL.getParamAlignment(0) == 8);
104
105 AL = AL.removeParamAttribute(C, 0, Attribute::Alignment);
106 EXPECT_FALSE(AL.hasParamAttr(0, Attribute::Alignment));
107 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
108 EXPECT_TRUE(AL.hasAttribute(0, Attribute::StackAlignment));
109 EXPECT_TRUE(AL.hasAttribute(0, Attribute::OptimizeNone));
110 EXPECT_TRUE(AL.getStackAlignment(0) == 32);
111
112 AL = AL.removeAttribute(C, 0, Attribute::StackAlignment);
113 EXPECT_FALSE(AL.hasParamAttr(0, Attribute::Alignment));
114 EXPECT_TRUE(AL.hasParamAttr(0, Attribute::ReadOnly));
115 EXPECT_FALSE(AL.hasAttribute(0, Attribute::StackAlignment));
116 EXPECT_TRUE(AL.hasAttribute(0, Attribute::OptimizeNone));
117
118 AttributeList AL2;
119 AL2 = AL2.addParamAttributes(C, 0, B_align_readonly);
120 AL2 = AL2.addAttributes(C, 0, B_stackalign_optnone);
121
122 AL2 = AL2.removeParamAttributes(C, 0, B_align);
123 EXPECT_FALSE(AL2.hasParamAttr(0, Attribute::Alignment));
124 EXPECT_TRUE(AL2.hasParamAttr(0, Attribute::ReadOnly));
125 EXPECT_TRUE(AL2.hasAttribute(0, Attribute::StackAlignment));
126 EXPECT_TRUE(AL2.hasAttribute(0, Attribute::OptimizeNone));
127 EXPECT_TRUE(AL2.getStackAlignment(0) == 32);
128
129 AL2 = AL2.removeAttributes(C, 0, B_stackalign);
130 EXPECT_FALSE(AL2.hasParamAttr(0, Attribute::Alignment));
131 EXPECT_TRUE(AL2.hasParamAttr(0, Attribute::ReadOnly));
132 EXPECT_FALSE(AL2.hasAttribute(0, Attribute::StackAlignment));
133 EXPECT_TRUE(AL2.hasAttribute(0, Attribute::OptimizeNone));
134 }
135
TEST(Attributes,AddMatchingAlignAttr)136 TEST(Attributes, AddMatchingAlignAttr) {
137 LLVMContext C;
138 AttributeList AL;
139 AL = AL.addAttribute(C, AttributeList::FirstArgIndex,
140 Attribute::getWithAlignment(C, 8));
141 AL = AL.addAttribute(C, AttributeList::FirstArgIndex + 1,
142 Attribute::getWithAlignment(C, 32));
143 EXPECT_EQ(8U, AL.getParamAlignment(0));
144 EXPECT_EQ(32U, AL.getParamAlignment(1));
145
146 AttrBuilder B;
147 B.addAttribute(Attribute::NonNull);
148 B.addAlignmentAttr(8);
149 AL = AL.addAttributes(C, AttributeList::FirstArgIndex, B);
150 EXPECT_EQ(8U, AL.getParamAlignment(0));
151 EXPECT_EQ(32U, AL.getParamAlignment(1));
152 EXPECT_TRUE(AL.hasParamAttribute(0, Attribute::NonNull));
153 }
154
TEST(Attributes,EmptyGet)155 TEST(Attributes, EmptyGet) {
156 LLVMContext C;
157 AttributeList EmptyLists[] = {AttributeList(), AttributeList()};
158 AttributeList AL = AttributeList::get(C, EmptyLists);
159 EXPECT_TRUE(AL.isEmpty());
160 }
161
TEST(Attributes,OverflowGet)162 TEST(Attributes, OverflowGet) {
163 LLVMContext C;
164 std::pair<unsigned, Attribute> Attrs[] = { { AttributeList::ReturnIndex, Attribute::get(C, Attribute::SExt) },
165 { AttributeList::FunctionIndex, Attribute::get(C, Attribute::ReadOnly) } };
166 AttributeList AL = AttributeList::get(C, Attrs);
167 EXPECT_EQ(2U, AL.getNumAttrSets());
168 }
169
170 } // end anonymous namespace
171