basic_block.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright (c) 2016 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/basic_block.h"
  15. #include <ostream>
  16. #include "source/opt/function.h"
  17. #include "source/opt/ir_context.h"
  18. #include "source/opt/module.h"
  19. #include "source/opt/reflect.h"
  20. #include "source/util/make_unique.h"
  21. namespace spvtools {
  22. namespace opt {
  23. namespace {
  24. const uint32_t kLoopMergeContinueBlockIdInIdx = 1;
  25. const uint32_t kLoopMergeMergeBlockIdInIdx = 0;
  26. const uint32_t kSelectionMergeMergeBlockIdInIdx = 0;
  27. } // namespace
  28. BasicBlock* BasicBlock::Clone(IRContext* context) const {
  29. BasicBlock* clone = new BasicBlock(
  30. std::unique_ptr<Instruction>(GetLabelInst()->Clone(context)));
  31. for (const auto& inst : insts_)
  32. // Use the incoming context
  33. clone->AddInstruction(std::unique_ptr<Instruction>(inst.Clone(context)));
  34. return clone;
  35. }
  36. const Instruction* BasicBlock::GetMergeInst() const {
  37. const Instruction* result = nullptr;
  38. // If it exists, the merge instruction immediately precedes the
  39. // terminator.
  40. auto iter = ctail();
  41. if (iter != cbegin()) {
  42. --iter;
  43. const auto opcode = iter->opcode();
  44. if (opcode == SpvOpLoopMerge || opcode == SpvOpSelectionMerge) {
  45. result = &*iter;
  46. }
  47. }
  48. return result;
  49. }
  50. Instruction* BasicBlock::GetMergeInst() {
  51. Instruction* result = nullptr;
  52. // If it exists, the merge instruction immediately precedes the
  53. // terminator.
  54. auto iter = tail();
  55. if (iter != begin()) {
  56. --iter;
  57. const auto opcode = iter->opcode();
  58. if (opcode == SpvOpLoopMerge || opcode == SpvOpSelectionMerge) {
  59. result = &*iter;
  60. }
  61. }
  62. return result;
  63. }
  64. const Instruction* BasicBlock::GetLoopMergeInst() const {
  65. if (auto* merge = GetMergeInst()) {
  66. if (merge->opcode() == SpvOpLoopMerge) {
  67. return merge;
  68. }
  69. }
  70. return nullptr;
  71. }
  72. Instruction* BasicBlock::GetLoopMergeInst() {
  73. if (auto* merge = GetMergeInst()) {
  74. if (merge->opcode() == SpvOpLoopMerge) {
  75. return merge;
  76. }
  77. }
  78. return nullptr;
  79. }
  80. void BasicBlock::KillAllInsts(bool killLabel) {
  81. ForEachInst([killLabel](Instruction* ip) {
  82. if (killLabel || ip->opcode() != SpvOpLabel) {
  83. ip->context()->KillInst(ip);
  84. }
  85. });
  86. }
  87. void BasicBlock::ForEachSuccessorLabel(
  88. const std::function<void(const uint32_t)>& f) const {
  89. const auto br = &insts_.back();
  90. switch (br->opcode()) {
  91. case SpvOpBranch: {
  92. f(br->GetOperand(0).words[0]);
  93. } break;
  94. case SpvOpBranchConditional:
  95. case SpvOpSwitch: {
  96. bool is_first = true;
  97. br->ForEachInId([&is_first, &f](const uint32_t* idp) {
  98. if (!is_first) f(*idp);
  99. is_first = false;
  100. });
  101. } break;
  102. default:
  103. break;
  104. }
  105. }
  106. void BasicBlock::ForEachSuccessorLabel(
  107. const std::function<void(uint32_t*)>& f) {
  108. auto br = &insts_.back();
  109. switch (br->opcode()) {
  110. case SpvOpBranch: {
  111. uint32_t tmp_id = br->GetOperand(0).words[0];
  112. f(&tmp_id);
  113. if (tmp_id != br->GetOperand(0).words[0]) br->SetOperand(0, {tmp_id});
  114. } break;
  115. case SpvOpBranchConditional:
  116. case SpvOpSwitch: {
  117. bool is_first = true;
  118. br->ForEachInId([&is_first, &f](uint32_t* idp) {
  119. if (!is_first) f(idp);
  120. is_first = false;
  121. });
  122. } break;
  123. default:
  124. break;
  125. }
  126. }
  127. bool BasicBlock::IsSuccessor(const BasicBlock* block) const {
  128. uint32_t succId = block->id();
  129. bool isSuccessor = false;
  130. ForEachSuccessorLabel([&isSuccessor, succId](const uint32_t label) {
  131. if (label == succId) isSuccessor = true;
  132. });
  133. return isSuccessor;
  134. }
  135. void BasicBlock::ForMergeAndContinueLabel(
  136. const std::function<void(const uint32_t)>& f) {
  137. auto ii = insts_.end();
  138. --ii;
  139. if (ii == insts_.begin()) return;
  140. --ii;
  141. if (ii->opcode() == SpvOpSelectionMerge || ii->opcode() == SpvOpLoopMerge) {
  142. ii->ForEachInId([&f](const uint32_t* idp) { f(*idp); });
  143. }
  144. }
  145. uint32_t BasicBlock::MergeBlockIdIfAny() const {
  146. auto merge_ii = cend();
  147. --merge_ii;
  148. uint32_t mbid = 0;
  149. if (merge_ii != cbegin()) {
  150. --merge_ii;
  151. if (merge_ii->opcode() == SpvOpLoopMerge) {
  152. mbid = merge_ii->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx);
  153. } else if (merge_ii->opcode() == SpvOpSelectionMerge) {
  154. mbid = merge_ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  155. }
  156. }
  157. return mbid;
  158. }
  159. uint32_t BasicBlock::ContinueBlockIdIfAny() const {
  160. auto merge_ii = cend();
  161. --merge_ii;
  162. uint32_t cbid = 0;
  163. if (merge_ii != cbegin()) {
  164. --merge_ii;
  165. if (merge_ii->opcode() == SpvOpLoopMerge) {
  166. cbid = merge_ii->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
  167. }
  168. }
  169. return cbid;
  170. }
  171. std::ostream& operator<<(std::ostream& str, const BasicBlock& block) {
  172. str << block.PrettyPrint();
  173. return str;
  174. }
  175. std::string BasicBlock::PrettyPrint(uint32_t options) const {
  176. std::ostringstream str;
  177. ForEachInst([&str, options](const Instruction* inst) {
  178. str << inst->PrettyPrint(options);
  179. if (!IsTerminatorInst(inst->opcode())) {
  180. str << std::endl;
  181. }
  182. });
  183. return str.str();
  184. }
  185. BasicBlock* BasicBlock::SplitBasicBlock(IRContext* context, uint32_t label_id,
  186. iterator iter) {
  187. assert(!insts_.empty());
  188. BasicBlock* new_block = new BasicBlock(MakeUnique<Instruction>(
  189. context, SpvOpLabel, 0, label_id, std::initializer_list<Operand>{}));
  190. new_block->insts_.Splice(new_block->end(), &insts_, iter, end());
  191. new_block->SetParent(GetParent());
  192. context->AnalyzeDefUse(new_block->GetLabelInst());
  193. // Update the phi nodes in the successor blocks to reference the new block id.
  194. const_cast<const BasicBlock*>(new_block)->ForEachSuccessorLabel(
  195. [new_block, this, context](const uint32_t label) {
  196. BasicBlock* target_bb = context->get_instr_block(label);
  197. target_bb->ForEachPhiInst(
  198. [this, new_block, context](Instruction* phi_inst) {
  199. bool changed = false;
  200. for (uint32_t i = 1; i < phi_inst->NumInOperands(); i += 2) {
  201. if (phi_inst->GetSingleWordInOperand(i) == this->id()) {
  202. changed = true;
  203. phi_inst->SetInOperand(i, {new_block->id()});
  204. }
  205. }
  206. if (changed) {
  207. context->UpdateDefUse(phi_inst);
  208. }
  209. });
  210. });
  211. if (context->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
  212. context->set_instr_block(new_block->GetLabelInst(), new_block);
  213. new_block->ForEachInst([new_block, context](Instruction* inst) {
  214. context->set_instr_block(inst, new_block);
  215. });
  216. }
  217. return new_block;
  218. }
  219. } // namespace opt
  220. } // namespace spvtools