-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[TBAA] Handle bitfields when generating !tbaa.struct metadata. #82922
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
556fcef
da9a942
32be3b7
98b7045
e4e4614
8267594
a2dde72
aa896c9
f372ca9
ef8a494
2f7f2d3
c4bcfeb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ | |
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "CodeGenTBAA.h" | ||
| #include "CGRecordLayout.h" | ||
| #include "CodeGenTypes.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/AST/Attr.h" | ||
| #include "clang/AST/Mangle.h" | ||
|
|
@@ -26,13 +28,14 @@ | |
| #include "llvm/IR/Metadata.h" | ||
| #include "llvm/IR/Module.h" | ||
| #include "llvm/IR/Type.h" | ||
| #include "llvm/Support/Debug.h" | ||
| using namespace clang; | ||
| using namespace CodeGen; | ||
|
|
||
| CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, llvm::Module &M, | ||
| CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes, llvm::Module &M, | ||
| const CodeGenOptions &CGO, | ||
| const LangOptions &Features, MangleContext &MContext) | ||
| : Context(Ctx), Module(M), CodeGenOpts(CGO), | ||
| : Context(Ctx), CGTypes(CGTypes), Module(M), CodeGenOpts(CGO), | ||
| Features(Features), MContext(MContext), MDHelper(M.getContext()), | ||
| Root(nullptr), Char(nullptr) | ||
| {} | ||
|
|
@@ -294,18 +297,49 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset, | |
| return false; | ||
|
|
||
| const ASTRecordLayout &Layout = Context.getASTRecordLayout(RD); | ||
| const CGRecordLayout &CGRL = CGTypes.getCGRecordLayout(RD); | ||
|
|
||
| unsigned idx = 0; | ||
| for (RecordDecl::field_iterator i = RD->field_begin(), | ||
| e = RD->field_end(); i != e; ++i, ++idx) { | ||
| if ((*i)->isZeroSize(Context) || (*i)->isUnnamedBitfield()) | ||
| for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end(); | ||
| i != e;) { | ||
| if ((*i)->isZeroSize(Context)) { | ||
| ++i; | ||
| ++idx; | ||
| continue; | ||
| uint64_t Offset = BaseOffset + | ||
| Layout.getFieldOffset(idx) / Context.getCharWidth(); | ||
| } | ||
|
|
||
| uint64_t Offset = | ||
| BaseOffset + Layout.getFieldOffset(idx) / Context.getCharWidth(); | ||
| QualType FieldQTy = i->getType(); | ||
| // Create a single field for consecutive named bitfields using char as | ||
| // base type. | ||
| if ((*i)->isBitField() && !(*i)->isUnnamedBitfield()) { | ||
| unsigned CurrentBitFieldSize = 0; | ||
| unsigned CurrentBitFieldOffset = CGRL.getBitFieldInfo(*i).Offset; | ||
| while (i != e && (*i)->isBitField() && !(*i)->isUnnamedBitfield()) { | ||
| const CGBitFieldInfo &Info = CGRL.getBitFieldInfo(*i); | ||
| if (CurrentBitFieldSize + CurrentBitFieldOffset != Info.Offset) | ||
| break; | ||
| CurrentBitFieldSize += Info.Size; | ||
|
||
| CurrentBitFieldOffset = Info.Offset; | ||
| ++i; | ||
| ++idx; | ||
| } | ||
| uint64_t Size = | ||
| llvm::divideCeil(CurrentBitFieldSize, Context.getCharWidth()); | ||
| llvm::MDNode *TBAAType = getChar(); | ||
| llvm::MDNode *TBAATag = | ||
| getAccessTagInfo(TBAAAccessInfo(TBAAType, Size)); | ||
| Fields.push_back( | ||
| llvm::MDBuilder::TBAAStructField(Offset, Size, TBAATag)); | ||
| continue; | ||
| } | ||
| if (!CollectFields(Offset, FieldQTy, Fields, | ||
| MayAlias || TypeHasMayAlias(FieldQTy))) | ||
| return false; | ||
|
|
||
| ++i; | ||
| ++idx; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this handles non-zero-length unnamed bitfields correctly; they count as part of a series of bitfields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, test showing that added in 54cff50, thanks!