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