llvm.org GIT mirror llvm / 702cc91
Move the Attributes::Builder outside of the Attributes class and into its own class named AttrBuilder. No functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@165960 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 8 years ago
20 changed file(s) with 221 addition(s) and 216 deletion(s). Raw diff Collapse all Expand all
2121
2222 namespace llvm {
2323
24 class AttrBuilder;
25 class AttributesImpl;
2426 class LLVMContext;
2527 class Type;
26
27 /// AttributeImpl - The internal representation of the Attributes class. This is
28 /// uniquified.
29 class AttributesImpl;
3028
3129 /// Attributes - A bitset of attributes.
3230 class Attributes {
10098
10199 /// get - Return a uniquified Attributes object. This takes the uniquified
102100 /// value from the Builder and wraps it in the Attributes class.
103 class Builder;
104101 static Attributes get(LLVMContext &Context, ArrayRef Vals);
105 static Attributes get(LLVMContext &Context, Builder &B);
106
107 //===--------------------------------------------------------------------===//
108 /// Attributes::Builder - This class is used in conjunction with the
109 /// Attributes::get method to create an Attributes object. The object itself
110 /// is uniquified. The Builder's value, however, is not. So this can be used
111 /// as a quick way to test for equality, presence of attributes, etc.
112 class Builder {
113 friend class Attributes;
114 uint64_t Bits;
115 public:
116 Builder() : Bits(0) {}
117 explicit Builder(uint64_t B) : Bits(B) {}
118 Builder(const Attributes &A) : Bits(A.Raw()) {}
119 Builder(const Builder &B) : Bits(B.Bits) {}
120
121 void clear() { Bits = 0; }
122
123 /// addAttribute - Add an attribute to the builder.
124 Builder &addAttribute(Attributes::AttrVal Val);
125
126 /// removeAttribute - Remove an attribute from the builder.
127 Builder &removeAttribute(Attributes::AttrVal Val);
128
129 /// addAttribute - Add the attributes from A to the builder.
130 Builder &addAttributes(const Attributes &A);
131
132 /// removeAttribute - Remove the attributes from A from the builder.
133 Builder &removeAttributes(const Attributes &A);
134
135 /// hasAttribute - Return true if the builder has the specified attribute.
136 bool hasAttribute(Attributes::AttrVal A) const;
137
138 /// hasAttributes - Return true if the builder has IR-level attributes.
139 bool hasAttributes() const;
140
141 /// hasAttributes - Return true if the builder has any attribute that's in
142 /// the specified attribute.
143 bool hasAttributes(const Attributes &A) const;
144
145 /// hasAlignmentAttr - Return true if the builder has an alignment
146 /// attribute.
147 bool hasAlignmentAttr() const;
148
149 /// getAlignment - Retrieve the alignment attribute, if it exists.
150 uint64_t getAlignment() const;
151
152 /// getStackAlignment - Retrieve the stack alignment attribute, if it
153 /// exists.
154 uint64_t getStackAlignment() const;
155
156 /// addAlignmentAttr - This turns an int alignment (which must be a power of
157 /// 2) into the form used internally in Attributes.
158 Builder &addAlignmentAttr(unsigned Align);
159
160 /// addStackAlignmentAttr - This turns an int stack alignment (which must be
161 /// a power of 2) into the form used internally in Attributes.
162 Builder &addStackAlignmentAttr(unsigned Align);
163
164 /// addRawValue - Add the raw value to the internal representation.
165 /// N.B. This should be used ONLY for decoding LLVM bitcode!
166 Builder &addRawValue(uint64_t Val);
167
168 /// @brief Remove attributes that are used on functions only.
169 void removeFunctionOnlyAttrs() {
170 removeAttribute(Attributes::NoReturn)
171 .removeAttribute(Attributes::NoUnwind)
172 .removeAttribute(Attributes::ReadNone)
173 .removeAttribute(Attributes::ReadOnly)
174 .removeAttribute(Attributes::NoInline)
175 .removeAttribute(Attributes::AlwaysInline)
176 .removeAttribute(Attributes::OptimizeForSize)
177 .removeAttribute(Attributes::StackProtect)
178 .removeAttribute(Attributes::StackProtectReq)
179 .removeAttribute(Attributes::NoRedZone)
180 .removeAttribute(Attributes::NoImplicitFloat)
181 .removeAttribute(Attributes::Naked)
182 .removeAttribute(Attributes::InlineHint)
183 .removeAttribute(Attributes::StackAlignment)
184 .removeAttribute(Attributes::UWTable)
185 .removeAttribute(Attributes::NonLazyBind)
186 .removeAttribute(Attributes::ReturnsTwice)
187 .removeAttribute(Attributes::AddressSafety);
188 }
189
190 bool operator==(const Builder &B) {
191 return Bits == B.Bits;
192 }
193 bool operator!=(const Builder &B) {
194 return Bits != B.Bits;
195 }
196 };
102 static Attributes get(LLVMContext &Context, AttrBuilder &B);
197103
198104 /// @brief Return true if the attribute is present.
199105 bool hasAttribute(AttrVal Val) const;
263169 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
264170 /// encoding of all the LLVM attributes found in the given attribute bitset.
265171 /// Any change to this encoding is a breaking change to bitcode compatibility.
266 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs) {
267 // FIXME: It doesn't make sense to store the alignment information as an
268 // expanded out value, we should store it as a log2 value. However, we
269 // can't just change that here without breaking bitcode compatibility. If
270 // this ever becomes a problem in practice, we should introduce new tag
271 // numbers in the bitcode file and have those tags use a more efficiently
272 // encoded alignment field.
273
274 // Store the alignment in the bitcode as a 16-bit raw value instead of a
275 // 5-bit log2 encoded value. Shift the bits above the alignment up by 11
276 // bits.
277 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
278 if (Attrs.hasAttribute(Attributes::Alignment))
279 EncodedAttrs |= Attrs.getAlignment() << 16;
280 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
281 return EncodedAttrs;
282 }
172 static uint64_t encodeLLVMAttributesForBitcode(Attributes Attrs);
283173
284174 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset
285175 /// containing the LLVM attributes that have been decoded from the given
286176 /// integer. This function must stay in sync with
287177 /// 'encodeLLVMAttributesForBitcode'.
288178 static Attributes decodeLLVMAttributesForBitcode(LLVMContext &C,
289 uint64_t EncodedAttrs) {
290 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
291 // the bits above 31 down by 11 bits.
292 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
293 assert((!Alignment || isPowerOf2_32(Alignment)) &&
294 "Alignment must be a power of two.");
295
296 Attributes::Builder B(EncodedAttrs & 0xffff);
297 if (Alignment)
298 B.addAlignmentAttr(Alignment);
299 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
300 return Attributes::get(C, B);
301 }
179 uint64_t EncodedAttrs);
302180
303181 /// getAsString - The set of Attributes set in Attributes is converted to a
304182 /// string of equivalent mnemonics. This is, presumably, for writing out the
305183 /// mnemonics for the assembly writer.
306184 /// @brief Convert attribute bits to text
307185 std::string getAsString() const;
186 };
187
188 //===----------------------------------------------------------------------===//
189 /// AttrBuilder - This class is used in conjunction with the Attributes::get
190 /// method to create an Attributes object. The object itself is uniquified. The
191 /// Builder's value, however, is not. So this can be used as a quick way to test
192 /// for equality, presence of attributes, etc.
193 class AttrBuilder {
194 friend class Attributes;
195 uint64_t Bits;
196 public:
197 AttrBuilder() : Bits(0) {}
198 explicit AttrBuilder(uint64_t B) : Bits(B) {}
199 AttrBuilder(const Attributes &A) : Bits(A.Raw()) {}
200 AttrBuilder(const AttrBuilder &B) : Bits(B.Bits) {}
201
202 void clear() { Bits = 0; }
203
204 /// addAttribute - Add an attribute to the builder.
205 AttrBuilder &addAttribute(Attributes::AttrVal Val);
206
207 /// removeAttribute - Remove an attribute from the builder.
208 AttrBuilder &removeAttribute(Attributes::AttrVal Val);
209
210 /// addAttribute - Add the attributes from A to the builder.
211 AttrBuilder &addAttributes(const Attributes &A);
212
213 /// removeAttribute - Remove the attributes from A from the builder.
214 AttrBuilder &removeAttributes(const Attributes &A);
215
216 /// hasAttribute - Return true if the builder has the specified attribute.
217 bool hasAttribute(Attributes::AttrVal A) const;
218
219 /// hasAttributes - Return true if the builder has IR-level attributes.
220 bool hasAttributes() const;
221
222 /// hasAttributes - Return true if the builder has any attribute that's in the
223 /// specified attribute.
224 bool hasAttributes(const Attributes &A) const;
225
226 /// hasAlignmentAttr - Return true if the builder has an alignment attribute.
227 bool hasAlignmentAttr() const;
228
229 /// getAlignment - Retrieve the alignment attribute, if it exists.
230 uint64_t getAlignment() const;
231
232 /// getStackAlignment - Retrieve the stack alignment attribute, if it exists.
233 uint64_t getStackAlignment() const;
234
235 /// addAlignmentAttr - This turns an int alignment (which must be a power of
236 /// 2) into the form used internally in Attributes.
237 AttrBuilder &addAlignmentAttr(unsigned Align);
238
239 /// addStackAlignmentAttr - This turns an int stack alignment (which must be a
240 /// power of 2) into the form used internally in Attributes.
241 AttrBuilder &addStackAlignmentAttr(unsigned Align);
242
243 /// addRawValue - Add the raw value to the internal representation.
244 /// N.B. This should be used ONLY for decoding LLVM bitcode!
245 AttrBuilder &addRawValue(uint64_t Val);
246
247 /// @brief Remove attributes that are used on functions only.
248 void removeFunctionOnlyAttrs() {
249 removeAttribute(Attributes::NoReturn)
250 .removeAttribute(Attributes::NoUnwind)
251 .removeAttribute(Attributes::ReadNone)
252 .removeAttribute(Attributes::ReadOnly)
253 .removeAttribute(Attributes::NoInline)
254 .removeAttribute(Attributes::AlwaysInline)
255 .removeAttribute(Attributes::OptimizeForSize)
256 .removeAttribute(Attributes::StackProtect)
257 .removeAttribute(Attributes::StackProtectReq)
258 .removeAttribute(Attributes::NoRedZone)
259 .removeAttribute(Attributes::NoImplicitFloat)
260 .removeAttribute(Attributes::Naked)
261 .removeAttribute(Attributes::InlineHint)
262 .removeAttribute(Attributes::StackAlignment)
263 .removeAttribute(Attributes::UWTable)
264 .removeAttribute(Attributes::NonLazyBind)
265 .removeAttribute(Attributes::ReturnsTwice)
266 .removeAttribute(Attributes::AddressSafety);
267 }
268
269 bool operator==(const AttrBuilder &B) {
270 return Bits == B.Bits;
271 }
272 bool operator!=(const AttrBuilder &B) {
273 return Bits != B.Bits;
274 }
308275 };
309276
310277 //===----------------------------------------------------------------------===//
321288
322289 static AttributeWithIndex get(LLVMContext &C, unsigned Idx,
323290 ArrayRef Attrs) {
324 Attributes::Builder B;
291 AttrBuilder B;
325292
326293 for (ArrayRef::iterator I = Attrs.begin(),
327294 E = Attrs.end(); I != E; ++I)
177177 ///
178178 void addFnAttr(Attributes::AttrVal N) {
179179 // Function Attributes are stored at ~0 index
180 Attributes::Builder B;
180 AttrBuilder B;
181181 B.addAttribute(N);
182182 addAttribute(~0U, Attributes::get(getContext(), B));
183183 }
277277 return getParamAttributes(n).hasAttribute(Attributes::NoAlias);
278278 }
279279 void setDoesNotAlias(unsigned n) {
280 Attributes::Builder B;
280 AttrBuilder B;
281281 B.addAttribute(Attributes::NoAlias);
282282 addAttribute(n, Attributes::get(getContext(), B));
283283 }
288288 return getParamAttributes(n).hasAttribute(Attributes::NoCapture);
289289 }
290290 void setDoesNotCapture(unsigned n) {
291 Attributes::Builder B;
291 AttrBuilder B;
292292 B.addAttribute(Attributes::NoCapture);
293293 addAttribute(n, Attributes::get(getContext(), B));
294294 }
12801280 /// @brief Return true if the call should not be inlined.
12811281 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
12821282 void setIsNoInline() {
1283 Attributes::Builder B;
1283 AttrBuilder B;
12841284 B.addAttribute(Attributes::NoInline);
12851285 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
12861286 }
12901290 return hasFnAttr(Attributes::ReturnsTwice);
12911291 }
12921292 void setCanReturnTwice() {
1293 Attributes::Builder B;
1293 AttrBuilder B;
12941294 B.addAttribute(Attributes::ReturnsTwice);
12951295 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
12961296 }
13001300 return hasFnAttr(Attributes::ReadNone);
13011301 }
13021302 void setDoesNotAccessMemory() {
1303 Attributes::Builder B;
1303 AttrBuilder B;
13041304 B.addAttribute(Attributes::ReadNone);
13051305 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
13061306 }
13101310 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
13111311 }
13121312 void setOnlyReadsMemory() {
1313 Attributes::Builder B;
1313 AttrBuilder B;
13141314 B.addAttribute(Attributes::ReadOnly);
13151315 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
13161316 }
13181318 /// @brief Determine if the call cannot return.
13191319 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
13201320 void setDoesNotReturn() {
1321 Attributes::Builder B;
1321 AttrBuilder B;
13221322 B.addAttribute(Attributes::NoReturn);
13231323 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
13241324 }
13261326 /// @brief Determine if the call cannot unwind.
13271327 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
13281328 void setDoesNotThrow() {
1329 Attributes::Builder B;
1329 AttrBuilder B;
13301330 B.addAttribute(Attributes::NoUnwind);
13311331 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
13321332 }
30353035 /// @brief Return true if the call should not be inlined.
30363036 bool isNoInline() const { return hasFnAttr(Attributes::NoInline); }
30373037 void setIsNoInline() {
3038 Attributes::Builder B;
3038 AttrBuilder B;
30393039 B.addAttribute(Attributes::NoInline);
30403040 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
30413041 }
30453045 return hasFnAttr(Attributes::ReadNone);
30463046 }
30473047 void setDoesNotAccessMemory() {
3048 Attributes::Builder B;
3048 AttrBuilder B;
30493049 B.addAttribute(Attributes::ReadNone);
30503050 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
30513051 }
30553055 return doesNotAccessMemory() || hasFnAttr(Attributes::ReadOnly);
30563056 }
30573057 void setOnlyReadsMemory() {
3058 Attributes::Builder B;
3058 AttrBuilder B;
30593059 B.addAttribute(Attributes::ReadOnly);
30603060 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
30613061 }
30633063 /// @brief Determine if the call cannot return.
30643064 bool doesNotReturn() const { return hasFnAttr(Attributes::NoReturn); }
30653065 void setDoesNotReturn() {
3066 Attributes::Builder B;
3066 AttrBuilder B;
30673067 B.addAttribute(Attributes::NoReturn);
30683068 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
30693069 }
30713071 /// @brief Determine if the call cannot unwind.
30723072 bool doesNotThrow() const { return hasFnAttr(Attributes::NoUnwind); }
30733073 void setDoesNotThrow() {
3074 Attributes::Builder B;
3074 AttrBuilder B;
30753075 B.addAttribute(Attributes::NoUnwind);
30763076 addAttribute(AttrListPtr::FunctionIndex, Attributes::get(getContext(), B));
30773077 }
18021802 * Set the alignment for a function parameter.
18031803 *
18041804 * @see llvm::Argument::addAttr()
1805 * @see llvm::Attributes::Builder::addAlignmentAttr()
1805 * @see llvm::AttrBuilder::addAlignmentAttr()
18061806 */
18071807 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align);
18081808
915915 /// ParseOptionalAttrs - Parse a potentially empty attribute list. AttrKind
916916 /// indicates what kind of attribute list this is: 0: function arg, 1: result,
917917 /// 2: function attr.
918 bool LLParser::ParseOptionalAttrs(Attributes::Builder &B, unsigned AttrKind) {
918 bool LLParser::ParseOptionalAttrs(AttrBuilder &B, unsigned AttrKind) {
919919 LocTy AttrLoc = Lex.getLoc();
920920 bool HaveError = false;
921921
14341434 // Parse the argument.
14351435 LocTy ArgLoc;
14361436 Type *ArgTy = 0;
1437 Attributes::Builder ArgAttrs;
1437 AttrBuilder ArgAttrs;
14381438 Value *V;
14391439 if (ParseType(ArgTy, ArgLoc))
14401440 return true;
14751475 } else {
14761476 LocTy TypeLoc = Lex.getLoc();
14771477 Type *ArgTy = 0;
1478 Attributes::Builder Attrs;
1478 AttrBuilder Attrs;
14791479 std::string Name;
14801480
14811481 if (ParseType(ArgTy) ||
26762676 unsigned Linkage;
26772677
26782678 unsigned Visibility;
2679 Attributes::Builder RetAttrs;
2679 AttrBuilder RetAttrs;
26802680 CallingConv::ID CC;
26812681 Type *RetType = 0;
26822682 LocTy RetTypeLoc = Lex.getLoc();
27402740
27412741 SmallVector ArgList;
27422742 bool isVarArg;
2743 Attributes::Builder FuncAttrs;
2743 AttrBuilder FuncAttrs;
27442744 std::string Section;
27452745 unsigned Alignment;
27462746 std::string GC;
32613261 /// OptionalAttrs 'to' TypeAndValue 'unwind' TypeAndValue
32623262 bool LLParser::ParseInvoke(Instruction *&Inst, PerFunctionState &PFS) {
32633263 LocTy CallLoc = Lex.getLoc();
3264 Attributes::Builder RetAttrs, FnAttrs;
3264 AttrBuilder RetAttrs, FnAttrs;
32653265 CallingConv::ID CC;
32663266 Type *RetType = 0;
32673267 LocTy RetTypeLoc;
36663666 /// ParameterList OptionalAttrs
36673667 bool LLParser::ParseCall(Instruction *&Inst, PerFunctionState &PFS,
36683668 bool isTail) {
3669 Attributes::Builder RetAttrs, FnAttrs;
3669 AttrBuilder RetAttrs, FnAttrs;
36703670 CallingConv::ID CC;
36713671 Type *RetType = 0;
36723672 LocTy RetTypeLoc;
174174 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
175175 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
176176 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
177 bool ParseOptionalAttrs(Attributes::Builder &Attrs, unsigned AttrKind);
177 bool ParseOptionalAttrs(AttrBuilder &Attrs, unsigned AttrKind);
178178 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
179179 bool ParseOptionalLinkage(unsigned &Linkage) {
180180 bool HasLinkage; return ParseOptionalLinkage(Linkage, HasLinkage);
480480 }
481481
482482 for (unsigned i = 0, e = Record.size(); i != e; i += 2) {
483 Attributes::Builder B(Record[i+1]);
483 AttrBuilder B(Record[i+1]);
484484 if (B.hasAttributes())
485485 Attrs.push_back(AttributeWithIndex::get(Record[i],
486486 Attributes::get(Context, B)));
313313 // the return. Ignore noalias because it doesn't affect the call sequence.
314314 const Function *F = ExitBB->getParent();
315315 Attributes CallerRetAttr = F->getAttributes().getRetAttributes();
316 if (Attributes::Builder(CalleeRetAttr).removeAttribute(Attributes::NoAlias) !=
317 Attributes::Builder(CallerRetAttr).removeAttribute(Attributes::NoAlias))
316 if (AttrBuilder(CalleeRetAttr).removeAttribute(Attributes::NoAlias) !=
317 AttrBuilder(CallerRetAttr).removeAttribute(Attributes::NoAlias))
318318 return false;
319319
320320 // It's not safe to eliminate the sign / zero extension of the return value.
355355 // Conservatively require the attributes of the call to match those of
356356 // the return. Ignore noalias because it doesn't affect the call sequence.
357357 Attributes CallerRetAttr = F->getAttributes().getRetAttributes();
358 if (Attributes::Builder(CallerRetAttr)
358 if (AttrBuilder(CallerRetAttr)
359359 .removeAttribute(Attributes::NoAlias).hasAttributes())
360360 return false;
361361
473473 Out << "AttributeWithIndex PAWI;"; nl(Out);
474474 for (unsigned i = 0; i < PAL.getNumSlots(); ++i) {
475475 unsigned index = PAL.getSlot(i).Index;
476 Attributes::Builder attrs(PAL.getSlot(i).Attrs);
476 AttrBuilder attrs(PAL.getSlot(i).Attrs);
477477 Out << "PAWI.Index = " << index << "U;\n";
478 Out << " Attributes::Builder B;\n";
478 Out << " AttrBuilder B;\n";
479479
480480 #define HANDLE_ATTR(X) \
481481 if (attrs.hasAttribute(Attributes::X)) \
764764 // required when new return value attributes are added.
765765 if (NRetTy->isVoidTy())
766766 RAttrs =
767 Attributes::get(NRetTy->getContext(), Attributes::Builder(RAttrs).
767 Attributes::get(NRetTy->getContext(), AttrBuilder(RAttrs).
768768 removeAttributes(Attributes::typeIncompatible(NRetTy)));
769769 else
770 assert(!Attributes::Builder(RAttrs).
770 assert(!AttrBuilder(RAttrs).
771771 hasAttributes(Attributes::typeIncompatible(NRetTy)) &&
772772 "Return attributes no longer compatible?");
773773
839839 Attributes FnAttrs = CallPAL.getFnAttributes();
840840 // Adjust in case the function was changed to return void.
841841 RAttrs =
842 Attributes::get(NF->getContext(), Attributes::Builder(RAttrs).
842 Attributes::get(NF->getContext(), AttrBuilder(RAttrs).
843843 removeAttributes(Attributes::typeIncompatible(NF->getReturnType())));
844844 if (RAttrs.hasAttributes())
845845 AttributesVec.push_back(AttributeWithIndex::get(AttrListPtr::ReturnIndex,
211211 MadeChange = true;
212212
213213 // Clear out any existing attributes.
214 Attributes::Builder B;
214 AttrBuilder B;
215215 B.addAttribute(Attributes::ReadOnly)
216216 .addAttribute(Attributes::ReadNone);
217217 F->removeAttribute(AttrListPtr::FunctionIndex,
356356
357357 ArgumentGraph AG;
358358
359 Attributes::Builder B;
359 AttrBuilder B;
360360 B.addAttribute(Attributes::NoCapture);
361361
362362 // Check each function in turn, determining which pointer arguments are not
20612061 }
20622062
20632063 static AttrListPtr StripNest(LLVMContext &C, const AttrListPtr &Attrs) {
2064 Attributes::Builder B;
2064 AttrBuilder B;
20652065 B.addAttribute(Attributes::Nest);
20662066
20672067 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
136136 // If the SCC doesn't unwind or doesn't throw, note this fact.
137137 if (!SCCMightUnwind || !SCCMightReturn)
138138 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
139 Attributes::Builder NewAttributes;
139 AttrBuilder NewAttributes;
140140
141141 if (!SCCMightUnwind)
142142 NewAttributes.addAttribute(Attributes::NoUnwind);
10061006 return false; // Cannot transform this return value.
10071007
10081008 if (!CallerPAL.isEmpty() && !Caller->use_empty()) {
1009 Attributes::Builder RAttrs = CallerPAL.getRetAttributes();
1009 AttrBuilder RAttrs = CallerPAL.getRetAttributes();
10101010 if (RAttrs.hasAttributes(Attributes::typeIncompatible(NewRetTy)))
10111011 return false; // Attribute not compatible with transformed value.
10121012 }
10371037 return false; // Cannot transform this parameter value.
10381038
10391039 Attributes Attrs = CallerPAL.getParamAttributes(i + 1);
1040 if (Attributes::Builder(Attrs).
1040 if (AttrBuilder(Attrs).
10411041 hasAttributes(Attributes::typeIncompatible(ParamTy)))
10421042 return false; // Attribute not compatible with transformed value.
10431043
11081108 attrVec.reserve(NumCommonArgs);
11091109
11101110 // Get any return attributes.
1111 Attributes::Builder RAttrs = CallerPAL.getRetAttributes();
1111 AttrBuilder RAttrs = CallerPAL.getRetAttributes();
11121112
11131113 // If the return value is not being used, the type may not be compatible
11141114 // with the existing attributes. Wipe out any problematic attributes.
773773 // Conservatively require the attributes of the call to match those of the
774774 // return. Ignore noalias because it doesn't affect the call sequence.
775775 Attributes CalleeRetAttr = CS.getAttributes().getRetAttributes();
776 if (Attributes::Builder(CalleeRetAttr).
776 if (AttrBuilder(CalleeRetAttr).
777777 removeAttribute(Attributes::NoAlias) !=
778 Attributes::Builder(CallerRetAttr).
778 AttrBuilder(CallerRetAttr).
779779 removeAttribute(Attributes::NoAlias))
780780 continue;
781781
17871787 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
17881788 Type *Params[] = { I8X };
17891789 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
1790 Attributes::Builder B;
1790 AttrBuilder B;
17911791 B.addAttribute(Attributes::NoUnwind);
17921792 AttrListPtr Attributes =
17931793 AttrListPtr().addAttr(M->getContext(), AttrListPtr::FunctionIndex,
18051805 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
18061806 Type *Params[] = { I8X };
18071807 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
1808 Attributes::Builder B;
1808 AttrBuilder B;
18091809 B.addAttribute(Attributes::NoUnwind);
18101810 AttrListPtr Attributes =
18111811 AttrListPtr().addAttr(M->getContext(), AttrListPtr::FunctionIndex,
18211821 if (!ReleaseCallee) {
18221822 LLVMContext &C = M->getContext();
18231823 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1824 Attributes::Builder B;
1824 AttrBuilder B;
18251825 B.addAttribute(Attributes::NoUnwind);
18261826 AttrListPtr Attributes =
18271827 AttrListPtr().addAttr(M->getContext(), AttrListPtr::FunctionIndex,
18391839 if (!RetainCallee) {
18401840 LLVMContext &C = M->getContext();
18411841 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1842 Attributes::Builder B;
1842 AttrBuilder B;
18431843 B.addAttribute(Attributes::NoUnwind);
18441844 AttrListPtr Attributes =
18451845 AttrListPtr().addAttr(M->getContext(), AttrListPtr::FunctionIndex,
18721872 if (!AutoreleaseCallee) {
18731873 LLVMContext &C = M->getContext();
18741874 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
1875 Attributes::Builder B;
1875 AttrBuilder B;
18761876 B.addAttribute(Attributes::NoUnwind);
18771877 AttrListPtr Attributes =
18781878 AttrListPtr().addAttr(M->getContext(), AttrListPtr::FunctionIndex,
38493849 Type *I8XX = PointerType::getUnqual(I8X);
38503850 Type *Params[] = { I8XX, I8X };
38513851
3852 Attributes::Builder BNoUnwind;
3852 AttrBuilder BNoUnwind;
38533853 BNoUnwind.addAttribute(Attributes::NoUnwind);
3854 Attributes::Builder BNoCapture;
3854 AttrBuilder BNoCapture;
38553855 BNoCapture.addAttribute(Attributes::NoCapture);
38563856 AttrListPtr Attributes = AttrListPtr()
38573857 .addAttr(M->getContext(), AttrListPtr::FunctionIndex,
38733873 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
38743874 Type *Params[] = { I8X };
38753875 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
3876 Attributes::Builder B;
3876 AttrBuilder B;
38773877 B.addAttribute(Attributes::NoUnwind);
38783878 AttrListPtr Attributes =
38793879 AttrListPtr().addAttr(M->getContext(), AttrListPtr::FunctionIndex,
38903890 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
38913891 Type *Params[] = { I8X };
38923892 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
3893 Attributes::Builder B;
3893 AttrBuilder B;
38943894 B.addAttribute(Attributes::NoUnwind);
38953895 AttrListPtr Attributes =
38963896 AttrListPtr().addAttr(M->getContext(), AttrListPtr::FunctionIndex,
347347 if (isa(EndPtr)) {
348348 // With a null EndPtr, this function won't capture the main argument.
349349 // It would be readonly too, except that it still may write to errno.
350 Attributes::Builder B;
350 AttrBuilder B;
351351 B.addAttribute(Attributes::NoCapture);
352352 CI->addAttribute(1, Attributes::get(Callee->getContext(), B));
353353 }
3232 Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {}
3333
3434 Attributes Attributes::get(LLVMContext &Context, ArrayRef Vals) {
35 Attributes::Builder B;
35 AttrBuilder B;
3636 for (ArrayRef::iterator I = Vals.begin(), E = Vals.end();
3737 I != E; ++I)
3838 B.addAttribute(*I);
3939 return Attributes::get(Context, B);
4040 }
4141
42 Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) {
42 Attributes Attributes::get(LLVMContext &Context, AttrBuilder &B) {
4343 // If there are no attributes, return an empty Attributes class.
4444 if (B.Bits == 0)
4545 return Attributes();
9595 }
9696
9797 Attributes Attributes::typeIncompatible(Type *Ty) {
98 Attributes::Builder Incompatible;
98 AttrBuilder Incompatible;
9999
100100 if (!Ty->isIntegerTy())
101101 // Attributes that only apply to integers.
111111 .addAttribute(Attributes::StructRet);
112112
113113 return Attributes::get(Ty->getContext(), Incompatible);
114 }
115
116 /// encodeLLVMAttributesForBitcode - This returns an integer containing an
117 /// encoding of all the LLVM attributes found in the given attribute bitset.
118 /// Any change to this encoding is a breaking change to bitcode compatibility.
119 uint64_t Attributes::encodeLLVMAttributesForBitcode(Attributes Attrs) {
120 // FIXME: It doesn't make sense to store the alignment information as an
121 // expanded out value, we should store it as a log2 value. However, we can't
122 // just change that here without breaking bitcode compatibility. If this ever
123 // becomes a problem in practice, we should introduce new tag numbers in the
124 // bitcode file and have those tags use a more efficiently encoded alignment
125 // field.
126
127 // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
128 // log2 encoded value. Shift the bits above the alignment up by 11 bits.
129 uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
130 if (Attrs.hasAttribute(Attributes::Alignment))
131 EncodedAttrs |= Attrs.getAlignment() << 16;
132 EncodedAttrs |= (Attrs.Raw() & (0xfffULL << 21)) << 11;
133 return EncodedAttrs;
134 }
135
136 /// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
137 /// the LLVM attributes that have been decoded from the given integer. This
138 /// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
139 Attributes Attributes::decodeLLVMAttributesForBitcode(LLVMContext &C,
140 uint64_t EncodedAttrs) {
141 // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
142 // the bits above 31 down by 11 bits.
143 unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
144 assert((!Alignment || isPowerOf2_32(Alignment)) &&
145 "Alignment must be a power of two.");
146
147 AttrBuilder B(EncodedAttrs & 0xffff);
148 if (Alignment)
149 B.addAlignmentAttr(Alignment);
150 B.addRawValue((EncodedAttrs & (0xfffULL << 32)) >> 11);
151 return Attributes::get(C, B);
114152 }
115153
116154 std::string Attributes::getAsString() const {
182220 }
183221
184222 //===----------------------------------------------------------------------===//
185 // Attributes::Builder Implementation
186 //===----------------------------------------------------------------------===//
187
188 Attributes::Builder &Attributes::Builder::addAttribute(Attributes::AttrVal Val){
223 // AttrBuilder Implementation
224 //===----------------------------------------------------------------------===//
225
226 AttrBuilder &AttrBuilder::addAttribute(Attributes::AttrVal Val){
189227 Bits |= AttributesImpl::getAttrMask(Val);
190228 return *this;
191229 }
192230
193 Attributes::Builder &Attributes::Builder::addRawValue(uint64_t Val) {
231 AttrBuilder &AttrBuilder::addRawValue(uint64_t Val) {
194232 Bits |= Val;
195233 return *this;
196234 }
197235
198 Attributes::Builder &Attributes::Builder::addAlignmentAttr(unsigned Align) {
236 AttrBuilder &AttrBuilder::addAlignmentAttr(unsigned Align) {
199237 if (Align == 0) return *this;
200238 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
201239 assert(Align <= 0x40000000 && "Alignment too large.");
202240 Bits |= (Log2_32(Align) + 1) << 16;
203241 return *this;
204242 }
205 Attributes::Builder &Attributes::Builder::addStackAlignmentAttr(unsigned Align){
243 AttrBuilder &AttrBuilder::addStackAlignmentAttr(unsigned Align){
206244 // Default alignment, allow the target to define how to align it.
207245 if (Align == 0) return *this;
208246 assert(isPowerOf2_32(Align) && "Alignment must be a power of two.");
211249 return *this;
212250 }
213251
214 Attributes::Builder &Attributes::Builder::
252 AttrBuilder &AttrBuilder::
215253 removeAttribute(Attributes::AttrVal Val) {
216254 Bits &= ~AttributesImpl::getAttrMask(Val);
217255 return *this;
218256 }
219257
220 Attributes::Builder &Attributes::Builder::addAttributes(const Attributes &A) {
258 AttrBuilder &AttrBuilder::addAttributes(const Attributes &A) {
221259 Bits |= A.Raw();
222260 return *this;
223261 }
224262
225 Attributes::Builder &Attributes::Builder::removeAttributes(const Attributes &A){
263 AttrBuilder &AttrBuilder::removeAttributes(const Attributes &A){
226264 Bits &= ~A.Raw();
227265 return *this;
228266 }
229267
230 bool Attributes::Builder::hasAttribute(Attributes::AttrVal A) const {
268 bool AttrBuilder::hasAttribute(Attributes::AttrVal A) const {
231269 return Bits & AttributesImpl::getAttrMask(A);
232270 }
233271
234 bool Attributes::Builder::hasAttributes() const {
272 bool AttrBuilder::hasAttributes() const {
235273 return Bits != 0;
236274 }
237 bool Attributes::Builder::hasAttributes(const Attributes &A) const {
275 bool AttrBuilder::hasAttributes(const Attributes &A) const {
238276 return Bits & A.Raw();
239277 }
240 bool Attributes::Builder::hasAlignmentAttr() const {
278 bool AttrBuilder::hasAlignmentAttr() const {
241279 return Bits & AttributesImpl::getAttrMask(Attributes::Alignment);
242280 }
243281
244 uint64_t Attributes::Builder::getAlignment() const {
282 uint64_t AttrBuilder::getAlignment() const {
245283 if (!hasAlignmentAttr())
246284 return 0;
247285 return 1U <<
248286 (((Bits & AttributesImpl::getAttrMask(Attributes::Alignment)) >> 16) - 1);
249287 }
250288
251 uint64_t Attributes::Builder::getStackAlignment() const {
289 uint64_t AttrBuilder::getStackAlignment() const {
252290 if (!hasAlignmentAttr())
253291 return 0;
254292 return 1U <<
496534 "Attempt to change alignment!");
497535 #endif
498536
499 Attributes::Builder NewAttrs =
500 Attributes::Builder(OldAttrs).addAttributes(Attrs);
501 if (NewAttrs == Attributes::Builder(OldAttrs))
537 AttrBuilder NewAttrs =
538 AttrBuilder(OldAttrs).addAttributes(Attrs);
539 if (NewAttrs == AttrBuilder(OldAttrs))
502540 return *this;
503541
504542 SmallVector NewAttrList;
514552 // If there are attributes already at this index, merge them in.
515553 if (i != e && OldAttrList[i].Index == Idx) {
516554 Attrs =
517 Attributes::get(C, Attributes::Builder(Attrs).
555 Attributes::get(C, AttrBuilder(Attrs).
518556 addAttributes(OldAttrList[i].Attrs));
519557 ++i;
520558 }
540578 if (AttrList == 0) return AttrListPtr();
541579
542580 Attributes OldAttrs = getAttributes(Idx);
543 Attributes::Builder NewAttrs =
544 Attributes::Builder(OldAttrs).removeAttributes(Attrs);
545 if (NewAttrs == Attributes::Builder(OldAttrs))
581 AttrBuilder NewAttrs =
582 AttrBuilder(OldAttrs).removeAttributes(Attrs);
583 if (NewAttrs == AttrBuilder(OldAttrs))
546584 return *this;
547585
548586 SmallVector NewAttrList;
555593
556594 // If there are attributes already at this index, merge them in.
557595 assert(OldAttrList[i].Index == Idx && "Attribute isn't set?");
558 Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs).
596 Attrs = Attributes::get(C, AttrBuilder(OldAttrList[i].Attrs).
559597 removeAttributes(Attrs));
560598 ++i;
561599 if (Attrs.hasAttributes()) // If any attributes left for this param, add them.
13801380 void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
13811381 Function *Func = unwrap(Fn);
13821382 const AttrListPtr PAL = Func->getAttributes();
1383 Attributes::Builder B(PA);
1383 AttrBuilder B(PA);
13841384 const AttrListPtr PALnew =
13851385 PAL.addAttr(Func->getContext(), AttrListPtr::FunctionIndex,
13861386 Attributes::get(Func->getContext(), B));
13901390 void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) {
13911391 Function *Func = unwrap(Fn);
13921392 const AttrListPtr PAL = Func->getAttributes();
1393 Attributes::Builder B(PA);
1393 AttrBuilder B(PA);
13941394 const AttrListPtr PALnew =
13951395 PAL.removeAttr(Func->getContext(), AttrListPtr::FunctionIndex,
13961396 Attributes::get(Func->getContext(), B));
14641464
14651465 void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
14661466 Argument *A = unwrap(Arg);
1467 Attributes::Builder B(PA);
1467 AttrBuilder B(PA);
14681468 A->addAttr(Attributes::get(A->getContext(), B));
14691469 }
14701470
14711471 void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) {
14721472 Argument *A = unwrap(Arg);
1473 Attributes::Builder B(PA);
1473 AttrBuilder B(PA);
14741474 A->removeAttr(Attributes::get(A->getContext(), B));
14751475 }
14761476
14831483
14841484
14851485 void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) {
1486 Attributes::Builder B;
1486 AttrBuilder B;
14871487 B.addAlignmentAttr(align);
14881488 unwrap(Arg)->addAttr(Attributes::
14891489 get(unwrap(Arg)->getContext(), B));
16751675 void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index,
16761676 LLVMAttribute PA) {
16771677 CallSite Call = CallSite(unwrap(Instr));
1678 Attributes::Builder B(PA);
1678 AttrBuilder B(PA);
16791679 Call.setAttributes(
16801680 Call.getAttributes().addAttr(Call->getContext(), index,
16811681 Attributes::get(Call->getContext(), B)));
16841684 void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index,
16851685 LLVMAttribute PA) {
16861686 CallSite Call = CallSite(unwrap(Instr));
1687 Attributes::Builder B(PA);
1687 AttrBuilder B(PA);
16881688 Call.setAttributes(
16891689 Call.getAttributes().removeAttr(Call->getContext(), index,
16901690 Attributes::get(Call->getContext(), B)));
16931693 void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index,
16941694 unsigned align) {
16951695 CallSite Call = CallSite(unwrap(Instr));
1696 Attributes::Builder B;
1696 AttrBuilder B;
16971697 B.addAlignmentAttr(align);
16981698 Call.setAttributes(Call.getAttributes().addAttr(Call->getContext(), index,
16991699 Attributes::get(Call->getContext(), B)));
566566 Attrs.hasAttribute(Attributes::AlwaysInline)), "Attributes "
567567 "'noinline and alwaysinline' are incompatible!", V);
568568
569 Assert1(!Attributes::Builder(Attrs).
569 Assert1(!AttrBuilder(Attrs).
570570 hasAttributes(Attributes::typeIncompatible(Ty)),
571571 "Wrong types for attribute: " +
572572 Attributes::typeIncompatible(Ty).getAsString(), V);
614614 }
615615
616616 Attributes FAttrs = Attrs.getFnAttributes();
617 Attributes::Builder NotFn(FAttrs);
617 AttrBuilder NotFn(FAttrs);
618618 NotFn.removeFunctionOnlyAttrs();
619619 Assert1(!NotFn.hasAttributes(), "Attributes '" +
620620 Attributes::get(V->getContext(), NotFn).getAsString() +