basic_block.cpp 8.2 KB

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