Skip to content

Commit 7ce61db

Browse files
committed
Merging r326404:
------------------------------------------------------------------------ r326404 | rnk | 2018-02-28 17:19:18 -0800 (Wed, 28 Feb 2018) | 14 lines [IPSCCP] do not break musttail invariant (PR36485) Do not replace results of `musttail` calls with a constant if the call itself can't be removed. Do not zap returns of `musttail` callees, if the call site can't be removed and replaced with a constant. Do not zap returns of `musttail`-calling blocks, this breaks invariant too. Patch by Fedor Indutny Differential Revision: https://reviews.llvm.org/D43695 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_60@329855 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 84bc444 commit 7ce61db

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

lib/Transforms/Scalar/SCCP.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
223223
/// represented here for efficient lookup.
224224
SmallPtrSet<Function *, 16> MRVFunctionsTracked;
225225

226+
/// MustTailFunctions - Each function here is a callee of non-removable
227+
/// musttail call site.
228+
SmallPtrSet<Function *, 16> MustTailCallees;
229+
226230
/// TrackingIncomingArguments - This is the set of functions for whose
227231
/// arguments we make optimistic assumptions about and try to prove as
228232
/// constants.
@@ -289,6 +293,18 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
289293
TrackedRetVals.insert(std::make_pair(F, LatticeVal()));
290294
}
291295

296+
/// AddMustTailCallee - If the SCCP solver finds that this function is called
297+
/// from non-removable musttail call site.
298+
void AddMustTailCallee(Function *F) {
299+
MustTailCallees.insert(F);
300+
}
301+
302+
/// Returns true if the given function is called from non-removable musttail
303+
/// call site.
304+
bool isMustTailCallee(Function *F) {
305+
return MustTailCallees.count(F);
306+
}
307+
292308
void AddArgumentTrackedFunction(Function *F) {
293309
TrackingIncomingArguments.insert(F);
294310
}
@@ -358,6 +374,12 @@ class SCCPSolver : public InstVisitor<SCCPSolver> {
358374
return MRVFunctionsTracked;
359375
}
360376

377+
/// getMustTailCallees - Get the set of functions which are called
378+
/// from non-removable musttail call sites.
379+
const SmallPtrSet<Function *, 16> getMustTailCallees() {
380+
return MustTailCallees;
381+
}
382+
361383
/// markOverdefined - Mark the specified value overdefined. This
362384
/// works with both scalars and structs.
363385
void markOverdefined(Value *V) {
@@ -1672,6 +1694,23 @@ static bool tryToReplaceWithConstant(SCCPSolver &Solver, Value *V) {
16721694
IV.isConstant() ? IV.getConstant() : UndefValue::get(V->getType());
16731695
}
16741696
assert(Const && "Constant is nullptr here!");
1697+
1698+
// Replacing `musttail` instructions with constant breaks `musttail` invariant
1699+
// unless the call itself can be removed
1700+
CallInst *CI = dyn_cast<CallInst>(V);
1701+
if (CI && CI->isMustTailCall() && !isInstructionTriviallyDead(CI)) {
1702+
CallSite CS(CI);
1703+
Function *F = CS.getCalledFunction();
1704+
1705+
// Don't zap returns of the callee
1706+
if (F)
1707+
Solver.AddMustTailCallee(F);
1708+
1709+
DEBUG(dbgs() << " Can\'t treat the result of musttail call : " << *CI
1710+
<< " as a constant\n");
1711+
return false;
1712+
}
1713+
16751714
DEBUG(dbgs() << " Constant: " << *Const << " = " << *V << '\n');
16761715

16771716
// Replaces all of the uses of a variable with uses of the constant.
@@ -1802,10 +1841,25 @@ static void findReturnsToZap(Function &F,
18021841
if (!Solver.isArgumentTrackedFunction(&F))
18031842
return;
18041843

1805-
for (BasicBlock &BB : F)
1844+
// There is a non-removable musttail call site of this function. Zapping
1845+
// returns is not allowed.
1846+
if (Solver.isMustTailCallee(&F)) {
1847+
DEBUG(dbgs() << "Can't zap returns of the function : " << F.getName()
1848+
<< " due to present musttail call of it\n");
1849+
return;
1850+
}
1851+
1852+
for (BasicBlock &BB : F) {
1853+
if (CallInst *CI = BB.getTerminatingMustTailCall()) {
1854+
DEBUG(dbgs() << "Can't zap return of the block due to present "
1855+
<< "musttail call : " << *CI << "\n");
1856+
return;
1857+
}
1858+
18061859
if (auto *RI = dyn_cast<ReturnInst>(BB.getTerminator()))
18071860
if (!isa<UndefValue>(RI->getOperand(0)))
18081861
ReturnsToZap.push_back(RI);
1862+
}
18091863
}
18101864

18111865
static bool runIPSCCP(Module &M, const DataLayout &DL,

0 commit comments

Comments
 (0)