Please use GitHub pull requests for new patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/Transforms/Utils/MemoryTaggingSupport.cpp
Show All 9 Lines | |||||
// | // | ||||
//===----------------------------------------------------------------------===// | //===----------------------------------------------------------------------===// | ||||
#include "llvm/Transforms/Utils/MemoryTaggingSupport.h" | #include "llvm/Transforms/Utils/MemoryTaggingSupport.h" | ||||
#include "llvm/Analysis/CFG.h" | #include "llvm/Analysis/CFG.h" | ||||
#include "llvm/Analysis/PostDominators.h" | #include "llvm/Analysis/PostDominators.h" | ||||
#include "llvm/Analysis/ValueTracking.h" | #include "llvm/Analysis/ValueTracking.h" | ||||
#include "llvm/IR/BasicBlock.h" | |||||
#include "llvm/IR/IntrinsicInst.h" | #include "llvm/IR/IntrinsicInst.h" | ||||
namespace llvm { | namespace llvm { | ||||
namespace memtag { | namespace memtag { | ||||
namespace { | namespace { | ||||
bool maybeReachableFromEachOther(const SmallVectorImpl<IntrinsicInst *> &Insts, | bool maybeReachableFromEachOther(const SmallVectorImpl<IntrinsicInst *> &Insts, | ||||
const DominatorTree *DT, size_t MaxLifetimes) { | const DominatorTree *DT, size_t MaxLifetimes) { | ||||
// If we have too many lifetime ends, give up, as the algorithm below is N^2. | // If we have too many lifetime ends, give up, as the algorithm below is N^2. | ||||
Show All 15 Lines | bool forAllReachableExits(const DominatorTree &DT, const PostDominatorTree &PDT, | ||||
const Instruction *Start, | const Instruction *Start, | ||||
const SmallVectorImpl<IntrinsicInst *> &Ends, | const SmallVectorImpl<IntrinsicInst *> &Ends, | ||||
const SmallVectorImpl<Instruction *> &RetVec, | const SmallVectorImpl<Instruction *> &RetVec, | ||||
llvm::function_ref<void(Instruction *)> Callback) { | llvm::function_ref<void(Instruction *)> Callback) { | ||||
if (Ends.size() == 1 && PDT.dominates(Ends[0], Start)) { | if (Ends.size() == 1 && PDT.dominates(Ends[0], Start)) { | ||||
Callback(Ends[0]); | Callback(Ends[0]); | ||||
return true; | return true; | ||||
} | } | ||||
SmallPtrSet<BasicBlock *, 2> EndBlocks; | |||||
for (auto *End : Ends) { | |||||
EndBlocks.insert(End->getParent()); | |||||
} | |||||
SmallVector<Instruction *, 8> ReachableRetVec; | SmallVector<Instruction *, 8> ReachableRetVec; | ||||
unsigned NumCoveredExits = 0; | unsigned NumCoveredExits = 0; | ||||
for (auto *RI : RetVec) { | for (auto *RI : RetVec) { | ||||
if (!isPotentiallyReachable(Start, RI, nullptr, &DT)) | if (!isPotentiallyReachable(Start, RI, nullptr, &DT)) | ||||
continue; | continue; | ||||
ReachableRetVec.push_back(RI); | ReachableRetVec.push_back(RI); | ||||
// TODO(fmayer): We don't support diamond shapes, where multiple lifetime | // If there is an end in the same basic block as the return, we know for | ||||
// ends together dominate the RI, but none of them does by itself. | // sure that the return is covered. Otherwise, we can check whether there | ||||
// Check how often this happens and decide whether to support this here. | // is a way to reach the RI from the start of the lifetime without passing | ||||
if (llvm::any_of(Ends, [&](auto *End) { return DT.dominates(End, RI); })) | // through an end. | ||||
if (EndBlocks.count(RI->getParent()) > 0 || | |||||
!isPotentiallyReachable(Start, RI, &EndBlocks, &DT)) { | |||||
++NumCoveredExits; | ++NumCoveredExits; | ||||
} | } | ||||
} | |||||
// If there's a mix of covered and non-covered exits, just put the untag | // If there's a mix of covered and non-covered exits, just put the untag | ||||
// on exits, so we avoid the redundancy of untagging twice. | // on exits, so we avoid the redundancy of untagging twice. | ||||
if (NumCoveredExits == ReachableRetVec.size()) { | if (NumCoveredExits == ReachableRetVec.size()) { | ||||
for (auto *End : Ends) | for (auto *End : Ends) | ||||
Callback(End); | Callback(End); | ||||
} else { | } else { | ||||
for (auto *RI : ReachableRetVec) | for (auto *RI : ReachableRetVec) | ||||
Callback(RI); | Callback(RI); | ||||
▲ Show 20 Lines • Show All 117 Lines • Show Last 20 Lines |