Skip to content

Commit 7edf856

Browse files
[clang][CodeGen][AA] Add llvm.errno.tbaa gathering int-compatible TBAA
1 parent 8a71390 commit 7edf856

File tree

9 files changed

+76
-26
lines changed

9 files changed

+76
-26
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

18811882
static 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+
19131924
llvm::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

19741985
llvm::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

21382151
void 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

21872202
void 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;

clang/lib/CodeGen/CGValue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ class LValue {
234234
// this lvalue.
235235
bool Nontemporal : 1;
236236

237+
// Lvalue is a reference to errno.
238+
bool Errno : 1;
239+
237240
LValueBaseInfo BaseInfo;
238241
TBAAAccessInfo TBAAInfo;
239242

@@ -261,6 +264,7 @@ class LValue {
261264
this->ImpreciseLifetime = false;
262265
this->Nontemporal = false;
263266
this->ThreadLocalRef = false;
267+
this->Errno = false;
264268
this->BaseIvarExp = nullptr;
265269
}
266270

@@ -318,6 +322,9 @@ class LValue {
318322
bool isNontemporal() const { return Nontemporal; }
319323
void setNontemporal(bool Value) { Nontemporal = Value; }
320324

325+
bool isErrno() const { return Errno; }
326+
void setErrno(bool Value) { Errno = Value; }
327+
321328
bool isObjCWeak() const {
322329
return Quals.getObjCGCAttr() == Qualifiers::Weak;
323330
}

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4270,15 +4270,17 @@ class CodeGenFunction : public CodeGenTypeCache {
42704270
llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty,
42714271
SourceLocation Loc,
42724272
AlignmentSource Source = AlignmentSource::Type,
4273-
bool isNontemporal = false) {
4273+
bool isNontemporal = false,
4274+
bool IsErrno = false) {
42744275
return EmitLoadOfScalar(Addr, Volatile, Ty, Loc, LValueBaseInfo(Source),
4275-
CGM.getTBAAAccessInfo(Ty), isNontemporal);
4276+
CGM.getTBAAAccessInfo(Ty), isNontemporal, IsErrno);
42764277
}
42774278

42784279
llvm::Value *EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty,
42794280
SourceLocation Loc, LValueBaseInfo BaseInfo,
42804281
TBAAAccessInfo TBAAInfo,
4281-
bool isNontemporal = false);
4282+
bool isNontemporal = false,
4283+
bool IsErrno = false);
42824284

42834285
/// EmitLoadOfScalar - Load a scalar value from an address, taking
42844286
/// care to appropriately convert from the memory representation to
@@ -4289,18 +4291,20 @@ class CodeGenFunction : public CodeGenTypeCache {
42894291
/// EmitStoreOfScalar - Store a scalar value to an address, taking
42904292
/// care to appropriately convert from the memory representation to
42914293
/// the LLVM value representation.
4292-
void EmitStoreOfScalar(llvm::Value *Value, Address Addr,
4293-
bool Volatile, QualType Ty,
4294+
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile,
4295+
QualType Ty,
42944296
AlignmentSource Source = AlignmentSource::Type,
4295-
bool isInit = false, bool isNontemporal = false) {
4297+
bool isInit = false, bool isNontemporal = false,
4298+
bool isErrno = false) {
42964299
EmitStoreOfScalar(Value, Addr, Volatile, Ty, LValueBaseInfo(Source),
4297-
CGM.getTBAAAccessInfo(Ty), isInit, isNontemporal);
4300+
CGM.getTBAAAccessInfo(Ty), isInit, isNontemporal,
4301+
isErrno);
42984302
}
42994303

4300-
void EmitStoreOfScalar(llvm::Value *Value, Address Addr,
4301-
bool Volatile, QualType Ty,
4302-
LValueBaseInfo BaseInfo, TBAAAccessInfo TBAAInfo,
4303-
bool isInit = false, bool isNontemporal = false);
4304+
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile,
4305+
QualType Ty, LValueBaseInfo BaseInfo,
4306+
TBAAAccessInfo TBAAInfo, bool isInit = false,
4307+
bool isNontemporal = false, bool isErrno = false);
43044308

43054309
/// EmitStoreOfScalar - Store a scalar value to an address, taking
43064310
/// care to appropriately convert from the memory representation to

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,13 @@ void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
15711571
Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
15721572
}
15731573

1574+
void CodeGenModule::DecorateInstructionWithErrnoTBAA(llvm::Instruction *Inst) {
1575+
if (TBAA) {
1576+
llvm::MDNode *IntegerNode = TBAA->getTypeInfo(Context.IntTy);
1577+
Inst->setMetadata(llvm::LLVMContext::MD_errno_tbaa, IntegerNode);
1578+
}
1579+
}
1580+
15741581
void CodeGenModule::DecorateInstructionWithInvariantGroup(
15751582
llvm::Instruction *I, const CXXRecordDecl *RD) {
15761583
I->setMetadata(llvm::LLVMContext::MD_invariant_group,

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ class CodeGenModule : public CodeGenTypeCache {
903903
void DecorateInstructionWithInvariantGroup(llvm::Instruction *I,
904904
const CXXRecordDecl *RD);
905905

906+
/// Adorn the instruction with a !llvm.errno.tbaa tag.
907+
void DecorateInstructionWithErrnoTBAA(llvm::Instruction *Inst);
908+
906909
/// Emit the given number of characters as a value of type size_t.
907910
llvm::ConstantInt *getSize(CharUnits numChars);
908911

llvm/include/llvm/IR/FixedMetadataKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,4 @@ LLVM_FIXED_MD_KIND(MD_DIAssignID, "DIAssignID", 38)
5353
LLVM_FIXED_MD_KIND(MD_coro_outside_frame, "coro.outside.frame", 39)
5454
LLVM_FIXED_MD_KIND(MD_mmra, "mmra", 40)
5555
LLVM_FIXED_MD_KIND(MD_noalias_addrspace, "noalias.addrspace", 41)
56+
LLVM_FIXED_MD_KIND(MD_errno_tbaa, "llvm.errno.tbaa", 42)

llvm/include/llvm/IR/Metadata.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -763,18 +763,18 @@ class MDString : public Metadata {
763763
/// memory access used by the alias-analysis infrastructure.
764764
struct AAMDNodes {
765765
explicit AAMDNodes() = default;
766-
explicit AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N)
767-
: TBAA(T), TBAAStruct(TS), Scope(S), NoAlias(N) {}
766+
explicit AAMDNodes(MDNode *T, MDNode *TS, MDNode *S, MDNode *N, MDNode *ET)
767+
: TBAA(T), TBAAStruct(TS), Scope(S), NoAlias(N), ErrnoTBAA(ET) {}
768768

769769
bool operator==(const AAMDNodes &A) const {
770770
return TBAA == A.TBAA && TBAAStruct == A.TBAAStruct && Scope == A.Scope &&
771-
NoAlias == A.NoAlias;
771+
NoAlias == A.NoAlias && ErrnoTBAA == A.ErrnoTBAA;
772772
}
773773

774774
bool operator!=(const AAMDNodes &A) const { return !(*this == A); }
775775

776776
explicit operator bool() const {
777-
return TBAA || TBAAStruct || Scope || NoAlias;
777+
return TBAA || TBAAStruct || Scope || NoAlias || ErrnoTBAA;
778778
}
779779

780780
/// The tag for type-based alias analysis.
@@ -788,6 +788,8 @@ struct AAMDNodes {
788788

789789
/// The tag specifying the noalias scope.
790790
MDNode *NoAlias = nullptr;
791+
792+
MDNode *ErrnoTBAA = nullptr;
791793

792794
// Shift tbaa Metadata node to start off bytes later
793795
static MDNode *shiftTBAA(MDNode *M, size_t off);
@@ -810,6 +812,7 @@ struct AAMDNodes {
810812
Result.TBAAStruct = Other.TBAAStruct == TBAAStruct ? TBAAStruct : nullptr;
811813
Result.Scope = Other.Scope == Scope ? Scope : nullptr;
812814
Result.NoAlias = Other.NoAlias == NoAlias ? NoAlias : nullptr;
815+
Result.ErrnoTBAA = Other.ErrnoTBAA == ErrnoTBAA ? ErrnoTBAA : nullptr;
813816
return Result;
814817
}
815818

@@ -822,6 +825,7 @@ struct AAMDNodes {
822825
TBAAStruct ? shiftTBAAStruct(TBAAStruct, Offset) : nullptr;
823826
Result.Scope = Scope;
824827
Result.NoAlias = NoAlias;
828+
Result.ErrnoTBAA = nullptr;
825829
return Result;
826830
}
827831

@@ -837,6 +841,7 @@ struct AAMDNodes {
837841
Result.TBAAStruct = TBAAStruct;
838842
Result.Scope = Scope;
839843
Result.NoAlias = NoAlias;
844+
Result.ErrnoTBAA = nullptr;
840845
return Result;
841846
}
842847

@@ -865,19 +870,20 @@ template<>
865870
struct DenseMapInfo<AAMDNodes> {
866871
static inline AAMDNodes getEmptyKey() {
867872
return AAMDNodes(DenseMapInfo<MDNode *>::getEmptyKey(),
868-
nullptr, nullptr, nullptr);
873+
nullptr, nullptr, nullptr, nullptr);
869874
}
870875

871876
static inline AAMDNodes getTombstoneKey() {
872877
return AAMDNodes(DenseMapInfo<MDNode *>::getTombstoneKey(),
873-
nullptr, nullptr, nullptr);
878+
nullptr, nullptr, nullptr, nullptr);
874879
}
875880

876881
static unsigned getHashValue(const AAMDNodes &Val) {
877882
return DenseMapInfo<MDNode *>::getHashValue(Val.TBAA) ^
878883
DenseMapInfo<MDNode *>::getHashValue(Val.TBAAStruct) ^
879884
DenseMapInfo<MDNode *>::getHashValue(Val.Scope) ^
880-
DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias);
885+
DenseMapInfo<MDNode *>::getHashValue(Val.NoAlias) ^
886+
DenseMapInfo<MDNode *>::getHashValue(Val.ErrnoTBAA);
881887
}
882888

883889
static bool isEqual(const AAMDNodes &LHS, const AAMDNodes &RHS) {

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ AAMDNodes AAMDNodes::merge(const AAMDNodes &Other) const {
525525
Result.TBAAStruct = nullptr;
526526
Result.Scope = MDNode::getMostGenericAliasScope(Scope, Other.Scope);
527527
Result.NoAlias = MDNode::intersect(NoAlias, Other.NoAlias);
528+
Result.ErrnoTBAA = nullptr;
528529
return Result;
529530
}
530531

@@ -533,6 +534,7 @@ AAMDNodes AAMDNodes::concat(const AAMDNodes &Other) const {
533534
Result.TBAA = Result.TBAAStruct = nullptr;
534535
Result.Scope = MDNode::getMostGenericAliasScope(Scope, Other.Scope);
535536
Result.NoAlias = MDNode::intersect(NoAlias, Other.NoAlias);
537+
Result.ErrnoTBAA = nullptr;
536538
return Result;
537539
}
538540

llvm/lib/IR/Metadata.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,7 @@ AAMDNodes Instruction::getAAMetadata() const {
17571757
Result.TBAAStruct = Info.lookup(LLVMContext::MD_tbaa_struct);
17581758
Result.Scope = Info.lookup(LLVMContext::MD_alias_scope);
17591759
Result.NoAlias = Info.lookup(LLVMContext::MD_noalias);
1760+
Result.ErrnoTBAA = Info.lookup(LLVMContext::MD_errno_tbaa);
17601761
}
17611762
return Result;
17621763
}
@@ -1766,6 +1767,7 @@ void Instruction::setAAMetadata(const AAMDNodes &N) {
17661767
setMetadata(LLVMContext::MD_tbaa_struct, N.TBAAStruct);
17671768
setMetadata(LLVMContext::MD_alias_scope, N.Scope);
17681769
setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1770+
setMetadata(LLVMContext::MD_errno_tbaa, N.ErrnoTBAA);
17691771
}
17701772

17711773
void Instruction::setNoSanitizeMetadata() {

0 commit comments

Comments
 (0)