2
0

basic_block.cpp 7.8 KB

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