summaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Scalar/LoopPredication.cpp
AgeCommit message (Collapse)Author
2024-11-07[Transforms] Remove unused includes (NFC) (#115263)Kazu Hirata
Identified with misc-include-cleaner.
2024-10-16[LLVM] Add `Intrinsic::getDeclarationIfExists` (#112428)Rahul Joshi
Add `Intrinsic::getDeclarationIfExists` to lookup an existing declaration of an intrinsic in a `Module`.
2024-08-03[SCEV] Use const SCEV * explicitly in more places.Florian Hahn
Use const SCEV * explicitly in more places to prepare for https://github.com/llvm/llvm-project/pull/91961. Split off as suggested.
2024-06-24Revert "[IR][NFC] Update IRBuilder to use InsertPosition (#96497)"Stephen Tozer
Reverts the above commit, as it updates a common header function and did not update all callsites: https://lab.llvm.org/buildbot/#/builders/29/builds/382 This reverts commit 6481dc57612671ebe77fe9c34214fba94e1b3b27.
2024-06-24[IR][NFC] Update IRBuilder to use InsertPosition (#96497)Stephen Tozer
Uses the new InsertPosition class (added in #94226) to simplify some of the IRBuilder interface, and removes the need to pass a BasicBlock alongside a BasicBlock::iterator, using the fact that we can now get the parent basic block from the iterator even if it points to the sentinel. This patch removes the BasicBlock argument from each constructor or call to setInsertPoint. This has no functional effect, but later on as we look to remove the `Instruction *InsertBefore` argument from instruction-creation (discussed [here](https://discourse.llvm.org/t/psa-instruction-constructors-changing-to-iterator-only-insertion/77845)), this will simplify the process by allowing us to deprecate the InsertPosition constructor directly and catch all the cases where we use instructions rather than iterators.
2023-11-21[NewPM] Remove LoopPredicationLegacyPass (#72944)Aiden Grossman
This pass isn't used/tested upstream anywhere, so remove it.
2023-09-19[LoopPredication] Fix division by zero in case of zero branch weights (#66506)Danila Malyutin
Treat the case where all branch weights are zero as if there was no profile. Fixes #66382
2023-09-01[llvm] Fix duplicate word typos. NFCFangrui Song
Those fixes were taken from https://reviews.llvm.org/D137338
2023-08-18[NFC][GuardUtils] Add util to extract widenable conditionsAleksandr Popov
This is the next preparation patch to support widenable conditions widening instead of branches widening. We've added parseWidenableGuard util which parses guard condition and collects all checks existing in the expression tree: D157276 Here we are adding util which walks similar way through the expression tree but looks up for widenable condition without collecting the checks. Therefore llvm::extractWidenableCondition could parse widenable branches with arbitrary position of widenable condition in the expression tree. llvm::parseWidenableBranch which is we are going to get rid of is being replaced by llvm::extractWidenableCondition where it's possible. Reviewed By: anna Differential Revision: https://reviews.llvm.org/D157529
2023-08-18[LoopPredication] Rework assumes of widened conditionsAleksandr Popov
Currently after widening br(WC && (c1 && c2)) we insert assume of (c1 && c2) which is joined to WC by And operation. But we are going to support more flexible form of widenable branches where WC could be placed arbitrary in the expression tree, e.g: br(c1 && (c2 && WC)). In that case we won't have (c1 && c2) in the IR. So we need to add explicit (c1 && c2) and then create an assumption of it. Reviewed By: anna Differential Revision: https://reviews.llvm.org/D157502
2023-08-10Revert "[NFC][LoopPredication] Add parsed checks logging"Aleksandr Popov
This reverts commit aa603c41caab63e246f4a4258c8b96e6ea06fdc9. Revert due to LLVM Buildbot failure
2023-08-10[NFC][LoopPredication] Add parsed checks loggingAleksandr Popov
Differential Revision: https://reviews.llvm.org/D157491
2023-08-10[NFC][LoopPredication] Extract guard parsing to GuardUtilsAleksandr Popov
Extract logic of parsing guards and widenable branches in order to reuse it in the GuardWidening Reviewed By: anna Differential Revision: https://reviews.llvm.org/D157276
2023-04-12Revert "[GuardUtils] Add asserts about loop varying widenable conditions"Anna Thomas
This reverts commit 5675757f5fc6e27ce01b3b12bdfd04044df53aa3. Assert maybe too strict. revert and investigate why assert fires.
2023-04-11[GuardUtils] Add asserts about loop varying widenable conditionsAnna Thomas
We have now seen two miscompiles because of widening widenable conditions at incorrect IR points and thereby changing a branch's loop invariant condition to a loop-varying one (see PR60234 and PR61963). This patch adds asserts in common guard utilities that we use for widening to proactively catch these bugs in future. Note that these asserts will not fire if we were to sink a widenable condition from out of a loop into a loop (that's also incorrect for the same reason as above). Tested this without the fix for PR60234 (guard widening miscompile) and confirmed the assert fires. WARNING: Sometimes, the assert can fire if we failed to hoist the invariant condition out of the loop. This is a pass-ordering issue or a limitation in LICM, which would need an investigation. See details in review. Differential Revision: https://reviews.llvm.org/D147752
2023-04-10[LoopPredication] Fix where we generate widened condition. PR61963Anna Thomas
Loop predication's predicateLoopExit pass does two incorrect things: It sinks the widenable call into the loop, thereby converting an invariant condition to a variant one It widens the widenable call at a branch thereby converting the branch into a loop-varying one. The latter is problematic when the branch may have been loop-invariant and prior optimizations (such as indvars) may have relied on this fact, and updated the deopt state accordingly. Now, when we widen this with a loop-varying condition, the deopt state is no longer correct. https://github.com/llvm/llvm-project/issues/61963 fixed. Differential Revision: https://reviews.llvm.org/D147662
2023-03-29[LoopPredication] Fix the LoopPredication by feezing the result of predication.Serguei Katkov
LoopPredication introduces the use of possibly posion value in branch (guard) instruction, so to avoid introducing undefined behavior it should be frozen. Reviewed By: mkazantsev Differential Revision: https://reviews.llvm.org/D146685
2023-02-27[LoopPredication] Account for critical edges when inserting assumes. PR26496Max Kazantsev
Loop predication can insert assumes to preserve knowledge about some facts that may otherwise be lost, because loop predication is a lossy transform. When a guard is represented as branch by widenable condition, it should insert it in the guarded block. However, if the guarded block has other predecessors than the guard block, then the condition might not dominate it. Currently we generate invalid code here. One possible fix here is to split critical edge and insert the assume there, but in this case we should modify CFG, which Loop Predication is not currently doing, and we want to keep it that way. The fix is to handle this case by inserting a Phi which takes `Cond` as input from the guard block and `true` from any other blocks. This is valid in terms of IR and does not introduce any new knowledge if we came from another block. Differential Revision: https://reviews.llvm.org/D144859 Reviewed By: nikic, skatkov
2023-01-19[llvm][ir] Purge MD_prof custom accessorsChristian Ulmann
This commit purges direct accesses to MD_prof metadata and replaces them with the accessors provided from the utility file wherever possible. This commit can be seen as the first step towards switching the branch weights to 64 bits. See post here: https://discourse.llvm.org/t/extend-md-prof-branch-weights-metadata-from-32-to-64-bits/67492 Reviewed By: davidxl, paulkirth Differential Revision: https://reviews.llvm.org/D141393
2023-01-11[NFC] Use TypeSize::geFixedValue() instead of TypeSize::getFixedSize()Guillaume Chatelet
This change is one of a series to implement the discussion from https://reviews.llvm.org/D141134.
2022-12-13[Transforms/Scalar] llvm::Optional => std::optionalFangrui Song
2022-12-04[llvm] Use std::nullopt instead of None in comments (NFC)Kazu Hirata
This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-02[Transforms] Use std::nullopt instead of None (NFC)Kazu Hirata
This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-11-26[Scalar] Use std::optional in LoopPredication.cpp (NFC)Kazu Hirata
This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-11-02[LoopPredication][NFCI] Perform 'visited' check before pushing to worklistMax Kazantsev
This prevents duplicates to be pushed into the stack and hypothetically should reduce memory footprint on ugly cornercases with multiple repeating duplicates in 'and' tree.
2022-10-31[AliasAnalysis] Introduce getModRefInfoMask() as a generalization of ↵Patrick Walton
pointsToConstantMemory(). The pointsToConstantMemory() method returns true only if the memory pointed to by the memory location is globally invariant. However, the LLVM memory model also has the semantic notion of *locally-invariant*: memory that is known to be invariant for the life of the SSA value representing that pointer. The most common example of this is a pointer argument that is marked readonly noalias, which the Rust compiler frequently emits. It'd be desirable for LLVM to treat locally-invariant memory the same way as globally-invariant memory when it's safe to do so. This patch implements that, by introducing the concept of a *ModRefInfo mask*. A ModRefInfo mask is a bound on the Mod/Ref behavior of an instruction that writes to a memory location, based on the knowledge that the memory is globally-constant memory (in which case the mask is NoModRef) or locally-constant memory (in which case the mask is Ref). ModRefInfo values for an instruction can be combined with the ModRefInfo mask by simply using the & operator. Where appropriate, this patch has modified uses of pointsToConstantMemory() to instead examine the mask. The most notable optimization change I noticed with this patch is that now redundant loads from readonly noalias pointers can be eliminated across calls, even when the pointer is captured. Internally, before this patch, AliasAnalysis was assigning Ref to reads from constant memory; now AA can assign NoModRef, which is a tighter bound. Differential Revision: https://reviews.llvm.org/D136659
2022-10-07[LoopPredication] Insert assumes of conditions of predicated guardsDmitry Makogon
As LoopPredication performs non-equivalent transforms removing some checks from loops, other passes may not be able to perform transforms they'd be able to do if the checks were left in loops. This patch makes LoopPredication insert assumes of the replaced conditions either after a guard call or in the true block of widenable condition branch. Differential Revision: https://reviews.llvm.org/D135354
2022-07-14[LoopPredication] Use isSafeToExpandAt() member function (NFC)Nikita Popov
As a followup to D129630, this switches a usage of the freestanding function in LoopPredication to use the member variant instead. This was the last use of the freestanding function, so drop it entirely.
2022-07-14[SCEVExpander] Make CanonicalMode handing in isSafeToExpand() more robust ↵Nikita Popov
(PR50506) isSafeToExpand() for addrecs depends on whether the SCEVExpander will be used in CanonicalMode. At least one caller currently gets this wrong, resulting in PR50506. Fix this by a) making the CanonicalMode argument on the freestanding functions required and b) adding member functions on SCEVExpander that automatically take the SCEVExpander mode into account. We can use the latter variant nearly everywhere, and thus make sure that there is no chance of CanonicalMode mismatch. Fixes https://github.com/llvm/llvm-project/issues/50506. Differential Revision: https://reviews.llvm.org/D129630
2022-06-20[llvm] Don't use Optional::getValue (NFC)Kazu Hirata
2022-03-03Cleanup includes: Transform/Scalarserge-sans-paille
Estimated impact on preprocessor output line: before: 1062981579 after: 1062494547 Discourse thread: https://discourse.llvm.org/t/include-what-you-use-include-cleanup Differential Revision: https://reviews.llvm.org/D120817
2022-02-06[llvm] Use = default (NFC)Kazu Hirata
2021-10-19[LoopPredication] Calculate profitability without BPIAnna Thomas
Using BPI within loop predication is non-trivial because BPI is only preserved lossily in loop pass manager (one fix exposed by lossy preservation is up for review at D111448). However, since loop predication is only used in downstream pipelines, it is hard to keep BPI from breaking for incomplete state with upstream changes in BPI. Also, correctly preserving BPI for all loop passes is a non-trivial undertaking (D110438 does this lossily), while the benefit of using it in loop predication isn't clear. In this patch, we rely on profile metadata to get almost similar benefit as BPI, without actually using the complete heuristics provided by BPI. This avoids the compile time explosion we tried to fix with D110438 and also avoids fragile bugs because BPI can be lossy in loop passes (D111448). Reviewed-By: asbirlea, apilipenko Differential Revision: https://reviews.llvm.org/D111668
2021-09-30[LoopPredication] Remove unused variableAnna Thomas
After rG452714f8f8037ff37f9358317651d1652e231db2, the Function `F` retrieved in LoopPredication is not used. Remove this unused variable to stop some buildbots (ASAN, clang-ppc) from failing.
2021-09-30[BPI] Keep BPI available in loop passes through LoopStandardAnalysisResultsAnna Thomas
This is analogous to D86156 (which preserves "lossy" BFI in loop passes). Lossy means that the analysis preserved may not be up to date with regards to new blocks that are added in loop passes, but BPI will not contain stale pointers to basic blocks that are deleted by the loop passes. This is achieved through BasicBlockCallbackVH in BPI, which calls eraseBlock that updates the data structures in BPI whenever a basic block is deleted. This patch does not have any changes in the upstream pipeline, since none of the loop passes in the pipeline use BPI currently. However, since BPI wasn't previously preserved in loop passes, the loop predication pass was invoking BPI *on the entire function* every time it ran in an LPM. This caused massive compile time in our downstream LPM invocation which contained loop predication. See updated test with an invocation of a loop-pipeline containing loop predication and -debug-pass turned ON. Reviewed-By: asbirlea, modimo Differential Revision: https://reviews.llvm.org/D110438
2021-09-16[LoopPredication] Report changes correctly when attempting loop exit predicationDaniil Suchkov
To make the IR easier to analyze, this pass makes some minor transformations. After that, even if it doesn't decide to optimize anything, it can't report that it changed nothing and preserved all the analyses. Reviewed By: reames Differential Revision: https://reviews.llvm.org/D109855
2021-09-02[LoopPredication] Fix MemorySSA crash in predicateLoopExitsAnna Thomas
The attached testcase crashes without the patch (Not the same accesses in the same order). When we move instructions before another instruction, we also need to update the memory accesses corresponding to it. Reviewed-By: asbirlea Differential Revision: https://reviews.llvm.org/D109197
2021-08-26[LoopPredication] Preserve MemorySSAAnna Thomas
Since LICM has now unconditionally moved to MemorySSA based form, all passes that run in same LPM as LICM need to preserve MemorySSA (i.e. our downstream pipeline). Added loop-mssa to all tests and perform -verify-memoryssa within LoopPredication itself. Differential Revision: https://reviews.llvm.org/D108724
2020-12-23[BPI] Improve static heuristics for "cold" paths.Evgeniy Brevnov
Current approach doesn't work well in cases when multiple paths are predicted to be "cold". By "cold" paths I mean those containing "unreachable" instruction, call marked with 'cold' attribute and 'unwind' handler of 'invoke' instruction. The issue is that heuristics are applied one by one until the first match and essentially ignores relative hotness/coldness of other paths. New approach unifies processing of "cold" paths by assigning predefined absolute weight to each block estimated to be "cold". Then we propagate these weights up/down IR similarly to existing approach. And finally set up edge probabilities based on estimated block weights. One important difference is how we propagate weight up. Existing approach propagates the same weight to all blocks that are post-dominated by a block with some "known" weight. This is useless at least because it always gives 50\50 distribution which is assumed by default anyway. Worse, it causes the algorithm to skip further heuristics and can miss setting more accurate probability. New algorithm propagates the weight up only to the blocks that dominates and post-dominated by a block with some "known" weight. In other words, those blocks that are either always executed or not executed together. In addition new approach processes loops in an uniform way as well. Essentially loop exit edges are estimated as "cold" paths relative to back edges and should be considered uniformly with other coldness/hotness markers. Reviewed By: yrouban Differential Revision: https://reviews.llvm.org/D79485
2020-10-29[NFC][SCEV] Refactor monotonic predicate checks to return enums instead of boolsMax Kazantsev
This patch gets rid of output parameter which is not needed for most users and prepares this API for further refactoring.
2020-10-23[SVE]Clarify TypeSize comparisons in llvm/lib/TransformsCaroline Concatto
Use isKnownXY comparators when one of the operands can be with scalable vectors or getFixedSize() for all the other cases. This patch also does bug fixes for getPrimitiveSizeInBits by using getFixedSize() near the places with the TypeSize comparison. Differential Revision: https://reviews.llvm.org/D89703
2020-05-25[Transforms] Fix typos. NFCMarek Kurdej
2020-05-20[SCEV] Move ScalarEvolutionExpander.cpp to Transforms/Utils (NFC).Florian Hahn
SCEVExpander modifies the underlying function so it is more suitable in Transforms/Utils, rather than Analysis. This allows using other transform utils in SCEVExpander. This patch was originally committed as b8a3c34eee06, but broke the modules build, as LoopAccessAnalysis was using the Expander. The code-gen part of LAA was moved to lib/Transforms recently, so this patch can be landed again. Reviewers: sanjoy.google, efriedma, reames Reviewed By: sanjoy.google Differential Revision: https://reviews.llvm.org/D71537
2020-04-30[BPI][NFC] Reuse post dominantor tree from analysis manager when availableEvgeniy Brevnov
Summary: Currenlty BPI unconditionally creates post dominator tree each time. While this is not incorrect we can save compile time by reusing existing post dominator tree (when it's valid) provided by analysis manager. Reviewers: skatkov, taewookoh, yrouban Reviewed By: skatkov Subscribers: hiraditya, steven_wu, dexonsmith, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D78987
2020-02-12Compute ORE, BPI, BFI in Loop passes.Alina Sbirlea
Summary: Passes ORE, BPI, BFI are not being preserved by Loop passes, hence it is incorrect to retrieve these passes as cached. This patch makes the loop passes in question compute a new instance. In some of these cases, however, it may be beneficial to change the Loop pass to a Function pass instead, similar to the change for LoopUnrollAndJam. Reviewers: chandlerc, dmgreen, jdoerfert, reames Subscribers: mehdi_amini, hiraditya, zzheng, steven_wu, dexonsmith, Whitney, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D72891
2020-02-04[NFCI] Update according to style.Alina Sbirlea
clang-tidy + clang-format
2020-01-16[BasicBlock] add helper getPostdominatingDeoptimizeCallFedor Sergeev
It appears to be rather useful when analyzing Loops with multiple deoptimizing exits, perhaps merged ones. For now it is used in LoopPredication, will be adding more uses in other loop passes. Reviewers: asbirlea, fhahn, skatkov, spatel, reames Reviewed By: reames Tags: #llvm Differential Revision: https://reviews.llvm.org/D72754
2020-01-04Revert "[SCEV] Move ScalarEvolutionExpander.cpp to Transforms/Utils (NFC)."Florian Hahn
This reverts commit 51ef53f3bd23559203fe9af82ff2facbfedc1db3, as it breaks some bots.
2020-01-04[SCEV] Move ScalarEvolutionExpander.cpp to Transforms/Utils (NFC).Florian Hahn
SCEVExpander modifies the underlying function so it is more suitable in Transforms/Utils, rather than Analysis. This allows using other transform utils in SCEVExpander. Reviewers: sanjoy.google, efriedma, reames Reviewed By: sanjoy.google Differential Revision: https://reviews.llvm.org/D71537
2019-11-21[LoopPred] Robustly handle partially unswitched loopsPhilip Reames
We may end up with a case where we have a widenable branch above the loop, but not all widenable branches within the loop have been removed. Since a widenable branch inhibit SCEVs ability to reason about exit counts (by design), we have a tradeoff between effectiveness of this optimization and allowing future widening of the branches within the loop. LoopPred is thought to be one of the most important optimizations for range check elimination, so let's pay the cost.