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

Commit 38a9a53

Browse files
author
Jun Bum Lim
committed
[AArch64]Merge halfword loads into a 32-bit load
Convert two halfword loads into a single 32-bit word load with bitfield extract instructions. For example : ldrh w0, [x2] ldrh w1, [x2, #2] becomes ldr w0, [x2] ubfx w1, w0, #16, #16 and w0, w0, #ffff git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@250719 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent d497ef1 commit 38a9a53

File tree

2 files changed

+264
-45
lines changed

2 files changed

+264
-45
lines changed

lib/Target/AArch64/AArch64LoadStoreOptimizer.cpp

+215-45
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ STATISTIC(NumPostFolded, "Number of post-index updates folded");
4141
STATISTIC(NumPreFolded, "Number of pre-index updates folded");
4242
STATISTIC(NumUnscaledPairCreated,
4343
"Number of load/store from unscaled generated");
44+
STATISTIC(NumSmallTypeMerged, "Number of small type loads merged");
4445

4546
static cl::opt<unsigned> ScanLimit("aarch64-load-store-scan-limit",
4647
cl::init(20), cl::Hidden);
@@ -77,12 +78,13 @@ typedef struct LdStPairFlags {
7778

7879
struct AArch64LoadStoreOpt : public MachineFunctionPass {
7980
static char ID;
80-
AArch64LoadStoreOpt() : MachineFunctionPass(ID) {
81+
AArch64LoadStoreOpt() : MachineFunctionPass(ID), IsStrictAlign(false) {
8182
initializeAArch64LoadStoreOptPass(*PassRegistry::getPassRegistry());
8283
}
8384

8485
const AArch64InstrInfo *TII;
8586
const TargetRegisterInfo *TRI;
87+
bool IsStrictAlign;
8688

8789
// Scan the instructions looking for a load/store that can be combined
8890
// with the current instruction into a load/store pair.
@@ -122,6 +124,9 @@ struct AArch64LoadStoreOpt : public MachineFunctionPass {
122124
mergeUpdateInsn(MachineBasicBlock::iterator I,
123125
MachineBasicBlock::iterator Update, bool IsPreIdx);
124126

127+
// Find and merge foldable ldr/str instructions.
128+
bool tryToMergeLdStInst(MachineBasicBlock::iterator &MBBI);
129+
125130
bool optimizeBlock(MachineBasicBlock &MBB);
126131

127132
bool runOnMachineFunction(MachineFunction &Fn) override;
@@ -151,6 +156,7 @@ static bool isUnscaledLdSt(unsigned Opc) {
151156
case AArch64::LDURWi:
152157
case AArch64::LDURXi:
153158
case AArch64::LDURSWi:
159+
case AArch64::LDURHHi:
154160
return true;
155161
}
156162
}
@@ -159,6 +165,20 @@ static bool isUnscaledLdSt(MachineInstr *MI) {
159165
return isUnscaledLdSt(MI->getOpcode());
160166
}
161167

168+
static bool isSmallTypeLdMerge(unsigned Opc) {
169+
switch (Opc) {
170+
default:
171+
return false;
172+
case AArch64::LDRHHui:
173+
case AArch64::LDURHHi:
174+
return true;
175+
// FIXME: Add other instructions (e.g, LDRBBui, LDURSHWi, LDRSHWui, etc.).
176+
}
177+
}
178+
static bool isSmallTypeLdMerge(MachineInstr *MI) {
179+
return isSmallTypeLdMerge(MI->getOpcode());
180+
}
181+
162182
// Scaling factor for unscaled load or store.
163183
static int getMemScale(MachineInstr *MI) {
164184
switch (MI->getOpcode()) {
@@ -168,6 +188,7 @@ static int getMemScale(MachineInstr *MI) {
168188
case AArch64::STRBBui:
169189
return 1;
170190
case AArch64::LDRHHui:
191+
case AArch64::LDURHHi:
171192
case AArch64::STRHHui:
172193
return 2;
173194
case AArch64::LDRSui:
@@ -238,6 +259,8 @@ static unsigned getMatchingNonSExtOpcode(unsigned Opc,
238259
case AArch64::STURSi:
239260
case AArch64::LDRSui:
240261
case AArch64::LDURSi:
262+
case AArch64::LDRHHui:
263+
case AArch64::LDURHHi:
241264
return Opc;
242265
case AArch64::LDRSWui:
243266
return AArch64::LDRWui;
@@ -283,6 +306,10 @@ static unsigned getMatchingPairOpcode(unsigned Opc) {
283306
case AArch64::LDRSWui:
284307
case AArch64::LDURSWi:
285308
return AArch64::LDPSWi;
309+
case AArch64::LDRHHui:
310+
return AArch64::LDRWui;
311+
case AArch64::LDURHHi:
312+
return AArch64::LDURWi;
286313
}
287314
}
288315

@@ -440,6 +467,21 @@ static const MachineOperand &getLdStOffsetOp(const MachineInstr *MI) {
440467
return MI->getOperand(Idx);
441468
}
442469

470+
// Copy MachineMemOperands from Op0 and Op1 to a new array assigned to MI.
471+
static void concatenateMemOperands(MachineInstr *MI, MachineInstr *Op0,
472+
MachineInstr *Op1) {
473+
assert(MI->memoperands_empty() && "expected a new machineinstr");
474+
size_t numMemRefs = (Op0->memoperands_end() - Op0->memoperands_begin()) +
475+
(Op1->memoperands_end() - Op1->memoperands_begin());
476+
477+
MachineFunction *MF = MI->getParent()->getParent();
478+
MachineSDNode::mmo_iterator MemBegin = MF->allocateMemRefsArray(numMemRefs);
479+
MachineSDNode::mmo_iterator MemEnd =
480+
std::copy(Op0->memoperands_begin(), Op0->memoperands_end(), MemBegin);
481+
MemEnd = std::copy(Op1->memoperands_begin(), Op1->memoperands_end(), MemEnd);
482+
MI->setMemRefs(MemBegin, MemEnd);
483+
}
484+
443485
MachineBasicBlock::iterator
444486
AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
445487
MachineBasicBlock::iterator Paired,
@@ -484,8 +526,78 @@ AArch64LoadStoreOpt::mergePairedInsns(MachineBasicBlock::iterator I,
484526
RtMI = I;
485527
Rt2MI = Paired;
486528
}
487-
// Handle Unscaled
529+
488530
int OffsetImm = getLdStOffsetOp(RtMI).getImm();
531+
532+
if (isSmallTypeLdMerge(Opc)) {
533+
// Change the scaled offset from small to large type.
534+
if (!IsUnscaled)
535+
OffsetImm /= 2;
536+
MachineInstr *RtNewDest = MergeForward ? I : Paired;
537+
// Construct the new load instruction.
538+
// FIXME: currently we support only halfword unsigned load. We need to
539+
// handle byte type, signed, and store instructions as well.
540+
MachineInstr *NewMemMI, *BitExtMI1, *BitExtMI2;
541+
NewMemMI = BuildMI(*I->getParent(), I, I->getDebugLoc(), TII->get(NewOpc))
542+
.addOperand(getLdStRegOp(RtNewDest))
543+
.addOperand(BaseRegOp)
544+
.addImm(OffsetImm);
545+
546+
// Copy MachineMemOperands from the original loads.
547+
concatenateMemOperands(NewMemMI, I, Paired);
548+
549+
DEBUG(
550+
dbgs()
551+
<< "Creating the new load and extract. Replacing instructions:\n ");
552+
DEBUG(I->print(dbgs()));
553+
DEBUG(dbgs() << " ");
554+
DEBUG(Paired->print(dbgs()));
555+
DEBUG(dbgs() << " with instructions:\n ");
556+
DEBUG((NewMemMI)->print(dbgs()));
557+
558+
MachineInstr *ExtDestMI = MergeForward ? Paired : I;
559+
if (ExtDestMI == Rt2MI) {
560+
// Create the bitfield extract for high half.
561+
BitExtMI1 = BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
562+
TII->get(AArch64::UBFMWri))
563+
.addOperand(getLdStRegOp(Rt2MI))
564+
.addReg(getLdStRegOp(RtNewDest).getReg())
565+
.addImm(16)
566+
.addImm(31);
567+
// Create the bitfield extract for low half.
568+
BitExtMI2 = BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
569+
TII->get(AArch64::ANDWri))
570+
.addOperand(getLdStRegOp(RtMI))
571+
.addReg(getLdStRegOp(RtNewDest).getReg())
572+
.addImm(15);
573+
} else {
574+
// Create the bitfield extract for low half.
575+
BitExtMI1 = BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
576+
TII->get(AArch64::ANDWri))
577+
.addOperand(getLdStRegOp(RtMI))
578+
.addReg(getLdStRegOp(RtNewDest).getReg())
579+
.addImm(15);
580+
// Create the bitfield extract for high half.
581+
BitExtMI2 = BuildMI(*I->getParent(), InsertionPoint, I->getDebugLoc(),
582+
TII->get(AArch64::UBFMWri))
583+
.addOperand(getLdStRegOp(Rt2MI))
584+
.addReg(getLdStRegOp(RtNewDest).getReg())
585+
.addImm(16)
586+
.addImm(31);
587+
}
588+
DEBUG(dbgs() << " ");
589+
DEBUG((BitExtMI1)->print(dbgs()));
590+
DEBUG(dbgs() << " ");
591+
DEBUG((BitExtMI2)->print(dbgs()));
592+
DEBUG(dbgs() << "\n");
593+
594+
// Erase the old instructions.
595+
I->eraseFromParent();
596+
Paired->eraseFromParent();
597+
return NextI;
598+
}
599+
600+
// Handle Unscaled
489601
if (IsUnscaled)
490602
OffsetImm /= OffsetStride;
491603

@@ -622,8 +734,7 @@ static bool mayAlias(MachineInstr *MIa,
622734
/// be combined with the current instruction into a load/store pair.
623735
MachineBasicBlock::iterator
624736
AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
625-
LdStPairFlags &Flags,
626-
unsigned Limit) {
737+
LdStPairFlags &Flags, unsigned Limit) {
627738
MachineBasicBlock::iterator E = I->getParent()->end();
628739
MachineBasicBlock::iterator MBBI = I;
629740
MachineInstr *FirstMI = I;
@@ -645,7 +756,8 @@ AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
645756
// range, plus allow an extra one in case we find a later insn that matches
646757
// with Offset-1)
647758
int OffsetStride = IsUnscaled ? getMemScale(FirstMI) : 1;
648-
if (!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
759+
if (!isSmallTypeLdMerge(Opc) &&
760+
!inBoundsForPair(IsUnscaled, Offset, OffsetStride))
649761
return E;
650762

651763
// Track which registers have been modified and used between the first insn
@@ -704,18 +816,32 @@ AArch64LoadStoreOpt::findMatchingInsn(MachineBasicBlock::iterator I,
704816
// If the resultant immediate offset of merging these instructions
705817
// is out of range for a pairwise instruction, bail and keep looking.
706818
bool MIIsUnscaled = isUnscaledLdSt(MI);
707-
if (!inBoundsForPair(MIIsUnscaled, MinOffset, OffsetStride)) {
819+
bool IsSmallTypeLd = isSmallTypeLdMerge(MI->getOpcode());
820+
if (!IsSmallTypeLd &&
821+
!inBoundsForPair(MIIsUnscaled, MinOffset, OffsetStride)) {
708822
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
709823
MemInsns.push_back(MI);
710824
continue;
711825
}
712-
// If the alignment requirements of the paired (scaled) instruction
713-
// can't express the offset of the unscaled input, bail and keep
714-
// looking.
715-
if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) {
716-
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
717-
MemInsns.push_back(MI);
718-
continue;
826+
827+
if (IsSmallTypeLd) {
828+
// If the alignment requirements of the larger type scaled load
829+
// instruction can't express the scaled offset of the smaller type
830+
// input, bail and keep looking.
831+
if (!IsUnscaled && alignTo(MinOffset, 2) != MinOffset) {
832+
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
833+
MemInsns.push_back(MI);
834+
continue;
835+
}
836+
} else {
837+
// If the alignment requirements of the paired (scaled) instruction
838+
// can't express the offset of the unscaled input, bail and keep
839+
// looking.
840+
if (IsUnscaled && (alignTo(MinOffset, OffsetStride) != MinOffset)) {
841+
trackRegDefsUses(MI, ModifiedRegs, UsedRegs, TRI);
842+
MemInsns.push_back(MI);
843+
continue;
844+
}
719845
}
720846
// If the destination register of the loads is the same register, bail
721847
// and keep looking. A load-pair instruction with both destination
@@ -996,24 +1122,94 @@ MachineBasicBlock::iterator AArch64LoadStoreOpt::findMatchingUpdateInsnBackward(
9961122
return E;
9971123
}
9981124

1125+
bool AArch64LoadStoreOpt::tryToMergeLdStInst(
1126+
MachineBasicBlock::iterator &MBBI) {
1127+
MachineInstr *MI = MBBI;
1128+
MachineBasicBlock::iterator E = MI->getParent()->end();
1129+
// If this is a volatile load/store, don't mess with it.
1130+
if (MI->hasOrderedMemoryRef())
1131+
return false;
1132+
1133+
// Make sure this is a reg+imm (as opposed to an address reloc).
1134+
if (!getLdStOffsetOp(MI).isImm())
1135+
return false;
1136+
1137+
// Check if this load/store has a hint to avoid pair formation.
1138+
// MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
1139+
if (TII->isLdStPairSuppressed(MI))
1140+
return false;
1141+
1142+
// Look ahead up to ScanLimit instructions for a pairable instruction.
1143+
LdStPairFlags Flags;
1144+
MachineBasicBlock::iterator Paired = findMatchingInsn(MBBI, Flags, ScanLimit);
1145+
if (Paired != E) {
1146+
if (isSmallTypeLdMerge(MI)) {
1147+
++NumSmallTypeMerged;
1148+
} else {
1149+
++NumPairCreated;
1150+
if (isUnscaledLdSt(MI))
1151+
++NumUnscaledPairCreated;
1152+
}
1153+
1154+
// Merge the loads into a pair. Keeping the iterator straight is a
1155+
// pain, so we let the merge routine tell us what the next instruction
1156+
// is after it's done mucking about.
1157+
MBBI = mergePairedInsns(MBBI, Paired, Flags);
1158+
return true;
1159+
}
1160+
return false;
1161+
}
1162+
9991163
bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB) {
10001164
bool Modified = false;
1001-
// Two tranformations to do here:
1002-
// 1) Find loads and stores that can be merged into a single load or store
1165+
// Three tranformations to do here:
1166+
// 1) Find halfword loads that can be merged into a single 32-bit word load
1167+
// with bitfield extract instructions.
1168+
// e.g.,
1169+
// ldrh w0, [x2]
1170+
// ldrh w1, [x2, #2]
1171+
// ; becomes
1172+
// ldr w0, [x2]
1173+
// ubfx w1, w0, #16, #16
1174+
// and w0, w0, #ffff
1175+
// 2) Find loads and stores that can be merged into a single load or store
10031176
// pair instruction.
10041177
// e.g.,
10051178
// ldr x0, [x2]
10061179
// ldr x1, [x2, #8]
10071180
// ; becomes
10081181
// ldp x0, x1, [x2]
1009-
// 2) Find base register updates that can be merged into the load or store
1182+
// 3) Find base register updates that can be merged into the load or store
10101183
// as a base-reg writeback.
10111184
// e.g.,
10121185
// ldr x0, [x2]
10131186
// add x2, x2, #4
10141187
// ; becomes
10151188
// ldr x0, [x2], #4
10161189

1190+
for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
1191+
!IsStrictAlign && MBBI != E;) {
1192+
MachineInstr *MI = MBBI;
1193+
switch (MI->getOpcode()) {
1194+
default:
1195+
// Just move on to the next instruction.
1196+
++MBBI;
1197+
break;
1198+
// Scaled instructions.
1199+
case AArch64::LDRHHui:
1200+
// Unscaled instructions.
1201+
case AArch64::LDURHHi: {
1202+
if (tryToMergeLdStInst(MBBI)) {
1203+
Modified = true;
1204+
break;
1205+
}
1206+
++MBBI;
1207+
break;
1208+
}
1209+
// FIXME: Do the other instructions.
1210+
}
1211+
}
1212+
10171213
for (MachineBasicBlock::iterator MBBI = MBB.begin(), E = MBB.end();
10181214
MBBI != E;) {
10191215
MachineInstr *MI = MBBI;
@@ -1046,35 +1242,7 @@ bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB) {
10461242
case AArch64::LDURWi:
10471243
case AArch64::LDURXi:
10481244
case AArch64::LDURSWi: {
1049-
// If this is a volatile load/store, don't mess with it.
1050-
if (MI->hasOrderedMemoryRef()) {
1051-
++MBBI;
1052-
break;
1053-
}
1054-
// Make sure this is a reg+imm (as opposed to an address reloc).
1055-
if (!getLdStOffsetOp(MI).isImm()) {
1056-
++MBBI;
1057-
break;
1058-
}
1059-
// Check if this load/store has a hint to avoid pair formation.
1060-
// MachineMemOperands hints are set by the AArch64StorePairSuppress pass.
1061-
if (TII->isLdStPairSuppressed(MI)) {
1062-
++MBBI;
1063-
break;
1064-
}
1065-
// Look ahead up to ScanLimit instructions for a pairable instruction.
1066-
LdStPairFlags Flags;
1067-
MachineBasicBlock::iterator Paired =
1068-
findMatchingInsn(MBBI, Flags, ScanLimit);
1069-
if (Paired != E) {
1070-
++NumPairCreated;
1071-
if (isUnscaledLdSt(MI))
1072-
++NumUnscaledPairCreated;
1073-
1074-
// Merge the loads into a pair. Keeping the iterator straight is a
1075-
// pain, so we let the merge routine tell us what the next instruction
1076-
// is after it's done mucking about.
1077-
MBBI = mergePairedInsns(MBBI, Paired, Flags);
1245+
if (tryToMergeLdStInst(MBBI)) {
10781246
Modified = true;
10791247
break;
10801248
}
@@ -1206,6 +1374,8 @@ bool AArch64LoadStoreOpt::optimizeBlock(MachineBasicBlock &MBB) {
12061374
bool AArch64LoadStoreOpt::runOnMachineFunction(MachineFunction &Fn) {
12071375
TII = static_cast<const AArch64InstrInfo *>(Fn.getSubtarget().getInstrInfo());
12081376
TRI = Fn.getSubtarget().getRegisterInfo();
1377+
IsStrictAlign = (static_cast<const AArch64Subtarget &>(Fn.getSubtarget()))
1378+
.requiresStrictAlign();
12091379

12101380
bool Modified = false;
12111381
for (auto &MBB : Fn)

0 commit comments

Comments
 (0)