LLVM 24.0.0git
AArch64WinCFI.cpp
Go to the documentation of this file.
1//===- AArch64WinCFI.cpp - Windows ARM64 unwind range insertion
2//-------------===//
3//
4// This pass runs after machine block layout and branch relaxation to describe
5// shrink-wrapped, handler-free functions as contiguous Windows unwind ranges.
6// It tracks where the real prologue's effects remain in force, then inserts
7// explicit range-boundary pseudos. Ranges entered with those effects already
8// in force receive a copy of its unwind recipe and an end_c marker, describing
9// a phantom prologue without emitting additional frame instructions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "AArch64.h"
14#include "AArch64InstrInfo.h"
16#include "AArch64Subtarget.h"
23#include <cassert>
24
25using namespace llvm;
26
27#define DEBUG_TYPE "aarch64-win-cfi"
28
29namespace {
30class AArch64WinCFILegacy : public MachineFunctionPass {
31public:
32 static char ID;
33 AArch64WinCFILegacy() : MachineFunctionPass(ID) {}
34
35 bool runOnMachineFunction(MachineFunction &MF) override;
36
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
38 AU.setPreservesCFG();
40 }
41
42 MachineFunctionProperties getRequiredProperties() const override {
43 return MachineFunctionProperties().setNoVRegs();
44 }
45
46 StringRef getPassName() const override {
47 return "AArch64 Windows unwind range insertion";
48 }
49};
50} // namespace
51
52char AArch64WinCFILegacy::ID = 0;
53
54INITIALIZE_PASS(AArch64WinCFILegacy, DEBUG_TYPE,
55 "AArch64 Windows unwind range insertion", false, false)
56
57static void endWinCFIRange(MachineBasicBlock &MBB, const AArch64InstrInfo &TII,
60 BuildMI(MBB, InsertPt, DebugLoc(), TII.get(AArch64::SEH_EndProc));
61 // Cover the final terminator too. Bundling the zero-size end marker with
62 // it preserves the terminator's CFG properties and keeps the MIR valid.
63 if (End->getPrevNode() && End->getPrevNode()->isTerminator())
64 End->bundleWithPred();
65}
66
68 const auto &ST = MF.getSubtarget<AArch64Subtarget>();
69
70 assert(ST.isTargetWindows() &&
71 "Windows CFI range insertion requires a Windows target");
72
73 // Without Windows CFI, there is no SEH frame recipe to split into ranges.
74 if (!MF.hasWinCFI())
75 return false;
76
77 // Leave handler-bearing functions on WinException's existing path until
78 // range splitting preserves their language-specific handler data.
80 return false;
81
82 // Funclets have independent frames; this pass describes a single frame.
83 if (MF.hasEHFunclets())
84 return false;
85
86 // Propagating the prologue's effects through the CFG needs an entry block.
87 if (MF.empty())
88 return false;
89
90 // Find the physical prologue's SEH marker. Only a prologue outside the entry
91 // block needs separate unwind ranges.
92 MachineBasicBlock *PrologueBlock = nullptr;
94 for (MachineBasicBlock &MBB : MF) {
95 for (const MachineInstr &MI : MBB) {
96 if (MI.getOpcode() == AArch64::SEH_StartProc)
98 "AArch64 Windows unwind range insertion cannot be run twice");
99 if (MI.getOpcode() == AArch64::SEH_PrologEnd) {
100 if (PrologueBlock)
102 "AArch64 Windows unwind range insertion does "
103 "not support multiple prologues");
104 PrologueBlock = &MBB;
105 }
106 if (MI.getOpcode() == AArch64::SEH_EpilogEnd)
107 EpilogueBlocks.insert(&MBB);
108 }
109 }
110 // No prologue found, or the prologue is in the entry block (so there is no
111 // shrink wrapping), so nothing to do.
112 if (!PrologueBlock || PrologueBlock == &MF.front())
113 return false;
114
115 // Collect the real prologue's SEH unwind pseudos to copy into phantom
116 // prologues later; do not collect the instructions that establish the frame.
117 // The first frame-setup instruction also locates the real range's start,
118 // excluding any frameless prefix within this block.
119 SmallVector<const MachineInstr *, 8> PrologueSEHInstructions;
120 auto PrologueStart = PrologueBlock->end();
121 for (MachineInstr &MI : *PrologueBlock) {
122 if (MI.getOpcode() == AArch64::SEH_PrologEnd)
123 break;
124 if (!MI.getFlag(MachineInstr::FrameSetup))
125 continue;
126 if (PrologueStart == PrologueBlock->end())
127 PrologueStart = MI.getIterator();
128 if (AArch64InstrInfo::isSEHInstruction(MI))
129 PrologueSEHInstructions.push_back(&MI);
130 }
131 // No prologue instructions, so nothing to do.
132 if (PrologueStart == PrologueBlock->end())
133 return false;
134
135 // The framed region starts at the prologue and includes epilogue blocks,
136 // but not their successors.
138 SmallVector<const MachineBasicBlock *, 16> PendingBlocks{PrologueBlock};
139 while (!PendingBlocks.empty()) {
140 const MachineBasicBlock *Block = PendingBlocks.pop_back_val();
141 if (!FramedBlocks.insert(Block).second || EpilogueBlocks.contains(Block))
142 continue;
143 append_range(PendingBlocks, Block->successors());
144 }
145
146 // Region membership must agree with the frame state on every reachable edge,
147 // including joins with frameless paths and loop backedges.
149 for (const MachineBasicBlock &Block : MF) {
150 bool FrameOnExit =
151 FramedBlocks.contains(&Block) && !EpilogueBlocks.contains(&Block);
152 for (const MachineBasicBlock *Successor : Block.successors()) {
153 bool FrameOnEntry =
154 Successor != PrologueBlock && FramedBlocks.contains(Successor);
155 if (FrameOnExit == FrameOnEntry)
156 continue;
157 // Only compute reachability on a mismatch. Dead predecessors do not
158 // constrain the frame state of their live successors.
159 if (ReachableBlocks.empty())
160 ReachableBlocks.insert_range(depth_first(&MF.front()));
161 if (ReachableBlocks.contains(&Block))
163 "inconsistent Windows ARM64 frame state at CFG join");
164 }
165 }
166
167 // Group framed blocks into contiguous records in final layout. Other gaps
168 // retain leaf-style unwinding.
169 const AArch64InstrInfo &TII = *ST.getInstrInfo();
170 MachineBasicBlock *PreviousLayoutBlock = nullptr;
171 bool RangeStarted = false;
172 for (MachineBasicBlock &MBB : MF) {
173 bool IsFramed = FramedBlocks.contains(&MBB);
174 bool IsPhysicalPrologue = &MBB == PrologueBlock;
175 // The physical prologue starts a new record. Otherwise only section
176 // boundaries and frameless gaps separate adjacent framed blocks.
177 if (RangeStarted &&
178 (MBB.isBeginSection() || !IsFramed || IsPhysicalPrologue)) {
179 endWinCFIRange(*PreviousLayoutBlock, TII, PreviousLayoutBlock->end());
180 RangeStarted = false;
181 }
182
183 if (IsFramed && !RangeStarted) {
184 if (IsPhysicalPrologue) {
185 BuildMI(MBB, PrologueStart, DebugLoc(),
186 TII.get(AArch64::SEH_StartProc));
187 } else {
188 // This range does not execute the prologue. Copy its recipe and use
189 // end_c to prevent partial-prologue unwinding near the range start.
190 auto InsertPt = MBB.begin();
191 BuildMI(MBB, InsertPt, DebugLoc(), TII.get(AArch64::SEH_StartProc));
192 for (const MachineInstr *MI : PrologueSEHInstructions)
193 MBB.insert(InsertPt, MF.CloneMachineInstr(MI));
194 BuildMI(MBB, InsertPt, DebugLoc(), TII.get(AArch64::SEH_EndC));
195 BuildMI(MBB, InsertPt, DebugLoc(), TII.get(AArch64::SEH_PrologEnd));
196 }
197 RangeStarted = true;
198 }
199
200 // Late transformations can append frameless code after an epilogue in the
201 // same block. End the record before such a suffix; otherwise PCs beyond the
202 // encoded epilogue would be treated as function body, causing the unwinder
203 // to apply the prologue recipe to an already-restored frame.
204 if (RangeStarted && EpilogueBlocks.contains(&MBB)) {
205 auto EpilogueEnd = llvm::find_if(MBB, [](const MachineInstr &MI) {
206 return MI.getOpcode() == AArch64::SEH_EpilogEnd;
207 });
208 auto Suffix = make_range(std::next(EpilogueEnd), MBB.end());
209 auto IsRealInstruction = [](const MachineInstr &MI) {
210 return !MI.isMetaInstruction() &&
211 !AArch64InstrInfo::isSEHInstruction(MI);
212 };
213 auto FirstInstrAfterEpilog = llvm::find_if(Suffix, IsRealInstruction);
214 // Only a lone four-byte terminator fits the epilogue's implicit final
215 // instruction. Terminator pseudos may expand to multiple instructions.
216 if (FirstInstrAfterEpilog != MBB.end() &&
217 (!FirstInstrAfterEpilog->isTerminator() ||
218 TII.getInstSizeInBytes(*FirstInstrAfterEpilog) != 4 ||
219 llvm::any_of(make_range(std::next(FirstInstrAfterEpilog), MBB.end()),
220 IsRealInstruction))) {
221 endWinCFIRange(MBB, TII, Suffix.begin());
222 RangeStarted = false;
223 }
224 }
225
226 // Close the record while emission is still in its text section, before a
227 // section switch or the end-of-function machinery runs.
228 if (RangeStarted && (MBB.isEndSection() || &MBB == &MF.back())) {
229 endWinCFIRange(MBB, TII, MBB.end());
230 RangeStarted = false;
231 }
232 PreviousLayoutBlock = &MBB;
233 }
234 MF.getInfo<AArch64FunctionInfo>()->setHasExplicitWinCFIRanges(true);
235 return true;
236}
237
238bool AArch64WinCFILegacy::runOnMachineFunction(MachineFunction &MF) {
239 return insertWinCFIRanges(MF);
240}
241
250
252 return new AArch64WinCFILegacy();
253}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static bool insertWinCFIRanges(MachineFunction &MF)
const AArch64InstrInfo MachineBasicBlock::iterator InsertPt
const AArch64InstrInfo & TII
MachineBasicBlock & MBB
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
#define DEBUG_TYPE
IRTranslator LLVM IR MI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:280
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
bool hasPersonalityFn() const
Check whether this function has a personality function.
Definition Function.h:899
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & back() const
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
Representation of each machine instruction.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:152
void insert_range(Range &&R)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:173
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
FunctionPass * createAArch64WinCFIPass()
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
iterator_range< df_iterator< T > > depth_first(const T &G)
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320