SwiftErrorValueTracking.h 3.9 KB

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