SpirvBasicBlock.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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) {}
  16. bool SpirvBasicBlock::hasTerminator() const {
  17. return !instructions.empty() &&
  18. isa<SpirvTerminator>(instructions.back().instruction);
  19. }
  20. bool SpirvBasicBlock::invokeVisitor(Visitor *visitor,
  21. llvm::ArrayRef<SpirvVariable *> vars,
  22. bool reverseOrder) {
  23. if (!visitor->visit(this, Visitor::Phase::Init))
  24. return false;
  25. if (reverseOrder) {
  26. for (auto iter = instructions.rbegin(); iter != instructions.rend();
  27. ++iter) {
  28. if (!iter->instruction->invokeVisitor(visitor))
  29. return false;
  30. }
  31. // If a basic block is the first basic block of a function, it should
  32. // include all the variables of the function.
  33. if (!vars.empty())
  34. for (auto var = vars.rbegin(); var != vars.rend(); ++var)
  35. if (!(*var)->invokeVisitor(visitor))
  36. return false;
  37. } else {
  38. // If a basic block is the first basic block of a function, it should
  39. // include all the variables of the function.
  40. if (!vars.empty())
  41. for (auto *var : vars)
  42. if (!var->invokeVisitor(visitor))
  43. return false;
  44. for (auto iter = instructions.begin(); iter != instructions.end(); ++iter) {
  45. if (!iter->instruction->invokeVisitor(visitor))
  46. return false;
  47. }
  48. }
  49. if (!visitor->visit(this, Visitor::Phase::Done))
  50. return false;
  51. return true;
  52. }
  53. void SpirvBasicBlock::addSuccessor(SpirvBasicBlock *bb) {
  54. assert(bb && "cannot add null basic block as successor");
  55. successors.push_back(bb);
  56. }
  57. } // end namespace spirv
  58. } // end namespace clang