blob: 597a237148a54aa2cbe74987cea0062d6981bd91 [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"
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +000025#include "llvm/CodeGen/TargetSchedule.h"
Evan Chengab8be962011-06-29 01:14:12 +000026#include "llvm/MC/MCInstrItineraries.h"
Evan Chengedf48962007-06-08 19:10:51 +000027#include "llvm/Support/CommandLine.h"
Evan Cheng4e654852007-05-16 02:00:57 +000028#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000031#include "llvm/Target/TargetInstrInfo.h"
32#include "llvm/Target/TargetLowering.h"
33#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetRegisterInfo.h"
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +000035#include "llvm/Target/TargetSubtargetInfo.h"
36
Evan Cheng4e654852007-05-16 02:00:57 +000037using namespace llvm;
38
Chris Lattnerf86e1df2008-01-07 05:40:58 +000039// Hidden options for help debugging.
40static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
41static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
42static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000043static cl::opt<bool> DisableSimple("disable-ifcvt-simple",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000044 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000045static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000046 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000047static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000048 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000049static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000050 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000051static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000052 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000053static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000054 cl::init(false), cl::Hidden);
Bob Wilson92fffe02010-06-15 22:18:54 +000055static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
Chris Lattnerf86e1df2008-01-07 05:40:58 +000056 cl::init(false), cl::Hidden);
Evan Cheng46df4eb2010-06-16 07:35:02 +000057static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold",
58 cl::init(true), cl::Hidden);
Evan Chengedf48962007-06-08 19:10:51 +000059
Evan Cheng1c9f91d2007-06-09 01:03:43 +000060STATISTIC(NumSimple, "Number of simple if-conversions performed");
61STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed");
62STATISTIC(NumTriangle, "Number of triangle if-conversions performed");
Evan Chengcc8fb462007-06-12 23:54:05 +000063STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed");
Evan Cheng1c9f91d2007-06-09 01:03:43 +000064STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
65STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
66STATISTIC(NumDiamonds, "Number of diamond if-conversions performed");
67STATISTIC(NumIfConvBBs, "Number of if-converted blocks");
Evan Cheng2c8c3a42007-06-15 07:36:12 +000068STATISTIC(NumDupBBs, "Number of duplicated blocks");
Evan Cheng8787c5f2011-12-19 22:01:30 +000069STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated");
Evan Cheng4e654852007-05-16 02:00:57 +000070
71namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000072 class IfConverter : public MachineFunctionPass {
Evan Chenge882fca2007-06-16 09:34:52 +000073 enum IfcvtKind {
Evan Cheng4e654852007-05-16 02:00:57 +000074 ICNotClassfied, // BB data valid, but not classified.
Evan Chengb6665f62007-06-04 06:47:22 +000075 ICSimpleFalse, // Same as ICSimple, but on the false path.
Evan Chenge882fca2007-06-16 09:34:52 +000076 ICSimple, // BB is entry of an one split, no rejoin sub-CFG.
77 ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition.
Evan Chengcc8fb462007-06-12 23:54:05 +000078 ICTriangleRev, // Same as ICTriangle, but true path rev condition.
Evan Cheng1c9f91d2007-06-09 01:03:43 +000079 ICTriangleFalse, // Same as ICTriangle, but on the false path.
Evan Chenge882fca2007-06-16 09:34:52 +000080 ICTriangle, // BB is entry of a triangle sub-CFG.
Evan Cheng9618bca2007-06-11 22:26:22 +000081 ICDiamond // BB is entry of a diamond sub-CFG.
Evan Cheng4e654852007-05-16 02:00:57 +000082 };
83
84 /// BBInfo - One per MachineBasicBlock, this is used to cache the result
85 /// if-conversion feasibility analysis. This includes results from
86 /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
Evan Cheng86cbfea2007-05-18 00:20:58 +000087 /// classification, and common tail block of its successors (if it's a
Evan Chengcf6cc112007-05-18 18:14:37 +000088 /// diamond shape), its size, whether it's predicable, and whether any
89 /// instruction can clobber the 'would-be' predicate.
Evan Chenga13aa952007-05-23 07:23:16 +000090 ///
Evan Cheng9618bca2007-06-11 22:26:22 +000091 /// IsDone - True if BB is not to be considered for ifcvt.
92 /// IsBeingAnalyzed - True if BB is currently being analyzed.
93 /// IsAnalyzed - True if BB has been analyzed (info is still valid).
94 /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed.
95 /// IsBrAnalyzable - True if AnalyzeBranch() returns false.
96 /// HasFallThrough - True if BB may fallthrough to the following BB.
97 /// IsUnpredicable - True if BB is known to be unpredicable.
Evan Cheng11ce02d2007-07-10 17:50:43 +000098 /// ClobbersPred - True if BB could modify predicates (e.g. has
Evan Cheng7a655472007-06-06 10:16:17 +000099 /// cmp, call, etc.)
Evan Cheng9618bca2007-06-11 22:26:22 +0000100 /// NonPredSize - Number of non-predicated instructions.
Evan Cheng8239daf2010-11-03 00:45:17 +0000101 /// ExtraCost - Extra cost for multi-cycle instructions.
102 /// ExtraCost2 - Some instructions are slower when predicated
Evan Chenga13aa952007-05-23 07:23:16 +0000103 /// BB - Corresponding MachineBasicBlock.
104 /// TrueBB / FalseBB- See AnalyzeBranch().
105 /// BrCond - Conditions for end of block conditional branches.
106 /// Predicate - Predicate used in the BB.
Evan Cheng4e654852007-05-16 02:00:57 +0000107 struct BBInfo {
Evan Cheng9618bca2007-06-11 22:26:22 +0000108 bool IsDone : 1;
109 bool IsBeingAnalyzed : 1;
110 bool IsAnalyzed : 1;
111 bool IsEnqueued : 1;
112 bool IsBrAnalyzable : 1;
113 bool HasFallThrough : 1;
114 bool IsUnpredicable : 1;
Evan Chenga2acf842007-06-15 21:18:05 +0000115 bool CannotBeCopied : 1;
Evan Cheng9618bca2007-06-11 22:26:22 +0000116 bool ClobbersPred : 1;
Evan Chenga13aa952007-05-23 07:23:16 +0000117 unsigned NonPredSize;
Bob Wilson2ad40d32010-10-26 00:02:21 +0000118 unsigned ExtraCost;
Evan Cheng8239daf2010-11-03 00:45:17 +0000119 unsigned ExtraCost2;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000120 MachineBasicBlock *BB;
121 MachineBasicBlock *TrueBB;
122 MachineBasicBlock *FalseBB;
Owen Anderson44eb65c2008-08-14 22:49:33 +0000123 SmallVector<MachineOperand, 4> BrCond;
124 SmallVector<MachineOperand, 4> Predicate;
Evan Chenge882fca2007-06-16 09:34:52 +0000125 BBInfo() : IsDone(false), IsBeingAnalyzed(false),
Evan Cheng9618bca2007-06-11 22:26:22 +0000126 IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
127 HasFallThrough(false), IsUnpredicable(false),
Evan Chenga2acf842007-06-15 21:18:05 +0000128 CannotBeCopied(false), ClobbersPred(false), NonPredSize(0),
Evan Cheng8239daf2010-11-03 00:45:17 +0000129 ExtraCost(0), ExtraCost2(0), BB(0), TrueBB(0), FalseBB(0) {}
Evan Chenge882fca2007-06-16 09:34:52 +0000130 };
131
Bob Wilson669db042010-06-15 18:19:27 +0000132 /// IfcvtToken - Record information about pending if-conversions to attempt:
Evan Chenge882fca2007-06-16 09:34:52 +0000133 /// BBI - Corresponding BBInfo.
134 /// Kind - Type of block. See IfcvtKind.
Bob Wilson9c4856a2009-05-13 23:25:24 +0000135 /// NeedSubsumption - True if the to-be-predicated BB has already been
Evan Chenge882fca2007-06-16 09:34:52 +0000136 /// predicated.
Evan Chenga1a87872007-06-18 08:37:25 +0000137 /// NumDups - Number of instructions that would be duplicated due
138 /// to this if-conversion. (For diamonds, the number of
139 /// identical instructions at the beginnings of both
140 /// paths).
141 /// NumDups2 - For diamonds, the number of identical instructions
142 /// at the ends of both paths.
Evan Chenge882fca2007-06-16 09:34:52 +0000143 struct IfcvtToken {
144 BBInfo &BBI;
145 IfcvtKind Kind;
Bob Wilson9c4856a2009-05-13 23:25:24 +0000146 bool NeedSubsumption;
Evan Chenga1a87872007-06-18 08:37:25 +0000147 unsigned NumDups;
148 unsigned NumDups2;
149 IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0)
Bob Wilson9c4856a2009-05-13 23:25:24 +0000150 : BBI(b), Kind(k), NeedSubsumption(s), NumDups(d), NumDups2(d2) {}
Evan Cheng4e654852007-05-16 02:00:57 +0000151 };
152
153 /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
154 /// basic block number.
155 std::vector<BBInfo> BBAnalysis;
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +0000156 TargetSchedModel SchedModel;
Evan Cheng4e654852007-05-16 02:00:57 +0000157
Benjamin Kramer69e42db2013-01-11 20:05:37 +0000158 const TargetLoweringBase *TLI;
Evan Cheng4e654852007-05-16 02:00:57 +0000159 const TargetInstrInfo *TII;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000160 const TargetRegisterInfo *TRI;
Jakub Staszak990f78d2011-08-03 22:34:43 +0000161 const MachineBranchProbabilityInfo *MBPI;
Evan Cheng791e2e02012-06-08 21:53:50 +0000162 MachineRegisterInfo *MRI;
Jakub Staszak990f78d2011-08-03 22:34:43 +0000163
Evan Cheng791e2e02012-06-08 21:53:50 +0000164 bool PreRegAlloc;
Evan Cheng4e654852007-05-16 02:00:57 +0000165 bool MadeChange;
Owen Anderson13bbe4b2009-06-24 23:41:44 +0000166 int FnNum;
Evan Cheng4e654852007-05-16 02:00:57 +0000167 public:
168 static char ID;
Owen Anderson081c34b2010-10-19 17:21:58 +0000169 IfConverter() : MachineFunctionPass(ID), FnNum(-1) {
170 initializeIfConverterPass(*PassRegistry::getPassRegistry());
171 }
Jakub Staszak990f78d2011-08-03 22:34:43 +0000172
Owen Anderson7571ee72010-09-28 20:42:15 +0000173 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Jakub Staszak990f78d2011-08-03 22:34:43 +0000174 AU.addRequired<MachineBranchProbabilityInfo>();
Owen Anderson7571ee72010-09-28 20:42:15 +0000175 MachineFunctionPass::getAnalysisUsage(AU);
176 }
Evan Cheng4e654852007-05-16 02:00:57 +0000177
178 virtual bool runOnMachineFunction(MachineFunction &MF);
Evan Cheng4e654852007-05-16 02:00:57 +0000179
180 private:
Evan Chengb6665f62007-06-04 06:47:22 +0000181 bool ReverseBranchCondition(BBInfo &BBI);
Owen Andersone3cc84a2010-10-01 22:45:50 +0000182 bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000183 const BranchProbability &Prediction) const;
Evan Chengac5f1422007-06-08 09:36:04 +0000184 bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
Owen Andersone3cc84a2010-10-01 22:45:50 +0000185 bool FalseBranch, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000186 const BranchProbability &Prediction) const;
Evan Chenga1a87872007-06-18 08:37:25 +0000187 bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
188 unsigned &Dups1, unsigned &Dups2) const;
Evan Chengac5f1422007-06-08 09:36:04 +0000189 void ScanInstructions(BBInfo &BBI);
Evan Chenge882fca2007-06-16 09:34:52 +0000190 BBInfo &AnalyzeBlock(MachineBasicBlock *BB,
191 std::vector<IfcvtToken*> &Tokens);
Owen Anderson44eb65c2008-08-14 22:49:33 +0000192 bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond,
Evan Chengac5f1422007-06-08 09:36:04 +0000193 bool isTriangle = false, bool RevBranch = false);
Bob Wilson6fb124b2010-06-15 18:57:15 +0000194 void AnalyzeBlocks(MachineFunction &MF, std::vector<IfcvtToken*> &Tokens);
Evan Chenga1a87872007-06-18 08:37:25 +0000195 void InvalidatePreds(MachineBasicBlock *BB);
Evan Cheng7e75ba82007-06-08 22:01:07 +0000196 void RemoveExtraEdges(BBInfo &BBI);
Evan Chenge882fca2007-06-16 09:34:52 +0000197 bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
198 bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
Evan Chenga1a87872007-06-18 08:37:25 +0000199 bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
200 unsigned NumDups1, unsigned NumDups2);
Evan Chenga13aa952007-05-23 07:23:16 +0000201 void PredicateBlock(BBInfo &BBI,
Evan Chenga1a87872007-06-18 08:37:25 +0000202 MachineBasicBlock::iterator E,
Evan Cheng46df4eb2010-06-16 07:35:02 +0000203 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng8787c5f2011-12-19 22:01:30 +0000204 SmallSet<unsigned, 4> &Redefs,
205 SmallSet<unsigned, 4> *LaterRedefs = 0);
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000206 void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000207 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng46df4eb2010-06-16 07:35:02 +0000208 SmallSet<unsigned, 4> &Redefs,
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000209 bool IgnoreBr = false);
Bob Wilson8eab75f2010-06-29 00:55:23 +0000210 void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
Evan Cheng5f702182007-06-01 00:12:12 +0000211
Evan Cheng8239daf2010-11-03 00:45:17 +0000212 bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
213 unsigned Cycle, unsigned Extra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000214 const BranchProbability &Prediction) const {
Evan Cheng8239daf2010-11-03 00:45:17 +0000215 return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000216 Prediction);
Evan Cheng13151432010-06-25 22:42:03 +0000217 }
218
Evan Cheng8239daf2010-11-03 00:45:17 +0000219 bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB,
220 unsigned TCycle, unsigned TExtra,
221 MachineBasicBlock &FBB,
222 unsigned FCycle, unsigned FExtra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000223 const BranchProbability &Prediction) const {
Evan Cheng8239daf2010-11-03 00:45:17 +0000224 return TCycle > 0 && FCycle > 0 &&
225 TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000226 Prediction);
Evan Chenga1a87872007-06-18 08:37:25 +0000227 }
228
Evan Chengd4de6d92007-06-07 02:12:15 +0000229 // blockAlwaysFallThrough - Block ends without a terminator.
230 bool blockAlwaysFallThrough(BBInfo &BBI) const {
Evan Cheng9618bca2007-06-11 22:26:22 +0000231 return BBI.IsBrAnalyzable && BBI.TrueBB == NULL;
Evan Cheng7a655472007-06-06 10:16:17 +0000232 }
233
Evan Chenge882fca2007-06-16 09:34:52 +0000234 // IfcvtTokenCmp - Used to sort if-conversion candidates.
235 static bool IfcvtTokenCmp(IfcvtToken *C1, IfcvtToken *C2) {
Evan Chenga1a87872007-06-18 08:37:25 +0000236 int Incr1 = (C1->Kind == ICDiamond)
237 ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
238 int Incr2 = (C2->Kind == ICDiamond)
239 ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
240 if (Incr1 > Incr2)
Evan Chenge882fca2007-06-16 09:34:52 +0000241 return true;
Evan Chenga1a87872007-06-18 08:37:25 +0000242 else if (Incr1 == Incr2) {
Bob Wilson9c4856a2009-05-13 23:25:24 +0000243 // Favors subsumption.
244 if (C1->NeedSubsumption == false && C2->NeedSubsumption == true)
Evan Chenge882fca2007-06-16 09:34:52 +0000245 return true;
Bob Wilson9c4856a2009-05-13 23:25:24 +0000246 else if (C1->NeedSubsumption == C2->NeedSubsumption) {
Evan Chenge882fca2007-06-16 09:34:52 +0000247 // Favors diamond over triangle, etc.
248 if ((unsigned)C1->Kind < (unsigned)C2->Kind)
249 return true;
250 else if (C1->Kind == C2->Kind)
251 return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
252 }
253 }
254 return false;
Evan Cheng5f702182007-06-01 00:12:12 +0000255 }
Evan Cheng4e654852007-05-16 02:00:57 +0000256 };
Evan Chenge882fca2007-06-16 09:34:52 +0000257
Evan Cheng4e654852007-05-16 02:00:57 +0000258 char IfConverter::ID = 0;
259}
260
Andrew Trick1dd8c852012-02-08 21:23:13 +0000261char &llvm::IfConverterID = IfConverter::ID;
262
Owen Anderson2ab36d32010-10-12 19:48:12 +0000263INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false)
Jakub Staszak990f78d2011-08-03 22:34:43 +0000264INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Owen Anderson2ab36d32010-10-12 19:48:12 +0000265INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false)
Bob Wilsona5971032009-10-28 20:46:46 +0000266
Evan Cheng4e654852007-05-16 02:00:57 +0000267bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng86cbfea2007-05-18 00:20:58 +0000268 TLI = MF.getTarget().getTargetLowering();
Evan Cheng4e654852007-05-16 02:00:57 +0000269 TII = MF.getTarget().getInstrInfo();
Evan Cheng46df4eb2010-06-16 07:35:02 +0000270 TRI = MF.getTarget().getRegisterInfo();
Jakub Staszak990f78d2011-08-03 22:34:43 +0000271 MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Evan Cheng791e2e02012-06-08 21:53:50 +0000272 MRI = &MF.getRegInfo();
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +0000273
274 const TargetSubtargetInfo &ST =
275 MF.getTarget().getSubtarget<TargetSubtargetInfo>();
276 SchedModel.init(*ST.getSchedModel(), &ST, TII);
277
Evan Cheng4e654852007-05-16 02:00:57 +0000278 if (!TII) return false;
279
Evan Cheng791e2e02012-06-08 21:53:50 +0000280 PreRegAlloc = MRI->isSSA();
281
282 bool BFChange = false;
283 if (!PreRegAlloc) {
284 // Tail merge tend to expose more if-conversion opportunities.
285 BranchFolder BF(true, false);
286 BFChange = BF.OptimizeFunction(MF, TII,
Evan Cheng86050dc2010-06-18 23:09:54 +0000287 MF.getTarget().getRegisterInfo(),
288 getAnalysisIfAvailable<MachineModuleInfo>());
Evan Cheng791e2e02012-06-08 21:53:50 +0000289 }
Evan Cheng86050dc2010-06-18 23:09:54 +0000290
David Greene083b7ff2010-01-04 22:02:01 +0000291 DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'"
Craig Topper96601ca2012-08-22 06:07:19 +0000292 << MF.getName() << "\'");
Evan Chengedf48962007-06-08 19:10:51 +0000293
294 if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
David Greene083b7ff2010-01-04 22:02:01 +0000295 DEBUG(dbgs() << " skipped\n");
Evan Chengedf48962007-06-08 19:10:51 +0000296 return false;
297 }
David Greene083b7ff2010-01-04 22:02:01 +0000298 DEBUG(dbgs() << "\n");
Evan Cheng5f702182007-06-01 00:12:12 +0000299
Evan Cheng4e654852007-05-16 02:00:57 +0000300 MF.RenumberBlocks();
Evan Cheng5f702182007-06-01 00:12:12 +0000301 BBAnalysis.resize(MF.getNumBlockIDs());
Evan Cheng4e654852007-05-16 02:00:57 +0000302
Evan Chenge882fca2007-06-16 09:34:52 +0000303 std::vector<IfcvtToken*> Tokens;
Evan Cheng47d25022007-05-18 01:55:58 +0000304 MadeChange = false;
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000305 unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
306 NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
307 while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
Bob Wilson9c4856a2009-05-13 23:25:24 +0000308 // Do an initial analysis for each basic block and find all the potential
309 // candidates to perform if-conversion.
Bob Wilson6fb124b2010-06-15 18:57:15 +0000310 bool Change = false;
311 AnalyzeBlocks(MF, Tokens);
Evan Chenge882fca2007-06-16 09:34:52 +0000312 while (!Tokens.empty()) {
313 IfcvtToken *Token = Tokens.back();
314 Tokens.pop_back();
315 BBInfo &BBI = Token->BBI;
316 IfcvtKind Kind = Token->Kind;
Nuno Lopes7ecbfd12008-11-04 13:02:59 +0000317 unsigned NumDups = Token->NumDups;
Duncan Sands20d629c2008-11-04 18:05:30 +0000318 unsigned NumDups2 = Token->NumDups2;
Nuno Lopes7ecbfd12008-11-04 13:02:59 +0000319
320 delete Token;
Evan Chengb6665f62007-06-04 06:47:22 +0000321
Evan Cheng9618bca2007-06-11 22:26:22 +0000322 // If the block has been evicted out of the queue or it has already been
323 // marked dead (due to it being predicated), then skip it.
Evan Chenge882fca2007-06-16 09:34:52 +0000324 if (BBI.IsDone)
325 BBI.IsEnqueued = false;
326 if (!BBI.IsEnqueued)
Evan Cheng9618bca2007-06-11 22:26:22 +0000327 continue;
Evan Chenge882fca2007-06-16 09:34:52 +0000328
Evan Cheng86ff2962007-06-14 20:28:52 +0000329 BBI.IsEnqueued = false;
Evan Cheng9618bca2007-06-11 22:26:22 +0000330
Evan Chengb6665f62007-06-04 06:47:22 +0000331 bool RetVal = false;
Evan Chenge882fca2007-06-16 09:34:52 +0000332 switch (Kind) {
Craig Topper5e25ee82012-02-05 08:31:47 +0000333 default: llvm_unreachable("Unexpected!");
Evan Chengb6665f62007-06-04 06:47:22 +0000334 case ICSimple:
Evan Chengcb78d672007-06-06 01:12:44 +0000335 case ICSimpleFalse: {
Evan Chenge882fca2007-06-16 09:34:52 +0000336 bool isFalse = Kind == ICSimpleFalse;
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000337 if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
Bob Wilson92fffe02010-06-15 22:18:54 +0000338 DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ?
339 " false" : "")
Bill Wendling44ff7942009-08-22 20:11:17 +0000340 << "): BB#" << BBI.BB->getNumber() << " ("
341 << ((Kind == ICSimpleFalse)
342 ? BBI.FalseBB->getNumber()
343 : BBI.TrueBB->getNumber()) << ") ");
Evan Chenge882fca2007-06-16 09:34:52 +0000344 RetVal = IfConvertSimple(BBI, Kind);
David Greene083b7ff2010-01-04 22:02:01 +0000345 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000346 if (RetVal) {
Dan Gohmanfe601042010-06-22 15:08:57 +0000347 if (isFalse) ++NumSimpleFalse;
348 else ++NumSimple;
Anton Korobeynikov4c71dfe2008-02-20 11:10:28 +0000349 }
Evan Chengb6665f62007-06-04 06:47:22 +0000350 break;
Evan Chengcb78d672007-06-06 01:12:44 +0000351 }
Evan Chenga13aa952007-05-23 07:23:16 +0000352 case ICTriangle:
Evan Chengcc8fb462007-06-12 23:54:05 +0000353 case ICTriangleRev:
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000354 case ICTriangleFalse:
Reid Spencera9bf49c2007-06-10 00:19:17 +0000355 case ICTriangleFRev: {
Evan Chenge882fca2007-06-16 09:34:52 +0000356 bool isFalse = Kind == ICTriangleFalse;
357 bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
Evan Chengcc8fb462007-06-12 23:54:05 +0000358 if (DisableTriangle && !isFalse && !isRev) break;
359 if (DisableTriangleR && !isFalse && isRev) break;
360 if (DisableTriangleF && isFalse && !isRev) break;
361 if (DisableTriangleFR && isFalse && isRev) break;
David Greene083b7ff2010-01-04 22:02:01 +0000362 DEBUG(dbgs() << "Ifcvt (Triangle");
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000363 if (isFalse)
David Greene083b7ff2010-01-04 22:02:01 +0000364 DEBUG(dbgs() << " false");
Evan Chengcc8fb462007-06-12 23:54:05 +0000365 if (isRev)
David Greene083b7ff2010-01-04 22:02:01 +0000366 DEBUG(dbgs() << " rev");
367 DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendling44ff7942009-08-22 20:11:17 +0000368 << BBI.TrueBB->getNumber() << ",F:"
369 << BBI.FalseBB->getNumber() << ") ");
Evan Chenge882fca2007-06-16 09:34:52 +0000370 RetVal = IfConvertTriangle(BBI, Kind);
David Greene083b7ff2010-01-04 22:02:01 +0000371 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000372 if (RetVal) {
Evan Chengcc8fb462007-06-12 23:54:05 +0000373 if (isFalse) {
Dan Gohmanfe601042010-06-22 15:08:57 +0000374 if (isRev) ++NumTriangleFRev;
375 else ++NumTriangleFalse;
Evan Chengcc8fb462007-06-12 23:54:05 +0000376 } else {
Dan Gohmanfe601042010-06-22 15:08:57 +0000377 if (isRev) ++NumTriangleRev;
378 else ++NumTriangle;
Evan Chengcc8fb462007-06-12 23:54:05 +0000379 }
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000380 }
Evan Chenga13aa952007-05-23 07:23:16 +0000381 break;
Reid Spencera9bf49c2007-06-10 00:19:17 +0000382 }
Evan Chenge882fca2007-06-16 09:34:52 +0000383 case ICDiamond: {
Evan Chengedf48962007-06-08 19:10:51 +0000384 if (DisableDiamond) break;
David Greene083b7ff2010-01-04 22:02:01 +0000385 DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
Bill Wendling44ff7942009-08-22 20:11:17 +0000386 << BBI.TrueBB->getNumber() << ",F:"
387 << BBI.FalseBB->getNumber() << ") ");
Nuno Lopes7ecbfd12008-11-04 13:02:59 +0000388 RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2);
David Greene083b7ff2010-01-04 22:02:01 +0000389 DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
Dan Gohmanfe601042010-06-22 15:08:57 +0000390 if (RetVal) ++NumDiamonds;
Evan Chenga13aa952007-05-23 07:23:16 +0000391 break;
392 }
Evan Chenge882fca2007-06-16 09:34:52 +0000393 }
394
Evan Chengb6665f62007-06-04 06:47:22 +0000395 Change |= RetVal;
Evan Chengedf48962007-06-08 19:10:51 +0000396
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000397 NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
398 NumTriangleFalse + NumTriangleFRev + NumDiamonds;
399 if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
Evan Chengedf48962007-06-08 19:10:51 +0000400 break;
Evan Cheng4e654852007-05-16 02:00:57 +0000401 }
Evan Chenga13aa952007-05-23 07:23:16 +0000402
Evan Chenga13aa952007-05-23 07:23:16 +0000403 if (!Change)
404 break;
Evan Chengb6665f62007-06-04 06:47:22 +0000405 MadeChange |= Change;
Evan Cheng4e654852007-05-16 02:00:57 +0000406 }
Evan Cheng47d25022007-05-18 01:55:58 +0000407
Evan Chenge882fca2007-06-16 09:34:52 +0000408 // Delete tokens in case of early exit.
409 while (!Tokens.empty()) {
410 IfcvtToken *Token = Tokens.back();
411 Tokens.pop_back();
412 delete Token;
413 }
414
415 Tokens.clear();
Evan Cheng47d25022007-05-18 01:55:58 +0000416 BBAnalysis.clear();
417
Evan Cheng6a5e2832010-06-18 22:17:13 +0000418 if (MadeChange && IfCvtBranchFold) {
Evan Chengcbc988b2011-05-12 00:56:58 +0000419 BranchFolder BF(false, false);
Evan Cheng030a0a02009-09-04 07:47:40 +0000420 BF.OptimizeFunction(MF, TII,
421 MF.getTarget().getRegisterInfo(),
422 getAnalysisIfAvailable<MachineModuleInfo>());
423 }
424
Evan Cheng86050dc2010-06-18 23:09:54 +0000425 MadeChange |= BFChange;
Evan Cheng4e654852007-05-16 02:00:57 +0000426 return MadeChange;
427}
428
Evan Chenge882fca2007-06-16 09:34:52 +0000429/// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
430/// its 'true' successor.
Evan Cheng4e654852007-05-16 02:00:57 +0000431static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
Evan Cheng86cbfea2007-05-18 00:20:58 +0000432 MachineBasicBlock *TrueBB) {
Evan Cheng4e654852007-05-16 02:00:57 +0000433 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
434 E = BB->succ_end(); SI != E; ++SI) {
435 MachineBasicBlock *SuccBB = *SI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000436 if (SuccBB != TrueBB)
Evan Cheng4e654852007-05-16 02:00:57 +0000437 return SuccBB;
438 }
439 return NULL;
440}
441
Evan Chenge882fca2007-06-16 09:34:52 +0000442/// ReverseBranchCondition - Reverse the condition of the end of the block
Bob Wilson9c4856a2009-05-13 23:25:24 +0000443/// branch. Swap block's 'true' and 'false' successors.
Evan Chengb6665f62007-06-04 06:47:22 +0000444bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
Stuart Hastings3bf91252010-06-17 22:43:56 +0000445 DebugLoc dl; // FIXME: this is nowhere
Evan Chengb6665f62007-06-04 06:47:22 +0000446 if (!TII->ReverseBranchCondition(BBI.BrCond)) {
447 TII->RemoveBranch(*BBI.BB);
Stuart Hastings3bf91252010-06-17 22:43:56 +0000448 TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl);
Evan Chengb6665f62007-06-04 06:47:22 +0000449 std::swap(BBI.TrueBB, BBI.FalseBB);
450 return true;
451 }
452 return false;
453}
454
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000455/// getNextBlock - Returns the next block in the function blocks ordering. If
456/// it is the end, returns NULL.
457static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) {
458 MachineFunction::iterator I = BB;
459 MachineFunction::iterator E = BB->getParent()->end();
460 if (++I == E)
461 return NULL;
462 return I;
463}
464
Evan Chengac5f1422007-06-08 09:36:04 +0000465/// ValidSimple - Returns true if the 'true' block (along with its
Evan Chenge882fca2007-06-16 09:34:52 +0000466/// predecessor) forms a valid simple shape for ifcvt. It also returns the
467/// number of instructions that the ifcvt would need to duplicate if performed
468/// in Dups.
Owen Anderson7571ee72010-09-28 20:42:15 +0000469bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000470 const BranchProbability &Prediction) const {
Evan Chenge882fca2007-06-16 09:34:52 +0000471 Dups = 0;
Evan Chenga1a87872007-06-18 08:37:25 +0000472 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng9618bca2007-06-11 22:26:22 +0000473 return false;
474
Evan Cheng9ffd3402007-06-19 21:45:13 +0000475 if (TrueBBI.IsBrAnalyzable)
476 return false;
477
Evan Chenga2acf842007-06-15 21:18:05 +0000478 if (TrueBBI.BB->pred_size() > 1) {
479 if (TrueBBI.CannotBeCopied ||
Owen Anderson7571ee72010-09-28 20:42:15 +0000480 !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000481 Prediction))
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000482 return false;
Evan Chenge882fca2007-06-16 09:34:52 +0000483 Dups = TrueBBI.NonPredSize;
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000484 }
485
Evan Cheng9ffd3402007-06-19 21:45:13 +0000486 return true;
Evan Cheng7a655472007-06-06 10:16:17 +0000487}
488
Evan Chengac5f1422007-06-08 09:36:04 +0000489/// ValidTriangle - Returns true if the 'true' and 'false' blocks (along
Evan Chengd4de6d92007-06-07 02:12:15 +0000490/// with their common predecessor) forms a valid triangle shape for ifcvt.
Evan Chenge882fca2007-06-16 09:34:52 +0000491/// If 'FalseBranch' is true, it checks if 'true' block's false branch
Bob Wilson92fffe02010-06-15 22:18:54 +0000492/// branches to the 'false' block rather than the other way around. It also
Evan Chenge882fca2007-06-16 09:34:52 +0000493/// returns the number of instructions that the ifcvt would need to duplicate
494/// if performed in 'Dups'.
Evan Chengac5f1422007-06-08 09:36:04 +0000495bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
Owen Andersone3cc84a2010-10-01 22:45:50 +0000496 bool FalseBranch, unsigned &Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000497 const BranchProbability &Prediction) const {
Evan Chenge882fca2007-06-16 09:34:52 +0000498 Dups = 0;
Evan Chenga1a87872007-06-18 08:37:25 +0000499 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
Evan Cheng9618bca2007-06-11 22:26:22 +0000500 return false;
501
Evan Chenga2acf842007-06-15 21:18:05 +0000502 if (TrueBBI.BB->pred_size() > 1) {
503 if (TrueBBI.CannotBeCopied)
504 return false;
505
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000506 unsigned Size = TrueBBI.NonPredSize;
Evan Chenge882fca2007-06-16 09:34:52 +0000507 if (TrueBBI.IsBrAnalyzable) {
Dan Gohman30359592008-01-29 13:02:09 +0000508 if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
Bob Wilson9c4856a2009-05-13 23:25:24 +0000509 // Ends with an unconditional branch. It will be removed.
Evan Chenge882fca2007-06-16 09:34:52 +0000510 --Size;
511 else {
512 MachineBasicBlock *FExit = FalseBranch
513 ? TrueBBI.TrueBB : TrueBBI.FalseBB;
514 if (FExit)
515 // Require a conditional branch
516 ++Size;
517 }
518 }
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000519 if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction))
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000520 return false;
Evan Chenge882fca2007-06-16 09:34:52 +0000521 Dups = Size;
Evan Cheng2c8c3a42007-06-15 07:36:12 +0000522 }
Evan Chengd4de6d92007-06-07 02:12:15 +0000523
Evan Chengac5f1422007-06-08 09:36:04 +0000524 MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
525 if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
Evan Chengd4de6d92007-06-07 02:12:15 +0000526 MachineFunction::iterator I = TrueBBI.BB;
527 if (++I == TrueBBI.BB->getParent()->end())
528 return false;
Evan Chengac5f1422007-06-08 09:36:04 +0000529 TExit = I;
Evan Chengd4de6d92007-06-07 02:12:15 +0000530 }
Evan Chengac5f1422007-06-08 09:36:04 +0000531 return TExit && TExit == FalseBBI.BB;
Evan Chengd4de6d92007-06-07 02:12:15 +0000532}
533
Evan Chengac5f1422007-06-08 09:36:04 +0000534/// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
Evan Chengd4de6d92007-06-07 02:12:15 +0000535/// with their common predecessor) forms a valid diamond shape for ifcvt.
Evan Chenga1a87872007-06-18 08:37:25 +0000536bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
537 unsigned &Dups1, unsigned &Dups2) const {
538 Dups1 = Dups2 = 0;
539 if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
540 FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
Evan Cheng9618bca2007-06-11 22:26:22 +0000541 return false;
542
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000543 MachineBasicBlock *TT = TrueBBI.TrueBB;
544 MachineBasicBlock *FT = FalseBBI.TrueBB;
545
546 if (!TT && blockAlwaysFallThrough(TrueBBI))
547 TT = getNextBlock(TrueBBI.BB);
548 if (!FT && blockAlwaysFallThrough(FalseBBI))
549 FT = getNextBlock(FalseBBI.BB);
550 if (TT != FT)
551 return false;
Evan Cheng9618bca2007-06-11 22:26:22 +0000552 if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
Evan Cheng1c9f91d2007-06-09 01:03:43 +0000553 return false;
Evan Chengc4047a82007-06-18 22:44:57 +0000554 if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
555 return false;
556
557 // FIXME: Allow true block to have an early exit?
558 if (TrueBBI.FalseBB || FalseBBI.FalseBB ||
559 (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred))
Evan Chenga1a87872007-06-18 08:37:25 +0000560 return false;
561
Bob Wilson7c730e72010-10-26 00:02:24 +0000562 // Count duplicate instructions at the beginning of the true and false blocks.
Jim Grosbach66f360e2010-06-07 21:28:55 +0000563 MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
564 MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
Bob Wilson7c730e72010-10-26 00:02:24 +0000565 MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
566 MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
567 while (TIB != TIE && FIB != FIE) {
Evan Chenga9934dc2010-06-18 21:52:57 +0000568 // Skip dbg_value instructions. These do not count.
Bob Wilson7c730e72010-10-26 00:02:24 +0000569 if (TIB->isDebugValue()) {
570 while (TIB != TIE && TIB->isDebugValue())
571 ++TIB;
572 if (TIB == TIE)
Evan Chenga9934dc2010-06-18 21:52:57 +0000573 break;
574 }
Bob Wilson7c730e72010-10-26 00:02:24 +0000575 if (FIB->isDebugValue()) {
576 while (FIB != FIE && FIB->isDebugValue())
577 ++FIB;
578 if (FIB == FIE)
Evan Chenga9934dc2010-06-18 21:52:57 +0000579 break;
580 }
Bob Wilson7c730e72010-10-26 00:02:24 +0000581 if (!TIB->isIdenticalTo(FIB))
582 break;
583 ++Dups1;
584 ++TIB;
585 ++FIB;
586 }
587
588 // Now, in preparation for counting duplicate instructions at the ends of the
589 // blocks, move the end iterators up past any branch instructions.
590 while (TIE != TIB) {
591 --TIE;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000592 if (!TIE->isBranch())
Bob Wilson7c730e72010-10-26 00:02:24 +0000593 break;
594 }
595 while (FIE != FIB) {
596 --FIE;
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000597 if (!FIE->isBranch())
Bob Wilson7c730e72010-10-26 00:02:24 +0000598 break;
599 }
600
601 // If Dups1 includes all of a block, then don't count duplicate
602 // instructions at the end of the blocks.
603 if (TIB == TIE || FIB == FIE)
604 return true;
605
606 // Count duplicate instructions at the ends of the blocks.
607 while (TIE != TIB && FIE != FIB) {
608 // Skip dbg_value instructions. These do not count.
609 if (TIE->isDebugValue()) {
610 while (TIE != TIB && TIE->isDebugValue())
611 --TIE;
612 if (TIE == TIB)
613 break;
614 }
615 if (FIE->isDebugValue()) {
616 while (FIE != FIB && FIE->isDebugValue())
617 --FIE;
618 if (FIE == FIB)
619 break;
620 }
621 if (!TIE->isIdenticalTo(FIE))
Evan Chenga1a87872007-06-18 08:37:25 +0000622 break;
623 ++Dups2;
Bob Wilson7c730e72010-10-26 00:02:24 +0000624 --TIE;
625 --FIE;
Evan Chenga1a87872007-06-18 08:37:25 +0000626 }
627
628 return true;
Evan Chengd4de6d92007-06-07 02:12:15 +0000629}
630
Evan Cheng9618bca2007-06-11 22:26:22 +0000631/// ScanInstructions - Scan all the instructions in the block to determine if
632/// the block is predicable. In most cases, that means all the instructions
Chris Lattner69244302008-01-07 01:56:04 +0000633/// in the block are isPredicable(). Also checks if the block contains any
Evan Cheng9618bca2007-06-11 22:26:22 +0000634/// instruction which can clobber a predicate (e.g. condition code register).
635/// If so, the block is not predicable unless it's the last instruction.
636void IfConverter::ScanInstructions(BBInfo &BBI) {
637 if (BBI.IsDone)
638 return;
639
Evan Cheng791e2e02012-06-08 21:53:50 +0000640 bool AlreadyPredicated = !BBI.Predicate.empty();
Evan Cheng9618bca2007-06-11 22:26:22 +0000641 // First analyze the end of BB branches.
Evan Chengb7c908b2007-06-14 21:26:08 +0000642 BBI.TrueBB = BBI.FalseBB = NULL;
Evan Cheng9618bca2007-06-11 22:26:22 +0000643 BBI.BrCond.clear();
644 BBI.IsBrAnalyzable =
645 !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
646 BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL;
647
648 if (BBI.BrCond.size()) {
649 // No false branch. This BB must end with a conditional branch and a
650 // fallthrough.
651 if (!BBI.FalseBB)
Bob Wilson92fffe02010-06-15 22:18:54 +0000652 BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
Evan Cheng2077e182009-06-15 21:24:34 +0000653 if (!BBI.FalseBB) {
654 // Malformed bcc? True and false blocks are the same?
655 BBI.IsUnpredicable = true;
656 return;
657 }
Evan Cheng9618bca2007-06-11 22:26:22 +0000658 }
659
660 // Then scan all the instructions.
661 BBI.NonPredSize = 0;
Bob Wilson2ad40d32010-10-26 00:02:21 +0000662 BBI.ExtraCost = 0;
Evan Cheng8239daf2010-11-03 00:45:17 +0000663 BBI.ExtraCost2 = 0;
Evan Cheng9618bca2007-06-11 22:26:22 +0000664 BBI.ClobbersPred = false;
Evan Cheng9618bca2007-06-11 22:26:22 +0000665 for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
666 I != E; ++I) {
Jim Grosbach870c8052010-06-04 23:01:26 +0000667 if (I->isDebugValue())
668 continue;
669
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000670 if (I->isNotDuplicable())
Evan Chenga2acf842007-06-15 21:18:05 +0000671 BBI.CannotBeCopied = true;
672
Evan Cheng9618bca2007-06-11 22:26:22 +0000673 bool isPredicated = TII->isPredicated(I);
Evan Cheng5a96b3d2011-12-07 07:15:52 +0000674 bool isCondBr = BBI.IsBrAnalyzable && I->isConditionalBranch();
Evan Cheng9618bca2007-06-11 22:26:22 +0000675
Joey Goulyb57d9962013-09-09 14:21:49 +0000676 // A conditional branch is not predicable, but it may be eliminated.
677 if (isCondBr)
678 continue;
679
680 if (!isPredicated) {
681 BBI.NonPredSize++;
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +0000682 unsigned ExtraPredCost = TII->getPredicationCost(&*I);
683 unsigned NumCycles = SchedModel.computeInstrLatency(&*I, false);
Joey Goulyb57d9962013-09-09 14:21:49 +0000684 if (NumCycles > 1)
685 BBI.ExtraCost += NumCycles-1;
686 BBI.ExtraCost2 += ExtraPredCost;
687 } else if (!AlreadyPredicated) {
688 // FIXME: This instruction is already predicated before the
689 // if-conversion pass. It's probably something like a conditional move.
690 // Mark this block unpredicable for now.
691 BBI.IsUnpredicable = true;
692 return;
Evan Chengd2c5eb82007-07-06 23:24:39 +0000693 }
Evan Cheng9618bca2007-06-11 22:26:22 +0000694
695 if (BBI.ClobbersPred && !isPredicated) {
696 // Predicate modification instruction should end the block (except for
697 // already predicated instructions and end of block branches).
Evan Cheng9618bca2007-06-11 22:26:22 +0000698 // Predicate may have been modified, the subsequent (currently)
Evan Chengd2c5eb82007-07-06 23:24:39 +0000699 // unpredicated instructions cannot be correctly predicated.
Evan Cheng9618bca2007-06-11 22:26:22 +0000700 BBI.IsUnpredicable = true;
701 return;
702 }
703
Evan Cheng11ce02d2007-07-10 17:50:43 +0000704 // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
705 // still potentially predicable.
706 std::vector<MachineOperand> PredDefs;
707 if (TII->DefinesPredicate(I, PredDefs))
Evan Cheng9618bca2007-06-11 22:26:22 +0000708 BBI.ClobbersPred = true;
709
Evan Chenge54cb162009-11-21 06:20:26 +0000710 if (!TII->isPredicable(I)) {
Evan Cheng9618bca2007-06-11 22:26:22 +0000711 BBI.IsUnpredicable = true;
712 return;
713 }
714 }
715}
716
717/// FeasibilityAnalysis - Determine if the block is a suitable candidate to be
718/// predicated by the specified predicate.
719bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
Owen Anderson44eb65c2008-08-14 22:49:33 +0000720 SmallVectorImpl<MachineOperand> &Pred,
Evan Cheng9618bca2007-06-11 22:26:22 +0000721 bool isTriangle, bool RevBranch) {
Evan Chenge882fca2007-06-16 09:34:52 +0000722 // If the block is dead or unpredicable, then it cannot be predicated.
723 if (BBI.IsDone || BBI.IsUnpredicable)
Evan Cheng9618bca2007-06-11 22:26:22 +0000724 return false;
725
Quentin Colombete644b772013-07-24 20:20:37 +0000726 // If it is already predicated, check if the new predicate subsumes
727 // its predicate.
728 if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate))
Evan Cheng9618bca2007-06-11 22:26:22 +0000729 return false;
730
731 if (BBI.BrCond.size()) {
732 if (!isTriangle)
733 return false;
734
Bob Wilson9c4856a2009-05-13 23:25:24 +0000735 // Test predicate subsumption.
Owen Anderson44eb65c2008-08-14 22:49:33 +0000736 SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
737 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Cheng9618bca2007-06-11 22:26:22 +0000738 if (RevBranch) {
739 if (TII->ReverseBranchCondition(Cond))
740 return false;
741 }
742 if (TII->ReverseBranchCondition(RevPred) ||
743 !TII->SubsumesPredicate(Cond, RevPred))
744 return false;
745 }
746
747 return true;
748}
749
Evan Chengac5f1422007-06-08 09:36:04 +0000750/// AnalyzeBlock - Analyze the structure of the sub-CFG starting from
Evan Chengcf6cc112007-05-18 18:14:37 +0000751/// the specified block. Record its successors and whether it looks like an
752/// if-conversion candidate.
Evan Chenge882fca2007-06-16 09:34:52 +0000753IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB,
754 std::vector<IfcvtToken*> &Tokens) {
Evan Cheng4e654852007-05-16 02:00:57 +0000755 BBInfo &BBI = BBAnalysis[BB->getNumber()];
756
Evan Cheng9618bca2007-06-11 22:26:22 +0000757 if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed)
758 return BBI;
Evan Cheng86cbfea2007-05-18 00:20:58 +0000759
Evan Cheng9618bca2007-06-11 22:26:22 +0000760 BBI.BB = BB;
761 BBI.IsBeingAnalyzed = true;
Evan Cheng9618bca2007-06-11 22:26:22 +0000762
763 ScanInstructions(BBI);
764
Jakub Staszak2b33f4c2011-07-10 02:00:16 +0000765 // Unanalyzable or ends with fallthrough or unconditional branch, or if is not
766 // considered for ifcvt anymore.
767 if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
Evan Cheng9618bca2007-06-11 22:26:22 +0000768 BBI.IsBeingAnalyzed = false;
769 BBI.IsAnalyzed = true;
770 return BBI;
Evan Cheng7a655472007-06-06 10:16:17 +0000771 }
772
Evan Cheng9618bca2007-06-11 22:26:22 +0000773 // Do not ifcvt if either path is a back edge to the entry block.
774 if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
775 BBI.IsBeingAnalyzed = false;
776 BBI.IsAnalyzed = true;
777 return BBI;
778 }
779
Evan Cheng2077e182009-06-15 21:24:34 +0000780 // Do not ifcvt if true and false fallthrough blocks are the same.
781 if (!BBI.FalseBB) {
782 BBI.IsBeingAnalyzed = false;
783 BBI.IsAnalyzed = true;
784 return BBI;
785 }
786
Evan Chenge882fca2007-06-16 09:34:52 +0000787 BBInfo &TrueBBI = AnalyzeBlock(BBI.TrueBB, Tokens);
788 BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB, Tokens);
Evan Cheng9618bca2007-06-11 22:26:22 +0000789
790 if (TrueBBI.IsDone && FalseBBI.IsDone) {
791 BBI.IsBeingAnalyzed = false;
792 BBI.IsAnalyzed = true;
793 return BBI;
794 }
795
Owen Anderson44eb65c2008-08-14 22:49:33 +0000796 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Chengb6665f62007-06-04 06:47:22 +0000797 bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
Evan Chengac5f1422007-06-08 09:36:04 +0000798
Evan Chenge882fca2007-06-16 09:34:52 +0000799 unsigned Dups = 0;
Evan Chenga1a87872007-06-18 08:37:25 +0000800 unsigned Dups2 = 0;
Evan Cheng791e2e02012-06-08 21:53:50 +0000801 bool TNeedSub = !TrueBBI.Predicate.empty();
802 bool FNeedSub = !FalseBBI.Predicate.empty();
Evan Chenge882fca2007-06-16 09:34:52 +0000803 bool Enqueued = false;
Benjamin Kramer6406d002010-09-29 22:38:50 +0000804
Jakub Staszak990f78d2011-08-03 22:34:43 +0000805 BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB);
806
Evan Chenga1a87872007-06-18 08:37:25 +0000807 if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000808 MeetIfcvtSizeLimit(*TrueBBI.BB, (TrueBBI.NonPredSize - (Dups + Dups2) +
Evan Cheng8239daf2010-11-03 00:45:17 +0000809 TrueBBI.ExtraCost), TrueBBI.ExtraCost2,
Bob Wilson2ad40d32010-10-26 00:02:21 +0000810 *FalseBBI.BB, (FalseBBI.NonPredSize - (Dups + Dups2) +
Evan Cheng8239daf2010-11-03 00:45:17 +0000811 FalseBBI.ExtraCost),FalseBBI.ExtraCost2,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000812 Prediction) &&
Evan Chengac5f1422007-06-08 09:36:04 +0000813 FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
814 FeasibilityAnalysis(FalseBBI, RevCond)) {
Evan Cheng4e654852007-05-16 02:00:57 +0000815 // Diamond:
816 // EBB
817 // / \_
818 // | |
819 // TBB FBB
820 // \ /
Evan Cheng86cbfea2007-05-18 00:20:58 +0000821 // TailBB
Evan Cheng993fc952007-06-05 23:46:14 +0000822 // Note TailBB can be empty.
Evan Chenga1a87872007-06-18 08:37:25 +0000823 Tokens.push_back(new IfcvtToken(BBI, ICDiamond, TNeedSub|FNeedSub, Dups,
824 Dups2));
Evan Chenge882fca2007-06-16 09:34:52 +0000825 Enqueued = true;
826 }
827
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000828 if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000829 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000830 TrueBBI.ExtraCost2, Prediction) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000831 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
832 // Triangle:
833 // EBB
834 // | \_
835 // | |
836 // | TBB
837 // | /
838 // FBB
839 Tokens.push_back(new IfcvtToken(BBI, ICTriangle, TNeedSub, Dups));
840 Enqueued = true;
841 }
Bob Wilson92fffe02010-06-15 22:18:54 +0000842
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000843 if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000844 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000845 TrueBBI.ExtraCost2, Prediction) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000846 FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
847 Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups));
848 Enqueued = true;
849 }
850
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000851 if (ValidSimple(TrueBBI, Dups, Prediction) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000852 MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000853 TrueBBI.ExtraCost2, Prediction) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000854 FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
855 // Simple (split, no rejoin):
856 // EBB
857 // | \_
858 // | |
859 // | TBB---> exit
Bob Wilson92fffe02010-06-15 22:18:54 +0000860 // |
Evan Chenge882fca2007-06-16 09:34:52 +0000861 // FBB
862 Tokens.push_back(new IfcvtToken(BBI, ICSimple, TNeedSub, Dups));
863 Enqueued = true;
864 }
865
866 if (CanRevCond) {
867 // Try the other path...
Owen Andersone3cc84a2010-10-01 22:45:50 +0000868 if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000869 Prediction.getCompl()) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000870 MeetIfcvtSizeLimit(*FalseBBI.BB,
871 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000872 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000873 FeasibilityAnalysis(FalseBBI, RevCond, true)) {
874 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups));
875 Enqueued = true;
876 }
877
Owen Andersone3cc84a2010-10-01 22:45:50 +0000878 if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000879 Prediction.getCompl()) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000880 MeetIfcvtSizeLimit(*FalseBBI.BB,
881 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000882 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000883 FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
884 Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups));
885 Enqueued = true;
886 }
887
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000888 if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) &&
Bob Wilson2ad40d32010-10-26 00:02:21 +0000889 MeetIfcvtSizeLimit(*FalseBBI.BB,
890 FalseBBI.NonPredSize + FalseBBI.ExtraCost,
Jakub Staszakf81b7f62011-07-10 02:58:07 +0000891 FalseBBI.ExtraCost2, Prediction.getCompl()) &&
Evan Chenge882fca2007-06-16 09:34:52 +0000892 FeasibilityAnalysis(FalseBBI, RevCond)) {
893 Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups));
894 Enqueued = true;
Evan Chengb6665f62007-06-04 06:47:22 +0000895 }
Evan Cheng4e654852007-05-16 02:00:57 +0000896 }
Evan Cheng4e654852007-05-16 02:00:57 +0000897
Evan Chenge882fca2007-06-16 09:34:52 +0000898 BBI.IsEnqueued = Enqueued;
Evan Cheng9618bca2007-06-11 22:26:22 +0000899 BBI.IsBeingAnalyzed = false;
900 BBI.IsAnalyzed = true;
901 return BBI;
Evan Chengcf6cc112007-05-18 18:14:37 +0000902}
903
Evan Cheng82582102007-05-30 19:49:19 +0000904/// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
Bob Wilson6fb124b2010-06-15 18:57:15 +0000905/// candidates.
906void IfConverter::AnalyzeBlocks(MachineFunction &MF,
Evan Chenge882fca2007-06-16 09:34:52 +0000907 std::vector<IfcvtToken*> &Tokens) {
Evan Cheng309db7c2011-04-27 19:32:43 +0000908 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
909 MachineBasicBlock *BB = I;
910 AnalyzeBlock(BB, Tokens);
Evan Cheng4e654852007-05-16 02:00:57 +0000911 }
Evan Cheng82582102007-05-30 19:49:19 +0000912
Evan Cheng5f702182007-06-01 00:12:12 +0000913 // Sort to favor more complex ifcvt scheme.
Evan Chenge882fca2007-06-16 09:34:52 +0000914 std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
Evan Cheng4e654852007-05-16 02:00:57 +0000915}
916
Evan Cheng7e75ba82007-06-08 22:01:07 +0000917/// canFallThroughTo - Returns true either if ToBB is the next block after BB or
918/// that all the intervening blocks are empty (given BB can fall through to its
919/// next block).
920static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
Evan Cheng46df4eb2010-06-16 07:35:02 +0000921 MachineFunction::iterator PI = BB;
922 MachineFunction::iterator I = llvm::next(PI);
Evan Chengc53ef582007-06-05 07:05:25 +0000923 MachineFunction::iterator TI = ToBB;
924 MachineFunction::iterator E = BB->getParent()->end();
Evan Cheng46df4eb2010-06-16 07:35:02 +0000925 while (I != TI) {
926 // Check isSuccessor to avoid case where the next block is empty, but
927 // it's not a successor.
928 if (I == E || !I->empty() || !PI->isSuccessor(I))
Evan Chengb6665f62007-06-04 06:47:22 +0000929 return false;
Evan Cheng46df4eb2010-06-16 07:35:02 +0000930 PI = I++;
931 }
Evan Chengb6665f62007-06-04 06:47:22 +0000932 return true;
Evan Chenga6b4f432007-05-21 22:22:58 +0000933}
934
Evan Chenga1a87872007-06-18 08:37:25 +0000935/// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed
936/// to determine if it can be if-converted. If predecessor is already enqueued,
937/// dequeue it!
938void IfConverter::InvalidatePreds(MachineBasicBlock *BB) {
Evan Chenga13aa952007-05-23 07:23:16 +0000939 for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
940 E = BB->pred_end(); PI != E; ++PI) {
941 BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
Evan Chengbc198ee2007-06-14 23:34:09 +0000942 if (PBBI.IsDone || PBBI.BB == BB)
Evan Chenge37e2432007-06-14 23:13:19 +0000943 continue;
Evan Chengbc198ee2007-06-14 23:34:09 +0000944 PBBI.IsAnalyzed = false;
945 PBBI.IsEnqueued = false;
Evan Chenga13aa952007-05-23 07:23:16 +0000946 }
947}
948
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000949/// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
950///
951static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
952 const TargetInstrInfo *TII) {
Stuart Hastings3bf91252010-06-17 22:43:56 +0000953 DebugLoc dl; // FIXME: this is nowhere
Dan Gohman1501cdb2008-08-22 16:07:55 +0000954 SmallVector<MachineOperand, 0> NoCond;
Stuart Hastings3bf91252010-06-17 22:43:56 +0000955 TII->InsertBranch(*BB, ToBB, NULL, NoCond, dl);
Evan Chengc8ed9ba2007-05-29 22:31:16 +0000956}
957
Evan Cheng7e75ba82007-06-08 22:01:07 +0000958/// RemoveExtraEdges - Remove true / false edges if either / both are no longer
959/// successors.
960void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
961 MachineBasicBlock *TBB = NULL, *FBB = NULL;
Owen Anderson44eb65c2008-08-14 22:49:33 +0000962 SmallVector<MachineOperand, 4> Cond;
Evan Chengc4047a82007-06-18 22:44:57 +0000963 if (!TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond))
964 BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
Evan Cheng7e75ba82007-06-08 22:01:07 +0000965}
966
Evan Cheng46df4eb2010-06-16 07:35:02 +0000967/// InitPredRedefs / UpdatePredRedefs - Defs by predicated instructions are
968/// modeled as read + write (sort like two-address instructions). These
969/// routines track register liveness and add implicit uses to if-converted
970/// instructions to conform to the model.
971static void InitPredRedefs(MachineBasicBlock *BB, SmallSet<unsigned,4> &Redefs,
972 const TargetRegisterInfo *TRI) {
973 for (MachineBasicBlock::livein_iterator I = BB->livein_begin(),
974 E = BB->livein_end(); I != E; ++I) {
975 unsigned Reg = *I;
Chad Rosier62c320a2013-05-22 23:17:36 +0000976 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
977 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000978 Redefs.insert(*SubRegs);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000979 }
980}
981
982static void UpdatePredRedefs(MachineInstr *MI, SmallSet<unsigned,4> &Redefs,
983 const TargetRegisterInfo *TRI,
984 bool AddImpUse = false) {
985 SmallVector<unsigned, 4> Defs;
986 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
987 const MachineOperand &MO = MI->getOperand(i);
988 if (!MO.isReg())
989 continue;
990 unsigned Reg = MO.getReg();
991 if (!Reg)
992 continue;
993 if (MO.isDef())
994 Defs.push_back(Reg);
995 else if (MO.isKill()) {
Chad Rosier62c320a2013-05-22 23:17:36 +0000996 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
997 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +0000998 Redefs.erase(*SubRegs);
Evan Cheng46df4eb2010-06-16 07:35:02 +0000999 }
1000 }
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +00001001 MachineInstrBuilder MIB(*MI->getParent()->getParent(), MI);
Evan Cheng46df4eb2010-06-16 07:35:02 +00001002 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1003 unsigned Reg = Defs[i];
Benjamin Kramer05d96f92012-08-22 15:37:57 +00001004 if (!Redefs.insert(Reg)) {
Evan Cheng46df4eb2010-06-16 07:35:02 +00001005 if (AddImpUse)
1006 // Treat predicated update as read + write.
Jakob Stoklund Olesen7b79b982012-12-20 18:08:06 +00001007 MIB.addReg(Reg, RegState::Implicit | RegState::Undef);
Evan Cheng46df4eb2010-06-16 07:35:02 +00001008 } else {
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001009 for (MCSubRegIterator SubRegs(Reg, TRI); SubRegs.isValid(); ++SubRegs)
1010 Redefs.insert(*SubRegs);
Evan Cheng46df4eb2010-06-16 07:35:02 +00001011 }
1012 }
1013}
1014
1015static void UpdatePredRedefs(MachineBasicBlock::iterator I,
1016 MachineBasicBlock::iterator E,
1017 SmallSet<unsigned,4> &Redefs,
1018 const TargetRegisterInfo *TRI) {
1019 while (I != E) {
1020 UpdatePredRedefs(I, Redefs, TRI);
1021 ++I;
1022 }
1023}
1024
Evan Chengb6665f62007-06-04 06:47:22 +00001025/// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
Evan Chenga6b4f432007-05-21 22:22:58 +00001026///
Evan Chenge882fca2007-06-16 09:34:52 +00001027bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
Evan Chenga6b4f432007-05-21 22:22:58 +00001028 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1029 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1030 BBInfo *CvtBBI = &TrueBBI;
1031 BBInfo *NextBBI = &FalseBBI;
Evan Chenga13aa952007-05-23 07:23:16 +00001032
Owen Anderson44eb65c2008-08-14 22:49:33 +00001033 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Chenge882fca2007-06-16 09:34:52 +00001034 if (Kind == ICSimpleFalse)
Evan Chengb5a06902007-06-01 20:29:21 +00001035 std::swap(CvtBBI, NextBBI);
Evan Chenga2acf842007-06-15 21:18:05 +00001036
Evan Chenga1a87872007-06-18 08:37:25 +00001037 if (CvtBBI->IsDone ||
1038 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
Evan Chenga2acf842007-06-15 21:18:05 +00001039 // Something has changed. It's no longer safe to predicate this block.
Evan Chenga2acf842007-06-15 21:18:05 +00001040 BBI.IsAnalyzed = false;
1041 CvtBBI->IsAnalyzed = false;
1042 return false;
Evan Chengb5a06902007-06-01 20:29:21 +00001043 }
Evan Chenga13aa952007-05-23 07:23:16 +00001044
Evan Cheng9a28cc12013-05-05 18:03:49 +00001045 if (CvtBBI->BB->hasAddressTaken())
1046 // Conservatively abort if-conversion if BB's address is taken.
1047 return false;
1048
Evan Chenge882fca2007-06-16 09:34:52 +00001049 if (Kind == ICSimpleFalse)
Dan Gohman279c22e2008-10-21 03:29:32 +00001050 if (TII->ReverseBranchCondition(Cond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001051 llvm_unreachable("Unable to reverse branch condition!");
Evan Chenga2acf842007-06-15 21:18:05 +00001052
Bob Wilson54eee522010-06-19 05:33:57 +00001053 // Initialize liveins to the first BB. These are potentiall redefined by
Evan Cheng46df4eb2010-06-16 07:35:02 +00001054 // predicated instructions.
1055 SmallSet<unsigned, 4> Redefs;
1056 InitPredRedefs(CvtBBI->BB, Redefs, TRI);
1057 InitPredRedefs(NextBBI->BB, Redefs, TRI);
1058
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001059 if (CvtBBI->BB->pred_size() > 1) {
1060 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson9c4856a2009-05-13 23:25:24 +00001061 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001062 // the entry block.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001063 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, Redefs);
Hal Finkel9af70142013-04-10 22:05:25 +00001064
1065 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1066 // explicitly remove CvtBBI as a successor.
1067 BBI.BB->removeSuccessor(CvtBBI->BB);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001068 } else {
Evan Cheng46df4eb2010-06-16 07:35:02 +00001069 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond, Redefs);
Evan Chenga6b4f432007-05-21 22:22:58 +00001070
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001071 // Merge converted block into entry block.
1072 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1073 MergeBlocks(BBI, *CvtBBI);
1074 }
Evan Chengd4de6d92007-06-07 02:12:15 +00001075
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001076 bool IterIfcvt = true;
Evan Cheng7e75ba82007-06-08 22:01:07 +00001077 if (!canFallThroughTo(BBI.BB, NextBBI->BB)) {
Evan Chengc8ed9ba2007-05-29 22:31:16 +00001078 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001079 BBI.HasFallThrough = false;
Evan Chengac5f1422007-06-08 09:36:04 +00001080 // Now ifcvt'd block will look like this:
1081 // BB:
1082 // ...
1083 // t, f = cmp
1084 // if t op
1085 // b BBf
1086 //
1087 // We cannot further ifcvt this block because the unconditional branch
1088 // will have to be predicated on the new condition, that will not be
1089 // available if cmp executes.
1090 IterIfcvt = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001091 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001092
Evan Cheng7e75ba82007-06-08 22:01:07 +00001093 RemoveExtraEdges(BBI);
1094
Evan Chenga13aa952007-05-23 07:23:16 +00001095 // Update block info. BB can be iteratively if-converted.
Evan Cheng9618bca2007-06-11 22:26:22 +00001096 if (!IterIfcvt)
1097 BBI.IsDone = true;
Evan Chenga1a87872007-06-18 08:37:25 +00001098 InvalidatePreds(BBI.BB);
Evan Cheng9618bca2007-06-11 22:26:22 +00001099 CvtBBI->IsDone = true;
Evan Chenga6b4f432007-05-21 22:22:58 +00001100
1101 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +00001102 return true;
1103}
1104
Evan Chengd6ddc302007-05-16 21:54:37 +00001105/// IfConvertTriangle - If convert a triangle sub-CFG.
1106///
Evan Chenge882fca2007-06-16 09:34:52 +00001107bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
Evan Chenga13aa952007-05-23 07:23:16 +00001108 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001109 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1110 BBInfo *CvtBBI = &TrueBBI;
1111 BBInfo *NextBBI = &FalseBBI;
Stuart Hastings3bf91252010-06-17 22:43:56 +00001112 DebugLoc dl; // FIXME: this is nowhere
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001113
Owen Anderson44eb65c2008-08-14 22:49:33 +00001114 SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
Evan Chenge882fca2007-06-16 09:34:52 +00001115 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001116 std::swap(CvtBBI, NextBBI);
Evan Chenga2acf842007-06-15 21:18:05 +00001117
Evan Chenga1a87872007-06-18 08:37:25 +00001118 if (CvtBBI->IsDone ||
1119 (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
Evan Chenga2acf842007-06-15 21:18:05 +00001120 // Something has changed. It's no longer safe to predicate this block.
Evan Chenga2acf842007-06-15 21:18:05 +00001121 BBI.IsAnalyzed = false;
1122 CvtBBI->IsAnalyzed = false;
1123 return false;
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001124 }
Evan Chenga2acf842007-06-15 21:18:05 +00001125
Evan Cheng9a28cc12013-05-05 18:03:49 +00001126 if (CvtBBI->BB->hasAddressTaken())
1127 // Conservatively abort if-conversion if BB's address is taken.
1128 return false;
1129
Evan Chenge882fca2007-06-16 09:34:52 +00001130 if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
Dan Gohman279c22e2008-10-21 03:29:32 +00001131 if (TII->ReverseBranchCondition(Cond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001132 llvm_unreachable("Unable to reverse branch condition!");
Evan Chenga2acf842007-06-15 21:18:05 +00001133
Evan Chenge882fca2007-06-16 09:34:52 +00001134 if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
Dan Gohman279c22e2008-10-21 03:29:32 +00001135 if (ReverseBranchCondition(*CvtBBI)) {
1136 // BB has been changed, modify its predecessors (except for this
1137 // one) so they don't get ifcvt'ed based on bad intel.
1138 for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(),
1139 E = CvtBBI->BB->pred_end(); PI != E; ++PI) {
1140 MachineBasicBlock *PBB = *PI;
1141 if (PBB == BBI.BB)
1142 continue;
1143 BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
1144 if (PBBI.IsEnqueued) {
1145 PBBI.IsAnalyzed = false;
1146 PBBI.IsEnqueued = false;
1147 }
Evan Chengbc198ee2007-06-14 23:34:09 +00001148 }
Evan Chengcc8fb462007-06-12 23:54:05 +00001149 }
1150 }
Evan Cheng8c529382007-06-01 07:41:07 +00001151
Bob Wilson54eee522010-06-19 05:33:57 +00001152 // Initialize liveins to the first BB. These are potentially redefined by
Evan Cheng46df4eb2010-06-16 07:35:02 +00001153 // predicated instructions.
1154 SmallSet<unsigned, 4> Redefs;
1155 InitPredRedefs(CvtBBI->BB, Redefs, TRI);
1156 InitPredRedefs(NextBBI->BB, Redefs, TRI);
1157
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001158 bool HasEarlyExit = CvtBBI->FalseBB != NULL;
Bob Wilson8eab75f2010-06-29 00:55:23 +00001159 if (CvtBBI->BB->pred_size() > 1) {
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001160 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson9c4856a2009-05-13 23:25:24 +00001161 // Copy instructions in the true block, predicate them, and add them to
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001162 // the entry block.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001163 CopyAndPredicateBlock(BBI, *CvtBBI, Cond, Redefs, true);
Hal Finkel9af70142013-04-10 22:05:25 +00001164
1165 // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1166 // explicitly remove CvtBBI as a successor.
1167 BBI.BB->removeSuccessor(CvtBBI->BB);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001168 } else {
1169 // Predicate the 'true' block after removing its branch.
1170 CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
Evan Cheng46df4eb2010-06-16 07:35:02 +00001171 PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond, Redefs);
Evan Chenga6b4f432007-05-21 22:22:58 +00001172
Evan Chengc4047a82007-06-18 22:44:57 +00001173 // Now merge the entry of the triangle with the true block.
1174 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
Bob Wilson8eab75f2010-06-29 00:55:23 +00001175 MergeBlocks(BBI, *CvtBBI, false);
Evan Chengc4047a82007-06-18 22:44:57 +00001176 }
1177
Evan Chengb6665f62007-06-04 06:47:22 +00001178 // If 'true' block has a 'false' successor, add an exit branch to it.
Evan Cheng8ed680c2007-06-05 01:31:40 +00001179 if (HasEarlyExit) {
Owen Anderson44eb65c2008-08-14 22:49:33 +00001180 SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
1181 CvtBBI->BrCond.end());
Evan Cheng8c529382007-06-01 07:41:07 +00001182 if (TII->ReverseBranchCondition(RevCond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001183 llvm_unreachable("Unable to reverse branch condition!");
Stuart Hastings3bf91252010-06-17 22:43:56 +00001184 TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond, dl);
Evan Chengc4047a82007-06-18 22:44:57 +00001185 BBI.BB->addSuccessor(CvtBBI->FalseBB);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001186 }
Evan Chengac5f1422007-06-08 09:36:04 +00001187
1188 // Merge in the 'false' block if the 'false' block has no other
Bob Wilson9c4856a2009-05-13 23:25:24 +00001189 // predecessors. Otherwise, add an unconditional branch to 'false'.
Evan Chengf5305f92007-06-05 00:07:37 +00001190 bool FalseBBDead = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001191 bool IterIfcvt = true;
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001192 bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB);
Evan Chengf4769612007-06-07 08:13:00 +00001193 if (!isFallThrough) {
1194 // Only merge them if the true block does not fallthrough to the false
1195 // block. By not merging them, we make it possible to iteratively
1196 // ifcvt the blocks.
Evan Chenga1a87872007-06-18 08:37:25 +00001197 if (!HasEarlyExit &&
Evan Cheng9a28cc12013-05-05 18:03:49 +00001198 NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough &&
1199 !NextBBI->BB->hasAddressTaken()) {
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001200 MergeBlocks(BBI, *NextBBI);
Evan Chengf4769612007-06-07 08:13:00 +00001201 FalseBBDead = true;
Evan Chengf4769612007-06-07 08:13:00 +00001202 } else {
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001203 InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
1204 BBI.HasFallThrough = false;
Evan Chengf4769612007-06-07 08:13:00 +00001205 }
Evan Chengac5f1422007-06-08 09:36:04 +00001206 // Mixed predicated and unpredicated code. This cannot be iteratively
1207 // predicated.
1208 IterIfcvt = false;
Evan Cheng3d6f60e2007-06-06 02:08:52 +00001209 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001210
Evan Cheng7e75ba82007-06-08 22:01:07 +00001211 RemoveExtraEdges(BBI);
Evan Chenga6b4f432007-05-21 22:22:58 +00001212
Evan Chenga13aa952007-05-23 07:23:16 +00001213 // Update block info. BB can be iteratively if-converted.
Bob Wilson92fffe02010-06-15 22:18:54 +00001214 if (!IterIfcvt)
Evan Cheng9618bca2007-06-11 22:26:22 +00001215 BBI.IsDone = true;
Evan Chenga1a87872007-06-18 08:37:25 +00001216 InvalidatePreds(BBI.BB);
Evan Cheng9618bca2007-06-11 22:26:22 +00001217 CvtBBI->IsDone = true;
Evan Chengf5305f92007-06-05 00:07:37 +00001218 if (FalseBBDead)
Evan Cheng9618bca2007-06-11 22:26:22 +00001219 NextBBI->IsDone = true;
Evan Chenga6b4f432007-05-21 22:22:58 +00001220
1221 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +00001222 return true;
Evan Cheng4e654852007-05-16 02:00:57 +00001223}
1224
Evan Chengd6ddc302007-05-16 21:54:37 +00001225/// IfConvertDiamond - If convert a diamond sub-CFG.
1226///
Evan Chenga1a87872007-06-18 08:37:25 +00001227bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
1228 unsigned NumDups1, unsigned NumDups2) {
Evan Chengb6665f62007-06-04 06:47:22 +00001229 BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
Evan Chengcf6cc112007-05-18 18:14:37 +00001230 BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
Evan Chenge882fca2007-06-16 09:34:52 +00001231 MachineBasicBlock *TailBB = TrueBBI.TrueBB;
Bob Wilson9c4856a2009-05-13 23:25:24 +00001232 // True block must fall through or end with an unanalyzable terminator.
Evan Chenge882fca2007-06-16 09:34:52 +00001233 if (!TailBB) {
Evan Chenga1a87872007-06-18 08:37:25 +00001234 if (blockAlwaysFallThrough(TrueBBI))
1235 TailBB = FalseBBI.TrueBB;
1236 assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
Evan Cheng4e654852007-05-16 02:00:57 +00001237 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001238
Evan Chenga1a87872007-06-18 08:37:25 +00001239 if (TrueBBI.IsDone || FalseBBI.IsDone ||
1240 TrueBBI.BB->pred_size() > 1 ||
1241 FalseBBI.BB->pred_size() > 1) {
1242 // Something has changed. It's no longer safe to predicate these blocks.
1243 BBI.IsAnalyzed = false;
1244 TrueBBI.IsAnalyzed = false;
1245 FalseBBI.IsAnalyzed = false;
1246 return false;
Evan Chenga6b4f432007-05-21 22:22:58 +00001247 }
Evan Chenga1a87872007-06-18 08:37:25 +00001248
Evan Cheng9a28cc12013-05-05 18:03:49 +00001249 if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
1250 // Conservatively abort if-conversion if either BB has its address taken.
1251 return false;
1252
Bob Wilson8eab75f2010-06-29 00:55:23 +00001253 // Put the predicated instructions from the 'true' block before the
1254 // instructions from the 'false' block, unless the true block would clobber
1255 // the predicate, in which case, do the opposite.
Evan Cheng993fc952007-06-05 23:46:14 +00001256 BBInfo *BBI1 = &TrueBBI;
1257 BBInfo *BBI2 = &FalseBBI;
Owen Anderson44eb65c2008-08-14 22:49:33 +00001258 SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
Dan Gohman279c22e2008-10-21 03:29:32 +00001259 if (TII->ReverseBranchCondition(RevCond))
Craig Topper5e25ee82012-02-05 08:31:47 +00001260 llvm_unreachable("Unable to reverse branch condition!");
Owen Anderson44eb65c2008-08-14 22:49:33 +00001261 SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
1262 SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
Evan Chenge7052132007-06-06 00:57:55 +00001263
Evan Chenge882fca2007-06-16 09:34:52 +00001264 // Figure out the more profitable ordering.
1265 bool DoSwap = false;
1266 if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred)
1267 DoSwap = true;
1268 else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) {
Evan Chengc4047a82007-06-18 22:44:57 +00001269 if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
Evan Chenge882fca2007-06-16 09:34:52 +00001270 DoSwap = true;
Evan Chenge882fca2007-06-16 09:34:52 +00001271 }
1272 if (DoSwap) {
Evan Chenge7052132007-06-06 00:57:55 +00001273 std::swap(BBI1, BBI2);
1274 std::swap(Cond1, Cond2);
Evan Chenge7052132007-06-06 00:57:55 +00001275 }
Evan Cheng993fc952007-06-05 23:46:14 +00001276
Evan Chenga1a87872007-06-18 08:37:25 +00001277 // Remove the conditional branch from entry to the blocks.
1278 BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1279
Jim Grosbach135ec502010-06-25 22:02:28 +00001280 // Initialize liveins to the first BB. These are potentially redefined by
Evan Cheng46df4eb2010-06-16 07:35:02 +00001281 // predicated instructions.
1282 SmallSet<unsigned, 4> Redefs;
1283 InitPredRedefs(BBI1->BB, Redefs, TRI);
1284
Evan Chenga1a87872007-06-18 08:37:25 +00001285 // Remove the duplicated instructions at the beginnings of both paths.
1286 MachineBasicBlock::iterator DI1 = BBI1->BB->begin();
1287 MachineBasicBlock::iterator DI2 = BBI2->BB->begin();
Jim Grosbachc834f412010-06-14 21:30:32 +00001288 MachineBasicBlock::iterator DIE1 = BBI1->BB->end();
1289 MachineBasicBlock::iterator DIE2 = BBI2->BB->end();
1290 // Skip dbg_value instructions
1291 while (DI1 != DIE1 && DI1->isDebugValue())
1292 ++DI1;
1293 while (DI2 != DIE2 && DI2->isDebugValue())
1294 ++DI2;
Evan Chenga1a87872007-06-18 08:37:25 +00001295 BBI1->NonPredSize -= NumDups1;
1296 BBI2->NonPredSize -= NumDups1;
Jim Grosbachd459f452010-06-28 20:26:00 +00001297
1298 // Skip past the dups on each side separately since there may be
1299 // differing dbg_value entries.
1300 for (unsigned i = 0; i < NumDups1; ++DI1) {
1301 if (!DI1->isDebugValue())
1302 ++i;
1303 }
Jim Grosbach9f054f02010-06-25 23:05:46 +00001304 while (NumDups1 != 0) {
Evan Chenga1a87872007-06-18 08:37:25 +00001305 ++DI2;
Jim Grosbachd459f452010-06-28 20:26:00 +00001306 if (!DI2->isDebugValue())
1307 --NumDups1;
Evan Chenga1a87872007-06-18 08:37:25 +00001308 }
Evan Cheng46df4eb2010-06-16 07:35:02 +00001309
1310 UpdatePredRedefs(BBI1->BB->begin(), DI1, Redefs, TRI);
Evan Chenga1a87872007-06-18 08:37:25 +00001311 BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1);
1312 BBI2->BB->erase(BBI2->BB->begin(), DI2);
1313
Evan Cheng8787c5f2011-12-19 22:01:30 +00001314 // Remove branch from 'true' block and remove duplicated instructions.
Evan Cheng993fc952007-06-05 23:46:14 +00001315 BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
Evan Chenga1a87872007-06-18 08:37:25 +00001316 DI1 = BBI1->BB->end();
Jim Grosbachc834f412010-06-14 21:30:32 +00001317 for (unsigned i = 0; i != NumDups2; ) {
1318 // NumDups2 only counted non-dbg_value instructions, so this won't
1319 // run off the head of the list.
1320 assert (DI1 != BBI1->BB->begin());
Evan Chenga1a87872007-06-18 08:37:25 +00001321 --DI1;
Jim Grosbachc834f412010-06-14 21:30:32 +00001322 // skip dbg_value instructions
1323 if (!DI1->isDebugValue())
1324 ++i;
1325 }
Evan Chenga1a87872007-06-18 08:37:25 +00001326 BBI1->BB->erase(DI1, BBI1->BB->end());
Evan Cheng993fc952007-06-05 23:46:14 +00001327
Evan Cheng8787c5f2011-12-19 22:01:30 +00001328 // Remove 'false' block branch and find the last instruction to predicate.
Evan Chenga1a87872007-06-18 08:37:25 +00001329 BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
1330 DI2 = BBI2->BB->end();
1331 while (NumDups2 != 0) {
Jim Grosbachc834f412010-06-14 21:30:32 +00001332 // NumDups2 only counted non-dbg_value instructions, so this won't
1333 // run off the head of the list.
1334 assert (DI2 != BBI2->BB->begin());
Evan Chenga1a87872007-06-18 08:37:25 +00001335 --DI2;
Jim Grosbachc834f412010-06-14 21:30:32 +00001336 // skip dbg_value instructions
1337 if (!DI2->isDebugValue())
1338 --NumDups2;
Evan Chenga1a87872007-06-18 08:37:25 +00001339 }
Evan Cheng8787c5f2011-12-19 22:01:30 +00001340
1341 // Remember which registers would later be defined by the false block.
1342 // This allows us not to predicate instructions in the true block that would
1343 // later be re-defined. That is, rather than
1344 // subeq r0, r1, #1
1345 // addne r0, r1, #1
1346 // generate:
1347 // sub r0, r1, #1
1348 // addne r0, r1, #1
1349 SmallSet<unsigned, 4> RedefsByFalse;
1350 SmallSet<unsigned, 4> ExtUses;
1351 if (TII->isProfitableToUnpredicate(*BBI1->BB, *BBI2->BB)) {
1352 for (MachineBasicBlock::iterator FI = BBI2->BB->begin(); FI != DI2; ++FI) {
1353 if (FI->isDebugValue())
1354 continue;
1355 SmallVector<unsigned, 4> Defs;
1356 for (unsigned i = 0, e = FI->getNumOperands(); i != e; ++i) {
1357 const MachineOperand &MO = FI->getOperand(i);
1358 if (!MO.isReg())
1359 continue;
1360 unsigned Reg = MO.getReg();
1361 if (!Reg)
1362 continue;
1363 if (MO.isDef()) {
1364 Defs.push_back(Reg);
1365 } else if (!RedefsByFalse.count(Reg)) {
1366 // These are defined before ctrl flow reach the 'false' instructions.
1367 // They cannot be modified by the 'true' instructions.
Chad Rosier62c320a2013-05-22 23:17:36 +00001368 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1369 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001370 ExtUses.insert(*SubRegs);
Evan Cheng8787c5f2011-12-19 22:01:30 +00001371 }
1372 }
1373
1374 for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
1375 unsigned Reg = Defs[i];
1376 if (!ExtUses.count(Reg)) {
Chad Rosier62c320a2013-05-22 23:17:36 +00001377 for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1378 SubRegs.isValid(); ++SubRegs)
Jakob Stoklund Olesen396618b2012-06-01 23:28:30 +00001379 RedefsByFalse.insert(*SubRegs);
Evan Cheng8787c5f2011-12-19 22:01:30 +00001380 }
1381 }
1382 }
1383 }
1384
1385 // Predicate the 'true' block.
1386 PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1, Redefs, &RedefsByFalse);
1387
1388 // Predicate the 'false' block.
Evan Cheng46df4eb2010-06-16 07:35:02 +00001389 PredicateBlock(*BBI2, DI2, *Cond2, Redefs);
Evan Cheng993fc952007-06-05 23:46:14 +00001390
Evan Chengc4047a82007-06-18 22:44:57 +00001391 // Merge the true block into the entry of the diamond.
Bob Wilson8eab75f2010-06-29 00:55:23 +00001392 MergeBlocks(BBI, *BBI1, TailBB == 0);
1393 MergeBlocks(BBI, *BBI2, TailBB == 0);
Evan Chenge7052132007-06-06 00:57:55 +00001394
Bob Wilson9c4856a2009-05-13 23:25:24 +00001395 // If the if-converted block falls through or unconditionally branches into
1396 // the tail block, and the tail block does not have other predecessors, then
Evan Chenga1a87872007-06-18 08:37:25 +00001397 // fold the tail block in as well. Otherwise, unless it falls through to the
1398 // tail, add a unconditional branch to it.
1399 if (TailBB) {
Pete Cooper9c58aa742011-11-04 23:49:14 +00001400 BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
Evan Cheng9a28cc12013-05-05 18:03:49 +00001401 bool CanMergeTail = !TailBBI.HasFallThrough &&
1402 !TailBBI.BB->hasAddressTaken();
Bob Wilson8eab75f2010-06-29 00:55:23 +00001403 // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
1404 // check if there are any other predecessors besides those.
1405 unsigned NumPreds = TailBB->pred_size();
1406 if (NumPreds > 1)
1407 CanMergeTail = false;
1408 else if (NumPreds == 1 && CanMergeTail) {
1409 MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
1410 if (*PI != BBI1->BB && *PI != BBI2->BB)
1411 CanMergeTail = false;
1412 }
1413 if (CanMergeTail) {
Evan Chengc4047a82007-06-18 22:44:57 +00001414 MergeBlocks(BBI, TailBBI);
Evan Chenga1a87872007-06-18 08:37:25 +00001415 TailBBI.IsDone = true;
1416 } else {
Bob Wilson8eab75f2010-06-29 00:55:23 +00001417 BBI.BB->addSuccessor(TailBB);
Evan Chengc4047a82007-06-18 22:44:57 +00001418 InsertUncondBranch(BBI.BB, TailBB, TII);
1419 BBI.HasFallThrough = false;
Evan Chenga1a87872007-06-18 08:37:25 +00001420 }
Evan Chenga6b4f432007-05-21 22:22:58 +00001421 }
1422
Bob Wilson8eab75f2010-06-29 00:55:23 +00001423 // RemoveExtraEdges won't work if the block has an unanalyzable branch,
1424 // which can happen here if TailBB is unanalyzable and is merged, so
1425 // explicitly remove BBI1 and BBI2 as successors.
1426 BBI.BB->removeSuccessor(BBI1->BB);
1427 BBI.BB->removeSuccessor(BBI2->BB);
Evan Cheng7e75ba82007-06-08 22:01:07 +00001428 RemoveExtraEdges(BBI);
1429
Evan Cheng993fc952007-06-05 23:46:14 +00001430 // Update block info.
Evan Cheng9618bca2007-06-11 22:26:22 +00001431 BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
Evan Chenga1a87872007-06-18 08:37:25 +00001432 InvalidatePreds(BBI.BB);
Evan Chenga6b4f432007-05-21 22:22:58 +00001433
1434 // FIXME: Must maintain LiveIns.
Evan Chenga6b4f432007-05-21 22:22:58 +00001435 return true;
Evan Cheng4e654852007-05-16 02:00:57 +00001436}
1437
Evan Cheng8787c5f2011-12-19 22:01:30 +00001438static bool MaySpeculate(const MachineInstr *MI,
1439 SmallSet<unsigned, 4> &LaterRedefs,
1440 const TargetInstrInfo *TII) {
1441 bool SawStore = true;
1442 if (!MI->isSafeToMove(TII, 0, SawStore))
1443 return false;
1444
1445 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1446 const MachineOperand &MO = MI->getOperand(i);
1447 if (!MO.isReg())
1448 continue;
1449 unsigned Reg = MO.getReg();
1450 if (!Reg)
1451 continue;
1452 if (MO.isDef() && !LaterRedefs.count(Reg))
1453 return false;
1454 }
1455
1456 return true;
1457}
1458
Evan Chenga1a87872007-06-18 08:37:25 +00001459/// PredicateBlock - Predicate instructions from the start of the block to the
1460/// specified end with the specified condition.
Evan Chenga13aa952007-05-23 07:23:16 +00001461void IfConverter::PredicateBlock(BBInfo &BBI,
Evan Chenga1a87872007-06-18 08:37:25 +00001462 MachineBasicBlock::iterator E,
Evan Cheng46df4eb2010-06-16 07:35:02 +00001463 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng8787c5f2011-12-19 22:01:30 +00001464 SmallSet<unsigned, 4> &Redefs,
1465 SmallSet<unsigned, 4> *LaterRedefs) {
1466 bool AnyUnpred = false;
1467 bool MaySpec = LaterRedefs != 0;
Evan Chenga1a87872007-06-18 08:37:25 +00001468 for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) {
Jim Grosbach870c8052010-06-04 23:01:26 +00001469 if (I->isDebugValue() || TII->isPredicated(I))
Evan Chenga13aa952007-05-23 07:23:16 +00001470 continue;
Evan Cheng8787c5f2011-12-19 22:01:30 +00001471 // It may be possible not to predicate an instruction if it's the 'true'
1472 // side of a diamond and the 'false' side may re-define the instruction's
1473 // defs.
1474 if (MaySpec && MaySpeculate(I, *LaterRedefs, TII)) {
1475 AnyUnpred = true;
1476 continue;
1477 }
1478 // If any instruction is predicated, then every instruction after it must
1479 // be predicated.
1480 MaySpec = false;
Evan Chengb6665f62007-06-04 06:47:22 +00001481 if (!TII->PredicateInstruction(I, Cond)) {
Torok Edwinf3689232009-07-12 20:07:01 +00001482#ifndef NDEBUG
David Greene083b7ff2010-01-04 22:02:01 +00001483 dbgs() << "Unable to predicate " << *I << "!\n";
Torok Edwinf3689232009-07-12 20:07:01 +00001484#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00001485 llvm_unreachable(0);
Evan Chengd6ddc302007-05-16 21:54:37 +00001486 }
Evan Cheng46df4eb2010-06-16 07:35:02 +00001487
Bob Wilson54eee522010-06-19 05:33:57 +00001488 // If the predicated instruction now redefines a register as the result of
Evan Cheng46df4eb2010-06-16 07:35:02 +00001489 // if-conversion, add an implicit kill.
1490 UpdatePredRedefs(I, Redefs, TRI, true);
Evan Cheng4e654852007-05-16 02:00:57 +00001491 }
Evan Chenga13aa952007-05-23 07:23:16 +00001492
Evan Cheng2acdbcc2007-06-08 19:17:12 +00001493 std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
1494
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001495 BBI.IsAnalyzed = false;
1496 BBI.NonPredSize = 0;
1497
Dan Gohmanfe601042010-06-22 15:08:57 +00001498 ++NumIfConvBBs;
Evan Cheng8787c5f2011-12-19 22:01:30 +00001499 if (AnyUnpred)
1500 ++NumUnpred;
Evan Chengb6665f62007-06-04 06:47:22 +00001501}
1502
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001503/// CopyAndPredicateBlock - Copy and predicate instructions from source BB to
1504/// the destination block. Skip end of block branches if IgnoreBr is true.
1505void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
Owen Anderson44eb65c2008-08-14 22:49:33 +00001506 SmallVectorImpl<MachineOperand> &Cond,
Evan Cheng46df4eb2010-06-16 07:35:02 +00001507 SmallSet<unsigned, 4> &Redefs,
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001508 bool IgnoreBr) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001509 MachineFunction &MF = *ToBBI.BB->getParent();
1510
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001511 for (MachineBasicBlock::iterator I = FromBBI.BB->begin(),
1512 E = FromBBI.BB->end(); I != E; ++I) {
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001513 // Do not copy the end of the block branches.
Evan Cheng5a96b3d2011-12-07 07:15:52 +00001514 if (IgnoreBr && I->isBranch())
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001515 break;
1516
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001517 MachineInstr *MI = MF.CloneMachineInstr(I);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001518 ToBBI.BB->insert(ToBBI.BB->end(), MI);
Bob Wilson2ad40d32010-10-26 00:02:21 +00001519 ToBBI.NonPredSize++;
Arnold Schwaighoferd42730d2013-09-30 15:28:56 +00001520 unsigned ExtraPredCost = TII->getPredicationCost(&*I);
1521 unsigned NumCycles = SchedModel.computeInstrLatency(&*I, false);
Evan Cheng8239daf2010-11-03 00:45:17 +00001522 if (NumCycles > 1)
1523 ToBBI.ExtraCost += NumCycles-1;
1524 ToBBI.ExtraCost2 += ExtraPredCost;
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001525
Bob Wilsonf50e9522010-06-18 17:07:23 +00001526 if (!TII->isPredicated(I) && !MI->isDebugValue()) {
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001527 if (!TII->PredicateInstruction(MI, Cond)) {
Torok Edwinf3689232009-07-12 20:07:01 +00001528#ifndef NDEBUG
David Greene083b7ff2010-01-04 22:02:01 +00001529 dbgs() << "Unable to predicate " << *I << "!\n";
Torok Edwinf3689232009-07-12 20:07:01 +00001530#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00001531 llvm_unreachable(0);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001532 }
Evan Cheng46df4eb2010-06-16 07:35:02 +00001533 }
1534
Bob Wilson54eee522010-06-19 05:33:57 +00001535 // If the predicated instruction now redefines a register as the result of
Evan Cheng46df4eb2010-06-16 07:35:02 +00001536 // if-conversion, add an implicit kill.
1537 UpdatePredRedefs(MI, Redefs, TRI, true);
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001538 }
1539
Bob Wilson8eab75f2010-06-29 00:55:23 +00001540 if (!IgnoreBr) {
1541 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
1542 FromBBI.BB->succ_end());
1543 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
1544 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
Evan Chengc4047a82007-06-18 22:44:57 +00001545
Bob Wilson8eab75f2010-06-29 00:55:23 +00001546 for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
1547 MachineBasicBlock *Succ = Succs[i];
1548 // Fallthrough edge can't be transferred.
1549 if (Succ == FallThrough)
1550 continue;
1551 ToBBI.BB->addSuccessor(Succ);
1552 }
Evan Chengc4047a82007-06-18 22:44:57 +00001553 }
1554
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001555 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1556 std::back_inserter(ToBBI.Predicate));
1557 std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate));
1558
1559 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1560 ToBBI.IsAnalyzed = false;
1561
Dan Gohmanfe601042010-06-22 15:08:57 +00001562 ++NumDupBBs;
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001563}
1564
Evan Cheng86cbfea2007-05-18 00:20:58 +00001565/// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
Bob Wilson8eab75f2010-06-29 00:55:23 +00001566/// This will leave FromBB as an empty block, so remove all of its
1567/// successor edges except for the fall-through edge. If AddEdges is true,
1568/// i.e., when FromBBI's branch is being moved, add those successor edges to
1569/// ToBBI.
1570void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
Evan Cheng9a28cc12013-05-05 18:03:49 +00001571 assert(!FromBBI.BB->hasAddressTaken() &&
1572 "Removing a BB whose address is taken!");
1573
Evan Cheng86cbfea2007-05-18 00:20:58 +00001574 ToBBI.BB->splice(ToBBI.BB->end(),
1575 FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
Evan Chenga13aa952007-05-23 07:23:16 +00001576
Evan Chengd4de6d92007-06-07 02:12:15 +00001577 std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
1578 FromBBI.BB->succ_end());
Evan Cheng7e75ba82007-06-08 22:01:07 +00001579 MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001580 MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
Evan Chengb6665f62007-06-04 06:47:22 +00001581
Evan Chengd4de6d92007-06-07 02:12:15 +00001582 for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
1583 MachineBasicBlock *Succ = Succs[i];
Evan Cheng7e75ba82007-06-08 22:01:07 +00001584 // Fallthrough edge can't be transferred.
Evan Chengd4de6d92007-06-07 02:12:15 +00001585 if (Succ == FallThrough)
1586 continue;
1587 FromBBI.BB->removeSuccessor(Succ);
Jakob Stoklund Olesendd4fc442013-01-24 23:59:08 +00001588 if (AddEdges && !ToBBI.BB->isSuccessor(Succ))
Bob Wilson8eab75f2010-06-29 00:55:23 +00001589 ToBBI.BB->addSuccessor(Succ);
Evan Chengd4de6d92007-06-07 02:12:15 +00001590 }
1591
Bob Wilson9c4856a2009-05-13 23:25:24 +00001592 // Now FromBBI always falls through to the next block!
Bob Wilson8308e8f2009-05-13 23:48:58 +00001593 if (NBB && !FromBBI.BB->isSuccessor(NBB))
Evan Cheng7e75ba82007-06-08 22:01:07 +00001594 FromBBI.BB->addSuccessor(NBB);
1595
Evan Cheng2c8c3a42007-06-15 07:36:12 +00001596 std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1597 std::back_inserter(ToBBI.Predicate));
1598 FromBBI.Predicate.clear();
1599
Evan Chenga13aa952007-05-23 07:23:16 +00001600 ToBBI.NonPredSize += FromBBI.NonPredSize;
Bob Wilson2ad40d32010-10-26 00:02:21 +00001601 ToBBI.ExtraCost += FromBBI.ExtraCost;
Evan Cheng8239daf2010-11-03 00:45:17 +00001602 ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
Evan Chenga13aa952007-05-23 07:23:16 +00001603 FromBBI.NonPredSize = 0;
Bob Wilson2ad40d32010-10-26 00:02:21 +00001604 FromBBI.ExtraCost = 0;
Evan Cheng8239daf2010-11-03 00:45:17 +00001605 FromBBI.ExtraCost2 = 0;
Evan Cheng7a655472007-06-06 10:16:17 +00001606
Evan Cheng9618bca2007-06-11 22:26:22 +00001607 ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
Evan Cheng1c9f91d2007-06-09 01:03:43 +00001608 ToBBI.HasFallThrough = FromBBI.HasFallThrough;
Evan Cheng86ff2962007-06-14 20:28:52 +00001609 ToBBI.IsAnalyzed = false;
1610 FromBBI.IsAnalyzed = false;
Evan Cheng4e654852007-05-16 02:00:57 +00001611}