Skip to content

Commit eb97b07

Browse files
committed
!fixup address comments, thanks
1 parent 29460f7 commit eb97b07

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

llvm/include/llvm/Analysis/TypeBasedAliasAnalysis.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ class MemoryLocation;
2929

3030
/// A simple AA result that uses TBAA metadata to answer queries.
3131
class TypeBasedAAResult : public AAResultBase {
32+
/// True if type sanitizer is enabled. When TypeSanitizer is used, don't use
33+
/// TBAA information for alias analysis as this might cause us to remove
34+
/// memory accesses that we need to verify at runtime.
35+
bool UsingTypeSanitizer;
36+
3237
public:
38+
TypeBasedAAResult(bool UsingTypeSanitizer)
39+
: UsingTypeSanitizer(UsingTypeSanitizer) {}
40+
3341
/// Handle invalidation events from the new pass manager.
3442
///
3543
/// By definition, this result is stateless and so remains valid.

llvm/lib/Analysis/TypeBasedAliasAnalysis.cpp

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -371,26 +371,10 @@ static bool isStructPathTBAA(const MDNode *MD) {
371371
return isa<MDNode>(MD->getOperand(0)) && MD->getNumOperands() >= 3;
372372
}
373373

374-
// When using the TypeSanitizer, don't use TBAA information for alias analysis.
375-
// This might cause us to remove memory accesses that we need to verify at
376-
// runtime.
377-
static bool usingSanitizeType(const Value *V) {
378-
const Function *F;
379-
380-
if (auto *I = dyn_cast<Instruction>(V))
381-
F = I->getParent()->getParent();
382-
else if (auto *A = dyn_cast<Argument>(V))
383-
F = A->getParent();
384-
else
385-
return false;
386-
387-
return F->hasFnAttribute(Attribute::SanitizeType);
388-
}
389-
390374
AliasResult TypeBasedAAResult::alias(const MemoryLocation &LocA,
391375
const MemoryLocation &LocB,
392376
AAQueryInfo &AAQI, const Instruction *) {
393-
if (!EnableTBAA || usingSanitizeType(LocA.Ptr) || usingSanitizeType(LocB.Ptr))
377+
if (!EnableTBAA || UsingTypeSanitizer || UsingTypeSanitizer)
394378
return AAResultBase::alias(LocA, LocB, AAQI, nullptr);
395379

396380
if (Aliases(LocA.AATags.TBAA, LocB.AATags.TBAA))
@@ -441,7 +425,7 @@ MemoryEffects TypeBasedAAResult::getMemoryEffects(const Function *F) {
441425
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
442426
const MemoryLocation &Loc,
443427
AAQueryInfo &AAQI) {
444-
if (!EnableTBAA || usingSanitizeType(Call))
428+
if (!EnableTBAA || UsingTypeSanitizer)
445429
return AAResultBase::getModRefInfo(Call, Loc, AAQI);
446430

447431
if (const MDNode *L = Loc.AATags.TBAA)
@@ -455,7 +439,7 @@ ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call,
455439
ModRefInfo TypeBasedAAResult::getModRefInfo(const CallBase *Call1,
456440
const CallBase *Call2,
457441
AAQueryInfo &AAQI) {
458-
if (!EnableTBAA || usingSanitizeType(Call1))
442+
if (!EnableTBAA || UsingTypeSanitizer)
459443
return AAResultBase::getModRefInfo(Call1, Call2, AAQI);
460444

461445
if (const MDNode *M1 = Call1->getMetadata(LLVMContext::MD_tbaa))
@@ -722,7 +706,7 @@ bool TypeBasedAAResult::Aliases(const MDNode *A, const MDNode *B) const {
722706
AnalysisKey TypeBasedAA::Key;
723707

724708
TypeBasedAAResult TypeBasedAA::run(Function &F, FunctionAnalysisManager &AM) {
725-
return TypeBasedAAResult();
709+
return TypeBasedAAResult(F.hasFnAttribute(Attribute::SanitizeType));
726710
}
727711

728712
char TypeBasedAAWrapperPass::ID = 0;
@@ -738,7 +722,7 @@ TypeBasedAAWrapperPass::TypeBasedAAWrapperPass() : ImmutablePass(ID) {
738722
}
739723

740724
bool TypeBasedAAWrapperPass::doInitialization(Module &M) {
741-
Result.reset(new TypeBasedAAResult());
725+
Result.reset(new TypeBasedAAResult(false));
742726
return false;
743727
}
744728

llvm/lib/Transforms/Instrumentation/TypeSanitizer.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ struct TypeSanitizer {
110110
TypeNameMapTy &TypeNames, Module &M);
111111

112112
const Triple TargetTriple;
113+
Regex AnonNameRegex;
113114
Type *IntptrTy;
114115
uint64_t PtrShift;
115116
IntegerType *OrdTy;
@@ -122,7 +123,8 @@ struct TypeSanitizer {
122123
} // namespace
123124

124125
TypeSanitizer::TypeSanitizer(Module &M)
125-
: TargetTriple(Triple(M.getTargetTriple())) {
126+
: TargetTriple(Triple(M.getTargetTriple())),
127+
AnonNameRegex("^_ZTS.*N[1-9][0-9]*_GLOBAL__N") {
126128
const DataLayout &DL = M.getDataLayout();
127129
IntptrTy = DL.getIntPtrType(M.getContext());
128130
PtrShift = countr_zero(IntptrTy->getPrimitiveSizeInBits() / 8);
@@ -237,16 +239,6 @@ static std::string encodeName(StringRef Name) {
237239
return Output;
238240
}
239241

240-
static bool isAnonymousNamespaceName(StringRef Name) {
241-
// Types that are in an anonymous namespace are local to this module.
242-
// FIXME: This should really be marked by the frontend in the metadata
243-
// instead of having us guess this from the mangled name. Moreover, the regex
244-
// here can pick up (unlikely) names in the non-reserved namespace (because
245-
// it needs to search into the type to pick up cases where the type in the
246-
// anonymous namespace is a template parameter, etc.).
247-
return AnonNameRegex.match(Name);
248-
}
249-
250242
std::string
251243
TypeSanitizer::getAnonymousStructIdentifier(const MDNode *MD,
252244
TypeNameMapTy &TypeNames) {
@@ -352,7 +344,13 @@ bool TypeSanitizer::generateBaseTypeDescriptor(
352344
TDSubTys.push_back(IntptrTy);
353345
TDSubData.push_back(ConstantInt::get(IntptrTy, Members.size()));
354346

355-
bool ShouldBeComdat = !isAnonymousNamespaceName(NameNode->getString());
347+
// Types that are in an anonymous namespace are local to this module.
348+
// FIXME: This should really be marked by the frontend in the metadata
349+
// instead of having us guess this from the mangled name. Moreover, the regex
350+
// here can pick up (unlikely) names in the non-reserved namespace (because
351+
// it needs to search into the type to pick up cases where the type in the
352+
// anonymous namespace is a template parameter, etc.).
353+
bool ShouldBeComdat = !AnonNameRegex.match(NameNode->getString());
356354
for (auto &Member : Members) {
357355
TDSubTys.push_back(Member.first->getType());
358356
TDSubData.push_back(Member.first);

llvm/unittests/Analysis/AliasSetTrackerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ TEST(AliasSetTracker, AliasUnknownInst) {
6262
TargetLibraryInfoImpl TLII(Trip);
6363
TargetLibraryInfo TLI(TLII);
6464
AAResults AA(TLI);
65-
TypeBasedAAResult TBAAR;
65+
TypeBasedAAResult TBAAR(false);
6666
AA.addAAResult(TBAAR);
6767

6868
// Initialize the alias set tracker for the @test function.

0 commit comments

Comments
 (0)