loop_descriptor.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. // Copyright (c) 2017 Google Inc.
  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/loop_descriptor.h"
  15. #include <algorithm>
  16. #include <iostream>
  17. #include <limits>
  18. #include <stack>
  19. #include <type_traits>
  20. #include <utility>
  21. #include <vector>
  22. #include "source/opt/cfg.h"
  23. #include "source/opt/constants.h"
  24. #include "source/opt/dominator_tree.h"
  25. #include "source/opt/ir_builder.h"
  26. #include "source/opt/ir_context.h"
  27. #include "source/opt/iterator.h"
  28. #include "source/opt/tree_iterator.h"
  29. #include "source/util/make_unique.h"
  30. namespace spvtools {
  31. namespace opt {
  32. // Takes in a phi instruction |induction| and the loop |header| and returns the
  33. // step operation of the loop.
  34. Instruction* Loop::GetInductionStepOperation(
  35. const Instruction* induction) const {
  36. // Induction must be a phi instruction.
  37. assert(induction->opcode() == spv::Op::OpPhi);
  38. Instruction* step = nullptr;
  39. analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
  40. // Traverse the incoming operands of the phi instruction.
  41. for (uint32_t operand_id = 1; operand_id < induction->NumInOperands();
  42. operand_id += 2) {
  43. // Incoming edge.
  44. BasicBlock* incoming_block =
  45. context_->cfg()->block(induction->GetSingleWordInOperand(operand_id));
  46. // Check if the block is dominated by header, and thus coming from within
  47. // the loop.
  48. if (IsInsideLoop(incoming_block)) {
  49. step = def_use_manager->GetDef(
  50. induction->GetSingleWordInOperand(operand_id - 1));
  51. break;
  52. }
  53. }
  54. if (!step || !IsSupportedStepOp(step->opcode())) {
  55. return nullptr;
  56. }
  57. // The induction variable which binds the loop must only be modified once.
  58. uint32_t lhs = step->GetSingleWordInOperand(0);
  59. uint32_t rhs = step->GetSingleWordInOperand(1);
  60. // One of the left hand side or right hand side of the step instruction must
  61. // be the induction phi and the other must be an OpConstant.
  62. if (lhs != induction->result_id() && rhs != induction->result_id()) {
  63. return nullptr;
  64. }
  65. if (def_use_manager->GetDef(lhs)->opcode() != spv::Op::OpConstant &&
  66. def_use_manager->GetDef(rhs)->opcode() != spv::Op::OpConstant) {
  67. return nullptr;
  68. }
  69. return step;
  70. }
  71. // Returns true if the |step| operation is an induction variable step operation
  72. // which is currently handled.
  73. bool Loop::IsSupportedStepOp(spv::Op step) const {
  74. switch (step) {
  75. case spv::Op::OpISub:
  76. case spv::Op::OpIAdd:
  77. return true;
  78. default:
  79. return false;
  80. }
  81. }
  82. bool Loop::IsSupportedCondition(spv::Op condition) const {
  83. switch (condition) {
  84. // <
  85. case spv::Op::OpULessThan:
  86. case spv::Op::OpSLessThan:
  87. // >
  88. case spv::Op::OpUGreaterThan:
  89. case spv::Op::OpSGreaterThan:
  90. // >=
  91. case spv::Op::OpSGreaterThanEqual:
  92. case spv::Op::OpUGreaterThanEqual:
  93. // <=
  94. case spv::Op::OpSLessThanEqual:
  95. case spv::Op::OpULessThanEqual:
  96. return true;
  97. default:
  98. return false;
  99. }
  100. }
  101. int64_t Loop::GetResidualConditionValue(spv::Op condition,
  102. int64_t initial_value,
  103. int64_t step_value,
  104. size_t number_of_iterations,
  105. size_t factor) {
  106. int64_t remainder =
  107. initial_value + (number_of_iterations % factor) * step_value;
  108. // We subtract or add one as the above formula calculates the remainder if the
  109. // loop where just less than or greater than. Adding or subtracting one should
  110. // give a functionally equivalent value.
  111. switch (condition) {
  112. case spv::Op::OpSGreaterThanEqual:
  113. case spv::Op::OpUGreaterThanEqual: {
  114. remainder -= 1;
  115. break;
  116. }
  117. case spv::Op::OpSLessThanEqual:
  118. case spv::Op::OpULessThanEqual: {
  119. remainder += 1;
  120. break;
  121. }
  122. default:
  123. break;
  124. }
  125. return remainder;
  126. }
  127. Instruction* Loop::GetConditionInst() const {
  128. BasicBlock* condition_block = FindConditionBlock();
  129. if (!condition_block) {
  130. return nullptr;
  131. }
  132. Instruction* branch_conditional = &*condition_block->tail();
  133. if (!branch_conditional ||
  134. branch_conditional->opcode() != spv::Op::OpBranchConditional) {
  135. return nullptr;
  136. }
  137. Instruction* condition_inst = context_->get_def_use_mgr()->GetDef(
  138. branch_conditional->GetSingleWordInOperand(0));
  139. if (IsSupportedCondition(condition_inst->opcode())) {
  140. return condition_inst;
  141. }
  142. return nullptr;
  143. }
  144. // Extract the initial value from the |induction| OpPhi instruction and store it
  145. // in |value|. If the function couldn't find the initial value of |induction|
  146. // return false.
  147. bool Loop::GetInductionInitValue(const Instruction* induction,
  148. int64_t* value) const {
  149. Instruction* constant_instruction = nullptr;
  150. analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
  151. for (uint32_t operand_id = 0; operand_id < induction->NumInOperands();
  152. operand_id += 2) {
  153. BasicBlock* bb = context_->cfg()->block(
  154. induction->GetSingleWordInOperand(operand_id + 1));
  155. if (!IsInsideLoop(bb)) {
  156. constant_instruction = def_use_manager->GetDef(
  157. induction->GetSingleWordInOperand(operand_id));
  158. }
  159. }
  160. if (!constant_instruction) return false;
  161. const analysis::Constant* constant =
  162. context_->get_constant_mgr()->FindDeclaredConstant(
  163. constant_instruction->result_id());
  164. if (!constant) return false;
  165. if (value) {
  166. const analysis::Integer* type = constant->type()->AsInteger();
  167. if (!type) {
  168. return false;
  169. }
  170. *value = type->IsSigned() ? constant->GetSignExtendedValue()
  171. : constant->GetZeroExtendedValue();
  172. }
  173. return true;
  174. }
  175. Loop::Loop(IRContext* context, DominatorAnalysis* dom_analysis,
  176. BasicBlock* header, BasicBlock* continue_target,
  177. BasicBlock* merge_target)
  178. : context_(context),
  179. loop_header_(header),
  180. loop_continue_(continue_target),
  181. loop_merge_(merge_target),
  182. loop_preheader_(nullptr),
  183. parent_(nullptr),
  184. loop_is_marked_for_removal_(false) {
  185. assert(context);
  186. assert(dom_analysis);
  187. loop_preheader_ = FindLoopPreheader(dom_analysis);
  188. loop_latch_ = FindLatchBlock();
  189. }
  190. BasicBlock* Loop::FindLoopPreheader(DominatorAnalysis* dom_analysis) {
  191. CFG* cfg = context_->cfg();
  192. DominatorTree& dom_tree = dom_analysis->GetDomTree();
  193. DominatorTreeNode* header_node = dom_tree.GetTreeNode(loop_header_);
  194. // The loop predecessor.
  195. BasicBlock* loop_pred = nullptr;
  196. auto header_pred = cfg->preds(loop_header_->id());
  197. for (uint32_t p_id : header_pred) {
  198. DominatorTreeNode* node = dom_tree.GetTreeNode(p_id);
  199. if (node && !dom_tree.Dominates(header_node, node)) {
  200. // The predecessor is not part of the loop, so potential loop preheader.
  201. if (loop_pred && node->bb_ != loop_pred) {
  202. // If we saw 2 distinct predecessors that are outside the loop, we don't
  203. // have a loop preheader.
  204. return nullptr;
  205. }
  206. loop_pred = node->bb_;
  207. }
  208. }
  209. // Safe guard against invalid code, SPIR-V spec forbids loop with the entry
  210. // node as header.
  211. assert(loop_pred && "The header node is the entry block ?");
  212. // So we have a unique basic block that can enter this loop.
  213. // If this loop is the unique successor of this block, then it is a loop
  214. // preheader.
  215. bool is_preheader = true;
  216. uint32_t loop_header_id = loop_header_->id();
  217. const auto* const_loop_pred = loop_pred;
  218. const_loop_pred->ForEachSuccessorLabel(
  219. [&is_preheader, loop_header_id](const uint32_t id) {
  220. if (id != loop_header_id) is_preheader = false;
  221. });
  222. if (is_preheader) return loop_pred;
  223. return nullptr;
  224. }
  225. bool Loop::IsInsideLoop(Instruction* inst) const {
  226. const BasicBlock* parent_block = context_->get_instr_block(inst);
  227. if (!parent_block) return false;
  228. return IsInsideLoop(parent_block);
  229. }
  230. bool Loop::IsBasicBlockInLoopSlow(const BasicBlock* bb) {
  231. assert(bb->GetParent() && "The basic block does not belong to a function");
  232. DominatorAnalysis* dom_analysis =
  233. context_->GetDominatorAnalysis(bb->GetParent());
  234. if (dom_analysis->IsReachable(bb) &&
  235. !dom_analysis->Dominates(GetHeaderBlock(), bb))
  236. return false;
  237. return true;
  238. }
  239. BasicBlock* Loop::GetOrCreatePreHeaderBlock() {
  240. if (loop_preheader_) return loop_preheader_;
  241. CFG* cfg = context_->cfg();
  242. loop_header_ = cfg->SplitLoopHeader(loop_header_);
  243. return loop_preheader_;
  244. }
  245. void Loop::SetContinueBlock(BasicBlock* continue_block) {
  246. assert(IsInsideLoop(continue_block));
  247. loop_continue_ = continue_block;
  248. }
  249. void Loop::SetLatchBlock(BasicBlock* latch) {
  250. #ifndef NDEBUG
  251. assert(latch->GetParent() && "The basic block does not belong to a function");
  252. const auto* const_latch = latch;
  253. const_latch->ForEachSuccessorLabel([this](uint32_t id) {
  254. assert((!IsInsideLoop(id) || id == GetHeaderBlock()->id()) &&
  255. "A predecessor of the continue block does not belong to the loop");
  256. });
  257. #endif // NDEBUG
  258. assert(IsInsideLoop(latch) && "The continue block is not in the loop");
  259. SetLatchBlockImpl(latch);
  260. }
  261. void Loop::SetMergeBlock(BasicBlock* merge) {
  262. #ifndef NDEBUG
  263. assert(merge->GetParent() && "The basic block does not belong to a function");
  264. #endif // NDEBUG
  265. assert(!IsInsideLoop(merge) && "The merge block is in the loop");
  266. SetMergeBlockImpl(merge);
  267. if (GetHeaderBlock()->GetLoopMergeInst()) {
  268. UpdateLoopMergeInst();
  269. }
  270. }
  271. void Loop::SetPreHeaderBlock(BasicBlock* preheader) {
  272. if (preheader) {
  273. assert(!IsInsideLoop(preheader) && "The preheader block is in the loop");
  274. assert(preheader->tail()->opcode() == spv::Op::OpBranch &&
  275. "The preheader block does not unconditionally branch to the header "
  276. "block");
  277. assert(preheader->tail()->GetSingleWordOperand(0) ==
  278. GetHeaderBlock()->id() &&
  279. "The preheader block does not unconditionally branch to the header "
  280. "block");
  281. }
  282. loop_preheader_ = preheader;
  283. }
  284. BasicBlock* Loop::FindLatchBlock() {
  285. CFG* cfg = context_->cfg();
  286. DominatorAnalysis* dominator_analysis =
  287. context_->GetDominatorAnalysis(loop_header_->GetParent());
  288. // Look at the predecessors of the loop header to find a predecessor block
  289. // which is dominated by the loop continue target. There should only be one
  290. // block which meets this criteria and this is the latch block, as per the
  291. // SPIR-V spec.
  292. for (uint32_t block_id : cfg->preds(loop_header_->id())) {
  293. if (dominator_analysis->Dominates(loop_continue_->id(), block_id)) {
  294. return cfg->block(block_id);
  295. }
  296. }
  297. assert(
  298. false &&
  299. "Every loop should have a latch block dominated by the continue target");
  300. return nullptr;
  301. }
  302. void Loop::GetExitBlocks(std::unordered_set<uint32_t>* exit_blocks) const {
  303. CFG* cfg = context_->cfg();
  304. exit_blocks->clear();
  305. for (uint32_t bb_id : GetBlocks()) {
  306. const BasicBlock* bb = cfg->block(bb_id);
  307. bb->ForEachSuccessorLabel([exit_blocks, this](uint32_t succ) {
  308. if (!IsInsideLoop(succ)) {
  309. exit_blocks->insert(succ);
  310. }
  311. });
  312. }
  313. }
  314. void Loop::GetMergingBlocks(
  315. std::unordered_set<uint32_t>* merging_blocks) const {
  316. assert(GetMergeBlock() && "This loop is not structured");
  317. CFG* cfg = context_->cfg();
  318. merging_blocks->clear();
  319. std::stack<const BasicBlock*> to_visit;
  320. to_visit.push(GetMergeBlock());
  321. while (!to_visit.empty()) {
  322. const BasicBlock* bb = to_visit.top();
  323. to_visit.pop();
  324. merging_blocks->insert(bb->id());
  325. for (uint32_t pred_id : cfg->preds(bb->id())) {
  326. if (!IsInsideLoop(pred_id) && !merging_blocks->count(pred_id)) {
  327. to_visit.push(cfg->block(pred_id));
  328. }
  329. }
  330. }
  331. }
  332. namespace {
  333. inline bool IsBasicBlockSafeToClone(IRContext* context, BasicBlock* bb) {
  334. for (Instruction& inst : *bb) {
  335. if (!inst.IsBranch() && !context->IsCombinatorInstruction(&inst))
  336. return false;
  337. }
  338. return true;
  339. }
  340. } // namespace
  341. bool Loop::IsSafeToClone() const {
  342. CFG& cfg = *context_->cfg();
  343. for (uint32_t bb_id : GetBlocks()) {
  344. BasicBlock* bb = cfg.block(bb_id);
  345. assert(bb);
  346. if (!IsBasicBlockSafeToClone(context_, bb)) return false;
  347. }
  348. // Look at the merge construct.
  349. if (GetHeaderBlock()->GetLoopMergeInst()) {
  350. std::unordered_set<uint32_t> blocks;
  351. GetMergingBlocks(&blocks);
  352. blocks.erase(GetMergeBlock()->id());
  353. for (uint32_t bb_id : blocks) {
  354. BasicBlock* bb = cfg.block(bb_id);
  355. assert(bb);
  356. if (!IsBasicBlockSafeToClone(context_, bb)) return false;
  357. }
  358. }
  359. return true;
  360. }
  361. bool Loop::IsLCSSA() const {
  362. CFG* cfg = context_->cfg();
  363. analysis::DefUseManager* def_use_mgr = context_->get_def_use_mgr();
  364. std::unordered_set<uint32_t> exit_blocks;
  365. GetExitBlocks(&exit_blocks);
  366. // Declare ir_context so we can capture context_ in the below lambda
  367. IRContext* ir_context = context_;
  368. for (uint32_t bb_id : GetBlocks()) {
  369. for (Instruction& insn : *cfg->block(bb_id)) {
  370. // All uses must be either:
  371. // - In the loop;
  372. // - In an exit block and in a phi instruction.
  373. if (!def_use_mgr->WhileEachUser(
  374. &insn,
  375. [&exit_blocks, ir_context, this](Instruction* use) -> bool {
  376. BasicBlock* parent = ir_context->get_instr_block(use);
  377. assert(parent && "Invalid analysis");
  378. if (IsInsideLoop(parent)) return true;
  379. if (use->opcode() != spv::Op::OpPhi) return false;
  380. return exit_blocks.count(parent->id());
  381. }))
  382. return false;
  383. }
  384. }
  385. return true;
  386. }
  387. bool Loop::ShouldHoistInstruction(IRContext* context, Instruction* inst) {
  388. return AreAllOperandsOutsideLoop(context, inst) &&
  389. inst->IsOpcodeCodeMotionSafe();
  390. }
  391. bool Loop::AreAllOperandsOutsideLoop(IRContext* context, Instruction* inst) {
  392. analysis::DefUseManager* def_use_mgr = context->get_def_use_mgr();
  393. bool all_outside_loop = true;
  394. const std::function<void(uint32_t*)> operand_outside_loop =
  395. [this, &def_use_mgr, &all_outside_loop](uint32_t* id) {
  396. if (this->IsInsideLoop(def_use_mgr->GetDef(*id))) {
  397. all_outside_loop = false;
  398. return;
  399. }
  400. };
  401. inst->ForEachInId(operand_outside_loop);
  402. return all_outside_loop;
  403. }
  404. void Loop::ComputeLoopStructuredOrder(
  405. std::vector<BasicBlock*>* ordered_loop_blocks, bool include_pre_header,
  406. bool include_merge) const {
  407. CFG& cfg = *context_->cfg();
  408. // Reserve the memory: all blocks in the loop + extra if needed.
  409. ordered_loop_blocks->reserve(GetBlocks().size() + include_pre_header +
  410. include_merge);
  411. if (include_pre_header && GetPreHeaderBlock())
  412. ordered_loop_blocks->push_back(loop_preheader_);
  413. bool is_shader =
  414. context_->get_feature_mgr()->HasCapability(spv::Capability::Shader);
  415. if (!is_shader) {
  416. cfg.ForEachBlockInReversePostOrder(
  417. loop_header_, [ordered_loop_blocks, this](BasicBlock* bb) {
  418. if (IsInsideLoop(bb)) ordered_loop_blocks->push_back(bb);
  419. });
  420. } else {
  421. // If this is a shader, it is possible that there are unreachable merge and
  422. // continue blocks that must be copied to retain the structured order.
  423. // The structured order will include these.
  424. std::list<BasicBlock*> order;
  425. cfg.ComputeStructuredOrder(loop_header_->GetParent(), loop_header_,
  426. loop_merge_, &order);
  427. for (BasicBlock* bb : order) {
  428. if (bb == GetMergeBlock()) {
  429. break;
  430. }
  431. ordered_loop_blocks->push_back(bb);
  432. }
  433. }
  434. if (include_merge && GetMergeBlock())
  435. ordered_loop_blocks->push_back(loop_merge_);
  436. }
  437. LoopDescriptor::LoopDescriptor(IRContext* context, const Function* f)
  438. : loops_(), placeholder_top_loop_(nullptr) {
  439. PopulateList(context, f);
  440. }
  441. LoopDescriptor::~LoopDescriptor() { ClearLoops(); }
  442. void LoopDescriptor::PopulateList(IRContext* context, const Function* f) {
  443. DominatorAnalysis* dom_analysis = context->GetDominatorAnalysis(f);
  444. ClearLoops();
  445. // Post-order traversal of the dominator tree to find all the OpLoopMerge
  446. // instructions.
  447. DominatorTree& dom_tree = dom_analysis->GetDomTree();
  448. for (DominatorTreeNode& node :
  449. make_range(dom_tree.post_begin(), dom_tree.post_end())) {
  450. Instruction* merge_inst = node.bb_->GetLoopMergeInst();
  451. if (merge_inst) {
  452. bool all_backedge_unreachable = true;
  453. for (uint32_t pid : context->cfg()->preds(node.bb_->id())) {
  454. if (dom_analysis->IsReachable(pid) &&
  455. dom_analysis->Dominates(node.bb_->id(), pid)) {
  456. all_backedge_unreachable = false;
  457. break;
  458. }
  459. }
  460. if (all_backedge_unreachable)
  461. continue; // ignore this one, we actually never branch back.
  462. // The id of the merge basic block of this loop.
  463. uint32_t merge_bb_id = merge_inst->GetSingleWordOperand(0);
  464. // The id of the continue basic block of this loop.
  465. uint32_t continue_bb_id = merge_inst->GetSingleWordOperand(1);
  466. // The merge target of this loop.
  467. BasicBlock* merge_bb = context->cfg()->block(merge_bb_id);
  468. // The continue target of this loop.
  469. BasicBlock* continue_bb = context->cfg()->block(continue_bb_id);
  470. // The basic block containing the merge instruction.
  471. BasicBlock* header_bb = context->get_instr_block(merge_inst);
  472. // Add the loop to the list of all the loops in the function.
  473. Loop* current_loop =
  474. new Loop(context, dom_analysis, header_bb, continue_bb, merge_bb);
  475. loops_.push_back(current_loop);
  476. // We have a bottom-up construction, so if this loop has nested-loops,
  477. // they are by construction at the tail of the loop list.
  478. for (auto itr = loops_.rbegin() + 1; itr != loops_.rend(); ++itr) {
  479. Loop* previous_loop = *itr;
  480. // If the loop already has a parent, then it has been processed.
  481. if (previous_loop->HasParent()) continue;
  482. // If the current loop does not dominates the previous loop then it is
  483. // not nested loop.
  484. if (!dom_analysis->Dominates(header_bb,
  485. previous_loop->GetHeaderBlock()))
  486. continue;
  487. // If the current loop merge dominates the previous loop then it is
  488. // not nested loop.
  489. if (dom_analysis->Dominates(merge_bb, previous_loop->GetHeaderBlock()))
  490. continue;
  491. current_loop->AddNestedLoop(previous_loop);
  492. }
  493. DominatorTreeNode* dom_merge_node = dom_tree.GetTreeNode(merge_bb);
  494. for (DominatorTreeNode& loop_node :
  495. make_range(node.df_begin(), node.df_end())) {
  496. // Check if we are in the loop.
  497. if (dom_tree.Dominates(dom_merge_node, &loop_node)) continue;
  498. current_loop->AddBasicBlock(loop_node.bb_);
  499. basic_block_to_loop_.insert(
  500. std::make_pair(loop_node.bb_->id(), current_loop));
  501. }
  502. }
  503. }
  504. for (Loop* loop : loops_) {
  505. if (!loop->HasParent()) placeholder_top_loop_.nested_loops_.push_back(loop);
  506. }
  507. }
  508. std::vector<Loop*> LoopDescriptor::GetLoopsInBinaryLayoutOrder() {
  509. std::vector<uint32_t> ids{};
  510. for (size_t i = 0; i < NumLoops(); ++i) {
  511. ids.push_back(GetLoopByIndex(i).GetHeaderBlock()->id());
  512. }
  513. std::vector<Loop*> loops{};
  514. if (!ids.empty()) {
  515. auto function = GetLoopByIndex(0).GetHeaderBlock()->GetParent();
  516. for (const auto& block : *function) {
  517. auto block_id = block.id();
  518. auto element = std::find(std::begin(ids), std::end(ids), block_id);
  519. if (element != std::end(ids)) {
  520. loops.push_back(&GetLoopByIndex(element - std::begin(ids)));
  521. }
  522. }
  523. }
  524. return loops;
  525. }
  526. BasicBlock* Loop::FindConditionBlock() const {
  527. if (!loop_merge_) {
  528. return nullptr;
  529. }
  530. BasicBlock* condition_block = nullptr;
  531. uint32_t in_loop_pred = 0;
  532. for (uint32_t p : context_->cfg()->preds(loop_merge_->id())) {
  533. if (IsInsideLoop(p)) {
  534. if (in_loop_pred) {
  535. // 2 in-loop predecessors.
  536. return nullptr;
  537. }
  538. in_loop_pred = p;
  539. }
  540. }
  541. if (!in_loop_pred) {
  542. // Merge block is unreachable.
  543. return nullptr;
  544. }
  545. BasicBlock* bb = context_->cfg()->block(in_loop_pred);
  546. if (!bb) return nullptr;
  547. const Instruction& branch = *bb->ctail();
  548. // Make sure the branch is a conditional branch.
  549. if (branch.opcode() != spv::Op::OpBranchConditional) return nullptr;
  550. // Make sure one of the two possible branches is to the merge block.
  551. if (branch.GetSingleWordInOperand(1) == loop_merge_->id() ||
  552. branch.GetSingleWordInOperand(2) == loop_merge_->id()) {
  553. condition_block = bb;
  554. }
  555. return condition_block;
  556. }
  557. bool Loop::FindNumberOfIterations(const Instruction* induction,
  558. const Instruction* branch_inst,
  559. size_t* iterations_out,
  560. int64_t* step_value_out,
  561. int64_t* init_value_out) const {
  562. // From the branch instruction find the branch condition.
  563. analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
  564. // Condition instruction from the OpConditionalBranch.
  565. Instruction* condition =
  566. def_use_manager->GetDef(branch_inst->GetSingleWordOperand(0));
  567. assert(IsSupportedCondition(condition->opcode()));
  568. // Get the constant manager from the ir context.
  569. analysis::ConstantManager* const_manager = context_->get_constant_mgr();
  570. // Find the constant value used by the condition variable. Exit out if it
  571. // isn't a constant int.
  572. const analysis::Constant* upper_bound =
  573. const_manager->FindDeclaredConstant(condition->GetSingleWordOperand(3));
  574. if (!upper_bound) return false;
  575. // Must be integer because of the opcode on the condition.
  576. const analysis::Integer* type = upper_bound->type()->AsInteger();
  577. if (!type || type->width() > 64) {
  578. return false;
  579. }
  580. int64_t condition_value = type->IsSigned()
  581. ? upper_bound->GetSignExtendedValue()
  582. : upper_bound->GetZeroExtendedValue();
  583. // Find the instruction which is stepping through the loop.
  584. //
  585. // GetInductionStepOperation returns nullptr if |step_inst| is OpConstantNull.
  586. Instruction* step_inst = GetInductionStepOperation(induction);
  587. if (!step_inst) return false;
  588. // Find the constant value used by the condition variable.
  589. const analysis::Constant* step_constant =
  590. const_manager->FindDeclaredConstant(step_inst->GetSingleWordOperand(3));
  591. if (!step_constant) return false;
  592. // Must be integer because of the opcode on the condition.
  593. int64_t step_value = 0;
  594. const analysis::Integer* step_type =
  595. step_constant->AsIntConstant()->type()->AsInteger();
  596. if (step_type->IsSigned()) {
  597. step_value = step_constant->AsIntConstant()->GetS32BitValue();
  598. } else {
  599. step_value = step_constant->AsIntConstant()->GetU32BitValue();
  600. }
  601. // If this is a subtraction step we should negate the step value.
  602. if (step_inst->opcode() == spv::Op::OpISub) {
  603. step_value = -step_value;
  604. }
  605. // Find the initial value of the loop and make sure it is a constant integer.
  606. int64_t init_value = 0;
  607. if (!GetInductionInitValue(induction, &init_value)) return false;
  608. // If iterations is non null then store the value in that.
  609. int64_t num_itrs = GetIterations(condition->opcode(), condition_value,
  610. init_value, step_value);
  611. // If the loop body will not be reached return false.
  612. if (num_itrs <= 0) {
  613. return false;
  614. }
  615. if (iterations_out) {
  616. assert(static_cast<size_t>(num_itrs) <= std::numeric_limits<size_t>::max());
  617. *iterations_out = static_cast<size_t>(num_itrs);
  618. }
  619. if (step_value_out) {
  620. *step_value_out = step_value;
  621. }
  622. if (init_value_out) {
  623. *init_value_out = init_value;
  624. }
  625. return true;
  626. }
  627. // We retrieve the number of iterations using the following formula, diff /
  628. // |step_value| where diff is calculated differently according to the
  629. // |condition| and uses the |condition_value| and |init_value|. If diff /
  630. // |step_value| is NOT cleanly divisible then we add one to the sum.
  631. int64_t Loop::GetIterations(spv::Op condition, int64_t condition_value,
  632. int64_t init_value, int64_t step_value) const {
  633. if (step_value == 0) {
  634. return 0;
  635. }
  636. int64_t diff = 0;
  637. switch (condition) {
  638. case spv::Op::OpSLessThan:
  639. case spv::Op::OpULessThan: {
  640. // If the condition is not met to begin with the loop will never iterate.
  641. if (!(init_value < condition_value)) return 0;
  642. diff = condition_value - init_value;
  643. // If the operation is a less then operation then the diff and step must
  644. // have the same sign otherwise the induction will never cross the
  645. // condition (either never true or always true).
  646. if ((diff < 0 && step_value > 0) || (diff > 0 && step_value < 0)) {
  647. return 0;
  648. }
  649. break;
  650. }
  651. case spv::Op::OpSGreaterThan:
  652. case spv::Op::OpUGreaterThan: {
  653. // If the condition is not met to begin with the loop will never iterate.
  654. if (!(init_value > condition_value)) return 0;
  655. diff = init_value - condition_value;
  656. // If the operation is a greater than operation then the diff and step
  657. // must have opposite signs. Otherwise the condition will always be true
  658. // or will never be true.
  659. if ((diff < 0 && step_value < 0) || (diff > 0 && step_value > 0)) {
  660. return 0;
  661. }
  662. break;
  663. }
  664. case spv::Op::OpSGreaterThanEqual:
  665. case spv::Op::OpUGreaterThanEqual: {
  666. // If the condition is not met to begin with the loop will never iterate.
  667. if (!(init_value >= condition_value)) return 0;
  668. // We subtract one to make it the same as spv::Op::OpGreaterThan as it is
  669. // functionally equivalent.
  670. diff = init_value - (condition_value - 1);
  671. // If the operation is a greater than operation then the diff and step
  672. // must have opposite signs. Otherwise the condition will always be true
  673. // or will never be true.
  674. if ((diff > 0 && step_value > 0) || (diff < 0 && step_value < 0)) {
  675. return 0;
  676. }
  677. break;
  678. }
  679. case spv::Op::OpSLessThanEqual:
  680. case spv::Op::OpULessThanEqual: {
  681. // If the condition is not met to begin with the loop will never iterate.
  682. if (!(init_value <= condition_value)) return 0;
  683. // We add one to make it the same as spv::Op::OpLessThan as it is
  684. // functionally equivalent.
  685. diff = (condition_value + 1) - init_value;
  686. // If the operation is a less than operation then the diff and step must
  687. // have the same sign otherwise the induction will never cross the
  688. // condition (either never true or always true).
  689. if ((diff < 0 && step_value > 0) || (diff > 0 && step_value < 0)) {
  690. return 0;
  691. }
  692. break;
  693. }
  694. default:
  695. assert(false &&
  696. "Could not retrieve number of iterations from the loop condition. "
  697. "Condition is not supported.");
  698. }
  699. // Take the abs of - step values.
  700. step_value = llabs(step_value);
  701. diff = llabs(diff);
  702. int64_t result = diff / step_value;
  703. if (diff % step_value != 0) {
  704. result += 1;
  705. }
  706. return result;
  707. }
  708. // Returns the list of induction variables within the loop.
  709. void Loop::GetInductionVariables(
  710. std::vector<Instruction*>& induction_variables) const {
  711. for (Instruction& inst : *loop_header_) {
  712. if (inst.opcode() == spv::Op::OpPhi) {
  713. induction_variables.push_back(&inst);
  714. }
  715. }
  716. }
  717. Instruction* Loop::FindConditionVariable(
  718. const BasicBlock* condition_block) const {
  719. // Find the branch instruction.
  720. const Instruction& branch_inst = *condition_block->ctail();
  721. Instruction* induction = nullptr;
  722. // Verify that the branch instruction is a conditional branch.
  723. if (branch_inst.opcode() == spv::Op::OpBranchConditional) {
  724. // From the branch instruction find the branch condition.
  725. analysis::DefUseManager* def_use_manager = context_->get_def_use_mgr();
  726. // Find the instruction representing the condition used in the conditional
  727. // branch.
  728. Instruction* condition =
  729. def_use_manager->GetDef(branch_inst.GetSingleWordOperand(0));
  730. // Ensure that the condition is a less than operation.
  731. if (condition && IsSupportedCondition(condition->opcode())) {
  732. // The left hand side operand of the operation.
  733. Instruction* variable_inst =
  734. def_use_manager->GetDef(condition->GetSingleWordOperand(2));
  735. // Make sure the variable instruction used is a phi.
  736. if (!variable_inst || variable_inst->opcode() != spv::Op::OpPhi)
  737. return nullptr;
  738. // Make sure the phi instruction only has two incoming blocks. Each
  739. // incoming block will be represented by two in operands in the phi
  740. // instruction, the value and the block which that value came from. We
  741. // assume the cannocalised phi will have two incoming values, one from the
  742. // preheader and one from the continue block.
  743. size_t max_supported_operands = 4;
  744. if (variable_inst->NumInOperands() == max_supported_operands) {
  745. // The operand index of the first incoming block label.
  746. uint32_t operand_label_1 = 1;
  747. // The operand index of the second incoming block label.
  748. uint32_t operand_label_2 = 3;
  749. // Make sure one of them is the preheader.
  750. if (!IsInsideLoop(
  751. variable_inst->GetSingleWordInOperand(operand_label_1)) &&
  752. !IsInsideLoop(
  753. variable_inst->GetSingleWordInOperand(operand_label_2))) {
  754. return nullptr;
  755. }
  756. // And make sure that the other is the latch block.
  757. if (variable_inst->GetSingleWordInOperand(operand_label_1) !=
  758. loop_latch_->id() &&
  759. variable_inst->GetSingleWordInOperand(operand_label_2) !=
  760. loop_latch_->id()) {
  761. return nullptr;
  762. }
  763. } else {
  764. return nullptr;
  765. }
  766. if (!FindNumberOfIterations(variable_inst, &branch_inst, nullptr))
  767. return nullptr;
  768. induction = variable_inst;
  769. }
  770. }
  771. return induction;
  772. }
  773. bool LoopDescriptor::CreatePreHeaderBlocksIfMissing() {
  774. auto modified = false;
  775. for (auto& loop : *this) {
  776. if (!loop.GetPreHeaderBlock()) {
  777. modified = true;
  778. // TODO(1841): Handle failure to create pre-header.
  779. loop.GetOrCreatePreHeaderBlock();
  780. }
  781. }
  782. return modified;
  783. }
  784. // Add and remove loops which have been marked for addition and removal to
  785. // maintain the state of the loop descriptor class.
  786. void LoopDescriptor::PostModificationCleanup() {
  787. LoopContainerType loops_to_remove_;
  788. for (Loop* loop : loops_) {
  789. if (loop->IsMarkedForRemoval()) {
  790. loops_to_remove_.push_back(loop);
  791. if (loop->HasParent()) {
  792. loop->GetParent()->RemoveChildLoop(loop);
  793. }
  794. }
  795. }
  796. for (Loop* loop : loops_to_remove_) {
  797. loops_.erase(std::find(loops_.begin(), loops_.end(), loop));
  798. delete loop;
  799. }
  800. for (auto& pair : loops_to_add_) {
  801. Loop* parent = pair.first;
  802. std::unique_ptr<Loop> loop = std::move(pair.second);
  803. if (parent) {
  804. loop->SetParent(nullptr);
  805. parent->AddNestedLoop(loop.get());
  806. for (uint32_t block_id : loop->GetBlocks()) {
  807. parent->AddBasicBlock(block_id);
  808. }
  809. }
  810. loops_.emplace_back(loop.release());
  811. }
  812. loops_to_add_.clear();
  813. }
  814. void LoopDescriptor::ClearLoops() {
  815. for (Loop* loop : loops_) {
  816. delete loop;
  817. }
  818. loops_.clear();
  819. }
  820. // Adds a new loop nest to the descriptor set.
  821. Loop* LoopDescriptor::AddLoopNest(std::unique_ptr<Loop> new_loop) {
  822. Loop* loop = new_loop.release();
  823. if (!loop->HasParent()) placeholder_top_loop_.nested_loops_.push_back(loop);
  824. // Iterate from inner to outer most loop, adding basic block to loop mapping
  825. // as we go.
  826. for (Loop& current_loop :
  827. make_range(iterator::begin(loop), iterator::end(nullptr))) {
  828. loops_.push_back(&current_loop);
  829. for (uint32_t bb_id : current_loop.GetBlocks())
  830. basic_block_to_loop_.insert(std::make_pair(bb_id, &current_loop));
  831. }
  832. return loop;
  833. }
  834. void LoopDescriptor::RemoveLoop(Loop* loop) {
  835. Loop* parent = loop->GetParent() ? loop->GetParent() : &placeholder_top_loop_;
  836. parent->nested_loops_.erase(std::find(parent->nested_loops_.begin(),
  837. parent->nested_loops_.end(), loop));
  838. std::for_each(
  839. loop->nested_loops_.begin(), loop->nested_loops_.end(),
  840. [loop](Loop* sub_loop) { sub_loop->SetParent(loop->GetParent()); });
  841. parent->nested_loops_.insert(parent->nested_loops_.end(),
  842. loop->nested_loops_.begin(),
  843. loop->nested_loops_.end());
  844. for (uint32_t bb_id : loop->GetBlocks()) {
  845. Loop* l = FindLoopForBasicBlock(bb_id);
  846. if (l == loop) {
  847. SetBasicBlockToLoop(bb_id, l->GetParent());
  848. } else {
  849. ForgetBasicBlock(bb_id);
  850. }
  851. }
  852. LoopContainerType::iterator it =
  853. std::find(loops_.begin(), loops_.end(), loop);
  854. assert(it != loops_.end());
  855. delete loop;
  856. loops_.erase(it);
  857. }
  858. } // namespace opt
  859. } // namespace spvtools