diff options
author | Nikita Popov <npopov@redhat.com> | 2024-08-19 09:55:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-19 09:55:30 +0200 |
commit | 83879f4f5311af334550c54c8279397a8aa33e7b (patch) | |
tree | a15977b9aa9419d24811bb055ccfab67ee34b70e /llvm/lib/Transforms/Utils/SimplifyCFG.cpp | |
parent | 985d64b03accbed8500a85372d716367d89b61be (diff) | |
download | llvm-83879f4f5311af334550c54c8279397a8aa33e7b.zip llvm-83879f4f5311af334550c54c8279397a8aa33e7b.tar.gz llvm-83879f4f5311af334550c54c8279397a8aa33e7b.tar.bz2 |
[SimplifyCFG] Don't block sinking for allocas if no phi created (#104579)
SimplifyCFG sinking currently does not sink loads/stores of allocas,
because historically SROA was unable to handle the resulting IR. Since
then, SROA both learned to speculate loads/stores over selects and phis,
*and* SimplifyCFG sinking has been deferred to the end of the function
simplification pipeline, which means that SROA happens before it.
As such, I believe that this workaround should no longer be necessary.
Given how sensitive SimplifyCFG sinking seems to be, this patch takes a
very conservative step towards removing this, by allowing sinking if we
don't actually need to form a phi over the pointer argument.
This fixes https://github.com/llvm/llvm-project/issues/104567, where
sinking a store to an escaped alloca allows converting a switch into
arithmetic.
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/SimplifyCFG.cpp | 43 |
1 files changed, 21 insertions, 22 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp index 37c8761..ebdf760 100644 --- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1994,28 +1994,6 @@ static bool canSinkInstructions( return false; } - // Because SROA can't handle speculating stores of selects, try not to sink - // loads, stores or lifetime markers of allocas when we'd have to create a - // PHI for the address operand. Also, because it is likely that loads or - // stores of allocas will disappear when Mem2Reg/SROA is run, don't sink - // them. - // This can cause code churn which can have unintended consequences down - // the line - see https://llvm.org/bugs/show_bug.cgi?id=30244. - // FIXME: This is a workaround for a deficiency in SROA - see - // https://llvm.org/bugs/show_bug.cgi?id=30188 - if (isa<StoreInst>(I0) && any_of(Insts, [](const Instruction *I) { - return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts()); - })) - return false; - if (isa<LoadInst>(I0) && any_of(Insts, [](const Instruction *I) { - return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts()); - })) - return false; - if (isLifeTimeMarker(I0) && any_of(Insts, [](const Instruction *I) { - return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts()); - })) - return false; - // For calls to be sinkable, they must all be indirect, or have same callee. // I.e. if we have two direct calls to different callees, we don't want to // turn that into an indirect call. Likewise, if we have an indirect call, @@ -2053,6 +2031,27 @@ static bool canSinkInstructions( return I->getOperand(OI) == I0->getOperand(OI); }; if (!all_of(Insts, SameAsI0)) { + // Because SROA historically couldn't handle speculating stores of + // selects, we try not to sink loads, stores or lifetime markers of + // allocas when we'd have to create a PHI for the address operand. + // TODO: SROA supports speculation for loads and stores now -- remove + // this hack? + if (isa<StoreInst>(I0) && OI == 1 && + any_of(Insts, [](const Instruction *I) { + return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts()); + })) + return false; + if (isa<LoadInst>(I0) && OI == 0 && + any_of(Insts, [](const Instruction *I) { + return isa<AllocaInst>(I->getOperand(0)->stripPointerCasts()); + })) + return false; + if (isLifeTimeMarker(I0) && OI == 1 && + any_of(Insts, [](const Instruction *I) { + return isa<AllocaInst>(I->getOperand(1)->stripPointerCasts()); + })) + return false; + if ((isa<Constant>(Op) && !replacingOperandWithVariableIsCheap(I0, OI)) || !canReplaceOperandWithVariable(I0, OI)) // We can't create a PHI from this GEP. |