-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[DebugInfo][RemoveDIs] Have getInsertionPtAfterDef return an iterator #73149
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 all commits
2ee48d5
77ff164
5cf5db4
1519c43
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 |
---|---|---|
|
@@ -596,35 +596,47 @@ void GuardWideningImpl::makeAvailableAt(Value *V, Instruction *Loc) const { | |
} | ||
|
||
// Return Instruction before which we can insert freeze for the value V as close | ||
// to def as possible. If there is no place to add freeze, return nullptr. | ||
static Instruction *getFreezeInsertPt(Value *V, const DominatorTree &DT) { | ||
// to def as possible. If there is no place to add freeze, return empty. | ||
static std::optional<BasicBlock::iterator> | ||
getFreezeInsertPt(Value *V, const DominatorTree &DT) { | ||
auto *I = dyn_cast<Instruction>(V); | ||
if (!I) | ||
return &*DT.getRoot()->getFirstNonPHIOrDbgOrAlloca(); | ||
return DT.getRoot()->getFirstNonPHIOrDbgOrAlloca()->getIterator(); | ||
|
||
auto *Res = I->getInsertionPointAfterDef(); | ||
std::optional<BasicBlock::iterator> Res = I->getInsertionPointAfterDef(); | ||
// If there is no place to add freeze - return nullptr. | ||
if (!Res || !DT.dominates(I, Res)) | ||
return nullptr; | ||
if (!Res || !DT.dominates(I, &**Res)) | ||
return std::nullopt; | ||
|
||
Instruction *ResInst = &**Res; | ||
|
||
// If there is a User dominated by original I, then it should be dominated | ||
// by Freeze instruction as well. | ||
if (any_of(I->users(), [&](User *U) { | ||
Instruction *User = cast<Instruction>(U); | ||
return Res != User && DT.dominates(I, User) && !DT.dominates(Res, User); | ||
return ResInst != User && DT.dominates(I, User) && | ||
!DT.dominates(ResInst, User); | ||
})) | ||
return nullptr; | ||
return std::nullopt; | ||
return Res; | ||
} | ||
|
||
Value *GuardWideningImpl::freezeAndPush(Value *Orig, Instruction *InsertPt) { | ||
if (isGuaranteedNotToBePoison(Orig, nullptr, InsertPt, &DT)) | ||
return Orig; | ||
Instruction *InsertPtAtDef = getFreezeInsertPt(Orig, DT); | ||
if (!InsertPtAtDef) | ||
return new FreezeInst(Orig, "gw.freeze", InsertPt); | ||
if (isa<Constant>(Orig) || isa<GlobalValue>(Orig)) | ||
return new FreezeInst(Orig, "gw.freeze", InsertPtAtDef); | ||
std::optional<BasicBlock::iterator> InsertPtAtDef = | ||
getFreezeInsertPt(Orig, DT); | ||
if (!InsertPtAtDef) { | ||
FreezeInst *FI = new FreezeInst(Orig, "gw.freeze"); | ||
FI->insertBefore(InsertPt); | ||
return FI; | ||
} | ||
if (isa<Constant>(Orig) || isa<GlobalValue>(Orig)) { | ||
BasicBlock::iterator InsertPt = *InsertPtAtDef; | ||
FreezeInst *FI = new FreezeInst(Orig, "gw.freeze"); | ||
FI->insertBefore(*InsertPt->getParent(), InsertPt); | ||
return FI; | ||
} | ||
|
||
SmallSet<Value *, 16> Visited; | ||
SmallVector<Value *, 16> Worklist; | ||
|
@@ -643,8 +655,10 @@ Value *GuardWideningImpl::freezeAndPush(Value *Orig, Instruction *InsertPt) { | |
if (Visited.insert(Def).second) { | ||
if (isGuaranteedNotToBePoison(Def, nullptr, InsertPt, &DT)) | ||
return true; | ||
CacheOfFreezes[Def] = new FreezeInst(Def, Def->getName() + ".gw.fr", | ||
getFreezeInsertPt(Def, DT)); | ||
BasicBlock::iterator InsertPt = *getFreezeInsertPt(Def, DT); | ||
FreezeInst *FI = new FreezeInst(Def, Def->getName() + ".gw.fr"); | ||
FI->insertBefore(*InsertPt->getParent(), InsertPt); | ||
CacheOfFreezes[Def] = FI; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a shame about the added verbosity. Is there anything we can do about that? (Update/add instruction ctors as we go along, etc). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can certainly restore this back to "insert in the constructor", I'm just shying away from landing that right now. IMO it's better to land as one-foul-swoop which breaks downstream builds, but only does it once. |
||
} | ||
|
||
if (CacheOfFreezes.count(Def)) | ||
|
@@ -685,8 +699,9 @@ Value *GuardWideningImpl::freezeAndPush(Value *Orig, Instruction *InsertPt) { | |
|
||
Value *Result = Orig; | ||
for (Value *V : NeedFreeze) { | ||
auto *FreezeInsertPt = getFreezeInsertPt(V, DT); | ||
FreezeInst *FI = new FreezeInst(V, V->getName() + ".gw.fr", FreezeInsertPt); | ||
BasicBlock::iterator FreezeInsertPt = *getFreezeInsertPt(V, DT); | ||
FreezeInst *FI = new FreezeInst(V, V->getName() + ".gw.fr"); | ||
FI->insertBefore(*FreezeInsertPt->getParent(), FreezeInsertPt); | ||
++FreezeAdded; | ||
if (V == Orig) | ||
Result = FI; | ||
|
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.
Should insertBefore be using InsertPtAtDef here instead?
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.
Could do -- the motivation of unwrapping into a local iterator variable was to avoid a double-deref for the first argument to insertBefore -- it always makes me feel like there's something wrong, so I figured the unwrapping would make it more legible + understandable. (YMMV).