llvm.org GIT mirror llvm / a82b376
[IR] Abstract away ArgNo+1 attribute indexing as much as possible Summary: Do three things to help with that: - Add AttributeList::FirstArgIndex, which is an enumerator currently set to 1. It allows us to change the indexing scheme with fewer changes. - Add addParamAttr/removeParamAttr. This just shortens addAttribute call sites that would otherwise need to spell out FirstArgIndex. - Remove some attribute-specific getters and setters from Function that take attribute list indices. Most of these were only used from BuildLibCalls, and doesNotAlias was only used to test or set if the return value is malloc-like. I'm happy to split the patch, but I think they are probably easier to review when taken together. This patch should be NFC, but it sets the stage to change the indexing scheme to this, which is more convenient when indexing into an array: 0: func attrs 1: retattrs 2...: arg attrs Reviewers: chandlerc, pete, javed.absar Subscribers: david2050, llvm-commits Differential Revision: https://reviews.llvm.org/D32811 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302060 91177308-0d34-0410-b5e6-96231b3b80d8 Reid Kleckner 2 years ago
25 changed file(s) with 397 addition(s) and 372 deletion(s). Raw diff Collapse all Expand all
284284 public:
285285 enum AttrIndex : unsigned {
286286 ReturnIndex = 0U,
287 FunctionIndex = ~0U
287 FunctionIndex = ~0U,
288 FirstArgIndex = 1,
288289 };
289290
290291 private:
335336 static AttributeList get(LLVMContext &C, unsigned Index,
336337 const AttrBuilder &B);
337338
339 /// Add an argument attribute to the list. Returns a new list because
340 /// attribute lists are immutable.
341 AttributeList addParamAttribute(LLVMContext &C, unsigned ArgNo,
342 Attribute::AttrKind Kind) const {
343 return addAttribute(C, ArgNo + FirstArgIndex, Kind);
344 }
345
338346 /// \brief Add an attribute to the attribute set at the given index. Because
339347 /// attribute sets are immutable, this returns a new set.
340348 AttributeList addAttribute(LLVMContext &C, unsigned Index,
432440 /// may be faster.
433441 bool hasFnAttribute(StringRef Kind) const;
434442
435 /// \brief Equivalent to hasAttribute(ArgNo + 1, Kind).
443 /// \brief Equivalent to hasAttribute(ArgNo + FirstArgIndex, Kind).
436444 bool hasParamAttribute(unsigned ArgNo, Attribute::AttrKind Kind) const;
437445
438446 /// \brief Return true if the specified attribute is set for at least one
338338 CALLSITE_DELEGATE_SETTER(addAttribute(i, Attr));
339339 }
340340
341 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
342 CALLSITE_DELEGATE_SETTER(addParamAttr(ArgNo, Kind));
343 }
344
341345 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
342346 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
343347 }
344348
345349 void removeAttribute(unsigned i, StringRef Kind) {
346350 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
351 }
352
353 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
354 CALLSITE_DELEGATE_SETTER(removeParamAttr(ArgNo, Kind));
347355 }
348356
349357 /// Return true if this function has the given attribute.
407415 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
408416 }
409417
410 /// Determine if the parameter or return value is marked with NoAlias
411 /// attribute.
412 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
413 bool doesNotAlias(unsigned n) const {
414 CALLSITE_DELEGATE_GETTER(doesNotAlias(n));
418 /// Determine if the return value is marked with NoAlias attribute.
419 bool returnDoesNotAlias() const {
420 CALLSITE_DELEGATE_GETTER(returnDoesNotAlias());
415421 }
416422
417423 /// Return true if the call should not be treated as a call to a builtin.
203203 addAttribute(AttributeList::FunctionIndex, Attr);
204204 }
205205
206 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
207 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
208 }
209
206210 /// @brief Remove function attributes from this function.
207211 void removeFnAttr(Attribute::AttrKind Kind) {
208212 removeAttribute(AttributeList::FunctionIndex, Kind);
210214
211215 /// @brief Remove function attribute from this function.
212216 void removeFnAttr(StringRef Kind) {
213 setAttributes(AttributeSets.removeAttribute(
217 setAttributes(getAttributes().removeAttribute(
214218 getContext(), AttributeList::FunctionIndex, Kind));
219 }
220
221 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
222 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
215223 }
216224
217225 /// \brief Set the entry count for this function.
458466 /// @brief Determine if the parameter or return value is marked with NoAlias
459467 /// attribute.
460468 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
461 bool doesNotAlias(unsigned n) const {
462 return AttributeSets.hasAttribute(n, Attribute::NoAlias);
463 }
464 void setDoesNotAlias(unsigned n) {
465 addAttribute(n, Attribute::NoAlias);
466 }
467
468 /// @brief Determine if the parameter can be captured.
469 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
470 bool doesNotCapture(unsigned n) const {
471 return AttributeSets.hasAttribute(n, Attribute::NoCapture);
472 }
473 void setDoesNotCapture(unsigned n) {
474 addAttribute(n, Attribute::NoCapture);
475 }
476
477 bool doesNotAccessMemory(unsigned n) const {
478 return AttributeSets.hasAttribute(n, Attribute::ReadNone);
479 }
480 void setDoesNotAccessMemory(unsigned n) {
481 addAttribute(n, Attribute::ReadNone);
482 }
483
484 bool onlyReadsMemory(unsigned n) const {
485 return doesNotAccessMemory(n) ||
486 AttributeSets.hasAttribute(n, Attribute::ReadOnly);
487 }
488 void setOnlyReadsMemory(unsigned n) {
489 addAttribute(n, Attribute::ReadOnly);
469 bool returnDoesNotAlias() const {
470 return AttributeSets.hasAttribute(AttributeList::ReturnIndex,
471 Attribute::NoAlias);
472 }
473 void setReturnDoesNotAlias() {
474 addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
490475 }
491476
492477 /// Optimize this function for minimum size (-Oz).
16571657 /// adds the attribute to the list of attributes.
16581658 void addAttribute(unsigned i, Attribute Attr);
16591659
1660 /// Adds the attribute to the indicated argument
1661 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
1662
16601663 /// removes the attribute from the list of attributes.
16611664 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
16621665
16631666 /// removes the attribute from the list of attributes.
16641667 void removeAttribute(unsigned i, StringRef Kind);
1668
1669 /// Removes the attribute from the given argument
1670 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
16651671
16661672 /// adds the dereferenceable attribute to the list of attributes.
16671673 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
17331739 return Attrs.getDereferenceableOrNullBytes(i);
17341740 }
17351741
1736 /// @brief Determine if the parameter or return value is marked with NoAlias
1737 /// attribute.
1738 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
1739 bool doesNotAlias(unsigned n) const {
1740 return Attrs.hasAttribute(n, Attribute::NoAlias);
1742 /// @brief Determine if the return value is marked with NoAlias attribute.
1743 bool returnDoesNotAlias() const {
1744 return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
17411745 }
17421746
17431747 /// Return true if the call should not be treated as a call to a
37493753 /// adds the attribute to the list of attributes.
37503754 void addAttribute(unsigned i, Attribute Attr);
37513755
3756 /// Adds the attribute to the indicated argument
3757 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
3758
37523759 /// removes the attribute from the list of attributes.
37533760 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
37543761
37553762 /// removes the attribute from the list of attributes.
37563763 void removeAttribute(unsigned i, StringRef Kind);
3764
3765 /// Removes the attribute from the given argument
3766 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind);
37573767
37583768 /// adds the dereferenceable attribute to the list of attributes.
37593769 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
38263836 return Attrs.getDereferenceableOrNullBytes(i);
38273837 }
38283838
3829 /// @brief Determine if the parameter or return value is marked with NoAlias
3830 /// attribute.
3831 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
3832 bool doesNotAlias(unsigned n) const {
3833 return Attrs.hasAttribute(n, Attribute::NoAlias);
3839 /// @brief Determine if the return value is marked with NoAlias attribute.
3840 bool returnDoesNotAlias() const {
3841 return Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
38343842 }
38353843
38363844 /// Return true if the call should not be treated as a call to a
428428
429429 if (Inst->getType()->isPointerTy()) {
430430 auto *Fn = CS.getCalledFunction();
431 if (Fn == nullptr || !Fn->doesNotAlias(AttributeList::ReturnIndex))
431 if (Fn == nullptr || !Fn->returnDoesNotAlias())
432432 // No need to call addNode() since we've added Inst at the
433433 // beginning of this function and we know it is not a global.
434434 Graph.addAttr(InstantiatedValue{Inst, 0}, getAttrUnknown());
3636 for (auto &Arg : CS.args()) {
3737 ArgInfo OrigArg{ArgRegs[i], Arg->getType(), ISD::ArgFlagsTy{},
3838 i < NumFixedArgs};
39 setArgFlags(OrigArg, i + 1, DL, CS);
39 setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, CS);
4040 OrigArgs.push_back(OrigArg);
4141 ++i;
4242 }
11221122 LLVMContext &AttributeList::getContext() const { return pImpl->getContext(); }
11231123
11241124 AttributeSet AttributeList::getParamAttributes(unsigned ArgNo) const {
1125 return getAttributes(ArgNo + 1);
1125 return getAttributes(ArgNo + FirstArgIndex);
11261126 }
11271127
11281128 AttributeSet AttributeList::getRetAttributes() const {
11881188 }
11891189
11901190 unsigned AttributeList::getParamAlignment(unsigned ArgNo) const {
1191 return getAttributes(ArgNo + 1).getAlignment();
1191 return getAttributes(ArgNo + FirstArgIndex).getAlignment();
11921192 }
11931193
11941194 unsigned AttributeList::getStackAlignment(unsigned Index) const {
8989 uint64_t Argument::getDereferenceableBytes() const {
9090 assert(getType()->isPointerTy() &&
9191 "Only pointers have dereferenceable bytes");
92 return getParent()->getDereferenceableBytes(getArgNo()+1);
92 return getParent()->getDereferenceableBytes(getArgNo() +
93 AttributeList::FirstArgIndex);
9394 }
9495
9596 uint64_t Argument::getDereferenceableOrNullBytes() const {
9697 assert(getType()->isPointerTy() &&
9798 "Only pointers have dereferenceable bytes");
98 return getParent()->getDereferenceableOrNullBytes(getArgNo()+1);
99 return getParent()->getDereferenceableOrNullBytes(
100 getArgNo() + AttributeList::FirstArgIndex);
99101 }
100102
101103 bool Argument::hasNestAttr() const {
138140
139141 void Argument::addAttrs(AttrBuilder &B) {
140142 AttributeList AL = getParent()->getAttributes();
141 AL = AL.addAttributes(Parent->getContext(), getArgNo() + 1, B);
143 AL = AL.addAttributes(Parent->getContext(),
144 getArgNo() + AttributeList::FirstArgIndex, B);
142145 getParent()->setAttributes(AL);
143146 }
144147
145148 void Argument::addAttr(Attribute::AttrKind Kind) {
146 getParent()->addAttribute(getArgNo() + 1, Kind);
149 getParent()->addAttribute(getArgNo() + AttributeList::FirstArgIndex, Kind);
147150 }
148151
149152 void Argument::addAttr(Attribute Attr) {
150 getParent()->addAttribute(getArgNo() + 1, Attr);
153 getParent()->addAttribute(getArgNo() + AttributeList::FirstArgIndex, Attr);
151154 }
152155
153156 void Argument::removeAttr(Attribute::AttrKind Kind) {
154 getParent()->removeAttribute(getArgNo() + 1, Kind);
157 getParent()->removeAttribute(getArgNo() + AttributeList::FirstArgIndex, Kind);
155158 }
156159
157160 bool Argument::hasAttribute(Attribute::AttrKind Kind) const {
334334 unsigned Index;
335335
336336 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
337 return getArgOperand(Index-1);
337 return getArgOperand(Index - AttributeList::FirstArgIndex);
338338 if (const Function *F = getCalledFunction())
339339 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
340340 Index)
341 return getArgOperand(Index-1);
342
341 return getArgOperand(Index - AttributeList::FirstArgIndex);
342
343343 return nullptr;
344344 }
345345
355355 setAttributes(PAL);
356356 }
357357
358 void CallInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
359 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
360 }
361
358362 void CallInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
359363 AttributeList PAL = getAttributes();
360364 PAL = PAL.removeAttribute(getContext(), i, Kind);
365369 AttributeList PAL = getAttributes();
366370 PAL = PAL.removeAttribute(getContext(), i, Kind);
367371 setAttributes(PAL);
372 }
373
374 void CallInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
375 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
368376 }
369377
370378 void CallInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
500508 MCall->setTailCall();
501509 if (Function *F = dyn_cast(MallocFunc)) {
502510 MCall->setCallingConv(F->getCallingConv());
503 if (!F->doesNotAlias(AttributeList::ReturnIndex))
504 F->setDoesNotAlias(AttributeList::ReturnIndex);
511 if (!F->returnDoesNotAlias())
512 F->setReturnDoesNotAlias();
505513 }
506514 assert(!MCall->getType()->isVoidTy() && "Malloc has void return type");
507515
694702 unsigned Index;
695703
696704 if (Attrs.hasAttrSomewhere(Attribute::Returned, &Index) && Index)
697 return getArgOperand(Index-1);
705 return getArgOperand(Index - AttributeList::FirstArgIndex);
698706 if (const Function *F = getCalledFunction())
699707 if (F->getAttributes().hasAttrSomewhere(Attribute::Returned, &Index) &&
700708 Index)
701 return getArgOperand(Index-1);
702
709 return getArgOperand(Index - AttributeList::FirstArgIndex);
710
703711 return nullptr;
704712 }
705713
755763 setAttributes(PAL);
756764 }
757765
766 void InvokeInst::addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
767 addAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
768 }
769
758770 void InvokeInst::removeAttribute(unsigned i, Attribute::AttrKind Kind) {
759771 AttributeList PAL = getAttributes();
760772 PAL = PAL.removeAttribute(getContext(), i, Kind);
765777 AttributeList PAL = getAttributes();
766778 PAL = PAL.removeAttribute(getContext(), i, Kind);
767779 setAttributes(PAL);
780 }
781
782 void InvokeInst::removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
783 removeAttribute(ArgNo + AttributeList::FirstArgIndex, Kind);
768784 }
769785
770786 void InvokeInst::addDereferenceableAttr(unsigned i, uint64_t Bytes) {
246246 unsigned i = 0;
247247 for (auto &Arg : F.args()) {
248248 ArgInfo OrigArg{VRegs[i], Arg.getType()};
249 setArgFlags(OrigArg, i + 1, DL, F);
249 setArgFlags(OrigArg, i + AttributeList::FirstArgIndex, DL, F);
250250 bool Split = false;
251251 LLT Ty = MRI.getType(VRegs[i]);
252252 unsigned Dst = VRegs[i];
353353 unsigned Idx = 0;
354354 for (auto &Arg : F.args()) {
355355 ArgInfo AInfo(VRegs[Idx], Arg.getType());
356 setArgFlags(AInfo, Idx + 1, DL, F);
356 setArgFlags(AInfo, Idx + AttributeList::FirstArgIndex, DL, F);
357357 splitToValueTypes(AInfo, ArgInfos, DL, MF.getRegInfo());
358358 Idx++;
359359 }
411411 // Can't use musttail due to prototype mismatch, but we can use tail.
412412 Call->setTailCall(true);
413413 // Set inreg so we pass it in EAX.
414 Call->addAttribute(1, Attribute::InReg);
414 Call->addParamAttr(0, Attribute::InReg);
415415 Builder.CreateRet(Call);
416416 return Trampoline;
417417 }
215215 Function *NewF =
216216 Function::Create(FnTy, GlobalValue::LinkageTypes::InternalLinkage,
217217 F.getName() + Suffix, M);
218 NewF->addAttribute(1, Attribute::NonNull);
219 NewF->addAttribute(1, Attribute::NoAlias);
218 NewF->addParamAttr(0, Attribute::NonNull);
219 NewF->addParamAttr(0, Attribute::NoAlias);
220220
221221 ValueToValueMapTy VMap;
222222 // Replace all args with undefs. The buildCoroutineFrame algorithm already
838838 // avoiding a register copy.
839839 if (PtrArg->hasStructRetAttr()) {
840840 unsigned ArgNo = PtrArg->getArgNo();
841 F->removeAttribute(ArgNo + 1, Attribute::StructRet);
842 F->addAttribute(ArgNo + 1, Attribute::NoAlias);
841 F->removeParamAttr(ArgNo, Attribute::StructRet);
842 F->addParamAttr(ArgNo, Attribute::NoAlias);
843843 for (Use &U : F->uses()) {
844844 CallSite CS(U.getUser());
845 CS.removeAttribute(ArgNo + 1, Attribute::StructRet);
846 CS.addAttribute(ArgNo + 1, Attribute::NoAlias);
845 CS.removeParamAttr(ArgNo, Attribute::StructRet);
846 CS.addParamAttr(ArgNo, Attribute::NoAlias);
847847 }
848848 }
849849
834834 // pointers.
835835 for (Function *F : SCCNodes) {
836836 // Already noalias.
837 if (F->doesNotAlias(0))
837 if (F->returnDoesNotAlias())
838838 continue;
839839
840840 // We can infer and propagate function attributes only when we know that the
854854
855855 bool MadeChange = false;
856856 for (Function *F : SCCNodes) {
857 if (F->doesNotAlias(AttributeList::ReturnIndex) ||
857 if (F->returnDoesNotAlias() ||
858858 !F->getReturnType()->isPointerTy())
859859 continue;
860860
861 F->setDoesNotAlias(AttributeList::ReturnIndex);
861 F->setReturnDoesNotAlias();
862862 ++NumNoAlias;
863863 MadeChange = true;
864864 }
38443844 if (V->getType()->isPointerTy() &&
38453845 !CS.paramHasAttr(ArgNo, Attribute::NonNull) &&
38463846 isKnownNonNullAt(V, CS.getInstruction(), &DT))
3847 Indices.push_back(ArgNo + 1);
3847 Indices.push_back(ArgNo + AttributeList::FirstArgIndex);
38483848 ArgNo++;
38493849 }
38503850
624624 F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
625625 F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
626626 F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
627 F->addAttribute(1, Attribute::ZExt);
628 F->addAttribute(2, Attribute::ZExt);
627 F->addParamAttr(0, Attribute::ZExt);
628 F->addParamAttr(1, Attribute::ZExt);
629629 }
630630 DFSanCheckedUnionFn = Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy);
631631 if (Function *F = dyn_cast(DFSanCheckedUnionFn)) {
632632 F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
633633 F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
634634 F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
635 F->addAttribute(1, Attribute::ZExt);
636 F->addAttribute(2, Attribute::ZExt);
635 F->addParamAttr(0, Attribute::ZExt);
636 F->addParamAttr(1, Attribute::ZExt);
637637 }
638638 DFSanUnionLoadFn =
639639 Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
647647 DFSanSetLabelFn =
648648 Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
649649 if (Function *F = dyn_cast(DFSanSetLabelFn)) {
650 F->addAttribute(1, Attribute::ZExt);
650 F->addParamAttr(0, Attribute::ZExt);
651651 }
652652 DFSanNonzeroLabelFn =
653653 Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
981981 if (AvoidNewBlocks) {
982982 CallInst *Call = IRB.CreateCall(DFS.DFSanCheckedUnionFn, {V1, V2});
983983 Call->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
984 Call->addAttribute(1, Attribute::ZExt);
985 Call->addAttribute(2, Attribute::ZExt);
984 Call->addParamAttr(0, Attribute::ZExt);
985 Call->addParamAttr(1, Attribute::ZExt);
986986
987987 CCS.Block = Pos->getParent();
988988 CCS.Shadow = Call;
994994 IRBuilder<> ThenIRB(BI);
995995 CallInst *Call = ThenIRB.CreateCall(DFS.DFSanUnionFn, {V1, V2});
996996 Call->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
997 Call->addAttribute(1, Attribute::ZExt);
998 Call->addAttribute(2, Attribute::ZExt);
997 Call->addParamAttr(0, Attribute::ZExt);
998 Call->addParamAttr(1, Attribute::ZExt);
999999
10001000 BasicBlock *Tail = BI->getSuccessor(0);
10011001 PHINode *Phi = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
240240
241241 if (Function *FunRes = dyn_cast(Res)) {
242242 if (auto AK = TLI.getExtAttrForI32Param(false))
243 FunRes->addAttribute(3, AK);
243 FunRes->addParamAttr(2, AK);
244244 }
245245 return Res;
246246 }
291291 Builder.CreateCall(getOrInsertValueProfilingCall(*M, *TLI, true), Args);
292292 }
293293 if (auto AK = TLI->getExtAttrForI32Param(false))
294 Call->addAttribute(3, AK);
294 Call->addParamAttr(2, AK);
295295 Ind->replaceAllUsesWith(Call);
296296 Ind->eraseFromParent();
297297 }
162162
163163 AttributeList Attr = AttributeList().addAttribute(
164164 C, AttributeList::FunctionIndex, Attribute::NoUnwind);
165 Attr = Attr.addAttribute(C, 1, Attribute::NoCapture);
165 Attr = Attr.addParamAttribute(C, 0, Attribute::NoCapture);
166166
167167 FunctionType *Fty = FunctionType::get(Type::getVoidTy(C), Params,
168168 /*isVarArg=*/false);
317317 LVI->getPredicateAt(ICmpInst::ICMP_EQ, V,
318318 ConstantPointerNull::get(Type),
319319 CS.getInstruction()) == LazyValueInfo::False)
320 Indices.push_back(ArgNo + 1);
320 Indices.push_back(ArgNo + AttributeList::FirstArgIndex);
321321 ArgNo++;
322322 }
323323
22852285 if (AH.getDereferenceableOrNullBytes(Index))
22862286 R.addAttribute(Attribute::get(Ctx, Attribute::DereferenceableOrNull,
22872287 AH.getDereferenceableOrNullBytes(Index)));
2288 if (AH.doesNotAlias(Index))
2288 if (AH.getAttributes().hasAttribute(Index, Attribute::NoAlias))
22892289 R.addAttribute(Attribute::NoAlias);
22902290
22912291 if (!R.empty())
22982298
22992299 for (Argument &A : F.args())
23002300 if (isa(A.getType()))
2301 RemoveNonValidAttrAtIndex(Ctx, F, A.getArgNo() + 1);
2301 RemoveNonValidAttrAtIndex(Ctx, F,
2302 A.getArgNo() + AttributeList::FirstArgIndex);
23022303
23032304 if (isa(F.getReturnType()))
23042305 RemoveNonValidAttrAtIndex(Ctx, F, AttributeList::ReturnIndex);
23342335 if (CallSite CS = CallSite(&I)) {
23352336 for (int i = 0, e = CS.arg_size(); i != e; i++)
23362337 if (isa(CS.getArgument(i)->getType()))
2337 RemoveNonValidAttrAtIndex(Ctx, CS, i + 1);
2338 RemoveNonValidAttrAtIndex(Ctx, CS, i + AttributeList::FirstArgIndex);
23382339 if (isa(CS.getType()))
23392340 RemoveNonValidAttrAtIndex(Ctx, CS, AttributeList::ReturnIndex);
23402341 }
5757 static bool setOnlyAccessesArgMemory(Function &F) {
5858 if (F.onlyAccessesArgMemory())
5959 return false;
60 F.setOnlyAccessesArgMemory ();
60 F.setOnlyAccessesArgMemory();
6161 ++NumArgMemOnly;
6262 return true;
6363 }
7070 return true;
7171 }
7272
73 static bool setDoesNotCapture(Function &F, unsigned n) {
74 if (F.doesNotCapture(n))
73 static bool setRetDoesNotAlias(Function &F) {
74 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NoAlias))
7575 return false;
76 F.setDoesNotCapture(n);
76 F.addAttribute(AttributeList::ReturnIndex, Attribute::NoAlias);
77 ++NumNoAlias;
78 return true;
79 }
80
81 static bool setDoesNotCapture(Function &F, unsigned ArgNo) {
82 if (F.hasParamAttribute(ArgNo, Attribute::NoCapture))
83 return false;
84 F.addParamAttr(ArgNo, Attribute::NoCapture);
7785 ++NumNoCapture;
7886 return true;
7987 }
8088
81 static bool setOnlyReadsMemory(Function &F, unsigned n) {
82 if (F.onlyReadsMemory(n))
89 static bool setOnlyReadsMemory(Function &F, unsigned ArgNo) {
90 if (F.hasParamAttribute(ArgNo, Attribute::ReadOnly))
8391 return false;
84 F.setOnlyReadsMemory(n);
92 F.addParamAttr(ArgNo, Attribute::ReadOnly);
8593 ++NumReadOnlyArg;
8694 return true;
8795 }
8896
89 static bool setDoesNotAlias(Function &F, unsigned n) {
90 if (F.doesNotAlias(n))
97 static bool setRetNonNull(Function &F) {
98 assert(F.getReturnType()->isPointerTy() &&
99 "nonnull applies only to pointers");
100 if (F.hasAttribute(AttributeList::ReturnIndex, Attribute::NonNull))
91101 return false;
92 F.setDoesNotAlias(n);
93 ++NumNoAlias;
94 return true;
95 }
96
97 static bool setNonNull(Function &F, unsigned n) {
98 assert(
99 (n != AttributeList::ReturnIndex || F.getReturnType()->isPointerTy()) &&
100 "nonnull applies only to pointers");
101 if (F.getAttributes().hasAttribute(n, Attribute::NonNull))
102 return false;
103 F.addAttribute(n, Attribute::NonNull);
102 F.addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
104103 ++NumNonNull;
105104 return true;
106105 }
115114 case LibFunc_strlen:
116115 Changed |= setOnlyReadsMemory(F);
117116 Changed |= setDoesNotThrow(F);
118 Changed |= setDoesNotCapture(F, 1);
117 Changed |= setDoesNotCapture(F, 0);
119118 return Changed;
120119 case LibFunc_strchr:
121120 case LibFunc_strrchr:
130129 case LibFunc_strtold:
131130 case LibFunc_strtoull:
132131 Changed |= setDoesNotThrow(F);
133 Changed |= setDoesNotCapture(F, 2);
134 Changed |= setOnlyReadsMemory(F, 1);
132 Changed |= setDoesNotCapture(F, 1);
133 Changed |= setOnlyReadsMemory(F, 0);
135134 return Changed;
136135 case LibFunc_strcpy:
137136 case LibFunc_stpcpy:
140139 case LibFunc_strncpy:
141140 case LibFunc_stpncpy:
142141 Changed |= setDoesNotThrow(F);
143 Changed |= setDoesNotCapture(F, 2);
144 Changed |= setOnlyReadsMemory(F, 2);
142 Changed |= setDoesNotCapture(F, 1);
143 Changed |= setOnlyReadsMemory(F, 1);
145144 return Changed;
146145 case LibFunc_strxfrm:
147146 Changed |= setDoesNotThrow(F);
148 Changed |= setDoesNotCapture(F, 1);
149 Changed |= setDoesNotCapture(F, 2);
150 Changed |= setOnlyReadsMemory(F, 2);
147 Changed |= setDoesNotCapture(F, 0);
148 Changed |= setDoesNotCapture(F, 1);
149 Changed |= setOnlyReadsMemory(F, 1);
151150 return Changed;
152151 case LibFunc_strcmp: // 0,1
153152 case LibFunc_strspn: // 0,1
158157 case LibFunc_strncasecmp: //
159158 Changed |= setOnlyReadsMemory(F);
160159 Changed |= setDoesNotThrow(F);
161 Changed |= setDoesNotCapture(F, 1);
162 Changed |= setDoesNotCapture(F, 2);
160 Changed |= setDoesNotCapture(F, 0);
161 Changed |= setDoesNotCapture(F, 1);
163162 return Changed;
164163 case LibFunc_strstr:
165164 case LibFunc_strpbrk:
166165 Changed |= setOnlyReadsMemory(F);
167166 Changed |= setDoesNotThrow(F);
168 Changed |= setDoesNotCapture(F, 2);
167 Changed |= setDoesNotCapture(F, 1);
169168 return Changed;
170169 case LibFunc_strtok:
171170 case LibFunc_strtok_r:
172171 Changed |= setDoesNotThrow(F);
172 Changed |= setDoesNotCapture(F, 1);
173 Changed |= setOnlyReadsMemory(F, 1);
174 return Changed;
175 case LibFunc_scanf:
176 Changed |= setDoesNotThrow(F);
177 Changed |= setDoesNotCapture(F, 0);
178 Changed |= setOnlyReadsMemory(F, 0);
179 return Changed;
180 case LibFunc_setbuf:
181 case LibFunc_setvbuf:
182 Changed |= setDoesNotThrow(F);
183 Changed |= setDoesNotCapture(F, 0);
184 return Changed;
185 case LibFunc_strdup:
186 case LibFunc_strndup:
187 Changed |= setDoesNotThrow(F);
188 Changed |= setRetDoesNotAlias(F);
189 Changed |= setDoesNotCapture(F, 0);
190 Changed |= setOnlyReadsMemory(F, 0);
191 return Changed;
192 case LibFunc_stat:
193 case LibFunc_statvfs:
194 Changed |= setDoesNotThrow(F);
195 Changed |= setDoesNotCapture(F, 0);
196 Changed |= setDoesNotCapture(F, 1);
197 Changed |= setOnlyReadsMemory(F, 0);
198 return Changed;
199 case LibFunc_sscanf:
200 Changed |= setDoesNotThrow(F);
201 Changed |= setDoesNotCapture(F, 0);
202 Changed |= setDoesNotCapture(F, 1);
203 Changed |= setOnlyReadsMemory(F, 0);
204 Changed |= setOnlyReadsMemory(F, 1);
205 return Changed;
206 case LibFunc_sprintf:
207 Changed |= setDoesNotThrow(F);
208 Changed |= setDoesNotCapture(F, 0);
209 Changed |= setDoesNotCapture(F, 1);
210 Changed |= setOnlyReadsMemory(F, 1);
211 return Changed;
212 case LibFunc_snprintf:
213 Changed |= setDoesNotThrow(F);
214 Changed |= setDoesNotCapture(F, 0);
173215 Changed |= setDoesNotCapture(F, 2);
174216 Changed |= setOnlyReadsMemory(F, 2);
175217 return Changed;
176 case LibFunc_scanf:
177 Changed |= setDoesNotThrow(F);
178 Changed |= setDoesNotCapture(F, 1);
179 Changed |= setOnlyReadsMemory(F, 1);
180 return Changed;
181 case LibFunc_setbuf:
182 case LibFunc_setvbuf:
183 Changed |= setDoesNotThrow(F);
184 Changed |= setDoesNotCapture(F, 1);
185 return Changed;
186 case LibFunc_strdup:
187 case LibFunc_strndup:
188 Changed |= setDoesNotThrow(F);
189 Changed |= setDoesNotAlias(F, 0);
190 Changed |= setDoesNotCapture(F, 1);
191 Changed |= setOnlyReadsMemory(F, 1);
192 return Changed;
193 case LibFunc_stat:
194 case LibFunc_statvfs:
218 case LibFunc_setitimer:
195219 Changed |= setDoesNotThrow(F);
196220 Changed |= setDoesNotCapture(F, 1);
197221 Changed |= setDoesNotCapture(F, 2);
198222 Changed |= setOnlyReadsMemory(F, 1);
199 return Changed;
200 case LibFunc_sscanf:
201 Changed |= setDoesNotThrow(F);
202 Changed |= setDoesNotCapture(F, 1);
203 Changed |= setDoesNotCapture(F, 2);
204 Changed |= setOnlyReadsMemory(F, 1);
205 Changed |= setOnlyReadsMemory(F, 2);
206 return Changed;
207 case LibFunc_sprintf:
208 Changed |= setDoesNotThrow(F);
209 Changed |= setDoesNotCapture(F, 1);
210 Changed |= setDoesNotCapture(F, 2);
211 Changed |= setOnlyReadsMemory(F, 2);
212 return Changed;
213 case LibFunc_snprintf:
214 Changed |= setDoesNotThrow(F);
215 Changed |= setDoesNotCapture(F, 1);
216 Changed |= setDoesNotCapture(F, 3);
217 Changed |= setOnlyReadsMemory(F, 3);
218 return Changed;
219 case LibFunc_setitimer:
220 Changed |= setDoesNotThrow(F);
221 Changed |= setDoesNotCapture(F, 2);
222 Changed |= setDoesNotCapture(F, 3);
223 Changed |= setOnlyReadsMemory(F, 2);
224223 return Changed;
225224 case LibFunc_system:
226225 // May throw; "system" is a valid pthread cancellation point.
227 Changed |= setDoesNotCapture(F, 1);
228 Changed |= setOnlyReadsMemory(F, 1);
226 Changed |= setDoesNotCapture(F, 0);
227 Changed |= setOnlyReadsMemory(F, 0);
229228 return Changed;
230229 case LibFunc_malloc:
231230 Changed |= setDoesNotThrow(F);
232 Changed |= setDoesNotAlias(F, 0);
231 Changed |= setRetDoesNotAlias(F);
233232 return Changed;
234233 case LibFunc_memcmp:
235234 Changed |= setOnlyReadsMemory(F);
236235 Changed |= setDoesNotThrow(F);
237 Changed |= setDoesNotCapture(F, 1);
238 Changed |= setDoesNotCapture(F, 2);
236 Changed |= setDoesNotCapture(F, 0);
237 Changed |= setDoesNotCapture(F, 1);
239238 return Changed;
240239 case LibFunc_memchr:
241240 case LibFunc_memrchr:
246245 case LibFunc_modff:
247246 case LibFunc_modfl:
248247 Changed |= setDoesNotThrow(F);
249 Changed |= setDoesNotCapture(F, 2);
248 Changed |= setDoesNotCapture(F, 1);
250249 return Changed;
251250 case LibFunc_memcpy:
252251 case LibFunc_mempcpy:
253252 case LibFunc_memccpy:
254253 case LibFunc_memmove:
255254 Changed |= setDoesNotThrow(F);
256 Changed |= setDoesNotCapture(F, 2);
257 Changed |= setOnlyReadsMemory(F, 2);
255 Changed |= setDoesNotCapture(F, 1);
256 Changed |= setOnlyReadsMemory(F, 1);
258257 return Changed;
259258 case LibFunc_memcpy_chk:
260259 Changed |= setDoesNotThrow(F);
261260 return Changed;
262261 case LibFunc_memalign:
263 Changed |= setDoesNotAlias(F, 0);
262 Changed |= setRetDoesNotAlias(F);
264263 return Changed;
265264 case LibFunc_mkdir:
266265 Changed |= setDoesNotThrow(F);
267 Changed |= setDoesNotCapture(F, 1);
268 Changed |= setOnlyReadsMemory(F, 1);
266 Changed |= setDoesNotCapture(F, 0);
267 Changed |= setOnlyReadsMemory(F, 0);
269268 return Changed;
270269 case LibFunc_mktime:
271270 Changed |= setDoesNotThrow(F);
272 Changed |= setDoesNotCapture(F, 1);
271 Changed |= setDoesNotCapture(F, 0);
273272 return Changed;
274273 case LibFunc_realloc:
275274 Changed |= setDoesNotThrow(F);
276 Changed |= setDoesNotAlias(F, 0);
277 Changed |= setDoesNotCapture(F, 1);
275 Changed |= setRetDoesNotAlias(F);
276 Changed |= setDoesNotCapture(F, 0);
278277 return Changed;
279278 case LibFunc_read:
280279 // May throw; "read" is a valid pthread cancellation point.
281 Changed |= setDoesNotCapture(F, 2);
280 Changed |= setDoesNotCapture(F, 1);
282281 return Changed;
283282 case LibFunc_rewind:
284283 Changed |= setDoesNotThrow(F);
285 Changed |= setDoesNotCapture(F, 1);
284 Changed |= setDoesNotCapture(F, 0);
286285 return Changed;
287286 case LibFunc_rmdir:
288287 case LibFunc_remove:
289288 case LibFunc_realpath:
290289 Changed |= setDoesNotThrow(F);
291 Changed |= setDoesNotCapture(F, 1);
292 Changed |= setOnlyReadsMemory(F, 1);
290 Changed |= setDoesNotCapture(F, 0);
291 Changed |= setOnlyReadsMemory(F, 0);
293292 return Changed;
294293 case LibFunc_rename:
295294 Changed |= setDoesNotThrow(F);
296 Changed |= setDoesNotCapture(F, 1);
297 Changed |= setDoesNotCapture(F, 2);
298 Changed |= setOnlyReadsMemory(F, 1);
299 Changed |= setOnlyReadsMemory(F, 2);
295 Changed |= setDoesNotCapture(F, 0);
296 Changed |= setDoesNotCapture(F, 1);
297 Changed |= setOnlyReadsMemory(F, 0);
298 Changed |= setOnlyReadsMemory(F, 1);
300299 return Changed;
301300 case LibFunc_readlink:
302301 Changed |= setDoesNotThrow(F);
303 Changed |= setDoesNotCapture(F, 1);
304 Changed |= setDoesNotCapture(F, 2);
305 Changed |= setOnlyReadsMemory(F, 1);
302 Changed |= setDoesNotCapture(F, 0);
303 Changed |= setDoesNotCapture(F, 1);
304 Changed |= setOnlyReadsMemory(F, 0);
306305 return Changed;
307306 case LibFunc_write:
308307 // May throw; "write" is a valid pthread cancellation point.
309 Changed |= setDoesNotCapture(F, 2);
310 Changed |= setOnlyReadsMemory(F, 2);
308 Changed |= setDoesNotCapture(F, 1);
309 Changed |= setOnlyReadsMemory(F, 1);
311310 return Changed;
312311 case LibFunc_bcopy:
313312 Changed |= setDoesNotThrow(F);
314 Changed |= setDoesNotCapture(F, 1);
315 Changed |= setDoesNotCapture(F, 2);
316 Changed |= setOnlyReadsMemory(F, 1);
313 Changed |= setDoesNotCapture(F, 0);
314 Changed |= setDoesNotCapture(F, 1);
315 Changed |= setOnlyReadsMemory(F, 0);
317316 return Changed;
318317 case LibFunc_bcmp:
319318 Changed |= setDoesNotThrow(F);
320319 Changed |= setOnlyReadsMemory(F);
321 Changed |= setDoesNotCapture(F, 1);
322 Changed |= setDoesNotCapture(F, 2);
320 Changed |= setDoesNotCapture(F, 0);
321 Changed |= setDoesNotCapture(F, 1);
323322 return Changed;
324323 case LibFunc_bzero:
325324 Changed |= setDoesNotThrow(F);
326 Changed |= setDoesNotCapture(F, 1);
325 Changed |= setDoesNotCapture(F, 0);
327326 return Changed;
328327 case LibFunc_calloc:
329328 Changed |= setDoesNotThrow(F);
330 Changed |= setDoesNotAlias(F, 0);
329 Changed |= setRetDoesNotAlias(F);
331330 return Changed;
332331 case LibFunc_chmod:
333332 case LibFunc_chown:
334333 Changed |= setDoesNotThrow(F);
335 Changed |= setDoesNotCapture(F, 1);
336 Changed |= setOnlyReadsMemory(F, 1);
334 Changed |= setDoesNotCapture(F, 0);
335 Changed |= setOnlyReadsMemory(F, 0);
337336 return Changed;
338337 case LibFunc_ctermid:
339338 case LibFunc_clearerr:
340339 case LibFunc_closedir:
341340 Changed |= setDoesNotThrow(F);
342 Changed |= setDoesNotCapture(F, 1);
341 Changed |= setDoesNotCapture(F, 0);
343342 return Changed;
344343 case LibFunc_atoi:
345344 case LibFunc_atol:
347346 case LibFunc_atoll:
348347 Changed |= setDoesNotThrow(F);
349348 Changed |= setOnlyReadsMemory(F);
350 Changed |= setDoesNotCapture(F, 1);
349 Changed |= setDoesNotCapture(F, 0);
351350 return Changed;
352351 case LibFunc_access:
353352 Changed |= setDoesNotThrow(F);
354 Changed |= setDoesNotCapture(F, 1);
355 Changed |= setOnlyReadsMemory(F, 1);
353 Changed |= setDoesNotCapture(F, 0);
354 Changed |= setOnlyReadsMemory(F, 0);
356355 return Changed;
357356 case LibFunc_fopen:
358357 Changed |= setDoesNotThrow(F);
359 Changed |= setDoesNotAlias(F, 0);
360 Changed |= setDoesNotCapture(F, 1);
361 Changed |= setDoesNotCapture(F, 2);
362 Changed |= setOnlyReadsMemory(F, 1);
363 Changed |= setOnlyReadsMemory(F, 2);
358 Changed |= setRetDoesNotAlias(F);
359 Changed |= setDoesNotCapture(F, 0);
360 Changed |= setDoesNotCapture(F, 1);
361 Changed |= setOnlyReadsMemory(F, 0);
362 Changed |= setOnlyReadsMemory(F, 1);
364363 return Changed;
365364 case LibFunc_fdopen:
366365 Changed |= setDoesNotThrow(F);
367 Changed |= setDoesNotAlias(F, 0);
368 Changed |= setDoesNotCapture(F, 2);
369 Changed |= setOnlyReadsMemory(F, 2);
366 Changed |= setRetDoesNotAlias(F);
367 Changed |= setDoesNotCapture(F, 1);
368 Changed |= setOnlyReadsMemory(F, 1);
370369 return Changed;
371370 case LibFunc_feof:
372371 case LibFunc_free:
383382 case LibFunc_funlockfile:
384383 case LibFunc_ftrylockfile:
385384 Changed |= setDoesNotThrow(F);
386 Changed |= setDoesNotCapture(F, 1);
385 Changed |= setDoesNotCapture(F, 0);
387386 return Changed;
388387 case LibFunc_ferror:
389388 Changed |= setDoesNotThrow(F);
390 Changed |= setDoesNotCapture(F, 1);
389 Changed |= setDoesNotCapture(F, 0);
391390 Changed |= setOnlyReadsMemory(F);
392391 return Changed;
393392 case LibFunc_fputc:
397396 case LibFunc_frexpl:
398397 case LibFunc_fstatvfs:
399398 Changed |= setDoesNotThrow(F);
399 Changed |= setDoesNotCapture(F, 1);
400 return Changed;
401 case LibFunc_fgets:
402 Changed |= setDoesNotThrow(F);
400403 Changed |= setDoesNotCapture(F, 2);
401404 return Changed;
402 case LibFunc_fgets:
403 Changed |= setDoesNotThrow(F);
405 case LibFunc_fread:
406 Changed |= setDoesNotThrow(F);
407 Changed |= setDoesNotCapture(F, 0);
404408 Changed |= setDoesNotCapture(F, 3);
405409 return Changed;
406 case LibFunc_fread:
407 Changed |= setDoesNotThrow(F);
408 Changed |= setDoesNotCapture(F, 1);
409 Changed |= setDoesNotCapture(F, 4);
410 return Changed;
411410 case LibFunc_fwrite:
412411 Changed |= setDoesNotThrow(F);
413 Changed |= setDoesNotCapture(F, 1);
414 Changed |= setDoesNotCapture(F, 4);
412 Changed |= setDoesNotCapture(F, 0);
413 Changed |= setDoesNotCapture(F, 3);
415414 // FIXME: readonly #1?
416415 return Changed;
417416 case LibFunc_fputs:
418417 Changed |= setDoesNotThrow(F);
419 Changed |= setDoesNotCapture(F, 1);
420 Changed |= setDoesNotCapture(F, 2);
421 Changed |= setOnlyReadsMemory(F, 1);
418 Changed |= setDoesNotCapture(F, 0);
419 Changed |= setDoesNotCapture(F, 1);
420 Changed |= setOnlyReadsMemory(F, 0);
422421 return Changed;
423422 case LibFunc_fscanf:
424423 case LibFunc_fprintf:
425424 Changed |= setDoesNotThrow(F);
426 Changed |= setDoesNotCapture(F, 1);
427 Changed |= setDoesNotCapture(F, 2);
428 Changed |= setOnlyReadsMemory(F, 2);
425 Changed |= setDoesNotCapture(F, 0);
426 Changed |= setDoesNotCapture(F, 1);
427 Changed |= setOnlyReadsMemory(F, 1);
429428 return Changed;
430429 case LibFunc_fgetpos:
431430 Changed |= setDoesNotThrow(F);
432 Changed |= setDoesNotCapture(F, 1);
433 Changed |= setDoesNotCapture(F, 2);
431 Changed |= setDoesNotCapture(F, 0);
432 Changed |= setDoesNotCapture(F, 1);
434433 return Changed;
435434 case LibFunc_getc:
436435 case LibFunc_getlogin_r:
437436 case LibFunc_getc_unlocked:
438437 Changed |= setDoesNotThrow(F);
439 Changed |= setDoesNotCapture(F, 1);
438 Changed |= setDoesNotCapture(F, 0);
440439 return Changed;
441440 case LibFunc_getenv:
442441 Changed |= setDoesNotThrow(F);
443442 Changed |= setOnlyReadsMemory(F);
444 Changed |= setDoesNotCapture(F, 1);
443 Changed |= setDoesNotCapture(F, 0);
445444 return Changed;
446445 case LibFunc_gets:
447446 case LibFunc_getchar:
449448 return Changed;
450449 case LibFunc_getitimer:
451450 Changed |= setDoesNotThrow(F);
452 Changed |= setDoesNotCapture(F, 2);
451 Changed |= setDoesNotCapture(F, 1);
453452 return Changed;
454453 case LibFunc_getpwnam:
455454 Changed |= setDoesNotThrow(F);
456 Changed |= setDoesNotCapture(F, 1);
457 Changed |= setOnlyReadsMemory(F, 1);
455 Changed |= setDoesNotCapture(F, 0);
456 Changed |= setOnlyReadsMemory(F, 0);
458457 return Changed;
459458 case LibFunc_ungetc:
460459 Changed |= setDoesNotThrow(F);
461 Changed |= setDoesNotCapture(F, 2);
460 Changed |= setDoesNotCapture(F, 1);
462461 return Changed;
463462 case LibFunc_uname:
464463 Changed |= setDoesNotThrow(F);
465 Changed |= setDoesNotCapture(F, 1);
464 Changed |= setDoesNotCapture(F, 0);
466465 return Changed;
467466 case LibFunc_unlink:
468467 Changed |= setDoesNotThrow(F);
469 Changed |= setDoesNotCapture(F, 1);
470 Changed |= setOnlyReadsMemory(F, 1);
468 Changed |= setDoesNotCapture(F, 0);
469 Changed |= setOnlyReadsMemory(F, 0);
471470 return Changed;
472471 case LibFunc_unsetenv:
473472 Changed |= setDoesNotThrow(F);
474 Changed |= setDoesNotCapture(F, 1);
475 Changed |= setOnlyReadsMemory(F, 1);
473 Changed |= setDoesNotCapture(F, 0);
474 Changed |= setOnlyReadsMemory(F, 0);
476475 return Changed;
477476 case LibFunc_utime:
478477 case LibFunc_utimes:
479478 Changed |= setDoesNotThrow(F);
480 Changed |= setDoesNotCapture(F, 1);
481 Changed |= setDoesNotCapture(F, 2);
482 Changed |= setOnlyReadsMemory(F, 1);
483 Changed |= setOnlyReadsMemory(F, 2);
479 Changed |= setDoesNotCapture(F, 0);
480 Changed |= setDoesNotCapture(F, 1);
481 Changed |= setOnlyReadsMemory(F, 0);
482 Changed |= setOnlyReadsMemory(F, 1);
484483 return Changed;
485484 case LibFunc_putc:
486485 Changed |= setDoesNotThrow(F);
487 Changed |= setDoesNotCapture(F, 2);
486 Changed |= setDoesNotCapture(F, 1);
488487 return Changed;
489488 case LibFunc_puts:
490489 case LibFunc_printf:
491490 case LibFunc_perror:
492491 Changed |= setDoesNotThrow(F);
493 Changed |= setDoesNotCapture(F, 1);
494 Changed |= setOnlyReadsMemory(F, 1);
492 Changed |= setDoesNotCapture(F, 0);
493 Changed |= setOnlyReadsMemory(F, 0);
495494 return Changed;
496495 case LibFunc_pread:
497496 // May throw; "pread" is a valid pthread cancellation point.
498 Changed |= setDoesNotCapture(F, 2);
497 Changed |= setDoesNotCapture(F, 1);
499498 return Changed;
500499 case LibFunc_pwrite:
501500 // May throw; "pwrite" is a valid pthread cancellation point.
501 Changed |= setDoesNotCapture(F, 1);
502 Changed |= setOnlyReadsMemory(F, 1);
503 return Changed;
504 case LibFunc_putchar:
505 Changed |= setDoesNotThrow(F);
506 return Changed;
507 case LibFunc_popen:
508 Changed |= setDoesNotThrow(F);
509 Changed |= setRetDoesNotAlias(F);
510 Changed |= setDoesNotCapture(F, 0);
511 Changed |= setDoesNotCapture(F, 1);
512 Changed |= setOnlyReadsMemory(F, 0);
513 Changed |= setOnlyReadsMemory(F, 1);
514 return Changed;
515 case LibFunc_pclose:
516 Changed |= setDoesNotThrow(F);
517 Changed |= setDoesNotCapture(F, 0);
518 return Changed;
519 case LibFunc_vscanf:
520 Changed |= setDoesNotThrow(F);
521 Changed |= setDoesNotCapture(F, 0);
522 Changed |= setOnlyReadsMemory(F, 0);
523 return Changed;
524 case LibFunc_vsscanf:
525 Changed |= setDoesNotThrow(F);
526 Changed |= setDoesNotCapture(F, 0);
527 Changed |= setDoesNotCapture(F, 1);
528 Changed |= setOnlyReadsMemory(F, 0);
529 Changed |= setOnlyReadsMemory(F, 1);
530 return Changed;
531 case LibFunc_vfscanf:
532 Changed |= setDoesNotThrow(F);
533 Changed |= setDoesNotCapture(F, 0);
534 Changed |= setDoesNotCapture(F, 1);
535 Changed |= setOnlyReadsMemory(F, 1);
536 return Changed;
537 case LibFunc_valloc:
538 Changed |= setDoesNotThrow(F);
539 Changed |= setRetDoesNotAlias(F);
540 return Changed;
541 case LibFunc_vprintf:
542 Changed |= setDoesNotThrow(F);
543 Changed |= setDoesNotCapture(F, 0);
544 Changed |= setOnlyReadsMemory(F, 0);
545 return Changed;
546 case LibFunc_vfprintf:
547 case LibFunc_vsprintf:
548 Changed |= setDoesNotThrow(F);
549 Changed |= setDoesNotCapture(F, 0);
550 Changed |= setDoesNotCapture(F, 1);
551 Changed |= setOnlyReadsMemory(F, 1);
552 return Changed;
553 case LibFunc_vsnprintf:
554 Changed |= setDoesNotThrow(F);
555 Changed |= setDoesNotCapture(F, 0);
502556 Changed |= setDoesNotCapture(F, 2);
503557 Changed |= setOnlyReadsMemory(F, 2);
504558 return Changed;
505 case LibFunc_putchar:
506 Changed |= setDoesNotThrow(F);
507 return Changed;
508 case LibFunc_popen:
509 Changed |= setDoesNotThrow(F);
510 Changed |= setDoesNotAlias(F, 0);
511 Changed |= setDoesNotCapture(F, 1);
512 Changed |= setDoesNotCapture(F, 2);
513 Changed |= setOnlyReadsMemory(F, 1);
514 Changed |= setOnlyReadsMemory(F, 2);
515 return Changed;
516 case LibFunc_pclose:
517 Changed |= setDoesNotThrow(F);
518 Changed |= setDoesNotCapture(F, 1);
519 return Changed;
520 case LibFunc_vscanf:
521 Changed |= setDoesNotThrow(F);
522 Changed |= setDoesNotCapture(F, 1);
523 Changed |= setOnlyReadsMemory(F, 1);
524 return Changed;
525 case LibFunc_vsscanf:
526 Changed |= setDoesNotThrow(F);
527 Changed |= setDoesNotCapture(F, 1);
528 Changed |= setDoesNotCapture(F, 2);
529 Changed |= setOnlyReadsMemory(F, 1);
530 Changed |= setOnlyReadsMemory(F, 2);
531 return Changed;
532 case LibFunc_vfscanf:
533 Changed |= setDoesNotThrow(F);
534 Changed |= setDoesNotCapture(F, 1);
535 Changed |= setDoesNotCapture(F, 2);
536 Changed |= setOnlyReadsMemory(F, 2);
537 return Changed;
538 case LibFunc_valloc:
539 Changed |= setDoesNotThrow(F);
540 Changed |= setDoesNotAlias(F, 0);
541 return Changed;
542 case LibFunc_vprintf:
543 Changed |= setDoesNotThrow(F);
544 Changed |= setDoesNotCapture(F, 1);
545 Changed |= setOnlyReadsMemory(F, 1);
546 return Changed;
547 case LibFunc_vfprintf:
548 case LibFunc_vsprintf:
549 Changed |= setDoesNotThrow(F);
550 Changed |= setDoesNotCapture(F, 1);
551 Changed |= setDoesNotCapture(F, 2);
552 Changed |= setOnlyReadsMemory(F, 2);
553 return Changed;
554 case LibFunc_vsnprintf:
555 Changed |= setDoesNotThrow(F);
556 Changed |= setDoesNotCapture(F, 1);
557 Changed |= setDoesNotCapture(F, 3);
558 Changed |= setOnlyReadsMemory(F, 3);
559 return Changed;
560559 case LibFunc_open:
561560 // May throw; "open" is a valid pthread cancellation point.
562 Changed |= setDoesNotCapture(F, 1);
563 Changed |= setOnlyReadsMemory(F, 1);
561 Changed |= setDoesNotCapture(F, 0);
562 Changed |= setOnlyReadsMemory(F, 0);
564563 return Changed;
565564 case LibFunc_opendir:
566565 Changed |= setDoesNotThrow(F);
567 Changed |= setDoesNotAlias(F, 0);
568 Changed |= setDoesNotCapture(F, 1);
569 Changed |= setOnlyReadsMemory(F, 1);
566 Changed |= setRetDoesNotAlias(F);
567 Changed |= setDoesNotCapture(F, 0);
568 Changed |= setOnlyReadsMemory(F, 0);
570569 return Changed;
571570 case LibFunc_tmpfile:
572571 Changed |= setDoesNotThrow(F);
573 Changed |= setDoesNotAlias(F, 0);
572 Changed |= setRetDoesNotAlias(F);
574573 return Changed;
575574 case LibFunc_times:
576575 Changed |= setDoesNotThrow(F);
577 Changed |= setDoesNotCapture(F, 1);
576 Changed |= setDoesNotCapture(F, 0);
578577 return Changed;
579578 case LibFunc_htonl:
580579 case LibFunc_htons:
585584 return Changed;
586585 case LibFunc_lstat:
587586 Changed |= setDoesNotThrow(F);
588 Changed |= setDoesNotCapture(F, 1);
589 Changed |= setDoesNotCapture(F, 2);
590 Changed |= setOnlyReadsMemory(F, 1);
587 Changed |= setDoesNotCapture(F, 0);
588 Changed |= setDoesNotCapture(F, 1);
589 Changed |= setOnlyReadsMemory(F, 0);
591590 return Changed;
592591 case LibFunc_lchown:
593592 Changed |= setDoesNotThrow(F);
594 Changed |= setDoesNotCapture(F, 1);
595 Changed |= setOnlyReadsMemory(F, 1);
593 Changed |= setDoesNotCapture(F, 0);
594 Changed |= setOnlyReadsMemory(F, 0);
596595 return Changed;
597596 case LibFunc_qsort:
598597 // May throw; places call through function pointer.
599 Changed |= setDoesNotCapture(F, 4);
598 Changed |= setDoesNotCapture(F, 3);
600599 return Changed;
601600 case LibFunc_dunder_strdup:
602601 case LibFunc_dunder_strndup:
603602 Changed |= setDoesNotThrow(F);
604 Changed |= setDoesNotAlias(F, 0);
605 Changed |= setDoesNotCapture(F, 1);
606 Changed |= setOnlyReadsMemory(F, 1);
603 Changed |= setRetDoesNotAlias(F);
604 Changed |= setDoesNotCapture(F, 0);
605 Changed |= setOnlyReadsMemory(F, 0);
607606 return Changed;
608607 case LibFunc_dunder_strtok_r:
609608 Changed |= setDoesNotThrow(F);
610 Changed |= setDoesNotCapture(F, 2);
611 Changed |= setOnlyReadsMemory(F, 2);
609 Changed |= setDoesNotCapture(F, 1);
610 Changed |= setOnlyReadsMemory(F, 1);
612611 return Changed;
613612 case LibFunc_under_IO_getc:
614613 Changed |= setDoesNotThrow(F);
615 Changed |= setDoesNotCapture(F, 1);
614 Changed |= setDoesNotCapture(F, 0);
616615 return Changed;
617616 case LibFunc_under_IO_putc:
618617 Changed |= setDoesNotThrow(F);
619 Changed |= setDoesNotCapture(F, 2);
618 Changed |= setDoesNotCapture(F, 1);
620619 return Changed;
621620 case LibFunc_dunder_isoc99_scanf:
622621 Changed |= setDoesNotThrow(F);
623 Changed |= setDoesNotCapture(F, 1);
624 Changed |= setOnlyReadsMemory(F, 1);
622 Changed |= setDoesNotCapture(F, 0);
623 Changed |= setOnlyReadsMemory(F, 0);
625624 return Changed;
626625 case LibFunc_stat64:
627626 case LibFunc_lstat64:
628627 case LibFunc_statvfs64:
629628 Changed |= setDoesNotThrow(F);
630 Changed |= setDoesNotCapture(F, 1);
631 Changed |= setDoesNotCapture(F, 2);
632 Changed |= setOnlyReadsMemory(F, 1);
629 Changed |= setDoesNotCapture(F, 0);
630 Changed |= setDoesNotCapture(F, 1);
631 Changed |= setOnlyReadsMemory(F, 0);
633632 return Changed;
634633 case LibFunc_dunder_isoc99_sscanf:
635634 Changed |= setDoesNotThrow(F);
636 Changed |= setDoesNotCapture(F, 1);
637 Changed |= setDoesNotCapture(F, 2);
638 Changed |= setOnlyReadsMemory(F, 1);
639 Changed |= setOnlyReadsMemory(F, 2);
635 Changed |= setDoesNotCapture(F, 0);
636 Changed |= setDoesNotCapture(F, 1);
637 Changed |= setOnlyReadsMemory(F, 0);
638 Changed |= setOnlyReadsMemory(F, 1);
640639 return Changed;
641640 case LibFunc_fopen64:
642641 Changed |= setDoesNotThrow(F);
643 Changed |= setDoesNotAlias(F, 0);
644 Changed |= setDoesNotCapture(F, 1);
645 Changed |= setDoesNotCapture(F, 2);
646 Changed |= setOnlyReadsMemory(F, 1);
647 Changed |= setOnlyReadsMemory(F, 2);
642 Changed |= setRetDoesNotAlias(F);
643 Changed |= setDoesNotCapture(F, 0);
644 Changed |= setDoesNotCapture(F, 1);
645 Changed |= setOnlyReadsMemory(F, 0);
646 Changed |= setOnlyReadsMemory(F, 1);
648647 return Changed;
649648 case LibFunc_fseeko64:
650649 case LibFunc_ftello64:
651650 Changed |= setDoesNotThrow(F);
652 Changed |= setDoesNotCapture(F, 1);
651 Changed |= setDoesNotCapture(F, 0);
653652 return Changed;
654653 case LibFunc_tmpfile64:
655654 Changed |= setDoesNotThrow(F);
656 Changed |= setDoesNotAlias(F, 0);
655 Changed |= setRetDoesNotAlias(F);
657656 return Changed;
658657 case LibFunc_fstat64:
659658 case LibFunc_fstatvfs64:
660659 Changed |= setDoesNotThrow(F);
661 Changed |= setDoesNotCapture(F, 2);
660 Changed |= setDoesNotCapture(F, 1);
662661 return Changed;
663662 case LibFunc_open64:
664663 // May throw; "open" is a valid pthread cancellation point.
665 Changed |= setDoesNotCapture(F, 1);
666 Changed |= setOnlyReadsMemory(F, 1);
664 Changed |= setDoesNotCapture(F, 0);
665 Changed |= setOnlyReadsMemory(F, 0);
667666 return Changed;
668667 case LibFunc_gettimeofday:
669668 // Currently some platforms have the restrict keyword on the arguments to
670669 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
671670 // arguments.
672671 Changed |= setDoesNotThrow(F);
673 Changed |= setDoesNotCapture(F, 1);
674 Changed |= setDoesNotCapture(F, 2);
672 Changed |= setDoesNotCapture(F, 0);
673 Changed |= setDoesNotCapture(F, 1);
675674 return Changed;
676675 case LibFunc_Znwj: // new(unsigned int)
677676 case LibFunc_Znwm: // new(unsigned long)
682681 case LibFunc_msvc_new_array_int: // new[](unsigned int)
683682 case LibFunc_msvc_new_array_longlong: // new[](unsigned long long)
684683 // Operator new always returns a nonnull noalias pointer
685 Changed |= setNonNull(F, AttributeList::ReturnIndex);
686 Changed |= setDoesNotAlias(F, AttributeList::ReturnIndex);
684 Changed |= setRetNonNull(F);
685 Changed |= setRetDoesNotAlias(F);
687686 return Changed;
688687 //TODO: add LibFunc entries for:
689688 //case LibFunc_memset_pattern4:
690689 //case LibFunc_memset_pattern8:
691690 case LibFunc_memset_pattern16:
692691 Changed |= setOnlyAccessesArgMemory(F);
693 Changed |= setDoesNotCapture(F, 1);
694 Changed |= setDoesNotCapture(F, 2);
695 Changed |= setOnlyReadsMemory(F, 2);
692 Changed |= setDoesNotCapture(F, 0);
693 Changed |= setDoesNotCapture(F, 1);
694 Changed |= setOnlyReadsMemory(F, 1);
696695 return Changed;
697696 // int __nvvm_reflect(const char *)
698697 case LibFunc_nvvm_reflect:
536536 if (isa(EndPtr)) {
537537 // With a null EndPtr, this function won't capture the main argument.
538538 // It would be readonly too, except that it still may write to errno.
539 CI->addAttribute(1, Attribute::NoCapture);
539 CI->addParamAttr(0, Attribute::NoCapture);
540540 }
541541
542542 return nullptr;
1919 LLVMContext Context;
2020 ModuleBuilder MB(Context, "x86_64-apple-macosx10.10", "");
2121 Function *F = MB.createFunctionDecl("");
22 SmallVector Attrs;
23 Attrs.push_back(
24 AttributeList::get(MB.getModule()->getContext(), 1U,
25 AttrBuilder().addAttribute(Attribute::StructRet)));
26 Attrs.push_back(
27 AttributeList::get(MB.getModule()->getContext(), 2U,
28 AttrBuilder().addAttribute(Attribute::ByVal)));
29 Attrs.push_back(
30 AttributeList::get(MB.getModule()->getContext(), ~0U,
31 AttrBuilder().addAttribute(Attribute::NoUnwind)));
32 F->setAttributes(AttributeList::get(MB.getModule()->getContext(), Attrs));
22 AttributeSet FnAttrs = AttributeSet::get(
23 Context, AttrBuilder().addAttribute(Attribute::NoUnwind));
24 AttributeSet RetAttrs; // None
25 AttributeSet ArgAttrs[2] = {
26 AttributeSet::get(Context,
27 AttrBuilder().addAttribute(Attribute::StructRet)),
28 AttributeSet::get(Context, AttrBuilder().addAttribute(Attribute::ByVal)),
29 };
30 F->setAttributes(AttributeList::get(Context, FnAttrs, RetAttrs, ArgAttrs));
3331
3432 auto ImplPtr = orc::createImplPointer(*F->getType(), *MB.getModule(), "", nullptr);
3533 orc::makeStub(*F, *ImplPtr);
562562 if (ae) {
563563 while (ai != ae) {
564564 unsigned argNo = intrinsic.ArgumentAttributes[ai].first;
565
566 OS << " const Attribute::AttrKind AttrParam" << argNo + 1 <<"[]= {";
565 unsigned attrIdx = argNo + 1; // Must match AttributeList::FirstArgIndex
566
567 OS << " const Attribute::AttrKind AttrParam" << attrIdx << "[]= {";
567568 bool addComma = false;
568569
569570 do {
604605 } while (ai != ae && intrinsic.ArgumentAttributes[ai].first == argNo);
605606 OS << "};\n";
606607 OS << " AS[" << numAttrs++ << "] = AttributeList::get(C, "
607 << argNo + 1 << ", AttrParam" << argNo + 1 << ");\n";
608 << attrIdx << ", AttrParam" << attrIdx << ");\n";
608609 }
609610 }
610611