CodeGenCommonISel.h 11 KB

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