blob: 7f28828a5d20adf606408958f5e189aac6f3617e [file] [log] [blame]
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +00001//===-- EarlyIfConversion.cpp - If-conversion on SSA form machine code ----===//
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// Early if-conversion is for out-of-order CPUs that don't have a lot of
11// predicable instructions. The goal is to eliminate conditional branches that
12// may mispredict.
13//
14// Instructions from both sides of the branch are executed specutatively, and a
15// cmov instruction selects the result.
16//
17//===----------------------------------------------------------------------===//
18
19#define DEBUG_TYPE "early-ifcvt"
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +000020#include "MachineTraceMetrics.h"
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000021#include "llvm/ADT/BitVector.h"
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +000022#include "llvm/ADT/PostOrderIterator.h"
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000023#include "llvm/ADT/SetVector.h"
24#include "llvm/ADT/SmallPtrSet.h"
25#include "llvm/ADT/SparseSet.h"
Jakob Stoklund Olesen786556c2012-08-13 21:03:27 +000026#include "llvm/ADT/Statistic.h"
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000027#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +000028#include "llvm/CodeGen/MachineDominators.h"
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000029#include "llvm/CodeGen/MachineFunction.h"
30#include "llvm/CodeGen/MachineFunctionPass.h"
Jakob Stoklund Olesen47730a72012-07-10 22:39:56 +000031#include "llvm/CodeGen/MachineLoopInfo.h"
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000032#include "llvm/CodeGen/MachineRegisterInfo.h"
33#include "llvm/CodeGen/Passes.h"
34#include "llvm/Target/TargetInstrInfo.h"
35#include "llvm/Target/TargetRegisterInfo.h"
Jakob Stoklund Olesene6521b52012-10-04 17:30:43 +000036#include "llvm/Target/TargetSubtargetInfo.h"
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000037#include "llvm/Support/CommandLine.h"
38#include "llvm/Support/Debug.h"
39#include "llvm/Support/raw_ostream.h"
40
41using namespace llvm;
42
43// Absolute maximum number of instructions allowed per speculated block.
44// This bypasses all other heuristics, so it should be set fairly high.
45static cl::opt<unsigned>
46BlockInstrLimit("early-ifcvt-limit", cl::init(30), cl::Hidden,
47 cl::desc("Maximum number of instructions per speculated block."));
48
49// Stress testing mode - disable heuristics.
50static cl::opt<bool> Stress("stress-early-ifcvt", cl::Hidden,
51 cl::desc("Turn all knobs to 11"));
52
Jakob Stoklund Olesen786556c2012-08-13 21:03:27 +000053STATISTIC(NumDiamondsSeen, "Number of diamonds");
54STATISTIC(NumDiamondsConv, "Number of diamonds converted");
55STATISTIC(NumTrianglesSeen, "Number of triangles");
56STATISTIC(NumTrianglesConv, "Number of triangles converted");
57
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000058//===----------------------------------------------------------------------===//
59// SSAIfConv
60//===----------------------------------------------------------------------===//
61//
62// The SSAIfConv class performs if-conversion on SSA form machine code after
Matt Beaumont-Gay00f43072012-07-04 01:09:45 +000063// determining if it is possible. The class contains no heuristics; external
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000064// code should be used to determine when if-conversion is a good idea.
65//
Matt Beaumont-Gay00f43072012-07-04 01:09:45 +000066// SSAIfConv can convert both triangles and diamonds:
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000067//
68// Triangle: Head Diamond: Head
Matt Beaumont-Gay00f43072012-07-04 01:09:45 +000069// | \ / \_
70// | \ / |
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000071// | [TF]BB FBB TBB
72// | / \ /
73// | / \ /
74// Tail Tail
75//
76// Instructions in the conditional blocks TBB and/or FBB are spliced into the
Matt Beaumont-Gay00f43072012-07-04 01:09:45 +000077// Head block, and phis in the Tail block are converted to select instructions.
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000078//
79namespace {
80class SSAIfConv {
81 const TargetInstrInfo *TII;
82 const TargetRegisterInfo *TRI;
83 MachineRegisterInfo *MRI;
84
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +000085public:
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +000086 /// The block containing the conditional branch.
87 MachineBasicBlock *Head;
88
89 /// The block containing phis after the if-then-else.
90 MachineBasicBlock *Tail;
91
92 /// The 'true' conditional block as determined by AnalyzeBranch.
93 MachineBasicBlock *TBB;
94
95 /// The 'false' conditional block as determined by AnalyzeBranch.
96 MachineBasicBlock *FBB;
97
98 /// isTriangle - When there is no 'else' block, either TBB or FBB will be
99 /// equal to Tail.
100 bool isTriangle() const { return TBB == Tail || FBB == Tail; }
101
Jakob Stoklund Olesen870da6d2012-08-10 20:19:17 +0000102 /// Returns the Tail predecessor for the True side.
103 MachineBasicBlock *getTPred() const { return TBB == Tail ? Head : TBB; }
104
105 /// Returns the Tail predecessor for the False side.
106 MachineBasicBlock *getFPred() const { return FBB == Tail ? Head : FBB; }
107
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000108 /// Information about each phi in the Tail block.
109 struct PHIInfo {
110 MachineInstr *PHI;
111 unsigned TReg, FReg;
112 // Latencies from Cond+Branch, TReg, and FReg to DstReg.
113 int CondCycles, TCycles, FCycles;
114
115 PHIInfo(MachineInstr *phi)
116 : PHI(phi), TReg(0), FReg(0), CondCycles(0), TCycles(0), FCycles(0) {}
117 };
118
119 SmallVector<PHIInfo, 8> PHIs;
120
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000121private:
122 /// The branch condition determined by AnalyzeBranch.
123 SmallVector<MachineOperand, 4> Cond;
124
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000125 /// Instructions in Head that define values used by the conditional blocks.
126 /// The hoisted instructions must be inserted after these instructions.
127 SmallPtrSet<MachineInstr*, 8> InsertAfter;
128
129 /// Register units clobbered by the conditional blocks.
130 BitVector ClobberedRegUnits;
131
132 // Scratch pad for findInsertionPoint.
133 SparseSet<unsigned> LiveRegUnits;
134
135 /// Insertion point in Head for speculatively executed instructions form TBB
136 /// and FBB.
137 MachineBasicBlock::iterator InsertionPoint;
138
139 /// Return true if all non-terminator instructions in MBB can be safely
140 /// speculated.
141 bool canSpeculateInstrs(MachineBasicBlock *MBB);
142
143 /// Find a valid insertion point in Head.
144 bool findInsertionPoint();
145
Jakob Stoklund Olesenbc70ff32012-08-13 20:49:04 +0000146 /// Replace PHI instructions in Tail with selects.
147 void replacePHIInstrs();
148
149 /// Insert selects and rewrite PHI operands to use them.
150 void rewritePHIOperands();
151
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000152public:
153 /// runOnMachineFunction - Initialize per-function data structures.
154 void runOnMachineFunction(MachineFunction &MF) {
155 TII = MF.getTarget().getInstrInfo();
156 TRI = MF.getTarget().getRegisterInfo();
157 MRI = &MF.getRegInfo();
158 LiveRegUnits.clear();
159 LiveRegUnits.setUniverse(TRI->getNumRegUnits());
160 ClobberedRegUnits.clear();
161 ClobberedRegUnits.resize(TRI->getNumRegUnits());
162 }
163
164 /// canConvertIf - If the sub-CFG headed by MBB can be if-converted,
165 /// initialize the internal state, and return true.
166 bool canConvertIf(MachineBasicBlock *MBB);
167
168 /// convertIf - If-convert the last block passed to canConvertIf(), assuming
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000169 /// it is possible. Add any erased blocks to RemovedBlocks.
170 void convertIf(SmallVectorImpl<MachineBasicBlock*> &RemovedBlocks);
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000171};
172} // end anonymous namespace
173
174
175/// canSpeculateInstrs - Returns true if all the instructions in MBB can safely
176/// be speculated. The terminators are not considered.
177///
178/// If instructions use any values that are defined in the head basic block,
179/// the defining instructions are added to InsertAfter.
180///
181/// Any clobbered regunits are added to ClobberedRegUnits.
182///
183bool SSAIfConv::canSpeculateInstrs(MachineBasicBlock *MBB) {
184 // Reject any live-in physregs. It's probably CPSR/EFLAGS, and very hard to
185 // get right.
186 if (!MBB->livein_empty()) {
187 DEBUG(dbgs() << "BB#" << MBB->getNumber() << " has live-ins.\n");
188 return false;
189 }
190
191 unsigned InstrCount = 0;
Jakob Stoklund Olesen86fc3102012-07-06 02:31:22 +0000192
193 // Check all instructions, except the terminators. It is assumed that
194 // terminators never have side effects or define any used register values.
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000195 for (MachineBasicBlock::iterator I = MBB->begin(),
196 E = MBB->getFirstTerminator(); I != E; ++I) {
197 if (I->isDebugValue())
198 continue;
199
200 if (++InstrCount > BlockInstrLimit && !Stress) {
201 DEBUG(dbgs() << "BB#" << MBB->getNumber() << " has more than "
202 << BlockInstrLimit << " instructions.\n");
203 return false;
204 }
205
206 // There shouldn't normally be any phis in a single-predecessor block.
207 if (I->isPHI()) {
208 DEBUG(dbgs() << "Can't hoist: " << *I);
209 return false;
210 }
211
212 // Don't speculate loads. Note that it may be possible and desirable to
213 // speculate GOT or constant pool loads that are guaranteed not to trap,
214 // but we don't support that for now.
215 if (I->mayLoad()) {
216 DEBUG(dbgs() << "Won't speculate load: " << *I);
217 return false;
218 }
219
220 // We never speculate stores, so an AA pointer isn't necessary.
221 bool DontMoveAcrossStore = true;
222 if (!I->isSafeToMove(TII, 0, DontMoveAcrossStore)) {
223 DEBUG(dbgs() << "Can't speculate: " << *I);
224 return false;
225 }
226
227 // Check for any dependencies on Head instructions.
228 for (MIOperands MO(I); MO.isValid(); ++MO) {
229 if (MO->isRegMask()) {
230 DEBUG(dbgs() << "Won't speculate regmask: " << *I);
231 return false;
232 }
233 if (!MO->isReg())
234 continue;
235 unsigned Reg = MO->getReg();
236
237 // Remember clobbered regunits.
238 if (MO->isDef() && TargetRegisterInfo::isPhysicalRegister(Reg))
239 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units)
240 ClobberedRegUnits.set(*Units);
241
242 if (!MO->readsReg() || !TargetRegisterInfo::isVirtualRegister(Reg))
243 continue;
244 MachineInstr *DefMI = MRI->getVRegDef(Reg);
245 if (!DefMI || DefMI->getParent() != Head)
246 continue;
247 if (InsertAfter.insert(DefMI))
248 DEBUG(dbgs() << "BB#" << MBB->getNumber() << " depends on " << *DefMI);
249 if (DefMI->isTerminator()) {
250 DEBUG(dbgs() << "Can't insert instructions below terminator.\n");
251 return false;
252 }
253 }
254 }
255 return true;
256}
257
258
259/// Find an insertion point in Head for the speculated instructions. The
260/// insertion point must be:
261///
262/// 1. Before any terminators.
263/// 2. After any instructions in InsertAfter.
264/// 3. Not have any clobbered regunits live.
265///
266/// This function sets InsertionPoint and returns true when successful, it
267/// returns false if no valid insertion point could be found.
268///
269bool SSAIfConv::findInsertionPoint() {
270 // Keep track of live regunits before the current position.
271 // Only track RegUnits that are also in ClobberedRegUnits.
272 LiveRegUnits.clear();
273 SmallVector<unsigned, 8> Reads;
274 MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator();
275 MachineBasicBlock::iterator I = Head->end();
276 MachineBasicBlock::iterator B = Head->begin();
277 while (I != B) {
278 --I;
279 // Some of the conditional code depends in I.
280 if (InsertAfter.count(I)) {
281 DEBUG(dbgs() << "Can't insert code after " << *I);
282 return false;
283 }
284
285 // Update live regunits.
286 for (MIOperands MO(I); MO.isValid(); ++MO) {
287 // We're ignoring regmask operands. That is conservatively correct.
288 if (!MO->isReg())
289 continue;
290 unsigned Reg = MO->getReg();
291 if (!TargetRegisterInfo::isPhysicalRegister(Reg))
292 continue;
293 // I clobbers Reg, so it isn't live before I.
294 if (MO->isDef())
295 for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units)
296 LiveRegUnits.erase(*Units);
297 // Unless I reads Reg.
298 if (MO->readsReg())
299 Reads.push_back(Reg);
300 }
301 // Anything read by I is live before I.
302 while (!Reads.empty())
303 for (MCRegUnitIterator Units(Reads.pop_back_val(), TRI); Units.isValid();
304 ++Units)
305 if (ClobberedRegUnits.test(*Units))
306 LiveRegUnits.insert(*Units);
307
308 // We can't insert before a terminator.
309 if (I != FirstTerm && I->isTerminator())
310 continue;
311
312 // Some of the clobbered registers are live before I, not a valid insertion
313 // point.
314 if (!LiveRegUnits.empty()) {
315 DEBUG({
316 dbgs() << "Would clobber";
317 for (SparseSet<unsigned>::const_iterator
318 i = LiveRegUnits.begin(), e = LiveRegUnits.end(); i != e; ++i)
319 dbgs() << ' ' << PrintRegUnit(*i, TRI);
320 dbgs() << " live before " << *I;
321 });
322 continue;
323 }
324
325 // This is a valid insertion point.
326 InsertionPoint = I;
327 DEBUG(dbgs() << "Can insert before " << *I);
328 return true;
329 }
330 DEBUG(dbgs() << "No legal insertion point found.\n");
331 return false;
332}
333
334
335
336/// canConvertIf - analyze the sub-cfg rooted in MBB, and return true if it is
337/// a potential candidate for if-conversion. Fill out the internal state.
338///
339bool SSAIfConv::canConvertIf(MachineBasicBlock *MBB) {
340 Head = MBB;
341 TBB = FBB = Tail = 0;
342
343 if (Head->succ_size() != 2)
344 return false;
345 MachineBasicBlock *Succ0 = Head->succ_begin()[0];
346 MachineBasicBlock *Succ1 = Head->succ_begin()[1];
347
348 // Canonicalize so Succ0 has MBB as its single predecessor.
349 if (Succ0->pred_size() != 1)
350 std::swap(Succ0, Succ1);
351
352 if (Succ0->pred_size() != 1 || Succ0->succ_size() != 1)
353 return false;
354
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000355 Tail = Succ0->succ_begin()[0];
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000356
357 // This is not a triangle.
358 if (Tail != Succ1) {
359 // Check for a diamond. We won't deal with any critical edges.
360 if (Succ1->pred_size() != 1 || Succ1->succ_size() != 1 ||
361 Succ1->succ_begin()[0] != Tail)
362 return false;
363 DEBUG(dbgs() << "\nDiamond: BB#" << Head->getNumber()
364 << " -> BB#" << Succ0->getNumber()
365 << "/BB#" << Succ1->getNumber()
366 << " -> BB#" << Tail->getNumber() << '\n');
367
368 // Live-in physregs are tricky to get right when speculating code.
369 if (!Tail->livein_empty()) {
370 DEBUG(dbgs() << "Tail has live-ins.\n");
371 return false;
372 }
373 } else {
374 DEBUG(dbgs() << "\nTriangle: BB#" << Head->getNumber()
375 << " -> BB#" << Succ0->getNumber()
376 << " -> BB#" << Tail->getNumber() << '\n');
377 }
378
379 // This is a triangle or a diamond.
380 // If Tail doesn't have any phis, there must be side effects.
381 if (Tail->empty() || !Tail->front().isPHI()) {
382 DEBUG(dbgs() << "No phis in tail.\n");
383 return false;
384 }
385
386 // The branch we're looking to eliminate must be analyzable.
387 Cond.clear();
388 if (TII->AnalyzeBranch(*Head, TBB, FBB, Cond)) {
389 DEBUG(dbgs() << "Branch not analyzable.\n");
390 return false;
391 }
392
393 // This is weird, probably some sort of degenerate CFG.
394 if (!TBB) {
395 DEBUG(dbgs() << "AnalyzeBranch didn't find conditional branch.\n");
396 return false;
397 }
398
399 // AnalyzeBranch doesn't set FBB on a fall-through branch.
400 // Make sure it is always set.
401 FBB = TBB == Succ0 ? Succ1 : Succ0;
402
403 // Any phis in the tail block must be convertible to selects.
404 PHIs.clear();
Jakob Stoklund Olesen870da6d2012-08-10 20:19:17 +0000405 MachineBasicBlock *TPred = getTPred();
406 MachineBasicBlock *FPred = getFPred();
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000407 for (MachineBasicBlock::iterator I = Tail->begin(), E = Tail->end();
408 I != E && I->isPHI(); ++I) {
409 PHIs.push_back(&*I);
410 PHIInfo &PI = PHIs.back();
411 // Find PHI operands corresponding to TPred and FPred.
412 for (unsigned i = 1; i != PI.PHI->getNumOperands(); i += 2) {
413 if (PI.PHI->getOperand(i+1).getMBB() == TPred)
414 PI.TReg = PI.PHI->getOperand(i).getReg();
415 if (PI.PHI->getOperand(i+1).getMBB() == FPred)
416 PI.FReg = PI.PHI->getOperand(i).getReg();
417 }
418 assert(TargetRegisterInfo::isVirtualRegister(PI.TReg) && "Bad PHI");
419 assert(TargetRegisterInfo::isVirtualRegister(PI.FReg) && "Bad PHI");
420
421 // Get target information.
422 if (!TII->canInsertSelect(*Head, Cond, PI.TReg, PI.FReg,
423 PI.CondCycles, PI.TCycles, PI.FCycles)) {
424 DEBUG(dbgs() << "Can't convert: " << *PI.PHI);
425 return false;
426 }
427 }
428
429 // Check that the conditional instructions can be speculated.
430 InsertAfter.clear();
431 ClobberedRegUnits.reset();
432 if (TBB != Tail && !canSpeculateInstrs(TBB))
433 return false;
434 if (FBB != Tail && !canSpeculateInstrs(FBB))
435 return false;
436
437 // Try to find a valid insertion point for the speculated instructions in the
438 // head basic block.
439 if (!findInsertionPoint())
440 return false;
441
Jakob Stoklund Olesen786556c2012-08-13 21:03:27 +0000442 if (isTriangle())
443 ++NumTrianglesSeen;
444 else
445 ++NumDiamondsSeen;
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000446 return true;
447}
448
Jakob Stoklund Olesenbc70ff32012-08-13 20:49:04 +0000449/// replacePHIInstrs - Completely replace PHI instructions with selects.
450/// This is possible when the only Tail predecessors are the if-converted
451/// blocks.
452void SSAIfConv::replacePHIInstrs() {
453 assert(Tail->pred_size() == 2 && "Cannot replace PHIs");
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000454 MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator();
455 assert(FirstTerm != Head->end() && "No terminators");
456 DebugLoc HeadDL = FirstTerm->getDebugLoc();
457
458 // Convert all PHIs to select instructions inserted before FirstTerm.
459 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
460 PHIInfo &PI = PHIs[i];
461 DEBUG(dbgs() << "If-converting " << *PI.PHI);
462 assert(PI.PHI->getNumOperands() == 5 && "Unexpected PHI operands.");
463 unsigned DstReg = PI.PHI->getOperand(0).getReg();
464 TII->insertSelect(*Head, FirstTerm, HeadDL, DstReg, Cond, PI.TReg, PI.FReg);
465 DEBUG(dbgs() << " --> " << *llvm::prior(FirstTerm));
466 PI.PHI->eraseFromParent();
467 PI.PHI = 0;
468 }
Jakob Stoklund Olesenbc70ff32012-08-13 20:49:04 +0000469}
470
471/// rewritePHIOperands - When there are additional Tail predecessors, insert
472/// select instructions in Head and rewrite PHI operands to use the selects.
473/// Keep the PHI instructions in Tail to handle the other predecessors.
474void SSAIfConv::rewritePHIOperands() {
475 MachineBasicBlock::iterator FirstTerm = Head->getFirstTerminator();
476 assert(FirstTerm != Head->end() && "No terminators");
477 DebugLoc HeadDL = FirstTerm->getDebugLoc();
478
479 // Convert all PHIs to select instructions inserted before FirstTerm.
480 for (unsigned i = 0, e = PHIs.size(); i != e; ++i) {
481 PHIInfo &PI = PHIs[i];
482 DEBUG(dbgs() << "If-converting " << *PI.PHI);
483 unsigned PHIDst = PI.PHI->getOperand(0).getReg();
484 unsigned DstReg = MRI->createVirtualRegister(MRI->getRegClass(PHIDst));
485 TII->insertSelect(*Head, FirstTerm, HeadDL, DstReg, Cond, PI.TReg, PI.FReg);
486 DEBUG(dbgs() << " --> " << *llvm::prior(FirstTerm));
487
488 // Rewrite PHI operands TPred -> (DstReg, Head), remove FPred.
489 for (unsigned i = PI.PHI->getNumOperands(); i != 1; i -= 2) {
490 MachineBasicBlock *MBB = PI.PHI->getOperand(i-1).getMBB();
491 if (MBB == getTPred()) {
492 PI.PHI->getOperand(i-1).setMBB(Head);
493 PI.PHI->getOperand(i-2).setReg(DstReg);
494 } else if (MBB == getFPred()) {
495 PI.PHI->RemoveOperand(i-1);
496 PI.PHI->RemoveOperand(i-2);
497 }
498 }
499 DEBUG(dbgs() << " --> " << *PI.PHI);
500 }
501}
502
503/// convertIf - Execute the if conversion after canConvertIf has determined the
504/// feasibility.
505///
506/// Any basic blocks erased will be added to RemovedBlocks.
507///
508void SSAIfConv::convertIf(SmallVectorImpl<MachineBasicBlock*> &RemovedBlocks) {
509 assert(Head && Tail && TBB && FBB && "Call canConvertIf first.");
510
Jakob Stoklund Olesen786556c2012-08-13 21:03:27 +0000511 // Update statistics.
512 if (isTriangle())
513 ++NumTrianglesConv;
514 else
515 ++NumDiamondsConv;
516
Jakob Stoklund Olesenbc70ff32012-08-13 20:49:04 +0000517 // Move all instructions into Head, except for the terminators.
518 if (TBB != Tail)
519 Head->splice(InsertionPoint, TBB, TBB->begin(), TBB->getFirstTerminator());
520 if (FBB != Tail)
521 Head->splice(InsertionPoint, FBB, FBB->begin(), FBB->getFirstTerminator());
522
523 // Are there extra Tail predecessors?
524 bool ExtraPreds = Tail->pred_size() != 2;
525 if (ExtraPreds)
526 rewritePHIOperands();
527 else
528 replacePHIInstrs();
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000529
530 // Fix up the CFG, temporarily leave Head without any successors.
531 Head->removeSuccessor(TBB);
532 Head->removeSuccessor(FBB);
533 if (TBB != Tail)
534 TBB->removeSuccessor(Tail);
535 if (FBB != Tail)
536 FBB->removeSuccessor(Tail);
537
538 // Fix up Head's terminators.
539 // It should become a single branch or a fallthrough.
Jakob Stoklund Olesenbc70ff32012-08-13 20:49:04 +0000540 DebugLoc HeadDL = Head->getFirstTerminator()->getDebugLoc();
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000541 TII->RemoveBranch(*Head);
542
543 // Erase the now empty conditional blocks. It is likely that Head can fall
544 // through to Tail, and we can join the two blocks.
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000545 if (TBB != Tail) {
546 RemovedBlocks.push_back(TBB);
547 TBB->eraseFromParent();
548 }
549 if (FBB != Tail) {
550 RemovedBlocks.push_back(FBB);
551 FBB->eraseFromParent();
552 }
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000553
554 assert(Head->succ_empty() && "Additional head successors?");
Jakob Stoklund Olesenbc70ff32012-08-13 20:49:04 +0000555 if (!ExtraPreds && Head->isLayoutSuccessor(Tail)) {
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000556 // Splice Tail onto the end of Head.
557 DEBUG(dbgs() << "Joining tail BB#" << Tail->getNumber()
558 << " into head BB#" << Head->getNumber() << '\n');
559 Head->splice(Head->end(), Tail,
560 Tail->begin(), Tail->end());
561 Head->transferSuccessorsAndUpdatePHIs(Tail);
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000562 RemovedBlocks.push_back(Tail);
563 Tail->eraseFromParent();
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000564 } else {
565 // We need a branch to Tail, let code placement work it out later.
566 DEBUG(dbgs() << "Converting to unconditional branch.\n");
567 SmallVector<MachineOperand, 0> EmptyCond;
568 TII->InsertBranch(*Head, Tail, 0, EmptyCond, HeadDL);
569 Head->addSuccessor(Tail);
570 }
571 DEBUG(dbgs() << *Head);
572}
573
574
575//===----------------------------------------------------------------------===//
576// EarlyIfConverter Pass
577//===----------------------------------------------------------------------===//
578
579namespace {
580class EarlyIfConverter : public MachineFunctionPass {
581 const TargetInstrInfo *TII;
582 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesen0fac6aa2012-08-08 18:19:58 +0000583 const MCSchedModel *SchedModel;
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000584 MachineRegisterInfo *MRI;
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000585 MachineDominatorTree *DomTree;
Jakob Stoklund Olesen47730a72012-07-10 22:39:56 +0000586 MachineLoopInfo *Loops;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000587 MachineTraceMetrics *Traces;
588 MachineTraceMetrics::Ensemble *MinInstr;
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000589 SSAIfConv IfConv;
590
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000591public:
592 static char ID;
593 EarlyIfConverter() : MachineFunctionPass(ID) {}
594 void getAnalysisUsage(AnalysisUsage &AU) const;
595 bool runOnMachineFunction(MachineFunction &MF);
596
597private:
598 bool tryConvertIf(MachineBasicBlock*);
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000599 void updateDomTree(ArrayRef<MachineBasicBlock*> Removed);
Jakob Stoklund Olesen47730a72012-07-10 22:39:56 +0000600 void updateLoops(ArrayRef<MachineBasicBlock*> Removed);
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000601 void invalidateTraces();
602 bool shouldConvertIf();
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000603};
604} // end anonymous namespace
605
606char EarlyIfConverter::ID = 0;
607char &llvm::EarlyIfConverterID = EarlyIfConverter::ID;
608
609INITIALIZE_PASS_BEGIN(EarlyIfConverter,
610 "early-ifcvt", "Early If Converter", false, false)
611INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000612INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000613INITIALIZE_PASS_DEPENDENCY(MachineTraceMetrics)
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000614INITIALIZE_PASS_END(EarlyIfConverter,
615 "early-ifcvt", "Early If Converter", false, false)
616
617void EarlyIfConverter::getAnalysisUsage(AnalysisUsage &AU) const {
618 AU.addRequired<MachineBranchProbabilityInfo>();
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000619 AU.addRequired<MachineDominatorTree>();
620 AU.addPreserved<MachineDominatorTree>();
Jakob Stoklund Olesen47730a72012-07-10 22:39:56 +0000621 AU.addRequired<MachineLoopInfo>();
622 AU.addPreserved<MachineLoopInfo>();
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000623 AU.addRequired<MachineTraceMetrics>();
624 AU.addPreserved<MachineTraceMetrics>();
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000625 MachineFunctionPass::getAnalysisUsage(AU);
626}
627
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000628/// Update the dominator tree after if-conversion erased some blocks.
629void EarlyIfConverter::updateDomTree(ArrayRef<MachineBasicBlock*> Removed) {
630 // convertIf can remove TBB, FBB, and Tail can be merged into Head.
631 // TBB and FBB should not dominate any blocks.
632 // Tail children should be transferred to Head.
633 MachineDomTreeNode *HeadNode = DomTree->getNode(IfConv.Head);
634 for (unsigned i = 0, e = Removed.size(); i != e; ++i) {
635 MachineDomTreeNode *Node = DomTree->getNode(Removed[i]);
636 assert(Node != HeadNode && "Cannot erase the head node");
637 while (Node->getNumChildren()) {
638 assert(Node->getBlock() == IfConv.Tail && "Unexpected children");
639 DomTree->changeImmediateDominator(Node->getChildren().back(), HeadNode);
640 }
641 DomTree->eraseNode(Removed[i]);
642 }
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000643}
644
Jakob Stoklund Olesen47730a72012-07-10 22:39:56 +0000645/// Update LoopInfo after if-conversion.
646void EarlyIfConverter::updateLoops(ArrayRef<MachineBasicBlock*> Removed) {
647 if (!Loops)
648 return;
649 // If-conversion doesn't change loop structure, and it doesn't mess with back
650 // edges, so updating LoopInfo is simply removing the dead blocks.
651 for (unsigned i = 0, e = Removed.size(); i != e; ++i)
652 Loops->removeBlock(Removed[i]);
653}
654
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000655/// Invalidate MachineTraceMetrics before if-conversion.
656void EarlyIfConverter::invalidateTraces() {
Jakob Stoklund Olesenef6c76c2012-07-30 20:57:50 +0000657 Traces->verifyAnalysis();
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000658 Traces->invalidate(IfConv.Head);
659 Traces->invalidate(IfConv.Tail);
660 Traces->invalidate(IfConv.TBB);
661 Traces->invalidate(IfConv.FBB);
Jakob Stoklund Olesenef6c76c2012-07-30 20:57:50 +0000662 Traces->verifyAnalysis();
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000663}
664
Jakob Stoklund Oleseneb74c082012-08-10 22:27:31 +0000665// Adjust cycles with downward saturation.
666static unsigned adjCycles(unsigned Cyc, int Delta) {
667 if (Delta < 0 && Cyc + Delta > Cyc)
668 return 0;
669 return Cyc + Delta;
670}
671
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000672/// Apply cost model and heuristics to the if-conversion in IfConv.
673/// Return true if the conversion is a good idea.
674///
675bool EarlyIfConverter::shouldConvertIf() {
Jakob Stoklund Olesend6cf5f42012-08-08 18:24:23 +0000676 // Stress testing mode disables all cost considerations.
677 if (Stress)
678 return true;
679
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000680 if (!MinInstr)
681 MinInstr = Traces->getEnsemble(MachineTraceMetrics::TS_MinInstrCount);
Jakob Stoklund Olesen84ef6ba2012-08-07 18:02:19 +0000682
Jakob Stoklund Oleseneb74c082012-08-10 22:27:31 +0000683 MachineTraceMetrics::Trace TBBTrace = MinInstr->getTrace(IfConv.getTPred());
684 MachineTraceMetrics::Trace FBBTrace = MinInstr->getTrace(IfConv.getFPred());
Jakob Stoklund Olesen84ef6ba2012-08-07 18:02:19 +0000685 DEBUG(dbgs() << "TBB: " << TBBTrace << "FBB: " << FBBTrace);
Jakob Stoklund Oleseneb74c082012-08-10 22:27:31 +0000686 unsigned MinCrit = std::min(TBBTrace.getCriticalPath(),
687 FBBTrace.getCriticalPath());
688
689 // Set a somewhat arbitrary limit on the critical path extension we accept.
690 unsigned CritLimit = SchedModel->MispredictPenalty/2;
691
692 // If-conversion only makes sense when there is unexploited ILP. Compute the
693 // maximum-ILP resource length of the trace after if-conversion. Compare it
694 // to the shortest critical path.
695 SmallVector<const MachineBasicBlock*, 1> ExtraBlocks;
696 if (IfConv.TBB != IfConv.Tail)
697 ExtraBlocks.push_back(IfConv.TBB);
698 unsigned ResLength = FBBTrace.getResourceLength(ExtraBlocks);
699 DEBUG(dbgs() << "Resource length " << ResLength
700 << ", minimal critical path " << MinCrit << '\n');
701 if (ResLength > MinCrit + CritLimit) {
702 DEBUG(dbgs() << "Not enough available ILP.\n");
Jakob Stoklund Olesen84ef6ba2012-08-07 18:02:19 +0000703 return false;
704 }
Jakob Stoklund Oleseneb74c082012-08-10 22:27:31 +0000705
706 // Assume that the depth of the first head terminator will also be the depth
707 // of the select instruction inserted, as determined by the flag dependency.
708 // TBB / FBB data dependencies may delay the select even more.
709 MachineTraceMetrics::Trace HeadTrace = MinInstr->getTrace(IfConv.Head);
710 unsigned BranchDepth =
711 HeadTrace.getInstrCycles(IfConv.Head->getFirstTerminator()).Depth;
712 DEBUG(dbgs() << "Branch depth: " << BranchDepth << '\n');
713
714 // Look at all the tail phis, and compute the critical path extension caused
715 // by inserting select instructions.
716 MachineTraceMetrics::Trace TailTrace = MinInstr->getTrace(IfConv.Tail);
717 for (unsigned i = 0, e = IfConv.PHIs.size(); i != e; ++i) {
718 SSAIfConv::PHIInfo &PI = IfConv.PHIs[i];
719 unsigned Slack = TailTrace.getInstrSlack(PI.PHI);
720 unsigned MaxDepth = Slack + TailTrace.getInstrCycles(PI.PHI).Depth;
721 DEBUG(dbgs() << "Slack " << Slack << ":\t" << *PI.PHI);
722
723 // The condition is pulled into the critical path.
724 unsigned CondDepth = adjCycles(BranchDepth, PI.CondCycles);
725 if (CondDepth > MaxDepth) {
726 unsigned Extra = CondDepth - MaxDepth;
727 DEBUG(dbgs() << "Condition adds " << Extra << " cycles.\n");
728 if (Extra > CritLimit) {
729 DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
730 return false;
731 }
732 }
733
734 // The TBB value is pulled into the critical path.
735 unsigned TDepth = adjCycles(TBBTrace.getPHIDepth(PI.PHI), PI.TCycles);
736 if (TDepth > MaxDepth) {
737 unsigned Extra = TDepth - MaxDepth;
738 DEBUG(dbgs() << "TBB data adds " << Extra << " cycles.\n");
739 if (Extra > CritLimit) {
740 DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
741 return false;
742 }
743 }
744
745 // The FBB value is pulled into the critical path.
746 unsigned FDepth = adjCycles(FBBTrace.getPHIDepth(PI.PHI), PI.FCycles);
747 if (FDepth > MaxDepth) {
748 unsigned Extra = FDepth - MaxDepth;
749 DEBUG(dbgs() << "FBB data adds " << Extra << " cycles.\n");
750 if (Extra > CritLimit) {
751 DEBUG(dbgs() << "Exceeds limit of " << CritLimit << '\n');
752 return false;
753 }
754 }
755 }
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000756 return true;
757}
758
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000759/// Attempt repeated if-conversion on MBB, return true if successful.
760///
761bool EarlyIfConverter::tryConvertIf(MachineBasicBlock *MBB) {
762 bool Changed = false;
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000763 while (IfConv.canConvertIf(MBB) && shouldConvertIf()) {
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000764 // If-convert MBB and update analyses.
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000765 invalidateTraces();
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000766 SmallVector<MachineBasicBlock*, 4> RemovedBlocks;
767 IfConv.convertIf(RemovedBlocks);
768 Changed = true;
769 updateDomTree(RemovedBlocks);
Jakob Stoklund Olesen47730a72012-07-10 22:39:56 +0000770 updateLoops(RemovedBlocks);
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000771 }
772 return Changed;
773}
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000774
775bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
776 DEBUG(dbgs() << "********** EARLY IF-CONVERSION **********\n"
David Blaikie986d76d2012-08-22 17:18:53 +0000777 << "********** Function: " << MF.getName() << '\n');
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000778 TII = MF.getTarget().getInstrInfo();
779 TRI = MF.getTarget().getRegisterInfo();
Jakob Stoklund Olesene6521b52012-10-04 17:30:43 +0000780 SchedModel =
781 MF.getTarget().getSubtarget<TargetSubtargetInfo>().getSchedModel();
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000782 MRI = &MF.getRegInfo();
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000783 DomTree = &getAnalysis<MachineDominatorTree>();
Jakob Stoklund Olesen47730a72012-07-10 22:39:56 +0000784 Loops = getAnalysisIfAvailable<MachineLoopInfo>();
Jakob Stoklund Olesen9f63e102012-07-26 18:38:11 +0000785 Traces = &getAnalysis<MachineTraceMetrics>();
786 MinInstr = 0;
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000787
788 bool Changed = false;
789 IfConv.runOnMachineFunction(MF);
790
Jakob Stoklund Olesen1f523dc2012-07-10 22:18:23 +0000791 // Visit blocks in dominator tree post-order. The post-order enables nested
792 // if-conversion in a single pass. The tryConvertIf() function may erase
793 // blocks, but only blocks dominated by the head block. This makes it safe to
794 // update the dominator tree while the post-order iterator is still active.
795 for (po_iterator<MachineDominatorTree*>
796 I = po_begin(DomTree), E = po_end(DomTree); I != E; ++I)
797 if (tryConvertIf(I->getBlock()))
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000798 Changed = true;
799
Jakob Stoklund Olesen33242fd2012-07-04 00:09:54 +0000800 MF.verify(this, "After early if-conversion");
801 return Changed;
802}