register_pressure.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/register_pressure.h"
  15. #include <algorithm>
  16. #include <iterator>
  17. #include "source/opt/cfg.h"
  18. #include "source/opt/def_use_manager.h"
  19. #include "source/opt/dominator_tree.h"
  20. #include "source/opt/function.h"
  21. #include "source/opt/ir_context.h"
  22. #include "source/opt/iterator.h"
  23. namespace spvtools {
  24. namespace opt {
  25. namespace {
  26. // Predicate for the FilterIterator to only consider instructions that are not
  27. // phi instructions defined in the basic block |bb|.
  28. class ExcludePhiDefinedInBlock {
  29. public:
  30. ExcludePhiDefinedInBlock(IRContext* context, const BasicBlock* bb)
  31. : context_(context), bb_(bb) {}
  32. bool operator()(Instruction* insn) const {
  33. return !(insn->opcode() == SpvOpPhi &&
  34. context_->get_instr_block(insn) == bb_);
  35. }
  36. private:
  37. IRContext* context_;
  38. const BasicBlock* bb_;
  39. };
  40. // Returns true if |insn| generates a SSA register that is likely to require a
  41. // physical register.
  42. bool CreatesRegisterUsage(Instruction* insn) {
  43. if (!insn->HasResultId()) return false;
  44. if (insn->opcode() == SpvOpUndef) return false;
  45. if (IsConstantInst(insn->opcode())) return false;
  46. if (insn->opcode() == SpvOpLabel) return false;
  47. return true;
  48. }
  49. // Compute the register liveness for each basic block of a function. This also
  50. // fill-up some information about the pick register usage and a break down of
  51. // register usage. This implements: "A non-iterative data-flow algorithm for
  52. // computing liveness sets in strict ssa programs" from Boissinot et al.
  53. class ComputeRegisterLiveness {
  54. public:
  55. ComputeRegisterLiveness(RegisterLiveness* reg_pressure, Function* f)
  56. : reg_pressure_(reg_pressure),
  57. context_(reg_pressure->GetContext()),
  58. function_(f),
  59. cfg_(*reg_pressure->GetContext()->cfg()),
  60. def_use_manager_(*reg_pressure->GetContext()->get_def_use_mgr()),
  61. dom_tree_(
  62. reg_pressure->GetContext()->GetDominatorAnalysis(f)->GetDomTree()),
  63. loop_desc_(*reg_pressure->GetContext()->GetLoopDescriptor(f)) {}
  64. // Computes the register liveness for |function_| and then estimate the
  65. // register usage. The liveness algorithm works in 2 steps:
  66. // - First, compute the liveness for each basic blocks, but will ignore any
  67. // back-edge;
  68. // - Second, walk loop forest to propagate registers crossing back-edges
  69. // (add iterative values into the liveness set).
  70. void Compute() {
  71. cfg_.ForEachBlockInPostOrder(&*function_->begin(), [this](BasicBlock* bb) {
  72. ComputePartialLiveness(bb);
  73. });
  74. DoLoopLivenessUnification();
  75. EvaluateRegisterRequirements();
  76. }
  77. private:
  78. // Registers all SSA register used by successors of |bb| in their phi
  79. // instructions.
  80. void ComputePhiUses(const BasicBlock& bb,
  81. RegisterLiveness::RegionRegisterLiveness::LiveSet* live) {
  82. uint32_t bb_id = bb.id();
  83. bb.ForEachSuccessorLabel([live, bb_id, this](uint32_t sid) {
  84. BasicBlock* succ_bb = cfg_.block(sid);
  85. succ_bb->ForEachPhiInst([live, bb_id, this](const Instruction* phi) {
  86. for (uint32_t i = 0; i < phi->NumInOperands(); i += 2) {
  87. if (phi->GetSingleWordInOperand(i + 1) == bb_id) {
  88. Instruction* insn_op =
  89. def_use_manager_.GetDef(phi->GetSingleWordInOperand(i));
  90. if (CreatesRegisterUsage(insn_op)) {
  91. live->insert(insn_op);
  92. break;
  93. }
  94. }
  95. }
  96. });
  97. });
  98. }
  99. // Computes register liveness for each basic blocks but ignores all
  100. // back-edges.
  101. void ComputePartialLiveness(BasicBlock* bb) {
  102. assert(reg_pressure_->Get(bb) == nullptr &&
  103. "Basic block already processed");
  104. RegisterLiveness::RegionRegisterLiveness* live_inout =
  105. reg_pressure_->GetOrInsert(bb->id());
  106. ComputePhiUses(*bb, &live_inout->live_out_);
  107. const BasicBlock* cbb = bb;
  108. cbb->ForEachSuccessorLabel([&live_inout, bb, this](uint32_t sid) {
  109. // Skip back edges.
  110. if (dom_tree_.Dominates(sid, bb->id())) {
  111. return;
  112. }
  113. BasicBlock* succ_bb = cfg_.block(sid);
  114. RegisterLiveness::RegionRegisterLiveness* succ_live_inout =
  115. reg_pressure_->Get(succ_bb);
  116. assert(succ_live_inout &&
  117. "Successor liveness analysis was not performed");
  118. ExcludePhiDefinedInBlock predicate(context_, succ_bb);
  119. auto filter =
  120. MakeFilterIteratorRange(succ_live_inout->live_in_.begin(),
  121. succ_live_inout->live_in_.end(), predicate);
  122. live_inout->live_out_.insert(filter.begin(), filter.end());
  123. });
  124. live_inout->live_in_ = live_inout->live_out_;
  125. for (Instruction& insn : make_range(bb->rbegin(), bb->rend())) {
  126. if (insn.opcode() == SpvOpPhi) {
  127. live_inout->live_in_.insert(&insn);
  128. break;
  129. }
  130. live_inout->live_in_.erase(&insn);
  131. insn.ForEachInId([live_inout, this](uint32_t* id) {
  132. Instruction* insn_op = def_use_manager_.GetDef(*id);
  133. if (CreatesRegisterUsage(insn_op)) {
  134. live_inout->live_in_.insert(insn_op);
  135. }
  136. });
  137. }
  138. }
  139. // Propagates the register liveness information of each loop iterators.
  140. void DoLoopLivenessUnification() {
  141. for (const Loop* loop : *loop_desc_.GetDummyRootLoop()) {
  142. DoLoopLivenessUnification(*loop);
  143. }
  144. }
  145. // Propagates the register liveness information of loop iterators trough-out
  146. // the loop body.
  147. void DoLoopLivenessUnification(const Loop& loop) {
  148. auto blocks_in_loop = MakeFilterIteratorRange(
  149. loop.GetBlocks().begin(), loop.GetBlocks().end(),
  150. [&loop, this](uint32_t bb_id) {
  151. return bb_id != loop.GetHeaderBlock()->id() &&
  152. loop_desc_[bb_id] == &loop;
  153. });
  154. RegisterLiveness::RegionRegisterLiveness* header_live_inout =
  155. reg_pressure_->Get(loop.GetHeaderBlock());
  156. assert(header_live_inout &&
  157. "Liveness analysis was not performed for the current block");
  158. ExcludePhiDefinedInBlock predicate(context_, loop.GetHeaderBlock());
  159. auto live_loop =
  160. MakeFilterIteratorRange(header_live_inout->live_in_.begin(),
  161. header_live_inout->live_in_.end(), predicate);
  162. for (uint32_t bb_id : blocks_in_loop) {
  163. BasicBlock* bb = cfg_.block(bb_id);
  164. RegisterLiveness::RegionRegisterLiveness* live_inout =
  165. reg_pressure_->Get(bb);
  166. live_inout->live_in_.insert(live_loop.begin(), live_loop.end());
  167. live_inout->live_out_.insert(live_loop.begin(), live_loop.end());
  168. }
  169. for (const Loop* inner_loop : loop) {
  170. RegisterLiveness::RegionRegisterLiveness* live_inout =
  171. reg_pressure_->Get(inner_loop->GetHeaderBlock());
  172. live_inout->live_in_.insert(live_loop.begin(), live_loop.end());
  173. live_inout->live_out_.insert(live_loop.begin(), live_loop.end());
  174. DoLoopLivenessUnification(*inner_loop);
  175. }
  176. }
  177. // Get the number of required registers for this each basic block.
  178. void EvaluateRegisterRequirements() {
  179. for (BasicBlock& bb : *function_) {
  180. RegisterLiveness::RegionRegisterLiveness* live_inout =
  181. reg_pressure_->Get(bb.id());
  182. assert(live_inout != nullptr && "Basic block not processed");
  183. size_t reg_count = live_inout->live_out_.size();
  184. for (Instruction* insn : live_inout->live_out_) {
  185. live_inout->AddRegisterClass(insn);
  186. }
  187. live_inout->used_registers_ = reg_count;
  188. std::unordered_set<uint32_t> die_in_block;
  189. for (Instruction& insn : make_range(bb.rbegin(), bb.rend())) {
  190. // If it is a phi instruction, the register pressure will not change
  191. // anymore.
  192. if (insn.opcode() == SpvOpPhi) {
  193. break;
  194. }
  195. insn.ForEachInId(
  196. [live_inout, &die_in_block, &reg_count, this](uint32_t* id) {
  197. Instruction* op_insn = def_use_manager_.GetDef(*id);
  198. if (!CreatesRegisterUsage(op_insn) ||
  199. live_inout->live_out_.count(op_insn)) {
  200. // already taken into account.
  201. return;
  202. }
  203. if (!die_in_block.count(*id)) {
  204. live_inout->AddRegisterClass(def_use_manager_.GetDef(*id));
  205. reg_count++;
  206. die_in_block.insert(*id);
  207. }
  208. });
  209. live_inout->used_registers_ =
  210. std::max(live_inout->used_registers_, reg_count);
  211. if (CreatesRegisterUsage(&insn)) {
  212. reg_count--;
  213. }
  214. }
  215. }
  216. }
  217. RegisterLiveness* reg_pressure_;
  218. IRContext* context_;
  219. Function* function_;
  220. CFG& cfg_;
  221. analysis::DefUseManager& def_use_manager_;
  222. DominatorTree& dom_tree_;
  223. LoopDescriptor& loop_desc_;
  224. };
  225. } // namespace
  226. // Get the number of required registers for each basic block.
  227. void RegisterLiveness::RegionRegisterLiveness::AddRegisterClass(
  228. Instruction* insn) {
  229. assert(CreatesRegisterUsage(insn) && "Instruction does not use a register");
  230. analysis::Type* type =
  231. insn->context()->get_type_mgr()->GetType(insn->type_id());
  232. RegisterLiveness::RegisterClass reg_class{type, false};
  233. insn->context()->get_decoration_mgr()->WhileEachDecoration(
  234. insn->result_id(), SpvDecorationUniform,
  235. [&reg_class](const Instruction&) {
  236. reg_class.is_uniform_ = true;
  237. return false;
  238. });
  239. AddRegisterClass(reg_class);
  240. }
  241. void RegisterLiveness::Analyze(Function* f) {
  242. block_pressure_.clear();
  243. ComputeRegisterLiveness(this, f).Compute();
  244. }
  245. void RegisterLiveness::ComputeLoopRegisterPressure(
  246. const Loop& loop, RegionRegisterLiveness* loop_reg_pressure) const {
  247. loop_reg_pressure->Clear();
  248. const RegionRegisterLiveness* header_live_inout = Get(loop.GetHeaderBlock());
  249. loop_reg_pressure->live_in_ = header_live_inout->live_in_;
  250. std::unordered_set<uint32_t> exit_blocks;
  251. loop.GetExitBlocks(&exit_blocks);
  252. for (uint32_t bb_id : exit_blocks) {
  253. const RegionRegisterLiveness* live_inout = Get(bb_id);
  254. loop_reg_pressure->live_out_.insert(live_inout->live_in_.begin(),
  255. live_inout->live_in_.end());
  256. }
  257. std::unordered_set<uint32_t> seen_insn;
  258. for (Instruction* insn : loop_reg_pressure->live_out_) {
  259. loop_reg_pressure->AddRegisterClass(insn);
  260. seen_insn.insert(insn->result_id());
  261. }
  262. for (Instruction* insn : loop_reg_pressure->live_in_) {
  263. if (!seen_insn.count(insn->result_id())) {
  264. continue;
  265. }
  266. loop_reg_pressure->AddRegisterClass(insn);
  267. seen_insn.insert(insn->result_id());
  268. }
  269. loop_reg_pressure->used_registers_ = 0;
  270. for (uint32_t bb_id : loop.GetBlocks()) {
  271. BasicBlock* bb = context_->cfg()->block(bb_id);
  272. const RegionRegisterLiveness* live_inout = Get(bb_id);
  273. assert(live_inout != nullptr && "Basic block not processed");
  274. loop_reg_pressure->used_registers_ = std::max(
  275. loop_reg_pressure->used_registers_, live_inout->used_registers_);
  276. for (Instruction& insn : *bb) {
  277. if (insn.opcode() == SpvOpPhi || !CreatesRegisterUsage(&insn) ||
  278. seen_insn.count(insn.result_id())) {
  279. continue;
  280. }
  281. loop_reg_pressure->AddRegisterClass(&insn);
  282. }
  283. }
  284. }
  285. void RegisterLiveness::SimulateFusion(
  286. const Loop& l1, const Loop& l2, RegionRegisterLiveness* sim_result) const {
  287. sim_result->Clear();
  288. // Compute the live-in state:
  289. // sim_result.live_in = l1.live_in U l2.live_in
  290. // This assumes that |l1| does not generated register that is live-out for
  291. // |l1|.
  292. const RegionRegisterLiveness* l1_header_live_inout = Get(l1.GetHeaderBlock());
  293. sim_result->live_in_ = l1_header_live_inout->live_in_;
  294. const RegionRegisterLiveness* l2_header_live_inout = Get(l2.GetHeaderBlock());
  295. sim_result->live_in_.insert(l2_header_live_inout->live_in_.begin(),
  296. l2_header_live_inout->live_in_.end());
  297. // The live-out set of the fused loop is the l2 live-out set.
  298. std::unordered_set<uint32_t> exit_blocks;
  299. l2.GetExitBlocks(&exit_blocks);
  300. for (uint32_t bb_id : exit_blocks) {
  301. const RegionRegisterLiveness* live_inout = Get(bb_id);
  302. sim_result->live_out_.insert(live_inout->live_in_.begin(),
  303. live_inout->live_in_.end());
  304. }
  305. // Compute the register usage information.
  306. std::unordered_set<uint32_t> seen_insn;
  307. for (Instruction* insn : sim_result->live_out_) {
  308. sim_result->AddRegisterClass(insn);
  309. seen_insn.insert(insn->result_id());
  310. }
  311. for (Instruction* insn : sim_result->live_in_) {
  312. if (!seen_insn.count(insn->result_id())) {
  313. continue;
  314. }
  315. sim_result->AddRegisterClass(insn);
  316. seen_insn.insert(insn->result_id());
  317. }
  318. sim_result->used_registers_ = 0;
  319. // The loop fusion is injecting the l1 before the l2, the latch of l1 will be
  320. // connected to the header of l2.
  321. // To compute the register usage, we inject the loop live-in (union of l1 and
  322. // l2 live-in header blocks) into the the live in/out of each basic block of
  323. // l1 to get the peak register usage. We then repeat the operation to for l2
  324. // basic blocks but in this case we inject the live-out of the latch of l1.
  325. auto live_loop = MakeFilterIteratorRange(
  326. sim_result->live_in_.begin(), sim_result->live_in_.end(),
  327. [&l1, &l2](Instruction* insn) {
  328. BasicBlock* bb = insn->context()->get_instr_block(insn);
  329. return insn->HasResultId() &&
  330. !(insn->opcode() == SpvOpPhi &&
  331. (bb == l1.GetHeaderBlock() || bb == l2.GetHeaderBlock()));
  332. });
  333. for (uint32_t bb_id : l1.GetBlocks()) {
  334. BasicBlock* bb = context_->cfg()->block(bb_id);
  335. const RegionRegisterLiveness* live_inout_info = Get(bb_id);
  336. assert(live_inout_info != nullptr && "Basic block not processed");
  337. RegionRegisterLiveness::LiveSet live_out = live_inout_info->live_out_;
  338. live_out.insert(live_loop.begin(), live_loop.end());
  339. sim_result->used_registers_ =
  340. std::max(sim_result->used_registers_,
  341. live_inout_info->used_registers_ + live_out.size() -
  342. live_inout_info->live_out_.size());
  343. for (Instruction& insn : *bb) {
  344. if (insn.opcode() == SpvOpPhi || !CreatesRegisterUsage(&insn) ||
  345. seen_insn.count(insn.result_id())) {
  346. continue;
  347. }
  348. sim_result->AddRegisterClass(&insn);
  349. }
  350. }
  351. const RegionRegisterLiveness* l1_latch_live_inout_info =
  352. Get(l1.GetLatchBlock()->id());
  353. assert(l1_latch_live_inout_info != nullptr && "Basic block not processed");
  354. RegionRegisterLiveness::LiveSet l1_latch_live_out =
  355. l1_latch_live_inout_info->live_out_;
  356. l1_latch_live_out.insert(live_loop.begin(), live_loop.end());
  357. auto live_loop_l2 =
  358. make_range(l1_latch_live_out.begin(), l1_latch_live_out.end());
  359. for (uint32_t bb_id : l2.GetBlocks()) {
  360. BasicBlock* bb = context_->cfg()->block(bb_id);
  361. const RegionRegisterLiveness* live_inout_info = Get(bb_id);
  362. assert(live_inout_info != nullptr && "Basic block not processed");
  363. RegionRegisterLiveness::LiveSet live_out = live_inout_info->live_out_;
  364. live_out.insert(live_loop_l2.begin(), live_loop_l2.end());
  365. sim_result->used_registers_ =
  366. std::max(sim_result->used_registers_,
  367. live_inout_info->used_registers_ + live_out.size() -
  368. live_inout_info->live_out_.size());
  369. for (Instruction& insn : *bb) {
  370. if (insn.opcode() == SpvOpPhi || !CreatesRegisterUsage(&insn) ||
  371. seen_insn.count(insn.result_id())) {
  372. continue;
  373. }
  374. sim_result->AddRegisterClass(&insn);
  375. }
  376. }
  377. }
  378. void RegisterLiveness::SimulateFission(
  379. const Loop& loop, const std::unordered_set<Instruction*>& moved_inst,
  380. const std::unordered_set<Instruction*>& copied_inst,
  381. RegionRegisterLiveness* l1_sim_result,
  382. RegionRegisterLiveness* l2_sim_result) const {
  383. l1_sim_result->Clear();
  384. l2_sim_result->Clear();
  385. // Filter predicates: consider instructions that only belong to the first and
  386. // second loop.
  387. auto belong_to_loop1 = [&moved_inst, &copied_inst, &loop](Instruction* insn) {
  388. return moved_inst.count(insn) || copied_inst.count(insn) ||
  389. !loop.IsInsideLoop(insn);
  390. };
  391. auto belong_to_loop2 = [&moved_inst](Instruction* insn) {
  392. return !moved_inst.count(insn);
  393. };
  394. const RegionRegisterLiveness* header_live_inout = Get(loop.GetHeaderBlock());
  395. // l1 live-in
  396. {
  397. auto live_loop = MakeFilterIteratorRange(
  398. header_live_inout->live_in_.begin(), header_live_inout->live_in_.end(),
  399. belong_to_loop1);
  400. l1_sim_result->live_in_.insert(live_loop.begin(), live_loop.end());
  401. }
  402. // l2 live-in
  403. {
  404. auto live_loop = MakeFilterIteratorRange(
  405. header_live_inout->live_in_.begin(), header_live_inout->live_in_.end(),
  406. belong_to_loop2);
  407. l2_sim_result->live_in_.insert(live_loop.begin(), live_loop.end());
  408. }
  409. std::unordered_set<uint32_t> exit_blocks;
  410. loop.GetExitBlocks(&exit_blocks);
  411. // l2 live-out.
  412. for (uint32_t bb_id : exit_blocks) {
  413. const RegionRegisterLiveness* live_inout = Get(bb_id);
  414. l2_sim_result->live_out_.insert(live_inout->live_in_.begin(),
  415. live_inout->live_in_.end());
  416. }
  417. // l1 live-out.
  418. {
  419. auto live_out = MakeFilterIteratorRange(l2_sim_result->live_out_.begin(),
  420. l2_sim_result->live_out_.end(),
  421. belong_to_loop1);
  422. l1_sim_result->live_out_.insert(live_out.begin(), live_out.end());
  423. }
  424. {
  425. auto live_out =
  426. MakeFilterIteratorRange(l2_sim_result->live_in_.begin(),
  427. l2_sim_result->live_in_.end(), belong_to_loop1);
  428. l1_sim_result->live_out_.insert(live_out.begin(), live_out.end());
  429. }
  430. // Lives out of l1 are live out of l2 so are live in of l2 as well.
  431. l2_sim_result->live_in_.insert(l1_sim_result->live_out_.begin(),
  432. l1_sim_result->live_out_.end());
  433. for (Instruction* insn : l1_sim_result->live_in_) {
  434. l1_sim_result->AddRegisterClass(insn);
  435. }
  436. for (Instruction* insn : l2_sim_result->live_in_) {
  437. l2_sim_result->AddRegisterClass(insn);
  438. }
  439. l1_sim_result->used_registers_ = 0;
  440. l2_sim_result->used_registers_ = 0;
  441. for (uint32_t bb_id : loop.GetBlocks()) {
  442. BasicBlock* bb = context_->cfg()->block(bb_id);
  443. const RegisterLiveness::RegionRegisterLiveness* live_inout = Get(bb_id);
  444. assert(live_inout != nullptr && "Basic block not processed");
  445. auto l1_block_live_out =
  446. MakeFilterIteratorRange(live_inout->live_out_.begin(),
  447. live_inout->live_out_.end(), belong_to_loop1);
  448. auto l2_block_live_out =
  449. MakeFilterIteratorRange(live_inout->live_out_.begin(),
  450. live_inout->live_out_.end(), belong_to_loop2);
  451. size_t l1_reg_count =
  452. std::distance(l1_block_live_out.begin(), l1_block_live_out.end());
  453. size_t l2_reg_count =
  454. std::distance(l2_block_live_out.begin(), l2_block_live_out.end());
  455. std::unordered_set<uint32_t> die_in_block;
  456. for (Instruction& insn : make_range(bb->rbegin(), bb->rend())) {
  457. if (insn.opcode() == SpvOpPhi) {
  458. break;
  459. }
  460. bool does_belong_to_loop1 = belong_to_loop1(&insn);
  461. bool does_belong_to_loop2 = belong_to_loop2(&insn);
  462. insn.ForEachInId([live_inout, &die_in_block, &l1_reg_count, &l2_reg_count,
  463. does_belong_to_loop1, does_belong_to_loop2,
  464. this](uint32_t* id) {
  465. Instruction* op_insn = context_->get_def_use_mgr()->GetDef(*id);
  466. if (!CreatesRegisterUsage(op_insn) ||
  467. live_inout->live_out_.count(op_insn)) {
  468. // already taken into account.
  469. return;
  470. }
  471. if (!die_in_block.count(*id)) {
  472. if (does_belong_to_loop1) {
  473. l1_reg_count++;
  474. }
  475. if (does_belong_to_loop2) {
  476. l2_reg_count++;
  477. }
  478. die_in_block.insert(*id);
  479. }
  480. });
  481. l1_sim_result->used_registers_ =
  482. std::max(l1_sim_result->used_registers_, l1_reg_count);
  483. l2_sim_result->used_registers_ =
  484. std::max(l2_sim_result->used_registers_, l2_reg_count);
  485. if (CreatesRegisterUsage(&insn)) {
  486. if (does_belong_to_loop1) {
  487. if (!l1_sim_result->live_in_.count(&insn)) {
  488. l1_sim_result->AddRegisterClass(&insn);
  489. }
  490. l1_reg_count--;
  491. }
  492. if (does_belong_to_loop2) {
  493. if (!l2_sim_result->live_in_.count(&insn)) {
  494. l2_sim_result->AddRegisterClass(&insn);
  495. }
  496. l2_reg_count--;
  497. }
  498. }
  499. }
  500. }
  501. }
  502. } // namespace opt
  503. } // namespace spvtools