@@ -2438,7 +2438,10 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
24382438
24392439 // Some ABIs may result in additional accesses to arguments that may
24402440 // otherwise not be present.
2441+ std::optional<llvm::Attribute::AttrKind> MemAttrForPtrArgs;
2442+ bool AddedPotentialArgAccess = false ;
24412443 auto AddPotentialArgAccess = [&]() {
2444+ AddedPotentialArgAccess = true ;
24422445 llvm::Attribute A = FuncAttrs.getAttribute (llvm::Attribute::Memory);
24432446 if (A.isValid ())
24442447 FuncAttrs.addMemoryAttr (A.getMemoryEffects () |
@@ -2499,11 +2502,13 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
24992502 // gcc specifies that 'const' functions have greater restrictions than
25002503 // 'pure' functions, so they also cannot have infinite loops.
25012504 FuncAttrs.addAttribute (llvm::Attribute::WillReturn);
2505+ MemAttrForPtrArgs = llvm::Attribute::ReadNone;
25022506 } else if (TargetDecl->hasAttr <PureAttr>()) {
25032507 FuncAttrs.addMemoryAttr (llvm::MemoryEffects::readOnly ());
25042508 FuncAttrs.addAttribute (llvm::Attribute::NoUnwind);
25052509 // gcc specifies that 'pure' functions cannot have infinite loops.
25062510 FuncAttrs.addAttribute (llvm::Attribute::WillReturn);
2511+ MemAttrForPtrArgs = llvm::Attribute::ReadOnly;
25072512 } else if (TargetDecl->hasAttr <NoAliasAttr>()) {
25082513 FuncAttrs.addMemoryAttr (llvm::MemoryEffects::inaccessibleOrArgMemOnly ());
25092514 FuncAttrs.addAttribute (llvm::Attribute::NoUnwind);
@@ -3011,6 +3016,27 @@ void CodeGenModule::ConstructAttributeList(StringRef Name,
30113016 }
30123017 assert (ArgNo == FI.arg_size ());
30133018
3019+ ArgNo = 0 ;
3020+ if (AddedPotentialArgAccess && MemAttrForPtrArgs) {
3021+ llvm::FunctionType *FunctionType = FunctionType =
3022+ getTypes ().GetFunctionType (FI);
3023+ for (CGFunctionInfo::const_arg_iterator I = FI.arg_begin (),
3024+ E = FI.arg_end ();
3025+ I != E; ++I, ++ArgNo) {
3026+ if (I->info .isDirect () || I->info .isExpand () ||
3027+ I->info .isCoerceAndExpand ()) {
3028+ unsigned FirstIRArg, NumIRArgs;
3029+ std::tie (FirstIRArg, NumIRArgs) = IRFunctionArgs.getIRArgs (ArgNo);
3030+ for (unsigned i = FirstIRArg; i < FirstIRArg + NumIRArgs; ++i) {
3031+ if (FunctionType->getParamType (i)->isPointerTy ()) {
3032+ ArgAttrs[i] =
3033+ ArgAttrs[i].addAttribute (getLLVMContext (), *MemAttrForPtrArgs);
3034+ }
3035+ }
3036+ }
3037+ }
3038+ }
3039+
30143040 AttrList = llvm::AttributeList::get (
30153041 getLLVMContext (), llvm::AttributeSet::get (getLLVMContext (), FuncAttrs),
30163042 llvm::AttributeSet::get (getLLVMContext (), RetAttrs), ArgAttrs);
0 commit comments