llvm.org GIT mirror llvm / 226889e
[WinEH] Add cleanupendpad instruction Summary: Add a `cleanupendpad` instruction, used to mark exceptional exits out of cleanups (for languages/targets that can abort a cleanup with another exception). The `cleanupendpad` instruction is similar to the `catchendpad` instruction in that it is an EH pad which is the target of unwind edges in the handler and which itself has an unwind edge to the next EH action. The `cleanupendpad` instruction, similar to `cleanupret` has a `cleanuppad` argument indicating which cleanup it exits. The unwind successors of a `cleanuppad`'s `cleanupendpad`s must agree with each other and with its `cleanupret`s. Update WinEHPrepare (and docs/tests) to accomodate `cleanupendpad`. Reviewers: rnk, andrew.w.kaylor, majnemer Subscribers: llvm-commits Differential Revision: http://reviews.llvm.org/D12433 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@246751 91177308-0d34-0410-b5e6-96231b3b80d8 Joseph Tremoulet 4 years ago
31 changed file(s) with 638 addition(s) and 138 deletion(s). Raw diff Collapse all Expand all
613613 a ``noexcept`` function should transitively unwind to a terminateblock. Throw
614614 specifications are not implemented by MSVC, and are not yet supported.
615615
616 New instructions are also used to mark the points where control is transferred
617 out of a catch/cleanup handler (which will correspond to exits from the
618 generated funclet). A catch handler which reaches its end by normal execution
619 executes a ``catchret`` instruction, which is a terminator indicating where in
620 the function control is returned to. A cleanup handler which reaches its end
621 by normal execution executes a ``cleanupret`` instruction, which is a terminator
622 indicating where the active exception will unwind to next. A catch or cleanup
623 handler which is exited by another exception being raised during its execution will
624 unwind through a ``catchendpad`` or ``cleanuupendpad`` (respectively). The
625 ``catchendpad`` and ``cleanupendpad`` instructions are considered "exception
626 handling pads" in the same sense that ``catchpad``, ``cleanuppad``, and
627 ``terminatepad`` are.
628
616629 Each of these new EH pad instructions has a way to identify which
617630 action should be considered after this action. The ``catchpad`` and
618631 ``terminatepad`` instructions are terminators, and have a label operand considered
619632 to be an unwind destination analogous to the unwind destination of an invoke. The
620633 ``cleanuppad`` instruction is different from the other two in that it is not a
621634 terminator. The code inside a cleanuppad runs before transferring control to the
622 next action, so the ``cleanupret`` instruction is the instruction that holds a
623 label operand and unwinds to the next EH pad. All of these "unwind edges" may
624 refer to a basic block that contains an EH pad instruction, or they may simply
625 unwind to the caller. Unwinding to the caller has roughly the same semantics as
626 the ``resume`` instruction in the ``landingpad`` model. When inlining through an
627 invoke, instructions that unwind to the caller are hooked up to unwind to the
628 unwind destination of the call site.
635 next action, so the ``cleanupret`` and ``cleanupendpad`` instructions are the
636 instructions that hold a label operand and unwind to the next EH pad. All of
637 these "unwind edges" may refer to a basic block that contains an EH pad instruction,
638 or they may simply unwind to the caller. Unwinding to the caller has roughly the
639 same semantics as the ``resume`` instruction in the ``landingpad`` model. When
640 inlining through an invoke, instructions that unwind to the caller are hooked
641 up to unwind to the unwind destination of the call site.
629642
630643 Putting things together, here is a hypothetical lowering of some C++ that uses
631644 all of the new IR instructions:
643656 Cleanup obj;
644657 may_throw();
645658 } catch (int e) {
659 may_throw();
646660 return e;
647661 }
648662 return 0;
665679 call void @"\01??_DCleanup@@QEAA@XZ"(%struct.Cleanup* nonnull %obj) nounwind
666680 br label %return
667681
668 return: ; preds = %invoke.cont.2, %catch
682 return: ; preds = %invoke.cont.2, %invoke.cont.3
669683 %retval.0 = phi i32 [ 0, %invoke.cont.2 ], [ %9, %catch ]
670684 ret i32 %retval.0
671685
678692
679693 lpad.catch: ; preds = %entry, %lpad.cleanup
680694 %catch = catchpad [%rtti.TypeDescriptor2* @"\01??_R0H@8", i32 0, i32* %e]
681 to label %catch unwind label %lpad.terminate
682
683 catch: ; preds = %lpad.catch
695 to label %catch.body unwind label %catchend
696
697 catch.body: ; preds = %lpad.catch
698 invoke void @"\01?may_throw@@YAXXZ"()
699 to label %invoke.cont.3 unwind label %catchend
700
701 invoke.cont.3: ; preds = %catch.body
684702 %9 = load i32, i32* %e, align 4
685703 catchret %catch label %return
686704
687 lpad.terminate:
705 catchend: ; preds = %lpad.catch, %catch.body
706 catchendpad unwind label %lpad.terminate
707
708 lpad.terminate: ; preds = %catchend
688709 terminatepad [void ()* @"\01?terminate@@YAXXZ"]
689710 unwind to caller
690711 }
47814781 ':ref:`resume `', ':ref:`catchpad `',
47824782 ':ref:`catchendpad `',
47834783 ':ref:`catchret `',
4784 ':ref:`cleanupendpad `',
47844785 ':ref:`cleanupret `',
47854786 ':ref:`terminatepad `',
47864787 and ':ref:`unreachable `'.
52345235 The '``catchendpad``' instruction is used by `LLVM's exception handling
52355236 system `_ to communicate to the
52365237 :ref:`personality function ` which invokes are associated
5237 with a chain of :ref:`catchpad ` instructions.
5238 with a chain of :ref:`catchpad ` instructions; propagating an
5239 exception out of a catch handler is represented by unwinding through its
5240 ``catchendpad``. Unwinding to the outer scope when a chain of catch handlers
5241 do not handle an exception is also represented by unwinding through their
5242 ``catchendpad``.
52385243
52395244 The ``nextaction`` label indicates where control should transfer to if
52405245 none of the ``catchpad`` instructions are suitable for catching the
52675272 - A catch-end block must have a '``catchendpad``' instruction as its
52685273 first non-PHI instruction.
52695274 - There can be only one '``catchendpad``' instruction within the
5270 catch block.
5275 catch-end block.
52715276 - A basic block that is not a catch-end block may not include a
52725277 '``catchendpad``' instruction.
52735278 - Exactly one catch block may unwind to a ``catchendpad``.
5274 - The unwind target of invokes between a ``catchpad`` and a
5275 corresponding ``catchret`` must be its ``catchendpad`` or
5276 an inner EH pad.
5279 - It is undefined behavior to execute a ``catchendpad`` if none of the
5280 '``catchpad``'s chained to it have been executed.
5281 - It is undefined behavior to execute a ``catchendpad`` twice without an
5282 intervening execution of one or more of the '``catchpad``'s chained to it.
5283 - It is undefined behavior to execute a ``catchendpad`` if, after the most
5284 recent execution of the normal successor edge of any ``catchpad`` chained
5285 to it, some ``catchret`` consuming that ``catchpad`` has already been
5286 executed.
5287 - It is undefined behavior to execute a ``catchendpad`` if, after the most
5288 recent execution of the normal successor edge of any ``catchpad`` chained
5289 to it, any other ``catchpad`` or ``cleanuppad`` has been executed but has
5290 not had a corresponding
5291 ``catchret``/``cleanupret``/``catchendpad``/``cleanupendpad`` executed.
52775292
52785293 Example:
52795294 """"""""
53205335 arbitrary code to, for example, run a C++ destructor.
53215336 Control then transfers to ``normal``.
53225337 It may be passed an optional, personality specific, value.
5338
53235339 It is undefined behavior to execute a ``catchret`` whose ``catchpad`` has
53245340 not been executed.
5325 It is undefined behavior to execute a ``catchret`` if any ``catchpad`` or
5326 ``cleanuppad`` has been executed, without subsequently executing a
5327 corresponding ``catchret``/``cleanupret`` or unwinding out of the inner
5328 pad, following the most recent execution of the ``catchret``'s corresponding
5329 ``catchpad``.
5330
5341
5342 It is undefined behavior to execute a ``catchret`` if, after the most recent
5343 execution of its ``catchpad``, some ``catchret`` or ``catchendpad`` linked
5344 to the same ``catchpad`` has already been executed.
5345
5346 It is undefined behavior to execute a ``catchret`` if, after the most recent
5347 execution of its ``catchpad``, any other ``catchpad`` or ``cleanuppad`` has
5348 been executed but has not had a corresponding
5349 ``catchret``/``cleanupret``/``catchendpad``/``cleanupendpad`` executed.
53315350
53325351 Example:
53335352 """"""""
53355354 .. code-block:: llvm
53365355
53375356 catchret %catch label %continue
5357
5358 .. _i_cleanupendpad:
5359
5360 '``cleanupendpad``' Instruction
5361 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5362
5363 Syntax:
5364 """""""
5365
5366 ::
5367
5368 cleanupendpad unwind label
5369 cleanupendpad unwind to caller
5370
5371 Overview:
5372 """""""""
5373
5374 The '``cleanupendpad``' instruction is used by `LLVM's exception handling
5375 system `_ to communicate to the
5376 :ref:`personality function ` which invokes are associated
5377 with a :ref:`cleanuppad ` instructions; propagating an exception
5378 out of a cleanup is represented by unwinding through its ``cleanupendpad``.
5379
5380 The ``nextaction`` label indicates where control should unwind to next, in the
5381 event that a cleanup is exited by means of an(other) exception being raised.
5382
5383 If a ``nextaction`` label is not present, the instruction unwinds out of
5384 its parent function. The
5385 :ref:`personality function ` will continue processing
5386 exception handling actions in the caller.
5387
5388 Arguments:
5389 """"""""""
5390
5391 The '``cleanupendpad``' instruction requires one argument, which indicates
5392 which ``cleanuppad`` it exits, and must be a :ref:`cleanuppad `.
5393 It also has an optional successor, ``nextaction``, indicating where control
5394 should transfer to.
5395
5396 Semantics:
5397 """"""""""
5398
5399 When and exception propagates to a ``cleanupendpad``, control is transfered to
5400 ``nextaction`` if it is present. If it is not present, control is transfered to
5401 the caller.
5402
5403 The ``cleanupendpad`` instruction has several restrictions:
5404
5405 - A cleanup-end block is a basic block which is the unwind destination of
5406 an exceptional instruction.
5407 - A cleanup-end block must have a '``cleanupendpad``' instruction as its
5408 first non-PHI instruction.
5409 - There can be only one '``cleanupendpad``' instruction within the
5410 cleanup-end block.
5411 - A basic block that is not a cleanup-end block may not include a
5412 '``cleanupendpad``' instruction.
5413 - It is undefined behavior to execute a ``cleanupendpad`` whose ``cleanuppad``
5414 has not been executed.
5415 - It is undefined behavior to execute a ``cleanupendpad`` if, after the most
5416 recent execution of its ``cleanuppad``, some ``cleanupret`` or ``cleanupendpad``
5417 consuming the same ``cleanuppad`` has already been executed.
5418 - It is undefined behavior to execute a ``cleanupendpad`` if, after the most
5419 recent execution of its ``cleanuppad``, any other ``cleanuppad`` or
5420 ``catchpad`` has been executed but has not had a corresponding
5421 ``cleanupret``/``catchret``/``cleanupendpad``/``catchendpad`` executed.
5422
5423 Example:
5424 """"""""
5425
5426 .. code-block:: llvm
5427
5428 cleanupendpad %cleanup unwind label %terminate
5429 cleanupendpad %cleanup unwind to caller
53385430
53395431 .. _i_cleanupret:
53405432
53705462 :ref:`personality function ` that one
53715463 :ref:`cleanuppad ` it transferred control to has ended.
53725464 It transfers control to ``continue`` or unwinds out of the function.
5465
53735466 It is undefined behavior to execute a ``cleanupret`` whose ``cleanuppad`` has
53745467 not been executed.
5375 It is undefined behavior to execute a ``cleanupret`` if any ``catchpad`` or
5376 ``cleanuppad`` has been executed, without subsequently executing a
5377 corresponding ``catchret``/``cleanupret`` or unwinding out of the inner pad,
5378 following the most recent execution of the ``cleanupret``'s corresponding
5379 ``cleanuppad``.
5468
5469 It is undefined behavior to execute a ``cleanupret`` if, after the most recent
5470 execution of its ``cleanuppad``, some ``cleanupret`` or ``cleanupendpad``
5471 consuming the same ``cleanuppad`` has already been executed.
5472
5473 It is undefined behavior to execute a ``cleanupret`` if, after the most recent
5474 execution of its ``cleanuppad``, any other ``cleanuppad`` or ``catchpad`` has
5475 been executed but has not had a corresponding
5476 ``cleanupret``/``catchret``/``cleanupendpad``/``catchendpad`` executed.
53805477
53815478 Example:
53825479 """"""""
84308527 information the :ref:`personality function ` requires to
84318528 execute the cleanup.
84328529 The ``resultval`` has the type :ref:`token ` and is used to
8433 match the ``cleanuppad`` to corresponding :ref:`cleanuprets `.
8530 match the ``cleanuppad`` to corresponding :ref:`cleanuprets `
8531 and :ref:`cleanupendpads `.
84348532
84358533 Arguments:
84368534 """"""""""
84408538
84418539 Semantics:
84428540 """"""""""
8443
8444 The '``cleanuppad``' instruction defines the values which are set by the
8445 :ref:`personality function ` upon re-entry to the function.
8446 As with calling conventions, how the personality function results are
8447 represented in LLVM IR is target specific.
84488541
84498542 When the call stack is being unwound due to an exception being thrown,
84508543 the :ref:`personality function ` transfers control to the
84518544 ``cleanuppad`` with the aid of the personality-specific arguments.
8545 As with calling conventions, how the personality function results are
8546 represented in LLVM IR is target specific.
84528547
84538548 The ``cleanuppad`` instruction has several restrictions:
84548549
84608555 cleanup block.
84618556 - A basic block that is not a cleanup block may not include a
84628557 '``cleanuppad``' instruction.
8463 - All '``cleanupret``'s which exit a ``cleanuppad`` must have the same
8464 exceptional successor.
8558 - All '``cleanupret``'s and '``cleanupendpad``'s which consume a ``cleanuppad``
8559 must have the same exceptional successor.
84658560 - It is undefined behavior for control to transfer from a ``cleanuppad`` to a
8466 ``ret`` without first executing a ``cleanupret`` that consumes the
8467 ``cleanuppad`` or unwinding out of the ``cleanuppad``.
8561 ``ret`` without first executing a ``cleanupret`` or ``cleanupendpad`` that
8562 consumes the ``cleanuppad``.
84688563 - It is undefined behavior for control to transfer from a ``cleanuppad`` to
8469 itself without first executing a ``cleanupret`` that consumes the
8470 ``cleanuppad`` or unwinding out of the ``cleanuppad``.
8564 itself without first executing a ``cleanupret`` or ``cleanupendpad`` that
8565 consumes the ``cleanuppad``.
84718566
84728567 Example:
84738568 """"""""
361361 FUNC_CODE_INST_TERMINATEPAD = 51, // TERMINATEPAD: [bb#,num,args...]
362362 FUNC_CODE_INST_CLEANUPPAD = 52, // CLEANUPPAD: [num,args...]
363363 FUNC_CODE_INST_CATCHENDPAD = 53, // CATCHENDPAD: [] or [bb#]
364 FUNC_CODE_INST_CLEANUPENDPAD = 54, // CLEANUPENDPAD: [val] or [val,bb#]
364365 };
365366
366367 enum UseListCodes {
680680 return Insert(CleanupReturnInst::Create(CleanupPad, UnwindBB));
681681 }
682682
683 CatchEndPadInst *CreateCleanupEndPad(CleanupPadInst *CleanupPad,
684 BasicBlock *UnwindBB = nullptr) {
685 return Insert(CleanupEndPadInst::Create(CleanupPad, UnwindBB));
686 }
687
683688 CatchPadInst *CreateCatchPad(BasicBlock *NormalDest, BasicBlock *UnwindDest,
684689 ArrayRef Args, const Twine &Name = "") {
685690 return Insert(CatchPadInst::Create(NormalDest, UnwindDest, Args), Name);
169169 RetTy visitResumeInst(ResumeInst &I) { DELEGATE(TerminatorInst);}
170170 RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);}
171171 RetTy visitCleanupReturnInst(CleanupReturnInst &I) { DELEGATE(TerminatorInst);}
172 RetTy visitCatchReturnInst(CatchReturnInst &I) { DELEGATE(TerminatorInst);}
172 RetTy visitCleanupEndPadInst(CleanupEndPadInst &I) { DELEGATE(TerminatorInst); }
173 RetTy visitCatchReturnInst(CatchReturnInst &I) { DELEGATE(TerminatorInst); }
173174 RetTy visitCatchPadInst(CatchPadInst &I) { DELEGATE(TerminatorInst);}
174175 RetTy visitCatchEndPadInst(CatchEndPadInst &I) { DELEGATE(TerminatorInst); }
175176 RetTy visitTerminatePadInst(TerminatePadInst &I) { DELEGATE(TerminatorInst);}
8181 case Instruction::CatchPad:
8282 case Instruction::CatchEndPad:
8383 case Instruction::CatchRet:
84 case Instruction::CleanupEndPad:
8485 case Instruction::CleanupRet:
8586 case Instruction::Invoke:
8687 case Instruction::Resume:
102102 HANDLE_TERM_INST ( 7, Unreachable , UnreachableInst)
103103 HANDLE_TERM_INST ( 8, CleanupRet , CleanupReturnInst)
104104 HANDLE_TERM_INST ( 9, CatchRet , CatchReturnInst)
105 HANDLE_TERM_INST (10, CatchPad , CatchPadInst)
106 HANDLE_TERM_INST (11, TerminatePad, TerminatePadInst)
107 HANDLE_TERM_INST (12, CatchEndPad , CatchEndPadInst)
108 LAST_TERM_INST (12)
105 HANDLE_TERM_INST (10, CatchPad , CatchPadInst)
106 HANDLE_TERM_INST (11, TerminatePad , TerminatePadInst)
107 HANDLE_TERM_INST (12, CatchEndPad , CatchEndPadInst)
108 HANDLE_TERM_INST (13, CleanupEndPad , CleanupEndPadInst)
109 LAST_TERM_INST (13)
109110
110111 // Standard binary operators...
111 FIRST_BINARY_INST(13)
112 HANDLE_BINARY_INST(13, Add , BinaryOperator)
113 HANDLE_BINARY_INST(14, FAdd , BinaryOperator)
114 HANDLE_BINARY_INST(15, Sub , BinaryOperator)
115 HANDLE_BINARY_INST(16, FSub , BinaryOperator)
116 HANDLE_BINARY_INST(17, Mul , BinaryOperator)
117 HANDLE_BINARY_INST(18, FMul , BinaryOperator)
118 HANDLE_BINARY_INST(19, UDiv , BinaryOperator)
119 HANDLE_BINARY_INST(20, SDiv , BinaryOperator)
120 HANDLE_BINARY_INST(21, FDiv , BinaryOperator)
121 HANDLE_BINARY_INST(22, URem , BinaryOperator)
122 HANDLE_BINARY_INST(23, SRem , BinaryOperator)
123 HANDLE_BINARY_INST(24, FRem , BinaryOperator)
112 FIRST_BINARY_INST(14)
113 HANDLE_BINARY_INST(14, Add , BinaryOperator)
114 HANDLE_BINARY_INST(15, FAdd , BinaryOperator)
115 HANDLE_BINARY_INST(16, Sub , BinaryOperator)
116 HANDLE_BINARY_INST(17, FSub , BinaryOperator)
117 HANDLE_BINARY_INST(18, Mul , BinaryOperator)
118 HANDLE_BINARY_INST(19, FMul , BinaryOperator)
119 HANDLE_BINARY_INST(20, UDiv , BinaryOperator)
120 HANDLE_BINARY_INST(21, SDiv , BinaryOperator)
121 HANDLE_BINARY_INST(22, FDiv , BinaryOperator)
122 HANDLE_BINARY_INST(23, URem , BinaryOperator)
123 HANDLE_BINARY_INST(24, SRem , BinaryOperator)
124 HANDLE_BINARY_INST(25, FRem , BinaryOperator)
124125
125126 // Logical operators (integer operands)
126 HANDLE_BINARY_INST(25, Shl , BinaryOperator) // Shift left (logical)
127 HANDLE_BINARY_INST(26, LShr , BinaryOperator) // Shift right (logical)
128 HANDLE_BINARY_INST(27, AShr , BinaryOperator) // Shift right (arithmetic)
129 HANDLE_BINARY_INST(28, And , BinaryOperator)
130 HANDLE_BINARY_INST(29, Or , BinaryOperator)
131 HANDLE_BINARY_INST(30, Xor , BinaryOperator)
132 LAST_BINARY_INST(30)
127 HANDLE_BINARY_INST(26, Shl , BinaryOperator) // Shift left (logical)
128 HANDLE_BINARY_INST(27, LShr , BinaryOperator) // Shift right (logical)
129 HANDLE_BINARY_INST(28, AShr , BinaryOperator) // Shift right (arithmetic)
130 HANDLE_BINARY_INST(29, And , BinaryOperator)
131 HANDLE_BINARY_INST(30, Or , BinaryOperator)
132 HANDLE_BINARY_INST(31, Xor , BinaryOperator)
133 LAST_BINARY_INST(31)
133134
134135 // Memory operators...
135 FIRST_MEMORY_INST(31)
136 HANDLE_MEMORY_INST(31, Alloca, AllocaInst) // Stack management
137 HANDLE_MEMORY_INST(32, Load , LoadInst ) // Memory manipulation instrs
138 HANDLE_MEMORY_INST(33, Store , StoreInst )
139 HANDLE_MEMORY_INST(34, GetElementPtr, GetElementPtrInst)
140 HANDLE_MEMORY_INST(35, Fence , FenceInst )
141 HANDLE_MEMORY_INST(36, AtomicCmpXchg , AtomicCmpXchgInst )
142 HANDLE_MEMORY_INST(37, AtomicRMW , AtomicRMWInst )
143 LAST_MEMORY_INST(37)
136 FIRST_MEMORY_INST(32)
137 HANDLE_MEMORY_INST(32, Alloca, AllocaInst) // Stack management
138 HANDLE_MEMORY_INST(33, Load , LoadInst ) // Memory manipulation instrs
139 HANDLE_MEMORY_INST(34, Store , StoreInst )
140 HANDLE_MEMORY_INST(35, GetElementPtr, GetElementPtrInst)
141 HANDLE_MEMORY_INST(36, Fence , FenceInst )
142 HANDLE_MEMORY_INST(37, AtomicCmpXchg , AtomicCmpXchgInst )
143 HANDLE_MEMORY_INST(38, AtomicRMW , AtomicRMWInst )
144 LAST_MEMORY_INST(38)
144145
145146 // Cast operators ...
146147 // NOTE: The order matters here because CastInst::isEliminableCastPair
147148 // NOTE: (see Instructions.cpp) encodes a table based on this ordering.
148 FIRST_CAST_INST(38)
149 HANDLE_CAST_INST(38, Trunc , TruncInst ) // Truncate integers
150 HANDLE_CAST_INST(39, ZExt , ZExtInst ) // Zero extend integers
151 HANDLE_CAST_INST(40, SExt , SExtInst ) // Sign extend integers
152 HANDLE_CAST_INST(41, FPToUI , FPToUIInst ) // floating point -> UInt
153 HANDLE_CAST_INST(42, FPToSI , FPToSIInst ) // floating point -> SInt
154 HANDLE_CAST_INST(43, UIToFP , UIToFPInst ) // UInt -> floating point
155 HANDLE_CAST_INST(44, SIToFP , SIToFPInst ) // SInt -> floating point
156 HANDLE_CAST_INST(45, FPTrunc , FPTruncInst ) // Truncate floating point
157 HANDLE_CAST_INST(46, FPExt , FPExtInst ) // Extend floating point
158 HANDLE_CAST_INST(47, PtrToInt, PtrToIntInst) // Pointer -> Integer
159 HANDLE_CAST_INST(48, IntToPtr, IntToPtrInst) // Integer -> Pointer
160 HANDLE_CAST_INST(49, BitCast , BitCastInst ) // Type cast
161 HANDLE_CAST_INST(50, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
162 LAST_CAST_INST(50)
149 FIRST_CAST_INST(39)
150 HANDLE_CAST_INST(39, Trunc , TruncInst ) // Truncate integers
151 HANDLE_CAST_INST(40, ZExt , ZExtInst ) // Zero extend integers
152 HANDLE_CAST_INST(41, SExt , SExtInst ) // Sign extend integers
153 HANDLE_CAST_INST(42, FPToUI , FPToUIInst ) // floating point -> UInt
154 HANDLE_CAST_INST(43, FPToSI , FPToSIInst ) // floating point -> SInt
155 HANDLE_CAST_INST(44, UIToFP , UIToFPInst ) // UInt -> floating point
156 HANDLE_CAST_INST(45, SIToFP , SIToFPInst ) // SInt -> floating point
157 HANDLE_CAST_INST(46, FPTrunc , FPTruncInst ) // Truncate floating point
158 HANDLE_CAST_INST(47, FPExt , FPExtInst ) // Extend floating point
159 HANDLE_CAST_INST(48, PtrToInt, PtrToIntInst) // Pointer -> Integer
160 HANDLE_CAST_INST(49, IntToPtr, IntToPtrInst) // Integer -> Pointer
161 HANDLE_CAST_INST(50, BitCast , BitCastInst ) // Type cast
162 HANDLE_CAST_INST(51, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
163 LAST_CAST_INST(51)
163164
164165 // Other operators...
165 FIRST_OTHER_INST(51)
166 HANDLE_OTHER_INST(51, ICmp , ICmpInst ) // Integer comparison instruction
167 HANDLE_OTHER_INST(52, FCmp , FCmpInst ) // Floating point comparison instr.
168 HANDLE_OTHER_INST(53, PHI , PHINode ) // PHI node instruction
169 HANDLE_OTHER_INST(54, Call , CallInst ) // Call a function
170 HANDLE_OTHER_INST(55, Select , SelectInst ) // select instruction
171 HANDLE_OTHER_INST(56, UserOp1, Instruction) // May be used internally in a pass
172 HANDLE_OTHER_INST(57, UserOp2, Instruction) // Internal to passes only
173 HANDLE_OTHER_INST(58, VAArg , VAArgInst ) // vaarg instruction
174 HANDLE_OTHER_INST(59, ExtractElement, ExtractElementInst)// extract from vector
175 HANDLE_OTHER_INST(60, InsertElement, InsertElementInst) // insert into vector
176 HANDLE_OTHER_INST(61, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
177 HANDLE_OTHER_INST(62, ExtractValue, ExtractValueInst)// extract from aggregate
178 HANDLE_OTHER_INST(63, InsertValue, InsertValueInst) // insert into aggregate
179 HANDLE_OTHER_INST(64, LandingPad, LandingPadInst) // Landing pad instruction.
180 HANDLE_OTHER_INST(65, CleanupPad, CleanupPadInst)
181 LAST_OTHER_INST(65)
166 FIRST_OTHER_INST(52)
167 HANDLE_OTHER_INST(52, ICmp , ICmpInst ) // Integer comparison instruction
168 HANDLE_OTHER_INST(53, FCmp , FCmpInst ) // Floating point comparison instr.
169 HANDLE_OTHER_INST(54, PHI , PHINode ) // PHI node instruction
170 HANDLE_OTHER_INST(55, Call , CallInst ) // Call a function
171 HANDLE_OTHER_INST(56, Select , SelectInst ) // select instruction
172 HANDLE_OTHER_INST(57, UserOp1, Instruction) // May be used internally in a pass
173 HANDLE_OTHER_INST(58, UserOp2, Instruction) // Internal to passes only
174 HANDLE_OTHER_INST(59, VAArg , VAArgInst ) // vaarg instruction
175 HANDLE_OTHER_INST(60, ExtractElement, ExtractElementInst)// extract from vector
176 HANDLE_OTHER_INST(61, InsertElement, InsertElementInst) // insert into vector
177 HANDLE_OTHER_INST(62, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
178 HANDLE_OTHER_INST(63, ExtractValue, ExtractValueInst)// extract from aggregate
179 HANDLE_OTHER_INST(64, InsertValue, InsertValueInst) // insert into aggregate
180 HANDLE_OTHER_INST(65, LandingPad, LandingPadInst) // Landing pad instruction.
181 HANDLE_OTHER_INST(66, CleanupPad, CleanupPadInst)
182 LAST_OTHER_INST(66)
182183
183184 #undef FIRST_TERM_INST
184185 #undef HANDLE_TERM_INST
395395 case Instruction::CatchPad:
396396 case Instruction::CatchEndPad:
397397 case Instruction::CleanupPad:
398 case Instruction::CleanupEndPad:
398399 case Instruction::LandingPad:
399400 case Instruction::TerminatePad:
400401 return true;
40194019 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)
40204020
40214021 //===----------------------------------------------------------------------===//
4022 // CleanupEndPadInst Class
4023 //===----------------------------------------------------------------------===//
4024
4025 class CleanupEndPadInst : public TerminatorInst {
4026 private:
4027 CleanupEndPadInst(const CleanupEndPadInst &CEPI);
4028
4029 void init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB);
4030 CleanupEndPadInst(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB,
4031 unsigned Values, Instruction *InsertBefore = nullptr);
4032 CleanupEndPadInst(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB,
4033 unsigned Values, BasicBlock *InsertAtEnd);
4034
4035 protected:
4036 // Note: Instruction needs to be a friend here to call cloneImpl.
4037 friend class Instruction;
4038 CleanupEndPadInst *cloneImpl() const;
4039
4040 public:
4041 static CleanupEndPadInst *Create(CleanupPadInst *CleanupPad,
4042 BasicBlock *UnwindBB = nullptr,
4043 Instruction *InsertBefore = nullptr) {
4044 unsigned Values = UnwindBB ? 2 : 1;
4045 return new (Values)
4046 CleanupEndPadInst(CleanupPad, UnwindBB, Values, InsertBefore);
4047 }
4048 static CleanupEndPadInst *Create(CleanupPadInst *CleanupPad,
4049 BasicBlock *UnwindBB,
4050 BasicBlock *InsertAtEnd) {
4051 unsigned Values = UnwindBB ? 2 : 1;
4052 return new (Values)
4053 CleanupEndPadInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4054 }
4055
4056 /// Provide fast operand accessors
4057 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4058
4059 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4060 bool unwindsToCaller() const { return !hasUnwindDest(); }
4061
4062 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4063
4064 /// Convenience accessors
4065 CleanupPadInst *getCleanupPad() const {
4066 return cast(Op<-1>());
4067 }
4068 void setCleanupPad(CleanupPadInst *CleanupPad) {
4069 assert(CleanupPad);
4070 Op<-1>() = CleanupPad;
4071 }
4072
4073 BasicBlock *getUnwindDest() const {
4074 return hasUnwindDest() ? cast(Op<-2>()) : nullptr;
4075 }
4076 void setUnwindDest(BasicBlock *NewDest) {
4077 assert(hasUnwindDest());
4078 assert(NewDest);
4079 Op<-2>() = NewDest;
4080 }
4081
4082 // Methods for support type inquiry through isa, cast, and dyn_cast:
4083 static inline bool classof(const Instruction *I) {
4084 return (I->getOpcode() == Instruction::CleanupEndPad);
4085 }
4086 static inline bool classof(const Value *V) {
4087 return isa(V) && classof(cast(V));
4088 }
4089
4090 private:
4091 BasicBlock *getSuccessorV(unsigned Idx) const override;
4092 unsigned getNumSuccessorsV() const override;
4093 void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4094
4095 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4096 // method so that subclasses cannot accidentally use it.
4097 void setInstructionSubclassData(unsigned short D) {
4098 Instruction::setInstructionSubclassData(D);
4099 }
4100 };
4101
4102 template <>
4103 struct OperandTraits
4104 : public VariadicOperandTraits {};
4105
4106 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupEndPadInst, Value)
4107
4108 //===----------------------------------------------------------------------===//
40224109 // CleanupReturnInst Class
40234110 //===----------------------------------------------------------------------===//
40244111
250250 LLVMLandingPad = 59,
251251 LLVMCleanupRet = 61,
252252 LLVMCatchRet = 62,
253 LLVMCatchPad = 63,
254 LLVMTerminatePad = 64,
255 LLVMCleanupPad = 65,
256 LLVMCatchEndPad = 66
253 LLVMCatchPad = 63,
254 LLVMTerminatePad = 64,
255 LLVMCleanupPad = 65,
256 LLVMCatchEndPad = 66,
257 LLVMCleanupEndPad = 67
257258
258259 } LLVMOpcode;
259260
12271228 macro(CatchPadInst) \
12281229 macro(TerminatePadInst) \
12291230 macro(CatchEndPadInst) \
1231 macro(CleanupEndPadInst) \
12301232 macro(UnaryInstruction) \
12311233 macro(AllocaInst) \
12321234 macro(CastInst) \
32063206 case Instruction::CatchEndPad:
32073207 case Instruction::CatchRet:
32083208 case Instruction::CleanupPad:
3209 case Instruction::CleanupEndPad:
32093210 case Instruction::CleanupRet:
32103211 case Instruction::TerminatePad:
32113212 return false; // Misc instructions which have effects
756756 INSTKEYWORD(terminatepad, TerminatePad);
757757 INSTKEYWORD(cleanuppad, CleanupPad);
758758 INSTKEYWORD(catchendpad, CatchEndPad);
759 INSTKEYWORD(cleanupendpad, CleanupEndPad);
759760 #undef INSTKEYWORD
760761
761762 #define DWKEYWORD(TYPE, TOKEN) \
46854685 case lltok::kw_terminatepad: return ParseTerminatePad(Inst, PFS);
46864686 case lltok::kw_cleanuppad: return ParseCleanupPad(Inst, PFS);
46874687 case lltok::kw_catchendpad: return ParseCatchEndPad(Inst, PFS);
4688 case lltok::kw_cleanupendpad: return ParseCleanupEndPad(Inst, PFS);
46884689 // Binary Operators.
46894690 case lltok::kw_add:
46904691 case lltok::kw_sub:
52425243 return false;
52435244 }
52445245
5246 /// ParseCatchEndPad
5247 /// ::= 'cleanupendpad' Value unwind ('to' 'caller' | TypeAndValue)
5248 bool LLParser::ParseCleanupEndPad(Instruction *&Inst, PerFunctionState &PFS) {
5249 Value *CleanupPad = nullptr;
5250
5251 if (ParseValue(Type::getTokenTy(Context), CleanupPad, PFS, OC_CleanupPad))
5252 return true;
5253
5254 if (ParseToken(lltok::kw_unwind, "expected 'unwind' in catchendpad"))
5255 return true;
5256
5257 BasicBlock *UnwindBB = nullptr;
5258 if (Lex.getKind() == lltok::kw_to) {
5259 Lex.Lex();
5260 if (Lex.getKind() == lltok::kw_caller) {
5261 Lex.Lex();
5262 } else {
5263 return true;
5264 }
5265 } else {
5266 if (ParseTypeAndBasicBlock(UnwindBB, PFS)) {
5267 return true;
5268 }
5269 }
5270
5271 Inst = CleanupEndPadInst::Create(cast(CleanupPad), UnwindBB);
5272 return false;
5273 }
5274
52455275 //===----------------------------------------------------------------------===//
52465276 // Binary Operators.
52475277 //===----------------------------------------------------------------------===//
474474 bool ParseTerminatePad(Instruction *&Inst, PerFunctionState &PFS);
475475 bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
476476 bool ParseCatchEndPad(Instruction *&Inst, PerFunctionState &PFS);
477 bool ParseCleanupEndPad(Instruction *&Inst, PerFunctionState &PFS);
477478
478479 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
479480 unsigned OperandType);
178178
179179 kw_ret, kw_br, kw_switch, kw_indirectbr, kw_invoke, kw_resume,
180180 kw_unreachable, kw_cleanupret, kw_catchret, kw_catchpad,
181 kw_terminatepad, kw_cleanuppad, kw_catchendpad,
181 kw_terminatepad, kw_cleanuppad, kw_catchendpad, kw_cleanupendpad,
182182
183183 kw_alloca, kw_load, kw_store, kw_fence, kw_cmpxchg, kw_atomicrmw,
184184 kw_getelementptr,
39983998 InstructionList.push_back(I);
39993999 break;
40004000 }
4001 case bitc::FUNC_CODE_INST_CLEANUPENDPAD: { // CLEANUPENDPADINST: [val] or [val,bb#]
4002 if (Record.size() != 1 && Record.size() != 2)
4003 return error("Invalid record");
4004 unsigned Idx = 0;
4005 Value *CleanupPad = getValue(Record, Idx++, NextValueNo,
4006 Type::getTokenTy(Context), OC_CleanupPad);
4007 if (!CleanupPad)
4008 return error("Invalid record");
4009
4010 BasicBlock *BB = nullptr;
4011 if (Record.size() == 2) {
4012 BB = getBasicBlock(Record[Idx++]);
4013 if (!BB)
4014 return error("Invalid record");
4015 }
4016 I = CleanupEndPadInst::Create(cast(CleanupPad), BB);
4017 InstructionList.push_back(I);
4018 break;
4019 }
40014020 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
40024021 // Check magic
40034022 if ((Record[0] >> 16) == SWITCH_INST_MAGIC) {
19051905 Vals.push_back(VE.getValueID(CEPI.getUnwindDest()));
19061906 break;
19071907 }
1908 case Instruction::CleanupEndPad: {
1909 Code = bitc::FUNC_CODE_INST_CLEANUPENDPAD;
1910 const auto &CEPI = cast(I);
1911 pushValue(CEPI.getCleanupPad(), InstID, Vals, VE);
1912 if (CEPI.hasUnwindDest())
1913 Vals.push_back(VE.getValueID(CEPI.getUnwindDest()));
1914 break;
1915 }
19081916 case Instruction::Unreachable:
19091917 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
19101918 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
12021202
12031203 void SelectionDAGBuilder::visitCleanupRet(const CleanupReturnInst &I) {
12041204 report_fatal_error("visitCleanupRet not yet implemented!");
1205 }
1206
1207 void SelectionDAGBuilder::visitCleanupEndPad(const CleanupEndPadInst &I) {
1208 report_fatal_error("visitCleanupEndPad not yet implemented!");
12051209 }
12061210
12071211 void SelectionDAGBuilder::visitTerminatePad(const TerminatePadInst &TPI) {
739739 void visitSwitch(const SwitchInst &I);
740740 void visitIndirectBr(const IndirectBrInst &I);
741741 void visitUnreachable(const UnreachableInst &I);
742 void visitCleanupEndPad(const CleanupEndPadInst &I);
742743 void visitCleanupRet(const CleanupReturnInst &I);
743744 void visitCatchEndPad(const CatchEndPadInst &I);
744745 void visitCatchRet(const CatchReturnInst &I);
15731573 case Invoke: return 0;
15741574 case Resume: return 0;
15751575 case Unreachable: return 0;
1576 case CleanupEndPad: return 0;
15761577 case CleanupRet: return 0;
15771578 case CatchEndPad: return 0;
15781579 case CatchRet: return 0;
402402 } else if (First->isEHPad()) {
403403 if (!ForExplicitEH)
404404 EntryBlocks.push_back(&Fn.getEntryBlock());
405 if (!isa(First))
405 if (!isa(First) && !isa(First))
406406 EntryBlocks.push_back(&BB);
407407 ForExplicitEH = true;
408408 }
29642964 if (isa(TI) || isa(TI) ||
29652965 isa(TI))
29662966 return BB;
2967 if (auto *CEPI = dyn_cast(TI))
2968 return CEPI->getCleanupPad()->getParent();
29672969 return cast(TI)->getCleanupPad()->getParent();
29682970 }
29692971
30343036 for (const BasicBlock &BB : *ParentFn) {
30353037 if (!BB.isEHPad())
30363038 continue;
3039 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
3040 // Skip cleanupendpads; they are exits, not entries.
3041 if (isa(FirstNonPHI))
3042 continue;
30373043 // Check if the EH Pad has no exceptional successors (i.e. it unwinds to
30383044 // caller). Cleanups are a little bit of a special case because their
30393045 // control flow cannot be determined by looking at the pad but instead by
30403046 // the pad's users.
30413047 bool HasNoSuccessors = false;
3042 const Instruction *FirstNonPHI = BB.getFirstNonPHI();
30433048 if (FirstNonPHI->mayThrow()) {
30443049 HasNoSuccessors = true;
30453050 } else if (auto *CPI = dyn_cast(FirstNonPHI)) {
3046 HasNoSuccessors =
3047 CPI->use_empty() ||
3048 cast(CPI->user_back())->unwindsToCaller();
3051 if (CPI->use_empty()) {
3052 HasNoSuccessors = true;
3053 } else {
3054 const Instruction *User = CPI->user_back();
3055 if (auto *CRI = dyn_cast(User))
3056 HasNoSuccessors = CRI->unwindsToCaller();
3057 else
3058 HasNoSuccessors = cast(User)->unwindsToCaller();
3059 }
30493060 }
30503061
30513062 if (!HasNoSuccessors)
30953106 BasicBlock *Color;
30963107 std::tie(Visiting, Color) = Worklist.pop_back_val();
30973108 Instruction *VisitingHead = Visiting->getFirstNonPHI();
3098 if (VisitingHead->isEHPad() && !isa(VisitingHead)) {
3109 if (VisitingHead->isEHPad() && !isa(VisitingHead) &&
3110 !isa(VisitingHead)) {
30993111 // Mark this as a funclet head as a member of itself.
31003112 FuncletBlocks[Visiting].insert(Visiting);
31013113 // Queue exits with the parent color.
31313143 FuncletBlocks[Color].insert(Visiting);
31323144 TerminatorInst *Terminator = Visiting->getTerminator();
31333145 if (isa(Terminator) ||
3134 isa(Terminator)) {
3146 isa(Terminator) ||
3147 isa(Terminator)) {
31353148 // These block's successors have already been queued with the parent
31363149 // color.
31373150 continue;
32873300 bool IsUnreachableCatchret = false;
32883301 if (auto *CRI = dyn_cast(TI))
32893302 IsUnreachableCatchret = CRI->getCatchPad() != CatchPad;
3290 // The token consumed by a CleanupPadInst must match the funclet token.
3303 // The token consumed by a CleanupReturnInst must match the funclet token.
32913304 bool IsUnreachableCleanupret = false;
32923305 if (auto *CRI = dyn_cast(TI))
32933306 IsUnreachableCleanupret = CRI->getCleanupPad() != CleanupPad;
3294 if (IsUnreachableRet || IsUnreachableCatchret || IsUnreachableCleanupret) {
3307 // The token consumed by a CleanupEndPadInst must match the funclet token.
3308 bool IsUnreachableCleanupendpad = false;
3309 if (auto *CEPI = dyn_cast(TI))
3310 IsUnreachableCleanupendpad = CEPI->getCleanupPad() != CleanupPad;
3311 if (IsUnreachableRet || IsUnreachableCatchret ||
3312 IsUnreachableCleanupret || IsUnreachableCleanupendpad) {
32953313 new UnreachableInst(BB->getContext(), TI);
32963314 TI->eraseFromParent();
32973315 }
28882888 writeOperand(CEPI->getUnwindDest(), /*PrintType=*/true);
28892889 else
28902890 Out << "to caller";
2891 } else if (const auto *CEPI = dyn_cast(&I)) {
2892 Out << ' ';
2893 writeOperand(CEPI->getCleanupPad(), /*PrintType=*/false);
2894
2895 Out << " unwind ";
2896 if (CEPI->hasUnwindDest())
2897 writeOperand(CEPI->getUnwindDest(), /*PrintType=*/true);
2898 else
2899 Out << "to caller";
28912900 } else if (const CallInst *CI = dyn_cast(&I)) {
28922901 // Print the calling convention being used.
28932902 if (CI->getCallingConv() != CallingConv::C) {
195195 case Invoke: return "invoke";
196196 case Resume: return "resume";
197197 case Unreachable: return "unreachable";
198 case CleanupEndPad: return "cleanupendpad";
198199 case CleanupRet: return "cleanupret";
199200 case CatchEndPad: return "catchendpad";
200201 case CatchRet: return "catchret";
466467 return !CI->doesNotThrow();
467468 if (const auto *CRI = dyn_cast(this))
468469 return CRI->unwindsToCaller();
470 if (const auto *CEPI = dyn_cast(this))
471 return CEPI->unwindsToCaller();
469472 if (const auto *CEPI = dyn_cast(this))
470473 return CEPI->unwindsToCaller();
471474 if (const auto *TPI = dyn_cast(this))
670670
671671 BasicBlock *ResumeInst::getSuccessorV(unsigned idx) const {
672672 llvm_unreachable("ResumeInst has no successors!");
673 }
674
675 //===----------------------------------------------------------------------===//
676 // CleanupEndPadInst Implementation
677 //===----------------------------------------------------------------------===//
678
679 CleanupEndPadInst::CleanupEndPadInst(const CleanupEndPadInst &CEPI)
680 : TerminatorInst(CEPI.getType(), Instruction::CleanupEndPad,
681 OperandTraits::op_end(this) -
682 CEPI.getNumOperands(),
683 CEPI.getNumOperands()) {
684 setInstructionSubclassData(CEPI.getSubclassDataFromInstruction());
685 setCleanupPad(CEPI.getCleanupPad());
686 if (BasicBlock *UnwindDest = CEPI.getUnwindDest())
687 setUnwindDest(UnwindDest);
688 }
689
690 void CleanupEndPadInst::init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB) {
691 setCleanupPad(CleanupPad);
692 if (UnwindBB) {
693 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
694 setUnwindDest(UnwindBB);
695 }
696 }
697
698 CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
699 BasicBlock *UnwindBB, unsigned Values,
700 Instruction *InsertBefore)
701 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
702 Instruction::CleanupEndPad,
703 OperandTraits::op_end(this) - Values,
704 Values, InsertBefore) {
705 init(CleanupPad, UnwindBB);
706 }
707
708 CleanupEndPadInst::CleanupEndPadInst(CleanupPadInst *CleanupPad,
709 BasicBlock *UnwindBB, unsigned Values,
710 BasicBlock *InsertAtEnd)
711 : TerminatorInst(Type::getVoidTy(CleanupPad->getContext()),
712 Instruction::CleanupEndPad,
713 OperandTraits::op_end(this) - Values,
714 Values, InsertAtEnd) {
715 init(CleanupPad, UnwindBB);
716 }
717
718 BasicBlock *CleanupEndPadInst::getSuccessorV(unsigned Idx) const {
719 assert(Idx == 0);
720 return getUnwindDest();
721 }
722 unsigned CleanupEndPadInst::getNumSuccessorsV() const {
723 return getNumSuccessors();
724 }
725 void CleanupEndPadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
726 assert(Idx == 0);
727 setUnwindDest(B);
673728 }
674729
675730 //===----------------------------------------------------------------------===//
39013956
39023957 ResumeInst *ResumeInst::cloneImpl() const { return new (1) ResumeInst(*this); }
39033958
3959 CleanupEndPadInst *CleanupEndPadInst::cloneImpl() const {
3960 return new (getNumOperands()) CleanupEndPadInst(*this);
3961 }
3962
39043963 CleanupReturnInst *CleanupReturnInst::cloneImpl() const {
39053964 return new (getNumOperands()) CleanupReturnInst(*this);
39063965 }
387387 void visitCatchPadInst(CatchPadInst &CPI);
388388 void visitCatchEndPadInst(CatchEndPadInst &CEPI);
389389 void visitCleanupPadInst(CleanupPadInst &CPI);
390 void visitCleanupEndPadInst(CleanupEndPadInst &CEPI);
390391 void visitCleanupReturnInst(CleanupReturnInst &CRI);
391392 void visitTerminatePadInst(TerminatePadInst &TPI);
392393
28312832 ;
28322833 else if (isa(TI))
28332834 ;
2835 else if (isa(TI))
2836 ;
28342837 else if (isa(TI))
28352838 ;
28362839 else
29612964 "CleanupPadInst not the first non-PHI instruction in the block.",
29622965 &CPI);
29632966
2964 CleanupReturnInst *FirstCRI = nullptr;
2965 for (User *U : CPI.users())
2967 User *FirstUser = nullptr;
2968 BasicBlock *FirstUnwindDest = nullptr;
2969 for (User *U : CPI.users()) {
2970 BasicBlock *UnwindDest;
29662971 if (CleanupReturnInst *CRI = dyn_cast(U)) {
2967 if (!FirstCRI)
2968 FirstCRI = CRI;
2969 else
2970 Assert(CRI->getUnwindDest() == FirstCRI->getUnwindDest(),
2971 "Cleanuprets from same cleanuppad have different exceptional "
2972 "successors.",
2973 FirstCRI, CRI);
2974 }
2972 UnwindDest = CRI->getUnwindDest();
2973 } else {
2974 UnwindDest = cast(U)->getUnwindDest();
2975 }
2976
2977 if (!FirstUser) {
2978 FirstUser = U;
2979 FirstUnwindDest = UnwindDest;
2980 } else {
2981 Assert(UnwindDest == FirstUnwindDest,
2982 "Cleanuprets/cleanupendpads from the same cleanuppad must "
2983 "have the same unwind destination",
2984 FirstUser, U);
2985 }
2986 }
29752987
29762988 visitInstruction(CPI);
2989 }
2990
2991 void Verifier::visitCleanupEndPadInst(CleanupEndPadInst &CEPI) {
2992 visitEHPadPredecessors(CEPI);
2993
2994 BasicBlock *BB = CEPI.getParent();
2995 Function *F = BB->getParent();
2996 Assert(F->hasPersonalityFn(),
2997 "CleanupEndPadInst needs to be in a function with a personality.",
2998 &CEPI);
2999
3000 // The cleanupendpad instruction must be the first non-PHI instruction in the
3001 // block.
3002 Assert(BB->getFirstNonPHI() == &CEPI,
3003 "CleanupEndPadInst not the first non-PHI instruction in the block.",
3004 &CEPI);
3005
3006 if (BasicBlock *UnwindDest = CEPI.getUnwindDest()) {
3007 Instruction *I = UnwindDest->getFirstNonPHI();
3008 Assert(
3009 I->isEHPad() && !isa(I),
3010 "CleanupEndPad must unwind to an EH block which is not a landingpad.",
3011 &CEPI);
3012 }
3013
3014 visitTerminatorInst(CEPI);
29773015 }
29783016
29793017 void Verifier::visitCleanupReturnInst(CleanupReturnInst &CRI) {
26972697 // Nothing to do here.
26982698 }
26992699
2700 void visitCleanupEndPadInst(CleanupEndPadInst &I) {
2701 DEBUG(dbgs() << "CleanupEndPad: " << I << "\n");
2702 // Nothing to do here.
2703 }
2704
27002705 void visitGetElementPtrInst(GetElementPtrInst &I) {
27012706 handleShadowOr(I);
27022707 }
327327 CEPI->eraseFromParent();
328328 UpdatePHINodes(BB);
329329 }
330 } else if (auto *CEPI = dyn_cast(I)) {
331 if (CEPI->unwindsToCaller()) {
332 CleanupEndPadInst::Create(CEPI->getCleanupPad(), UnwindDest, CEPI);
333 CEPI->eraseFromParent();
334 UpdatePHINodes(BB);
335 }
330336 } else if (auto *TPI = dyn_cast(I)) {
331337 if (TPI->unwindsToCaller()) {
332338 SmallVector TerminatePadArgs;
33 ; RUN: sed -e s/.T4:// %s | not llvm-as -disable-output 2>&1 | FileCheck --check-prefix=CHECK4 %s
44 ; RUN: sed -e s/.T5:// %s | not llvm-as -disable-output 2>&1 | FileCheck --check-prefix=CHECK5 %s
55 ; RUN: sed -e s/.T6:// %s | not llvm-as -disable-output 2>&1 | FileCheck --check-prefix=CHECK6 %s
6 ; RUN: sed -e s/.T7:// %s | not llvm-as -disable-output 2>&1 | FileCheck --check-prefix=CHECK7 %s
7 ; RUN: sed -e s/.T8:// %s | not llvm-as -disable-output 2>&1 | FileCheck --check-prefix=CHECK8 %s
8 ; RUN: sed -e s/.T9:// %s | not llvm-as -disable-output 2>&1 | FileCheck --check-prefix=CHECK9 %s
69
710 ;T1: define void @f() {
811 ;T1: entry:
5659 ;T6: next:
5760 ;T6: %x = catchpad [] to label %entry unwind label %next
5861 ;T6: }
62
63 ;T7: define void @f() {
64 ;T7: entry:
65 ;T7: ; operator constraint requires an operator
66 ;T7: cleanupendpad undef unwind to caller
67 ;T7: ; CHECK7: [[@LINE-1]]:20: error: Cleanuppad value required in this position
68 ;T7: }
69
70 ;T8: define void @f() {
71 ;T8: entry:
72 ;T8: %x = catchpad []
73 ;T8: to label %next unwind label %entry
74 ;T8: next:
75 ;T8: ; cleanupret first operand's operator must be cleanuppad
76 ;T8: cleanupendpad %x unwind label next
77 ;T8: ; CHECK8: [[@LINE-1]]:20: error: '%x' is not a cleanuppad
78 ;T8: }
79
80 ;T9: define void @f() {
81 ;T9: entry:
82 ;T9: ; cleanupret's first operand's operator must be cleanuppad
83 ;T9: ; (forward reference case)
84 ;T9: cleanupendpad %x unwind label %next
85 ;T9: ; CHECK9: [[@LINE-1]]:20: error: '%x' is not a cleanuppad
86 ;T9: next:
87 ;T9: %x = catchpad [] to label %entry unwind label %next
88 ;T9: }
178178 bb:
179179 catchendpad unwind to caller
180180 }
181
182 define void @cleanupendpad0() personality i32 (...)* @__gxx_personality_v0 {
183 entry:
184 invoke void @_Z3quxv() optsize
185 to label %exit unwind label %pad
186 pad:
187 %cp = cleanuppad [i7 4]
188 invoke void @_Z3quxv() optsize
189 to label %stop unwind label %endpad
190 stop:
191 unreachable
192 endpad:
193 cleanupendpad %cp unwind label %pad
194 exit:
195 ret void
196 }
197
198 ; forward ref by name
199 define void @cleanupendpad1() personality i32 (...)* @__gxx_personality_v0 {
200 entry:
201 invoke void @_Z3quxv() optsize
202 to label %exit unwind label %pad
203 endpad:
204 cleanupendpad %cp unwind to caller
205 pad:
206 %cp = cleanuppad []
207 invoke void @_Z3quxv() optsize
208 to label %stop unwind label %endpad
209 stop:
210 unreachable
211 exit:
212 ret void
213 }
214
215 ; forward ref by ID
216 define void @cleanupendpad2() personality i32 (...)* @__gxx_personality_v0 {
217 entry:
218 invoke void @_Z3quxv() optsize
219 to label %exit unwind label %pad
220 endpad:
221 cleanupendpad %0 unwind label %pad
222 pad:
223 %0 = cleanuppad []
224 invoke void @_Z3quxv() optsize
225 to label %stop unwind label %endpad
226 stop:
227 unreachable
228 exit:
229 ret void
230 }
6262 resume { i8*, i32 } zeroinitializer
6363 }
6464
65 define i8 @call_with_same_range() {
66 ; CHECK-LABEL: @call_with_same_range
67 ; CHECK: tail call i8 @call_with_range
68 bitcast i8 0 to i8
69 %out = call i8 @dummy(), !range !0
70 ret i8 %out
71 }
72
6573 define i8 @invoke_with_same_range() personality i8* undef {
6674 ; CHECK-LABEL: @invoke_with_same_range()
6775 ; CHECK: tail call i8 @invoke_with_range()
7583 resume { i8*, i32 } zeroinitializer
7684 }
7785
78 define i8 @call_with_same_range() {
79 ; CHECK-LABEL: @call_with_same_range
80 ; CHECK: tail call i8 @call_with_range
81 bitcast i8 0 to i8
82 %out = call i8 @dummy(), !range !0
83 ret i8 %out
84 }
85
8686
8787
8888 declare i8 @dummy();
243243 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
244244 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
245245 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
246 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPENDPAD)
246247 STRINGIFY_CODE(FUNC_CODE, INST_CATCHENDPAD)
247248 STRINGIFY_CODE(FUNC_CODE, INST_TERMINATEPAD)
248249 STRINGIFY_CODE(FUNC_CODE, INST_PHI)