Skip to content

Commit 0b46db8

Browse files
committed
[TSAR, Memory] Add description for several functions.
1 parent 99c72f1 commit 0b46db8

File tree

4 files changed

+109
-97
lines changed

4 files changed

+109
-97
lines changed

include/tsar/Analysis/Memory/DefinedMemory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ class DefUseSet {
209209
return mUses.insert(Uses.begin(), Uses.end());
210210
}
211211

212-
/// TODO: description
212+
/// Replaced collapsed memory locations with its expanded forms which are
213+
/// of kind `Default`.
213214
void updateCollapsedLocations() {
214215
LocationSet DestDefs, DestMayDefs, DestUses;
215216
for (auto &Loc : mDefs) {

include/tsar/Analysis/Memory/MemoryLocationRange.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,18 @@ struct MemoryLocationRange {
217217
return KindStr;
218218
}
219219

220-
// TODO: description
220+
/// If the location is associated with an array, i.e. it has a non-empty list
221+
/// of dimensions, convert it to an ordinary location with an empty list of
222+
/// dimensions and return it. The new location covers the entire memory of
223+
/// the array, even through the dimensions may not be completely used.
221224
MemoryLocationRange expand() const {
222225
if (DimList.empty())
223226
return *this;
224227
assert(UpperBound.hasValue() && "UpperBound must have a value!");
225228
auto FullSize = UpperBound.getValue();;
226229
for (std::size_t I = 1; I < DimList.size(); ++I)
227230
FullSize *= DimList[I].DimSize;
228-
return MemoryLocationRange(Ptr, LowerBound, LocationSize(FullSize), AATags);
231+
return MemoryLocationRange(Ptr, 0, LocationSize(FullSize), AATags);
229232
}
230233
};
231234

@@ -255,14 +258,14 @@ llvm::Optional<MemoryLocationRange> intersect(
255258
llvm::SmallVectorImpl<MemoryLocationRange> *RC = nullptr,
256259
unsigned Threshold = 10);
257260

258-
/// TODO: description.
259-
/// Returns distance between LHS and RHS.
261+
/// Returns the distance between LHS and RHS if it can be calculated.
260262
inline llvm::Optional<int64_t> compareSCEVs(const llvm::SCEV *LHS,
261263
const llvm::SCEV *RHS,
262264
llvm::ScalarEvolution *SE) {
263265
assert(SE && "ScalarEvolution must be specified!");
264266
assert(LHS && RHS && "SCEV must be specified!");
265-
if (auto Const = llvm::dyn_cast<llvm::SCEVConstant>(SE->getMinusSCEV(LHS, RHS)))
267+
if (auto Const = llvm::dyn_cast<llvm::SCEVConstant>(
268+
SE->getMinusSCEV(LHS, RHS)))
266269
return Const->getAPInt().getSExtValue();
267270
return llvm::None;
268271
}

lib/Analysis/Memory/DefinedMemory.cpp

Lines changed: 33 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ class AddUnknownAccessFunctor :
247247
/// loop nest associated with region `R`, this value will be set to `false`,
248248
/// `true` otherwise.
249249
std::pair<MemoryLocationRange, bool> aggregate(
250-
DFRegion *R, DFNode *Node, const MemoryLocationRange &Loc,
250+
DFRegion *R, const MemoryLocationRange &Loc,
251251
const ReachDFFwk *Fwk) {
252252
typedef MemoryLocationRange::Dimension Dimension;
253253
typedef MemoryLocationRange::LocKind LocKind;
@@ -344,11 +344,6 @@ std::pair<MemoryLocationRange, bool> aggregate(
344344
if (auto *DFL = dyn_cast<DFLoop>(R)) {
345345
assert(DFL->getLoop() && C->getLoop() &&
346346
"Loops for the DFLoop and AddRecExpr must be specified!");
347-
if (!DFL->getLoop()->getExitingBlock() || !DFL->getLoop()->getLoopLatch()) {
348-
LLVM_DEBUG(dbgs() << "[AGGREGATE] Multiple exiting and latch blocks "
349-
"are prohibited.\n");
350-
break;
351-
}
352347
if (DFL->getLoop()->contains(C->getLoop()))
353348
++LoopMatchCount;
354349
}
@@ -358,23 +353,23 @@ std::pair<MemoryLocationRange, bool> aggregate(
358353
break;
359354
}
360355
auto AddRecStep = cast<SCEVConstant>(StepSCEV)->getAPInt().getSExtValue();
356+
if (!C->getLoop()->getExitingBlock() || !C->getLoop()->getLoopLatch()) {
357+
LLVM_DEBUG(dbgs() << "[AGGREGATE] Multiple exiting and latch blocks "
358+
"are prohibited.\n");
359+
break;
360+
}
361361
unsigned int TripCount = 0;
362-
if (auto *DFL = dyn_cast<DFLoop>(R)) {
363-
auto Loop = DFL->getLoop();
364-
if (Loop->getLoopLatch() == Loop->getExitingBlock() &&
365-
Loop->getLoopLatch() != nullptr) {
366-
LLVM_DEBUG(dbgs() << "[AGGREGATE] Node kind: Latch and Exiting.\n");
367-
TripCount = SE->getSmallConstantTripCount(C->getLoop());
368-
} else {
369-
LLVM_DEBUG(dbgs() << "[AGGREGATE] Node kind: Latch or Exiting.\n");
370-
auto BTC = SE->getBackedgeTakenCount(C->getLoop());
371-
if (isa<SCEVConstant>(BTC))
372-
TripCount = cast<SCEVConstant>(BTC)->getAPInt().getZExtValue();
373-
}
374-
LLVM_DEBUG(dbgs() << "[AGGREGATE] Total trip count: " << TripCount << "\n");
362+
if (C->getLoop()->getLoopLatch() == C->getLoop()->getExitingBlock()) {
363+
LLVM_DEBUG(dbgs() << "[AGGREGATE] Node kind: Latch and Exiting.\n");
364+
TripCount = SE->getSmallConstantTripCount(C->getLoop());
365+
} else {
366+
LLVM_DEBUG(dbgs() << "[AGGREGATE] Node kind: Latch or Exiting.\n");
367+
auto BTC = SE->getBackedgeTakenCount(C->getLoop());
368+
if (isa<SCEVConstant>(BTC))
369+
TripCount = cast<SCEVConstant>(BTC)->getAPInt().getZExtValue();
375370
}
376-
if (TripCount > 1) {
377-
LLVM_DEBUG(dbgs() << "[AGGREGATE] Aggregate constant range.\n");
371+
if (TripCount > 0) {
372+
LLVM_DEBUG(dbgs() << "[AGGREGATE] Aggregate a constant range.\n");
378373
int64_t SignedRangeMin = SE->getSignedRangeMin(SCEV).getSExtValue();
379374
if (SignedRangeMin < 0) {
380375
LLVM_DEBUG(dbgs() << "[AGGREGATE] Range bounds must be "
@@ -384,7 +379,7 @@ std::pair<MemoryLocationRange, bool> aggregate(
384379
DimInfo.Start = SE->getConstant(SCEV->getType(), SignedRangeMin);
385380
DimInfo.End = SE->getConstant(SCEV->getType(),
386381
SignedRangeMin + std::abs(AddRecStep) * (TripCount - 1));
387-
} else if (TripCount == 0) {
382+
} else {
388383
if (std::abs(AddRecStep) != 1)
389384
break;
390385
auto Bounds = C->getLoop()->getBounds(*SE);
@@ -433,13 +428,14 @@ std::pair<MemoryLocationRange, bool> aggregate(
433428
if (Bounds->getDirection() == Loop::LoopBounds::Direction::Increasing) {
434429
if (Predicate == Predicate::ICMP_SLT ||
435430
Predicate == Predicate::ICMP_ULT) {
436-
FinalSCEV = SE->getMinusSCEV(FinalSCEV, SE->getOne(FinalSCEV->getType()));
431+
FinalSCEV = SE->getMinusSCEV(FinalSCEV,
432+
SE->getOne(FinalSCEV->getType()));
437433
}
438434
} else {
439-
//std::swap(InitSCEV, FinalSCEV);
440435
if (Predicate == Predicate::ICMP_SGT ||
441436
Predicate == Predicate::ICMP_UGT) {
442-
FinalSCEV = SE->getAddExpr(SE->getOne(FinalSCEV->getType()), FinalSCEV);
437+
FinalSCEV = SE->getAddExpr(SE->getOne(FinalSCEV->getType()),
438+
FinalSCEV);
443439
}
444440
}
445441
auto ZeroItr = C->evaluateAtIteration(SE->getZero(C->getType()), *SE);
@@ -468,11 +464,9 @@ std::pair<MemoryLocationRange, bool> aggregate(
468464
LLVM_DEBUG(dbgs() << "[AGGREGATE] Incorrect bounds.\n");
469465
break;
470466
}
471-
} else {
472-
LLVM_DEBUG(dbgs() << "[AGGREGATE] Invalid trip count.\n");
473-
break;
474467
}
475-
DimInfo.Step = SE->getConstant(C->getStepRecurrence(*SE)->getType(), std::abs(AddRecStep));
468+
DimInfo.Step = SE->getConstant(C->getStepRecurrence(*SE)->getType(),
469+
std::abs(AddRecStep));
476470
assert(DimInfo.Start && DimInfo.End && DimInfo.Step &&
477471
"Dimension bounds and step must be specified!");
478472
if (isa<SCEVConstant>(DimInfo.End) &&
@@ -582,14 +576,12 @@ class AddKnownAccessFunctor :
582576

583577
private:
584578
void addMust(const MemoryLocationRange &Loc) {
585-
static_cast<ImpTy *>(this)->addMust(
586-
aggregate(nullptr, nullptr, Loc, DFF).first);
579+
static_cast<ImpTy *>(this)->addMust(aggregate(nullptr, Loc, DFF).first);
587580
static_cast<ImpTy *>(this)->addMust(Loc);
588581
}
589582

590583
void addMay(const MemoryLocationRange &Loc) {
591-
static_cast<ImpTy *>(this)->addMay(
592-
aggregate(nullptr, nullptr, Loc, DFF).first);
584+
static_cast<ImpTy *>(this)->addMay(aggregate(nullptr, Loc, DFF).first);
593585
static_cast<ImpTy *>(this)->addMay(Loc);
594586
}
595587

@@ -1035,7 +1027,6 @@ void ReachDFFwk::collapse(DFRegion *R) {
10351027
dbgs() << " (" << Loc.getKindAsString() << ")\n";
10361028
};
10371029
#endif
1038-
//llvm::SmallVector<std::pair<MemoryLocationRange, DFNode *>, 16> ExpAccessesWithNodes;
10391030
for (DFNode *N : R->getNodes()) {
10401031
auto DefItr = getDefInfo().find(N);
10411032
assert(DefItr != getDefInfo().end() &&
@@ -1059,7 +1050,7 @@ void ReachDFFwk::collapse(DFRegion *R) {
10591050
continue;
10601051
}
10611052
LLVM_DEBUG(printLocInfo("[COLLAPSE] Collapse Use location: ", Loc));
1062-
distribute(aggregate(R, N, Loc, this), Loc, OwnUses, OtherUses);
1053+
distribute(aggregate(R, Loc, this), Loc, OwnUses, OtherUses);
10631054
}
10641055
OwnUses.clarify(OtherUses);
10651056
for (auto &Loc : OwnUses) {
@@ -1125,12 +1116,12 @@ void ReachDFFwk::collapse(DFRegion *R) {
11251116
for (auto &L : Locs) {
11261117
LLVM_DEBUG(printLocInfo(
11271118
"[COLLAPSE] Add location from a latch node: ", L));
1128-
distribute(aggregate(R, N, L, this), Loc, OwnDefs, OtherDefs);
1119+
distribute(aggregate(R, L, this), Loc, OwnDefs, OtherDefs);
11291120
}
11301121
} else {
11311122
LLVM_DEBUG(printLocInfo(
11321123
"[COLLAPSE] Collapse MayDef location of a latch node: ", Loc));
1133-
distribute(aggregate(R, N, Loc, this), Loc, OwnMayDefs, OtherMayDefs);
1124+
distribute(aggregate(R, Loc, this), Loc, OwnMayDefs, OtherMayDefs);
11341125
}
11351126
}
11361127
LLVM_DEBUG(dbgs() << "[COLLAPSE] Check ExitingDefs.\n");
@@ -1139,11 +1130,11 @@ void ReachDFFwk::collapse(DFRegion *R) {
11391130
if (!Locs.empty()) {
11401131
for (auto &L : Locs) {
11411132
LLVM_DEBUG(printLocInfo("[COLLAPSE] Collapse Def location: ", L));
1142-
distribute(aggregate(R, N, L, this), Loc, OwnDefs, OtherDefs);
1133+
distribute(aggregate(R, L, this), Loc, OwnDefs, OtherDefs);
11431134
}
11441135
} else {
11451136
LLVM_DEBUG(printLocInfo("[COLLAPSE] Collapse MayDef location: ", Loc));
1146-
distribute(aggregate(R, N, Loc, this), Loc, OwnMayDefs, OtherMayDefs);
1137+
distribute(aggregate(R, Loc, this), Loc, OwnMayDefs, OtherMayDefs);
11471138
}
11481139
}
11491140
OwnDefs.clarify(OtherDefs);
@@ -1152,12 +1143,10 @@ void ReachDFFwk::collapse(DFRegion *R) {
11521143
if (Loc.Kind & MemoryLocationRange::LocKind::Hint)
11531144
continue;
11541145
LLVM_DEBUG(printLocInfo("[COLLAPSE] Collapse MayDef location: ", Loc));
1155-
distribute(aggregate(R, N, Loc, this), Loc, OwnMayDefs, OtherMayDefs);
1146+
distribute(aggregate(R, Loc, this), Loc, OwnMayDefs, OtherMayDefs);
11561147
}
11571148
OwnMayDefs.clarify(OtherMayDefs);
11581149
DefUse->addMayDefs(OwnMayDefs);
1159-
//for (auto &ExpLoc: DU->getExplicitAccesses())
1160-
// ExpAccessesWithNodes.push_back(std::make_pair(ExpLoc, N));
11611150
DefUse->addExplicitAccesses(DU->getExplicitAccesses());
11621151
DefUse->addExplicitUnknowns(DU->getExplicitUnknowns());
11631152
for (auto Loc : DU->getAddressAccesses())
@@ -1170,9 +1159,7 @@ void ReachDFFwk::collapse(DFRegion *R) {
11701159
for (auto Inst : DU->getUnknownInsts())
11711160
DefUse->addUnknownInst(Inst);
11721161
}
1173-
for (auto &ExpNode : DefUse->getExplicitAccesses()) {
1174-
auto &Loc = ExpNode/*.first*/;
1175-
//DefUse->addExplicitAccess(Loc);
1162+
for (auto &Loc : DefUse->getExplicitAccesses()) {
11761163
auto *EM{AT.find(Loc)};
11771164
if (EM) {
11781165
if (auto *DFL{dyn_cast<DFLoop>(R)})
@@ -1187,7 +1174,7 @@ void ReachDFFwk::collapse(DFRegion *R) {
11871174
EM = EM->getTopLevelParent();
11881175
}
11891176
LLVM_DEBUG(printLocInfo("[COLLAPSE] Collapse Explicit Access: ", Loc));
1190-
MemoryLocationRange ExpLoc = aggregate(R, /*ExpNode.second*/ nullptr, Loc, this).first;
1177+
MemoryLocationRange ExpLoc = aggregate(R, Loc, this).first;
11911178
MemoryLocationRange NewLoc(Loc);
11921179
NewLoc.Kind |= MemoryLocationRange::LocKind::Hint;
11931180
// We use top-level parent (EM) if it is available to update def-use set.

0 commit comments

Comments
 (0)