[llvm] ddf167c - [GlobalISel] Fix G_ZEXTLOAD being converted to G_SEXTLOAD incorrectly.
Amara Emerson via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 18 10:05:13 PST 2023
Author: Amara Emerson
Date: 2023-02-18T10:05:08-08:00
New Revision: ddf167c442018e807819aa0b2e56546977870333
URL: https://github.com/llvm/llvm-project/commit/ddf167c442018e807819aa0b2e56546977870333
DIFF: https://github.com/llvm/llvm-project/commit/ddf167c442018e807819aa0b2e56546977870333.diff
LOG: [GlobalISel] Fix G_ZEXTLOAD being converted to G_SEXTLOAD incorrectly.
The extending loads combine tries to prefer sign-extends folding into loads vs
zexts, and in cases where a G_ZEXTLOAD is first used by a G_ZEXT, and then used
by a G_SEXT, it would select the G_SEXT even though the load is already
zero-extending.
Fixes issue #59630
Added:
Modified:
llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-extending-loads.mir
Removed:
################################################################################
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index d83bf24e4db11..ec4e45ed0d951 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -399,7 +399,8 @@ namespace {
/// Select a preference between two uses. CurrentUse is the current preference
/// while *ForCandidate is attributes of the candidate under consideration.
-PreferredTuple ChoosePreferredUse(PreferredTuple &CurrentUse,
+PreferredTuple ChoosePreferredUse(MachineInstr &LoadMI,
+ PreferredTuple &CurrentUse,
const LLT TyForCandidate,
unsigned OpcodeForCandidate,
MachineInstr *MIForCandidate) {
@@ -425,8 +426,10 @@ PreferredTuple ChoosePreferredUse(PreferredTuple &CurrentUse,
return {TyForCandidate, OpcodeForCandidate, MIForCandidate};
// Prefer sign extensions to zero extensions as sign-extensions tend to be
- // more expensive.
- if (CurrentUse.Ty == TyForCandidate) {
+ // more expensive. Don't do this if the load is already a zero-extend load
+ // though, otherwise we'll rewrite a zero-extend load into a sign-extend
+ // later.
+ if (!isa<GZExtLoad>(LoadMI) && CurrentUse.Ty == TyForCandidate) {
if (CurrentUse.ExtendOpcode == TargetOpcode::G_SEXT &&
OpcodeForCandidate == TargetOpcode::G_ZEXT)
return CurrentUse;
@@ -566,7 +569,7 @@ bool CombinerHelper::matchCombineExtendingLoads(MachineInstr &MI,
.Action != LegalizeActions::Legal)
continue;
}
- Preferred = ChoosePreferredUse(Preferred,
+ Preferred = ChoosePreferredUse(MI, Preferred,
MRI.getType(UseMI.getOperand(0).getReg()),
UseMI.getOpcode(), &UseMI);
}
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-extending-loads.mir b/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-extending-loads.mir
index abefe434e94d3..47c85f7678593 100644
--- a/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-extending-loads.mir
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/prelegalizercombiner-extending-loads.mir
@@ -459,3 +459,24 @@ body: |
$w0 = COPY %2(s32)
RET_ReallyLR implicit $w0
...
+---
+name: test_dont_zextload_to_sextload
+body: |
+ bb.0:
+ liveins: $x0
+ ; CHECK-LABEL: name: test_dont_zextload_to_sextload
+ ; CHECK: liveins: $x0
+ ; CHECK-NEXT: {{ $}}
+ ; CHECK-NEXT: [[COPY:%[0-9]+]]:_(p0) = COPY $x0
+ ; CHECK-NEXT: [[ZEXTLOAD:%[0-9]+]]:_(s64) = G_ZEXTLOAD [[COPY]](p0) :: (load (s8))
+ ; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(s32) = G_TRUNC [[ZEXTLOAD]](s64)
+ ; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[TRUNC]](s32)
+ ; CHECK-NEXT: $x0 = COPY [[ZEXTLOAD]](s64)
+ ; CHECK-NEXT: $x1 = COPY [[SEXT]](s64)
+ %0:_(p0) = COPY $x0
+ %1:_(s32) = G_ZEXTLOAD %0 :: (load (s8))
+ %2:_(s64) = G_ZEXT %1
+ %3:_(s64) = G_SEXT %1
+ $x0 = COPY %2
+ $x1 = COPY %3
+...
More information about the llvm-commits
mailing list