-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[GlobalIsel][NFC] Move cast code #100196
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[GlobalIsel][NFC] Move cast code #100196
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Preparation for more cast combines
@llvm/pr-subscribers-llvm-globalisel Author: Thorsten Schütt (tschuett) ChangesPreparation for more cast combines Full diff: https://github.com/llvm/llvm-project/pull/100196.diff 3 Files Affected:
diff --git a/llvm/lib/CodeGen/GlobalISel/CMakeLists.txt b/llvm/lib/CodeGen/GlobalISel/CMakeLists.txt
index 54ac7f72011a6..a15b76440364b 100644
--- a/llvm/lib/CodeGen/GlobalISel/CMakeLists.txt
+++ b/llvm/lib/CodeGen/GlobalISel/CMakeLists.txt
@@ -6,6 +6,7 @@ add_llvm_component_library(LLVMGlobalISel
GlobalISel.cpp
Combiner.cpp
CombinerHelper.cpp
+ CombinerHelperCasts.cpp
CombinerHelperVectorOps.cpp
GIMatchTableExecutor.cpp
GISelChangeObserver.cpp
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
index e77ea3e76ad71..208f554eb8f98 100644
--- a/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelper.cpp
@@ -7457,92 +7457,3 @@ void CombinerHelper::applyExpandFPowI(MachineInstr &MI, int64_t Exponent) {
Builder.buildCopy(Dst, *Res);
MI.eraseFromParent();
}
-
-bool CombinerHelper::matchSextOfTrunc(const MachineOperand &MO,
- BuildFnTy &MatchInfo) {
- GSext *Sext = cast<GSext>(getDefIgnoringCopies(MO.getReg(), MRI));
- GTrunc *Trunc = cast<GTrunc>(getDefIgnoringCopies(Sext->getSrcReg(), MRI));
-
- Register Dst = Sext->getReg(0);
- Register Src = Trunc->getSrcReg();
-
- LLT DstTy = MRI.getType(Dst);
- LLT SrcTy = MRI.getType(Src);
-
- if (DstTy == SrcTy) {
- MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
- return true;
- }
-
- if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
- isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
- MatchInfo = [=](MachineIRBuilder &B) {
- B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoSWrap);
- };
- return true;
- }
-
- if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
- isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}})) {
- MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
- return true;
- }
-
- return false;
-}
-
-bool CombinerHelper::matchZextOfTrunc(const MachineOperand &MO,
- BuildFnTy &MatchInfo) {
- GZext *Zext = cast<GZext>(getDefIgnoringCopies(MO.getReg(), MRI));
- GTrunc *Trunc = cast<GTrunc>(getDefIgnoringCopies(Zext->getSrcReg(), MRI));
-
- Register Dst = Zext->getReg(0);
- Register Src = Trunc->getSrcReg();
-
- LLT DstTy = MRI.getType(Dst);
- LLT SrcTy = MRI.getType(Src);
-
- if (DstTy == SrcTy) {
- MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
- return true;
- }
-
- if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
- isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
- MatchInfo = [=](MachineIRBuilder &B) {
- B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoUWrap);
- };
- return true;
- }
-
- if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
- isLegalOrBeforeLegalizer({TargetOpcode::G_ZEXT, {DstTy, SrcTy}})) {
- MatchInfo = [=](MachineIRBuilder &B) {
- B.buildZExt(Dst, Src, MachineInstr::MIFlag::NonNeg);
- };
- return true;
- }
-
- return false;
-}
-
-bool CombinerHelper::matchNonNegZext(const MachineOperand &MO,
- BuildFnTy &MatchInfo) {
- GZext *Zext = cast<GZext>(MRI.getVRegDef(MO.getReg()));
-
- Register Dst = Zext->getReg(0);
- Register Src = Zext->getSrcReg();
-
- LLT DstTy = MRI.getType(Dst);
- LLT SrcTy = MRI.getType(Src);
- const auto &TLI = getTargetLowering();
-
- // Convert zext nneg to sext if sext is the preferred form for the target.
- if (isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}}) &&
- TLI.isSExtCheaperThanZExt(getMVTForLLT(SrcTy), getMVTForLLT(DstTy))) {
- MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
- return true;
- }
-
- return false;
-}
diff --git a/llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp b/llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp
new file mode 100644
index 0000000000000..8fe69f21fafd1
--- /dev/null
+++ b/llvm/lib/CodeGen/GlobalISel/CombinerHelperCasts.cpp
@@ -0,0 +1,115 @@
+//===- CombinerHelperCasts.cpp---------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements CombinerHelper for G_ANYEXT, G_SEXT, G_TRUNC, and
+// G_ZEXT
+//
+//===----------------------------------------------------------------------===//
+#include "llvm/CodeGen/GlobalISel/CombinerHelper.h"
+#include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
+#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
+#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
+#include "llvm/CodeGen/GlobalISel/Utils.h"
+#include "llvm/CodeGen/LowLevelTypeUtils.h"
+#include "llvm/CodeGen/MachineOperand.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/CodeGen/TargetOpcodes.h"
+#include "llvm/Support/Casting.h"
+
+#define DEBUG_TYPE "gi-combiner"
+
+using namespace llvm;
+
+bool CombinerHelper::matchSextOfTrunc(const MachineOperand &MO,
+ BuildFnTy &MatchInfo) {
+ GSext *Sext = cast<GSext>(getDefIgnoringCopies(MO.getReg(), MRI));
+ GTrunc *Trunc = cast<GTrunc>(getDefIgnoringCopies(Sext->getSrcReg(), MRI));
+
+ Register Dst = Sext->getReg(0);
+ Register Src = Trunc->getSrcReg();
+
+ LLT DstTy = MRI.getType(Dst);
+ LLT SrcTy = MRI.getType(Src);
+
+ if (DstTy == SrcTy) {
+ MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
+ return true;
+ }
+
+ if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
+ isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
+ MatchInfo = [=](MachineIRBuilder &B) {
+ B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoSWrap);
+ };
+ return true;
+ }
+
+ if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
+ isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}})) {
+ MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
+ return true;
+ }
+
+ return false;
+}
+
+bool CombinerHelper::matchZextOfTrunc(const MachineOperand &MO,
+ BuildFnTy &MatchInfo) {
+ GZext *Zext = cast<GZext>(getDefIgnoringCopies(MO.getReg(), MRI));
+ GTrunc *Trunc = cast<GTrunc>(getDefIgnoringCopies(Zext->getSrcReg(), MRI));
+
+ Register Dst = Zext->getReg(0);
+ Register Src = Trunc->getSrcReg();
+
+ LLT DstTy = MRI.getType(Dst);
+ LLT SrcTy = MRI.getType(Src);
+
+ if (DstTy == SrcTy) {
+ MatchInfo = [=](MachineIRBuilder &B) { B.buildCopy(Dst, Src); };
+ return true;
+ }
+
+ if (DstTy.getScalarSizeInBits() < SrcTy.getScalarSizeInBits() &&
+ isLegalOrBeforeLegalizer({TargetOpcode::G_TRUNC, {DstTy, SrcTy}})) {
+ MatchInfo = [=](MachineIRBuilder &B) {
+ B.buildTrunc(Dst, Src, MachineInstr::MIFlag::NoUWrap);
+ };
+ return true;
+ }
+
+ if (DstTy.getScalarSizeInBits() > SrcTy.getScalarSizeInBits() &&
+ isLegalOrBeforeLegalizer({TargetOpcode::G_ZEXT, {DstTy, SrcTy}})) {
+ MatchInfo = [=](MachineIRBuilder &B) {
+ B.buildZExt(Dst, Src, MachineInstr::MIFlag::NonNeg);
+ };
+ return true;
+ }
+
+ return false;
+}
+
+bool CombinerHelper::matchNonNegZext(const MachineOperand &MO,
+ BuildFnTy &MatchInfo) {
+ GZext *Zext = cast<GZext>(MRI.getVRegDef(MO.getReg()));
+
+ Register Dst = Zext->getReg(0);
+ Register Src = Zext->getSrcReg();
+
+ LLT DstTy = MRI.getType(Dst);
+ LLT SrcTy = MRI.getType(Src);
+ const auto &TLI = getTargetLowering();
+
+ // Convert zext nneg to sext if sext is the preferred form for the target.
+ if (isLegalOrBeforeLegalizer({TargetOpcode::G_SEXT, {DstTy, SrcTy}}) &&
+ TLI.isSExtCheaperThanZExt(getMVTForLLT(SrcTy), getMVTForLLT(DstTy))) {
+ MatchInfo = [=](MachineIRBuilder &B) { B.buildSExt(Dst, Src); };
+ return true;
+ }
+
+ return false;
+}
|
arsenm
approved these changes
Jul 24, 2024
yuxuanchen1997
pushed a commit
that referenced
this pull request
Jul 25, 2024
Summary: Preparation for more cast combines Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250659
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Preparation for more cast combines