Skip to content

Commit a16fc2f

Browse files
committed
Teach MachineCSE how to do simple cross-block CSE involving physregs. This allows, for example, eliminating duplicate cmpl's on x86. Part of rdar://problem/8259436 .
llvm-svn: 130862
1 parent 7e00586 commit a16fc2f

File tree

3 files changed

+67
-29
lines changed

3 files changed

+67
-29
lines changed

llvm/lib/CodeGen/MachineCSE.cpp

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ namespace {
8282
MachineBasicBlock::const_iterator E) const ;
8383
bool hasLivePhysRegDefUses(const MachineInstr *MI,
8484
const MachineBasicBlock *MBB,
85-
SmallSet<unsigned,8> &PhysRefs) const;
85+
SmallSet<unsigned,8> &PhysRefs,
86+
SmallVector<unsigned,8> &PhysDefs) const;
8687
bool PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
8788
SmallSet<unsigned,8> &PhysRefs) const;
8889
bool isCSECandidate(MachineInstr *MI);
@@ -189,7 +190,8 @@ MachineCSE::isPhysDefTriviallyDead(unsigned Reg,
189190
/// instruction does not uses a physical register.
190191
bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
191192
const MachineBasicBlock *MBB,
192-
SmallSet<unsigned,8> &PhysRefs) const {
193+
SmallSet<unsigned,8> &PhysRefs,
194+
SmallVector<unsigned,8> &PhysDefs) const{
193195
MachineBasicBlock::const_iterator I = MI; I = llvm::next(I);
194196
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
195197
const MachineOperand &MO = MI->getOperand(i);
@@ -206,6 +208,7 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
206208
if (MO.isDef() &&
207209
(MO.isDead() || isPhysDefTriviallyDead(Reg, I, MBB->end())))
208210
continue;
211+
PhysDefs.push_back(Reg);
209212
PhysRefs.insert(Reg);
210213
for (const unsigned *Alias = TRI->getAliasSet(Reg); *Alias; ++Alias)
211214
PhysRefs.insert(*Alias);
@@ -216,35 +219,40 @@ bool MachineCSE::hasLivePhysRegDefUses(const MachineInstr *MI,
216219

217220
bool MachineCSE::PhysRegDefsReach(MachineInstr *CSMI, MachineInstr *MI,
218221
SmallSet<unsigned,8> &PhysRefs) const {
219-
// For now conservatively returns false if the common subexpression is
220-
// not in the same basic block as the given instruction.
221-
MachineBasicBlock *MBB = MI->getParent();
222-
if (CSMI->getParent() != MBB)
223-
return false;
224-
MachineBasicBlock::const_iterator I = CSMI; I = llvm::next(I);
225-
MachineBasicBlock::const_iterator E = MI;
222+
// Look backward from MI to find CSMI.
226223
unsigned LookAheadLeft = LookAheadLimit;
224+
MachineBasicBlock::const_reverse_iterator I(MI);
225+
MachineBasicBlock::const_reverse_iterator E(MI->getParent()->rend());
227226
while (LookAheadLeft) {
228-
// Skip over dbg_value's.
229-
while (I != E && I->isDebugValue())
230-
++I;
227+
while (LookAheadLeft && I != E) {
228+
// Skip over dbg_value's.
229+
while (I != E && I->isDebugValue())
230+
++I;
231231

232-
if (I == E)
233-
return true;
232+
if (&*I == CSMI)
233+
return true;
234234

235-
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
236-
const MachineOperand &MO = I->getOperand(i);
237-
if (!MO.isReg() || !MO.isDef())
238-
continue;
239-
unsigned MOReg = MO.getReg();
240-
if (TargetRegisterInfo::isVirtualRegister(MOReg))
241-
continue;
242-
if (PhysRefs.count(MOReg))
243-
return false;
244-
}
235+
for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
236+
const MachineOperand &MO = I->getOperand(i);
237+
if (!MO.isReg() || !MO.isDef())
238+
continue;
239+
unsigned MOReg = MO.getReg();
240+
if (TargetRegisterInfo::isVirtualRegister(MOReg))
241+
continue;
242+
if (PhysRefs.count(MOReg))
243+
return false;
244+
}
245245

246-
--LookAheadLeft;
247-
++I;
246+
--LookAheadLeft;
247+
++I;
248+
}
249+
// Go back another BB; for now, only go back at most one BB.
250+
MachineBasicBlock *CSBB = CSMI->getParent();
251+
MachineBasicBlock *BB = MI->getParent();
252+
if (!CSBB->isSuccessor(BB) || BB->pred_size() != 1)
253+
return false;
254+
I = CSBB->rbegin();
255+
E = CSBB->rend();
248256
}
249257

250258
return false;
@@ -395,7 +403,8 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
395403
// used, then it's not safe to replace it with a common subexpression.
396404
// It's also not safe if the instruction uses physical registers.
397405
SmallSet<unsigned,8> PhysRefs;
398-
if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs)) {
406+
SmallVector<unsigned,8> DirectPhysRefs;
407+
if (FoundCSE && hasLivePhysRegDefUses(MI, MBB, PhysRefs, DirectPhysRefs)) {
399408
FoundCSE = false;
400409

401410
// ... Unless the CS is local and it also defines the physical register
@@ -448,6 +457,13 @@ bool MachineCSE::ProcessBlock(MachineBasicBlock *MBB) {
448457
MRI->clearKillFlags(CSEPairs[i].second);
449458
}
450459
MI->eraseFromParent();
460+
if (!DirectPhysRefs.empty() && CSMI->getParent() != MBB) {
461+
assert(CSMI->getParent()->isSuccessor(MBB));
462+
SmallVector<unsigned,8>::iterator PI = DirectPhysRefs.begin(),
463+
PE = DirectPhysRefs.end();
464+
for (; PI != PE; ++PI)
465+
MBB->addLiveIn(*PI);
466+
}
451467
++NumCSEs;
452468
if (!PhysRefs.empty())
453469
++NumPhysCSEs;

llvm/test/CodeGen/Thumb2/thumb2-cbnz.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ bb7: ; preds = %bb3
2121

2222
bb9: ; preds = %bb7
2323
; CHECK: cmp r0, #0
24-
; CHECK: cmp r0, #0
25-
; CHECK-NEXT: cbnz
24+
; CHECK-NOT: cmp
25+
; CHECK: cbnz
2626
%0 = tail call double @floor(double %b) nounwind readnone ; <double> [#uses=0]
2727
br label %bb11
2828

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: llc < %s -mtriple=x86_64-apple-darwin10 | FileCheck %s
2+
3+
define i32 @cmp(i32* %aa, i32* %bb) nounwind readnone ssp {
4+
entry:
5+
%a = load i32* %aa
6+
%b = load i32* %bb
7+
%cmp = icmp sgt i32 %a, %b
8+
br i1 %cmp, label %return, label %if.end
9+
; CHECK: cmp:
10+
; CHECK: cmpl
11+
; CHECK: jg
12+
if.end: ; preds = %entry
13+
; CHECK-NOT: cmpl
14+
; CHECK: cmov
15+
%cmp4 = icmp slt i32 %a, %b
16+
%. = select i1 %cmp4, i32 2, i32 111
17+
br label %return
18+
19+
return: ; preds = %if.end, %entry
20+
%retval.0 = phi i32 [ 1, %entry ], [ %., %if.end ]
21+
ret i32 %retval.0
22+
}

0 commit comments

Comments
 (0)