llvm.org GIT mirror llvm / 280a6e6
Remove 'unwinds to' support from mainline. This patch undoes r47802 r47989 r48047 r48084 r48085 r48086 r48088 r48096 r48099 r48109 and r48123. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@50265 91177308-0d34-0410-b5e6-96231b3b80d8 Nick Lewycky 12 years ago
28 changed file(s) with 62 addition(s) and 339 deletion(s). Raw diff Collapse all Expand all
117117 block_iterator block_begin() const { return Blocks.begin(); }
118118 block_iterator block_end() const { return Blocks.end(); }
119119
120 /// isLoopExit - True if this block can branch to another block that is
121 /// outside of the current loop.
120 /// isLoopExit - True if terminator in the block can branch to another block
121 /// that is outside of the current loop.
122122 ///
123123 bool isLoopExit(const BlockT *BB) const {
124124 typedef GraphTraits BlockTraits;
4848 /// modifying a program. However, the verifier will ensure that basic blocks
4949 /// are "well formed".
5050 /// @brief LLVM Basic Block Representation
51 class BasicBlock : public User { // Basic blocks are data objects also
51 class BasicBlock : public Value { // Basic blocks are data objects also
5252 public:
5353 typedef iplist InstListType;
5454 private :
5555 InstListType InstList;
5656 BasicBlock *Prev, *Next; // Next and Prev links for our intrusive linked list
5757 Function *Parent;
58 Use unwindDest;
5958
6059 void setParent(Function *parent);
6160 void setNext(BasicBlock *N) { Next = N; }
7069 /// InsertBefore is null), or before the specified basic block.
7170 ///
7271 explicit BasicBlock(const std::string &Name = "", Function *Parent = 0,
73 BasicBlock *InsertBefore = 0, BasicBlock *UnwindDest = 0);
72 BasicBlock *InsertBefore = 0);
7473 public:
7574 /// Instruction iterators...
7675 typedef InstListType::iterator iterator;
7877
7978 // allocate space for exactly zero operands
8079 static BasicBlock *Create(const std::string &Name = "", Function *Parent = 0,
81 BasicBlock *InsertBefore = 0, BasicBlock *UnwindDest = 0) {
82 return new(!!UnwindDest) BasicBlock(Name, Parent, InsertBefore, UnwindDest);
80 BasicBlock *InsertBefore = 0) {
81 return new BasicBlock(Name, Parent, InsertBefore);
8382 }
8483 ~BasicBlock();
85
86 /// getUnwindDest - Returns the BasicBlock that flow will enter if an unwind
87 /// instruction occurs in this block. May be null, in which case unwinding
88 /// is undefined in this block.
89 const BasicBlock *getUnwindDest() const;
90 BasicBlock *getUnwindDest();
91
92 /// setUnwindDest - Set which BasicBlock flow will enter if an unwind is
93 /// executed within this block. It may be set to null if unwinding is not
94 /// permitted in this block.
95 void setUnwindDest(BasicBlock *unwindDest);
9684
9785 /// getParent - Return the enclosing method, or null if none
9886 ///
9987 const Function *getParent() const { return Parent; }
10088 Function *getParent() { return Parent; }
10189
90 /// use_back - Specialize the methods defined in Value, as we know that an
91 /// BasicBlock can only be used by Instructions (specifically PHI and terms).
92 Instruction *use_back() { return cast(*use_begin());}
93 const Instruction *use_back() const { return cast(*use_begin());}
94
10295 /// getTerminator() - If this is a well formed basic block, then this returns
10396 /// a pointer to the terminator instruction. If it is not, then you get a
10497 /// null pointer back.
186179 /// update the PHI nodes that reside in the block. Note that this should be
187180 /// called while the predecessor still refers to this block.
188181 ///
189 /// DontDeleteUselessPHIs will keep PHIs that have one value or the same
190 /// value for all entries.
191 ///
192 /// OnlyDeleteOne will only remove one entry from a PHI, in case there were
193 /// duplicate entries for the Pred.
194 ///
195 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false,
196 bool OnlyDeleteOne = false);
182 void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs = false);
197183
198184 /// splitBasicBlock - This splits a basic block into two at the specified
199185 /// instruction. Note that all instructions BEFORE the specified iterator
201201 // this is so information only available in the pointer type (e.g. address
202202 // spaces) is retained.
203203 FUNC_CODE_INST_STORE2 = 24, // STORE: [ptrty,ptr,val, align, vol]
204 FUNC_CODE_INST_GETRESULT = 25, // GETRESULT: [ty, opval, n]
205
206 FUNC_CODE_INST_BB_UNWINDDEST = 26 // BB_UNWINDDEST: [bb#]
204 FUNC_CODE_INST_GETRESULT = 25 // GETRESULT: [ty, opval, n]
207205 };
208206 } // End bitc namespace
209207 } // End llvm namespace
3333 typedef PredIterator<_Ptr,_USE_iterator> _Self;
3434 typedef typename super::pointer pointer;
3535
36 inline void advancePastNonPreds() {
37 // Loop to ignore non predecessor uses (for example PHI nodes)...
38 while (!It.atEnd()) {
39 if (isa(*It) || isa(*It))
40 break;
36 inline void advancePastNonTerminators() {
37 // Loop to ignore non terminator uses (for example PHI nodes)...
38 while (!It.atEnd() && !isa(*It))
4139 ++It;
42 }
4340 }
4441
4542 inline PredIterator(_Ptr *bb) : It(bb->use_begin()) {
46 advancePastNonPreds();
43 advancePastNonTerminators();
4744 }
4845 inline PredIterator(_Ptr *bb, bool) : It(bb->use_end()) {}
4946
5249
5350 inline pointer operator*() const {
5451 assert(!It.atEnd() && "pred_iterator out of range!");
55 if (isa(*It)) // not dyn_cast due to const-correctness
56 return cast(*It)->getParent();
57
58 return cast<_Ptr>(*It);
52 return cast<TerminatorInst>(*It)->getParent();
5953 }
6054 inline pointer *operator->() const { return &(operator*()); }
6155
6256 inline _Self& operator++() { // Preincrement
6357 assert(!It.atEnd() && "pred_iterator out of range!");
64 ++It; advancePastNonPreds();
58 ++It; advancePastNonTerminators();
6559 return *this;
6660 }
6761
10599 inline SuccIterator(Term_ T, bool) // end iterator
106100 : Term(T), idx(Term->getNumSuccessors()) {
107101 assert(T && "getTerminator returned null!");
108 if (Term->getParent()->getUnwindDest())
109 ++idx;
110102 }
111103
112104 inline const _Self &operator=(const _Self &I) {
122114 inline bool operator==(const _Self& x) const { return idx == x.idx; }
123115 inline bool operator!=(const _Self& x) const { return !operator==(x); }
124116
125 inline pointer operator*() const {
126 if (idx == Term->getNumSuccessors())
127 return Term->getParent()->getUnwindDest();
128
129 return Term->getSuccessor(idx);
130 }
117 inline pointer operator*() const { return Term->getSuccessor(idx); }
131118 inline pointer operator->() const { return operator*(); }
132119
133120 inline _Self& operator++() { ++idx; return *this; } // Preincrement
473473 KEYWORD("asm", ASM_TOK);
474474 KEYWORD("sideeffect", SIDEEFFECT);
475475 KEYWORD("gc", GC);
476 KEYWORD("unwinds", UNWINDS);
477476
478477 KEYWORD("cc", CC_TOK);
479478 KEYWORD("ccc", CCC_TOK);
516516
517517 /// defineBBVal - This is a definition of a new basic block with the specified
518518 /// identifier which must be the same as CurFun.NextValNum, if its numeric.
519 static BasicBlock *defineBBVal(const ValID &ID, BasicBlock *unwindDest) {
519 static BasicBlock *defineBBVal(const ValID &ID) {
520520 assert(inFunctionScope() && "Can't get basic block at global scope!");
521521
522522 BasicBlock *BB = 0;
558558 }
559559
560560 ID.destroy();
561 BB->setUnwindDest(unwindDest);
562561 return BB;
563562 }
564563
10621061 %token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
10631062 %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
10641063 %token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1065 %token DATALAYOUT UNWINDS
1064 %token DATALAYOUT
10661065 %type OptCallingConv
10671066 %type OptParamAttrs ParamAttr
10681067 %type OptFuncAttrs FuncAttr
25582557 CHECK_FOR_ERROR
25592558 }
25602559 | /* empty */ { // Empty space between instruction lists
2561 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), 0);
2562 CHECK_FOR_ERROR
2563 }
2564 | UNWINDS TO ValueRef { // Only the unwind to block
2565 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), getBBVal($3));
2560 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
25662561 CHECK_FOR_ERROR
25672562 }
25682563 | LABELSTR { // Labelled (named) basic block
2569 $$ = defineBBVal(ValID::createLocalName(*$1), 0);
2564 $$ = defineBBVal(ValID::createLocalName(*$1));
25702565 delete $1;
25712566 CHECK_FOR_ERROR
2572 }
2573 | LABELSTR UNWINDS TO ValueRef {
2574 $$ = defineBBVal(ValID::createLocalName(*$1), getBBVal($4));
2575 delete $1;
2576 CHECK_FOR_ERROR
2567
25772568 };
25782569
25792570 BBTerminatorInst :
516516
517517 /// defineBBVal - This is a definition of a new basic block with the specified
518518 /// identifier which must be the same as CurFun.NextValNum, if its numeric.
519 static BasicBlock *defineBBVal(const ValID &ID, BasicBlock *unwindDest) {
519 static BasicBlock *defineBBVal(const ValID &ID) {
520520 assert(inFunctionScope() && "Can't get basic block at global scope!");
521521
522522 BasicBlock *BB = 0;
558558 }
559559
560560 ID.destroy();
561 BB->setUnwindDest(unwindDest);
562561 return BB;
563562 }
564563
10621061 %token OPAQUE EXTERNAL TARGET TRIPLE ALIGN ADDRSPACE
10631062 %token DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
10641063 %token CC_TOK CCC_TOK FASTCC_TOK COLDCC_TOK X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
1065 %token DATALAYOUT UNWINDS
1064 %token DATALAYOUT
10661065 %type OptCallingConv
10671066 %type OptParamAttrs ParamAttr
10681067 %type OptFuncAttrs FuncAttr
25582557 CHECK_FOR_ERROR
25592558 }
25602559 | /* empty */ { // Empty space between instruction lists
2561 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), 0);
2562 CHECK_FOR_ERROR
2563 }
2564 | UNWINDS TO ValueRef { // Only the unwind to block
2565 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum), getBBVal($3));
2560 $$ = defineBBVal(ValID::createLocalID(CurFun.NextValNum));
25662561 CHECK_FOR_ERROR
25672562 }
25682563 | LABELSTR { // Labelled (named) basic block
2569 $$ = defineBBVal(ValID::createLocalName(*$1), 0);
2564 $$ = defineBBVal(ValID::createLocalName(*$1));
25702565 delete $1;
25712566 CHECK_FOR_ERROR
2572 }
2573 | LABELSTR UNWINDS TO ValueRef {
2574 $$ = defineBBVal(ValID::createLocalName(*$1), getBBVal($4));
2575 delete $1;
2576 CHECK_FOR_ERROR
2567
25772568 };
25782569
25792570 BBTerminatorInst :
12231223 CurBB = FunctionBBs[0];
12241224 continue;
12251225
1226 case bitc::FUNC_CODE_INST_BB_UNWINDDEST: // BB_UNWINDDEST: [bb#]
1227 if (CurBB->getUnwindDest())
1228 return Error("Only permit one BB_UNWINDDEST per BB");
1229 if (Record.size() != 1)
1230 return Error("Invalid BB_UNWINDDEST record");
1231
1232 CurBB->setUnwindDest(getBasicBlock(Record[0]));
1233 continue;
1234
12351226 case bitc::FUNC_CODE_INST_BINOP: { // BINOP: [opval, ty, opval, opcode]
12361227 unsigned OpNum = 0;
12371228 Value *LHS, *RHS;
969969 unsigned InstID = CstEnd;
970970
971971 // Finally, emit all the instructions, in order.
972 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
973 if (const BasicBlock *unwindDest = BB->getUnwindDest()) {
974 Vals.push_back(VE.getValueID(unwindDest));
975 Stream.EmitRecord(bitc::FUNC_CODE_INST_BB_UNWINDDEST, Vals);
976 Vals.clear();
977 }
978
972 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
979973 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end();
980974 I != E; ++I) {
981975 WriteInstruction(*I, InstID, VE, Stream, Vals);
982976 if (I->getType() != Type::VoidTy)
983977 ++InstID;
984978 }
985 }
986979
987980 // Emit names for all the instructions etc.
988981 WriteValueSymbolTable(F.getValueSymbolTable(), VE, Stream);
3030
3131 STATISTIC(NumRemoved, "Number of invokes removed");
3232 STATISTIC(NumUnreach, "Number of noreturn calls optimized");
33 STATISTIC(NumBBUnwind, "Number of unwind dest removed from blocks");
3433
3534 namespace {
3635 struct VISIBILITY_HIDDEN PruneEH : public CallGraphSCCPass {
151150 bool PruneEH::SimplifyFunction(Function *F) {
152151 bool MadeChange = false;
153152 for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
154 bool couldUnwind = false;
155
156153 if (InvokeInst *II = dyn_cast(BB->getTerminator()))
157154 if (II->doesNotThrow()) {
158155 SmallVector Args(II->op_begin()+3, II->op_end());
182179
183180 ++NumRemoved;
184181 MadeChange = true;
185 } else {
186 couldUnwind = true;
187182 }
188183
189184 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; )
190 if (CallInst *CI = dyn_cast(I++)) {
185 if (CallInst *CI = dyn_cast(I++))
191186 if (CI->doesNotReturn() && !isa(I)) {
192187 // This call calls a function that cannot return. Insert an
193188 // unreachable instruction after it and simplify the code. Do this
203198 MadeChange = true;
204199 ++NumUnreach;
205200 break;
206 } else if (!CI->doesNotThrow()) {
207 couldUnwind = true;
208201 }
209 }
210
211 // Strip 'unwindTo' off of BBs that have no calls/invokes without nounwind.
212 if (!couldUnwind && BB->getUnwindDest()) {
213 MadeChange = true;
214 ++NumBBUnwind;
215 BB->getUnwindDest()->removePredecessor(BB, false, true);
216 BB->setUnwindDest(NULL);
217 }
218 }
202 }
203
219204 return MadeChange;
220205 }
221206
1117911179
1118011180 // Recursively visit successors. If this is a branch or switch on a
1118111181 // constant, only visit the reachable successor.
11182 if (BB->getUnwindDest())
11183 Worklist.push_back(BB->getUnwindDest());
1118411182 TerminatorInst *TI = BB->getTerminator();
1118511183 if (BranchInst *BI = dyn_cast(TI)) {
1118611184 if (BI->isConditional() && isa(BI->getCondition())) {
1118711185 bool CondVal = cast(BI->getCondition())->getZExtValue();
1118811186 BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
11189 if (ReachableBB != BB->getUnwindDest())
11190 Worklist.push_back(ReachableBB);
11187 Worklist.push_back(ReachableBB);
1119111188 continue;
1119211189 }
1119311190 } else if (SwitchInst *SI = dyn_cast(TI)) {
1119611193 for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i)
1119711194 if (SI->getCaseValue(i) == Cond) {
1119811195 BasicBlock *ReachableBB = SI->getSuccessor(i);
11199 if (ReachableBB != BB->getUnwindDest())
11200 Worklist.push_back(ReachableBB);
11196 Worklist.push_back(ReachableBB);
1120111197 continue;
1120211198 }
1120311199
404404 }
405405
406406 // Remap all instructions in the most recent iteration
407 for (unsigned i = 0; i < NewBlocks.size(); ++i) {
408 BasicBlock *NB = NewBlocks[i];
409 if (BasicBlock *UnwindDest = NB->getUnwindDest())
410 NB->setUnwindDest(cast(LastValueMap[UnwindDest]));
411
412 for (BasicBlock::iterator I = NB->begin(), E = NB->end(); I != E; ++I)
407 for (unsigned i = 0; i < NewBlocks.size(); ++i)
408 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
409 E = NewBlocks[i]->end(); I != E; ++I)
413410 RemapInstruction(I, LastValueMap);
414 }
415411 }
416412
417413 // The latch block exits the loop. If there are any PHI nodes in the
818818 }
819819
820820 // Rewrite the code to refer to itself.
821 for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i) {
822 BasicBlock *NB = NewBlocks[i];
823 if (BasicBlock *UnwindDest = NB->getUnwindDest())
824 NB->setUnwindDest(cast(ValueMap[UnwindDest]));
825
826 for (BasicBlock::iterator I = NB->begin(), E = NB->end(); I != E; ++I)
821 for (unsigned i = 0, e = NewBlocks.size(); i != e; ++i)
822 for (BasicBlock::iterator I = NewBlocks[i]->begin(),
823 E = NewBlocks[i]->end(); I != E; ++I)
827824 RemapInstruction(I, ValueMap);
828 }
829825
830826 // Rewrite the original preheader to select between versions of the loop.
831827 BranchInst *OldBR = cast(OrigPreheader->getTerminator());
17261726 // If there are any PHI nodes in this successor, drop entries for BB now.
17271727 BasicBlock *DeadBB = BlocksToErase[i];
17281728 while (!DeadBB->use_empty()) {
1729 if (BasicBlock *PredBB = dyn_cast(DeadBB->use_back())) {
1730 PredBB->setUnwindDest(NULL);
1731 continue;
1732 }
1733
17341729 Instruction *I = cast(DeadBB->use_back());
17351730 bool Folded = ConstantFoldTerminator(I->getParent());
17361731 if (!Folded) {
160160 while (isa(SplitIt))
161161 ++SplitIt;
162162 BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
163 New->setUnwindDest(Old->getUnwindDest());
164163
165164 // The new block lives in whichever loop the old one did.
166165 if (Loop *L = LI.getLoopFor(Old))
209208 BranchInst *BI = BranchInst::Create(BB, NewBB);
210209
211210 // Move the edges from Preds to point to NewBB instead of BB.
212 for (unsigned i = 0; i != NumPreds; ++i) {
211 for (unsigned i = 0; i != NumPreds; ++i)
213212 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
214
215 if (Preds[i]->getUnwindDest() == BB)
216 Preds[i]->setUnwindDest(NewBB);
217 }
218213
219214 // Update dominator tree and dominator frontier if available.
220215 DominatorTree *DT = P ? P->getAnalysisToUpdate() : 0;
3333 ClonedCodeInfo *CodeInfo) {
3434 BasicBlock *NewBB = BasicBlock::Create("", F);
3535 if (BB->hasName()) NewBB->setName(BB->getName()+NameSuffix);
36 NewBB->setUnwindDest(const_cast(BB->getUnwindDest()));
3736
3837 bool hasCalls = false, hasDynamicAllocas = false, hasStaticAllocas = false;
3938
107106 // references as we go. This uses ValueMap to do all the hard work.
108107 //
109108 for (Function::iterator BB = cast(ValueMap[OldFunc->begin()]),
110 BE = NewFunc->end(); BB != BE; ++BB) {
111 // Fix up the unwind destination.
112 if (BasicBlock *UnwindDest = BB->getUnwindDest())
113 BB->setUnwindDest(cast(ValueMap[UnwindDest]));
114
109 BE = NewFunc->end(); BB != BE; ++BB)
115110 // Loop over all instructions, fixing each one as we find it...
116111 for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II)
117112 RemapInstruction(II, ValueMap);
118 }
119113 }
120114
121115 /// CloneFunction - Return a copy of the specified function, but without
129129 for(SmallVector::iterator NBItr = NewBlocks.begin(),
130130 NBE = NewBlocks.end(); NBItr != NBE; ++NBItr) {
131131 BasicBlock *NB = *NBItr;
132
133 if (BasicBlock *UnwindDest = NB->getUnwindDest())
134 NB->setUnwindDest(cast(ValueMap[UnwindDest]));
135
136132 for(BasicBlock::iterator BI = NB->begin(), BE = NB->end();
137133 BI != BE; ++BI) {
138134 Instruction *Insn = BI;
6767 //Second loop to do the remapping
6868 for (std::vector::const_iterator BB = clonedTrace.begin(),
6969 BE = clonedTrace.end(); BB != BE; ++BB) {
70
71 //Remap the unwind destination
72 if (BasicBlock *UnwindDest = (*BB)->getUnwindDest())
73 (*BB)->setUnwindDest(cast(ValueMap[UnwindDest]));
74
7570 for (BasicBlock::iterator I = (*BB)->begin(); I != (*BB)->end(); ++I) {
7671 //Loop over all the operands of the instruction
7772 for (unsigned op=0, E = I->getNumOperands(); op != E; ++op) {
202202
203203 BasicBlock *OrigBB = TheCall->getParent();
204204 Function *Caller = OrigBB->getParent();
205 BasicBlock *UnwindBB = OrigBB->getUnwindDest();
206205
207206 // GC poses two hazards to inlining, which only occur when the callee has GC:
208207 // 1. If the caller has no GC, then the callee's GC must be propagated to the
414413 TerminatorInst *Term = BB->getTerminator();
415414 if (isa(Term)) {
416415 new UnreachableInst(Term);
417 BB->getInstList().erase(Term);
418 }
419 }
420
421 // If we are inlining a function that unwinds into a BB with an unwind dest,
422 // turn the inlined unwinds into branches to the unwind dest.
423 if (InlinedFunctionInfo.ContainsUnwinds && UnwindBB && isa(TheCall))
424 for (Function::iterator BB = FirstNewBlock, E = Caller->end();
425 BB != E; ++BB) {
426 TerminatorInst *Term = BB->getTerminator();
427 if (isa(Term)) {
428 BranchInst::Create(UnwindBB, Term);
429416 BB->getInstList().erase(Term);
430417 }
431418 }
124124 if (LI->getLoopFor(BB)) continue;
125125
126126 bool BlockUnreachable = false;
127 TerminatorInst *TI = BB->getTerminator();
127128
128129 // Check to see if any successors of this block are non-loop-header loops
129130 // that are not the header.
130 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I) {
131 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i) {
131132 // If this successor is not in a loop, BB is clearly ok.
132 Loop *L = LI->getLoopFor(*I);
133 Loop *L = LI->getLoopFor(TI->getSuccessor(i));
133134 if (!L) continue;
134135
135136 // If the succ is the loop header, and if L is a top-level loop, then this
136137 // is an entrance into a loop through the header, which is also ok.
137 if (L->getHeader() == *I && L->getParentLoop() == 0)
138 if (L->getHeader() == TI->getSuccessor(i) && L->getParentLoop() == 0)
138139 continue;
139140
140141 // Otherwise, this is an entrance into a loop from some place invalid.
152153 // loop by replacing the terminator.
153154
154155 // Remove PHI entries from the successors.
155 for (succ_iterator I = succ_begin(BB), E = succ_end(BB); I != E; ++I)
156 (*I)->removePredecessor(BB);
156 for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
157 TI->getSuccessor(i)->removePredecessor(BB);
157158
158159 // Add a new unreachable instruction before the old terminator.
159 TerminatorInst *TI = BB->getTerminator();
160160 new UnreachableInst(TI);
161161
162162 // Delete the dead terminator.
575575 }
576576
577577 // Now that all of the PHI nodes have been inserted and adjusted, modify the
578 // backedge blocks to branch to the BEBlock instead of the header.
578 // backedge blocks to just to the BEBlock instead of the header.
579579 for (unsigned i = 0, e = BackedgeBlocks.size(); i != e; ++i) {
580580 TerminatorInst *TI = BackedgeBlocks[i]->getTerminator();
581581 for (unsigned Op = 0, e = TI->getNumSuccessors(); Op != e; ++Op)
582582 if (TI->getSuccessor(Op) == Header)
583583 TI->setSuccessor(Op, BEBlock);
584
585 if (BackedgeBlocks[i]->getUnwindDest() == Header)
586 BackedgeBlocks[i]->setUnwindDest(BEBlock);
587584 }
588585
589586 //===--- Update all analyses which we must preserve now -----------------===//
13411341 SmallVector UncondBranchPreds;
13421342 SmallVector CondBranchPreds;
13431343 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
1344 if ((*PI)->getUnwindDest() == BB) continue;
1345
13461344 TerminatorInst *PTI = (*PI)->getTerminator();
13471345 if (BranchInst *BI = dyn_cast(PTI)) {
13481346 if (BI->isUnconditional())
14071405 SmallVector Preds(pred_begin(BB), pred_end(BB));
14081406 while (!Preds.empty()) {
14091407 BasicBlock *Pred = Preds.back();
1410
1411 if (Pred->getUnwindDest() == BB) {
1412 Pred->setUnwindDest(NULL);
1413 Changed = true;
1414 }
1415
14161408 if (BranchInst *BI = dyn_cast(Pred->getTerminator())) {
1417 if (BI->isUnconditional() && BI->getSuccessor(0) == BB) {
1409 if (BI->isUnconditional()) {
14181410 Pred->getInstList().pop_back(); // nuke uncond branch
14191411 new UnwindInst(Pred); // Use unwind.
14201412 Changed = true;
18391831
18401832 BasicBlock *OnlySucc = 0;
18411833 if (OnlyPred && OnlyPred != BB && // Don't break self loops
1842 OnlyPred->getUnwindDest() != BB &&
18431834 OnlyPred->getTerminator()->getOpcode() != Instruction::Invoke) {
18441835 // Check to see if there is only one distinct successor...
18451836 succ_iterator SI(succ_begin(OnlyPred)), SE(succ_end(OnlyPred));
18511842 }
18521843 }
18531844
1854 if (OnlySucc && (BB->getUnwindDest() == OnlyPred->getUnwindDest() ||
1855 !BB->getUnwindDest() || !OnlyPred->getUnwindDest())) {
1845 if (OnlySucc) {
18561846 DOUT << "Merging: " << *BB << "into: " << *OnlyPred;
18571847
18581848 // Resolve any PHI nodes at the start of the block. They are all
18711861
18721862 // Move all definitions in the successor to the predecessor.
18731863 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList());
1874
1875 // Move the unwind destination block
1876 if (!OnlyPred->getUnwindDest() && BB->getUnwindDest())
1877 OnlyPred->setUnwindDest(BB->getUnwindDest());
18781864
18791865 // Make all PHI nodes that referred to BB now refer to Pred as their
18801866 // source.
11621162 /// printBasicBlock - This member is called for each basic block in a method.
11631163 ///
11641164 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
1165 if (BB->hasName()) // Print out the label if it exists...
1166 Out << '\n' << getLLVMName(BB->getName(), LabelPrefix) << ':';
1167
1168 if (const BasicBlock* unwindDest = BB->getUnwindDest()) {
1169 if (BB->hasName())
1170 Out << ' ';
1171
1172 Out << "unwinds to";
1173 writeOperand(unwindDest, false);
1174 }
1175
1176 if (!BB->hasName() && !BB->use_empty()) { // Don't print block # of no uses...
1165 if (BB->hasName()) { // Print out the label if it exists...
1166 Out << "\n" << getLLVMName(BB->getName(), LabelPrefix) << ':';
1167 } else if (!BB->use_empty()) { // Don't print block # of no uses...
11771168 Out << "\n;
11781169 int Slot = Machine.getLocalSlot(BB);
11791170 if (Slot != -1)
7373
7474
7575 BasicBlock::BasicBlock(const std::string &Name, Function *NewParent,
76 BasicBlock *InsertBefore, BasicBlock *Dest)
77 : User(Type::LabelTy, Value::BasicBlockVal, &unwindDest, 0/*FIXME*/), Parent(0) {
76 BasicBlock *InsertBefore)
77 : Value(Type::LabelTy, Value::BasicBlockVal), Parent(0) {
7878
7979 // Make sure that we get added to a function
8080 LeakDetector::addGarbageObject(this);
8888 }
8989
9090 setName(Name);
91 unwindDest.init(NULL, this);
92 setUnwindDest(Dest);
9391 }
9492
9593
116114
117115 void BasicBlock::eraseFromParent() {
118116 getParent()->getBasicBlockList().erase(this);
119 }
120
121 const BasicBlock *BasicBlock::getUnwindDest() const {
122 return cast_or_null(unwindDest.get());
123 }
124
125 BasicBlock *BasicBlock::getUnwindDest() {
126 return cast_or_null(unwindDest.get());
127 }
128
129 void BasicBlock::setUnwindDest(BasicBlock *dest) {
130 NumOperands = unwindDest ? 1 : 0;
131 unwindDest.set(dest);
132117 }
133118
134119 /// moveBefore - Unlink this basic block from its current function and
169154 }
170155
171156 void BasicBlock::dropAllReferences() {
172 setUnwindDest(NULL);
173157 for(iterator I = begin(), E = end(); I != E; ++I)
174158 I->dropAllReferences();
175159 }
191175 /// called while the predecessor still refers to this block.
192176 ///
193177 void BasicBlock::removePredecessor(BasicBlock *Pred,
194 bool DontDeleteUselessPHIs,
195 bool OnlyDeleteOne) {
178 bool DontDeleteUselessPHIs) {
196179 assert((hasNUsesOrMore(16)||// Reduce cost of this assertion for complex CFGs.
197180 find(pred_begin(this), pred_end(this), Pred) != pred_end(this)) &&
198181 "removePredecessor: BB is not a predecessor!");
227210 // Yup, loop through and nuke the PHI nodes
228211 while (PHINode *PN = dyn_cast(&front())) {
229212 // Remove the predecessor first.
230 if (OnlyDeleteOne) {
231 int idx = PN->getBasicBlockIndex(Pred);
232 PN->removeIncomingValue(idx, !DontDeleteUselessPHIs);
233 } else
234 PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs);
213 PN->removeIncomingValue(Pred, !DontDeleteUselessPHIs);
235214
236215 // If the PHI _HAD_ two uses, replace PHI node with its now *single* value
237216 if (max_idx == 2) {
252231 PHINode *PN;
253232 for (iterator II = begin(); (PN = dyn_cast(II)); ) {
254233 ++II;
255 if (OnlyDeleteOne) {
256 int idx = PN->getBasicBlockIndex(Pred);
257 PN->removeIncomingValue(idx, false);
258 } else
259 PN->removeIncomingValue(Pred, false);
260
234 PN->removeIncomingValue(Pred, false);
261235 // If all incoming values to the Phi are the same, we can replace the Phi
262236 // with that value.
263237 Value* PNV = 0;
286260 assert(I != InstList.end() &&
287261 "Trying to get me to create degenerate basic block!");
288262
289 BasicBlock *New = new(0/*FIXME*/) BasicBlock(BBName, getParent(), getNext());
263 BasicBlock *New = BasicBlock::Create(BBName, getParent(), getNext());
290264
291265 // Move all of the specified instructions from the original basic block into
292266 // the new basic block.
528528
529529 // Ensure that basic blocks have terminators!
530530 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
531
532 // Ensure that the BB doesn't point out of its Function for unwinding.
533 Assert2(!BB.getUnwindDest() ||
534 BB.getUnwindDest()->getParent() == BB.getParent(),
535 "Basic Block unwinds to block in different function!",
536 &BB, BB.getUnwindDest());
537531
538532 // Check constraints that this basic block imposes on all of the PHI nodes in
539533 // it.
+0
-13
test/Transforms/Inline/unwindto.ll less more
None ; RUN: llvm-as < %s | opt -inline | llvm-dis | grep "br label %cleanup"
1
2 define void @g() {
3 unwind
4 }
5
6 define i32 @f1() {
7 entry: unwinds to %cleanup
8 call void @g()
9 ret i32 0
10 cleanup:
11 ret i32 1
12 }
+0
-15
test/Transforms/PruneEH/unwindto.ll less more
None ; RUN: llvm-as < %s | opt -prune-eh | llvm-dis | not grep {unwinds to}
1
2 define i8 @test7(i1 %b) {
3 entry: unwinds to %cleanup
4 br i1 %b, label %cond_true, label %cond_false
5 cond_true: unwinds to %cleanup
6 br label %cleanup
7 cond_false: unwinds to %cleanup
8 br label %cleanup
9 cleanup:
10 %x = phi i8 [0, %entry], [1, %cond_true], [1, %cond_true],
11 [2, %cond_false], [2, %cond_false]
12 ret i8 %x
13 }
14
+0
-61
test/Transforms/SimplifyCFG/unwindto.ll less more
None ; RUN: llvm-as < %s | opt -simplifycfg | llvm-dis | grep {unwinds to} | count 3
1
2 declare void @g(i32)
3
4 define i32 @f1() {
5 entry:
6 br label %bb1
7 bb1: unwinds to %cleanup1
8 call void @g(i32 0)
9 br label %bb2
10 bb2: unwinds to %cleanup2
11 call void @g(i32 1)
12 br label %exit
13 exit:
14 ret i32 0
15 cleanup1:
16 ret i32 1
17 cleanup2:
18 ret i32 2
19 }
20
21 define i32 @f2() {
22 entry: unwinds to %cleanup
23 br label %bb1
24 bb1: unwinds to %cleanup
25 br label %bb2
26 bb2: unwinds to %cleanup
27 br label %bb3
28 bb3:
29 br label %bb4
30 bb4: unwinds to %cleanup
31 ret i32 0
32 cleanup:
33 ret i32 1
34 }
35
36 define i32 @f3() {
37 entry: unwinds to %cleanup
38 call void @g(i32 0)
39 ret i32 0
40 cleanup:
41 unwind
42 }
43
44 define i32 @f4() {
45 entry: unwinds to %cleanup
46 call void @g(i32 0)
47 br label %cleanup
48 cleanup:
49 unwind
50 }
51
52 define i32 @f5() {
53 entry: unwinds to %cleanup
54 call void @g(i32 0)
55 br label %other
56 other:
57 ret i32 0
58 cleanup:
59 unwind
60 }
171171 switch (CodeID) {
172172 default: return 0;
173173 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
174 case bitc::FUNC_CODE_INST_BB_UNWINDDEST: return "UNWINDDEST";
175174
176175 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
177176 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";