Skip to content

Commit 493eed1

Browse files
committed
optimize relooper id counters
1 parent 9e1c9c1 commit 493eed1

18 files changed

+1604
-1611
lines changed

src/relooper/Relooper.cpp

+4-8
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ void Branch::Render(Block *Target, bool SetLabel) {
101101

102102
// Block
103103

104-
int Block::IdCounter = 1; // 0 is reserved for clearings
105-
106-
Block::Block(const char *CodeInit, const char *BranchVarInit) : Parent(NULL), Id(Block::IdCounter++), IsCheckedMultipleEntry(false) {
104+
Block::Block(const char *CodeInit, const char *BranchVarInit) : Parent(NULL), Id(-1), IsCheckedMultipleEntry(false) {
107105
Code = strdup(CodeInit);
108106
BranchVar = BranchVarInit ? strdup(BranchVarInit) : NULL;
109107
}
@@ -273,10 +271,6 @@ void Block::Render(bool InLoop) {
273271
}
274272
}
275273

276-
// Shape
277-
278-
int Shape::IdCounter = 0;
279-
280274
// MultipleShape
281275

282276
void MultipleShape::RenderLoopPrefix() {
@@ -360,7 +354,7 @@ void EmulatedShape::Render(bool InLoop) {
360354

361355
// Relooper
362356

363-
Relooper::Relooper() : Root(NULL), Emulate(false) {
357+
Relooper::Relooper() : Root(NULL), Emulate(false), BlockIdCounter(1), ShapeIdCounter(0) { // block ID 0 is reserved for clearings
364358
}
365359

366360
Relooper::~Relooper() {
@@ -369,6 +363,7 @@ Relooper::~Relooper() {
369363
}
370364

371365
void Relooper::AddBlock(Block *New) {
366+
New->Id = BlockIdCounter++;
372367
Blocks.push_back(New);
373368
}
374369

@@ -473,6 +468,7 @@ void Relooper::Calculate(Block *Entry) {
473468

474469
// Add a shape to the list of shapes in this Relooper calculation
475470
void Notice(Shape *New) {
471+
New->Id = Parent->ShapeIdCounter++;
476472
Parent->Shapes.push_back(New);
477473
}
478474

src/relooper/Relooper.h

+5-9
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ struct Block {
5757
BlockBranchMap ProcessedBranchesOut;
5858
BlockSet ProcessedBranchesIn;
5959
Shape *Parent; // The shape we are directly inside
60-
int Id; // A unique identifier
60+
int Id; // A unique identifier, defined when added to relooper
6161
const char *Code; // The string representation of the code in this block. Owning pointer (we copy the input)
6262
const char *BranchVar; // If we have more than one branch out, the variable whose value determines where we go
6363
bool IsCheckedMultipleEntry; // If true, we are a multiple entry, so reaching us requires setting the label variable
@@ -69,9 +69,6 @@ struct Block {
6969

7070
// Prints out the instructions code and branchings
7171
void Render(bool InLoop);
72-
73-
// INTERNAL
74-
static int IdCounter;
7572
};
7673

7774
// Represents a structured control flow shape, one of
@@ -99,7 +96,7 @@ class LoopShape;
9996
class EmulatedShape;
10097

10198
struct Shape {
102-
int Id; // A unique identifier. Used to identify loops, labels are Lx where x is the Id.
99+
int Id; // A unique identifier. Used to identify loops, labels are Lx where x is the Id. Defined when added to relooper
103100
Shape *Next; // The shape that will appear in the code right after this one
104101
Shape *Natural; // The shape that control flow gets to naturally (if there is Next, then this is Next)
105102

@@ -111,7 +108,7 @@ struct Shape {
111108
};
112109
ShapeType Type;
113110

114-
Shape(ShapeType TypeInit) : Id(Shape::IdCounter++), Next(NULL), Type(TypeInit) {}
111+
Shape(ShapeType TypeInit) : Id(-1), Next(NULL), Type(TypeInit) {}
115112
virtual ~Shape() {}
116113

117114
virtual void Render(bool InLoop) = 0;
@@ -121,9 +118,6 @@ struct Shape {
121118
static LoopShape *IsLoop(Shape *It) { return It && It->Type == Loop ? (LoopShape*)It : NULL; }
122119
static LabeledShape *IsLabeled(Shape *It) { return IsMultiple(It) || IsLoop(It) ? (LabeledShape*)It : NULL; }
123120
static EmulatedShape *IsEmulated(Shape *It) { return It && It->Type == Emulated ? (EmulatedShape*)It : NULL; }
124-
125-
// INTERNAL
126-
static int IdCounter;
127121
};
128122

129123
struct SimpleShape : public Shape {
@@ -191,6 +185,8 @@ struct Relooper {
191185
std::deque<Shape*> Shapes;
192186
Shape *Root;
193187
bool Emulate;
188+
int BlockIdCounter;
189+
int ShapeIdCounter;
194190

195191
Relooper();
196192
~Relooper();

0 commit comments

Comments
 (0)