#ifndef LLVM_CODEGEN_LEXICALSCOPES_H
#define LLVM_CODEGEN_LEXICALSCOPES_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Metadata.h"
#include "llvm/Support/DebugLoc.h"
#include "llvm/Support/ValueHandle.h"
#include <utility>
namespace llvm {
class MachineInstr;
class MachineBasicBlock;
class MachineFunction;
class LexicalScope;
typedef std::pair<const MachineInstr *, const MachineInstr *> InsnRange;
class LexicalScopes {
public:
LexicalScopes() : MF(NULL), CurrentFnLexicalScope(NULL) {}
~LexicalScopes();
void initialize(const MachineFunction &);
void reset();
bool empty() { return CurrentFnLexicalScope == NULL; }
bool isCurrentFunctionScope(const LexicalScope *LS) {
return LS == CurrentFnLexicalScope;
}
LexicalScope *getCurrentFunctionScope() const {
return CurrentFnLexicalScope;
}
void getMachineBasicBlocks(DebugLoc DL,
SmallPtrSet<const MachineBasicBlock *, 4> &MBBs);
bool dominates(DebugLoc DL, MachineBasicBlock *MBB);
LexicalScope *findLexicalScope(DebugLoc DL);
ArrayRef<LexicalScope *> getAbstractScopesList() const {
return AbstractScopesList;
}
LexicalScope *findAbstractScope(const MDNode *N) {
return AbstractScopeMap.lookup(N);
}
LexicalScope *findInlinedScope(DebugLoc DL) {
return InlinedLexicalScopeMap.lookup(DL);
}
LexicalScope *findLexicalScope(const MDNode *N) {
return LexicalScopeMap.lookup(N);
}
void dump();
private:
LexicalScope *getOrCreateLexicalScope(DebugLoc DL);
LexicalScope *getOrCreateRegularScope(MDNode *Scope);
LexicalScope *getOrCreateInlinedScope(MDNode *Scope, MDNode *InlinedAt);
LexicalScope *getOrCreateAbstractScope(const MDNode *N);
void extractLexicalScopes(SmallVectorImpl<InsnRange> &MIRanges,
DenseMap<const MachineInstr *, LexicalScope *> &M);
void constructScopeNest(LexicalScope *Scope);
void
assignInstructionRanges(SmallVectorImpl<InsnRange> &MIRanges,
DenseMap<const MachineInstr *, LexicalScope *> &M);
private:
const MachineFunction *MF;
DenseMap<const MDNode *, LexicalScope *> LexicalScopeMap;
DenseMap<DebugLoc, LexicalScope *> InlinedLexicalScopeMap;
DenseMap<const MDNode *, LexicalScope *> AbstractScopeMap;
SmallVector<LexicalScope *, 4> AbstractScopesList;
LexicalScope *CurrentFnLexicalScope;
};
class LexicalScope {
public:
LexicalScope(LexicalScope *P, const MDNode *D, const MDNode *I, bool A)
: Parent(P), Desc(D), InlinedAtLocation(I), AbstractScope(A), LastInsn(0),
FirstInsn(0), DFSIn(0), DFSOut(0) {
if (Parent)
Parent->addChild(this);
}
LexicalScope *getParent() const { return Parent; }
const MDNode *getDesc() const { return Desc; }
const MDNode *getInlinedAt() const { return InlinedAtLocation; }
const MDNode *getScopeNode() const { return Desc; }
bool isAbstractScope() const { return AbstractScope; }
SmallVectorImpl<LexicalScope *> &getChildren() { return Children; }
SmallVectorImpl<InsnRange> &getRanges() { return Ranges; }
void addChild(LexicalScope *S) { Children.push_back(S); }
void openInsnRange(const MachineInstr *MI) {
if (!FirstInsn)
FirstInsn = MI;
if (Parent)
Parent->openInsnRange(MI);
}
void extendInsnRange(const MachineInstr *MI) {
assert(FirstInsn && "MI Range is not open!");
LastInsn = MI;
if (Parent)
Parent->extendInsnRange(MI);
}
void closeInsnRange(LexicalScope *NewScope = NULL) {
assert(LastInsn && "Last insn missing!");
Ranges.push_back(InsnRange(FirstInsn, LastInsn));
FirstInsn = NULL;
LastInsn = NULL;
if (Parent && (!NewScope || !Parent->dominates(NewScope)))
Parent->closeInsnRange(NewScope);
}
bool dominates(const LexicalScope *S) const {
if (S == this)
return true;
if (DFSIn < S->getDFSIn() && DFSOut > S->getDFSOut())
return true;
return false;
}
unsigned getDFSOut() const { return DFSOut; }
void setDFSOut(unsigned O) { DFSOut = O; }
unsigned getDFSIn() const { return DFSIn; }
void setDFSIn(unsigned I) { DFSIn = I; }
void dump(unsigned Indent = 0) const;
private:
LexicalScope *Parent; AssertingVH<const MDNode> Desc; AssertingVH<const MDNode> InlinedAtLocation; bool AbstractScope; SmallVector<LexicalScope *, 4> Children; SmallVector<InsnRange, 4> Ranges;
const MachineInstr *LastInsn; const MachineInstr *FirstInsn; unsigned DFSIn, DFSOut; };
}
#endif