blob: 95a5394f90b460912e959a641ac6f9f09fb9d8d1 [file] [log] [blame]
Evan Cheng4e654852007-05-16 02:00:57 +00001//===-- IfConversion.cpp - Machine code if conversion pass. ---------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Evan Cheng and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the machine instruction level if-conversion pass.
11//
12//===----------------------------------------------------------------------===//
13
Evan Cheng5f702182007-06-01 00:12:12 +000014#define DEBUG_TYPE "ifcvt"
15#include "llvm/Function.h"
Evan Cheng4e654852007-05-16 02:00:57 +000016#include "llvm/CodeGen/Passes.h"
17#include "llvm/CodeGen/MachineModuleInfo.h"
18#include "llvm/CodeGen/MachineFunctionPass.h"
19#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng86cbfea2007-05-18 00:20:58 +000020#include "llvm/Target/TargetLowering.h"
Evan Cheng4e654852007-05-16 02:00:57 +000021#include "llvm/Target/TargetMachine.h"
22#include "llvm/Support/Debug.h"
Evan Cheng36489bb2007-05-18 19:26:33 +000023#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng4e654852007-05-16 02:00:57 +000024#include "llvm/ADT/Statistic.h"
25using namespace llvm;
26
Evan Chengb6665f62007-06-04 06:47:22 +000027STATISTIC(NumSimple, "Number of simple if-conversions performed");
28STATISTIC(NumSimpleRev, "Number of simple (reversed) if-conversions performed");
29STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
30STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
Evan Cheng4e654852007-05-16 02:00:57 +000031STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
32
33namespace {
34 class IfConverter : public MachineFunctionPass {
35 enum BBICKind {
Evan Chenga13aa952007-05-23 07:23:16 +000036 ICNotAnalyzed, // BB has not been analyzed.
37 ICReAnalyze, // BB must be re-analyzed.
Evan Cheng4e654852007-05-16 02:00:57 +000038 ICNotClassfied, // BB data valid, but not classified.
Evan Chengb6665f62007-06-04 06:47:22 +000039 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
40 ICSimpleFalse, // Same as ICSimple, but on the false path.
Evan Chenga6b4f432007-05-21 22:22:58 +000041 ICTriangle, // BB is entry of a triangle sub-CFG.
42 ICDiamond, // BB is entry of a diamond sub-CFG.
43 ICChild, // BB is part of the sub-CFG that'll be predicated.
44 ICDead // BB has been converted and merged, it's now dead.
Evan Cheng4e654852007-05-16 02:00:57 +000045 };
46
47 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
48 /// if-conversion feasibility analysis. This includes results from
49 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
Evan Cheng86cbfea2007-05-18 00:20:58 +000050 /// classification, and common tail block of its successors (if it's a
Evan Chengcf6cc112007-05-18 18:14:37 +000051 /// diamond shape), its size, whether it's predicable, and whether any
52 /// instruction can clobber the 'would-be' predicate.
Evan Chenga13aa952007-05-23 07:23:16 +000053 ///
54 /// Kind - Type of block. See BBICKind.
55 /// NonPredSize - Number of non-predicated instructions.
Evan Chengb6665f62007-06-04 06:47:22 +000056 /// IsAnalyzable - True if AnalyzeBranch() returns false.
Evan Chenga13aa952007-05-23 07:23:16 +000057 /// ModifyPredicate - FIXME: Not used right now. True if BB would modify
58 /// the predicate (e.g. has cmp, call, etc.)
59 /// BB - Corresponding MachineBasicBlock.
60 /// TrueBB / FalseBB- See AnalyzeBranch().
61 /// BrCond - Conditions for end of block conditional branches.
62 /// Predicate - Predicate used in the BB.
Evan Cheng4e654852007-05-16 02:00:57 +000063 struct BBInfo {
64 BBICKind Kind;
Evan Chenga13aa952007-05-23 07:23:16 +000065 unsigned NonPredSize;
Evan Chengb6665f62007-06-04 06:47:22 +000066 bool IsAnalyzable;
Evan Chenga13aa952007-05-23 07:23:16 +000067 bool ModifyPredicate;
Evan Cheng86cbfea2007-05-18 00:20:58 +000068 MachineBasicBlock *BB;
69 MachineBasicBlock *TrueBB;
70 MachineBasicBlock *FalseBB;
71 MachineBasicBlock *TailBB;
Evan Chenga13aa952007-05-23 07:23:16 +000072 std::vector<MachineOperand> BrCond;
73 std::vector<MachineOperand> Predicate;
Evan Chengb6665f62007-06-04 06:47:22 +000074 BBInfo() : Kind(ICNotAnalyzed), NonPredSize(0),
75 IsAnalyzable(false), ModifyPredicate(false),
Evan Chenga6b4f432007-05-21 22:22:58 +000076 BB(0), TrueBB(0), FalseBB(0), TailBB(0) {}
Evan Cheng4e654852007-05-16 02:00:57 +000077 };
78
Evan Cheng82582102007-05-30 19:49:19 +000079 /// Roots - Basic blocks that do not have successors. These are the starting
80 /// points of Graph traversal.
81 std::vector<MachineBasicBlock*> Roots;
82
Evan Cheng4e654852007-05-16 02:00:57 +000083 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
84 /// basic block number.
85 std::vector<BBInfo> BBAnalysis;
86
Evan Cheng86cbfea2007-05-18 00:20:58 +000087 const TargetLowering *TLI;
Evan Cheng4e654852007-05-16 02:00:57 +000088 const TargetInstrInfo *TII;
89 bool MadeChange;
90 public:
91 static char ID;
92 IfConverter() : MachineFunctionPass((intptr_t)&ID) {}
93
94 virtual bool runOnMachineFunction(MachineFunction &MF);
95 virtual const char *getPassName() const { return "If converter"; }
96
97 private:
Evan Chengb6665f62007-06-04 06:47:22 +000098 bool ReverseBranchCondition(BBInfo &BBI);
Evan Chengcf6cc112007-05-18 18:14:37 +000099 void StructuralAnalysis(MachineBasicBlock *BB);
Evan Chengb6665f62007-06-04 06:47:22 +0000100 bool FeasibilityAnalysis(BBInfo &BBI,
101 std::vector<MachineOperand> &Cond,
102 bool IgnoreTerm = false);
Evan Cheng82582102007-05-30 19:49:19 +0000103 bool AttemptRestructuring(BBInfo &BBI);
104 bool AnalyzeBlocks(MachineFunction &MF,
Evan Chenga13aa952007-05-23 07:23:16 +0000105 std::vector<BBInfo*> &Candidates);
Evan Cheng82582102007-05-30 19:49:19 +0000106 void ReTryPreds(MachineBasicBlock *BB);
Evan Chengb6665f62007-06-04 06:47:22 +0000107 bool IfConvertSimple(BBInfo &BBI);
Evan Cheng4e654852007-05-16 02:00:57 +0000108 bool IfConvertTriangle(BBInfo &BBI);
Evan Chengcf6cc112007-05-18 18:14:37 +0000109 bool IfConvertDiamond(BBInfo &BBI);
Evan Chenga13aa952007-05-23 07:23:16 +0000110 void PredicateBlock(BBInfo &BBI,
Evan Cheng4e654852007-05-16 02:00:57 +0000111 std::vector<MachineOperand> &Cond,
112 bool IgnoreTerm = false);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000113 void MergeBlocks(BBInfo &TrueBBI, BBInfo &FalseBBI);
Evan Cheng5f702182007-06-01 00:12:12 +0000114
115 // IfcvtCandidateCmp - Used to sort if-conversion candidates.
116 static bool IfcvtCandidateCmp(BBInfo* C1, BBInfo* C2){
117 // Favor diamond over triangle, etc.
118 return (unsigned)C1->Kind < (unsigned)C2->Kind;
119 }
Evan Cheng4e654852007-05-16 02:00:57 +0000120 };
121 char IfConverter::ID = 0;
122}
123
124FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
125
126bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng86cbfea2007-05-18 00:20:58 +0000127 TLI = MF.getTarget().getTargetLowering();
Evan Cheng4e654852007-05-16 02:00:57 +0000128 TII = MF.getTarget().getInstrInfo();
129 if (!TII) return false;
130
Evan Cheng5f702182007-06-01 00:12:12 +0000131 DOUT << "\nIfcvt: function \'" << MF.getFunction()->getName() << "\'\n";
132
Evan Cheng4e654852007-05-16 02:00:57 +0000133 MF.RenumberBlocks();
Evan Cheng5f702182007-06-01 00:12:12 +0000134 BBAnalysis.resize(MF.getNumBlockIDs());
Evan Cheng4e654852007-05-16 02:00:57 +0000135
Evan Cheng82582102007-05-30 19:49:19 +0000136 // Look for root nodes, i.e. blocks without successors.
137 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
138 if (I->succ_size() == 0)
139 Roots.push_back(I);
140
Evan Cheng7f8ff8a2007-05-18 19:32:08 +0000141 std::vector<BBInfo*> Candidates;
Evan Cheng47d25022007-05-18 01:55:58 +0000142 MadeChange = false;
Evan Chenga13aa952007-05-23 07:23:16 +0000143 while (true) {
Evan Chenga13aa952007-05-23 07:23:16 +0000144 // Do an intial analysis for each basic block and finding all the potential
145 // candidates to perform if-convesion.
Evan Chengb6665f62007-06-04 06:47:22 +0000146 bool Change = AnalyzeBlocks(MF, Candidates);
Evan Chenga13aa952007-05-23 07:23:16 +0000147 while (!Candidates.empty()) {
148 BBInfo &BBI = *Candidates.back();
149 Candidates.pop_back();
Evan Chengb6665f62007-06-04 06:47:22 +0000150
151 bool RetVal = false;
Evan Chenga13aa952007-05-23 07:23:16 +0000152 switch (BBI.Kind) {
153 default: assert(false && "Unexpected!");
154 break;
Evan Cheng5f702182007-06-01 00:12:12 +0000155 case ICReAnalyze:
156 // One or more of 'childrean' have been modified, abort!
157 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000158 case ICSimple:
159 case ICSimpleFalse:
160 DOUT << "Ifcvt (Simple" << (BBI.Kind == ICSimpleFalse ? " false" : "")
161 << "): BB#" << BBI.BB->getNumber() << " ";
162 RetVal = IfConvertSimple(BBI);
163 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
164 if (RetVal)
165 if (BBI.Kind == ICSimple) NumSimple++;
166 else NumSimpleRev++;
167 break;
Evan Chenga13aa952007-05-23 07:23:16 +0000168 case ICTriangle:
Evan Chengb6665f62007-06-04 06:47:22 +0000169 DOUT << "Ifcvt (Triangle): BB#" << BBI.BB->getNumber() << " ";
170 RetVal = IfConvertTriangle(BBI);
171 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
172 if (RetVal) NumTriangle++;
Evan Chenga13aa952007-05-23 07:23:16 +0000173 break;
174 case ICDiamond:
Evan Chengb6665f62007-06-04 06:47:22 +0000175 DOUT << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " ";
176 RetVal = IfConvertDiamond(BBI);
177 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
178 if (RetVal) NumDiamonds++;
Evan Chenga13aa952007-05-23 07:23:16 +0000179 break;
180 }
Evan Chengb6665f62007-06-04 06:47:22 +0000181 Change |= RetVal;
Evan Cheng4e654852007-05-16 02:00:57 +0000182 }
Evan Chenga13aa952007-05-23 07:23:16 +0000183
Evan Chenga13aa952007-05-23 07:23:16 +0000184 if (!Change)
185 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000186 MadeChange |= Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000187 }
Evan Cheng47d25022007-05-18 01:55:58 +0000188
Evan Cheng82582102007-05-30 19:49:19 +0000189 Roots.clear();
Evan Cheng47d25022007-05-18 01:55:58 +0000190 BBAnalysis.clear();
191
Evan Cheng4e654852007-05-16 02:00:57 +0000192 return MadeChange;
193}
194
195static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng86cbfea2007-05-18 00:20:58 +0000196 MachineBasicBlock *TrueBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000197 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
198 E = BB->succ_end(); SI != E; ++SI) {
199 MachineBasicBlock *SuccBB = *SI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000200 if (SuccBB != TrueBB)
Evan Cheng4e654852007-05-16 02:00:57 +0000201 return SuccBB;
202 }
203 return NULL;
204}
205
Evan Chengb6665f62007-06-04 06:47:22 +0000206bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
207 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
208 TII->RemoveBranch(*BBI.BB);
209 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond);
210 std::swap(BBI.TrueBB, BBI.FalseBB);
211 return true;
212 }
213 return false;
214}
215
Evan Chengcf6cc112007-05-18 18:14:37 +0000216/// StructuralAnalysis - Analyze the structure of the sub-CFG starting from
217/// the specified block. Record its successors and whether it looks like an
218/// if-conversion candidate.
219void IfConverter::StructuralAnalysis(MachineBasicBlock *BB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000220 BBInfo &BBI = BBAnalysis[BB->getNumber()];
221
Evan Chengb6665f62007-06-04 06:47:22 +0000222 if (BBI.Kind == ICReAnalyze)
223 BBI.BrCond.clear();
224 else {
Evan Chenga13aa952007-05-23 07:23:16 +0000225 if (BBI.Kind != ICNotAnalyzed)
226 return; // Already analyzed.
227 BBI.BB = BB;
228 BBI.NonPredSize = std::distance(BB->begin(), BB->end());
Evan Chenga13aa952007-05-23 07:23:16 +0000229 }
Evan Cheng86cbfea2007-05-18 00:20:58 +0000230
Evan Cheng4bec8ae2007-05-25 00:59:01 +0000231 // Look for 'root' of a simple (non-nested) triangle or diamond.
232 BBI.Kind = ICNotClassfied;
Evan Chengb6665f62007-06-04 06:47:22 +0000233 BBI.IsAnalyzable =
234 !TII->AnalyzeBranch(*BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
235 if (!BBI.IsAnalyzable || BBI.BrCond.size() == 0)
Evan Cheng4bec8ae2007-05-25 00:59:01 +0000236 return;
237
Evan Chengcf6cc112007-05-18 18:14:37 +0000238 StructuralAnalysis(BBI.TrueBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000239 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengd6ddc302007-05-16 21:54:37 +0000240
241 // No false branch. This BB must end with a conditional branch and a
242 // fallthrough.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000243 if (!BBI.FalseBB)
244 BBI.FalseBB = findFalseBlock(BB, BBI.TrueBB);
245 assert(BBI.FalseBB && "Expected to find the fallthrough block!");
Evan Chengc5d05ef2007-05-16 05:11:10 +0000246
Evan Chengcf6cc112007-05-18 18:14:37 +0000247 StructuralAnalysis(BBI.FalseBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000248 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chengb6665f62007-06-04 06:47:22 +0000249
250 // Look for more opportunities to if-convert a triangle. Try to restructure
251 // the CFG to form a triangle with the 'false' path.
252 std::vector<MachineOperand> RevCond(BBI.BrCond);
253 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
254 if (FalseBBI.FalseBB) {
255 if (TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB)
256 return;
257 std::vector<MachineOperand> Cond(BBI.BrCond);
258 if (CanRevCond &&
259 FalseBBI.TrueBB && FalseBBI.BB->pred_size() == 1 &&
260 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
261 std::vector<MachineOperand> FalseCond(FalseBBI.BrCond);
262 if (FalseBBI.TrueBB == BBI.TrueBB &&
263 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
264 // Reverse 'true' and 'false' paths.
265 ReverseBranchCondition(BBI);
266 BBI.Kind = ICTriangle;
267 FalseBBI.Kind = ICChild;
268 } else if (FalseBBI.FalseBB == BBI.TrueBB &&
269 !TII->ReverseBranchCondition(FalseCond) &&
270 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
271 // Reverse 'false' block's 'true' and 'false' paths and then
272 // reverse 'true' and 'false' paths.
273 ReverseBranchCondition(FalseBBI);
274 ReverseBranchCondition(BBI);
275 BBI.Kind = ICTriangle;
276 FalseBBI.Kind = ICChild;
277 }
278 }
279 } else if (TrueBBI.TrueBB == FalseBBI.TrueBB && CanRevCond &&
280 TrueBBI.BB->pred_size() == 1 &&
281 TrueBBI.BB->pred_size() == 1 &&
282 // Check the 'true' and 'false' blocks if either isn't ended with
283 // a branch. If the block does not fallthrough to another block
284 // then we need to add a branch to its successor.
285 !(TrueBBI.ModifyPredicate &&
286 !TrueBBI.TrueBB && TrueBBI.BB->succ_size()) &&
287 !(FalseBBI.ModifyPredicate &&
288 !FalseBBI.TrueBB && FalseBBI.BB->succ_size()) &&
289 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
290 FeasibilityAnalysis(FalseBBI, RevCond)) {
Evan Cheng4e654852007-05-16 02:00:57 +0000291 // Diamond:
292 // EBB
293 // / \_
294 // | |
295 // TBB FBB
296 // \ /
Evan Cheng86cbfea2007-05-18 00:20:58 +0000297 // TailBB
Evan Cheng4e654852007-05-16 02:00:57 +0000298 // Note MBB can be empty in case both TBB and FBB are return blocks.
Evan Chenga6b4f432007-05-21 22:22:58 +0000299 BBI.Kind = ICDiamond;
300 TrueBBI.Kind = FalseBBI.Kind = ICChild;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000301 BBI.TailBB = TrueBBI.TrueBB;
Evan Chengb6665f62007-06-04 06:47:22 +0000302 } else {
303 // FIXME: Consider duplicating if BB is small.
304 bool TryTriangle = TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB &&
305 BBI.TrueBB->pred_size() == 1;
306 bool TrySimple = TrueBBI.BrCond.size() == 0 && BBI.TrueBB->pred_size() == 1;
307 if ((TryTriangle || TrySimple) &&
308 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
309 if (TryTriangle) {
310 // Triangle:
311 // EBB
312 // | \_
313 // | |
314 // | TBB
315 // | /
316 // FBB
317 BBI.Kind = ICTriangle;
318 TrueBBI.Kind = ICChild;
319 } else {
320 // Simple (split, no rejoin):
321 // EBB
322 // | \_
323 // | |
324 // | TBB---> exit
325 // |
326 // FBB
327 BBI.Kind = ICSimple;
328 TrueBBI.Kind = ICChild;
329 }
330 } else if (FalseBBI.BrCond.size() == 0 && BBI.FalseBB->pred_size() == 1) {
331 // Try 'simple' on the other path...
332 std::vector<MachineOperand> RevCond(BBI.BrCond);
333 if (TII->ReverseBranchCondition(RevCond))
334 assert(false && "Unable to reverse branch condition!");
335 if (FeasibilityAnalysis(FalseBBI, RevCond)) {
336 BBI.Kind = ICSimpleFalse;
337 FalseBBI.Kind = ICChild;
338 }
339 }
Evan Cheng4e654852007-05-16 02:00:57 +0000340 }
341 return;
342}
343
Evan Chengcf6cc112007-05-18 18:14:37 +0000344/// FeasibilityAnalysis - Determine if the block is predicable. In most
345/// cases, that means all the instructions in the block has M_PREDICABLE flag.
346/// Also checks if the block contains any instruction which can clobber a
347/// predicate (e.g. condition code register). If so, the block is not
Evan Chengb6665f62007-06-04 06:47:22 +0000348/// predicable unless it's the last instruction. If IgnoreTerm is true then
349/// all the terminator instructions are skipped.
350bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
351 std::vector<MachineOperand> &Cond,
352 bool IgnoreTerm) {
353 // If the block is dead, or it is going to be the entry block of a sub-CFG
354 // that will be if-converted, then it cannot be predicated.
355 if (BBI.Kind != ICNotAnalyzed &&
356 BBI.Kind != ICNotClassfied &&
357 BBI.Kind != ICChild)
358 return false;
359
360 // Check predication threshold.
Evan Chenga13aa952007-05-23 07:23:16 +0000361 if (BBI.NonPredSize == 0 || BBI.NonPredSize > TLI->getIfCvtBlockSizeLimit())
Evan Chengb6665f62007-06-04 06:47:22 +0000362 return false;
363
364 // If it is already predicated, check if its predicate subsumes the new
365 // predicate.
366 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Cond))
367 return false;
Evan Chengcf6cc112007-05-18 18:14:37 +0000368
369 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
370 I != E; ++I) {
Evan Chengb6665f62007-06-04 06:47:22 +0000371 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
372 continue;
Evan Chengcf6cc112007-05-18 18:14:37 +0000373 // TODO: check if instruction clobbers predicate.
Evan Chengcf6cc112007-05-18 18:14:37 +0000374 if (!I->isPredicable())
Evan Chengb6665f62007-06-04 06:47:22 +0000375 return false;
Evan Chengcf6cc112007-05-18 18:14:37 +0000376 }
377
Evan Chengb6665f62007-06-04 06:47:22 +0000378 return true;
Evan Chengcf6cc112007-05-18 18:14:37 +0000379}
380
Evan Cheng82582102007-05-30 19:49:19 +0000381/// AttemptRestructuring - Restructure the sub-CFG rooted in the given block to
382/// expose more if-conversion opportunities. e.g.
383///
384/// cmp
385/// b le BB1
386/// / \____
387/// / |
388/// cmp |
389/// b eq BB1 |
390/// / \____ |
391/// / \ |
392/// BB1
393/// ==>
394///
395/// cmp
396/// b eq BB1
397/// / \____
398/// / |
399/// cmp |
400/// b le BB1 |
401/// / \____ |
402/// / \ |
403/// BB1
404bool IfConverter::AttemptRestructuring(BBInfo &BBI) {
405 return false;
406}
407
408/// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
409/// candidates. It returns true if any CFG restructuring is done to expose more
410/// if-conversion opportunities.
411bool IfConverter::AnalyzeBlocks(MachineFunction &MF,
Evan Chenga13aa952007-05-23 07:23:16 +0000412 std::vector<BBInfo*> &Candidates) {
Evan Cheng82582102007-05-30 19:49:19 +0000413 bool Change = false;
Evan Cheng36489bb2007-05-18 19:26:33 +0000414 std::set<MachineBasicBlock*> Visited;
Evan Cheng82582102007-05-30 19:49:19 +0000415 for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
416 for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited),
417 E = idf_ext_end(Roots[i], Visited); I != E; ++I) {
418 MachineBasicBlock *BB = *I;
419 StructuralAnalysis(BB);
420 BBInfo &BBI = BBAnalysis[BB->getNumber()];
421 switch (BBI.Kind) {
Evan Chengb6665f62007-06-04 06:47:22 +0000422 case ICSimple:
423 case ICSimpleFalse:
Evan Cheng82582102007-05-30 19:49:19 +0000424 case ICTriangle:
425 case ICDiamond:
426 Candidates.push_back(&BBI);
427 break;
428 default:
429 Change |= AttemptRestructuring(BBI);
430 break;
431 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000432 }
Evan Cheng4e654852007-05-16 02:00:57 +0000433 }
Evan Cheng82582102007-05-30 19:49:19 +0000434
Evan Cheng5f702182007-06-01 00:12:12 +0000435 // Sort to favor more complex ifcvt scheme.
436 std::stable_sort(Candidates.begin(), Candidates.end(), IfcvtCandidateCmp);
437
Evan Cheng82582102007-05-30 19:49:19 +0000438 return Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000439}
440
Evan Chengb6665f62007-06-04 06:47:22 +0000441/// isNextBlock - Returns true either if ToBB the next block after BB or
442/// that all the intervening blocks are empty.
Evan Chenga6b4f432007-05-21 22:22:58 +0000443static bool isNextBlock(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
Evan Cheng3ec42542007-06-04 20:33:36 +0000444 MachineFunction *MF = BB->getParent();
Evan Chengb6665f62007-06-04 06:47:22 +0000445 MachineFunction::iterator I = BB;
Evan Cheng3ec42542007-06-04 20:33:36 +0000446 while (++I != MF->end() && I != MachineFunction::iterator(ToBB))
Evan Chengb6665f62007-06-04 06:47:22 +0000447 if (!I->empty())
448 return false;
449 return true;
Evan Chenga6b4f432007-05-21 22:22:58 +0000450}
451
Evan Cheng82582102007-05-30 19:49:19 +0000452/// ReTryPreds - Invalidate predecessor BB info so it would be re-analyzed
Evan Chenga13aa952007-05-23 07:23:16 +0000453/// to determine if it can be if-converted.
Evan Cheng82582102007-05-30 19:49:19 +0000454void IfConverter::ReTryPreds(MachineBasicBlock *BB) {
Evan Chenga13aa952007-05-23 07:23:16 +0000455 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
456 E = BB->pred_end(); PI != E; ++PI) {
457 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
458 PBBI.Kind = ICReAnalyze;
459 }
460}
461
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000462/// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
463///
464static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
465 const TargetInstrInfo *TII) {
466 std::vector<MachineOperand> NoCond;
467 TII->InsertBranch(*BB, ToBB, NULL, NoCond);
468}
469
Evan Chengb6665f62007-06-04 06:47:22 +0000470/// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
Evan Chenga6b4f432007-05-21 22:22:58 +0000471///
Evan Chengb6665f62007-06-04 06:47:22 +0000472bool IfConverter::IfConvertSimple(BBInfo &BBI) {
473 bool ReverseCond = BBI.Kind == ICSimpleFalse;
Evan Chengb5a06902007-06-01 20:29:21 +0000474
Evan Chenga13aa952007-05-23 07:23:16 +0000475 BBI.Kind = ICNotClassfied;
476
Evan Chenga6b4f432007-05-21 22:22:58 +0000477 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
478 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
479 BBInfo *CvtBBI = &TrueBBI;
480 BBInfo *NextBBI = &FalseBBI;
Evan Chenga13aa952007-05-23 07:23:16 +0000481
Evan Chengb6665f62007-06-04 06:47:22 +0000482 std::vector<MachineOperand> Cond(BBI.BrCond);
Evan Chengb5a06902007-06-01 20:29:21 +0000483 if (ReverseCond) {
484 std::swap(CvtBBI, NextBBI);
Evan Chengb6665f62007-06-04 06:47:22 +0000485 TII->ReverseBranchCondition(Cond);
Evan Chengb5a06902007-06-01 20:29:21 +0000486 }
Evan Chenga13aa952007-05-23 07:23:16 +0000487
Evan Chengb6665f62007-06-04 06:47:22 +0000488 PredicateBlock(*CvtBBI, Cond);
489 // If the 'true' block ends without a branch, add a conditional branch
490 // to its successor unless that happens to be the 'false' block.
491 if (CvtBBI->IsAnalyzable && CvtBBI->TrueBB == NULL) {
492 assert(CvtBBI->BB->succ_size() == 1 && "Unexpected!");
493 MachineBasicBlock *SuccBB = *CvtBBI->BB->succ_begin();
494 if (SuccBB != NextBBI->BB)
495 TII->InsertBranch(*CvtBBI->BB, SuccBB, NULL, Cond);
496 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000497
Evan Chengb6665f62007-06-04 06:47:22 +0000498 // Merge converted block into entry block. Also add an unconditional branch
499 // to the 'false' branch.
Evan Chenga13aa952007-05-23 07:23:16 +0000500 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000501 MergeBlocks(BBI, *CvtBBI);
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000502 if (!isNextBlock(BBI.BB, NextBBI->BB))
503 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
Evan Chengb6665f62007-06-04 06:47:22 +0000504 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
Evan Chenga6b4f432007-05-21 22:22:58 +0000505
Evan Chenga13aa952007-05-23 07:23:16 +0000506 // Update block info. BB can be iteratively if-converted.
507 BBI.Kind = ICNotAnalyzed;
508 BBI.TrueBB = BBI.FalseBB = NULL;
509 BBI.BrCond.clear();
510 TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
Evan Cheng82582102007-05-30 19:49:19 +0000511 ReTryPreds(BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000512 CvtBBI->Kind = ICDead;
513
514 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000515 return true;
516}
517
Evan Chengd6ddc302007-05-16 21:54:37 +0000518/// IfConvertTriangle - If convert a triangle sub-CFG.
519///
Evan Cheng4e654852007-05-16 02:00:57 +0000520bool IfConverter::IfConvertTriangle(BBInfo &BBI) {
Evan Chenga13aa952007-05-23 07:23:16 +0000521 BBI.Kind = ICNotClassfied;
Evan Chengcf6cc112007-05-18 18:14:37 +0000522
Evan Chenga13aa952007-05-23 07:23:16 +0000523 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Cheng8c529382007-06-01 07:41:07 +0000524
Evan Chenga6b4f432007-05-21 22:22:58 +0000525 // Predicate the 'true' block after removing its branch.
Evan Chenga13aa952007-05-23 07:23:16 +0000526 TrueBBI.NonPredSize -= TII->RemoveBranch(*BBI.TrueBB);
527 PredicateBlock(TrueBBI, BBI.BrCond);
Evan Chenga6b4f432007-05-21 22:22:58 +0000528
Evan Chengb6665f62007-06-04 06:47:22 +0000529 // If 'true' block has a 'false' successor, add an exit branch to it.
Evan Cheng8c529382007-06-01 07:41:07 +0000530 if (TrueBBI.FalseBB) {
531 std::vector<MachineOperand> RevCond(TrueBBI.BrCond);
532 if (TII->ReverseBranchCondition(RevCond))
533 assert(false && "Unable to reverse branch condition!");
534 TII->InsertBranch(*BBI.TrueBB, TrueBBI.FalseBB, NULL, RevCond);
535 }
536
537 // Join the 'true' and 'false' blocks if the 'false' block has no other
538 // predecessors. Otherwise, add a unconditional branch from 'true' to 'false'.
Evan Chenga6b4f432007-05-21 22:22:58 +0000539 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Cheng8c529382007-06-01 07:41:07 +0000540 if (FalseBBI.BB->pred_size() == 2)
541 MergeBlocks(TrueBBI, FalseBBI);
Evan Chengb6665f62007-06-04 06:47:22 +0000542 else if (!isNextBlock(TrueBBI.BB, FalseBBI.BB))
Evan Cheng8c529382007-06-01 07:41:07 +0000543 InsertUncondBranch(TrueBBI.BB, FalseBBI.BB, TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000544
545 // Now merge the entry of the triangle with the true block.
Evan Chenga13aa952007-05-23 07:23:16 +0000546 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000547 MergeBlocks(BBI, TrueBBI);
Evan Chenga13aa952007-05-23 07:23:16 +0000548 std::copy(BBI.BrCond.begin(), BBI.BrCond.end(),
549 std::back_inserter(BBI.Predicate));
Evan Chenga6b4f432007-05-21 22:22:58 +0000550
Evan Chenga13aa952007-05-23 07:23:16 +0000551 // Update block info. BB can be iteratively if-converted.
552 BBI.Kind = ICNotClassfied;
553 BBI.TrueBB = BBI.FalseBB = NULL;
554 BBI.BrCond.clear();
555 TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
Evan Cheng82582102007-05-30 19:49:19 +0000556 ReTryPreds(BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000557 TrueBBI.Kind = ICDead;
558
559 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000560 return true;
Evan Cheng4e654852007-05-16 02:00:57 +0000561}
562
Evan Chengd6ddc302007-05-16 21:54:37 +0000563/// IfConvertDiamond - If convert a diamond sub-CFG.
564///
Evan Cheng4e654852007-05-16 02:00:57 +0000565bool IfConverter::IfConvertDiamond(BBInfo &BBI) {
Evan Chenga13aa952007-05-23 07:23:16 +0000566 BBI.Kind = ICNotClassfied;
567
Evan Chengb6665f62007-06-04 06:47:22 +0000568 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +0000569 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +0000570
Evan Chenga6b4f432007-05-21 22:22:58 +0000571 SmallVector<MachineInstr*, 2> Dups;
Evan Chengb6665f62007-06-04 06:47:22 +0000572 if (!BBI.TailBB) {
573 // No common merge block. Check if the terminators (e.g. return) are
574 // the same or predicable.
575 MachineBasicBlock::iterator TT = BBI.TrueBB->getFirstTerminator();
576 MachineBasicBlock::iterator FT = BBI.FalseBB->getFirstTerminator();
577 while (TT != BBI.TrueBB->end() && FT != BBI.FalseBB->end()) {
578 if (TT->isIdenticalTo(FT))
579 Dups.push_back(TT); // Will erase these later.
580 else if (!TT->isPredicable() && !FT->isPredicable())
581 return false; // Can't if-convert. Abort!
582 ++TT;
583 ++FT;
Evan Chengcf6cc112007-05-18 18:14:37 +0000584 }
Evan Chengcf6cc112007-05-18 18:14:37 +0000585
Evan Chengb6665f62007-06-04 06:47:22 +0000586 // One of the two pathes have more terminators, make sure they are
587 // all predicable.
588 while (TT != BBI.TrueBB->end()) {
589 if (!TT->isPredicable()) {
590 return false; // Can't if-convert. Abort!
Evan Cheng4e654852007-05-16 02:00:57 +0000591 }
Evan Chengb6665f62007-06-04 06:47:22 +0000592 ++TT;
593 }
594 while (FT != BBI.FalseBB->end()) {
595 if (!FT->isPredicable()) {
596 return false; // Can't if-convert. Abort!
597 }
598 ++FT;
Evan Cheng4e654852007-05-16 02:00:57 +0000599 }
Evan Cheng4e654852007-05-16 02:00:57 +0000600 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000601
Evan Chenga6b4f432007-05-21 22:22:58 +0000602 // Remove the duplicated instructions from the 'true' block.
603 for (unsigned i = 0, e = Dups.size(); i != e; ++i) {
604 Dups[i]->eraseFromParent();
Evan Chenga13aa952007-05-23 07:23:16 +0000605 --TrueBBI.NonPredSize;
Evan Chenga6b4f432007-05-21 22:22:58 +0000606 }
607
Evan Chengb6665f62007-06-04 06:47:22 +0000608 // Check the 'true' and 'false' blocks if either isn't ended with a branch.
609 // Either the block fallthrough to another block or it ends with a
610 // return. If it's the former, add a branch to its successor.
611 bool TrueNeedBr = !TrueBBI.TrueBB && BBI.TrueBB->succ_size();
612 bool FalseNeedBr = !FalseBBI.TrueBB && BBI.FalseBB->succ_size();
Evan Chenga6b4f432007-05-21 22:22:58 +0000613
Evan Chenga6b4f432007-05-21 22:22:58 +0000614 // Merge the 'true' and 'false' blocks by copying the instructions
615 // from the 'false' block to the 'true' block. That is, unless the true
616 // block would clobber the predicate, in that case, do the opposite.
Evan Chengb6665f62007-06-04 06:47:22 +0000617 std::vector<MachineOperand> RevCond(BBI.BrCond);
618 TII->ReverseBranchCondition(RevCond);
Evan Chenga6b4f432007-05-21 22:22:58 +0000619 BBInfo *CvtBBI;
Evan Chenga13aa952007-05-23 07:23:16 +0000620 if (!TrueBBI.ModifyPredicate) {
Evan Chengb6665f62007-06-04 06:47:22 +0000621 // Predicate the 'true' block after removing its branch.
622 TrueBBI.NonPredSize -= TII->RemoveBranch(*BBI.TrueBB);
623 PredicateBlock(TrueBBI, BBI.BrCond);
624
625 // Predicate the 'false' block.
626 PredicateBlock(FalseBBI, RevCond, true);
627
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000628 if (TrueNeedBr)
629 TII->InsertBranch(*BBI.TrueBB, *BBI.TrueBB->succ_begin(), NULL,
630 BBI.BrCond);
631 // Add an unconditional branch from 'false' to to 'false' successor if it
632 // will not be the fallthrough block.
633 if (FalseNeedBr &&
634 !isNextBlock(BBI.BB, *BBI.FalseBB->succ_begin()))
635 InsertUncondBranch(BBI.FalseBB, *BBI.FalseBB->succ_begin(), TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000636 MergeBlocks(TrueBBI, FalseBBI);
637 CvtBBI = &TrueBBI;
638 } else {
Evan Chengb6665f62007-06-04 06:47:22 +0000639 // Predicate the 'false' block after removing its branch.
640 FalseBBI.NonPredSize -= TII->RemoveBranch(*BBI.FalseBB);
641 PredicateBlock(FalseBBI, RevCond);
642
643 // Predicate the 'false' block.
644 PredicateBlock(TrueBBI, BBI.BrCond, true);
645
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000646 // Add a conditional branch from 'false' to 'false' successor if needed.
647 if (FalseNeedBr)
648 TII->InsertBranch(*BBI.FalseBB, *BBI.FalseBB->succ_begin(), NULL,
649 RevCond);
650 // Add an unconditional branch from 'true' to to 'true' successor if it
651 // will not be the fallthrough block.
652 if (TrueNeedBr &&
653 !isNextBlock(BBI.BB, *BBI.TrueBB->succ_begin()))
654 InsertUncondBranch(BBI.TrueBB, *BBI.TrueBB->succ_begin(), TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000655 MergeBlocks(FalseBBI, TrueBBI);
656 CvtBBI = &FalseBBI;
657 }
658
659 // Remove the conditional branch from entry to the blocks.
Evan Chenga13aa952007-05-23 07:23:16 +0000660 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000661
Evan Chenga13aa952007-05-23 07:23:16 +0000662 bool OkToIfcvt = true;
Evan Chenga6b4f432007-05-21 22:22:58 +0000663 // Merge the combined block into the entry of the diamond if the entry
664 // block is its only predecessor. Otherwise, insert an unconditional
665 // branch from entry to the if-converted block.
666 if (CvtBBI->BB->pred_size() == 1) {
Evan Chenga6b4f432007-05-21 22:22:58 +0000667 MergeBlocks(BBI, *CvtBBI);
668 CvtBBI = &BBI;
Evan Chenga13aa952007-05-23 07:23:16 +0000669 OkToIfcvt = false;
Evan Chengb6665f62007-06-04 06:47:22 +0000670 } else if (!isNextBlock(BBI.BB, CvtBBI->BB))
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000671 InsertUncondBranch(BBI.BB, CvtBBI->BB, TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000672
Evan Cheng58fbb9f2007-05-29 23:37:20 +0000673 // If the if-converted block fallthrough or unconditionally branch into the
674 // tail block, and the tail block does not have other predecessors, then
Evan Chenga6b4f432007-05-21 22:22:58 +0000675 // fold the tail block in as well.
Evan Cheng58fbb9f2007-05-29 23:37:20 +0000676 if (BBI.TailBB &&
Evan Chengf15d44c2007-05-31 20:53:33 +0000677 BBI.TailBB->pred_size() == 1 && CvtBBI->BB->succ_size() == 1) {
Evan Chenga13aa952007-05-23 07:23:16 +0000678 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000679 BBInfo TailBBI = BBAnalysis[BBI.TailBB->getNumber()];
680 MergeBlocks(*CvtBBI, TailBBI);
681 TailBBI.Kind = ICDead;
682 }
683
Evan Chenga13aa952007-05-23 07:23:16 +0000684 // Update block info. BB may be iteratively if-converted.
685 if (OkToIfcvt) {
686 BBI.Kind = ICNotClassfied;
687 BBI.TrueBB = BBI.FalseBB = NULL;
688 BBI.BrCond.clear();
689 TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
Evan Cheng82582102007-05-30 19:49:19 +0000690 ReTryPreds(BBI.BB);
Evan Chenga13aa952007-05-23 07:23:16 +0000691 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000692 TrueBBI.Kind = ICDead;
693 FalseBBI.Kind = ICDead;
694
695 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000696 return true;
Evan Cheng4e654852007-05-16 02:00:57 +0000697}
698
Evan Cheng4e654852007-05-16 02:00:57 +0000699/// PredicateBlock - Predicate every instruction in the block with the specified
700/// condition. If IgnoreTerm is true, skip over all terminator instructions.
Evan Chenga13aa952007-05-23 07:23:16 +0000701void IfConverter::PredicateBlock(BBInfo &BBI,
Evan Cheng4e654852007-05-16 02:00:57 +0000702 std::vector<MachineOperand> &Cond,
703 bool IgnoreTerm) {
Evan Chenga13aa952007-05-23 07:23:16 +0000704 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
Evan Cheng4e654852007-05-16 02:00:57 +0000705 I != E; ++I) {
Evan Chengb6665f62007-06-04 06:47:22 +0000706 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
Evan Cheng4e654852007-05-16 02:00:57 +0000707 continue;
Evan Chengb6665f62007-06-04 06:47:22 +0000708 if (TII->isPredicated(I))
Evan Chenga13aa952007-05-23 07:23:16 +0000709 continue;
Evan Chengb6665f62007-06-04 06:47:22 +0000710 if (!TII->PredicateInstruction(I, Cond)) {
Evan Chengfe57a7e2007-06-01 00:55:26 +0000711 cerr << "Unable to predicate " << *I << "!\n";
Evan Chengd6ddc302007-05-16 21:54:37 +0000712 abort();
713 }
Evan Cheng4e654852007-05-16 02:00:57 +0000714 }
Evan Chenga13aa952007-05-23 07:23:16 +0000715
716 BBI.NonPredSize = 0;
Evan Chengb6665f62007-06-04 06:47:22 +0000717 NumIfConvBBs++;
718}
719
720/// TransferPreds - Transfer all the predecessors of FromBB to ToBB.
721///
722static void TransferPreds(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
723 for (MachineBasicBlock::pred_iterator I = FromBB->pred_begin(),
724 E = FromBB->pred_end(); I != E; ++I) {
725 MachineBasicBlock *Pred = *I;
726 Pred->removeSuccessor(FromBB);
727 if (!Pred->isSuccessor(ToBB))
728 Pred->addSuccessor(ToBB);
729 }
730}
731
732/// TransferSuccs - Transfer all the successors of FromBB to ToBB.
733///
734static void TransferSuccs(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
735 for (MachineBasicBlock::succ_iterator I = FromBB->succ_begin(),
736 E = FromBB->succ_end(); I != E; ++I) {
737 FromBB->removeSuccessor(*I);
738 if (!ToBB->isSuccessor(*I))
739 ToBB->addSuccessor(*I);
740 }
Evan Cheng4e654852007-05-16 02:00:57 +0000741}
742
Evan Cheng86cbfea2007-05-18 00:20:58 +0000743/// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
Evan Cheng4e654852007-05-16 02:00:57 +0000744///
Evan Cheng86cbfea2007-05-18 00:20:58 +0000745void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) {
746 ToBBI.BB->splice(ToBBI.BB->end(),
747 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
Evan Chenga13aa952007-05-23 07:23:16 +0000748
749 // If FromBBI is previously a successor, remove it from ToBBI's successor
750 // list and update its TrueBB / FalseBB field if needed.
751 if (ToBBI.BB->isSuccessor(FromBBI.BB))
752 ToBBI.BB->removeSuccessor(FromBBI.BB);
753
Evan Chengb6665f62007-06-04 06:47:22 +0000754 // Redirect all branches to FromBB to ToBB.
755 for (MachineBasicBlock::pred_iterator I = FromBBI.BB->pred_begin(),
756 E = FromBBI.BB->pred_end(); I != E; ++I)
757 (*I)->ReplaceUsesOfBlockWith(FromBBI.BB, ToBBI.BB);
758
Evan Chenga13aa952007-05-23 07:23:16 +0000759 // Transfer preds / succs and update size.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000760 TransferPreds(ToBBI.BB, FromBBI.BB);
761 TransferSuccs(ToBBI.BB, FromBBI.BB);
Evan Chenga13aa952007-05-23 07:23:16 +0000762 ToBBI.NonPredSize += FromBBI.NonPredSize;
763 FromBBI.NonPredSize = 0;
Evan Cheng4e654852007-05-16 02:00:57 +0000764}