Skip to content

Commit 03b1ee2

Browse files
committed
[TSAR, Memory, Transform] Minor fix.
1 parent baca2b3 commit 03b1ee2

File tree

11 files changed

+163
-120
lines changed

11 files changed

+163
-120
lines changed

include/tsar/Analysis/Memory/DefinedMemory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class DefUseSet {
211211

212212
/// Replaced collapsed memory locations with its expanded forms which are
213213
/// of kind `Default`.
214-
void updateCollapsedLocations() {
214+
void summarizeCollapsedLocations() {
215215
LocationSet DestDefs, DestMayDefs, DestUses;
216216
for (auto &Loc : mDefs) {
217217
if (Loc.Kind == MemoryLocationRange::LocKind::Collapsed)

include/tsar/Analysis/Memory/MemoryLocationRange.h

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
#include <llvm/ADT/APInt.h>
2929
#include <llvm/ADT/BitmaskEnum.h>
3030
#include <llvm/Analysis/MemoryLocation.h>
31-
#include <llvm/Analysis/ScalarEvolution.h>
32-
#include <llvm/Analysis/ScalarEvolutionExpressions.h>
31+
32+
namespace llvm {
33+
class ScalarEvolution;
34+
class SCEV;
35+
}
3336

3437
namespace tsar {
3538

@@ -62,32 +65,7 @@ struct MemoryLocationRange {
6265
return Start == Other.Start && End == Other.End && Step == Other.Step &&
6366
DimSize == Other.DimSize;
6467
}
65-
inline void print(llvm::raw_ostream &OS, bool IsDebug = false) const {
66-
auto PrintSCEV = [](const llvm::SCEV *Expr, llvm::raw_ostream &OS) {
67-
if (!Expr)
68-
OS << "(nullptr)";
69-
else
70-
Expr->print(OS);
71-
};
72-
if (IsDebug) {
73-
OS << "{Start: ";
74-
PrintSCEV(Start, OS);
75-
OS << ", End: ";
76-
PrintSCEV(End, OS);
77-
OS << ", Step: ";
78-
PrintSCEV(Step, OS);
79-
OS << ", DimSize: " << DimSize << "}";
80-
} else {
81-
OS << "[";
82-
PrintSCEV(Start, OS);
83-
OS << ":";
84-
PrintSCEV(End, OS);
85-
OS << ":";
86-
PrintSCEV(Step, OS);
87-
OS << "," << DimSize;
88-
OS << "]";
89-
}
90-
}
68+
void print(llvm::raw_ostream &OS, bool IsDebug = false) const;
9169
};
9270

9371
const llvm::Value * Ptr;
@@ -220,7 +198,7 @@ struct MemoryLocationRange {
220198
/// If the location is associated with an array, i.e. it has a non-empty list
221199
/// of dimensions, convert it to an ordinary location with an empty list of
222200
/// dimensions and return it. The new location covers the entire memory of
223-
/// the array, even through the dimensions may not be completely used.
201+
/// the array, even though the dimensions are not completely used.
224202
MemoryLocationRange expand() const {
225203
if (DimList.empty())
226204
return *this;
@@ -257,18 +235,6 @@ llvm::Optional<MemoryLocationRange> intersect(
257235
llvm::SmallVectorImpl<MemoryLocationRange> *LC = nullptr,
258236
llvm::SmallVectorImpl<MemoryLocationRange> *RC = nullptr,
259237
unsigned Threshold = 10);
260-
261-
/// Returns the distance between LHS and RHS if it can be calculated.
262-
inline llvm::Optional<int64_t> compareSCEVs(const llvm::SCEV *LHS,
263-
const llvm::SCEV *RHS,
264-
llvm::ScalarEvolution *SE) {
265-
assert(SE && "ScalarEvolution must be specified!");
266-
assert(LHS && RHS && "SCEV must be specified!");
267-
if (auto Const = llvm::dyn_cast<llvm::SCEVConstant>(
268-
SE->getMinusSCEV(LHS, RHS)))
269-
return Const->getAPInt().getSExtValue();
270-
return llvm::None;
271-
}
272238
}
273239

274240
namespace llvm {

include/tsar/Analysis/Memory/MemorySetInfo.h

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,9 @@
2626
#define TSAR_MEMORY_SET_INFO_H
2727

2828
#include "tsar/Analysis/Memory/MemoryLocationRange.h"
29+
#include "tsar/Support/SCEVUtils.h"
2930
#include <llvm/Analysis/ScalarEvolutionExpressions.h>
3031

31-
using llvm::cast;
32-
using llvm::dyn_cast;
33-
using llvm::isa;
34-
using llvm::SCEVConstant;
35-
3632
namespace tsar {
3733
/// Provide traits for objects stored in a MemorySet.
3834
///
@@ -243,10 +239,13 @@ template<> struct MemorySetInfo<MemoryLocationRange> {
243239
auto &Right = RHS.DimList[I];
244240
if (Left == Right)
245241
continue;
246-
assert(isa<SCEVConstant>(Left.Step) && isa<SCEVConstant>(Right.Step) &&
247-
"Dimension step must be constant!");
248-
if (!isa<SCEVConstant>(Left.Start) || !isa<SCEVConstant>(Left.End) ||
249-
!isa<SCEVConstant>(Right.Start) || !isa<SCEVConstant>(Right.End)) {
242+
assert(llvm::isa<llvm::SCEVConstant>(Left.Step) &&
243+
llvm::isa<llvm::SCEVConstant>(Right.Step) &&
244+
"Dimension step must be constant!");
245+
if (!llvm::isa<llvm::SCEVConstant>(Left.Start) ||
246+
!llvm::isa<llvm::SCEVConstant>(Left.End) ||
247+
!llvm::isa<llvm::SCEVConstant>(Right.Start) ||
248+
!llvm::isa<llvm::SCEVConstant>(Right.End)) {
250249
auto CmpLeftStartRightEnd = compareSCEVs(Left.Start, Right.End, SE);
251250
auto CmpRightStartLeftEnd = compareSCEVs(Right.Start, Left.End, SE);
252251
if (CmpLeftStartRightEnd &&
@@ -255,20 +254,20 @@ template<> struct MemorySetInfo<MemoryLocationRange> {
255254
(*CmpRightStartLeftEnd == 0 || *CmpRightStartLeftEnd == 1)) {
256255
++JoinableDimCount;
257256
continue;
258-
} else
259-
return false;
257+
}
258+
return false;
260259
}
261-
auto LeftStart = cast<SCEVConstant>(Left.Start)->
260+
auto LeftStart = llvm::cast<llvm::SCEVConstant>(Left.Start)->
262261
getValue()->getZExtValue();
263-
auto LeftEnd = cast<SCEVConstant>(Left.End)->
262+
auto LeftEnd = llvm::cast<llvm::SCEVConstant>(Left.End)->
264263
getValue()->getZExtValue();
265-
auto LeftStep = cast<SCEVConstant>(Left.Step)->
264+
auto LeftStep = llvm::cast<llvm::SCEVConstant>(Left.Step)->
266265
getValue()->getZExtValue();
267-
auto RightStart = cast<SCEVConstant>(Right.Start)->
266+
auto RightStart = llvm::cast<llvm::SCEVConstant>(Right.Start)->
268267
getValue()->getZExtValue();
269-
auto RightEnd = cast<SCEVConstant>(Right.End)->
268+
auto RightEnd = llvm::cast<llvm::SCEVConstant>(Right.End)->
270269
getValue()->getZExtValue();
271-
auto RightStep = cast<SCEVConstant>(Right.Step)->
270+
auto RightStep = llvm::cast<llvm::SCEVConstant>(Right.Step)->
272271
getValue()->getZExtValue();
273272
assert(LeftStart <= LeftEnd && RightStart <= RightEnd &&
274273
"Start of dimension must be less or equal than End.");
@@ -306,10 +305,10 @@ template<> struct MemorySetInfo<MemoryLocationRange> {
306305
for (size_t I = 0; I < To.DimList.size(); I++) {
307306
auto &DimTo = To.DimList[I];
308307
auto &DimFrom = What.DimList[I];
309-
if (!isa<SCEVConstant>(DimTo.Start) ||
310-
!isa<SCEVConstant>(DimTo.End) ||
311-
!isa<SCEVConstant>(DimFrom.Start) ||
312-
!isa<SCEVConstant>(DimFrom.End)) {
308+
if (!llvm::isa<llvm::SCEVConstant>(DimTo.Start) ||
309+
!llvm::isa<llvm::SCEVConstant>(DimTo.End) ||
310+
!llvm::isa<llvm::SCEVConstant>(DimFrom.Start) ||
311+
!llvm::isa<llvm::SCEVConstant>(DimFrom.End)) {
313312
auto CmpFromEndToStart = compareSCEVs(DimTo.Start, DimFrom.End, SE);
314313
auto CmpToEndFromStart = compareSCEVs(DimFrom.Start, DimTo.End, SE);
315314
if (CmpFromEndToStart &&

include/tsar/Support/SCEVUtils.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,5 +74,22 @@ const llvm::SCEV* findGCD(llvm::ArrayRef<const llvm::SCEV *> Expressions,
7474
///
7575
/// This function implements Sieve of Atkin with cache.
7676
std::vector<std::size_t> countPrimeNumbers(std::size_t Bound);
77+
78+
/// If LHS and RHS have the same sequence of type casts, add them and cast
79+
/// the result back to the original type.
80+
const llvm::SCEV *addSCEVAndCast(const llvm::SCEV *LHS,
81+
const llvm::SCEV *RHS,
82+
llvm::ScalarEvolution *SE);
83+
84+
/// If LHS and RHS have the same sequence of type casts, subtract them and cast
85+
/// the result back to the original type.
86+
const llvm::SCEV *subtractSCEVAndCast(const llvm::SCEV *LHS,
87+
const llvm::SCEV *RHS,
88+
llvm::ScalarEvolution *SE);
89+
90+
/// Returns the distance between LHS and RHS if it can be calculated.
91+
llvm::Optional<int64_t> compareSCEVs(const llvm::SCEV *LHS,
92+
const llvm::SCEV *RHS,
93+
llvm::ScalarEvolution *SE);
7794
}
7895
#endif//TSAR_SCEV_UTILS_H

include/tsar/Transform/IR/RedundantEdgeElimination.h

Lines changed: 0 additions & 46 deletions
This file was deleted.

lib/Analysis/Memory/GlobalDefinedMemory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ bool GlobalDefinedMemory::runOnModule(Module &SCC) {
197197
auto DefUseSetItr = ReachDefFwk.getDefInfo().find(DFF);
198198
assert(DefUseSetItr != ReachDefFwk.getDefInfo().end() &&
199199
"Def-use set must exist for a function!");
200-
DefUseSetItr->get<DefUseSet>()->updateCollapsedLocations();
200+
DefUseSetItr->get<DefUseSet>()->summarizeCollapsedLocations();
201201
Wrapper->try_emplace(F, std::move(DefUseSetItr->get<DefUseSet>()));
202202
LLVM_DEBUG(dbgs() << "[GLOBAL DEFINED MEMORY]: leave " << F->getName()
203203
<< "\n";);

lib/Analysis/Memory/GlobalLiveMemory.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,18 +319,17 @@ bool GlobalLiveMemory::runOnModule(Module &M) {
319319
LS->setOut(MayLives);
320320
LiveDFFwk LiveFwk(IntraLiveInfo, DefInfo, DT);
321321
solveDataFlowDownward(&LiveFwk, TopRegion);
322-
auto ExpandCollapsed = [](LiveSet *LS) {
322+
auto ExpandCollapsed = [](LiveSet &LS) {
323323
typedef MemorySet<MemoryLocationRange> LocationSet;
324-
assert(LS && "LiveSet must be specified!");
325324
LocationSet DestIn, DestOut;
326325
auto expand = [](const LocationSet &From, LocationSet &To) {
327326
for (auto Loc : From)
328327
To.insert(Loc.expand());
329328
};
330-
expand(LS->getIn(), DestIn);
331-
expand(LS->getOut(), DestOut);
332-
LS->setIn(DestIn);
333-
LS->setOut(DestOut);
329+
expand(LS.getIn(), DestIn);
330+
expand(LS.getOut(), DestOut);
331+
LS.setIn(std::move(DestIn));
332+
LS.setOut(std::move(DestOut));
334333
};
335334
auto &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(*F);
336335
for (auto &CallRecord : *CGN) {
@@ -358,9 +357,9 @@ bool GlobalLiveMemory::runOnModule(Module &M) {
358357
CallLiveOut.insert(MemoryLocationRange(Arg, 0, Loc.Size));
359358
},
360359
[](Instruction &, AccessInfo, AccessInfo) {});
361-
ExpandCollapsed(CallLS.get());
360+
ExpandCollapsed(*CallLS.get());
362361
}
363-
ExpandCollapsed(IntraLiveInfo[TopRegion].get());
362+
ExpandCollapsed(*IntraLiveInfo[TopRegion].get());
364363
Wrapper->try_emplace(F, std::move(IntraLiveInfo[TopRegion]));
365364
}
366365
LLVM_DEBUG(visitedFunctionsLog(LiveSetForCalls));

lib/Analysis/Memory/MemoryLocationRange.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,30 @@ llvm::Optional<MemoryLocationRange> intersect(
729729
return Int;
730730
}
731731
}
732+
733+
void Dimension::print(llvm::raw_ostream &OS, bool IsDebug) const {
734+
auto PrintSCEV = [](const llvm::SCEV *Expr, llvm::raw_ostream &OS) {
735+
if (!Expr)
736+
OS << "(nullptr)";
737+
else
738+
Expr->print(OS);
739+
};
740+
if (IsDebug) {
741+
OS << "{Start: ";
742+
PrintSCEV(Start, OS);
743+
OS << ", End: ";
744+
PrintSCEV(End, OS);
745+
OS << ", Step: ";
746+
PrintSCEV(Step, OS);
747+
OS << ", DimSize: " << DimSize << "}";
748+
} else {
749+
OS << "[";
750+
PrintSCEV(Start, OS);
751+
OS << ":";
752+
PrintSCEV(End, OS);
753+
OS << ":";
754+
PrintSCEV(Step, OS);
755+
OS << "," << DimSize;
756+
OS << "]";
757+
}
758+
}

lib/Core/Query.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ void addAfterSROAAnalysis(const GlobalOptions &GO, const DataLayout &DL,
187187
Passes.add(createMemoryMatcherPass());
188188
Passes.add(createGlobalsAccessCollector());
189189
Passes.add(createCallExtractorPass());
190+
Passes.add(createRedundantEdgeEliminationPass());
191+
Passes.add(createUnreachableBlockEliminationPass());
190192
Passes.add(createGlobalDefinedMemoryPass());
191193
Passes.add(createGlobalLiveMemoryPass());
192194
Passes.add(createFunctionMemoryAttrsAnalysis());

0 commit comments

Comments
 (0)