2007-01-19 07:51:42 +00:00
|
|
|
//===-- ARMLoadStoreOptimizer.cpp - ARM load / store opt. pass ----*- C++ -*-=//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-01-19 07:51:42 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a pass that performs load / store related peephole
|
|
|
|
// optimizations. This pass should be run after register allocation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "arm-ldst-opt"
|
|
|
|
#include "ARM.h"
|
|
|
|
#include "ARMAddressingModes.h"
|
2009-08-08 03:20:32 +00:00
|
|
|
#include "ARMBaseInstrInfo.h"
|
2007-03-07 20:30:36 +00:00
|
|
|
#include "ARMMachineFunctionInfo.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "ARMRegisterInfo.h"
|
2009-06-15 08:28:29 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2009-08-13 21:58:54 +00:00
|
|
|
#include "llvm/Function.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2009-06-13 09:12:55 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2007-03-06 18:02:41 +00:00
|
|
|
#include "llvm/CodeGen/RegisterScavenging.h"
|
2009-06-15 08:28:29 +00:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2009-06-15 08:28:29 +00:00
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2009-07-08 18:01:40 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-06-13 09:12:55 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2009-06-19 23:17:27 +00:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2009-06-13 09:12:55 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-01-19 07:51:42 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
STATISTIC(NumLDMGened , "Number of ldm instructions generated");
|
|
|
|
STATISTIC(NumSTMGened , "Number of stm instructions generated");
|
|
|
|
STATISTIC(NumFLDMGened, "Number of fldm instructions generated");
|
|
|
|
STATISTIC(NumFSTMGened, "Number of fstm instructions generated");
|
2009-06-13 09:12:55 +00:00
|
|
|
STATISTIC(NumLdStMoved, "Number of load / store instructions moved");
|
2009-06-18 02:04:01 +00:00
|
|
|
STATISTIC(NumLDRDFormed,"Number of ldrd created before allocation");
|
|
|
|
STATISTIC(NumSTRDFormed,"Number of strd created before allocation");
|
|
|
|
STATISTIC(NumLDRD2LDM, "Number of ldrd instructions turned back into ldm");
|
|
|
|
STATISTIC(NumSTRD2STM, "Number of strd instructions turned back into stm");
|
|
|
|
STATISTIC(NumLDRD2LDR, "Number of ldrd instructions turned back into ldr's");
|
|
|
|
STATISTIC(NumSTRD2STR, "Number of strd instructions turned back into str's");
|
2009-06-13 09:12:55 +00:00
|
|
|
|
|
|
|
/// ARMAllocLoadStoreOpt - Post- register allocation pass the combine
|
|
|
|
/// load / store instructions to form ldm / stm instructions.
|
2007-01-19 07:51:42 +00:00
|
|
|
|
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct ARMLoadStoreOpt : public MachineFunctionPass {
|
2007-05-03 01:11:54 +00:00
|
|
|
static char ID;
|
2008-09-04 17:05:41 +00:00
|
|
|
ARMLoadStoreOpt() : MachineFunctionPass(&ID) {}
|
2007-05-01 21:15:47 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
const TargetInstrInfo *TII;
|
2008-02-10 18:45:23 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
2007-03-07 20:30:36 +00:00
|
|
|
ARMFunctionInfo *AFI;
|
2007-03-06 18:02:41 +00:00
|
|
|
RegScavenger *RS;
|
2009-07-09 23:11:34 +00:00
|
|
|
bool isThumb2;
|
2007-01-19 07:51:42 +00:00
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "ARM load / store optimization pass";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct MemOpQueueEntry {
|
|
|
|
int Offset;
|
|
|
|
unsigned Position;
|
|
|
|
MachineBasicBlock::iterator MBBI;
|
|
|
|
bool Merged;
|
|
|
|
MemOpQueueEntry(int o, int p, MachineBasicBlock::iterator i)
|
|
|
|
: Offset(o), Position(p), MBBI(i), Merged(false) {};
|
|
|
|
};
|
|
|
|
typedef SmallVector<MemOpQueueEntry,8> MemOpQueue;
|
|
|
|
typedef MemOpQueue::iterator MemOpQueueIter;
|
|
|
|
|
2009-06-05 19:08:58 +00:00
|
|
|
bool MergeOps(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI,
|
2009-06-05 18:19:23 +00:00
|
|
|
int Offset, unsigned Base, bool BaseKill, int Opcode,
|
|
|
|
ARMCC::CondCodes Pred, unsigned PredReg, unsigned Scratch,
|
|
|
|
DebugLoc dl, SmallVector<std::pair<unsigned, bool>, 8> &Regs);
|
2009-06-05 17:56:14 +00:00
|
|
|
void MergeLDR_STR(MachineBasicBlock &MBB, unsigned SIndex, unsigned Base,
|
|
|
|
int Opcode, unsigned Size,
|
|
|
|
ARMCC::CondCodes Pred, unsigned PredReg,
|
|
|
|
unsigned Scratch, MemOpQueue &MemOps,
|
|
|
|
SmallVector<MachineBasicBlock::iterator, 4> &Merges);
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2007-03-08 02:55:08 +00:00
|
|
|
void AdvanceRS(MachineBasicBlock &MBB, MemOpQueue &MemOps);
|
2009-06-15 08:28:29 +00:00
|
|
|
bool FixInvalidRegPairOp(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI);
|
2009-07-09 23:11:34 +00:00
|
|
|
bool MergeBaseUpdateLoadStore(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
const TargetInstrInfo *TII,
|
|
|
|
bool &Advance,
|
|
|
|
MachineBasicBlock::iterator &I);
|
|
|
|
bool MergeBaseUpdateLSMultiple(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
bool &Advance,
|
|
|
|
MachineBasicBlock::iterator &I);
|
2007-01-19 07:51:42 +00:00
|
|
|
bool LoadStoreMultipleOpti(MachineBasicBlock &MBB);
|
|
|
|
bool MergeReturnIntoLDM(MachineBasicBlock &MBB);
|
|
|
|
};
|
2007-05-03 01:11:54 +00:00
|
|
|
char ARMLoadStoreOpt::ID = 0;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int getLoadStoreMultipleOpcode(int Opcode) {
|
|
|
|
switch (Opcode) {
|
|
|
|
case ARM::LDR:
|
|
|
|
NumLDMGened++;
|
|
|
|
return ARM::LDM;
|
|
|
|
case ARM::STR:
|
|
|
|
NumSTMGened++;
|
|
|
|
return ARM::STM;
|
2009-07-09 23:11:34 +00:00
|
|
|
case ARM::t2LDRi8:
|
|
|
|
case ARM::t2LDRi12:
|
|
|
|
NumLDMGened++;
|
|
|
|
return ARM::t2LDM;
|
|
|
|
case ARM::t2STRi8:
|
|
|
|
case ARM::t2STRi12:
|
|
|
|
NumSTMGened++;
|
|
|
|
return ARM::t2STM;
|
2007-01-19 07:51:42 +00:00
|
|
|
case ARM::FLDS:
|
|
|
|
NumFLDMGened++;
|
|
|
|
return ARM::FLDMS;
|
|
|
|
case ARM::FSTS:
|
|
|
|
NumFSTMGened++;
|
|
|
|
return ARM::FSTMS;
|
|
|
|
case ARM::FLDD:
|
|
|
|
NumFLDMGened++;
|
|
|
|
return ARM::FLDMD;
|
|
|
|
case ARM::FSTD:
|
|
|
|
NumFSTMGened++;
|
|
|
|
return ARM::FSTMD;
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unhandled opcode!");
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-08-04 01:43:45 +00:00
|
|
|
static bool isT2i32Load(unsigned Opc) {
|
|
|
|
return Opc == ARM::t2LDRi12 || Opc == ARM::t2LDRi8;
|
|
|
|
}
|
|
|
|
|
2009-07-09 23:11:34 +00:00
|
|
|
static bool isi32Load(unsigned Opc) {
|
2009-08-04 01:43:45 +00:00
|
|
|
return Opc == ARM::LDR || isT2i32Load(Opc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isT2i32Store(unsigned Opc) {
|
|
|
|
return Opc == ARM::t2STRi12 || Opc == ARM::t2STRi8;
|
2009-07-09 23:11:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isi32Store(unsigned Opc) {
|
2009-08-04 01:43:45 +00:00
|
|
|
return Opc == ARM::STR || isT2i32Store(Opc);
|
2009-07-09 23:11:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-05 19:08:58 +00:00
|
|
|
/// MergeOps - Create and insert a LDM or STM with Base as base register and
|
2007-01-19 07:51:42 +00:00
|
|
|
/// registers in Regs as the register operands that would be loaded / stored.
|
2009-08-11 15:33:49 +00:00
|
|
|
/// It returns true if the transformation is done.
|
2009-06-05 18:19:23 +00:00
|
|
|
bool
|
2009-06-05 19:08:58 +00:00
|
|
|
ARMLoadStoreOpt::MergeOps(MachineBasicBlock &MBB,
|
2009-06-05 18:19:23 +00:00
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
int Offset, unsigned Base, bool BaseKill,
|
|
|
|
int Opcode, ARMCC::CondCodes Pred,
|
|
|
|
unsigned PredReg, unsigned Scratch, DebugLoc dl,
|
|
|
|
SmallVector<std::pair<unsigned, bool>, 8> &Regs) {
|
2007-01-19 07:51:42 +00:00
|
|
|
// Only a single register to load / store. Don't bother.
|
|
|
|
unsigned NumRegs = Regs.size();
|
|
|
|
if (NumRegs <= 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ARM_AM::AMSubMode Mode = ARM_AM::ia;
|
2009-07-09 23:11:34 +00:00
|
|
|
bool isAM4 = isi32Load(Opcode) || isi32Store(Opcode);
|
2009-08-04 08:34:18 +00:00
|
|
|
if (isAM4 && Offset == 4) {
|
|
|
|
if (isThumb2)
|
|
|
|
// Thumb2 does not support ldmib / stmib.
|
|
|
|
return false;
|
2007-01-19 07:51:42 +00:00
|
|
|
Mode = ARM_AM::ib;
|
2009-08-04 08:34:18 +00:00
|
|
|
} else if (isAM4 && Offset == -4 * (int)NumRegs + 4) {
|
|
|
|
if (isThumb2)
|
|
|
|
// Thumb2 does not support ldmda / stmda.
|
|
|
|
return false;
|
2007-01-19 07:51:42 +00:00
|
|
|
Mode = ARM_AM::da;
|
2009-08-04 08:34:18 +00:00
|
|
|
} else if (isAM4 && Offset == -4 * (int)NumRegs) {
|
2007-01-19 07:51:42 +00:00
|
|
|
Mode = ARM_AM::db;
|
2009-08-04 08:34:18 +00:00
|
|
|
} else if (Offset != 0) {
|
2007-01-19 07:51:42 +00:00
|
|
|
// If starting offset isn't zero, insert a MI to materialize a new base.
|
|
|
|
// But only do so if it is cost effective, i.e. merging more than two
|
|
|
|
// loads / stores.
|
|
|
|
if (NumRegs <= 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
unsigned NewBase;
|
2009-07-09 23:11:34 +00:00
|
|
|
if (isi32Load(Opcode))
|
2007-01-19 07:51:42 +00:00
|
|
|
// If it is a load, then just use one of the destination register to
|
|
|
|
// use as the new base.
|
2007-03-06 21:59:20 +00:00
|
|
|
NewBase = Regs[NumRegs-1].first;
|
2007-01-19 07:51:42 +00:00
|
|
|
else {
|
2007-03-07 02:38:05 +00:00
|
|
|
// Use the scratch register to use as a new base.
|
|
|
|
NewBase = Scratch;
|
2007-03-06 21:59:20 +00:00
|
|
|
if (NewBase == 0)
|
|
|
|
return false;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
2009-08-07 00:34:42 +00:00
|
|
|
int BaseOpc = !isThumb2
|
|
|
|
? ARM::ADDri
|
|
|
|
: ((Base == ARM::SP) ? ARM::t2ADDrSPi : ARM::t2ADDri);
|
2007-01-19 07:51:42 +00:00
|
|
|
if (Offset < 0) {
|
2009-08-07 00:34:42 +00:00
|
|
|
BaseOpc = !isThumb2
|
|
|
|
? ARM::SUBri
|
|
|
|
: ((Base == ARM::SP) ? ARM::t2SUBrSPi : ARM::t2SUBri);
|
2007-01-19 07:51:42 +00:00
|
|
|
Offset = - Offset;
|
|
|
|
}
|
2009-07-09 23:11:34 +00:00
|
|
|
int ImmedOffset = isThumb2
|
|
|
|
? ARM_AM::getT2SOImmVal(Offset) : ARM_AM::getSOImmVal(Offset);
|
|
|
|
if (ImmedOffset == -1)
|
|
|
|
// FIXME: Try t2ADDri12 or t2SUBri12?
|
2007-01-19 07:51:42 +00:00
|
|
|
return false; // Probably not worth it then.
|
2007-03-06 21:59:20 +00:00
|
|
|
|
2009-02-13 02:25:56 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(BaseOpc), NewBase)
|
2009-07-08 21:03:57 +00:00
|
|
|
.addReg(Base, getKillRegState(BaseKill)).addImm(Offset)
|
2007-07-10 18:08:01 +00:00
|
|
|
.addImm(Pred).addReg(PredReg).addReg(0);
|
2007-01-19 07:51:42 +00:00
|
|
|
Base = NewBase;
|
2007-03-06 21:59:20 +00:00
|
|
|
BaseKill = true; // New base is always killed right its use.
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isDPR = Opcode == ARM::FLDD || Opcode == ARM::FSTD;
|
2009-08-04 01:43:45 +00:00
|
|
|
bool isDef = isi32Load(Opcode) || Opcode == ARM::FLDS || Opcode == ARM::FLDD;
|
2007-01-19 07:51:42 +00:00
|
|
|
Opcode = getLoadStoreMultipleOpcode(Opcode);
|
|
|
|
MachineInstrBuilder MIB = (isAM4)
|
2009-02-13 02:25:56 +00:00
|
|
|
? BuildMI(MBB, MBBI, dl, TII->get(Opcode))
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(Base, getKillRegState(BaseKill))
|
2007-07-05 07:18:20 +00:00
|
|
|
.addImm(ARM_AM::getAM4ModeImm(Mode)).addImm(Pred).addReg(PredReg)
|
2009-02-13 02:25:56 +00:00
|
|
|
: BuildMI(MBB, MBBI, dl, TII->get(Opcode))
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(Base, getKillRegState(BaseKill))
|
2007-05-15 01:29:07 +00:00
|
|
|
.addImm(ARM_AM::getAM5Opc(Mode, false, isDPR ? NumRegs<<1 : NumRegs))
|
2007-07-05 07:18:20 +00:00
|
|
|
.addImm(Pred).addReg(PredReg);
|
2009-10-01 01:33:39 +00:00
|
|
|
MIB.addReg(0); // Add optional writeback (0 for now).
|
2007-01-19 07:51:42 +00:00
|
|
|
for (unsigned i = 0; i != NumRegs; ++i)
|
2009-05-13 21:33:08 +00:00
|
|
|
MIB = MIB.addReg(Regs[i].first, getDefRegState(isDef)
|
|
|
|
| getKillRegState(Regs[i].second));
|
2007-01-19 07:51:42 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-06 21:59:20 +00:00
|
|
|
/// MergeLDR_STR - Merge a number of load / store instructions into one or more
|
|
|
|
/// load / store multiple instructions.
|
2009-06-05 17:56:14 +00:00
|
|
|
void
|
2007-03-07 02:38:05 +00:00
|
|
|
ARMLoadStoreOpt::MergeLDR_STR(MachineBasicBlock &MBB, unsigned SIndex,
|
2009-06-05 17:56:14 +00:00
|
|
|
unsigned Base, int Opcode, unsigned Size,
|
|
|
|
ARMCC::CondCodes Pred, unsigned PredReg,
|
|
|
|
unsigned Scratch, MemOpQueue &MemOps,
|
|
|
|
SmallVector<MachineBasicBlock::iterator, 4> &Merges) {
|
2009-07-09 23:11:34 +00:00
|
|
|
bool isAM4 = isi32Load(Opcode) || isi32Store(Opcode);
|
2007-01-19 07:51:42 +00:00
|
|
|
int Offset = MemOps[SIndex].Offset;
|
|
|
|
int SOffset = Offset;
|
|
|
|
unsigned Pos = MemOps[SIndex].Position;
|
|
|
|
MachineBasicBlock::iterator Loc = MemOps[SIndex].MBBI;
|
2009-06-05 18:19:23 +00:00
|
|
|
DebugLoc dl = Loc->getDebugLoc();
|
|
|
|
unsigned PReg = Loc->getOperand(0).getReg();
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned PRegNum = ARMRegisterInfo::getRegisterNumbering(PReg);
|
2009-06-05 18:19:23 +00:00
|
|
|
bool isKill = Loc->getOperand(0).isKill();
|
2007-05-15 01:29:07 +00:00
|
|
|
|
|
|
|
SmallVector<std::pair<unsigned,bool>, 8> Regs;
|
2007-03-06 21:59:20 +00:00
|
|
|
Regs.push_back(std::make_pair(PReg, isKill));
|
2007-01-19 07:51:42 +00:00
|
|
|
for (unsigned i = SIndex+1, e = MemOps.size(); i != e; ++i) {
|
|
|
|
int NewOffset = MemOps[i].Offset;
|
|
|
|
unsigned Reg = MemOps[i].MBBI->getOperand(0).getReg();
|
|
|
|
unsigned RegNum = ARMRegisterInfo::getRegisterNumbering(Reg);
|
2007-03-06 21:59:20 +00:00
|
|
|
isKill = MemOps[i].MBBI->getOperand(0).isKill();
|
2007-01-19 07:51:42 +00:00
|
|
|
// AM4 - register numbers in ascending order.
|
|
|
|
// AM5 - consecutive register numbers in ascending order.
|
|
|
|
if (NewOffset == Offset + (int)Size &&
|
|
|
|
((isAM4 && RegNum > PRegNum) || RegNum == PRegNum+1)) {
|
|
|
|
Offset += Size;
|
2007-03-06 21:59:20 +00:00
|
|
|
Regs.push_back(std::make_pair(Reg, isKill));
|
2007-01-19 07:51:42 +00:00
|
|
|
PRegNum = RegNum;
|
|
|
|
} else {
|
|
|
|
// Can't merge this in. Try merge the earlier ones first.
|
2009-06-05 19:08:58 +00:00
|
|
|
if (MergeOps(MBB, ++Loc, SOffset, Base, false, Opcode, Pred, PredReg,
|
2009-06-05 18:19:23 +00:00
|
|
|
Scratch, dl, Regs)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
Merges.push_back(prior(Loc));
|
|
|
|
for (unsigned j = SIndex; j < i; ++j) {
|
|
|
|
MBB.erase(MemOps[j].MBBI);
|
|
|
|
MemOps[j].Merged = true;
|
|
|
|
}
|
|
|
|
}
|
2009-06-05 17:56:14 +00:00
|
|
|
MergeLDR_STR(MBB, i, Base, Opcode, Size, Pred, PredReg, Scratch,
|
|
|
|
MemOps, Merges);
|
|
|
|
return;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (MemOps[i].Position > Pos) {
|
|
|
|
Pos = MemOps[i].Position;
|
|
|
|
Loc = MemOps[i].MBBI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-26 19:00:32 +00:00
|
|
|
bool BaseKill = Loc->findRegisterUseOperandIdx(Base, true) != -1;
|
2009-06-05 19:08:58 +00:00
|
|
|
if (MergeOps(MBB, ++Loc, SOffset, Base, BaseKill, Opcode, Pred, PredReg,
|
2009-06-05 18:19:23 +00:00
|
|
|
Scratch, dl, Regs)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
Merges.push_back(prior(Loc));
|
|
|
|
for (unsigned i = SIndex, e = MemOps.size(); i != e; ++i) {
|
|
|
|
MBB.erase(MemOps[i].MBBI);
|
|
|
|
MemOps[i].Merged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-05 17:56:14 +00:00
|
|
|
return;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool isMatchingDecrement(MachineInstr *MI, unsigned Base,
|
2009-08-04 01:43:45 +00:00
|
|
|
unsigned Bytes, unsigned Limit,
|
|
|
|
ARMCC::CondCodes Pred, unsigned PredReg){
|
2007-07-05 07:18:20 +00:00
|
|
|
unsigned MyPredReg = 0;
|
2009-07-09 23:11:34 +00:00
|
|
|
if (!MI)
|
|
|
|
return false;
|
2009-08-04 01:43:45 +00:00
|
|
|
if (MI->getOpcode() != ARM::t2SUBri &&
|
2009-08-07 00:34:42 +00:00
|
|
|
MI->getOpcode() != ARM::t2SUBrSPi &&
|
|
|
|
MI->getOpcode() != ARM::t2SUBrSPi12 &&
|
|
|
|
MI->getOpcode() != ARM::tSUBspi &&
|
2009-08-04 01:43:45 +00:00
|
|
|
MI->getOpcode() != ARM::SUBri)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Make sure the offset fits in 8 bits.
|
|
|
|
if (Bytes <= 0 || (Limit && Bytes >= Limit))
|
|
|
|
return false;
|
2009-07-09 23:11:34 +00:00
|
|
|
|
2009-08-07 00:34:42 +00:00
|
|
|
unsigned Scale = (MI->getOpcode() == ARM::tSUBspi) ? 4 : 1; // FIXME
|
2009-07-09 23:11:34 +00:00
|
|
|
return (MI->getOperand(0).getReg() == Base &&
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).getReg() == Base &&
|
2009-08-07 00:34:42 +00:00
|
|
|
(MI->getOperand(2).getImm()*Scale) == Bytes &&
|
2009-08-08 03:20:32 +00:00
|
|
|
llvm::getInstrPredicate(MI, MyPredReg) == Pred &&
|
2007-07-05 07:18:20 +00:00
|
|
|
MyPredReg == PredReg);
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool isMatchingIncrement(MachineInstr *MI, unsigned Base,
|
2009-08-04 01:43:45 +00:00
|
|
|
unsigned Bytes, unsigned Limit,
|
|
|
|
ARMCC::CondCodes Pred, unsigned PredReg){
|
2007-07-05 07:18:20 +00:00
|
|
|
unsigned MyPredReg = 0;
|
2009-07-09 23:11:34 +00:00
|
|
|
if (!MI)
|
|
|
|
return false;
|
2009-08-04 01:43:45 +00:00
|
|
|
if (MI->getOpcode() != ARM::t2ADDri &&
|
2009-08-07 00:34:42 +00:00
|
|
|
MI->getOpcode() != ARM::t2ADDrSPi &&
|
|
|
|
MI->getOpcode() != ARM::t2ADDrSPi12 &&
|
|
|
|
MI->getOpcode() != ARM::tADDspi &&
|
2009-08-04 01:43:45 +00:00
|
|
|
MI->getOpcode() != ARM::ADDri)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (Bytes <= 0 || (Limit && Bytes >= Limit))
|
2009-07-09 23:11:34 +00:00
|
|
|
// Make sure the offset fits in 8 bits.
|
2009-08-04 01:43:45 +00:00
|
|
|
return false;
|
2009-07-09 23:11:34 +00:00
|
|
|
|
2009-08-07 00:34:42 +00:00
|
|
|
unsigned Scale = (MI->getOpcode() == ARM::tADDspi) ? 4 : 1; // FIXME
|
2009-07-09 23:11:34 +00:00
|
|
|
return (MI->getOperand(0).getReg() == Base &&
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).getReg() == Base &&
|
2009-08-07 00:34:42 +00:00
|
|
|
(MI->getOperand(2).getImm()*Scale) == Bytes &&
|
2009-08-08 03:20:32 +00:00
|
|
|
llvm::getInstrPredicate(MI, MyPredReg) == Pred &&
|
2007-07-05 07:18:20 +00:00
|
|
|
MyPredReg == PredReg);
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline unsigned getLSMultipleTransferSize(MachineInstr *MI) {
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default: return 0;
|
|
|
|
case ARM::LDR:
|
|
|
|
case ARM::STR:
|
2009-07-09 23:11:34 +00:00
|
|
|
case ARM::t2LDRi8:
|
|
|
|
case ARM::t2LDRi12:
|
|
|
|
case ARM::t2STRi8:
|
|
|
|
case ARM::t2STRi12:
|
2007-01-19 07:51:42 +00:00
|
|
|
case ARM::FLDS:
|
|
|
|
case ARM::FSTS:
|
|
|
|
return 4;
|
|
|
|
case ARM::FLDD:
|
|
|
|
case ARM::FSTD:
|
|
|
|
return 8;
|
|
|
|
case ARM::LDM:
|
|
|
|
case ARM::STM:
|
2009-08-04 01:43:45 +00:00
|
|
|
case ARM::t2LDM:
|
|
|
|
case ARM::t2STM:
|
2009-10-01 01:33:39 +00:00
|
|
|
return (MI->getNumOperands() - 5) * 4;
|
2007-01-19 07:51:42 +00:00
|
|
|
case ARM::FLDMS:
|
|
|
|
case ARM::FSTMS:
|
|
|
|
case ARM::FLDMD:
|
|
|
|
case ARM::FSTMD:
|
|
|
|
return ARM_AM::getAM5Offset(MI->getOperand(1).getImm()) * 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-09 23:11:34 +00:00
|
|
|
/// MergeBaseUpdateLSMultiple - Fold proceeding/trailing inc/dec of base
|
2007-01-19 07:51:42 +00:00
|
|
|
/// register into the LDM/STM/FLDM{D|S}/FSTM{D|S} op when possible:
|
|
|
|
///
|
|
|
|
/// stmia rn, <ra, rb, rc>
|
|
|
|
/// rn := rn + 4 * 3;
|
|
|
|
/// =>
|
|
|
|
/// stmia rn!, <ra, rb, rc>
|
|
|
|
///
|
|
|
|
/// rn := rn - 4 * 3;
|
|
|
|
/// ldmia rn, <ra, rb, rc>
|
|
|
|
/// =>
|
|
|
|
/// ldmdb rn!, <ra, rb, rc>
|
2009-07-09 23:11:34 +00:00
|
|
|
bool ARMLoadStoreOpt::MergeBaseUpdateLSMultiple(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
bool &Advance,
|
|
|
|
MachineBasicBlock::iterator &I) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
unsigned Base = MI->getOperand(0).getReg();
|
|
|
|
unsigned Bytes = getLSMultipleTransferSize(MI);
|
2007-07-05 07:18:20 +00:00
|
|
|
unsigned PredReg = 0;
|
2009-08-08 03:20:32 +00:00
|
|
|
ARMCC::CondCodes Pred = llvm::getInstrPredicate(MI, PredReg);
|
2007-01-19 07:51:42 +00:00
|
|
|
int Opcode = MI->getOpcode();
|
2009-07-09 23:11:34 +00:00
|
|
|
bool isAM4 = Opcode == ARM::LDM || Opcode == ARM::t2LDM ||
|
|
|
|
Opcode == ARM::STM || Opcode == ARM::t2STM;
|
2007-01-19 07:51:42 +00:00
|
|
|
|
|
|
|
if (isAM4) {
|
|
|
|
if (ARM_AM::getAM4WBFlag(MI->getOperand(1).getImm()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Can't use the updating AM4 sub-mode if the base register is also a dest
|
|
|
|
// register. e.g. ldmdb r0!, {r0, r1, r2}. The behavior is undefined.
|
2007-05-15 01:29:07 +00:00
|
|
|
for (unsigned i = 3, e = MI->getNumOperands(); i != e; ++i) {
|
2007-01-19 07:51:42 +00:00
|
|
|
if (MI->getOperand(i).getReg() == Base)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ARM_AM::AMSubMode Mode = ARM_AM::getAM4SubMode(MI->getOperand(1).getImm());
|
|
|
|
if (MBBI != MBB.begin()) {
|
|
|
|
MachineBasicBlock::iterator PrevMBBI = prior(MBBI);
|
|
|
|
if (Mode == ARM_AM::ia &&
|
2009-08-04 01:43:45 +00:00
|
|
|
isMatchingDecrement(PrevMBBI, Base, Bytes, 0, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).setImm(ARM_AM::getAM4ModeImm(ARM_AM::db, true));
|
2009-10-01 01:33:39 +00:00
|
|
|
MI->getOperand(4).setReg(Base);
|
|
|
|
MI->getOperand(4).setIsDef();
|
2007-01-19 07:51:42 +00:00
|
|
|
MBB.erase(PrevMBBI);
|
|
|
|
return true;
|
|
|
|
} else if (Mode == ARM_AM::ib &&
|
2009-08-04 01:43:45 +00:00
|
|
|
isMatchingDecrement(PrevMBBI, Base, Bytes, 0, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).setImm(ARM_AM::getAM4ModeImm(ARM_AM::da, true));
|
2009-10-01 01:33:39 +00:00
|
|
|
MI->getOperand(4).setReg(Base); // WB to base
|
|
|
|
MI->getOperand(4).setIsDef();
|
2007-01-19 07:51:42 +00:00
|
|
|
MBB.erase(PrevMBBI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MBBI != MBB.end()) {
|
|
|
|
MachineBasicBlock::iterator NextMBBI = next(MBBI);
|
|
|
|
if ((Mode == ARM_AM::ia || Mode == ARM_AM::ib) &&
|
2009-08-04 01:43:45 +00:00
|
|
|
isMatchingIncrement(NextMBBI, Base, Bytes, 0, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).setImm(ARM_AM::getAM4ModeImm(Mode, true));
|
2009-10-01 01:33:39 +00:00
|
|
|
MI->getOperand(4).setReg(Base); // WB to base
|
|
|
|
MI->getOperand(4).setIsDef();
|
2007-09-19 21:48:07 +00:00
|
|
|
if (NextMBBI == I) {
|
|
|
|
Advance = true;
|
|
|
|
++I;
|
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
MBB.erase(NextMBBI);
|
|
|
|
return true;
|
|
|
|
} else if ((Mode == ARM_AM::da || Mode == ARM_AM::db) &&
|
2009-08-04 01:43:45 +00:00
|
|
|
isMatchingDecrement(NextMBBI, Base, Bytes, 0, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).setImm(ARM_AM::getAM4ModeImm(Mode, true));
|
2009-10-01 01:33:39 +00:00
|
|
|
MI->getOperand(4).setReg(Base); // WB to base
|
|
|
|
MI->getOperand(4).setIsDef();
|
2007-09-19 21:48:07 +00:00
|
|
|
if (NextMBBI == I) {
|
|
|
|
Advance = true;
|
|
|
|
++I;
|
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
MBB.erase(NextMBBI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// FLDM{D|S}, FSTM{D|S} addressing mode 5 ops.
|
|
|
|
if (ARM_AM::getAM5WBFlag(MI->getOperand(1).getImm()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ARM_AM::AMSubMode Mode = ARM_AM::getAM5SubMode(MI->getOperand(1).getImm());
|
|
|
|
unsigned Offset = ARM_AM::getAM5Offset(MI->getOperand(1).getImm());
|
|
|
|
if (MBBI != MBB.begin()) {
|
|
|
|
MachineBasicBlock::iterator PrevMBBI = prior(MBBI);
|
|
|
|
if (Mode == ARM_AM::ia &&
|
2009-08-04 01:43:45 +00:00
|
|
|
isMatchingDecrement(PrevMBBI, Base, Bytes, 0, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).setImm(ARM_AM::getAM5Opc(ARM_AM::db, true, Offset));
|
2009-10-01 01:33:39 +00:00
|
|
|
MI->getOperand(4).setReg(Base); // WB to base
|
|
|
|
MI->getOperand(4).setIsDef();
|
2007-01-19 07:51:42 +00:00
|
|
|
MBB.erase(PrevMBBI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MBBI != MBB.end()) {
|
|
|
|
MachineBasicBlock::iterator NextMBBI = next(MBBI);
|
|
|
|
if (Mode == ARM_AM::ia &&
|
2009-08-04 01:43:45 +00:00
|
|
|
isMatchingIncrement(NextMBBI, Base, Bytes, 0, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MI->getOperand(1).setImm(ARM_AM::getAM5Opc(ARM_AM::ia, true, Offset));
|
2009-10-01 01:33:39 +00:00
|
|
|
MI->getOperand(4).setReg(Base); // WB to base
|
|
|
|
MI->getOperand(4).setIsDef();
|
2007-09-19 21:48:07 +00:00
|
|
|
if (NextMBBI == I) {
|
|
|
|
Advance = true;
|
|
|
|
++I;
|
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
MBB.erase(NextMBBI);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getPreIndexedLoadStoreOpcode(unsigned Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
case ARM::LDR: return ARM::LDR_PRE;
|
|
|
|
case ARM::STR: return ARM::STR_PRE;
|
|
|
|
case ARM::FLDS: return ARM::FLDMS;
|
|
|
|
case ARM::FLDD: return ARM::FLDMD;
|
|
|
|
case ARM::FSTS: return ARM::FSTMS;
|
|
|
|
case ARM::FSTD: return ARM::FSTMD;
|
2009-07-09 23:11:34 +00:00
|
|
|
case ARM::t2LDRi8:
|
|
|
|
case ARM::t2LDRi12:
|
|
|
|
return ARM::t2LDR_PRE;
|
|
|
|
case ARM::t2STRi8:
|
|
|
|
case ARM::t2STRi12:
|
|
|
|
return ARM::t2STR_PRE;
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unhandled opcode!");
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getPostIndexedLoadStoreOpcode(unsigned Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
case ARM::LDR: return ARM::LDR_POST;
|
|
|
|
case ARM::STR: return ARM::STR_POST;
|
|
|
|
case ARM::FLDS: return ARM::FLDMS;
|
|
|
|
case ARM::FLDD: return ARM::FLDMD;
|
|
|
|
case ARM::FSTS: return ARM::FSTMS;
|
|
|
|
case ARM::FSTD: return ARM::FSTMD;
|
2009-07-09 23:11:34 +00:00
|
|
|
case ARM::t2LDRi8:
|
|
|
|
case ARM::t2LDRi12:
|
|
|
|
return ARM::t2LDR_POST;
|
|
|
|
case ARM::t2STRi8:
|
|
|
|
case ARM::t2STRi12:
|
|
|
|
return ARM::t2STR_POST;
|
2009-07-14 16:55:14 +00:00
|
|
|
default: llvm_unreachable("Unhandled opcode!");
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-09 23:11:34 +00:00
|
|
|
/// MergeBaseUpdateLoadStore - Fold proceeding/trailing inc/dec of base
|
2007-01-19 07:51:42 +00:00
|
|
|
/// register into the LDR/STR/FLD{D|S}/FST{D|S} op when possible:
|
2009-07-09 23:11:34 +00:00
|
|
|
bool ARMLoadStoreOpt::MergeBaseUpdateLoadStore(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator MBBI,
|
|
|
|
const TargetInstrInfo *TII,
|
|
|
|
bool &Advance,
|
|
|
|
MachineBasicBlock::iterator &I) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
unsigned Base = MI->getOperand(1).getReg();
|
2007-03-06 21:59:20 +00:00
|
|
|
bool BaseKill = MI->getOperand(1).isKill();
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned Bytes = getLSMultipleTransferSize(MI);
|
|
|
|
int Opcode = MI->getOpcode();
|
2009-02-13 02:25:56 +00:00
|
|
|
DebugLoc dl = MI->getDebugLoc();
|
2009-08-04 01:43:45 +00:00
|
|
|
bool isAM5 = Opcode == ARM::FLDD || Opcode == ARM::FLDS ||
|
|
|
|
Opcode == ARM::FSTD || Opcode == ARM::FSTS;
|
2007-01-19 07:51:42 +00:00
|
|
|
bool isAM2 = Opcode == ARM::LDR || Opcode == ARM::STR;
|
2009-07-09 23:11:34 +00:00
|
|
|
if (isAM2 && ARM_AM::getAM2Offset(MI->getOperand(3).getImm()) != 0)
|
|
|
|
return false;
|
2009-08-04 01:43:45 +00:00
|
|
|
else if (isAM5 && ARM_AM::getAM5Offset(MI->getOperand(2).getImm()) != 0)
|
2007-01-19 07:51:42 +00:00
|
|
|
return false;
|
2009-08-04 01:43:45 +00:00
|
|
|
else if (isT2i32Load(Opcode) || isT2i32Store(Opcode))
|
|
|
|
if (MI->getOperand(2).getImm() != 0)
|
|
|
|
return false;
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2009-07-09 23:11:34 +00:00
|
|
|
bool isLd = isi32Load(Opcode) || Opcode == ARM::FLDS || Opcode == ARM::FLDD;
|
2007-01-19 07:51:42 +00:00
|
|
|
// Can't do the merge if the destination register is the same as the would-be
|
|
|
|
// writeback register.
|
|
|
|
if (isLd && MI->getOperand(0).getReg() == Base)
|
|
|
|
return false;
|
|
|
|
|
2007-07-05 07:18:20 +00:00
|
|
|
unsigned PredReg = 0;
|
2009-08-08 03:20:32 +00:00
|
|
|
ARMCC::CondCodes Pred = llvm::getInstrPredicate(MI, PredReg);
|
2007-01-19 07:51:42 +00:00
|
|
|
bool DoMerge = false;
|
|
|
|
ARM_AM::AddrOpc AddSub = ARM_AM::add;
|
|
|
|
unsigned NewOpc = 0;
|
2009-08-04 01:43:45 +00:00
|
|
|
// AM2 - 12 bits, thumb2 - 8 bits.
|
|
|
|
unsigned Limit = isAM5 ? 0 : (isAM2 ? 0x1000 : 0x100);
|
2007-01-19 07:51:42 +00:00
|
|
|
if (MBBI != MBB.begin()) {
|
|
|
|
MachineBasicBlock::iterator PrevMBBI = prior(MBBI);
|
2009-08-04 01:43:45 +00:00
|
|
|
if (isMatchingDecrement(PrevMBBI, Base, Bytes, Limit, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
DoMerge = true;
|
|
|
|
AddSub = ARM_AM::sub;
|
|
|
|
NewOpc = getPreIndexedLoadStoreOpcode(Opcode);
|
2009-08-04 01:43:45 +00:00
|
|
|
} else if (!isAM5 &&
|
|
|
|
isMatchingIncrement(PrevMBBI, Base, Bytes, Limit,Pred,PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
DoMerge = true;
|
|
|
|
NewOpc = getPreIndexedLoadStoreOpcode(Opcode);
|
|
|
|
}
|
|
|
|
if (DoMerge)
|
|
|
|
MBB.erase(PrevMBBI);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoMerge && MBBI != MBB.end()) {
|
|
|
|
MachineBasicBlock::iterator NextMBBI = next(MBBI);
|
2009-08-04 01:43:45 +00:00
|
|
|
if (!isAM5 &&
|
|
|
|
isMatchingDecrement(NextMBBI, Base, Bytes, Limit, Pred, PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
DoMerge = true;
|
|
|
|
AddSub = ARM_AM::sub;
|
|
|
|
NewOpc = getPostIndexedLoadStoreOpcode(Opcode);
|
2009-08-04 01:43:45 +00:00
|
|
|
} else if (isMatchingIncrement(NextMBBI, Base, Bytes, Limit,Pred,PredReg)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
DoMerge = true;
|
|
|
|
NewOpc = getPostIndexedLoadStoreOpcode(Opcode);
|
|
|
|
}
|
2007-09-19 21:48:07 +00:00
|
|
|
if (DoMerge) {
|
|
|
|
if (NextMBBI == I) {
|
|
|
|
Advance = true;
|
|
|
|
++I;
|
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
MBB.erase(NextMBBI);
|
2007-09-19 21:48:07 +00:00
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!DoMerge)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool isDPR = NewOpc == ARM::FLDMD || NewOpc == ARM::FSTMD;
|
2009-08-04 21:12:13 +00:00
|
|
|
unsigned Offset = 0;
|
|
|
|
if (isAM5)
|
|
|
|
Offset = ARM_AM::getAM5Opc((AddSub == ARM_AM::sub)
|
|
|
|
? ARM_AM::db
|
|
|
|
: ARM_AM::ia, true, (isDPR ? 2 : 1));
|
|
|
|
else if (isAM2)
|
|
|
|
Offset = ARM_AM::getAM2Opc(AddSub, Bytes, ARM_AM::no_shift);
|
|
|
|
else
|
|
|
|
Offset = AddSub == ARM_AM::sub ? -Bytes : Bytes;
|
2007-01-19 07:51:42 +00:00
|
|
|
if (isLd) {
|
2009-08-04 01:43:45 +00:00
|
|
|
if (isAM5)
|
2007-05-15 01:29:07 +00:00
|
|
|
// FLDMS, FLDMD
|
2009-02-13 02:25:56 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(NewOpc))
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(Base, getKillRegState(BaseKill))
|
2007-07-05 07:18:20 +00:00
|
|
|
.addImm(Offset).addImm(Pred).addReg(PredReg)
|
2009-10-01 01:33:39 +00:00
|
|
|
.addReg(Base, getDefRegState(true)) // WB base register
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(MI->getOperand(0).getReg(), RegState::Define);
|
2009-08-04 01:43:45 +00:00
|
|
|
else if (isAM2)
|
|
|
|
// LDR_PRE, LDR_POST,
|
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(NewOpc), MI->getOperand(0).getReg())
|
|
|
|
.addReg(Base, RegState::Define)
|
2007-07-05 07:18:20 +00:00
|
|
|
.addReg(Base).addReg(0).addImm(Offset).addImm(Pred).addReg(PredReg);
|
2007-01-19 07:51:42 +00:00
|
|
|
else
|
2009-08-04 01:43:45 +00:00
|
|
|
// t2LDR_PRE, t2LDR_POST
|
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(NewOpc), MI->getOperand(0).getReg())
|
|
|
|
.addReg(Base, RegState::Define)
|
|
|
|
.addReg(Base).addImm(Offset).addImm(Pred).addReg(PredReg);
|
|
|
|
} else {
|
|
|
|
MachineOperand &MO = MI->getOperand(0);
|
|
|
|
if (isAM5)
|
2007-05-15 01:29:07 +00:00
|
|
|
// FSTMS, FSTMD
|
2009-02-13 02:25:56 +00:00
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(NewOpc)).addReg(Base).addImm(Offset)
|
2007-07-05 07:18:20 +00:00
|
|
|
.addImm(Pred).addReg(PredReg)
|
2009-10-01 01:33:39 +00:00
|
|
|
.addReg(Base, getDefRegState(true)) // WB base register
|
2009-05-13 21:33:08 +00:00
|
|
|
.addReg(MO.getReg(), getKillRegState(MO.isKill()));
|
2009-08-04 01:43:45 +00:00
|
|
|
else if (isAM2)
|
|
|
|
// STR_PRE, STR_POST
|
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(NewOpc), Base)
|
|
|
|
.addReg(MO.getReg(), getKillRegState(MO.isKill()))
|
|
|
|
.addReg(Base).addReg(0).addImm(Offset).addImm(Pred).addReg(PredReg);
|
|
|
|
else
|
|
|
|
// t2STR_PRE, t2STR_POST
|
|
|
|
BuildMI(MBB, MBBI, dl, TII->get(NewOpc), Base)
|
|
|
|
.addReg(MO.getReg(), getKillRegState(MO.isKill()))
|
|
|
|
.addReg(Base).addImm(Offset).addImm(Pred).addReg(PredReg);
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
MBB.erase(MBBI);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-03-06 18:02:41 +00:00
|
|
|
/// isMemoryOp - Returns true if instruction is a memory operations (that this
|
|
|
|
/// pass is capable of operating on).
|
2009-07-09 23:11:34 +00:00
|
|
|
static bool isMemoryOp(const MachineInstr *MI) {
|
2007-03-06 18:02:41 +00:00
|
|
|
int Opcode = MI->getOpcode();
|
|
|
|
switch (Opcode) {
|
|
|
|
default: break;
|
|
|
|
case ARM::LDR:
|
|
|
|
case ARM::STR:
|
2008-10-03 15:45:36 +00:00
|
|
|
return MI->getOperand(1).isReg() && MI->getOperand(2).getReg() == 0;
|
2007-03-06 18:02:41 +00:00
|
|
|
case ARM::FLDS:
|
|
|
|
case ARM::FSTS:
|
2008-10-03 15:45:36 +00:00
|
|
|
return MI->getOperand(1).isReg();
|
2007-03-06 18:02:41 +00:00
|
|
|
case ARM::FLDD:
|
|
|
|
case ARM::FSTD:
|
2008-10-03 15:45:36 +00:00
|
|
|
return MI->getOperand(1).isReg();
|
2009-07-09 23:11:34 +00:00
|
|
|
case ARM::t2LDRi8:
|
|
|
|
case ARM::t2LDRi12:
|
|
|
|
case ARM::t2STRi8:
|
|
|
|
case ARM::t2STRi12:
|
2009-09-27 09:46:04 +00:00
|
|
|
return MI->getOperand(1).isReg();
|
2007-03-06 18:02:41 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-08 02:55:08 +00:00
|
|
|
/// AdvanceRS - Advance register scavenger to just before the earliest memory
|
|
|
|
/// op that is being merged.
|
|
|
|
void ARMLoadStoreOpt::AdvanceRS(MachineBasicBlock &MBB, MemOpQueue &MemOps) {
|
|
|
|
MachineBasicBlock::iterator Loc = MemOps[0].MBBI;
|
|
|
|
unsigned Position = MemOps[0].Position;
|
|
|
|
for (unsigned i = 1, e = MemOps.size(); i != e; ++i) {
|
|
|
|
if (MemOps[i].Position < Position) {
|
|
|
|
Position = MemOps[i].Position;
|
|
|
|
Loc = MemOps[i].MBBI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Loc != MBB.begin())
|
|
|
|
RS->forward(prior(Loc));
|
|
|
|
}
|
|
|
|
|
2009-06-13 09:12:55 +00:00
|
|
|
static int getMemoryOpOffset(const MachineInstr *MI) {
|
|
|
|
int Opcode = MI->getOpcode();
|
|
|
|
bool isAM2 = Opcode == ARM::LDR || Opcode == ARM::STR;
|
2009-06-15 08:28:29 +00:00
|
|
|
bool isAM3 = Opcode == ARM::LDRD || Opcode == ARM::STRD;
|
2009-06-13 09:12:55 +00:00
|
|
|
unsigned NumOperands = MI->getDesc().getNumOperands();
|
|
|
|
unsigned OffField = MI->getOperand(NumOperands-3).getImm();
|
2009-07-09 23:11:34 +00:00
|
|
|
|
|
|
|
if (Opcode == ARM::t2LDRi12 || Opcode == ARM::t2LDRi8 ||
|
|
|
|
Opcode == ARM::t2STRi12 || Opcode == ARM::t2STRi8 ||
|
|
|
|
Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8)
|
|
|
|
return OffField;
|
|
|
|
|
2009-06-13 09:12:55 +00:00
|
|
|
int Offset = isAM2
|
2009-06-15 08:28:29 +00:00
|
|
|
? ARM_AM::getAM2Offset(OffField)
|
|
|
|
: (isAM3 ? ARM_AM::getAM3Offset(OffField)
|
|
|
|
: ARM_AM::getAM5Offset(OffField) * 4);
|
2009-06-13 09:12:55 +00:00
|
|
|
if (isAM2) {
|
|
|
|
if (ARM_AM::getAM2Op(OffField) == ARM_AM::sub)
|
|
|
|
Offset = -Offset;
|
2009-06-15 08:28:29 +00:00
|
|
|
} else if (isAM3) {
|
|
|
|
if (ARM_AM::getAM3Op(OffField) == ARM_AM::sub)
|
|
|
|
Offset = -Offset;
|
2009-06-13 09:12:55 +00:00
|
|
|
} else {
|
|
|
|
if (ARM_AM::getAM5Op(OffField) == ARM_AM::sub)
|
|
|
|
Offset = -Offset;
|
|
|
|
}
|
|
|
|
return Offset;
|
|
|
|
}
|
|
|
|
|
2009-06-15 08:28:29 +00:00
|
|
|
static void InsertLDR_STR(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI,
|
|
|
|
int OffImm, bool isDef,
|
|
|
|
DebugLoc dl, unsigned NewOpc,
|
2009-09-27 09:46:04 +00:00
|
|
|
unsigned Reg, bool RegDeadKill, bool RegUndef,
|
|
|
|
unsigned BaseReg, bool BaseKill, bool BaseUndef,
|
|
|
|
unsigned OffReg, bool OffKill, bool OffUndef,
|
2009-06-15 08:28:29 +00:00
|
|
|
ARMCC::CondCodes Pred, unsigned PredReg,
|
2009-09-27 09:46:04 +00:00
|
|
|
const TargetInstrInfo *TII, bool isT2) {
|
|
|
|
int Offset = OffImm;
|
|
|
|
if (!isT2) {
|
|
|
|
if (OffImm < 0)
|
|
|
|
Offset = ARM_AM::getAM2Opc(ARM_AM::sub, -OffImm, ARM_AM::no_shift);
|
|
|
|
else
|
|
|
|
Offset = ARM_AM::getAM2Opc(ARM_AM::add, OffImm, ARM_AM::no_shift);
|
|
|
|
}
|
|
|
|
if (isDef) {
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
|
|
|
|
TII->get(NewOpc))
|
2009-06-19 01:59:04 +00:00
|
|
|
.addReg(Reg, getDefRegState(true) | getDeadRegState(RegDeadKill))
|
2009-09-27 09:46:04 +00:00
|
|
|
.addReg(BaseReg, getKillRegState(BaseKill)|getUndefRegState(BaseUndef));
|
|
|
|
if (!isT2)
|
|
|
|
MIB.addReg(OffReg, getKillRegState(OffKill)|getUndefRegState(OffUndef));
|
|
|
|
MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
|
|
|
|
} else {
|
|
|
|
MachineInstrBuilder MIB = BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
|
|
|
|
TII->get(NewOpc))
|
|
|
|
.addReg(Reg, getKillRegState(RegDeadKill) | getUndefRegState(RegUndef))
|
|
|
|
.addReg(BaseReg, getKillRegState(BaseKill)|getUndefRegState(BaseUndef));
|
|
|
|
if (!isT2)
|
|
|
|
MIB.addReg(OffReg, getKillRegState(OffKill)|getUndefRegState(OffUndef));
|
|
|
|
MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
|
|
|
|
}
|
2009-06-15 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMLoadStoreOpt::FixInvalidRegPairOp(MachineBasicBlock &MBB,
|
|
|
|
MachineBasicBlock::iterator &MBBI) {
|
|
|
|
MachineInstr *MI = &*MBBI;
|
|
|
|
unsigned Opcode = MI->getOpcode();
|
2009-09-27 09:46:04 +00:00
|
|
|
if (Opcode == ARM::LDRD || Opcode == ARM::STRD ||
|
|
|
|
Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8) {
|
2009-06-15 08:28:29 +00:00
|
|
|
unsigned EvenReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned OddReg = MI->getOperand(1).getReg();
|
|
|
|
unsigned EvenRegNum = TRI->getDwarfRegNum(EvenReg, false);
|
|
|
|
unsigned OddRegNum = TRI->getDwarfRegNum(OddReg, false);
|
|
|
|
if ((EvenRegNum & 1) == 0 && (EvenRegNum + 1) == OddRegNum)
|
|
|
|
return false;
|
|
|
|
|
2009-09-27 09:46:04 +00:00
|
|
|
bool isT2 = Opcode == ARM::t2LDRDi8 || Opcode == ARM::t2STRDi8;
|
|
|
|
bool isLd = Opcode == ARM::LDRD || Opcode == ARM::t2LDRDi8;
|
2009-06-19 01:59:04 +00:00
|
|
|
bool EvenDeadKill = isLd ?
|
|
|
|
MI->getOperand(0).isDead() : MI->getOperand(0).isKill();
|
2009-09-27 09:46:04 +00:00
|
|
|
bool EvenUndef = MI->getOperand(0).isUndef();
|
2009-06-19 01:59:04 +00:00
|
|
|
bool OddDeadKill = isLd ?
|
|
|
|
MI->getOperand(1).isDead() : MI->getOperand(1).isKill();
|
2009-09-27 09:46:04 +00:00
|
|
|
bool OddUndef = MI->getOperand(1).isUndef();
|
2009-06-15 08:28:29 +00:00
|
|
|
const MachineOperand &BaseOp = MI->getOperand(2);
|
|
|
|
unsigned BaseReg = BaseOp.getReg();
|
|
|
|
bool BaseKill = BaseOp.isKill();
|
2009-09-27 09:46:04 +00:00
|
|
|
bool BaseUndef = BaseOp.isUndef();
|
|
|
|
unsigned OffReg = isT2 ? 0 : MI->getOperand(3).getReg();
|
|
|
|
bool OffKill = isT2 ? false : MI->getOperand(3).isKill();
|
|
|
|
bool OffUndef = isT2 ? false : MI->getOperand(3).isUndef();
|
2009-06-15 08:28:29 +00:00
|
|
|
int OffImm = getMemoryOpOffset(MI);
|
|
|
|
unsigned PredReg = 0;
|
2009-08-08 03:20:32 +00:00
|
|
|
ARMCC::CondCodes Pred = llvm::getInstrPredicate(MI, PredReg);
|
2009-06-15 08:28:29 +00:00
|
|
|
|
|
|
|
if (OddRegNum > EvenRegNum && OffReg == 0 && OffImm == 0) {
|
|
|
|
// Ascending register numbers and no offset. It's safe to change it to a
|
|
|
|
// ldm or stm.
|
2009-09-27 09:46:04 +00:00
|
|
|
unsigned NewOpc = (isLd)
|
|
|
|
? (isT2 ? ARM::t2LDM : ARM::LDM)
|
|
|
|
: (isT2 ? ARM::t2STM : ARM::STM);
|
2009-06-18 02:04:01 +00:00
|
|
|
if (isLd) {
|
|
|
|
BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(NewOpc))
|
|
|
|
.addReg(BaseReg, getKillRegState(BaseKill))
|
|
|
|
.addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
|
|
|
|
.addImm(Pred).addReg(PredReg)
|
2009-10-01 01:33:39 +00:00
|
|
|
.addReg(0)
|
2009-06-19 01:59:04 +00:00
|
|
|
.addReg(EvenReg, getDefRegState(isLd) | getDeadRegState(EvenDeadKill))
|
2009-10-01 01:33:39 +00:00
|
|
|
.addReg(OddReg, getDefRegState(isLd) | getDeadRegState(OddDeadKill));
|
2009-06-18 02:04:01 +00:00
|
|
|
++NumLDRD2LDM;
|
|
|
|
} else {
|
|
|
|
BuildMI(MBB, MBBI, MBBI->getDebugLoc(), TII->get(NewOpc))
|
|
|
|
.addReg(BaseReg, getKillRegState(BaseKill))
|
|
|
|
.addImm(ARM_AM::getAM4ModeImm(ARM_AM::ia))
|
|
|
|
.addImm(Pred).addReg(PredReg)
|
2009-10-01 01:33:39 +00:00
|
|
|
.addReg(0)
|
2009-09-27 09:46:04 +00:00
|
|
|
.addReg(EvenReg,
|
|
|
|
getKillRegState(EvenDeadKill) | getUndefRegState(EvenUndef))
|
|
|
|
.addReg(OddReg,
|
2009-10-01 01:33:39 +00:00
|
|
|
getKillRegState(OddDeadKill) | getUndefRegState(OddUndef));
|
2009-06-18 02:04:01 +00:00
|
|
|
++NumSTRD2STM;
|
|
|
|
}
|
2009-06-15 08:28:29 +00:00
|
|
|
} else {
|
|
|
|
// Split into two instructions.
|
2009-09-27 09:46:04 +00:00
|
|
|
assert((!isT2 || !OffReg) &&
|
|
|
|
"Thumb2 ldrd / strd does not encode offset register!");
|
|
|
|
unsigned NewOpc = (isLd)
|
|
|
|
? (isT2 ? (OffImm < 0 ? ARM::t2LDRi8 : ARM::t2LDRi12) : ARM::LDR)
|
|
|
|
: (isT2 ? (OffImm < 0 ? ARM::t2STRi8 : ARM::t2STRi12) : ARM::STR);
|
2009-06-15 08:28:29 +00:00
|
|
|
DebugLoc dl = MBBI->getDebugLoc();
|
|
|
|
// If this is a load and base register is killed, it may have been
|
|
|
|
// re-defed by the load, make sure the first load does not clobber it.
|
2009-06-18 02:04:01 +00:00
|
|
|
if (isLd &&
|
2009-06-15 08:28:29 +00:00
|
|
|
(BaseKill || OffKill) &&
|
|
|
|
(TRI->regsOverlap(EvenReg, BaseReg) ||
|
|
|
|
(OffReg && TRI->regsOverlap(EvenReg, OffReg)))) {
|
|
|
|
assert(!TRI->regsOverlap(OddReg, BaseReg) &&
|
|
|
|
(!OffReg || !TRI->regsOverlap(OddReg, OffReg)));
|
2009-09-27 09:46:04 +00:00
|
|
|
InsertLDR_STR(MBB, MBBI, OffImm+4, isLd, dl, NewOpc,
|
|
|
|
OddReg, OddDeadKill, false,
|
|
|
|
BaseReg, false, BaseUndef, OffReg, false, OffUndef,
|
|
|
|
Pred, PredReg, TII, isT2);
|
|
|
|
InsertLDR_STR(MBB, MBBI, OffImm, isLd, dl, NewOpc,
|
|
|
|
EvenReg, EvenDeadKill, false,
|
|
|
|
BaseReg, BaseKill, BaseUndef, OffReg, OffKill, OffUndef,
|
|
|
|
Pred, PredReg, TII, isT2);
|
2009-06-15 08:28:29 +00:00
|
|
|
} else {
|
2009-06-19 01:59:04 +00:00
|
|
|
InsertLDR_STR(MBB, MBBI, OffImm, isLd, dl, NewOpc,
|
2009-09-27 09:46:04 +00:00
|
|
|
EvenReg, EvenDeadKill, EvenUndef,
|
|
|
|
BaseReg, false, BaseUndef, OffReg, false, OffUndef,
|
|
|
|
Pred, PredReg, TII, isT2);
|
2009-06-19 01:59:04 +00:00
|
|
|
InsertLDR_STR(MBB, MBBI, OffImm+4, isLd, dl, NewOpc,
|
2009-09-27 09:46:04 +00:00
|
|
|
OddReg, OddDeadKill, OddUndef,
|
|
|
|
BaseReg, BaseKill, BaseUndef, OffReg, OffKill, OffUndef,
|
|
|
|
Pred, PredReg, TII, isT2);
|
2009-06-15 08:28:29 +00:00
|
|
|
}
|
2009-06-18 02:04:01 +00:00
|
|
|
if (isLd)
|
|
|
|
++NumLDRD2LDR;
|
|
|
|
else
|
|
|
|
++NumSTRD2STR;
|
2009-06-15 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MBBI = prior(MBBI);
|
|
|
|
MBB.erase(MI);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
/// LoadStoreMultipleOpti - An optimization pass to turn multiple LDR / STR
|
|
|
|
/// ops of the same base and incrementing offset into LDM / STM ops.
|
|
|
|
bool ARMLoadStoreOpt::LoadStoreMultipleOpti(MachineBasicBlock &MBB) {
|
|
|
|
unsigned NumMerges = 0;
|
|
|
|
unsigned NumMemOps = 0;
|
|
|
|
MemOpQueue MemOps;
|
|
|
|
unsigned CurrBase = 0;
|
|
|
|
int CurrOpc = -1;
|
|
|
|
unsigned CurrSize = 0;
|
2007-05-15 01:29:07 +00:00
|
|
|
ARMCC::CondCodes CurrPred = ARMCC::AL;
|
2007-07-05 07:18:20 +00:00
|
|
|
unsigned CurrPredReg = 0;
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned Position = 0;
|
2009-06-05 17:56:14 +00:00
|
|
|
SmallVector<MachineBasicBlock::iterator,4> Merges;
|
2007-03-06 18:02:41 +00:00
|
|
|
|
2007-03-07 02:38:05 +00:00
|
|
|
RS->enterBasicBlock(&MBB);
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
|
|
|
|
while (MBBI != E) {
|
2009-06-15 08:28:29 +00:00
|
|
|
if (FixInvalidRegPairOp(MBB, MBBI))
|
|
|
|
continue;
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
bool Advance = false;
|
|
|
|
bool TryMerge = false;
|
|
|
|
bool Clobber = false;
|
|
|
|
|
2007-03-06 18:02:41 +00:00
|
|
|
bool isMemOp = isMemoryOp(MBBI);
|
2007-01-19 07:51:42 +00:00
|
|
|
if (isMemOp) {
|
2007-03-06 18:02:41 +00:00
|
|
|
int Opcode = MBBI->getOpcode();
|
|
|
|
unsigned Size = getLSMultipleTransferSize(MBBI);
|
2007-01-19 07:51:42 +00:00
|
|
|
unsigned Base = MBBI->getOperand(1).getReg();
|
2007-07-05 07:18:20 +00:00
|
|
|
unsigned PredReg = 0;
|
2009-08-08 03:20:32 +00:00
|
|
|
ARMCC::CondCodes Pred = llvm::getInstrPredicate(MBBI, PredReg);
|
2009-06-13 09:12:55 +00:00
|
|
|
int Offset = getMemoryOpOffset(MBBI);
|
2007-01-19 07:51:42 +00:00
|
|
|
// Watch out for:
|
|
|
|
// r4 := ldr [r5]
|
|
|
|
// r5 := ldr [r5, #4]
|
|
|
|
// r6 := ldr [r5, #8]
|
|
|
|
//
|
|
|
|
// The second ldr has effectively broken the chain even though it
|
|
|
|
// looks like the later ldr(s) use the same base register. Try to
|
|
|
|
// merge the ldr's so far, including this one. But don't try to
|
|
|
|
// combine the following ldr(s).
|
2009-07-09 23:11:34 +00:00
|
|
|
Clobber = (isi32Load(Opcode) && Base == MBBI->getOperand(0).getReg());
|
2007-01-19 07:51:42 +00:00
|
|
|
if (CurrBase == 0 && !Clobber) {
|
|
|
|
// Start of a new chain.
|
|
|
|
CurrBase = Base;
|
|
|
|
CurrOpc = Opcode;
|
|
|
|
CurrSize = Size;
|
2007-05-15 01:29:07 +00:00
|
|
|
CurrPred = Pred;
|
2007-07-05 07:18:20 +00:00
|
|
|
CurrPredReg = PredReg;
|
2007-01-19 07:51:42 +00:00
|
|
|
MemOps.push_back(MemOpQueueEntry(Offset, Position, MBBI));
|
|
|
|
NumMemOps++;
|
|
|
|
Advance = true;
|
|
|
|
} else {
|
|
|
|
if (Clobber) {
|
|
|
|
TryMerge = true;
|
|
|
|
Advance = true;
|
|
|
|
}
|
|
|
|
|
2007-05-15 01:29:07 +00:00
|
|
|
if (CurrOpc == Opcode && CurrBase == Base && CurrPred == Pred) {
|
2007-07-05 07:18:20 +00:00
|
|
|
// No need to match PredReg.
|
2007-01-19 07:51:42 +00:00
|
|
|
// Continue adding to the queue.
|
|
|
|
if (Offset > MemOps.back().Offset) {
|
|
|
|
MemOps.push_back(MemOpQueueEntry(Offset, Position, MBBI));
|
|
|
|
NumMemOps++;
|
|
|
|
Advance = true;
|
|
|
|
} else {
|
|
|
|
for (MemOpQueueIter I = MemOps.begin(), E = MemOps.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (Offset < I->Offset) {
|
|
|
|
MemOps.insert(I, MemOpQueueEntry(Offset, Position, MBBI));
|
|
|
|
NumMemOps++;
|
|
|
|
Advance = true;
|
|
|
|
break;
|
|
|
|
} else if (Offset == I->Offset) {
|
|
|
|
// Collision! This can't be merged!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Advance) {
|
|
|
|
++Position;
|
|
|
|
++MBBI;
|
2009-10-22 06:47:35 +00:00
|
|
|
if (MBBI == E)
|
|
|
|
// Reach the end of the block, try merging the memory instructions.
|
|
|
|
TryMerge = true;
|
2007-01-19 07:51:42 +00:00
|
|
|
} else
|
|
|
|
TryMerge = true;
|
|
|
|
|
|
|
|
if (TryMerge) {
|
|
|
|
if (NumMemOps > 1) {
|
2007-03-07 02:38:05 +00:00
|
|
|
// Try to find a free register to use as a new base in case it's needed.
|
|
|
|
// First advance to the instruction just before the start of the chain.
|
2007-03-08 02:55:08 +00:00
|
|
|
AdvanceRS(MBB, MemOps);
|
2009-08-18 21:14:54 +00:00
|
|
|
// Find a scratch register.
|
2009-09-11 19:49:06 +00:00
|
|
|
unsigned Scratch = RS->FindUnusedReg(ARM::GPRRegisterClass);
|
2007-03-07 02:38:05 +00:00
|
|
|
// Process the load / store instructions.
|
|
|
|
RS->forward(prior(MBBI));
|
|
|
|
|
|
|
|
// Merge ops.
|
2009-06-05 17:56:14 +00:00
|
|
|
Merges.clear();
|
|
|
|
MergeLDR_STR(MBB, 0, CurrBase, CurrOpc, CurrSize,
|
|
|
|
CurrPred, CurrPredReg, Scratch, MemOps, Merges);
|
2007-03-07 02:38:05 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
// Try folding preceeding/trailing base inc/dec into the generated
|
|
|
|
// LDM/STM ops.
|
2009-06-05 17:56:14 +00:00
|
|
|
for (unsigned i = 0, e = Merges.size(); i < e; ++i)
|
2009-07-09 23:11:34 +00:00
|
|
|
if (MergeBaseUpdateLSMultiple(MBB, Merges[i], Advance, MBBI))
|
2009-06-03 06:14:58 +00:00
|
|
|
++NumMerges;
|
2009-06-05 17:56:14 +00:00
|
|
|
NumMerges += Merges.size();
|
2007-01-19 07:51:42 +00:00
|
|
|
|
2007-03-07 02:38:05 +00:00
|
|
|
// Try folding preceeding/trailing base inc/dec into those load/store
|
|
|
|
// that were not merged to form LDM/STM ops.
|
|
|
|
for (unsigned i = 0; i != NumMemOps; ++i)
|
|
|
|
if (!MemOps[i].Merged)
|
2009-07-09 23:11:34 +00:00
|
|
|
if (MergeBaseUpdateLoadStore(MBB, MemOps[i].MBBI, TII,Advance,MBBI))
|
2009-06-03 06:14:58 +00:00
|
|
|
++NumMerges;
|
2007-03-07 02:38:05 +00:00
|
|
|
|
2009-08-11 15:33:49 +00:00
|
|
|
// RS may be pointing to an instruction that's deleted.
|
2007-03-07 02:38:05 +00:00
|
|
|
RS->skipTo(prior(MBBI));
|
2009-06-04 01:15:28 +00:00
|
|
|
} else if (NumMemOps == 1) {
|
|
|
|
// Try folding preceeding/trailing base inc/dec into the single
|
|
|
|
// load/store.
|
2009-07-09 23:11:34 +00:00
|
|
|
if (MergeBaseUpdateLoadStore(MBB, MemOps[0].MBBI, TII, Advance, MBBI)) {
|
2009-06-04 01:15:28 +00:00
|
|
|
++NumMerges;
|
|
|
|
RS->forward(prior(MBBI));
|
|
|
|
}
|
2007-03-07 02:38:05 +00:00
|
|
|
}
|
2007-01-19 07:51:42 +00:00
|
|
|
|
|
|
|
CurrBase = 0;
|
|
|
|
CurrOpc = -1;
|
2007-05-15 01:29:07 +00:00
|
|
|
CurrSize = 0;
|
|
|
|
CurrPred = ARMCC::AL;
|
2007-07-05 07:18:20 +00:00
|
|
|
CurrPredReg = 0;
|
2007-01-19 07:51:42 +00:00
|
|
|
if (NumMemOps) {
|
|
|
|
MemOps.clear();
|
|
|
|
NumMemOps = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If iterator hasn't been advanced and this is not a memory op, skip it.
|
|
|
|
// It can't start a new chain anyway.
|
|
|
|
if (!Advance && !isMemOp && MBBI != E) {
|
|
|
|
++Position;
|
|
|
|
++MBBI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NumMerges > 0;
|
|
|
|
}
|
|
|
|
|
2009-06-13 09:12:55 +00:00
|
|
|
namespace {
|
|
|
|
struct OffsetCompare {
|
|
|
|
bool operator()(const MachineInstr *LHS, const MachineInstr *RHS) const {
|
|
|
|
int LOffset = getMemoryOpOffset(LHS);
|
|
|
|
int ROffset = getMemoryOpOffset(RHS);
|
|
|
|
assert(LHS == RHS || LOffset != ROffset);
|
|
|
|
return LOffset > ROffset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
/// MergeReturnIntoLDM - If this is a exit BB, try merging the return op
|
|
|
|
/// (bx lr) into the preceeding stack restore so it directly restore the value
|
|
|
|
/// of LR into pc.
|
|
|
|
/// ldmfd sp!, {r7, lr}
|
|
|
|
/// bx lr
|
|
|
|
/// =>
|
|
|
|
/// ldmfd sp!, {r7, pc}
|
|
|
|
bool ARMLoadStoreOpt::MergeReturnIntoLDM(MachineBasicBlock &MBB) {
|
|
|
|
if (MBB.empty()) return false;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator MBBI = prior(MBB.end());
|
2009-07-09 23:11:34 +00:00
|
|
|
if (MBBI != MBB.begin() &&
|
2009-07-11 06:43:01 +00:00
|
|
|
(MBBI->getOpcode() == ARM::BX_RET || MBBI->getOpcode() == ARM::tBX_RET)) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineInstr *PrevMI = prior(MBBI);
|
2009-07-09 23:11:34 +00:00
|
|
|
if (PrevMI->getOpcode() == ARM::LDM || PrevMI->getOpcode() == ARM::t2LDM) {
|
2007-01-19 07:51:42 +00:00
|
|
|
MachineOperand &MO = PrevMI->getOperand(PrevMI->getNumOperands()-1);
|
2009-08-04 01:43:45 +00:00
|
|
|
if (MO.getReg() != ARM::LR)
|
|
|
|
return false;
|
|
|
|
unsigned NewOpc = isThumb2 ? ARM::t2LDM_RET : ARM::LDM_RET;
|
|
|
|
PrevMI->setDesc(TII->get(NewOpc));
|
|
|
|
MO.setReg(ARM::PC);
|
|
|
|
MBB.erase(MBBI);
|
|
|
|
return true;
|
2007-01-19 07:51:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMLoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
|
2007-03-06 18:02:41 +00:00
|
|
|
const TargetMachine &TM = Fn.getTarget();
|
2007-03-07 20:30:36 +00:00
|
|
|
AFI = Fn.getInfo<ARMFunctionInfo>();
|
2007-03-06 18:02:41 +00:00
|
|
|
TII = TM.getInstrInfo();
|
2008-02-10 18:45:23 +00:00
|
|
|
TRI = TM.getRegisterInfo();
|
2007-03-07 02:38:05 +00:00
|
|
|
RS = new RegScavenger();
|
2009-07-09 23:11:34 +00:00
|
|
|
isThumb2 = AFI->isThumb2Function();
|
2007-03-06 18:02:41 +00:00
|
|
|
|
2007-01-19 07:51:42 +00:00
|
|
|
bool Modified = false;
|
|
|
|
for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
|
|
|
|
++MFI) {
|
|
|
|
MachineBasicBlock &MBB = *MFI;
|
|
|
|
Modified |= LoadStoreMultipleOpti(MBB);
|
|
|
|
Modified |= MergeReturnIntoLDM(MBB);
|
|
|
|
}
|
2007-03-06 18:02:41 +00:00
|
|
|
|
|
|
|
delete RS;
|
2007-01-19 07:51:42 +00:00
|
|
|
return Modified;
|
|
|
|
}
|
2009-06-13 09:12:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// ARMPreAllocLoadStoreOpt - Pre- register allocation pass that move
|
|
|
|
/// load / stores from consecutive locations close to make it more
|
|
|
|
/// likely they will be combined later.
|
|
|
|
|
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct ARMPreAllocLoadStoreOpt : public MachineFunctionPass{
|
2009-06-13 09:12:55 +00:00
|
|
|
static char ID;
|
|
|
|
ARMPreAllocLoadStoreOpt() : MachineFunctionPass(&ID) {}
|
|
|
|
|
2009-06-15 08:28:29 +00:00
|
|
|
const TargetData *TD;
|
2009-06-13 09:12:55 +00:00
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const TargetRegisterInfo *TRI;
|
2009-06-15 08:28:29 +00:00
|
|
|
const ARMSubtarget *STI;
|
2009-06-13 09:12:55 +00:00
|
|
|
MachineRegisterInfo *MRI;
|
2009-09-25 21:44:53 +00:00
|
|
|
MachineFunction *MF;
|
2009-06-13 09:12:55 +00:00
|
|
|
|
|
|
|
virtual bool runOnMachineFunction(MachineFunction &Fn);
|
|
|
|
|
|
|
|
virtual const char *getPassName() const {
|
|
|
|
return "ARM pre- register allocation load / store optimization pass";
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2009-06-15 20:54:56 +00:00
|
|
|
bool CanFormLdStDWord(MachineInstr *Op0, MachineInstr *Op1, DebugLoc &dl,
|
|
|
|
unsigned &NewOpc, unsigned &EvenReg,
|
|
|
|
unsigned &OddReg, unsigned &BaseReg,
|
2009-09-27 09:46:04 +00:00
|
|
|
unsigned &OffReg, int &Offset,
|
2009-09-25 21:44:53 +00:00
|
|
|
unsigned &PredReg, ARMCC::CondCodes &Pred,
|
|
|
|
bool &isT2);
|
2009-06-13 09:12:55 +00:00
|
|
|
bool RescheduleOps(MachineBasicBlock *MBB,
|
|
|
|
SmallVector<MachineInstr*, 4> &Ops,
|
|
|
|
unsigned Base, bool isLd,
|
|
|
|
DenseMap<MachineInstr*, unsigned> &MI2LocMap);
|
|
|
|
bool RescheduleLoadStoreInstrs(MachineBasicBlock *MBB);
|
|
|
|
};
|
|
|
|
char ARMPreAllocLoadStoreOpt::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ARMPreAllocLoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
|
2009-06-15 08:28:29 +00:00
|
|
|
TD = Fn.getTarget().getTargetData();
|
2009-06-13 09:12:55 +00:00
|
|
|
TII = Fn.getTarget().getInstrInfo();
|
|
|
|
TRI = Fn.getTarget().getRegisterInfo();
|
2009-06-15 08:28:29 +00:00
|
|
|
STI = &Fn.getTarget().getSubtarget<ARMSubtarget>();
|
2009-06-13 09:12:55 +00:00
|
|
|
MRI = &Fn.getRegInfo();
|
2009-09-25 21:44:53 +00:00
|
|
|
MF = &Fn;
|
2009-06-13 09:12:55 +00:00
|
|
|
|
|
|
|
bool Modified = false;
|
|
|
|
for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
|
|
|
|
++MFI)
|
|
|
|
Modified |= RescheduleLoadStoreInstrs(MFI);
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
2009-06-19 23:17:27 +00:00
|
|
|
static bool IsSafeAndProfitableToMove(bool isLd, unsigned Base,
|
|
|
|
MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock::iterator E,
|
|
|
|
SmallPtrSet<MachineInstr*, 4> &MemOps,
|
|
|
|
SmallSet<unsigned, 4> &MemRegs,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
2009-06-13 09:12:55 +00:00
|
|
|
// Are there stores / loads / calls between them?
|
|
|
|
// FIXME: This is overly conservative. We should make use of alias information
|
|
|
|
// some day.
|
2009-06-19 23:17:27 +00:00
|
|
|
SmallSet<unsigned, 4> AddedRegPressure;
|
2009-06-13 09:12:55 +00:00
|
|
|
while (++I != E) {
|
2009-06-19 23:17:27 +00:00
|
|
|
if (MemOps.count(&*I))
|
|
|
|
continue;
|
2009-06-13 09:12:55 +00:00
|
|
|
const TargetInstrDesc &TID = I->getDesc();
|
|
|
|
if (TID.isCall() || TID.isTerminator() || TID.hasUnmodeledSideEffects())
|
|
|
|
return false;
|
|
|
|
if (isLd && TID.mayStore())
|
|
|
|
return false;
|
|
|
|
if (!isLd) {
|
|
|
|
if (TID.mayLoad())
|
|
|
|
return false;
|
|
|
|
// It's not safe to move the first 'str' down.
|
|
|
|
// str r1, [r0]
|
|
|
|
// strh r5, [r0]
|
|
|
|
// str r4, [r0, #+4]
|
2009-06-19 23:17:27 +00:00
|
|
|
if (TID.mayStore())
|
2009-06-13 09:12:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (unsigned j = 0, NumOps = I->getNumOperands(); j != NumOps; ++j) {
|
|
|
|
MachineOperand &MO = I->getOperand(j);
|
2009-06-19 23:17:27 +00:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (MO.isDef() && TRI->regsOverlap(Reg, Base))
|
2009-06-13 09:12:55 +00:00
|
|
|
return false;
|
2009-06-19 23:17:27 +00:00
|
|
|
if (Reg != Base && !MemRegs.count(Reg))
|
|
|
|
AddedRegPressure.insert(Reg);
|
2009-06-13 09:12:55 +00:00
|
|
|
}
|
|
|
|
}
|
2009-06-19 23:17:27 +00:00
|
|
|
|
|
|
|
// Estimate register pressure increase due to the transformation.
|
|
|
|
if (MemRegs.size() <= 4)
|
|
|
|
// Ok if we are moving small number of instructions.
|
|
|
|
return true;
|
|
|
|
return AddedRegPressure.size() <= MemRegs.size() * 2;
|
2009-06-13 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
2009-06-15 20:54:56 +00:00
|
|
|
bool
|
|
|
|
ARMPreAllocLoadStoreOpt::CanFormLdStDWord(MachineInstr *Op0, MachineInstr *Op1,
|
|
|
|
DebugLoc &dl,
|
|
|
|
unsigned &NewOpc, unsigned &EvenReg,
|
|
|
|
unsigned &OddReg, unsigned &BaseReg,
|
2009-09-27 09:46:04 +00:00
|
|
|
unsigned &OffReg, int &Offset,
|
2009-06-15 20:54:56 +00:00
|
|
|
unsigned &PredReg,
|
2009-09-25 21:44:53 +00:00
|
|
|
ARMCC::CondCodes &Pred,
|
|
|
|
bool &isT2) {
|
2009-09-29 07:07:30 +00:00
|
|
|
// Make sure we're allowed to generate LDRD/STRD.
|
|
|
|
if (!STI->hasV5TEOps())
|
|
|
|
return false;
|
|
|
|
|
2009-06-15 20:54:56 +00:00
|
|
|
// FIXME: FLDS / FSTS -> FLDD / FSTD
|
2009-09-25 21:44:53 +00:00
|
|
|
unsigned Scale = 1;
|
2009-06-15 20:54:56 +00:00
|
|
|
unsigned Opcode = Op0->getOpcode();
|
|
|
|
if (Opcode == ARM::LDR)
|
|
|
|
NewOpc = ARM::LDRD;
|
|
|
|
else if (Opcode == ARM::STR)
|
|
|
|
NewOpc = ARM::STRD;
|
2009-09-25 21:44:53 +00:00
|
|
|
else if (Opcode == ARM::t2LDRi8 || Opcode == ARM::t2LDRi12) {
|
|
|
|
NewOpc = ARM::t2LDRDi8;
|
|
|
|
Scale = 4;
|
|
|
|
isT2 = true;
|
|
|
|
} else if (Opcode == ARM::t2STRi8 || Opcode == ARM::t2STRi12) {
|
|
|
|
NewOpc = ARM::t2STRDi8;
|
|
|
|
Scale = 4;
|
|
|
|
isT2 = true;
|
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
2009-09-26 02:43:36 +00:00
|
|
|
// Make sure the offset registers match.
|
2009-09-25 21:44:53 +00:00
|
|
|
if (!isT2 &&
|
|
|
|
(Op0->getOperand(2).getReg() != Op1->getOperand(2).getReg()))
|
|
|
|
return false;
|
2009-06-15 20:54:56 +00:00
|
|
|
|
|
|
|
// Must sure the base address satisfies i64 ld / st alignment requirement.
|
|
|
|
if (!Op0->hasOneMemOperand() ||
|
2009-09-25 20:36:54 +00:00
|
|
|
!(*Op0->memoperands_begin())->getValue() ||
|
|
|
|
(*Op0->memoperands_begin())->isVolatile())
|
2009-06-15 08:28:29 +00:00
|
|
|
return false;
|
|
|
|
|
2009-09-25 20:36:54 +00:00
|
|
|
unsigned Align = (*Op0->memoperands_begin())->getAlignment();
|
2009-09-25 21:44:53 +00:00
|
|
|
Function *Func = MF->getFunction();
|
2009-06-15 08:28:29 +00:00
|
|
|
unsigned ReqAlign = STI->hasV6Ops()
|
2009-09-25 21:44:53 +00:00
|
|
|
? TD->getPrefTypeAlignment(Type::getInt64Ty(Func->getContext()))
|
|
|
|
: 8; // Pre-v6 need 8-byte align
|
2009-06-15 20:54:56 +00:00
|
|
|
if (Align < ReqAlign)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Then make sure the immediate offset fits.
|
|
|
|
int OffImm = getMemoryOpOffset(Op0);
|
2009-09-27 09:46:04 +00:00
|
|
|
if (isT2) {
|
|
|
|
if (OffImm < 0) {
|
|
|
|
if (OffImm < -255)
|
|
|
|
// Can't fall back to t2LDRi8 / t2STRi8.
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
int Limit = (1 << 8) * Scale;
|
|
|
|
if (OffImm >= Limit || (OffImm & (Scale-1)))
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-25 21:44:53 +00:00
|
|
|
Offset = OffImm;
|
2009-09-27 09:46:04 +00:00
|
|
|
} else {
|
|
|
|
ARM_AM::AddrOpc AddSub = ARM_AM::add;
|
|
|
|
if (OffImm < 0) {
|
|
|
|
AddSub = ARM_AM::sub;
|
|
|
|
OffImm = - OffImm;
|
|
|
|
}
|
|
|
|
int Limit = (1 << 8) * Scale;
|
|
|
|
if (OffImm >= Limit || (OffImm & (Scale-1)))
|
|
|
|
return false;
|
2009-09-25 21:44:53 +00:00
|
|
|
Offset = ARM_AM::getAM3Opc(AddSub, OffImm);
|
2009-09-27 09:46:04 +00:00
|
|
|
}
|
2009-06-15 20:54:56 +00:00
|
|
|
EvenReg = Op0->getOperand(0).getReg();
|
2009-06-15 21:18:20 +00:00
|
|
|
OddReg = Op1->getOperand(0).getReg();
|
2009-06-15 20:54:56 +00:00
|
|
|
if (EvenReg == OddReg)
|
|
|
|
return false;
|
|
|
|
BaseReg = Op0->getOperand(1).getReg();
|
2009-09-25 21:44:53 +00:00
|
|
|
if (!isT2)
|
|
|
|
OffReg = Op0->getOperand(2).getReg();
|
2009-08-08 03:20:32 +00:00
|
|
|
Pred = llvm::getInstrPredicate(Op0, PredReg);
|
2009-06-15 20:54:56 +00:00
|
|
|
dl = Op0->getDebugLoc();
|
|
|
|
return true;
|
2009-06-15 08:28:29 +00:00
|
|
|
}
|
|
|
|
|
2009-06-13 09:12:55 +00:00
|
|
|
bool ARMPreAllocLoadStoreOpt::RescheduleOps(MachineBasicBlock *MBB,
|
|
|
|
SmallVector<MachineInstr*, 4> &Ops,
|
|
|
|
unsigned Base, bool isLd,
|
|
|
|
DenseMap<MachineInstr*, unsigned> &MI2LocMap) {
|
|
|
|
bool RetVal = false;
|
|
|
|
|
|
|
|
// Sort by offset (in reverse order).
|
|
|
|
std::sort(Ops.begin(), Ops.end(), OffsetCompare());
|
|
|
|
|
|
|
|
// The loads / stores of the same base are in order. Scan them from first to
|
|
|
|
// last and check for the followins:
|
|
|
|
// 1. Any def of base.
|
|
|
|
// 2. Any gaps.
|
|
|
|
while (Ops.size() > 1) {
|
|
|
|
unsigned FirstLoc = ~0U;
|
|
|
|
unsigned LastLoc = 0;
|
|
|
|
MachineInstr *FirstOp = 0;
|
|
|
|
MachineInstr *LastOp = 0;
|
|
|
|
int LastOffset = 0;
|
2009-06-18 02:04:01 +00:00
|
|
|
unsigned LastOpcode = 0;
|
2009-06-13 09:12:55 +00:00
|
|
|
unsigned LastBytes = 0;
|
|
|
|
unsigned NumMove = 0;
|
|
|
|
for (int i = Ops.size() - 1; i >= 0; --i) {
|
|
|
|
MachineInstr *Op = Ops[i];
|
|
|
|
unsigned Loc = MI2LocMap[Op];
|
|
|
|
if (Loc <= FirstLoc) {
|
|
|
|
FirstLoc = Loc;
|
|
|
|
FirstOp = Op;
|
|
|
|
}
|
|
|
|
if (Loc >= LastLoc) {
|
|
|
|
LastLoc = Loc;
|
|
|
|
LastOp = Op;
|
|
|
|
}
|
|
|
|
|
2009-06-18 02:04:01 +00:00
|
|
|
unsigned Opcode = Op->getOpcode();
|
|
|
|
if (LastOpcode && Opcode != LastOpcode)
|
|
|
|
break;
|
|
|
|
|
2009-06-13 09:12:55 +00:00
|
|
|
int Offset = getMemoryOpOffset(Op);
|
|
|
|
unsigned Bytes = getLSMultipleTransferSize(Op);
|
|
|
|
if (LastBytes) {
|
|
|
|
if (Bytes != LastBytes || Offset != (LastOffset + (int)Bytes))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
LastOffset = Offset;
|
|
|
|
LastBytes = Bytes;
|
2009-06-18 02:04:01 +00:00
|
|
|
LastOpcode = Opcode;
|
2009-09-25 21:44:53 +00:00
|
|
|
if (++NumMove == 8) // FIXME: Tune this limit.
|
2009-06-13 09:12:55 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NumMove <= 1)
|
|
|
|
Ops.pop_back();
|
|
|
|
else {
|
2009-06-19 23:17:27 +00:00
|
|
|
SmallPtrSet<MachineInstr*, 4> MemOps;
|
|
|
|
SmallSet<unsigned, 4> MemRegs;
|
|
|
|
for (int i = NumMove-1; i >= 0; --i) {
|
|
|
|
MemOps.insert(Ops[i]);
|
|
|
|
MemRegs.insert(Ops[i]->getOperand(0).getReg());
|
|
|
|
}
|
2009-06-13 09:12:55 +00:00
|
|
|
|
|
|
|
// Be conservative, if the instructions are too far apart, don't
|
|
|
|
// move them. We want to limit the increase of register pressure.
|
2009-06-19 23:17:27 +00:00
|
|
|
bool DoMove = (LastLoc - FirstLoc) <= NumMove*4; // FIXME: Tune this.
|
2009-06-13 09:12:55 +00:00
|
|
|
if (DoMove)
|
2009-06-19 23:17:27 +00:00
|
|
|
DoMove = IsSafeAndProfitableToMove(isLd, Base, FirstOp, LastOp,
|
|
|
|
MemOps, MemRegs, TRI);
|
2009-06-13 09:12:55 +00:00
|
|
|
if (!DoMove) {
|
|
|
|
for (unsigned i = 0; i != NumMove; ++i)
|
|
|
|
Ops.pop_back();
|
|
|
|
} else {
|
|
|
|
// This is the new location for the loads / stores.
|
|
|
|
MachineBasicBlock::iterator InsertPos = isLd ? FirstOp : LastOp;
|
2009-06-19 23:17:27 +00:00
|
|
|
while (InsertPos != MBB->end() && MemOps.count(InsertPos))
|
2009-06-13 09:12:55 +00:00
|
|
|
++InsertPos;
|
2009-06-15 08:28:29 +00:00
|
|
|
|
|
|
|
// If we are moving a pair of loads / stores, see if it makes sense
|
|
|
|
// to try to allocate a pair of registers that can form register pairs.
|
2009-06-15 20:54:56 +00:00
|
|
|
MachineInstr *Op0 = Ops.back();
|
|
|
|
MachineInstr *Op1 = Ops[Ops.size()-2];
|
|
|
|
unsigned EvenReg = 0, OddReg = 0;
|
|
|
|
unsigned BaseReg = 0, OffReg = 0, PredReg = 0;
|
|
|
|
ARMCC::CondCodes Pred = ARMCC::AL;
|
2009-09-25 21:44:53 +00:00
|
|
|
bool isT2 = false;
|
2009-06-15 20:54:56 +00:00
|
|
|
unsigned NewOpc = 0;
|
2009-09-27 09:46:04 +00:00
|
|
|
int Offset = 0;
|
2009-06-15 20:54:56 +00:00
|
|
|
DebugLoc dl;
|
|
|
|
if (NumMove == 2 && CanFormLdStDWord(Op0, Op1, dl, NewOpc,
|
|
|
|
EvenReg, OddReg, BaseReg, OffReg,
|
2009-09-25 21:44:53 +00:00
|
|
|
Offset, PredReg, Pred, isT2)) {
|
2009-06-15 20:54:56 +00:00
|
|
|
Ops.pop_back();
|
|
|
|
Ops.pop_back();
|
2009-06-15 08:28:29 +00:00
|
|
|
|
2009-06-15 20:54:56 +00:00
|
|
|
// Form the pair instruction.
|
2009-06-18 02:04:01 +00:00
|
|
|
if (isLd) {
|
2009-09-25 21:44:53 +00:00
|
|
|
MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos,
|
|
|
|
dl, TII->get(NewOpc))
|
2009-06-15 08:28:29 +00:00
|
|
|
.addReg(EvenReg, RegState::Define)
|
|
|
|
.addReg(OddReg, RegState::Define)
|
2009-09-25 21:44:53 +00:00
|
|
|
.addReg(BaseReg);
|
|
|
|
if (!isT2)
|
|
|
|
MIB.addReg(OffReg);
|
|
|
|
MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
|
2009-06-18 02:04:01 +00:00
|
|
|
++NumLDRDFormed;
|
|
|
|
} else {
|
2009-09-25 21:44:53 +00:00
|
|
|
MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos,
|
|
|
|
dl, TII->get(NewOpc))
|
2009-06-15 08:28:29 +00:00
|
|
|
.addReg(EvenReg)
|
|
|
|
.addReg(OddReg)
|
2009-09-25 21:44:53 +00:00
|
|
|
.addReg(BaseReg);
|
|
|
|
if (!isT2)
|
|
|
|
MIB.addReg(OffReg);
|
|
|
|
MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
|
2009-06-18 02:04:01 +00:00
|
|
|
++NumSTRDFormed;
|
|
|
|
}
|
|
|
|
MBB->erase(Op0);
|
|
|
|
MBB->erase(Op1);
|
2009-06-15 08:28:29 +00:00
|
|
|
|
|
|
|
// Add register allocation hints to form register pairs.
|
|
|
|
MRI->setRegAllocationHint(EvenReg, ARMRI::RegPairEven, OddReg);
|
|
|
|
MRI->setRegAllocationHint(OddReg, ARMRI::RegPairOdd, EvenReg);
|
2009-06-15 20:54:56 +00:00
|
|
|
} else {
|
|
|
|
for (unsigned i = 0; i != NumMove; ++i) {
|
|
|
|
MachineInstr *Op = Ops.back();
|
|
|
|
Ops.pop_back();
|
|
|
|
MBB->splice(InsertPos, MBB, Op);
|
|
|
|
}
|
2009-06-13 09:12:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NumLdStMoved += NumMove;
|
|
|
|
RetVal = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ARMPreAllocLoadStoreOpt::RescheduleLoadStoreInstrs(MachineBasicBlock *MBB) {
|
|
|
|
bool RetVal = false;
|
|
|
|
|
|
|
|
DenseMap<MachineInstr*, unsigned> MI2LocMap;
|
|
|
|
DenseMap<unsigned, SmallVector<MachineInstr*, 4> > Base2LdsMap;
|
|
|
|
DenseMap<unsigned, SmallVector<MachineInstr*, 4> > Base2StsMap;
|
|
|
|
SmallVector<unsigned, 4> LdBases;
|
|
|
|
SmallVector<unsigned, 4> StBases;
|
|
|
|
|
|
|
|
unsigned Loc = 0;
|
|
|
|
MachineBasicBlock::iterator MBBI = MBB->begin();
|
|
|
|
MachineBasicBlock::iterator E = MBB->end();
|
|
|
|
while (MBBI != E) {
|
|
|
|
for (; MBBI != E; ++MBBI) {
|
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
const TargetInstrDesc &TID = MI->getDesc();
|
|
|
|
if (TID.isCall() || TID.isTerminator()) {
|
|
|
|
// Stop at barriers.
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
MI2LocMap[MI] = Loc++;
|
|
|
|
if (!isMemoryOp(MI))
|
|
|
|
continue;
|
|
|
|
unsigned PredReg = 0;
|
2009-08-08 03:20:32 +00:00
|
|
|
if (llvm::getInstrPredicate(MI, PredReg) != ARMCC::AL)
|
2009-06-13 09:12:55 +00:00
|
|
|
continue;
|
|
|
|
|
2009-09-25 21:44:53 +00:00
|
|
|
int Opc = MI->getOpcode();
|
|
|
|
bool isLd = isi32Load(Opc) || Opc == ARM::FLDS || Opc == ARM::FLDD;
|
2009-06-13 09:12:55 +00:00
|
|
|
unsigned Base = MI->getOperand(1).getReg();
|
|
|
|
int Offset = getMemoryOpOffset(MI);
|
|
|
|
|
|
|
|
bool StopHere = false;
|
|
|
|
if (isLd) {
|
|
|
|
DenseMap<unsigned, SmallVector<MachineInstr*, 4> >::iterator BI =
|
|
|
|
Base2LdsMap.find(Base);
|
|
|
|
if (BI != Base2LdsMap.end()) {
|
|
|
|
for (unsigned i = 0, e = BI->second.size(); i != e; ++i) {
|
|
|
|
if (Offset == getMemoryOpOffset(BI->second[i])) {
|
|
|
|
StopHere = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!StopHere)
|
|
|
|
BI->second.push_back(MI);
|
|
|
|
} else {
|
|
|
|
SmallVector<MachineInstr*, 4> MIs;
|
|
|
|
MIs.push_back(MI);
|
|
|
|
Base2LdsMap[Base] = MIs;
|
|
|
|
LdBases.push_back(Base);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DenseMap<unsigned, SmallVector<MachineInstr*, 4> >::iterator BI =
|
|
|
|
Base2StsMap.find(Base);
|
|
|
|
if (BI != Base2StsMap.end()) {
|
|
|
|
for (unsigned i = 0, e = BI->second.size(); i != e; ++i) {
|
|
|
|
if (Offset == getMemoryOpOffset(BI->second[i])) {
|
|
|
|
StopHere = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!StopHere)
|
|
|
|
BI->second.push_back(MI);
|
|
|
|
} else {
|
|
|
|
SmallVector<MachineInstr*, 4> MIs;
|
|
|
|
MIs.push_back(MI);
|
|
|
|
Base2StsMap[Base] = MIs;
|
|
|
|
StBases.push_back(Base);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (StopHere) {
|
2009-06-19 23:17:27 +00:00
|
|
|
// Found a duplicate (a base+offset combination that's seen earlier).
|
|
|
|
// Backtrack.
|
2009-06-13 09:12:55 +00:00
|
|
|
--Loc;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-schedule loads.
|
|
|
|
for (unsigned i = 0, e = LdBases.size(); i != e; ++i) {
|
|
|
|
unsigned Base = LdBases[i];
|
|
|
|
SmallVector<MachineInstr*, 4> &Lds = Base2LdsMap[Base];
|
|
|
|
if (Lds.size() > 1)
|
|
|
|
RetVal |= RescheduleOps(MBB, Lds, Base, true, MI2LocMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Re-schedule stores.
|
|
|
|
for (unsigned i = 0, e = StBases.size(); i != e; ++i) {
|
|
|
|
unsigned Base = StBases[i];
|
|
|
|
SmallVector<MachineInstr*, 4> &Sts = Base2StsMap[Base];
|
|
|
|
if (Sts.size() > 1)
|
|
|
|
RetVal |= RescheduleOps(MBB, Sts, Base, false, MI2LocMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MBBI != E) {
|
|
|
|
Base2LdsMap.clear();
|
|
|
|
Base2StsMap.clear();
|
|
|
|
LdBases.clear();
|
|
|
|
StBases.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// createARMLoadStoreOptimizationPass - returns an instance of the load / store
|
|
|
|
/// optimization pass.
|
|
|
|
FunctionPass *llvm::createARMLoadStoreOptimizationPass(bool PreAlloc) {
|
|
|
|
if (PreAlloc)
|
|
|
|
return new ARMPreAllocLoadStoreOpt();
|
|
|
|
return new ARMLoadStoreOpt();
|
|
|
|
}
|