SSAContext.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //===- SSAContext.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 LLVM IR.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/IR/SSAContext.h"
  15. #include "llvm/IR/Argument.h"
  16. #include "llvm/IR/BasicBlock.h"
  17. #include "llvm/IR/Function.h"
  18. #include "llvm/IR/Instruction.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. Value *SSAContext::ValueRefNull = nullptr;
  23. void SSAContext::setFunction(Function &Fn) { F = &Fn; }
  24. BasicBlock *SSAContext::getEntryBlock(Function &F) {
  25. return &F.getEntryBlock();
  26. }
  27. const BasicBlock *SSAContext::getEntryBlock(const Function &F) {
  28. return &F.getEntryBlock();
  29. }
  30. void SSAContext::appendBlockDefs(SmallVectorImpl<Value *> &defs,
  31. BasicBlock &block) {
  32. for (auto &instr : block.instructionsWithoutDebug(/*SkipPseudoOp=*/true)) {
  33. if (instr.isTerminator())
  34. break;
  35. if (instr.getType()->isVoidTy())
  36. continue;
  37. auto *def = &instr;
  38. defs.push_back(def);
  39. }
  40. }
  41. void SSAContext::appendBlockDefs(SmallVectorImpl<const Value *> &defs,
  42. const BasicBlock &block) {
  43. for (auto &instr : block) {
  44. if (instr.isTerminator())
  45. break;
  46. defs.push_back(&instr);
  47. }
  48. }
  49. void SSAContext::appendBlockTerms(SmallVectorImpl<Instruction *> &terms,
  50. BasicBlock &block) {
  51. terms.push_back(block.getTerminator());
  52. }
  53. void SSAContext::appendBlockTerms(SmallVectorImpl<const Instruction *> &terms,
  54. const BasicBlock &block) {
  55. terms.push_back(block.getTerminator());
  56. }
  57. const BasicBlock *SSAContext::getDefBlock(const Value *value) const {
  58. if (const auto *instruction = dyn_cast<Instruction>(value))
  59. return instruction->getParent();
  60. return nullptr;
  61. }
  62. bool SSAContext::comesBefore(const Instruction *lhs, const Instruction *rhs) {
  63. return lhs->comesBefore(rhs);
  64. }
  65. bool SSAContext::isConstantValuePhi(const Instruction &Instr) {
  66. if (auto *Phi = dyn_cast<PHINode>(&Instr))
  67. return Phi->hasConstantValue();
  68. return false;
  69. }
  70. Printable SSAContext::print(const Value *V) const {
  71. return Printable([V](raw_ostream &Out) { V->print(Out); });
  72. }
  73. Printable SSAContext::print(const Instruction *Inst) const {
  74. return print(cast<Value>(Inst));
  75. }
  76. Printable SSAContext::print(const BasicBlock *BB) const {
  77. if (!BB)
  78. return Printable([](raw_ostream &Out) { Out << "<nullptr>"; });
  79. if (BB->hasName())
  80. return Printable([BB](raw_ostream &Out) { Out << BB->getName(); });
  81. return Printable([BB](raw_ostream &Out) {
  82. ModuleSlotTracker MST{BB->getParent()->getParent(), false};
  83. MST.incorporateFunction(*BB->getParent());
  84. Out << MST.getLocalSlot(BB);
  85. });
  86. }