@@ -47,76 +47,78 @@ using namespace llvm;
47
47
#define DEBUG_TYPE " phi-node-elimination"
48
48
49
49
static cl::opt<bool >
50
- DisableEdgeSplitting (" disable-phi-elim-edge-splitting" , cl::init(false ),
51
- cl::Hidden, cl::desc(" Disable critical edge splitting "
52
- " during PHI elimination" ));
50
+ DisableEdgeSplitting (" disable-phi-elim-edge-splitting" , cl::init(false ),
51
+ cl::Hidden,
52
+ cl::desc(" Disable critical edge splitting "
53
+ " during PHI elimination" ));
53
54
54
55
static cl::opt<bool >
55
- SplitAllCriticalEdges (" phi-elim-split-all-critical-edges" , cl::init(false ),
56
- cl::Hidden, cl::desc(" Split all critical edges during "
57
- " PHI elimination" ));
56
+ SplitAllCriticalEdges (" phi-elim-split-all-critical-edges" , cl::init(false ),
57
+ cl::Hidden,
58
+ cl::desc(" Split all critical edges during "
59
+ " PHI elimination" ));
58
60
59
61
static cl::opt<bool > NoPhiElimLiveOutEarlyExit (
60
62
" no-phi-elim-live-out-early-exit" , cl::init(false ), cl::Hidden,
61
63
cl::desc(" Do not use an early exit if isLiveOutPastPHIs returns true." ));
62
64
63
65
namespace {
64
66
65
- class PHIElimination : public MachineFunctionPass {
66
- MachineRegisterInfo *MRI = nullptr ; // Machine register information
67
- LiveVariables *LV = nullptr ;
68
- LiveIntervals *LIS = nullptr ;
67
+ class PHIElimination : public MachineFunctionPass {
68
+ MachineRegisterInfo *MRI = nullptr ; // Machine register information
69
+ LiveVariables *LV = nullptr ;
70
+ LiveIntervals *LIS = nullptr ;
69
71
70
- public:
71
- static char ID; // Pass identification, replacement for typeid
72
+ public:
73
+ static char ID; // Pass identification, replacement for typeid
72
74
73
- PHIElimination () : MachineFunctionPass(ID) {
74
- initializePHIEliminationPass (*PassRegistry::getPassRegistry ());
75
- }
75
+ PHIElimination () : MachineFunctionPass(ID) {
76
+ initializePHIEliminationPass (*PassRegistry::getPassRegistry ());
77
+ }
76
78
77
- bool runOnMachineFunction (MachineFunction &MF) override ;
78
- void getAnalysisUsage (AnalysisUsage &AU) const override ;
79
+ bool runOnMachineFunction (MachineFunction &MF) override ;
80
+ void getAnalysisUsage (AnalysisUsage &AU) const override ;
79
81
80
- private:
81
- // / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
82
- // / in predecessor basic blocks.
83
- bool EliminatePHINodes (MachineFunction &MF, MachineBasicBlock &MBB);
82
+ private:
83
+ // / EliminatePHINodes - Eliminate phi nodes by inserting copy instructions
84
+ // / in predecessor basic blocks.
85
+ bool EliminatePHINodes (MachineFunction &MF, MachineBasicBlock &MBB);
84
86
85
- void LowerPHINode (MachineBasicBlock &MBB,
86
- MachineBasicBlock::iterator LastPHIIt,
87
- bool AllEdgesCritical);
87
+ void LowerPHINode (MachineBasicBlock &MBB,
88
+ MachineBasicBlock::iterator LastPHIIt,
89
+ bool AllEdgesCritical);
88
90
89
- // / analyzePHINodes - Gather information about the PHI nodes in
90
- // / here. In particular, we want to map the number of uses of a virtual
91
- // / register which is used in a PHI node. We map that to the BB the
92
- // / vreg is coming from. This is used later to determine when the vreg
93
- // / is killed in the BB.
94
- void analyzePHINodes (const MachineFunction& MF);
91
+ // / analyzePHINodes - Gather information about the PHI nodes in
92
+ // / here. In particular, we want to map the number of uses of a virtual
93
+ // / register which is used in a PHI node. We map that to the BB the
94
+ // / vreg is coming from. This is used later to determine when the vreg
95
+ // / is killed in the BB.
96
+ void analyzePHINodes (const MachineFunction & MF);
95
97
96
- // / Split critical edges where necessary for good coalescer performance.
97
- bool SplitPHIEdges (MachineFunction &MF, MachineBasicBlock &MBB,
98
- MachineLoopInfo *MLI,
99
- std::vector<SparseBitVector<>> *LiveInSets);
98
+ // / Split critical edges where necessary for good coalescer performance.
99
+ bool SplitPHIEdges (MachineFunction &MF, MachineBasicBlock &MBB,
100
+ MachineLoopInfo *MLI,
101
+ std::vector<SparseBitVector<>> *LiveInSets);
100
102
101
- // These functions are temporary abstractions around LiveVariables and
102
- // LiveIntervals, so they can go away when LiveVariables does.
103
- bool isLiveIn (Register Reg, const MachineBasicBlock *MBB);
104
- bool isLiveOutPastPHIs (Register Reg, const MachineBasicBlock *MBB);
103
+ // These functions are temporary abstractions around LiveVariables and
104
+ // LiveIntervals, so they can go away when LiveVariables does.
105
+ bool isLiveIn (Register Reg, const MachineBasicBlock *MBB);
106
+ bool isLiveOutPastPHIs (Register Reg, const MachineBasicBlock *MBB);
105
107
106
- using BBVRegPair = std::pair<unsigned , Register>;
107
- using VRegPHIUse = DenseMap<BBVRegPair, unsigned >;
108
+ using BBVRegPair = std::pair<unsigned , Register>;
109
+ using VRegPHIUse = DenseMap<BBVRegPair, unsigned >;
108
110
109
- // Count the number of non-undef PHI uses of each register in each BB.
110
- VRegPHIUse VRegPHIUseCount;
111
+ // Count the number of non-undef PHI uses of each register in each BB.
112
+ VRegPHIUse VRegPHIUseCount;
111
113
112
- // Defs of PHI sources which are implicit_def.
113
- SmallPtrSet<MachineInstr*, 4 > ImpDefs;
114
+ // Defs of PHI sources which are implicit_def.
115
+ SmallPtrSet<MachineInstr *, 4 > ImpDefs;
114
116
115
- // Map reusable lowered PHI node -> incoming join register.
116
- using LoweredPHIMap =
117
- DenseMap<MachineInstr*, unsigned , MachineInstrExpressionTrait>;
118
- LoweredPHIMap LoweredPHIs;
119
- };
117
+ // Map reusable lowered PHI node -> incoming join register.
118
+ using LoweredPHIMap =
119
+ DenseMap<MachineInstr *, unsigned , MachineInstrExpressionTrait>;
120
+ LoweredPHIMap LoweredPHIs;
121
+ };
120
122
121
123
} // end anonymous namespace
122
124
@@ -126,11 +128,11 @@ STATISTIC(NumReused, "Number of reused lowered phis");
126
128
127
129
char PHIElimination::ID = 0 ;
128
130
129
- char & llvm::PHIEliminationID = PHIElimination::ID;
131
+ char & llvm::PHIEliminationID = PHIElimination::ID;
130
132
131
133
INITIALIZE_PASS_BEGIN (PHIElimination, DEBUG_TYPE,
132
- " Eliminate PHI nodes for register allocation" ,
133
- false , false )
134
+ " Eliminate PHI nodes for register allocation" , false ,
135
+ false )
134
136
INITIALIZE_PASS_DEPENDENCY(LiveVariablesWrapperPass)
135
137
INITIALIZE_PASS_END(PHIElimination, DEBUG_TYPE,
136
138
" Eliminate PHI nodes for register allocation" , false , false )
@@ -238,11 +240,11 @@ bool PHIElimination::runOnMachineFunction(MachineFunction &MF) {
238
240
bool PHIElimination::EliminatePHINodes (MachineFunction &MF,
239
241
MachineBasicBlock &MBB) {
240
242
if (MBB.empty () || !MBB.front ().isPHI ())
241
- return false ; // Quick exit for basic blocks without PHIs.
243
+ return false ; // Quick exit for basic blocks without PHIs.
242
244
243
245
// Get an iterator to the last PHI node.
244
246
MachineBasicBlock::iterator LastPHIIt =
245
- std::prev (MBB.SkipPHIsAndLabels (MBB.begin ()));
247
+ std::prev (MBB.SkipPHIsAndLabels (MBB.begin ()));
246
248
247
249
// If all incoming edges are critical, we try to deduplicate identical PHIs so
248
250
// that we generate fewer copies. If at any edge is non-critical, we either
@@ -301,8 +303,8 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
301
303
// Create a new register for the incoming PHI arguments.
302
304
MachineFunction &MF = *MBB.getParent ();
303
305
unsigned IncomingReg = 0 ;
304
- bool EliminateNow = true ; // delay elimination of nodes in LoweredPHIs
305
- bool reusedIncoming = false ; // Is IncomingReg reused from an earlier PHI?
306
+ bool EliminateNow = true ; // delay elimination of nodes in LoweredPHIs
307
+ bool reusedIncoming = false ; // Is IncomingReg reused from an earlier PHI?
306
308
307
309
// Insert a register to register copy at the top of the current block (but
308
310
// after any remaining phi nodes) which copies the new incoming register
@@ -313,7 +315,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
313
315
// If all sources of a PHI node are implicit_def or undef uses, just emit an
314
316
// implicit_def instead of a copy.
315
317
PHICopy = BuildMI (MBB, AfterPHIsIt, MPhi->getDebugLoc (),
316
- TII->get (TargetOpcode::IMPLICIT_DEF), DestReg);
318
+ TII->get (TargetOpcode::IMPLICIT_DEF), DestReg);
317
319
else {
318
320
// Can we reuse an earlier PHI node? This only happens for critical edges,
319
321
// typically those created by tail duplication. Typically, an identical PHI
@@ -339,8 +341,8 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
339
341
}
340
342
341
343
// Give the target possiblity to handle special cases fallthrough otherwise
342
- PHICopy = TII->createPHIDestinationCopy (MBB, AfterPHIsIt, MPhi-> getDebugLoc (),
343
- IncomingReg, DestReg);
344
+ PHICopy = TII->createPHIDestinationCopy (
345
+ MBB, AfterPHIsIt, MPhi-> getDebugLoc (), IncomingReg, DestReg);
344
346
}
345
347
346
348
if (MPhi->peekDebugInstrNum ()) {
@@ -367,8 +369,8 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
367
369
// by default, so it's before the OldKill. But some Target hooks for
368
370
// createPHIDestinationCopy() may modify the default insert position of
369
371
// PHICopy.
370
- for (auto I = MBB.SkipPHIsAndLabels (MBB.begin ()), E = MBB.end ();
371
- I != E; ++I) {
372
+ for (auto I = MBB.SkipPHIsAndLabels (MBB.begin ()), E = MBB.end (); I != E;
373
+ ++I) {
372
374
if (I == PHICopy)
373
375
break ;
374
376
@@ -420,11 +422,10 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
420
422
LiveInterval &IncomingLI = LIS->getOrCreateEmptyInterval (IncomingReg);
421
423
VNInfo *IncomingVNI = IncomingLI.getVNInfoAt (MBBStartIndex);
422
424
if (!IncomingVNI)
423
- IncomingVNI = IncomingLI.getNextValue (MBBStartIndex,
424
- LIS->getVNInfoAllocator ());
425
- IncomingLI.addSegment (LiveInterval::Segment (MBBStartIndex,
426
- DestCopyIndex.getRegSlot (),
427
- IncomingVNI));
425
+ IncomingVNI =
426
+ IncomingLI.getNextValue (MBBStartIndex, LIS->getVNInfoAllocator ());
427
+ IncomingLI.addSegment (LiveInterval::Segment (
428
+ MBBStartIndex, DestCopyIndex.getRegSlot (), IncomingVNI));
428
429
}
429
430
430
431
LiveInterval &DestLI = LIS->getInterval (DestReg);
@@ -485,24 +486,24 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
485
486
486
487
// Now loop over all of the incoming arguments, changing them to copy into the
487
488
// IncomingReg register in the corresponding predecessor basic block.
488
- SmallPtrSet<MachineBasicBlock*, 8 > MBBsInsertedInto;
489
+ SmallPtrSet<MachineBasicBlock *, 8 > MBBsInsertedInto;
489
490
for (int i = NumSrcs - 1 ; i >= 0 ; --i) {
490
491
Register SrcReg = MPhi->getOperand (i * 2 + 1 ).getReg ();
491
- unsigned SrcSubReg = MPhi->getOperand (i* 2 + 1 ).getSubReg ();
492
- bool SrcUndef = MPhi->getOperand (i* 2 + 1 ).isUndef () ||
493
- isImplicitlyDefined (SrcReg, *MRI);
492
+ unsigned SrcSubReg = MPhi->getOperand (i * 2 + 1 ).getSubReg ();
493
+ bool SrcUndef = MPhi->getOperand (i * 2 + 1 ).isUndef () ||
494
+ isImplicitlyDefined (SrcReg, *MRI);
494
495
assert (SrcReg.isVirtual () &&
495
496
" Machine PHI Operands must all be virtual registers!" );
496
497
497
498
// Get the MachineBasicBlock equivalent of the BasicBlock that is the source
498
499
// path the PHI.
499
- MachineBasicBlock &opBlock = *MPhi->getOperand (i* 2 + 2 ).getMBB ();
500
+ MachineBasicBlock &opBlock = *MPhi->getOperand (i * 2 + 2 ).getMBB ();
500
501
501
502
// Check to make sure we haven't already emitted the copy for this block.
502
503
// This can happen because PHI nodes may have multiple entries for the same
503
504
// basic block.
504
505
if (!MBBsInsertedInto.insert (&opBlock).second )
505
- continue ; // If the copy has already been emitted, we're done.
506
+ continue ; // If the copy has already been emitted, we're done.
506
507
507
508
MachineInstr *SrcRegDef = MRI->getVRegDef (SrcReg);
508
509
if (SrcRegDef && TII->isUnspillableTerminator (SrcRegDef)) {
@@ -529,7 +530,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
529
530
// Find a safe location to insert the copy, this may be the first terminator
530
531
// in the block (or end()).
531
532
MachineBasicBlock::iterator InsertPos =
532
- findPHICopyInsertPoint (&opBlock, &MBB, SrcReg);
533
+ findPHICopyInsertPoint (&opBlock, &MBB, SrcReg);
533
534
534
535
// Insert the copy.
535
536
MachineInstr *NewSrcInstr = nullptr ;
@@ -538,9 +539,9 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
538
539
// The source register is undefined, so there is no need for a real
539
540
// COPY, but we still need to ensure joint dominance by defs.
540
541
// Insert an IMPLICIT_DEF instruction.
541
- NewSrcInstr = BuildMI (opBlock, InsertPos, MPhi-> getDebugLoc (),
542
- TII-> get (TargetOpcode::IMPLICIT_DEF ),
543
- IncomingReg);
542
+ NewSrcInstr =
543
+ BuildMI (opBlock, InsertPos, MPhi-> getDebugLoc ( ),
544
+ TII-> get (TargetOpcode::IMPLICIT_DEF), IncomingReg);
544
545
545
546
// Clean up the old implicit-def, if there even was one.
546
547
if (MachineInstr *DefMI = MRI->getVRegDef (SrcReg))
@@ -687,7 +688,7 @@ void PHIElimination::LowerPHINode(MachineBasicBlock &MBB,
687
688
// / particular, we want to map the number of uses of a virtual register which is
688
689
// / used in a PHI node. We map that to the BB the vreg is coming from. This is
689
690
// / used later to determine when the vreg is killed in the BB.
690
- void PHIElimination::analyzePHINodes (const MachineFunction& MF) {
691
+ void PHIElimination::analyzePHINodes (const MachineFunction & MF) {
691
692
for (const auto &MBB : MF) {
692
693
for (const auto &BBI : MBB) {
693
694
if (!BBI.isPHI ())
@@ -703,12 +704,11 @@ void PHIElimination::analyzePHINodes(const MachineFunction& MF) {
703
704
}
704
705
}
705
706
706
- bool PHIElimination::SplitPHIEdges (MachineFunction &MF,
707
- MachineBasicBlock &MBB,
707
+ bool PHIElimination::SplitPHIEdges (MachineFunction &MF, MachineBasicBlock &MBB,
708
708
MachineLoopInfo *MLI,
709
709
std::vector<SparseBitVector<>> *LiveInSets) {
710
710
if (MBB.empty () || !MBB.front ().isPHI () || MBB.isEHPad ())
711
- return false ; // Quick exit for basic blocks without PHIs.
711
+ return false ; // Quick exit for basic blocks without PHIs.
712
712
713
713
const MachineLoop *CurLoop = MLI ? MLI->getLoopFor (&MBB) : nullptr ;
714
714
bool IsLoopHeader = CurLoop && &MBB == CurLoop->getHeader ();
@@ -718,7 +718,7 @@ bool PHIElimination::SplitPHIEdges(MachineFunction &MF,
718
718
BBI != BBE && BBI->isPHI (); ++BBI) {
719
719
for (unsigned i = 1 , e = BBI->getNumOperands (); i != e; i += 2 ) {
720
720
Register Reg = BBI->getOperand (i).getReg ();
721
- MachineBasicBlock *PreMBB = BBI->getOperand (i+ 1 ).getMBB ();
721
+ MachineBasicBlock *PreMBB = BBI->getOperand (i + 1 ).getMBB ();
722
722
// Is there a critical edge from PreMBB to MBB?
723
723
if (PreMBB->succ_size () == 1 )
724
724
continue ;
@@ -799,9 +799,9 @@ bool PHIElimination::isLiveOutPastPHIs(Register Reg,
799
799
" isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals" );
800
800
// LiveVariables considers uses in PHIs to be in the predecessor basic block,
801
801
// so that a register used only in a PHI is not live out of the block. In
802
- // contrast, LiveIntervals considers uses in PHIs to be on the edge rather than
803
- // in the predecessor basic block, so that a register used only in a PHI is live
804
- // out of the block.
802
+ // contrast, LiveIntervals considers uses in PHIs to be on the edge rather
803
+ // than in the predecessor basic block, so that a register used only in a PHI
804
+ // is live out of the block.
805
805
if (LIS) {
806
806
const LiveInterval &LI = LIS->getInterval (Reg);
807
807
for (const MachineBasicBlock *SI : MBB->successors ())
0 commit comments