blob: bbb977f090c52698ce035cc5aca686cf229b2437 [file] [log] [blame]
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +00001//===-- CoalesceBranches.cpp - Coalesce blocks with the same condition ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9///
10/// \file
11/// Coalesce basic blocks guarded by the same branch condition into a single
12/// basic block.
13///
14//===----------------------------------------------------------------------===//
15
Lei Huang34e66212017-09-12 18:39:11 +000016#include "PPC.h"
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000017#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/Statistic.h"
19#include "llvm/CodeGen/MachineDominators.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachinePostDominators.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/CodeGen/Passes.h"
David Blaikie1be62f02017-11-03 22:32:11 +000024#include "llvm/CodeGen/TargetFrameLowering.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000025#include "llvm/CodeGen/TargetInstrInfo.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000026#include "llvm/CodeGen/TargetSubtargetInfo.h"
David Blaikie3f833ed2017-11-08 01:01:31 +000027#include "llvm/Support/Debug.h"
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000028
29using namespace llvm;
30
Lei Huang34e66212017-09-12 18:39:11 +000031#define DEBUG_TYPE "ppc-branch-coalescing"
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000032
33STATISTIC(NumBlocksCoalesced, "Number of blocks coalesced");
34STATISTIC(NumPHINotMoved, "Number of PHI Nodes that cannot be merged");
35STATISTIC(NumBlocksNotCoalesced, "Number of blocks not coalesced");
36
Lei Huang34e66212017-09-12 18:39:11 +000037namespace llvm {
38 void initializePPCBranchCoalescingPass(PassRegistry&);
39}
40
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000041//===----------------------------------------------------------------------===//
Lei Huang34e66212017-09-12 18:39:11 +000042// PPCBranchCoalescing
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000043//===----------------------------------------------------------------------===//
44///
45/// Improve scheduling by coalescing branches that depend on the same condition.
46/// This pass looks for blocks that are guarded by the same branch condition
47/// and attempts to merge the blocks together. Such opportunities arise from
48/// the expansion of select statements in the IR.
49///
Lei Huang34e66212017-09-12 18:39:11 +000050/// This pass does not handle implicit operands on branch statements. In order
51/// to run on targets that use implicit operands, changes need to be made in the
52/// canCoalesceBranch and canMerge methods.
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000053///
Lei Huang34e66212017-09-12 18:39:11 +000054/// Example: the following LLVM IR
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000055///
Lei Huang34e66212017-09-12 18:39:11 +000056/// %test = icmp eq i32 %x 0
57/// %tmp1 = select i1 %test, double %a, double 2.000000e-03
58/// %tmp2 = select i1 %test, double %b, double 5.000000e-03
59///
60/// expands to the following machine code:
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000061///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000062/// %bb.0: derived from LLVM BB %entry
Francis Visoiu Mistrihfb7b14f72018-02-09 01:14:44 +000063/// liveins: %f1 %f3 %x6
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000064/// <SNIP1>
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000065/// %0 = COPY %f1; F8RC:%0
66/// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%4
67/// %8 = LXSDX %zero8, killed %7, implicit %rm;
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000068/// mem:LD8[ConstantPool] F8RC:%8 G8RC:%7
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000069/// BCC 76, %5, <%bb.2>; CRRC:%5
70/// Successors according to CFG: %bb.1(?%) %bb.2(?%)
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000071///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000072/// %bb.1: derived from LLVM BB %entry
73/// Predecessors according to CFG: %bb.0
74/// Successors according to CFG: %bb.2(?%)
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000075///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000076/// %bb.2: derived from LLVM BB %entry
77/// Predecessors according to CFG: %bb.0 %bb.1
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000078/// %9 = PHI %8, <%bb.1>, %0, <%bb.0>;
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000079/// F8RC:%9,%8,%0
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000080/// <SNIP2>
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000081/// BCC 76, %5, <%bb.4>; CRRC:%5
82/// Successors according to CFG: %bb.3(?%) %bb.4(?%)
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000083///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000084/// %bb.3: derived from LLVM BB %entry
85/// Predecessors according to CFG: %bb.2
86/// Successors according to CFG: %bb.4(?%)
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000087///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000088/// %bb.4: derived from LLVM BB %entry
89/// Predecessors according to CFG: %bb.2 %bb.3
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000090/// %13 = PHI %12, <%bb.3>, %2, <%bb.2>;
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +000091/// F8RC:%13,%12,%2
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000092/// <SNIP3>
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +000093/// BLR8 implicit %lr8, implicit %rm, implicit %f1
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000094///
95/// When this pattern is detected, branch coalescing will try to collapse
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000096/// it by moving code in %bb.2 to %bb.0 and/or %bb.4 and removing %bb.3.
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +000097///
98/// If all conditions are meet, IR should collapse to:
99///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000100/// %bb.0: derived from LLVM BB %entry
Francis Visoiu Mistrihfb7b14f72018-02-09 01:14:44 +0000101/// liveins: %f1 %f3 %x6
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000102/// <SNIP1>
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000103/// %0 = COPY %f1; F8RC:%0
104/// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%4
105/// %8 = LXSDX %zero8, killed %7, implicit %rm;
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +0000106/// mem:LD8[ConstantPool] F8RC:%8 G8RC:%7
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000107/// <SNIP2>
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000108/// BCC 76, %5, <%bb.4>; CRRC:%5
109/// Successors according to CFG: %bb.1(0x2aaaaaaa / 0x80000000 = 33.33%)
110/// %bb.4(0x55555554 / 0x80000000 = 66.67%)
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000111///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000112/// %bb.1: derived from LLVM BB %entry
113/// Predecessors according to CFG: %bb.0
114/// Successors according to CFG: %bb.4(0x40000000 / 0x80000000 = 50.00%)
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000115///
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +0000116/// %bb.4: derived from LLVM BB %entry
117/// Predecessors according to CFG: %bb.0 %bb.1
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000118/// %9 = PHI %8, <%bb.1>, %0, <%bb.0>;
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +0000119/// F8RC:%9,%8,%0
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000120/// %13 = PHI %12, <%bb.1>, %2, <%bb.0>;
Francis Visoiu Mistrih93ef1452017-11-30 12:12:19 +0000121/// F8RC:%13,%12,%2
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000122/// <SNIP3>
Francis Visoiu Mistriha8a83d12017-12-07 10:40:31 +0000123/// BLR8 implicit %lr8, implicit %rm, implicit %f1
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000124///
125/// Branch Coalescing does not split blocks, it moves everything in the same
126/// direction ensuring it does not break use/definition semantics.
127///
128/// PHI nodes and its corresponding use instructions are moved to its successor
129/// block if there are no uses within the successor block PHI nodes. PHI
130/// node ordering cannot be assumed.
131///
132/// Non-PHI can be moved up to the predecessor basic block or down to the
133/// successor basic block following any PHI instructions. Whether it moves
134/// up or down depends on whether the register(s) defined in the instructions
135/// are used in current block or in any PHI instructions at the beginning of
136/// the successor block.
137
138namespace {
139
Lei Huang34e66212017-09-12 18:39:11 +0000140class PPCBranchCoalescing : public MachineFunctionPass {
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000141 struct CoalescingCandidateInfo {
Simon Pilgrimb01bb3a2017-03-03 12:09:11 +0000142 MachineBasicBlock *BranchBlock; // Block containing the branch
143 MachineBasicBlock *BranchTargetBlock; // Block branched to
144 MachineBasicBlock *FallThroughBlock; // Fall-through if branch not taken
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000145 SmallVector<MachineOperand, 4> Cond;
146 bool MustMoveDown;
147 bool MustMoveUp;
148
149 CoalescingCandidateInfo();
150 void clear();
151 };
152
153 MachineDominatorTree *MDT;
154 MachinePostDominatorTree *MPDT;
155 const TargetInstrInfo *TII;
156 MachineRegisterInfo *MRI;
157
158 void initialize(MachineFunction &F);
159 bool canCoalesceBranch(CoalescingCandidateInfo &Cand);
160 bool identicalOperands(ArrayRef<MachineOperand> OperandList1,
161 ArrayRef<MachineOperand> OperandList2) const;
162 bool validateCandidates(CoalescingCandidateInfo &SourceRegion,
163 CoalescingCandidateInfo &TargetRegion) const;
164
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000165public:
166 static char ID;
167
Lei Huang34e66212017-09-12 18:39:11 +0000168 PPCBranchCoalescing() : MachineFunctionPass(ID) {
169 initializePPCBranchCoalescingPass(*PassRegistry::getPassRegistry());
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000170 }
171
172 void getAnalysisUsage(AnalysisUsage &AU) const override {
173 AU.addRequired<MachineDominatorTree>();
174 AU.addRequired<MachinePostDominatorTree>();
175 MachineFunctionPass::getAnalysisUsage(AU);
176 }
177
178 StringRef getPassName() const override { return "Branch Coalescing"; }
179
180 bool mergeCandidates(CoalescingCandidateInfo &SourceRegion,
181 CoalescingCandidateInfo &TargetRegion);
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000182 bool canMoveToBeginning(const MachineInstr &MI,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000183 const MachineBasicBlock &MBB) const;
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000184 bool canMoveToEnd(const MachineInstr &MI,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000185 const MachineBasicBlock &MBB) const;
186 bool canMerge(CoalescingCandidateInfo &SourceRegion,
187 CoalescingCandidateInfo &TargetRegion) const;
188 void moveAndUpdatePHIs(MachineBasicBlock *SourceRegionMBB,
189 MachineBasicBlock *TargetRegionMBB);
190 bool runOnMachineFunction(MachineFunction &MF) override;
191};
192} // End anonymous namespace.
193
Lei Huang34e66212017-09-12 18:39:11 +0000194char PPCBranchCoalescing::ID = 0;
195/// createPPCBranchCoalescingPass - returns an instance of the Branch Coalescing
196/// Pass
197FunctionPass *llvm::createPPCBranchCoalescingPass() {
198 return new PPCBranchCoalescing();
199}
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000200
Lei Huang34e66212017-09-12 18:39:11 +0000201INITIALIZE_PASS_BEGIN(PPCBranchCoalescing, DEBUG_TYPE,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000202 "Branch Coalescing", false, false)
203INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
204INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
Lei Huang34e66212017-09-12 18:39:11 +0000205INITIALIZE_PASS_END(PPCBranchCoalescing, DEBUG_TYPE, "Branch Coalescing",
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000206 false, false)
207
Lei Huang34e66212017-09-12 18:39:11 +0000208PPCBranchCoalescing::CoalescingCandidateInfo::CoalescingCandidateInfo()
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000209 : BranchBlock(nullptr), BranchTargetBlock(nullptr),
210 FallThroughBlock(nullptr), MustMoveDown(false), MustMoveUp(false) {}
211
Lei Huang34e66212017-09-12 18:39:11 +0000212void PPCBranchCoalescing::CoalescingCandidateInfo::clear() {
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000213 BranchBlock = nullptr;
214 BranchTargetBlock = nullptr;
215 FallThroughBlock = nullptr;
216 Cond.clear();
217 MustMoveDown = false;
218 MustMoveUp = false;
219}
220
Lei Huang34e66212017-09-12 18:39:11 +0000221void PPCBranchCoalescing::initialize(MachineFunction &MF) {
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000222 MDT = &getAnalysis<MachineDominatorTree>();
223 MPDT = &getAnalysis<MachinePostDominatorTree>();
224 TII = MF.getSubtarget().getInstrInfo();
225 MRI = &MF.getRegInfo();
226}
227
228///
229/// Analyze the branch statement to determine if it can be coalesced. This
230/// method analyses the branch statement for the given candidate to determine
231/// if it can be coalesced. If the branch can be coalesced, then the
232/// BranchTargetBlock and the FallThroughBlock are recorded in the specified
233/// Candidate.
234///
235///\param[in,out] Cand The coalescing candidate to analyze
236///\return true if and only if the branch can be coalesced, false otherwise
237///
Lei Huang34e66212017-09-12 18:39:11 +0000238bool PPCBranchCoalescing::canCoalesceBranch(CoalescingCandidateInfo &Cand) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000239 LLVM_DEBUG(dbgs() << "Determine if branch block "
240 << Cand.BranchBlock->getNumber() << " can be coalesced:");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000241 MachineBasicBlock *FalseMBB = nullptr;
242
243 if (TII->analyzeBranch(*Cand.BranchBlock, Cand.BranchTargetBlock, FalseMBB,
244 Cand.Cond)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000245 LLVM_DEBUG(dbgs() << "TII unable to Analyze Branch - skip\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000246 return false;
247 }
248
249 for (auto &I : Cand.BranchBlock->terminators()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000250 LLVM_DEBUG(dbgs() << "Looking at terminator : " << I << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000251 if (!I.isBranch())
252 continue;
253
Lei Huang34e66212017-09-12 18:39:11 +0000254 // The analyzeBranch method does not include any implicit operands.
255 // This is not an issue on PPC but must be handled on other targets.
256 // For this pass to be made target-independent, the analyzeBranch API
257 // need to be updated to support implicit operands and there would
258 // need to be a way to verify that any implicit operands would not be
259 // clobbered by merging blocks. This would include identifying the
260 // implicit operands as well as the basic block they are defined in.
261 // This could be done by changing the analyzeBranch API to have it also
262 // record and return the implicit operands and the blocks where they are
263 // defined. Alternatively, the BranchCoalescing code would need to be
264 // extended to identify the implicit operands. The analysis in canMerge
265 // must then be extended to prove that none of the implicit operands are
266 // changed in the blocks that are combined during coalescing.
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000267 if (I.getNumOperands() != I.getNumExplicitOperands()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000268 LLVM_DEBUG(dbgs() << "Terminator contains implicit operands - skip : "
269 << I << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000270 return false;
271 }
272 }
273
274 if (Cand.BranchBlock->isEHPad() || Cand.BranchBlock->hasEHPadSuccessor()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000275 LLVM_DEBUG(dbgs() << "EH Pad - skip\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000276 return false;
277 }
278
279 // For now only consider triangles (i.e, BranchTargetBlock is set,
280 // FalseMBB is null, and BranchTargetBlock is a successor to BranchBlock)
Simon Pilgrim83c37c42017-03-10 22:44:47 +0000281 if (!Cand.BranchTargetBlock || FalseMBB ||
282 !Cand.BranchBlock->isSuccessor(Cand.BranchTargetBlock)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000283 LLVM_DEBUG(dbgs() << "Does not form a triangle - skip\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000284 return false;
285 }
286
287 // Ensure there are only two successors
288 if (Cand.BranchBlock->succ_size() != 2) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000289 LLVM_DEBUG(dbgs() << "Does not have 2 successors - skip\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000290 return false;
291 }
292
293 // Sanity check - the block must be able to fall through
294 assert(Cand.BranchBlock->canFallThrough() &&
295 "Expecting the block to fall through!");
296
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000297 // We have already ensured there are exactly two successors to
298 // BranchBlock and that BranchTargetBlock is a successor to BranchBlock.
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000299 // Ensure the single fall though block is empty.
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000300 MachineBasicBlock *Succ =
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000301 (*Cand.BranchBlock->succ_begin() == Cand.BranchTargetBlock)
302 ? *Cand.BranchBlock->succ_rbegin()
303 : *Cand.BranchBlock->succ_begin();
304
305 assert(Succ && "Expecting a valid fall-through block\n");
306
307 if (!Succ->empty()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000308 LLVM_DEBUG(dbgs() << "Fall-through block contains code -- skip\n");
309 return false;
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000310 }
311
312 if (!Succ->isSuccessor(Cand.BranchTargetBlock)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000313 LLVM_DEBUG(
314 dbgs()
315 << "Successor of fall through block is not branch taken block\n");
316 return false;
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000317 }
318
319 Cand.FallThroughBlock = Succ;
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000320 LLVM_DEBUG(dbgs() << "Valid Candidate\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000321 return true;
322}
323
324///
325/// Determine if the two operand lists are identical
326///
327/// \param[in] OpList1 operand list
328/// \param[in] OpList2 operand list
329/// \return true if and only if the operands lists are identical
330///
Lei Huang34e66212017-09-12 18:39:11 +0000331bool PPCBranchCoalescing::identicalOperands(
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000332 ArrayRef<MachineOperand> OpList1, ArrayRef<MachineOperand> OpList2) const {
333
334 if (OpList1.size() != OpList2.size()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000335 LLVM_DEBUG(dbgs() << "Operand list is different size\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000336 return false;
337 }
338
339 for (unsigned i = 0; i < OpList1.size(); ++i) {
340 const MachineOperand &Op1 = OpList1[i];
341 const MachineOperand &Op2 = OpList2[i];
342
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000343 LLVM_DEBUG(dbgs() << "Op1: " << Op1 << "\n"
344 << "Op2: " << Op2 << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000345
346 if (Op1.isIdenticalTo(Op2)) {
Lei Huang34e66212017-09-12 18:39:11 +0000347 // filter out instructions with physical-register uses
348 if (Op1.isReg() && TargetRegisterInfo::isPhysicalRegister(Op1.getReg())
349 // If the physical register is constant then we can assume the value
350 // has not changed between uses.
351 && !(Op1.isUse() && MRI->isConstantPhysReg(Op1.getReg()))) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000352 LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n");
Lei Huang34e66212017-09-12 18:39:11 +0000353 return false;
354 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000355 LLVM_DEBUG(dbgs() << "Op1 and Op2 are identical!\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000356 continue;
357 }
358
359 // If the operands are not identical, but are registers, check to see if the
360 // definition of the register produces the same value. If they produce the
361 // same value, consider them to be identical.
362 if (Op1.isReg() && Op2.isReg() &&
363 TargetRegisterInfo::isVirtualRegister(Op1.getReg()) &&
364 TargetRegisterInfo::isVirtualRegister(Op2.getReg())) {
365 MachineInstr *Op1Def = MRI->getVRegDef(Op1.getReg());
366 MachineInstr *Op2Def = MRI->getVRegDef(Op2.getReg());
367 if (TII->produceSameValue(*Op1Def, *Op2Def, MRI)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000368 LLVM_DEBUG(dbgs() << "Op1Def: " << *Op1Def << " and " << *Op2Def
369 << " produce the same value!\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000370 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000371 LLVM_DEBUG(dbgs() << "Operands produce different values\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000372 return false;
373 }
374 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000375 LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000376 return false;
377 }
378 }
Lei Huang34e66212017-09-12 18:39:11 +0000379
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000380 return true;
381}
382
383///
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000384/// Moves ALL PHI instructions in SourceMBB to beginning of TargetMBB
385/// and update them to refer to the new block. PHI node ordering
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000386/// cannot be assumed so it does not matter where the PHI instructions
387/// are moved to in TargetMBB.
388///
389/// \param[in] SourceMBB block to move PHI instructions from
390/// \param[in] TargetMBB block to move PHI instructions to
391///
Lei Huang34e66212017-09-12 18:39:11 +0000392void PPCBranchCoalescing::moveAndUpdatePHIs(MachineBasicBlock *SourceMBB,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000393 MachineBasicBlock *TargetMBB) {
394
395 MachineBasicBlock::iterator MI = SourceMBB->begin();
396 MachineBasicBlock::iterator ME = SourceMBB->getFirstNonPHI();
397
398 if (MI == ME) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000399 LLVM_DEBUG(dbgs() << "SourceMBB contains no PHI instructions.\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000400 return;
401 }
402
403 // Update all PHI instructions in SourceMBB and move to top of TargetMBB
404 for (MachineBasicBlock::iterator Iter = MI; Iter != ME; Iter++) {
405 MachineInstr &PHIInst = *Iter;
406 for (unsigned i = 2, e = PHIInst.getNumOperands() + 1; i != e; i += 2) {
407 MachineOperand &MO = PHIInst.getOperand(i);
408 if (MO.getMBB() == SourceMBB)
409 MO.setMBB(TargetMBB);
410 }
411 }
412 TargetMBB->splice(TargetMBB->begin(), SourceMBB, MI, ME);
413}
414
415///
416/// This function checks if MI can be moved to the beginning of the TargetMBB
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000417/// following PHI instructions. A MI instruction can be moved to beginning of
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000418/// the TargetMBB if there are no uses of it within the TargetMBB PHI nodes.
419///
420/// \param[in] MI the machine instruction to move.
Simon Pilgrim7b227fe2017-03-02 18:59:07 +0000421/// \param[in] TargetMBB the machine basic block to move to
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000422/// \return true if it is safe to move MI to beginning of TargetMBB,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000423/// false otherwise.
424///
Lei Huang34e66212017-09-12 18:39:11 +0000425bool PPCBranchCoalescing::canMoveToBeginning(const MachineInstr &MI,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000426 const MachineBasicBlock &TargetMBB
427 ) const {
428
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000429 LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to beginning of "
430 << TargetMBB.getNumber() << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000431
432 for (auto &Def : MI.defs()) { // Looking at Def
433 for (auto &Use : MRI->use_instructions(Def.getReg())) {
434 if (Use.isPHI() && Use.getParent() == &TargetMBB) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000435 LLVM_DEBUG(dbgs() << " *** used in a PHI -- cannot move ***\n");
436 return false;
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000437 }
438 }
439 }
440
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000441 LLVM_DEBUG(dbgs() << " Safe to move to the beginning.\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000442 return true;
443}
444
445///
446/// This function checks if MI can be moved to the end of the TargetMBB,
447/// immediately before the first terminator. A MI instruction can be moved
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000448/// to then end of the TargetMBB if no PHI node defines what MI uses within
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000449/// it's own MBB.
450///
451/// \param[in] MI the machine instruction to move.
Simon Pilgrim7b227fe2017-03-02 18:59:07 +0000452/// \param[in] TargetMBB the machine basic block to move to
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000453/// \return true if it is safe to move MI to end of TargetMBB,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000454/// false otherwise.
455///
Lei Huang34e66212017-09-12 18:39:11 +0000456bool PPCBranchCoalescing::canMoveToEnd(const MachineInstr &MI,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000457 const MachineBasicBlock &TargetMBB
458 ) const {
459
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000460 LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to end of "
461 << TargetMBB.getNumber() << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000462
463 for (auto &Use : MI.uses()) {
464 if (Use.isReg() && TargetRegisterInfo::isVirtualRegister(Use.getReg())) {
465 MachineInstr *DefInst = MRI->getVRegDef(Use.getReg());
466 if (DefInst->isPHI() && DefInst->getParent() == MI.getParent()) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000467 LLVM_DEBUG(dbgs() << " *** Cannot move this instruction ***\n");
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000468 return false;
469 } else {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000470 LLVM_DEBUG(
471 dbgs() << " *** def is in another block -- safe to move!\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000472 }
473 }
474 }
475
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000476 LLVM_DEBUG(dbgs() << " Safe to move to the end.\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000477 return true;
478}
479
480///
481/// This method checks to ensure the two coalescing candidates follows the
482/// expected pattern required for coalescing.
483///
484/// \param[in] SourceRegion The candidate to move statements from
485/// \param[in] TargetRegion The candidate to move statements to
486/// \return true if all instructions in SourceRegion.BranchBlock can be merged
487/// into a block in TargetRegion; false otherwise.
488///
Lei Huang34e66212017-09-12 18:39:11 +0000489bool PPCBranchCoalescing::validateCandidates(
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000490 CoalescingCandidateInfo &SourceRegion,
491 CoalescingCandidateInfo &TargetRegion) const {
492
493 if (TargetRegion.BranchTargetBlock != SourceRegion.BranchBlock)
494 llvm_unreachable("Expecting SourceRegion to immediately follow TargetRegion");
495 else if (!MDT->dominates(TargetRegion.BranchBlock, SourceRegion.BranchBlock))
496 llvm_unreachable("Expecting TargetRegion to dominate SourceRegion");
497 else if (!MPDT->dominates(SourceRegion.BranchBlock, TargetRegion.BranchBlock))
498 llvm_unreachable("Expecting SourceRegion to post-dominate TargetRegion");
499 else if (!TargetRegion.FallThroughBlock->empty() ||
500 !SourceRegion.FallThroughBlock->empty())
501 llvm_unreachable("Expecting fall-through blocks to be empty");
502
503 return true;
504}
505
506///
507/// This method determines whether the two coalescing candidates can be merged.
508/// In order to be merged, all instructions must be able to
509/// 1. Move to the beginning of the SourceRegion.BranchTargetBlock;
510/// 2. Move to the end of the TargetRegion.BranchBlock.
511/// Merging involves moving the instructions in the
512/// TargetRegion.BranchTargetBlock (also SourceRegion.BranchBlock).
513///
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000514/// This function first try to move instructions from the
515/// TargetRegion.BranchTargetBlock down, to the beginning of the
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000516/// SourceRegion.BranchTargetBlock. This is not possible if any register defined
517/// in TargetRegion.BranchTargetBlock is used in a PHI node in the
518/// SourceRegion.BranchTargetBlock. In this case, check whether the statement
519/// can be moved up, to the end of the TargetRegion.BranchBlock (immediately
520/// before the branch statement). If it cannot move, then these blocks cannot
521/// be merged.
522///
523/// Note that there is no analysis for moving instructions past the fall-through
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000524/// blocks because they are confirmed to be empty. An assert is thrown if they
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000525/// are not.
526///
527/// \param[in] SourceRegion The candidate to move statements from
528/// \param[in] TargetRegion The candidate to move statements to
529/// \return true if all instructions in SourceRegion.BranchBlock can be merged
530/// into a block in TargetRegion, false otherwise.
531///
Lei Huang34e66212017-09-12 18:39:11 +0000532bool PPCBranchCoalescing::canMerge(CoalescingCandidateInfo &SourceRegion,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000533 CoalescingCandidateInfo &TargetRegion) const {
534 if (!validateCandidates(SourceRegion, TargetRegion))
535 return false;
536
537 // Walk through PHI nodes first and see if they force the merge into the
538 // SourceRegion.BranchTargetBlock.
539 for (MachineBasicBlock::iterator
540 I = SourceRegion.BranchBlock->instr_begin(),
541 E = SourceRegion.BranchBlock->getFirstNonPHI();
542 I != E; ++I) {
543 for (auto &Def : I->defs())
544 for (auto &Use : MRI->use_instructions(Def.getReg())) {
545 if (Use.isPHI() && Use.getParent() == SourceRegion.BranchTargetBlock) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000546 LLVM_DEBUG(dbgs()
547 << "PHI " << *I
548 << " defines register used in another "
549 "PHI within branch target block -- can't merge\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000550 NumPHINotMoved++;
551 return false;
552 }
553 if (Use.getParent() == SourceRegion.BranchBlock) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000554 LLVM_DEBUG(dbgs() << "PHI " << *I
555 << " defines register used in this "
556 "block -- all must move down\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000557 SourceRegion.MustMoveDown = true;
558 }
559 }
560 }
561
562 // Walk through the MI to see if they should be merged into
563 // TargetRegion.BranchBlock (up) or SourceRegion.BranchTargetBlock (down)
564 for (MachineBasicBlock::iterator
565 I = SourceRegion.BranchBlock->getFirstNonPHI(),
566 E = SourceRegion.BranchBlock->end();
567 I != E; ++I) {
568 if (!canMoveToBeginning(*I, *SourceRegion.BranchTargetBlock)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000569 LLVM_DEBUG(dbgs() << "Instruction " << *I
570 << " cannot move down - must move up!\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000571 SourceRegion.MustMoveUp = true;
572 }
573 if (!canMoveToEnd(*I, *TargetRegion.BranchBlock)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000574 LLVM_DEBUG(dbgs() << "Instruction " << *I
575 << " cannot move up - must move down!\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000576 SourceRegion.MustMoveDown = true;
577 }
578 }
579
580 return (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) ? false : true;
581}
582
583/// Merge the instructions from SourceRegion.BranchBlock,
584/// SourceRegion.BranchTargetBlock, and SourceRegion.FallThroughBlock into
585/// TargetRegion.BranchBlock, TargetRegion.BranchTargetBlock and
586/// TargetRegion.FallThroughBlock respectively.
587///
588/// The successors for blocks in TargetRegion will be updated to use the
589/// successors from blocks in SourceRegion. Finally, the blocks in SourceRegion
590/// will be removed from the function.
591///
592/// A region consists of a BranchBlock, a FallThroughBlock, and a
593/// BranchTargetBlock. Branch coalesce works on patterns where the
594/// TargetRegion's BranchTargetBlock must also be the SourceRegions's
595/// BranchBlock.
596///
597/// Before mergeCandidates:
598///
599/// +---------------------------+
600/// | TargetRegion.BranchBlock |
601/// +---------------------------+
602/// / |
603/// / +--------------------------------+
604/// | | TargetRegion.FallThroughBlock |
605/// \ +--------------------------------+
606/// \ |
607/// +----------------------------------+
608/// | TargetRegion.BranchTargetBlock |
609/// | SourceRegion.BranchBlock |
610/// +----------------------------------+
611/// / |
612/// / +--------------------------------+
613/// | | SourceRegion.FallThroughBlock |
614/// \ +--------------------------------+
615/// \ |
616/// +----------------------------------+
617/// | SourceRegion.BranchTargetBlock |
618/// +----------------------------------+
619///
620/// After mergeCandidates:
621///
622/// +-----------------------------+
623/// | TargetRegion.BranchBlock |
624/// | SourceRegion.BranchBlock |
625/// +-----------------------------+
626/// / |
627/// / +---------------------------------+
628/// | | TargetRegion.FallThroughBlock |
629/// | | SourceRegion.FallThroughBlock |
630/// \ +---------------------------------+
631/// \ |
632/// +----------------------------------+
633/// | SourceRegion.BranchTargetBlock |
634/// +----------------------------------+
635///
636/// \param[in] SourceRegion The candidate to move blocks from
637/// \param[in] TargetRegion The candidate to move blocks to
638///
Lei Huang34e66212017-09-12 18:39:11 +0000639bool PPCBranchCoalescing::mergeCandidates(CoalescingCandidateInfo &SourceRegion,
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000640 CoalescingCandidateInfo &TargetRegion) {
641
642 if (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) {
643 llvm_unreachable("Cannot have both MustMoveDown and MustMoveUp set!");
644 return false;
645 }
646
647 if (!validateCandidates(SourceRegion, TargetRegion))
648 return false;
649
650 // Start the merging process by first handling the BranchBlock.
651 // Move any PHIs in SourceRegion.BranchBlock down to the branch-taken block
652 moveAndUpdatePHIs(SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);
653
654 // Move remaining instructions in SourceRegion.BranchBlock into
655 // TargetRegion.BranchBlock
656 MachineBasicBlock::iterator firstInstr =
657 SourceRegion.BranchBlock->getFirstNonPHI();
658 MachineBasicBlock::iterator lastInstr =
659 SourceRegion.BranchBlock->getFirstTerminator();
660
661 MachineBasicBlock *Source = SourceRegion.MustMoveDown
662 ? SourceRegion.BranchTargetBlock
663 : TargetRegion.BranchBlock;
664
665 MachineBasicBlock::iterator Target =
666 SourceRegion.MustMoveDown
667 ? SourceRegion.BranchTargetBlock->getFirstNonPHI()
668 : TargetRegion.BranchBlock->getFirstTerminator();
669
670 Source->splice(Target, SourceRegion.BranchBlock, firstInstr, lastInstr);
671
672 // Once PHI and instructions have been moved we need to clean up the
673 // control flow.
674
675 // Remove SourceRegion.FallThroughBlock before transferring successors of
676 // SourceRegion.BranchBlock to TargetRegion.BranchBlock.
677 SourceRegion.BranchBlock->removeSuccessor(SourceRegion.FallThroughBlock);
678 TargetRegion.BranchBlock->transferSuccessorsAndUpdatePHIs(
679 SourceRegion.BranchBlock);
680 // Update branch in TargetRegion.BranchBlock to jump to
681 // SourceRegion.BranchTargetBlock
682 // In this case, TargetRegion.BranchTargetBlock == SourceRegion.BranchBlock.
683 TargetRegion.BranchBlock->ReplaceUsesOfBlockWith(
684 SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);
685 // Remove the branch statement(s) in SourceRegion.BranchBlock
686 MachineBasicBlock::iterator I =
687 SourceRegion.BranchBlock->terminators().begin();
688 while (I != SourceRegion.BranchBlock->terminators().end()) {
689 MachineInstr &CurrInst = *I;
690 ++I;
691 if (CurrInst.isBranch())
692 CurrInst.eraseFromParent();
693 }
694
695 // Fall-through block should be empty since this is part of the condition
696 // to coalesce the branches.
697 assert(TargetRegion.FallThroughBlock->empty() &&
698 "FallThroughBlocks should be empty!");
699
Simon Pilgrim455e2f32017-03-10 22:53:19 +0000700 // Transfer successor information and move PHIs down to the
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000701 // branch-taken block.
702 TargetRegion.FallThroughBlock->transferSuccessorsAndUpdatePHIs(
703 SourceRegion.FallThroughBlock);
704 TargetRegion.FallThroughBlock->removeSuccessor(SourceRegion.BranchBlock);
705
706 // Remove the blocks from the function.
707 assert(SourceRegion.BranchBlock->empty() &&
708 "Expecting branch block to be empty!");
709 SourceRegion.BranchBlock->eraseFromParent();
710
711 assert(SourceRegion.FallThroughBlock->empty() &&
712 "Expecting fall-through block to be empty!\n");
713 SourceRegion.FallThroughBlock->eraseFromParent();
714
715 NumBlocksCoalesced++;
716 return true;
717}
718
Lei Huang34e66212017-09-12 18:39:11 +0000719bool PPCBranchCoalescing::runOnMachineFunction(MachineFunction &MF) {
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000720
Matthias Braunf1caa282017-12-15 22:22:58 +0000721 if (skipFunction(MF.getFunction()) || MF.empty())
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000722 return false;
723
724 bool didSomething = false;
725
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000726 LLVM_DEBUG(dbgs() << "******** Branch Coalescing ********\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000727 initialize(MF);
728
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000729 LLVM_DEBUG(dbgs() << "Function: "; MF.dump(); dbgs() << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000730
731 CoalescingCandidateInfo Cand1, Cand2;
732 // Walk over blocks and find candidates to merge
733 // Continue trying to merge with the first candidate found, as long as merging
734 // is successfull.
735 for (MachineBasicBlock &MBB : MF) {
736 bool MergedCandidates = false;
737 do {
738 MergedCandidates = false;
739 Cand1.clear();
740 Cand2.clear();
741
742 Cand1.BranchBlock = &MBB;
743
744 // If unable to coalesce the branch, then continue to next block
745 if (!canCoalesceBranch(Cand1))
746 break;
747
748 Cand2.BranchBlock = Cand1.BranchTargetBlock;
749 if (!canCoalesceBranch(Cand2))
750 break;
751
752 // Sanity check
753 // The branch-taken block of the second candidate should post-dominate the
754 // first candidate
755 assert(MPDT->dominates(Cand2.BranchTargetBlock, Cand1.BranchBlock) &&
756 "Branch-taken block should post-dominate first candidate");
757
758 if (!identicalOperands(Cand1.Cond, Cand2.Cond)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000759 LLVM_DEBUG(dbgs() << "Blocks " << Cand1.BranchBlock->getNumber()
760 << " and " << Cand2.BranchBlock->getNumber()
761 << " have different branches\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000762 break;
763 }
764 if (!canMerge(Cand2, Cand1)) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000765 LLVM_DEBUG(dbgs() << "Cannot merge blocks "
766 << Cand1.BranchBlock->getNumber() << " and "
767 << Cand2.BranchBlock->getNumber() << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000768 NumBlocksNotCoalesced++;
769 continue;
770 }
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000771 LLVM_DEBUG(dbgs() << "Merging blocks " << Cand1.BranchBlock->getNumber()
772 << " and " << Cand1.BranchTargetBlock->getNumber()
773 << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000774 MergedCandidates = mergeCandidates(Cand2, Cand1);
775 if (MergedCandidates)
776 didSomething = true;
777
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000778 LLVM_DEBUG(dbgs() << "Function after merging: "; MF.dump();
779 dbgs() << "\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000780 } while (MergedCandidates);
781 }
782
783#ifndef NDEBUG
784 // Verify MF is still valid after branch coalescing
785 if (didSomething)
786 MF.verify(nullptr, "Error in code produced by branch coalescing");
787#endif // NDEBUG
788
Nicola Zaghend34e60c2018-05-14 12:53:11 +0000789 LLVM_DEBUG(dbgs() << "Finished Branch Coalescing\n");
Nemanja Ivanovicb223cfa2017-03-01 20:29:34 +0000790 return didSomething;
791}