LLVM: lib/Transforms/Utils/DebugSSAUpdater.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
18
19using namespace llvm;
20
21#define DEBUG_TYPE "debug-ssa-updater"
22
24 OS << "DbgVal{ ";
26 OS << "undef }";
27 return;
28 }
29 if (Phi) {
30 OS << *Phi << "}";
31 return;
32 }
34 << " }";
35}
36
38 OS << "DbgPhi ";
40 OS << "[" << BB->BB.getName() << ", " << DV << "] ";
41}
42
44
46 : InsertedPHIs(NewPHI) {}
47
49
51 return AV.count(BB);
52}
53
55 return AV.lookup(BB);
56}
57
61
63 DbgValueDef Res = getValueAtEndOfBlockInternal(BB);
64 return Res;
65}
66
68
69
72
73
74
77
78 bool IsFirstPred = true;
81 PredValues.push_back(std::make_pair(PredBB, PredVal));
82
83
84 if (IsFirstPred) {
85 SingularValue = PredVal;
86 IsFirstPred = false;
87 } else if (!PredVal.agreesWith(SingularValue))
89 }
90
91
92 if (PredValues.empty())
94
95
96 if (!SingularValue.IsUndef)
97 return SingularValue;
98
99
101
102
103 for (const auto &PredValue : PredValues)
104 InsertedPHI->addIncoming(PredValue.first, PredValue.second);
105
106
107
108
109
110 if (InsertedPHIs)
111 InsertedPHIs->push_back(InsertedPHI);
112
113 LLVM_DEBUG(dbgs() << " Inserted PHI: " << *InsertedPHI << "\n");
114 return InsertedPHI;
115}
116
123
124namespace llvm {
125
127public:
132
135
137 private:
139 unsigned Idx;
140
141 public:
145 : PHI(P), Idx(PHI->getNumIncomingValues()) {}
146
148 ++Idx;
149 return *this;
150 }
153
156 };
157
160
161
162
168
169
170
174
175
181
182
183
186 PHI->addIncoming(Pred, Val);
187 }
188
189
191 return Val.Phi;
192 }
193
194
195
198 if (PHI && PHI->getNumIncomingValues() == 0)
199 return PHI;
200 return nullptr;
201 }
202
203
204
206};
207
208}
209
210
211
212
214 if (AV.contains(BB))
215 return AV[BB];
216
217 SSAUpdaterImpl Impl(this, &AV, InsertedPHIs);
218 return Impl.GetValue(BB);
219}
220
222 if (Inner == Outer)
223 return true;
225 return false;
227}
228
232
235 int NumRecordsFound = 0;
237 bool DeclareRecordFound = false;
238
239 LLVM_DEBUG(dbgs() << "Finding variable info for " << *Var << " at "
240 << InlinedAt << "\n");
241
242 for (auto &BB : *F) {
243 auto &DbgRecordValues = BlockDbgRecordValues[&BB];
244 bool FoundInstructionInScope = false;
245 for (auto &I : BB) {
247
249 if (DVR.getVariable() == Var &&
250 DVR.getDebugLoc().getInlinedAt() == InlinedAt) {
251 assert(!DVR.isDbgAssign() && "No support for #dbg_assign yet.");
252 if (DVR.isDbgDeclare())
253 DeclareRecordFound = true;
254 ++NumRecordsFound;
255 LastRecordFound = &DVR;
256 DbgRecordValues.push_back(&DVR);
257 }
258 }
259 if (!FoundInstructionInScope && I.getDebugLoc()) {
260 if (I.getDebugLoc().getInlinedAt() == InlinedAt &&
263 FoundInstructionInScope = true;
264 HasAnyInstructionsInScope.insert(&BB);
265 }
266 }
267 }
268 LLVM_DEBUG(dbgs() << "DbgRecordValues found in '" << BB.getName() << "':\n";
269 for_each(DbgRecordValues, [](auto *DV) { DV->dump(); }));
270 }
271
272 if (!NumRecordsFound) {
273 LLVM_DEBUG(dbgs() << "No dbg_records found for variable!\n");
274 return;
275 }
276
277
278
279
280
281
282
283 if (DeclareRecordFound) {
284
285 LLVM_DEBUG(dbgs() << "Single location found for variable!\n");
286 assert(NumRecordsFound == 1 &&
287 "Found multiple records for a #dbg_declare variable!");
288 OrigSingleLocVariableValueTable[DVA] = DbgValueDef(LastRecordFound);
289 return;
290 }
291
292
293
297 for (auto &[BB, DVs] : BlockDbgRecordValues) {
298 auto *DbgBB = SSAUpdater.getDbgSSABlock(BB);
299 if (DVs.empty())
300 continue;
301 auto *LastValueInBlock = DVs.back();
302 LLVM_DEBUG(dbgs() << "Last value in " << BB->getName() << ": "
303 << *LastValueInBlock << "\n");
305 }
306
308 if (!HasAnyInstructionsInScope.contains(&BB)) {
309 LLVM_DEBUG(dbgs() << "Skipping finding debug ranges for '" << BB.getName()
310 << "' due to no in-scope instructions.\n");
311 continue;
312 }
313 LLVM_DEBUG(dbgs() << "Finding live-in value for '" << BB.getName()
314 << "'...\n");
317 LLVM_DEBUG(dbgs() << "Found live-in: " << LiveValue << "\n");
318 auto HasValidValue = [](DbgValueDef DV) {
319 return !DV.IsUndef && DV.Phi == nullptr;
320 };
321
324 for (auto *DVR : BlockDbgRecordValues[&BB]) {
325
328 if (HasValidValue(LiveValue))
329 BlockDbgRanges.push_back({LastIt, DVRStartIt, LiveValue});
331 LastIt = DVRStartIt;
332 }
333
334
335
336 if (HasValidValue(LiveValue))
337 BlockDbgRanges.push_back({LastIt, BB.end(), LiveValue});
338 LLVM_DEBUG(dbgs() << "Create set of ranges with " << BlockDbgRanges.size()
339 << " entries!\n");
340 if (!BlockDbgRanges.empty())
341 OrigVariableValueRangeTable[DVA].append(BlockDbgRanges);
342 }
343}
344
347 OS << "Variable Table for '" << DVA.getVariable()->getName() << "' (at "
350 OS << " Empty!\n";
351 return;
352 }
354 OS << " SingleLoc: " << OrigSingleLocVariableValueTable[DVA] << "\n";
355 return;
356 }
357 OS << " LocRange:\n";
358 for (DbgRangeEntry RangeEntry : OrigVariableValueRangeTable[DVA]) {
359 OS << " (";
360 if (RangeEntry.Start == RangeEntry.Start->getParent()->begin() &&
361 RangeEntry.End == RangeEntry.Start->getParent()->end()) {
362 OS << RangeEntry.Start->getParent()->getName();
363 } else {
364 OS << RangeEntry.Start->getParent()->getName() << ": "
365 << *RangeEntry.Start << ", ";
366 if (RangeEntry.End == RangeEntry.Start->getParent()->end())
367 OS << "..";
368 else
369 OS << *RangeEntry.End;
370 }
371 OS << ") [" << RangeEntry.Value << "]\n";
372 }
373}
374
376 auto ExistingID = ValueToIDMap.find(V);
377 if (ExistingID != ValueToIDMap.end())
378 return ExistingID->second;
379
380 ValueID NewID = NextID++;
381 ValueToIDMap.insert({V, NewID});
382
383 assert(!ValueIDToNameMap.contains(NewID) &&
384 "New value ID already maps to a name?");
385 std::string &ValueText = ValueIDToNameMap[NewID];
387 V->printAsOperand(Stream, true);
388 return NewID;
389}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
bool isContained(DIScope *Inner, DIScope *Outer)
Definition DebugSSAUpdater.cpp:221
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
DenseMap< MachineBasicBlock *, Register > AvailableValsTy
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
LLVM Basic Block Representation.
InstListType::iterator iterator
Instruction iterators...
DILocalScope * getScope() const
Get the local scope for this variable.
Base class for scope-like contexts.
LLVM_ABI DIScope * getScope() const
StringRef getName() const
DebugSSAUpdater & Updater
DbgSSABlock * operator*()
Definition DebugSSAUpdater.cpp:120
Thin wrapper around a block successor iterator.
DebugSSAUpdater & Updater
DbgSSABlock * operator*()
Definition DebugSSAUpdater.cpp:117
iterator_range< DbgSSABlockPredIterator > predecessors()
DbgSSABlockSuccIterator succ_end()
DbgSSABlockPredIterator pred_end()
DbgSSABlockPredIterator pred_begin()
DbgSSABlockSuccIterator succ_begin()
DbgSSAPhi * newPHI()
SSAUpdater has requested a PHI: create that within this block record.
Represents the live-in definitions of a variable to a block with multiple predecessors.
SmallVector< std::pair< DbgSSABlock *, DbgValueDef >, 4 > IncomingValues
void addIncoming(DbgSSABlock *BB, DbgValueDef DV)
void print(raw_ostream &OS) const
Definition DebugSSAUpdater.cpp:37
void printValues(DebugVariableAggregate DVA, raw_ostream &OS)
Definition DebugSSAUpdater.cpp:345
void addVariable(Function *F, DebugVariableAggregate DVA)
Definition DebugSSAUpdater.cpp:229
bool hasSingleLocEntry(DebugVariableAggregate DVA) const
bool hasVariableEntry(DebugVariableAggregate DVA) const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
Class used to determine the live ranges of debug variables in IR using SSA construction (via the SSAU...
void initialize()
Definition DebugSSAUpdater.cpp:48
DebugSSAUpdater(SmallVectorImpl< DbgSSAPhi * > *InsertedPHIs=nullptr)
If InsertedPHIs is specified, it will be filled in with all PHI Nodes created by rewriting.
Definition DebugSSAUpdater.cpp:45
DbgValueDef findValueForBlock(DbgSSABlock *BB) const
Return the value for the specified block if the DebugSSAUpdater has one, otherwise return nullptr.
Definition DebugSSAUpdater.cpp:54
void addAvailableValue(DbgSSABlock *BB, DbgValueDef DV)
Indicate that a rewritten value is available in the specified block with the specified value.
Definition DebugSSAUpdater.cpp:58
DbgValueDef getValueAtEndOfBlock(DbgSSABlock *BB)
Construct SSA form, materializing a value that is live at the end of the specified block.
Definition DebugSSAUpdater.cpp:62
DbgValueDef getValueInMiddleOfBlock(DbgSSABlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block.
Definition DebugSSAUpdater.cpp:67
bool hasValueForBlock(DbgSSABlock *BB) const
Return true if the DebugSSAUpdater already has a value for the specified block.
Definition DebugSSAUpdater.cpp:50
Identifies a unique instance of a whole variable (discards/ignores fragment information).
const DILocation * getInlinedAt() const
const DILocalVariable * getVariable() const
Implements a dense probed hash-table based set.
bool operator!=(const PHI_iterator &X) const
Definition DebugSSAUpdater.cpp:152
DbgSSABlock * getIncomingBlock()
Definition DebugSSAUpdater.cpp:155
DbgValueDef getIncomingValue()
Definition DebugSSAUpdater.cpp:154
PHI_iterator & operator++()
Definition DebugSSAUpdater.cpp:147
PHI_iterator(DbgSSAPhi *P)
Definition DebugSSAUpdater.cpp:142
bool operator==(const PHI_iterator &X) const
Definition DebugSSAUpdater.cpp:151
PHI_iterator(DbgSSAPhi *P, bool)
Definition DebugSSAUpdater.cpp:144
DbgValueDef ValT
Definition DebugSSAUpdater.cpp:129
DbgSSAPhi PhiT
Definition DebugSSAUpdater.cpp:130
static void FindPredecessorBlocks(DbgSSABlock *BB, SmallVectorImpl< DbgSSABlock * > *Preds)
FindPredecessorBlocks - Put the predecessors of BB into the Preds vector.
Definition DebugSSAUpdater.cpp:163
static DbgSSAPhi * ValueIsPHI(DbgValueDef Val, DebugSSAUpdater *Updater)
ValueIsPHI - Check if a value is a PHI.
Definition DebugSSAUpdater.cpp:190
static PHI_iterator PHI_end(PhiT *PHI)
Definition DebugSSAUpdater.cpp:159
DbgSSABlock BlkT
Definition DebugSSAUpdater.cpp:128
static void AddPHIOperand(DbgSSAPhi *PHI, DbgValueDef Val, DbgSSABlock *Pred)
AddPHIOperand - Add the specified value as an operand of the PHI for the specified predecessor block.
Definition DebugSSAUpdater.cpp:184
static DbgValueDef GetPHIValue(DbgSSAPhi *PHI)
GetPHIValue - For the specified PHI instruction, return the value that it defines.
Definition DebugSSAUpdater.cpp:205
static DbgSSAPhi * CreateEmptyPHI(DbgSSABlock *BB, unsigned NumPreds, DebugSSAUpdater *Updater)
CreateEmptyPHI - Create a new debug PHI entry for the specified block.
Definition DebugSSAUpdater.cpp:176
static BlkSucc_iterator BlkSucc_end(BlkT *BB)
Definition DebugSSAUpdater.cpp:134
static PHI_iterator PHI_begin(PhiT *PHI)
Definition DebugSSAUpdater.cpp:158
DbgSSABlockSuccIterator BlkSucc_iterator
Definition DebugSSAUpdater.cpp:131
static BlkSucc_iterator BlkSucc_begin(BlkT *BB)
Definition DebugSSAUpdater.cpp:133
static DbgSSAPhi * ValueIsNewPHI(DbgValueDef Val, DebugSSAUpdater *Updater)
ValueIsNewPHI - Like ValueIsPHI but also check if the PHI has no source operands, i....
Definition DebugSSAUpdater.cpp:196
static DbgValueDef GetPoisonVal(DbgSSABlock *BB, DebugSSAUpdater *Updater)
GetPoisonVal - Get an undefined value of the same type as the value being handled.
Definition DebugSSAUpdater.cpp:171
Helper class for SSA formation on a set of values defined in multiple blocks.
ValueID addValue(Value *V)
Definition DebugSSAUpdater.cpp:375
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
LLVM Value Representation.
std::pair< iterator, bool > insert(const ValueT &V)
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
self_iterator getIterator()
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an std::string.
This is an optimization pass for GlobalISel generic memory operations.
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
decltype(auto) cast(const From &Val)
cast - Return the argument parameter cast to the specified type.
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
A definition of a variable; can represent either a debug value, no definition (the variable has not y...
void print(raw_ostream &OS) const
Definition DebugSSAUpdater.cpp:23
DIExpression * Expression
bool agreesWith(DbgValueDef Other) const