119523ed2SAndreas Simbuerger //===- DeadCodeElimination.cpp - Eliminate dead iteration ----------------===//
219523ed2SAndreas Simbuerger //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
619523ed2SAndreas Simbuerger //
719523ed2SAndreas Simbuerger //===----------------------------------------------------------------------===//
819523ed2SAndreas Simbuerger //
919523ed2SAndreas Simbuerger // The polyhedral dead code elimination pass analyses a SCoP to eliminate
1019523ed2SAndreas Simbuerger // statement instances that can be proven dead.
1119523ed2SAndreas Simbuerger // As a consequence, the code generated for this SCoP may execute a statement
1219523ed2SAndreas Simbuerger // less often. This means, a statement may be executed only in certain loop
1319523ed2SAndreas Simbuerger // iterations or it may not even be part of the generated code at all.
1419523ed2SAndreas Simbuerger //
1519523ed2SAndreas Simbuerger // This code:
1619523ed2SAndreas Simbuerger //
1719523ed2SAndreas Simbuerger // for (i = 0; i < N; i++)
1819523ed2SAndreas Simbuerger // arr[i] = 0;
1919523ed2SAndreas Simbuerger // for (i = 0; i < N; i++)
2019523ed2SAndreas Simbuerger // arr[i] = 10;
2119523ed2SAndreas Simbuerger // for (i = 0; i < N; i++)
2219523ed2SAndreas Simbuerger // arr[i] = i;
2319523ed2SAndreas Simbuerger //
2419523ed2SAndreas Simbuerger // is e.g. simplified to:
2519523ed2SAndreas Simbuerger //
2619523ed2SAndreas Simbuerger // for (i = 0; i < N; i++)
2719523ed2SAndreas Simbuerger // arr[i] = i;
2819523ed2SAndreas Simbuerger //
2919523ed2SAndreas Simbuerger // The idea and the algorithm used was first implemented by Sven Verdoolaege in
3019523ed2SAndreas Simbuerger // the 'ppcg' tool.
3119523ed2SAndreas Simbuerger //
3219523ed2SAndreas Simbuerger //===----------------------------------------------------------------------===//
3319523ed2SAndreas Simbuerger
348796451dSMichael Kruse #include "polly/DeadCodeElimination.h"
35f6557f98SJohannes Doerfert #include "polly/DependenceInfo.h"
3619523ed2SAndreas Simbuerger #include "polly/LinkAllPasses.h"
37935b2a36SMichael Kruse #include "polly/Options.h"
3819523ed2SAndreas Simbuerger #include "polly/ScopInfo.h"
3919523ed2SAndreas Simbuerger #include "llvm/Support/CommandLine.h"
40d680edfbSTobias Grosser #include "isl/isl-noexceptions.h"
4119523ed2SAndreas Simbuerger
4219523ed2SAndreas Simbuerger using namespace llvm;
4319523ed2SAndreas Simbuerger using namespace polly;
4419523ed2SAndreas Simbuerger
4519523ed2SAndreas Simbuerger namespace {
4619523ed2SAndreas Simbuerger
4719523ed2SAndreas Simbuerger cl::opt<int> DCEPreciseSteps(
4819523ed2SAndreas Simbuerger "polly-dce-precise-steps",
4919523ed2SAndreas Simbuerger cl::desc("The number of precise steps between two approximating "
5019523ed2SAndreas Simbuerger "iterations. (A value of -1 schedules another approximation stage "
5119523ed2SAndreas Simbuerger "before the actual dead code elimination."),
52*95a13425SFangrui Song cl::init(-1), cl::cat(PollyCategory));
5319523ed2SAndreas Simbuerger
54bd93df93SMichael Kruse class DeadCodeElimWrapperPass final : public ScopPass {
5519523ed2SAndreas Simbuerger public:
5619523ed2SAndreas Simbuerger static char ID;
DeadCodeElimWrapperPass()578796451dSMichael Kruse explicit DeadCodeElimWrapperPass() : ScopPass(ID) {}
5819523ed2SAndreas Simbuerger
59c80d6979STobias Grosser /// Remove dead iterations from the schedule of @p S.
60909a3bf2SJohannes Doerfert bool runOnScop(Scop &S) override;
6119523ed2SAndreas Simbuerger
62c80d6979STobias Grosser /// Register all analyses and transformation required.
63909a3bf2SJohannes Doerfert void getAnalysisUsage(AnalysisUsage &AU) const override;
648796451dSMichael Kruse };
6519523ed2SAndreas Simbuerger
668796451dSMichael Kruse char DeadCodeElimWrapperPass::ID = 0;
678796451dSMichael Kruse
68c80d6979STobias Grosser /// Return the set of live iterations.
69780ce0f8STobias Grosser ///
70780ce0f8STobias Grosser /// The set of live iterations are all iterations that write to memory and for
71780ce0f8STobias Grosser /// which we can not prove that there will be a later write that _must_
72780ce0f8STobias Grosser /// overwrite the same memory location and is consequently the only one that
73780ce0f8STobias Grosser /// is visible after the execution of the SCoP.
74780ce0f8STobias Grosser ///
758796451dSMichael Kruse /// To compute the live outs, we compute for the data-locations that are
768796451dSMichael Kruse /// must-written to the last statement that touches these locations. On top of
778796451dSMichael Kruse /// this we add all statements that perform may-write accesses.
788796451dSMichael Kruse ///
798796451dSMichael Kruse /// We could be more precise by removing may-write accesses for which we know
808796451dSMichael Kruse /// that they are overwritten by a must-write after. However, at the moment the
818796451dSMichael Kruse /// only may-writes we introduce access the full (unbounded) array, such that
828796451dSMichael Kruse /// bounded write accesses can not overwrite all of the data-locations. As
838796451dSMichael Kruse /// this means may-writes are in the current situation always live, there is
848796451dSMichael Kruse /// no point in trying to remove them from the live-out set.
getLiveOut(Scop & S)858796451dSMichael Kruse static isl::union_set getLiveOut(Scop &S) {
8661bd3a48STobias Grosser isl::union_map Schedule = S.getSchedule();
875ab39ff2STobias Grosser isl::union_map MustWrites = S.getMustWrites();
883e618c33STobias Grosser isl::union_map WriteIterations = MustWrites.reverse();
893e618c33STobias Grosser isl::union_map WriteTimes = WriteIterations.apply_range(Schedule);
9019523ed2SAndreas Simbuerger
913e618c33STobias Grosser isl::union_map LastWriteTimes = WriteTimes.lexmax();
923e618c33STobias Grosser isl::union_map LastWriteIterations =
933e618c33STobias Grosser LastWriteTimes.apply_range(Schedule.reverse());
9419523ed2SAndreas Simbuerger
953e618c33STobias Grosser isl::union_set Live = LastWriteIterations.range();
965ab39ff2STobias Grosser isl::union_map MayWrites = S.getMayWrites();
973e618c33STobias Grosser Live = Live.unite(MayWrites.domain());
983e618c33STobias Grosser return Live.coalesce();
9919523ed2SAndreas Simbuerger }
10019523ed2SAndreas Simbuerger
10119523ed2SAndreas Simbuerger /// Performs polyhedral dead iteration elimination by:
10219523ed2SAndreas Simbuerger /// o Assuming that the last write to each location is live.
10319523ed2SAndreas Simbuerger /// o Following each RAW dependency from a live iteration backwards and adding
10419523ed2SAndreas Simbuerger /// that iteration to the live set.
10519523ed2SAndreas Simbuerger ///
10619523ed2SAndreas Simbuerger /// To ensure the set of live iterations does not get too complex we always
10719523ed2SAndreas Simbuerger /// combine a certain number of precise steps with one approximating step that
10819523ed2SAndreas Simbuerger /// simplifies the life set with an affine hull.
runDeadCodeElimination(Scop & S,int PreciseSteps,const Dependences & D)1098796451dSMichael Kruse static bool runDeadCodeElimination(Scop &S, int PreciseSteps,
1108796451dSMichael Kruse const Dependences &D) {
1117e6424baSJohannes Doerfert if (!D.hasValidDependences())
11219523ed2SAndreas Simbuerger return false;
11319523ed2SAndreas Simbuerger
1143e618c33STobias Grosser isl::union_set Live = getLiveOut(S);
1156a6d9df7STobias Grosser isl::union_map Dep =
1166a6d9df7STobias Grosser D.getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED);
1173e618c33STobias Grosser Dep = Dep.reverse();
11819523ed2SAndreas Simbuerger
11919523ed2SAndreas Simbuerger if (PreciseSteps == -1)
1203e618c33STobias Grosser Live = Live.affine_hull();
12119523ed2SAndreas Simbuerger
12231df6f31STobias Grosser isl::union_set OriginalDomain = S.getDomains();
12319523ed2SAndreas Simbuerger int Steps = 0;
12419523ed2SAndreas Simbuerger while (true) {
12519523ed2SAndreas Simbuerger Steps++;
12619523ed2SAndreas Simbuerger
1273e618c33STobias Grosser isl::union_set Extra = Live.apply(Dep);
12819523ed2SAndreas Simbuerger
1293e618c33STobias Grosser if (Extra.is_subset(Live))
13019523ed2SAndreas Simbuerger break;
13119523ed2SAndreas Simbuerger
1323e618c33STobias Grosser Live = Live.unite(Extra);
13319523ed2SAndreas Simbuerger
13419523ed2SAndreas Simbuerger if (Steps > PreciseSteps) {
13519523ed2SAndreas Simbuerger Steps = 0;
1363e618c33STobias Grosser Live = Live.affine_hull();
13719523ed2SAndreas Simbuerger }
13819523ed2SAndreas Simbuerger
1393e618c33STobias Grosser Live = Live.intersect(OriginalDomain);
14019523ed2SAndreas Simbuerger }
14119523ed2SAndreas Simbuerger
1423e618c33STobias Grosser Live = Live.coalesce();
1433e618c33STobias Grosser
1448796451dSMichael Kruse return S.restrictDomains(Live);
1458796451dSMichael Kruse }
1468796451dSMichael Kruse
runOnScop(Scop & S)1478796451dSMichael Kruse bool DeadCodeElimWrapperPass::runOnScop(Scop &S) {
1488796451dSMichael Kruse auto &DI = getAnalysis<DependenceInfo>();
1498796451dSMichael Kruse const Dependences &Deps = DI.getDependences(Dependences::AL_Statement);
1508796451dSMichael Kruse
1518796451dSMichael Kruse bool Changed = runDeadCodeElimination(S, DCEPreciseSteps, Deps);
15211e38735STobias Grosser
15311e38735STobias Grosser // FIXME: We can probably avoid the recomputation of all dependences by
15411e38735STobias Grosser // updating them explicitly.
15511e38735STobias Grosser if (Changed)
1562a798853SHongbin Zheng DI.recomputeDependences(Dependences::AL_Statement);
1578796451dSMichael Kruse
1588796451dSMichael Kruse return false;
15919523ed2SAndreas Simbuerger }
16019523ed2SAndreas Simbuerger
getAnalysisUsage(AnalysisUsage & AU) const1618796451dSMichael Kruse void DeadCodeElimWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
16219523ed2SAndreas Simbuerger ScopPass::getAnalysisUsage(AU);
163f6557f98SJohannes Doerfert AU.addRequired<DependenceInfo>();
16419523ed2SAndreas Simbuerger }
16519523ed2SAndreas Simbuerger
1668796451dSMichael Kruse } // namespace
16719523ed2SAndreas Simbuerger
createDeadCodeElimWrapperPass()1688796451dSMichael Kruse Pass *polly::createDeadCodeElimWrapperPass() {
1698796451dSMichael Kruse return new DeadCodeElimWrapperPass();
1708796451dSMichael Kruse }
1718796451dSMichael Kruse
run(Scop & S,ScopAnalysisManager & SAM,ScopStandardAnalysisResults & SAR,SPMUpdater & U)1728796451dSMichael Kruse llvm::PreservedAnalyses DeadCodeElimPass::run(Scop &S, ScopAnalysisManager &SAM,
1738796451dSMichael Kruse ScopStandardAnalysisResults &SAR,
1748796451dSMichael Kruse SPMUpdater &U) {
1758796451dSMichael Kruse DependenceAnalysis::Result &DA = SAM.getResult<DependenceAnalysis>(S, SAR);
1768796451dSMichael Kruse const Dependences &Deps = DA.getDependences(Dependences::AL_Statement);
1778796451dSMichael Kruse
1788796451dSMichael Kruse bool Changed = runDeadCodeElimination(S, DCEPreciseSteps, Deps);
1798796451dSMichael Kruse
1808796451dSMichael Kruse // FIXME: We can probably avoid the recomputation of all dependences by
1818796451dSMichael Kruse // updating them explicitly.
1828796451dSMichael Kruse if (Changed)
1838796451dSMichael Kruse DA.recomputeDependences(Dependences::AL_Statement);
1848796451dSMichael Kruse
1858796451dSMichael Kruse if (!Changed)
1868796451dSMichael Kruse return PreservedAnalyses::all();
1878796451dSMichael Kruse
1888796451dSMichael Kruse PreservedAnalyses PA;
1898796451dSMichael Kruse PA.preserveSet<AllAnalysesOn<Module>>();
1908796451dSMichael Kruse PA.preserveSet<AllAnalysesOn<Function>>();
1918796451dSMichael Kruse PA.preserveSet<AllAnalysesOn<Loop>>();
1928796451dSMichael Kruse return PA;
1938796451dSMichael Kruse }
1948796451dSMichael Kruse
1958796451dSMichael Kruse INITIALIZE_PASS_BEGIN(DeadCodeElimWrapperPass, "polly-dce",
19619523ed2SAndreas Simbuerger "Polly - Remove dead iterations", false, false)
197f6557f98SJohannes Doerfert INITIALIZE_PASS_DEPENDENCY(DependenceInfo)
19899191c78SJohannes Doerfert INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass)
1998796451dSMichael Kruse INITIALIZE_PASS_END(DeadCodeElimWrapperPass, "polly-dce",
2008796451dSMichael Kruse "Polly - Remove dead iterations", false, false)
201