File: | lib/Target/X86/X86ExpandPseudo.cpp |
Warning: | line 166, column 1 Potential memory leak |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===------- X86ExpandPseudo.cpp - Expand pseudo instructions -------------===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | // | |||
10 | // This file contains a pass that expands pseudo instructions into target | |||
11 | // instructions to allow proper scheduling, if-conversion, other late | |||
12 | // optimizations, or simply the encoding of the instructions. | |||
13 | // | |||
14 | //===----------------------------------------------------------------------===// | |||
15 | ||||
16 | #include "X86.h" | |||
17 | #include "X86FrameLowering.h" | |||
18 | #include "X86InstrBuilder.h" | |||
19 | #include "X86InstrInfo.h" | |||
20 | #include "X86MachineFunctionInfo.h" | |||
21 | #include "X86Subtarget.h" | |||
22 | #include "llvm/Analysis/EHPersonalities.h" | |||
23 | #include "llvm/CodeGen/MachineFunctionPass.h" | |||
24 | #include "llvm/CodeGen/MachineInstrBuilder.h" | |||
25 | #include "llvm/CodeGen/Passes.h" // For IDs of passes that are preserved. | |||
26 | #include "llvm/IR/GlobalValue.h" | |||
27 | using namespace llvm; | |||
28 | ||||
29 | #define DEBUG_TYPE"x86-pseudo" "x86-pseudo" | |||
30 | ||||
31 | namespace { | |||
32 | class X86ExpandPseudo : public MachineFunctionPass { | |||
33 | public: | |||
34 | static char ID; | |||
35 | X86ExpandPseudo() : MachineFunctionPass(ID) {} | |||
36 | ||||
37 | void getAnalysisUsage(AnalysisUsage &AU) const override { | |||
38 | AU.setPreservesCFG(); | |||
39 | AU.addPreservedID(MachineLoopInfoID); | |||
40 | AU.addPreservedID(MachineDominatorsID); | |||
41 | MachineFunctionPass::getAnalysisUsage(AU); | |||
42 | } | |||
43 | ||||
44 | const X86Subtarget *STI; | |||
45 | const X86InstrInfo *TII; | |||
46 | const X86RegisterInfo *TRI; | |||
47 | const X86MachineFunctionInfo *X86FI; | |||
48 | const X86FrameLowering *X86FL; | |||
49 | ||||
50 | bool runOnMachineFunction(MachineFunction &Fn) override; | |||
51 | ||||
52 | MachineFunctionProperties getRequiredProperties() const override { | |||
53 | return MachineFunctionProperties().set( | |||
54 | MachineFunctionProperties::Property::NoVRegs); | |||
55 | } | |||
56 | ||||
57 | StringRef getPassName() const override { | |||
58 | return "X86 pseudo instruction expansion pass"; | |||
59 | } | |||
60 | ||||
61 | private: | |||
62 | void ExpandICallBranchFunnel(MachineBasicBlock *MBB, | |||
63 | MachineBasicBlock::iterator MBBI); | |||
64 | ||||
65 | bool ExpandMI(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI); | |||
66 | bool ExpandMBB(MachineBasicBlock &MBB); | |||
67 | }; | |||
68 | char X86ExpandPseudo::ID = 0; | |||
69 | } // End anonymous namespace. | |||
70 | ||||
71 | void X86ExpandPseudo::ExpandICallBranchFunnel( | |||
72 | MachineBasicBlock *MBB, MachineBasicBlock::iterator MBBI) { | |||
73 | MachineBasicBlock *JTMBB = MBB; | |||
74 | MachineInstr *JTInst = &*MBBI; | |||
75 | MachineFunction *MF = MBB->getParent(); | |||
76 | const BasicBlock *BB = MBB->getBasicBlock(); | |||
77 | auto InsPt = MachineFunction::iterator(MBB); | |||
78 | ++InsPt; | |||
79 | ||||
80 | std::vector<std::pair<MachineBasicBlock *, unsigned>> TargetMBBs; | |||
81 | DebugLoc DL = JTInst->getDebugLoc(); | |||
82 | MachineOperand Selector = JTInst->getOperand(0); | |||
83 | const GlobalValue *CombinedGlobal = JTInst->getOperand(1).getGlobal(); | |||
84 | ||||
85 | auto CmpTarget = [&](unsigned Target) { | |||
86 | BuildMI(*MBB, MBBI, DL, TII->get(X86::LEA64r), X86::R11) | |||
87 | .addReg(X86::RIP) | |||
88 | .addImm(1) | |||
89 | .addReg(0) | |||
90 | .addGlobalAddress(CombinedGlobal, | |||
91 | JTInst->getOperand(2 + 2 * Target).getImm()) | |||
92 | .addReg(0); | |||
93 | BuildMI(*MBB, MBBI, DL, TII->get(X86::CMP64rr)) | |||
94 | .add(Selector) | |||
95 | .addReg(X86::R11); | |||
96 | }; | |||
97 | ||||
98 | auto CreateMBB = [&]() { | |||
99 | auto *NewMBB = MF->CreateMachineBasicBlock(BB); | |||
100 | MBB->addSuccessor(NewMBB); | |||
101 | return NewMBB; | |||
102 | }; | |||
103 | ||||
104 | auto EmitCondJump = [&](unsigned Opcode, MachineBasicBlock *ThenMBB) { | |||
105 | BuildMI(*MBB, MBBI, DL, TII->get(Opcode)).addMBB(ThenMBB); | |||
106 | ||||
107 | auto *ElseMBB = CreateMBB(); | |||
108 | MF->insert(InsPt, ElseMBB); | |||
109 | MBB = ElseMBB; | |||
110 | MBBI = MBB->end(); | |||
111 | }; | |||
112 | ||||
113 | auto EmitCondJumpTarget = [&](unsigned Opcode, unsigned Target) { | |||
114 | auto *ThenMBB = CreateMBB(); | |||
115 | TargetMBBs.push_back({ThenMBB, Target}); | |||
116 | EmitCondJump(Opcode, ThenMBB); | |||
117 | }; | |||
118 | ||||
119 | auto EmitTailCall = [&](unsigned Target) { | |||
120 | BuildMI(*MBB, MBBI, DL, TII->get(X86::TAILJMPd64)) | |||
121 | .add(JTInst->getOperand(3 + 2 * Target)); | |||
122 | }; | |||
123 | ||||
124 | std::function<void(unsigned, unsigned)> EmitBranchFunnel = | |||
125 | [&](unsigned FirstTarget, unsigned NumTargets) { | |||
126 | if (NumTargets == 1) { | |||
127 | EmitTailCall(FirstTarget); | |||
128 | return; | |||
129 | } | |||
130 | ||||
131 | if (NumTargets == 2) { | |||
132 | CmpTarget(FirstTarget + 1); | |||
133 | EmitCondJumpTarget(X86::JB_1, FirstTarget); | |||
134 | EmitTailCall(FirstTarget + 1); | |||
135 | return; | |||
136 | } | |||
137 | ||||
138 | if (NumTargets < 6) { | |||
139 | CmpTarget(FirstTarget + 1); | |||
140 | EmitCondJumpTarget(X86::JB_1, FirstTarget); | |||
141 | EmitCondJumpTarget(X86::JE_1, FirstTarget + 1); | |||
142 | EmitBranchFunnel(FirstTarget + 2, NumTargets - 2); | |||
143 | return; | |||
144 | } | |||
145 | ||||
146 | auto *ThenMBB = CreateMBB(); | |||
147 | CmpTarget(FirstTarget + (NumTargets / 2)); | |||
148 | EmitCondJump(X86::JB_1, ThenMBB); | |||
149 | EmitCondJumpTarget(X86::JE_1, FirstTarget + (NumTargets / 2)); | |||
150 | EmitBranchFunnel(FirstTarget + (NumTargets / 2) + 1, | |||
151 | NumTargets - (NumTargets / 2) - 1); | |||
152 | ||||
153 | MF->insert(InsPt, ThenMBB); | |||
154 | MBB = ThenMBB; | |||
155 | MBBI = MBB->end(); | |||
156 | EmitBranchFunnel(FirstTarget, NumTargets / 2); | |||
157 | }; | |||
158 | ||||
159 | EmitBranchFunnel(0, (JTInst->getNumOperands() - 2) / 2); | |||
160 | for (auto P : TargetMBBs) { | |||
161 | MF->insert(InsPt, P.first); | |||
162 | BuildMI(P.first, DL, TII->get(X86::TAILJMPd64)) | |||
163 | .add(JTInst->getOperand(3 + 2 * P.second)); | |||
164 | } | |||
165 | JTMBB->erase(JTInst); | |||
166 | } | |||
| ||||
167 | ||||
168 | /// If \p MBBI is a pseudo instruction, this method expands | |||
169 | /// it to the corresponding (sequence of) actual instruction(s). | |||
170 | /// \returns true if \p MBBI has been expanded. | |||
171 | bool X86ExpandPseudo::ExpandMI(MachineBasicBlock &MBB, | |||
172 | MachineBasicBlock::iterator MBBI) { | |||
173 | MachineInstr &MI = *MBBI; | |||
174 | unsigned Opcode = MI.getOpcode(); | |||
175 | DebugLoc DL = MBBI->getDebugLoc(); | |||
176 | switch (Opcode) { | |||
| ||||
177 | default: | |||
178 | return false; | |||
179 | case X86::TCRETURNdi: | |||
180 | case X86::TCRETURNdicc: | |||
181 | case X86::TCRETURNri: | |||
182 | case X86::TCRETURNmi: | |||
183 | case X86::TCRETURNdi64: | |||
184 | case X86::TCRETURNdi64cc: | |||
185 | case X86::TCRETURNri64: | |||
186 | case X86::TCRETURNmi64: { | |||
187 | bool isMem = Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64; | |||
188 | MachineOperand &JumpTarget = MBBI->getOperand(0); | |||
189 | MachineOperand &StackAdjust = MBBI->getOperand(isMem ? 5 : 1); | |||
190 | assert(StackAdjust.isImm() && "Expecting immediate value.")(static_cast <bool> (StackAdjust.isImm() && "Expecting immediate value." ) ? void (0) : __assert_fail ("StackAdjust.isImm() && \"Expecting immediate value.\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 190, __extension__ __PRETTY_FUNCTION__)); | |||
191 | ||||
192 | // Adjust stack pointer. | |||
193 | int StackAdj = StackAdjust.getImm(); | |||
194 | int MaxTCDelta = X86FI->getTCReturnAddrDelta(); | |||
195 | int Offset = 0; | |||
196 | assert(MaxTCDelta <= 0 && "MaxTCDelta should never be positive")(static_cast <bool> (MaxTCDelta <= 0 && "MaxTCDelta should never be positive" ) ? void (0) : __assert_fail ("MaxTCDelta <= 0 && \"MaxTCDelta should never be positive\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 196, __extension__ __PRETTY_FUNCTION__)); | |||
197 | ||||
198 | // Incoporate the retaddr area. | |||
199 | Offset = StackAdj - MaxTCDelta; | |||
200 | assert(Offset >= 0 && "Offset should never be negative")(static_cast <bool> (Offset >= 0 && "Offset should never be negative" ) ? void (0) : __assert_fail ("Offset >= 0 && \"Offset should never be negative\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 200, __extension__ __PRETTY_FUNCTION__)); | |||
201 | ||||
202 | if (Opcode == X86::TCRETURNdicc || Opcode == X86::TCRETURNdi64cc) { | |||
203 | assert(Offset == 0 && "Conditional tail call cannot adjust the stack.")(static_cast <bool> (Offset == 0 && "Conditional tail call cannot adjust the stack." ) ? void (0) : __assert_fail ("Offset == 0 && \"Conditional tail call cannot adjust the stack.\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 203, __extension__ __PRETTY_FUNCTION__)); | |||
204 | } | |||
205 | ||||
206 | if (Offset) { | |||
207 | // Check for possible merge with preceding ADD instruction. | |||
208 | Offset += X86FL->mergeSPUpdates(MBB, MBBI, true); | |||
209 | X86FL->emitSPUpdate(MBB, MBBI, DL, Offset, /*InEpilogue=*/true); | |||
210 | } | |||
211 | ||||
212 | // Jump to label or value in register. | |||
213 | bool IsWin64 = STI->isTargetWin64(); | |||
214 | if (Opcode == X86::TCRETURNdi || Opcode == X86::TCRETURNdicc || | |||
215 | Opcode == X86::TCRETURNdi64 || Opcode == X86::TCRETURNdi64cc) { | |||
216 | unsigned Op; | |||
217 | switch (Opcode) { | |||
218 | case X86::TCRETURNdi: | |||
219 | Op = X86::TAILJMPd; | |||
220 | break; | |||
221 | case X86::TCRETURNdicc: | |||
222 | Op = X86::TAILJMPd_CC; | |||
223 | break; | |||
224 | case X86::TCRETURNdi64cc: | |||
225 | assert(!MBB.getParent()->hasWinCFI() &&(static_cast <bool> (!MBB.getParent()->hasWinCFI() && "Conditional tail calls confuse " "the Win64 unwinder.") ? void (0) : __assert_fail ("!MBB.getParent()->hasWinCFI() && \"Conditional tail calls confuse \" \"the Win64 unwinder.\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 227, __extension__ __PRETTY_FUNCTION__)) | |||
226 | "Conditional tail calls confuse "(static_cast <bool> (!MBB.getParent()->hasWinCFI() && "Conditional tail calls confuse " "the Win64 unwinder.") ? void (0) : __assert_fail ("!MBB.getParent()->hasWinCFI() && \"Conditional tail calls confuse \" \"the Win64 unwinder.\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 227, __extension__ __PRETTY_FUNCTION__)) | |||
227 | "the Win64 unwinder.")(static_cast <bool> (!MBB.getParent()->hasWinCFI() && "Conditional tail calls confuse " "the Win64 unwinder.") ? void (0) : __assert_fail ("!MBB.getParent()->hasWinCFI() && \"Conditional tail calls confuse \" \"the Win64 unwinder.\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 227, __extension__ __PRETTY_FUNCTION__)); | |||
228 | Op = X86::TAILJMPd64_CC; | |||
229 | break; | |||
230 | default: | |||
231 | // Note: Win64 uses REX prefixes indirect jumps out of functions, but | |||
232 | // not direct ones. | |||
233 | Op = X86::TAILJMPd64; | |||
234 | break; | |||
235 | } | |||
236 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op)); | |||
237 | if (JumpTarget.isGlobal()) { | |||
238 | MIB.addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(), | |||
239 | JumpTarget.getTargetFlags()); | |||
240 | } else { | |||
241 | assert(JumpTarget.isSymbol())(static_cast <bool> (JumpTarget.isSymbol()) ? void (0) : __assert_fail ("JumpTarget.isSymbol()", "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 241, __extension__ __PRETTY_FUNCTION__)); | |||
242 | MIB.addExternalSymbol(JumpTarget.getSymbolName(), | |||
243 | JumpTarget.getTargetFlags()); | |||
244 | } | |||
245 | if (Op == X86::TAILJMPd_CC || Op == X86::TAILJMPd64_CC) { | |||
246 | MIB.addImm(MBBI->getOperand(2).getImm()); | |||
247 | } | |||
248 | ||||
249 | } else if (Opcode == X86::TCRETURNmi || Opcode == X86::TCRETURNmi64) { | |||
250 | unsigned Op = (Opcode == X86::TCRETURNmi) | |||
251 | ? X86::TAILJMPm | |||
252 | : (IsWin64 ? X86::TAILJMPm64_REX : X86::TAILJMPm64); | |||
253 | MachineInstrBuilder MIB = BuildMI(MBB, MBBI, DL, TII->get(Op)); | |||
254 | for (unsigned i = 0; i != 5; ++i) | |||
255 | MIB.add(MBBI->getOperand(i)); | |||
256 | } else if (Opcode == X86::TCRETURNri64) { | |||
257 | BuildMI(MBB, MBBI, DL, | |||
258 | TII->get(IsWin64 ? X86::TAILJMPr64_REX : X86::TAILJMPr64)) | |||
259 | .addReg(JumpTarget.getReg(), RegState::Kill); | |||
260 | } else { | |||
261 | BuildMI(MBB, MBBI, DL, TII->get(X86::TAILJMPr)) | |||
262 | .addReg(JumpTarget.getReg(), RegState::Kill); | |||
263 | } | |||
264 | ||||
265 | MachineInstr &NewMI = *std::prev(MBBI); | |||
266 | NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI); | |||
267 | ||||
268 | // Delete the pseudo instruction TCRETURN. | |||
269 | MBB.erase(MBBI); | |||
270 | ||||
271 | return true; | |||
272 | } | |||
273 | case X86::EH_RETURN: | |||
274 | case X86::EH_RETURN64: { | |||
275 | MachineOperand &DestAddr = MBBI->getOperand(0); | |||
276 | assert(DestAddr.isReg() && "Offset should be in register!")(static_cast <bool> (DestAddr.isReg() && "Offset should be in register!" ) ? void (0) : __assert_fail ("DestAddr.isReg() && \"Offset should be in register!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 276, __extension__ __PRETTY_FUNCTION__)); | |||
277 | const bool Uses64BitFramePtr = | |||
278 | STI->isTarget64BitLP64() || STI->isTargetNaCl64(); | |||
279 | unsigned StackPtr = TRI->getStackRegister(); | |||
280 | BuildMI(MBB, MBBI, DL, | |||
281 | TII->get(Uses64BitFramePtr ? X86::MOV64rr : X86::MOV32rr), StackPtr) | |||
282 | .addReg(DestAddr.getReg()); | |||
283 | // The EH_RETURN pseudo is really removed during the MC Lowering. | |||
284 | return true; | |||
285 | } | |||
286 | case X86::IRET: { | |||
287 | // Adjust stack to erase error code | |||
288 | int64_t StackAdj = MBBI->getOperand(0).getImm(); | |||
289 | X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, true); | |||
290 | // Replace pseudo with machine iret | |||
291 | BuildMI(MBB, MBBI, DL, | |||
292 | TII->get(STI->is64Bit() ? X86::IRET64 : X86::IRET32)); | |||
293 | MBB.erase(MBBI); | |||
294 | return true; | |||
295 | } | |||
296 | case X86::RET: { | |||
297 | // Adjust stack to erase error code | |||
298 | int64_t StackAdj = MBBI->getOperand(0).getImm(); | |||
299 | MachineInstrBuilder MIB; | |||
300 | if (StackAdj == 0) { | |||
301 | MIB = BuildMI(MBB, MBBI, DL, | |||
302 | TII->get(STI->is64Bit() ? X86::RETQ : X86::RETL)); | |||
303 | } else if (isUInt<16>(StackAdj)) { | |||
304 | MIB = BuildMI(MBB, MBBI, DL, | |||
305 | TII->get(STI->is64Bit() ? X86::RETIQ : X86::RETIL)) | |||
306 | .addImm(StackAdj); | |||
307 | } else { | |||
308 | assert(!STI->is64Bit() &&(static_cast <bool> (!STI->is64Bit() && "shouldn't need to do this for x86_64 targets!" ) ? void (0) : __assert_fail ("!STI->is64Bit() && \"shouldn't need to do this for x86_64 targets!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 309, __extension__ __PRETTY_FUNCTION__)) | |||
309 | "shouldn't need to do this for x86_64 targets!")(static_cast <bool> (!STI->is64Bit() && "shouldn't need to do this for x86_64 targets!" ) ? void (0) : __assert_fail ("!STI->is64Bit() && \"shouldn't need to do this for x86_64 targets!\"" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 309, __extension__ __PRETTY_FUNCTION__)); | |||
310 | // A ret can only handle immediates as big as 2**16-1. If we need to pop | |||
311 | // off bytes before the return address, we must do it manually. | |||
312 | BuildMI(MBB, MBBI, DL, TII->get(X86::POP32r)).addReg(X86::ECX, RegState::Define); | |||
313 | X86FL->emitSPUpdate(MBB, MBBI, DL, StackAdj, /*InEpilogue=*/true); | |||
314 | BuildMI(MBB, MBBI, DL, TII->get(X86::PUSH32r)).addReg(X86::ECX); | |||
315 | MIB = BuildMI(MBB, MBBI, DL, TII->get(X86::RETL)); | |||
316 | } | |||
317 | for (unsigned I = 1, E = MBBI->getNumOperands(); I != E; ++I) | |||
318 | MIB.add(MBBI->getOperand(I)); | |||
319 | MBB.erase(MBBI); | |||
320 | return true; | |||
321 | } | |||
322 | case X86::EH_RESTORE: { | |||
323 | // Restore ESP and EBP, and optionally ESI if required. | |||
324 | bool IsSEH = isAsynchronousEHPersonality(classifyEHPersonality( | |||
325 | MBB.getParent()->getFunction().getPersonalityFn())); | |||
326 | X86FL->restoreWin32EHStackPointers(MBB, MBBI, DL, /*RestoreSP=*/IsSEH); | |||
327 | MBBI->eraseFromParent(); | |||
328 | return true; | |||
329 | } | |||
330 | case X86::LCMPXCHG8B_SAVE_EBX: | |||
331 | case X86::LCMPXCHG16B_SAVE_RBX: { | |||
332 | // Perform the following transformation. | |||
333 | // SaveRbx = pseudocmpxchg Addr, <4 opds for the address>, InArg, SaveRbx | |||
334 | // => | |||
335 | // [E|R]BX = InArg | |||
336 | // actualcmpxchg Addr | |||
337 | // [E|R]BX = SaveRbx | |||
338 | const MachineOperand &InArg = MBBI->getOperand(6); | |||
339 | unsigned SaveRbx = MBBI->getOperand(7).getReg(); | |||
340 | ||||
341 | unsigned ActualInArg = | |||
342 | Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::EBX : X86::RBX; | |||
343 | // Copy the input argument of the pseudo into the argument of the | |||
344 | // actual instruction. | |||
345 | TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, InArg.getReg(), | |||
346 | InArg.isKill()); | |||
347 | // Create the actual instruction. | |||
348 | unsigned ActualOpc = | |||
349 | Opcode == X86::LCMPXCHG8B_SAVE_EBX ? X86::LCMPXCHG8B : X86::LCMPXCHG16B; | |||
350 | MachineInstr *NewInstr = BuildMI(MBB, MBBI, DL, TII->get(ActualOpc)); | |||
351 | // Copy the operands related to the address. | |||
352 | for (unsigned Idx = 1; Idx < 6; ++Idx) | |||
353 | NewInstr->addOperand(MBBI->getOperand(Idx)); | |||
354 | // Finally, restore the value of RBX. | |||
355 | TII->copyPhysReg(MBB, MBBI, DL, ActualInArg, SaveRbx, | |||
356 | /*SrcIsKill*/ true); | |||
357 | ||||
358 | // Delete the pseudo. | |||
359 | MBBI->eraseFromParent(); | |||
360 | return true; | |||
361 | } | |||
362 | case TargetOpcode::ICALL_BRANCH_FUNNEL: | |||
363 | ExpandICallBranchFunnel(&MBB, MBBI); | |||
364 | return true; | |||
365 | } | |||
366 | llvm_unreachable("Previous switch has a fallthrough?")::llvm::llvm_unreachable_internal("Previous switch has a fallthrough?" , "/build/llvm-toolchain-snapshot-7~svn329677/lib/Target/X86/X86ExpandPseudo.cpp" , 366); | |||
367 | } | |||
368 | ||||
369 | /// Expand all pseudo instructions contained in \p MBB. | |||
370 | /// \returns true if any expansion occurred for \p MBB. | |||
371 | bool X86ExpandPseudo::ExpandMBB(MachineBasicBlock &MBB) { | |||
372 | bool Modified = false; | |||
373 | ||||
374 | // MBBI may be invalidated by the expansion. | |||
375 | MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end(); | |||
376 | while (MBBI != E) { | |||
377 | MachineBasicBlock::iterator NMBBI = std::next(MBBI); | |||
378 | Modified |= ExpandMI(MBB, MBBI); | |||
379 | MBBI = NMBBI; | |||
380 | } | |||
381 | ||||
382 | return Modified; | |||
383 | } | |||
384 | ||||
385 | bool X86ExpandPseudo::runOnMachineFunction(MachineFunction &MF) { | |||
386 | STI = &static_cast<const X86Subtarget &>(MF.getSubtarget()); | |||
387 | TII = STI->getInstrInfo(); | |||
388 | TRI = STI->getRegisterInfo(); | |||
389 | X86FI = MF.getInfo<X86MachineFunctionInfo>(); | |||
390 | X86FL = STI->getFrameLowering(); | |||
391 | ||||
392 | bool Modified = false; | |||
393 | for (MachineBasicBlock &MBB : MF) | |||
394 | Modified |= ExpandMBB(MBB); | |||
395 | return Modified; | |||
396 | } | |||
397 | ||||
398 | /// Returns an instance of the pseudo instruction expansion pass. | |||
399 | FunctionPass *llvm::createX86ExpandPseudoPass() { | |||
400 | return new X86ExpandPseudo(); | |||
401 | } |
1 | // Implementation of std::function -*- C++ -*- |
2 | |
3 | // Copyright (C) 2004-2017 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file include/bits/function.h |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{functional} |
28 | */ |
29 | |
30 | #ifndef _GLIBCXX_STD_FUNCTION_H1 |
31 | #define _GLIBCXX_STD_FUNCTION_H1 1 |
32 | |
33 | #pragma GCC system_header |
34 | |
35 | #if __cplusplus201103L < 201103L |
36 | # include <bits/c++0x_warning.h> |
37 | #else |
38 | |
39 | #if __cpp_rtti199711 |
40 | # include <typeinfo> |
41 | #endif |
42 | #include <bits/stl_function.h> |
43 | #include <bits/invoke.h> |
44 | #include <bits/refwrap.h> |
45 | #include <bits/functexcept.h> |
46 | |
47 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
48 | { |
49 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
50 | |
51 | /** |
52 | * Derives from @c unary_function or @c binary_function, or perhaps |
53 | * nothing, depending on the number of arguments provided. The |
54 | * primary template is the basis case, which derives nothing. |
55 | */ |
56 | template<typename _Res, typename... _ArgTypes> |
57 | struct _Maybe_unary_or_binary_function { }; |
58 | |
59 | /// Derives from @c unary_function, as appropriate. |
60 | template<typename _Res, typename _T1> |
61 | struct _Maybe_unary_or_binary_function<_Res, _T1> |
62 | : std::unary_function<_T1, _Res> { }; |
63 | |
64 | /// Derives from @c binary_function, as appropriate. |
65 | template<typename _Res, typename _T1, typename _T2> |
66 | struct _Maybe_unary_or_binary_function<_Res, _T1, _T2> |
67 | : std::binary_function<_T1, _T2, _Res> { }; |
68 | |
69 | |
70 | /** |
71 | * @brief Exception class thrown when class template function's |
72 | * operator() is called with an empty target. |
73 | * @ingroup exceptions |
74 | */ |
75 | class bad_function_call : public std::exception |
76 | { |
77 | public: |
78 | virtual ~bad_function_call() noexcept; |
79 | |
80 | const char* what() const noexcept; |
81 | }; |
82 | |
83 | /** |
84 | * Trait identifying "location-invariant" types, meaning that the |
85 | * address of the object (or any of its members) will not escape. |
86 | * Trivially copyable types are location-invariant and users can |
87 | * specialize this trait for other types. |
88 | */ |
89 | template<typename _Tp> |
90 | struct __is_location_invariant |
91 | : is_trivially_copyable<_Tp>::type |
92 | { }; |
93 | |
94 | class _Undefined_class; |
95 | |
96 | union _Nocopy_types |
97 | { |
98 | void* _M_object; |
99 | const void* _M_const_object; |
100 | void (*_M_function_pointer)(); |
101 | void (_Undefined_class::*_M_member_pointer)(); |
102 | }; |
103 | |
104 | union [[gnu::may_alias]] _Any_data |
105 | { |
106 | void* _M_access() { return &_M_pod_data[0]; } |
107 | const void* _M_access() const { return &_M_pod_data[0]; } |
108 | |
109 | template<typename _Tp> |
110 | _Tp& |
111 | _M_access() |
112 | { return *static_cast<_Tp*>(_M_access()); } |
113 | |
114 | template<typename _Tp> |
115 | const _Tp& |
116 | _M_access() const |
117 | { return *static_cast<const _Tp*>(_M_access()); } |
118 | |
119 | _Nocopy_types _M_unused; |
120 | char _M_pod_data[sizeof(_Nocopy_types)]; |
121 | }; |
122 | |
123 | enum _Manager_operation |
124 | { |
125 | __get_type_info, |
126 | __get_functor_ptr, |
127 | __clone_functor, |
128 | __destroy_functor |
129 | }; |
130 | |
131 | // Simple type wrapper that helps avoid annoying const problems |
132 | // when casting between void pointers and pointers-to-pointers. |
133 | template<typename _Tp> |
134 | struct _Simple_type_wrapper |
135 | { |
136 | _Simple_type_wrapper(_Tp __value) : __value(__value) { } |
137 | |
138 | _Tp __value; |
139 | }; |
140 | |
141 | template<typename _Tp> |
142 | struct __is_location_invariant<_Simple_type_wrapper<_Tp> > |
143 | : __is_location_invariant<_Tp> |
144 | { }; |
145 | |
146 | template<typename _Signature> |
147 | class function; |
148 | |
149 | /// Base class of all polymorphic function object wrappers. |
150 | class _Function_base |
151 | { |
152 | public: |
153 | static const std::size_t _M_max_size = sizeof(_Nocopy_types); |
154 | static const std::size_t _M_max_align = __alignof__(_Nocopy_types); |
155 | |
156 | template<typename _Functor> |
157 | class _Base_manager |
158 | { |
159 | protected: |
160 | static const bool __stored_locally = |
161 | (__is_location_invariant<_Functor>::value |
162 | && sizeof(_Functor) <= _M_max_size |
163 | && __alignof__(_Functor) <= _M_max_align |
164 | && (_M_max_align % __alignof__(_Functor) == 0)); |
165 | |
166 | typedef integral_constant<bool, __stored_locally> _Local_storage; |
167 | |
168 | // Retrieve a pointer to the function object |
169 | static _Functor* |
170 | _M_get_pointer(const _Any_data& __source) |
171 | { |
172 | const _Functor* __ptr = |
173 | __stored_locally? std::__addressof(__source._M_access<_Functor>()) |
174 | /* have stored a pointer */ : __source._M_access<_Functor*>(); |
175 | return const_cast<_Functor*>(__ptr); |
176 | } |
177 | |
178 | // Clone a location-invariant function object that fits within |
179 | // an _Any_data structure. |
180 | static void |
181 | _M_clone(_Any_data& __dest, const _Any_data& __source, true_type) |
182 | { |
183 | ::new (__dest._M_access()) _Functor(__source._M_access<_Functor>()); |
184 | } |
185 | |
186 | // Clone a function object that is not location-invariant or |
187 | // that cannot fit into an _Any_data structure. |
188 | static void |
189 | _M_clone(_Any_data& __dest, const _Any_data& __source, false_type) |
190 | { |
191 | __dest._M_access<_Functor*>() = |
192 | new _Functor(*__source._M_access<_Functor*>()); |
193 | } |
194 | |
195 | // Destroying a location-invariant object may still require |
196 | // destruction. |
197 | static void |
198 | _M_destroy(_Any_data& __victim, true_type) |
199 | { |
200 | __victim._M_access<_Functor>().~_Functor(); |
201 | } |
202 | |
203 | // Destroying an object located on the heap. |
204 | static void |
205 | _M_destroy(_Any_data& __victim, false_type) |
206 | { |
207 | delete __victim._M_access<_Functor*>(); |
208 | } |
209 | |
210 | public: |
211 | static bool |
212 | _M_manager(_Any_data& __dest, const _Any_data& __source, |
213 | _Manager_operation __op) |
214 | { |
215 | switch (__op) |
216 | { |
217 | #if __cpp_rtti199711 |
218 | case __get_type_info: |
219 | __dest._M_access<const type_info*>() = &typeid(_Functor); |
220 | break; |
221 | #endif |
222 | case __get_functor_ptr: |
223 | __dest._M_access<_Functor*>() = _M_get_pointer(__source); |
224 | break; |
225 | |
226 | case __clone_functor: |
227 | _M_clone(__dest, __source, _Local_storage()); |
228 | break; |
229 | |
230 | case __destroy_functor: |
231 | _M_destroy(__dest, _Local_storage()); |
232 | break; |
233 | } |
234 | return false; |
235 | } |
236 | |
237 | static void |
238 | _M_init_functor(_Any_data& __functor, _Functor&& __f) |
239 | { _M_init_functor(__functor, std::move(__f), _Local_storage()); } |
240 | |
241 | template<typename _Signature> |
242 | static bool |
243 | _M_not_empty_function(const function<_Signature>& __f) |
244 | { return static_cast<bool>(__f); } |
245 | |
246 | template<typename _Tp> |
247 | static bool |
248 | _M_not_empty_function(_Tp* __fp) |
249 | { return __fp != nullptr; } |
250 | |
251 | template<typename _Class, typename _Tp> |
252 | static bool |
253 | _M_not_empty_function(_Tp _Class::* __mp) |
254 | { return __mp != nullptr; } |
255 | |
256 | template<typename _Tp> |
257 | static bool |
258 | _M_not_empty_function(const _Tp&) |
259 | { return true; } |
260 | |
261 | private: |
262 | static void |
263 | _M_init_functor(_Any_data& __functor, _Functor&& __f, true_type) |
264 | { ::new (__functor._M_access()) _Functor(std::move(__f)); } |
265 | |
266 | static void |
267 | _M_init_functor(_Any_data& __functor, _Functor&& __f, false_type) |
268 | { __functor._M_access<_Functor*>() = new _Functor(std::move(__f)); } |
269 | }; |
270 | |
271 | _Function_base() : _M_manager(nullptr) { } |
272 | |
273 | ~_Function_base() |
274 | { |
275 | if (_M_manager) |
276 | _M_manager(_M_functor, _M_functor, __destroy_functor); |
277 | } |
278 | |
279 | bool _M_empty() const { return !_M_manager; } |
280 | |
281 | typedef bool (*_Manager_type)(_Any_data&, const _Any_data&, |
282 | _Manager_operation); |
283 | |
284 | _Any_data _M_functor; |
285 | _Manager_type _M_manager; |
286 | }; |
287 | |
288 | template<typename _Signature, typename _Functor> |
289 | class _Function_handler; |
290 | |
291 | template<typename _Res, typename _Functor, typename... _ArgTypes> |
292 | class _Function_handler<_Res(_ArgTypes...), _Functor> |
293 | : public _Function_base::_Base_manager<_Functor> |
294 | { |
295 | typedef _Function_base::_Base_manager<_Functor> _Base; |
296 | |
297 | public: |
298 | static _Res |
299 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
300 | { |
301 | return (*_Base::_M_get_pointer(__functor))( |
302 | std::forward<_ArgTypes>(__args)...); |
303 | } |
304 | }; |
305 | |
306 | template<typename _Functor, typename... _ArgTypes> |
307 | class _Function_handler<void(_ArgTypes...), _Functor> |
308 | : public _Function_base::_Base_manager<_Functor> |
309 | { |
310 | typedef _Function_base::_Base_manager<_Functor> _Base; |
311 | |
312 | public: |
313 | static void |
314 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
315 | { |
316 | (*_Base::_M_get_pointer(__functor))( |
317 | std::forward<_ArgTypes>(__args)...); |
318 | } |
319 | }; |
320 | |
321 | template<typename _Class, typename _Member, typename _Res, |
322 | typename... _ArgTypes> |
323 | class _Function_handler<_Res(_ArgTypes...), _Member _Class::*> |
324 | : public _Function_handler<void(_ArgTypes...), _Member _Class::*> |
325 | { |
326 | typedef _Function_handler<void(_ArgTypes...), _Member _Class::*> |
327 | _Base; |
328 | |
329 | public: |
330 | static _Res |
331 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
332 | { |
333 | return std::__invoke(_Base::_M_get_pointer(__functor)->__value, |
334 | std::forward<_ArgTypes>(__args)...); |
335 | } |
336 | }; |
337 | |
338 | template<typename _Class, typename _Member, typename... _ArgTypes> |
339 | class _Function_handler<void(_ArgTypes...), _Member _Class::*> |
340 | : public _Function_base::_Base_manager< |
341 | _Simple_type_wrapper< _Member _Class::* > > |
342 | { |
343 | typedef _Member _Class::* _Functor; |
344 | typedef _Simple_type_wrapper<_Functor> _Wrapper; |
345 | typedef _Function_base::_Base_manager<_Wrapper> _Base; |
346 | |
347 | public: |
348 | static bool |
349 | _M_manager(_Any_data& __dest, const _Any_data& __source, |
350 | _Manager_operation __op) |
351 | { |
352 | switch (__op) |
353 | { |
354 | #if __cpp_rtti199711 |
355 | case __get_type_info: |
356 | __dest._M_access<const type_info*>() = &typeid(_Functor); |
357 | break; |
358 | #endif |
359 | case __get_functor_ptr: |
360 | __dest._M_access<_Functor*>() = |
361 | &_Base::_M_get_pointer(__source)->__value; |
362 | break; |
363 | |
364 | default: |
365 | _Base::_M_manager(__dest, __source, __op); |
366 | } |
367 | return false; |
368 | } |
369 | |
370 | static void |
371 | _M_invoke(const _Any_data& __functor, _ArgTypes&&... __args) |
372 | { |
373 | std::__invoke(_Base::_M_get_pointer(__functor)->__value, |
374 | std::forward<_ArgTypes>(__args)...); |
375 | } |
376 | }; |
377 | |
378 | template<typename _From, typename _To> |
379 | using __check_func_return_type |
380 | = __or_<is_void<_To>, is_same<_From, _To>, is_convertible<_From, _To>>; |
381 | |
382 | /** |
383 | * @brief Primary class template for std::function. |
384 | * @ingroup functors |
385 | * |
386 | * Polymorphic function wrapper. |
387 | */ |
388 | template<typename _Res, typename... _ArgTypes> |
389 | class function<_Res(_ArgTypes...)> |
390 | : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, |
391 | private _Function_base |
392 | { |
393 | template<typename _Func, |
394 | typename _Res2 = typename result_of<_Func&(_ArgTypes...)>::type> |
395 | struct _Callable : __check_func_return_type<_Res2, _Res> { }; |
396 | |
397 | // Used so the return type convertibility checks aren't done when |
398 | // performing overload resolution for copy construction/assignment. |
399 | template<typename _Tp> |
400 | struct _Callable<function, _Tp> : false_type { }; |
401 | |
402 | template<typename _Cond, typename _Tp> |
403 | using _Requires = typename enable_if<_Cond::value, _Tp>::type; |
404 | |
405 | public: |
406 | typedef _Res result_type; |
407 | |
408 | // [3.7.2.1] construct/copy/destroy |
409 | |
410 | /** |
411 | * @brief Default construct creates an empty function call wrapper. |
412 | * @post @c !(bool)*this |
413 | */ |
414 | function() noexcept |
415 | : _Function_base() { } |
416 | |
417 | /** |
418 | * @brief Creates an empty function call wrapper. |
419 | * @post @c !(bool)*this |
420 | */ |
421 | function(nullptr_t) noexcept |
422 | : _Function_base() { } |
423 | |
424 | /** |
425 | * @brief %Function copy constructor. |
426 | * @param __x A %function object with identical call signature. |
427 | * @post @c bool(*this) == bool(__x) |
428 | * |
429 | * The newly-created %function contains a copy of the target of @a |
430 | * __x (if it has one). |
431 | */ |
432 | function(const function& __x); |
433 | |
434 | /** |
435 | * @brief %Function move constructor. |
436 | * @param __x A %function object rvalue with identical call signature. |
437 | * |
438 | * The newly-created %function contains the target of @a __x |
439 | * (if it has one). |
440 | */ |
441 | function(function&& __x) noexcept : _Function_base() |
442 | { |
443 | __x.swap(*this); |
444 | } |
445 | |
446 | /** |
447 | * @brief Builds a %function that targets a copy of the incoming |
448 | * function object. |
449 | * @param __f A %function object that is callable with parameters of |
450 | * type @c T1, @c T2, ..., @c TN and returns a value convertible |
451 | * to @c Res. |
452 | * |
453 | * The newly-created %function object will target a copy of |
454 | * @a __f. If @a __f is @c reference_wrapper<F>, then this function |
455 | * object will contain a reference to the function object @c |
456 | * __f.get(). If @a __f is a NULL function pointer or NULL |
457 | * pointer-to-member, the newly-created object will be empty. |
458 | * |
459 | * If @a __f is a non-NULL function pointer or an object of type @c |
460 | * reference_wrapper<F>, this function will not throw. |
461 | */ |
462 | template<typename _Functor, |
463 | typename = _Requires<__not_<is_same<_Functor, function>>, void>, |
464 | typename = _Requires<_Callable<_Functor>, void>> |
465 | function(_Functor); |
466 | |
467 | /** |
468 | * @brief %Function assignment operator. |
469 | * @param __x A %function with identical call signature. |
470 | * @post @c (bool)*this == (bool)x |
471 | * @returns @c *this |
472 | * |
473 | * The target of @a __x is copied to @c *this. If @a __x has no |
474 | * target, then @c *this will be empty. |
475 | * |
476 | * If @a __x targets a function pointer or a reference to a function |
477 | * object, then this operation will not throw an %exception. |
478 | */ |
479 | function& |
480 | operator=(const function& __x) |
481 | { |
482 | function(__x).swap(*this); |
483 | return *this; |
484 | } |
485 | |
486 | /** |
487 | * @brief %Function move-assignment operator. |
488 | * @param __x A %function rvalue with identical call signature. |
489 | * @returns @c *this |
490 | * |
491 | * The target of @a __x is moved to @c *this. If @a __x has no |
492 | * target, then @c *this will be empty. |
493 | * |
494 | * If @a __x targets a function pointer or a reference to a function |
495 | * object, then this operation will not throw an %exception. |
496 | */ |
497 | function& |
498 | operator=(function&& __x) noexcept |
499 | { |
500 | function(std::move(__x)).swap(*this); |
501 | return *this; |
502 | } |
503 | |
504 | /** |
505 | * @brief %Function assignment to zero. |
506 | * @post @c !(bool)*this |
507 | * @returns @c *this |
508 | * |
509 | * The target of @c *this is deallocated, leaving it empty. |
510 | */ |
511 | function& |
512 | operator=(nullptr_t) noexcept |
513 | { |
514 | if (_M_manager) |
515 | { |
516 | _M_manager(_M_functor, _M_functor, __destroy_functor); |
517 | _M_manager = nullptr; |
518 | _M_invoker = nullptr; |
519 | } |
520 | return *this; |
521 | } |
522 | |
523 | /** |
524 | * @brief %Function assignment to a new target. |
525 | * @param __f A %function object that is callable with parameters of |
526 | * type @c T1, @c T2, ..., @c TN and returns a value convertible |
527 | * to @c Res. |
528 | * @return @c *this |
529 | * |
530 | * This %function object wrapper will target a copy of @a |
531 | * __f. If @a __f is @c reference_wrapper<F>, then this function |
532 | * object will contain a reference to the function object @c |
533 | * __f.get(). If @a __f is a NULL function pointer or NULL |
534 | * pointer-to-member, @c this object will be empty. |
535 | * |
536 | * If @a __f is a non-NULL function pointer or an object of type @c |
537 | * reference_wrapper<F>, this function will not throw. |
538 | */ |
539 | template<typename _Functor> |
540 | _Requires<_Callable<typename decay<_Functor>::type>, function&> |
541 | operator=(_Functor&& __f) |
542 | { |
543 | function(std::forward<_Functor>(__f)).swap(*this); |
544 | return *this; |
545 | } |
546 | |
547 | /// @overload |
548 | template<typename _Functor> |
549 | function& |
550 | operator=(reference_wrapper<_Functor> __f) noexcept |
551 | { |
552 | function(__f).swap(*this); |
553 | return *this; |
554 | } |
555 | |
556 | // [3.7.2.2] function modifiers |
557 | |
558 | /** |
559 | * @brief Swap the targets of two %function objects. |
560 | * @param __x A %function with identical call signature. |
561 | * |
562 | * Swap the targets of @c this function object and @a __f. This |
563 | * function will not throw an %exception. |
564 | */ |
565 | void swap(function& __x) noexcept |
566 | { |
567 | std::swap(_M_functor, __x._M_functor); |
568 | std::swap(_M_manager, __x._M_manager); |
569 | std::swap(_M_invoker, __x._M_invoker); |
570 | } |
571 | |
572 | // [3.7.2.3] function capacity |
573 | |
574 | /** |
575 | * @brief Determine if the %function wrapper has a target. |
576 | * |
577 | * @return @c true when this %function object contains a target, |
578 | * or @c false when it is empty. |
579 | * |
580 | * This function will not throw an %exception. |
581 | */ |
582 | explicit operator bool() const noexcept |
583 | { return !_M_empty(); } |
584 | |
585 | // [3.7.2.4] function invocation |
586 | |
587 | /** |
588 | * @brief Invokes the function targeted by @c *this. |
589 | * @returns the result of the target. |
590 | * @throws bad_function_call when @c !(bool)*this |
591 | * |
592 | * The function call operator invokes the target function object |
593 | * stored by @c this. |
594 | */ |
595 | _Res operator()(_ArgTypes... __args) const; |
596 | |
597 | #if __cpp_rtti199711 |
598 | // [3.7.2.5] function target access |
599 | /** |
600 | * @brief Determine the type of the target of this function object |
601 | * wrapper. |
602 | * |
603 | * @returns the type identifier of the target function object, or |
604 | * @c typeid(void) if @c !(bool)*this. |
605 | * |
606 | * This function will not throw an %exception. |
607 | */ |
608 | const type_info& target_type() const noexcept; |
609 | |
610 | /** |
611 | * @brief Access the stored target function object. |
612 | * |
613 | * @return Returns a pointer to the stored target function object, |
614 | * if @c typeid(_Functor).equals(target_type()); otherwise, a NULL |
615 | * pointer. |
616 | * |
617 | * This function does not throw exceptions. |
618 | * |
619 | * @{ |
620 | */ |
621 | template<typename _Functor> _Functor* target() noexcept; |
622 | |
623 | template<typename _Functor> const _Functor* target() const noexcept; |
624 | // @} |
625 | #endif |
626 | |
627 | private: |
628 | using _Invoker_type = _Res (*)(const _Any_data&, _ArgTypes&&...); |
629 | _Invoker_type _M_invoker; |
630 | }; |
631 | |
632 | #if __cpp_deduction_guides >= 201606 |
633 | template<typename> |
634 | struct __function_guide_helper |
635 | { }; |
636 | |
637 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
638 | struct __function_guide_helper< |
639 | _Res (_Tp::*) (_Args...) noexcept(_Nx) |
640 | > |
641 | { using type = _Res(_Args...); }; |
642 | |
643 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
644 | struct __function_guide_helper< |
645 | _Res (_Tp::*) (_Args...) & noexcept(_Nx) |
646 | > |
647 | { using type = _Res(_Args...); }; |
648 | |
649 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
650 | struct __function_guide_helper< |
651 | _Res (_Tp::*) (_Args...) const noexcept(_Nx) |
652 | > |
653 | { using type = _Res(_Args...); }; |
654 | |
655 | template<typename _Res, typename _Tp, bool _Nx, typename... _Args> |
656 | struct __function_guide_helper< |
657 | _Res (_Tp::*) (_Args...) const & noexcept(_Nx) |
658 | > |
659 | { using type = _Res(_Args...); }; |
660 | |
661 | template<typename _Res, typename... _ArgTypes> |
662 | function(_Res(*)(_ArgTypes...)) -> function<_Res(_ArgTypes...)>; |
663 | |
664 | template<typename _Functor, typename _Signature = typename |
665 | __function_guide_helper<decltype(&_Functor::operator())>::type> |
666 | function(_Functor) -> function<_Signature>; |
667 | #endif |
668 | |
669 | // Out-of-line member definitions. |
670 | template<typename _Res, typename... _ArgTypes> |
671 | function<_Res(_ArgTypes...)>:: |
672 | function(const function& __x) |
673 | : _Function_base() |
674 | { |
675 | if (static_cast<bool>(__x)) |
676 | { |
677 | __x._M_manager(_M_functor, __x._M_functor, __clone_functor); |
678 | _M_invoker = __x._M_invoker; |
679 | _M_manager = __x._M_manager; |
680 | } |
681 | } |
682 | |
683 | template<typename _Res, typename... _ArgTypes> |
684 | template<typename _Functor, typename, typename> |
685 | function<_Res(_ArgTypes...)>:: |
686 | function(_Functor __f) |
687 | : _Function_base() |
688 | { |
689 | typedef _Function_handler<_Res(_ArgTypes...), _Functor> _My_handler; |
690 | |
691 | if (_My_handler::_M_not_empty_function(__f)) |
692 | { |
693 | _My_handler::_M_init_functor(_M_functor, std::move(__f)); |
694 | _M_invoker = &_My_handler::_M_invoke; |
695 | _M_manager = &_My_handler::_M_manager; |
696 | } |
697 | } |
698 | |
699 | template<typename _Res, typename... _ArgTypes> |
700 | _Res |
701 | function<_Res(_ArgTypes...)>:: |
702 | operator()(_ArgTypes... __args) const |
703 | { |
704 | if (_M_empty()) |
705 | __throw_bad_function_call(); |
706 | return _M_invoker(_M_functor, std::forward<_ArgTypes>(__args)...); |
707 | } |
708 | |
709 | #if __cpp_rtti199711 |
710 | template<typename _Res, typename... _ArgTypes> |
711 | const type_info& |
712 | function<_Res(_ArgTypes...)>:: |
713 | target_type() const noexcept |
714 | { |
715 | if (_M_manager) |
716 | { |
717 | _Any_data __typeinfo_result; |
718 | _M_manager(__typeinfo_result, _M_functor, __get_type_info); |
719 | return *__typeinfo_result._M_access<const type_info*>(); |
720 | } |
721 | else |
722 | return typeid(void); |
723 | } |
724 | |
725 | template<typename _Res, typename... _ArgTypes> |
726 | template<typename _Functor> |
727 | _Functor* |
728 | function<_Res(_ArgTypes...)>:: |
729 | target() noexcept |
730 | { |
731 | const function* __const_this = this; |
732 | const _Functor* __func = __const_this->template target<_Functor>(); |
733 | return const_cast<_Functor*>(__func); |
734 | } |
735 | |
736 | template<typename _Res, typename... _ArgTypes> |
737 | template<typename _Functor> |
738 | const _Functor* |
739 | function<_Res(_ArgTypes...)>:: |
740 | target() const noexcept |
741 | { |
742 | if (typeid(_Functor) == target_type() && _M_manager) |
743 | { |
744 | _Any_data __ptr; |
745 | _M_manager(__ptr, _M_functor, __get_functor_ptr); |
746 | return __ptr._M_access<const _Functor*>(); |
747 | } |
748 | else |
749 | return nullptr; |
750 | } |
751 | #endif |
752 | |
753 | // [20.7.15.2.6] null pointer comparisons |
754 | |
755 | /** |
756 | * @brief Compares a polymorphic function object wrapper against 0 |
757 | * (the NULL pointer). |
758 | * @returns @c true if the wrapper has no target, @c false otherwise |
759 | * |
760 | * This function will not throw an %exception. |
761 | */ |
762 | template<typename _Res, typename... _Args> |
763 | inline bool |
764 | operator==(const function<_Res(_Args...)>& __f, nullptr_t) noexcept |
765 | { return !static_cast<bool>(__f); } |
766 | |
767 | /// @overload |
768 | template<typename _Res, typename... _Args> |
769 | inline bool |
770 | operator==(nullptr_t, const function<_Res(_Args...)>& __f) noexcept |
771 | { return !static_cast<bool>(__f); } |
772 | |
773 | /** |
774 | * @brief Compares a polymorphic function object wrapper against 0 |
775 | * (the NULL pointer). |
776 | * @returns @c false if the wrapper has no target, @c true otherwise |
777 | * |
778 | * This function will not throw an %exception. |
779 | */ |
780 | template<typename _Res, typename... _Args> |
781 | inline bool |
782 | operator!=(const function<_Res(_Args...)>& __f, nullptr_t) noexcept |
783 | { return static_cast<bool>(__f); } |
784 | |
785 | /// @overload |
786 | template<typename _Res, typename... _Args> |
787 | inline bool |
788 | operator!=(nullptr_t, const function<_Res(_Args...)>& __f) noexcept |
789 | { return static_cast<bool>(__f); } |
790 | |
791 | |
792 | // [20.7.15.2.7] specialized algorithms |
793 | |
794 | /** |
795 | * @brief Swap the targets of two polymorphic function object wrappers. |
796 | * |
797 | * This function will not throw an %exception. |
798 | */ |
799 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
800 | // 2062. Effect contradictions w/o no-throw guarantee of std::function swaps |
801 | template<typename _Res, typename... _Args> |
802 | inline void |
803 | swap(function<_Res(_Args...)>& __x, function<_Res(_Args...)>& __y) noexcept |
804 | { __x.swap(__y); } |
805 | |
806 | _GLIBCXX_END_NAMESPACE_VERSION |
807 | } // namespace std |
808 | |
809 | #endif // C++11 |
810 | |
811 | #endif // _GLIBCXX_STD_FUNCTION_H |