2014-05-24 12:50:23 +00:00
|
|
|
//=- AArch64LoadStoreOptimizer.cpp - AArch64 load/store opt. pass -*- C++ -*-=//
|
2014-03-29 10:18:08 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a pass that performs load / store related peephole
|
|
|
|
// optimizations. This pass should be run after register allocation.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
#include "AArch64InstrInfo.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
#include "AArch64Subtarget.h"
|
2014-05-24 12:50:23 +00:00
|
|
|
#include "MCTargetDesc/AArch64AddressingModes.h"
|
2014-03-29 10:18:08 +00:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2015-05-21 21:36:46 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2014-07-25 11:42:14 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-03-29 10:18:08 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-07-25 11:42:14 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-03-29 10:18:08 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
#define DEBUG_TYPE "aarch64-ldst-opt"
|
2014-04-22 02:41:26 +00:00
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
/// AArch64AllocLoadStoreOpt - Post-register allocation pass to combine
|
2014-03-29 10:18:08 +00:00
|
|
|
/// load / store instructions to form ldp / stp instructions.
|
|
|
|
|
|
|
|
STATISTIC(NumPairCreated, "Number of load/store pair instructions generated");
|
|
|
|
STATISTIC(NumPostFolded, "Number of post-index updates folded");
|
|
|
|
STATISTIC(NumPreFolded, "Number of pre-index updates folded");
|
|
|
|
STATISTIC(NumUnscaledPairCreated,
|
|
|
|
"Number of load/store from unscaled generated");
|
|
|
|
|
2014-06-04 12:40:35 +00:00
|
|
|
static cl::opt<unsigned> ScanLimit("aarch64-load-store-scan-limit",
|
|
|
|
cl::init(20), cl::Hidden);
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
// Place holder while testing unscaled load/store combining
|
2014-06-04 12:40:35 +00:00
|
|
|
static cl::opt<bool> EnableAArch64UnscaledMemOp(
|
|
|
|
"aarch64-unscaled-mem-op", cl::Hidden,
|
|
|
|
cl::desc("Allow AArch64 unscaled load/store combining"), cl::init(true));
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
namespace {
|
2014-05-24 12:50:23 +00:00
|
|
|
struct AArch64LoadStoreOpt : public MachineFunctionPass {
|
2014-03-29 10:18:08 +00:00
|
|
|
static char ID;
|
2014-05-24 12:50:23 +00:00
|
|
|
AArch64LoadStoreOpt() : MachineFunctionPass(ID) {}
|
2014-03-29 10:18:08 +00:00
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
const AArch64InstrInfo *TII;
|
2014-03-29 10:18:08 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
|
|
|
|
// Scan the instructions looking for a load/store that can be combined
|
|
|
|
// with the current instruction into a load/store pair.
|
|
|
|
// Return the matching instruction if one is found, else MBB->end().
|
2014-06-04 12:36:28 +00:00
|
|
|
// If a matching instruction is found, MergeForward is set to true if the
|
2014-03-29 10:18:08 +00:00
|
|
|
// merge is to remove the first instruction and replace the second with
|
|
|
|
// a pair-wise insn, and false if the reverse is true.
|
2015-03-06 22:42:10 +00:00
|
|
|
// \p SExtIdx[out] gives the index of the result of the load pair that
|
|
|
|
// must be extended. The value of SExtIdx assumes that the paired load
|
|
|
|
// produces the value in this order: (I, returned iterator), i.e.,
|
|
|
|
// -1 means no value has to be extended, 0 means I, and 1 means the
|
|
|
|
// returned iterator.
|
2014-03-29 10:18:08 +00:00
|
|
|
MachineBasicBlock::iterator findMatchingInsn(MachineBasicBlock::iterator I,
|
2015-03-06 22:42:10 +00:00
|
|
|
bool &MergeForward, int &SExtIdx,
|
2014-03-29 10:18:08 +00:00
|
|
|
unsigned Limit);
|
|
|
|
// Merge the two instructions indicated into a single pair-wise instruction.
|
2014-06-04 12:36:28 +00:00
|
|
|
// If MergeForward is true, erase the first instruction and fold its
|
2014-03-29 10:18:08 +00:00
|
|
|
// operation into the second. If false, the reverse. Return the instruction
|
2014-04-03 23:43:12 +00:00
|
|
|
// following the first instruction (which may change during processing).
|
2015-03-06 22:42:10 +00:00
|
|
|
// \p SExtIdx index of the result that must be extended for a paired load.
|
|
|
|
// -1 means none, 0 means I, and 1 means Paired.
|
2014-03-29 10:18:08 +00:00
|
|
|
MachineBasicBlock::iterator
|
|
|
|
mergePairedInsns(MachineBasicBlock::iterator I,
|
2015-03-06 22:42:10 +00:00
|
|
|
MachineBasicBlock::iterator Paired, bool MergeForward,
|
|
|
|
int SExtIdx);
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
// Scan the instruction list to find a base register update that can
|
|
|
|
// be combined with the current instruction (a load or store) using
|
|
|
|
// pre or post indexed addressing with writeback. Scan forwards.
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
findMatchingUpdateInsnForward(MachineBasicBlock::iterator I, unsigned Limit,
|
|
|
|
int Value);
|
|
|
|
|
|
|
|
// Scan the instruction list to find a base register update that can
|
|
|
|
// be combined with the current instruction (a load or store) using
|
|
|
|
// pre or post indexed addressing with writeback. Scan backwards.
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
findMatchingUpdateInsnBackward(MachineBasicBlock::iterator I, unsigned Limit);
|
|
|
|
|
|
|
|
// Merge a pre-index base register update into a ld/st instruction.
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
mergePreIdxUpdateInsn(MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock::iterator Update);
|
|
|
|
|
|
|
|
// Merge a post-index base register update into a ld/st instruction.
|
|
|
|
MachineBasicBlock::iterator
|
|
|
|
mergePostIdxUpdateInsn(MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock::iterator Update);
|
|
|
|
|
|
|
|
bool optimizeBlock(MachineBasicBlock &MBB);
|
|
|
|
|
2014-04-29 07:58:25 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction &Fn) override;
|
2014-03-29 10:18:08 +00:00
|
|
|
|
2014-04-29 07:58:25 +00:00
|
|
|
const char *getPassName() const override {
|
2014-05-24 12:50:23 +00:00
|
|
|
return "AArch64 load / store optimization pass";
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int getMemSize(MachineInstr *MemMI);
|
|
|
|
};
|
2014-05-24 12:50:23 +00:00
|
|
|
char AArch64LoadStoreOpt::ID = 0;
|
2014-08-11 22:42:31 +00:00
|
|
|
} // namespace
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
static bool isUnscaledLdst(unsigned Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
return false;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STURSi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STURDi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STURQi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STURWi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STURXi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDURSi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDURDi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDURQi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDURWi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDURXi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return true;
|
2015-01-24 01:25:54 +00:00
|
|
|
case AArch64::LDURSWi:
|
|
|
|
return true;
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Size in bytes of the data moved by an unscaled load or store
|
2014-05-24 12:50:23 +00:00
|
|
|
int AArch64LoadStoreOpt::getMemSize(MachineInstr *MemMI) {
|
2014-03-29 10:18:08 +00:00
|
|
|
switch (MemMI->getOpcode()) {
|
|
|
|
default:
|
2014-06-03 16:33:13 +00:00
|
|
|
llvm_unreachable("Opcode has unknown size!");
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRSui:
|
|
|
|
case AArch64::STURSi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 4;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRDui:
|
|
|
|
case AArch64::STURDi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 8;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRQui:
|
|
|
|
case AArch64::STURQi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 16;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRWui:
|
|
|
|
case AArch64::STURWi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 4;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRXui:
|
|
|
|
case AArch64::STURXi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 8;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDRSui:
|
|
|
|
case AArch64::LDURSi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 4;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDRDui:
|
|
|
|
case AArch64::LDURDi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 8;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDRQui:
|
|
|
|
case AArch64::LDURQi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 16;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDRWui:
|
|
|
|
case AArch64::LDURWi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 4;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::LDRXui:
|
|
|
|
case AArch64::LDURXi:
|
2014-03-29 10:18:08 +00:00
|
|
|
return 8;
|
2015-01-24 01:25:54 +00:00
|
|
|
case AArch64::LDRSWui:
|
|
|
|
case AArch64::LDURSWi:
|
|
|
|
return 4;
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-06 22:42:10 +00:00
|
|
|
static unsigned getMatchingNonSExtOpcode(unsigned Opc,
|
|
|
|
bool *IsValidLdStrOpc = nullptr) {
|
|
|
|
if (IsValidLdStrOpc)
|
|
|
|
*IsValidLdStrOpc = true;
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
if (IsValidLdStrOpc)
|
|
|
|
*IsValidLdStrOpc = false;
|
|
|
|
return UINT_MAX;
|
|
|
|
case AArch64::STRDui:
|
|
|
|
case AArch64::STURDi:
|
|
|
|
case AArch64::STRQui:
|
|
|
|
case AArch64::STURQi:
|
|
|
|
case AArch64::STRWui:
|
|
|
|
case AArch64::STURWi:
|
|
|
|
case AArch64::STRXui:
|
|
|
|
case AArch64::STURXi:
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
case AArch64::LDURDi:
|
|
|
|
case AArch64::LDRQui:
|
|
|
|
case AArch64::LDURQi:
|
|
|
|
case AArch64::LDRWui:
|
|
|
|
case AArch64::LDURWi:
|
|
|
|
case AArch64::LDRXui:
|
|
|
|
case AArch64::LDURXi:
|
|
|
|
case AArch64::STRSui:
|
|
|
|
case AArch64::STURSi:
|
|
|
|
case AArch64::LDRSui:
|
|
|
|
case AArch64::LDURSi:
|
|
|
|
return Opc;
|
|
|
|
case AArch64::LDRSWui:
|
|
|
|
return AArch64::LDRWui;
|
|
|
|
case AArch64::LDURSWi:
|
|
|
|
return AArch64::LDURWi;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-29 10:18:08 +00:00
|
|
|
static unsigned getMatchingPairOpcode(unsigned Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Opcode has no pairwise equivalent!");
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRSui:
|
|
|
|
case AArch64::STURSi:
|
|
|
|
return AArch64::STPSi;
|
|
|
|
case AArch64::STRDui:
|
|
|
|
case AArch64::STURDi:
|
|
|
|
return AArch64::STPDi;
|
|
|
|
case AArch64::STRQui:
|
|
|
|
case AArch64::STURQi:
|
|
|
|
return AArch64::STPQi;
|
|
|
|
case AArch64::STRWui:
|
|
|
|
case AArch64::STURWi:
|
|
|
|
return AArch64::STPWi;
|
|
|
|
case AArch64::STRXui:
|
|
|
|
case AArch64::STURXi:
|
|
|
|
return AArch64::STPXi;
|
|
|
|
case AArch64::LDRSui:
|
|
|
|
case AArch64::LDURSi:
|
|
|
|
return AArch64::LDPSi;
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
case AArch64::LDURDi:
|
|
|
|
return AArch64::LDPDi;
|
|
|
|
case AArch64::LDRQui:
|
|
|
|
case AArch64::LDURQi:
|
|
|
|
return AArch64::LDPQi;
|
|
|
|
case AArch64::LDRWui:
|
|
|
|
case AArch64::LDURWi:
|
|
|
|
return AArch64::LDPWi;
|
|
|
|
case AArch64::LDRXui:
|
|
|
|
case AArch64::LDURXi:
|
|
|
|
return AArch64::LDPXi;
|
2015-01-24 01:25:54 +00:00
|
|
|
case AArch64::LDRSWui:
|
|
|
|
case AArch64::LDURSWi:
|
|
|
|
return AArch64::LDPSWi;
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getPreIndexedOpcode(unsigned Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Opcode has no pre-indexed equivalent!");
|
2014-06-04 12:40:35 +00:00
|
|
|
case AArch64::STRSui:
|
|
|
|
return AArch64::STRSpre;
|
|
|
|
case AArch64::STRDui:
|
|
|
|
return AArch64::STRDpre;
|
|
|
|
case AArch64::STRQui:
|
|
|
|
return AArch64::STRQpre;
|
|
|
|
case AArch64::STRWui:
|
|
|
|
return AArch64::STRWpre;
|
|
|
|
case AArch64::STRXui:
|
|
|
|
return AArch64::STRXpre;
|
|
|
|
case AArch64::LDRSui:
|
|
|
|
return AArch64::LDRSpre;
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
return AArch64::LDRDpre;
|
|
|
|
case AArch64::LDRQui:
|
|
|
|
return AArch64::LDRQpre;
|
|
|
|
case AArch64::LDRWui:
|
|
|
|
return AArch64::LDRWpre;
|
|
|
|
case AArch64::LDRXui:
|
|
|
|
return AArch64::LDRXpre;
|
2015-01-24 01:25:54 +00:00
|
|
|
case AArch64::LDRSWui:
|
|
|
|
return AArch64::LDRSWpre;
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getPostIndexedOpcode(unsigned Opc) {
|
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Opcode has no post-indexed wise equivalent!");
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRSui:
|
|
|
|
return AArch64::STRSpost;
|
|
|
|
case AArch64::STRDui:
|
|
|
|
return AArch64::STRDpost;
|
|
|
|
case AArch64::STRQui:
|
|
|
|
return AArch64::STRQpost;
|
|
|
|
case AArch64::STRWui:
|
|
|
|
return AArch64::STRWpost;
|
|
|
|
case AArch64::STRXui:
|
|
|
|
return AArch64::STRXpost;
|
|
|
|
case AArch64::LDRSui:
|
|
|
|
return AArch64::LDRSpost;
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
return AArch64::LDRDpost;
|
|
|
|
case AArch64::LDRQui:
|
|
|
|
return AArch64::LDRQpost;
|
|
|
|
case AArch64::LDRWui:
|
|
|
|
return AArch64::LDRWpost;
|
|
|
|
case AArch64::LDRXui:
|
|
|
|
return AArch64::LDRXpost;
|
2015-01-24 01:25:54 +00:00
|
|
|
case AArch64::LDRSWui:
|
|
|
|
return AArch64::LDRSWpost;
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
2014-05-24 12:50:23 +00:00
|
|
|
AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock::iterator Paired,
|
2015-03-06 22:42:10 +00:00
|
|
|
bool MergeForward, int SExtIdx) {
|
2014-03-29 10:18:08 +00:00
|
|
|
MachineBasicBlock::iterator NextI = I;
|
|
|
|
++NextI;
|
|
|
|
// If NextI is the second of the two instructions to be merged, we need
|
|
|
|
// to skip one further. Either way we merge will invalidate the iterator,
|
|
|
|
// and we don't need to scan the new instruction, as it's a pairwise
|
|
|
|
// instruction, which we're not considering for further action anyway.
|
|
|
|
if (NextI == Paired)
|
|
|
|
++NextI;
|
|
|
|
|
2015-03-06 22:42:10 +00:00
|
|
|
unsigned Opc =
|
|
|
|
SExtIdx == -1 ? I->getOpcode() : getMatchingNonSExtOpcode(I->getOpcode());
|
|
|
|
bool IsUnscaled = isUnscaledLdst(Opc);
|
2014-05-24 12:50:23 +00:00
|
|
|
int OffsetStride =
|
|
|
|
IsUnscaled && EnableAArch64UnscaledMemOp ? getMemSize(I) : 1;
|
2014-03-29 10:18:08 +00:00
|
|
|
|
2015-03-06 22:42:10 +00:00
|
|
|
unsigned NewOpc = getMatchingPairOpcode(Opc);
|
2014-03-29 10:18:08 +00:00
|
|
|
// Insert our new paired instruction after whichever of the paired
|
2014-06-04 12:36:28 +00:00
|
|
|
// instructions MergeForward indicates.
|
|
|
|
MachineBasicBlock::iterator InsertionPoint = MergeForward ? Paired : I;
|
|
|
|
// Also based on MergeForward is from where we copy the base register operand
|
2014-03-29 10:18:08 +00:00
|
|
|
// so we get the flags compatible with the input code.
|
|
|
|
MachineOperand &BaseRegOp =
|
2014-06-04 12:36:28 +00:00
|
|
|
MergeForward ? Paired->getOperand(1) : I->getOperand(1);
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
// Which register is Rt and which is Rt2 depends on the offset order.
|
|
|
|
MachineInstr *RtMI, *Rt2MI;
|
|
|
|
if (I->getOperand(2).getImm() ==
|
|
|
|
Paired->getOperand(2).getImm() + OffsetStride) {
|
|
|
|
RtMI = Paired;
|
|
|
|
Rt2MI = I;
|
2015-03-06 22:42:10 +00:00
|
|
|
// Here we swapped the assumption made for SExtIdx.
|
|
|
|
// I.e., we turn ldp I, Paired into ldp Paired, I.
|
|
|
|
// Update the index accordingly.
|
|
|
|
if (SExtIdx != -1)
|
|
|
|
SExtIdx = (SExtIdx + 1) % 2;
|
2014-03-29 10:18:08 +00:00
|
|
|
} else {
|
|
|
|
RtMI = I;
|
|
|
|
Rt2MI = Paired;
|
|
|
|
}
|
|
|
|
// Handle Unscaled
|
|
|
|
int OffsetImm = RtMI->getOperand(2).getImm();
|
2014-05-24 12:50:23 +00:00
|
|
|
if (IsUnscaled && EnableAArch64UnscaledMemOp)
|
2014-03-29 10:18:08 +00:00
|
|
|
OffsetImm /= OffsetStride;
|
|
|
|
|
|
|
|
// Construct the new instruction.
|
|
|
|
MachineInstrBuilder MIB = BuildMI(*I->getParent(), InsertionPoint,
|
|
|
|
I->getDebugLoc(), TII->get(NewOpc))
|
|
|
|
.addOperand(RtMI->getOperand(0))
|
|
|
|
.addOperand(Rt2MI->getOperand(0))
|
|
|
|
.addOperand(BaseRegOp)
|
|
|
|
.addImm(OffsetImm);
|
|
|
|
(void)MIB;
|
|
|
|
|
|
|
|
// FIXME: Do we need/want to copy the mem operands from the source
|
|
|
|
// instructions? Probably. What uses them after this?
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Creating pair load/store. Replacing instructions:\n ");
|
|
|
|
DEBUG(I->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << " ");
|
|
|
|
DEBUG(Paired->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << " with instruction:\n ");
|
2015-03-06 22:42:10 +00:00
|
|
|
|
|
|
|
if (SExtIdx != -1) {
|
|
|
|
// Generate the sign extension for the proper result of the ldp.
|
|
|
|
// I.e., with X1, that would be:
|
|
|
|
// %W1<def> = KILL %W1, %X1<imp-def>
|
|
|
|
// %X1<def> = SBFMXri %X1<kill>, 0, 31
|
|
|
|
MachineOperand &DstMO = MIB->getOperand(SExtIdx);
|
|
|
|
// Right now, DstMO has the extended register, since it comes from an
|
|
|
|
// extended opcode.
|
|
|
|
unsigned DstRegX = DstMO.getReg();
|
|
|
|
// Get the W variant of that register.
|
|
|
|
unsigned DstRegW = TRI->getSubReg(DstRegX, AArch64::sub_32);
|
|
|
|
// Update the result of LDP to use the W instead of the X variant.
|
|
|
|
DstMO.setReg(DstRegW);
|
|
|
|
DEBUG(((MachineInstr *)MIB)->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << "\n");
|
|
|
|
// Make the machine verifier happy by providing a definition for
|
|
|
|
// the X register.
|
|
|
|
// Insert this definition right after the generated LDP, i.e., before
|
|
|
|
// InsertionPoint.
|
|
|
|
MachineInstrBuilder MIBKill =
|
|
|
|
BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::KILL), DstRegW)
|
|
|
|
.addReg(DstRegW)
|
|
|
|
.addReg(DstRegX, RegState::Define);
|
|
|
|
MIBKill->getOperand(2).setImplicit();
|
|
|
|
// Create the sign extension.
|
|
|
|
MachineInstrBuilder MIBSXTW =
|
|
|
|
BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
|
|
|
|
TII->get(AArch64::SBFMXri), DstRegX)
|
|
|
|
.addReg(DstRegX)
|
|
|
|
.addImm(0)
|
|
|
|
.addImm(31);
|
|
|
|
(void)MIBSXTW;
|
|
|
|
DEBUG(dbgs() << " Extend operand:\n ");
|
|
|
|
DEBUG(((MachineInstr *)MIBSXTW)->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << "\n");
|
|
|
|
} else {
|
|
|
|
DEBUG(((MachineInstr *)MIB)->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << "\n");
|
|
|
|
}
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
// Erase the old instructions.
|
|
|
|
I->eraseFromParent();
|
|
|
|
Paired->eraseFromParent();
|
|
|
|
|
|
|
|
return NextI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// trackRegDefsUses - Remember what registers the specified instruction uses
|
|
|
|
/// and modifies.
|
|
|
|
static void trackRegDefsUses(MachineInstr *MI, BitVector &ModifiedRegs,
|
|
|
|
BitVector &UsedRegs,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (MO.isRegMask())
|
|
|
|
ModifiedRegs.setBitsNotInMask(MO.getRegMask());
|
|
|
|
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (MO.isDef()) {
|
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
ModifiedRegs.set(*AI);
|
|
|
|
} else {
|
|
|
|
assert(MO.isUse() && "Reg operand not a def and not a use?!?");
|
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
UsedRegs.set(*AI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool inBoundsForPair(bool IsUnscaled, int Offset, int OffsetStride) {
|
|
|
|
if (!IsUnscaled && (Offset > 63 || Offset < -64))
|
|
|
|
return false;
|
|
|
|
if (IsUnscaled) {
|
|
|
|
// Convert the byte-offset used by unscaled into an "element" offset used
|
|
|
|
// by the scaled pair load/store instructions.
|
2014-06-04 12:36:28 +00:00
|
|
|
int ElemOffset = Offset / OffsetStride;
|
|
|
|
if (ElemOffset > 63 || ElemOffset < -64)
|
2014-03-29 10:18:08 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do alignment, specialized to power of 2 and for signed ints,
|
|
|
|
// avoiding having to do a C-style cast from uint_64t to int when
|
|
|
|
// using RoundUpToAlignment from include/llvm/Support/MathExtras.h.
|
|
|
|
// FIXME: Move this function to include/MathExtras.h?
|
|
|
|
static int alignTo(int Num, int PowOf2) {
|
|
|
|
return (Num + PowOf2 - 1) & ~(PowOf2 - 1);
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:36:46 +00:00
|
|
|
static bool mayAlias(MachineInstr *MIa, MachineInstr *MIb,
|
|
|
|
const AArch64InstrInfo *TII) {
|
|
|
|
// One of the instructions must modify memory.
|
|
|
|
if (!MIa->mayStore() && !MIb->mayStore())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Both instructions must be memory operations.
|
|
|
|
if (!MIa->mayLoadOrStore() && !MIb->mayLoadOrStore())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !TII->areMemAccessesTriviallyDisjoint(MIa, MIb);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool mayAlias(MachineInstr *MIa,
|
|
|
|
SmallVectorImpl<MachineInstr *> &MemInsns,
|
|
|
|
const AArch64InstrInfo *TII) {
|
|
|
|
for (auto &MIb : MemInsns)
|
|
|
|
if (mayAlias(MIa, MIb, TII))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-29 10:18:08 +00:00
|
|
|
/// findMatchingInsn - Scan the instructions looking for a load/store that can
|
|
|
|
/// be combined with the current instruction into a load/store pair.
|
|
|
|
MachineBasicBlock::iterator
|
2014-05-24 12:50:23 +00:00
|
|
|
AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
|
2015-03-06 22:42:10 +00:00
|
|
|
bool &MergeForward, int &SExtIdx,
|
|
|
|
unsigned Limit) {
|
2014-03-29 10:18:08 +00:00
|
|
|
MachineBasicBlock::iterator E = I->getParent()->end();
|
|
|
|
MachineBasicBlock::iterator MBBI = I;
|
|
|
|
MachineInstr *FirstMI = I;
|
|
|
|
++MBBI;
|
|
|
|
|
2015-05-18 20:27:55 +00:00
|
|
|
unsigned Opc = FirstMI->getOpcode();
|
2014-06-04 12:36:28 +00:00
|
|
|
bool MayLoad = FirstMI->mayLoad();
|
2014-03-29 10:18:08 +00:00
|
|
|
bool IsUnscaled = isUnscaledLdst(Opc);
|
|
|
|
unsigned Reg = FirstMI->getOperand(0).getReg();
|
|
|
|
unsigned BaseReg = FirstMI->getOperand(1).getReg();
|
|
|
|
int Offset = FirstMI->getOperand(2).getImm();
|
|
|
|
|
|
|
|
// Early exit if the first instruction modifies the base register.
|
|
|
|
// e.g., ldr x0, [x0]
|
|
|
|
// Early exit if the offset if not possible to match. (6 bits of positive
|
|
|
|
// range, plus allow an extra one in case we find a later insn that matches
|
|
|
|
// with Offset-1
|
|
|
|
if (FirstMI->modifiesRegister(BaseReg, TRI))
|
|
|
|
return E;
|
|
|
|
int OffsetStride =
|
2014-05-24 12:50:23 +00:00
|
|
|
IsUnscaled && EnableAArch64UnscaledMemOp ? getMemSize(FirstMI) : 1;
|
2014-03-29 10:18:08 +00:00
|
|
|
if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// Track which registers have been modified and used between the first insn
|
|
|
|
// (inclusive) and the second insn.
|
|
|
|
BitVector ModifiedRegs, UsedRegs;
|
|
|
|
ModifiedRegs.resize(TRI->getNumRegs());
|
|
|
|
UsedRegs.resize(TRI->getNumRegs());
|
2015-05-21 21:36:46 +00:00
|
|
|
|
|
|
|
// Remember any instructions that read/write memory between FirstMI and MI.
|
|
|
|
SmallVector<MachineInstr *, 4> MemInsns;
|
|
|
|
|
2014-03-29 10:18:08 +00:00
|
|
|
for (unsigned Count = 0; MBBI != E && Count < Limit; ++MBBI) {
|
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
// Skip DBG_VALUE instructions. Otherwise debug info can affect the
|
|
|
|
// optimization by changing how far we scan.
|
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Now that we know this is a real instruction, count it.
|
|
|
|
++Count;
|
|
|
|
|
2015-03-06 22:42:10 +00:00
|
|
|
bool CanMergeOpc = Opc == MI->getOpcode();
|
|
|
|
SExtIdx = -1;
|
|
|
|
if (!CanMergeOpc) {
|
|
|
|
bool IsValidLdStrOpc;
|
|
|
|
unsigned NonSExtOpc = getMatchingNonSExtOpcode(Opc, &IsValidLdStrOpc);
|
|
|
|
if (!IsValidLdStrOpc)
|
|
|
|
continue;
|
|
|
|
// Opc will be the first instruction in the pair.
|
|
|
|
SExtIdx = NonSExtOpc == (unsigned)Opc ? 1 : 0;
|
|
|
|
CanMergeOpc = NonSExtOpc == getMatchingNonSExtOpcode(MI->getOpcode());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CanMergeOpc && MI->getOperand(2).isImm()) {
|
2014-03-29 10:18:08 +00:00
|
|
|
// If we've found another instruction with the same opcode, check to see
|
|
|
|
// if the base and offset are compatible with our starting instruction.
|
|
|
|
// These instructions all have scaled immediate operands, so we just
|
|
|
|
// check for +1/-1. Make sure to check the new instruction offset is
|
|
|
|
// actually an immediate and not a symbolic reference destined for
|
|
|
|
// a relocation.
|
|
|
|
//
|
|
|
|
// Pairwise instructions have a 7-bit signed offset field. Single insns
|
|
|
|
// have a 12-bit unsigned offset field. To be a valid combine, the
|
|
|
|
// final offset must be in range.
|
|
|
|
unsigned MIBaseReg = MI->getOperand(1).getReg();
|
|
|
|
int MIOffset = MI->getOperand(2).getImm();
|
|
|
|
if (BaseReg == MIBaseReg && ((Offset == MIOffset + OffsetStride) ||
|
|
|
|
(Offset + OffsetStride == MIOffset))) {
|
|
|
|
int MinOffset = Offset < MIOffset ? Offset : MIOffset;
|
|
|
|
// If this is a volatile load/store that otherwise matched, stop looking
|
|
|
|
// as something is going on that we don't have enough information to
|
|
|
|
// safely transform. Similarly, stop if we see a hint to avoid pairs.
|
|
|
|
if (MI->hasOrderedMemoryRef() || TII->isLdStPairSuppressed(MI))
|
|
|
|
return E;
|
|
|
|
// If the resultant immediate offset of merging these instructions
|
|
|
|
// is out of range for a pairwise instruction, bail and keep looking.
|
|
|
|
bool MIIsUnscaled = isUnscaledLdst(MI->getOpcode());
|
|
|
|
if (!inBoundsForPair(MIIsUnscaled, MinOffset, OffsetStride)) {
|
|
|
|
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
|
2015-05-21 21:36:46 +00:00
|
|
|
if (MI->mayLoadOrStore())
|
|
|
|
MemInsns.push_back(MI);
|
2014-03-29 10:18:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If the alignment requirements of the paired (scaled) instruction
|
|
|
|
// can't express the offset of the unscaled input, bail and keep
|
|
|
|
// looking.
|
2014-05-24 12:50:23 +00:00
|
|
|
if (IsUnscaled && EnableAArch64UnscaledMemOp &&
|
2014-03-29 10:18:08 +00:00
|
|
|
(alignTo(MinOffset, OffsetStride) != MinOffset)) {
|
|
|
|
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
|
2015-05-21 21:36:46 +00:00
|
|
|
if (MI->mayLoadOrStore())
|
|
|
|
MemInsns.push_back(MI);
|
2014-03-29 10:18:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// If the destination register of the loads is the same register, bail
|
|
|
|
// and keep looking. A load-pair instruction with both destination
|
|
|
|
// registers the same is UNPREDICTABLE and will result in an exception.
|
2014-06-04 12:36:28 +00:00
|
|
|
if (MayLoad && Reg == MI->getOperand(0).getReg()) {
|
2014-03-29 10:18:08 +00:00
|
|
|
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
|
2015-05-21 21:36:46 +00:00
|
|
|
if (MI->mayLoadOrStore())
|
|
|
|
MemInsns.push_back(MI);
|
2014-03-29 10:18:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the Rt of the second instruction was not modified or used between
|
2015-05-21 21:36:46 +00:00
|
|
|
// the two instructions and none of the instructions between the second
|
|
|
|
// and first alias with the second, we can combine the second into the
|
|
|
|
// first.
|
2014-03-29 10:18:08 +00:00
|
|
|
if (!ModifiedRegs[MI->getOperand(0).getReg()] &&
|
[AArch64] Remove an overly conservative check when generating store pairs.
Store instructions do not modify register values and therefore it's safe
to form a store pair even if the source register has been read in between
the two store instructions.
Previously, the read of w1 (see below) prevented the formation of a stp.
str w0, [x2]
ldr w8, [x2, #8]
add w0, w8, w1
str w1, [x2, #4]
ret
We now generate the following code.
stp w0, w1, [x2]
ldr w8, [x2, #8]
add w0, w8, w1
ret
All correctness tests with -Ofast on A57 with Spec200x and EEMBC pass.
Performance results for SPEC2K were within noise.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239432 91177308-0d34-0410-b5e6-96231b3b80d8
2015-06-09 20:59:41 +00:00
|
|
|
!(MI->mayLoad() && UsedRegs[MI->getOperand(0).getReg()]) &&
|
2015-05-21 21:36:46 +00:00
|
|
|
!mayAlias(MI, MemInsns, TII)) {
|
2014-06-04 12:36:28 +00:00
|
|
|
MergeForward = false;
|
2014-03-29 10:18:08 +00:00
|
|
|
return MBBI;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Likewise, if the Rt of the first instruction is not modified or used
|
2015-05-21 21:36:46 +00:00
|
|
|
// between the two instructions and none of the instructions between the
|
|
|
|
// first and the second alias with the first, we can combine the first
|
|
|
|
// into the second.
|
2014-03-29 10:18:08 +00:00
|
|
|
if (!ModifiedRegs[FirstMI->getOperand(0).getReg()] &&
|
[AArch64] Remove an overly conservative check when generating store pairs.
Store instructions do not modify register values and therefore it's safe
to form a store pair even if the source register has been read in between
the two store instructions.
Previously, the read of w1 (see below) prevented the formation of a stp.
str w0, [x2]
ldr w8, [x2, #8]
add w0, w8, w1
str w1, [x2, #4]
ret
We now generate the following code.
stp w0, w1, [x2]
ldr w8, [x2, #8]
add w0, w8, w1
ret
All correctness tests with -Ofast on A57 with Spec200x and EEMBC pass.
Performance results for SPEC2K were within noise.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239432 91177308-0d34-0410-b5e6-96231b3b80d8
2015-06-09 20:59:41 +00:00
|
|
|
!(FirstMI->mayLoad() &&
|
|
|
|
UsedRegs[FirstMI->getOperand(0).getReg()]) &&
|
2015-05-21 21:36:46 +00:00
|
|
|
!mayAlias(FirstMI, MemInsns, TII)) {
|
2014-06-04 12:36:28 +00:00
|
|
|
MergeForward = true;
|
2014-03-29 10:18:08 +00:00
|
|
|
return MBBI;
|
|
|
|
}
|
|
|
|
// Unable to combine these instructions due to interference in between.
|
|
|
|
// Keep looking.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:36:46 +00:00
|
|
|
// If the instruction wasn't a matching load or store. Stop searching if we
|
|
|
|
// encounter a call instruction that might modify memory.
|
|
|
|
if (MI->isCall())
|
2014-03-29 10:18:08 +00:00
|
|
|
return E;
|
|
|
|
|
|
|
|
// Update modified / uses register lists.
|
|
|
|
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
|
|
|
|
|
|
|
|
// Otherwise, if the base register is modified, we have no match, so
|
|
|
|
// return early.
|
|
|
|
if (ModifiedRegs[BaseReg])
|
|
|
|
return E;
|
2015-05-21 21:36:46 +00:00
|
|
|
|
|
|
|
// Update list of instructions that read/write memory.
|
|
|
|
if (MI->mayLoadOrStore())
|
|
|
|
MemInsns.push_back(MI);
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator
|
2014-05-24 12:50:23 +00:00
|
|
|
AArch64LoadStoreOpt::mergePreIdxUpdateInsn(MachineBasicBlock::iterator I,
|
|
|
|
MachineBasicBlock::iterator Update) {
|
|
|
|
assert((Update->getOpcode() == AArch64::ADDXri ||
|
|
|
|
Update->getOpcode() == AArch64::SUBXri) &&
|
2014-03-29 10:18:08 +00:00
|
|
|
"Unexpected base register update instruction to merge!");
|
|
|
|
MachineBasicBlock::iterator NextI = I;
|
|
|
|
// Return the instruction following the merged instruction, which is
|
|
|
|
// the instruction following our unmerged load. Unless that's the add/sub
|
|
|
|
// instruction we're merging, in which case it's the one after that.
|
|
|
|
if (++NextI == Update)
|
|
|
|
++NextI;
|
|
|
|
|
|
|
|
int Value = Update->getOperand(2).getImm();
|
2014-05-24 12:50:23 +00:00
|
|
|
assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
|
2014-03-29 10:18:08 +00:00
|
|
|
"Can't merge 1 << 12 offset into pre-indexed load / store");
|
2014-05-24 12:50:23 +00:00
|
|
|
if (Update->getOpcode() == AArch64::SUBXri)
|
2014-03-29 10:18:08 +00:00
|
|
|
Value = -Value;
|
|
|
|
|
|
|
|
unsigned NewOpc = getPreIndexedOpcode(I->getOpcode());
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
|
2014-05-22 11:56:20 +00:00
|
|
|
.addOperand(Update->getOperand(0))
|
2014-03-29 10:18:08 +00:00
|
|
|
.addOperand(I->getOperand(0))
|
|
|
|
.addOperand(I->getOperand(1))
|
|
|
|
.addImm(Value);
|
|
|
|
(void)MIB;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Creating pre-indexed load/store.");
|
|
|
|
DEBUG(dbgs() << " Replacing instructions:\n ");
|
|
|
|
DEBUG(I->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << " ");
|
|
|
|
DEBUG(Update->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << " with instruction:\n ");
|
|
|
|
DEBUG(((MachineInstr *)MIB)->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << "\n");
|
|
|
|
|
|
|
|
// Erase the old instructions for the block.
|
|
|
|
I->eraseFromParent();
|
|
|
|
Update->eraseFromParent();
|
|
|
|
|
|
|
|
return NextI;
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
MachineBasicBlock::iterator AArch64LoadStoreOpt::mergePostIdxUpdateInsn(
|
|
|
|
MachineBasicBlock::iterator I, MachineBasicBlock::iterator Update) {
|
|
|
|
assert((Update->getOpcode() == AArch64::ADDXri ||
|
|
|
|
Update->getOpcode() == AArch64::SUBXri) &&
|
2014-03-29 10:18:08 +00:00
|
|
|
"Unexpected base register update instruction to merge!");
|
|
|
|
MachineBasicBlock::iterator NextI = I;
|
|
|
|
// Return the instruction following the merged instruction, which is
|
|
|
|
// the instruction following our unmerged load. Unless that's the add/sub
|
|
|
|
// instruction we're merging, in which case it's the one after that.
|
|
|
|
if (++NextI == Update)
|
|
|
|
++NextI;
|
|
|
|
|
|
|
|
int Value = Update->getOperand(2).getImm();
|
2014-05-24 12:50:23 +00:00
|
|
|
assert(AArch64_AM::getShiftValue(Update->getOperand(3).getImm()) == 0 &&
|
2014-03-29 10:18:08 +00:00
|
|
|
"Can't merge 1 << 12 offset into post-indexed load / store");
|
2014-05-24 12:50:23 +00:00
|
|
|
if (Update->getOpcode() == AArch64::SUBXri)
|
2014-03-29 10:18:08 +00:00
|
|
|
Value = -Value;
|
|
|
|
|
|
|
|
unsigned NewOpc = getPostIndexedOpcode(I->getOpcode());
|
|
|
|
MachineInstrBuilder MIB =
|
|
|
|
BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
|
2014-05-22 11:56:20 +00:00
|
|
|
.addOperand(Update->getOperand(0))
|
2014-03-29 10:18:08 +00:00
|
|
|
.addOperand(I->getOperand(0))
|
|
|
|
.addOperand(I->getOperand(1))
|
|
|
|
.addImm(Value);
|
|
|
|
(void)MIB;
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "Creating post-indexed load/store.");
|
|
|
|
DEBUG(dbgs() << " Replacing instructions:\n ");
|
|
|
|
DEBUG(I->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << " ");
|
|
|
|
DEBUG(Update->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << " with instruction:\n ");
|
|
|
|
DEBUG(((MachineInstr *)MIB)->print(dbgs()));
|
|
|
|
DEBUG(dbgs() << "\n");
|
|
|
|
|
|
|
|
// Erase the old instructions for the block.
|
|
|
|
I->eraseFromParent();
|
|
|
|
Update->eraseFromParent();
|
|
|
|
|
|
|
|
return NextI;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isMatchingUpdateInsn(MachineInstr *MI, unsigned BaseReg,
|
|
|
|
int Offset) {
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
break;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::SUBXri:
|
2014-03-29 10:18:08 +00:00
|
|
|
// Negate the offset for a SUB instruction.
|
|
|
|
Offset *= -1;
|
|
|
|
// FALLTHROUGH
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::ADDXri:
|
2014-03-29 10:18:08 +00:00
|
|
|
// Make sure it's a vanilla immediate operand, not a relocation or
|
|
|
|
// anything else we can't handle.
|
|
|
|
if (!MI->getOperand(2).isImm())
|
|
|
|
break;
|
|
|
|
// Watch out for 1 << 12 shifted value.
|
2014-05-24 12:50:23 +00:00
|
|
|
if (AArch64_AM::getShiftValue(MI->getOperand(3).getImm()))
|
2014-03-29 10:18:08 +00:00
|
|
|
break;
|
|
|
|
// If the instruction has the base register as source and dest and the
|
|
|
|
// immediate will fit in a signed 9-bit integer, then we have a match.
|
|
|
|
if (MI->getOperand(0).getReg() == BaseReg &&
|
|
|
|
MI->getOperand(1).getReg() == BaseReg &&
|
|
|
|
MI->getOperand(2).getImm() <= 255 &&
|
|
|
|
MI->getOperand(2).getImm() >= -256) {
|
|
|
|
// If we have a non-zero Offset, we check that it matches the amount
|
|
|
|
// we're adding to the register.
|
|
|
|
if (!Offset || Offset == MI->getOperand(2).getImm())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnForward(
|
|
|
|
MachineBasicBlock::iterator I, unsigned Limit, int Value) {
|
2014-03-29 10:18:08 +00:00
|
|
|
MachineBasicBlock::iterator E = I->getParent()->end();
|
|
|
|
MachineInstr *MemMI = I;
|
|
|
|
MachineBasicBlock::iterator MBBI = I;
|
|
|
|
const MachineFunction &MF = *MemMI->getParent()->getParent();
|
|
|
|
|
|
|
|
unsigned DestReg = MemMI->getOperand(0).getReg();
|
|
|
|
unsigned BaseReg = MemMI->getOperand(1).getReg();
|
|
|
|
int Offset = MemMI->getOperand(2).getImm() *
|
|
|
|
TII->getRegClass(MemMI->getDesc(), 0, TRI, MF)->getSize();
|
|
|
|
|
|
|
|
// If the base register overlaps the destination register, we can't
|
|
|
|
// merge the update.
|
|
|
|
if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// Scan forward looking for post-index opportunities.
|
|
|
|
// Updating instructions can't be formed if the memory insn already
|
|
|
|
// has an offset other than the value we're looking for.
|
|
|
|
if (Offset != Value)
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// Track which registers have been modified and used between the first insn
|
|
|
|
// (inclusive) and the second insn.
|
|
|
|
BitVector ModifiedRegs, UsedRegs;
|
|
|
|
ModifiedRegs.resize(TRI->getNumRegs());
|
|
|
|
UsedRegs.resize(TRI->getNumRegs());
|
|
|
|
++MBBI;
|
|
|
|
for (unsigned Count = 0; MBBI != E; ++MBBI) {
|
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
// Skip DBG_VALUE instructions. Otherwise debug info can affect the
|
|
|
|
// optimization by changing how far we scan.
|
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Now that we know this is a real instruction, count it.
|
|
|
|
++Count;
|
|
|
|
|
|
|
|
// If we found a match, return it.
|
|
|
|
if (isMatchingUpdateInsn(MI, BaseReg, Value))
|
|
|
|
return MBBI;
|
|
|
|
|
|
|
|
// Update the status of what the instruction clobbered and used.
|
|
|
|
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
|
|
|
|
|
|
|
|
// Otherwise, if the base register is used or modified, we have no match, so
|
|
|
|
// return early.
|
|
|
|
if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
|
|
|
|
MachineBasicBlock::iterator I, unsigned Limit) {
|
2014-03-29 10:18:08 +00:00
|
|
|
MachineBasicBlock::iterator B = I->getParent()->begin();
|
|
|
|
MachineBasicBlock::iterator E = I->getParent()->end();
|
|
|
|
MachineInstr *MemMI = I;
|
|
|
|
MachineBasicBlock::iterator MBBI = I;
|
|
|
|
const MachineFunction &MF = *MemMI->getParent()->getParent();
|
|
|
|
|
|
|
|
unsigned DestReg = MemMI->getOperand(0).getReg();
|
|
|
|
unsigned BaseReg = MemMI->getOperand(1).getReg();
|
|
|
|
int Offset = MemMI->getOperand(2).getImm();
|
|
|
|
unsigned RegSize = TII->getRegClass(MemMI->getDesc(), 0, TRI, MF)->getSize();
|
|
|
|
|
|
|
|
// If the load/store is the first instruction in the block, there's obviously
|
|
|
|
// not any matching update. Ditto if the memory offset isn't zero.
|
|
|
|
if (MBBI == B || Offset != 0)
|
|
|
|
return E;
|
|
|
|
// If the base register overlaps the destination register, we can't
|
|
|
|
// merge the update.
|
|
|
|
if (DestReg == BaseReg || TRI->isSubRegister(BaseReg, DestReg))
|
|
|
|
return E;
|
|
|
|
|
|
|
|
// Track which registers have been modified and used between the first insn
|
|
|
|
// (inclusive) and the second insn.
|
|
|
|
BitVector ModifiedRegs, UsedRegs;
|
|
|
|
ModifiedRegs.resize(TRI->getNumRegs());
|
|
|
|
UsedRegs.resize(TRI->getNumRegs());
|
|
|
|
--MBBI;
|
|
|
|
for (unsigned Count = 0; MBBI != B; --MBBI) {
|
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
// Skip DBG_VALUE instructions. Otherwise debug info can affect the
|
|
|
|
// optimization by changing how far we scan.
|
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Now that we know this is a real instruction, count it.
|
|
|
|
++Count;
|
|
|
|
|
|
|
|
// If we found a match, return it.
|
|
|
|
if (isMatchingUpdateInsn(MI, BaseReg, RegSize))
|
|
|
|
return MBBI;
|
|
|
|
|
|
|
|
// Update the status of what the instruction clobbered and used.
|
|
|
|
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
|
|
|
|
|
|
|
|
// Otherwise, if the base register is used or modified, we have no match, so
|
|
|
|
// return early.
|
|
|
|
if (ModifiedRegs[BaseReg] || UsedRegs[BaseReg])
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
return E;
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB) {
|
2014-03-29 10:18:08 +00:00
|
|
|
bool Modified = false;
|
|
|
|
// Two tranformations to do here:
|
|
|
|
// 1) Find loads and stores that can be merged into a single load or store
|
|
|
|
// pair instruction.
|
|
|
|
// e.g.,
|
|
|
|
// ldr x0, [x2]
|
|
|
|
// ldr x1, [x2, #8]
|
|
|
|
// ; becomes
|
|
|
|
// ldp x0, x1, [x2]
|
|
|
|
// 2) Find base register updates that can be merged into the load or store
|
|
|
|
// as a base-reg writeback.
|
|
|
|
// e.g.,
|
|
|
|
// ldr x0, [x2]
|
|
|
|
// add x2, x2, #4
|
|
|
|
// ; becomes
|
|
|
|
// ldr x0, [x2], #4
|
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
|
|
|
|
MBBI != E;) {
|
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
switch (MI->getOpcode()) {
|
|
|
|
default:
|
|
|
|
// Just move on to the next instruction.
|
|
|
|
++MBBI;
|
|
|
|
break;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRSui:
|
|
|
|
case AArch64::STRDui:
|
|
|
|
case AArch64::STRQui:
|
|
|
|
case AArch64::STRXui:
|
|
|
|
case AArch64::STRWui:
|
|
|
|
case AArch64::LDRSui:
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
case AArch64::LDRQui:
|
|
|
|
case AArch64::LDRXui:
|
|
|
|
case AArch64::LDRWui:
|
2015-01-24 01:25:54 +00:00
|
|
|
case AArch64::LDRSWui:
|
2014-03-29 10:18:08 +00:00
|
|
|
// do the unscaled versions as well
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STURSi:
|
|
|
|
case AArch64::STURDi:
|
|
|
|
case AArch64::STURQi:
|
|
|
|
case AArch64::STURWi:
|
|
|
|
case AArch64::STURXi:
|
|
|
|
case AArch64::LDURSi:
|
|
|
|
case AArch64::LDURDi:
|
|
|
|
case AArch64::LDURQi:
|
|
|
|
case AArch64::LDURWi:
|
2015-01-24 01:25:54 +00:00
|
|
|
case AArch64::LDURXi:
|
|
|
|
case AArch64::LDURSWi: {
|
2014-03-29 10:18:08 +00:00
|
|
|
// If this is a volatile load/store, don't mess with it.
|
|
|
|
if (MI->hasOrderedMemoryRef()) {
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Make sure this is a reg+imm (as opposed to an address reloc).
|
|
|
|
if (!MI->getOperand(2).isImm()) {
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Check if this load/store has a hint to avoid pair formation.
|
2014-05-24 12:50:23 +00:00
|
|
|
// MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
|
2014-03-29 10:18:08 +00:00
|
|
|
if (TII->isLdStPairSuppressed(MI)) {
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Look ahead up to ScanLimit instructions for a pairable instruction.
|
2014-06-04 12:36:28 +00:00
|
|
|
bool MergeForward = false;
|
2015-03-06 22:42:10 +00:00
|
|
|
int SExtIdx = -1;
|
2014-03-29 10:18:08 +00:00
|
|
|
MachineBasicBlock::iterator Paired =
|
2015-03-06 22:42:10 +00:00
|
|
|
findMatchingInsn(MBBI, MergeForward, SExtIdx, ScanLimit);
|
2014-03-29 10:18:08 +00:00
|
|
|
if (Paired != E) {
|
|
|
|
// Merge the loads into a pair. Keeping the iterator straight is a
|
|
|
|
// pain, so we let the merge routine tell us what the next instruction
|
|
|
|
// is after it's done mucking about.
|
2015-03-06 22:42:10 +00:00
|
|
|
MBBI = mergePairedInsns(MBBI, Paired, MergeForward, SExtIdx);
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
Modified = true;
|
|
|
|
++NumPairCreated;
|
|
|
|
if (isUnscaledLdst(MI->getOpcode()))
|
|
|
|
++NumUnscaledPairCreated;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FIXME: Do the other instructions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
|
|
|
|
MBBI != E;) {
|
|
|
|
MachineInstr *MI = MBBI;
|
|
|
|
// Do update merging. It's simpler to keep this separate from the above
|
|
|
|
// switch, though not strictly necessary.
|
2015-05-18 20:27:55 +00:00
|
|
|
unsigned Opc = MI->getOpcode();
|
2014-03-29 10:18:08 +00:00
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
// Just move on to the next instruction.
|
|
|
|
++MBBI;
|
|
|
|
break;
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STRSui:
|
|
|
|
case AArch64::STRDui:
|
|
|
|
case AArch64::STRQui:
|
|
|
|
case AArch64::STRXui:
|
|
|
|
case AArch64::STRWui:
|
|
|
|
case AArch64::LDRSui:
|
|
|
|
case AArch64::LDRDui:
|
|
|
|
case AArch64::LDRQui:
|
|
|
|
case AArch64::LDRXui:
|
|
|
|
case AArch64::LDRWui:
|
2014-03-29 10:18:08 +00:00
|
|
|
// do the unscaled versions as well
|
2014-05-24 12:50:23 +00:00
|
|
|
case AArch64::STURSi:
|
|
|
|
case AArch64::STURDi:
|
|
|
|
case AArch64::STURQi:
|
|
|
|
case AArch64::STURWi:
|
|
|
|
case AArch64::STURXi:
|
|
|
|
case AArch64::LDURSi:
|
|
|
|
case AArch64::LDURDi:
|
|
|
|
case AArch64::LDURQi:
|
|
|
|
case AArch64::LDURWi:
|
|
|
|
case AArch64::LDURXi: {
|
2014-03-29 10:18:08 +00:00
|
|
|
// Make sure this is a reg+imm (as opposed to an address reloc).
|
|
|
|
if (!MI->getOperand(2).isImm()) {
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Look ahead up to ScanLimit instructions for a mergable instruction.
|
|
|
|
MachineBasicBlock::iterator Update =
|
|
|
|
findMatchingUpdateInsnForward(MBBI, ScanLimit, 0);
|
|
|
|
if (Update != E) {
|
|
|
|
// Merge the update into the ld/st.
|
|
|
|
MBBI = mergePostIdxUpdateInsn(MBBI, Update);
|
|
|
|
Modified = true;
|
|
|
|
++NumPostFolded;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Don't know how to handle pre/post-index versions, so move to the next
|
|
|
|
// instruction.
|
|
|
|
if (isUnscaledLdst(Opc)) {
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look back to try to find a pre-index instruction. For example,
|
|
|
|
// add x0, x0, #8
|
|
|
|
// ldr x1, [x0]
|
|
|
|
// merged into:
|
|
|
|
// ldr x1, [x0, #8]!
|
|
|
|
Update = findMatchingUpdateInsnBackward(MBBI, ScanLimit);
|
|
|
|
if (Update != E) {
|
|
|
|
// Merge the update into the ld/st.
|
|
|
|
MBBI = mergePreIdxUpdateInsn(MBBI, Update);
|
|
|
|
Modified = true;
|
|
|
|
++NumPreFolded;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Look forward to try to find a post-index instruction. For example,
|
|
|
|
// ldr x1, [x0, #64]
|
|
|
|
// add x0, x0, #64
|
|
|
|
// merged into:
|
[ARM64] Fix wrong comment in load/store optimization pass.
ldr x1, [x0, #64]
add x0, x0, #64
->
ldr x1, [x0], #64
is not a valid transformation, the correct transformation (and what the code actually does) is:
ldr x1, [x0, #64]
add x0, x0, #64
->
ldr x1, [x0, #64]!
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208998 91177308-0d34-0410-b5e6-96231b3b80d8
2014-05-16 16:50:13 +00:00
|
|
|
// ldr x1, [x0, #64]!
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
// The immediate in the load/store is scaled by the size of the register
|
|
|
|
// being loaded. The immediate in the add we're looking for,
|
|
|
|
// however, is not, so adjust here.
|
|
|
|
int Value = MI->getOperand(2).getImm() *
|
|
|
|
TII->getRegClass(MI->getDesc(), 0, TRI, *(MBB.getParent()))
|
|
|
|
->getSize();
|
|
|
|
Update = findMatchingUpdateInsnForward(MBBI, ScanLimit, Value);
|
|
|
|
if (Update != E) {
|
|
|
|
// Merge the update into the ld/st.
|
|
|
|
MBBI = mergePreIdxUpdateInsn(MBBI, Update);
|
|
|
|
Modified = true;
|
|
|
|
++NumPreFolded;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing found. Just move to the next instruction.
|
|
|
|
++MBBI;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FIXME: Do the other instructions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
2014-05-24 12:50:23 +00:00
|
|
|
bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
|
2015-01-28 03:51:33 +00:00
|
|
|
TII = static_cast<const AArch64InstrInfo *>(Fn.getSubtarget().getInstrInfo());
|
|
|
|
TRI = Fn.getSubtarget().getRegisterInfo();
|
2014-03-29 10:18:08 +00:00
|
|
|
|
|
|
|
bool Modified = false;
|
2014-04-02 18:00:53 +00:00
|
|
|
for (auto &MBB : Fn)
|
2014-03-29 10:18:08 +00:00
|
|
|
Modified |= optimizeBlock(MBB);
|
|
|
|
|
|
|
|
return Modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Do we need/want a pre-alloc pass like ARM has to try to keep
|
|
|
|
// loads and stores near one another?
|
|
|
|
|
|
|
|
/// createARMLoadStoreOptimizationPass - returns an instance of the load / store
|
|
|
|
/// optimization pass.
|
2014-05-24 12:50:23 +00:00
|
|
|
FunctionPass *llvm::createAArch64LoadStoreOptimizationPass() {
|
|
|
|
return new AArch64LoadStoreOpt();
|
2014-03-29 10:18:08 +00:00
|
|
|
}
|