llvm.org GIT mirror llvm / 868145e
[IR] Remove terminatepad It turns out that terminatepad gives little benefit over a cleanuppad which calls the termination function. This is not sufficient to implement fully generic filters but MSVC doesn't support them which makes terminatepad a little over-designed. Depends on D15478. Differential Revision: http://reviews.llvm.org/D15479 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@255522 91177308-0d34-0410-b5e6-96231b3b80d8 David Majnemer 3 years ago
41 changed file(s) with 110 addition(s) and 771 deletion(s). Raw diff Collapse all Expand all
614614 The following new instructions are considered "exception handling pads", in that
615615 they must be the first non-phi instruction of a basic block that may be the
616616 unwind destination of an EH flow edge:
617 ``catchswitch``, ``catchpad``, ``cleanuppad``, and ``terminatepad``.
617 ``catchswitch``, ``catchpad``, and ``cleanuppad``.
618618 As with landingpads, when entering a try scope, if the
619619 frontend encounters a call site that may throw an exception, it should emit an
620620 invoke that unwinds to a ``catchswitch`` block. Similarly, inside the scope of a
621 C++ object with a destructor, invokes should unwind to a ``cleanuppad``. The
622 ``terminatepad`` instruction exists to represent ``noexcept`` and throw
623 specifications with one combined instruction. All potentially throwing calls in
624 a ``noexcept`` function should transitively unwind to a terminateblock. Throw
625 specifications are not implemented by MSVC, and are not yet supported.
621 C++ object with a destructor, invokes should unwind to a ``cleanuppad``.
626622
627623 New instructions are also used to mark the points where control is transferred
628624 out of a catch/cleanup handler (which will correspond to exits from the
633629 indicating where the active exception will unwind to next.
634630
635631 Each of these new EH pad instructions has a way to identify which action should
636 be considered after this action. The ``catchswitch`` and ``terminatepad``
637 instructions are terminators, and have a unwind destination operand analogous
638 to the unwind destination of an invoke. The ``cleanuppad`` instruction is not
632 be considered after this action. The ``catchswitch`` instruction is a terminator
633 and has an unwind destination operand analogous to the unwind destination of an
634 invoke. The ``cleanuppad`` instruction is not
639635 a terminator, so the unwind destination is stored on the ``cleanupret``
640636 instruction instead. Successfully executing a catch handler should resume
641637 normal control flow, so neither ``catchpad`` nor ``catchret`` instructions can
706702 catchret from %catch to label %return
707703
708704 lpad.terminate: ; preds = %catch.body, %lpad.catch
709 terminatepad within none [void ()* @"\01?terminate@@YAXXZ"] unwind to caller
705 cleanuppad within none []
706 call void @"\01?terminate@@YAXXZ"
707 unreachable
710708 }
711709
712710 Funclet parent tokens
724722 token that is always consumed by its immediate successor ``catchpad``
725723 instructions. This ensures that every catch handler modelled by a ``catchpad``
726724 belongs to exactly one ``catchswitch``, which models the dispatch point after a
727 C++ try. The ``terminatepad`` instruction cannot contain lexically nested
728 funclets inside the termination action, so it does not produce a token.
725 C++ try.
729726
730727 Here is an example of what this nesting looks like using some hypothetical
731728 C++ code:
50035003 ':ref:`resume `', ':ref:`catchswitch `',
50045004 ':ref:`catchret `',
50055005 ':ref:`cleanupret `',
5006 ':ref:`terminatepad `',
50075006 and ':ref:`unreachable `'.
50085007
50095008 .. _i_ret:
53875386 this operand may be the token ``none``.
53885387
53895388 The ``default`` argument is the label of another basic block beginning with a
5390 "pad" instruction, one of ``cleanuppad``, ``terminatepad``, or
5391 ``catchswitch``.
5389 "pad" instruction, one of ``cleanuppad`` or ``catchswitch``.
53925390
53935391 The ``handlers`` are a list of successor blocks that each begin with a
53945392 :ref:`catchpad ` instruction.
54725470 is undefined behavior if any descendant pads have been entered but not yet
54735471 exited.
54745472 2) implicitly via a call (which unwinds all the way to the current function's caller),
5475 or via a ``catchswitch``, ``cleanupret``, or ``terminatepad`` that unwinds to caller.
5473 or via a ``catchswitch`` or a ``cleanupret`` that unwinds to caller.
54765474 3) implicitly via an unwind edge whose destination EH pad isn't a descendant of
54775475 the ``catchpad``. When the ``catchpad`` is exited in this manner, it is
54785476 undefined behavior if the destination EH pad has a parent which is not an
55875585
55885586 cleanupret from %cleanup unwind to caller
55895587 cleanupret from %cleanup unwind label %continue
5590
5591 .. _i_terminatepad:
5592
5593 '``terminatepad``' Instruction
5594 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5595
5596 Syntax:
5597 """""""
5598
5599 ::
5600
5601 terminatepad within [*] unwind label
5602 terminatepad within [*] unwind to caller
5603
5604 Overview:
5605 """""""""
5606
5607 The '``terminatepad``' instruction is used by `LLVM's exception handling
5608 system `_ to specify that a basic block
5609 is a terminate block --- one where a personality routine may decide to
5610 terminate the program.
5611 The ``args`` correspond to whatever information the personality
5612 routine requires to know if this is an appropriate place to terminate the
5613 program. Control is transferred to the ``exception`` label if the
5614 personality routine decides not to terminate the program for the
5615 in-flight exception.
5616
5617 Arguments:
5618 """"""""""
5619
5620 The instruction takes a list of arbitrary values which are interpreted
5621 by the :ref:`personality function `.
5622
5623 The ``terminatepad`` may be given an ``exception`` label to
5624 transfer control to if the in-flight exception matches the ``args``.
5625
5626 Semantics:
5627 """"""""""
5628
5629 When the call stack is being unwound due to an exception being thrown,
5630 the exception is compared against the ``args``. If it matches,
5631 then control is transfered to the ``exception`` basic block. Otherwise,
5632 the program is terminated via personality-specific means. Typically,
5633 the first argument to ``terminatepad`` specifies what function the
5634 personality should defer to in order to terminate the program.
5635
5636 The ``terminatepad`` instruction is both a terminator and a "pad" instruction,
5637 meaning that is always the only non-phi instruction in the basic block.
5638
5639 Example:
5640 """"""""
5641
5642 .. code-block:: llvm
5643
5644 ;; A terminate block which only permits integers.
5645 terminatepad within none [i8** @_ZTIi] unwind label %continue
56465588
56475589 .. _i_unreachable:
56485590
86858627 is undefined behavior if any descendant pads have been entered but not yet
86868628 exited.
86878629 2) implicitly via a call (which unwinds all the way to the current function's caller),
8688 or via a ``catchswitch``, ``cleanupret``, or ``terminatepad`` that unwinds to caller.
8630 or via a ``catchswitch`` or a ``cleanupret`` that unwinds to caller.
86898631 3) implicitly via an unwind edge whose destination EH pad isn't a descendant of
86908632 the ``cleanuppad``. When the ``cleanuppad`` is exited in this manner, it is
86918633 undefined behavior if the destination EH pad has a parent which is not an
420420 FUNC_CODE_INST_CLEANUPRET = 48, // CLEANUPRET: [val] or [val,bb#]
421421 FUNC_CODE_INST_CATCHRET = 49, // CATCHRET: [val,bb#]
422422 FUNC_CODE_INST_CATCHPAD = 50, // CATCHPAD: [bb#,bb#,num,args...]
423 FUNC_CODE_INST_TERMINATEPAD = 51, // TERMINATEPAD: [bb#,num,args...]
424 FUNC_CODE_INST_CLEANUPPAD = 52, // CLEANUPPAD: [num,args...]
425 FUNC_CODE_INST_CATCHSWITCH = 53, // CATCHSWITCH: [num,args...] or [num,args...,bb]
423 FUNC_CODE_INST_CLEANUPPAD = 51, // CLEANUPPAD: [num,args...]
424 FUNC_CODE_INST_CATCHSWITCH = 52, // CATCHSWITCH: [num,args...] or [num,args...,bb]
425 // 53 is unused.
426426 // 54 is unused.
427427 FUNC_CODE_OPERAND_BUNDLE = 55, // OPERAND_BUNDLE: [tag#, value...]
428428 };
717717 CatchPadInst *CreateCatchPad(Value *ParentPad, ArrayRef Args,
718718 const Twine &Name = "") {
719719 return Insert(CatchPadInst::Create(ParentPad, Args), Name);
720 }
721
722 TerminatePadInst *CreateTerminatePad(Value *ParentPad,
723 BasicBlock *UnwindBB = nullptr,
724 ArrayRef Args = None,
725 const Twine &Name = "") {
726 return Insert(TerminatePadInst::Create(ParentPad, UnwindBB, Args), Name);
727720 }
728721
729722 CleanupPadInst *CreateCleanupPad(Value *ParentPad,
171171 RetTy visitCleanupReturnInst(CleanupReturnInst &I) { DELEGATE(TerminatorInst);}
172172 RetTy visitCatchReturnInst(CatchReturnInst &I) { DELEGATE(TerminatorInst); }
173173 RetTy visitCatchSwitchInst(CatchSwitchInst &I) { DELEGATE(TerminatorInst);}
174 RetTy visitTerminatePadInst(TerminatePadInst &I) { DELEGATE(TerminatorInst);}
175174 RetTy visitICmpInst(ICmpInst &I) { DELEGATE(CmpInst);}
176175 RetTy visitFCmpInst(FCmpInst &I) { DELEGATE(CmpInst);}
177176 RetTy visitAllocaInst(AllocaInst &I) { DELEGATE(UnaryInstruction);}
8686 case Instruction::CleanupRet:
8787 case Instruction::Invoke:
8888 case Instruction::Resume:
89 case Instruction::TerminatePad:
9089 return true;
9190 default:
9291 return false;
116116 HANDLE_TERM_INST ( 8, CleanupRet , CleanupReturnInst)
117117 HANDLE_TERM_INST ( 9, CatchRet , CatchReturnInst)
118118 HANDLE_TERM_INST (10, CatchSwitch , CatchSwitchInst)
119 HANDLE_TERM_INST (11, TerminatePad , TerminatePadInst)
120 LAST_TERM_INST (11)
119 LAST_TERM_INST (10)
121120
122121 // Standard binary operators...
123 FIRST_BINARY_INST(12)
124 HANDLE_BINARY_INST(12, Add , BinaryOperator)
125 HANDLE_BINARY_INST(13, FAdd , BinaryOperator)
126 HANDLE_BINARY_INST(14, Sub , BinaryOperator)
127 HANDLE_BINARY_INST(15, FSub , BinaryOperator)
128 HANDLE_BINARY_INST(16, Mul , BinaryOperator)
129 HANDLE_BINARY_INST(17, FMul , BinaryOperator)
130 HANDLE_BINARY_INST(18, UDiv , BinaryOperator)
131 HANDLE_BINARY_INST(19, SDiv , BinaryOperator)
132 HANDLE_BINARY_INST(20, FDiv , BinaryOperator)
133 HANDLE_BINARY_INST(21, URem , BinaryOperator)
134 HANDLE_BINARY_INST(22, SRem , BinaryOperator)
135 HANDLE_BINARY_INST(23, FRem , BinaryOperator)
122 FIRST_BINARY_INST(11)
123 HANDLE_BINARY_INST(11, Add , BinaryOperator)
124 HANDLE_BINARY_INST(12, FAdd , BinaryOperator)
125 HANDLE_BINARY_INST(13, Sub , BinaryOperator)
126 HANDLE_BINARY_INST(14, FSub , BinaryOperator)
127 HANDLE_BINARY_INST(15, Mul , BinaryOperator)
128 HANDLE_BINARY_INST(16, FMul , BinaryOperator)
129 HANDLE_BINARY_INST(17, UDiv , BinaryOperator)
130 HANDLE_BINARY_INST(18, SDiv , BinaryOperator)
131 HANDLE_BINARY_INST(19, FDiv , BinaryOperator)
132 HANDLE_BINARY_INST(20, URem , BinaryOperator)
133 HANDLE_BINARY_INST(21, SRem , BinaryOperator)
134 HANDLE_BINARY_INST(22, FRem , BinaryOperator)
136135
137136 // Logical operators (integer operands)
138 HANDLE_BINARY_INST(24, Shl , BinaryOperator) // Shift left (logical)
139 HANDLE_BINARY_INST(25, LShr , BinaryOperator) // Shift right (logical)
140 HANDLE_BINARY_INST(26, AShr , BinaryOperator) // Shift right (arithmetic)
141 HANDLE_BINARY_INST(27, And , BinaryOperator)
142 HANDLE_BINARY_INST(28, Or , BinaryOperator)
143 HANDLE_BINARY_INST(29, Xor , BinaryOperator)
144 LAST_BINARY_INST(29)
137 HANDLE_BINARY_INST(23, Shl , BinaryOperator) // Shift left (logical)
138 HANDLE_BINARY_INST(24, LShr , BinaryOperator) // Shift right (logical)
139 HANDLE_BINARY_INST(25, AShr , BinaryOperator) // Shift right (arithmetic)
140 HANDLE_BINARY_INST(26, And , BinaryOperator)
141 HANDLE_BINARY_INST(27, Or , BinaryOperator)
142 HANDLE_BINARY_INST(28, Xor , BinaryOperator)
143 LAST_BINARY_INST(28)
145144
146145 // Memory operators...
147 FIRST_MEMORY_INST(30)
148 HANDLE_MEMORY_INST(30, Alloca, AllocaInst) // Stack management
149 HANDLE_MEMORY_INST(31, Load , LoadInst ) // Memory manipulation instrs
150 HANDLE_MEMORY_INST(32, Store , StoreInst )
151 HANDLE_MEMORY_INST(33, GetElementPtr, GetElementPtrInst)
152 HANDLE_MEMORY_INST(34, Fence , FenceInst )
153 HANDLE_MEMORY_INST(35, AtomicCmpXchg , AtomicCmpXchgInst )
154 HANDLE_MEMORY_INST(36, AtomicRMW , AtomicRMWInst )
155 LAST_MEMORY_INST(36)
146 FIRST_MEMORY_INST(29)
147 HANDLE_MEMORY_INST(29, Alloca, AllocaInst) // Stack management
148 HANDLE_MEMORY_INST(30, Load , LoadInst ) // Memory manipulation instrs
149 HANDLE_MEMORY_INST(31, Store , StoreInst )
150 HANDLE_MEMORY_INST(32, GetElementPtr, GetElementPtrInst)
151 HANDLE_MEMORY_INST(33, Fence , FenceInst )
152 HANDLE_MEMORY_INST(34, AtomicCmpXchg , AtomicCmpXchgInst )
153 HANDLE_MEMORY_INST(35, AtomicRMW , AtomicRMWInst )
154 LAST_MEMORY_INST(35)
156155
157156 // Cast operators ...
158157 // NOTE: The order matters here because CastInst::isEliminableCastPair
159158 // NOTE: (see Instructions.cpp) encodes a table based on this ordering.
160 FIRST_CAST_INST(37)
161 HANDLE_CAST_INST(37, Trunc , TruncInst ) // Truncate integers
162 HANDLE_CAST_INST(38, ZExt , ZExtInst ) // Zero extend integers
163 HANDLE_CAST_INST(39, SExt , SExtInst ) // Sign extend integers
164 HANDLE_CAST_INST(40, FPToUI , FPToUIInst ) // floating point -> UInt
165 HANDLE_CAST_INST(41, FPToSI , FPToSIInst ) // floating point -> SInt
166 HANDLE_CAST_INST(42, UIToFP , UIToFPInst ) // UInt -> floating point
167 HANDLE_CAST_INST(43, SIToFP , SIToFPInst ) // SInt -> floating point
168 HANDLE_CAST_INST(44, FPTrunc , FPTruncInst ) // Truncate floating point
169 HANDLE_CAST_INST(45, FPExt , FPExtInst ) // Extend floating point
170 HANDLE_CAST_INST(46, PtrToInt, PtrToIntInst) // Pointer -> Integer
171 HANDLE_CAST_INST(47, IntToPtr, IntToPtrInst) // Integer -> Pointer
172 HANDLE_CAST_INST(48, BitCast , BitCastInst ) // Type cast
173 HANDLE_CAST_INST(49, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
174 LAST_CAST_INST(49)
175
176 FIRST_FUNCLETPAD_INST(50)
177 HANDLE_FUNCLETPAD_INST(50, CleanupPad, CleanupPadInst)
178 HANDLE_FUNCLETPAD_INST(51, CatchPad , CatchPadInst)
179 LAST_FUNCLETPAD_INST(51)
159 FIRST_CAST_INST(36)
160 HANDLE_CAST_INST(36, Trunc , TruncInst ) // Truncate integers
161 HANDLE_CAST_INST(37, ZExt , ZExtInst ) // Zero extend integers
162 HANDLE_CAST_INST(38, SExt , SExtInst ) // Sign extend integers
163 HANDLE_CAST_INST(39, FPToUI , FPToUIInst ) // floating point -> UInt
164 HANDLE_CAST_INST(40, FPToSI , FPToSIInst ) // floating point -> SInt
165 HANDLE_CAST_INST(41, UIToFP , UIToFPInst ) // UInt -> floating point
166 HANDLE_CAST_INST(42, SIToFP , SIToFPInst ) // SInt -> floating point
167 HANDLE_CAST_INST(43, FPTrunc , FPTruncInst ) // Truncate floating point
168 HANDLE_CAST_INST(44, FPExt , FPExtInst ) // Extend floating point
169 HANDLE_CAST_INST(45, PtrToInt, PtrToIntInst) // Pointer -> Integer
170 HANDLE_CAST_INST(46, IntToPtr, IntToPtrInst) // Integer -> Pointer
171 HANDLE_CAST_INST(47, BitCast , BitCastInst ) // Type cast
172 HANDLE_CAST_INST(48, AddrSpaceCast, AddrSpaceCastInst) // addrspace cast
173 LAST_CAST_INST(48)
174
175 FIRST_FUNCLETPAD_INST(49)
176 HANDLE_FUNCLETPAD_INST(49, CleanupPad, CleanupPadInst)
177 HANDLE_FUNCLETPAD_INST(50, CatchPad , CatchPadInst)
178 LAST_FUNCLETPAD_INST(50)
180179
181180 // Other operators...
182 FIRST_OTHER_INST(52)
183 HANDLE_OTHER_INST(52, ICmp , ICmpInst ) // Integer comparison instruction
184 HANDLE_OTHER_INST(53, FCmp , FCmpInst ) // Floating point comparison instr.
185 HANDLE_OTHER_INST(54, PHI , PHINode ) // PHI node instruction
186 HANDLE_OTHER_INST(55, Call , CallInst ) // Call a function
187 HANDLE_OTHER_INST(56, Select , SelectInst ) // select instruction
188 HANDLE_OTHER_INST(57, UserOp1, Instruction) // May be used internally in a pass
189 HANDLE_OTHER_INST(58, UserOp2, Instruction) // Internal to passes only
190 HANDLE_OTHER_INST(59, VAArg , VAArgInst ) // vaarg instruction
191 HANDLE_OTHER_INST(60, ExtractElement, ExtractElementInst)// extract from vector
192 HANDLE_OTHER_INST(61, InsertElement, InsertElementInst) // insert into vector
193 HANDLE_OTHER_INST(62, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
194 HANDLE_OTHER_INST(63, ExtractValue, ExtractValueInst)// extract from aggregate
195 HANDLE_OTHER_INST(64, InsertValue, InsertValueInst) // insert into aggregate
196 HANDLE_OTHER_INST(65, LandingPad, LandingPadInst) // Landing pad instruction.
197 LAST_OTHER_INST(65)
181 FIRST_OTHER_INST(51)
182 HANDLE_OTHER_INST(51, ICmp , ICmpInst ) // Integer comparison instruction
183 HANDLE_OTHER_INST(52, FCmp , FCmpInst ) // Floating point comparison instr.
184 HANDLE_OTHER_INST(53, PHI , PHINode ) // PHI node instruction
185 HANDLE_OTHER_INST(54, Call , CallInst ) // Call a function
186 HANDLE_OTHER_INST(55, Select , SelectInst ) // select instruction
187 HANDLE_OTHER_INST(56, UserOp1, Instruction) // May be used internally in a pass
188 HANDLE_OTHER_INST(57, UserOp2, Instruction) // Internal to passes only
189 HANDLE_OTHER_INST(58, VAArg , VAArgInst ) // vaarg instruction
190 HANDLE_OTHER_INST(59, ExtractElement, ExtractElementInst)// extract from vector
191 HANDLE_OTHER_INST(60, InsertElement, InsertElementInst) // insert into vector
192 HANDLE_OTHER_INST(61, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
193 HANDLE_OTHER_INST(62, ExtractValue, ExtractValueInst)// extract from aggregate
194 HANDLE_OTHER_INST(63, InsertValue, InsertValueInst) // insert into aggregate
195 HANDLE_OTHER_INST(64, LandingPad, LandingPadInst) // Landing pad instruction.
196 LAST_OTHER_INST(64)
198197
199198 #undef FIRST_TERM_INST
200199 #undef HANDLE_TERM_INST
395395 case Instruction::CatchPad:
396396 case Instruction::CleanupPad:
397397 case Instruction::LandingPad:
398 case Instruction::TerminatePad:
399398 return true;
400399 default:
401400 return false;
39913991 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)
39923992
39933993 //===----------------------------------------------------------------------===//
3994 // TerminatePadInst Class
3995 //===----------------------------------------------------------------------===//
3996
3997 class TerminatePadInst : public TerminatorInst {
3998 private:
3999 void init(Value *ParentPad, BasicBlock *BB, ArrayRef Args);
4000
4001 TerminatePadInst(const TerminatePadInst &TPI);
4002
4003 explicit TerminatePadInst(Value *ParentPad, BasicBlock *BB,
4004 ArrayRef Args, unsigned Values,
4005 Instruction *InsertBefore);
4006 explicit TerminatePadInst(Value *ParentPad, BasicBlock *BB,
4007 ArrayRef Args, unsigned Values,
4008 BasicBlock *InsertAtEnd);
4009
4010 protected:
4011 // Note: Instruction needs to be a friend here to call cloneImpl.
4012 friend class Instruction;
4013 TerminatePadInst *cloneImpl() const;
4014
4015 public:
4016 static TerminatePadInst *Create(Value *ParentPad, BasicBlock *BB = nullptr,
4017 ArrayRef Args = None,
4018 Instruction *InsertBefore = nullptr) {
4019 unsigned Values = unsigned(Args.size()) + 1;
4020 if (BB)
4021 ++Values;
4022 return new (Values)
4023 TerminatePadInst(ParentPad, BB, Args, Values, InsertBefore);
4024 }
4025 static TerminatePadInst *Create(Value *ParentPad, BasicBlock *BB,
4026 ArrayRef Args,
4027 BasicBlock *InsertAtEnd) {
4028 unsigned Values = unsigned(Args.size()) + 1;
4029 if (BB)
4030 ++Values;
4031 return new (Values)
4032 TerminatePadInst(ParentPad, BB, Args, Values, InsertAtEnd);
4033 }
4034
4035 /// Provide fast operand accessors
4036 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4037
4038 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4039 bool unwindsToCaller() const { return !hasUnwindDest(); }
4040
4041 /// getNumArgOperands - Return the number of terminatepad arguments.
4042 ///
4043 unsigned getNumArgOperands() const {
4044 unsigned NumOperands = getNumOperands();
4045 if (hasUnwindDest())
4046 return NumOperands - 2;
4047 return NumOperands - 1;
4048 }
4049
4050 /// Convenience accessors
4051 Value *getParentPad() const { return Op<-1>(); }
4052 void setParentPad(Value *ParentPad) {
4053 assert(ParentPad);
4054 Op<-1>() = ParentPad;
4055 }
4056
4057 /// getArgOperand/setArgOperand - Return/set the i-th terminatepad argument.
4058 ///
4059 Value *getArgOperand(unsigned i) const { return getOperand(i); }
4060 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
4061
4062 const_op_iterator arg_begin() const { return op_begin(); }
4063 op_iterator arg_begin() { return op_begin(); }
4064
4065 const_op_iterator arg_end() const {
4066 if (hasUnwindDest())
4067 return op_end() - 2;
4068 return op_end() - 1;
4069 }
4070
4071 op_iterator arg_end() {
4072 if (hasUnwindDest())
4073 return op_end() - 2;
4074 return op_end() - 1;
4075 }
4076
4077 /// arg_operands - iteration adapter for range-for loops.
4078 iterator_range arg_operands() {
4079 return make_range(arg_begin(), arg_end());
4080 }
4081
4082 /// arg_operands - iteration adapter for range-for loops.
4083 iterator_range arg_operands() const {
4084 return make_range(arg_begin(), arg_end());
4085 }
4086
4087 /// \brief Wrappers for getting the \c Use of a terminatepad argument.
4088 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
4089 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
4090
4091 // get*Dest - Return the destination basic blocks...
4092 BasicBlock *getUnwindDest() const {
4093 if (!hasUnwindDest())
4094 return nullptr;
4095 return cast(Op<-2>());
4096 }
4097 void setUnwindDest(BasicBlock *B) {
4098 assert(B && hasUnwindDest());
4099 Op<-2>() = B;
4100 }
4101
4102 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4103
4104 // Methods for support type inquiry through isa, cast, and dyn_cast:
4105 static inline bool classof(const Instruction *I) {
4106 return I->getOpcode() == Instruction::TerminatePad;
4107 }
4108 static inline bool classof(const Value *V) {
4109 return isa(V) && classof(cast(V));
4110 }
4111
4112 private:
4113 BasicBlock *getSuccessorV(unsigned idx) const override;
4114 unsigned getNumSuccessorsV() const override;
4115 void setSuccessorV(unsigned idx, BasicBlock *B) override;
4116
4117 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4118 // method so that subclasses cannot accidentally use it.
4119 void setInstructionSubclassData(unsigned short D) {
4120 Instruction::setInstructionSubclassData(D);
4121 }
4122 };
4123
4124 template <>
4125 struct OperandTraits
4126 : public VariadicOperandTraits {};
4127
4128 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(TerminatePadInst, Value)
4129
4130 //===----------------------------------------------------------------------===//
41313994 // CleanupPadInst Class
41323995 //===----------------------------------------------------------------------===//
41333996 class CleanupPadInst : public FuncletPadInst {
288288 DIBuilder &Builder, bool Deref, int Offset = 0);
289289
290290 /// Replace 'BB's terminator with one that does not have an unwind successor
291 /// block. Rewrites `invoke` to `call`, `terminatepad unwind label %foo` to
292 /// `terminatepad unwind to caller`, etc. Updates any PHIs in unwind successor.
291 /// block. Rewrites `invoke` to `call`, etc. Updates any PHIs in unwind
292 /// successor.
293293 ///
294294 /// \param BB Block whose terminator will be replaced. Its terminator must
295295 /// have an unwind successor.
251251 LLVMCleanupRet = 61,
252252 LLVMCatchRet = 62,
253253 LLVMCatchPad = 63,
254 LLVMTerminatePad = 64,
255 LLVMCleanupPad = 65,
256 LLVMCatchSwitch = 66
254 LLVMCleanupPad = 64,
255 LLVMCatchSwitch = 65
257256 } LLVMOpcode;
258257
259258 typedef enum {
12231222 macro(ResumeInst) \
12241223 macro(CleanupReturnInst) \
12251224 macro(CatchReturnInst) \
1226 macro(TerminatePadInst) \
12271225 macro(FuncletPadInst) \
12281226 macro(CatchPadInst) \
12291227 macro(CleanupPadInst) \
5656 // contain" is used to distinguish from being "transitively contained" in
5757 // a nested funclet).
5858 //
59 // Note: Despite not being funclets in the truest sense, terminatepad and
60 // catchswitch are considered to belong to their own funclet for the purposes
61 // of coloring.
59 // Note: Despite not being a funclet in the truest sense, a catchswitch is
60 // considered to belong to its own funclet for the purposes of coloring.
6261
6362 DEBUG_WITH_TYPE("winehprepare-coloring", dbgs() << "\nColoring funclets for "
6463 << F.getName() << "\n");
9797 while (IP->isEHPad()) {
9898 if (isa(IP) || isa(IP)) {
9999 ++IP;
100 } else if (auto *TPI = dyn_cast(IP)) {
101 IP = TPI->getUnwindDest()->getFirstNonPHI()->getIterator();
102100 } else if (isa(IP)) {
103101 IP = MustDominate->getFirstInsertionPt();
104102 } else {
34353435 case Instruction::CatchRet:
34363436 case Instruction::CleanupPad:
34373437 case Instruction::CleanupRet:
3438 case Instruction::TerminatePad:
34393438 return false; // Misc instructions which have effects
34403439 }
34413440 }
762762 INSTKEYWORD(catchret, CatchRet);
763763 INSTKEYWORD(catchswitch, CatchSwitch);
764764 INSTKEYWORD(catchpad, CatchPad);
765 INSTKEYWORD(terminatepad, TerminatePad);
766765 INSTKEYWORD(cleanuppad, CleanupPad);
767766 #undef INSTKEYWORD
768767
47224722 case lltok::kw_catchret: return ParseCatchRet(Inst, PFS);
47234723 case lltok::kw_catchswitch: return ParseCatchSwitch(Inst, PFS);
47244724 case lltok::kw_catchpad: return ParseCatchPad(Inst, PFS);
4725 case lltok::kw_terminatepad:return ParseTerminatePad(Inst, PFS);
47264725 case lltok::kw_cleanuppad: return ParseCleanupPad(Inst, PFS);
47274726 // Binary Operators.
47284727 case lltok::kw_add:
52845283 return false;
52855284 }
52865285
5287 /// ParseTerminatePad
5288 /// ::= 'terminatepad' within Parent ParamList 'to' TypeAndValue
5289 bool LLParser::ParseTerminatePad(Instruction *&Inst, PerFunctionState &PFS) {
5290 Value *ParentPad = nullptr;
5291
5292 if (ParseToken(lltok::kw_within, "expected 'within' after terminatepad"))
5293 return true;
5294
5295 if (Lex.getKind() != lltok::kw_none && Lex.getKind() != lltok::LocalVar &&
5296 Lex.getKind() != lltok::LocalVarID)
5297 return TokError("expected scope value for terminatepad");
5298
5299 if (ParseValue(Type::getTokenTy(Context), ParentPad, PFS))
5300 return true;
5301
5302 SmallVector Args;
5303 if (ParseExceptionArgs(Args, PFS))
5304 return true;
5305
5306 if (ParseToken(lltok::kw_unwind, "expected 'unwind' in terminatepad"))
5307 return true;
5308
5309 BasicBlock *UnwindBB = nullptr;
5310 if (Lex.getKind() == lltok::kw_to) {
5311 Lex.Lex();
5312 if (ParseToken(lltok::kw_caller, "expected 'caller' in terminatepad"))
5313 return true;
5314 } else {
5315 if (ParseTypeAndBasicBlock(UnwindBB, PFS)) {
5316 return true;
5317 }
5318 }
5319
5320 Inst = TerminatePadInst::Create(ParentPad, UnwindBB, Args);
5321 return false;
5322 }
5323
53245286 /// ParseCleanupPad
53255287 /// ::= 'cleanuppad' within Parent ParamList
53265288 bool LLParser::ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS) {
464464 bool ParseCatchRet(Instruction *&Inst, PerFunctionState &PFS);
465465 bool ParseCatchSwitch(Instruction *&Inst, PerFunctionState &PFS);
466466 bool ParseCatchPad(Instruction *&Inst, PerFunctionState &PFS);
467 bool ParseTerminatePad(Instruction *&Inst, PerFunctionState &PFS);
468467 bool ParseCleanupPad(Instruction *&Inst, PerFunctionState &PFS);
469468
470469 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
184184
185185 kw_ret, kw_br, kw_switch, kw_indirectbr, kw_invoke, kw_resume,
186186 kw_unreachable, kw_cleanupret, kw_catchswitch, kw_catchret, kw_catchpad,
187 kw_terminatepad, kw_cleanuppad,
187 kw_cleanuppad,
188188
189189 kw_alloca, kw_load, kw_store, kw_fence, kw_cmpxchg, kw_atomicrmw,
190190 kw_getelementptr,
44834483 InstructionList.push_back(I);
44844484 break;
44854485 }
4486 case bitc::FUNC_CODE_INST_TERMINATEPAD: { // TERMINATEPAD: [tok,bb#,num,(ty,val)*]
4487 // We must have, at minimum, the outer scope and the number of arguments.
4488 if (Record.size() < 2)
4489 return error("Invalid record");
4490
4491 unsigned Idx = 0;
4492
4493 Value *ParentPad =
4494 getValue(Record, Idx++, NextValueNo, Type::getTokenTy(Context));
4495
4496 unsigned NumArgOperands = Record[Idx++];
4497
4498 SmallVector Args;
4499 for (unsigned Op = 0; Op != NumArgOperands; ++Op) {
4500 Value *Val;
4501 if (getValueTypePair(Record, Idx, NextValueNo, Val))
4502 return error("Invalid record");
4503 Args.push_back(Val);
4504 }
4505
4506 BasicBlock *UnwindDest = nullptr;
4507 if (Idx + 1 == Record.size()) {
4508 UnwindDest = getBasicBlock(Record[Idx++]);
4509 if (!UnwindDest)
4510 return error("Invalid record");
4511 }
4512
4513 if (Record.size() != Idx)
4514 return error("Invalid record");
4515
4516 I = TerminatePadInst::Create(ParentPad, UnwindDest, Args);
4517 InstructionList.push_back(I);
4518 break;
4519 }
45204486 case bitc::FUNC_CODE_INST_CATCHPAD:
45214487 case bitc::FUNC_CODE_INST_CLEANUPPAD: { // [tok,num,(ty,val)*]
45224488 // We must have, at minimum, the outer scope and the number of arguments.
20242024 Vals.push_back(VE.getValueID(CatchSwitch.getUnwindDest()));
20252025 break;
20262026 }
2027 case Instruction::TerminatePad: {
2028 Code = bitc::FUNC_CODE_INST_TERMINATEPAD;
2029 const auto &TPI = cast(I);
2030
2031 pushValue(TPI.getParentPad(), InstID, Vals, VE);
2032
2033 unsigned NumArgOperands = TPI.getNumArgOperands();
2034 Vals.push_back(NumArgOperands);
2035 for (unsigned Op = 0; Op != NumArgOperands; ++Op)
2036 PushValueAndType(TPI.getArgOperand(Op), InstID, Vals, VE);
2037
2038 if (TPI.hasUnwindDest())
2039 Vals.push_back(VE.getValueID(TPI.getUnwindDest()));
2040 break;
2041 }
20422027 case Instruction::Unreachable:
20432028 Code = bitc::FUNC_CODE_INST_UNREACHABLE;
20442029 AbbrevToUse = FUNCTION_INST_UNREACHABLE_ABBREV;
12291229 /// When an invoke or a cleanupret unwinds to the next EH pad, there are
12301230 /// many places it could ultimately go. In the IR, we have a single unwind
12311231 /// destination, but in the machine CFG, we enumerate all the possible blocks.
1232 /// This function skips over imaginary basic blocks that hold catchswitch or
1233 /// terminatepad instructions, and finds all the "real" machine
1232 /// This function skips over imaginary basic blocks that hold catchswitch
1233 /// instructions, and finds all the "real" machine
12341234 /// basic block destinations. As those destinations may not be successors of
12351235 /// EHPadBB, here we also calculate the edge probability to those destinations.
12361236 /// The passed-in Prob is the edge probability to EHPadBB.
12971297 SDValue Ret =
12981298 DAG.getNode(ISD::CLEANUPRET, getCurSDLoc(), MVT::Other, getControlRoot());
12991299 DAG.setRoot(Ret);
1300 }
1301
1302 void SelectionDAGBuilder::visitTerminatePad(const TerminatePadInst &TPI) {
1303 report_fatal_error("visitTerminatePad not yet implemented!");
13041300 }
13051301
13061302 void SelectionDAGBuilder::visitCatchSwitch(const CatchSwitchInst &CSI) {
739739 void visitCatchSwitch(const CatchSwitchInst &I);
740740 void visitCatchRet(const CatchReturnInst &I);
741741 void visitCatchPad(const CatchPadInst &I);
742 void visitTerminatePad(const TerminatePadInst &TPI);
743742 void visitCleanupPad(const CleanupPadInst &CPI);
744743
745744 BranchProbability getEdgeProbability(const MachineBasicBlock *Src,
15731573 case CatchRet: return 0;
15741574 case CatchPad: return 0;
15751575 case CatchSwitch: return 0;
1576 case TerminatePad: return 0;
15771576 case CleanupPad: return 0;
15781577 case Add: return ISD::ADD;
15791578 case FAdd: return ISD::FADD;
6969 void replaceUseWithLoad(Value *V, Use &U, AllocaInst *&SpillSlot,
7070 DenseMap &Loads, Function &F);
7171 bool prepareExplicitEH(Function &F);
72 void replaceTerminatePadWithCleanup(Function &F);
7372 void colorFunclets(Function &F);
7473
7574 void demotePHIsOnFunclets(Function &F);
522521 calculateStateNumbersForInvokes(Fn, FuncInfo);
523522 }
524523
525 void WinEHPrepare::replaceTerminatePadWithCleanup(Function &F) {
526 if (Personality != EHPersonality::MSVC_CXX)
527 return;
528 for (BasicBlock &BB : F) {
529 Instruction *First = BB.getFirstNonPHI();
530 auto *TPI = dyn_cast(First);
531 if (!TPI)
532 continue;
533
534 if (TPI->getNumArgOperands() != 1)
535 report_fatal_error(
536 "Expected a unary terminatepad for MSVC C++ personalities!");
537
538 auto *TerminateFn = dyn_cast(TPI->getArgOperand(0));
539 if (!TerminateFn)
540 report_fatal_error("Function operand expected in terminatepad for MSVC "
541 "C++ personalities!");
542
543 // Insert the cleanuppad instruction.
544 auto *CPI =
545 CleanupPadInst::Create(TPI->getParentPad(), {},
546 Twine("terminatepad.for.", BB.getName()), &BB);
547
548 // Insert the call to the terminate instruction.
549 auto *CallTerminate = CallInst::Create(TerminateFn, {}, &BB);
550 CallTerminate->setDoesNotThrow();
551 CallTerminate->setDoesNotReturn();
552 CallTerminate->setCallingConv(TerminateFn->getCallingConv());
553
554 // Insert a new terminator for the cleanuppad using the same successor as
555 // the terminatepad.
556 CleanupReturnInst::Create(CPI, TPI->getUnwindDest(), &BB);
557
558 // Let's remove the terminatepad now that we've inserted the new
559 // instructions.
560 TPI->eraseFromParent();
561 }
562 }
563
564524 void WinEHPrepare::colorFunclets(Function &F) {
565525 BlockColors = colorEHFunclets(F);
566526
883843 // their existence can trick us into thinking values are alive when they are
884844 // not.
885845 removeUnreachableBlocks(F);
886
887 replaceTerminatePadWithCleanup(F);
888846
889847 // Determine which blocks are reachable from which funclet entries.
890848 colorFunclets(F);
29162916 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
29172917 }
29182918 Out << ']';
2919 } else if (const auto *TPI = dyn_cast(&I)) {
2920 Out << " within ";
2921 writeOperand(TPI->getParentPad(), /*PrintType=*/false);
2922 Out << " [";
2923 for (unsigned Op = 0, NumOps = TPI->getNumArgOperands(); Op < NumOps;
2924 ++Op) {
2925 if (Op > 0)
2926 Out << ", ";
2927 writeOperand(TPI->getArgOperand(Op), /*PrintType=*/true);
2928 }
2929 Out << "] unwind ";
2930 if (TPI->hasUnwindDest())
2931 writeOperand(TPI->getUnwindDest(), /*PrintType=*/true);
2932 else
2933 Out << "to caller";
29342919 } else if (isa(I) && !Operand) {
29352920 Out << " void";
29362921 } else if (const auto *CRI = dyn_cast(&I)) {
205205 case CatchRet: return "catchret";
206206 case CatchPad: return "catchpad";
207207 case CatchSwitch: return "catchswitch";
208 case TerminatePad: return "terminatepad";
209208
210209 // Standard binary operators...
211210 case Add: return "add";
420419 case Instruction::AtomicRMW:
421420 case Instruction::CatchPad:
422421 case Instruction::CatchRet:
423 case Instruction::TerminatePad:
424422 return true;
425423 case Instruction::Call:
426424 return !cast(this)->doesNotAccessMemory();
443441 case Instruction::AtomicRMW:
444442 case Instruction::CatchPad:
445443 case Instruction::CatchRet:
446 case Instruction::TerminatePad:
447444 return true;
448445 case Instruction::Call:
449446 return !cast(this)->onlyReadsMemory();
476473 return CRI->unwindsToCaller();
477474 if (const auto *CatchSwitch = dyn_cast(this))
478475 return CatchSwitch->unwindsToCaller();
479 if (const auto *TPI = dyn_cast(this))
480 return TPI->unwindsToCaller();
481476 return isa(this);
482477 }
483478
977977 OperandTraits::op_end(this) - Values, Values,
978978 InsertAtEnd) {
979979 init(ParentPad, Args, NameStr);
980 }
981
982 //===----------------------------------------------------------------------===//
983 // TerminatePadInst Implementation
984 //===----------------------------------------------------------------------===//
985 void TerminatePadInst::init(Value *ParentPad, BasicBlock *BB,
986 ArrayRef Args) {
987 if (BB) {
988 setInstructionSubclassData(getSubclassDataFromInstruction() | 1);
989 setUnwindDest(BB);
990 }
991 std::copy(Args.begin(), Args.end(), arg_begin());
992 setParentPad(ParentPad);
993 }
994
995 TerminatePadInst::TerminatePadInst(const TerminatePadInst &TPI)
996 : TerminatorInst(TPI.getType(), Instruction::TerminatePad,
997 OperandTraits::op_end(this) -
998 TPI.getNumOperands(),
999 TPI.getNumOperands()) {
1000 setInstructionSubclassData(TPI.getSubclassDataFromInstruction());
1001 std::copy(TPI.op_begin(), TPI.op_end(), op_begin());
1002 }
1003
1004 TerminatePadInst::TerminatePadInst(Value *ParentPad, BasicBlock *BB,
1005 ArrayRef Args, unsigned Values,
1006 Instruction *InsertBefore)
1007 : TerminatorInst(Type::getVoidTy(ParentPad->getContext()),
1008 Instruction::TerminatePad,
1009 OperandTraits::op_end(this) - Values,
1010 Values, InsertBefore) {
1011 init(ParentPad, BB, Args);
1012 }
1013
1014 TerminatePadInst::TerminatePadInst(Value *ParentPad, BasicBlock *BB,
1015 ArrayRef Args, unsigned Values,
1016 BasicBlock *InsertAtEnd)
1017 : TerminatorInst(Type::getVoidTy(ParentPad->getContext()),
1018 Instruction::TerminatePad,
1019 OperandTraits::op_end(this) - Values,
1020 Values, InsertAtEnd) {
1021 init(ParentPad, BB, Args);
1022 }
1023
1024 BasicBlock *TerminatePadInst::getSuccessorV(unsigned Idx) const {
1025 assert(Idx == 0);
1026 return getUnwindDest();
1027 }
1028 unsigned TerminatePadInst::getNumSuccessorsV() const {
1029 return getNumSuccessors();
1030 }
1031 void TerminatePadInst::setSuccessorV(unsigned Idx, BasicBlock *B) {
1032 assert(Idx == 0);
1033 return setUnwindDest(B);
1034980 }
1035981
1036982 //===----------------------------------------------------------------------===//
40243970 return new (getNumOperands()) FuncletPadInst(*this);
40253971 }
40263972
4027 TerminatePadInst *TerminatePadInst::cloneImpl() const {
4028 return new (getNumOperands()) TerminatePadInst(*this);
4029 }
4030
40313973 UnreachableInst *UnreachableInst::cloneImpl() const {
40323974 LLVMContext &Context = getContext();
40333975 return new UnreachableInst(Context);
402402 void visitCleanupPadInst(CleanupPadInst &CPI);
403403 void visitCatchSwitchInst(CatchSwitchInst &CatchSwitch);
404404 void visitCleanupReturnInst(CleanupReturnInst &CRI);
405 void visitTerminatePadInst(TerminatePadInst &TPI);
406405
407406 void VerifyCallSite(CallSite CS);
408407 void verifyMustTailCall(CallInst &CI);
28982897 if (auto *II = dyn_cast(TI)) {
28992898 Assert(II->getUnwindDest() == BB && II->getNormalDest() != BB,
29002899 "EH pad must be jumped to via an unwind edge", &I, II);
2901 } else if (!isa(TI) && !isa(TI) &&
2902 !isa(TI)) {
2900 } else if (!isa(TI) && !isa(TI)) {
29032901 Assert(false, "EH pad must be jumped to via an unwind edge", &I, TI);
29042902 }
29052903 }
30012999 BasicBlock *UnwindDest;
30023000 if (CleanupReturnInst *CRI = dyn_cast(U)) {
30033001 UnwindDest = CRI->getUnwindDest();
3004 } else if (isa(U) || isa(U) ||
3005 isaInst>(U)) {
3002 } else if (isa(U) || isaInst>(U)) {
30063003 continue;
30073004 } else {
30083005 Assert(false, "bogus cleanuppad use", &CPI);
30693066 }
30703067
30713068 visitTerminatorInst(CRI);
3072 }
3073
3074 void Verifier::visitTerminatePadInst(TerminatePadInst &TPI) {
3075 visitEHPadPredecessors(TPI);
3076
3077 BasicBlock *BB = TPI.getParent();
3078 Function *F = BB->getParent();
3079 Assert(F->hasPersonalityFn(),
3080 "TerminatePadInst needs to be in a function with a personality.",
3081 &TPI);
3082
3083 // The terminatepad instruction must be the first non-PHI instruction in the
3084 // block.
3085 Assert(BB->getFirstNonPHI() == &TPI,
3086 "TerminatePadInst not the first non-PHI instruction in the block.",
3087 &TPI);
3088
3089 if (BasicBlock *UnwindDest = TPI.getUnwindDest()) {
3090 Instruction *I = UnwindDest->getFirstNonPHI();
3091 Assert(I->isEHPad() && !isa(I),
3092 "TerminatePadInst must unwind to an EH block which is not a "
3093 "landingpad.",
3094 &TPI);
3095 }
3096
3097 auto *ParentPad = TPI.getParentPad();
3098 Assert(isa(ParentPad) || isa(ParentPad) ||
3099 isa(ParentPad) || isa(ParentPad),
3100 "TerminatePadInst has an invalid parent.", ParentPad);
3101
3102 visitTerminatorInst(TPI);
31033069 }
31043070
31053071 void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
26942694 setOrigin(&I, getCleanOrigin());
26952695 }
26962696
2697 void visitTerminatePad(TerminatePadInst &I) {
2698 DEBUG(dbgs() << "TerminatePad: " << I << "\n");
2699 // Nothing to do here.
2700 }
2701
27022697 void visitGetElementPtrInst(GetElementPtrInst &I) {
27032698 handleShadowOr(I);
27042699 }
342342 continue;
343343
344344 Instruction *Replacement = nullptr;
345 if (auto *TPI = dyn_cast(I)) {
346 if (TPI->unwindsToCaller()) {
347 SmallVector TerminatePadArgs;
348 for (Value *ArgOperand : TPI->arg_operands())
349 TerminatePadArgs.push_back(ArgOperand);
350 Replacement = TerminatePadInst::Create(TPI->getParentPad(), UnwindDest,
351 TerminatePadArgs, TPI);
352 }
353 } else if (auto *CatchSwitch = dyn_cast(I)) {
345 if (auto *CatchSwitch = dyn_cast(I)) {
354346 if (CatchSwitch->unwindsToCaller()) {
355347 auto *NewCatchSwitch = CatchSwitchInst::Create(
356348 CatchSwitch->getParentPad(), UnwindDest,
14401432 if (!I->isEHPad())
14411433 continue;
14421434
1443 if (auto *TPI = dyn_cast(I)) {
1444 if (isa(TPI->getParentPad()))
1445 TPI->setParentPad(CallSiteEHPad);
1446 } else if (auto *CatchSwitch = dyn_cast(I)) {
1435 if (auto *CatchSwitch = dyn_cast(I)) {
14471436 if (isa(CatchSwitch->getParentPad()))
14481437 CatchSwitch->setParentPad(CallSiteEHPad);
14491438 } else {
13371337 if (auto *CRI = dyn_cast(TI)) {
13381338 NewTI = CleanupReturnInst::Create(CRI->getCleanupPad(), nullptr, CRI);
13391339 UnwindDest = CRI->getUnwindDest();
1340 } else if (auto *TPI = dyn_cast(TI)) {
1341 SmallVector TerminatePadArgs;
1342 for (Value *Operand : TPI->arg_operands())
1343 TerminatePadArgs.push_back(Operand);
1344 NewTI = TerminatePadInst::Create(TPI->getParentPad(), nullptr,
1345 TerminatePadArgs, TPI);
1346 UnwindDest = TPI->getUnwindDest();
13471340 } else if (auto *CatchSwitch = dyn_cast(TI)) {
13481341 auto *NewCatchSwitch = CatchSwitchInst::Create(
13491342 CatchSwitch->getParentPad(), nullptr, CatchSwitch->getNumHandlers(),
34973497 }
34983498 } else if ((isa(TI) &&
34993499 cast(TI)->getUnwindDest() == BB) ||
3500 isa<TerminatePadInst>(TI) || isa<CatchSwitchInst>(TI)) {
3500 isa<CatchSwitchInst>(TI)) {
35013501 removeUnwindEdge(TI->getParent());
35023502 Changed = true;
35033503 } else if (isa(TI)) {
773773 invoke void @f.ccc() to label %normal unwind label %catchswitch3
774774
775775 catchswitch1:
776 %cs1 = catchswitch within none [label %catchpad1] unwind label %terminate.1
776 %cs1 = catchswitch within none [label %catchpad1] unwind to caller
777777
778778 catchpad1:
779779 catchpad within %cs1 []
801801
802802 cleanuppad1:
803803 %clean.1 = cleanuppad within none []
804 unreachable
804805 ; CHECK: %clean.1 = cleanuppad within none []
805 invoke void @f.ccc() to label %normal unwind label %terminate.2
806
807 terminate.1:
808 terminatepad within none [] unwind to caller
809 ; CHECK: terminatepad within none [] unwind to caller
810
811 terminate.2:
812 terminatepad within %clean.1 [i32* %arg1] unwind label %normal.pre
813 ; CHECK: terminatepad within %clean.1 [i32* %arg1] unwind label %normal.pre
814
815 normal.pre:
816 terminatepad within %clean.1 [i32* %arg1, i32* %arg2] unwind to caller
817 ; CHECK: terminatepad within %clean.1 [i32* %arg1, i32* %arg2] unwind to caller
806 ; CHECK-NEXT: unreachable
818807
819808 normal:
820809 ret i32 0
851840 ret i32 0
852841
853842 terminate:
854 terminatepad within %cs [] unwind to caller
855 ; CHECK: terminatepad within %cs [] unwind to caller
843 cleanuppad within %cs []
844 unreachable
845 ; CHECK: cleanuppad within %cs []
846 ; CHECK-NEXT: unreachable
856847
857848 continue:
858849 ret i32 0
9292 unreachable
9393 }
9494
95 ; CHECK-LABEL: @test3(
96 define void @test3() personality i32 (...)* @__CxxFrameHandler3 {
97 entry:
98 invoke void @f()
99 to label %invoke.cont unwind label %terminate
100
101 invoke.cont:
102 ret void
103
104 terminate:
105 ; CHECK: cleanuppad within none []
106 ; CHECK: call void @__std_terminate()
107 ; CHECK: unreachable
108 terminatepad within none [void ()* @__std_terminate] unwind to caller
109 }
110
11195 ; CHECK-LABEL: @test4(
11296 define void @test4(i1 %x) personality i32 (...)* @__CxxFrameHandler3 {
11397 entry:
122122 ret i8 0
123123 }
124124
125 define void @terminatepad0() personality i32 (...)* @__gxx_personality_v0 {
126 entry:
127 br label %try.cont
128
129 try.cont:
130 invoke void @_Z3quxv() optsize
131 to label %try.cont unwind label %bb
132 bb:
133 terminatepad within none [i7 4] unwind label %bb
134 }
135
136 define void @terminatepad1() personality i32 (...)* @__gxx_personality_v0 {
137 entry:
138 br label %try.cont
139
140 try.cont:
141 invoke void @_Z3quxv() optsize
142 to label %try.cont unwind label %bb
143 bb:
144 terminatepad within none [i7 4] unwind to caller
145 }
146
147125 define void @cleanuppad() personality i32 (...)* @__gxx_personality_v0 {
148126 entry:
149127 br label %try.cont
+0
-53
test/Transforms/Inline/PR25155.ll less more
None ; RUN: opt < %s -inline -S | FileCheck %s
1 target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
2 target triple = "x86_64-pc-windows-msvc18.0.0"
3
4 define void @f() personality i32 (...)* @__CxxFrameHandler3 {
5 entry:
6 invoke void @g()
7 to label %try.cont unwind label %catch.dispatch
8
9 catch.dispatch: ; preds = %entry
10 %cs1 = catchswitch within none [label %catch] unwind to caller
11
12 catch: ; preds = %catch.dispatch
13 %0 = catchpad within %cs1 [i8* null, i32 64, i8* null]
14 invoke void @dtor()
15 to label %invoke.cont.1 unwind label %ehcleanup
16
17 invoke.cont.1: ; preds = %catch
18 catchret from %0 to label %try.cont
19
20 try.cont: ; preds = %entry, %invoke.cont.1
21 ret void
22
23 ehcleanup:
24 %cp2 = cleanuppad within none []
25 call void @g()
26 cleanupret from %cp2 unwind to caller
27 }
28
29 ; CHECK-LABEL: define void @f(
30
31 ; CHECK: invoke void @g()
32 ; CHECK: to label %dtor.exit unwind label %terminate.i
33
34 ; CHECK: terminate.i:
35 ; CHECK-NEXT: terminatepad within %0 [void ()* @terminate] unwind label %ehcleanup
36
37 declare i32 @__CxxFrameHandler3(...)
38
39 define internal void @dtor() personality i32 (...)* @__CxxFrameHandler3 {
40 entry:
41 invoke void @g()
42 to label %invoke.cont unwind label %terminate
43
44 invoke.cont: ; preds = %entry
45 ret void
46
47 terminate: ; preds = %entry
48 terminatepad within none [void ()* @terminate] unwind to caller
49 }
50
51 declare void @g()
52 declare void @terminate()
1919
2020 pad: ; preds = %throw
2121 %phi2 = phi i8* [ %tmp96, %throw ]
22 terminatepad within none [] unwind label %blah
23
24 blah:
2522 %cs = catchswitch within none [label %unreachable] unwind label %blah2
2623
2724 unreachable:
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
7365 define i8 @invoke_with_same_range() personality i8* undef {
7466 ; CHECK-LABEL: @invoke_with_same_range()
7567 ; CHECK: tail call i8 @invoke_with_range()
8375 resume { i8*, i32 } zeroinitializer
8476 }
8577
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();
218218 cleanupret from %cp2 unwind to caller
219219 }
220220
221 ; This tests the case where a terminatepad unwinds to a cleanuppad.
222 ; I'm not sure how this case would arise, but it seems to be syntactically
223 ; legal so I'm testing it.
224 ;
225 ; CHECK-LABEL: define void @f5()
226 ; CHECK: entry:
227 ; CHECK: invoke void @g()
228 ; CHECK: to label %try.cont unwind label %terminate
229 ; CHECK: terminate:
230 ; CHECK: terminatepad within none [i7 4] unwind to caller
231 ; CHECK-NOT: cleanuppad
232 ; CHECK: try.cont:
233 ; CHECK: invoke void @g()
234 ; CHECK: to label %try.cont.1 unwind label %terminate.1
235 ; CHECK: terminate.1:
236 ; CHECK: terminatepad within none [i7 4] unwind label %ehcleanup.2
237 ; CHECK-NOT: ehcleanup.1:
238 ; CHECK: ehcleanup.2:
239 ; CHECK: [[TMP:\%.+]] = cleanuppad
240 ; CHECK: call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
241 ; CHECK: cleanupret from [[TMP]] unwind to caller
242 ; CHECK: }
243 define void @f5() personality i8* bitcast (i32 (...)* @__CxxFrameHandler3 to i8*) {
244 entry:
245 %a = alloca %struct.S2, align 1
246 invoke void @g()
247 to label %try.cont unwind label %terminate
248
249 terminate: ; preds = %entry
250 terminatepad within none [i7 4] unwind label %ehcleanup
251
252 ehcleanup: ; preds = %terminate
253 %0 = cleanuppad within none []
254 cleanupret from %0 unwind to caller
255
256 try.cont: ; preds = %entry
257 invoke void @g()
258 to label %try.cont.1 unwind label %terminate.1
259
260 terminate.1: ; preds = %try.cont
261 terminatepad within none [i7 4] unwind label %ehcleanup.1
262
263 ehcleanup.1: ; preds = %terminate.1
264 %1 = cleanuppad within none []
265 cleanupret from %1 unwind label %ehcleanup.2
266
267 ehcleanup.2: ; preds = %ehcleanup.1
268 %2 = cleanuppad within none []
269 call void @"\01??1S2@@QEAA@XZ"(%struct.S2* %a)
270 cleanupret from %2 unwind to caller
271
272 try.cont.1: ; preds = %try.cont
273 ret void
274 }
275
276221 ; This case tests simplification of an otherwise empty cleanup pad that contains
277222 ; a PHI node.
278223 ;
6464 unreachable
6565 }
6666
67 ; CHECK-LABEL: define void @test4()
68 define void @test4() personality i8* bitcast (void ()* @Personality to i8*) {
69 entry:
70 invoke void @f()
71 to label %exit unwind label %terminate.pad
72 terminate.pad:
73 ; CHECK: terminatepad within none [] unwind to caller
74 terminatepad within none [] unwind label %unreachable.unwind
75 exit:
76 ret void
77 unreachable.unwind:
78 cleanuppad within none []
79 unreachable
80 }
81
8267 ; CHECK-LABEL: define void @test5()
8368 define void @test5() personality i8* bitcast (void ()* @Personality to i8*) {
8469 entry:
258258 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
259259 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
260260 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
261 STRINGIFY_CODE(FUNC_CODE, INST_TERMINATEPAD)
262261 STRINGIFY_CODE(FUNC_CODE, INST_PHI)
263262 STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
264263 STRINGIFY_CODE(FUNC_CODE, INST_LOAD)