Skip to content

Commit b64e7b7

Browse files
author
Evan Cheng
committed
Fix two-address pass's aggressive instruction commuting heuristics. It's meant
to catch cases like: %reg1024<def> = MOV r1 %reg1025<def> = MOV r0 %reg1026<def> = ADD %reg1024, %reg1025 r0 = MOV %reg1026 By commuting ADD, it let coalescer eliminate all of the copies. However, there was a bug in the heuristics where it ended up commuting the ADD in: %reg1024<def> = MOV r0 %reg1025<def> = MOV 0 %reg1026<def> = ADD %reg1024, %reg1025 r0 = MOV %reg1026 That did no benefit but rather ensure the last MOV would not be coalesced. rdar://11355268 llvm-svn: 156048
1 parent 26ebbb7 commit b64e7b7

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

llvm/lib/CodeGen/TwoAddressInstructionPass.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace {
102102
MachineInstr *FindLastUseInMBB(unsigned Reg, MachineBasicBlock *MBB,
103103
unsigned Dist);
104104

105-
bool isProfitableToCommute(unsigned regB, unsigned regC,
105+
bool isProfitableToCommute(unsigned regA, unsigned regB, unsigned regC,
106106
MachineInstr *MI, MachineBasicBlock *MBB,
107107
unsigned Dist);
108108

@@ -567,7 +567,8 @@ regsAreCompatible(unsigned RegA, unsigned RegB, const TargetRegisterInfo *TRI) {
567567
/// isProfitableToReMat - Return true if it's potentially profitable to commute
568568
/// the two-address instruction that's being processed.
569569
bool
570-
TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
570+
TwoAddressInstructionPass::isProfitableToCommute(unsigned regA, unsigned regB,
571+
unsigned regC,
571572
MachineInstr *MI, MachineBasicBlock *MBB,
572573
unsigned Dist) {
573574
if (OptLevel == CodeGenOpt::None)
@@ -604,15 +605,15 @@ TwoAddressInstructionPass::isProfitableToCommute(unsigned regB, unsigned regC,
604605
// %reg1026<def> = ADD %reg1024, %reg1025
605606
// r0 = MOV %reg1026
606607
// Commute the ADD to hopefully eliminate an otherwise unavoidable copy.
607-
unsigned FromRegB = getMappedReg(regB, SrcRegMap);
608-
unsigned FromRegC = getMappedReg(regC, SrcRegMap);
609-
unsigned ToRegB = getMappedReg(regB, DstRegMap);
610-
unsigned ToRegC = getMappedReg(regC, DstRegMap);
611-
if ((FromRegB && ToRegB && !regsAreCompatible(FromRegB, ToRegB, TRI)) &&
612-
((!FromRegC && !ToRegC) ||
613-
regsAreCompatible(FromRegB, ToRegC, TRI) ||
614-
regsAreCompatible(FromRegC, ToRegB, TRI)))
615-
return true;
608+
unsigned ToRegA = getMappedReg(regA, DstRegMap);
609+
if (ToRegA) {
610+
unsigned FromRegB = getMappedReg(regB, SrcRegMap);
611+
unsigned FromRegC = getMappedReg(regC, SrcRegMap);
612+
bool BComp = !FromRegB || regsAreCompatible(FromRegB, ToRegA, TRI);
613+
bool CComp = !FromRegC || regsAreCompatible(FromRegC, ToRegA, TRI);
614+
if (BComp != CComp)
615+
return !BComp && CComp;
616+
}
616617

617618
// If there is a use of regC between its last def (could be livein) and this
618619
// instruction, then bail.
@@ -1211,6 +1212,9 @@ TryInstructionTransform(MachineBasicBlock::iterator &mi,
12111212
return true; // Done with this instruction.
12121213
}
12131214

1215+
if (TargetRegisterInfo::isVirtualRegister(regA))
1216+
ScanUses(regA, &*mbbi, Processed);
1217+
12141218
// Check if it is profitable to commute the operands.
12151219
unsigned SrcOp1, SrcOp2;
12161220
unsigned regC = 0;
@@ -1230,7 +1234,7 @@ TryInstructionTransform(MachineBasicBlock::iterator &mi,
12301234
// If C dies but B does not, swap the B and C operands.
12311235
// This makes the live ranges of A and C joinable.
12321236
TryCommute = true;
1233-
else if (isProfitableToCommute(regB, regC, &MI, mbbi, Dist)) {
1237+
else if (isProfitableToCommute(regA, regB, regC, &MI, mbbi, Dist)) {
12341238
TryCommute = true;
12351239
AggressiveCommute = true;
12361240
}
@@ -1252,9 +1256,6 @@ TryInstructionTransform(MachineBasicBlock::iterator &mi,
12521256
return true;
12531257
}
12541258

1255-
if (TargetRegisterInfo::isVirtualRegister(regA))
1256-
ScanUses(regA, &*mbbi, Processed);
1257-
12581259
if (MI.isConvertibleTo3Addr()) {
12591260
// This instruction is potentially convertible to a true
12601261
// three-address instruction. Check if it is profitable.

llvm/test/CodeGen/X86/4char-promote.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
; A test for checking PR 9623
2-
;RUN: llc -march=x86-64 -mcpu=corei7 -promote-elements < %s | FileCheck %s
2+
; RUN: llc -march=x86-64 -mcpu=corei7 -promote-elements < %s | FileCheck %s
33

44
target triple = "x86_64-apple-darwin"
55

66
; CHECK: pmulld
77
; CHECK: paddd
8-
; CHECK: movdqa
8+
; CHECK-NOT: movdqa
9+
; CHECK: ret
910

1011
define <4 x i8> @foo(<4 x i8> %x, <4 x i8> %y) {
1112
entry:

llvm/test/CodeGen/X86/jump_sign.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ declare i32 @bar(...)
2222
declare i32 @baz(...)
2323

2424
; rdar://10633221
25+
; rdar://11355268
2526
define i32 @g(i32 %a, i32 %b) nounwind {
2627
entry:
2728
; CHECK: g:
@@ -39,6 +40,8 @@ entry:
3940
; CHECK: h:
4041
; CHECK-NOT: cmp
4142
; CHECK: cmov
43+
; CHECK-NOT: movl
44+
; CHECK: ret
4245
%cmp = icmp slt i32 %b, %a
4346
%sub = sub nsw i32 %a, %b
4447
%cond = select i1 %cmp, i32 %sub, i32 0
@@ -49,6 +52,8 @@ entry:
4952
; CHECK: i:
5053
; CHECK-NOT: cmp
5154
; CHECK: cmov
55+
; CHECK-NOT: movl
56+
; CHECK: ret
5257
%cmp = icmp sgt i32 %a, %b
5358
%sub = sub nsw i32 %a, %b
5459
%cond = select i1 %cmp, i32 %sub, i32 0
@@ -59,6 +64,8 @@ entry:
5964
; CHECK: j:
6065
; CHECK-NOT: cmp
6166
; CHECK: cmov
67+
; CHECK-NOT: movl
68+
; CHECK: ret
6269
%cmp = icmp ugt i32 %a, %b
6370
%sub = sub i32 %a, %b
6471
%cond = select i1 %cmp, i32 %sub, i32 0
@@ -69,6 +76,8 @@ entry:
6976
; CHECK: k:
7077
; CHECK-NOT: cmp
7178
; CHECK: cmov
79+
; CHECK-NOT: movl
80+
; CHECK: ret
7281
%cmp = icmp ult i32 %b, %a
7382
%sub = sub i32 %a, %b
7483
%cond = select i1 %cmp, i32 %sub, i32 0

0 commit comments

Comments
 (0)