Skip to content

Commit 244e7f0

Browse files
author
Thorsten Schütt
committed
more flags
1 parent 96640f6 commit 244e7f0

File tree

4 files changed

+116
-22
lines changed

4 files changed

+116
-22
lines changed

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,8 @@ class MachineIRBuilder {
737737
/// \pre \p Op must be smaller than \p Res
738738
///
739739
/// \return The newly created instruction.
740-
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op);
740+
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op,
741+
std::optional<unsigned> Flags = std::nullopt);
741742

742743
/// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
743744
/// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
@@ -1222,7 +1223,8 @@ class MachineIRBuilder {
12221223
/// \pre \p Res must be smaller than \p Op
12231224
///
12241225
/// \return The newly created instruction.
1225-
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op);
1226+
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op,
1227+
std::optional<unsigned> Flags = std::nullopt);
12261228

12271229
/// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
12281230
///

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7157,13 +7157,26 @@ bool CombinerHelper::matchSextOfTrunc(const MachineOperand &MO,
71577157
LLT DstTy = MRI.getType(Dst);
71587158
LLT SrcTy = MRI.getType(Src);
71597159

7160-
// The types have to match for a no-op.
7161-
if (DstTy != SrcTy)
7162-
return false;
7160+
if (DstTy == SrcTy) {
7161+
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7162+
return true;
7163+
}
71637164

7164-
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7165+
if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
7166+
isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
7167+
MatchInfo = [=](MachineIRBuilder &B) {
7168+
B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoSWrap);
7169+
};
7170+
return true;
7171+
}
71657172

7166-
return true;
7173+
if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
7174+
isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}})) {
7175+
MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
7176+
return true;
7177+
}
7178+
7179+
return false;
71677180
}
71687181

71697182
bool CombinerHelper::matchZextOfTrunc(const MachineOperand &MO,
@@ -7177,11 +7190,26 @@ bool CombinerHelper::matchZextOfTrunc(const MachineOperand &MO,
71777190
LLT DstTy = MRI.getType(Dst);
71787191
LLT SrcTy = MRI.getType(Src);
71797192

7180-
// The types have to match for a no-op.
7181-
if (DstTy != SrcTy)
7182-
return false;
7193+
if (DstTy == SrcTy) {
7194+
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7195+
return true;
7196+
}
71837197

7184-
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7198+
if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
7199+
isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
7200+
MatchInfo = [=](MachineIRBuilder &B) {
7201+
B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoUWrap);
7202+
};
7203+
return true;
7204+
}
71857205

7186-
return true;
7206+
if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
7207+
isLegalOrBeforeLegalizer({TargetOpcode::G_ZEXT, {DstTy, SrcTy}})) {
7208+
MatchInfo = [=](MachineIRBuilder &B) {
7209+
B.buildZExt(Dst, Src, MachineInstr::MIFlag::NonNeg);
7210+
};
7211+
return true;
7212+
}
7213+
7214+
return false;
71877215
}

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,9 @@ MachineInstrBuilder MachineIRBuilder::buildSExt(const DstOp &Res,
488488
}
489489

490490
MachineInstrBuilder MachineIRBuilder::buildZExt(const DstOp &Res,
491-
const SrcOp &Op) {
492-
return buildInstr(TargetOpcode::G_ZEXT, Res, Op);
491+
const SrcOp &Op,
492+
std::optional<unsigned> Flags) {
493+
return buildInstr(TargetOpcode::G_ZEXT, Res, Op, Flags);
493494
}
494495

495496
unsigned MachineIRBuilder::getBoolExtOp(bool IsVec, bool IsFP) const {
@@ -867,9 +868,10 @@ MachineInstrBuilder MachineIRBuilder::buildIntrinsic(Intrinsic::ID ID,
867868
return buildIntrinsic(ID, Results, HasSideEffects, isConvergent);
868869
}
869870

870-
MachineInstrBuilder MachineIRBuilder::buildTrunc(const DstOp &Res,
871-
const SrcOp &Op) {
872-
return buildInstr(TargetOpcode::G_TRUNC, Res, Op);
871+
MachineInstrBuilder
872+
MachineIRBuilder::buildTrunc(const DstOp &Res, const SrcOp &Op,
873+
std::optional<unsigned> Flags) {
874+
return buildInstr(TargetOpcode::G_TRUNC, Res, Op, Flags);
873875
}
874876

875877
MachineInstrBuilder

llvm/test/CodeGen/AArch64/GlobalISel/combine-with-flags.mir

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ body: |
131131
; CHECK: liveins: $w0, $w1
132132
; CHECK-NEXT: {{ $}}
133133
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
134-
; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(s16) = nsw G_TRUNC [[COPY]](s64)
135-
; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s32) = G_SEXT [[TRUNC]](s16)
136-
; CHECK-NEXT: $w1 = COPY [[SEXT]](s32)
134+
; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(s32) = nsw G_TRUNC [[COPY]](s64)
135+
; CHECK-NEXT: $w1 = COPY [[TRUNC]](s32)
137136
%0:_(s64) = COPY $x0
138137
%2:_(s16) = nsw G_TRUNC %0
139138
%3:_(s32) = G_SEXT %2
@@ -261,8 +260,7 @@ body: |
261260
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
262261
; CHECK-NEXT: [[COPY1:%[0-9]+]]:_(s64) = COPY $x1
263262
; CHECK-NEXT: %bv0:_(<2 x s64>) = G_BUILD_VECTOR [[COPY]](s64), [[COPY1]](s64)
264-
; CHECK-NEXT: %t:_(<2 x s16>) = nuw G_TRUNC %bv0(<2 x s64>)
265-
; CHECK-NEXT: %z:_(<2 x s32>) = G_ZEXT %t(<2 x s16>)
263+
; CHECK-NEXT: %z:_(<2 x s32>) = nuw G_TRUNC %bv0(<2 x s64>)
266264
; CHECK-NEXT: $d0 = COPY %z(<2 x s32>)
267265
%0:_(s64) = COPY $x0
268266
%1:_(s64) = COPY $x1
@@ -289,3 +287,67 @@ body: |
289287
%z:_(<vscale x 2 x s64>) = G_ZEXT %t
290288
$z0 = COPY %z
291289
...
290+
---
291+
name: zext_trunc_nuw_to_zext
292+
body: |
293+
bb.0:
294+
liveins: $w0, $w1
295+
; CHECK-LABEL: name: zext_trunc_nuw_to_zext
296+
; CHECK: liveins: $w0, $w1
297+
; CHECK-NEXT: {{ $}}
298+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $w0
299+
; CHECK-NEXT: %2:_(s64) = nneg G_ZEXT [[COPY]](s32)
300+
; CHECK-NEXT: $x1 = COPY %2(s64)
301+
%0:_(s32) = COPY $w0
302+
%2:_(s16) = nuw G_TRUNC %0
303+
%3:_(s64) = G_ZEXT %2
304+
$x1 = COPY %3
305+
...
306+
---
307+
name: zext_trunc_nuw_to_trunc
308+
body: |
309+
bb.0:
310+
liveins: $w0, $w1
311+
; CHECK-LABEL: name: zext_trunc_nuw_to_trunc
312+
; CHECK: liveins: $w0, $w1
313+
; CHECK-NEXT: {{ $}}
314+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
315+
; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(s32) = nuw G_TRUNC [[COPY]](s64)
316+
; CHECK-NEXT: $w1 = COPY [[TRUNC]](s32)
317+
%0:_(s64) = COPY $x0
318+
%2:_(s16) = nuw G_TRUNC %0
319+
%3:_(s32) = G_ZEXT %2
320+
$w1 = COPY %3
321+
...
322+
---
323+
name: sext_trunc_nsw_to_sext
324+
body: |
325+
bb.0:
326+
liveins: $w0, $w1
327+
; CHECK-LABEL: name: sext_trunc_nsw_to_sext
328+
; CHECK: liveins: $w0, $w1
329+
; CHECK-NEXT: {{ $}}
330+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s32) = COPY $w0
331+
; CHECK-NEXT: [[SEXT:%[0-9]+]]:_(s64) = G_SEXT [[COPY]](s32)
332+
; CHECK-NEXT: $x1 = COPY [[SEXT]](s64)
333+
%0:_(s32) = COPY $w0
334+
%2:_(s16) = nsw G_TRUNC %0
335+
%3:_(s64) = G_SEXT %2
336+
$x1 = COPY %3
337+
...
338+
---
339+
name: sext_trunc_nsw_to_trunc
340+
body: |
341+
bb.0:
342+
liveins: $w0, $w1
343+
; CHECK-LABEL: name: sext_trunc_nsw_to_trunc
344+
; CHECK: liveins: $w0, $w1
345+
; CHECK-NEXT: {{ $}}
346+
; CHECK-NEXT: [[COPY:%[0-9]+]]:_(s64) = COPY $x0
347+
; CHECK-NEXT: [[TRUNC:%[0-9]+]]:_(s32) = nsw G_TRUNC [[COPY]](s64)
348+
; CHECK-NEXT: $w1 = COPY [[TRUNC]](s32)
349+
%0:_(s64) = COPY $x0
350+
%2:_(s16) = nsw G_TRUNC %0
351+
%3:_(s32) = G_SEXT %2
352+
$w1 = COPY %3
353+
...

0 commit comments

Comments
 (0)