| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#ifndef LLVM_CODEGEN_LEXICALSCOPES_H |
| 17 |
#define LLVM_CODEGEN_LEXICALSCOPES_H |
| 18 |
|
| 19 |
#include "llvm/ADT/ArrayRef.h" |
| 20 |
#include "llvm/ADT/DenseMap.h" |
| 21 |
#include "llvm/ADT/SmallPtrSet.h" |
| 22 |
#include "llvm/ADT/SmallVector.h" |
| 23 |
#include "llvm/IR/DebugInfoMetadata.h" |
| 24 |
#include <cassert> |
| 25 |
#include <unordered_map> |
| 26 |
#include <utility> |
| 27 |
|
| 28 |
namespace llvm { |
| 29 |
|
| 30 |
class MachineBasicBlock; |
| 31 |
class MachineFunction; |
| 32 |
class MachineInstr; |
| 33 |
class MDNode; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
using InsnRange = std::pair<const MachineInstr *, const MachineInstr *>; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class LexicalScope { |
| 45 |
public: |
| 46 |
LexicalScope(LexicalScope *P, const DILocalScope *D, const DILocation *I, |
| 47 |
bool A) |
| 48 |
: Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A) { |
| 49 |
assert(D); |
| 50 |
assert(D->getSubprogram()->getUnit()->getEmissionKind() != |
| 51 |
DICompileUnit::NoDebug && |
| 52 |
"Don't build lexical scopes for non-debug locations"); |
| 53 |
assert(D->isResolved() && "Expected resolved node"); |
| 54 |
assert((!I || I->isResolved()) && "Expected resolved node"); |
| 55 |
if (Parent) |
| 56 |
Parent->addChild(this); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
LexicalScope *getParent() const { return Parent; } |
| 61 |
const MDNode *getDesc() const { return Desc; } |
| 62 |
const DILocation *getInlinedAt() const { return InlinedAtLocation; } |
| 63 |
const DILocalScope *getScopeNode() const { return Desc; } |
| 64 |
bool isAbstractScope() const { return AbstractScope; } |
| 65 |
SmallVectorImpl<LexicalScope *> &getChildren() { return Children; } |
| 66 |
SmallVectorImpl<InsnRange> &getRanges() { return Ranges; } |
| 67 |
|
| 68 |
|
| 69 |
void addChild(LexicalScope *S) { Children.push_back(S); } |
| 70 |
|
| 71 |
|
| 72 |
void openInsnRange(const MachineInstr *MI) { |
| 73 |
if (!FirstInsn) |
| 74 |
FirstInsn = MI; |
| 75 |
|
| 76 |
if (Parent) |
| 77 |
Parent->openInsnRange(MI); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
void extendInsnRange(const MachineInstr *MI) { |
| 83 |
assert(FirstInsn && "MI Range is not open!"); |
| 84 |
LastInsn = MI; |
| 85 |
if (Parent) |
| 86 |
Parent->extendInsnRange(MI); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
void closeInsnRange(LexicalScope *NewScope = nullptr) { |
| 93 |
assert(LastInsn && "Last insn missing!"); |
| 94 |
Ranges.push_back(InsnRange(FirstInsn, LastInsn)); |
| 95 |
FirstInsn = nullptr; |
| 96 |
LastInsn = nullptr; |
| 97 |
|
| 98 |
|
| 99 |
if (Parent && (!NewScope || !Parent->dominates(NewScope))) |
| 100 |
Parent->closeInsnRange(NewScope); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
bool dominates(const LexicalScope *S) const { |
| 105 |
if (S == this) |
| 106 |
return true; |
| 107 |
if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut()) |
| 108 |
return true; |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
unsigned getDFSOut() const { return DFSOut; } |
| 114 |
void setDFSOut(unsigned O) { DFSOut = O; } |
| 115 |
unsigned getDFSIn() const { return DFSIn; } |
| 116 |
void setDFSIn(unsigned I) { DFSIn = I; } |
| 117 |
|
| 118 |
|
| 119 |
void dump(unsigned Indent = 0) const; |
| 120 |
|
| 121 |
private: |
| 122 |
LexicalScope *Parent; |
| 123 |
const DILocalScope *Desc; |
| 124 |
const DILocation *InlinedAtLocation; |
| 125 |
|
| 126 |
bool AbstractScope; |
| 127 |
SmallVector<LexicalScope *, 4> Children; |
| 128 |
|
| 129 |
SmallVector<InsnRange, 4> Ranges; |
| 130 |
|
| 131 |
const MachineInstr *LastInsn = nullptr; |
| 132 |
const MachineInstr *FirstInsn = nullptr; |
| 133 |
unsigned DFSIn = 0; |
| 134 |
unsigned DFSOut = 0; |
| 135 |
}; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
class LexicalScopes { |
| 142 |
public: |
| 143 |
LexicalScopes() = default; |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
void initialize(const MachineFunction &); |
| 148 |
|
| 149 |
|
| 150 |
void reset(); |
| 151 |
|
| 152 |
|
| 153 |
bool empty() { return CurrentFnLexicalScope == nullptr; } |
| 154 |
|
| 155 |
|
| 156 |
LexicalScope *getCurrentFunctionScope() const { |
| 157 |
return CurrentFnLexicalScope; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
void getMachineBasicBlocks(const DILocation *DL, |
| 164 |
SmallPtrSetImpl<const MachineBasicBlock *> &MBBs); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
bool dominates(const DILocation *DL, MachineBasicBlock *MBB); |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
LexicalScope *findLexicalScope(const DILocation *DL); |
| 173 |
|
| 174 |
|
| 175 |
ArrayRef<LexicalScope *> getAbstractScopesList() const { |
| 176 |
return AbstractScopesList; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
LexicalScope *findAbstractScope(const DILocalScope *N) { |
| 181 |
auto I = AbstractScopeMap.find(N); |
| 182 |
return I != AbstractScopeMap.end() ? &I->second : nullptr; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
LexicalScope *findInlinedScope(const DILocalScope *N, const DILocation *IA) { |
| 187 |
auto I = InlinedLexicalScopeMap.find(std::make_pair(N, IA)); |
| 188 |
return I != InlinedLexicalScopeMap.end() ? &I->second : nullptr; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
LexicalScope *findLexicalScope(const DILocalScope *N) { |
| 193 |
auto I = LexicalScopeMap.find(N); |
| 194 |
return I != LexicalScopeMap.end() ? &I->second : nullptr; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
LexicalScope *getOrCreateAbstractScope(const DILocalScope *Scope); |
| 199 |
|
| 200 |
private: |
| 201 |
|
| 202 |
|
| 203 |
LexicalScope *getOrCreateLexicalScope(const DILocalScope *Scope, |
| 204 |
const DILocation *IA = nullptr); |
| 205 |
LexicalScope *getOrCreateLexicalScope(const DILocation *DL) { |
| 206 |
return DL ? getOrCreateLexicalScope(DL->getScope(), DL->getInlinedAt()) |
| 207 |
: nullptr; |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
LexicalScope *getOrCreateRegularScope(const DILocalScope *Scope); |
| 212 |
|
| 213 |
|
| 214 |
LexicalScope *getOrCreateInlinedScope(const DILocalScope *Scope, |
| 215 |
const DILocation *InlinedAt); |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges, |
| 220 |
DenseMap<const MachineInstr *, LexicalScope *> &M); |
| 221 |
void constructScopeNest(LexicalScope *Scope); |
| 222 |
void |
| 223 |
assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges, |
| 224 |
DenseMap<const MachineInstr *, LexicalScope *> &M); |
| 225 |
|
| 226 |
const MachineFunction *MF = nullptr; |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
std::unordered_map<const DILocalScope *, LexicalScope> LexicalScopeMap; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
std::unordered_map<std::pair<const DILocalScope *, const DILocation *>, |
| 235 |
LexicalScope, |
| 236 |
pair_hash<const DILocalScope *, const DILocation *>> |
| 237 |
InlinedLexicalScopeMap; |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
std::unordered_map<const DILocalScope *, LexicalScope> AbstractScopeMap; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
SmallVector<LexicalScope *, 4> AbstractScopesList; |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
LexicalScope *CurrentFnLexicalScope = nullptr; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
using BlockSetT = SmallPtrSet<const MachineBasicBlock *, 4>; |
| 254 |
DenseMap<const DILocation *, std::unique_ptr<BlockSetT>> DominatedBlocks; |
| 255 |
}; |
| 256 |
|
| 257 |
} |
| 258 |
|
| 259 |
#endif |