Skip to content

Commit 19f0191

Browse files
author
Thorsten Schütt
committed
more flags
1 parent 7903d57 commit 19f0191

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
@@ -746,7 +746,8 @@ class MachineIRBuilder {
746746
/// \pre \p Op must be smaller than \p Res
747747
///
748748
/// \return The newly created instruction.
749-
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op);
749+
MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op,
750+
std::optional<unsigned> Flags = std::nullopt);
750751

751752
/// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
752753
/// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
@@ -1231,7 +1232,8 @@ class MachineIRBuilder {
12311232
/// \pre \p Res must be smaller than \p Op
12321233
///
12331234
/// \return The newly created instruction.
1234-
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op);
1235+
MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op,
1236+
std::optional<unsigned> Flags = std::nullopt);
12351237

12361238
/// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
12371239
///

llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp

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

7266-
// The types have to match for a no-op.
7267-
if (DstTy != SrcTy)
7268-
return false;
7266+
if (DstTy == SrcTy) {
7267+
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7268+
return true;
7269+
}
72697270

7270-
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7271+
if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
7272+
isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
7273+
MatchInfo = [=](MachineIRBuilder &B) {
7274+
B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoSWrap);
7275+
};
7276+
return true;
7277+
}
72717278

7272-
return true;
7279+
if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
7280+
isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}})) {
7281+
MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
7282+
return true;
7283+
}
7284+
7285+
return false;
72737286
}
72747287

72757288
bool CombinerHelper::matchZextOfTrunc(const MachineOperand &MO,
@@ -7283,11 +7296,26 @@ bool CombinerHelper::matchZextOfTrunc(const MachineOperand &MO,
72837296
LLT DstTy = MRI.getType(Dst);
72847297
LLT SrcTy = MRI.getType(Src);
72857298

7286-
// The types have to match for a no-op.
7287-
if (DstTy != SrcTy)
7288-
return false;
7299+
if (DstTy == SrcTy) {
7300+
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7301+
return true;
7302+
}
72897303

7290-
MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
7304+
if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
7305+
isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
7306+
MatchInfo = [=](MachineIRBuilder &B) {
7307+
B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoUWrap);
7308+
};
7309+
return true;
7310+
}
72917311

7292-
return true;
7312+
if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
7313+
isLegalOrBeforeLegalizer({TargetOpcode::G_ZEXT, {DstTy, SrcTy}})) {
7314+
MatchInfo = [=](MachineIRBuilder &B) {
7315+
B.buildZExt(Dst, Src, MachineInstr::MIFlag::NonNeg);
7316+
};
7317+
return true;
7318+
}
7319+
7320+
return false;
72937321
}

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

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

492492
MachineInstrBuilder MachineIRBuilder::buildZExt(const DstOp &Res,
493-
const SrcOp &Op) {
494-
return buildInstr(TargetOpcode::G_ZEXT, Res, Op);
493+
const SrcOp &Op,
494+
std::optional<unsigned> Flags) {
495+
return buildInstr(TargetOpcode::G_ZEXT, Res, Op, Flags);
495496
}
496497

497498
unsigned MachineIRBuilder::getBoolExtOp(bool IsVec, bool IsFP) const {
@@ -869,9 +870,10 @@ MachineInstrBuilder MachineIRBuilder::buildIntrinsic(Intrinsic::ID ID,
869870
return buildIntrinsic(ID, Results, HasSideEffects, isConvergent);
870871
}
871872

872-
MachineInstrBuilder MachineIRBuilder::buildTrunc(const DstOp &Res,
873-
const SrcOp &Op) {
874-
return buildInstr(TargetOpcode::G_TRUNC, Res, Op);
873+
MachineInstrBuilder
874+
MachineIRBuilder::buildTrunc(const DstOp &Res, const SrcOp &Op,
875+
std::optional<unsigned> Flags) {
876+
return buildInstr(TargetOpcode::G_TRUNC, Res, Op, Flags);
875877
}
876878

877879
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)