@@ -1875,7 +1875,8 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue,
18751875 SourceLocation Loc) {
18761876 return EmitLoadOfScalar (lvalue.getAddress (), lvalue.isVolatile (),
18771877 lvalue.getType (), Loc, lvalue.getBaseInfo (),
1878- lvalue.getTBAAInfo (), lvalue.isNontemporal ());
1878+ lvalue.getTBAAInfo (), lvalue.isNontemporal (),
1879+ lvalue.isErrno ());
18791880}
18801881
18811882static bool hasBooleanRepresentation (QualType Ty) {
@@ -1910,6 +1911,16 @@ static bool getRangeForType(CodeGenFunction &CGF, QualType Ty,
19101911 return true ;
19111912}
19121913
1914+ static bool maybeDereferencingErrno (const Expr *E) {
1915+ if (auto *CE = dyn_cast<CallExpr>(E))
1916+ if (auto *ICE = dyn_cast<ImplicitCastExpr>(CE->getCallee ());
1917+ ICE && ICE->getCastKind () == CK_FunctionToPointerDecay)
1918+ if (auto *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr ()))
1919+ if (auto *FD = dyn_cast<FunctionDecl>(DRE->getDecl ()))
1920+ return FD->getName () == " __errno_location" ;
1921+ return false ;
1922+ }
1923+
19131924llvm::MDNode *CodeGenFunction::getRangeForLoadFromType (QualType Ty) {
19141925 llvm::APInt Min, End;
19151926 if (!getRangeForType (*this , Ty, Min, End, CGM.getCodeGenOpts ().StrictEnums ,
@@ -1972,11 +1983,11 @@ bool CodeGenFunction::EmitScalarRangeCheck(llvm::Value *Value, QualType Ty,
19721983}
19731984
19741985llvm::Value *CodeGenFunction::EmitLoadOfScalar (Address Addr, bool Volatile,
1975- QualType Ty,
1976- SourceLocation Loc,
1986+ QualType Ty, SourceLocation Loc,
19771987 LValueBaseInfo BaseInfo,
19781988 TBAAAccessInfo TBAAInfo,
1979- bool isNontemporal) {
1989+ bool isNontemporal,
1990+ bool IsErrno) {
19801991 if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr.getBasePointer ()))
19811992 if (GV->isThreadLocal ())
19821993 Addr = Addr.withPointer (Builder.CreateThreadLocalAddress (GV),
@@ -2038,6 +2049,8 @@ llvm::Value *CodeGenFunction::EmitLoadOfScalar(Address Addr, bool Volatile,
20382049 }
20392050
20402051 CGM.DecorateInstructionWithTBAA (Load, TBAAInfo);
2052+ if (IsErrno)
2053+ CGM.DecorateInstructionWithErrnoTBAA (Load);
20412054
20422055 if (EmitScalarRangeCheck (Load, Ty, Loc)) {
20432056 // In order to prevent the optimizer from throwing away the check, don't
@@ -2132,14 +2145,14 @@ static void EmitStoreOfMatrixScalar(llvm::Value *value, LValue lvalue,
21322145 value->getType ()->isVectorTy ());
21332146 CGF.EmitStoreOfScalar (value, Addr, lvalue.isVolatile (), lvalue.getType (),
21342147 lvalue.getBaseInfo (), lvalue.getTBAAInfo (), isInit,
2135- lvalue.isNontemporal ());
2148+ lvalue.isNontemporal (), lvalue. isErrno () );
21362149}
21372150
21382151void CodeGenFunction::EmitStoreOfScalar (llvm::Value *Value, Address Addr,
21392152 bool Volatile, QualType Ty,
21402153 LValueBaseInfo BaseInfo,
2141- TBAAAccessInfo TBAAInfo,
2142- bool isInit , bool isNontemporal ) {
2154+ TBAAAccessInfo TBAAInfo, bool isInit,
2155+ bool isNontemporal , bool IsErrno ) {
21432156 if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr.getBasePointer ()))
21442157 if (GV->isThreadLocal ())
21452158 Addr = Addr.withPointer (Builder.CreateThreadLocalAddress (GV),
@@ -2182,6 +2195,8 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *Value, Address Addr,
21822195 }
21832196
21842197 CGM.DecorateInstructionWithTBAA (Store, TBAAInfo);
2198+ if (IsErrno)
2199+ CGM.DecorateInstructionWithErrnoTBAA (Store);
21852200}
21862201
21872202void CodeGenFunction::EmitStoreOfScalar (llvm::Value *value, LValue lvalue,
@@ -2193,7 +2208,8 @@ void CodeGenFunction::EmitStoreOfScalar(llvm::Value *value, LValue lvalue,
21932208
21942209 EmitStoreOfScalar (value, lvalue.getAddress (), lvalue.isVolatile (),
21952210 lvalue.getType (), lvalue.getBaseInfo (),
2196- lvalue.getTBAAInfo (), isInit, lvalue.isNontemporal ());
2211+ lvalue.getTBAAInfo (), isInit, lvalue.isNontemporal (),
2212+ lvalue.isErrno ());
21972213}
21982214
21992215// Emit a load of a LValue of matrix type. This may require casting the pointer
@@ -3245,6 +3261,8 @@ LValue CodeGenFunction::EmitUnaryOpLValue(const UnaryOperator *E) {
32453261 &TBAAInfo);
32463262 LValue LV = MakeAddrLValue (Addr, T, BaseInfo, TBAAInfo);
32473263 LV.getQuals ().setAddressSpace (ExprTy.getAddressSpace ());
3264+ if (bool IsErrno = maybeDereferencingErrno (E->getSubExpr ()))
3265+ LV.setErrno (IsErrno);
32483266
32493267 // We should not generate __weak write barrier on indirect reference
32503268 // of a pointer to object; as in void foo (__weak id *param); *param = 0;
0 commit comments