CFIFixup.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //===------ CFIFixup.cpp - Insert CFI remember/restore instructions -------===//
  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 pass inserts the necessary instructions to adjust for the inconsistency
  10. // of the call-frame information caused by final machine basic block layout.
  11. // The pass relies in constraints LLVM imposes on the placement of
  12. // save/restore points (cf. ShrinkWrap):
  13. // * there is a single basic block, containing the function prologue
  14. // * possibly multiple epilogue blocks, where each epilogue block is
  15. // complete and self-contained, i.e. CSR restore instructions (and the
  16. // corresponding CFI instructions are not split across two or more blocks.
  17. // * prologue and epilogue blocks are outside of any loops
  18. // Thus, during execution, at the beginning and at the end of each basic block
  19. // the function can be in one of two states:
  20. // - "has a call frame", if the function has executed the prologue, and
  21. // has not executed any epilogue
  22. // - "does not have a call frame", if the function has not executed the
  23. // prologue, or has executed an epilogue
  24. // which can be computed by a single RPO traversal.
  25. // In order to accommodate backends which do not generate unwind info in
  26. // epilogues we compute an additional property "strong no call frame on entry",
  27. // which is set for the entry point of the function and for every block
  28. // reachable from the entry along a path that does not execute the prologue. If
  29. // this property holds, it takes precedence over the "has a call frame"
  30. // property.
  31. // From the point of view of the unwind tables, the "has/does not have call
  32. // frame" state at beginning of each block is determined by the state at the end
  33. // of the previous block, in layout order. Where these states differ, we insert
  34. // compensating CFI instructions, which come in two flavours:
  35. // - CFI instructions, which reset the unwind table state to the initial one.
  36. // This is done by a target specific hook and is expected to be trivial
  37. // to implement, for example it could be:
  38. // .cfi_def_cfa <sp>, 0
  39. // .cfi_same_value <rN>
  40. // .cfi_same_value <rN-1>
  41. // ...
  42. // where <rN> are the callee-saved registers.
  43. // - CFI instructions, which reset the unwind table state to the one
  44. // created by the function prologue. These are
  45. // .cfi_restore_state
  46. // .cfi_remember_state
  47. // In this case we also insert a `.cfi_remember_state` after the last CFI
  48. // instruction in the function prologue.
  49. //
  50. // Known limitations:
  51. // * the pass cannot handle an epilogue preceding the prologue in the basic
  52. // block layout
  53. // * the pass does not handle functions where SP is used as a frame pointer and
  54. // SP adjustments up and down are done in different basic blocks (TODO)
  55. //===----------------------------------------------------------------------===//
  56. #include "llvm/CodeGen/CFIFixup.h"
  57. #include "llvm/ADT/PostOrderIterator.h"
  58. #include "llvm/ADT/SmallBitVector.h"
  59. #include "llvm/CodeGen/Passes.h"
  60. #include "llvm/CodeGen/TargetFrameLowering.h"
  61. #include "llvm/CodeGen/TargetInstrInfo.h"
  62. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  63. #include "llvm/MC/MCAsmInfo.h"
  64. #include "llvm/MC/MCDwarf.h"
  65. #include "llvm/Target/TargetMachine.h"
  66. using namespace llvm;
  67. #define DEBUG_TYPE "cfi-fixup"
  68. char CFIFixup::ID = 0;
  69. INITIALIZE_PASS(CFIFixup, "cfi-fixup",
  70. "Insert CFI remember/restore state instructions", false, false)
  71. FunctionPass *llvm::createCFIFixup() { return new CFIFixup(); }
  72. static bool isPrologueCFIInstruction(const MachineInstr &MI) {
  73. return MI.getOpcode() == TargetOpcode::CFI_INSTRUCTION &&
  74. MI.getFlag(MachineInstr::FrameSetup);
  75. }
  76. static bool containsPrologue(const MachineBasicBlock &MBB) {
  77. return llvm::any_of(MBB.instrs(), isPrologueCFIInstruction);
  78. }
  79. static bool containsEpilogue(const MachineBasicBlock &MBB) {
  80. return llvm::any_of(llvm::reverse(MBB), [](const auto &MI) {
  81. return MI.getOpcode() == TargetOpcode::CFI_INSTRUCTION &&
  82. MI.getFlag(MachineInstr::FrameDestroy);
  83. });
  84. }
  85. bool CFIFixup::runOnMachineFunction(MachineFunction &MF) {
  86. const TargetFrameLowering &TFL = *MF.getSubtarget().getFrameLowering();
  87. if (!TFL.enableCFIFixup(MF))
  88. return false;
  89. const unsigned NumBlocks = MF.getNumBlockIDs();
  90. if (NumBlocks < 2)
  91. return false;
  92. struct BlockFlags {
  93. bool Reachable : 1;
  94. bool StrongNoFrameOnEntry : 1;
  95. bool HasFrameOnEntry : 1;
  96. bool HasFrameOnExit : 1;
  97. };
  98. SmallVector<BlockFlags, 32> BlockInfo(NumBlocks, {false, false, false, false});
  99. BlockInfo[0].Reachable = true;
  100. BlockInfo[0].StrongNoFrameOnEntry = true;
  101. // Compute the presence/absence of frame at each basic block.
  102. MachineBasicBlock *PrologueBlock = nullptr;
  103. ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
  104. for (MachineBasicBlock *MBB : RPOT) {
  105. BlockFlags &Info = BlockInfo[MBB->getNumber()];
  106. // Set to true if the current block contains the prologue or the epilogue,
  107. // respectively.
  108. bool HasPrologue = false;
  109. bool HasEpilogue = false;
  110. if (!PrologueBlock && !Info.HasFrameOnEntry && containsPrologue(*MBB)) {
  111. PrologueBlock = MBB;
  112. HasPrologue = true;
  113. }
  114. if (Info.HasFrameOnEntry || HasPrologue)
  115. HasEpilogue = containsEpilogue(*MBB);
  116. // If the function has a call frame at the entry of the current block or the
  117. // current block contains the prologue, then the function has a call frame
  118. // at the exit of the block, unless the block contains the epilogue.
  119. Info.HasFrameOnExit = (Info.HasFrameOnEntry || HasPrologue) && !HasEpilogue;
  120. // Set the successors' state on entry.
  121. for (MachineBasicBlock *Succ : MBB->successors()) {
  122. BlockFlags &SuccInfo = BlockInfo[Succ->getNumber()];
  123. SuccInfo.Reachable = true;
  124. SuccInfo.StrongNoFrameOnEntry |=
  125. Info.StrongNoFrameOnEntry && !HasPrologue;
  126. SuccInfo.HasFrameOnEntry = Info.HasFrameOnExit;
  127. }
  128. }
  129. if (!PrologueBlock)
  130. return false;
  131. // Walk the blocks of the function in "physical" order.
  132. // Every block inherits the frame state (as recorded in the unwind tables)
  133. // of the previous block. If the intended frame state is different, insert
  134. // compensating CFI instructions.
  135. const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
  136. bool Change = false;
  137. // `InsertPt` always points to the point in a preceding block where we have to
  138. // insert a `.cfi_remember_state`, in the case that the current block needs a
  139. // `.cfi_restore_state`.
  140. MachineBasicBlock *InsertMBB = PrologueBlock;
  141. MachineBasicBlock::iterator InsertPt = PrologueBlock->begin();
  142. for (MachineInstr &MI : *PrologueBlock)
  143. if (isPrologueCFIInstruction(MI))
  144. InsertPt = std::next(MI.getIterator());
  145. assert(InsertPt != PrologueBlock->begin() &&
  146. "Inconsistent notion of \"prologue block\"");
  147. // No point starting before the prologue block.
  148. // TODO: the unwind tables will still be incorrect if an epilogue physically
  149. // preceeds the prologue.
  150. MachineFunction::iterator CurrBB = std::next(PrologueBlock->getIterator());
  151. bool HasFrame = BlockInfo[PrologueBlock->getNumber()].HasFrameOnExit;
  152. while (CurrBB != MF.end()) {
  153. const BlockFlags &Info = BlockInfo[CurrBB->getNumber()];
  154. if (!Info.Reachable) {
  155. ++CurrBB;
  156. continue;
  157. }
  158. #ifndef NDEBUG
  159. if (!Info.StrongNoFrameOnEntry) {
  160. for (auto *Pred : CurrBB->predecessors()) {
  161. BlockFlags &PredInfo = BlockInfo[Pred->getNumber()];
  162. assert((!PredInfo.Reachable ||
  163. Info.HasFrameOnEntry == PredInfo.HasFrameOnExit) &&
  164. "Inconsistent call frame state");
  165. }
  166. }
  167. #endif
  168. if (!Info.StrongNoFrameOnEntry && Info.HasFrameOnEntry && !HasFrame) {
  169. // Reset to the "after prologue" state.
  170. // Insert a `.cfi_remember_state` into the last block known to have a
  171. // stack frame.
  172. unsigned CFIIndex =
  173. MF.addFrameInst(MCCFIInstruction::createRememberState(nullptr));
  174. BuildMI(*InsertMBB, InsertPt, DebugLoc(),
  175. TII.get(TargetOpcode::CFI_INSTRUCTION))
  176. .addCFIIndex(CFIIndex);
  177. // Insert a `.cfi_restore_state` at the beginning of the current block.
  178. CFIIndex = MF.addFrameInst(MCCFIInstruction::createRestoreState(nullptr));
  179. InsertPt = BuildMI(*CurrBB, CurrBB->begin(), DebugLoc(),
  180. TII.get(TargetOpcode::CFI_INSTRUCTION))
  181. .addCFIIndex(CFIIndex);
  182. ++InsertPt;
  183. InsertMBB = &*CurrBB;
  184. Change = true;
  185. } else if ((Info.StrongNoFrameOnEntry || !Info.HasFrameOnEntry) &&
  186. HasFrame) {
  187. // Reset to the state upon function entry.
  188. TFL.resetCFIToInitialState(*CurrBB);
  189. Change = true;
  190. }
  191. HasFrame = Info.HasFrameOnExit;
  192. ++CurrBB;
  193. }
  194. return Change;
  195. }