blob: f4485d5d9a2028ddd2f040ce30191a8b28f2506f [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng4e654852007-05-16 02:00:57 +00007//
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"
Evan Cheng4e654852007-05-16 02:00:57 +000015#include "llvm/CodeGen/Passes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "BranchFolding.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallSet.h"
19#include "llvm/ADT/Statistic.h"
Jakub Staszak07672672011-08-03 22:53:41 +000020#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Evan Cheng4e654852007-05-16 02:00:57 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Evan Cheng791e2e02012-06-08 21:53:50 +000024#include "llvm/CodeGen/MachineRegisterInfo.h"
Evan Chengab8be962011-06-29 01:14:12 +000025#include "llvm/MC/MCInstrItineraries.h"
Evan Chengedf48962007-06-08 19:10:51 +000026#include "llvm/Support/CommandLine.h"
Evan Cheng4e654852007-05-16 02:00:57 +000027#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000028#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/Target/TargetInstrInfo.h"
31#include "llvm/Target/TargetLowering.h"
32#include "llvm/Target/TargetMachine.h"
33#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng4e654852007-05-16 02:00:57 +000034using namespace llvm;
35
Chris Lattnerf86e1df2008-01-07 05:40:58 +000036// Hidden options for help debugging.
37static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
38static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
39static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000040static cl::opt<bool> DisableSimple("disable-ifcvt-simple",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000041 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000042static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000043 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000044static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000045 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000046static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000047 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000048static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000049 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000050static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000051 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000052static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000053 cl::init(false), cl::Hidden);
Evan Cheng46df4eb2010-06-16 07:35:02 +000054static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold",
55 cl::init(true), cl::Hidden);
Evan Chengedf48962007-06-08 19:10:51 +000056
Evan Cheng1c9f91d2007-06-09 01:03:43 +000057STATISTIC(NumSimple, "Number of simple if-conversions performed");
58STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
59STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
Evan Chengcc8fb462007-06-12 23:54:05 +000060STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed");
Evan Cheng1c9f91d2007-06-09 01:03:43 +000061STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
62STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
63STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
64STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
Evan Cheng2c8c3a42007-06-15 07:36:12 +000065STATISTIC(NumDupBBs, "Number of duplicated blocks");
Evan Cheng8787c5f2011-12-19 22:01:30 +000066STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated");
Evan Cheng4e654852007-05-16 02:00:57 +000067
68namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000069 class IfConverter : public MachineFunctionPass {
Evan Chenge882fca2007-06-16 09:34:52 +000070 enum IfcvtKind {
Evan Cheng4e654852007-05-16 02:00:57 +000071 ICNotClassfied, // BB data valid, but not classified.
Evan Chengb6665f62007-06-04 06:47:22 +000072 ICSimpleFalse, // Same as ICSimple, but on the false path.
Evan Chenge882fca2007-06-16 09:34:52 +000073 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
74 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
Evan Chengcc8fb462007-06-12 23:54:05 +000075 ICTriangleRev, // Same as ICTriangle, but true path rev condition.
Evan Cheng1c9f91d2007-06-09 01:03:43 +000076 ICTriangleFalse, // Same as ICTriangle, but on the false path.
Evan Chenge882fca2007-06-16 09:34:52 +000077 ICTriangle, // BB is entry of a triangle sub-CFG.
Evan Cheng9618bca2007-06-11 22:26:22 +000078 ICDiamond // BB is entry of a diamond sub-CFG.
Evan Cheng4e654852007-05-16 02:00:57 +000079 };
80
81 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
82 /// if-conversion feasibility analysis. This includes results from
83 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
Evan Cheng86cbfea2007-05-18 00:20:58 +000084 /// classification, and common tail block of its successors (if it's a
Evan Chengcf6cc112007-05-18 18:14:37 +000085 /// diamond shape), its size, whether it's predicable, and whether any
86 /// instruction can clobber the 'would-be' predicate.
Evan Chenga13aa952007-05-23 07:23:16 +000087 ///
Evan Cheng9618bca2007-06-11 22:26:22 +000088 /// IsDone - True if BB is not to be considered for ifcvt.
89 /// IsBeingAnalyzed - True if BB is currently being analyzed.
90 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
91 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
92 /// IsBrAnalyzable - True if AnalyzeBranch() returns false.
93 /// HasFallThrough - True if BB may fallthrough to the following BB.
94 /// IsUnpredicable - True if BB is known to be unpredicable.
Evan Cheng11ce02d2007-07-10 17:50:43 +000095 /// ClobbersPred - True if BB could modify predicates (e.g. has
Evan Cheng7a655472007-06-06 10:16:17 +000096 /// cmp, call, etc.)
Evan Cheng9618bca2007-06-11 22:26:22 +000097 /// NonPredSize - Number of non-predicated instructions.
Evan Cheng8239daf2010-11-03 00:45:17 +000098 /// ExtraCost - Extra cost for multi-cycle instructions.
99 /// ExtraCost2 - Some instructions are slower when predicated
Evan Chenga13aa952007-05-23 07:23:16 +0000100 /// BB - Corresponding MachineBasicBlock.
101 /// TrueBB / FalseBB- See AnalyzeBranch().
102 /// BrCond - Conditions for end of block conditional branches.
103 /// Predicate - Predicate used in the BB.
Evan Cheng4e654852007-05-16 02:00:57 +0000104 struct BBInfo {
Evan Cheng9618bca2007-06-11 22:26:22 +0000105 bool IsDone : 1;
106 bool IsBeingAnalyzed : 1;
107 bool IsAnalyzed : 1;
108 bool IsEnqueued : 1;
109 bool IsBrAnalyzable : 1;
110 bool HasFallThrough : 1;
111 bool IsUnpredicable : 1;
Evan Chenga2acf842007-06-15 21:18:05 +0000112 bool CannotBeCopied : 1;
Evan Cheng9618bca2007-06-11 22:26:22 +0000113 bool ClobbersPred : 1;
Evan Chenga13aa952007-05-23 07:23:16 +0000114 unsigned NonPredSize;
Bob Wilson2ad40d32010-10-26 00:02:21 +0000115 unsigned ExtraCost;
Evan Cheng8239daf2010-11-03 00:45:17 +0000116 unsigned ExtraCost2;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000117 MachineBasicBlock *BB;
118 MachineBasicBlock *TrueBB;
119 MachineBasicBlock *FalseBB;
Owen Anderson44eb65c2008-08-14 22:49:33 +0000120 SmallVector<MachineOperand, 4> BrCond;
121 SmallVector<MachineOperand, 4> Predicate;
Evan Chenge882fca2007-06-16 09:34:52 +0000122 BBInfo() : IsDone(false), IsBeingAnalyzed(false),
Evan Cheng9618bca2007-06-11 22:26:22 +0000123 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
124 HasFallThrough(false), IsUnpredicable(false),
Evan Chenga2acf842007-06-15 21:18:05 +0000125 CannotBeCopied(false), ClobbersPred(false), NonPredSize(0),
Evan Cheng8239daf2010-11-03 00:45:17 +0000126 ExtraCost(0), ExtraCost2(0), BB(0), TrueBB(0), FalseBB(0) {}
Evan Chenge882fca2007-06-16 09:34:52 +0000127 };
128
Bob Wilson669db042010-06-15 18:19:27 +0000129 /// IfcvtToken - Record information about pending if-conversions to attempt:
Evan Chenge882fca2007-06-16 09:34:52 +0000130 /// BBI - Corresponding BBInfo.
131 /// Kind - Type of block. See IfcvtKind.
Bob Wilson9c4856a2009-05-13 23:25:24 +0000132 /// NeedSubsumption - True if the to-be-predicated BB has already been
Evan Chenge882fca2007-06-16 09:34:52 +0000133 /// predicated.
Evan Chenga1a87872007-06-18 08:37:25 +0000134 /// NumDups - Number of instructions that would be duplicated due
135 /// to this if-conversion. (For diamonds, the number of
136 /// identical instructions at the beginnings of both
137 /// paths).
138 /// NumDups2 - For diamonds, the number of identical instructions
139 /// at the ends of both paths.
Evan Chenge882fca2007-06-16 09:34:52 +0000140 struct IfcvtToken {
141 BBInfo &BBI;
142 IfcvtKind Kind;
Bob Wilson9c4856a2009-05-13 23:25:24 +0000143 bool NeedSubsumption;
Evan Chenga1a87872007-06-18 08:37:25 +0000144 unsigned NumDups;
145 unsigned NumDups2;
146 IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0)
Bob Wilson9c4856a2009-05-13 23:25:24 +0000147 : BBI(b), Kind(k), NeedSubsumption(s), NumDups(d), NumDups2(d2) {}
Evan Cheng4e654852007-05-16 02:00:57 +0000148 };
149
150 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
151 /// basic block number.
152 std::vector<BBInfo> BBAnalysis;
153
Benjamin Kramer69e42db2013-01-11 20:05:37 +0000154 const TargetLoweringBase *TLI;
Evan Cheng4e654852007-05-16 02:00:57 +0000155 const TargetInstrInfo *TII;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000156 const TargetRegisterInfo *TRI;
Evan Cheng3ef1c872010-09-10 01:29:16 +0000157 const InstrItineraryData *InstrItins;
Jakub Staszak990f78d2011-08-03 22:34:43 +0000158 const MachineBranchProbabilityInfo *MBPI;
Evan Cheng791e2e02012-06-08 21:53:50 +0000159 MachineRegisterInfo *MRI;
Jakub Staszak990f78d2011-08-03 22:34:43 +0000160
Evan Cheng791e2e02012-06-08 21:53:50 +0000161 bool PreRegAlloc;
Evan Cheng4e654852007-05-16 02:00:57 +0000162 bool MadeChange;
Owen Anderson13bbe4b2009-06-24 23:41:44 +0000163 int FnNum;
Evan Cheng4e654852007-05-16 02:00:57 +0000164 public:
165 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000166 IfConverter() : MachineFunctionPass(ID), FnNum(-1) {
167 initializeIfConverterPass(*PassRegistry::getPassRegistry());
168 }
Jakub Staszak990f78d2011-08-03 22:34:43 +0000169
Owen Anderson7571ee72010-09-28 20:42:15 +0000170 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak990f78d2011-08-03 22:34:43 +0000171 AU.addRequired<MachineBranchProbabilityInfo>();
Owen Anderson7571ee72010-09-28 20:42:15 +0000172 MachineFunctionPass::getAnalysisUsage(AU);
173 }
Evan Cheng4e654852007-05-16 02:00:57 +0000174
175 virtual bool runOnMachineFunction(MachineFunction &MF);
Evan Cheng4e654852007-05-16 02:00:57 +0000176
177 private:
Evan Chengb6665f62007-06-04 06:47:22 +0000178 bool ReverseBranchCondition(BBInfo &BBI);
Owen Andersone3cc84a2010-10-01 22:45:50 +0000179 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000180 const BranchProbability &Prediction) const;
Evan Chengac5f1422007-06-08 09:36:04 +0000181 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
Owen Andersone3cc84a2010-10-01 22:45:50 +0000182 bool FalseBranch, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000183 const BranchProbability &Prediction) const;
Evan Chenga1a87872007-06-18 08:37:25 +0000184 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
185 unsigned &Dups1, unsigned &Dups2) const;
Evan Chengac5f1422007-06-08 09:36:04 +0000186 void ScanInstructions(BBInfo &BBI);
Evan Chenge882fca2007-06-16 09:34:52 +0000187 BBInfo &AnalyzeBlock(MachineBasicBlock *BB,
188 std::vector<IfcvtToken*> &Tokens);
Owen Anderson44eb65c2008-08-14 22:49:33 +0000189 bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond,
Evan Chengac5f1422007-06-08 09:36:04 +0000190 bool isTriangle = false, bool RevBranch = false);
Bob Wilson6fb124b2010-06-15 18:57:15 +0000191 void AnalyzeBlocks(MachineFunction &MF, std::vector<IfcvtToken*> &Tokens);
Evan Chenga1a87872007-06-18 08:37:25 +0000192 void InvalidatePreds(MachineBasicBlock *BB);
Evan Cheng7e75ba82007-06-08 22:01:07 +0000193 void RemoveExtraEdges(BBInfo &BBI);
Evan Chenge882fca2007-06-16 09:34:52 +0000194 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
195 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
Evan Chenga1a87872007-06-18 08:37:25 +0000196 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
197 unsigned NumDups1, unsigned NumDups2);
Evan Chenga13aa952007-05-23 07:23:16 +0000198 void PredicateBlock(BBInfo &BBI,
Evan Chenga1a87872007-06-18 08:37:25 +0000199 MachineBasicBlock::iterator E,
Evan Cheng46df4eb2010-06-16 07:35:02 +0000200 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng8787c5f2011-12-19 22:01:30 +0000201 SmallSet<unsigned, 4> &Redefs,
202 SmallSet<unsigned, 4> *LaterRedefs = 0);
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000203 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000204 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng46df4eb2010-06-16 07:35:02 +0000205 SmallSet<unsigned, 4> &Redefs,
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000206 bool IgnoreBr = false);
Bob Wilson8eab75f2010-06-29 00:55:23 +0000207 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
Evan Cheng5f702182007-06-01 00:12:12 +0000208
Evan Cheng8239daf2010-11-03 00:45:17 +0000209 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
210 unsigned Cycle, unsigned Extra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000211 const BranchProbability &Prediction) const {
Evan Cheng8239daf2010-11-03 00:45:17 +0000212 return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000213 Prediction);
Evan Cheng13151432010-06-25 22:42:03 +0000214 }
215
Evan Cheng8239daf2010-11-03 00:45:17 +0000216 bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB,
217 unsigned TCycle, unsigned TExtra,
218 MachineBasicBlock &FBB,
219 unsigned FCycle, unsigned FExtra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000220 const BranchProbability &Prediction) const {
Evan Cheng8239daf2010-11-03 00:45:17 +0000221 return TCycle > 0 && FCycle > 0 &&
222 TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000223 Prediction);
Evan Chenga1a87872007-06-18 08:37:25 +0000224 }
225
Evan Chengd4de6d92007-06-07 02:12:15 +0000226 // blockAlwaysFallThrough - Block ends without a terminator.
227 bool blockAlwaysFallThrough(BBInfo &BBI) const {
Evan Cheng9618bca2007-06-11 22:26:22 +0000228 return BBI.IsBrAnalyzable && BBI.TrueBB == NULL;
Evan Cheng7a655472007-06-06 10:16:17 +0000229 }
230
Evan Chenge882fca2007-06-16 09:34:52 +0000231 // IfcvtTokenCmp - Used to sort if-conversion candidates.
232 static bool IfcvtTokenCmp(IfcvtToken *C1, IfcvtToken *C2) {
Evan Chenga1a87872007-06-18 08:37:25 +0000233 int Incr1 = (C1->Kind == ICDiamond)
234 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
235 int Incr2 = (C2->Kind == ICDiamond)
236 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
237 if (Incr1 > Incr2)
Evan Chenge882fca2007-06-16 09:34:52 +0000238 return true;
Evan Chenga1a87872007-06-18 08:37:25 +0000239 else if (Incr1 == Incr2) {
Bob Wilson9c4856a2009-05-13 23:25:24 +0000240 // Favors subsumption.
241 if (C1->NeedSubsumption == false && C2->NeedSubsumption == true)
Evan Chenge882fca2007-06-16 09:34:52 +0000242 return true;
Bob Wilson9c4856a2009-05-13 23:25:24 +0000243 else if (C1->NeedSubsumption == C2->NeedSubsumption) {
Evan Chenge882fca2007-06-16 09:34:52 +0000244 // Favors diamond over triangle, etc.
245 if ((unsigned)C1->Kind < (unsigned)C2->Kind)
246 return true;
247 else if (C1->Kind == C2->Kind)
248 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
249 }
250 }
251 return false;
Evan Cheng5f702182007-06-01 00:12:12 +0000252 }
Evan Cheng4e654852007-05-16 02:00:57 +0000253 };
Evan Chenge882fca2007-06-16 09:34:52 +0000254
Evan Cheng4e654852007-05-16 02:00:57 +0000255 char IfConverter::ID = 0;
256}
257
Andrew Trick1dd8c852012-02-08 21:23:13 +0000258char &llvm::IfConverterID = IfConverter::ID;
259
Owen Anderson2ab36d32010-10-12 19:48:12 +0000260INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false)
Jakub Staszak990f78d2011-08-03 22:34:43 +0000261INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Owen Anderson2ab36d32010-10-12 19:48:12 +0000262INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false)
Bob Wilsona5971032009-10-28 20:46:46 +0000263
Evan Cheng4e654852007-05-16 02:00:57 +0000264bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng86cbfea2007-05-18 00:20:58 +0000265 TLI = MF.getTarget().getTargetLowering();
Evan Cheng4e654852007-05-16 02:00:57 +0000266 TII = MF.getTarget().getInstrInfo();
Evan Cheng46df4eb2010-06-16 07:35:02 +0000267 TRI = MF.getTarget().getRegisterInfo();
Jakub Staszak990f78d2011-08-03 22:34:43 +0000268 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Evan Cheng791e2e02012-06-08 21:53:50 +0000269 MRI = &MF.getRegInfo();
Evan Cheng3ef1c872010-09-10 01:29:16 +0000270 InstrItins = MF.getTarget().getInstrItineraryData();
Evan Cheng4e654852007-05-16 02:00:57 +0000271 if (!TII) return false;
272
Evan Cheng791e2e02012-06-08 21:53:50 +0000273 PreRegAlloc = MRI->isSSA();
274
275 bool BFChange = false;
276 if (!PreRegAlloc) {
277 // Tail merge tend to expose more if-conversion opportunities.
278 BranchFolder BF(true, false);
279 BFChange = BF.OptimizeFunction(MF, TII,
Evan Cheng86050dc2010-06-18 23:09:54 +0000280 MF.getTarget().getRegisterInfo(),
281 getAnalysisIfAvailable<MachineModuleInfo>());
Evan Cheng791e2e02012-06-08 21:53:50 +0000282 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000283
David Greene083b7ff2010-01-04 22:02:01 +0000284 DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'"
Craig Topper96601ca2012-08-22 06:07:19 +0000285 << MF.getName() << "\'");
Evan Chengedf48962007-06-08 19:10:51 +0000286
287 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
David Greene083b7ff2010-01-04 22:02:01 +0000288 DEBUG(dbgs() << " skipped\n");
Evan Chengedf48962007-06-08 19:10:51 +0000289 return false;
290 }
David Greene083b7ff2010-01-04 22:02:01 +0000291 DEBUG(dbgs() << "\n");
Evan Cheng5f702182007-06-01 00:12:12 +0000292
Evan Cheng4e654852007-05-16 02:00:57 +0000293 MF.RenumberBlocks();
Evan Cheng5f702182007-06-01 00:12:12 +0000294 BBAnalysis.resize(MF.getNumBlockIDs());
Evan Cheng4e654852007-05-16 02:00:57 +0000295
Evan Chenge882fca2007-06-16 09:34:52 +0000296 std::vector<IfcvtToken*> Tokens;
Evan Cheng47d25022007-05-18 01:55:58 +0000297 MadeChange = false;
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000298 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
299 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
300 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
Bob Wilson9c4856a2009-05-13 23:25:24 +0000301 // Do an initial analysis for each basic block and find all the potential
302 // candidates to perform if-conversion.
Bob Wilson6fb124b2010-06-15 18:57:15 +0000303 bool Change = false;
304 AnalyzeBlocks(MF, Tokens);
Evan Chenge882fca2007-06-16 09:34:52 +0000305 while (!Tokens.empty()) {
306 IfcvtToken *Token = Tokens.back();
307 Tokens.pop_back();
308 BBInfo &BBI = Token->BBI;
309 IfcvtKind Kind = Token->Kind;
Nuno Lopes7ecbfd12008-11-04 13:02:59 +0000310 unsigned NumDups = Token->NumDups;
Duncan Sands20d629c2008-11-04 18:05:30 +0000311 unsigned NumDups2 = Token->NumDups2;
Nuno Lopes7ecbfd12008-11-04 13:02:59 +0000312
313 delete Token;
Evan Chengb6665f62007-06-04 06:47:22 +0000314
Evan Cheng9618bca2007-06-11 22:26:22 +0000315 // If the block has been evicted out of the queue or it has already been
316 // marked dead (due to it being predicated), then skip it.
Evan Chenge882fca2007-06-16 09:34:52 +0000317 if (BBI.IsDone)
318 BBI.IsEnqueued = false;
319 if (!BBI.IsEnqueued)
Evan Cheng9618bca2007-06-11 22:26:22 +0000320 continue;
Evan Chenge882fca2007-06-16 09:34:52 +0000321
Evan Cheng86ff2962007-06-14 20:28:52 +0000322 BBI.IsEnqueued = false;
Evan Cheng9618bca2007-06-11 22:26:22 +0000323
Evan Chengb6665f62007-06-04 06:47:22 +0000324 bool RetVal = false;
Evan Chenge882fca2007-06-16 09:34:52 +0000325 switch (Kind) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000326 default: llvm_unreachable("Unexpected!");
Evan Chengb6665f62007-06-04 06:47:22 +0000327 case ICSimple:
Evan Chengcb78d672007-06-06 01:12:44 +0000328 case ICSimpleFalse: {
Evan Chenge882fca2007-06-16 09:34:52 +0000329 bool isFalse = Kind == ICSimpleFalse;
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000330 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
Bob Wilson92fffe02010-06-15 22:18:54 +0000331 DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ?
332 " false" : "")
Bill Wendling44ff7942009-08-22 20:11:17 +0000333 << "): BB#" << BBI.BB->getNumber() << " ("
334 << ((Kind == ICSimpleFalse)
335 ? BBI.FalseBB->getNumber()
336 : BBI.TrueBB->getNumber()) << ") ");
Evan Chenge882fca2007-06-16 09:34:52 +0000337 RetVal = IfConvertSimple(BBI, Kind);
David Greene083b7ff2010-01-04 22:02:01 +0000338 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000339 if (RetVal) {
Dan Gohmanfe601042010-06-22 15:08:57 +0000340 if (isFalse) ++NumSimpleFalse;
341 else ++NumSimple;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000342 }
Evan Chengb6665f62007-06-04 06:47:22 +0000343 break;
Evan Chengcb78d672007-06-06 01:12:44 +0000344 }
Evan Chenga13aa952007-05-23 07:23:16 +0000345 case ICTriangle:
Evan Chengcc8fb462007-06-12 23:54:05 +0000346 case ICTriangleRev:
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000347 case ICTriangleFalse:
Reid Spencera9bf49c2007-06-10 00:19:17 +0000348 case ICTriangleFRev: {
Evan Chenge882fca2007-06-16 09:34:52 +0000349 bool isFalse = Kind == ICTriangleFalse;
350 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
Evan Chengcc8fb462007-06-12 23:54:05 +0000351 if (DisableTriangle && !isFalse && !isRev) break;
352 if (DisableTriangleR && !isFalse && isRev) break;
353 if (DisableTriangleF && isFalse && !isRev) break;
354 if (DisableTriangleFR && isFalse && isRev) break;
David Greene083b7ff2010-01-04 22:02:01 +0000355 DEBUG(dbgs() << "Ifcvt (Triangle");
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000356 if (isFalse)
David Greene083b7ff2010-01-04 22:02:01 +0000357 DEBUG(dbgs() << " false");
Evan Chengcc8fb462007-06-12 23:54:05 +0000358 if (isRev)
David Greene083b7ff2010-01-04 22:02:01 +0000359 DEBUG(dbgs() << " rev");
360 DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendling44ff7942009-08-22 20:11:17 +0000361 << BBI.TrueBB->getNumber() << ",F:"
362 << BBI.FalseBB->getNumber() << ") ");
Evan Chenge882fca2007-06-16 09:34:52 +0000363 RetVal = IfConvertTriangle(BBI, Kind);
David Greene083b7ff2010-01-04 22:02:01 +0000364 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000365 if (RetVal) {
Evan Chengcc8fb462007-06-12 23:54:05 +0000366 if (isFalse) {
Dan Gohmanfe601042010-06-22 15:08:57 +0000367 if (isRev) ++NumTriangleFRev;
368 else ++NumTriangleFalse;
Evan Chengcc8fb462007-06-12 23:54:05 +0000369 } else {
Dan Gohmanfe601042010-06-22 15:08:57 +0000370 if (isRev) ++NumTriangleRev;
371 else ++NumTriangle;
Evan Chengcc8fb462007-06-12 23:54:05 +0000372 }
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000373 }
Evan Chenga13aa952007-05-23 07:23:16 +0000374 break;
Reid Spencera9bf49c2007-06-10 00:19:17 +0000375 }
Evan Chenge882fca2007-06-16 09:34:52 +0000376 case ICDiamond: {
Evan Chengedf48962007-06-08 19:10:51 +0000377 if (DisableDiamond) break;
David Greene083b7ff2010-01-04 22:02:01 +0000378 DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendling44ff7942009-08-22 20:11:17 +0000379 << BBI.TrueBB->getNumber() << ",F:"
380 << BBI.FalseBB->getNumber() << ") ");
Nuno Lopes7ecbfd12008-11-04 13:02:59 +0000381 RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2);
David Greene083b7ff2010-01-04 22:02:01 +0000382 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Dan Gohmanfe601042010-06-22 15:08:57 +0000383 if (RetVal) ++NumDiamonds;
Evan Chenga13aa952007-05-23 07:23:16 +0000384 break;
385 }
Evan Chenge882fca2007-06-16 09:34:52 +0000386 }
387
Evan Chengb6665f62007-06-04 06:47:22 +0000388 Change |= RetVal;
Evan Chengedf48962007-06-08 19:10:51 +0000389
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000390 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
391 NumTriangleFalse + NumTriangleFRev + NumDiamonds;
392 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
Evan Chengedf48962007-06-08 19:10:51 +0000393 break;
Evan Cheng4e654852007-05-16 02:00:57 +0000394 }
Evan Chenga13aa952007-05-23 07:23:16 +0000395
Evan Chenga13aa952007-05-23 07:23:16 +0000396 if (!Change)
397 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000398 MadeChange |= Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000399 }
Evan Cheng47d25022007-05-18 01:55:58 +0000400
Evan Chenge882fca2007-06-16 09:34:52 +0000401 // Delete tokens in case of early exit.
402 while (!Tokens.empty()) {
403 IfcvtToken *Token = Tokens.back();
404 Tokens.pop_back();
405 delete Token;
406 }
407
408 Tokens.clear();
Evan Cheng47d25022007-05-18 01:55:58 +0000409 BBAnalysis.clear();
410
Evan Cheng6a5e2832010-06-18 22:17:13 +0000411 if (MadeChange && IfCvtBranchFold) {
Evan Chengcbc988b2011-05-12 00:56:58 +0000412 BranchFolder BF(false, false);
Evan Cheng030a0a02009-09-04 07:47:40 +0000413 BF.OptimizeFunction(MF, TII,
414 MF.getTarget().getRegisterInfo(),
415 getAnalysisIfAvailable<MachineModuleInfo>());
416 }
417
Evan Cheng86050dc2010-06-18 23:09:54 +0000418 MadeChange |= BFChange;
Evan Cheng4e654852007-05-16 02:00:57 +0000419 return MadeChange;
420}
421
Evan Chenge882fca2007-06-16 09:34:52 +0000422/// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
423/// its 'true' successor.
Evan Cheng4e654852007-05-16 02:00:57 +0000424static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng86cbfea2007-05-18 00:20:58 +0000425 MachineBasicBlock *TrueBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000426 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
427 E = BB->succ_end(); SI != E; ++SI) {
428 MachineBasicBlock *SuccBB = *SI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000429 if (SuccBB != TrueBB)
Evan Cheng4e654852007-05-16 02:00:57 +0000430 return SuccBB;
431 }
432 return NULL;
433}
434
Evan Chenge882fca2007-06-16 09:34:52 +0000435/// ReverseBranchCondition - Reverse the condition of the end of the block
Bob Wilson9c4856a2009-05-13 23:25:24 +0000436/// branch. Swap block's 'true' and 'false' successors.
Evan Chengb6665f62007-06-04 06:47:22 +0000437bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
Stuart Hastings3bf91252010-06-17 22:43:56 +0000438 DebugLoc dl; // FIXME: this is nowhere
Evan Chengb6665f62007-06-04 06:47:22 +0000439 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
440 TII->RemoveBranch(*BBI.BB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000441 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl);
Evan Chengb6665f62007-06-04 06:47:22 +0000442 std::swap(BBI.TrueBB, BBI.FalseBB);
443 return true;
444 }
445 return false;
446}
447
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000448/// getNextBlock - Returns the next block in the function blocks ordering. If
449/// it is the end, returns NULL.
450static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) {
451 MachineFunction::iterator I = BB;
452 MachineFunction::iterator E = BB->getParent()->end();
453 if (++I == E)
454 return NULL;
455 return I;
456}
457
Evan Chengac5f1422007-06-08 09:36:04 +0000458/// ValidSimple - Returns true if the 'true' block (along with its
Evan Chenge882fca2007-06-16 09:34:52 +0000459/// predecessor) forms a valid simple shape for ifcvt. It also returns the
460/// number of instructions that the ifcvt would need to duplicate if performed
461/// in Dups.
Owen Anderson7571ee72010-09-28 20:42:15 +0000462bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000463 const BranchProbability &Prediction) const {
Evan Chenge882fca2007-06-16 09:34:52 +0000464 Dups = 0;
Evan Chenga1a87872007-06-18 08:37:25 +0000465 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng9618bca2007-06-11 22:26:22 +0000466 return false;
467
Evan Cheng9ffd3402007-06-19 21:45:13 +0000468 if (TrueBBI.IsBrAnalyzable)
469 return false;
470
Evan Chenga2acf842007-06-15 21:18:05 +0000471 if (TrueBBI.BB->pred_size() > 1) {
472 if (TrueBBI.CannotBeCopied ||
Owen Anderson7571ee72010-09-28 20:42:15 +0000473 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000474 Prediction))
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000475 return false;
Evan Chenge882fca2007-06-16 09:34:52 +0000476 Dups = TrueBBI.NonPredSize;
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000477 }
478
Evan Cheng9ffd3402007-06-19 21:45:13 +0000479 return true;
Evan Cheng7a655472007-06-06 10:16:17 +0000480}
481
Evan Chengac5f1422007-06-08 09:36:04 +0000482/// ValidTriangle - Returns true if the 'true' and 'false' blocks (along
Evan Chengd4de6d92007-06-07 02:12:15 +0000483/// with their common predecessor) forms a valid triangle shape for ifcvt.
Evan Chenge882fca2007-06-16 09:34:52 +0000484/// If 'FalseBranch' is true, it checks if 'true' block's false branch
Bob Wilson92fffe02010-06-15 22:18:54 +0000485/// branches to the 'false' block rather than the other way around. It also
Evan Chenge882fca2007-06-16 09:34:52 +0000486/// returns the number of instructions that the ifcvt would need to duplicate
487/// if performed in 'Dups'.
Evan Chengac5f1422007-06-08 09:36:04 +0000488bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
Owen Andersone3cc84a2010-10-01 22:45:50 +0000489 bool FalseBranch, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000490 const BranchProbability &Prediction) const {
Evan Chenge882fca2007-06-16 09:34:52 +0000491 Dups = 0;
Evan Chenga1a87872007-06-18 08:37:25 +0000492 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng9618bca2007-06-11 22:26:22 +0000493 return false;
494
Evan Chenga2acf842007-06-15 21:18:05 +0000495 if (TrueBBI.BB->pred_size() > 1) {
496 if (TrueBBI.CannotBeCopied)
497 return false;
498
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000499 unsigned Size = TrueBBI.NonPredSize;
Evan Chenge882fca2007-06-16 09:34:52 +0000500 if (TrueBBI.IsBrAnalyzable) {
Dan Gohman30359592008-01-29 13:02:09 +0000501 if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
Bob Wilson9c4856a2009-05-13 23:25:24 +0000502 // Ends with an unconditional branch. It will be removed.
Evan Chenge882fca2007-06-16 09:34:52 +0000503 --Size;
504 else {
505 MachineBasicBlock *FExit = FalseBranch
506 ? TrueBBI.TrueBB : TrueBBI.FalseBB;
507 if (FExit)
508 // Require a conditional branch
509 ++Size;
510 }
511 }
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000512 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction))
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000513 return false;
Evan Chenge882fca2007-06-16 09:34:52 +0000514 Dups = Size;
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000515 }
Evan Chengd4de6d92007-06-07 02:12:15 +0000516
Evan Chengac5f1422007-06-08 09:36:04 +0000517 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
518 if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
Evan Chengd4de6d92007-06-07 02:12:15 +0000519 MachineFunction::iterator I = TrueBBI.BB;
520 if (++I == TrueBBI.BB->getParent()->end())
521 return false;
Evan Chengac5f1422007-06-08 09:36:04 +0000522 TExit = I;
Evan Chengd4de6d92007-06-07 02:12:15 +0000523 }
Evan Chengac5f1422007-06-08 09:36:04 +0000524 return TExit && TExit == FalseBBI.BB;
Evan Chengd4de6d92007-06-07 02:12:15 +0000525}
526
Evan Chengac5f1422007-06-08 09:36:04 +0000527/// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
Evan Chengd4de6d92007-06-07 02:12:15 +0000528/// with their common predecessor) forms a valid diamond shape for ifcvt.
Evan Chenga1a87872007-06-18 08:37:25 +0000529bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
530 unsigned &Dups1, unsigned &Dups2) const {
531 Dups1 = Dups2 = 0;
532 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
533 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
Evan Cheng9618bca2007-06-11 22:26:22 +0000534 return false;
535
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000536 MachineBasicBlock *TT = TrueBBI.TrueBB;
537 MachineBasicBlock *FT = FalseBBI.TrueBB;
538
539 if (!TT && blockAlwaysFallThrough(TrueBBI))
540 TT = getNextBlock(TrueBBI.BB);
541 if (!FT && blockAlwaysFallThrough(FalseBBI))
542 FT = getNextBlock(FalseBBI.BB);
543 if (TT != FT)
544 return false;
Evan Cheng9618bca2007-06-11 22:26:22 +0000545 if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000546 return false;
Evan Chengc4047a82007-06-18 22:44:57 +0000547 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
548 return false;
549
550 // FIXME: Allow true block to have an early exit?
551 if (TrueBBI.FalseBB || FalseBBI.FalseBB ||
552 (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred))
Evan Chenga1a87872007-06-18 08:37:25 +0000553 return false;
554
Bob Wilson7c730e72010-10-26 00:02:24 +0000555 // Count duplicate instructions at the beginning of the true and false blocks.
Jim Grosbach66f360e2010-06-07 21:28:55 +0000556 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
557 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
Bob Wilson7c730e72010-10-26 00:02:24 +0000558 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
559 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
560 while (TIB != TIE && FIB != FIE) {
Evan Chenga9934dc2010-06-18 21:52:57 +0000561 // Skip dbg_value instructions. These do not count.
Bob Wilson7c730e72010-10-26 00:02:24 +0000562 if (TIB->isDebugValue()) {
563 while (TIB != TIE && TIB->isDebugValue())
564 ++TIB;
565 if (TIB == TIE)
Evan Chenga9934dc2010-06-18 21:52:57 +0000566 break;
567 }
Bob Wilson7c730e72010-10-26 00:02:24 +0000568 if (FIB->isDebugValue()) {
569 while (FIB != FIE && FIB->isDebugValue())
570 ++FIB;
571 if (FIB == FIE)
Evan Chenga9934dc2010-06-18 21:52:57 +0000572 break;
573 }
Bob Wilson7c730e72010-10-26 00:02:24 +0000574 if (!TIB->isIdenticalTo(FIB))
575 break;
576 ++Dups1;
577 ++TIB;
578 ++FIB;
579 }
580
581 // Now, in preparation for counting duplicate instructions at the ends of the
582 // blocks, move the end iterators up past any branch instructions.
583 while (TIE != TIB) {
584 --TIE;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000585 if (!TIE->isBranch())
Bob Wilson7c730e72010-10-26 00:02:24 +0000586 break;
587 }
588 while (FIE != FIB) {
589 --FIE;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000590 if (!FIE->isBranch())
Bob Wilson7c730e72010-10-26 00:02:24 +0000591 break;
592 }
593
594 // If Dups1 includes all of a block, then don't count duplicate
595 // instructions at the end of the blocks.
596 if (TIB == TIE || FIB == FIE)
597 return true;
598
599 // Count duplicate instructions at the ends of the blocks.
600 while (TIE != TIB && FIE != FIB) {
601 // Skip dbg_value instructions. These do not count.
602 if (TIE->isDebugValue()) {
603 while (TIE != TIB && TIE->isDebugValue())
604 --TIE;
605 if (TIE == TIB)
606 break;
607 }
608 if (FIE->isDebugValue()) {
609 while (FIE != FIB && FIE->isDebugValue())
610 --FIE;
611 if (FIE == FIB)
612 break;
613 }
614 if (!TIE->isIdenticalTo(FIE))
Evan Chenga1a87872007-06-18 08:37:25 +0000615 break;
616 ++Dups2;
Bob Wilson7c730e72010-10-26 00:02:24 +0000617 --TIE;
618 --FIE;
Evan Chenga1a87872007-06-18 08:37:25 +0000619 }
620
621 return true;
Evan Chengd4de6d92007-06-07 02:12:15 +0000622}
623
Evan Cheng9618bca2007-06-11 22:26:22 +0000624/// ScanInstructions - Scan all the instructions in the block to determine if
625/// the block is predicable. In most cases, that means all the instructions
Chris Lattner69244302008-01-07 01:56:04 +0000626/// in the block are isPredicable(). Also checks if the block contains any
Evan Cheng9618bca2007-06-11 22:26:22 +0000627/// instruction which can clobber a predicate (e.g. condition code register).
628/// If so, the block is not predicable unless it's the last instruction.
629void IfConverter::ScanInstructions(BBInfo &BBI) {
630 if (BBI.IsDone)
631 return;
632
Evan Cheng791e2e02012-06-08 21:53:50 +0000633 bool AlreadyPredicated = !BBI.Predicate.empty();
Evan Cheng9618bca2007-06-11 22:26:22 +0000634 // First analyze the end of BB branches.
Evan Chengb7c908b2007-06-14 21:26:08 +0000635 BBI.TrueBB = BBI.FalseBB = NULL;
Evan Cheng9618bca2007-06-11 22:26:22 +0000636 BBI.BrCond.clear();
637 BBI.IsBrAnalyzable =
638 !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
639 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL;
640
641 if (BBI.BrCond.size()) {
642 // No false branch. This BB must end with a conditional branch and a
643 // fallthrough.
644 if (!BBI.FalseBB)
Bob Wilson92fffe02010-06-15 22:18:54 +0000645 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
Evan Cheng2077e182009-06-15 21:24:34 +0000646 if (!BBI.FalseBB) {
647 // Malformed bcc? True and false blocks are the same?
648 BBI.IsUnpredicable = true;
649 return;
650 }
Evan Cheng9618bca2007-06-11 22:26:22 +0000651 }
652
653 // Then scan all the instructions.
654 BBI.NonPredSize = 0;
Bob Wilson2ad40d32010-10-26 00:02:21 +0000655 BBI.ExtraCost = 0;
Evan Cheng8239daf2010-11-03 00:45:17 +0000656 BBI.ExtraCost2 = 0;
Evan Cheng9618bca2007-06-11 22:26:22 +0000657 BBI.ClobbersPred = false;
Evan Cheng9618bca2007-06-11 22:26:22 +0000658 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
659 I != E; ++I) {
Jim Grosbach870c8052010-06-04 23:01:26 +0000660 if (I->isDebugValue())
661 continue;
662
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000663 if (I->isNotDuplicable())
Evan Chenga2acf842007-06-15 21:18:05 +0000664 BBI.CannotBeCopied = true;
665
Evan Cheng9618bca2007-06-11 22:26:22 +0000666 bool isPredicated = TII->isPredicated(I);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000667 bool isCondBr = BBI.IsBrAnalyzable && I->isConditionalBranch();
Evan Cheng9618bca2007-06-11 22:26:22 +0000668
Evan Chengd2c5eb82007-07-06 23:24:39 +0000669 if (!isCondBr) {
Evan Cheng3ef1c872010-09-10 01:29:16 +0000670 if (!isPredicated) {
Bob Wilson2ad40d32010-10-26 00:02:21 +0000671 BBI.NonPredSize++;
Evan Cheng8239daf2010-11-03 00:45:17 +0000672 unsigned ExtraPredCost = 0;
673 unsigned NumCycles = TII->getInstrLatency(InstrItins, &*I,
674 &ExtraPredCost);
675 if (NumCycles > 1)
676 BBI.ExtraCost += NumCycles-1;
677 BBI.ExtraCost2 += ExtraPredCost;
Evan Cheng3ef1c872010-09-10 01:29:16 +0000678 } else if (!AlreadyPredicated) {
Evan Chengd2c5eb82007-07-06 23:24:39 +0000679 // FIXME: This instruction is already predicated before the
680 // if-conversion pass. It's probably something like a conditional move.
681 // Mark this block unpredicable for now.
682 BBI.IsUnpredicable = true;
683 return;
684 }
Evan Chengd2c5eb82007-07-06 23:24:39 +0000685 }
Evan Cheng9618bca2007-06-11 22:26:22 +0000686
687 if (BBI.ClobbersPred && !isPredicated) {
688 // Predicate modification instruction should end the block (except for
689 // already predicated instructions and end of block branches).
690 if (isCondBr) {
Bob Wilson9c4856a2009-05-13 23:25:24 +0000691 // A conditional branch is not predicable, but it may be eliminated.
Evan Cheng9618bca2007-06-11 22:26:22 +0000692 continue;
693 }
694
695 // Predicate may have been modified, the subsequent (currently)
Evan Chengd2c5eb82007-07-06 23:24:39 +0000696 // unpredicated instructions cannot be correctly predicated.
Evan Cheng9618bca2007-06-11 22:26:22 +0000697 BBI.IsUnpredicable = true;
698 return;
699 }
700
Evan Cheng11ce02d2007-07-10 17:50:43 +0000701 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
702 // still potentially predicable.
703 std::vector<MachineOperand> PredDefs;
704 if (TII->DefinesPredicate(I, PredDefs))
Evan Cheng9618bca2007-06-11 22:26:22 +0000705 BBI.ClobbersPred = true;
706
Evan Chenge54cb162009-11-21 06:20:26 +0000707 if (!TII->isPredicable(I)) {
Evan Cheng9618bca2007-06-11 22:26:22 +0000708 BBI.IsUnpredicable = true;
709 return;
710 }
711 }
712}
713
714/// FeasibilityAnalysis - Determine if the block is a suitable candidate to be
715/// predicated by the specified predicate.
716bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000717 SmallVectorImpl<MachineOperand> &Pred,
Evan Cheng9618bca2007-06-11 22:26:22 +0000718 bool isTriangle, bool RevBranch) {
Evan Chenge882fca2007-06-16 09:34:52 +0000719 // If the block is dead or unpredicable, then it cannot be predicated.
720 if (BBI.IsDone || BBI.IsUnpredicable)
Evan Cheng9618bca2007-06-11 22:26:22 +0000721 return false;
722
Evan Cheng9618bca2007-06-11 22:26:22 +0000723 // If it is already predicated, check if its predicate subsumes the new
724 // predicate.
725 if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Pred))
726 return false;
727
728 if (BBI.BrCond.size()) {
729 if (!isTriangle)
730 return false;
731
Bob Wilson9c4856a2009-05-13 23:25:24 +0000732 // Test predicate subsumption.
Owen Anderson44eb65c2008-08-14 22:49:33 +0000733 SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
734 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng9618bca2007-06-11 22:26:22 +0000735 if (RevBranch) {
736 if (TII->ReverseBranchCondition(Cond))
737 return false;
738 }
739 if (TII->ReverseBranchCondition(RevPred) ||
740 !TII->SubsumesPredicate(Cond, RevPred))
741 return false;
742 }
743
744 return true;
745}
746
Evan Chengac5f1422007-06-08 09:36:04 +0000747/// AnalyzeBlock - Analyze the structure of the sub-CFG starting from
Evan Chengcf6cc112007-05-18 18:14:37 +0000748/// the specified block. Record its successors and whether it looks like an
749/// if-conversion candidate.
Evan Chenge882fca2007-06-16 09:34:52 +0000750IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB,
751 std::vector<IfcvtToken*> &Tokens) {
Evan Cheng4e654852007-05-16 02:00:57 +0000752 BBInfo &BBI = BBAnalysis[BB->getNumber()];
753
Evan Cheng9618bca2007-06-11 22:26:22 +0000754 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed)
755 return BBI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000756
Evan Cheng9618bca2007-06-11 22:26:22 +0000757 BBI.BB = BB;
758 BBI.IsBeingAnalyzed = true;
Evan Cheng9618bca2007-06-11 22:26:22 +0000759
760 ScanInstructions(BBI);
761
Jakub Staszak2b33f4c2011-07-10 02:00:16 +0000762 // Unanalyzable or ends with fallthrough or unconditional branch, or if is not
763 // considered for ifcvt anymore.
764 if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
Evan Cheng9618bca2007-06-11 22:26:22 +0000765 BBI.IsBeingAnalyzed = false;
766 BBI.IsAnalyzed = true;
767 return BBI;
Evan Cheng7a655472007-06-06 10:16:17 +0000768 }
769
Evan Cheng9618bca2007-06-11 22:26:22 +0000770 // Do not ifcvt if either path is a back edge to the entry block.
771 if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
772 BBI.IsBeingAnalyzed = false;
773 BBI.IsAnalyzed = true;
774 return BBI;
775 }
776
Evan Cheng2077e182009-06-15 21:24:34 +0000777 // Do not ifcvt if true and false fallthrough blocks are the same.
778 if (!BBI.FalseBB) {
779 BBI.IsBeingAnalyzed = false;
780 BBI.IsAnalyzed = true;
781 return BBI;
782 }
783
Evan Chenge882fca2007-06-16 09:34:52 +0000784 BBInfo &TrueBBI = AnalyzeBlock(BBI.TrueBB, Tokens);
785 BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB, Tokens);
Evan Cheng9618bca2007-06-11 22:26:22 +0000786
787 if (TrueBBI.IsDone && FalseBBI.IsDone) {
788 BBI.IsBeingAnalyzed = false;
789 BBI.IsAnalyzed = true;
790 return BBI;
791 }
792
Owen Anderson44eb65c2008-08-14 22:49:33 +0000793 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Chengb6665f62007-06-04 06:47:22 +0000794 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
Evan Chengac5f1422007-06-08 09:36:04 +0000795
Evan Chenge882fca2007-06-16 09:34:52 +0000796 unsigned Dups = 0;
Evan Chenga1a87872007-06-18 08:37:25 +0000797 unsigned Dups2 = 0;
Evan Cheng791e2e02012-06-08 21:53:50 +0000798 bool TNeedSub = !TrueBBI.Predicate.empty();
799 bool FNeedSub = !FalseBBI.Predicate.empty();
Evan Chenge882fca2007-06-16 09:34:52 +0000800 bool Enqueued = false;
Benjamin Kramer6406d002010-09-29 22:38:50 +0000801
Jakub Staszak990f78d2011-08-03 22:34:43 +0000802 BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB);
803
Evan Chenga1a87872007-06-18 08:37:25 +0000804 if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000805 MeetIfcvtSizeLimit(*TrueBBI.BB, (TrueBBI.NonPredSize - (Dups + Dups2) +
Evan Cheng8239daf2010-11-03 00:45:17 +0000806 TrueBBI.ExtraCost), TrueBBI.ExtraCost2,
Bob Wilson2ad40d32010-10-26 00:02:21 +0000807 *FalseBBI.BB, (FalseBBI.NonPredSize - (Dups + Dups2) +
Evan Cheng8239daf2010-11-03 00:45:17 +0000808 FalseBBI.ExtraCost),FalseBBI.ExtraCost2,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000809 Prediction) &&
Evan Chengac5f1422007-06-08 09:36:04 +0000810 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
811 FeasibilityAnalysis(FalseBBI, RevCond)) {
Evan Cheng4e654852007-05-16 02:00:57 +0000812 // Diamond:
813 // EBB
814 // / \_
815 // | |
816 // TBB FBB
817 // \ /
Evan Cheng86cbfea2007-05-18 00:20:58 +0000818 // TailBB
Evan Cheng993fc952007-06-05 23:46:14 +0000819 // Note TailBB can be empty.
Evan Chenga1a87872007-06-18 08:37:25 +0000820 Tokens.push_back(new IfcvtToken(BBI, ICDiamond, TNeedSub|FNeedSub, Dups,
821 Dups2));
Evan Chenge882fca2007-06-16 09:34:52 +0000822 Enqueued = true;
823 }
824
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000825 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000826 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000827 TrueBBI.ExtraCost2, Prediction) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000828 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
829 // Triangle:
830 // EBB
831 // | \_
832 // | |
833 // | TBB
834 // | /
835 // FBB
836 Tokens.push_back(new IfcvtToken(BBI, ICTriangle, TNeedSub, Dups));
837 Enqueued = true;
838 }
Bob Wilson92fffe02010-06-15 22:18:54 +0000839
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000840 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000841 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000842 TrueBBI.ExtraCost2, Prediction) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000843 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
844 Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups));
845 Enqueued = true;
846 }
847
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000848 if (ValidSimple(TrueBBI, Dups, Prediction) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000849 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000850 TrueBBI.ExtraCost2, Prediction) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000851 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
852 // Simple (split, no rejoin):
853 // EBB
854 // | \_
855 // | |
856 // | TBB---> exit
Bob Wilson92fffe02010-06-15 22:18:54 +0000857 // |
Evan Chenge882fca2007-06-16 09:34:52 +0000858 // FBB
859 Tokens.push_back(new IfcvtToken(BBI, ICSimple, TNeedSub, Dups));
860 Enqueued = true;
861 }
862
863 if (CanRevCond) {
864 // Try the other path...
Owen Andersone3cc84a2010-10-01 22:45:50 +0000865 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000866 Prediction.getCompl()) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000867 MeetIfcvtSizeLimit(*FalseBBI.BB,
868 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000869 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000870 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
871 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups));
872 Enqueued = true;
873 }
874
Owen Andersone3cc84a2010-10-01 22:45:50 +0000875 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000876 Prediction.getCompl()) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000877 MeetIfcvtSizeLimit(*FalseBBI.BB,
878 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000879 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000880 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
881 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups));
882 Enqueued = true;
883 }
884
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000885 if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000886 MeetIfcvtSizeLimit(*FalseBBI.BB,
887 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000888 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000889 FeasibilityAnalysis(FalseBBI, RevCond)) {
890 Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups));
891 Enqueued = true;
Evan Chengb6665f62007-06-04 06:47:22 +0000892 }
Evan Cheng4e654852007-05-16 02:00:57 +0000893 }
Evan Cheng4e654852007-05-16 02:00:57 +0000894
Evan Chenge882fca2007-06-16 09:34:52 +0000895 BBI.IsEnqueued = Enqueued;
Evan Cheng9618bca2007-06-11 22:26:22 +0000896 BBI.IsBeingAnalyzed = false;
897 BBI.IsAnalyzed = true;
898 return BBI;
Evan Chengcf6cc112007-05-18 18:14:37 +0000899}
900
Evan Cheng82582102007-05-30 19:49:19 +0000901/// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
Bob Wilson6fb124b2010-06-15 18:57:15 +0000902/// candidates.
903void IfConverter::AnalyzeBlocks(MachineFunction &MF,
Evan Chenge882fca2007-06-16 09:34:52 +0000904 std::vector<IfcvtToken*> &Tokens) {
Evan Cheng309db7c2011-04-27 19:32:43 +0000905 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
906 MachineBasicBlock *BB = I;
907 AnalyzeBlock(BB, Tokens);
Evan Cheng4e654852007-05-16 02:00:57 +0000908 }
Evan Cheng82582102007-05-30 19:49:19 +0000909
Evan Cheng5f702182007-06-01 00:12:12 +0000910 // Sort to favor more complex ifcvt scheme.
Evan Chenge882fca2007-06-16 09:34:52 +0000911 std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
Evan Cheng4e654852007-05-16 02:00:57 +0000912}
913
Evan Cheng7e75ba82007-06-08 22:01:07 +0000914/// canFallThroughTo - Returns true either if ToBB is the next block after BB or
915/// that all the intervening blocks are empty (given BB can fall through to its
916/// next block).
917static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
Evan Cheng46df4eb2010-06-16 07:35:02 +0000918 MachineFunction::iterator PI = BB;
919 MachineFunction::iterator I = llvm::next(PI);
Evan Chengc53ef582007-06-05 07:05:25 +0000920 MachineFunction::iterator TI = ToBB;
921 MachineFunction::iterator E = BB->getParent()->end();
Evan Cheng46df4eb2010-06-16 07:35:02 +0000922 while (I != TI) {
923 // Check isSuccessor to avoid case where the next block is empty, but
924 // it's not a successor.
925 if (I == E || !I->empty() || !PI->isSuccessor(I))
Evan Chengb6665f62007-06-04 06:47:22 +0000926 return false;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000927 PI = I++;
928 }
Evan Chengb6665f62007-06-04 06:47:22 +0000929 return true;
Evan Chenga6b4f432007-05-21 22:22:58 +0000930}
931
Evan Chenga1a87872007-06-18 08:37:25 +0000932/// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed
933/// to determine if it can be if-converted. If predecessor is already enqueued,
934/// dequeue it!
935void IfConverter::InvalidatePreds(MachineBasicBlock *BB) {
Evan Chenga13aa952007-05-23 07:23:16 +0000936 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
937 E = BB->pred_end(); PI != E; ++PI) {
938 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
Evan Chengbc198ee2007-06-14 23:34:09 +0000939 if (PBBI.IsDone || PBBI.BB == BB)
Evan Chenge37e2432007-06-14 23:13:19 +0000940 continue;
Evan Chengbc198ee2007-06-14 23:34:09 +0000941 PBBI.IsAnalyzed = false;
942 PBBI.IsEnqueued = false;
Evan Chenga13aa952007-05-23 07:23:16 +0000943 }
944}
945
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000946/// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
947///
948static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
949 const TargetInstrInfo *TII) {
Stuart Hastings3bf91252010-06-17 22:43:56 +0000950 DebugLoc dl; // FIXME: this is nowhere
Dan Gohman1501cdb2008-08-22 16:07:55 +0000951 SmallVector<MachineOperand, 0> NoCond;
Stuart Hastings3bf91252010-06-17 22:43:56 +0000952 TII->InsertBranch(*BB, ToBB, NULL, NoCond, dl);
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000953}
954
Evan Cheng7e75ba82007-06-08 22:01:07 +0000955/// RemoveExtraEdges - Remove true / false edges if either / both are no longer
956/// successors.
957void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
958 MachineBasicBlock *TBB = NULL, *FBB = NULL;
Owen Anderson44eb65c2008-08-14 22:49:33 +0000959 SmallVector<MachineOperand, 4> Cond;
Evan Chengc4047a82007-06-18 22:44:57 +0000960 if (!TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond))
961 BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
Evan Cheng7e75ba82007-06-08 22:01:07 +0000962}
963
Evan Cheng46df4eb2010-06-16 07:35:02 +0000964/// InitPredRedefs / UpdatePredRedefs - Defs by predicated instructions are
965/// modeled as read + write (sort like two-address instructions). These
966/// routines track register liveness and add implicit uses to if-converted
967/// instructions to conform to the model.
968static void InitPredRedefs(MachineBasicBlock *BB, SmallSet<unsigned,4> &Redefs,
969 const TargetRegisterInfo *TRI) {
970 for (MachineBasicBlock::livein_iterator I = BB->livein_begin(),
971 E = BB->livein_end(); I != E; ++I) {
972 unsigned Reg = *I;
Chad Rosier62c320a2013-05-22 23:17:36 +0000973 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
974 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000975 Redefs.insert(*SubRegs);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000976 }
977}
978
979static void UpdatePredRedefs(MachineInstr *MI, SmallSet<unsigned,4> &Redefs,
980 const TargetRegisterInfo *TRI,
981 bool AddImpUse = false) {
982 SmallVector<unsigned, 4> Defs;
983 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
984 const MachineOperand &MO = MI->getOperand(i);
985 if (!MO.isReg())
986 continue;
987 unsigned Reg = MO.getReg();
988 if (!Reg)
989 continue;
990 if (MO.isDef())
991 Defs.push_back(Reg);
992 else if (MO.isKill()) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000993 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
994 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000995 Redefs.erase(*SubRegs);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000996 }
997 }
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +0000998 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000999 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1000 unsigned Reg = Defs[i];
Benjamin Kramer05d96f92012-08-22 15:37:57 +00001001 if (!Redefs.insert(Reg)) {
Evan Cheng46df4eb2010-06-16 07:35:02 +00001002 if (AddImpUse)
1003 // Treat predicated update as read + write.
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +00001004 MIB.addReg(Reg, RegState::Implicit | RegState::Undef);
Evan Cheng46df4eb2010-06-16 07:35:02 +00001005 } else {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001006 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
1007 Redefs.insert(*SubRegs);
Evan Cheng46df4eb2010-06-16 07:35:02 +00001008 }
1009 }
1010}
1011
1012static void UpdatePredRedefs(MachineBasicBlock::iterator I,
1013 MachineBasicBlock::iterator E,
1014 SmallSet<unsigned,4> &Redefs,
1015 const TargetRegisterInfo *TRI) {
1016 while (I != E) {
1017 UpdatePredRedefs(I, Redefs, TRI);
1018 ++I;
1019 }
1020}
1021
Evan Chengb6665f62007-06-04 06:47:22 +00001022/// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
Evan Chenga6b4f432007-05-21 22:22:58 +00001023///
Evan Chenge882fca2007-06-16 09:34:52 +00001024bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
Evan Chenga6b4f432007-05-21 22:22:58 +00001025 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1026 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1027 BBInfo *CvtBBI = &TrueBBI;
1028 BBInfo *NextBBI = &FalseBBI;
Evan Chenga13aa952007-05-23 07:23:16 +00001029
Owen Anderson44eb65c2008-08-14 22:49:33 +00001030 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Chenge882fca2007-06-16 09:34:52 +00001031 if (Kind == ICSimpleFalse)
Evan Chengb5a06902007-06-01 20:29:21 +00001032 std::swap(CvtBBI, NextBBI);
Evan Chenga2acf842007-06-15 21:18:05 +00001033
Evan Chenga1a87872007-06-18 08:37:25 +00001034 if (CvtBBI->IsDone ||
1035 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
Evan Chenga2acf842007-06-15 21:18:05 +00001036 // Something has changed. It's no longer safe to predicate this block.
Evan Chenga2acf842007-06-15 21:18:05 +00001037 BBI.IsAnalyzed = false;
1038 CvtBBI->IsAnalyzed = false;
1039 return false;
Evan Chengb5a06902007-06-01 20:29:21 +00001040 }
Evan Chenga13aa952007-05-23 07:23:16 +00001041
Evan Cheng9a28cc12013-05-05 18:03:49 +00001042 if (CvtBBI->BB->hasAddressTaken())
1043 // Conservatively abort if-conversion if BB's address is taken.
1044 return false;
1045
Evan Chenge882fca2007-06-16 09:34:52 +00001046 if (Kind == ICSimpleFalse)
Dan Gohman279c22e2008-10-21 03:29:32 +00001047 if (TII->ReverseBranchCondition(Cond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001048 llvm_unreachable("Unable to reverse branch condition!");
Evan Chenga2acf842007-06-15 21:18:05 +00001049
Bob Wilson54eee522010-06-19 05:33:57 +00001050 // Initialize liveins to the first BB. These are potentiall redefined by
Evan Cheng46df4eb2010-06-16 07:35:02 +00001051 // predicated instructions.
1052 SmallSet<unsigned, 4> Redefs;
1053 InitPredRedefs(CvtBBI->BB, Redefs, TRI);
1054 InitPredRedefs(NextBBI->BB, Redefs, TRI);
1055
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001056 if (CvtBBI->BB->pred_size() > 1) {
1057 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson9c4856a2009-05-13 23:25:24 +00001058 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001059 // the entry block.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001060 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, Redefs);
Hal Finkel9af70142013-04-10 22:05:25 +00001061
1062 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1063 // explicitly remove CvtBBI as a successor.
1064 BBI.BB->removeSuccessor(CvtBBI->BB);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001065 } else {
Evan Cheng46df4eb2010-06-16 07:35:02 +00001066 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond, Redefs);
Evan Chenga6b4f432007-05-21 22:22:58 +00001067
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001068 // Merge converted block into entry block.
1069 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1070 MergeBlocks(BBI, *CvtBBI);
1071 }
Evan Chengd4de6d92007-06-07 02:12:15 +00001072
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001073 bool IterIfcvt = true;
Evan Cheng7e75ba82007-06-08 22:01:07 +00001074 if (!canFallThroughTo(BBI.BB, NextBBI->BB)) {
Evan Chengc8ed9ba2007-05-29 22:31:16 +00001075 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001076 BBI.HasFallThrough = false;
Evan Chengac5f1422007-06-08 09:36:04 +00001077 // Now ifcvt'd block will look like this:
1078 // BB:
1079 // ...
1080 // t, f = cmp
1081 // if t op
1082 // b BBf
1083 //
1084 // We cannot further ifcvt this block because the unconditional branch
1085 // will have to be predicated on the new condition, that will not be
1086 // available if cmp executes.
1087 IterIfcvt = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001088 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001089
Evan Cheng7e75ba82007-06-08 22:01:07 +00001090 RemoveExtraEdges(BBI);
1091
Evan Chenga13aa952007-05-23 07:23:16 +00001092 // Update block info. BB can be iteratively if-converted.
Evan Cheng9618bca2007-06-11 22:26:22 +00001093 if (!IterIfcvt)
1094 BBI.IsDone = true;
Evan Chenga1a87872007-06-18 08:37:25 +00001095 InvalidatePreds(BBI.BB);
Evan Cheng9618bca2007-06-11 22:26:22 +00001096 CvtBBI->IsDone = true;
Evan Chenga6b4f432007-05-21 22:22:58 +00001097
1098 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +00001099 return true;
1100}
1101
Evan Chengd6ddc302007-05-16 21:54:37 +00001102/// IfConvertTriangle - If convert a triangle sub-CFG.
1103///
Evan Chenge882fca2007-06-16 09:34:52 +00001104bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
Evan Chenga13aa952007-05-23 07:23:16 +00001105 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001106 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1107 BBInfo *CvtBBI = &TrueBBI;
1108 BBInfo *NextBBI = &FalseBBI;
Stuart Hastings3bf91252010-06-17 22:43:56 +00001109 DebugLoc dl; // FIXME: this is nowhere
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001110
Owen Anderson44eb65c2008-08-14 22:49:33 +00001111 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Chenge882fca2007-06-16 09:34:52 +00001112 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001113 std::swap(CvtBBI, NextBBI);
Evan Chenga2acf842007-06-15 21:18:05 +00001114
Evan Chenga1a87872007-06-18 08:37:25 +00001115 if (CvtBBI->IsDone ||
1116 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
Evan Chenga2acf842007-06-15 21:18:05 +00001117 // Something has changed. It's no longer safe to predicate this block.
Evan Chenga2acf842007-06-15 21:18:05 +00001118 BBI.IsAnalyzed = false;
1119 CvtBBI->IsAnalyzed = false;
1120 return false;
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001121 }
Evan Chenga2acf842007-06-15 21:18:05 +00001122
Evan Cheng9a28cc12013-05-05 18:03:49 +00001123 if (CvtBBI->BB->hasAddressTaken())
1124 // Conservatively abort if-conversion if BB's address is taken.
1125 return false;
1126
Evan Chenge882fca2007-06-16 09:34:52 +00001127 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Dan Gohman279c22e2008-10-21 03:29:32 +00001128 if (TII->ReverseBranchCondition(Cond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001129 llvm_unreachable("Unable to reverse branch condition!");
Evan Chenga2acf842007-06-15 21:18:05 +00001130
Evan Chenge882fca2007-06-16 09:34:52 +00001131 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
Dan Gohman279c22e2008-10-21 03:29:32 +00001132 if (ReverseBranchCondition(*CvtBBI)) {
1133 // BB has been changed, modify its predecessors (except for this
1134 // one) so they don't get ifcvt'ed based on bad intel.
1135 for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(),
1136 E = CvtBBI->BB->pred_end(); PI != E; ++PI) {
1137 MachineBasicBlock *PBB = *PI;
1138 if (PBB == BBI.BB)
1139 continue;
1140 BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
1141 if (PBBI.IsEnqueued) {
1142 PBBI.IsAnalyzed = false;
1143 PBBI.IsEnqueued = false;
1144 }
Evan Chengbc198ee2007-06-14 23:34:09 +00001145 }
Evan Chengcc8fb462007-06-12 23:54:05 +00001146 }
1147 }
Evan Cheng8c529382007-06-01 07:41:07 +00001148
Bob Wilson54eee522010-06-19 05:33:57 +00001149 // Initialize liveins to the first BB. These are potentially redefined by
Evan Cheng46df4eb2010-06-16 07:35:02 +00001150 // predicated instructions.
1151 SmallSet<unsigned, 4> Redefs;
1152 InitPredRedefs(CvtBBI->BB, Redefs, TRI);
1153 InitPredRedefs(NextBBI->BB, Redefs, TRI);
1154
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001155 bool HasEarlyExit = CvtBBI->FalseBB != NULL;
Bob Wilson8eab75f2010-06-29 00:55:23 +00001156 if (CvtBBI->BB->pred_size() > 1) {
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001157 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson9c4856a2009-05-13 23:25:24 +00001158 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001159 // the entry block.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001160 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, Redefs, true);
Hal Finkel9af70142013-04-10 22:05:25 +00001161
1162 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1163 // explicitly remove CvtBBI as a successor.
1164 BBI.BB->removeSuccessor(CvtBBI->BB);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001165 } else {
1166 // Predicate the 'true' block after removing its branch.
1167 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
Evan Cheng46df4eb2010-06-16 07:35:02 +00001168 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond, Redefs);
Evan Chenga6b4f432007-05-21 22:22:58 +00001169
Evan Chengc4047a82007-06-18 22:44:57 +00001170 // Now merge the entry of the triangle with the true block.
1171 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson8eab75f2010-06-29 00:55:23 +00001172 MergeBlocks(BBI, *CvtBBI, false);
Evan Chengc4047a82007-06-18 22:44:57 +00001173 }
1174
Evan Chengb6665f62007-06-04 06:47:22 +00001175 // If 'true' block has a 'false' successor, add an exit branch to it.
Evan Cheng8ed680c2007-06-05 01:31:40 +00001176 if (HasEarlyExit) {
Owen Anderson44eb65c2008-08-14 22:49:33 +00001177 SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
1178 CvtBBI->BrCond.end());
Evan Cheng8c529382007-06-01 07:41:07 +00001179 if (TII->ReverseBranchCondition(RevCond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001180 llvm_unreachable("Unable to reverse branch condition!");
Stuart Hastings3bf91252010-06-17 22:43:56 +00001181 TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond, dl);
Evan Chengc4047a82007-06-18 22:44:57 +00001182 BBI.BB->addSuccessor(CvtBBI->FalseBB);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001183 }
Evan Chengac5f1422007-06-08 09:36:04 +00001184
1185 // Merge in the 'false' block if the 'false' block has no other
Bob Wilson9c4856a2009-05-13 23:25:24 +00001186 // predecessors. Otherwise, add an unconditional branch to 'false'.
Evan Chengf5305f92007-06-05 00:07:37 +00001187 bool FalseBBDead = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001188 bool IterIfcvt = true;
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001189 bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB);
Evan Chengf4769612007-06-07 08:13:00 +00001190 if (!isFallThrough) {
1191 // Only merge them if the true block does not fallthrough to the false
1192 // block. By not merging them, we make it possible to iteratively
1193 // ifcvt the blocks.
Evan Chenga1a87872007-06-18 08:37:25 +00001194 if (!HasEarlyExit &&
Evan Cheng9a28cc12013-05-05 18:03:49 +00001195 NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough &&
1196 !NextBBI->BB->hasAddressTaken()) {
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001197 MergeBlocks(BBI, *NextBBI);
Evan Chengf4769612007-06-07 08:13:00 +00001198 FalseBBDead = true;
Evan Chengf4769612007-06-07 08:13:00 +00001199 } else {
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001200 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
1201 BBI.HasFallThrough = false;
Evan Chengf4769612007-06-07 08:13:00 +00001202 }
Evan Chengac5f1422007-06-08 09:36:04 +00001203 // Mixed predicated and unpredicated code. This cannot be iteratively
1204 // predicated.
1205 IterIfcvt = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001206 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001207
Evan Cheng7e75ba82007-06-08 22:01:07 +00001208 RemoveExtraEdges(BBI);
Evan Chenga6b4f432007-05-21 22:22:58 +00001209
Evan Chenga13aa952007-05-23 07:23:16 +00001210 // Update block info. BB can be iteratively if-converted.
Bob Wilson92fffe02010-06-15 22:18:54 +00001211 if (!IterIfcvt)
Evan Cheng9618bca2007-06-11 22:26:22 +00001212 BBI.IsDone = true;
Evan Chenga1a87872007-06-18 08:37:25 +00001213 InvalidatePreds(BBI.BB);
Evan Cheng9618bca2007-06-11 22:26:22 +00001214 CvtBBI->IsDone = true;
Evan Chengf5305f92007-06-05 00:07:37 +00001215 if (FalseBBDead)
Evan Cheng9618bca2007-06-11 22:26:22 +00001216 NextBBI->IsDone = true;
Evan Chenga6b4f432007-05-21 22:22:58 +00001217
1218 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +00001219 return true;
Evan Cheng4e654852007-05-16 02:00:57 +00001220}
1221
Evan Chengd6ddc302007-05-16 21:54:37 +00001222/// IfConvertDiamond - If convert a diamond sub-CFG.
1223///
Evan Chenga1a87872007-06-18 08:37:25 +00001224bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
1225 unsigned NumDups1, unsigned NumDups2) {
Evan Chengb6665f62007-06-04 06:47:22 +00001226 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +00001227 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chenge882fca2007-06-16 09:34:52 +00001228 MachineBasicBlock *TailBB = TrueBBI.TrueBB;
Bob Wilson9c4856a2009-05-13 23:25:24 +00001229 // True block must fall through or end with an unanalyzable terminator.
Evan Chenge882fca2007-06-16 09:34:52 +00001230 if (!TailBB) {
Evan Chenga1a87872007-06-18 08:37:25 +00001231 if (blockAlwaysFallThrough(TrueBBI))
1232 TailBB = FalseBBI.TrueBB;
1233 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
Evan Cheng4e654852007-05-16 02:00:57 +00001234 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001235
Evan Chenga1a87872007-06-18 08:37:25 +00001236 if (TrueBBI.IsDone || FalseBBI.IsDone ||
1237 TrueBBI.BB->pred_size() > 1 ||
1238 FalseBBI.BB->pred_size() > 1) {
1239 // Something has changed. It's no longer safe to predicate these blocks.
1240 BBI.IsAnalyzed = false;
1241 TrueBBI.IsAnalyzed = false;
1242 FalseBBI.IsAnalyzed = false;
1243 return false;
Evan Chenga6b4f432007-05-21 22:22:58 +00001244 }
Evan Chenga1a87872007-06-18 08:37:25 +00001245
Evan Cheng9a28cc12013-05-05 18:03:49 +00001246 if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
1247 // Conservatively abort if-conversion if either BB has its address taken.
1248 return false;
1249
Bob Wilson8eab75f2010-06-29 00:55:23 +00001250 // Put the predicated instructions from the 'true' block before the
1251 // instructions from the 'false' block, unless the true block would clobber
1252 // the predicate, in which case, do the opposite.
Evan Cheng993fc952007-06-05 23:46:14 +00001253 BBInfo *BBI1 = &TrueBBI;
1254 BBInfo *BBI2 = &FalseBBI;
Owen Anderson44eb65c2008-08-14 22:49:33 +00001255 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
Dan Gohman279c22e2008-10-21 03:29:32 +00001256 if (TII->ReverseBranchCondition(RevCond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001257 llvm_unreachable("Unable to reverse branch condition!");
Owen Anderson44eb65c2008-08-14 22:49:33 +00001258 SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
1259 SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
Evan Chenge7052132007-06-06 00:57:55 +00001260
Evan Chenge882fca2007-06-16 09:34:52 +00001261 // Figure out the more profitable ordering.
1262 bool DoSwap = false;
1263 if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred)
1264 DoSwap = true;
1265 else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) {
Evan Chengc4047a82007-06-18 22:44:57 +00001266 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
Evan Chenge882fca2007-06-16 09:34:52 +00001267 DoSwap = true;
Evan Chenge882fca2007-06-16 09:34:52 +00001268 }
1269 if (DoSwap) {
Evan Chenge7052132007-06-06 00:57:55 +00001270 std::swap(BBI1, BBI2);
1271 std::swap(Cond1, Cond2);
Evan Chenge7052132007-06-06 00:57:55 +00001272 }
Evan Cheng993fc952007-06-05 23:46:14 +00001273
Evan Chenga1a87872007-06-18 08:37:25 +00001274 // Remove the conditional branch from entry to the blocks.
1275 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1276
Jim Grosbach135ec502010-06-25 22:02:28 +00001277 // Initialize liveins to the first BB. These are potentially redefined by
Evan Cheng46df4eb2010-06-16 07:35:02 +00001278 // predicated instructions.
1279 SmallSet<unsigned, 4> Redefs;
1280 InitPredRedefs(BBI1->BB, Redefs, TRI);
1281
Evan Chenga1a87872007-06-18 08:37:25 +00001282 // Remove the duplicated instructions at the beginnings of both paths.
1283 MachineBasicBlock::iterator DI1 = BBI1->BB->begin();
1284 MachineBasicBlock::iterator DI2 = BBI2->BB->begin();
Jim Grosbachc834f412010-06-14 21:30:32 +00001285 MachineBasicBlock::iterator DIE1 = BBI1->BB->end();
1286 MachineBasicBlock::iterator DIE2 = BBI2->BB->end();
1287 // Skip dbg_value instructions
1288 while (DI1 != DIE1 && DI1->isDebugValue())
1289 ++DI1;
1290 while (DI2 != DIE2 && DI2->isDebugValue())
1291 ++DI2;
Evan Chenga1a87872007-06-18 08:37:25 +00001292 BBI1->NonPredSize -= NumDups1;
1293 BBI2->NonPredSize -= NumDups1;
Jim Grosbachd459f452010-06-28 20:26:00 +00001294
1295 // Skip past the dups on each side separately since there may be
1296 // differing dbg_value entries.
1297 for (unsigned i = 0; i < NumDups1; ++DI1) {
1298 if (!DI1->isDebugValue())
1299 ++i;
1300 }
Jim Grosbach9f054f02010-06-25 23:05:46 +00001301 while (NumDups1 != 0) {
Evan Chenga1a87872007-06-18 08:37:25 +00001302 ++DI2;
Jim Grosbachd459f452010-06-28 20:26:00 +00001303 if (!DI2->isDebugValue())
1304 --NumDups1;
Evan Chenga1a87872007-06-18 08:37:25 +00001305 }
Evan Cheng46df4eb2010-06-16 07:35:02 +00001306
1307 UpdatePredRedefs(BBI1->BB->begin(), DI1, Redefs, TRI);
Evan Chenga1a87872007-06-18 08:37:25 +00001308 BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1);
1309 BBI2->BB->erase(BBI2->BB->begin(), DI2);
1310
Evan Cheng8787c5f2011-12-19 22:01:30 +00001311 // Remove branch from 'true' block and remove duplicated instructions.
Evan Cheng993fc952007-06-05 23:46:14 +00001312 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
Evan Chenga1a87872007-06-18 08:37:25 +00001313 DI1 = BBI1->BB->end();
Jim Grosbachc834f412010-06-14 21:30:32 +00001314 for (unsigned i = 0; i != NumDups2; ) {
1315 // NumDups2 only counted non-dbg_value instructions, so this won't
1316 // run off the head of the list.
1317 assert (DI1 != BBI1->BB->begin());
Evan Chenga1a87872007-06-18 08:37:25 +00001318 --DI1;
Jim Grosbachc834f412010-06-14 21:30:32 +00001319 // skip dbg_value instructions
1320 if (!DI1->isDebugValue())
1321 ++i;
1322 }
Evan Chenga1a87872007-06-18 08:37:25 +00001323 BBI1->BB->erase(DI1, BBI1->BB->end());
Evan Cheng993fc952007-06-05 23:46:14 +00001324
Evan Cheng8787c5f2011-12-19 22:01:30 +00001325 // Remove 'false' block branch and find the last instruction to predicate.
Evan Chenga1a87872007-06-18 08:37:25 +00001326 BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
1327 DI2 = BBI2->BB->end();
1328 while (NumDups2 != 0) {
Jim Grosbachc834f412010-06-14 21:30:32 +00001329 // NumDups2 only counted non-dbg_value instructions, so this won't
1330 // run off the head of the list.
1331 assert (DI2 != BBI2->BB->begin());
Evan Chenga1a87872007-06-18 08:37:25 +00001332 --DI2;
Jim Grosbachc834f412010-06-14 21:30:32 +00001333 // skip dbg_value instructions
1334 if (!DI2->isDebugValue())
1335 --NumDups2;
Evan Chenga1a87872007-06-18 08:37:25 +00001336 }
Evan Cheng8787c5f2011-12-19 22:01:30 +00001337
1338 // Remember which registers would later be defined by the false block.
1339 // This allows us not to predicate instructions in the true block that would
1340 // later be re-defined. That is, rather than
1341 // subeq r0, r1, #1
1342 // addne r0, r1, #1
1343 // generate:
1344 // sub r0, r1, #1
1345 // addne r0, r1, #1
1346 SmallSet<unsigned, 4> RedefsByFalse;
1347 SmallSet<unsigned, 4> ExtUses;
1348 if (TII->isProfitableToUnpredicate(*BBI1->BB, *BBI2->BB)) {
1349 for (MachineBasicBlock::iterator FI = BBI2->BB->begin(); FI != DI2; ++FI) {
1350 if (FI->isDebugValue())
1351 continue;
1352 SmallVector<unsigned, 4> Defs;
1353 for (unsigned i = 0, e = FI->getNumOperands(); i != e; ++i) {
1354 const MachineOperand &MO = FI->getOperand(i);
1355 if (!MO.isReg())
1356 continue;
1357 unsigned Reg = MO.getReg();
1358 if (!Reg)
1359 continue;
1360 if (MO.isDef()) {
1361 Defs.push_back(Reg);
1362 } else if (!RedefsByFalse.count(Reg)) {
1363 // These are defined before ctrl flow reach the 'false' instructions.
1364 // They cannot be modified by the 'true' instructions.
Chad Rosier62c320a2013-05-22 23:17:36 +00001365 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1366 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001367 ExtUses.insert(*SubRegs);
Evan Cheng8787c5f2011-12-19 22:01:30 +00001368 }
1369 }
1370
1371 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1372 unsigned Reg = Defs[i];
1373 if (!ExtUses.count(Reg)) {
Chad Rosier62c320a2013-05-22 23:17:36 +00001374 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1375 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001376 RedefsByFalse.insert(*SubRegs);
Evan Cheng8787c5f2011-12-19 22:01:30 +00001377 }
1378 }
1379 }
1380 }
1381
1382 // Predicate the 'true' block.
1383 PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1, Redefs, &RedefsByFalse);
1384
1385 // Predicate the 'false' block.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001386 PredicateBlock(*BBI2, DI2, *Cond2, Redefs);
Evan Cheng993fc952007-06-05 23:46:14 +00001387
Evan Chengc4047a82007-06-18 22:44:57 +00001388 // Merge the true block into the entry of the diamond.
Bob Wilson8eab75f2010-06-29 00:55:23 +00001389 MergeBlocks(BBI, *BBI1, TailBB == 0);
1390 MergeBlocks(BBI, *BBI2, TailBB == 0);
Evan Chenge7052132007-06-06 00:57:55 +00001391
Bob Wilson9c4856a2009-05-13 23:25:24 +00001392 // If the if-converted block falls through or unconditionally branches into
1393 // the tail block, and the tail block does not have other predecessors, then
Evan Chenga1a87872007-06-18 08:37:25 +00001394 // fold the tail block in as well. Otherwise, unless it falls through to the
1395 // tail, add a unconditional branch to it.
1396 if (TailBB) {
Pete Cooper9c58aa742011-11-04 23:49:14 +00001397 BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
Evan Cheng9a28cc12013-05-05 18:03:49 +00001398 bool CanMergeTail = !TailBBI.HasFallThrough &&
1399 !TailBBI.BB->hasAddressTaken();
Bob Wilson8eab75f2010-06-29 00:55:23 +00001400 // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
1401 // check if there are any other predecessors besides those.
1402 unsigned NumPreds = TailBB->pred_size();
1403 if (NumPreds > 1)
1404 CanMergeTail = false;
1405 else if (NumPreds == 1 && CanMergeTail) {
1406 MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
1407 if (*PI != BBI1->BB && *PI != BBI2->BB)
1408 CanMergeTail = false;
1409 }
1410 if (CanMergeTail) {
Evan Chengc4047a82007-06-18 22:44:57 +00001411 MergeBlocks(BBI, TailBBI);
Evan Chenga1a87872007-06-18 08:37:25 +00001412 TailBBI.IsDone = true;
1413 } else {
Bob Wilson8eab75f2010-06-29 00:55:23 +00001414 BBI.BB->addSuccessor(TailBB);
Evan Chengc4047a82007-06-18 22:44:57 +00001415 InsertUncondBranch(BBI.BB, TailBB, TII);
1416 BBI.HasFallThrough = false;
Evan Chenga1a87872007-06-18 08:37:25 +00001417 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001418 }
1419
Bob Wilson8eab75f2010-06-29 00:55:23 +00001420 // RemoveExtraEdges won't work if the block has an unanalyzable branch,
1421 // which can happen here if TailBB is unanalyzable and is merged, so
1422 // explicitly remove BBI1 and BBI2 as successors.
1423 BBI.BB->removeSuccessor(BBI1->BB);
1424 BBI.BB->removeSuccessor(BBI2->BB);
Evan Cheng7e75ba82007-06-08 22:01:07 +00001425 RemoveExtraEdges(BBI);
1426
Evan Cheng993fc952007-06-05 23:46:14 +00001427 // Update block info.
Evan Cheng9618bca2007-06-11 22:26:22 +00001428 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
Evan Chenga1a87872007-06-18 08:37:25 +00001429 InvalidatePreds(BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +00001430
1431 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +00001432 return true;
Evan Cheng4e654852007-05-16 02:00:57 +00001433}
1434
Evan Cheng8787c5f2011-12-19 22:01:30 +00001435static bool MaySpeculate(const MachineInstr *MI,
1436 SmallSet<unsigned, 4> &LaterRedefs,
1437 const TargetInstrInfo *TII) {
1438 bool SawStore = true;
1439 if (!MI->isSafeToMove(TII, 0, SawStore))
1440 return false;
1441
1442 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1443 const MachineOperand &MO = MI->getOperand(i);
1444 if (!MO.isReg())
1445 continue;
1446 unsigned Reg = MO.getReg();
1447 if (!Reg)
1448 continue;
1449 if (MO.isDef() && !LaterRedefs.count(Reg))
1450 return false;
1451 }
1452
1453 return true;
1454}
1455
Evan Chenga1a87872007-06-18 08:37:25 +00001456/// PredicateBlock - Predicate instructions from the start of the block to the
1457/// specified end with the specified condition.
Evan Chenga13aa952007-05-23 07:23:16 +00001458void IfConverter::PredicateBlock(BBInfo &BBI,
Evan Chenga1a87872007-06-18 08:37:25 +00001459 MachineBasicBlock::iterator E,
Evan Cheng46df4eb2010-06-16 07:35:02 +00001460 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng8787c5f2011-12-19 22:01:30 +00001461 SmallSet<unsigned, 4> &Redefs,
1462 SmallSet<unsigned, 4> *LaterRedefs) {
1463 bool AnyUnpred = false;
1464 bool MaySpec = LaterRedefs != 0;
Evan Chenga1a87872007-06-18 08:37:25 +00001465 for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) {
Jim Grosbach870c8052010-06-04 23:01:26 +00001466 if (I->isDebugValue() || TII->isPredicated(I))
Evan Chenga13aa952007-05-23 07:23:16 +00001467 continue;
Evan Cheng8787c5f2011-12-19 22:01:30 +00001468 // It may be possible not to predicate an instruction if it's the 'true'
1469 // side of a diamond and the 'false' side may re-define the instruction's
1470 // defs.
1471 if (MaySpec && MaySpeculate(I, *LaterRedefs, TII)) {
1472 AnyUnpred = true;
1473 continue;
1474 }
1475 // If any instruction is predicated, then every instruction after it must
1476 // be predicated.
1477 MaySpec = false;
Evan Chengb6665f62007-06-04 06:47:22 +00001478 if (!TII->PredicateInstruction(I, Cond)) {
Torok Edwinf3689232009-07-12 20:07:01 +00001479#ifndef NDEBUG
David Greene083b7ff2010-01-04 22:02:01 +00001480 dbgs() << "Unable to predicate " << *I << "!\n";
Torok Edwinf3689232009-07-12 20:07:01 +00001481#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00001482 llvm_unreachable(0);
Evan Chengd6ddc302007-05-16 21:54:37 +00001483 }
Evan Cheng46df4eb2010-06-16 07:35:02 +00001484
Bob Wilson54eee522010-06-19 05:33:57 +00001485 // If the predicated instruction now redefines a register as the result of
Evan Cheng46df4eb2010-06-16 07:35:02 +00001486 // if-conversion, add an implicit kill.
1487 UpdatePredRedefs(I, Redefs, TRI, true);
Evan Cheng4e654852007-05-16 02:00:57 +00001488 }
Evan Chenga13aa952007-05-23 07:23:16 +00001489
Evan Cheng2acdbcc2007-06-08 19:17:12 +00001490 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
1491
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001492 BBI.IsAnalyzed = false;
1493 BBI.NonPredSize = 0;
1494
Dan Gohmanfe601042010-06-22 15:08:57 +00001495 ++NumIfConvBBs;
Evan Cheng8787c5f2011-12-19 22:01:30 +00001496 if (AnyUnpred)
1497 ++NumUnpred;
Evan Chengb6665f62007-06-04 06:47:22 +00001498}
1499
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001500/// CopyAndPredicateBlock - Copy and predicate instructions from source BB to
1501/// the destination block. Skip end of block branches if IgnoreBr is true.
1502void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson44eb65c2008-08-14 22:49:33 +00001503 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng46df4eb2010-06-16 07:35:02 +00001504 SmallSet<unsigned, 4> &Redefs,
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001505 bool IgnoreBr) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001506 MachineFunction &MF = *ToBBI.BB->getParent();
1507
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001508 for (MachineBasicBlock::iterator I = FromBBI.BB->begin(),
1509 E = FromBBI.BB->end(); I != E; ++I) {
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001510 // Do not copy the end of the block branches.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001511 if (IgnoreBr && I->isBranch())
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001512 break;
1513
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001514 MachineInstr *MI = MF.CloneMachineInstr(I);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001515 ToBBI.BB->insert(ToBBI.BB->end(), MI);
Bob Wilson2ad40d32010-10-26 00:02:21 +00001516 ToBBI.NonPredSize++;
Evan Cheng8239daf2010-11-03 00:45:17 +00001517 unsigned ExtraPredCost = 0;
1518 unsigned NumCycles = TII->getInstrLatency(InstrItins, &*I, &ExtraPredCost);
1519 if (NumCycles > 1)
1520 ToBBI.ExtraCost += NumCycles-1;
1521 ToBBI.ExtraCost2 += ExtraPredCost;
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001522
Bob Wilsonf50e9522010-06-18 17:07:23 +00001523 if (!TII->isPredicated(I) && !MI->isDebugValue()) {
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001524 if (!TII->PredicateInstruction(MI, Cond)) {
Torok Edwinf3689232009-07-12 20:07:01 +00001525#ifndef NDEBUG
David Greene083b7ff2010-01-04 22:02:01 +00001526 dbgs() << "Unable to predicate " << *I << "!\n";
Torok Edwinf3689232009-07-12 20:07:01 +00001527#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00001528 llvm_unreachable(0);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001529 }
Evan Cheng46df4eb2010-06-16 07:35:02 +00001530 }
1531
Bob Wilson54eee522010-06-19 05:33:57 +00001532 // If the predicated instruction now redefines a register as the result of
Evan Cheng46df4eb2010-06-16 07:35:02 +00001533 // if-conversion, add an implicit kill.
1534 UpdatePredRedefs(MI, Redefs, TRI, true);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001535 }
1536
Bob Wilson8eab75f2010-06-29 00:55:23 +00001537 if (!IgnoreBr) {
1538 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
1539 FromBBI.BB->succ_end());
1540 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
1541 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
Evan Chengc4047a82007-06-18 22:44:57 +00001542
Bob Wilson8eab75f2010-06-29 00:55:23 +00001543 for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
1544 MachineBasicBlock *Succ = Succs[i];
1545 // Fallthrough edge can't be transferred.
1546 if (Succ == FallThrough)
1547 continue;
1548 ToBBI.BB->addSuccessor(Succ);
1549 }
Evan Chengc4047a82007-06-18 22:44:57 +00001550 }
1551
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001552 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1553 std::back_inserter(ToBBI.Predicate));
1554 std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate));
1555
1556 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1557 ToBBI.IsAnalyzed = false;
1558
Dan Gohmanfe601042010-06-22 15:08:57 +00001559 ++NumDupBBs;
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001560}
1561
Evan Cheng86cbfea2007-05-18 00:20:58 +00001562/// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
Bob Wilson8eab75f2010-06-29 00:55:23 +00001563/// This will leave FromBB as an empty block, so remove all of its
1564/// successor edges except for the fall-through edge. If AddEdges is true,
1565/// i.e., when FromBBI's branch is being moved, add those successor edges to
1566/// ToBBI.
1567void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
Evan Cheng9a28cc12013-05-05 18:03:49 +00001568 assert(!FromBBI.BB->hasAddressTaken() &&
1569 "Removing a BB whose address is taken!");
1570
Evan Cheng86cbfea2007-05-18 00:20:58 +00001571 ToBBI.BB->splice(ToBBI.BB->end(),
1572 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
Evan Chenga13aa952007-05-23 07:23:16 +00001573
Evan Chengd4de6d92007-06-07 02:12:15 +00001574 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
1575 FromBBI.BB->succ_end());
Evan Cheng7e75ba82007-06-08 22:01:07 +00001576 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001577 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
Evan Chengb6665f62007-06-04 06:47:22 +00001578
Evan Chengd4de6d92007-06-07 02:12:15 +00001579 for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
1580 MachineBasicBlock *Succ = Succs[i];
Evan Cheng7e75ba82007-06-08 22:01:07 +00001581 // Fallthrough edge can't be transferred.
Evan Chengd4de6d92007-06-07 02:12:15 +00001582 if (Succ == FallThrough)
1583 continue;
1584 FromBBI.BB->removeSuccessor(Succ);
Jakob Stoklund Olesendd4fc442013-01-24 23:59:08 +00001585 if (AddEdges && !ToBBI.BB->isSuccessor(Succ))
Bob Wilson8eab75f2010-06-29 00:55:23 +00001586 ToBBI.BB->addSuccessor(Succ);
Evan Chengd4de6d92007-06-07 02:12:15 +00001587 }
1588
Bob Wilson9c4856a2009-05-13 23:25:24 +00001589 // Now FromBBI always falls through to the next block!
Bob Wilson8308e8f2009-05-13 23:48:58 +00001590 if (NBB && !FromBBI.BB->isSuccessor(NBB))
Evan Cheng7e75ba82007-06-08 22:01:07 +00001591 FromBBI.BB->addSuccessor(NBB);
1592
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001593 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1594 std::back_inserter(ToBBI.Predicate));
1595 FromBBI.Predicate.clear();
1596
Evan Chenga13aa952007-05-23 07:23:16 +00001597 ToBBI.NonPredSize += FromBBI.NonPredSize;
Bob Wilson2ad40d32010-10-26 00:02:21 +00001598 ToBBI.ExtraCost += FromBBI.ExtraCost;
Evan Cheng8239daf2010-11-03 00:45:17 +00001599 ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
Evan Chenga13aa952007-05-23 07:23:16 +00001600 FromBBI.NonPredSize = 0;
Bob Wilson2ad40d32010-10-26 00:02:21 +00001601 FromBBI.ExtraCost = 0;
Evan Cheng8239daf2010-11-03 00:45:17 +00001602 FromBBI.ExtraCost2 = 0;
Evan Cheng7a655472007-06-06 10:16:17 +00001603
Evan Cheng9618bca2007-06-11 22:26:22 +00001604 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001605 ToBBI.HasFallThrough = FromBBI.HasFallThrough;
Evan Cheng86ff2962007-06-14 20:28:52 +00001606 ToBBI.IsAnalyzed = false;
1607 FromBBI.IsAnalyzed = false;
Evan Cheng4e654852007-05-16 02:00:57 +00001608}