MachineSSAContext.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //===- MachineSSAContext.cpp ------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. /// \file
  9. ///
  10. /// This file defines a specialization of the GenericSSAContext<X>
  11. /// template class for Machine IR.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/CodeGen/MachineSSAContext.h"
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. #include "llvm/CodeGen/MachineFunction.h"
  17. #include "llvm/CodeGen/MachineInstr.h"
  18. #include "llvm/CodeGen/MachineRegisterInfo.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. const Register MachineSSAContext::ValueRefNull{};
  22. void MachineSSAContext::setFunction(MachineFunction &Fn) {
  23. MF = &Fn;
  24. RegInfo = &MF->getRegInfo();
  25. }
  26. MachineBasicBlock *MachineSSAContext::getEntryBlock(MachineFunction &F) {
  27. return &F.front();
  28. }
  29. void MachineSSAContext::appendBlockTerms(
  30. SmallVectorImpl<const MachineInstr *> &terms,
  31. const MachineBasicBlock &block) {
  32. for (auto &T : block.terminators())
  33. terms.push_back(&T);
  34. }
  35. void MachineSSAContext::appendBlockDefs(SmallVectorImpl<Register> &defs,
  36. const MachineBasicBlock &block) {
  37. for (const MachineInstr &instr : block.instrs()) {
  38. for (const MachineOperand &op : instr.operands()) {
  39. if (op.isReg() && op.isDef())
  40. defs.push_back(op.getReg());
  41. }
  42. }
  43. }
  44. /// Get the defining block of a value.
  45. MachineBasicBlock *MachineSSAContext::getDefBlock(Register value) const {
  46. if (!value)
  47. return nullptr;
  48. return RegInfo->getVRegDef(value)->getParent();
  49. }
  50. bool MachineSSAContext::isConstantValuePhi(const MachineInstr &Phi) {
  51. return Phi.isConstantValuePHI();
  52. }
  53. Printable MachineSSAContext::print(const MachineBasicBlock *Block) const {
  54. if (!Block)
  55. return Printable([](raw_ostream &Out) { Out << "<nullptr>"; });
  56. return Printable([Block](raw_ostream &Out) { Block->printName(Out); });
  57. }
  58. Printable MachineSSAContext::print(const MachineInstr *I) const {
  59. return Printable([I](raw_ostream &Out) { I->print(Out); });
  60. }
  61. Printable MachineSSAContext::print(Register Value) const {
  62. auto *MRI = RegInfo;
  63. return Printable([MRI, Value](raw_ostream &Out) {
  64. Out << printReg(Value, MRI->getTargetRegisterInfo(), 0, MRI);
  65. if (Value) {
  66. // Try to print the definition.
  67. if (auto *Instr = MRI->getUniqueVRegDef(Value)) {
  68. Out << ": ";
  69. Instr->print(Out);
  70. }
  71. }
  72. });
  73. }