2012-01-31 22:00:27 +08:00
|
|
|
//===- DeadCodeElimination.cpp - Eliminate dead iteration ----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-02-22 04:51:40 +08:00
|
|
|
// The polyhedral dead code elimination pass analyses a SCoP to eliminate
|
|
|
|
// statement instances that can be proven dead.
|
|
|
|
// As a consequence, the code generated for this SCoP may execute a statement
|
|
|
|
// less often. This means, a statement may be executed only in certain loop
|
|
|
|
// iterations or it may not even be part of the generated code at all.
|
2013-03-23 08:16:05 +08:00
|
|
|
//
|
2014-02-22 04:51:40 +08:00
|
|
|
// This code:
|
2013-03-23 08:16:05 +08:00
|
|
|
//
|
2014-02-22 04:51:40 +08:00
|
|
|
// for (i = 0; i < N; i++)
|
|
|
|
// arr[i] = 0;
|
|
|
|
// for (i = 0; i < N; i++)
|
|
|
|
// arr[i] = 10;
|
|
|
|
// for (i = 0; i < N; i++)
|
|
|
|
// arr[i] = i;
|
|
|
|
//
|
|
|
|
// is e.g. simplified to:
|
|
|
|
//
|
|
|
|
// for (i = 0; i < N; i++)
|
|
|
|
// arr[i] = i;
|
|
|
|
//
|
|
|
|
// The idea and the algorithm used was first implemented by Sven Verdoolaege in
|
|
|
|
// the 'ppcg' tool.
|
2013-03-23 08:16:05 +08:00
|
|
|
//
|
2012-01-31 22:00:27 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "polly/Dependences.h"
|
|
|
|
#include "polly/LinkAllPasses.h"
|
|
|
|
#include "polly/ScopInfo.h"
|
2014-02-21 05:43:54 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-07-11 15:12:10 +08:00
|
|
|
#include "isl/flow.h"
|
2014-02-21 05:43:54 +08:00
|
|
|
#include "isl/set.h"
|
|
|
|
#include "isl/map.h"
|
|
|
|
#include "isl/union_map.h"
|
2012-01-31 22:00:27 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace polly;
|
|
|
|
|
|
|
|
namespace {
|
2014-02-21 05:43:54 +08:00
|
|
|
|
2014-02-22 04:51:46 +08:00
|
|
|
cl::opt<int> DCEPreciseSteps(
|
|
|
|
"polly-dce-precise-steps",
|
2014-02-24 16:52:20 +08:00
|
|
|
cl::desc("The number of precise steps between two approximating "
|
|
|
|
"iterations. (A value of -1 schedules another approximation stage "
|
|
|
|
"before the actual dead code elimination."),
|
2014-03-14 07:37:43 +08:00
|
|
|
cl::ZeroOrMore, cl::init(-1));
|
2012-01-31 22:00:27 +08:00
|
|
|
|
2013-01-08 16:27:46 +08:00
|
|
|
class DeadCodeElim : public ScopPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2013-03-23 08:16:05 +08:00
|
|
|
explicit DeadCodeElim() : ScopPass(ID) {}
|
2012-01-31 22:00:27 +08:00
|
|
|
|
2013-01-08 16:27:46 +08:00
|
|
|
virtual bool runOnScop(Scop &S);
|
2014-02-21 05:43:54 +08:00
|
|
|
|
2013-01-08 16:27:46 +08:00
|
|
|
void printScop(llvm::raw_ostream &OS) const;
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const;
|
2014-02-21 05:43:54 +08:00
|
|
|
|
|
|
|
private:
|
2014-07-11 15:12:10 +08:00
|
|
|
/// @brief Return the set of live iterations.
|
|
|
|
///
|
|
|
|
/// The set of live iterations are all iterations that write to memory and for
|
|
|
|
/// which we can not prove that there will be a later write that _must_
|
|
|
|
/// overwrite the same memory location and is consequently the only one that
|
|
|
|
/// is visible after the execution of the SCoP.
|
|
|
|
///
|
|
|
|
isl_union_set *getLiveOut(Scop &S);
|
2014-02-22 04:51:46 +08:00
|
|
|
bool eliminateDeadCode(Scop &S, int PreciseSteps);
|
2013-01-08 16:27:46 +08:00
|
|
|
};
|
2012-01-31 22:00:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
char DeadCodeElim::ID = 0;
|
|
|
|
|
2014-07-11 15:12:10 +08:00
|
|
|
// To compute the live outs, we first assume all must and may-writes are exposed
|
|
|
|
// and then subtract the set of statements that are definitely overwritten.
|
|
|
|
isl_union_set *DeadCodeElim::getLiveOut(Scop &S) {
|
2014-07-11 17:02:41 +08:00
|
|
|
isl_union_map *Kills = S.getMustWrites();
|
|
|
|
isl_union_map *Empty = isl_union_map_empty(S.getParamSpace());
|
2014-07-11 15:12:10 +08:00
|
|
|
|
|
|
|
isl_union_map *Covering;
|
|
|
|
isl_union_map *Writes = S.getWrites();
|
|
|
|
isl_union_map_compute_flow(Kills, Empty, isl_union_map_copy(Writes),
|
|
|
|
S.getSchedule(), NULL, &Covering, NULL, NULL);
|
|
|
|
|
|
|
|
isl_union_map *Exposed = Writes;
|
|
|
|
Exposed =
|
|
|
|
isl_union_map_subtract_domain(Exposed, isl_union_map_domain(Covering));
|
|
|
|
return isl_union_map_domain(Exposed);
|
2014-02-21 05:43:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Performs polyhedral dead iteration elimination by:
|
|
|
|
/// o Assuming that the last write to each location is live.
|
|
|
|
/// o Following each RAW dependency from a live iteration backwards and adding
|
|
|
|
/// that iteration to the live set.
|
2014-02-22 04:51:46 +08:00
|
|
|
///
|
|
|
|
/// To ensure the set of live iterations does not get too complex we always
|
|
|
|
/// combine a certain number of precise steps with one approximating step that
|
|
|
|
/// simplifies the life set with an affine hull.
|
|
|
|
bool DeadCodeElim::eliminateDeadCode(Scop &S, int PreciseSteps) {
|
2012-01-31 22:00:27 +08:00
|
|
|
Dependences *D = &getAnalysis<Dependences>();
|
2014-02-23 23:15:44 +08:00
|
|
|
|
|
|
|
if (!D->hasValidDependences())
|
|
|
|
return false;
|
|
|
|
|
2014-07-11 15:12:10 +08:00
|
|
|
isl_union_set *Live = getLiveOut(S);
|
2014-06-21 00:37:11 +08:00
|
|
|
isl_union_map *Dep =
|
|
|
|
D->getDependences(Dependences::TYPE_RAW | Dependences::TYPE_RED);
|
2014-02-21 05:43:54 +08:00
|
|
|
Dep = isl_union_map_reverse(Dep);
|
2012-01-31 22:00:27 +08:00
|
|
|
|
2014-02-24 16:52:20 +08:00
|
|
|
if (PreciseSteps == -1)
|
|
|
|
Live = isl_union_set_affine_hull(Live);
|
|
|
|
|
2014-02-21 05:43:54 +08:00
|
|
|
isl_union_set *OriginalDomain = S.getDomains();
|
2014-02-22 04:51:46 +08:00
|
|
|
int Steps = 0;
|
2014-02-22 05:06:08 +08:00
|
|
|
while (true) {
|
2014-02-21 05:43:54 +08:00
|
|
|
isl_union_set *Extra;
|
2014-02-22 04:51:46 +08:00
|
|
|
Steps++;
|
2012-01-31 22:00:27 +08:00
|
|
|
|
2014-02-21 05:43:54 +08:00
|
|
|
Extra =
|
|
|
|
isl_union_set_apply(isl_union_set_copy(Live), isl_union_map_copy(Dep));
|
2012-01-31 22:00:27 +08:00
|
|
|
|
2014-02-21 05:43:54 +08:00
|
|
|
if (isl_union_set_is_subset(Extra, Live)) {
|
|
|
|
isl_union_set_free(Extra);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Live = isl_union_set_union(Live, Extra);
|
2014-02-22 04:51:46 +08:00
|
|
|
|
|
|
|
if (Steps > PreciseSteps) {
|
|
|
|
Steps = 0;
|
2014-02-21 05:43:54 +08:00
|
|
|
Live = isl_union_set_affine_hull(Live);
|
2014-02-22 04:51:46 +08:00
|
|
|
}
|
|
|
|
|
2014-02-21 05:43:54 +08:00
|
|
|
Live = isl_union_set_intersect(Live, isl_union_set_copy(OriginalDomain));
|
|
|
|
}
|
|
|
|
isl_union_map_free(Dep);
|
|
|
|
isl_union_set_free(OriginalDomain);
|
|
|
|
|
|
|
|
return S.restrictDomains(isl_union_set_coalesce(Live));
|
2012-01-31 22:00:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-22 04:51:46 +08:00
|
|
|
bool DeadCodeElim::runOnScop(Scop &S) {
|
|
|
|
return eliminateDeadCode(S, DCEPreciseSteps);
|
|
|
|
}
|
2014-02-21 05:43:54 +08:00
|
|
|
|
2013-03-23 08:16:05 +08:00
|
|
|
void DeadCodeElim::printScop(raw_ostream &OS) const {}
|
2012-01-31 22:00:27 +08:00
|
|
|
|
|
|
|
void DeadCodeElim::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
ScopPass::getAnalysisUsage(AU);
|
|
|
|
AU.addRequired<Dependences>();
|
|
|
|
}
|
|
|
|
|
2013-03-23 08:16:05 +08:00
|
|
|
Pass *polly::createDeadCodeElimPass() { return new DeadCodeElim(); }
|
|
|
|
|
2012-01-31 22:00:27 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(DeadCodeElim, "polly-dce",
|
2013-04-10 14:55:45 +08:00
|
|
|
"Polly - Remove dead iterations", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(Dependences)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(ScopInfo)
|
2013-03-23 08:16:05 +08:00
|
|
|
INITIALIZE_PASS_END(DeadCodeElim, "polly-dce", "Polly - Remove dead iterations",
|
|
|
|
false, false)
|