blob: 05e66970c78942d846b2b8f6c88f397080d225d9 [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.
Evan Cheng7a655472007-06-06 10:16:17 +000044 ICDead // BB cannot be if-converted again.
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 Cheng7a655472007-06-06 10:16:17 +000057 /// ModifyPredicate - True if BB would modify the predicate (e.g. has
58 /// cmp, call, etc.)
Evan Chenga13aa952007-05-23 07:23:16 +000059 /// 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 Cheng7a655472007-06-06 10:16:17 +000099 bool BlockModifyPredicate(MachineBasicBlock *BB) const;
Evan Chengcf6cc112007-05-18 18:14:37 +0000100 void StructuralAnalysis(MachineBasicBlock *BB);
Evan Chengb6665f62007-06-04 06:47:22 +0000101 bool FeasibilityAnalysis(BBInfo &BBI,
102 std::vector<MachineOperand> &Cond,
103 bool IgnoreTerm = false);
Evan Cheng82582102007-05-30 19:49:19 +0000104 bool AttemptRestructuring(BBInfo &BBI);
105 bool AnalyzeBlocks(MachineFunction &MF,
Evan Chenga13aa952007-05-23 07:23:16 +0000106 std::vector<BBInfo*> &Candidates);
Evan Cheng82582102007-05-30 19:49:19 +0000107 void ReTryPreds(MachineBasicBlock *BB);
Evan Chengb6665f62007-06-04 06:47:22 +0000108 bool IfConvertSimple(BBInfo &BBI);
Evan Cheng4e654852007-05-16 02:00:57 +0000109 bool IfConvertTriangle(BBInfo &BBI);
Evan Chengcf6cc112007-05-18 18:14:37 +0000110 bool IfConvertDiamond(BBInfo &BBI);
Evan Chenga13aa952007-05-23 07:23:16 +0000111 void PredicateBlock(BBInfo &BBI,
Evan Cheng4e654852007-05-16 02:00:57 +0000112 std::vector<MachineOperand> &Cond,
113 bool IgnoreTerm = false);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000114 void MergeBlocks(BBInfo &TrueBBI, BBInfo &FalseBBI);
Evan Cheng5f702182007-06-01 00:12:12 +0000115
Evan Cheng7a655472007-06-06 10:16:17 +0000116 // blockFallsThrough - Block ends without a terminator.
117 bool blockFallsThrough(BBInfo &BBI) const {
118 return BBI.IsAnalyzable && BBI.TrueBB == NULL;
119 }
120
Evan Cheng5f702182007-06-01 00:12:12 +0000121 // IfcvtCandidateCmp - Used to sort if-conversion candidates.
122 static bool IfcvtCandidateCmp(BBInfo* C1, BBInfo* C2){
123 // Favor diamond over triangle, etc.
124 return (unsigned)C1->Kind < (unsigned)C2->Kind;
125 }
Evan Cheng4e654852007-05-16 02:00:57 +0000126 };
127 char IfConverter::ID = 0;
128}
129
130FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
131
132bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng86cbfea2007-05-18 00:20:58 +0000133 TLI = MF.getTarget().getTargetLowering();
Evan Cheng4e654852007-05-16 02:00:57 +0000134 TII = MF.getTarget().getInstrInfo();
135 if (!TII) return false;
136
Evan Cheng5f702182007-06-01 00:12:12 +0000137 DOUT << "\nIfcvt: function \'" << MF.getFunction()->getName() << "\'\n";
138
Evan Cheng4e654852007-05-16 02:00:57 +0000139 MF.RenumberBlocks();
Evan Cheng5f702182007-06-01 00:12:12 +0000140 BBAnalysis.resize(MF.getNumBlockIDs());
Evan Cheng4e654852007-05-16 02:00:57 +0000141
Evan Cheng82582102007-05-30 19:49:19 +0000142 // Look for root nodes, i.e. blocks without successors.
143 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
144 if (I->succ_size() == 0)
145 Roots.push_back(I);
146
Evan Cheng7f8ff8a2007-05-18 19:32:08 +0000147 std::vector<BBInfo*> Candidates;
Evan Cheng47d25022007-05-18 01:55:58 +0000148 MadeChange = false;
Evan Chenga13aa952007-05-23 07:23:16 +0000149 while (true) {
Evan Chenga13aa952007-05-23 07:23:16 +0000150 // Do an intial analysis for each basic block and finding all the potential
151 // candidates to perform if-convesion.
Evan Chengb6665f62007-06-04 06:47:22 +0000152 bool Change = AnalyzeBlocks(MF, Candidates);
Evan Chenga13aa952007-05-23 07:23:16 +0000153 while (!Candidates.empty()) {
154 BBInfo &BBI = *Candidates.back();
155 Candidates.pop_back();
Evan Chengb6665f62007-06-04 06:47:22 +0000156
157 bool RetVal = false;
Evan Chenga13aa952007-05-23 07:23:16 +0000158 switch (BBI.Kind) {
159 default: assert(false && "Unexpected!");
160 break;
Evan Cheng5f702182007-06-01 00:12:12 +0000161 case ICReAnalyze:
Evan Chengf5305f92007-06-05 00:07:37 +0000162 // One or more of 'children' have been modified, abort!
163 case ICDead:
164 // Block has been already been if-converted, abort!
Evan Cheng5f702182007-06-01 00:12:12 +0000165 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000166 case ICSimple:
Evan Chengcb78d672007-06-06 01:12:44 +0000167 case ICSimpleFalse: {
168 bool isRev = BBI.Kind == ICSimpleFalse;
Evan Chengb6665f62007-06-04 06:47:22 +0000169 DOUT << "Ifcvt (Simple" << (BBI.Kind == ICSimpleFalse ? " false" : "")
Evan Cheng7a655472007-06-06 10:16:17 +0000170 << "): BB#" << BBI.BB->getNumber() << " ("
171 << ((BBI.Kind == ICSimpleFalse)
172 ? BBI.FalseBB->getNumber() : BBI.TrueBB->getNumber()) << ") ";
Evan Chengb6665f62007-06-04 06:47:22 +0000173 RetVal = IfConvertSimple(BBI);
174 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
175 if (RetVal)
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000176 if (isRev) NumSimpleRev++;
177 else NumSimple++;
Evan Chengb6665f62007-06-04 06:47:22 +0000178 break;
Evan Chengcb78d672007-06-06 01:12:44 +0000179 }
Evan Chenga13aa952007-05-23 07:23:16 +0000180 case ICTriangle:
Evan Cheng7a655472007-06-06 10:16:17 +0000181 DOUT << "Ifcvt (Triangle): BB#" << BBI.BB->getNumber() << " (T:"
182 << BBI.TrueBB->getNumber() << ",F:" << BBI.FalseBB->getNumber()
183 << ") ";
Evan Chengb6665f62007-06-04 06:47:22 +0000184 RetVal = IfConvertTriangle(BBI);
185 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
186 if (RetVal) NumTriangle++;
Evan Chenga13aa952007-05-23 07:23:16 +0000187 break;
188 case ICDiamond:
Evan Cheng7a655472007-06-06 10:16:17 +0000189 DOUT << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
190 << BBI.TrueBB->getNumber() << ",F:" << BBI.FalseBB->getNumber();
191 if (BBI.TailBB)
192 DOUT << "," << BBI.TailBB->getNumber() ;
193 DOUT << ") ";
Evan Chengb6665f62007-06-04 06:47:22 +0000194 RetVal = IfConvertDiamond(BBI);
195 DOUT << (RetVal ? "succeeded!" : "failed!") << "\n";
196 if (RetVal) NumDiamonds++;
Evan Chenga13aa952007-05-23 07:23:16 +0000197 break;
198 }
Evan Chengb6665f62007-06-04 06:47:22 +0000199 Change |= RetVal;
Evan Cheng4e654852007-05-16 02:00:57 +0000200 }
Evan Chenga13aa952007-05-23 07:23:16 +0000201
Evan Chenga13aa952007-05-23 07:23:16 +0000202 if (!Change)
203 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000204 MadeChange |= Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000205 }
Evan Cheng47d25022007-05-18 01:55:58 +0000206
Evan Cheng82582102007-05-30 19:49:19 +0000207 Roots.clear();
Evan Cheng47d25022007-05-18 01:55:58 +0000208 BBAnalysis.clear();
209
Evan Cheng4e654852007-05-16 02:00:57 +0000210 return MadeChange;
211}
212
213static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng86cbfea2007-05-18 00:20:58 +0000214 MachineBasicBlock *TrueBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000215 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
216 E = BB->succ_end(); SI != E; ++SI) {
217 MachineBasicBlock *SuccBB = *SI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000218 if (SuccBB != TrueBB)
Evan Cheng4e654852007-05-16 02:00:57 +0000219 return SuccBB;
220 }
221 return NULL;
222}
223
Evan Chengb6665f62007-06-04 06:47:22 +0000224bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
225 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
226 TII->RemoveBranch(*BBI.BB);
227 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond);
228 std::swap(BBI.TrueBB, BBI.FalseBB);
229 return true;
230 }
231 return false;
232}
233
Evan Cheng7a655472007-06-06 10:16:17 +0000234/// BlockModifyPredicate - Returns true if any instruction in the block may
235/// clobber the condition code or register(s) used to predicate instructions,
236/// e.g. call, cmp.
237bool IfConverter::BlockModifyPredicate(MachineBasicBlock *BB) const {
238 for (MachineBasicBlock::const_reverse_iterator I = BB->rbegin(),
239 E = BB->rend(); I != E; ++I)
240 if (I->getInstrDescriptor()->Flags & M_CLOBBERS_PRED)
241 return true;
242 return false;
243}
244
Evan Chengcf6cc112007-05-18 18:14:37 +0000245/// StructuralAnalysis - Analyze the structure of the sub-CFG starting from
246/// the specified block. Record its successors and whether it looks like an
247/// if-conversion candidate.
248void IfConverter::StructuralAnalysis(MachineBasicBlock *BB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000249 BBInfo &BBI = BBAnalysis[BB->getNumber()];
250
Evan Chengf5305f92007-06-05 00:07:37 +0000251 if (BBI.Kind == ICReAnalyze) {
Evan Chengb6665f62007-06-04 06:47:22 +0000252 BBI.BrCond.clear();
Evan Chengf5305f92007-06-05 00:07:37 +0000253 BBI.TrueBB = BBI.FalseBB = NULL;
254 } else {
Evan Chenga13aa952007-05-23 07:23:16 +0000255 if (BBI.Kind != ICNotAnalyzed)
256 return; // Already analyzed.
257 BBI.BB = BB;
258 BBI.NonPredSize = std::distance(BB->begin(), BB->end());
Evan Cheng7a655472007-06-06 10:16:17 +0000259 BBI.ModifyPredicate = BlockModifyPredicate(BB);
Evan Chenga13aa952007-05-23 07:23:16 +0000260 }
Evan Cheng86cbfea2007-05-18 00:20:58 +0000261
Evan Cheng4bec8ae2007-05-25 00:59:01 +0000262 // Look for 'root' of a simple (non-nested) triangle or diamond.
263 BBI.Kind = ICNotClassfied;
Evan Chengb6665f62007-06-04 06:47:22 +0000264 BBI.IsAnalyzable =
265 !TII->AnalyzeBranch(*BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
266 if (!BBI.IsAnalyzable || BBI.BrCond.size() == 0)
Evan Cheng4bec8ae2007-05-25 00:59:01 +0000267 return;
Evan Chenge0043172007-06-05 20:38:42 +0000268 // Do not ifcvt if either path is a back edge to the entry block.
269 if (BBI.TrueBB == BB || BBI.FalseBB == BB)
270 return;
Evan Cheng4bec8ae2007-05-25 00:59:01 +0000271
Evan Chengcf6cc112007-05-18 18:14:37 +0000272 StructuralAnalysis(BBI.TrueBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000273 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengd6ddc302007-05-16 21:54:37 +0000274
275 // No false branch. This BB must end with a conditional branch and a
276 // fallthrough.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000277 if (!BBI.FalseBB)
278 BBI.FalseBB = findFalseBlock(BB, BBI.TrueBB);
279 assert(BBI.FalseBB && "Expected to find the fallthrough block!");
Evan Chengc5d05ef2007-05-16 05:11:10 +0000280
Evan Chengcf6cc112007-05-18 18:14:37 +0000281 StructuralAnalysis(BBI.FalseBB);
Evan Cheng86cbfea2007-05-18 00:20:58 +0000282 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Cheng7a655472007-06-06 10:16:17 +0000283
284 // If both paths are dead, then forget about it.
285 if (TrueBBI.Kind == ICDead && FalseBBI.Kind == ICDead) {
286 BBI.Kind = ICDead;
287 return;
288 }
289
Evan Chengb6665f62007-06-04 06:47:22 +0000290 // Look for more opportunities to if-convert a triangle. Try to restructure
291 // the CFG to form a triangle with the 'false' path.
292 std::vector<MachineOperand> RevCond(BBI.BrCond);
293 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
294 if (FalseBBI.FalseBB) {
295 if (TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB)
296 return;
297 std::vector<MachineOperand> Cond(BBI.BrCond);
298 if (CanRevCond &&
299 FalseBBI.TrueBB && FalseBBI.BB->pred_size() == 1 &&
300 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
301 std::vector<MachineOperand> FalseCond(FalseBBI.BrCond);
302 if (FalseBBI.TrueBB == BBI.TrueBB &&
303 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
304 // Reverse 'true' and 'false' paths.
305 ReverseBranchCondition(BBI);
306 BBI.Kind = ICTriangle;
307 FalseBBI.Kind = ICChild;
308 } else if (FalseBBI.FalseBB == BBI.TrueBB &&
309 !TII->ReverseBranchCondition(FalseCond) &&
310 TII->SubsumesPredicate(FalseCond, BBI.BrCond)) {
311 // Reverse 'false' block's 'true' and 'false' paths and then
312 // reverse 'true' and 'false' paths.
313 ReverseBranchCondition(FalseBBI);
314 ReverseBranchCondition(BBI);
315 BBI.Kind = ICTriangle;
316 FalseBBI.Kind = ICChild;
317 }
318 }
319 } else if (TrueBBI.TrueBB == FalseBBI.TrueBB && CanRevCond &&
320 TrueBBI.BB->pred_size() == 1 &&
Evan Cheng993fc952007-06-05 23:46:14 +0000321 FalseBBI.BB->pred_size() == 1 &&
Evan Chengb6665f62007-06-04 06:47:22 +0000322 // Check the 'true' and 'false' blocks if either isn't ended with
323 // a branch. If the block does not fallthrough to another block
324 // then we need to add a branch to its successor.
325 !(TrueBBI.ModifyPredicate &&
326 !TrueBBI.TrueBB && TrueBBI.BB->succ_size()) &&
327 !(FalseBBI.ModifyPredicate &&
328 !FalseBBI.TrueBB && FalseBBI.BB->succ_size()) &&
329 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
330 FeasibilityAnalysis(FalseBBI, RevCond)) {
Evan Cheng4e654852007-05-16 02:00:57 +0000331 // Diamond:
332 // EBB
333 // / \_
334 // | |
335 // TBB FBB
336 // \ /
Evan Cheng86cbfea2007-05-18 00:20:58 +0000337 // TailBB
Evan Cheng993fc952007-06-05 23:46:14 +0000338 // Note TailBB can be empty.
Evan Chenga6b4f432007-05-21 22:22:58 +0000339 BBI.Kind = ICDiamond;
340 TrueBBI.Kind = FalseBBI.Kind = ICChild;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000341 BBI.TailBB = TrueBBI.TrueBB;
Evan Chengb6665f62007-06-04 06:47:22 +0000342 } else {
343 // FIXME: Consider duplicating if BB is small.
344 bool TryTriangle = TrueBBI.TrueBB && TrueBBI.TrueBB == BBI.FalseBB &&
Evan Chenge7052132007-06-06 00:57:55 +0000345 TrueBBI.BB->pred_size() == 1;
346 bool TrySimple = TrueBBI.BrCond.size() == 0 && TrueBBI.BB->pred_size() == 1;
Evan Chengb6665f62007-06-04 06:47:22 +0000347 if ((TryTriangle || TrySimple) &&
348 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
349 if (TryTriangle) {
350 // Triangle:
351 // EBB
352 // | \_
353 // | |
354 // | TBB
355 // | /
356 // FBB
357 BBI.Kind = ICTriangle;
358 TrueBBI.Kind = ICChild;
359 } else {
360 // Simple (split, no rejoin):
361 // EBB
362 // | \_
363 // | |
364 // | TBB---> exit
365 // |
366 // FBB
367 BBI.Kind = ICSimple;
368 TrueBBI.Kind = ICChild;
369 }
Evan Chenge7052132007-06-06 00:57:55 +0000370 } else if (FalseBBI.BrCond.size() == 0 && FalseBBI.BB->pred_size() == 1) {
371 // Try the other path...
Evan Cheng7a655472007-06-06 10:16:17 +0000372 bool TryTriangle = FalseBBI.TrueBB && FalseBBI.TrueBB == BBI.TrueBB &&
373 FalseBBI.BB->pred_size() == 1;
Evan Chengb6665f62007-06-04 06:47:22 +0000374 std::vector<MachineOperand> RevCond(BBI.BrCond);
Evan Chenge7052132007-06-06 00:57:55 +0000375 if (!TII->ReverseBranchCondition(RevCond) &&
376 FeasibilityAnalysis(FalseBBI, RevCond)) {
Evan Cheng7a655472007-06-06 10:16:17 +0000377 if (TryTriangle) {
Evan Chenge7052132007-06-06 00:57:55 +0000378 // Reverse 'true' and 'false' paths.
379 ReverseBranchCondition(BBI);
380 BBI.Kind = ICTriangle;
381 FalseBBI.Kind = ICChild;
382 } else {
383 BBI.Kind = ICSimpleFalse;
384 FalseBBI.Kind = ICChild;
385 }
Evan Chengb6665f62007-06-04 06:47:22 +0000386 }
387 }
Evan Cheng4e654852007-05-16 02:00:57 +0000388 }
389 return;
390}
391
Evan Chengcf6cc112007-05-18 18:14:37 +0000392/// FeasibilityAnalysis - Determine if the block is predicable. In most
393/// cases, that means all the instructions in the block has M_PREDICABLE flag.
394/// Also checks if the block contains any instruction which can clobber a
395/// predicate (e.g. condition code register). If so, the block is not
Evan Chengb6665f62007-06-04 06:47:22 +0000396/// predicable unless it's the last instruction. If IgnoreTerm is true then
397/// all the terminator instructions are skipped.
398bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
399 std::vector<MachineOperand> &Cond,
400 bool IgnoreTerm) {
401 // If the block is dead, or it is going to be the entry block of a sub-CFG
402 // that will be if-converted, then it cannot be predicated.
403 if (BBI.Kind != ICNotAnalyzed &&
404 BBI.Kind != ICNotClassfied &&
405 BBI.Kind != ICChild)
406 return false;
407
408 // Check predication threshold.
Evan Chenga13aa952007-05-23 07:23:16 +0000409 if (BBI.NonPredSize == 0 || BBI.NonPredSize > TLI->getIfCvtBlockSizeLimit())
Evan Chengb6665f62007-06-04 06:47:22 +0000410 return false;
411
412 // If it is already predicated, check if its predicate subsumes the new
413 // predicate.
414 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Cond))
415 return false;
Evan Chengcf6cc112007-05-18 18:14:37 +0000416
417 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
418 I != E; ++I) {
Evan Chengb6665f62007-06-04 06:47:22 +0000419 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
420 continue;
Evan Chengcf6cc112007-05-18 18:14:37 +0000421 // TODO: check if instruction clobbers predicate.
Evan Chengcf6cc112007-05-18 18:14:37 +0000422 if (!I->isPredicable())
Evan Chengb6665f62007-06-04 06:47:22 +0000423 return false;
Evan Chengcf6cc112007-05-18 18:14:37 +0000424 }
425
Evan Chengb6665f62007-06-04 06:47:22 +0000426 return true;
Evan Chengcf6cc112007-05-18 18:14:37 +0000427}
428
Evan Cheng82582102007-05-30 19:49:19 +0000429/// AttemptRestructuring - Restructure the sub-CFG rooted in the given block to
430/// expose more if-conversion opportunities. e.g.
431///
432/// cmp
433/// b le BB1
434/// / \____
435/// / |
436/// cmp |
437/// b eq BB1 |
438/// / \____ |
439/// / \ |
440/// BB1
441/// ==>
442///
443/// cmp
444/// b eq BB1
445/// / \____
446/// / |
447/// cmp |
448/// b le BB1 |
449/// / \____ |
450/// / \ |
451/// BB1
452bool IfConverter::AttemptRestructuring(BBInfo &BBI) {
453 return false;
454}
455
456/// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
457/// candidates. It returns true if any CFG restructuring is done to expose more
458/// if-conversion opportunities.
459bool IfConverter::AnalyzeBlocks(MachineFunction &MF,
Evan Chenga13aa952007-05-23 07:23:16 +0000460 std::vector<BBInfo*> &Candidates) {
Evan Cheng82582102007-05-30 19:49:19 +0000461 bool Change = false;
Evan Cheng36489bb2007-05-18 19:26:33 +0000462 std::set<MachineBasicBlock*> Visited;
Evan Cheng82582102007-05-30 19:49:19 +0000463 for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
464 for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited),
465 E = idf_ext_end(Roots[i], Visited); I != E; ++I) {
466 MachineBasicBlock *BB = *I;
467 StructuralAnalysis(BB);
468 BBInfo &BBI = BBAnalysis[BB->getNumber()];
469 switch (BBI.Kind) {
Evan Chengb6665f62007-06-04 06:47:22 +0000470 case ICSimple:
471 case ICSimpleFalse:
Evan Cheng82582102007-05-30 19:49:19 +0000472 case ICTriangle:
473 case ICDiamond:
474 Candidates.push_back(&BBI);
475 break;
476 default:
477 Change |= AttemptRestructuring(BBI);
478 break;
479 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000480 }
Evan Cheng4e654852007-05-16 02:00:57 +0000481 }
Evan Cheng82582102007-05-30 19:49:19 +0000482
Evan Cheng5f702182007-06-01 00:12:12 +0000483 // Sort to favor more complex ifcvt scheme.
484 std::stable_sort(Candidates.begin(), Candidates.end(), IfcvtCandidateCmp);
485
Evan Cheng82582102007-05-30 19:49:19 +0000486 return Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000487}
488
Evan Chengb6665f62007-06-04 06:47:22 +0000489/// isNextBlock - Returns true either if ToBB the next block after BB or
490/// that all the intervening blocks are empty.
Evan Chenga6b4f432007-05-21 22:22:58 +0000491static bool isNextBlock(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
Evan Chengb6665f62007-06-04 06:47:22 +0000492 MachineFunction::iterator I = BB;
Evan Chengc53ef582007-06-05 07:05:25 +0000493 MachineFunction::iterator TI = ToBB;
494 MachineFunction::iterator E = BB->getParent()->end();
495 while (++I != TI)
496 if (I == E || !I->empty())
Evan Chengb6665f62007-06-04 06:47:22 +0000497 return false;
498 return true;
Evan Chenga6b4f432007-05-21 22:22:58 +0000499}
500
Evan Cheng82582102007-05-30 19:49:19 +0000501/// ReTryPreds - Invalidate predecessor BB info so it would be re-analyzed
Evan Chenga13aa952007-05-23 07:23:16 +0000502/// to determine if it can be if-converted.
Evan Cheng82582102007-05-30 19:49:19 +0000503void IfConverter::ReTryPreds(MachineBasicBlock *BB) {
Evan Chenga13aa952007-05-23 07:23:16 +0000504 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
505 E = BB->pred_end(); PI != E; ++PI) {
506 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
507 PBBI.Kind = ICReAnalyze;
508 }
509}
510
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000511/// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
512///
513static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
514 const TargetInstrInfo *TII) {
515 std::vector<MachineOperand> NoCond;
516 TII->InsertBranch(*BB, ToBB, NULL, NoCond);
517}
518
Evan Chengb6665f62007-06-04 06:47:22 +0000519/// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
Evan Chenga6b4f432007-05-21 22:22:58 +0000520///
Evan Chengb6665f62007-06-04 06:47:22 +0000521bool IfConverter::IfConvertSimple(BBInfo &BBI) {
Evan Chenga6b4f432007-05-21 22:22:58 +0000522 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
523 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
524 BBInfo *CvtBBI = &TrueBBI;
525 BBInfo *NextBBI = &FalseBBI;
Evan Chenga13aa952007-05-23 07:23:16 +0000526
Evan Chengb6665f62007-06-04 06:47:22 +0000527 std::vector<MachineOperand> Cond(BBI.BrCond);
Evan Cheng7a655472007-06-06 10:16:17 +0000528 if (BBI.Kind == ICSimpleFalse) {
Evan Chengb5a06902007-06-01 20:29:21 +0000529 std::swap(CvtBBI, NextBBI);
Evan Chengb6665f62007-06-04 06:47:22 +0000530 TII->ReverseBranchCondition(Cond);
Evan Chengb5a06902007-06-01 20:29:21 +0000531 }
Evan Chenga13aa952007-05-23 07:23:16 +0000532
Evan Chengb6665f62007-06-04 06:47:22 +0000533 PredicateBlock(*CvtBBI, Cond);
534 // If the 'true' block ends without a branch, add a conditional branch
535 // to its successor unless that happens to be the 'false' block.
536 if (CvtBBI->IsAnalyzable && CvtBBI->TrueBB == NULL) {
537 assert(CvtBBI->BB->succ_size() == 1 && "Unexpected!");
538 MachineBasicBlock *SuccBB = *CvtBBI->BB->succ_begin();
539 if (SuccBB != NextBBI->BB)
540 TII->InsertBranch(*CvtBBI->BB, SuccBB, NULL, Cond);
541 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000542
Evan Chengb6665f62007-06-04 06:47:22 +0000543 // Merge converted block into entry block. Also add an unconditional branch
544 // to the 'false' branch.
Evan Chenga13aa952007-05-23 07:23:16 +0000545 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000546 MergeBlocks(BBI, *CvtBBI);
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000547 bool IterIfcvt = true;
548 if (!isNextBlock(BBI.BB, NextBBI->BB)) {
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000549 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
Evan Cheng7a655472007-06-06 10:16:17 +0000550 if (BBI.ModifyPredicate)
551 // Now ifcvt'd block will look like this:
552 // BB:
553 // ...
554 // t, f = cmp
555 // if t op
556 // b BBf
557 //
558 // We cannot further ifcvt this block because the unconditional branch will
559 // have to be predicated on the new condition, that will not be available
560 // if cmp executes.
561 IterIfcvt = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000562 }
Evan Chengb6665f62007-06-04 06:47:22 +0000563 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
Evan Chenga6b4f432007-05-21 22:22:58 +0000564
Evan Chenga13aa952007-05-23 07:23:16 +0000565 // Update block info. BB can be iteratively if-converted.
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000566 if (IterIfcvt)
567 BBI.Kind = ICReAnalyze;
Evan Cheng7a655472007-06-06 10:16:17 +0000568 else
569 BBI.Kind = ICDead;
Evan Cheng82582102007-05-30 19:49:19 +0000570 ReTryPreds(BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000571 CvtBBI->Kind = ICDead;
572
573 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000574 return true;
575}
576
Evan Chengd6ddc302007-05-16 21:54:37 +0000577/// IfConvertTriangle - If convert a triangle sub-CFG.
578///
Evan Cheng4e654852007-05-16 02:00:57 +0000579bool IfConverter::IfConvertTriangle(BBInfo &BBI) {
Evan Chenga13aa952007-05-23 07:23:16 +0000580 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Cheng8c529382007-06-01 07:41:07 +0000581
Evan Chenga6b4f432007-05-21 22:22:58 +0000582 // Predicate the 'true' block after removing its branch.
Evan Chenga13aa952007-05-23 07:23:16 +0000583 TrueBBI.NonPredSize -= TII->RemoveBranch(*BBI.TrueBB);
584 PredicateBlock(TrueBBI, BBI.BrCond);
Evan Chenga6b4f432007-05-21 22:22:58 +0000585
Evan Chengb6665f62007-06-04 06:47:22 +0000586 // If 'true' block has a 'false' successor, add an exit branch to it.
Evan Cheng8ed680c2007-06-05 01:31:40 +0000587 bool HasEarlyExit = TrueBBI.FalseBB != NULL;
588 if (HasEarlyExit) {
Evan Cheng8c529382007-06-01 07:41:07 +0000589 std::vector<MachineOperand> RevCond(TrueBBI.BrCond);
590 if (TII->ReverseBranchCondition(RevCond))
591 assert(false && "Unable to reverse branch condition!");
592 TII->InsertBranch(*BBI.TrueBB, TrueBBI.FalseBB, NULL, RevCond);
593 }
594
595 // Join the 'true' and 'false' blocks if the 'false' block has no other
596 // predecessors. Otherwise, add a unconditional branch from 'true' to 'false'.
Evan Chenga6b4f432007-05-21 22:22:58 +0000597 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chengf5305f92007-06-05 00:07:37 +0000598 bool FalseBBDead = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000599 bool IterIfcvt = true;
Evan Cheng8ed680c2007-06-05 01:31:40 +0000600 if (!HasEarlyExit && FalseBBI.BB->pred_size() == 2) {
Evan Cheng8c529382007-06-01 07:41:07 +0000601 MergeBlocks(TrueBBI, FalseBBI);
Evan Chengf5305f92007-06-05 00:07:37 +0000602 FalseBBDead = true;
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000603 } else if (!isNextBlock(TrueBBI.BB, FalseBBI.BB)) {
Evan Cheng8c529382007-06-01 07:41:07 +0000604 InsertUncondBranch(TrueBBI.BB, FalseBBI.BB, TII);
Evan Cheng7a655472007-06-06 10:16:17 +0000605 if (BBI.ModifyPredicate || TrueBBI.ModifyPredicate)
606 // Now ifcvt'd block will look like this:
607 // BB:
608 // ...
609 // t, f = cmp
610 // if t op
611 // b BBf
612 //
613 // We cannot further ifcvt this block because the unconditional branch will
614 // have to be predicated on the new condition, that will not be available
615 // if cmp executes.
616 IterIfcvt = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000617 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000618
619 // Now merge the entry of the triangle with the true block.
Evan Chenga13aa952007-05-23 07:23:16 +0000620 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000621 MergeBlocks(BBI, TrueBBI);
Evan Chenga13aa952007-05-23 07:23:16 +0000622 std::copy(BBI.BrCond.begin(), BBI.BrCond.end(),
623 std::back_inserter(BBI.Predicate));
Evan Chenga6b4f432007-05-21 22:22:58 +0000624
Evan Chenga13aa952007-05-23 07:23:16 +0000625 // Update block info. BB can be iteratively if-converted.
Evan Cheng3d6f60e2007-06-06 02:08:52 +0000626 if (IterIfcvt)
627 BBI.Kind = ICReAnalyze;
Evan Cheng7a655472007-06-06 10:16:17 +0000628 else
629 BBI.Kind = ICDead;
Evan Cheng82582102007-05-30 19:49:19 +0000630 ReTryPreds(BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000631 TrueBBI.Kind = ICDead;
Evan Chengf5305f92007-06-05 00:07:37 +0000632 if (FalseBBDead)
633 FalseBBI.Kind = ICDead;
Evan Chenga6b4f432007-05-21 22:22:58 +0000634
635 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000636 return true;
Evan Cheng4e654852007-05-16 02:00:57 +0000637}
638
Evan Chengd6ddc302007-05-16 21:54:37 +0000639/// IfConvertDiamond - If convert a diamond sub-CFG.
640///
Evan Cheng4e654852007-05-16 02:00:57 +0000641bool IfConverter::IfConvertDiamond(BBInfo &BBI) {
Evan Chengb6665f62007-06-04 06:47:22 +0000642 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +0000643 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +0000644
Evan Chenga6b4f432007-05-21 22:22:58 +0000645 SmallVector<MachineInstr*, 2> Dups;
Evan Chengb6665f62007-06-04 06:47:22 +0000646 if (!BBI.TailBB) {
647 // No common merge block. Check if the terminators (e.g. return) are
648 // the same or predicable.
649 MachineBasicBlock::iterator TT = BBI.TrueBB->getFirstTerminator();
650 MachineBasicBlock::iterator FT = BBI.FalseBB->getFirstTerminator();
651 while (TT != BBI.TrueBB->end() && FT != BBI.FalseBB->end()) {
652 if (TT->isIdenticalTo(FT))
653 Dups.push_back(TT); // Will erase these later.
654 else if (!TT->isPredicable() && !FT->isPredicable())
655 return false; // Can't if-convert. Abort!
656 ++TT;
657 ++FT;
Evan Chengcf6cc112007-05-18 18:14:37 +0000658 }
Evan Chengcf6cc112007-05-18 18:14:37 +0000659
Evan Chengb6665f62007-06-04 06:47:22 +0000660 // One of the two pathes have more terminators, make sure they are
661 // all predicable.
662 while (TT != BBI.TrueBB->end()) {
663 if (!TT->isPredicable()) {
664 return false; // Can't if-convert. Abort!
Evan Cheng4e654852007-05-16 02:00:57 +0000665 }
Evan Chengb6665f62007-06-04 06:47:22 +0000666 ++TT;
667 }
668 while (FT != BBI.FalseBB->end()) {
669 if (!FT->isPredicable()) {
670 return false; // Can't if-convert. Abort!
671 }
672 ++FT;
Evan Cheng4e654852007-05-16 02:00:57 +0000673 }
Evan Cheng4e654852007-05-16 02:00:57 +0000674 }
Evan Chenga6b4f432007-05-21 22:22:58 +0000675
Evan Chenga6b4f432007-05-21 22:22:58 +0000676 // Remove the duplicated instructions from the 'true' block.
677 for (unsigned i = 0, e = Dups.size(); i != e; ++i) {
678 Dups[i]->eraseFromParent();
Evan Chenga13aa952007-05-23 07:23:16 +0000679 --TrueBBI.NonPredSize;
Evan Chenga6b4f432007-05-21 22:22:58 +0000680 }
681
Evan Chenga6b4f432007-05-21 22:22:58 +0000682 // Merge the 'true' and 'false' blocks by copying the instructions
683 // from the 'false' block to the 'true' block. That is, unless the true
684 // block would clobber the predicate, in that case, do the opposite.
Evan Cheng993fc952007-06-05 23:46:14 +0000685 BBInfo *BBI1 = &TrueBBI;
686 BBInfo *BBI2 = &FalseBBI;
Evan Chengb6665f62007-06-04 06:47:22 +0000687 std::vector<MachineOperand> RevCond(BBI.BrCond);
688 TII->ReverseBranchCondition(RevCond);
Evan Cheng993fc952007-06-05 23:46:14 +0000689 std::vector<MachineOperand> *Cond1 = &BBI.BrCond;
690 std::vector<MachineOperand> *Cond2 = &RevCond;
Evan Cheng993fc952007-06-05 23:46:14 +0000691 // Check the 'true' and 'false' blocks if either isn't ended with a branch.
692 // Either the block fallthrough to another block or it ends with a
693 // return. If it's the former, add a branch to its successor.
694 bool NeedBr1 = !BBI1->TrueBB && BBI1->BB->succ_size();
Evan Chenge7052132007-06-06 00:57:55 +0000695 bool NeedBr2 = !BBI2->TrueBB && BBI2->BB->succ_size();
696
697 if ((TrueBBI.ModifyPredicate && !FalseBBI.ModifyPredicate) ||
698 (!TrueBBI.ModifyPredicate && !FalseBBI.ModifyPredicate &&
699 NeedBr1 && !NeedBr2)) {
700 std::swap(BBI1, BBI2);
701 std::swap(Cond1, Cond2);
702 std::swap(NeedBr1, NeedBr2);
703 }
Evan Cheng993fc952007-06-05 23:46:14 +0000704
705 // Predicate the 'true' block after removing its branch.
706 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
707 PredicateBlock(*BBI1, *Cond1);
708
709 // Add an early exit branch if needed.
710 if (NeedBr1)
711 TII->InsertBranch(*BBI1->BB, *BBI1->BB->succ_begin(), NULL, *Cond1);
712
713 // Predicate the 'false' block.
714 PredicateBlock(*BBI2, *Cond2, true);
715
716 // Add an unconditional branch from 'false' to to 'false' successor if it
717 // will not be the fallthrough block.
718 if (NeedBr2 && !isNextBlock(BBI2->BB, *BBI2->BB->succ_begin()))
719 InsertUncondBranch(BBI2->BB, *BBI2->BB->succ_begin(), TII);
720
721 // Keep them as two separate blocks if there is an early exit.
722 if (!NeedBr1)
723 MergeBlocks(*BBI1, *BBI2);
Evan Cheng993fc952007-06-05 23:46:14 +0000724
Evan Chenga6b4f432007-05-21 22:22:58 +0000725 // Remove the conditional branch from entry to the blocks.
Evan Chenga13aa952007-05-23 07:23:16 +0000726 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000727
Evan Cheng993fc952007-06-05 23:46:14 +0000728 // Merge the combined block into the entry of the diamond.
729 MergeBlocks(BBI, *BBI1);
Evan Chenga6b4f432007-05-21 22:22:58 +0000730
Evan Chenge7052132007-06-06 00:57:55 +0000731 // 'True' and 'false' aren't combined, see if we need to add a unconditional
732 // branch to the 'false' block.
733 if (NeedBr1 && !isNextBlock(BBI.BB, BBI2->BB))
734 InsertUncondBranch(BBI1->BB, BBI2->BB, TII);
735
Evan Cheng58fbb9f2007-05-29 23:37:20 +0000736 // If the if-converted block fallthrough or unconditionally branch into the
737 // tail block, and the tail block does not have other predecessors, then
Evan Chenga6b4f432007-05-21 22:22:58 +0000738 // fold the tail block in as well.
Evan Cheng993fc952007-06-05 23:46:14 +0000739 BBInfo *CvtBBI = NeedBr1 ? BBI2 : &BBI;
Evan Cheng58fbb9f2007-05-29 23:37:20 +0000740 if (BBI.TailBB &&
Evan Chengf15d44c2007-05-31 20:53:33 +0000741 BBI.TailBB->pred_size() == 1 && CvtBBI->BB->succ_size() == 1) {
Evan Chenga13aa952007-05-23 07:23:16 +0000742 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
Evan Chenga6b4f432007-05-21 22:22:58 +0000743 BBInfo TailBBI = BBAnalysis[BBI.TailBB->getNumber()];
744 MergeBlocks(*CvtBBI, TailBBI);
745 TailBBI.Kind = ICDead;
746 }
747
Evan Cheng993fc952007-06-05 23:46:14 +0000748 // Update block info.
Evan Cheng7a655472007-06-06 10:16:17 +0000749 BBI.Kind = ICDead;
Evan Chenga6b4f432007-05-21 22:22:58 +0000750 TrueBBI.Kind = ICDead;
751 FalseBBI.Kind = ICDead;
752
753 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +0000754 return true;
Evan Cheng4e654852007-05-16 02:00:57 +0000755}
756
Evan Cheng4e654852007-05-16 02:00:57 +0000757/// PredicateBlock - Predicate every instruction in the block with the specified
758/// condition. If IgnoreTerm is true, skip over all terminator instructions.
Evan Chenga13aa952007-05-23 07:23:16 +0000759void IfConverter::PredicateBlock(BBInfo &BBI,
Evan Cheng4e654852007-05-16 02:00:57 +0000760 std::vector<MachineOperand> &Cond,
761 bool IgnoreTerm) {
Evan Chenga13aa952007-05-23 07:23:16 +0000762 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
Evan Cheng4e654852007-05-16 02:00:57 +0000763 I != E; ++I) {
Evan Chengb6665f62007-06-04 06:47:22 +0000764 if (IgnoreTerm && TII->isTerminatorInstr(I->getOpcode()))
Evan Cheng4e654852007-05-16 02:00:57 +0000765 continue;
Evan Chengb6665f62007-06-04 06:47:22 +0000766 if (TII->isPredicated(I))
Evan Chenga13aa952007-05-23 07:23:16 +0000767 continue;
Evan Chengb6665f62007-06-04 06:47:22 +0000768 if (!TII->PredicateInstruction(I, Cond)) {
Evan Chengfe57a7e2007-06-01 00:55:26 +0000769 cerr << "Unable to predicate " << *I << "!\n";
Evan Chengd6ddc302007-05-16 21:54:37 +0000770 abort();
771 }
Evan Cheng4e654852007-05-16 02:00:57 +0000772 }
Evan Chenga13aa952007-05-23 07:23:16 +0000773
774 BBI.NonPredSize = 0;
Evan Chengb6665f62007-06-04 06:47:22 +0000775 NumIfConvBBs++;
776}
777
778/// TransferPreds - Transfer all the predecessors of FromBB to ToBB.
779///
780static void TransferPreds(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
781 for (MachineBasicBlock::pred_iterator I = FromBB->pred_begin(),
782 E = FromBB->pred_end(); I != E; ++I) {
783 MachineBasicBlock *Pred = *I;
784 Pred->removeSuccessor(FromBB);
785 if (!Pred->isSuccessor(ToBB))
786 Pred->addSuccessor(ToBB);
787 }
788}
789
790/// TransferSuccs - Transfer all the successors of FromBB to ToBB.
791///
792static void TransferSuccs(MachineBasicBlock *ToBB, MachineBasicBlock *FromBB) {
793 for (MachineBasicBlock::succ_iterator I = FromBB->succ_begin(),
794 E = FromBB->succ_end(); I != E; ++I) {
Evan Chengf5305f92007-06-05 00:07:37 +0000795 MachineBasicBlock *Succ = *I;
796 FromBB->removeSuccessor(Succ);
797 if (!ToBB->isSuccessor(Succ))
798 ToBB->addSuccessor(Succ);
Evan Chengb6665f62007-06-04 06:47:22 +0000799 }
Evan Cheng4e654852007-05-16 02:00:57 +0000800}
801
Evan Cheng86cbfea2007-05-18 00:20:58 +0000802/// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
Evan Cheng4e654852007-05-16 02:00:57 +0000803///
Evan Cheng86cbfea2007-05-18 00:20:58 +0000804void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) {
805 ToBBI.BB->splice(ToBBI.BB->end(),
806 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
Evan Chenga13aa952007-05-23 07:23:16 +0000807
808 // If FromBBI is previously a successor, remove it from ToBBI's successor
809 // list and update its TrueBB / FalseBB field if needed.
810 if (ToBBI.BB->isSuccessor(FromBBI.BB))
811 ToBBI.BB->removeSuccessor(FromBBI.BB);
812
Evan Chengb6665f62007-06-04 06:47:22 +0000813 // Redirect all branches to FromBB to ToBB.
Evan Chenga1a9f402007-06-05 22:03:53 +0000814 std::vector<MachineBasicBlock *> Preds(FromBBI.BB->pred_begin(),
815 FromBBI.BB->pred_end());
816 for (unsigned i = 0, e = Preds.size(); i != e; ++i)
817 Preds[i]->ReplaceUsesOfBlockWith(FromBBI.BB, ToBBI.BB);
Evan Chengb6665f62007-06-04 06:47:22 +0000818
Evan Chenga13aa952007-05-23 07:23:16 +0000819 // Transfer preds / succs and update size.
Evan Cheng86cbfea2007-05-18 00:20:58 +0000820 TransferPreds(ToBBI.BB, FromBBI.BB);
Evan Cheng7a655472007-06-06 10:16:17 +0000821 if (!blockFallsThrough(FromBBI.BB))
822 TransferSuccs(ToBBI.BB, FromBBI.BB);
Evan Chenga13aa952007-05-23 07:23:16 +0000823 ToBBI.NonPredSize += FromBBI.NonPredSize;
824 FromBBI.NonPredSize = 0;
Evan Cheng7a655472007-06-06 10:16:17 +0000825
826 ToBBI.ModifyPredicate |= FromBBI.ModifyPredicate;
Evan Cheng4e654852007-05-16 02:00:57 +0000827}