SpirvBasicBlock.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //===--- SpirvBasicBlock.cpp - SPIR-V Basic Block Implementation -*- C++ -*-==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "clang/SPIRV/SpirvBasicBlock.h"
  10. #include "clang/SPIRV/SpirvVisitor.h"
  11. namespace clang {
  12. namespace spirv {
  13. SpirvBasicBlock::SpirvBasicBlock(llvm::StringRef name)
  14. : labelId(0), labelName(name), mergeTarget(nullptr),
  15. continueTarget(nullptr), debugScope(nullptr) {}
  16. SpirvBasicBlock::~SpirvBasicBlock() {
  17. for (auto instructionNode : instructions)
  18. instructionNode.instruction->releaseMemory();
  19. if (debugScope)
  20. debugScope->releaseMemory();
  21. }
  22. bool SpirvBasicBlock::hasTerminator() const {
  23. return !instructions.empty() &&
  24. isa<SpirvTerminator>(instructions.back().instruction);
  25. }
  26. bool SpirvBasicBlock::invokeVisitor(Visitor *visitor,
  27. llvm::ArrayRef<SpirvVariable *> vars,
  28. bool reverseOrder) {
  29. if (!visitor->visit(this, Visitor::Phase::Init))
  30. return false;
  31. if (debugScope && !visitor->visit(debugScope))
  32. return false;
  33. if (reverseOrder) {
  34. for (auto iter = instructions.rbegin(); iter != instructions.rend();
  35. ++iter) {
  36. if (!iter->instruction->invokeVisitor(visitor))
  37. return false;
  38. }
  39. // If a basic block is the first basic block of a function, it should
  40. // include all the variables of the function.
  41. if (!vars.empty()) {
  42. for (auto var = vars.rbegin(); var != vars.rend(); ++var) {
  43. if (!(*var)->invokeVisitor(visitor))
  44. return false;
  45. }
  46. }
  47. } else {
  48. // If a basic block is the first basic block of a function, it should
  49. // include all the variables of the function.
  50. if (!vars.empty()) {
  51. for (auto *var : vars) {
  52. if (!var->invokeVisitor(visitor))
  53. return false;
  54. }
  55. }
  56. for (auto iter = instructions.begin(); iter != instructions.end(); ++iter) {
  57. if (!iter->instruction->invokeVisitor(visitor))
  58. return false;
  59. }
  60. }
  61. if (!visitor->visit(this, Visitor::Phase::Done))
  62. return false;
  63. return true;
  64. }
  65. void SpirvBasicBlock::addSuccessor(SpirvBasicBlock *bb) {
  66. assert(bb && "cannot add null basic block as successor");
  67. successors.push_back(bb);
  68. }
  69. } // end namespace spirv
  70. } // end namespace clang