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