blob: 6243b876ac86868f0346036ee49df6762a8c176a [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
14#define DEBUG_TYPE "ifconversion"
15#include "llvm/CodeGen/Passes.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/MachineFunctionPass.h"
18#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng86cbfea2007-05-18 00:20:58 +000019#include "llvm/Target/TargetLowering.h"
Evan Cheng4e654852007-05-16 02:00:57 +000020#include "llvm/Target/TargetMachine.h"
21#include "llvm/Support/Debug.h"
Evan Cheng36489bb2007-05-18 19:26:33 +000022#include "llvm/ADT/DepthFirstIterator.h"
Evan Cheng4e654852007-05-16 02:00:57 +000023#include "llvm/ADT/Statistic.h"
24using namespace llvm;
25
26STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
27
28namespace {
29 class IfConverter : public MachineFunctionPass {
30 enum BBICKind {
31 ICInvalid, // BB data invalid.
32 ICNotClassfied, // BB data valid, but not classified.
33 ICTriangle, // BB is part of a triangle sub-CFG.
34 ICDiamond, // BB is part of a diamond sub-CFG.
35 ICTriangleEntry, // BB is entry of a triangle sub-CFG.
36 ICDiamondEntry // BB is entry of a diamond sub-CFG.
37 };
38
39 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
40 /// if-conversion feasibility analysis. This includes results from
41 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
Evan Cheng86cbfea2007-05-18 00:20:58 +000042 /// classification, and common tail block of its successors (if it's a
Evan Chengcf6cc112007-05-18 18:14:37 +000043 /// diamond shape), its size, whether it's predicable, and whether any
44 /// instruction can clobber the 'would-be' predicate.
Evan Cheng4e654852007-05-16 02:00:57 +000045 struct BBInfo {
46 BBICKind Kind;
Evan Chengcf6cc112007-05-18 18:14:37 +000047 unsigned Size;
48 bool isPredicable;
49 bool ClobbersPred;
Evan Cheng86cbfea2007-05-18 00:20:58 +000050 MachineBasicBlock *BB;
51 MachineBasicBlock *TrueBB;
52 MachineBasicBlock *FalseBB;
53 MachineBasicBlock *TailBB;
Evan Cheng4e654852007-05-16 02:00:57 +000054 std::vector<MachineOperand> Cond;
Evan Chengcf6cc112007-05-18 18:14:37 +000055 BBInfo() : Kind(ICInvalid), Size(0), isPredicable(false),
56 ClobbersPred(false), BB(0), TrueBB(0), FalseBB(0), TailBB(0) {}
Evan Cheng4e654852007-05-16 02:00:57 +000057 };
58
59 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
60 /// basic block number.
61 std::vector<BBInfo> BBAnalysis;
62
Evan Cheng86cbfea2007-05-18 00:20:58 +000063 const TargetLowering *TLI;
Evan Cheng4e654852007-05-16 02:00:57 +000064 const TargetInstrInfo *TII;
65 bool MadeChange;
66 public:
67 static char ID;
68 IfConverter() : MachineFunctionPass((intptr_t)&ID) {}
69
70 virtual bool runOnMachineFunction(MachineFunction &MF);
71 virtual const char *getPassName() const { return "If converter"; }
72
73 private:
Evan Chengcf6cc112007-05-18 18:14:37 +000074 void StructuralAnalysis(MachineBasicBlock *BB);
75 void FeasibilityAnalysis(BBInfo &BBI);
Evan Cheng4e654852007-05-16 02:00:57 +000076 void InitialFunctionAnalysis(MachineFunction &MF,
Evan Cheng7f8ff8a2007-05-18 19:32:08 +000077 std::vector<BBInfo*> &Candidates);
Evan Cheng4e654852007-05-16 02:00:57 +000078 bool IfConvertTriangle(BBInfo &BBI);
Evan Chengcf6cc112007-05-18 18:14:37 +000079 bool IfConvertDiamond(BBInfo &BBI);
Evan Cheng4e654852007-05-16 02:00:57 +000080 void PredicateBlock(MachineBasicBlock *BB,
81 std::vector<MachineOperand> &Cond,
82 bool IgnoreTerm = false);
Evan Cheng86cbfea2007-05-18 00:20:58 +000083 void MergeBlocks(BBInfo &TrueBBI, BBInfo &FalseBBI);
Evan Cheng4e654852007-05-16 02:00:57 +000084 };
85 char IfConverter::ID = 0;
86}
87
88FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
89
90bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng86cbfea2007-05-18 00:20:58 +000091 TLI = MF.getTarget().getTargetLowering();
Evan Cheng4e654852007-05-16 02:00:57 +000092 TII = MF.getTarget().getInstrInfo();
93 if (!TII) return false;
94
Evan Cheng4e654852007-05-16 02:00:57 +000095 MF.RenumberBlocks();
96 unsigned NumBBs = MF.getNumBlockIDs();
97 BBAnalysis.resize(NumBBs);
98
Evan Cheng7f8ff8a2007-05-18 19:32:08 +000099 std::vector<BBInfo*> Candidates;
Evan Cheng4e654852007-05-16 02:00:57 +0000100 // Do an intial analysis for each basic block and finding all the potential
101 // candidates to perform if-convesion.
102 InitialFunctionAnalysis(MF, Candidates);
103
Evan Cheng47d25022007-05-18 01:55:58 +0000104 MadeChange = false;
Evan Cheng4e654852007-05-16 02:00:57 +0000105 for (unsigned i = 0, e = Candidates.size(); i != e; ++i) {
Evan Cheng7f8ff8a2007-05-18 19:32:08 +0000106 BBInfo &BBI = *Candidates[i];
Evan Cheng4e654852007-05-16 02:00:57 +0000107 switch (BBI.Kind) {
108 default: assert(false && "Unexpected!");
109 break;
110 case ICTriangleEntry:
111 MadeChange |= IfConvertTriangle(BBI);
112 break;
113 case ICDiamondEntry:
114 MadeChange |= IfConvertDiamond(BBI);
115 break;
116 }
117 }
Evan Cheng47d25022007-05-18 01:55:58 +0000118
119 BBAnalysis.clear();
120
Evan Cheng4e654852007-05-16 02:00:57 +0000121 return MadeChange;
122}
123
124static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng86cbfea2007-05-18 00:20:58 +0000125 MachineBasicBlock *TrueBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000126 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
127 E = BB->succ_end(); SI != E; ++SI) {
128 MachineBasicBlock *SuccBB = *SI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000129 if (SuccBB != TrueBB)
Evan Cheng4e654852007-05-16 02:00:57 +0000130 return SuccBB;
131 }
132 return NULL;
133}
134
Evan Chengcf6cc112007-05-18 18:14:37 +0000135/// StructuralAnalysis - Analyze the structure of the sub-CFG starting from
136/// the specified block. Record its successors and whether it looks like an
137/// if-conversion candidate.
138void IfConverter::StructuralAnalysis(MachineBasicBlock *BB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000139 BBInfo &BBI = BBAnalysis[BB->getNumber()];
140
141 if (BBI.Kind != ICInvalid)
142 return; // Always analyzed.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000143 BBI.BB = BB;
144 BBI.Size = std::distance(BB->begin(), BB->end());
Evan Cheng4e654852007-05-16 02:00:57 +0000145
146 // Look for 'root' of a simple (non-nested) triangle or diamond.
147 BBI.Kind = ICNotClassfied;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000148 if (TII->AnalyzeBranch(*BB, BBI.TrueBB, BBI.FalseBB, BBI.Cond)
149 || !BBI.TrueBB || BBI.Cond.size() == 0)
Evan Cheng4e654852007-05-16 02:00:57 +0000150 return;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000151
152 // Not a candidate if 'true' block has another predecessor.
153 // FIXME: Use or'd predicate or predicated cmp.
154 if (BBI.TrueBB->pred_size() > 1)
155 return;
156
157 // Not a candidate if 'true' block is going to be if-converted.
Evan Chengcf6cc112007-05-18 18:14:37 +0000158 StructuralAnalysis(BBI.TrueBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000159 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
160 if (TrueBBI.Kind != ICNotClassfied)
Evan Cheng4e654852007-05-16 02:00:57 +0000161 return;
Evan Chengd6ddc302007-05-16 21:54:37 +0000162
Evan Cheng47d25022007-05-18 01:55:58 +0000163 // TODO: Only handle very simple cases for now.
164 if (TrueBBI.FalseBB || TrueBBI.Cond.size())
165 return;
166
Evan Chengd6ddc302007-05-16 21:54:37 +0000167 // No false branch. This BB must end with a conditional branch and a
168 // fallthrough.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000169 if (!BBI.FalseBB)
170 BBI.FalseBB = findFalseBlock(BB, BBI.TrueBB);
171 assert(BBI.FalseBB && "Expected to find the fallthrough block!");
Evan Chengc5d05ef2007-05-16 05:11:10 +0000172
Evan Cheng86cbfea2007-05-18 00:20:58 +0000173 // Not a candidate if 'false' block has another predecessor.
174 // FIXME: Invert condition and swap 'true' / 'false' blocks?
175 if (BBI.FalseBB->pred_size() > 1)
176 return;
177
178 // Not a candidate if 'false' block is going to be if-converted.
Evan Chengcf6cc112007-05-18 18:14:37 +0000179 StructuralAnalysis(BBI.FalseBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000180 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
181 if (FalseBBI.Kind != ICNotClassfied)
Evan Cheng4e654852007-05-16 02:00:57 +0000182 return;
183
184 // TODO: Only handle very simple cases for now.
Evan Cheng47d25022007-05-18 01:55:58 +0000185 if (FalseBBI.FalseBB || FalseBBI.Cond.size())
Evan Cheng4e654852007-05-16 02:00:57 +0000186 return;
187
Evan Cheng86cbfea2007-05-18 00:20:58 +0000188 if (TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000189 // Triangle:
190 // EBB
191 // | \_
192 // | |
193 // | TBB
194 // | /
195 // FBB
196 BBI.Kind = ICTriangleEntry;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000197 TrueBBI.Kind = FalseBBI.Kind = ICTriangle;
198 } else if (TrueBBI.TrueBB == FalseBBI.TrueBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000199 // Diamond:
200 // EBB
201 // / \_
202 // | |
203 // TBB FBB
204 // \ /
Evan Cheng86cbfea2007-05-18 00:20:58 +0000205 // TailBB
Evan Cheng4e654852007-05-16 02:00:57 +0000206 // Note MBB can be empty in case both TBB and FBB are return blocks.
207 BBI.Kind = ICDiamondEntry;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000208 TrueBBI.Kind = FalseBBI.Kind = ICDiamond;
209 BBI.TailBB = TrueBBI.TrueBB;
Evan Cheng4e654852007-05-16 02:00:57 +0000210 }
211 return;
212}
213
Evan Chengcf6cc112007-05-18 18:14:37 +0000214/// FeasibilityAnalysis - Determine if the block is predicable. In most
215/// cases, that means all the instructions in the block has M_PREDICABLE flag.
216/// Also checks if the block contains any instruction which can clobber a
217/// predicate (e.g. condition code register). If so, the block is not
218/// predicable unless it's the last instruction. Note, this function assumes
219/// all the terminator instructions can be converted or deleted so it ignore
220/// them.
221void IfConverter::FeasibilityAnalysis(BBInfo &BBI) {
222 if (BBI.Size == 0 || BBI.Size > TLI->getIfCvtBlockSizeLimit())
223 return;
224
225 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
226 I != E; ++I) {
227 // TODO: check if instruction clobbers predicate.
228 if (TII->isTerminatorInstr(I->getOpcode()))
229 break;
230 if (!I->isPredicable())
231 return;
232 }
233
234 BBI.isPredicable = true;
235}
236
Evan Chengd6ddc302007-05-16 21:54:37 +0000237/// InitialFunctionAnalysis - Analyze all blocks and find entries for all
238/// if-conversion candidates.
Evan Cheng4e654852007-05-16 02:00:57 +0000239void IfConverter::InitialFunctionAnalysis(MachineFunction &MF,
Evan Cheng7f8ff8a2007-05-18 19:32:08 +0000240 std::vector<BBInfo*> &Candidates) {
Evan Cheng36489bb2007-05-18 19:26:33 +0000241 std::set<MachineBasicBlock*> Visited;
242 MachineBasicBlock *Entry = MF.begin();
243 for (df_ext_iterator<MachineBasicBlock*> DFI = df_ext_begin(Entry, Visited),
244 E = df_ext_end(Entry, Visited); DFI != E; ++DFI) {
245 MachineBasicBlock *BB = *DFI;
Evan Chengcf6cc112007-05-18 18:14:37 +0000246 StructuralAnalysis(BB);
Evan Cheng4e654852007-05-16 02:00:57 +0000247 BBInfo &BBI = BBAnalysis[BB->getNumber()];
248 if (BBI.Kind == ICTriangleEntry || BBI.Kind == ICDiamondEntry)
Evan Cheng7f8ff8a2007-05-18 19:32:08 +0000249 Candidates.push_back(&BBI);
Evan Cheng4e654852007-05-16 02:00:57 +0000250 }
251}
252
Evan Cheng86cbfea2007-05-18 00:20:58 +0000253/// TransferPreds - Transfer all the predecessors of FromBB to ToBB.
254///
255static void TransferPreds(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
256 std::vector<MachineBasicBlock*> Preds(FromBB->pred_begin(),
257 FromBB->pred_end());
258 for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
259 MachineBasicBlock *Pred = Preds[i];
260 Pred->removeSuccessor(FromBB);
261 if (!Pred->isSuccessor(ToBB))
262 Pred->addSuccessor(ToBB);
263 }
264}
265
266/// TransferSuccs - Transfer all the successors of FromBB to ToBB.
267///
268static void TransferSuccs(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
269 std::vector<MachineBasicBlock*> Succs(FromBB->succ_begin(),
270 FromBB->succ_end());
271 for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
272 MachineBasicBlock *Succ = Succs[i];
273 FromBB->removeSuccessor(Succ);
274 if (!ToBB->isSuccessor(Succ))
275 ToBB->addSuccessor(Succ);
276 }
277}
278
Evan Chengd6ddc302007-05-16 21:54:37 +0000279/// IfConvertTriangle - If convert a triangle sub-CFG.
280///
Evan Cheng4e654852007-05-16 02:00:57 +0000281bool IfConverter::IfConvertTriangle(BBInfo &BBI) {
Evan Chengcf6cc112007-05-18 18:14:37 +0000282 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
283 FeasibilityAnalysis(TrueBBI);
284
285 if (TrueBBI.isPredicable) {
Evan Cheng86cbfea2007-05-18 00:20:58 +0000286 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
287
Evan Cheng4e654852007-05-16 02:00:57 +0000288 // Predicate the 'true' block after removing its branch.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000289 TrueBBI.Size -= TII->RemoveBranch(*BBI.TrueBB);
290 PredicateBlock(BBI.TrueBB, BBI.Cond);
Evan Cheng4e654852007-05-16 02:00:57 +0000291
292 // Join the 'true' and 'false' blocks by copying the instructions
293 // from the 'false' block to the 'true' block.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000294 BBI.TrueBB->removeSuccessor(BBI.FalseBB);
295 MergeBlocks(TrueBBI, FalseBBI);
Evan Cheng4e654852007-05-16 02:00:57 +0000296
Evan Cheng86cbfea2007-05-18 00:20:58 +0000297 // Now merge the entry of the triangle with the true block.
298 BBI.Size -= TII->RemoveBranch(*BBI.BB);
299 MergeBlocks(BBI, TrueBBI);
300
301 // Update block info.
302 TrueBBI.Kind = ICInvalid;
303 FalseBBI.Kind = ICInvalid;
Evan Cheng4e654852007-05-16 02:00:57 +0000304
305 // FIXME: Must maintain LiveIns.
306 NumIfConvBBs++;
307 return true;
308 }
309 return false;
310}
311
Evan Chengd6ddc302007-05-16 21:54:37 +0000312/// IfConvertDiamond - If convert a diamond sub-CFG.
313///
Evan Cheng4e654852007-05-16 02:00:57 +0000314bool IfConverter::IfConvertDiamond(BBInfo &BBI) {
Evan Chengcf6cc112007-05-18 18:14:37 +0000315 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
316 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
317 FeasibilityAnalysis(TrueBBI);
318 FeasibilityAnalysis(FalseBBI);
319
320 if (TrueBBI.isPredicable && FalseBBI.isPredicable) {
321 // Check the 'true' and 'false' blocks if either isn't ended with a branch.
322 // Either the block fallthrough to another block or it ends with a
323 // return. If it's the former, add a conditional branch to its successor.
324 bool Proceed = true;
325 bool TrueNeedCBr = !TrueBBI.TrueBB && BBI.TrueBB->succ_size();
326 bool FalseNeedCBr = !FalseBBI.TrueBB && BBI.FalseBB->succ_size();
327 if (TrueNeedCBr && TrueBBI.ClobbersPred) {
328 TrueBBI.isPredicable = false;
329 Proceed = false;
330 }
331 if (FalseNeedCBr && FalseBBI.ClobbersPred) {
332 FalseBBI.isPredicable = false;
333 Proceed = false;
334 }
335 if (!Proceed)
336 return false;
337
Evan Cheng4e654852007-05-16 02:00:57 +0000338 std::vector<MachineInstr*> Dups;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000339 if (!BBI.TailBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000340 // No common merge block. Check if the terminators (e.g. return) are
Evan Chengc3a289c2007-05-16 20:56:08 +0000341 // the same or predicable.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000342 MachineBasicBlock::iterator TT = BBI.TrueBB->getFirstTerminator();
343 MachineBasicBlock::iterator FT = BBI.FalseBB->getFirstTerminator();
344 while (TT != BBI.TrueBB->end() && FT != BBI.FalseBB->end()) {
Evan Cheng4e654852007-05-16 02:00:57 +0000345 if (TT->isIdenticalTo(FT))
346 Dups.push_back(TT); // Will erase these later.
Evan Chengc3a289c2007-05-16 20:56:08 +0000347 else if (!TT->isPredicable() && !FT->isPredicable())
Evan Cheng4e654852007-05-16 02:00:57 +0000348 return false; // Can't if-convert. Abort!
349 ++TT;
350 ++FT;
351 }
Evan Chengd6ddc302007-05-16 21:54:37 +0000352 // One of the two pathes have more terminators, make sure they are all
353 // predicable.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000354 while (TT != BBI.TrueBB->end())
Evan Chengc3a289c2007-05-16 20:56:08 +0000355 if (!TT->isPredicable())
Evan Cheng4e654852007-05-16 02:00:57 +0000356 return false; // Can't if-convert. Abort!
Evan Cheng86cbfea2007-05-18 00:20:58 +0000357 while (FT != BBI.FalseBB->end())
Evan Chengc3a289c2007-05-16 20:56:08 +0000358 if (!FT->isPredicable())
Evan Cheng4e654852007-05-16 02:00:57 +0000359 return false; // Can't if-convert. Abort!
360 }
361
362 // Remove the duplicated instructions from the 'true' block.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000363 for (unsigned i = 0, e = Dups.size(); i != e; ++i) {
Evan Cheng4e654852007-05-16 02:00:57 +0000364 Dups[i]->eraseFromParent();
Evan Cheng86cbfea2007-05-18 00:20:58 +0000365 --TrueBBI.Size;
366 }
Evan Cheng4e654852007-05-16 02:00:57 +0000367
368 // Predicate the 'true' block after removing its branch.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000369 TrueBBI.Size -= TII->RemoveBranch(*BBI.TrueBB);
370 PredicateBlock(BBI.TrueBB, BBI.Cond);
Evan Cheng4e654852007-05-16 02:00:57 +0000371
Evan Chengcf6cc112007-05-18 18:14:37 +0000372 // Add a conditional branch to 'true' successor if needed.
373 if (TrueNeedCBr)
Evan Cheng47d25022007-05-18 01:55:58 +0000374 TII->InsertBranch(*BBI.TrueBB, *BBI.TrueBB->succ_begin(), NULL, BBI.Cond);
375
Evan Cheng4e654852007-05-16 02:00:57 +0000376 // Predicate the 'false' block.
377 std::vector<MachineOperand> NewCond(BBI.Cond);
378 TII->ReverseBranchCondition(NewCond);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000379 PredicateBlock(BBI.FalseBB, NewCond, true);
Evan Cheng4e654852007-05-16 02:00:57 +0000380
Evan Chengcf6cc112007-05-18 18:14:37 +0000381 // Add a conditional branch to 'false' successor if needed.
382 if (FalseNeedCBr)
Evan Cheng47d25022007-05-18 01:55:58 +0000383 TII->InsertBranch(*BBI.FalseBB, *BBI.FalseBB->succ_begin(), NULL,NewCond);
384
Evan Cheng86cbfea2007-05-18 00:20:58 +0000385 // Merge the 'true' and 'false' blocks by copying the instructions
Evan Cheng36489bb2007-05-18 19:26:33 +0000386 // from the 'false' block to the 'true' block. That is, unless the true
387 // block would clobber the predicate, in that case, do the opposite.
388 BBInfo *CvtBBI;
389 if (!TrueBBI.ClobbersPred) {
390 MergeBlocks(TrueBBI, FalseBBI);
391 CvtBBI = &TrueBBI;
392 } else {
393 MergeBlocks(FalseBBI, TrueBBI);
394 CvtBBI = &FalseBBI;
395 }
Evan Cheng4e654852007-05-16 02:00:57 +0000396
Evan Cheng86cbfea2007-05-18 00:20:58 +0000397 // Remove the conditional branch from entry to the blocks.
398 BBI.Size -= TII->RemoveBranch(*BBI.BB);
399
400 // Merge the combined block into the entry of the diamond if the entry
Evan Cheng36489bb2007-05-18 19:26:33 +0000401 // block is its only predecessor. Otherwise, insert an unconditional
402 // branch from entry to the if-converted block.
403 if (CvtBBI->BB->pred_size() == 1) {
404 BBI.BB->removeSuccessor(CvtBBI->BB);
405 MergeBlocks(BBI, *CvtBBI);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000406 CvtBBI = &BBI;
407 } else {
408 std::vector<MachineOperand> NoCond;
Evan Cheng36489bb2007-05-18 19:26:33 +0000409 TII->InsertBranch(*BBI.BB, CvtBBI->BB, NULL, NoCond);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000410 }
411
412 // If the if-converted block fallthrough into the tail block, then
413 // fold the tail block in as well.
414 if (BBI.TailBB && CvtBBI->BB->succ_size() == 1) {
415 CvtBBI->Size -= TII->RemoveBranch(*CvtBBI->BB);
416 CvtBBI->BB->removeSuccessor(BBI.TailBB);
417 BBInfo TailBBI = BBAnalysis[BBI.TailBB->getNumber()];
418 MergeBlocks(*CvtBBI, TailBBI);
419 TailBBI.Kind = ICInvalid;
420 }
421
422 // Update block info.
423 TrueBBI.Kind = ICInvalid;
424 FalseBBI.Kind = ICInvalid;
Evan Cheng4e654852007-05-16 02:00:57 +0000425
426 // FIXME: Must maintain LiveIns.
427 NumIfConvBBs += 2;
428 return true;
429 }
430 return false;
431}
432
Evan Cheng4e654852007-05-16 02:00:57 +0000433/// PredicateBlock - Predicate every instruction in the block with the specified
434/// condition. If IgnoreTerm is true, skip over all terminator instructions.
435void IfConverter::PredicateBlock(MachineBasicBlock *BB,
436 std::vector<MachineOperand> &Cond,
437 bool IgnoreTerm) {
438 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
439 I != E; ++I) {
440 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
441 continue;
Evan Chengd6ddc302007-05-16 21:54:37 +0000442 if (!TII->PredicateInstruction(&*I, Cond)) {
443 cerr << "Unable to predication " << *I << "!\n";
444 abort();
445 }
Evan Cheng4e654852007-05-16 02:00:57 +0000446 }
447}
448
Evan Cheng86cbfea2007-05-18 00:20:58 +0000449/// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
Evan Cheng4e654852007-05-16 02:00:57 +0000450///
Evan Cheng86cbfea2007-05-18 00:20:58 +0000451void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) {
452 ToBBI.BB->splice(ToBBI.BB->end(),
453 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
454 TransferPreds(ToBBI.BB, FromBBI.BB);
455 TransferSuccs(ToBBI.BB, FromBBI.BB);
456 ToBBI.Size += FromBBI.Size;
457 FromBBI.Size = 0;
Evan Cheng4e654852007-05-16 02:00:57 +0000458}