Skip to content

Commit 82bf28e

Browse files
alexmalyshevfacebook-github-bot
authored andcommitted
Minor refactors to lir::Function
Summary: Mostly just consolidating code in the implementation file, keeping method order the same across the .h and the .cpp, and adding comments. Reviewed By: yoney Differential Revision: D81698609 fbshipit-source-id: 0456fe67a22499f9f83a894168395f62cfd58628
1 parent fd3bd38 commit 82bf28e

2 files changed

Lines changed: 41 additions & 26 deletions

File tree

cinderx/Jit/lir/function.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77

88
namespace jit::lir {
99

10+
namespace {
11+
1012
// Helper for copyOperand.
11-
static void copyIndirect(
13+
void copyIndirect(
1214
UnorderedMap<LinkedOperand*, int>& instr_refs,
1315
Operand* dest_op,
1416
MemoryIndirect* source_op) {
@@ -58,7 +60,7 @@ static void copyIndirect(
5860

5961
// Helper for copyOperandBase.
6062
// Assume that type and data type are already be set.
61-
static void copyOperand(
63+
void copyOperand(
6264
UnorderedMap<int, BasicBlock*>& block_index_map,
6365
UnorderedMap<LinkedOperand*, int>& instr_refs,
6466
Operand* operand,
@@ -97,7 +99,7 @@ static void copyOperand(
9799
}
98100

99101
// Helper for deepCopyBasicBlocks.
100-
static void copyInput(
102+
void copyInput(
101103
UnorderedMap<int, BasicBlock*>& block_index_map,
102104
UnorderedMap<LinkedOperand*, int>& instr_refs,
103105
OperandBase* input,
@@ -117,7 +119,7 @@ static void copyInput(
117119
}
118120

119121
// Helper for deepCopyBasicBlocks.
120-
static void connectLinkedOperands(
122+
void connectLinkedOperands(
121123
UnorderedMap<int, Instruction*>& output_index_map_,
122124
UnorderedMap<LinkedOperand*, int>& instr_refs_) {
123125
for (auto& [operand, instr_index] : instr_refs_) {
@@ -129,7 +131,7 @@ static void connectLinkedOperands(
129131
// Helper used in copyFrom.
130132
// Expects blocks to be initialized into block_index_map_.
131133
// Copies the instructions and successors from src_blocks.
132-
static void deepCopyBasicBlocks(
134+
void deepCopyBasicBlocks(
133135
const std::vector<BasicBlock*>& src_blocks,
134136
UnorderedMap<int, BasicBlock*>& block_index_map_,
135137
const hir::Instr* origin) {
@@ -163,6 +165,16 @@ static void deepCopyBasicBlocks(
163165
connectLinkedOperands(output_index_map, instr_refs);
164166
}
165167

168+
} // namespace
169+
170+
int Function::allocateId() {
171+
return next_id_++;
172+
}
173+
174+
void Function::setNextId(int id) {
175+
next_id_ = id;
176+
}
177+
166178
Function::CopyResult Function::copyFrom(
167179
const Function* src_func,
168180
BasicBlock* prev_bb,
@@ -216,13 +228,25 @@ BasicBlock* Function::allocateBasicBlockAfter(BasicBlock* block) {
216228
return new_block;
217229
}
218230

231+
const std::vector<BasicBlock*>& Function::basicblocks() const {
232+
return basic_blocks_;
233+
}
234+
235+
std::vector<BasicBlock*>& Function::basicblocks() {
236+
return basic_blocks_;
237+
}
238+
219239
BasicBlock* Function::entryBlock() const {
220240
if (basic_blocks_.empty()) {
221241
return nullptr;
222242
}
223243
return basic_blocks_.front();
224244
}
225245

246+
size_t Function::getNumBasicBlocks() const {
247+
return basic_blocks_.size();
248+
}
249+
226250
void Function::sortBasicBlocks() {
227251
BasicBlockSorter sorter(basic_blocks_);
228252
basic_blocks_ = sorter.getSortedBlocks();

cinderx/Jit/lir/function.h

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
#include "cinderx/Jit/lir/block.h"
66

77
#include <deque>
8-
#include <memory>
98
#include <vector>
109

1110
namespace jit::lir {
1211

1312
class Function {
1413
public:
15-
int allocateId() {
16-
return next_id_++;
17-
}
18-
1914
struct CopyResult {
2015
int begin_bb;
2116
int end_bb;
2217
};
2318

19+
// Allocate a new ID for a basic block or an instruction.
20+
int allocateId();
21+
22+
// Set the next ID to return from allocateId(). Only meant to be used by the
23+
// LIR parser.
24+
void setNextId(int id);
25+
2426
// Deep copy function into dest_func.
2527
// Insert the blocks between prev_bb and next_bb.
2628
// Assumes that prev_bb and next_bb appear consecutively
@@ -34,25 +36,21 @@ class Function {
3436
BasicBlock* next_bb,
3537
const hir::Instr* origin);
3638

39+
// Create a new block and insert it as the last block in the CFG.
3740
BasicBlock* allocateBasicBlock();
3841

42+
// Create a new block and insert it in a given spot in the CFG.
3943
BasicBlock* allocateBasicBlockAfter(BasicBlock* block);
4044

4145
// Returns the list of all the basic blocks.
4246
// The basic blocks will be in RPO as long as the CFG has not been
4347
// modified since the last call to SortRPO().
44-
const std::vector<BasicBlock*>& basicblocks() const {
45-
return basic_blocks_;
46-
}
47-
std::vector<BasicBlock*>& basicblocks() {
48-
return basic_blocks_;
49-
}
48+
const std::vector<BasicBlock*>& basicblocks() const;
49+
std::vector<BasicBlock*>& basicblocks();
5050

5151
BasicBlock* entryBlock() const;
5252

53-
size_t getNumBasicBlocks() const {
54-
return basic_blocks_.size();
55-
}
53+
size_t getNumBasicBlocks() const;
5654

5755
void sortBasicBlocks();
5856

@@ -80,13 +78,6 @@ class Function {
8078

8179
// The next id to assign to a BasicBlock or Instruction.
8280
int next_id_{0};
83-
84-
// used in parser
85-
void setNextId(int id) {
86-
next_id_ = id;
87-
}
88-
89-
friend class Parser;
9081
};
9182

9283
} // namespace jit::lir

0 commit comments

Comments
 (0)