Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1 | //===-- IfConversion.cpp - Machine code if conversion pass. ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Justin Lebar | acc4710 | 2016-04-01 01:09:03 +0000 | [diff] [blame] | 10 | // This file implements the machine instruction level if-conversion pass, which |
| 11 | // tries to convert conditional branches into predicated instructions. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "BranchFolding.h" |
| 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SmallSet.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/LivePhysRegs.h" |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" |
Jakub Staszak | 3ef20e3 | 2011-08-03 22:53:41 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Jakob Stoklund Olesen | f623e98 | 2012-12-20 18:08:06 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/TargetSchedule.h" |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CommandLine.h" |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetInstrInfo.h" |
| 33 | #include "llvm/Target/TargetLowering.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetRegisterInfo.h" |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 35 | #include "llvm/Target/TargetSubtargetInfo.h" |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 36 | #include <algorithm> |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 37 | #include <utility> |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 38 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "ifcvt" |
| 42 | |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 43 | // Hidden options for help debugging. |
| 44 | static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden); |
| 45 | static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden); |
| 46 | static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 47 | static cl::opt<bool> DisableSimple("disable-ifcvt-simple", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 48 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 49 | static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 50 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 51 | static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 52 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 53 | static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 54 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 55 | static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 56 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 57 | static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 58 | cl::init(false), cl::Hidden); |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 59 | static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond", |
Chris Lattner | 769c86b | 2008-01-07 05:40:58 +0000 | [diff] [blame] | 60 | cl::init(false), cl::Hidden); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 61 | static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold", |
| 62 | cl::init(true), cl::Hidden); |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 63 | |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 64 | STATISTIC(NumSimple, "Number of simple if-conversions performed"); |
| 65 | STATISTIC(NumSimpleFalse, "Number of simple (F) if-conversions performed"); |
| 66 | STATISTIC(NumTriangle, "Number of triangle if-conversions performed"); |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 67 | STATISTIC(NumTriangleRev, "Number of triangle (R) if-conversions performed"); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 68 | STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed"); |
| 69 | STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed"); |
| 70 | STATISTIC(NumDiamonds, "Number of diamond if-conversions performed"); |
| 71 | STATISTIC(NumIfConvBBs, "Number of if-converted blocks"); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 72 | STATISTIC(NumDupBBs, "Number of duplicated blocks"); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 73 | STATISTIC(NumUnpred, "Number of true blocks of diamonds unpredicated"); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 74 | |
| 75 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 76 | class IfConverter : public MachineFunctionPass { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 77 | enum IfcvtKind { |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 78 | ICNotClassfied, // BB data valid, but not classified. |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 79 | ICSimpleFalse, // Same as ICSimple, but on the false path. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 80 | ICSimple, // BB is entry of an one split, no rejoin sub-CFG. |
| 81 | ICTriangleFRev, // Same as ICTriangleFalse, but false path rev condition. |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 82 | ICTriangleRev, // Same as ICTriangle, but true path rev condition. |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 83 | ICTriangleFalse, // Same as ICTriangle, but on the false path. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 84 | ICTriangle, // BB is entry of a triangle sub-CFG. |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 85 | ICDiamond // BB is entry of a diamond sub-CFG. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 88 | /// One per MachineBasicBlock, this is used to cache the result |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 89 | /// if-conversion feasibility analysis. This includes results from |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 90 | /// TargetInstrInfo::analyzeBranch() (i.e. TBB, FBB, and Cond), and its |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 91 | /// classification, and common tail block of its successors (if it's a |
Evan Cheng | 2e82cef | 2007-05-18 18:14:37 +0000 | [diff] [blame] | 92 | /// diamond shape), its size, whether it's predicable, and whether any |
| 93 | /// instruction can clobber the 'would-be' predicate. |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 94 | /// |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 95 | /// IsDone - True if BB is not to be considered for ifcvt. |
| 96 | /// IsBeingAnalyzed - True if BB is currently being analyzed. |
| 97 | /// IsAnalyzed - True if BB has been analyzed (info is still valid). |
| 98 | /// IsEnqueued - True if BB has been enqueued to be ifcvt'ed. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 99 | /// IsBrAnalyzable - True if analyzeBranch() returns false. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 100 | /// HasFallThrough - True if BB may fallthrough to the following BB. |
| 101 | /// IsUnpredicable - True if BB is known to be unpredicable. |
Evan Cheng | fbc7309 | 2007-07-10 17:50:43 +0000 | [diff] [blame] | 102 | /// ClobbersPred - True if BB could modify predicates (e.g. has |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 103 | /// cmp, call, etc.) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 104 | /// NonPredSize - Number of non-predicated instructions. |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 105 | /// ExtraCost - Extra cost for multi-cycle instructions. |
| 106 | /// ExtraCost2 - Some instructions are slower when predicated |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 107 | /// BB - Corresponding MachineBasicBlock. |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 108 | /// TrueBB / FalseBB- See analyzeBranch(). |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 109 | /// BrCond - Conditions for end of block conditional branches. |
| 110 | /// Predicate - Predicate used in the BB. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 111 | struct BBInfo { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 112 | bool IsDone : 1; |
| 113 | bool IsBeingAnalyzed : 1; |
| 114 | bool IsAnalyzed : 1; |
| 115 | bool IsEnqueued : 1; |
| 116 | bool IsBrAnalyzable : 1; |
| 117 | bool HasFallThrough : 1; |
| 118 | bool IsUnpredicable : 1; |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 119 | bool CannotBeCopied : 1; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 120 | bool ClobbersPred : 1; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 121 | unsigned NonPredSize; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 122 | unsigned ExtraCost; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 123 | unsigned ExtraCost2; |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 124 | MachineBasicBlock *BB; |
| 125 | MachineBasicBlock *TrueBB; |
| 126 | MachineBasicBlock *FalseBB; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 127 | SmallVector<MachineOperand, 4> BrCond; |
| 128 | SmallVector<MachineOperand, 4> Predicate; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 129 | BBInfo() : IsDone(false), IsBeingAnalyzed(false), |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 130 | IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false), |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 131 | HasFallThrough(false), IsUnpredicable(false), |
| 132 | CannotBeCopied(false), ClobbersPred(false), NonPredSize(0), |
| 133 | ExtraCost(0), ExtraCost2(0), BB(nullptr), TrueBB(nullptr), |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 134 | FalseBB(nullptr) {} |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 137 | /// Record information about pending if-conversions to attempt: |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 138 | /// BBI - Corresponding BBInfo. |
| 139 | /// Kind - Type of block. See IfcvtKind. |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 140 | /// NeedSubsumption - True if the to-be-predicated BB has already been |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 141 | /// predicated. |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 142 | /// NumDups - Number of instructions that would be duplicated due |
| 143 | /// to this if-conversion. (For diamonds, the number of |
| 144 | /// identical instructions at the beginnings of both |
| 145 | /// paths). |
| 146 | /// NumDups2 - For diamonds, the number of identical instructions |
| 147 | /// at the ends of both paths. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 148 | struct IfcvtToken { |
| 149 | BBInfo &BBI; |
| 150 | IfcvtKind Kind; |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 151 | bool NeedSubsumption; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 152 | unsigned NumDups; |
| 153 | unsigned NumDups2; |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 154 | IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0) |
| 155 | : BBI(b), Kind(k), NeedSubsumption(s), NumDups(d), NumDups2(d2) {} |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 158 | /// Results of if-conversion feasibility analysis indexed by basic block |
| 159 | /// number. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 160 | std::vector<BBInfo> BBAnalysis; |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 161 | TargetSchedModel SchedModel; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 162 | |
Benjamin Kramer | 56b31bd | 2013-01-11 20:05:37 +0000 | [diff] [blame] | 163 | const TargetLoweringBase *TLI; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 164 | const TargetInstrInfo *TII; |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 165 | const TargetRegisterInfo *TRI; |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 166 | const MachineBranchProbabilityInfo *MBPI; |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 167 | MachineRegisterInfo *MRI; |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 168 | |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 169 | LivePhysRegs Redefs; |
| 170 | LivePhysRegs DontKill; |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 171 | |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 172 | bool PreRegAlloc; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 173 | bool MadeChange; |
Owen Anderson | 816e283 | 2009-06-24 23:41:44 +0000 | [diff] [blame] | 174 | int FnNum; |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 175 | std::function<bool(const Function &)> PredicateFtor; |
| 176 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 177 | public: |
| 178 | static char ID; |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 179 | IfConverter(std::function<bool(const Function &)> Ftor = nullptr) |
Benjamin Kramer | 82de7d3 | 2016-05-27 14:27:24 +0000 | [diff] [blame] | 180 | : MachineFunctionPass(ID), FnNum(-1), PredicateFtor(std::move(Ftor)) { |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 181 | initializeIfConverterPass(*PassRegistry::getPassRegistry()); |
| 182 | } |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 183 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 184 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Akira Hatanaka | bbd33f6 | 2014-08-07 19:30:13 +0000 | [diff] [blame] | 185 | AU.addRequired<MachineBlockFrequencyInfo>(); |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 186 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Owen Anderson | 1b35f4c | 2010-09-28 20:42:15 +0000 | [diff] [blame] | 187 | MachineFunctionPass::getAnalysisUsage(AU); |
| 188 | } |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 189 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 190 | bool runOnMachineFunction(MachineFunction &MF) override; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 191 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 192 | MachineFunctionProperties getRequiredProperties() const override { |
| 193 | return MachineFunctionProperties().set( |
| 194 | MachineFunctionProperties::Property::AllVRegsAllocated); |
| 195 | } |
| 196 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 197 | private: |
Kyle Butt | 59f2a2a | 2016-07-27 20:19:31 +0000 | [diff] [blame] | 198 | bool ReverseBranchCondition(BBInfo &BBI) const; |
Owen Anderson | f31f33e | 2010-10-01 22:45:50 +0000 | [diff] [blame] | 199 | bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 200 | BranchProbability Prediction) const; |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 201 | bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, |
Owen Anderson | f31f33e | 2010-10-01 22:45:50 +0000 | [diff] [blame] | 202 | bool FalseBranch, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 203 | BranchProbability Prediction) const; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 204 | bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI, |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 205 | unsigned &Dups1, unsigned &Dups2) const; |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 206 | void AnalyzeBranches(BBInfo &BBI); |
| 207 | void ScanInstructions(BBInfo &BBI, |
| 208 | MachineBasicBlock::iterator &Begin, |
| 209 | MachineBasicBlock::iterator &End) const; |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 210 | void AnalyzeBlock(MachineBasicBlock *MBB, |
| 211 | std::vector<std::unique_ptr<IfcvtToken>> &Tokens); |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 212 | bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond, |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 213 | bool isTriangle = false, bool RevBranch = false); |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 214 | void AnalyzeBlocks(MachineFunction &MF, |
| 215 | std::vector<std::unique_ptr<IfcvtToken>> &Tokens); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 216 | void InvalidatePreds(MachineBasicBlock *BB); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 217 | void RemoveExtraEdges(BBInfo &BBI); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 218 | bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind); |
| 219 | bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 220 | bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, |
| 221 | unsigned NumDups1, unsigned NumDups2); |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 222 | void PredicateBlock(BBInfo &BBI, |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 223 | MachineBasicBlock::iterator E, |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 224 | SmallVectorImpl<MachineOperand> &Cond, |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 225 | SmallSet<unsigned, 4> *LaterRedefs = nullptr); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 226 | void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 227 | SmallVectorImpl<MachineOperand> &Cond, |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 228 | bool IgnoreBr = false); |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 229 | void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true); |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 230 | |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 231 | bool MeetIfcvtSizeLimit(MachineBasicBlock &BB, |
| 232 | unsigned Cycle, unsigned Extra, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 233 | BranchProbability Prediction) const { |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 234 | return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 235 | Prediction); |
Evan Cheng | 02b184d | 2010-06-25 22:42:03 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 238 | bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB, |
| 239 | unsigned TCycle, unsigned TExtra, |
| 240 | MachineBasicBlock &FBB, |
| 241 | unsigned FCycle, unsigned FExtra, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 242 | BranchProbability Prediction) const { |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 243 | return TCycle > 0 && FCycle > 0 && |
| 244 | TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 245 | Prediction); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 248 | /// Returns true if Block ends without a terminator. |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 249 | bool blockAlwaysFallThrough(BBInfo &BBI) const { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 250 | return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr; |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 253 | /// Used to sort if-conversion candidates. |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 254 | static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1, |
| 255 | const std::unique_ptr<IfcvtToken> &C2) { |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 256 | int Incr1 = (C1->Kind == ICDiamond) |
| 257 | ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups; |
| 258 | int Incr2 = (C2->Kind == ICDiamond) |
| 259 | ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups; |
| 260 | if (Incr1 > Incr2) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 261 | return true; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 262 | else if (Incr1 == Incr2) { |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 263 | // Favors subsumption. |
David Blaikie | dc3f01e | 2015-03-09 01:57:13 +0000 | [diff] [blame] | 264 | if (!C1->NeedSubsumption && C2->NeedSubsumption) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 265 | return true; |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 266 | else if (C1->NeedSubsumption == C2->NeedSubsumption) { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 267 | // Favors diamond over triangle, etc. |
| 268 | if ((unsigned)C1->Kind < (unsigned)C2->Kind) |
| 269 | return true; |
| 270 | else if (C1->Kind == C2->Kind) |
| 271 | return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber(); |
| 272 | } |
| 273 | } |
| 274 | return false; |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 275 | } |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 276 | }; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 277 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 278 | char IfConverter::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 279 | } |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 280 | |
Andrew Trick | 1fa5bcb | 2012-02-08 21:23:13 +0000 | [diff] [blame] | 281 | char &llvm::IfConverterID = IfConverter::ID; |
| 282 | |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 283 | INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false) |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 284 | INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 285 | INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false) |
Bob Wilson | 97b9312 | 2009-10-28 20:46:46 +0000 | [diff] [blame] | 286 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 287 | bool IfConverter::runOnMachineFunction(MachineFunction &MF) { |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 288 | if (skipFunction(*MF.getFunction()) || |
| 289 | (PredicateFtor && !PredicateFtor(*MF.getFunction()))) |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 290 | return false; |
| 291 | |
Eric Christopher | 3d4276f | 2015-01-27 07:31:29 +0000 | [diff] [blame] | 292 | const TargetSubtargetInfo &ST = MF.getSubtarget(); |
| 293 | TLI = ST.getTargetLowering(); |
| 294 | TII = ST.getInstrInfo(); |
| 295 | TRI = ST.getRegisterInfo(); |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 296 | BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>()); |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 297 | MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 298 | MRI = &MF.getRegInfo(); |
Pete Cooper | 1175945 | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 299 | SchedModel.init(ST.getSchedModel(), &ST, TII); |
Arnold Schwaighofer | d2f96b9 | 2013-09-30 15:28:56 +0000 | [diff] [blame] | 300 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 301 | if (!TII) return false; |
| 302 | |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 303 | PreRegAlloc = MRI->isSSA(); |
| 304 | |
| 305 | bool BFChange = false; |
| 306 | if (!PreRegAlloc) { |
| 307 | // Tail merge tend to expose more if-conversion opportunities. |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 308 | BranchFolder BF(true, false, MBFI, *MBPI); |
Eric Christopher | 3d4276f | 2015-01-27 07:31:29 +0000 | [diff] [blame] | 309 | BFChange = BF.OptimizeFunction(MF, TII, ST.getRegisterInfo(), |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 310 | getAnalysisIfAvailable<MachineModuleInfo>()); |
Evan Cheng | c5adcca | 2012-06-08 21:53:50 +0000 | [diff] [blame] | 311 | } |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 312 | |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 313 | DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'" |
Craig Topper | a538d83 | 2012-08-22 06:07:19 +0000 | [diff] [blame] | 314 | << MF.getName() << "\'"); |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 315 | |
| 316 | if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) { |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 317 | DEBUG(dbgs() << " skipped\n"); |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 318 | return false; |
| 319 | } |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 320 | DEBUG(dbgs() << "\n"); |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 321 | |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 322 | MF.RenumberBlocks(); |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 323 | BBAnalysis.resize(MF.getNumBlockIDs()); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 324 | |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 325 | std::vector<std::unique_ptr<IfcvtToken>> Tokens; |
Evan Cheng | 478b805 | 2007-05-18 01:55:58 +0000 | [diff] [blame] | 326 | MadeChange = false; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 327 | unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + |
| 328 | NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds; |
| 329 | while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) { |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 330 | // Do an initial analysis for each basic block and find all the potential |
| 331 | // candidates to perform if-conversion. |
Bob Wilson | fc7d739 | 2010-06-15 18:57:15 +0000 | [diff] [blame] | 332 | bool Change = false; |
| 333 | AnalyzeBlocks(MF, Tokens); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 334 | while (!Tokens.empty()) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 335 | std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back()); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 336 | Tokens.pop_back(); |
| 337 | BBInfo &BBI = Token->BBI; |
| 338 | IfcvtKind Kind = Token->Kind; |
Nuno Lopes | 57c6594 | 2008-11-04 13:02:59 +0000 | [diff] [blame] | 339 | unsigned NumDups = Token->NumDups; |
Duncan Sands | dd571d3 | 2008-11-04 18:05:30 +0000 | [diff] [blame] | 340 | unsigned NumDups2 = Token->NumDups2; |
Nuno Lopes | 57c6594 | 2008-11-04 13:02:59 +0000 | [diff] [blame] | 341 | |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 342 | // If the block has been evicted out of the queue or it has already been |
| 343 | // marked dead (due to it being predicated), then skip it. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 344 | if (BBI.IsDone) |
| 345 | BBI.IsEnqueued = false; |
| 346 | if (!BBI.IsEnqueued) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 347 | continue; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 348 | |
Evan Cheng | 1e6f08b | 2007-06-14 20:28:52 +0000 | [diff] [blame] | 349 | BBI.IsEnqueued = false; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 350 | |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 351 | bool RetVal = false; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 352 | switch (Kind) { |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 353 | default: llvm_unreachable("Unexpected!"); |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 354 | case ICSimple: |
Evan Cheng | b30a894 | 2007-06-06 01:12:44 +0000 | [diff] [blame] | 355 | case ICSimpleFalse: { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 356 | bool isFalse = Kind == ICSimpleFalse; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 357 | if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break; |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 358 | DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ? |
| 359 | " false" : "") |
Bill Wendling | fb36316 | 2009-08-22 20:11:17 +0000 | [diff] [blame] | 360 | << "): BB#" << BBI.BB->getNumber() << " (" |
| 361 | << ((Kind == ICSimpleFalse) |
| 362 | ? BBI.FalseBB->getNumber() |
| 363 | : BBI.TrueBB->getNumber()) << ") "); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 364 | RetVal = IfConvertSimple(BBI, Kind); |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 365 | DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); |
Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 366 | if (RetVal) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 367 | if (isFalse) ++NumSimpleFalse; |
| 368 | else ++NumSimple; |
Anton Korobeynikov | 035eaac | 2008-02-20 11:10:28 +0000 | [diff] [blame] | 369 | } |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 370 | break; |
Evan Cheng | b30a894 | 2007-06-06 01:12:44 +0000 | [diff] [blame] | 371 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 372 | case ICTriangle: |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 373 | case ICTriangleRev: |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 374 | case ICTriangleFalse: |
Reid Spencer | 14b62a5 | 2007-06-10 00:19:17 +0000 | [diff] [blame] | 375 | case ICTriangleFRev: { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 376 | bool isFalse = Kind == ICTriangleFalse; |
| 377 | bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev); |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 378 | if (DisableTriangle && !isFalse && !isRev) break; |
| 379 | if (DisableTriangleR && !isFalse && isRev) break; |
| 380 | if (DisableTriangleF && isFalse && !isRev) break; |
| 381 | if (DisableTriangleFR && isFalse && isRev) break; |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 382 | DEBUG(dbgs() << "Ifcvt (Triangle"); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 383 | if (isFalse) |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 384 | DEBUG(dbgs() << " false"); |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 385 | if (isRev) |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 386 | DEBUG(dbgs() << " rev"); |
| 387 | DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:" |
Bill Wendling | fb36316 | 2009-08-22 20:11:17 +0000 | [diff] [blame] | 388 | << BBI.TrueBB->getNumber() << ",F:" |
| 389 | << BBI.FalseBB->getNumber() << ") "); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 390 | RetVal = IfConvertTriangle(BBI, Kind); |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 391 | DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 392 | if (RetVal) { |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 393 | if (isFalse) { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 394 | if (isRev) ++NumTriangleFRev; |
| 395 | else ++NumTriangleFalse; |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 396 | } else { |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 397 | if (isRev) ++NumTriangleRev; |
| 398 | else ++NumTriangle; |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 399 | } |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 400 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 401 | break; |
Reid Spencer | 14b62a5 | 2007-06-10 00:19:17 +0000 | [diff] [blame] | 402 | } |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 403 | case ICDiamond: { |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 404 | if (DisableDiamond) break; |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 405 | DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:" |
Bill Wendling | fb36316 | 2009-08-22 20:11:17 +0000 | [diff] [blame] | 406 | << BBI.TrueBB->getNumber() << ",F:" |
| 407 | << BBI.FalseBB->getNumber() << ") "); |
Nuno Lopes | 57c6594 | 2008-11-04 13:02:59 +0000 | [diff] [blame] | 408 | RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2); |
David Greene | 72e47cd | 2010-01-04 22:02:01 +0000 | [diff] [blame] | 409 | DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 410 | if (RetVal) ++NumDiamonds; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 411 | break; |
| 412 | } |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 415 | Change |= RetVal; |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 416 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 417 | NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev + |
| 418 | NumTriangleFalse + NumTriangleFRev + NumDiamonds; |
| 419 | if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit) |
Evan Cheng | e93ccc0 | 2007-06-08 19:10:51 +0000 | [diff] [blame] | 420 | break; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 421 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 422 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 423 | if (!Change) |
| 424 | break; |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 425 | MadeChange |= Change; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 426 | } |
Evan Cheng | 478b805 | 2007-05-18 01:55:58 +0000 | [diff] [blame] | 427 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 428 | Tokens.clear(); |
Evan Cheng | 478b805 | 2007-05-18 01:55:58 +0000 | [diff] [blame] | 429 | BBAnalysis.clear(); |
| 430 | |
Evan Cheng | cf9e8a9 | 2010-06-18 22:17:13 +0000 | [diff] [blame] | 431 | if (MadeChange && IfCvtBranchFold) { |
Haicheng Wu | 5b458cc | 2016-06-09 15:24:29 +0000 | [diff] [blame] | 432 | BranchFolder BF(false, false, MBFI, *MBPI); |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 433 | BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(), |
Evan Cheng | 3d2fce0 | 2009-09-04 07:47:40 +0000 | [diff] [blame] | 434 | getAnalysisIfAvailable<MachineModuleInfo>()); |
| 435 | } |
| 436 | |
Evan Cheng | 2d51c7c | 2010-06-18 23:09:54 +0000 | [diff] [blame] | 437 | MadeChange |= BFChange; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 438 | return MadeChange; |
| 439 | } |
| 440 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 441 | /// BB has a fallthrough. Find its 'false' successor given its 'true' successor. |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 442 | static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB, |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 443 | MachineBasicBlock *TrueBB) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 444 | for (MachineBasicBlock *SuccBB : BB->successors()) { |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 445 | if (SuccBB != TrueBB) |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 446 | return SuccBB; |
| 447 | } |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 448 | return nullptr; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 451 | /// Reverse the condition of the end of the block branch. Swap block's 'true' |
| 452 | /// and 'false' successors. |
Kyle Butt | 59f2a2a | 2016-07-27 20:19:31 +0000 | [diff] [blame] | 453 | bool IfConverter::ReverseBranchCondition(BBInfo &BBI) const { |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 454 | DebugLoc dl; // FIXME: this is nowhere |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 455 | if (!TII->ReverseBranchCondition(BBI.BrCond)) { |
| 456 | TII->RemoveBranch(*BBI.BB); |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 457 | TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl); |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 458 | std::swap(BBI.TrueBB, BBI.FalseBB); |
| 459 | return true; |
| 460 | } |
| 461 | return false; |
| 462 | } |
| 463 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 464 | /// Returns the next block in the function blocks ordering. If it is the end, |
| 465 | /// returns NULL. |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 466 | static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) { |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 467 | MachineFunction::iterator I = BB->getIterator(); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 468 | MachineFunction::iterator E = BB->getParent()->end(); |
| 469 | if (++I == E) |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 470 | return nullptr; |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 471 | return &*I; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 474 | /// Returns true if the 'true' block (along with its predecessor) forms a valid |
| 475 | /// simple shape for ifcvt. It also returns the number of instructions that the |
| 476 | /// ifcvt would need to duplicate if performed in Dups. |
Owen Anderson | 1b35f4c | 2010-09-28 20:42:15 +0000 | [diff] [blame] | 477 | bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 478 | BranchProbability Prediction) const { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 479 | Dups = 0; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 480 | if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 481 | return false; |
| 482 | |
Evan Cheng | a955c02 | 2007-06-19 21:45:13 +0000 | [diff] [blame] | 483 | if (TrueBBI.IsBrAnalyzable) |
| 484 | return false; |
| 485 | |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 486 | if (TrueBBI.BB->pred_size() > 1) { |
| 487 | if (TrueBBI.CannotBeCopied || |
Owen Anderson | 1b35f4c | 2010-09-28 20:42:15 +0000 | [diff] [blame] | 488 | !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 489 | Prediction)) |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 490 | return false; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 491 | Dups = TrueBBI.NonPredSize; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Evan Cheng | a955c02 | 2007-06-19 21:45:13 +0000 | [diff] [blame] | 494 | return true; |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 497 | /// Returns true if the 'true' and 'false' blocks (along with their common |
| 498 | /// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is |
| 499 | /// true, it checks if 'true' block's false branch branches to the 'false' block |
| 500 | /// rather than the other way around. It also returns the number of instructions |
| 501 | /// that the ifcvt would need to duplicate if performed in 'Dups'. |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 502 | bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, |
Owen Anderson | f31f33e | 2010-10-01 22:45:50 +0000 | [diff] [blame] | 503 | bool FalseBranch, unsigned &Dups, |
Cong Hou | c536bd9 | 2015-09-10 23:10:42 +0000 | [diff] [blame] | 504 | BranchProbability Prediction) const { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 505 | Dups = 0; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 506 | if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 507 | return false; |
| 508 | |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 509 | if (TrueBBI.BB->pred_size() > 1) { |
| 510 | if (TrueBBI.CannotBeCopied) |
| 511 | return false; |
| 512 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 513 | unsigned Size = TrueBBI.NonPredSize; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 514 | if (TrueBBI.IsBrAnalyzable) { |
Dan Gohman | 70de4cb | 2008-01-29 13:02:09 +0000 | [diff] [blame] | 515 | if (TrueBBI.TrueBB && TrueBBI.BrCond.empty()) |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 516 | // Ends with an unconditional branch. It will be removed. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 517 | --Size; |
| 518 | else { |
| 519 | MachineBasicBlock *FExit = FalseBranch |
| 520 | ? TrueBBI.TrueBB : TrueBBI.FalseBB; |
| 521 | if (FExit) |
| 522 | // Require a conditional branch |
| 523 | ++Size; |
| 524 | } |
| 525 | } |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 526 | if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction)) |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 527 | return false; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 528 | Dups = Size; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 529 | } |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 530 | |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 531 | MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB; |
| 532 | if (!TExit && blockAlwaysFallThrough(TrueBBI)) { |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 533 | MachineFunction::iterator I = TrueBBI.BB->getIterator(); |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 534 | if (++I == TrueBBI.BB->getParent()->end()) |
| 535 | return false; |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 536 | TExit = &*I; |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 537 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 538 | return TExit && TExit == FalseBBI.BB; |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 541 | /// Increment \p It until it points to a non-debug instruction or to \p End. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 542 | /// @param It Iterator to increment |
| 543 | /// @param End Iterator that points to end. Will be compared to It |
| 544 | /// @returns true if It == End, false otherwise. |
| 545 | static inline bool skipDebugInstructionsForward( |
| 546 | MachineBasicBlock::iterator &It, |
| 547 | MachineBasicBlock::iterator &End) { |
| 548 | while (It != End && It->isDebugValue()) |
| 549 | It++; |
| 550 | return It == End; |
| 551 | } |
| 552 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 553 | /// Decrement \p It until it points to a non-debug instruction or to \p Begin. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 554 | /// @param It Iterator to decrement. |
David Majnemer | 70c93fa | 2016-08-06 08:37:12 +0000 | [diff] [blame] | 555 | /// @param Begin Iterator that points to beginning. Will be compared to It |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 556 | /// @returns true if It == Begin, false otherwise. |
| 557 | static inline bool skipDebugInstructionsBackward( |
| 558 | MachineBasicBlock::iterator &It, |
| 559 | MachineBasicBlock::iterator &Begin) { |
| 560 | while (It != Begin && It->isDebugValue()) |
| 561 | It--; |
| 562 | return It == Begin; |
| 563 | } |
| 564 | |
Kyle Butt | 4f0e287 | 2016-08-06 01:52:33 +0000 | [diff] [blame] | 565 | /// Count duplicated instructions and move the iterators to show where they |
| 566 | /// are. |
| 567 | /// @param TIB True Iterator Begin |
| 568 | /// @param FIB False Iterator Begin |
| 569 | /// These two iterators initially point to the first instruction of the two |
| 570 | /// blocks, and finally point to the first non-shared instruction. |
| 571 | /// @param TIE True Iterator End |
| 572 | /// @param FIE False Iterator End |
| 573 | /// These two iterators initially point to End() for the two blocks() and |
| 574 | /// finally point to the first shared instruction in the tail. |
| 575 | /// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of |
| 576 | /// two blocks. |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 577 | static void countDuplicatedInstructions( |
| 578 | MachineBasicBlock::iterator &TIB, |
| 579 | MachineBasicBlock::iterator &FIB, |
| 580 | MachineBasicBlock::iterator &TIE, |
| 581 | MachineBasicBlock::iterator &FIE, |
| 582 | unsigned &Dups1, unsigned &Dups2, |
| 583 | MachineBasicBlock &TBB, MachineBasicBlock &FBB, |
| 584 | bool SkipConditionalBranches) { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 585 | |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 586 | while (TIB != TIE && FIB != FIE) { |
Evan Cheng | c0e0d85 | 2010-06-18 21:52:57 +0000 | [diff] [blame] | 587 | // Skip dbg_value instructions. These do not count. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 588 | if(skipDebugInstructionsForward(TIB, TIE)) |
| 589 | break; |
| 590 | if(skipDebugInstructionsForward(FIB, FIE)) |
| 591 | break; |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 592 | if (!TIB->isIdenticalTo(*FIB)) |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 593 | break; |
| 594 | ++Dups1; |
| 595 | ++TIB; |
| 596 | ++FIB; |
| 597 | } |
| 598 | |
| 599 | // Now, in preparation for counting duplicate instructions at the ends of the |
| 600 | // blocks, move the end iterators up past any branch instructions. |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 601 | // If both blocks are returning don't skip the branches, since they will |
| 602 | // likely be both identical return instructions. In such cases the return |
| 603 | // can be left unpredicated. |
| 604 | // Check for already containing all of the block. |
| 605 | if (TIB == TIE || FIB == FIE) |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 606 | return; |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 607 | --TIE; |
| 608 | --FIE; |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 609 | if (!TBB.succ_empty() || !FBB.succ_empty()) { |
| 610 | if (SkipConditionalBranches) { |
| 611 | while (TIE != TIB && TIE->isBranch()) |
| 612 | --TIE; |
| 613 | while (FIE != FIB && FIE->isBranch()) |
| 614 | --FIE; |
| 615 | } else { |
| 616 | while (TIE != TIB && TIE->isUnconditionalBranch()) |
| 617 | --TIE; |
| 618 | while (FIE != FIB && FIE->isUnconditionalBranch()) |
| 619 | --FIE; |
| 620 | } |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // If Dups1 includes all of a block, then don't count duplicate |
| 624 | // instructions at the end of the blocks. |
Nico Weber | 99ceee8 | 2016-08-07 20:18:04 +0000 | [diff] [blame] | 625 | if (TIB == TIE || FIB == FIE) |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 626 | return; |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 627 | |
| 628 | // Count duplicate instructions at the ends of the blocks. |
| 629 | while (TIE != TIB && FIE != FIB) { |
| 630 | // Skip dbg_value instructions. These do not count. |
Kyle Butt | fe91682 | 2016-08-06 01:52:31 +0000 | [diff] [blame] | 631 | if (skipDebugInstructionsBackward(TIE, TIB)) |
| 632 | break; |
| 633 | if (skipDebugInstructionsBackward(FIE, FIB)) |
| 634 | break; |
Duncan P. N. Exon Smith | fd8cc23 | 2016-02-27 20:01:33 +0000 | [diff] [blame] | 635 | if (!TIE->isIdenticalTo(*FIE)) |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 636 | break; |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 637 | // If we are trying to make sure the conditional branches are the same, we |
| 638 | // still don't want to count them. |
| 639 | if (SkipConditionalBranches || !TIE->isBranch()) |
| 640 | ++Dups2; |
Bob Wilson | e1961fe | 2010-10-26 00:02:24 +0000 | [diff] [blame] | 641 | --TIE; |
| 642 | --FIE; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 643 | } |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 644 | } |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 645 | |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 646 | /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along |
| 647 | /// with their common predecessor) forms a valid diamond shape for ifcvt. |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 648 | bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI, |
| 649 | unsigned &Dups1, unsigned &Dups2) const { |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 650 | Dups1 = Dups2 = 0; |
| 651 | if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone || |
| 652 | FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone) |
| 653 | return false; |
| 654 | |
| 655 | MachineBasicBlock *TT = TrueBBI.TrueBB; |
| 656 | MachineBasicBlock *FT = FalseBBI.TrueBB; |
| 657 | |
| 658 | if (!TT && blockAlwaysFallThrough(TrueBBI)) |
| 659 | TT = getNextBlock(TrueBBI.BB); |
| 660 | if (!FT && blockAlwaysFallThrough(FalseBBI)) |
| 661 | FT = getNextBlock(FalseBBI.BB); |
| 662 | if (TT != FT) |
| 663 | return false; |
| 664 | if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable)) |
| 665 | return false; |
| 666 | if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) |
| 667 | return false; |
| 668 | |
| 669 | // FIXME: Allow true block to have an early exit? |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 670 | if (TrueBBI.FalseBB || FalseBBI.FalseBB || |
| 671 | (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred)) |
Kyle Butt | 9b6d99b | 2016-07-27 20:19:33 +0000 | [diff] [blame] | 672 | return false; |
| 673 | |
| 674 | // Count duplicate instructions at the beginning and end of the true and |
| 675 | // false blocks. |
| 676 | MachineBasicBlock::iterator TIB = TrueBBI.BB->begin(); |
| 677 | MachineBasicBlock::iterator FIB = FalseBBI.BB->begin(); |
| 678 | MachineBasicBlock::iterator TIE = TrueBBI.BB->end(); |
| 679 | MachineBasicBlock::iterator FIE = FalseBBI.BB->end(); |
| 680 | countDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2, |
| 681 | *TrueBBI.BB, *FalseBBI.BB, |
| 682 | /* SkipConditionalBranches */ true); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 683 | return true; |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 684 | } |
| 685 | |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 686 | /// AnalyzeBranches - Look at the branches at the end of a block to determine if |
| 687 | /// the block is predicable. |
| 688 | void IfConverter::AnalyzeBranches(BBInfo &BBI) { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 689 | if (BBI.IsDone) |
| 690 | return; |
| 691 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 692 | BBI.TrueBB = BBI.FalseBB = nullptr; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 693 | BBI.BrCond.clear(); |
| 694 | BBI.IsBrAnalyzable = |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 695 | !TII->analyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 696 | BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 697 | |
| 698 | if (BBI.BrCond.size()) { |
| 699 | // No false branch. This BB must end with a conditional branch and a |
| 700 | // fallthrough. |
| 701 | if (!BBI.FalseBB) |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 702 | BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB); |
Evan Cheng | b9bff58 | 2009-06-15 21:24:34 +0000 | [diff] [blame] | 703 | if (!BBI.FalseBB) { |
| 704 | // Malformed bcc? True and false blocks are the same? |
| 705 | BBI.IsUnpredicable = true; |
Evan Cheng | b9bff58 | 2009-06-15 21:24:34 +0000 | [diff] [blame] | 706 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 707 | } |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 708 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 709 | |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 710 | /// ScanInstructions - Scan all the instructions in the block to determine if |
| 711 | /// the block is predicable. In most cases, that means all the instructions |
| 712 | /// in the block are isPredicable(). Also checks if the block contains any |
| 713 | /// instruction which can clobber a predicate (e.g. condition code register). |
| 714 | /// If so, the block is not predicable unless it's the last instruction. |
| 715 | void IfConverter::ScanInstructions(BBInfo &BBI, |
| 716 | MachineBasicBlock::iterator &Begin, |
| 717 | MachineBasicBlock::iterator &End) const { |
| 718 | if (BBI.IsDone || BBI.IsUnpredicable) |
| 719 | return; |
| 720 | |
| 721 | bool AlreadyPredicated = !BBI.Predicate.empty(); |
| 722 | |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 723 | BBI.NonPredSize = 0; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 724 | BBI.ExtraCost = 0; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 725 | BBI.ExtraCost2 = 0; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 726 | BBI.ClobbersPred = false; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 727 | for (MachineInstr &MI : make_range(Begin, End)) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 728 | if (MI.isDebugValue()) |
Jim Grosbach | a1e08fb | 2010-06-04 23:01:26 +0000 | [diff] [blame] | 729 | continue; |
| 730 | |
Justin Lebar | 7dba2e0 | 2016-04-15 01:38:41 +0000 | [diff] [blame] | 731 | // It's unsafe to duplicate convergent instructions in this context, so set |
| 732 | // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the |
| 733 | // following CFG, which is subject to our "simple" transformation. |
| 734 | // |
| 735 | // BB0 // if (c1) goto BB1; else goto BB2; |
| 736 | // / \ |
| 737 | // BB1 | |
| 738 | // | BB2 // if (c2) goto TBB; else goto FBB; |
| 739 | // | / | |
| 740 | // | / | |
| 741 | // TBB | |
| 742 | // | | |
| 743 | // | FBB |
| 744 | // | |
| 745 | // exit |
| 746 | // |
| 747 | // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd |
| 748 | // be unconditional, and in BB2, they'd be predicated upon c2), and suppose |
| 749 | // TBB contains a convergent instruction. This is safe iff doing so does |
| 750 | // not add a control-flow dependency to the convergent instruction -- i.e., |
| 751 | // it's safe iff the set of control flows that leads us to the convergent |
| 752 | // instruction does not get smaller after the transformation. |
| 753 | // |
| 754 | // Originally we executed TBB if c1 || c2. After the transformation, there |
| 755 | // are two copies of TBB's instructions. We get to the first if c1, and we |
| 756 | // get to the second if !c1 && c2. |
| 757 | // |
| 758 | // There are clearly fewer ways to satisfy the condition "c1" than |
| 759 | // "c1 || c2". Since we've shrunk the set of control flows which lead to |
| 760 | // our convergent instruction, the transformation is unsafe. |
| 761 | if (MI.isNotDuplicable() || MI.isConvergent()) |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 762 | BBI.CannotBeCopied = true; |
| 763 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 764 | bool isPredicated = TII->isPredicated(MI); |
| 765 | bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch(); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 766 | |
Joey Gouly | a5153cb | 2013-09-09 14:21:49 +0000 | [diff] [blame] | 767 | // A conditional branch is not predicable, but it may be eliminated. |
| 768 | if (isCondBr) |
| 769 | continue; |
| 770 | |
| 771 | if (!isPredicated) { |
| 772 | BBI.NonPredSize++; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 773 | unsigned ExtraPredCost = TII->getPredicationCost(MI); |
| 774 | unsigned NumCycles = SchedModel.computeInstrLatency(&MI, false); |
Joey Gouly | a5153cb | 2013-09-09 14:21:49 +0000 | [diff] [blame] | 775 | if (NumCycles > 1) |
| 776 | BBI.ExtraCost += NumCycles-1; |
| 777 | BBI.ExtraCost2 += ExtraPredCost; |
| 778 | } else if (!AlreadyPredicated) { |
| 779 | // FIXME: This instruction is already predicated before the |
| 780 | // if-conversion pass. It's probably something like a conditional move. |
| 781 | // Mark this block unpredicable for now. |
| 782 | BBI.IsUnpredicable = true; |
| 783 | return; |
Evan Cheng | 96c1457 | 2007-07-06 23:24:39 +0000 | [diff] [blame] | 784 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 785 | |
| 786 | if (BBI.ClobbersPred && !isPredicated) { |
| 787 | // Predicate modification instruction should end the block (except for |
| 788 | // already predicated instructions and end of block branches). |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 789 | // Predicate may have been modified, the subsequent (currently) |
Evan Cheng | 96c1457 | 2007-07-06 23:24:39 +0000 | [diff] [blame] | 790 | // unpredicated instructions cannot be correctly predicated. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 791 | BBI.IsUnpredicable = true; |
| 792 | return; |
| 793 | } |
| 794 | |
Evan Cheng | fbc7309 | 2007-07-10 17:50:43 +0000 | [diff] [blame] | 795 | // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are |
| 796 | // still potentially predicable. |
| 797 | std::vector<MachineOperand> PredDefs; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 798 | if (TII->DefinesPredicate(MI, PredDefs)) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 799 | BBI.ClobbersPred = true; |
| 800 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 801 | if (!TII->isPredicable(MI)) { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 802 | BBI.IsUnpredicable = true; |
| 803 | return; |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 808 | /// Determine if the block is a suitable candidate to be predicated by the |
| 809 | /// specified predicate. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 810 | bool IfConverter::FeasibilityAnalysis(BBInfo &BBI, |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 811 | SmallVectorImpl<MachineOperand> &Pred, |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 812 | bool isTriangle, bool RevBranch) { |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 813 | // If the block is dead or unpredicable, then it cannot be predicated. |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 814 | if (BBI.IsDone || BBI.IsUnpredicable) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 815 | return false; |
| 816 | |
Ahmed Bougacha | 7173b66 | 2015-03-21 01:23:15 +0000 | [diff] [blame] | 817 | // If it is already predicated but we couldn't analyze its terminator, the |
| 818 | // latter might fallthrough, but we can't determine where to. |
| 819 | // Conservatively avoid if-converting again. |
| 820 | if (BBI.Predicate.size() && !BBI.IsBrAnalyzable) |
| 821 | return false; |
| 822 | |
Quentin Colombet | bdab227 | 2013-07-24 20:20:37 +0000 | [diff] [blame] | 823 | // If it is already predicated, check if the new predicate subsumes |
| 824 | // its predicate. |
| 825 | if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate)) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 826 | return false; |
| 827 | |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 828 | if (BBI.BrCond.size()) { |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 829 | if (!isTriangle) |
| 830 | return false; |
| 831 | |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 832 | // Test predicate subsumption. |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 833 | SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end()); |
| 834 | SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 835 | if (RevBranch) { |
| 836 | if (TII->ReverseBranchCondition(Cond)) |
| 837 | return false; |
| 838 | } |
| 839 | if (TII->ReverseBranchCondition(RevPred) || |
| 840 | !TII->SubsumesPredicate(Cond, RevPred)) |
| 841 | return false; |
| 842 | } |
| 843 | |
| 844 | return true; |
| 845 | } |
| 846 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 847 | /// Analyze the structure of the sub-CFG starting from the specified block. |
| 848 | /// Record its successors and whether it looks like an if-conversion candidate. |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 849 | void IfConverter::AnalyzeBlock( |
| 850 | MachineBasicBlock *MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) { |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 851 | struct BBState { |
| 852 | BBState(MachineBasicBlock *BB) : MBB(BB), SuccsAnalyzed(false) {} |
| 853 | MachineBasicBlock *MBB; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 854 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 855 | /// This flag is true if MBB's successors have been analyzed. |
| 856 | bool SuccsAnalyzed; |
| 857 | }; |
Evan Cheng | 0f745da | 2007-05-18 00:20:58 +0000 | [diff] [blame] | 858 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 859 | // Push MBB to the stack. |
| 860 | SmallVector<BBState, 16> BBStack(1, MBB); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 861 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 862 | while (!BBStack.empty()) { |
| 863 | BBState &State = BBStack.back(); |
| 864 | MachineBasicBlock *BB = State.MBB; |
| 865 | BBInfo &BBI = BBAnalysis[BB->getNumber()]; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 866 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 867 | if (!State.SuccsAnalyzed) { |
| 868 | if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) { |
| 869 | BBStack.pop_back(); |
| 870 | continue; |
| 871 | } |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 872 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 873 | BBI.BB = BB; |
| 874 | BBI.IsBeingAnalyzed = true; |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 875 | |
Kyle Butt | 54bf3ce | 2016-08-06 01:52:34 +0000 | [diff] [blame] | 876 | AnalyzeBranches(BBI); |
| 877 | MachineBasicBlock::iterator Begin = BBI.BB->begin(); |
| 878 | MachineBasicBlock::iterator End = BBI.BB->end(); |
| 879 | ScanInstructions(BBI, Begin, End); |
Evan Cheng | b9bff58 | 2009-06-15 21:24:34 +0000 | [diff] [blame] | 880 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 881 | // Unanalyzable or ends with fallthrough or unconditional branch, or if is |
| 882 | // not considered for ifcvt anymore. |
| 883 | if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) { |
| 884 | BBI.IsBeingAnalyzed = false; |
| 885 | BBI.IsAnalyzed = true; |
| 886 | BBStack.pop_back(); |
| 887 | continue; |
| 888 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 889 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 890 | // Do not ifcvt if either path is a back edge to the entry block. |
| 891 | if (BBI.TrueBB == BB || BBI.FalseBB == BB) { |
| 892 | BBI.IsBeingAnalyzed = false; |
| 893 | BBI.IsAnalyzed = true; |
| 894 | BBStack.pop_back(); |
| 895 | continue; |
| 896 | } |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 897 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 898 | // Do not ifcvt if true and false fallthrough blocks are the same. |
| 899 | if (!BBI.FalseBB) { |
| 900 | BBI.IsBeingAnalyzed = false; |
| 901 | BBI.IsAnalyzed = true; |
| 902 | BBStack.pop_back(); |
| 903 | continue; |
| 904 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 905 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 906 | // Push the False and True blocks to the stack. |
| 907 | State.SuccsAnalyzed = true; |
| 908 | BBStack.push_back(BBI.FalseBB); |
| 909 | BBStack.push_back(BBI.TrueBB); |
| 910 | continue; |
| 911 | } |
Benjamin Kramer | 2016f0e | 2010-09-29 22:38:50 +0000 | [diff] [blame] | 912 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 913 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
| 914 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
Jakub Staszak | 15e5b74 | 2011-08-03 22:34:43 +0000 | [diff] [blame] | 915 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 916 | if (TrueBBI.IsDone && FalseBBI.IsDone) { |
| 917 | BBI.IsBeingAnalyzed = false; |
| 918 | BBI.IsAnalyzed = true; |
| 919 | BBStack.pop_back(); |
| 920 | continue; |
| 921 | } |
| 922 | |
| 923 | SmallVector<MachineOperand, 4> |
| 924 | RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); |
| 925 | bool CanRevCond = !TII->ReverseBranchCondition(RevCond); |
| 926 | |
| 927 | unsigned Dups = 0; |
| 928 | unsigned Dups2 = 0; |
| 929 | bool TNeedSub = !TrueBBI.Predicate.empty(); |
| 930 | bool FNeedSub = !FalseBBI.Predicate.empty(); |
| 931 | bool Enqueued = false; |
| 932 | |
| 933 | BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB); |
| 934 | |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 935 | if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) && |
| 936 | MeetIfcvtSizeLimit(*TrueBBI.BB, (TrueBBI.NonPredSize - (Dups + Dups2) + |
| 937 | TrueBBI.ExtraCost), TrueBBI.ExtraCost2, |
| 938 | *FalseBBI.BB, (FalseBBI.NonPredSize - (Dups + Dups2) + |
| 939 | FalseBBI.ExtraCost),FalseBBI.ExtraCost2, |
| 940 | Prediction) && |
| 941 | FeasibilityAnalysis(TrueBBI, BBI.BrCond) && |
| 942 | FeasibilityAnalysis(FalseBBI, RevCond)) { |
| 943 | // Diamond: |
| 944 | // EBB |
| 945 | // / \_ |
| 946 | // | | |
| 947 | // TBB FBB |
| 948 | // \ / |
| 949 | // TailBB |
| 950 | // Note TailBB can be empty. |
| 951 | Tokens.push_back(llvm::make_unique<IfcvtToken>( |
| 952 | BBI, ICDiamond, TNeedSub | FNeedSub, Dups, Dups2)); |
| 953 | Enqueued = true; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 956 | if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) && |
| 957 | MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, |
| 958 | TrueBBI.ExtraCost2, Prediction) && |
| 959 | FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) { |
| 960 | // Triangle: |
| 961 | // EBB |
| 962 | // | \_ |
| 963 | // | | |
| 964 | // | TBB |
| 965 | // | / |
| 966 | // FBB |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 967 | Tokens.push_back( |
| 968 | llvm::make_unique<IfcvtToken>(BBI, ICTriangle, TNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 969 | Enqueued = true; |
| 970 | } |
| 971 | |
| 972 | if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) && |
| 973 | MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, |
| 974 | TrueBBI.ExtraCost2, Prediction) && |
| 975 | FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 976 | Tokens.push_back( |
| 977 | llvm::make_unique<IfcvtToken>(BBI, ICTriangleRev, TNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 978 | Enqueued = true; |
| 979 | } |
| 980 | |
| 981 | if (ValidSimple(TrueBBI, Dups, Prediction) && |
| 982 | MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, |
| 983 | TrueBBI.ExtraCost2, Prediction) && |
| 984 | FeasibilityAnalysis(TrueBBI, BBI.BrCond)) { |
| 985 | // Simple (split, no rejoin): |
| 986 | // EBB |
| 987 | // | \_ |
| 988 | // | | |
| 989 | // | TBB---> exit |
| 990 | // | |
| 991 | // FBB |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 992 | Tokens.push_back( |
| 993 | llvm::make_unique<IfcvtToken>(BBI, ICSimple, TNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 994 | Enqueued = true; |
| 995 | } |
| 996 | |
| 997 | if (CanRevCond) { |
| 998 | // Try the other path... |
| 999 | if (ValidTriangle(FalseBBI, TrueBBI, false, Dups, |
| 1000 | Prediction.getCompl()) && |
| 1001 | MeetIfcvtSizeLimit(*FalseBBI.BB, |
| 1002 | FalseBBI.NonPredSize + FalseBBI.ExtraCost, |
| 1003 | FalseBBI.ExtraCost2, Prediction.getCompl()) && |
| 1004 | FeasibilityAnalysis(FalseBBI, RevCond, true)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1005 | Tokens.push_back(llvm::make_unique<IfcvtToken>(BBI, ICTriangleFalse, |
| 1006 | FNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1007 | Enqueued = true; |
| 1008 | } |
| 1009 | |
| 1010 | if (ValidTriangle(FalseBBI, TrueBBI, true, Dups, |
| 1011 | Prediction.getCompl()) && |
| 1012 | MeetIfcvtSizeLimit(*FalseBBI.BB, |
| 1013 | FalseBBI.NonPredSize + FalseBBI.ExtraCost, |
Jakub Staszak | 9b07c0a | 2011-07-10 02:58:07 +0000 | [diff] [blame] | 1014 | FalseBBI.ExtraCost2, Prediction.getCompl()) && |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1015 | FeasibilityAnalysis(FalseBBI, RevCond, true, true)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1016 | Tokens.push_back( |
| 1017 | llvm::make_unique<IfcvtToken>(BBI, ICTriangleFRev, FNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1018 | Enqueued = true; |
| 1019 | } |
| 1020 | |
| 1021 | if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) && |
| 1022 | MeetIfcvtSizeLimit(*FalseBBI.BB, |
| 1023 | FalseBBI.NonPredSize + FalseBBI.ExtraCost, |
| 1024 | FalseBBI.ExtraCost2, Prediction.getCompl()) && |
| 1025 | FeasibilityAnalysis(FalseBBI, RevCond)) { |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1026 | Tokens.push_back( |
| 1027 | llvm::make_unique<IfcvtToken>(BBI, ICSimpleFalse, FNeedSub, Dups)); |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1028 | Enqueued = true; |
| 1029 | } |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Akira Hatanaka | 14348aa | 2015-06-24 20:34:35 +0000 | [diff] [blame] | 1032 | BBI.IsEnqueued = Enqueued; |
| 1033 | BBI.IsBeingAnalyzed = false; |
| 1034 | BBI.IsAnalyzed = true; |
| 1035 | BBStack.pop_back(); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1036 | } |
Evan Cheng | 2e82cef | 2007-05-18 18:14:37 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1039 | /// Analyze all blocks and find entries for all if-conversion candidates. |
Justin Lebar | 3a7bc57 | 2016-02-22 17:51:28 +0000 | [diff] [blame] | 1040 | void IfConverter::AnalyzeBlocks( |
| 1041 | MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1042 | for (MachineBasicBlock &MBB : MF) |
| 1043 | AnalyzeBlock(&MBB, Tokens); |
Evan Cheng | 905a8f4 | 2007-05-30 19:49:19 +0000 | [diff] [blame] | 1044 | |
Evan Cheng | 20e0599 | 2007-06-01 00:12:12 +0000 | [diff] [blame] | 1045 | // Sort to favor more complex ifcvt scheme. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1046 | std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1049 | /// Returns true either if ToMBB is the next block after MBB or that all the |
| 1050 | /// intervening blocks are empty (given MBB can fall through to its next block). |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1051 | static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) { |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 1052 | MachineFunction::iterator PI = BB->getIterator(); |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1053 | MachineFunction::iterator I = std::next(PI); |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 1054 | MachineFunction::iterator TI = ToBB->getIterator(); |
Evan Cheng | 2c1acd6 | 2007-06-05 07:05:25 +0000 | [diff] [blame] | 1055 | MachineFunction::iterator E = BB->getParent()->end(); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1056 | while (I != TI) { |
| 1057 | // Check isSuccessor to avoid case where the next block is empty, but |
| 1058 | // it's not a successor. |
Duncan P. N. Exon Smith | 5ae5939 | 2015-10-09 19:13:58 +0000 | [diff] [blame] | 1059 | if (I == E || !I->empty() || !PI->isSuccessor(&*I)) |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1060 | return false; |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1061 | PI = I++; |
| 1062 | } |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1063 | return true; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1066 | /// Invalidate predecessor BB info so it would be re-analyzed to determine if it |
| 1067 | /// can be if-converted. If predecessor is already enqueued, dequeue it! |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1068 | void IfConverter::InvalidatePreds(MachineBasicBlock *BB) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1069 | for (const MachineBasicBlock *Predecessor : BB->predecessors()) { |
Saleem Abdulrasool | f158ca3 | 2014-08-09 17:21:29 +0000 | [diff] [blame] | 1070 | BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()]; |
Evan Cheng | add9776 | 2007-06-14 23:34:09 +0000 | [diff] [blame] | 1071 | if (PBBI.IsDone || PBBI.BB == BB) |
Evan Cheng | 9fc56c0 | 2007-06-14 23:13:19 +0000 | [diff] [blame] | 1072 | continue; |
Evan Cheng | add9776 | 2007-06-14 23:34:09 +0000 | [diff] [blame] | 1073 | PBBI.IsAnalyzed = false; |
| 1074 | PBBI.IsEnqueued = false; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1078 | /// Inserts an unconditional branch from \p MBB to \p ToMBB. |
Evan Cheng | c2237ce | 2007-05-29 22:31:16 +0000 | [diff] [blame] | 1079 | static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB, |
| 1080 | const TargetInstrInfo *TII) { |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 1081 | DebugLoc dl; // FIXME: this is nowhere |
Dan Gohman | 14714cb | 2008-08-22 16:07:55 +0000 | [diff] [blame] | 1082 | SmallVector<MachineOperand, 0> NoCond; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1083 | TII->InsertBranch(*BB, ToBB, nullptr, NoCond, dl); |
Evan Cheng | c2237ce | 2007-05-29 22:31:16 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1086 | /// Remove true / false edges if either / both are no longer successors. |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1087 | void IfConverter::RemoveExtraEdges(BBInfo &BBI) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1088 | MachineBasicBlock *TBB = nullptr, *FBB = nullptr; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1089 | SmallVector<MachineOperand, 4> Cond; |
Jacques Pienaar | 71c30a1 | 2016-07-15 14:41:04 +0000 | [diff] [blame] | 1090 | if (!TII->analyzeBranch(*BBI.BB, TBB, FBB, Cond)) |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1091 | BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty()); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1094 | /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1095 | /// values defined in MI which are also live/used by MI. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1096 | static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) { |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1097 | const TargetRegisterInfo *TRI = MI.getParent()->getParent() |
| 1098 | ->getSubtarget().getRegisterInfo(); |
| 1099 | |
| 1100 | // Before stepping forward past MI, remember which regs were live |
| 1101 | // before MI. This is needed to set the Undef flag only when reg is |
| 1102 | // dead. |
| 1103 | SparseSet<unsigned> LiveBeforeMI; |
| 1104 | LiveBeforeMI.setUniverse(TRI->getNumRegs()); |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1105 | for (unsigned Reg : Redefs) |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1106 | LiveBeforeMI.insert(Reg); |
| 1107 | |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1108 | SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Clobbers; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1109 | Redefs.stepForward(MI, Clobbers); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1110 | |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1111 | // Now add the implicit uses for each of the clobbered values. |
| 1112 | for (auto Reg : Clobbers) { |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1113 | // FIXME: Const cast here is nasty, but better than making StepForward |
| 1114 | // take a mutable instruction instead of const. |
Pete Cooper | 2748391 | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 1115 | MachineOperand &Op = const_cast<MachineOperand&>(*Reg.second); |
| 1116 | MachineInstr *OpMI = Op.getParent(); |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1117 | MachineInstrBuilder MIB(*OpMI->getParent()->getParent(), OpMI); |
Pete Cooper | ce9ad75 | 2015-05-05 22:09:41 +0000 | [diff] [blame] | 1118 | if (Op.isRegMask()) { |
| 1119 | // First handle regmasks. They clobber any entries in the mask which |
| 1120 | // means that we need a def for those registers. |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1121 | if (LiveBeforeMI.count(Reg.first)) |
| 1122 | MIB.addReg(Reg.first, RegState::Implicit); |
Pete Cooper | ce9ad75 | 2015-05-05 22:09:41 +0000 | [diff] [blame] | 1123 | |
| 1124 | // We also need to add an implicit def of this register for the later |
| 1125 | // use to read from. |
| 1126 | // For the register allocator to have allocated a register clobbered |
| 1127 | // by the call which is used later, it must be the case that |
| 1128 | // the call doesn't return. |
| 1129 | MIB.addReg(Reg.first, RegState::Implicit | RegState::Define); |
| 1130 | continue; |
| 1131 | } |
| 1132 | assert(Op.isReg() && "Register operand required"); |
Pete Cooper | 2748391 | 2015-05-06 22:51:04 +0000 | [diff] [blame] | 1133 | if (Op.isDead()) { |
| 1134 | // If we found a dead def, but it needs to be live, then remove the dead |
| 1135 | // flag. |
| 1136 | if (Redefs.contains(Op.getReg())) |
| 1137 | Op.setIsDead(false); |
| 1138 | } |
Jonas Paulsson | 196986c | 2016-08-03 05:46:35 +0000 | [diff] [blame] | 1139 | if (LiveBeforeMI.count(Reg.first)) |
| 1140 | MIB.addReg(Reg.first, RegState::Implicit); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1141 | } |
| 1142 | } |
| 1143 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1144 | /// Remove kill flags from operands with a registers in the \p DontKill set. |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1145 | static void RemoveKills(MachineInstr &MI, const LivePhysRegs &DontKill) { |
Duncan P. N. Exon Smith | f9ab416 | 2016-02-27 17:05:33 +0000 | [diff] [blame] | 1146 | for (MIBundleOperands O(MI); O.isValid(); ++O) { |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1147 | if (!O->isReg() || !O->isKill()) |
| 1148 | continue; |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1149 | if (DontKill.contains(O->getReg())) |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1150 | O->setIsKill(false); |
| 1151 | } |
| 1152 | } |
| 1153 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1154 | /// Walks a range of machine instructions and removes kill flags for registers |
| 1155 | /// in the \p DontKill set. |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1156 | static void RemoveKills(MachineBasicBlock::iterator I, |
| 1157 | MachineBasicBlock::iterator E, |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1158 | const LivePhysRegs &DontKill, |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1159 | const MCRegisterInfo &MCRI) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1160 | for (MachineInstr &MI : make_range(I, E)) |
| 1161 | RemoveKills(MI, DontKill); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1164 | /// If convert a simple (split, no rejoin) sub-CFG. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1165 | bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) { |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1166 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
| 1167 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
| 1168 | BBInfo *CvtBBI = &TrueBBI; |
| 1169 | BBInfo *NextBBI = &FalseBBI; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1170 | |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1171 | SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1172 | if (Kind == ICSimpleFalse) |
Evan Cheng | 4dcf1e8 | 2007-06-01 20:29:21 +0000 | [diff] [blame] | 1173 | std::swap(CvtBBI, NextBBI); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1174 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1175 | if (CvtBBI->IsDone || |
| 1176 | (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) { |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1177 | // Something has changed. It's no longer safe to predicate this block. |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1178 | BBI.IsAnalyzed = false; |
| 1179 | CvtBBI->IsAnalyzed = false; |
| 1180 | return false; |
Evan Cheng | 4dcf1e8 | 2007-06-01 20:29:21 +0000 | [diff] [blame] | 1181 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1182 | |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1183 | if (CvtBBI->BB->hasAddressTaken()) |
| 1184 | // Conservatively abort if-conversion if BB's address is taken. |
| 1185 | return false; |
| 1186 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1187 | if (Kind == ICSimpleFalse) |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1188 | if (TII->ReverseBranchCondition(Cond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1189 | llvm_unreachable("Unable to reverse branch condition!"); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1190 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1191 | // Initialize liveins to the first BB. These are potentiall redefined by |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1192 | // predicated instructions. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1193 | Redefs.init(TRI); |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 1194 | Redefs.addLiveIns(*CvtBBI->BB); |
| 1195 | Redefs.addLiveIns(*NextBBI->BB); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1196 | |
| 1197 | // Compute a set of registers which must not be killed by instructions in |
| 1198 | // BB1: This is everything live-in to BB2. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1199 | DontKill.init(TRI); |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 1200 | DontKill.addLiveIns(*NextBBI->BB); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1201 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1202 | if (CvtBBI->BB->pred_size() > 1) { |
| 1203 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1204 | // Copy instructions in the true block, predicate them, and add them to |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1205 | // the entry block. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1206 | CopyAndPredicateBlock(BBI, *CvtBBI, Cond); |
Hal Finkel | 95081bf | 2013-04-10 22:05:25 +0000 | [diff] [blame] | 1207 | |
| 1208 | // RemoveExtraEdges won't work if the block has an unanalyzable branch, so |
| 1209 | // explicitly remove CvtBBI as a successor. |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 1210 | BBI.BB->removeSuccessor(CvtBBI->BB, true); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1211 | } else { |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1212 | RemoveKills(CvtBBI->BB->begin(), CvtBBI->BB->end(), DontKill, *TRI); |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1213 | PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1214 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1215 | // Merge converted block into entry block. |
| 1216 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
| 1217 | MergeBlocks(BBI, *CvtBBI); |
| 1218 | } |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 1219 | |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1220 | bool IterIfcvt = true; |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1221 | if (!canFallThroughTo(BBI.BB, NextBBI->BB)) { |
Evan Cheng | c2237ce | 2007-05-29 22:31:16 +0000 | [diff] [blame] | 1222 | InsertUncondBranch(BBI.BB, NextBBI->BB, TII); |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1223 | BBI.HasFallThrough = false; |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 1224 | // Now ifcvt'd block will look like this: |
| 1225 | // BB: |
| 1226 | // ... |
| 1227 | // t, f = cmp |
| 1228 | // if t op |
| 1229 | // b BBf |
| 1230 | // |
| 1231 | // We cannot further ifcvt this block because the unconditional branch |
| 1232 | // will have to be predicated on the new condition, that will not be |
| 1233 | // available if cmp executes. |
| 1234 | IterIfcvt = false; |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1235 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1236 | |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1237 | RemoveExtraEdges(BBI); |
| 1238 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1239 | // Update block info. BB can be iteratively if-converted. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1240 | if (!IterIfcvt) |
| 1241 | BBI.IsDone = true; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1242 | InvalidatePreds(BBI.BB); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1243 | CvtBBI->IsDone = true; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1244 | |
| 1245 | // FIXME: Must maintain LiveIns. |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1246 | return true; |
| 1247 | } |
| 1248 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1249 | /// If convert a triangle sub-CFG. |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1250 | bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) { |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1251 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1252 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
| 1253 | BBInfo *CvtBBI = &TrueBBI; |
| 1254 | BBInfo *NextBBI = &FalseBBI; |
Stuart Hastings | 0125b64 | 2010-06-17 22:43:56 +0000 | [diff] [blame] | 1255 | DebugLoc dl; // FIXME: this is nowhere |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1256 | |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1257 | SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1258 | if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1259 | std::swap(CvtBBI, NextBBI); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1260 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1261 | if (CvtBBI->IsDone || |
| 1262 | (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) { |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1263 | // Something has changed. It's no longer safe to predicate this block. |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1264 | BBI.IsAnalyzed = false; |
| 1265 | CvtBBI->IsAnalyzed = false; |
| 1266 | return false; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1267 | } |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1268 | |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1269 | if (CvtBBI->BB->hasAddressTaken()) |
| 1270 | // Conservatively abort if-conversion if BB's address is taken. |
| 1271 | return false; |
| 1272 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1273 | if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1274 | if (TII->ReverseBranchCondition(Cond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1275 | llvm_unreachable("Unable to reverse branch condition!"); |
Evan Cheng | 23402fc | 2007-06-15 21:18:05 +0000 | [diff] [blame] | 1276 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1277 | if (Kind == ICTriangleRev || Kind == ICTriangleFRev) { |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1278 | if (ReverseBranchCondition(*CvtBBI)) { |
| 1279 | // BB has been changed, modify its predecessors (except for this |
| 1280 | // one) so they don't get ifcvt'ed based on bad intel. |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1281 | for (MachineBasicBlock *PBB : CvtBBI->BB->predecessors()) { |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1282 | if (PBB == BBI.BB) |
| 1283 | continue; |
| 1284 | BBInfo &PBBI = BBAnalysis[PBB->getNumber()]; |
| 1285 | if (PBBI.IsEnqueued) { |
| 1286 | PBBI.IsAnalyzed = false; |
| 1287 | PBBI.IsEnqueued = false; |
| 1288 | } |
Evan Cheng | add9776 | 2007-06-14 23:34:09 +0000 | [diff] [blame] | 1289 | } |
Evan Cheng | 9acfa7b | 2007-06-12 23:54:05 +0000 | [diff] [blame] | 1290 | } |
| 1291 | } |
Evan Cheng | 6a2cf07 | 2007-06-01 07:41:07 +0000 | [diff] [blame] | 1292 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1293 | // Initialize liveins to the first BB. These are potentially redefined by |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1294 | // predicated instructions. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1295 | Redefs.init(TRI); |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 1296 | Redefs.addLiveIns(*CvtBBI->BB); |
| 1297 | Redefs.addLiveIns(*NextBBI->BB); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1298 | |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1299 | DontKill.clear(); |
| 1300 | |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1301 | bool HasEarlyExit = CvtBBI->FalseBB != nullptr; |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1302 | BranchProbability CvtNext, CvtFalse, BBNext, BBCvt; |
Saleem Abdulrasool | f158ca3 | 2014-08-09 17:21:29 +0000 | [diff] [blame] | 1303 | |
Manman Ren | b681918 | 2014-01-29 23:18:47 +0000 | [diff] [blame] | 1304 | if (HasEarlyExit) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1305 | // Get probabilities before modifying CvtBBI->BB and BBI.BB. |
| 1306 | CvtNext = MBPI->getEdgeProbability(CvtBBI->BB, NextBBI->BB); |
| 1307 | CvtFalse = MBPI->getEdgeProbability(CvtBBI->BB, CvtBBI->FalseBB); |
| 1308 | BBNext = MBPI->getEdgeProbability(BBI.BB, NextBBI->BB); |
| 1309 | BBCvt = MBPI->getEdgeProbability(BBI.BB, CvtBBI->BB); |
Manman Ren | b681918 | 2014-01-29 23:18:47 +0000 | [diff] [blame] | 1310 | } |
Saleem Abdulrasool | f158ca3 | 2014-08-09 17:21:29 +0000 | [diff] [blame] | 1311 | |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1312 | if (CvtBBI->BB->pred_size() > 1) { |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1313 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1314 | // Copy instructions in the true block, predicate them, and add them to |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1315 | // the entry block. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1316 | CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true); |
Hal Finkel | 95081bf | 2013-04-10 22:05:25 +0000 | [diff] [blame] | 1317 | |
| 1318 | // RemoveExtraEdges won't work if the block has an unanalyzable branch, so |
| 1319 | // explicitly remove CvtBBI as a successor. |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 1320 | BBI.BB->removeSuccessor(CvtBBI->BB, true); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1321 | } else { |
| 1322 | // Predicate the 'true' block after removing its branch. |
| 1323 | CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB); |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1324 | PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1325 | |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1326 | // Now merge the entry of the triangle with the true block. |
| 1327 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1328 | MergeBlocks(BBI, *CvtBBI, false); |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1331 | // If 'true' block has a 'false' successor, add an exit branch to it. |
Evan Cheng | 6e4babe | 2007-06-05 01:31:40 +0000 | [diff] [blame] | 1332 | if (HasEarlyExit) { |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1333 | SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(), |
| 1334 | CvtBBI->BrCond.end()); |
Evan Cheng | 6a2cf07 | 2007-06-01 07:41:07 +0000 | [diff] [blame] | 1335 | if (TII->ReverseBranchCondition(RevCond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1336 | llvm_unreachable("Unable to reverse branch condition!"); |
Hans Wennborg | 1dbaf67 | 2015-12-01 03:49:42 +0000 | [diff] [blame] | 1337 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1338 | // Update the edge probability for both CvtBBI->FalseBB and NextBBI. |
| 1339 | // NewNext = New_Prob(BBI.BB, NextBBI->BB) = |
| 1340 | // Prob(BBI.BB, NextBBI->BB) + |
| 1341 | // Prob(BBI.BB, CvtBBI->BB) * Prob(CvtBBI->BB, NextBBI->BB) |
| 1342 | // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) = |
| 1343 | // Prob(BBI.BB, CvtBBI->BB) * Prob(CvtBBI->BB, CvtBBI->FalseBB) |
| 1344 | auto NewTrueBB = getNextBlock(BBI.BB); |
| 1345 | auto NewNext = BBNext + BBCvt * CvtNext; |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1346 | auto NewTrueBBIter = find(BBI.BB->successors(), NewTrueBB); |
Cong Hou | cb07d70 | 2015-12-01 21:50:20 +0000 | [diff] [blame] | 1347 | if (NewTrueBBIter != BBI.BB->succ_end()) |
| 1348 | BBI.BB->setSuccProbability(NewTrueBBIter, NewNext); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1349 | |
| 1350 | auto NewFalse = BBCvt * CvtFalse; |
| 1351 | TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, nullptr, RevCond, dl); |
| 1352 | BBI.BB->addSuccessor(CvtBBI->FalseBB, NewFalse); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1353 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 1354 | |
| 1355 | // Merge in the 'false' block if the 'false' block has no other |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1356 | // predecessors. Otherwise, add an unconditional branch to 'false'. |
Evan Cheng | 17aad816 | 2007-06-05 00:07:37 +0000 | [diff] [blame] | 1357 | bool FalseBBDead = false; |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1358 | bool IterIfcvt = true; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1359 | bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB); |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1360 | if (!isFallThrough) { |
| 1361 | // Only merge them if the true block does not fallthrough to the false |
| 1362 | // block. By not merging them, we make it possible to iteratively |
| 1363 | // ifcvt the blocks. |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1364 | if (!HasEarlyExit && |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1365 | NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough && |
| 1366 | !NextBBI->BB->hasAddressTaken()) { |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1367 | MergeBlocks(BBI, *NextBBI); |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1368 | FalseBBDead = true; |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1369 | } else { |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1370 | InsertUncondBranch(BBI.BB, NextBBI->BB, TII); |
| 1371 | BBI.HasFallThrough = false; |
Evan Cheng | d3f3f0a | 2007-06-07 08:13:00 +0000 | [diff] [blame] | 1372 | } |
Evan Cheng | 7783f82 | 2007-06-08 09:36:04 +0000 | [diff] [blame] | 1373 | // Mixed predicated and unpredicated code. This cannot be iteratively |
| 1374 | // predicated. |
| 1375 | IterIfcvt = false; |
Evan Cheng | e4ec918 | 2007-06-06 02:08:52 +0000 | [diff] [blame] | 1376 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1377 | |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1378 | RemoveExtraEdges(BBI); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1379 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1380 | // Update block info. BB can be iteratively if-converted. |
Bob Wilson | 8105144 | 2010-06-15 22:18:54 +0000 | [diff] [blame] | 1381 | if (!IterIfcvt) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1382 | BBI.IsDone = true; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1383 | InvalidatePreds(BBI.BB); |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1384 | CvtBBI->IsDone = true; |
Evan Cheng | 17aad816 | 2007-06-05 00:07:37 +0000 | [diff] [blame] | 1385 | if (FalseBBDead) |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1386 | NextBBI->IsDone = true; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1387 | |
| 1388 | // FIXME: Must maintain LiveIns. |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1389 | return true; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1392 | /// If convert a diamond sub-CFG. |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1393 | bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, |
| 1394 | unsigned NumDups1, unsigned NumDups2) { |
| 1395 | BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; |
| 1396 | BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; |
| 1397 | MachineBasicBlock *TailBB = TrueBBI.TrueBB; |
| 1398 | // True block must fall through or end with an unanalyzable terminator. |
| 1399 | if (!TailBB) { |
| 1400 | if (blockAlwaysFallThrough(TrueBBI)) |
| 1401 | TailBB = FalseBBI.TrueBB; |
| 1402 | assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!"); |
| 1403 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1404 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1405 | if (TrueBBI.IsDone || FalseBBI.IsDone || |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1406 | TrueBBI.BB->pred_size() > 1 || |
| 1407 | FalseBBI.BB->pred_size() > 1) { |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1408 | // Something has changed. It's no longer safe to predicate these blocks. |
| 1409 | BBI.IsAnalyzed = false; |
| 1410 | TrueBBI.IsAnalyzed = false; |
| 1411 | FalseBBI.IsAnalyzed = false; |
| 1412 | return false; |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1413 | } |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1414 | |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1415 | if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken()) |
| 1416 | // Conservatively abort if-conversion if either BB has its address taken. |
| 1417 | return false; |
| 1418 | |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1419 | // Put the predicated instructions from the 'true' block before the |
| 1420 | // instructions from the 'false' block, unless the true block would clobber |
| 1421 | // the predicate, in which case, do the opposite. |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1422 | BBInfo *BBI1 = &TrueBBI; |
| 1423 | BBInfo *BBI2 = &FalseBBI; |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1424 | SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); |
Dan Gohman | 97d95d6 | 2008-10-21 03:29:32 +0000 | [diff] [blame] | 1425 | if (TII->ReverseBranchCondition(RevCond)) |
Craig Topper | ee4dab5 | 2012-02-05 08:31:47 +0000 | [diff] [blame] | 1426 | llvm_unreachable("Unable to reverse branch condition!"); |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1427 | SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond; |
| 1428 | SmallVector<MachineOperand, 4> *Cond2 = &RevCond; |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1429 | |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1430 | // Figure out the more profitable ordering. |
| 1431 | bool DoSwap = false; |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1432 | if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1433 | DoSwap = true; |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1434 | else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) { |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1435 | if (TrueBBI.NonPredSize > FalseBBI.NonPredSize) |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1436 | DoSwap = true; |
Evan Cheng | 3a51c85 | 2007-06-16 09:34:52 +0000 | [diff] [blame] | 1437 | } |
| 1438 | if (DoSwap) { |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1439 | std::swap(BBI1, BBI2); |
| 1440 | std::swap(Cond1, Cond2); |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1441 | } |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1442 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1443 | // Remove the conditional branch from entry to the blocks. |
| 1444 | BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB); |
| 1445 | |
Krzysztof Parzyszek | a003b76 | 2016-08-11 18:42:06 +0000 | [diff] [blame] | 1446 | // Initialize the Redefs: |
| 1447 | // - BB2 live-in regs need implicit uses before being redefined by BB1 |
| 1448 | // instructions. |
| 1449 | // - BB1 live-out regs need implicit uses before being redefined by BB2 |
| 1450 | // instructions. We start with BB1 live-ins so we have the live-out regs |
| 1451 | // after tracking the BB1 instructions. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1452 | Redefs.init(TRI); |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 1453 | Redefs.addLiveIns(*BBI1->BB); |
Krzysztof Parzyszek | a003b76 | 2016-08-11 18:42:06 +0000 | [diff] [blame] | 1454 | Redefs.addLiveIns(*BBI2->BB); |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1455 | |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1456 | // Remove the duplicated instructions at the beginnings of both paths. |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1457 | // Skip dbg_value instructions |
Benjamin Kramer | 6b56896 | 2015-06-23 14:47:29 +0000 | [diff] [blame] | 1458 | MachineBasicBlock::iterator DI1 = BBI1->BB->getFirstNonDebugInstr(); |
| 1459 | MachineBasicBlock::iterator DI2 = BBI2->BB->getFirstNonDebugInstr(); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1460 | BBI1->NonPredSize -= NumDups1; |
| 1461 | BBI2->NonPredSize -= NumDups1; |
Jim Grosbach | ee6e29a | 2010-06-28 20:26:00 +0000 | [diff] [blame] | 1462 | |
| 1463 | // Skip past the dups on each side separately since there may be |
| 1464 | // differing dbg_value entries. |
| 1465 | for (unsigned i = 0; i < NumDups1; ++DI1) { |
| 1466 | if (!DI1->isDebugValue()) |
| 1467 | ++i; |
| 1468 | } |
Jim Grosbach | c34befc | 2010-06-25 23:05:46 +0000 | [diff] [blame] | 1469 | while (NumDups1 != 0) { |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1470 | ++DI2; |
Jim Grosbach | ee6e29a | 2010-06-28 20:26:00 +0000 | [diff] [blame] | 1471 | if (!DI2->isDebugValue()) |
| 1472 | --NumDups1; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1473 | } |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1474 | |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1475 | // Compute a set of registers which must not be killed by instructions in BB1: |
| 1476 | // This is everything used+live in BB2 after the duplicated instructions. We |
| 1477 | // can compute this set by simulating liveness backwards from the end of BB2. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1478 | DontKill.init(TRI); |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1479 | for (const MachineInstr &MI : make_range(BBI2->BB->rbegin(), |
| 1480 | MachineBasicBlock::reverse_iterator(DI2))) |
| 1481 | DontKill.stepBackward(MI); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1482 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1483 | for (const MachineInstr &MI : make_range(BBI1->BB->begin(), DI1)) { |
Pete Cooper | 7605e37 | 2015-05-05 20:14:22 +0000 | [diff] [blame] | 1484 | SmallVector<std::pair<unsigned, const MachineOperand*>, 4> IgnoredClobbers; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1485 | Redefs.stepForward(MI, IgnoredClobbers); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1486 | } |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1487 | BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1); |
| 1488 | BBI2->BB->erase(BBI2->BB->begin(), DI2); |
| 1489 | |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1490 | // Remove branch from the 'true' block, unless it was not analyzable. |
| 1491 | // Non-analyzable branches need to be preserved, since in such cases, |
| 1492 | // the CFG structure is not an actual diamond (the join block may not |
| 1493 | // be present). |
| 1494 | if (BBI1->IsBrAnalyzable) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1495 | BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB); |
| 1496 | // Remove duplicated instructions. |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1497 | DI1 = BBI1->BB->end(); |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1498 | for (unsigned i = 0; i != NumDups2; ) { |
| 1499 | // NumDups2 only counted non-dbg_value instructions, so this won't |
| 1500 | // run off the head of the list. |
| 1501 | assert (DI1 != BBI1->BB->begin()); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1502 | --DI1; |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1503 | // skip dbg_value instructions |
| 1504 | if (!DI1->isDebugValue()) |
| 1505 | ++i; |
| 1506 | } |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1507 | BBI1->BB->erase(DI1, BBI1->BB->end()); |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1508 | |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1509 | // Kill flags in the true block for registers living into the false block |
| 1510 | // must be removed. |
| 1511 | RemoveKills(BBI1->BB->begin(), BBI1->BB->end(), DontKill, *TRI); |
| 1512 | |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1513 | // Remove 'false' block branch (unless it was not analyzable), and find |
| 1514 | // the last instruction to predicate. |
| 1515 | if (BBI2->IsBrAnalyzable) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1516 | BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1517 | DI2 = BBI2->BB->end(); |
| 1518 | while (NumDups2 != 0) { |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1519 | // NumDups2 only counted non-dbg_value instructions, so this won't |
| 1520 | // run off the head of the list. |
| 1521 | assert (DI2 != BBI2->BB->begin()); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1522 | --DI2; |
Jim Grosbach | 412800d | 2010-06-14 21:30:32 +0000 | [diff] [blame] | 1523 | // skip dbg_value instructions |
| 1524 | if (!DI2->isDebugValue()) |
| 1525 | --NumDups2; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1526 | } |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1527 | |
| 1528 | // Remember which registers would later be defined by the false block. |
| 1529 | // This allows us not to predicate instructions in the true block that would |
| 1530 | // later be re-defined. That is, rather than |
| 1531 | // subeq r0, r1, #1 |
| 1532 | // addne r0, r1, #1 |
| 1533 | // generate: |
| 1534 | // sub r0, r1, #1 |
| 1535 | // addne r0, r1, #1 |
| 1536 | SmallSet<unsigned, 4> RedefsByFalse; |
| 1537 | SmallSet<unsigned, 4> ExtUses; |
| 1538 | if (TII->isProfitableToUnpredicate(*BBI1->BB, *BBI2->BB)) { |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1539 | for (const MachineInstr &FI : make_range(BBI2->BB->begin(), DI2)) { |
| 1540 | if (FI.isDebugValue()) |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1541 | continue; |
| 1542 | SmallVector<unsigned, 4> Defs; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1543 | for (const MachineOperand &MO : FI.operands()) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1544 | if (!MO.isReg()) |
| 1545 | continue; |
| 1546 | unsigned Reg = MO.getReg(); |
| 1547 | if (!Reg) |
| 1548 | continue; |
| 1549 | if (MO.isDef()) { |
| 1550 | Defs.push_back(Reg); |
| 1551 | } else if (!RedefsByFalse.count(Reg)) { |
| 1552 | // These are defined before ctrl flow reach the 'false' instructions. |
| 1553 | // They cannot be modified by the 'true' instructions. |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 1554 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 1555 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1556 | ExtUses.insert(*SubRegs); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
| 1559 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1560 | for (unsigned Reg : Defs) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1561 | if (!ExtUses.count(Reg)) { |
Chad Rosier | abdb1d6 | 2013-05-22 23:17:36 +0000 | [diff] [blame] | 1562 | for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); |
| 1563 | SubRegs.isValid(); ++SubRegs) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 1564 | RedefsByFalse.insert(*SubRegs); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | // Predicate the 'true' block. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1571 | PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1, &RedefsByFalse); |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1572 | |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1573 | // After predicating BBI1, if there is a predicated terminator in BBI1 and |
| 1574 | // a non-predicated in BBI2, then we don't want to predicate the one from |
| 1575 | // BBI2. The reason is that if we merged these blocks, we would end up with |
| 1576 | // two predicated terminators in the same block. |
| 1577 | if (!BBI2->BB->empty() && (DI2 == BBI2->BB->end())) { |
| 1578 | MachineBasicBlock::iterator BBI1T = BBI1->BB->getFirstTerminator(); |
| 1579 | MachineBasicBlock::iterator BBI2T = BBI2->BB->getFirstTerminator(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1580 | if (BBI1T != BBI1->BB->end() && TII->isPredicated(*BBI1T) && |
| 1581 | BBI2T != BBI2->BB->end() && !TII->isPredicated(*BBI2T)) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1582 | --DI2; |
| 1583 | } |
| 1584 | |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1585 | // Predicate the 'false' block. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1586 | PredicateBlock(*BBI2, DI2, *Cond2); |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1587 | |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1588 | // Merge the true block into the entry of the diamond. |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1589 | MergeBlocks(BBI, *BBI1, TailBB == nullptr); |
| 1590 | MergeBlocks(BBI, *BBI2, TailBB == nullptr); |
Evan Cheng | 3056599 | 2007-06-06 00:57:55 +0000 | [diff] [blame] | 1591 | |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1592 | // If the if-converted block falls through or unconditionally branches into |
| 1593 | // the tail block, and the tail block does not have other predecessors, then |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1594 | // fold the tail block in as well. Otherwise, unless it falls through to the |
| 1595 | // tail, add a unconditional branch to it. |
| 1596 | if (TailBB) { |
Pete Cooper | 77c703f | 2011-11-04 23:49:14 +0000 | [diff] [blame] | 1597 | BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()]; |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1598 | bool CanMergeTail = !TailBBI.HasFallThrough && |
| 1599 | !TailBBI.BB->hasAddressTaken(); |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1600 | // The if-converted block can still have a predicated terminator |
| 1601 | // (e.g. a predicated return). If that is the case, we cannot merge |
| 1602 | // it with the tail block. |
| 1603 | MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator(); |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1604 | if (TI != BBI.BB->end() && TII->isPredicated(*TI)) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1605 | CanMergeTail = false; |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1606 | // There may still be a fall-through edge from BBI1 or BBI2 to TailBB; |
| 1607 | // check if there are any other predecessors besides those. |
| 1608 | unsigned NumPreds = TailBB->pred_size(); |
| 1609 | if (NumPreds > 1) |
| 1610 | CanMergeTail = false; |
| 1611 | else if (NumPreds == 1 && CanMergeTail) { |
| 1612 | MachineBasicBlock::pred_iterator PI = TailBB->pred_begin(); |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1613 | if (*PI != BBI1->BB && *PI != BBI2->BB) |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1614 | CanMergeTail = false; |
| 1615 | } |
| 1616 | if (CanMergeTail) { |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1617 | MergeBlocks(BBI, TailBBI); |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1618 | TailBBI.IsDone = true; |
| 1619 | } else { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1620 | BBI.BB->addSuccessor(TailBB, BranchProbability::getOne()); |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1621 | InsertUncondBranch(BBI.BB, TailBB, TII); |
| 1622 | BBI.HasFallThrough = false; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1623 | } |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1626 | // RemoveExtraEdges won't work if the block has an unanalyzable branch, |
| 1627 | // which can happen here if TailBB is unanalyzable and is merged, so |
| 1628 | // explicitly remove BBI1 and BBI2 as successors. |
Diana Picus | 68be1eb | 2016-08-14 02:10:18 +0000 | [diff] [blame] | 1629 | BBI.BB->removeSuccessor(BBI1->BB); |
| 1630 | BBI.BB->removeSuccessor(BBI2->BB, true); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1631 | RemoveExtraEdges(BBI); |
| 1632 | |
Evan Cheng | 7948422 | 2007-06-05 23:46:14 +0000 | [diff] [blame] | 1633 | // Update block info. |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1634 | BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true; |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1635 | InvalidatePreds(BBI.BB); |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1636 | |
| 1637 | // FIXME: Must maintain LiveIns. |
Evan Cheng | e26c091 | 2007-05-21 22:22:58 +0000 | [diff] [blame] | 1638 | return true; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1641 | static bool MaySpeculate(const MachineInstr &MI, |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 1642 | SmallSet<unsigned, 4> &LaterRedefs) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1643 | bool SawStore = true; |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1644 | if (!MI.isSafeToMove(nullptr, SawStore)) |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1645 | return false; |
| 1646 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1647 | for (const MachineOperand &MO : MI.operands()) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1648 | if (!MO.isReg()) |
| 1649 | continue; |
| 1650 | unsigned Reg = MO.getReg(); |
| 1651 | if (!Reg) |
| 1652 | continue; |
| 1653 | if (MO.isDef() && !LaterRedefs.count(Reg)) |
| 1654 | return false; |
| 1655 | } |
| 1656 | |
| 1657 | return true; |
| 1658 | } |
| 1659 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1660 | /// Predicate instructions from the start of the block to the specified end with |
| 1661 | /// the specified condition. |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1662 | void IfConverter::PredicateBlock(BBInfo &BBI, |
Evan Cheng | 51eb2c3 | 2007-06-18 08:37:25 +0000 | [diff] [blame] | 1663 | MachineBasicBlock::iterator E, |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1664 | SmallVectorImpl<MachineOperand> &Cond, |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1665 | SmallSet<unsigned, 4> *LaterRedefs) { |
| 1666 | bool AnyUnpred = false; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1667 | bool MaySpec = LaterRedefs != nullptr; |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1668 | for (MachineInstr &I : make_range(BBI.BB->begin(), E)) { |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1669 | if (I.isDebugValue() || TII->isPredicated(I)) |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1670 | continue; |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1671 | // It may be possible not to predicate an instruction if it's the 'true' |
| 1672 | // side of a diamond and the 'false' side may re-define the instruction's |
| 1673 | // defs. |
Matthias Braun | 07066cc | 2015-05-19 21:22:20 +0000 | [diff] [blame] | 1674 | if (MaySpec && MaySpeculate(I, *LaterRedefs)) { |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1675 | AnyUnpred = true; |
| 1676 | continue; |
| 1677 | } |
| 1678 | // If any instruction is predicated, then every instruction after it must |
| 1679 | // be predicated. |
| 1680 | MaySpec = false; |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1681 | if (!TII->PredicateInstruction(I, Cond)) { |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1682 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1683 | dbgs() << "Unable to predicate " << I << "!\n"; |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1684 | #endif |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1685 | llvm_unreachable(nullptr); |
Evan Cheng | af71610 | 2007-05-16 21:54:37 +0000 | [diff] [blame] | 1686 | } |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1687 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1688 | // If the predicated instruction now redefines a register as the result of |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1689 | // if-conversion, add an implicit kill. |
Duncan P. N. Exon Smith | 0490cde | 2016-06-30 23:04:51 +0000 | [diff] [blame] | 1690 | UpdatePredRedefs(I, Redefs); |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1691 | } |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1692 | |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 1693 | BBI.Predicate.append(Cond.begin(), Cond.end()); |
Evan Cheng | df1a429 | 2007-06-08 19:17:12 +0000 | [diff] [blame] | 1694 | |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1695 | BBI.IsAnalyzed = false; |
| 1696 | BBI.NonPredSize = 0; |
| 1697 | |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1698 | ++NumIfConvBBs; |
Evan Cheng | 4266a79 | 2011-12-19 22:01:30 +0000 | [diff] [blame] | 1699 | if (AnyUnpred) |
| 1700 | ++NumUnpred; |
Evan Cheng | 312b723 | 2007-06-04 06:47:22 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1703 | /// Copy and predicate instructions from source BB to the destination block. |
| 1704 | /// Skip end of block branches if IgnoreBr is true. |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1705 | void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, |
Owen Anderson | 4f6bf04 | 2008-08-14 22:49:33 +0000 | [diff] [blame] | 1706 | SmallVectorImpl<MachineOperand> &Cond, |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1707 | bool IgnoreBr) { |
Dan Gohman | 3b46030 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 1708 | MachineFunction &MF = *ToBBI.BB->getParent(); |
| 1709 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1710 | for (MachineInstr &I : *FromBBI.BB) { |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1711 | // Do not copy the end of the block branches. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1712 | if (IgnoreBr && I.isBranch()) |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1713 | break; |
| 1714 | |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1715 | MachineInstr *MI = MF.CloneMachineInstr(&I); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1716 | ToBBI.BB->insert(ToBBI.BB->end(), MI); |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 1717 | ToBBI.NonPredSize++; |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1718 | unsigned ExtraPredCost = TII->getPredicationCost(I); |
| 1719 | unsigned NumCycles = SchedModel.computeInstrLatency(&I, false); |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 1720 | if (NumCycles > 1) |
| 1721 | ToBBI.ExtraCost += NumCycles-1; |
| 1722 | ToBBI.ExtraCost2 += ExtraPredCost; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1723 | |
Bob Wilson | f82c8fc | 2010-06-18 17:07:23 +0000 | [diff] [blame] | 1724 | if (!TII->isPredicated(I) && !MI->isDebugValue()) { |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1725 | if (!TII->PredicateInstruction(*MI, Cond)) { |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1726 | #ifndef NDEBUG |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1727 | dbgs() << "Unable to predicate " << I << "!\n"; |
Torok Edwin | 08954aa | 2009-07-12 20:07:01 +0000 | [diff] [blame] | 1728 | #endif |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1729 | llvm_unreachable(nullptr); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1730 | } |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Bob Wilson | 4581434 | 2010-06-19 05:33:57 +0000 | [diff] [blame] | 1733 | // If the predicated instruction now redefines a register as the result of |
Evan Cheng | f128bdc | 2010-06-16 07:35:02 +0000 | [diff] [blame] | 1734 | // if-conversion, add an implicit kill. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1735 | UpdatePredRedefs(*MI, Redefs); |
Matthias Braun | d616ccc | 2013-10-11 19:04:37 +0000 | [diff] [blame] | 1736 | |
| 1737 | // Some kill flags may not be correct anymore. |
Andrew Trick | 276dd45 | 2013-10-14 20:45:17 +0000 | [diff] [blame] | 1738 | if (!DontKill.empty()) |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 1739 | RemoveKills(*MI, DontKill); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1742 | if (!IgnoreBr) { |
| 1743 | std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(), |
| 1744 | FromBBI.BB->succ_end()); |
| 1745 | MachineBasicBlock *NBB = getNextBlock(FromBBI.BB); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1746 | MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr; |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1747 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1748 | for (MachineBasicBlock *Succ : Succs) { |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1749 | // Fallthrough edge can't be transferred. |
| 1750 | if (Succ == FallThrough) |
| 1751 | continue; |
| 1752 | ToBBI.BB->addSuccessor(Succ); |
| 1753 | } |
Evan Cheng | 0598b2d | 2007-06-18 22:44:57 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 1756 | ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end()); |
| 1757 | ToBBI.Predicate.append(Cond.begin(), Cond.end()); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1758 | |
| 1759 | ToBBI.ClobbersPred |= FromBBI.ClobbersPred; |
| 1760 | ToBBI.IsAnalyzed = false; |
| 1761 | |
Dan Gohman | d2d1ae1 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1762 | ++NumDupBBs; |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Matthias Braun | 2c93179 | 2016-08-17 02:51:57 +0000 | [diff] [blame] | 1765 | /// Move all instructions from FromBB to the end of ToBB. This will leave |
| 1766 | /// FromBB as an empty block, so remove all of its successor edges except for |
| 1767 | /// the fall-through edge. If AddEdges is true, i.e., when FromBBI's branch is |
| 1768 | /// being moved, add those successor edges to ToBBI. |
Bob Wilson | 1e5da55 | 2010-06-29 00:55:23 +0000 | [diff] [blame] | 1769 | void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) { |
Evan Cheng | 8b8e8d8 | 2013-05-05 18:03:49 +0000 | [diff] [blame] | 1770 | assert(!FromBBI.BB->hasAddressTaken() && |
| 1771 | "Removing a BB whose address is taken!"); |
| 1772 | |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1773 | // In case FromBBI.BB contains terminators (e.g. return instruction), |
| 1774 | // first move the non-terminator instructions, then the terminators. |
| 1775 | MachineBasicBlock::iterator FromTI = FromBBI.BB->getFirstTerminator(); |
| 1776 | MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator(); |
| 1777 | ToBBI.BB->splice(ToTI, FromBBI.BB, FromBBI.BB->begin(), FromTI); |
| 1778 | |
| 1779 | // If FromBB has non-predicated terminator we should copy it at the end. |
Duncan P. N. Exon Smith | 6307eb5 | 2016-02-23 02:46:52 +0000 | [diff] [blame] | 1780 | if (FromTI != FromBBI.BB->end() && !TII->isPredicated(*FromTI)) |
Krzysztof Parzyszek | 2451c48 | 2016-01-20 13:14:52 +0000 | [diff] [blame] | 1781 | ToTI = ToBBI.BB->end(); |
| 1782 | ToBBI.BB->splice(ToTI, FromBBI.BB, FromTI, FromBBI.BB->end()); |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1783 | |
Cong Hou | b9e8d48 | 2015-12-17 01:29:08 +0000 | [diff] [blame] | 1784 | // Force normalizing the successors' probabilities of ToBBI.BB to convert all |
| 1785 | // unknown probabilities into known ones. |
| 1786 | // FIXME: This usage is too tricky and in the future we would like to |
| 1787 | // eliminate all unknown probabilities in MBB. |
| 1788 | ToBBI.BB->normalizeSuccProbs(); |
| 1789 | |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1790 | SmallVector<MachineBasicBlock *, 4> FromSuccs(FromBBI.BB->succ_begin(), |
| 1791 | FromBBI.BB->succ_end()); |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1792 | MachineBasicBlock *NBB = getNextBlock(FromBBI.BB); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 1793 | MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr; |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1794 | // The edge probability from ToBBI.BB to FromBBI.BB, which is only needed when |
Hans Wennborg | 1dbaf67 | 2015-12-01 03:49:42 +0000 | [diff] [blame] | 1795 | // AddEdges is true and FromBBI.BB is a successor of ToBBI.BB. |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1796 | auto To2FromProb = BranchProbability::getZero(); |
Cong Hou | fa1917c | 2015-12-01 00:02:51 +0000 | [diff] [blame] | 1797 | if (AddEdges && ToBBI.BB->isSuccessor(FromBBI.BB)) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1798 | To2FromProb = MBPI->getEdgeProbability(ToBBI.BB, FromBBI.BB); |
| 1799 | // Set the edge probability from ToBBI.BB to FromBBI.BB to zero to avoid the |
| 1800 | // edge probability being merged to other edges when this edge is removed |
| 1801 | // later. |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1802 | ToBBI.BB->setSuccProbability(find(ToBBI.BB->successors(), FromBBI.BB), |
| 1803 | BranchProbability::getZero()); |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Matthias Braun | b1e0558 | 2016-08-17 02:51:59 +0000 | [diff] [blame^] | 1806 | for (MachineBasicBlock *Succ : FromSuccs) { |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1807 | // Fallthrough edge can't be transferred. |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 1808 | if (Succ == FallThrough) |
| 1809 | continue; |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1810 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1811 | auto NewProb = BranchProbability::getZero(); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1812 | if (AddEdges) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1813 | // Calculate the edge probability for the edge from ToBBI.BB to Succ, |
| 1814 | // which is a portion of the edge probability from FromBBI.BB to Succ. The |
| 1815 | // portion ratio is the edge probability from ToBBI.BB to FromBBI.BB (if |
| 1816 | // FromBBI is a successor of ToBBI.BB. See comment below for excepion). |
| 1817 | NewProb = MBPI->getEdgeProbability(FromBBI.BB, Succ); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1818 | |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1819 | // To2FromProb is 0 when FromBBI.BB is not a successor of ToBBI.BB. This |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1820 | // only happens when if-converting a diamond CFG and FromBBI.BB is the |
| 1821 | // tail BB. In this case FromBBI.BB post-dominates ToBBI.BB and hence we |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1822 | // could just use the probabilities on FromBBI.BB's out-edges when adding |
| 1823 | // new successors. |
| 1824 | if (!To2FromProb.isZero()) |
| 1825 | NewProb *= To2FromProb; |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 1828 | FromBBI.BB->removeSuccessor(Succ); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1829 | |
| 1830 | if (AddEdges) { |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1831 | // If the edge from ToBBI.BB to Succ already exists, update the |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 1832 | // probability of this edge by adding NewProb to it. An example is shown |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1833 | // below, in which A is ToBBI.BB and B is FromBBI.BB. In this case we |
| 1834 | // don't have to set C as A's successor as it already is. We only need to |
| 1835 | // update the edge probability on A->C. Note that B will not be |
| 1836 | // immediately removed from A's successors. It is possible that B->D is |
| 1837 | // not removed either if D is a fallthrough of B. Later the edge A->D |
| 1838 | // (generated here) and B->D will be combined into one edge. To maintain |
| 1839 | // correct edge probability of this combined edge, we need to set the edge |
| 1840 | // probability of A->B to zero, which is already done above. The edge |
| 1841 | // probability on A->D is calculated by scaling the original probability |
| 1842 | // on A->B by the probability of B->D. |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1843 | // |
| 1844 | // Before ifcvt: After ifcvt (assume B->D is kept): |
| 1845 | // |
| 1846 | // A A |
| 1847 | // /| /|\ |
| 1848 | // / B / B| |
| 1849 | // | /| | || |
| 1850 | // |/ | | |/ |
| 1851 | // C D C D |
| 1852 | // |
| 1853 | if (ToBBI.BB->isSuccessor(Succ)) |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1854 | ToBBI.BB->setSuccProbability( |
David Majnemer | 4253126 | 2016-08-12 03:55:06 +0000 | [diff] [blame] | 1855 | find(ToBBI.BB->successors(), Succ), |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1856 | MBPI->getEdgeProbability(ToBBI.BB, Succ) + NewProb); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1857 | else |
Cong Hou | d97c100 | 2015-12-01 05:29:22 +0000 | [diff] [blame] | 1858 | ToBBI.BB->addSuccessor(Succ, NewProb); |
Cong Hou | d40105d | 2015-09-18 20:22:41 +0000 | [diff] [blame] | 1859 | } |
Evan Cheng | be9859e | 2007-06-07 02:12:15 +0000 | [diff] [blame] | 1860 | } |
| 1861 | |
Bob Wilson | 2371f4f | 2009-05-13 23:25:24 +0000 | [diff] [blame] | 1862 | // Now FromBBI always falls through to the next block! |
Bob Wilson | 43f21dd | 2009-05-13 23:48:58 +0000 | [diff] [blame] | 1863 | if (NBB && !FromBBI.BB->isSuccessor(NBB)) |
Evan Cheng | 288f154 | 2007-06-08 22:01:07 +0000 | [diff] [blame] | 1864 | FromBBI.BB->addSuccessor(NBB); |
| 1865 | |
Cong Hou | c106989 | 2015-12-13 09:26:17 +0000 | [diff] [blame] | 1866 | // Normalize the probabilities of ToBBI.BB's successors with all adjustment |
| 1867 | // we've done above. |
| 1868 | ToBBI.BB->normalizeSuccProbs(); |
| 1869 | |
Benjamin Kramer | 4f6ac16 | 2015-02-28 10:11:12 +0000 | [diff] [blame] | 1870 | ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end()); |
Evan Cheng | 92fb545 | 2007-06-15 07:36:12 +0000 | [diff] [blame] | 1871 | FromBBI.Predicate.clear(); |
| 1872 | |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1873 | ToBBI.NonPredSize += FromBBI.NonPredSize; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 1874 | ToBBI.ExtraCost += FromBBI.ExtraCost; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 1875 | ToBBI.ExtraCost2 += FromBBI.ExtraCost2; |
Evan Cheng | d0e6691 | 2007-05-23 07:23:16 +0000 | [diff] [blame] | 1876 | FromBBI.NonPredSize = 0; |
Bob Wilson | efd360c | 2010-10-26 00:02:21 +0000 | [diff] [blame] | 1877 | FromBBI.ExtraCost = 0; |
Evan Cheng | debf9c5 | 2010-11-03 00:45:17 +0000 | [diff] [blame] | 1878 | FromBBI.ExtraCost2 = 0; |
Evan Cheng | 9030b98 | 2007-06-06 10:16:17 +0000 | [diff] [blame] | 1879 | |
Evan Cheng | 4dd31a7 | 2007-06-11 22:26:22 +0000 | [diff] [blame] | 1880 | ToBBI.ClobbersPred |= FromBBI.ClobbersPred; |
Evan Cheng | 2117d1f | 2007-06-09 01:03:43 +0000 | [diff] [blame] | 1881 | ToBBI.HasFallThrough = FromBBI.HasFallThrough; |
Evan Cheng | 1e6f08b | 2007-06-14 20:28:52 +0000 | [diff] [blame] | 1882 | ToBBI.IsAnalyzed = false; |
| 1883 | FromBBI.IsAnalyzed = false; |
Evan Cheng | f5e53a5 | 2007-05-16 02:00:57 +0000 | [diff] [blame] | 1884 | } |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 1885 | |
| 1886 | FunctionPass * |
| 1887 | llvm::createIfConverter(std::function<bool(const Function &)> Ftor) { |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 1888 | return new IfConverter(std::move(Ftor)); |
Akira Hatanaka | 4a61619 | 2015-06-08 18:50:43 +0000 | [diff] [blame] | 1889 | } |