loop_utils.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 <algorithm>
  15. #include <memory>
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #include <utility>
  19. #include <vector>
  20. #include "source/cfa.h"
  21. #include "source/opt/cfg.h"
  22. #include "source/opt/ir_builder.h"
  23. #include "source/opt/ir_context.h"
  24. #include "source/opt/loop_descriptor.h"
  25. #include "source/opt/loop_utils.h"
  26. namespace spvtools {
  27. namespace opt {
  28. namespace {
  29. // Return true if |bb| is dominated by at least one block in |exits|
  30. inline bool DominatesAnExit(BasicBlock* bb,
  31. const std::unordered_set<BasicBlock*>& exits,
  32. const DominatorTree& dom_tree) {
  33. for (BasicBlock* e_bb : exits)
  34. if (dom_tree.Dominates(bb, e_bb)) return true;
  35. return false;
  36. }
  37. // Utility class to rewrite out-of-loop uses of an in-loop definition in terms
  38. // of phi instructions to achieve a LCSSA form.
  39. // For a given definition, the class user registers phi instructions using that
  40. // definition in all loop exit blocks by which the definition escapes.
  41. // Then, when rewriting a use of the definition, the rewriter walks the
  42. // paths from the use the loop exits. At each step, it will insert a phi
  43. // instruction to merge the incoming value according to exit blocks definition.
  44. class LCSSARewriter {
  45. public:
  46. LCSSARewriter(IRContext* context, const DominatorTree& dom_tree,
  47. const std::unordered_set<BasicBlock*>& exit_bb,
  48. BasicBlock* merge_block)
  49. : context_(context),
  50. cfg_(context_->cfg()),
  51. dom_tree_(dom_tree),
  52. exit_bb_(exit_bb),
  53. merge_block_id_(merge_block ? merge_block->id() : 0) {}
  54. struct UseRewriter {
  55. explicit UseRewriter(LCSSARewriter* base, const Instruction& def_insn)
  56. : base_(base), def_insn_(def_insn) {}
  57. // Rewrites the use of |def_insn_| by the instruction |user| at the index
  58. // |operand_index| in terms of phi instruction. This recursively builds new
  59. // phi instructions from |user| to the loop exit blocks' phis. The use of
  60. // |def_insn_| in |user| is replaced by the relevant phi instruction at the
  61. // end of the operation.
  62. // It is assumed that |user| does not dominates any of the loop exit basic
  63. // block. This operation does not update the def/use manager, instead it
  64. // records what needs to be updated. The actual update is performed by
  65. // UpdateManagers.
  66. void RewriteUse(BasicBlock* bb, Instruction* user, uint32_t operand_index) {
  67. assert(
  68. (user->opcode() != spv::Op::OpPhi || bb != GetParent(user)) &&
  69. "The root basic block must be the incoming edge if |user| is a phi "
  70. "instruction");
  71. assert((user->opcode() == spv::Op::OpPhi || bb == GetParent(user)) &&
  72. "The root basic block must be the instruction parent if |user| is "
  73. "not "
  74. "phi instruction");
  75. Instruction* new_def = GetOrBuildIncoming(bb->id());
  76. user->SetOperand(operand_index, {new_def->result_id()});
  77. rewritten_.insert(user);
  78. }
  79. // In-place update of some managers (avoid full invalidation).
  80. inline void UpdateManagers() {
  81. analysis::DefUseManager* def_use_mgr = base_->context_->get_def_use_mgr();
  82. // Register all new definitions.
  83. for (Instruction* insn : rewritten_) {
  84. def_use_mgr->AnalyzeInstDef(insn);
  85. }
  86. // Register all new uses.
  87. for (Instruction* insn : rewritten_) {
  88. def_use_mgr->AnalyzeInstUse(insn);
  89. }
  90. }
  91. private:
  92. // Return the basic block that |instr| belongs to.
  93. BasicBlock* GetParent(Instruction* instr) {
  94. return base_->context_->get_instr_block(instr);
  95. }
  96. // Builds a phi instruction for the basic block |bb|. The function assumes
  97. // that |defining_blocks| contains the list of basic block that define the
  98. // usable value for each predecessor of |bb|.
  99. inline Instruction* CreatePhiInstruction(
  100. BasicBlock* bb, const std::vector<uint32_t>& defining_blocks) {
  101. std::vector<uint32_t> incomings;
  102. const std::vector<uint32_t>& bb_preds = base_->cfg_->preds(bb->id());
  103. assert(bb_preds.size() == defining_blocks.size());
  104. for (size_t i = 0; i < bb_preds.size(); i++) {
  105. incomings.push_back(
  106. GetOrBuildIncoming(defining_blocks[i])->result_id());
  107. incomings.push_back(bb_preds[i]);
  108. }
  109. InstructionBuilder builder(base_->context_, &*bb->begin(),
  110. IRContext::kAnalysisInstrToBlockMapping);
  111. Instruction* incoming_phi =
  112. builder.AddPhi(def_insn_.type_id(), incomings);
  113. rewritten_.insert(incoming_phi);
  114. return incoming_phi;
  115. }
  116. // Builds a phi instruction for the basic block |bb|, all incoming values
  117. // will be |value|.
  118. inline Instruction* CreatePhiInstruction(BasicBlock* bb,
  119. const Instruction& value) {
  120. std::vector<uint32_t> incomings;
  121. const std::vector<uint32_t>& bb_preds = base_->cfg_->preds(bb->id());
  122. for (size_t i = 0; i < bb_preds.size(); i++) {
  123. incomings.push_back(value.result_id());
  124. incomings.push_back(bb_preds[i]);
  125. }
  126. InstructionBuilder builder(base_->context_, &*bb->begin(),
  127. IRContext::kAnalysisInstrToBlockMapping);
  128. Instruction* incoming_phi =
  129. builder.AddPhi(def_insn_.type_id(), incomings);
  130. rewritten_.insert(incoming_phi);
  131. return incoming_phi;
  132. }
  133. // Return the new def to use for the basic block |bb_id|.
  134. // If |bb_id| does not have a suitable def to use then we:
  135. // - return the common def used by all predecessors;
  136. // - if there is no common def, then we build a new phi instr at the
  137. // beginning of |bb_id| and return this new instruction.
  138. Instruction* GetOrBuildIncoming(uint32_t bb_id) {
  139. assert(base_->cfg_->block(bb_id) != nullptr && "Unknown basic block");
  140. Instruction*& incoming_phi = bb_to_phi_[bb_id];
  141. if (incoming_phi) {
  142. return incoming_phi;
  143. }
  144. BasicBlock* bb = &*base_->cfg_->block(bb_id);
  145. // If this is an exit basic block, look if there already is an eligible
  146. // phi instruction. An eligible phi has |def_insn_| as all incoming
  147. // values.
  148. if (base_->exit_bb_.count(bb)) {
  149. // Look if there is an eligible phi in this block.
  150. if (!bb->WhileEachPhiInst([&incoming_phi, this](Instruction* phi) {
  151. for (uint32_t i = 0; i < phi->NumInOperands(); i += 2) {
  152. if (phi->GetSingleWordInOperand(i) != def_insn_.result_id())
  153. return true;
  154. }
  155. incoming_phi = phi;
  156. rewritten_.insert(incoming_phi);
  157. return false;
  158. })) {
  159. return incoming_phi;
  160. }
  161. incoming_phi = CreatePhiInstruction(bb, def_insn_);
  162. return incoming_phi;
  163. }
  164. // Get the block that defines the value to use for each predecessor.
  165. // If the vector has 1 value, then it means that this block does not need
  166. // to build a phi instruction unless |bb_id| is the loop merge block.
  167. const std::vector<uint32_t>& defining_blocks =
  168. base_->GetDefiningBlocks(bb_id);
  169. // Special case for structured loops: merge block might be different from
  170. // the exit block set. To maintain structured properties it will ease
  171. // transformations if the merge block also holds a phi instruction like
  172. // the exit ones.
  173. if (defining_blocks.size() > 1 || bb_id == base_->merge_block_id_) {
  174. if (defining_blocks.size() > 1) {
  175. incoming_phi = CreatePhiInstruction(bb, defining_blocks);
  176. } else {
  177. assert(bb_id == base_->merge_block_id_);
  178. incoming_phi =
  179. CreatePhiInstruction(bb, *GetOrBuildIncoming(defining_blocks[0]));
  180. }
  181. } else {
  182. incoming_phi = GetOrBuildIncoming(defining_blocks[0]);
  183. }
  184. return incoming_phi;
  185. }
  186. LCSSARewriter* base_;
  187. const Instruction& def_insn_;
  188. std::unordered_map<uint32_t, Instruction*> bb_to_phi_;
  189. std::unordered_set<Instruction*> rewritten_;
  190. };
  191. private:
  192. // Return the new def to use for the basic block |bb_id|.
  193. // If |bb_id| does not have a suitable def to use then we:
  194. // - return the common def used by all predecessors;
  195. // - if there is no common def, then we build a new phi instr at the
  196. // beginning of |bb_id| and return this new instruction.
  197. const std::vector<uint32_t>& GetDefiningBlocks(uint32_t bb_id) {
  198. assert(cfg_->block(bb_id) != nullptr && "Unknown basic block");
  199. std::vector<uint32_t>& defining_blocks = bb_to_defining_blocks_[bb_id];
  200. if (defining_blocks.size()) return defining_blocks;
  201. // Check if one of the loop exit basic block dominates |bb_id|.
  202. for (const BasicBlock* e_bb : exit_bb_) {
  203. if (dom_tree_.Dominates(e_bb->id(), bb_id)) {
  204. defining_blocks.push_back(e_bb->id());
  205. return defining_blocks;
  206. }
  207. }
  208. // Process parents, they will returns their suitable blocks.
  209. // If they are all the same, this means this basic block is dominated by a
  210. // common block, so we won't need to build a phi instruction.
  211. for (uint32_t pred_id : cfg_->preds(bb_id)) {
  212. const std::vector<uint32_t>& pred_blocks = GetDefiningBlocks(pred_id);
  213. if (pred_blocks.size() == 1)
  214. defining_blocks.push_back(pred_blocks[0]);
  215. else
  216. defining_blocks.push_back(pred_id);
  217. }
  218. assert(defining_blocks.size());
  219. if (std::all_of(defining_blocks.begin(), defining_blocks.end(),
  220. [&defining_blocks](uint32_t id) {
  221. return id == defining_blocks[0];
  222. })) {
  223. // No need for a phi.
  224. defining_blocks.resize(1);
  225. }
  226. return defining_blocks;
  227. }
  228. IRContext* context_;
  229. CFG* cfg_;
  230. const DominatorTree& dom_tree_;
  231. const std::unordered_set<BasicBlock*>& exit_bb_;
  232. uint32_t merge_block_id_;
  233. // This map represent the set of known paths. For each key, the vector
  234. // represent the set of blocks holding the definition to be used to build the
  235. // phi instruction.
  236. // If the vector has 0 value, then the path is unknown yet, and must be built.
  237. // If the vector has 1 value, then the value defined by that basic block
  238. // should be used.
  239. // If the vector has more than 1 value, then a phi node must be created, the
  240. // basic block ordering is the same as the predecessor ordering.
  241. std::unordered_map<uint32_t, std::vector<uint32_t>> bb_to_defining_blocks_;
  242. };
  243. // Make the set |blocks| closed SSA. The set is closed SSA if all the uses
  244. // outside the set are phi instructions in exiting basic block set (hold by
  245. // |lcssa_rewriter|).
  246. inline void MakeSetClosedSSA(IRContext* context, Function* function,
  247. const std::unordered_set<uint32_t>& blocks,
  248. const std::unordered_set<BasicBlock*>& exit_bb,
  249. LCSSARewriter* lcssa_rewriter) {
  250. CFG& cfg = *context->cfg();
  251. DominatorTree& dom_tree =
  252. context->GetDominatorAnalysis(function)->GetDomTree();
  253. analysis::DefUseManager* def_use_manager = context->get_def_use_mgr();
  254. for (uint32_t bb_id : blocks) {
  255. BasicBlock* bb = cfg.block(bb_id);
  256. // If bb does not dominate an exit block, then it cannot have escaping defs.
  257. if (!DominatesAnExit(bb, exit_bb, dom_tree)) continue;
  258. for (Instruction& inst : *bb) {
  259. LCSSARewriter::UseRewriter rewriter(lcssa_rewriter, inst);
  260. def_use_manager->ForEachUse(
  261. &inst, [&blocks, &rewriter, &exit_bb, context](
  262. Instruction* use, uint32_t operand_index) {
  263. BasicBlock* use_parent = context->get_instr_block(use);
  264. assert(use_parent);
  265. if (blocks.count(use_parent->id())) return;
  266. if (use->opcode() == spv::Op::OpPhi) {
  267. // If the use is a Phi instruction and the incoming block is
  268. // coming from the loop, then that's consistent with LCSSA form.
  269. if (exit_bb.count(use_parent)) {
  270. return;
  271. } else {
  272. // That's not an exit block, but the user is a phi instruction.
  273. // Consider the incoming branch only.
  274. use_parent = context->get_instr_block(
  275. use->GetSingleWordOperand(operand_index + 1));
  276. }
  277. }
  278. // Rewrite the use. Note that this call does not invalidate the
  279. // def/use manager. So this operation is safe.
  280. rewriter.RewriteUse(use_parent, use, operand_index);
  281. });
  282. rewriter.UpdateManagers();
  283. }
  284. }
  285. }
  286. } // namespace
  287. void LoopUtils::CreateLoopDedicatedExits() {
  288. Function* function = loop_->GetHeaderBlock()->GetParent();
  289. LoopDescriptor& loop_desc = *context_->GetLoopDescriptor(function);
  290. CFG& cfg = *context_->cfg();
  291. analysis::DefUseManager* def_use_mgr = context_->get_def_use_mgr();
  292. const IRContext::Analysis PreservedAnalyses =
  293. IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping;
  294. // Gathers the set of basic block that are not in this loop and have at least
  295. // one predecessor in the loop and one not in the loop.
  296. std::unordered_set<uint32_t> exit_bb_set;
  297. loop_->GetExitBlocks(&exit_bb_set);
  298. std::unordered_set<BasicBlock*> new_loop_exits;
  299. bool made_change = false;
  300. // For each block, we create a new one that gathers all branches from
  301. // the loop and fall into the block.
  302. for (uint32_t non_dedicate_id : exit_bb_set) {
  303. BasicBlock* non_dedicate = cfg.block(non_dedicate_id);
  304. const std::vector<uint32_t>& bb_pred = cfg.preds(non_dedicate_id);
  305. // Ignore the block if all the predecessors are in the loop.
  306. if (std::all_of(bb_pred.begin(), bb_pred.end(),
  307. [this](uint32_t id) { return loop_->IsInsideLoop(id); })) {
  308. new_loop_exits.insert(non_dedicate);
  309. continue;
  310. }
  311. made_change = true;
  312. Function::iterator insert_pt = function->begin();
  313. for (; insert_pt != function->end() && &*insert_pt != non_dedicate;
  314. ++insert_pt) {
  315. }
  316. assert(insert_pt != function->end() && "Basic Block not found");
  317. // Create the dedicate exit basic block.
  318. // TODO(1841): Handle id overflow.
  319. BasicBlock& exit = *insert_pt.InsertBefore(std::unique_ptr<BasicBlock>(
  320. new BasicBlock(std::unique_ptr<Instruction>(new Instruction(
  321. context_, spv::Op::OpLabel, 0, context_->TakeNextId(), {})))));
  322. exit.SetParent(function);
  323. // Redirect in loop predecessors to |exit| block.
  324. for (uint32_t exit_pred_id : bb_pred) {
  325. if (loop_->IsInsideLoop(exit_pred_id)) {
  326. BasicBlock* pred_block = cfg.block(exit_pred_id);
  327. pred_block->ForEachSuccessorLabel([non_dedicate, &exit](uint32_t* id) {
  328. if (*id == non_dedicate->id()) *id = exit.id();
  329. });
  330. // Update the CFG.
  331. // |non_dedicate|'s predecessor list will be updated at the end of the
  332. // loop.
  333. cfg.RegisterBlock(pred_block);
  334. }
  335. }
  336. // Register the label to the def/use manager, requires for the phi patching.
  337. def_use_mgr->AnalyzeInstDefUse(exit.GetLabelInst());
  338. context_->set_instr_block(exit.GetLabelInst(), &exit);
  339. InstructionBuilder builder(context_, &exit, PreservedAnalyses);
  340. // Now jump from our dedicate basic block to the old exit.
  341. // We also reset the insert point so all instructions are inserted before
  342. // the branch.
  343. builder.SetInsertPoint(builder.AddBranch(non_dedicate->id()));
  344. non_dedicate->ForEachPhiInst(
  345. [&builder, &exit, def_use_mgr, this](Instruction* phi) {
  346. // New phi operands for this instruction.
  347. std::vector<uint32_t> new_phi_op;
  348. // Phi operands for the dedicated exit block.
  349. std::vector<uint32_t> exit_phi_op;
  350. for (uint32_t i = 0; i < phi->NumInOperands(); i += 2) {
  351. uint32_t def_id = phi->GetSingleWordInOperand(i);
  352. uint32_t incoming_id = phi->GetSingleWordInOperand(i + 1);
  353. if (loop_->IsInsideLoop(incoming_id)) {
  354. exit_phi_op.push_back(def_id);
  355. exit_phi_op.push_back(incoming_id);
  356. } else {
  357. new_phi_op.push_back(def_id);
  358. new_phi_op.push_back(incoming_id);
  359. }
  360. }
  361. // Build the new phi instruction dedicated exit block.
  362. Instruction* exit_phi = builder.AddPhi(phi->type_id(), exit_phi_op);
  363. // Build the new incoming branch.
  364. new_phi_op.push_back(exit_phi->result_id());
  365. new_phi_op.push_back(exit.id());
  366. // Rewrite operands.
  367. uint32_t idx = 0;
  368. for (; idx < new_phi_op.size(); idx++)
  369. phi->SetInOperand(idx, {new_phi_op[idx]});
  370. // Remove extra operands, from last to first (more efficient).
  371. for (uint32_t j = phi->NumInOperands() - 1; j >= idx; j--)
  372. phi->RemoveInOperand(j);
  373. // Update the def/use manager for this |phi|.
  374. def_use_mgr->AnalyzeInstUse(phi);
  375. });
  376. // Update the CFG.
  377. cfg.RegisterBlock(&exit);
  378. cfg.RemoveNonExistingEdges(non_dedicate->id());
  379. new_loop_exits.insert(&exit);
  380. // If non_dedicate is in a loop, add the new dedicated exit in that loop.
  381. if (Loop* parent_loop = loop_desc[non_dedicate])
  382. parent_loop->AddBasicBlock(&exit);
  383. }
  384. if (new_loop_exits.size() == 1) {
  385. loop_->SetMergeBlock(*new_loop_exits.begin());
  386. }
  387. if (made_change) {
  388. context_->InvalidateAnalysesExceptFor(
  389. PreservedAnalyses | IRContext::kAnalysisCFG |
  390. IRContext::Analysis::kAnalysisLoopAnalysis);
  391. }
  392. }
  393. void LoopUtils::MakeLoopClosedSSA() {
  394. CreateLoopDedicatedExits();
  395. Function* function = loop_->GetHeaderBlock()->GetParent();
  396. CFG& cfg = *context_->cfg();
  397. DominatorTree& dom_tree =
  398. context_->GetDominatorAnalysis(function)->GetDomTree();
  399. std::unordered_set<BasicBlock*> exit_bb;
  400. {
  401. std::unordered_set<uint32_t> exit_bb_id;
  402. loop_->GetExitBlocks(&exit_bb_id);
  403. for (uint32_t bb_id : exit_bb_id) {
  404. exit_bb.insert(cfg.block(bb_id));
  405. }
  406. }
  407. LCSSARewriter lcssa_rewriter(context_, dom_tree, exit_bb,
  408. loop_->GetMergeBlock());
  409. MakeSetClosedSSA(context_, function, loop_->GetBlocks(), exit_bb,
  410. &lcssa_rewriter);
  411. // Make sure all defs post-dominated by the merge block have their last use no
  412. // further than the merge block.
  413. if (loop_->GetMergeBlock()) {
  414. std::unordered_set<uint32_t> merging_bb_id;
  415. loop_->GetMergingBlocks(&merging_bb_id);
  416. merging_bb_id.erase(loop_->GetMergeBlock()->id());
  417. // Reset the exit set, now only the merge block is the exit.
  418. exit_bb.clear();
  419. exit_bb.insert(loop_->GetMergeBlock());
  420. // LCSSARewriter is reusable here only because it forces the creation of a
  421. // phi instruction in the merge block.
  422. MakeSetClosedSSA(context_, function, merging_bb_id, exit_bb,
  423. &lcssa_rewriter);
  424. }
  425. context_->InvalidateAnalysesExceptFor(
  426. IRContext::Analysis::kAnalysisCFG |
  427. IRContext::Analysis::kAnalysisDominatorAnalysis |
  428. IRContext::Analysis::kAnalysisLoopAnalysis);
  429. }
  430. Loop* LoopUtils::CloneLoop(LoopCloningResult* cloning_result) const {
  431. // Compute the structured order of the loop basic blocks and store it in the
  432. // vector ordered_loop_blocks.
  433. std::vector<BasicBlock*> ordered_loop_blocks;
  434. loop_->ComputeLoopStructuredOrder(&ordered_loop_blocks);
  435. // Clone the loop.
  436. return CloneLoop(cloning_result, ordered_loop_blocks);
  437. }
  438. Loop* LoopUtils::CloneAndAttachLoopToHeader(LoopCloningResult* cloning_result) {
  439. // Clone the loop.
  440. Loop* new_loop = CloneLoop(cloning_result);
  441. // Create a new exit block/label for the new loop.
  442. // TODO(1841): Handle id overflow.
  443. std::unique_ptr<Instruction> new_label{new Instruction(
  444. context_, spv::Op::OpLabel, 0, context_->TakeNextId(), {})};
  445. std::unique_ptr<BasicBlock> new_exit_bb{new BasicBlock(std::move(new_label))};
  446. new_exit_bb->SetParent(loop_->GetMergeBlock()->GetParent());
  447. // Create an unconditional branch to the header block.
  448. InstructionBuilder builder{context_, new_exit_bb.get()};
  449. builder.AddBranch(loop_->GetHeaderBlock()->id());
  450. // Save the ids of the new and old merge block.
  451. const uint32_t old_merge_block = loop_->GetMergeBlock()->id();
  452. const uint32_t new_merge_block = new_exit_bb->id();
  453. // Replace the uses of the old merge block in the new loop with the new merge
  454. // block.
  455. for (std::unique_ptr<BasicBlock>& basic_block : cloning_result->cloned_bb_) {
  456. for (Instruction& inst : *basic_block) {
  457. // For each operand in each instruction check if it is using the old merge
  458. // block and change it to be the new merge block.
  459. auto replace_merge_use = [old_merge_block,
  460. new_merge_block](uint32_t* id) {
  461. if (*id == old_merge_block) *id = new_merge_block;
  462. };
  463. inst.ForEachInOperand(replace_merge_use);
  464. }
  465. }
  466. const uint32_t old_header = loop_->GetHeaderBlock()->id();
  467. const uint32_t new_header = new_loop->GetHeaderBlock()->id();
  468. analysis::DefUseManager* def_use = context_->get_def_use_mgr();
  469. def_use->ForEachUse(old_header,
  470. [new_header, this](Instruction* inst, uint32_t operand) {
  471. if (!this->loop_->IsInsideLoop(inst))
  472. inst->SetOperand(operand, {new_header});
  473. });
  474. // TODO(1841): Handle failure to create pre-header.
  475. def_use->ForEachUse(
  476. loop_->GetOrCreatePreHeaderBlock()->id(),
  477. [new_merge_block, this](Instruction* inst, uint32_t operand) {
  478. if (this->loop_->IsInsideLoop(inst))
  479. inst->SetOperand(operand, {new_merge_block});
  480. });
  481. new_loop->SetMergeBlock(new_exit_bb.get());
  482. new_loop->SetPreHeaderBlock(loop_->GetPreHeaderBlock());
  483. // Add the new block into the cloned instructions.
  484. cloning_result->cloned_bb_.push_back(std::move(new_exit_bb));
  485. return new_loop;
  486. }
  487. Loop* LoopUtils::CloneLoop(
  488. LoopCloningResult* cloning_result,
  489. const std::vector<BasicBlock*>& ordered_loop_blocks) const {
  490. analysis::DefUseManager* def_use_mgr = context_->get_def_use_mgr();
  491. std::unique_ptr<Loop> new_loop = MakeUnique<Loop>(context_);
  492. CFG& cfg = *context_->cfg();
  493. // Clone and place blocks in a SPIR-V compliant order (dominators first).
  494. for (BasicBlock* old_bb : ordered_loop_blocks) {
  495. // For each basic block in the loop, we clone it and register the mapping
  496. // between old and new ids.
  497. BasicBlock* new_bb = old_bb->Clone(context_);
  498. new_bb->SetParent(&function_);
  499. // TODO(1841): Handle id overflow.
  500. new_bb->GetLabelInst()->SetResultId(context_->TakeNextId());
  501. def_use_mgr->AnalyzeInstDef(new_bb->GetLabelInst());
  502. context_->set_instr_block(new_bb->GetLabelInst(), new_bb);
  503. cloning_result->cloned_bb_.emplace_back(new_bb);
  504. cloning_result->old_to_new_bb_[old_bb->id()] = new_bb;
  505. cloning_result->new_to_old_bb_[new_bb->id()] = old_bb;
  506. cloning_result->value_map_[old_bb->id()] = new_bb->id();
  507. if (loop_->IsInsideLoop(old_bb)) new_loop->AddBasicBlock(new_bb);
  508. for (auto new_inst = new_bb->begin(), old_inst = old_bb->begin();
  509. new_inst != new_bb->end(); ++new_inst, ++old_inst) {
  510. cloning_result->ptr_map_[&*new_inst] = &*old_inst;
  511. if (new_inst->HasResultId()) {
  512. // TODO(1841): Handle id overflow.
  513. new_inst->SetResultId(context_->TakeNextId());
  514. cloning_result->value_map_[old_inst->result_id()] =
  515. new_inst->result_id();
  516. // Only look at the defs for now, uses are not updated yet.
  517. def_use_mgr->AnalyzeInstDef(&*new_inst);
  518. }
  519. }
  520. }
  521. // All instructions (including all labels) have been cloned,
  522. // remap instruction operands id with the new ones.
  523. for (std::unique_ptr<BasicBlock>& bb_ref : cloning_result->cloned_bb_) {
  524. BasicBlock* bb = bb_ref.get();
  525. for (Instruction& insn : *bb) {
  526. insn.ForEachInId([cloning_result](uint32_t* old_id) {
  527. // If the operand is defined in the loop, remap the id.
  528. auto id_it = cloning_result->value_map_.find(*old_id);
  529. if (id_it != cloning_result->value_map_.end()) {
  530. *old_id = id_it->second;
  531. }
  532. });
  533. // Only look at what the instruction uses. All defs are register, so all
  534. // should be fine now.
  535. def_use_mgr->AnalyzeInstUse(&insn);
  536. context_->set_instr_block(&insn, bb);
  537. }
  538. cfg.RegisterBlock(bb);
  539. }
  540. PopulateLoopNest(new_loop.get(), *cloning_result);
  541. return new_loop.release();
  542. }
  543. void LoopUtils::PopulateLoopNest(
  544. Loop* new_loop, const LoopCloningResult& cloning_result) const {
  545. std::unordered_map<Loop*, Loop*> loop_mapping;
  546. loop_mapping[loop_] = new_loop;
  547. if (loop_->HasParent()) loop_->GetParent()->AddNestedLoop(new_loop);
  548. PopulateLoopDesc(new_loop, loop_, cloning_result);
  549. for (Loop& sub_loop :
  550. make_range(++TreeDFIterator<Loop>(loop_), TreeDFIterator<Loop>())) {
  551. Loop* cloned = new Loop(context_);
  552. if (Loop* parent = loop_mapping[sub_loop.GetParent()])
  553. parent->AddNestedLoop(cloned);
  554. loop_mapping[&sub_loop] = cloned;
  555. PopulateLoopDesc(cloned, &sub_loop, cloning_result);
  556. }
  557. loop_desc_->AddLoopNest(std::unique_ptr<Loop>(new_loop));
  558. }
  559. // Populates |new_loop| descriptor according to |old_loop|'s one.
  560. void LoopUtils::PopulateLoopDesc(
  561. Loop* new_loop, Loop* old_loop,
  562. const LoopCloningResult& cloning_result) const {
  563. for (uint32_t bb_id : old_loop->GetBlocks()) {
  564. BasicBlock* bb = cloning_result.old_to_new_bb_.at(bb_id);
  565. new_loop->AddBasicBlock(bb);
  566. }
  567. new_loop->SetHeaderBlock(
  568. cloning_result.old_to_new_bb_.at(old_loop->GetHeaderBlock()->id()));
  569. if (old_loop->GetLatchBlock())
  570. new_loop->SetLatchBlock(
  571. cloning_result.old_to_new_bb_.at(old_loop->GetLatchBlock()->id()));
  572. if (old_loop->GetContinueBlock())
  573. new_loop->SetContinueBlock(
  574. cloning_result.old_to_new_bb_.at(old_loop->GetContinueBlock()->id()));
  575. if (old_loop->GetMergeBlock()) {
  576. auto it =
  577. cloning_result.old_to_new_bb_.find(old_loop->GetMergeBlock()->id());
  578. BasicBlock* bb = it != cloning_result.old_to_new_bb_.end()
  579. ? it->second
  580. : old_loop->GetMergeBlock();
  581. new_loop->SetMergeBlock(bb);
  582. }
  583. if (old_loop->GetPreHeaderBlock()) {
  584. auto it =
  585. cloning_result.old_to_new_bb_.find(old_loop->GetPreHeaderBlock()->id());
  586. if (it != cloning_result.old_to_new_bb_.end()) {
  587. new_loop->SetPreHeaderBlock(it->second);
  588. }
  589. }
  590. }
  591. // Class to gather some metrics about a region of interest.
  592. void CodeMetrics::Analyze(const Loop& loop) {
  593. CFG& cfg = *loop.GetContext()->cfg();
  594. roi_size_ = 0;
  595. block_sizes_.clear();
  596. for (uint32_t id : loop.GetBlocks()) {
  597. const BasicBlock* bb = cfg.block(id);
  598. size_t bb_size = 0;
  599. bb->ForEachInst([&bb_size](const Instruction* insn) {
  600. if (insn->opcode() == spv::Op::OpLabel) return;
  601. if (insn->IsNop()) return;
  602. if (insn->opcode() == spv::Op::OpPhi) return;
  603. bb_size++;
  604. });
  605. block_sizes_[bb->id()] = bb_size;
  606. roi_size_ += bb_size;
  607. }
  608. }
  609. } // namespace opt
  610. } // namespace spvtools