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

Commit

Permalink
Merge pull request #83 from arielb1/bad-arm-2
Browse files Browse the repository at this point in the history
backport fixes to LLVM 4.0 ARM codegen bugs
  • Loading branch information
alexcrichton authored Jun 18, 2017
2 parents ee545e1 + 1d9b6cd commit 5415ff0
Show file tree
Hide file tree
Showing 10 changed files with 1,703 additions and 6 deletions.
2 changes: 2 additions & 0 deletions include/llvm/CodeGen/MIRYamlMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ struct MachineFunction {
StringRef Name;
unsigned Alignment = 0;
bool ExposesReturnsTwice = false;
bool NoVRegs;
// GISel MachineFunctionProperties.
bool Legalized = false;
bool RegBankSelected = false;
Expand All @@ -405,6 +406,7 @@ template <> struct MappingTraits<MachineFunction> {
YamlIO.mapRequired("name", MF.Name);
YamlIO.mapOptional("alignment", MF.Alignment);
YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
YamlIO.mapOptional("noVRegs", MF.NoVRegs);
YamlIO.mapOptional("legalized", MF.Legalized);
YamlIO.mapOptional("regBankSelected", MF.RegBankSelected);
YamlIO.mapOptional("selected", MF.Selected);
Expand Down
3 changes: 2 additions & 1 deletion lib/CodeGen/IfConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1353,7 +1353,8 @@ static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) {
return false;
PI = I++;
}
return true;
// Finally see if the last I is indeed a successor to PI.
return PI->isSuccessor(&*I);
}

/// Invalidate predecessor BB info so it would be re-analyzed to determine if it
Expand Down
2 changes: 2 additions & 0 deletions lib/CodeGen/MIRParser/MIRParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,8 @@ bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
MF.setAlignment(YamlMF.Alignment);
MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);

if (YamlMF.NoVRegs)
MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
if (YamlMF.Legalized)
MF.getProperties().set(MachineFunctionProperties::Property::Legalized);
if (YamlMF.RegBankSelected)
Expand Down
2 changes: 2 additions & 0 deletions lib/CodeGen/MIRPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ void MIRPrinter::print(const MachineFunction &MF) {
YamlMF.Alignment = MF.getAlignment();
YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();

YamlMF.NoVRegs = MF.getProperties().hasProperty(
MachineFunctionProperties::Property::NoVRegs);
YamlMF.Legalized = MF.getProperties().hasProperty(
MachineFunctionProperties::Property::Legalized);
YamlMF.RegBankSelected = MF.getProperties().hasProperty(
Expand Down
48 changes: 43 additions & 5 deletions lib/Target/ARM/ARMConstantIslandPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,13 @@ bool ARMConstantIslands::undoLRSpillRestore() {
MI->eraseFromParent();
MadeChange = true;
}
if (MI->getOpcode() == ARM::tPUSH &&
MI->getOperand(2).getReg() == ARM::LR &&
MI->getNumExplicitOperands() == 3) {
// Just remove the push.
MI->eraseFromParent();
MadeChange = true;
}
}
return MadeChange;
}
Expand Down Expand Up @@ -1984,6 +1991,16 @@ static bool jumpTableFollowsTB(MachineInstr *JTMI, MachineInstr *CPEMI) {
&*MBB->begin() == CPEMI;
}

static bool registerDefinedBetween(unsigned Reg,
MachineBasicBlock::iterator From,
MachineBasicBlock::iterator To,
const TargetRegisterInfo *TRI) {
for (auto I = From; I != To; ++I)
if (I->modifiesRegister(Reg, TRI))
return true;
return false;
}

/// optimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller
/// jumptables when it's possible.
bool ARMConstantIslands::optimizeThumb2JumpTables() {
Expand Down Expand Up @@ -2033,7 +2050,7 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
unsigned DeadSize = 0;
bool CanDeleteLEA = false;
bool BaseRegKill = false;

unsigned IdxReg = ~0U;
bool IdxRegKill = true;
if (isThumb2) {
Expand Down Expand Up @@ -2061,6 +2078,12 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
IdxReg = Shift->getOperand(2).getReg();
unsigned ShiftedIdxReg = Shift->getOperand(0).getReg();

// It's important that IdxReg is live until the actual TBB/TBH. Most of
// the range is checked later, but the LEA might still clobber it and not
// actually get removed.
if (BaseReg == IdxReg && !jumpTableFollowsTB(MI, User.CPEMI))
continue;

MachineInstr *Load = User.MI->getNextNode();
if (Load->getOpcode() != ARM::tLDRr)
continue;
Expand All @@ -2070,6 +2093,16 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
continue;

// If we're in PIC mode, there should be another ADD following.
auto *TRI = STI->getRegisterInfo();

// %base cannot be redefined after the load as it will appear before
// TBB/TBH like:
// %base =
// %base =
// tBB %base, %idx
if (registerDefinedBetween(BaseReg, Load->getNextNode(), MBB->end(), TRI))
continue;

if (isPositionIndependentOrROPI) {
MachineInstr *Add = Load->getNextNode();
if (Add->getOpcode() != ARM::tADDrr ||
Expand All @@ -2079,22 +2112,27 @@ bool ARMConstantIslands::optimizeThumb2JumpTables() {
continue;
if (Add->getOperand(0).getReg() != MI->getOperand(0).getReg())
continue;

if (registerDefinedBetween(IdxReg, Add->getNextNode(), MI, TRI))
// IdxReg gets redefined in the middle of the sequence.
continue;
Add->eraseFromParent();
DeadSize += 2;
} else {
if (Load->getOperand(0).getReg() != MI->getOperand(0).getReg())
continue;
if (registerDefinedBetween(IdxReg, Load->getNextNode(), MI, TRI))
// IdxReg gets redefined in the middle of the sequence.
continue;
}


// Now safe to delete the load and lsl. The LEA will be removed later.
CanDeleteLEA = true;
Shift->eraseFromParent();
Load->eraseFromParent();
DeadSize += 4;
}

DEBUG(dbgs() << "Shrink JT: " << *MI);
MachineInstr *CPEMI = User.CPEMI;
unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT;
Expand Down
Loading

0 comments on commit 5415ff0

Please sign in to comment.