llvm.org GIT mirror llvm / e6e8826
Initial commit of the 'landingpad' instruction. This implements the 'landingpad' instruction. It's used to indicate that a basic block is a landing pad. There are several restrictions on its use (see LangRef.html for more detail). These restrictions allow the exception handling code to gather the information it needs in a much more sane way. This patch has the definition, implementation, C interface, parsing, and bitcode support in it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@137501 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 9 years ago
23 changed file(s) with 454 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
60216021 <resultval> = landingpad <somety> personality <type> <pers_fn> cleanup <clause>*
60226022
60236023 <clause> := catch <type> <value>
6024 <clause> := filter <type> <value> {, <type> <value>}*
6024 <clause> := filter <array constant type> <array constant>
60256025
60266026
60276027
Overview:
60406040 cleanup flag indicates that the landing pad block is a cleanup.

60416041
60426042

A clause begins with the clause type — catch

6043 or filter — and contains a list of global variables
6044 representing the "types" that may be caught or filtered respectively. The
6045 'landingpad' instruction must contain at least
6043 or filter — and contains the global variable representing the
6044 "type" that may be caught or filtered respectively. Unlike the
6045 catch clause, the filter clause takes an array constant as
6046 its argument. Use "[0 x i8**] undef" for a filter which cannot
6047 throw. The 'landingpad' instruction must contain at least
60466048 one clause or the cleanup flag.

60476049
60486050
Semantics:
60786080 catch i8** @_ZTIi
60796081 ;; A landing pad that is a cleanup.
60806082 %res = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0
6081 cleanup
6083 cleanup
60826084 ;; A landing pad which can catch an integer and can only throw a double.
60836085 %res = landingpad { i8*, i32 } personality i32 (...)* @__gxx_personality_v0
60846086 catch i8** @_ZTIi
6085 filter i8** @_ZTId
6087 filter [1 x i8**] [@_ZTId]
60866088
60876089
60886090
2121
2222 namespace llvm {
2323
24 class LandingPadInst;
2425 class TerminatorInst;
2526 class LLVMContext;
2627 class BlockAddress;
257258 /// to refer to basic block New instead of to us.
258259 void replaceSuccessorsPhiUsesWith(BasicBlock *New);
259260
261 /// isLandingPad - Return true if this basic block is a landing pad. I.e.,
262 /// it's the destination of the 'unwind' edge of an invoke instruction.
263 bool isLandingPad() const;
264
265 /// getLandingPadInst() - Return the landingpad instruction associated with
266 /// the landing pad.
267 LandingPadInst *getLandingPadInst();
268
260269 private:
261270 /// AdjustBlockAddressRefCount - BasicBlock stores the number of BlockAddress
262271 /// objects using it. This is almost always 0, sometimes one, possibly but
307307 // align, vol,
308308 // ordering, synchscope]
309309 FUNC_CODE_INST_RESUME = 39, // RESUME: [opval]
310 FUNC_CODE_INST_LOADATOMIC = 40, // LOAD: [opty, op, align, vol,
310 FUNC_CODE_INST_LANDINGPAD = 40, // LANDINGPAD: [ty,val,val,num,id0,val0...]
311 FUNC_CODE_INST_LOADATOMIC = 41, // LOAD: [opty, op, align, vol,
311312 // ordering, synchscope]
312 FUNC_CODE_INST_STOREATOMIC = 41 // STORE: [ptrty,ptr,val, align, vol
313 FUNC_CODE_INST_STOREATOMIC = 42 // STORE: [ptrty,ptr,val, align, vol
313314 // ordering, synchscope]
314315 };
315316 } // End bitc namespace
171171 HANDLE_OTHER_INST(56, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
172172 HANDLE_OTHER_INST(57, ExtractValue, ExtractValueInst)// extract from aggregate
173173 HANDLE_OTHER_INST(58, InsertValue, InsertValueInst) // insert into aggregate
174 LAST_OTHER_INST(58)
174 HANDLE_OTHER_INST(59, LandingPad, LandingPadInst) // Landing pad instruction.
175 LAST_OTHER_INST(59)
175176
176177 #undef FIRST_TERM_INST
177178 #undef HANDLE_TERM_INST
21052105
21062106 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
21072107
2108 //===----------------------------------------------------------------------===//
2109 // LandingPadInst Class
2110 //===----------------------------------------------------------------------===//
2111
2112 //===---------------------------------------------------------------------------
2113 /// LandingPadInst - The landingpad instruction holds all of the information
2114 /// necessary to generate correct exception handling. The landingpad instruction
2115 /// cannot be moved from the top of a landing pad block, which itself is
2116 /// accessible only from the 'unwind' edge of an invoke. This uses the
2117 /// SubclassData field in Value to store whether or not the landingpad is a
2118 /// cleanup.
2119 ///
2120 class LandingPadInst : public Instruction {
2121 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2122 /// the number actually in use.
2123 unsigned ReservedSpace;
2124 LandingPadInst(const LandingPadInst &LP);
2125 public:
2126 enum ClauseType { Catch, Filter };
2127 private:
2128 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2129 // Allocate space for exactly zero operands.
2130 void *operator new(size_t s) {
2131 return User::operator new(s, 0);
2132 }
2133 void growOperands(unsigned Size);
2134 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2135
2136 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2137 unsigned NumReservedValues, const Twine &NameStr,
2138 Instruction *InsertBefore);
2139 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2140 unsigned NumReservedValues, const Twine &NameStr,
2141 BasicBlock *InsertAtEnd);
2142 protected:
2143 virtual LandingPadInst *clone_impl() const;
2144 public:
2145 /// Constructors - NumReservedClauses is a hint for the number of incoming
2146 /// clauses that this landingpad will have (use 0 if you really have no idea).
2147 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2148 unsigned NumReservedClauses,
2149 const Twine &NameStr = "",
2150 Instruction *InsertBefore = 0);
2151 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2152 unsigned NumReservedClauses,
2153 const Twine &NameStr, BasicBlock *InsertAtEnd);
2154 ~LandingPadInst();
2155
2156 /// Provide fast operand accessors
2157 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2158
2159 /// getPersonalityFn - Get the personality function associated with this
2160 /// landing pad.
2161 Value *getPersonalityFn() const { return getOperand(0); }
2162
2163 /// isCleanup - Return 'true' if this landingpad instruction is a
2164 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2165 /// doesn't catch the exception.
2166 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2167
2168 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2169 void setCleanup(bool V) {
2170 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2171 (V ? 1 : 0));
2172 }
2173
2174 /// addClause - Add a catch or filter clause to the landing pad.
2175 void addClause(Value *ClauseVal);
2176
2177 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2178 /// to determine what type of clause this is.
2179 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2180
2181 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2182 bool isCatch(unsigned Idx) const {
2183 return !isa(OperandList[Idx + 1]->getType());
2184 }
2185
2186 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2187 bool isFilter(unsigned Idx) const {
2188 return isa(OperandList[Idx + 1]->getType());
2189 }
2190
2191 /// getNumClauses - Get the number of clauses for this landing pad.
2192 unsigned getNumClauses() const { return getNumOperands() - 1; }
2193
2194 /// reserveClauses - Grow the size of the operand list to accomodate the new
2195 /// number of clauses.
2196 void reserveClauses(unsigned Size) { growOperands(Size); }
2197
2198 // Methods for support type inquiry through isa, cast, and dyn_cast:
2199 static inline bool classof(const LandingPadInst *) { return true; }
2200 static inline bool classof(const Instruction *I) {
2201 return I->getOpcode() == Instruction::LandingPad;
2202 }
2203 static inline bool classof(const Value *V) {
2204 return isa(V) && classof(cast(V));
2205 }
2206 };
2207
2208 template <>
2209 struct OperandTraits : public HungoffOperandTraits<2> {
2210 };
2211
2212 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
21082213
21092214 //===----------------------------------------------------------------------===//
21102215 // ReturnInst Class
26942799 Op<-1>() = reinterpret_cast(B);
26952800 }
26962801
2802 /// getLandingPadInst - Get the landingpad instruction from the landing pad
2803 /// block (the unwind destination).
2804 LandingPadInst *getLandingPadInst() const;
2805
26972806 BasicBlock *getSuccessor(unsigned i) const {
26982807 assert(i < 2 && "Successor # out of range for invoke!");
26992808 return i == 0 ? getNormalDest() : getUnwindDest();
12031203 return Insert(InsertValueInst::Create(Agg, Val, Idxs), Name);
12041204 }
12051205
1206 LandingPadInst *CreateLandingPad(Type *Ty, Value *PersFn, unsigned NumClauses,
1207 const Twine &Name = "") {
1208 return Insert(LandingPadInst::Create(Ty, PersFn, NumClauses, Name));
1209 }
1210
12061211 //===--------------------------------------------------------------------===//
12071212 // Utility creation methods
12081213 //===--------------------------------------------------------------------===//
194194 RetTy visitShuffleVectorInst(ShuffleVectorInst &I) { DELEGATE(Instruction); }
195195 RetTy visitExtractValueInst(ExtractValueInst &I) { DELEGATE(Instruction);}
196196 RetTy visitInsertValueInst(InsertValueInst &I) { DELEGATE(Instruction); }
197 RetTy visitLandingPadInst(LandingPadInst &I) { DELEGATE(Instruction); }
197198
198199 // Next level propagators: If the user does not overload a specific
199200 // instruction type, they can overload one of these to get the whole class
190190 LLVMAtomicRMW = 57,
191191
192192 /* Exception Handling Operators */
193 LLVMResume = 58
193 LLVMResume = 58,
194 LLVMLandingPad = 59
194195
195196 } LLVMOpcode;
196197
281282 LLVMRealUNE, /**< True if unordered or not equal */
282283 LLVMRealPredicateTrue /**< Always true (always folded) */
283284 } LLVMRealPredicate;
285
286 typedef enum {
287 LLVMLandingPadCatch, /**< A catch clause */
288 LLVMLandingPadFilter /**< A filter clause */
289 } LLVMLandingPadClauseTy;
284290
285291 void LLVMInitializeCore(LLVMPassRegistryRef R);
286292
468474 macro(GetElementPtrInst) \
469475 macro(InsertElementInst) \
470476 macro(InsertValueInst) \
477 macro(LandingPadInst) \
471478 macro(PHINode) \
472479 macro(SelectInst) \
473480 macro(ShuffleVectorInst) \
836843 /* Add a destination to the indirectbr instruction */
837844 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest);
838845
846 /* Add a catch or filter clause to the landingpad instruction */
847 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal);
848
849 /* Set the 'cleanup' flag in the landingpad instruction */
850 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val);
851
839852 /* Arithmetic */
840853 LLVMValueRef LLVMBuildAdd(LLVMBuilderRef, LLVMValueRef LHS, LLVMValueRef RHS,
841854 const char *Name);
583583
584584 KEYWORD(x);
585585 KEYWORD(blockaddress);
586
587 KEYWORD(personality);
588 KEYWORD(cleanup);
589 KEYWORD(catch);
590 KEYWORD(filter);
586591 #undef KEYWORD
587592
588593 // Keywords for types.
652657 INSTKEYWORD(shufflevector, ShuffleVector);
653658 INSTKEYWORD(extractvalue, ExtractValue);
654659 INSTKEYWORD(insertvalue, InsertValue);
660 INSTKEYWORD(landingpad, LandingPad);
655661 #undef INSTKEYWORD
656662
657663 // Check for [us]0x[0-9A-Fa-f]+ which are Hexadecimal constant generated by
29442944 case lltok::kw_insertelement: return ParseInsertElement(Inst, PFS);
29452945 case lltok::kw_shufflevector: return ParseShuffleVector(Inst, PFS);
29462946 case lltok::kw_phi: return ParsePHI(Inst, PFS);
2947 case lltok::kw_landingpad: return ParseLandingPad(Inst, PFS);
29472948 case lltok::kw_call: return ParseCall(Inst, PFS, false);
29482949 case lltok::kw_tail: return ParseCall(Inst, PFS, true);
29492950 // Memory.
35163517 PN->addIncoming(PHIVals[i].first, PHIVals[i].second);
35173518 Inst = PN;
35183519 return AteExtraComma ? InstExtraComma : InstNormal;
3520 }
3521
3522 /// ParseLandingPad
3523 /// ::= 'landingpad' Type 'personality' TypeAndValue 'cleanup'? Clause+
3524 /// Clause
3525 /// ::= 'catch' TypeAndValue
3526 /// ::= 'filter'
3527 /// ::= 'filter' TypeAndValue ( ',' TypeAndValue )*
3528 bool LLParser::ParseLandingPad(Instruction *&Inst, PerFunctionState &PFS) {
3529 Type *Ty = 0; LocTy TyLoc;
3530 Value *PersFn; LocTy PersFnLoc;
3531 LocTy LPLoc = Lex.getLoc();
3532
3533 if (ParseType(Ty, TyLoc) ||
3534 ParseToken(lltok::kw_personality, "expected 'personality'") ||
3535 ParseTypeAndValue(PersFn, PersFnLoc, PFS))
3536 return true;
3537
3538 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, 0);
3539 LP->setCleanup(EatIfPresent(lltok::kw_cleanup));
3540
3541 while (Lex.getKind() == lltok::kw_catch || Lex.getKind() == lltok::kw_filter){
3542 LandingPadInst::ClauseType CT;
3543 if (EatIfPresent(lltok::kw_catch))
3544 CT = LandingPadInst::Catch;
3545 else if (EatIfPresent(lltok::kw_filter))
3546 CT = LandingPadInst::Filter;
3547 else
3548 return TokError("expected 'catch' or 'filter' clause type");
3549
3550 Value *V; LocTy VLoc;
3551 if (ParseTypeAndValue(V, VLoc, PFS)) {
3552 delete LP;
3553 return true;
3554 }
3555
3556 LP->addClause(V);
3557 }
3558
3559 Inst = LP;
3560 return false;
35193561 }
35203562
35213563 /// ParseCall
359359 bool ParseInsertElement(Instruction *&I, PerFunctionState &PFS);
360360 bool ParseShuffleVector(Instruction *&I, PerFunctionState &PFS);
361361 int ParsePHI(Instruction *&I, PerFunctionState &PFS);
362 bool ParseLandingPad(Instruction *&I, PerFunctionState &PFS);
362363 bool ParseCall(Instruction *&I, PerFunctionState &PFS, bool isTail);
363364 int ParseAlloc(Instruction *&I, PerFunctionState &PFS);
364365 int ParseLoad(Instruction *&I, PerFunctionState &PFS,
123123 kw_fptoui, kw_fptosi, kw_inttoptr, kw_ptrtoint, kw_bitcast,
124124 kw_select, kw_va_arg,
125125
126 kw_landingpad, kw_personality, kw_cleanup, kw_catch, kw_filter,
127
126128 kw_ret, kw_br, kw_switch, kw_indirectbr, kw_invoke, kw_unwind, kw_resume,
127129 kw_unreachable,
128130
25422542 break;
25432543 }
25442544
2545 case bitc::FUNC_CODE_INST_LANDINGPAD: {
2546 // LANDINGPAD: [ty, val, val, num, (id0,val0 ...)?]
2547 unsigned Idx = 0;
2548 if (Record.size() < 4)
2549 return Error("Invalid LANDINGPAD record");
2550 Type *Ty = getTypeByID(Record[Idx++]);
2551 if (!Ty) return Error("Invalid LANDINGPAD record");
2552 Value *PersFn = 0;
2553 if (getValueTypePair(Record, Idx, NextValueNo, PersFn))
2554 return Error("Invalid LANDINGPAD record");
2555
2556 bool IsCleanup = !!Record[Idx++];
2557 unsigned NumClauses = Record[Idx++];
2558 LandingPadInst *LP = LandingPadInst::Create(Ty, PersFn, NumClauses);
2559 LP->setCleanup(IsCleanup);
2560 for (unsigned J = 0; J != NumClauses; ++J) {
2561 LandingPadInst::ClauseType CT =
2562 LandingPadInst::ClauseType(Record[Idx++]); (void)CT;
2563 Value *Val;
2564
2565 if (getValueTypePair(Record, Idx, NextValueNo, Val)) {
2566 delete LP;
2567 return Error("Invalid LANDINGPAD record");
2568 }
2569
2570 assert((CT != LandingPadInst::Catch ||
2571 !isa(Val->getType())) &&
2572 "Catch clause has a invalid type!");
2573 assert((CT != LandingPadInst::Filter ||
2574 isa(Val->getType())) &&
2575 "Filter clause has invalid type!");
2576 LP->addClause(Val);
2577 }
2578
2579 I = LP;
2580 break;
2581 }
2582
25452583 case bitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [instty, opty, op, align]
25462584 if (Record.size() != 4)
25472585 return Error("Invalid ALLOCA record");
11641164 break;
11651165 }
11661166
1167 case Instruction::LandingPad: {
1168 const LandingPadInst &LP = cast(I);
1169 Code = bitc::FUNC_CODE_INST_LANDINGPAD;
1170 Vals.push_back(VE.getTypeID(LP.getType()));
1171 PushValueAndType(LP.getPersonalityFn(), InstID, Vals, VE);
1172 Vals.push_back(LP.isCleanup());
1173 Vals.push_back(LP.getNumClauses());
1174 for (unsigned I = 0, E = LP.getNumClauses(); I != E; ++I) {
1175 if (LP.isCatch(I))
1176 Vals.push_back(LandingPadInst::Catch);
1177 else
1178 Vals.push_back(LandingPadInst::Filter);
1179 PushValueAndType(LP.getClause(I), InstID, Vals, VE);
1180 }
1181 break;
1182 }
1183
11671184 case Instruction::Alloca:
11681185 Code = bitc::FUNC_CODE_INST_ALLOCA;
11691186 Vals.push_back(VE.getTypeID(I.getType()));
29822982 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
29832983 DAG.getVTList(&ValValueVTs[0], NumValValues),
29842984 &Values[0], NumValValues));
2985 }
2986
2987 void SelectionDAGBuilder::visitLandingPad(const LandingPadInst &I) {
29852988 }
29862989
29872990 void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
511511
512512 void visitExtractValue(const ExtractValueInst &I);
513513 void visitInsertValue(const InsertValueInst &I);
514 void visitLandingPad(const LandingPadInst &I);
514515
515516 void visitGetElementPtr(const User &I);
516517 void visitSelect(const User &I);
514514 void visitShuffleVectorInst(ShuffleVectorInst &I);
515515 void visitExtractValueInst(ExtractValueInst &EVI);
516516 void visitInsertValueInst(InsertValueInst &IVI);
517 void visitLandingPadInst(LandingPadInst &I) { markAnythingOverdefined(&I); }
517518
518519 // Instructions that cannot be folded away.
519520 void visitStoreInst (StoreInst &I);
17471747 writeOperand(I.getOperand(1), true);
17481748 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
17491749 Out << ", " << *i;
1750 } else if (const LandingPadInst *LPI = dyn_cast(&I)) {
1751 Out << ' ';
1752 TypePrinter.print(I.getType(), Out);
1753 Out << " personality ";
1754 writeOperand(I.getOperand(0), true); Out << '\n';
1755
1756 if (LPI->isCleanup())
1757 Out << " cleanup";
1758
1759 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
1760 if (i != 0 || LPI->isCleanup()) Out << "\n";
1761 if (LPI->isCatch(i))
1762 Out << " catch ";
1763 else
1764 Out << " filter ";
1765
1766 writeOperand(LPI->getClause(i), true);
1767 }
17501768 } else if (isa(I) && !Operand) {
17511769 Out << " void";
17521770 } else if (const CallInst *CI = dyn_cast(&I)) {
347347 }
348348 }
349349 }
350
351 /// isLandingPad - Return true if this basic block is a landing pad. I.e., it's
352 /// the destination of the 'unwind' edge of an invoke instruction.
353 bool BasicBlock::isLandingPad() const {
354 return isa(getFirstNonPHI());
355 }
356
357 /// getLandingPadInst() - Return the landingpad instruction associated with
358 /// the landing pad.
359 LandingPadInst *BasicBlock::getLandingPadInst() {
360 return dyn_cast(getFirstNonPHI());
361 }
16821682 Name));
16831683 }
16841684
1685 LLVMValueRef LLVMBuildLandingPad(LLVMBuilderRef B, LLVMTypeRef Ty,
1686 LLVMValueRef PersFn, unsigned NumClauses,
1687 const char *Name) {
1688 return wrap(unwrap(B)->CreateLandingPad(unwrap(Ty),
1689 cast(unwrap(PersFn)),
1690 NumClauses, Name));
1691 }
1692
16851693 LLVMValueRef LLVMBuildResume(LLVMBuilderRef B, LLVMValueRef Exn) {
16861694 return wrap(unwrap(B)->CreateResume(unwrap(Exn)));
16871695 }
16971705
16981706 void LLVMAddDestination(LLVMValueRef IndirectBr, LLVMBasicBlockRef Dest) {
16991707 unwrap(IndirectBr)->addDestination(unwrap(Dest));
1708 }
1709
1710 void LLVMAddClause(LLVMValueRef LandingPad, LLVMValueRef ClauseVal) {
1711 unwrap(LandingPad)->
1712 addClause(cast(unwrap(ClauseVal)));
1713 }
1714
1715 void LLVMSetCleanup(LLVMValueRef LandingPad, LLVMBool Val) {
1716 unwrap(LandingPad)->setCleanup(Val);
17001717 }
17011718
17021719 /*--.. Arithmetic ..........................................................--*/
161161 case ShuffleVector: return "shufflevector";
162162 case ExtractValue: return "extractvalue";
163163 case InsertValue: return "insertvalue";
164 case LandingPad: return "landingpad";
164165
165166 default: return " ";
166167 }
165165 return ConstantValue;
166166 }
167167
168 //===----------------------------------------------------------------------===//
169 // LandingPadInst Implementation
170 //===----------------------------------------------------------------------===//
171
172 LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
173 unsigned NumReservedValues, const Twine &NameStr,
174 Instruction *InsertBefore)
175 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertBefore) {
176 init(PersonalityFn, 1 + NumReservedValues, NameStr);
177 }
178
179 LandingPadInst::LandingPadInst(Type *RetTy, Value *PersonalityFn,
180 unsigned NumReservedValues, const Twine &NameStr,
181 BasicBlock *InsertAtEnd)
182 : Instruction(RetTy, Instruction::LandingPad, 0, 0, InsertAtEnd) {
183 init(PersonalityFn, 1 + NumReservedValues, NameStr);
184 }
185
186 LandingPadInst::LandingPadInst(const LandingPadInst &LP)
187 : Instruction(LP.getType(), Instruction::LandingPad,
188 allocHungoffUses(LP.getNumOperands()), LP.getNumOperands()),
189 ReservedSpace(LP.getNumOperands()) {
190 Use *OL = OperandList, *InOL = LP.OperandList;
191 for (unsigned I = 0, E = ReservedSpace; I != E; ++I)
192 OL[I] = InOL[I];
193
194 setCleanup(LP.isCleanup());
195 }
196
197 LandingPadInst::~LandingPadInst() {
198 dropHungoffUses();
199 }
200
201 LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
202 unsigned NumReservedClauses,
203 const Twine &NameStr,
204 Instruction *InsertBefore) {
205 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
206 InsertBefore);
207 }
208
209 LandingPadInst *LandingPadInst::Create(Type *RetTy, Value *PersonalityFn,
210 unsigned NumReservedClauses,
211 const Twine &NameStr,
212 BasicBlock *InsertAtEnd) {
213 return new LandingPadInst(RetTy, PersonalityFn, NumReservedClauses, NameStr,
214 InsertAtEnd);
215 }
216
217 void LandingPadInst::init(Value *PersFn, unsigned NumReservedValues,
218 const Twine &NameStr) {
219 ReservedSpace = NumReservedValues;
220 NumOperands = 1;
221 OperandList = allocHungoffUses(ReservedSpace);
222 OperandList[0] = PersFn;
223 setName(NameStr);
224 setCleanup(false);
225 }
226
227 /// growOperands - grow operands - This grows the operand list in response to a
228 /// push_back style of operation. This grows the number of ops by 2 times.
229 void LandingPadInst::growOperands(unsigned Size) {
230 unsigned e = getNumOperands();
231 if (ReservedSpace >= e + Size) return;
232 ReservedSpace = (e + Size / 2) * 2;
233
234 Use *NewOps = allocHungoffUses(ReservedSpace);
235 Use *OldOps = OperandList;
236 for (unsigned i = 0; i != e; ++i)
237 NewOps[i] = OldOps[i];
238
239 OperandList = NewOps;
240 Use::zap(OldOps, OldOps + e, true);
241 }
242
243 void LandingPadInst::addClause(Value *Val) {
244 unsigned OpNo = getNumOperands();
245 growOperands(1);
246 assert(OpNo < ReservedSpace && "Growing didn't work!");
247 ++NumOperands;
248 OperandList[OpNo] = Val;
249 }
168250
169251 //===----------------------------------------------------------------------===//
170252 // CallInst Implementation
493575 setAttributes(PAL);
494576 }
495577
578 LandingPadInst *InvokeInst::getLandingPadInst() const {
579 return cast(getUnwindDest()->getFirstNonPHI());
580 }
496581
497582 //===----------------------------------------------------------------------===//
498583 // ReturnInst Implementation
33433428 return new PHINode(*this);
33443429 }
33453430
3431 LandingPadInst *LandingPadInst::clone_impl() const {
3432 return new LandingPadInst(*this);
3433 }
3434
33463435 ReturnInst *ReturnInst::clone_impl() const {
33473436 return new(getNumOperands()) ReturnInst(*this);
33483437 }
3434 // * It is illegal to have a ret instruction that returns a value that does not
3535 // agree with the function return value type.
3636 // * Function call argument types match the function prototype
37 // * A landing pad is defined by a landingpad instruction, and can be jumped to
38 // only by the unwind edge of an invoke instruction.
39 // * A landingpad instruction must be the first non-PHI instruction in the
40 // block.
41 // * All landingpad instructions must use the same personality function with
42 // the same function.
3743 // * All other things that are tested by asserts spread about the code...
3844 //
3945 //===----------------------------------------------------------------------===//
130136 /// already.
131137 SmallPtrSet MDNodes;
132138
139 /// PersonalityFn - The personality function referenced by the
140 /// LandingPadInsts. All LandingPadInsts within the same function must use
141 /// the same personality function.
142 const Value *PersonalityFn;
143
133144 Verifier()
134 : FunctionPass(ID),
135 Broken(false), RealPass(true), action(AbortProcessAction),
136 Mod(0), Context(0), DT(0), MessagesStr(Messages) {
137 initializeVerifierPass(*PassRegistry::getPassRegistry());
138 }
145 : FunctionPass(ID), Broken(false), RealPass(true),
146 action(AbortProcessAction), Mod(0), Context(0), DT(0),
147 MessagesStr(Messages), PersonalityFn(0) {
148 initializeVerifierPass(*PassRegistry::getPassRegistry());
149 }
139150 explicit Verifier(VerifierFailureAction ctn)
140 : FunctionPass(ID),
141 Broken(false), RealPass(true), action(ctn), Mod(0), Context(0), DT(0),
142 MessagesStr(Messages) {
143 initializeVerifierPass(*PassRegistry::getPassRegistry());
144 }
151 : FunctionPass(ID), Broken(false), RealPass(true), action(ctn), Mod(0),
152 Context(0), DT(0), MessagesStr(Messages), PersonalityFn(0) {
153 initializeVerifierPass(*PassRegistry::getPassRegistry());
154 }
145155
146156 bool doInitialization(Module &M) {
147157 Mod = &M;
164174
165175 visit(F);
166176 InstsInThisBlock.clear();
177 PersonalityFn = 0;
167178
168179 // If this is a real pass, in a pass manager, we must abort before
169180 // returning back to the pass manager, or else the pass manager may try to
283294 void visitAllocaInst(AllocaInst &AI);
284295 void visitExtractValueInst(ExtractValueInst &EVI);
285296 void visitInsertValueInst(InsertValueInst &IVI);
297 void visitLandingPadInst(LandingPadInst &LPI);
286298
287299 void VerifyCallSite(CallSite CS);
288300 bool PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty,
14001412 visitInstruction(IVI);
14011413 }
14021414
1415 void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
1416 BasicBlock *BB = LPI.getParent();
1417
1418 // The landingpad instruction is ill-formed if it doesn't have any clauses and
1419 // isn't a cleanup.
1420 Assert1(LPI.getNumClauses() > 0 || LPI.isCleanup(),
1421 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
1422
1423 // The landingpad instruction defines its parent as a landing pad block. The
1424 // landing pad block may be branched to only by the unwind edge of an invoke.
1425 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1426 const InvokeInst *II = dyn_cast((*I)->getTerminator());
1427 Assert1(II && II->getUnwindDest() == BB,
1428 "Block containing LandingPadInst must be jumped to "
1429 "only by the unwind edge of an invoke.", &LPI);
1430 }
1431
1432 // The landingpad instruction must be the first non-PHI instruction in the
1433 // block.
1434 Assert1(LPI.getParent()->getLandingPadInst() == &LPI,
1435 "LandingPadInst not the first non-PHI instruction in the block.",
1436 &LPI);
1437
1438 // The personality functions for all landingpad instructions within the same
1439 // function should match.
1440 if (PersonalityFn)
1441 Assert1(LPI.getPersonalityFn() == PersonalityFn,
1442 "Personality function doesn't match others in function", &LPI);
1443 PersonalityFn = LPI.getPersonalityFn();
1444
1445 visitInstruction(LPI);
1446 }
1447
14031448 /// verifyInstruction - Verify that an instruction is well formed.
14041449 ///
14051450 void Verifier::visitInstruction(Instruction &I) {