CodeGenCommonISel.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CodeGenCommonISel.h - Common code between ISels ---------*- 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 file declares common utilities that are shared between SelectionDAG and
  15. // GlobalISel frameworks.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CODEGEN_CODEGENCOMMONISEL_H
  19. #define LLVM_CODEGEN_CODEGENCOMMONISEL_H
  20. #include "llvm/CodeGen/MachineBasicBlock.h"
  21. #include <cassert>
  22. namespace llvm {
  23. class BasicBlock;
  24. class MachineBasicBlock;
  25. /// Encapsulates all of the information needed to generate a stack protector
  26. /// check, and signals to isel when initialized that one needs to be generated.
  27. ///
  28. /// *NOTE* The following is a high level documentation of SelectionDAG Stack
  29. /// Protector Generation. This is now also ported be shared with GlobalISel,
  30. /// but without any significant changes.
  31. ///
  32. /// High Level Overview of ISel Stack Protector Generation:
  33. ///
  34. /// Previously, the "stack protector" IR pass handled stack protector
  35. /// generation. This necessitated splitting basic blocks at the IR level to
  36. /// create the success/failure basic blocks in the tail of the basic block in
  37. /// question. As a result of this, calls that would have qualified for the
  38. /// sibling call optimization were no longer eligible for optimization since
  39. /// said calls were no longer right in the "tail position" (i.e. the immediate
  40. /// predecessor of a ReturnInst instruction).
  41. ///
  42. /// Since the sibling call optimization causes the callee to reuse the caller's
  43. /// stack, if we could delay the generation of the stack protector check until
  44. /// later in CodeGen after the sibling call decision was made, we get both the
  45. /// tail call optimization and the stack protector check!
  46. ///
  47. /// A few goals in solving this problem were:
  48. ///
  49. /// 1. Preserve the architecture independence of stack protector generation.
  50. ///
  51. /// 2. Preserve the normal IR level stack protector check for platforms like
  52. /// OpenBSD for which we support platform-specific stack protector
  53. /// generation.
  54. ///
  55. /// The main problem that guided the present solution is that one can not
  56. /// solve this problem in an architecture independent manner at the IR level
  57. /// only. This is because:
  58. ///
  59. /// 1. The decision on whether or not to perform a sibling call on certain
  60. /// platforms (for instance i386) requires lower level information
  61. /// related to available registers that can not be known at the IR level.
  62. ///
  63. /// 2. Even if the previous point were not true, the decision on whether to
  64. /// perform a tail call is done in LowerCallTo in SelectionDAG (or
  65. /// CallLowering in GlobalISel) which occurs after the Stack Protector
  66. /// Pass. As a result, one would need to put the relevant callinst into the
  67. /// stack protector check success basic block (where the return inst is
  68. /// placed) and then move it back later at ISel/MI time before the
  69. /// stack protector check if the tail call optimization failed. The MI
  70. /// level option was nixed immediately since it would require
  71. /// platform-specific pattern matching. The ISel level option was
  72. /// nixed because SelectionDAG only processes one IR level basic block at a
  73. /// time implying one could not create a DAG Combine to move the callinst.
  74. ///
  75. /// To get around this problem:
  76. ///
  77. /// 1. SelectionDAG can only process one block at a time, we can generate
  78. /// multiple machine basic blocks for one IR level basic block.
  79. /// This is how we handle bit tests and switches.
  80. ///
  81. /// 2. At the MI level, tail calls are represented via a special return
  82. /// MIInst called "tcreturn". Thus if we know the basic block in which we
  83. /// wish to insert the stack protector check, we get the correct behavior
  84. /// by always inserting the stack protector check right before the return
  85. /// statement. This is a "magical transformation" since no matter where
  86. /// the stack protector check intrinsic is, we always insert the stack
  87. /// protector check code at the end of the BB.
  88. ///
  89. /// Given the aforementioned constraints, the following solution was devised:
  90. ///
  91. /// 1. On platforms that do not support ISel stack protector check
  92. /// generation, allow for the normal IR level stack protector check
  93. /// generation to continue.
  94. ///
  95. /// 2. On platforms that do support ISel stack protector check
  96. /// generation:
  97. ///
  98. /// a. Use the IR level stack protector pass to decide if a stack
  99. /// protector is required/which BB we insert the stack protector check
  100. /// in by reusing the logic already therein.
  101. ///
  102. /// b. After we finish selecting the basic block, we produce the validation
  103. /// code with one of these techniques:
  104. /// 1) with a call to a guard check function
  105. /// 2) with inlined instrumentation
  106. ///
  107. /// 1) We insert a call to the check function before the terminator.
  108. ///
  109. /// 2) We first find a splice point in the parent basic block
  110. /// before the terminator and then splice the terminator of said basic
  111. /// block into the success basic block. Then we code-gen a new tail for
  112. /// the parent basic block consisting of the two loads, the comparison,
  113. /// and finally two branches to the success/failure basic blocks. We
  114. /// conclude by code-gening the failure basic block if we have not
  115. /// code-gened it already (all stack protector checks we generate in
  116. /// the same function, use the same failure basic block).
  117. class StackProtectorDescriptor {
  118. public:
  119. StackProtectorDescriptor() = default;
  120. /// Returns true if all fields of the stack protector descriptor are
  121. /// initialized implying that we should/are ready to emit a stack protector.
  122. bool shouldEmitStackProtector() const {
  123. return ParentMBB && SuccessMBB && FailureMBB;
  124. }
  125. bool shouldEmitFunctionBasedCheckStackProtector() const {
  126. return ParentMBB && !SuccessMBB && !FailureMBB;
  127. }
  128. /// Initialize the stack protector descriptor structure for a new basic
  129. /// block.
  130. void initialize(const BasicBlock *BB, MachineBasicBlock *MBB,
  131. bool FunctionBasedInstrumentation) {
  132. // Make sure we are not initialized yet.
  133. assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is "
  134. "already initialized!");
  135. ParentMBB = MBB;
  136. if (!FunctionBasedInstrumentation) {
  137. SuccessMBB = addSuccessorMBB(BB, MBB, /* IsLikely */ true);
  138. FailureMBB = addSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB);
  139. }
  140. }
  141. /// Reset state that changes when we handle different basic blocks.
  142. ///
  143. /// This currently includes:
  144. ///
  145. /// 1. The specific basic block we are generating a
  146. /// stack protector for (ParentMBB).
  147. ///
  148. /// 2. The successor machine basic block that will contain the tail of
  149. /// parent mbb after we create the stack protector check (SuccessMBB). This
  150. /// BB is visited only on stack protector check success.
  151. void resetPerBBState() {
  152. ParentMBB = nullptr;
  153. SuccessMBB = nullptr;
  154. }
  155. /// Reset state that only changes when we switch functions.
  156. ///
  157. /// This currently includes:
  158. ///
  159. /// 1. FailureMBB since we reuse the failure code path for all stack
  160. /// protector checks created in an individual function.
  161. ///
  162. /// 2.The guard variable since the guard variable we are checking against is
  163. /// always the same.
  164. void resetPerFunctionState() { FailureMBB = nullptr; }
  165. MachineBasicBlock *getParentMBB() { return ParentMBB; }
  166. MachineBasicBlock *getSuccessMBB() { return SuccessMBB; }
  167. MachineBasicBlock *getFailureMBB() { return FailureMBB; }
  168. private:
  169. /// The basic block for which we are generating the stack protector.
  170. ///
  171. /// As a result of stack protector generation, we will splice the
  172. /// terminators of this basic block into the successor mbb SuccessMBB and
  173. /// replace it with a compare/branch to the successor mbbs
  174. /// SuccessMBB/FailureMBB depending on whether or not the stack protector
  175. /// was violated.
  176. MachineBasicBlock *ParentMBB = nullptr;
  177. /// A basic block visited on stack protector check success that contains the
  178. /// terminators of ParentMBB.
  179. MachineBasicBlock *SuccessMBB = nullptr;
  180. /// This basic block visited on stack protector check failure that will
  181. /// contain a call to __stack_chk_fail().
  182. MachineBasicBlock *FailureMBB = nullptr;
  183. /// Add a successor machine basic block to ParentMBB. If the successor mbb
  184. /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic
  185. /// block will be created. Assign a large weight if IsLikely is true.
  186. MachineBasicBlock *addSuccessorMBB(const BasicBlock *BB,
  187. MachineBasicBlock *ParentMBB,
  188. bool IsLikely,
  189. MachineBasicBlock *SuccMBB = nullptr);
  190. };
  191. /// Find the split point at which to splice the end of BB into its success stack
  192. /// protector check machine basic block.
  193. ///
  194. /// On many platforms, due to ABI constraints, terminators, even before register
  195. /// allocation, use physical registers. This creates an issue for us since
  196. /// physical registers at this point can not travel across basic
  197. /// blocks. Luckily, selectiondag always moves physical registers into vregs
  198. /// when they enter functions and moves them through a sequence of copies back
  199. /// into the physical registers right before the terminator creating a
  200. /// ``Terminator Sequence''. This function is searching for the beginning of the
  201. /// terminator sequence so that we can ensure that we splice off not just the
  202. /// terminator, but additionally the copies that move the vregs into the
  203. /// physical registers.
  204. MachineBasicBlock::iterator
  205. findSplitPointForStackProtector(MachineBasicBlock *BB,
  206. const TargetInstrInfo &TII);
  207. } // namespace llvm
  208. #endif // LLVM_CODEGEN_CODEGENCOMMONISEL_H
  209. #ifdef __GNUC__
  210. #pragma GCC diagnostic pop
  211. #endif