basic_block.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. WhileEachSuccessorLabel([f](const uint32_t l) {
  97. f(l);
  98. return true;
  99. });
  100. }
  101. bool BasicBlock::WhileEachSuccessorLabel(
  102. const std::function<bool(const uint32_t)>& f) const {
  103. const auto br = &insts_.back();
  104. switch (br->opcode()) {
  105. case SpvOpBranch:
  106. return f(br->GetOperand(0).words[0]);
  107. case SpvOpBranchConditional:
  108. case SpvOpSwitch: {
  109. bool is_first = true;
  110. return br->WhileEachInId([&is_first, &f](const uint32_t* idp) {
  111. if (!is_first) return f(*idp);
  112. is_first = false;
  113. return true;
  114. });
  115. }
  116. default:
  117. return true;
  118. }
  119. }
  120. void BasicBlock::ForEachSuccessorLabel(
  121. const std::function<void(uint32_t*)>& f) {
  122. auto br = &insts_.back();
  123. switch (br->opcode()) {
  124. case SpvOpBranch: {
  125. uint32_t tmp_id = br->GetOperand(0).words[0];
  126. f(&tmp_id);
  127. if (tmp_id != br->GetOperand(0).words[0]) br->SetOperand(0, {tmp_id});
  128. } break;
  129. case SpvOpBranchConditional:
  130. case SpvOpSwitch: {
  131. bool is_first = true;
  132. br->ForEachInId([&is_first, &f](uint32_t* idp) {
  133. if (!is_first) f(idp);
  134. is_first = false;
  135. });
  136. } break;
  137. default:
  138. break;
  139. }
  140. }
  141. bool BasicBlock::IsSuccessor(const BasicBlock* block) const {
  142. uint32_t succId = block->id();
  143. bool isSuccessor = false;
  144. ForEachSuccessorLabel([&isSuccessor, succId](const uint32_t label) {
  145. if (label == succId) isSuccessor = true;
  146. });
  147. return isSuccessor;
  148. }
  149. void BasicBlock::ForMergeAndContinueLabel(
  150. const std::function<void(const uint32_t)>& f) {
  151. auto ii = insts_.end();
  152. --ii;
  153. if (ii == insts_.begin()) return;
  154. --ii;
  155. if (ii->opcode() == SpvOpSelectionMerge || ii->opcode() == SpvOpLoopMerge) {
  156. ii->ForEachInId([&f](const uint32_t* idp) { f(*idp); });
  157. }
  158. }
  159. uint32_t BasicBlock::MergeBlockIdIfAny() const {
  160. auto merge_ii = cend();
  161. --merge_ii;
  162. uint32_t mbid = 0;
  163. if (merge_ii != cbegin()) {
  164. --merge_ii;
  165. if (merge_ii->opcode() == SpvOpLoopMerge) {
  166. mbid = merge_ii->GetSingleWordInOperand(kLoopMergeMergeBlockIdInIdx);
  167. } else if (merge_ii->opcode() == SpvOpSelectionMerge) {
  168. mbid = merge_ii->GetSingleWordInOperand(kSelectionMergeMergeBlockIdInIdx);
  169. }
  170. }
  171. return mbid;
  172. }
  173. uint32_t BasicBlock::MergeBlockId() const {
  174. uint32_t mbid = MergeBlockIdIfAny();
  175. assert(mbid && "Expected block to have a corresponding merge block");
  176. return mbid;
  177. }
  178. uint32_t BasicBlock::ContinueBlockIdIfAny() const {
  179. auto merge_ii = cend();
  180. --merge_ii;
  181. uint32_t cbid = 0;
  182. if (merge_ii != cbegin()) {
  183. --merge_ii;
  184. if (merge_ii->opcode() == SpvOpLoopMerge) {
  185. cbid = merge_ii->GetSingleWordInOperand(kLoopMergeContinueBlockIdInIdx);
  186. }
  187. }
  188. return cbid;
  189. }
  190. uint32_t BasicBlock::ContinueBlockId() const {
  191. uint32_t cbid = ContinueBlockIdIfAny();
  192. assert(cbid && "Expected block to have a corresponding continue target");
  193. return cbid;
  194. }
  195. std::ostream& operator<<(std::ostream& str, const BasicBlock& block) {
  196. str << block.PrettyPrint();
  197. return str;
  198. }
  199. void BasicBlock::Dump() const {
  200. std::cerr << "Basic block #" << id() << "\n" << *this << "\n ";
  201. }
  202. std::string BasicBlock::PrettyPrint(uint32_t options) const {
  203. std::ostringstream str;
  204. ForEachInst([&str, options](const Instruction* inst) {
  205. str << inst->PrettyPrint(options);
  206. if (!IsTerminatorInst(inst->opcode())) {
  207. str << std::endl;
  208. }
  209. });
  210. return str.str();
  211. }
  212. BasicBlock* BasicBlock::SplitBasicBlock(IRContext* context, uint32_t label_id,
  213. iterator iter) {
  214. assert(!insts_.empty());
  215. std::unique_ptr<BasicBlock> new_block_temp =
  216. MakeUnique<BasicBlock>(MakeUnique<Instruction>(
  217. context, SpvOpLabel, 0, label_id, std::initializer_list<Operand>{}));
  218. BasicBlock* new_block = new_block_temp.get();
  219. function_->InsertBasicBlockAfter(std::move(new_block_temp), this);
  220. new_block->insts_.Splice(new_block->end(), &insts_, iter, end());
  221. assert(new_block->GetParent() == GetParent() &&
  222. "The parent should already be set appropriately.");
  223. context->AnalyzeDefUse(new_block->GetLabelInst());
  224. // Update the phi nodes in the successor blocks to reference the new block id.
  225. const_cast<const BasicBlock*>(new_block)->ForEachSuccessorLabel(
  226. [new_block, this, context](const uint32_t label) {
  227. BasicBlock* target_bb = context->get_instr_block(label);
  228. target_bb->ForEachPhiInst(
  229. [this, new_block, context](Instruction* phi_inst) {
  230. bool changed = false;
  231. for (uint32_t i = 1; i < phi_inst->NumInOperands(); i += 2) {
  232. if (phi_inst->GetSingleWordInOperand(i) == this->id()) {
  233. changed = true;
  234. phi_inst->SetInOperand(i, {new_block->id()});
  235. }
  236. }
  237. if (changed) {
  238. context->UpdateDefUse(phi_inst);
  239. }
  240. });
  241. });
  242. if (context->AreAnalysesValid(IRContext::kAnalysisInstrToBlockMapping)) {
  243. context->set_instr_block(new_block->GetLabelInst(), new_block);
  244. new_block->ForEachInst([new_block, context](Instruction* inst) {
  245. context->set_instr_block(inst, new_block);
  246. });
  247. }
  248. return new_block;
  249. }
  250. } // namespace opt
  251. } // namespace spvtools