blob: 12de9eed4b85e503eca66f112d6e14c7c308d013 [file] [log] [blame]
Chris Lattner4d1e46e2002-05-07 18:07:59 +00001//===-- BasicBlockUtils.cpp - BasicBlock Utilities -------------------------==//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner4d1e46e2002-05-07 18:07:59 +00009//
10// This family of functions perform manipulations on basic blocks, and
11// instructions contained within basic blocks.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Chris Lattner54b9c3b2008-04-21 01:28:02 +000016#include "llvm/Analysis/AliasAnalysis.h"
Nick Lewycky81e48042013-07-27 01:24:00 +000017#include "llvm/Analysis/CFG.h"
Cameron Zwarich30127872011-01-18 04:11:31 +000018#include "llvm/Analysis/Dominators.h"
Chris Lattnerb5b79972011-01-11 08:13:40 +000019#include "llvm/Analysis/LoopInfo.h"
20#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000021#include "llvm/IR/Constant.h"
22#include "llvm/IR/DataLayout.h"
23#include "llvm/IR/Function.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Type.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000027#include "llvm/Support/ErrorHandling.h"
Dan Gohmanafc36a92009-05-02 18:29:22 +000028#include "llvm/Support/ValueHandle.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Transforms/Scalar.h"
30#include "llvm/Transforms/Utils/Local.h"
Chris Lattner4d1e46e2002-05-07 18:07:59 +000031#include <algorithm>
Chris Lattnerf7703df2004-01-09 06:12:26 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner71af9b02008-12-03 06:40:52 +000034/// DeleteDeadBlock - Delete the specified block, which must have no
35/// predecessors.
36void llvm::DeleteDeadBlock(BasicBlock *BB) {
Chris Lattner2973a252008-12-03 07:45:15 +000037 assert((pred_begin(BB) == pred_end(BB) ||
38 // Can delete self loop.
39 BB->getSinglePredecessor() == BB) && "Block is not dead!");
Chris Lattner2b1ba242008-12-03 06:37:44 +000040 TerminatorInst *BBTerm = BB->getTerminator();
Jakub Staszake673b542013-01-14 23:16:36 +000041
Chris Lattner2b1ba242008-12-03 06:37:44 +000042 // Loop through all of our successors and make sure they know that one
43 // of their predecessors is going away.
44 for (unsigned i = 0, e = BBTerm->getNumSuccessors(); i != e; ++i)
45 BBTerm->getSuccessor(i)->removePredecessor(BB);
Jakub Staszake673b542013-01-14 23:16:36 +000046
Chris Lattner2b1ba242008-12-03 06:37:44 +000047 // Zap all the instructions in the block.
48 while (!BB->empty()) {
49 Instruction &I = BB->back();
50 // If this instruction is used, replace uses with an arbitrary value.
51 // Because control flow can't get here, we don't care what we replace the
52 // value with. Note that since this block is unreachable, and all values
53 // contained within it must dominate their uses, that all uses will
54 // eventually be removed (they are themselves dead).
55 if (!I.use_empty())
Owen Anderson9e9a0d52009-07-30 23:03:37 +000056 I.replaceAllUsesWith(UndefValue::get(I.getType()));
Chris Lattner2b1ba242008-12-03 06:37:44 +000057 BB->getInstList().pop_back();
58 }
Jakub Staszake673b542013-01-14 23:16:36 +000059
Chris Lattner2b1ba242008-12-03 06:37:44 +000060 // Zap the block!
61 BB->eraseFromParent();
Chris Lattner2b1ba242008-12-03 06:37:44 +000062}
63
Chris Lattner29874e02008-12-03 19:44:02 +000064/// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
65/// any single-entry PHI nodes in it, fold them away. This handles the case
66/// when all entries to the PHI nodes in a block are guaranteed equal, such as
67/// when the block has exactly one predecessor.
Chris Lattnerb5b79972011-01-11 08:13:40 +000068void llvm::FoldSingleEntryPHINodes(BasicBlock *BB, Pass *P) {
69 if (!isa<PHINode>(BB->begin())) return;
Jakub Staszake673b542013-01-14 23:16:36 +000070
Chris Lattnerb5b79972011-01-11 08:13:40 +000071 AliasAnalysis *AA = 0;
72 MemoryDependenceAnalysis *MemDep = 0;
73 if (P) {
74 AA = P->getAnalysisIfAvailable<AliasAnalysis>();
75 MemDep = P->getAnalysisIfAvailable<MemoryDependenceAnalysis>();
76 }
Jakub Staszake673b542013-01-14 23:16:36 +000077
Chris Lattner29874e02008-12-03 19:44:02 +000078 while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
79 if (PN->getIncomingValue(0) != PN)
80 PN->replaceAllUsesWith(PN->getIncomingValue(0));
81 else
Owen Anderson9e9a0d52009-07-30 23:03:37 +000082 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
Jakub Staszake673b542013-01-14 23:16:36 +000083
Chris Lattnerb5b79972011-01-11 08:13:40 +000084 if (MemDep)
85 MemDep->removeInstruction(PN); // Memdep updates AA itself.
86 else if (AA && isa<PointerType>(PN->getType()))
87 AA->deleteValue(PN);
Jakub Staszake673b542013-01-14 23:16:36 +000088
Chris Lattner29874e02008-12-03 19:44:02 +000089 PN->eraseFromParent();
90 }
91}
92
93
Dan Gohmanafc36a92009-05-02 18:29:22 +000094/// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
95/// is dead. Also recursively delete any operands that become dead as
96/// a result. This includes tracing the def-use list from the PHI to see if
Dan Gohman35738ac2009-05-04 22:30:44 +000097/// it is ultimately unused or if it reaches an unused cycle.
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +000098bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI) {
Dan Gohmanafc36a92009-05-02 18:29:22 +000099 // Recursively deleting a PHI may cause multiple PHIs to be deleted
100 // or RAUW'd undef, so use an array of WeakVH for the PHIs to delete.
101 SmallVector<WeakVH, 8> PHIs;
102 for (BasicBlock::iterator I = BB->begin();
103 PHINode *PN = dyn_cast<PHINode>(I); ++I)
104 PHIs.push_back(PN);
105
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000106 bool Changed = false;
Dan Gohmanafc36a92009-05-02 18:29:22 +0000107 for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
108 if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
Benjamin Kramer8e0d1c02012-08-29 15:32:21 +0000109 Changed |= RecursivelyDeleteDeadPHINode(PN, TLI);
Dan Gohman90fe0bd2010-01-05 15:45:31 +0000110
111 return Changed;
Dan Gohmanafc36a92009-05-02 18:29:22 +0000112}
113
Dan Gohman438b5832009-10-31 17:33:01 +0000114/// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
115/// if possible. The return value indicates success or failure.
Chris Lattner88202922009-11-01 04:57:33 +0000116bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, Pass *P) {
Dan Gohman1c034dc2010-08-17 17:07:02 +0000117 // Don't merge away blocks who have their address taken.
118 if (BB->hasAddressTaken()) return false;
Jakub Staszake673b542013-01-14 23:16:36 +0000119
Dan Gohman1c034dc2010-08-17 17:07:02 +0000120 // Can't merge if there are multiple predecessors, or no predecessors.
121 BasicBlock *PredBB = BB->getUniquePredecessor();
Dan Gohman438b5832009-10-31 17:33:01 +0000122 if (!PredBB) return false;
Dan Gohman1c034dc2010-08-17 17:07:02 +0000123
Dan Gohman438b5832009-10-31 17:33:01 +0000124 // Don't break self-loops.
125 if (PredBB == BB) return false;
126 // Don't break invokes.
127 if (isa<InvokeInst>(PredBB->getTerminator())) return false;
Jakub Staszake673b542013-01-14 23:16:36 +0000128
Dan Gohman438b5832009-10-31 17:33:01 +0000129 succ_iterator SI(succ_begin(PredBB)), SE(succ_end(PredBB));
Chris Lattnerdc85f8a2011-01-08 19:08:40 +0000130 BasicBlock *OnlySucc = BB;
Dan Gohman438b5832009-10-31 17:33:01 +0000131 for (; SI != SE; ++SI)
132 if (*SI != OnlySucc) {
133 OnlySucc = 0; // There are multiple distinct successors!
134 break;
135 }
Jakub Staszake673b542013-01-14 23:16:36 +0000136
Dan Gohman438b5832009-10-31 17:33:01 +0000137 // Can't merge if there are multiple successors.
138 if (!OnlySucc) return false;
Devang Patele435a5d2008-09-09 01:06:56 +0000139
Dan Gohman438b5832009-10-31 17:33:01 +0000140 // Can't merge if there is PHI loop.
141 for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; ++BI) {
142 if (PHINode *PN = dyn_cast<PHINode>(BI)) {
143 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
144 if (PN->getIncomingValue(i) == PN)
145 return false;
146 } else
147 break;
148 }
149
150 // Begin by getting rid of unneeded PHIs.
Chris Lattnerdc85f8a2011-01-08 19:08:40 +0000151 if (isa<PHINode>(BB->front()))
Chris Lattnerb5b79972011-01-11 08:13:40 +0000152 FoldSingleEntryPHINodes(BB, P);
Jakub Staszake673b542013-01-14 23:16:36 +0000153
Owen Andersonb31b06d2008-07-17 00:01:40 +0000154 // Delete the unconditional branch from the predecessor...
155 PredBB->getInstList().pop_back();
Jakub Staszake673b542013-01-14 23:16:36 +0000156
Owen Andersonb31b06d2008-07-17 00:01:40 +0000157 // Make all PHI nodes that referred to BB now refer to Pred as their
158 // source...
159 BB->replaceAllUsesWith(PredBB);
Jakub Staszake673b542013-01-14 23:16:36 +0000160
Jay Foad95c3e482011-06-23 09:09:15 +0000161 // Move all definitions in the successor to the predecessor...
162 PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
Jakub Staszake673b542013-01-14 23:16:36 +0000163
Dan Gohman438b5832009-10-31 17:33:01 +0000164 // Inherit predecessors name if it exists.
Owen Anderson11f2ec82008-07-17 19:42:29 +0000165 if (!PredBB->hasName())
166 PredBB->takeName(BB);
Jakub Staszake673b542013-01-14 23:16:36 +0000167
Owen Andersonb31b06d2008-07-17 00:01:40 +0000168 // Finally, erase the old block and update dominator info.
169 if (P) {
Chris Lattnerdc85f8a2011-01-08 19:08:40 +0000170 if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) {
171 if (DomTreeNode *DTN = DT->getNode(BB)) {
172 DomTreeNode *PredDTN = DT->getNode(PredBB);
Jakob Stoklund Olesenfbbd4ab2011-01-11 22:54:38 +0000173 SmallVector<DomTreeNode*, 8> Children(DTN->begin(), DTN->end());
Craig Topper6227d5c2013-07-04 01:31:24 +0000174 for (SmallVectorImpl<DomTreeNode *>::iterator DI = Children.begin(),
Owen Andersonb31b06d2008-07-17 00:01:40 +0000175 DE = Children.end(); DI != DE; ++DI)
176 DT->changeImmediateDominator(*DI, PredDTN);
177
178 DT->eraseNode(BB);
179 }
Jakub Staszake673b542013-01-14 23:16:36 +0000180
Chris Lattnerdc85f8a2011-01-08 19:08:40 +0000181 if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
182 LI->removeBlock(BB);
Jakub Staszake673b542013-01-14 23:16:36 +0000183
Chris Lattnerb6810992011-01-11 08:16:49 +0000184 if (MemoryDependenceAnalysis *MD =
185 P->getAnalysisIfAvailable<MemoryDependenceAnalysis>())
186 MD->invalidateCachedPredecessors();
Owen Andersonb31b06d2008-07-17 00:01:40 +0000187 }
188 }
Jakub Staszake673b542013-01-14 23:16:36 +0000189
Owen Andersonb31b06d2008-07-17 00:01:40 +0000190 BB->eraseFromParent();
Dan Gohman438b5832009-10-31 17:33:01 +0000191 return true;
Owen Andersonb31b06d2008-07-17 00:01:40 +0000192}
193
Chris Lattner0f67dd62005-04-21 16:04:49 +0000194/// ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
195/// with a value, then remove and delete the original instruction.
196///
Chris Lattnerf7703df2004-01-09 06:12:26 +0000197void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
198 BasicBlock::iterator &BI, Value *V) {
Chris Lattner18961502002-06-25 16:12:52 +0000199 Instruction &I = *BI;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000200 // Replaces all of the uses of the instruction with uses of the value
Chris Lattner18961502002-06-25 16:12:52 +0000201 I.replaceAllUsesWith(V);
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000202
Chris Lattner86cc4232007-02-11 01:37:51 +0000203 // Make sure to propagate a name if there is one already.
204 if (I.hasName() && !V->hasName())
205 V->takeName(&I);
Misha Brukmanfd939082005-04-21 23:48:37 +0000206
Misha Brukman5560c9d2003-08-18 14:43:39 +0000207 // Delete the unnecessary instruction now...
Chris Lattner18961502002-06-25 16:12:52 +0000208 BI = BIL.erase(BI);
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000209}
210
211
Chris Lattner0f67dd62005-04-21 16:04:49 +0000212/// ReplaceInstWithInst - Replace the instruction specified by BI with the
213/// instruction specified by I. The original instruction is deleted and BI is
214/// updated to point to the new instruction.
215///
Chris Lattnerf7703df2004-01-09 06:12:26 +0000216void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
217 BasicBlock::iterator &BI, Instruction *I) {
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000218 assert(I->getParent() == 0 &&
219 "ReplaceInstWithInst: Instruction already inserted into basic block!");
220
221 // Insert the new instruction into the basic block...
Chris Lattner18961502002-06-25 16:12:52 +0000222 BasicBlock::iterator New = BIL.insert(BI, I);
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000223
224 // Replace all uses of the old instruction, and delete it.
225 ReplaceInstWithValue(BIL, BI, I);
226
227 // Move BI back to point to the newly inserted instruction
Chris Lattner18961502002-06-25 16:12:52 +0000228 BI = New;
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000229}
230
Chris Lattner0f67dd62005-04-21 16:04:49 +0000231/// ReplaceInstWithInst - Replace the instruction specified by From with the
232/// instruction specified by To.
233///
Chris Lattnerf7703df2004-01-09 06:12:26 +0000234void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
Chris Lattner18961502002-06-25 16:12:52 +0000235 BasicBlock::iterator BI(From);
236 ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
Chris Lattner4d1e46e2002-05-07 18:07:59 +0000237}
Chris Lattnerb0f0ef82002-07-29 22:32:08 +0000238
Jakub Staszake673b542013-01-14 23:16:36 +0000239/// SplitEdge - Split the edge connecting specified block. Pass P must
240/// not be NULL.
Bob Wilsonadb6f222010-02-16 19:49:17 +0000241BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, Pass *P) {
Bob Wilsonae23daf2010-02-16 21:06:42 +0000242 unsigned SuccNum = GetSuccessorNumber(BB, Succ);
Jakub Staszake673b542013-01-14 23:16:36 +0000243
Devang Patel80198932007-07-06 21:39:20 +0000244 // If this is a critical edge, let SplitCriticalEdge do it.
Bob Wilsonadb6f222010-02-16 19:49:17 +0000245 TerminatorInst *LatchTerm = BB->getTerminator();
246 if (SplitCriticalEdge(LatchTerm, SuccNum, P))
Devang Patel80198932007-07-06 21:39:20 +0000247 return LatchTerm->getSuccessor(SuccNum);
248
249 // If the edge isn't critical, then BB has a single successor or Succ has a
250 // single pred. Split the block.
Devang Patel80198932007-07-06 21:39:20 +0000251 if (BasicBlock *SP = Succ->getSinglePredecessor()) {
252 // If the successor only has a single pred, split the top of the successor
253 // block.
254 assert(SP == BB && "CFG broken");
Devang Patel8a88a142008-11-03 23:14:09 +0000255 SP = NULL;
Devang Patel80198932007-07-06 21:39:20 +0000256 return SplitBlock(Succ, Succ->begin(), P);
Devang Patel80198932007-07-06 21:39:20 +0000257 }
Jakub Staszake673b542013-01-14 23:16:36 +0000258
Chris Lattnerb0433d42011-01-08 18:47:43 +0000259 // Otherwise, if BB has a single successor, split it at the bottom of the
260 // block.
261 assert(BB->getTerminator()->getNumSuccessors() == 1 &&
Jakub Staszake673b542013-01-14 23:16:36 +0000262 "Should have a single succ!");
Chris Lattnerb0433d42011-01-08 18:47:43 +0000263 return SplitBlock(BB, BB->getTerminator(), P);
Devang Patel80198932007-07-06 21:39:20 +0000264}
265
266/// SplitBlock - Split the specified block at the specified instruction - every
267/// thing before SplitPt stays in Old and everything starting with SplitPt moves
268/// to a new block. The two blocks are joined by an unconditional branch and
269/// the loop info is updated.
270///
271BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt, Pass *P) {
Devang Patel80198932007-07-06 21:39:20 +0000272 BasicBlock::iterator SplitIt = SplitPt;
Bill Wendlingab82fd92011-08-17 21:21:31 +0000273 while (isa<PHINode>(SplitIt) || isa<LandingPadInst>(SplitIt))
Devang Patel80198932007-07-06 21:39:20 +0000274 ++SplitIt;
275 BasicBlock *New = Old->splitBasicBlock(SplitIt, Old->getName()+".split");
276
Dan Gohman5c89b522009-09-08 15:45:00 +0000277 // The new block lives in whichever loop the old one did. This preserves
278 // LCSSA as well, because we force the split point to be after any PHI nodes.
Chris Lattnerdc85f8a2011-01-08 19:08:40 +0000279 if (LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>())
Owen Andersona90793b2008-10-03 06:55:35 +0000280 if (Loop *L = LI->getLoopFor(Old))
281 L->addBasicBlockToLoop(New, LI->getBase());
Devang Patel80198932007-07-06 21:39:20 +0000282
Evan Cheng0f1666b2010-04-05 21:16:25 +0000283 if (DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>()) {
Gabor Greife2d50042010-09-10 22:25:58 +0000284 // Old dominates New. New node dominates all other nodes dominated by Old.
Rafael Espindola605e2b52011-08-24 18:07:01 +0000285 if (DomTreeNode *OldNode = DT->getNode(Old)) {
286 std::vector<DomTreeNode *> Children;
287 for (DomTreeNode::iterator I = OldNode->begin(), E = OldNode->end();
Jakub Staszake673b542013-01-14 23:16:36 +0000288 I != E; ++I)
Rafael Espindola605e2b52011-08-24 18:07:01 +0000289 Children.push_back(*I);
Devang Patela8a8a362007-07-19 02:29:24 +0000290
Evan Cheng0f1666b2010-04-05 21:16:25 +0000291 DomTreeNode *NewNode = DT->addNewBlock(New,Old);
Devang Patela8a8a362007-07-19 02:29:24 +0000292 for (std::vector<DomTreeNode *>::iterator I = Children.begin(),
Jakub Staszake673b542013-01-14 23:16:36 +0000293 E = Children.end(); I != E; ++I)
Devang Patela8a8a362007-07-19 02:29:24 +0000294 DT->changeImmediateDominator(*I, NewNode);
Rafael Espindola605e2b52011-08-24 18:07:01 +0000295 }
Evan Cheng0f1666b2010-04-05 21:16:25 +0000296 }
Devang Patel80198932007-07-06 21:39:20 +0000297
Devang Patel80198932007-07-06 21:39:20 +0000298 return New;
299}
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000300
Bill Wendlinga644b332011-08-18 05:25:23 +0000301/// UpdateAnalysisInformation - Update DominatorTree, LoopInfo, and LCCSA
302/// analysis information.
Bill Wendlingd4770142011-08-18 17:57:57 +0000303static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
Bill Wendling9210e842011-08-18 20:39:32 +0000304 ArrayRef<BasicBlock *> Preds,
305 Pass *P, bool &HasLoopExit) {
Bill Wendlinga644b332011-08-18 05:25:23 +0000306 if (!P) return;
307
308 LoopInfo *LI = P->getAnalysisIfAvailable<LoopInfo>();
309 Loop *L = LI ? LI->getLoopFor(OldBB) : 0;
Bill Wendlinga644b332011-08-18 05:25:23 +0000310
311 // If we need to preserve loop analyses, collect some information about how
312 // this split will affect loops.
313 bool IsLoopEntry = !!L;
314 bool SplitMakesNewLoopHeader = false;
315 if (LI) {
Bill Wendling7e8840c2011-08-19 00:05:40 +0000316 bool PreserveLCSSA = P->mustPreserveAnalysisID(LCSSAID);
Bill Wendling9210e842011-08-18 20:39:32 +0000317 for (ArrayRef<BasicBlock*>::iterator
318 i = Preds.begin(), e = Preds.end(); i != e; ++i) {
319 BasicBlock *Pred = *i;
Bill Wendling7e8840c2011-08-19 00:05:40 +0000320
Bill Wendlinga644b332011-08-18 05:25:23 +0000321 // If we need to preserve LCSSA, determine if any of the preds is a loop
322 // exit.
323 if (PreserveLCSSA)
Bill Wendling9210e842011-08-18 20:39:32 +0000324 if (Loop *PL = LI->getLoopFor(Pred))
Bill Wendlinga644b332011-08-18 05:25:23 +0000325 if (!PL->contains(OldBB))
326 HasLoopExit = true;
327
328 // If we need to preserve LoopInfo, note whether any of the preds crosses
329 // an interesting loop boundary.
330 if (!L) continue;
Bill Wendling9210e842011-08-18 20:39:32 +0000331 if (L->contains(Pred))
Bill Wendlinga644b332011-08-18 05:25:23 +0000332 IsLoopEntry = false;
333 else
334 SplitMakesNewLoopHeader = true;
335 }
336 }
337
338 // Update dominator tree if available.
339 DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
340 if (DT)
341 DT->splitBlock(NewBB);
342
343 if (!L) return;
344
345 if (IsLoopEntry) {
346 // Add the new block to the nearest enclosing loop (and not an adjacent
347 // loop). To find this, examine each of the predecessors and determine which
348 // loops enclose them, and select the most-nested loop which contains the
349 // loop containing the block being split.
350 Loop *InnermostPredLoop = 0;
Bill Wendling9210e842011-08-18 20:39:32 +0000351 for (ArrayRef<BasicBlock*>::iterator
352 i = Preds.begin(), e = Preds.end(); i != e; ++i) {
353 BasicBlock *Pred = *i;
354 if (Loop *PredLoop = LI->getLoopFor(Pred)) {
Bill Wendlinga644b332011-08-18 05:25:23 +0000355 // Seek a loop which actually contains the block being split (to avoid
356 // adjacent loops).
357 while (PredLoop && !PredLoop->contains(OldBB))
358 PredLoop = PredLoop->getParentLoop();
359
360 // Select the most-nested of these loops which contains the block.
361 if (PredLoop && PredLoop->contains(OldBB) &&
362 (!InnermostPredLoop ||
363 InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
364 InnermostPredLoop = PredLoop;
365 }
Bill Wendling9210e842011-08-18 20:39:32 +0000366 }
Bill Wendlinga644b332011-08-18 05:25:23 +0000367
368 if (InnermostPredLoop)
369 InnermostPredLoop->addBasicBlockToLoop(NewBB, LI->getBase());
370 } else {
371 L->addBasicBlockToLoop(NewBB, LI->getBase());
372 if (SplitMakesNewLoopHeader)
373 L->moveToHeader(NewBB);
374 }
375}
376
Bill Wendling1c44d862011-08-18 20:51:04 +0000377/// UpdatePHINodes - Update the PHI nodes in OrigBB to include the values coming
378/// from NewBB. This also updates AliasAnalysis, if available.
379static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
380 ArrayRef<BasicBlock*> Preds, BranchInst *BI,
381 Pass *P, bool HasLoopExit) {
382 // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
383 AliasAnalysis *AA = P ? P->getAnalysisIfAvailable<AliasAnalysis>() : 0;
384 for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
385 PHINode *PN = cast<PHINode>(I++);
386
387 // Check to see if all of the values coming in are the same. If so, we
388 // don't need to create a new PHI node, unless it's needed for LCSSA.
389 Value *InVal = 0;
390 if (!HasLoopExit) {
391 InVal = PN->getIncomingValueForBlock(Preds[0]);
392 for (unsigned i = 1, e = Preds.size(); i != e; ++i)
393 if (InVal != PN->getIncomingValueForBlock(Preds[i])) {
394 InVal = 0;
395 break;
396 }
397 }
398
399 if (InVal) {
400 // If all incoming values for the new PHI would be the same, just don't
401 // make a new PHI. Instead, just remove the incoming values from the old
402 // PHI.
Hal Finkelfc3b7bb2013-10-04 23:41:05 +0000403 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
404 // Explicitly check the BB index here to handle duplicates in Preds.
405 int Idx = PN->getBasicBlockIndex(Preds[i]);
406 if (Idx >= 0)
407 PN->removeIncomingValue(Idx, false);
408 }
Bill Wendling1c44d862011-08-18 20:51:04 +0000409 } else {
410 // If the values coming into the block are not the same, we need a PHI.
411 // Create the new PHI node, insert it into NewBB at the end of the block
412 PHINode *NewPHI =
413 PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
414 if (AA) AA->copyValue(PN, NewPHI);
Jakub Staszake673b542013-01-14 23:16:36 +0000415
Bill Wendling1c44d862011-08-18 20:51:04 +0000416 // Move all of the PHI values for 'Preds' to the new PHI.
417 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
418 Value *V = PN->removeIncomingValue(Preds[i], false);
419 NewPHI->addIncoming(V, Preds[i]);
420 }
421
422 InVal = NewPHI;
423 }
424
425 // Add an incoming value to the PHI node in the loop for the preheader
426 // edge.
427 PN->addIncoming(InVal, NewBB);
428 }
429}
430
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000431/// SplitBlockPredecessors - This method transforms BB by introducing a new
432/// basic block into the function, and moving some of the predecessors of BB to
433/// be predecessors of the new block. The new predecessors are indicated by the
434/// Preds array, which has NumPreds elements in it. The new block is given a
435/// suffix of 'Suffix'.
436///
Dan Gohman5c89b522009-09-08 15:45:00 +0000437/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
Cameron Zwarich30127872011-01-18 04:11:31 +0000438/// LoopInfo, and LCCSA but no other analyses. In particular, it does not
439/// preserve LoopSimplify (because it's complicated to handle the case where one
440/// of the edges being split is an exit of a loop with other exits).
Dan Gohman5c89b522009-09-08 15:45:00 +0000441///
Jakub Staszake673b542013-01-14 23:16:36 +0000442BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
Jakub Staszak2fac1d52011-12-09 21:19:53 +0000443 ArrayRef<BasicBlock*> Preds,
444 const char *Suffix, Pass *P) {
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000445 // Create new basic block, insert right before the original block.
Owen Anderson1d0be152009-08-13 21:58:54 +0000446 BasicBlock *NewBB = BasicBlock::Create(BB->getContext(), BB->getName()+Suffix,
447 BB->getParent(), BB);
Jakub Staszake673b542013-01-14 23:16:36 +0000448
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000449 // The new block unconditionally branches to the old block.
450 BranchInst *BI = BranchInst::Create(BB, NewBB);
Jakub Staszake673b542013-01-14 23:16:36 +0000451
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000452 // Move the edges from Preds to point to NewBB instead of BB.
Jakub Staszak2fac1d52011-12-09 21:19:53 +0000453 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
Dan Gohmanb8eb17c2009-11-05 18:25:44 +0000454 // This is slightly more strict than necessary; the minimum requirement
455 // is that there be no more than one indirectbr branching to BB. And
456 // all BlockAddress uses would need to be updated.
457 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
458 "Cannot split an edge from an IndirectBrInst");
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000459 Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
Dan Gohman5c89b522009-09-08 15:45:00 +0000460 }
461
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000462 // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
463 // node becomes an incoming value for BB's phi node. However, if the Preds
464 // list is empty, we need to insert dummy entries into the PHI nodes in BB to
465 // account for the newly created predecessor.
Jakub Staszak2fac1d52011-12-09 21:19:53 +0000466 if (Preds.size() == 0) {
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000467 // Insert dummy values as the incoming value.
468 for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
Owen Anderson9e9a0d52009-07-30 23:03:37 +0000469 cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000470 return NewBB;
471 }
Dan Gohman5c89b522009-09-08 15:45:00 +0000472
Bill Wendlinga644b332011-08-18 05:25:23 +0000473 // Update DominatorTree, LoopInfo, and LCCSA analysis information.
474 bool HasLoopExit = false;
Jakub Staszak2fac1d52011-12-09 21:19:53 +0000475 UpdateAnalysisInformation(BB, NewBB, Preds, P, HasLoopExit);
Dan Gohman5c89b522009-09-08 15:45:00 +0000476
Bill Wendling1c44d862011-08-18 20:51:04 +0000477 // Update the PHI nodes in BB with the values coming from NewBB.
Jakub Staszak2fac1d52011-12-09 21:19:53 +0000478 UpdatePHINodes(BB, NewBB, Preds, BI, P, HasLoopExit);
Chris Lattner54b9c3b2008-04-21 01:28:02 +0000479 return NewBB;
480}
Chris Lattner52c95852008-11-27 08:10:05 +0000481
Bill Wendling7e8840c2011-08-19 00:05:40 +0000482/// SplitLandingPadPredecessors - This method transforms the landing pad,
483/// OrigBB, by introducing two new basic blocks into the function. One of those
484/// new basic blocks gets the predecessors listed in Preds. The other basic
485/// block gets the remaining predecessors of OrigBB. The landingpad instruction
486/// OrigBB is clone into both of the new basic blocks. The new blocks are given
487/// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
Jakub Staszake673b542013-01-14 23:16:36 +0000488///
Bill Wendling7e8840c2011-08-19 00:05:40 +0000489/// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
490/// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
491/// it does not preserve LoopSimplify (because it's complicated to handle the
492/// case where one of the edges being split is an exit of a loop with other
493/// exits).
Jakub Staszake673b542013-01-14 23:16:36 +0000494///
Bill Wendling7e8840c2011-08-19 00:05:40 +0000495void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
496 ArrayRef<BasicBlock*> Preds,
497 const char *Suffix1, const char *Suffix2,
498 Pass *P,
499 SmallVectorImpl<BasicBlock*> &NewBBs) {
500 assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
501
502 // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
503 // it right before the original block.
504 BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
505 OrigBB->getName() + Suffix1,
506 OrigBB->getParent(), OrigBB);
507 NewBBs.push_back(NewBB1);
508
509 // The new block unconditionally branches to the old block.
510 BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
511
512 // Move the edges from Preds to point to NewBB1 instead of OrigBB.
513 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
514 // This is slightly more strict than necessary; the minimum requirement
515 // is that there be no more than one indirectbr branching to BB. And
516 // all BlockAddress uses would need to be updated.
517 assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
518 "Cannot split an edge from an IndirectBrInst");
519 Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
520 }
521
522 // Update DominatorTree, LoopInfo, and LCCSA analysis information.
523 bool HasLoopExit = false;
524 UpdateAnalysisInformation(OrigBB, NewBB1, Preds, P, HasLoopExit);
525
526 // Update the PHI nodes in OrigBB with the values coming from NewBB1.
527 UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, P, HasLoopExit);
528
Bill Wendling7e8840c2011-08-19 00:05:40 +0000529 // Move the remaining edges from OrigBB to point to NewBB2.
530 SmallVector<BasicBlock*, 8> NewBB2Preds;
531 for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
532 i != e; ) {
533 BasicBlock *Pred = *i++;
Bill Wendling94657b92011-08-19 23:46:30 +0000534 if (Pred == NewBB1) continue;
Bill Wendling7e8840c2011-08-19 00:05:40 +0000535 assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
536 "Cannot split an edge from an IndirectBrInst");
Bill Wendling7e8840c2011-08-19 00:05:40 +0000537 NewBB2Preds.push_back(Pred);
538 e = pred_end(OrigBB);
539 }
540
Bill Wendling94657b92011-08-19 23:46:30 +0000541 BasicBlock *NewBB2 = 0;
542 if (!NewBB2Preds.empty()) {
543 // Create another basic block for the rest of OrigBB's predecessors.
544 NewBB2 = BasicBlock::Create(OrigBB->getContext(),
545 OrigBB->getName() + Suffix2,
546 OrigBB->getParent(), OrigBB);
547 NewBBs.push_back(NewBB2);
Bill Wendling7e8840c2011-08-19 00:05:40 +0000548
Bill Wendling94657b92011-08-19 23:46:30 +0000549 // The new block unconditionally branches to the old block.
550 BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
551
552 // Move the remaining edges from OrigBB to point to NewBB2.
553 for (SmallVectorImpl<BasicBlock*>::iterator
554 i = NewBB2Preds.begin(), e = NewBB2Preds.end(); i != e; ++i)
555 (*i)->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
556
557 // Update DominatorTree, LoopInfo, and LCCSA analysis information.
558 HasLoopExit = false;
559 UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, P, HasLoopExit);
560
561 // Update the PHI nodes in OrigBB with the values coming from NewBB2.
562 UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, P, HasLoopExit);
563 }
Bill Wendling7e8840c2011-08-19 00:05:40 +0000564
565 LandingPadInst *LPad = OrigBB->getLandingPadInst();
566 Instruction *Clone1 = LPad->clone();
567 Clone1->setName(Twine("lpad") + Suffix1);
568 NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
569
Bill Wendling94657b92011-08-19 23:46:30 +0000570 if (NewBB2) {
571 Instruction *Clone2 = LPad->clone();
572 Clone2->setName(Twine("lpad") + Suffix2);
573 NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
Bill Wendling7e8840c2011-08-19 00:05:40 +0000574
Bill Wendling94657b92011-08-19 23:46:30 +0000575 // Create a PHI node for the two cloned landingpad instructions.
576 PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
577 PN->addIncoming(Clone1, NewBB1);
578 PN->addIncoming(Clone2, NewBB2);
579 LPad->replaceAllUsesWith(PN);
580 LPad->eraseFromParent();
581 } else {
582 // There is no second clone. Just replace the landing pad with the first
583 // clone.
584 LPad->replaceAllUsesWith(Clone1);
585 LPad->eraseFromParent();
586 }
Bill Wendling7e8840c2011-08-19 00:05:40 +0000587}
588
Evan Chengc3f507f2011-01-29 04:46:23 +0000589/// FoldReturnIntoUncondBranch - This method duplicates the specified return
590/// instruction into a predecessor which ends in an unconditional branch. If
591/// the return instruction returns a value defined by a PHI, propagate the
592/// right value into the return. It returns the new return instruction in the
593/// predecessor.
594ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
595 BasicBlock *Pred) {
596 Instruction *UncondBranch = Pred->getTerminator();
597 // Clone the return and add it to the end of the predecessor.
598 Instruction *NewRet = RI->clone();
599 Pred->getInstList().push_back(NewRet);
Jakub Staszake673b542013-01-14 23:16:36 +0000600
Evan Chengc3f507f2011-01-29 04:46:23 +0000601 // If the return instruction returns a value, and if the value was a
602 // PHI node in "BB", propagate the right value into the return.
603 for (User::op_iterator i = NewRet->op_begin(), e = NewRet->op_end();
Evan Cheng9c777a42012-07-27 21:21:26 +0000604 i != e; ++i) {
605 Value *V = *i;
606 Instruction *NewBC = 0;
607 if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
608 // Return value might be bitcasted. Clone and insert it before the
609 // return instruction.
610 V = BCI->getOperand(0);
611 NewBC = BCI->clone();
612 Pred->getInstList().insert(NewRet, NewBC);
613 *i = NewBC;
614 }
615 if (PHINode *PN = dyn_cast<PHINode>(V)) {
616 if (PN->getParent() == BB) {
617 if (NewBC)
618 NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
619 else
620 *i = PN->getIncomingValueForBlock(Pred);
621 }
622 }
623 }
Jakub Staszake673b542013-01-14 23:16:36 +0000624
Evan Chengc3f507f2011-01-29 04:46:23 +0000625 // Update any PHI nodes in the returning block to realize that we no
626 // longer branch to them.
627 BB->removePredecessor(Pred);
628 UncondBranch->eraseFromParent();
629 return cast<ReturnInst>(NewRet);
Mike Stumpfe095f32009-05-04 18:40:41 +0000630}
Devang Patel40348e82011-04-29 22:28:59 +0000631
Evgeniy Stepanov4a2dec02012-10-19 10:48:31 +0000632/// SplitBlockAndInsertIfThen - Split the containing block at the
633/// specified instruction - everything before and including Cmp stays
634/// in the old basic block, and everything after Cmp is moved to a
635/// new block. The two blocks are connected by a conditional branch
636/// (with value of Cmp being the condition).
637/// Before:
638/// Head
639/// Cmp
640/// Tail
641/// After:
642/// Head
643/// Cmp
644/// if (Cmp)
645/// ThenBlock
646/// Tail
647///
648/// If Unreachable is true, then ThenBlock ends with
649/// UnreachableInst, otherwise it branches to Tail.
650/// Returns the NewBasicBlock's terminator.
651
652TerminatorInst *llvm::SplitBlockAndInsertIfThen(Instruction *Cmp,
653 bool Unreachable, MDNode *BranchWeights) {
654 Instruction *SplitBefore = Cmp->getNextNode();
655 BasicBlock *Head = SplitBefore->getParent();
656 BasicBlock *Tail = Head->splitBasicBlock(SplitBefore);
657 TerminatorInst *HeadOldTerm = Head->getTerminator();
658 LLVMContext &C = Head->getContext();
659 BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
660 TerminatorInst *CheckTerm;
661 if (Unreachable)
662 CheckTerm = new UnreachableInst(C, ThenBlock);
663 else
664 CheckTerm = BranchInst::Create(Tail, ThenBlock);
665 BranchInst *HeadNewTerm =
666 BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/Tail, Cmp);
667 HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
668 ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
669 return CheckTerm;
670}
Tom Stellard01d72032013-08-06 02:43:45 +0000671
672/// GetIfCondition - Given a basic block (BB) with two predecessors,
673/// check to see if the merge at this block is due
674/// to an "if condition". If so, return the boolean condition that determines
675/// which entry into BB will be taken. Also, return by references the block
676/// that will be entered from if the condition is true, and the block that will
677/// be entered if the condition is false.
678///
679/// This does no checking to see if the true/false blocks have large or unsavory
680/// instructions in them.
681Value *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
682 BasicBlock *&IfFalse) {
683 PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
684 BasicBlock *Pred1 = NULL;
685 BasicBlock *Pred2 = NULL;
686
687 if (SomePHI) {
688 if (SomePHI->getNumIncomingValues() != 2)
689 return NULL;
690 Pred1 = SomePHI->getIncomingBlock(0);
691 Pred2 = SomePHI->getIncomingBlock(1);
692 } else {
693 pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
694 if (PI == PE) // No predecessor
695 return NULL;
696 Pred1 = *PI++;
697 if (PI == PE) // Only one predecessor
698 return NULL;
699 Pred2 = *PI++;
700 if (PI != PE) // More than two predecessors
701 return NULL;
702 }
703
704 // We can only handle branches. Other control flow will be lowered to
705 // branches if possible anyway.
706 BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
707 BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
708 if (Pred1Br == 0 || Pred2Br == 0)
709 return 0;
710
711 // Eliminate code duplication by ensuring that Pred1Br is conditional if
712 // either are.
713 if (Pred2Br->isConditional()) {
714 // If both branches are conditional, we don't have an "if statement". In
715 // reality, we could transform this case, but since the condition will be
716 // required anyway, we stand no chance of eliminating it, so the xform is
717 // probably not profitable.
718 if (Pred1Br->isConditional())
719 return 0;
720
721 std::swap(Pred1, Pred2);
722 std::swap(Pred1Br, Pred2Br);
723 }
724
725 if (Pred1Br->isConditional()) {
726 // The only thing we have to watch out for here is to make sure that Pred2
727 // doesn't have incoming edges from other blocks. If it does, the condition
728 // doesn't dominate BB.
729 if (Pred2->getSinglePredecessor() == 0)
730 return 0;
731
732 // If we found a conditional branch predecessor, make sure that it branches
733 // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
734 if (Pred1Br->getSuccessor(0) == BB &&
735 Pred1Br->getSuccessor(1) == Pred2) {
736 IfTrue = Pred1;
737 IfFalse = Pred2;
738 } else if (Pred1Br->getSuccessor(0) == Pred2 &&
739 Pred1Br->getSuccessor(1) == BB) {
740 IfTrue = Pred2;
741 IfFalse = Pred1;
742 } else {
743 // We know that one arm of the conditional goes to BB, so the other must
744 // go somewhere unrelated, and this must not be an "if statement".
745 return 0;
746 }
747
748 return Pred1Br->getCondition();
749 }
750
751 // Ok, if we got here, both predecessors end with an unconditional branch to
752 // BB. Don't panic! If both blocks only have a single (identical)
753 // predecessor, and THAT is a conditional branch, then we're all ok!
754 BasicBlock *CommonPred = Pred1->getSinglePredecessor();
755 if (CommonPred == 0 || CommonPred != Pred2->getSinglePredecessor())
756 return 0;
757
758 // Otherwise, if this is a conditional branch, then we can use it!
759 BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
760 if (BI == 0) return 0;
761
762 assert(BI->isConditional() && "Two successors but not conditional?");
763 if (BI->getSuccessor(0) == Pred1) {
764 IfTrue = Pred1;
765 IfFalse = Pred2;
766 } else {
767 IfTrue = Pred2;
768 IfFalse = Pred1;
769 }
770 return BI->getCondition();
771}