Skip to content

Commit 728abe1

Browse files
committed
[GS/StackClash] Also use LatticeT to implement RegConstValues.
1 parent cf02d92 commit 728abe1

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

bolt/lib/Passes/StackClashAnalysis.cpp

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ template <typename T, auto MergeValLambda> class LatticeT {
140140
}
141141
};
142142

143-
144143
template <typename T, auto M>
145144
raw_ostream &operator<<(raw_ostream &OS, const LatticeT<T, M> &V);
146145

@@ -235,6 +234,38 @@ void addToMaxMap(RegMaxValuesT &M, MCPhysReg R, const uint64_t Value) {
235234
MIt->second = std::max(MIt->second, Value);
236235
}
237236

237+
using Reg2ConstValT = SmallDenseMap<MCPhysReg, uint64_t, 1>;
238+
bool RegConstValuesValMerge(Reg2ConstValT &v1, const Reg2ConstValT &v2) {
239+
SmallVector<MCPhysReg, 1> RegConstValuesToRemove;
240+
for (auto Reg2ConstValue : v1) {
241+
const MCPhysReg R(Reg2ConstValue.first);
242+
// const uint64_t ConstValue(Reg2ConstValue.second);
243+
auto v2Reg2ConstValue = v2.find(R);
244+
if (v2Reg2ConstValue == v2.end())
245+
RegConstValuesToRemove.push_back(R);
246+
else if (Reg2ConstValue.second != v2Reg2ConstValue->second) {
247+
RegConstValuesToRemove.push_back(R);
248+
#if 0
249+
// FIXME: how to add this back in?
250+
addToMaxMap(RegMaxValues, R, ConstValue);
251+
#endif
252+
}
253+
}
254+
for (MCPhysReg R : RegConstValuesToRemove)
255+
v1.erase(R);
256+
return true;
257+
}
258+
#if 0
259+
raw_ostream &operator<<(raw_ostream &OS, const Reg2ConstValT &M) {
260+
for (auto Reg2Value : M) {
261+
print_reg(OS, Reg2Value.first, nullptr);
262+
OS << ":" << Reg2Value.second << ",";
263+
}
264+
return OS;
265+
}
266+
#endif
267+
using RegConstValuesT = LatticeT<Reg2ConstValT, RegConstValuesValMerge>;
268+
238269
template <typename T, auto M>
239270
raw_ostream &operator<<(raw_ostream &OS, const LatticeT<T, M> &V) {
240271
if (V == V.Top())
@@ -246,14 +277,13 @@ raw_ostream &operator<<(raw_ostream &OS, const LatticeT<T, M> &V) {
246277
return OS;
247278
}
248279

249-
250280
struct State {
251281
// Store the maximum possible offset to which the stack extends
252282
// beyond the furthest probe seen.
253283
MaxOffsetSinceLastProbeT MaxOffsetSinceLastProbe;
254284
/// ExactValues stores registers that we know have a specific
255285
/// constant value.
256-
SmallDenseMap<MCPhysReg, uint64_t, 1> RegConstValues;
286+
RegConstValuesT RegConstValues;
257287
/// RegMaxValues stores registers that we know have a value in the
258288
/// range [0, MaxValue-1].
259289
// FIXME: also make this std::optional!!!
@@ -286,6 +316,7 @@ struct State {
286316
State &operator&=(const State &StateIn) {
287317
MaxOffsetSinceLastProbe &= StateIn.MaxOffsetSinceLastProbe;
288318

319+
#if 0
289320
SmallVector<MCPhysReg, 1> RegConstValuesToRemove;
290321
for (auto Reg2ConstValue : RegConstValues) {
291322
const MCPhysReg R(Reg2ConstValue.first);
@@ -300,6 +331,8 @@ struct State {
300331
}
301332
for (MCPhysReg R : RegConstValuesToRemove)
302333
RegConstValues.erase(R);
334+
#endif
335+
RegConstValues &= StateIn.RegConstValues;
303336

304337
RegMaxValues &= StateIn.RegMaxValues;
305338

@@ -341,7 +374,12 @@ raw_ostream &print_state(raw_ostream &OS, const State &S,
341374
else
342375
OS << *(S.MaxOffsetSinceLastProbe);
343376
OS << "), RegConstValues(";
344-
PrintRegMap(OS, S.RegConstValues, BC);
377+
if (S.RegConstValues.hasVal()) {
378+
OS << "(";
379+
PrintRegMap(OS, *S.RegConstValues, BC);
380+
OS << ")";
381+
} else
382+
OS << S.RegConstValues;
345383
OS << "), RegMaxValues(";
346384
if (S.RegMaxValues.hasVal()) {
347385
OS << "(";
@@ -392,8 +430,9 @@ bool checkNonConstSPOffsetChange(const BinaryContext &BC, BinaryFunction &BF,
392430
// non-constant amount, and hence violates stack-clash properties.
393431
if (Next)
394432
Next->LastStackGrowingInsts.insert(MCInstInBBReference::get(&Point, BF));
395-
if (auto OC = BC.MIB->getOffsetChange(Point, Cur.RegConstValues,
396-
Cur.RegMaxValues.getValOrDefault());
433+
if (auto OC =
434+
BC.MIB->getOffsetChange(Point, Cur.RegConstValues.getValOrDefault(),
435+
Cur.RegMaxValues.getValOrDefault());
397436
OC && OC.ToReg == SP) {
398437
if (OC.FromReg == SP) {
399438
IsNonConstantSPOffsetChange = false;
@@ -497,6 +536,7 @@ class StackClashDFAnalysis
497536
if (BB.isEntryPoint()) {
498537
Next.Reg2MaxOffset = Reg2MaxOffsetValT();
499538
Next.RegMaxValues = Reg2MaxValT();
539+
Next.RegConstValues = Reg2ConstValT();
500540
}
501541
return Next;
502542
}
@@ -546,7 +586,8 @@ class StackClashDFAnalysis
546586
BC.printInstruction(dbgs(), Point);
547587
dbgs() << "\n";
548588
});
549-
Next.RegConstValues[ConstValueReg] = ConstValue;
589+
if (Next.RegConstValues.hasVal())
590+
(*Next.RegConstValues)[ConstValueReg] = ConstValue;
550591
}
551592

552593
MCPhysReg MaxValueReg = BC.MIB->getNoRegister();
@@ -580,8 +621,8 @@ class StackClashDFAnalysis
580621
assert(Operand.isReg());
581622
if (Next.RegMaxValues.hasVal() && Operand.getReg() != MaxValueReg)
582623
Next.RegMaxValues->erase(Operand.getReg());
583-
if (Operand.getReg() != ConstValueReg)
584-
Next.RegConstValues.erase(Operand.getReg());
624+
if (Next.RegConstValues.hasVal() && Operand.getReg() != ConstValueReg)
625+
Next.RegConstValues->erase(Operand.getReg());
585626
}
586627

587628
if (!Next.MaxOffsetSinceLastProbe)
@@ -604,8 +645,9 @@ class StackClashDFAnalysis
604645
}
605646

606647
MCPhysReg FixedOffsetRegJustSet = BC.MIB->getNoRegister();
607-
if (auto OC = BC.MIB->getOffsetChange(Point, Cur.RegConstValues,
608-
Cur.RegMaxValues.getValOrDefault()))
648+
if (auto OC =
649+
BC.MIB->getOffsetChange(Point, Cur.RegConstValues.getValOrDefault(),
650+
Cur.RegMaxValues.getValOrDefault()))
609651
if (Next.Reg2MaxOffset.hasVal() && OC.OffsetChange) {
610652
int64_t Offset = *OC.OffsetChange;
611653
if (OC.FromReg == SP) {

0 commit comments

Comments
 (0)