blob: 1023f8fd95064fb7b5bcaf8ce3eac91a7c42edd1 [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:
Evan Chengf5305f92007-06-05 00:07:37 +0000156 // One or more of 'children' have been modified, abort!
157 case ICDead:
158 // Block has been already been if-converted, abort!
Evan Cheng5f702182007-06-01 00:12:12 +0000159 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000160 case ICSimple:
161 case ICSimpleFalse:
162 DOUT << "Ifcvt (Simple" << (BBI.Kind == ICSimpleFalse ? " false" : "")
163 << "): BB#" << BBI.BB->getNumber() << " ";
164 RetVal = IfConvertSimple(BBI);
165 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
166 if (RetVal)
167 if (BBI.Kind == ICSimple) NumSimple++;
168 else NumSimpleRev++;
169 break;
Evan Chenga13aa952007-05-23 07:23:16 +0000170 case ICTriangle:
Evan Chengb6665f62007-06-04 06:47:22 +0000171 DOUT << "Ifcvt (Triangle): BB#" << BBI.BB->getNumber() << " ";
172 RetVal = IfConvertTriangle(BBI);
173 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
174 if (RetVal) NumTriangle++;
Evan Chenga13aa952007-05-23 07:23:16 +0000175 break;
176 case ICDiamond:
Evan Chengb6665f62007-06-04 06:47:22 +0000177 DOUT << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " ";
178 RetVal = IfConvertDiamond(BBI);
179 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
180 if (RetVal) NumDiamonds++;
Evan Chenga13aa952007-05-23 07:23:16 +0000181 break;
182 }
Evan Chengb6665f62007-06-04 06:47:22 +0000183 Change |= RetVal;
Evan Cheng4e654852007-05-16 02:00:57 +0000184 }
Evan Chenga13aa952007-05-23 07:23:16 +0000185
Evan Chenga13aa952007-05-23 07:23:16 +0000186 if (!Change)
187 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000188 MadeChange |= Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000189 }
Evan Cheng47d25022007-05-18 01:55:58 +0000190
Evan Cheng82582102007-05-30 19:49:19 +0000191 Roots.clear();
Evan Cheng47d25022007-05-18 01:55:58 +0000192 BBAnalysis.clear();
193
Evan Cheng4e654852007-05-16 02:00:57 +0000194 return MadeChange;
195}
196
197static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng86cbfea2007-05-18 00:20:58 +0000198 MachineBasicBlock *TrueBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000199 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
200 E = BB->succ_end(); SI != E; ++SI) {
201 MachineBasicBlock *SuccBB = *SI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000202 if (SuccBB != TrueBB)
Evan Cheng4e654852007-05-16 02:00:57 +0000203 return SuccBB;
204 }
205 return NULL;
206}
207
Evan Chengb6665f62007-06-04 06:47:22 +0000208bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
209 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
210 TII->RemoveBranch(*BBI.BB);
211 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond);
212 std::swap(BBI.TrueBB, BBI.FalseBB);
213 return true;
214 }
215 return false;
216}
217
Evan Chengcf6cc112007-05-18 18:14:37 +0000218/// StructuralAnalysis - Analyze the structure of the sub-CFG starting from
219/// the specified block. Record its successors and whether it looks like an
220/// if-conversion candidate.
221void IfConverter::StructuralAnalysis(MachineBasicBlock *BB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000222 BBInfo &BBI = BBAnalysis[BB->getNumber()];
223
Evan Chengf5305f92007-06-05 00:07:37 +0000224 if (BBI.Kind == ICReAnalyze) {
Evan Chengb6665f62007-06-04 06:47:22 +0000225 BBI.BrCond.clear();
Evan Chengf5305f92007-06-05 00:07:37 +0000226 BBI.TrueBB = BBI.FalseBB = NULL;
227 } else {
Evan Chenga13aa952007-05-23 07:23:16 +0000228 if (BBI.Kind != ICNotAnalyzed)
229 return; // Already analyzed.
230 BBI.BB = BB;
231 BBI.NonPredSize = std::distance(BB->begin(), BB->end());
Evan Chenga13aa952007-05-23 07:23:16 +0000232 }
Evan Cheng86cbfea2007-05-18 00:20:58 +0000233
Evan Cheng4bec8ae2007-05-25 00:59:01 +0000234 // Look for 'root' of a simple (non-nested) triangle or diamond.
235 BBI.Kind = ICNotClassfied;
Evan Chengb6665f62007-06-04 06:47:22 +0000236 BBI.IsAnalyzable =
237 !TII->AnalyzeBranch(*BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
238 if (!BBI.IsAnalyzable || BBI.BrCond.size() == 0)
Evan Cheng4bec8ae2007-05-25 00:59:01 +0000239 return;
240
Evan Chengcf6cc112007-05-18 18:14:37 +0000241 StructuralAnalysis(BBI.TrueBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000242 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengd6ddc302007-05-16 21:54:37 +0000243
244 // No false branch. This BB must end with a conditional branch and a
245 // fallthrough.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000246 if (!BBI.FalseBB)
247 BBI.FalseBB = findFalseBlock(BB, BBI.TrueBB);
248 assert(BBI.FalseBB && "Expected to find the fallthrough block!");
Evan Chengc5d05ef2007-05-16 05:11:10 +0000249
Evan Chengcf6cc112007-05-18 18:14:37 +0000250 StructuralAnalysis(BBI.FalseBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000251 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chengb6665f62007-06-04 06:47:22 +0000252
253 // Look for more opportunities to if-convert a triangle. Try to restructure
254 // the CFG to form a triangle with the 'false' path.
255 std::vector<MachineOperand> RevCond(BBI.BrCond);
256 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
257 if (FalseBBI.FalseBB) {
258 if (TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB)
259 return;
260 std::vector<MachineOperand> Cond(BBI.BrCond);
261 if (CanRevCond &&
262 FalseBBI.TrueBB && FalseBBI.BB->pred_size() == 1 &&
263 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
264 std::vector<MachineOperand> FalseCond(FalseBBI.BrCond);
265 if (FalseBBI.TrueBB == BBI.TrueBB &&
266 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
267 // Reverse 'true' and 'false' paths.
268 ReverseBranchCondition(BBI);
269 BBI.Kind = ICTriangle;
270 FalseBBI.Kind = ICChild;
271 } else if (FalseBBI.FalseBB == BBI.TrueBB &&
272 !TII->ReverseBranchCondition(FalseCond) &&
273 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
274 // Reverse 'false' block's 'true' and 'false' paths and then
275 // reverse 'true' and 'false' paths.
276 ReverseBranchCondition(FalseBBI);
277 ReverseBranchCondition(BBI);
278 BBI.Kind = ICTriangle;
279 FalseBBI.Kind = ICChild;
280 }
281 }
282 } else if (TrueBBI.TrueBB == FalseBBI.TrueBB && CanRevCond &&
283 TrueBBI.BB->pred_size() == 1 &&
284 TrueBBI.BB->pred_size() == 1 &&
285 // Check the 'true' and 'false' blocks if either isn't ended with
286 // a branch. If the block does not fallthrough to another block
287 // then we need to add a branch to its successor.
288 !(TrueBBI.ModifyPredicate &&
289 !TrueBBI.TrueBB && TrueBBI.BB->succ_size()) &&
290 !(FalseBBI.ModifyPredicate &&
291 !FalseBBI.TrueBB && FalseBBI.BB->succ_size()) &&
292 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
293 FeasibilityAnalysis(FalseBBI, RevCond)) {
Evan Cheng4e654852007-05-16 02:00:57 +0000294 // Diamond:
295 // EBB
296 // / \_
297 // | |
298 // TBB FBB
299 // \ /
Evan Cheng86cbfea2007-05-18 00:20:58 +0000300 // TailBB
Evan Cheng4e654852007-05-16 02:00:57 +0000301 // Note MBB can be empty in case both TBB and FBB are return blocks.
Evan Chenga6b4f432007-05-21 22:22:58 +0000302 BBI.Kind = ICDiamond;
303 TrueBBI.Kind = FalseBBI.Kind = ICChild;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000304 BBI.TailBB = TrueBBI.TrueBB;
Evan Chengb6665f62007-06-04 06:47:22 +0000305 } else {
306 // FIXME: Consider duplicating if BB is small.
307 bool TryTriangle = TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB &&
308 BBI.TrueBB->pred_size() == 1;
309 bool TrySimple = TrueBBI.BrCond.size() == 0 && BBI.TrueBB->pred_size() == 1;
310 if ((TryTriangle || TrySimple) &&
311 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
312 if (TryTriangle) {
313 // Triangle:
314 // EBB
315 // | \_
316 // | |
317 // | TBB
318 // | /
319 // FBB
320 BBI.Kind = ICTriangle;
321 TrueBBI.Kind = ICChild;
322 } else {
323 // Simple (split, no rejoin):
324 // EBB
325 // | \_
326 // | |
327 // | TBB---> exit
328 // |
329 // FBB
330 BBI.Kind = ICSimple;
331 TrueBBI.Kind = ICChild;
332 }
333 } else if (FalseBBI.BrCond.size() == 0 && BBI.FalseBB->pred_size() == 1) {
334 // Try 'simple' on the other path...
335 std::vector<MachineOperand> RevCond(BBI.BrCond);
336 if (TII->ReverseBranchCondition(RevCond))
337 assert(false && "Unable to reverse branch condition!");
338 if (FeasibilityAnalysis(FalseBBI, RevCond)) {
339 BBI.Kind = ICSimpleFalse;
340 FalseBBI.Kind = ICChild;
341 }
342 }
Evan Cheng4e654852007-05-16 02:00:57 +0000343 }
344 return;
345}
346
Evan Chengcf6cc112007-05-18 18:14:37 +0000347/// FeasibilityAnalysis - Determine if the block is predicable. In most
348/// cases, that means all the instructions in the block has M_PREDICABLE flag.
349/// Also checks if the block contains any instruction which can clobber a
350/// predicate (e.g. condition code register). If so, the block is not
Evan Chengb6665f62007-06-04 06:47:22 +0000351/// predicable unless it's the last instruction. If IgnoreTerm is true then
352/// all the terminator instructions are skipped.
353bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
354 std::vector<MachineOperand> &Cond,
355 bool IgnoreTerm) {
356 // If the block is dead, or it is going to be the entry block of a sub-CFG
357 // that will be if-converted, then it cannot be predicated.
358 if (BBI.Kind != ICNotAnalyzed &&
359 BBI.Kind != ICNotClassfied &&
360 BBI.Kind != ICChild)
361 return false;
362
363 // Check predication threshold.
Evan Chenga13aa952007-05-23 07:23:16 +0000364 if (BBI.NonPredSize == 0 || BBI.NonPredSize > TLI->getIfCvtBlockSizeLimit())
Evan Chengb6665f62007-06-04 06:47:22 +0000365 return false;
366
367 // If it is already predicated, check if its predicate subsumes the new
368 // predicate.
369 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Cond))
370 return false;
Evan Chengcf6cc112007-05-18 18:14:37 +0000371
372 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
373 I != E; ++I) {
Evan Chengb6665f62007-06-04 06:47:22 +0000374 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
375 continue;
Evan Chengcf6cc112007-05-18 18:14:37 +0000376 // TODO: check if instruction clobbers predicate.
Evan Chengcf6cc112007-05-18 18:14:37 +0000377 if (!I->isPredicable())
Evan Chengb6665f62007-06-04 06:47:22 +0000378 return false;
Evan Chengcf6cc112007-05-18 18:14:37 +0000379 }
380
Evan Chengb6665f62007-06-04 06:47:22 +0000381 return true;
Evan Chengcf6cc112007-05-18 18:14:37 +0000382}
383
Evan Cheng82582102007-05-30 19:49:19 +0000384/// AttemptRestructuring - Restructure the sub-CFG rooted in the given block to
385/// expose more if-conversion opportunities. e.g.
386///
387/// cmp
388/// b le BB1
389/// / \____
390/// / |
391/// cmp |
392/// b eq BB1 |
393/// / \____ |
394/// / \ |
395/// BB1
396/// ==>
397///
398/// cmp
399/// b eq BB1
400/// / \____
401/// / |
402/// cmp |
403/// b le BB1 |
404/// / \____ |
405/// / \ |
406/// BB1
407bool IfConverter::AttemptRestructuring(BBInfo &BBI) {
408 return false;
409}
410
411/// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
412/// candidates. It returns true if any CFG restructuring is done to expose more
413/// if-conversion opportunities.
414bool IfConverter::AnalyzeBlocks(MachineFunction &MF,
Evan Chenga13aa952007-05-23 07:23:16 +0000415 std::vector<BBInfo*> &Candidates) {
Evan Cheng82582102007-05-30 19:49:19 +0000416 bool Change = false;
Evan Cheng36489bb2007-05-18 19:26:33 +0000417 std::set<MachineBasicBlock*> Visited;
Evan Cheng82582102007-05-30 19:49:19 +0000418 for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
419 for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited),
420 E = idf_ext_end(Roots[i], Visited); I != E; ++I) {
421 MachineBasicBlock *BB = *I;
422 StructuralAnalysis(BB);
423 BBInfo &BBI = BBAnalysis[BB->getNumber()];
424 switch (BBI.Kind) {
Evan Chengb6665f62007-06-04 06:47:22 +0000425 case ICSimple:
426 case ICSimpleFalse:
Evan Cheng82582102007-05-30 19:49:19 +0000427 case ICTriangle:
428 case ICDiamond:
429 Candidates.push_back(&BBI);
430 break;
431 default:
432 Change |= AttemptRestructuring(BBI);
433 break;
434 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000435 }
Evan Cheng4e654852007-05-16 02:00:57 +0000436 }
Evan Cheng82582102007-05-30 19:49:19 +0000437
Evan Cheng5f702182007-06-01 00:12:12 +0000438 // Sort to favor more complex ifcvt scheme.
439 std::stable_sort(Candidates.begin(), Candidates.end(), IfcvtCandidateCmp);
440
Evan Cheng82582102007-05-30 19:49:19 +0000441 return Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000442}
443
Evan Chengb6665f62007-06-04 06:47:22 +0000444/// isNextBlock - Returns true either if ToBB the next block after BB or
445/// that all the intervening blocks are empty.
Evan Chenga6b4f432007-05-21 22:22:58 +0000446static bool isNextBlock(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
Evan Cheng3ec42542007-06-04 20:33:36 +0000447 MachineFunction *MF = BB->getParent();
Evan Chengb6665f62007-06-04 06:47:22 +0000448 MachineFunction::iterator I = BB;
Evan Cheng3ec42542007-06-04 20:33:36 +0000449 while (++I != MF->end() && I != MachineFunction::iterator(ToBB))
Evan Chengb6665f62007-06-04 06:47:22 +0000450 if (!I->empty())
451 return false;
452 return true;
Evan Chenga6b4f432007-05-21 22:22:58 +0000453}
454
Evan Cheng82582102007-05-30 19:49:19 +0000455/// ReTryPreds - Invalidate predecessor BB info so it would be re-analyzed
Evan Chenga13aa952007-05-23 07:23:16 +0000456/// to determine if it can be if-converted.
Evan Cheng82582102007-05-30 19:49:19 +0000457void IfConverter::ReTryPreds(MachineBasicBlock *BB) {
Evan Chenga13aa952007-05-23 07:23:16 +0000458 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
459 E = BB->pred_end(); PI != E; ++PI) {
460 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
461 PBBI.Kind = ICReAnalyze;
462 }
463}
464
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000465/// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
466///
467static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
468 const TargetInstrInfo *TII) {
469 std::vector<MachineOperand> NoCond;
470 TII->InsertBranch(*BB, ToBB, NULL, NoCond);
471}
472
Evan Chengb6665f62007-06-04 06:47:22 +0000473/// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
Evan Chenga6b4f432007-05-21 22:22:58 +0000474///
Evan Chengb6665f62007-06-04 06:47:22 +0000475bool IfConverter::IfConvertSimple(BBInfo &BBI) {
476 bool ReverseCond = BBI.Kind == ICSimpleFalse;
Evan Chengb5a06902007-06-01 20:29:21 +0000477
Evan Chenga13aa952007-05-23 07:23:16 +0000478 BBI.Kind = ICNotClassfied;
479
Evan Chenga6b4f432007-05-21 22:22:58 +0000480 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
481 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
482 BBInfo *CvtBBI = &TrueBBI;
483 BBInfo *NextBBI = &FalseBBI;
Evan Chenga13aa952007-05-23 07:23:16 +0000484
Evan Chengb6665f62007-06-04 06:47:22 +0000485 std::vector<MachineOperand> Cond(BBI.BrCond);
Evan Chengb5a06902007-06-01 20:29:21 +0000486 if (ReverseCond) {
487 std::swap(CvtBBI, NextBBI);
Evan Chengb6665f62007-06-04 06:47:22 +0000488 TII->ReverseBranchCondition(Cond);
Evan Chengb5a06902007-06-01 20:29:21 +0000489 }
Evan Chenga13aa952007-05-23 07:23:16 +0000490
Evan Chengb6665f62007-06-04 06:47:22 +0000491 PredicateBlock(*CvtBBI, Cond);
492 // If the 'true' block ends without a branch, add a conditional branch
493 // to its successor unless that happens to be the 'false' block.
494 if (CvtBBI->IsAnalyzable && CvtBBI->TrueBB == NULL) {
495 assert(CvtBBI->BB->succ_size() == 1 && "Unexpected!");
496 MachineBasicBlock *SuccBB = *CvtBBI->BB->succ_begin();
497 if (SuccBB != NextBBI->BB)
498 TII->InsertBranch(*CvtBBI->BB, SuccBB, NULL, Cond);
499 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000500
Evan Chengb6665f62007-06-04 06:47:22 +0000501 // Merge converted block into entry block. Also add an unconditional branch
502 // to the 'false' branch.
Evan Chenga13aa952007-05-23 07:23:16 +0000503 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000504 MergeBlocks(BBI, *CvtBBI);
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000505 if (!isNextBlock(BBI.BB, NextBBI->BB))
506 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
Evan Chengb6665f62007-06-04 06:47:22 +0000507 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
Evan Chenga6b4f432007-05-21 22:22:58 +0000508
Evan Chenga13aa952007-05-23 07:23:16 +0000509 // Update block info. BB can be iteratively if-converted.
Evan Chengf5305f92007-06-05 00:07:37 +0000510 BBI.Kind = ICReAnalyze;
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 Cheng8ed680c2007-06-05 01:31:40 +0000530 bool HasEarlyExit = TrueBBI.FalseBB != NULL;
531 if (HasEarlyExit) {
Evan Cheng8c529382007-06-01 07:41:07 +0000532 std::vector<MachineOperand> RevCond(TrueBBI.BrCond);
533 if (TII->ReverseBranchCondition(RevCond))
534 assert(false && "Unable to reverse branch condition!");
535 TII->InsertBranch(*BBI.TrueBB, TrueBBI.FalseBB, NULL, RevCond);
536 }
537
538 // Join the 'true' and 'false' blocks if the 'false' block has no other
539 // predecessors. Otherwise, add a unconditional branch from 'true' to 'false'.
Evan Chenga6b4f432007-05-21 22:22:58 +0000540 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chengf5305f92007-06-05 00:07:37 +0000541 bool FalseBBDead = false;
Evan Cheng8ed680c2007-06-05 01:31:40 +0000542 if (!HasEarlyExit && FalseBBI.BB->pred_size() == 2) {
Evan Cheng8c529382007-06-01 07:41:07 +0000543 MergeBlocks(TrueBBI, FalseBBI);
Evan Chengf5305f92007-06-05 00:07:37 +0000544 FalseBBDead = true;
545 } else if (!isNextBlock(TrueBBI.BB, FalseBBI.BB))
Evan Cheng8c529382007-06-01 07:41:07 +0000546 InsertUncondBranch(TrueBBI.BB, FalseBBI.BB, TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000547
548 // Now merge the entry of the triangle with the true block.
Evan Chenga13aa952007-05-23 07:23:16 +0000549 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000550 MergeBlocks(BBI, TrueBBI);
Evan Chenga13aa952007-05-23 07:23:16 +0000551 std::copy(BBI.BrCond.begin(), BBI.BrCond.end(),
552 std::back_inserter(BBI.Predicate));
Evan Chenga6b4f432007-05-21 22:22:58 +0000553
Evan Chenga13aa952007-05-23 07:23:16 +0000554 // Update block info. BB can be iteratively if-converted.
Evan Chengf5305f92007-06-05 00:07:37 +0000555 BBI.Kind = ICReAnalyze;
Evan Cheng82582102007-05-30 19:49:19 +0000556 ReTryPreds(BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000557 TrueBBI.Kind = ICDead;
Evan Chengf5305f92007-06-05 00:07:37 +0000558 if (FalseBBDead)
559 FalseBBI.Kind = ICDead;
Evan Chenga6b4f432007-05-21 22:22:58 +0000560
561 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000562 return true;
Evan Cheng4e654852007-05-16 02:00:57 +0000563}
564
Evan Chengd6ddc302007-05-16 21:54:37 +0000565/// IfConvertDiamond - If convert a diamond sub-CFG.
566///
Evan Cheng4e654852007-05-16 02:00:57 +0000567bool IfConverter::IfConvertDiamond(BBInfo &BBI) {
Evan Chenga13aa952007-05-23 07:23:16 +0000568 BBI.Kind = ICNotClassfied;
569
Evan Chengb6665f62007-06-04 06:47:22 +0000570 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +0000571 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +0000572
Evan Chenga6b4f432007-05-21 22:22:58 +0000573 SmallVector<MachineInstr*, 2> Dups;
Evan Chengb6665f62007-06-04 06:47:22 +0000574 if (!BBI.TailBB) {
575 // No common merge block. Check if the terminators (e.g. return) are
576 // the same or predicable.
577 MachineBasicBlock::iterator TT = BBI.TrueBB->getFirstTerminator();
578 MachineBasicBlock::iterator FT = BBI.FalseBB->getFirstTerminator();
579 while (TT != BBI.TrueBB->end() && FT != BBI.FalseBB->end()) {
580 if (TT->isIdenticalTo(FT))
581 Dups.push_back(TT); // Will erase these later.
582 else if (!TT->isPredicable() && !FT->isPredicable())
583 return false; // Can't if-convert. Abort!
584 ++TT;
585 ++FT;
Evan Chengcf6cc112007-05-18 18:14:37 +0000586 }
Evan Chengcf6cc112007-05-18 18:14:37 +0000587
Evan Chengb6665f62007-06-04 06:47:22 +0000588 // One of the two pathes have more terminators, make sure they are
589 // all predicable.
590 while (TT != BBI.TrueBB->end()) {
591 if (!TT->isPredicable()) {
592 return false; // Can't if-convert. Abort!
Evan Cheng4e654852007-05-16 02:00:57 +0000593 }
Evan Chengb6665f62007-06-04 06:47:22 +0000594 ++TT;
595 }
596 while (FT != BBI.FalseBB->end()) {
597 if (!FT->isPredicable()) {
598 return false; // Can't if-convert. Abort!
599 }
600 ++FT;
Evan Cheng4e654852007-05-16 02:00:57 +0000601 }
Evan Cheng4e654852007-05-16 02:00:57 +0000602 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000603
Evan Chenga6b4f432007-05-21 22:22:58 +0000604 // Remove the duplicated instructions from the 'true' block.
605 for (unsigned i = 0, e = Dups.size(); i != e; ++i) {
606 Dups[i]->eraseFromParent();
Evan Chenga13aa952007-05-23 07:23:16 +0000607 --TrueBBI.NonPredSize;
Evan Chenga6b4f432007-05-21 22:22:58 +0000608 }
609
Evan Chengb6665f62007-06-04 06:47:22 +0000610 // Check the 'true' and 'false' blocks if either isn't ended with a branch.
611 // Either the block fallthrough to another block or it ends with a
612 // return. If it's the former, add a branch to its successor.
613 bool TrueNeedBr = !TrueBBI.TrueBB && BBI.TrueBB->succ_size();
614 bool FalseNeedBr = !FalseBBI.TrueBB && BBI.FalseBB->succ_size();
Evan Chenga6b4f432007-05-21 22:22:58 +0000615
Evan Chenga6b4f432007-05-21 22:22:58 +0000616 // Merge the 'true' and 'false' blocks by copying the instructions
617 // from the 'false' block to the 'true' block. That is, unless the true
618 // block would clobber the predicate, in that case, do the opposite.
Evan Chengb6665f62007-06-04 06:47:22 +0000619 std::vector<MachineOperand> RevCond(BBI.BrCond);
620 TII->ReverseBranchCondition(RevCond);
Evan Chenga6b4f432007-05-21 22:22:58 +0000621 BBInfo *CvtBBI;
Evan Chenga13aa952007-05-23 07:23:16 +0000622 if (!TrueBBI.ModifyPredicate) {
Evan Chengb6665f62007-06-04 06:47:22 +0000623 // Predicate the 'true' block after removing its branch.
624 TrueBBI.NonPredSize -= TII->RemoveBranch(*BBI.TrueBB);
625 PredicateBlock(TrueBBI, BBI.BrCond);
626
627 // Predicate the 'false' block.
628 PredicateBlock(FalseBBI, RevCond, true);
629
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000630 if (TrueNeedBr)
631 TII->InsertBranch(*BBI.TrueBB, *BBI.TrueBB->succ_begin(), NULL,
632 BBI.BrCond);
633 // Add an unconditional branch from 'false' to to 'false' successor if it
634 // will not be the fallthrough block.
635 if (FalseNeedBr &&
636 !isNextBlock(BBI.BB, *BBI.FalseBB->succ_begin()))
637 InsertUncondBranch(BBI.FalseBB, *BBI.FalseBB->succ_begin(), TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000638 MergeBlocks(TrueBBI, FalseBBI);
639 CvtBBI = &TrueBBI;
640 } else {
Evan Chengb6665f62007-06-04 06:47:22 +0000641 // Predicate the 'false' block after removing its branch.
642 FalseBBI.NonPredSize -= TII->RemoveBranch(*BBI.FalseBB);
643 PredicateBlock(FalseBBI, RevCond);
644
645 // Predicate the 'false' block.
646 PredicateBlock(TrueBBI, BBI.BrCond, true);
647
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000648 // Add a conditional branch from 'false' to 'false' successor if needed.
649 if (FalseNeedBr)
650 TII->InsertBranch(*BBI.FalseBB, *BBI.FalseBB->succ_begin(), NULL,
651 RevCond);
652 // Add an unconditional branch from 'true' to to 'true' successor if it
653 // will not be the fallthrough block.
654 if (TrueNeedBr &&
655 !isNextBlock(BBI.BB, *BBI.TrueBB->succ_begin()))
656 InsertUncondBranch(BBI.TrueBB, *BBI.TrueBB->succ_begin(), TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000657 MergeBlocks(FalseBBI, TrueBBI);
658 CvtBBI = &FalseBBI;
659 }
660
661 // Remove the conditional branch from entry to the blocks.
Evan Chenga13aa952007-05-23 07:23:16 +0000662 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000663
Evan Chenga13aa952007-05-23 07:23:16 +0000664 bool OkToIfcvt = true;
Evan Chenga6b4f432007-05-21 22:22:58 +0000665 // Merge the combined block into the entry of the diamond if the entry
666 // block is its only predecessor. Otherwise, insert an unconditional
667 // branch from entry to the if-converted block.
668 if (CvtBBI->BB->pred_size() == 1) {
Evan Chenga6b4f432007-05-21 22:22:58 +0000669 MergeBlocks(BBI, *CvtBBI);
670 CvtBBI = &BBI;
Evan Chenga13aa952007-05-23 07:23:16 +0000671 OkToIfcvt = false;
Evan Chengb6665f62007-06-04 06:47:22 +0000672 } else if (!isNextBlock(BBI.BB, CvtBBI->BB))
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000673 InsertUncondBranch(BBI.BB, CvtBBI->BB, TII);
Evan Chenga6b4f432007-05-21 22:22:58 +0000674
Evan Cheng58fbb9f2007-05-29 23:37:20 +0000675 // If the if-converted block fallthrough or unconditionally branch into the
676 // tail block, and the tail block does not have other predecessors, then
Evan Chenga6b4f432007-05-21 22:22:58 +0000677 // fold the tail block in as well.
Evan Cheng58fbb9f2007-05-29 23:37:20 +0000678 if (BBI.TailBB &&
Evan Chengf15d44c2007-05-31 20:53:33 +0000679 BBI.TailBB->pred_size() == 1 && CvtBBI->BB->succ_size() == 1) {
Evan Chenga13aa952007-05-23 07:23:16 +0000680 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000681 BBInfo TailBBI = BBAnalysis[BBI.TailBB->getNumber()];
682 MergeBlocks(*CvtBBI, TailBBI);
683 TailBBI.Kind = ICDead;
684 }
685
Evan Chenga13aa952007-05-23 07:23:16 +0000686 // Update block info. BB may be iteratively if-converted.
687 if (OkToIfcvt) {
Evan Chengf5305f92007-06-05 00:07:37 +0000688 BBI.Kind = ICReAnalyze;
Evan Cheng82582102007-05-30 19:49:19 +0000689 ReTryPreds(BBI.BB);
Evan Chenga13aa952007-05-23 07:23:16 +0000690 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000691 TrueBBI.Kind = ICDead;
692 FalseBBI.Kind = ICDead;
693
694 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000695 return true;
Evan Cheng4e654852007-05-16 02:00:57 +0000696}
697
Evan Cheng4e654852007-05-16 02:00:57 +0000698/// PredicateBlock - Predicate every instruction in the block with the specified
699/// condition. If IgnoreTerm is true, skip over all terminator instructions.
Evan Chenga13aa952007-05-23 07:23:16 +0000700void IfConverter::PredicateBlock(BBInfo &BBI,
Evan Cheng4e654852007-05-16 02:00:57 +0000701 std::vector<MachineOperand> &Cond,
702 bool IgnoreTerm) {
Evan Chenga13aa952007-05-23 07:23:16 +0000703 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
Evan Cheng4e654852007-05-16 02:00:57 +0000704 I != E; ++I) {
Evan Chengb6665f62007-06-04 06:47:22 +0000705 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
Evan Cheng4e654852007-05-16 02:00:57 +0000706 continue;
Evan Chengb6665f62007-06-04 06:47:22 +0000707 if (TII->isPredicated(I))
Evan Chenga13aa952007-05-23 07:23:16 +0000708 continue;
Evan Chengb6665f62007-06-04 06:47:22 +0000709 if (!TII->PredicateInstruction(I, Cond)) {
Evan Chengfe57a7e2007-06-01 00:55:26 +0000710 cerr << "Unable to predicate " << *I << "!\n";
Evan Chengd6ddc302007-05-16 21:54:37 +0000711 abort();
712 }
Evan Cheng4e654852007-05-16 02:00:57 +0000713 }
Evan Chenga13aa952007-05-23 07:23:16 +0000714
715 BBI.NonPredSize = 0;
Evan Chengb6665f62007-06-04 06:47:22 +0000716 NumIfConvBBs++;
717}
718
719/// TransferPreds - Transfer all the predecessors of FromBB to ToBB.
720///
721static void TransferPreds(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
722 for (MachineBasicBlock::pred_iterator I = FromBB->pred_begin(),
723 E = FromBB->pred_end(); I != E; ++I) {
724 MachineBasicBlock *Pred = *I;
725 Pred->removeSuccessor(FromBB);
726 if (!Pred->isSuccessor(ToBB))
727 Pred->addSuccessor(ToBB);
728 }
729}
730
731/// TransferSuccs - Transfer all the successors of FromBB to ToBB.
732///
733static void TransferSuccs(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
734 for (MachineBasicBlock::succ_iterator I = FromBB->succ_begin(),
735 E = FromBB->succ_end(); I != E; ++I) {
Evan Chengf5305f92007-06-05 00:07:37 +0000736 MachineBasicBlock *Succ = *I;
737 FromBB->removeSuccessor(Succ);
738 if (!ToBB->isSuccessor(Succ))
739 ToBB->addSuccessor(Succ);
Evan Chengb6665f62007-06-04 06:47:22 +0000740 }
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}