|
|
|||
File indexing completed on 2026-05-10 08:43:27
0001 //===- CodeGenCommonISel.h - Common code between ISels ---------*- C++ -*--===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 // 0009 // This file declares common utilities that are shared between SelectionDAG and 0010 // GlobalISel frameworks. 0011 // 0012 //===----------------------------------------------------------------------===// 0013 0014 #ifndef LLVM_CODEGEN_CODEGENCOMMONISEL_H 0015 #define LLVM_CODEGEN_CODEGENCOMMONISEL_H 0016 0017 #include "llvm/CodeGen/MachineBasicBlock.h" 0018 #include <cassert> 0019 namespace llvm { 0020 0021 class BasicBlock; 0022 enum FPClassTest : unsigned; 0023 0024 /// Encapsulates all of the information needed to generate a stack protector 0025 /// check, and signals to isel when initialized that one needs to be generated. 0026 /// 0027 /// *NOTE* The following is a high level documentation of SelectionDAG Stack 0028 /// Protector Generation. This is now also ported be shared with GlobalISel, 0029 /// but without any significant changes. 0030 /// 0031 /// High Level Overview of ISel Stack Protector Generation: 0032 /// 0033 /// Previously, the "stack protector" IR pass handled stack protector 0034 /// generation. This necessitated splitting basic blocks at the IR level to 0035 /// create the success/failure basic blocks in the tail of the basic block in 0036 /// question. As a result of this, calls that would have qualified for the 0037 /// sibling call optimization were no longer eligible for optimization since 0038 /// said calls were no longer right in the "tail position" (i.e. the immediate 0039 /// predecessor of a ReturnInst instruction). 0040 /// 0041 /// Since the sibling call optimization causes the callee to reuse the caller's 0042 /// stack, if we could delay the generation of the stack protector check until 0043 /// later in CodeGen after the sibling call decision was made, we get both the 0044 /// tail call optimization and the stack protector check! 0045 /// 0046 /// A few goals in solving this problem were: 0047 /// 0048 /// 1. Preserve the architecture independence of stack protector generation. 0049 /// 0050 /// 2. Preserve the normal IR level stack protector check for platforms like 0051 /// OpenBSD for which we support platform-specific stack protector 0052 /// generation. 0053 /// 0054 /// The main problem that guided the present solution is that one can not 0055 /// solve this problem in an architecture independent manner at the IR level 0056 /// only. This is because: 0057 /// 0058 /// 1. The decision on whether or not to perform a sibling call on certain 0059 /// platforms (for instance i386) requires lower level information 0060 /// related to available registers that can not be known at the IR level. 0061 /// 0062 /// 2. Even if the previous point were not true, the decision on whether to 0063 /// perform a tail call is done in LowerCallTo in SelectionDAG (or 0064 /// CallLowering in GlobalISel) which occurs after the Stack Protector 0065 /// Pass. As a result, one would need to put the relevant callinst into the 0066 /// stack protector check success basic block (where the return inst is 0067 /// placed) and then move it back later at ISel/MI time before the 0068 /// stack protector check if the tail call optimization failed. The MI 0069 /// level option was nixed immediately since it would require 0070 /// platform-specific pattern matching. The ISel level option was 0071 /// nixed because SelectionDAG only processes one IR level basic block at a 0072 /// time implying one could not create a DAG Combine to move the callinst. 0073 /// 0074 /// To get around this problem: 0075 /// 0076 /// 1. SelectionDAG can only process one block at a time, we can generate 0077 /// multiple machine basic blocks for one IR level basic block. 0078 /// This is how we handle bit tests and switches. 0079 /// 0080 /// 2. At the MI level, tail calls are represented via a special return 0081 /// MIInst called "tcreturn". Thus if we know the basic block in which we 0082 /// wish to insert the stack protector check, we get the correct behavior 0083 /// by always inserting the stack protector check right before the return 0084 /// statement. This is a "magical transformation" since no matter where 0085 /// the stack protector check intrinsic is, we always insert the stack 0086 /// protector check code at the end of the BB. 0087 /// 0088 /// Given the aforementioned constraints, the following solution was devised: 0089 /// 0090 /// 1. On platforms that do not support ISel stack protector check 0091 /// generation, allow for the normal IR level stack protector check 0092 /// generation to continue. 0093 /// 0094 /// 2. On platforms that do support ISel stack protector check 0095 /// generation: 0096 /// 0097 /// a. Use the IR level stack protector pass to decide if a stack 0098 /// protector is required/which BB we insert the stack protector check 0099 /// in by reusing the logic already therein. 0100 /// 0101 /// b. After we finish selecting the basic block, we produce the validation 0102 /// code with one of these techniques: 0103 /// 1) with a call to a guard check function 0104 /// 2) with inlined instrumentation 0105 /// 0106 /// 1) We insert a call to the check function before the terminator. 0107 /// 0108 /// 2) We first find a splice point in the parent basic block 0109 /// before the terminator and then splice the terminator of said basic 0110 /// block into the success basic block. Then we code-gen a new tail for 0111 /// the parent basic block consisting of the two loads, the comparison, 0112 /// and finally two branches to the success/failure basic blocks. We 0113 /// conclude by code-gening the failure basic block if we have not 0114 /// code-gened it already (all stack protector checks we generate in 0115 /// the same function, use the same failure basic block). 0116 class StackProtectorDescriptor { 0117 public: 0118 StackProtectorDescriptor() = default; 0119 0120 /// Returns true if all fields of the stack protector descriptor are 0121 /// initialized implying that we should/are ready to emit a stack protector. 0122 bool shouldEmitStackProtector() const { 0123 return ParentMBB && SuccessMBB && FailureMBB; 0124 } 0125 0126 bool shouldEmitFunctionBasedCheckStackProtector() const { 0127 return ParentMBB && !SuccessMBB && !FailureMBB; 0128 } 0129 0130 /// Initialize the stack protector descriptor structure for a new basic 0131 /// block. 0132 void initialize(const BasicBlock *BB, MachineBasicBlock *MBB, 0133 bool FunctionBasedInstrumentation) { 0134 // Make sure we are not initialized yet. 0135 assert(!shouldEmitStackProtector() && "Stack Protector Descriptor is " 0136 "already initialized!"); 0137 ParentMBB = MBB; 0138 if (!FunctionBasedInstrumentation) { 0139 SuccessMBB = addSuccessorMBB(BB, MBB, /* IsLikely */ true); 0140 FailureMBB = addSuccessorMBB(BB, MBB, /* IsLikely */ false, FailureMBB); 0141 } 0142 } 0143 0144 /// Reset state that changes when we handle different basic blocks. 0145 /// 0146 /// This currently includes: 0147 /// 0148 /// 1. The specific basic block we are generating a 0149 /// stack protector for (ParentMBB). 0150 /// 0151 /// 2. The successor machine basic block that will contain the tail of 0152 /// parent mbb after we create the stack protector check (SuccessMBB). This 0153 /// BB is visited only on stack protector check success. 0154 void resetPerBBState() { 0155 ParentMBB = nullptr; 0156 SuccessMBB = nullptr; 0157 } 0158 0159 /// Reset state that only changes when we switch functions. 0160 /// 0161 /// This currently includes: 0162 /// 0163 /// 1. FailureMBB since we reuse the failure code path for all stack 0164 /// protector checks created in an individual function. 0165 /// 0166 /// 2.The guard variable since the guard variable we are checking against is 0167 /// always the same. 0168 void resetPerFunctionState() { FailureMBB = nullptr; } 0169 0170 MachineBasicBlock *getParentMBB() { return ParentMBB; } 0171 MachineBasicBlock *getSuccessMBB() { return SuccessMBB; } 0172 MachineBasicBlock *getFailureMBB() { return FailureMBB; } 0173 0174 private: 0175 /// The basic block for which we are generating the stack protector. 0176 /// 0177 /// As a result of stack protector generation, we will splice the 0178 /// terminators of this basic block into the successor mbb SuccessMBB and 0179 /// replace it with a compare/branch to the successor mbbs 0180 /// SuccessMBB/FailureMBB depending on whether or not the stack protector 0181 /// was violated. 0182 MachineBasicBlock *ParentMBB = nullptr; 0183 0184 /// A basic block visited on stack protector check success that contains the 0185 /// terminators of ParentMBB. 0186 MachineBasicBlock *SuccessMBB = nullptr; 0187 0188 /// This basic block visited on stack protector check failure that will 0189 /// contain a call to __stack_chk_fail(). 0190 MachineBasicBlock *FailureMBB = nullptr; 0191 0192 /// Add a successor machine basic block to ParentMBB. If the successor mbb 0193 /// has not been created yet (i.e. if SuccMBB = 0), then the machine basic 0194 /// block will be created. Assign a large weight if IsLikely is true. 0195 MachineBasicBlock *addSuccessorMBB(const BasicBlock *BB, 0196 MachineBasicBlock *ParentMBB, 0197 bool IsLikely, 0198 MachineBasicBlock *SuccMBB = nullptr); 0199 }; 0200 0201 /// Find the split point at which to splice the end of BB into its success stack 0202 /// protector check machine basic block. 0203 /// 0204 /// On many platforms, due to ABI constraints, terminators, even before register 0205 /// allocation, use physical registers. This creates an issue for us since 0206 /// physical registers at this point can not travel across basic 0207 /// blocks. Luckily, selectiondag always moves physical registers into vregs 0208 /// when they enter functions and moves them through a sequence of copies back 0209 /// into the physical registers right before the terminator creating a 0210 /// ``Terminator Sequence''. This function is searching for the beginning of the 0211 /// terminator sequence so that we can ensure that we splice off not just the 0212 /// terminator, but additionally the copies that move the vregs into the 0213 /// physical registers. 0214 MachineBasicBlock::iterator 0215 findSplitPointForStackProtector(MachineBasicBlock *BB, 0216 const TargetInstrInfo &TII); 0217 0218 /// Evaluates if the specified FP class test is better performed as the inverse 0219 /// (i.e. fewer instructions should be required to lower it). An example is the 0220 /// test "inf|normal|subnormal|zero", which is an inversion of "nan". 0221 /// 0222 /// \param Test The test as specified in 'is_fpclass' intrinsic invocation. 0223 /// \param UseFCmp The intention is to perform the comparison using 0224 /// floating-point compare instructions which check for nan. 0225 /// 0226 /// \returns The inverted test, or fcNone, if inversion does not produce a 0227 /// simpler test. 0228 FPClassTest invertFPClassTestIfSimpler(FPClassTest Test, bool UseFCmp); 0229 0230 /// Assuming the instruction \p MI is going to be deleted, attempt to salvage 0231 /// debug users of \p MI by writing the effect of \p MI in a DIExpression. 0232 void salvageDebugInfoForDbgValue(const MachineRegisterInfo &MRI, 0233 MachineInstr &MI, 0234 ArrayRef<MachineOperand *> DbgUsers); 0235 0236 } // namespace llvm 0237 0238 #endif // LLVM_CODEGEN_CODEGENCOMMONISEL_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|