Skip to content

Commit a401e10

Browse files
committed
[AArch64][GlobalISel][RegBankSelect] Guess the bank for loads using the MMO.
We had this patch downstream for a long time, we need to find the users of the IR load to guess the bank since with opaque pointers we lost the type information.
1 parent 5c950a3 commit a401e10

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
753753
*AArch64GenRegisterBankInfo::PartMappings[OpRegBankIdx[1]].RegBank,
754754
OpSize[0]);
755755
break;
756-
case TargetOpcode::G_LOAD:
756+
case TargetOpcode::G_LOAD: {
757757
// Loading in vector unit is slightly more expensive.
758758
// This is actually only true for the LD1R and co instructions,
759759
// but anyway for the fast mode this number does not matter and
@@ -771,6 +771,33 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
771771
break;
772772
}
773773

774+
// Try to guess the type of the load from the MMO.
775+
const auto &MMO = **MI.memoperands_begin();
776+
const Value *LdVal = MMO.getValue();
777+
if (LdVal) {
778+
Type *EltTy = nullptr;
779+
if (const GlobalValue *GV = dyn_cast<GlobalValue>(LdVal)) {
780+
EltTy = GV->getValueType();
781+
} else {
782+
// FIXME: grubbing around uses is pretty ugly, but with no more
783+
// `getPointerElementType` there's not much else we can do.
784+
for (const auto *LdUser : LdVal->users()) {
785+
if (isa<LoadInst>(LdUser)) {
786+
EltTy = LdUser->getType();
787+
break;
788+
}
789+
if (isa<StoreInst>(LdUser) && LdUser->getOperand(1) == LdVal) {
790+
EltTy = LdUser->getOperand(0)->getType();
791+
break;
792+
}
793+
}
794+
}
795+
if (EltTy && EltTy->isFPOrFPVectorTy()) {
796+
OpRegBankIdx[0] = PMI_FirstFPR;
797+
break;
798+
}
799+
}
800+
774801
// Check if that load feeds fp instructions.
775802
// In that case, we want the default mapping to be on FPR
776803
// instead of blind map every scalar to GPR.
@@ -788,6 +815,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
788815
}))
789816
OpRegBankIdx[0] = PMI_FirstFPR;
790817
break;
818+
}
791819
case TargetOpcode::G_STORE:
792820
// Check if that store is fed by fp instructions.
793821
if (OpRegBankIdx[0] == PMI_FirstGPR) {
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py
2+
# RUN: llc -O0 -mtriple=aarch64-apple-ios -run-pass=regbankselect -verify-machineinstrs %s -o - | FileCheck %s -check-prefix=CHECK
3+
4+
--- |
5+
target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
6+
7+
@var_fp = global float 0.0
8+
@var_int = global i32 0
9+
10+
define float @fp_load_phi() { ret float undef }
11+
define i32 @int_load_phi() { ret i32 undef }
12+
13+
...
14+
---
15+
name: fp_load_phi
16+
legalized: true
17+
regBankSelected: false
18+
tracksRegLiveness: true
19+
body: |
20+
; CHECK-LABEL: name: fp_load_phi
21+
; CHECK: bb.0:
22+
; CHECK: successors: %bb.1(0x80000000)
23+
; CHECK: liveins: $w0
24+
; CHECK: [[COPY:%[0-9]+]]:gpr(s32) = COPY $w0
25+
; CHECK: [[GV:%[0-9]+]]:gpr(p0) = G_GLOBAL_VALUE @var_fp
26+
; CHECK: %fp_load:fpr(s32) = G_LOAD [[GV]](p0) :: (load (s32) from @var_fp)
27+
; CHECK: bb.1:
28+
; CHECK: successors: %bb.1(0x40000000), %bb.2(0x40000000)
29+
; CHECK: [[PHI:%[0-9]+]]:fpr(s32) = PHI %fp_load(s32), %bb.0, [[PHI]](s32), %bb.1
30+
; CHECK: G_BRCOND [[COPY]](s32), %bb.1
31+
; CHECK: bb.2:
32+
; CHECK: $s0 = COPY [[PHI]](s32)
33+
; CHECK: RET_ReallyLR implicit $s0
34+
; Here we're checking that the load is assigned an FPR bank, since it's
35+
; loading from an fp type in the IR.
36+
bb.0:
37+
liveins: $w0
38+
successors: %bb.1
39+
%0:_(s32) = COPY $w0
40+
%1:_(p0) = G_GLOBAL_VALUE @var_fp
41+
%fp_load:_(s32) = G_LOAD %1 :: (load 4 from @var_fp)
42+
43+
bb.1:
44+
successors: %bb.1, %bb.2
45+
%2:_(s32) = PHI %fp_load, %bb.0, %2, %bb.1
46+
G_BRCOND %0, %bb.1
47+
48+
bb.2:
49+
$s0 = COPY %2
50+
RET_ReallyLR implicit $s0
51+
...
52+
53+
---
54+
name: int_load_phi
55+
legalized: true
56+
regBankSelected: false
57+
tracksRegLiveness: true
58+
body: |
59+
; CHECK-LABEL: name: int_load_phi
60+
; CHECK: bb.0:
61+
; CHECK: successors: %bb.1(0x80000000)
62+
; CHECK: liveins: $w0
63+
; CHECK: [[COPY:%[0-9]+]]:gpr(s32) = COPY $w0
64+
; CHECK: [[GV:%[0-9]+]]:gpr(p0) = G_GLOBAL_VALUE @var_fp
65+
; CHECK: %fp_load:gpr(s32) = G_LOAD [[GV]](p0) :: (load (s32) from @var_int)
66+
; CHECK: bb.1:
67+
; CHECK: successors: %bb.1(0x40000000), %bb.2(0x40000000)
68+
; CHECK: [[PHI:%[0-9]+]]:gpr(s32) = PHI %fp_load(s32), %bb.0, [[PHI]](s32), %bb.1
69+
; CHECK: G_BRCOND [[COPY]](s32), %bb.1
70+
; CHECK: bb.2:
71+
; CHECK: $s0 = COPY [[PHI]](s32)
72+
; CHECK: RET_ReallyLR implicit $s0
73+
bb.0:
74+
liveins: $w0
75+
successors: %bb.1
76+
%0:_(s32) = COPY $w0
77+
%1:_(p0) = G_GLOBAL_VALUE @var_fp
78+
%fp_load:_(s32) = G_LOAD %1 :: (load 4 from @var_int)
79+
80+
bb.1:
81+
successors: %bb.1, %bb.2
82+
%2:_(s32) = PHI %fp_load, %bb.0, %2, %bb.1
83+
G_BRCOND %0, %bb.1
84+
85+
bb.2:
86+
$s0 = COPY %2
87+
RET_ReallyLR implicit $s0
88+
...

0 commit comments

Comments
 (0)