Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 5415ff0

Browse files
authored
Merge pull request #83 from arielb1/bad-arm-2
backport fixes to LLVM 4.0 ARM codegen bugs
2 parents ee545e1 + 1d9b6cd commit 5415ff0

File tree

10 files changed

+1703
-6
lines changed

10 files changed

+1703
-6
lines changed

include/llvm/CodeGen/MIRYamlMapping.h

+2
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ struct MachineFunction {
381381
StringRef Name;
382382
unsigned Alignment = 0;
383383
bool ExposesReturnsTwice = false;
384+
bool NoVRegs;
384385
// GISel MachineFunctionProperties.
385386
bool Legalized = false;
386387
bool RegBankSelected = false;
@@ -405,6 +406,7 @@ template <> struct MappingTraits<MachineFunction> {
405406
YamlIO.mapRequired("name", MF.Name);
406407
YamlIO.mapOptional("alignment", MF.Alignment);
407408
YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
409+
YamlIO.mapOptional("noVRegs", MF.NoVRegs);
408410
YamlIO.mapOptional("legalized", MF.Legalized);
409411
YamlIO.mapOptional("regBankSelected", MF.RegBankSelected);
410412
YamlIO.mapOptional("selected", MF.Selected);

lib/CodeGen/IfConversion.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1353,7 +1353,8 @@ static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) {
13531353
return false;
13541354
PI = I++;
13551355
}
1356-
return true;
1356+
// Finally see if the last I is indeed a successor to PI.
1357+
return PI->isSuccessor(&*I);
13571358
}
13581359

13591360
/// Invalidate predecessor BB info so it would be re-analyzed to determine if it

lib/CodeGen/MIRParser/MIRParser.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
330330
MF.setAlignment(YamlMF.Alignment);
331331
MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
332332

333+
if (YamlMF.NoVRegs)
334+
MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
333335
if (YamlMF.Legalized)
334336
MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
335337
if (YamlMF.RegBankSelected)

lib/CodeGen/MIRPrinter.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ void MIRPrinter::print(const MachineFunction &MF) {
175175
YamlMF.Alignment = MF.getAlignment();
176176
YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
177177

178+
YamlMF.NoVRegs = MF.getProperties().hasProperty(
179+
MachineFunctionProperties::Property::NoVRegs);
178180
YamlMF.Legalized = MF.getProperties().hasProperty(
179181
MachineFunctionProperties::Property::Legalized);
180182
YamlMF.RegBankSelected = MF.getProperties().hasProperty(

lib/Target/ARM/ARMConstantIslandPass.cpp

+43-5
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,13 @@ bool ARMConstantIslands::undoLRSpillRestore() {
17151715
MI->eraseFromParent();
17161716
MadeChange = true;
17171717
}
1718+
if (MI->getOpcode() == ARM::tPUSH &&
1719+
MI->getOperand(2).getReg() == ARM::LR &&
1720+
MI->getNumExplicitOperands() == 3) {
1721+
// Just remove the push.
1722+
MI->eraseFromParent();
1723+
MadeChange = true;
1724+
}
17181725
}
17191726
return MadeChange;
17201727
}
@@ -1984,6 +1991,16 @@ static bool jumpTableFollowsTB(MachineInstr *JTMI, MachineInstr *CPEMI) {
19841991
&*MBB->begin() == CPEMI;
19851992
}
19861993

1994+
static bool registerDefinedBetween(unsigned Reg,
1995+
MachineBasicBlock::iterator From,
1996+
MachineBasicBlock::iterator To,
1997+
const TargetRegisterInfo *TRI) {
1998+
for (auto I = From; I != To; ++I)
1999+
if (I->modifiesRegister(Reg, TRI))
2000+
return true;
2001+
return false;
2002+
}
2003+
19872004
/// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
19882005
/// jumptables when it's possible.
19892006
bool ARMConstantIslands::optimizeThumb2JumpTables() {
@@ -2033,7 +2050,7 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
20332050
unsigned DeadSize = 0;
20342051
bool CanDeleteLEA = false;
20352052
bool BaseRegKill = false;
2036-
2053+
20372054
unsigned IdxReg = ~0U;
20382055
bool IdxRegKill = true;
20392056
if (isThumb2) {
@@ -2061,6 +2078,12 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
20612078
IdxReg = Shift->getOperand(2).getReg();
20622079
unsigned ShiftedIdxReg = Shift->getOperand(0).getReg();
20632080

2081+
// It's important that IdxReg is live until the actual TBB/TBH. Most of
2082+
// the range is checked later, but the LEA might still clobber it and not
2083+
// actually get removed.
2084+
if (BaseReg == IdxReg && !jumpTableFollowsTB(MI, User.CPEMI))
2085+
continue;
2086+
20642087
MachineInstr *Load = User.MI->getNextNode();
20652088
if (Load->getOpcode() != ARM::tLDRr)
20662089
continue;
@@ -2070,6 +2093,16 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
20702093
continue;
20712094

20722095
// If we're in PIC mode, there should be another ADD following.
2096+
auto *TRI = STI->getRegisterInfo();
2097+
2098+
// %base cannot be redefined after the load as it will appear before
2099+
// TBB/TBH like:
2100+
// %base =
2101+
// %base =
2102+
// tBB %base, %idx
2103+
if (registerDefinedBetween(BaseReg, Load->getNextNode(), MBB->end(), TRI))
2104+
continue;
2105+
20732106
if (isPositionIndependentOrROPI) {
20742107
MachineInstr *Add = Load->getNextNode();
20752108
if (Add->getOpcode() != ARM::tADDrr ||
@@ -2079,22 +2112,27 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
20792112
continue;
20802113
if (Add->getOperand(0).getReg() != MI->getOperand(0).getReg())
20812114
continue;
2082-
2115+
if (registerDefinedBetween(IdxReg, Add->getNextNode(), MI, TRI))
2116+
// IdxReg gets redefined in the middle of the sequence.
2117+
continue;
20832118
Add->eraseFromParent();
20842119
DeadSize += 2;
20852120
} else {
20862121
if (Load->getOperand(0).getReg() != MI->getOperand(0).getReg())
20872122
continue;
2123+
if (registerDefinedBetween(IdxReg, Load->getNextNode(), MI, TRI))
2124+
// IdxReg gets redefined in the middle of the sequence.
2125+
continue;
20882126
}
2089-
2090-
2127+
2128+
20912129
// Now safe to delete the load and lsl. The LEA will be removed later.
20922130
CanDeleteLEA = true;
20932131
Shift->eraseFromParent();
20942132
Load->eraseFromParent();
20952133
DeadSize += 4;
20962134
}
2097-
2135+
20982136
DEBUG(dbgs() << "Shrink JT: " << *MI);
20992137
MachineInstr *CPEMI = User.CPEMI;
21002138
unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;

0 commit comments

Comments
 (0)