llvm.org GIT mirror llvm / d11161f
Merging r168354: into 3.2 release branch Make the AttrListPtr object a part of the LLVMContext. When code deletes the context, the AttributeImpls that the AttrListPtr points to are now invalid. Therefore, instead of keeping a separate managed static for the AttrListPtrs that's reference counted, move it into the LLVMContext and delete it when deleting the AttributeImpls. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_32@168486 91177308-0d34-0410-b5e6-96231b3b80d8 Pawel Wodnicki 6 years ago
12 changed file(s) with 104 addition(s) and 126 deletion(s). Raw diff Collapse all Expand all
317317 FunctionIndex = ~0U
318318 };
319319 private:
320 /// AttrList - The attributes that we are managing. This can be null to
321 /// represent the empty attributes list.
320 /// @brief The attributes that we are managing. This can be null to represent
321 /// the empty attributes list.
322322 AttributeListImpl *AttrList;
323
324 /// @brief The attributes for the specified index are returned. Attributes
325 /// for the result are denoted with Idx = 0.
326 Attributes getAttributes(unsigned Idx) const;
327
328 explicit AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {}
323329 public:
324330 AttrListPtr() : AttrList(0) {}
325 AttrListPtr(const AttrListPtr &P);
331 AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {}
326332 const AttrListPtr &operator=(const AttrListPtr &RHS);
327 ~AttrListPtr();
328333
329334 //===--------------------------------------------------------------------===//
330335 // Attribute List Construction and Mutation
331336 //===--------------------------------------------------------------------===//
332337
333338 /// get - Return a Attributes list with the specified parameters in it.
334 static AttrListPtr get(ArrayRef Attrs);
339 static AttrListPtr get(LLVMContext &C, ArrayRef Attrs);
335340
336341 /// addAttr - Add the specified attribute at the specified index to this
337342 /// attribute list. Since attribute lists are immutable, this
418423 const AttributeWithIndex &getSlot(unsigned Slot) const;
419424
420425 void dump() const;
421
422 private:
423 explicit AttrListPtr(AttributeListImpl *L);
424
425 /// getAttributes - The attributes for the specified index are
426 /// returned. Attributes for the result are denoted with Idx = 0.
427 Attributes getAttributes(unsigned Idx) const;
428426 };
429427
430428 } // End llvm namespace
27932793 Attributes::get(RetType->getContext(),
27942794 FuncAttrs)));
27952795
2796 AttrListPtr PAL = AttrListPtr::get(Attrs);
2796 AttrListPtr PAL = AttrListPtr::get(Context, Attrs);
27972797
27982798 if (PAL.getParamAttributes(1).hasAttribute(Attributes::StructRet) &&
27992799 !RetType->isVoidTy())
33503350 FnAttrs)));
33513351
33523352 // Finish off the Attributes and check them
3353 AttrListPtr PAL = AttrListPtr::get(Attrs);
3353 AttrListPtr PAL = AttrListPtr::get(Context, Attrs);
33543354
33553355 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB, Args);
33563356 II->setCallingConv(CC);
37523752 FnAttrs)));
37533753
37543754 // Finish off the Attributes and check them
3755 AttrListPtr PAL = AttrListPtr::get(Attrs);
3755 AttrListPtr PAL = AttrListPtr::get(Context, Attrs);
37563756
37573757 CallInst *CI = CallInst::Create(Callee, Args);
37583758 CI->setTailCall(isTail);
486486 Attributes::get(Context, B)));
487487 }
488488
489 MAttributes.push_back(AttrListPtr::get(Attrs));
489 MAttributes.push_back(AttrListPtr::get(Context, Attrs));
490490 Attrs.clear();
491491 break;
492492 }
610610
611611 // Recompute the parameter attributes list based on the new arguments for
612612 // the function.
613 NF->setAttributes(AttrListPtr::get(AttributesVec));
613 NF->setAttributes(AttrListPtr::get(F->getContext(), AttributesVec));
614614 AttributesVec.clear();
615615
616616 F->getParent()->getFunctionList().insert(F, NF);
730730 New = InvokeInst::Create(NF, II->getNormalDest(), II->getUnwindDest(),
731731 Args, "", Call);
732732 cast(New)->setCallingConv(CS.getCallingConv());
733 cast(New)->setAttributes(AttrListPtr::get(AttributesVec));
733 cast(New)->setAttributes(AttrListPtr::get(II->getContext(),
734 AttributesVec));
734735 } else {
735736 New = CallInst::Create(NF, Args, "", Call);
736737 cast(New)->setCallingConv(CS.getCallingConv());
737 cast(New)->setAttributes(AttrListPtr::get(AttributesVec));
738 cast(New)->setAttributes(AttrListPtr::get(New->getContext(),
739 AttributesVec));
738740 if (cast(Call)->isTailCall())
739741 cast(New)->setTailCall();
740742 }
279279 if (FnAttrs.hasAttributes())
280280 AttributesVec.push_back(AttributeWithIndex::get(AttrListPtr::FunctionIndex,
281281 FnAttrs));
282 PAL = AttrListPtr::get(AttributesVec);
282 PAL = AttrListPtr::get(Fn.getContext(), AttributesVec);
283283 }
284284
285285 Instruction *New;
805805 FnAttrs));
806806
807807 // Reconstruct the AttributesList based on the vector we constructed.
808 AttrListPtr NewPAL = AttrListPtr::get(AttributesVec);
808 AttrListPtr NewPAL = AttrListPtr::get(F->getContext(), AttributesVec);
809809
810810 // Create the new function type based on the recomputed parameters.
811811 FunctionType *NFTy = FunctionType::get(NRetTy, Params, FTy->isVarArg());
873873 FnAttrs));
874874
875875 // Reconstruct the AttributesList based on the vector we constructed.
876 AttrListPtr NewCallPAL = AttrListPtr::get(AttributesVec);
876 AttrListPtr NewCallPAL = AttrListPtr::get(F->getContext(), AttributesVec);
877877
878878 Instruction *New;
879879 if (InvokeInst *II = dyn_cast(Call)) {
11761176 if (NewRetTy->isVoidTy())
11771177 Caller->setName(""); // Void type should not have a name.
11781178
1179 const AttrListPtr &NewCallerPAL = AttrListPtr::get(attrVec);
1179 const AttrListPtr &NewCallerPAL = AttrListPtr::get(Callee->getContext(),
1180 attrVec);
11801181
11811182 Instruction *NC;
11821183 if (InvokeInst *II = dyn_cast(Caller)) {
13541355 NestF->getType() == PointerType::getUnqual(NewFTy) ?
13551356 NestF : ConstantExpr::getBitCast(NestF,
13561357 PointerType::getUnqual(NewFTy));
1357 const AttrListPtr &NewPAL = AttrListPtr::get(NewAttrs);
1358 const AttrListPtr &NewPAL = AttrListPtr::get(FTy->getContext(), NewAttrs);
13581359
13591360 Instruction *NewCaller;
13601361 if (InvokeInst *II = dyn_cast(Caller)) {
4646 ArrayRef(AVs, 2));
4747
4848 LLVMContext &Context = B.GetInsertBlock()->getContext();
49 Constant *StrLen = M->getOrInsertFunction("strlen", AttrListPtr::get(AWI),
49 Constant *StrLen = M->getOrInsertFunction("strlen",
50 AttrListPtr::get(M->getContext(),
51 AWI),
5052 TD->getIntPtrType(Context),
5153 B.getInt8PtrTy(),
5254 NULL);
7375 ArrayRef(AVs, 2));
7476
7577 LLVMContext &Context = B.GetInsertBlock()->getContext();
76 Constant *StrNLen = M->getOrInsertFunction("strnlen", AttrListPtr::get(AWI),
78 Constant *StrNLen = M->getOrInsertFunction("strnlen",
79 AttrListPtr::get(M->getContext(),
80 AWI),
7781 TD->getIntPtrType(Context),
7882 B.getInt8PtrTy(),
7983 TD->getIntPtrType(Context),
101105
102106 Type *I8Ptr = B.getInt8PtrTy();
103107 Type *I32Ty = B.getInt32Ty();
104 Constant *StrChr = M->getOrInsertFunction("strchr", AttrListPtr::get(AWI),
108 Constant *StrChr = M->getOrInsertFunction("strchr",
109 AttrListPtr::get(M->getContext(),
110 AWI),
105111 I8Ptr, I8Ptr, I32Ty, NULL);
106112 CallInst *CI = B.CreateCall2(StrChr, CastToCStr(Ptr, B),
107113 ConstantInt::get(I32Ty, C), "strchr");
126132 ArrayRef(AVs, 2));
127133
128134 LLVMContext &Context = B.GetInsertBlock()->getContext();
129 Value *StrNCmp = M->getOrInsertFunction("strncmp", AttrListPtr::get(AWI),
135 Value *StrNCmp = M->getOrInsertFunction("strncmp",
136 AttrListPtr::get(M->getContext(),
137 AWI),
130138 B.getInt32Ty(),
131139 B.getInt8PtrTy(),
132140 B.getInt8PtrTy(),
154162 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
155163 Attributes::NoUnwind);
156164 Type *I8Ptr = B.getInt8PtrTy();
157 Value *StrCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
165 Value *StrCpy = M->getOrInsertFunction(Name,
166 AttrListPtr::get(M->getContext(), AWI),
158167 I8Ptr, I8Ptr, I8Ptr, NULL);
159168 CallInst *CI = B.CreateCall2(StrCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
160169 Name);
177186 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
178187 Attributes::NoUnwind);
179188 Type *I8Ptr = B.getInt8PtrTy();
180 Value *StrNCpy = M->getOrInsertFunction(Name, AttrListPtr::get(AWI),
189 Value *StrNCpy = M->getOrInsertFunction(Name,
190 AttrListPtr::get(M->getContext(),
191 AWI),
181192 I8Ptr, I8Ptr, I8Ptr,
182193 Len->getType(), NULL);
183194 CallInst *CI = B.CreateCall3(StrNCpy, CastToCStr(Dst, B), CastToCStr(Src, B),
202213 Attributes::NoUnwind);
203214 LLVMContext &Context = B.GetInsertBlock()->getContext();
204215 Value *MemCpy = M->getOrInsertFunction("__memcpy_chk",
205 AttrListPtr::get(AWI),
216 AttrListPtr::get(M->getContext(), AWI),
206217 B.getInt8PtrTy(),
207218 B.getInt8PtrTy(),
208219 B.getInt8PtrTy(),
230241 AWI = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
231242 ArrayRef(AVs, 2));
232243 LLVMContext &Context = B.GetInsertBlock()->getContext();
233 Value *MemChr = M->getOrInsertFunction("memchr", AttrListPtr::get(AWI),
244 Value *MemChr = M->getOrInsertFunction("memchr",
245 AttrListPtr::get(M->getContext(), AWI),
234246 B.getInt8PtrTy(),
235247 B.getInt8PtrTy(),
236248 B.getInt32Ty(),
260272 ArrayRef(AVs, 2));
261273
262274 LLVMContext &Context = B.GetInsertBlock()->getContext();
263 Value *MemCmp = M->getOrInsertFunction("memcmp", AttrListPtr::get(AWI),
275 Value *MemCmp = M->getOrInsertFunction("memcmp",
276 AttrListPtr::get(M->getContext(), AWI),
264277 B.getInt32Ty(),
265278 B.getInt8PtrTy(),
266279 B.getInt8PtrTy(),
337350 AWI[1] = AttributeWithIndex::get(M->getContext(), AttrListPtr::FunctionIndex,
338351 Attributes::NoUnwind);
339352
340 Value *PutS = M->getOrInsertFunction("puts", AttrListPtr::get(AWI),
353 Value *PutS = M->getOrInsertFunction("puts",
354 AttrListPtr::get(M->getContext(), AWI),
341355 B.getInt32Ty(),
342356 B.getInt8PtrTy(),
343357 NULL);
361375 Attributes::NoUnwind);
362376 Constant *F;
363377 if (File->getType()->isPointerTy())
364 F = M->getOrInsertFunction("fputc", AttrListPtr::get(AWI),
378 F = M->getOrInsertFunction("fputc",
379 AttrListPtr::get(M->getContext(), AWI),
365380 B.getInt32Ty(),
366381 B.getInt32Ty(), File->getType(),
367382 NULL);
395410 StringRef FPutsName = TLI->getName(LibFunc::fputs);
396411 Constant *F;
397412 if (File->getType()->isPointerTy())
398 F = M->getOrInsertFunction(FPutsName, AttrListPtr::get(AWI),
413 F = M->getOrInsertFunction(FPutsName,
414 AttrListPtr::get(M->getContext(), AWI),
399415 B.getInt32Ty(),
400416 B.getInt8PtrTy(),
401417 File->getType(), NULL);
428444 StringRef FWriteName = TLI->getName(LibFunc::fwrite);
429445 Constant *F;
430446 if (File->getType()->isPointerTy())
431 F = M->getOrInsertFunction(FWriteName, AttrListPtr::get(AWI),
447 F = M->getOrInsertFunction(FWriteName,
448 AttrListPtr::get(M->getContext(), AWI),
432449 TD->getIntPtrType(Context),
433450 B.getInt8PtrTy(),
434451 TD->getIntPtrType(Context),
354354 // AttributeListImpl Definition
355355 //===----------------------------------------------------------------------===//
356356
357 namespace llvm {
358 class AttributeListImpl;
359 }
360
361 static ManagedStatic > AttributesLists;
362
363 namespace llvm {
364 static ManagedStatic > ALMutex;
365
366 class AttributeListImpl : public FoldingSetNode {
367 sys::cas_flag RefCount;
368
369 // AttributesList is uniqued, these should not be publicly available.
370 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
371 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
372 ~AttributeListImpl(); // Private implementation
373 public:
374 SmallVector Attrs;
375
376 AttributeListImpl(ArrayRef attrs)
377 : Attrs(attrs.begin(), attrs.end()) {
378 RefCount = 0;
379 }
380
381 void AddRef() {
382 sys::SmartScopedLock Lock(*ALMutex);
383 ++RefCount;
384 }
385 void DropRef() {
386 sys::SmartScopedLock Lock(*ALMutex);
387 if (!AttributesLists.isConstructed())
388 return;
389 sys::cas_flag new_val = --RefCount;
390 if (new_val == 0)
391 delete this;
392 }
393
394 void Profile(FoldingSetNodeID &ID) const {
395 Profile(ID, Attrs);
396 }
397 static void Profile(FoldingSetNodeID &ID, ArrayRef Attrs){
398 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
399 ID.AddInteger(Attrs[i].Attrs.Raw());
400 ID.AddInteger(Attrs[i].Index);
401 }
402 }
403 };
404
405 } // end llvm namespace
406
407 AttributeListImpl::~AttributeListImpl() {
408 // NOTE: Lock must be acquired by caller.
409 AttributesLists->RemoveNode(this);
410 }
411
412 AttrListPtr AttrListPtr::get(ArrayRef Attrs) {
357 AttrListPtr AttrListPtr::get(LLVMContext &C,
358 ArrayRef Attrs) {
413359 // If there are no attributes then return a null AttributesList pointer.
414360 if (Attrs.empty())
415361 return AttrListPtr();
424370 #endif
425371
426372 // Otherwise, build a key to look up the existing attributes.
373 LLVMContextImpl *pImpl = C.pImpl;
427374 FoldingSetNodeID ID;
428375 AttributeListImpl::Profile(ID, Attrs);
429 void *InsertPos;
430
431 sys::SmartScopedLock Lock(*ALMutex);
432
433 AttributeListImpl *PAL =
434 AttributesLists->FindNodeOrInsertPos(ID, InsertPos);
376
377 void *InsertPoint;
378 AttributeListImpl *PA = pImpl->AttrsLists.FindNodeOrInsertPos(ID,
379 InsertPoint);
435380
436381 // If we didn't find any existing attributes of the same shape then
437382 // create a new one and insert it.
438 if (!PAL) {
439 PAL = new AttributeListImpl(Attrs);
440 AttributesLists->InsertNode(PAL, InsertPos);
383 if (!PA) {
384 PA = new AttributeListImpl(Attrs);
385 pImpl->AttrsLists.InsertNode(PA, InsertPoint);
441386 }
442387
443388 // Return the AttributesList that we found or created.
444 return AttrListPtr(PAL);
389 return AttrListPtr(PA);
445390 }
446391
447392 //===----------------------------------------------------------------------===//
448393 // AttrListPtr Method Implementations
449394 //===----------------------------------------------------------------------===//
450395
451 AttrListPtr::AttrListPtr(AttributeListImpl *LI) : AttrList(LI) {
452 if (LI) LI->AddRef();
453 }
454
455 AttrListPtr::AttrListPtr(const AttrListPtr &P) : AttrList(P.AttrList) {
456 if (AttrList) AttrList->AddRef();
457 }
458
459396 const AttrListPtr &AttrListPtr::operator=(const AttrListPtr &RHS) {
460 sys::SmartScopedLock Lock(*ALMutex);
461397 if (AttrList == RHS.AttrList) return *this;
462 if (AttrList) AttrList->DropRef();
398
463399 AttrList = RHS.AttrList;
464 if (AttrList) AttrList->AddRef();
465 return *this;
466 }
467
468 AttrListPtr::~AttrListPtr() {
469 if (AttrList) AttrList->DropRef();
400 return *this;
470401 }
471402
472403 /// getNumSlots - Return the number of slots used in this attribute list.
506437 for (unsigned i = 0, e = Attrs.size(); i != e; ++i)
507438 if (Attrs[i].Attrs.hasAttribute(Attr))
508439 return true;
440
509441 return false;
510442 }
511443
561493 OldAttrList.begin()+i, OldAttrList.end());
562494 }
563495
564 return get(NewAttrList);
496 return get(C, NewAttrList);
565497 }
566498
567499 AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx,
600532 NewAttrList.insert(NewAttrList.end(),
601533 OldAttrList.begin()+i, OldAttrList.end());
602534
603 return get(NewAttrList);
535 return get(C, NewAttrList);
604536 }
605537
606538 void AttrListPtr::dump() const {
1414 #ifndef LLVM_ATTRIBUTESIMPL_H
1515 #define LLVM_ATTRIBUTESIMPL_H
1616
17 #include "llvm/Attributes.h"
1718 #include "llvm/ADT/FoldingSet.h"
1819
1920 namespace llvm {
20
21 class Attributes;
2221
2322 class AttributesImpl : public FoldingSetNode {
2423 uint64_t Bits; // FIXME: We will be expanding this.
4544 }
4645 };
4746
47 class AttributeListImpl : public FoldingSetNode {
48 // AttributesList is uniqued, these should not be publicly available.
49 void operator=(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
50 AttributeListImpl(const AttributeListImpl &) LLVM_DELETED_FUNCTION;
51 public:
52 SmallVector Attrs;
53
54 AttributeListImpl(ArrayRef attrs)
55 : Attrs(attrs.begin(), attrs.end()) {}
56
57 void Profile(FoldingSetNodeID &ID) const {
58 Profile(ID, Attrs);
59 }
60 static void Profile(FoldingSetNodeID &ID, ArrayRef Attrs){
61 for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
62 ID.AddInteger(Attrs[i].Attrs.Raw());
63 ID.AddInteger(Attrs[i].Index);
64 }
65 }
66 };
67
4868 } // end llvm namespace
4969
5070 #endif
9696
9797 // Destroy attributes.
9898 for (FoldingSetIterator I = AttrsSet.begin(),
99 E = AttrsSet.end(); I != E;) {
99 E = AttrsSet.end(); I != E; ) {
100100 FoldingSetIterator Elem = I++;
101 delete &*Elem;
102 }
103
104 // Destroy attribute lists.
105 for (FoldingSetIterator I = AttrsLists.begin(),
106 E = AttrsLists.end(); I != E; ) {
107 FoldingSetIterator Elem = I++;
101108 delete &*Elem;
102109 }
103110
255255 FPMapTy FPConstants;
256256
257257 FoldingSet AttrsSet;
258
258 FoldingSet AttrsLists;
259
259260 StringMap MDStringCache;
260261
261262 FoldingSet MDNodeSet;
620620
621621 OS << " }\n";
622622 OS << " }\n";
623 OS << " return AttrListPtr::get(ArrayRef(AWI, "
623 OS << " return AttrListPtr::get(C, ArrayRef(AWI, "
624624 "NumAttrs));\n";
625625 OS << "}\n";
626626 OS << "#endif // GET_INTRINSIC_ATTRIBUTES\n\n";