SwiftErrorValueTracking.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SwiftErrorValueTracking.h - Track swifterror VReg vals --*- C++ -*--===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This implements a limited mem2reg-like analysis to promote uses of function
  15. // arguments and allocas marked with swiftalloc from memory into virtual
  16. // registers tracked by this class.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CODEGEN_SWIFTERRORVALUETRACKING_H
  20. #define LLVM_CODEGEN_SWIFTERRORVALUETRACKING_H
  21. #include "llvm/ADT/DenseMap.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/CodeGen/Register.h"
  24. #include "llvm/IR/BasicBlock.h"
  25. #include "llvm/IR/DebugLoc.h"
  26. #include <functional>
  27. #include <type_traits>
  28. #include <utility>
  29. namespace llvm {
  30. class Function;
  31. class MachineBasicBlock;
  32. class MachineFunction;
  33. class MachineInstr;
  34. class TargetInstrInfo;
  35. class TargetLowering;
  36. class SwiftErrorValueTracking {
  37. // Some useful objects to reduce the number of function arguments needed.
  38. MachineFunction *MF;
  39. const Function *Fn;
  40. const TargetLowering *TLI;
  41. const TargetInstrInfo *TII;
  42. /// A map from swifterror value in a basic block to the virtual register it is
  43. /// currently represented by.
  44. DenseMap<std::pair<const MachineBasicBlock *, const Value *>, Register>
  45. VRegDefMap;
  46. /// A list of upward exposed vreg uses that need to be satisfied by either a
  47. /// copy def or a phi node at the beginning of the basic block representing
  48. /// the predecessor(s) swifterror value.
  49. DenseMap<std::pair<const MachineBasicBlock *, const Value *>, Register>
  50. VRegUpwardsUse;
  51. /// A map from instructions that define/use a swifterror value to the virtual
  52. /// register that represents that def/use.
  53. llvm::DenseMap<PointerIntPair<const Instruction *, 1, bool>, Register>
  54. VRegDefUses;
  55. /// The swifterror argument of the current function.
  56. const Value *SwiftErrorArg;
  57. using SwiftErrorValues = SmallVector<const Value*, 1>;
  58. /// A function can only have a single swifterror argument. And if it does
  59. /// have a swifterror argument, it must be the first entry in
  60. /// SwiftErrorVals.
  61. SwiftErrorValues SwiftErrorVals;
  62. public:
  63. /// Initialize data structures for specified new function.
  64. void setFunction(MachineFunction &MF);
  65. /// Get the (unique) function argument that was marked swifterror, or nullptr
  66. /// if this function has no swifterror args.
  67. const Value *getFunctionArg() const {
  68. return SwiftErrorArg;
  69. }
  70. /// Get or create the swifterror value virtual register in
  71. /// VRegDefMap for this basic block.
  72. Register getOrCreateVReg(const MachineBasicBlock *, const Value *);
  73. /// Set the swifterror virtual register in the VRegDefMap for this
  74. /// basic block.
  75. void setCurrentVReg(const MachineBasicBlock *MBB, const Value *, Register);
  76. /// Get or create the swifterror value virtual register for a def of a
  77. /// swifterror by an instruction.
  78. Register getOrCreateVRegDefAt(const Instruction *, const MachineBasicBlock *,
  79. const Value *);
  80. /// Get or create the swifterror value virtual register for a use of a
  81. /// swifterror by an instruction.
  82. Register getOrCreateVRegUseAt(const Instruction *, const MachineBasicBlock *,
  83. const Value *);
  84. /// Create initial definitions of swifterror values in the entry block of the
  85. /// current function.
  86. bool createEntriesInEntryBlock(DebugLoc DbgLoc);
  87. /// Propagate assigned swifterror vregs through a function, synthesizing PHI
  88. /// nodes when needed to maintain consistency.
  89. void propagateVRegs();
  90. void preassignVRegs(MachineBasicBlock *MBB, BasicBlock::const_iterator Begin,
  91. BasicBlock::const_iterator End);
  92. };
  93. }
  94. #endif
  95. #ifdef __GNUC__
  96. #pragma GCC diagnostic pop
  97. #endif