diff --git a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h --- a/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h +++ b/llvm/include/llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h @@ -325,6 +325,21 @@ return true; } + // trunc(sext x) -> x + Register SextSrc; + if (mi_match(SrcReg, MRI, m_GSExt(m_Reg(SextSrc)))) { + LLT DstTy = MRI.getType(DstReg); + LLT SextSrcTy = MRI.getType(SextSrc); + if (DstTy == SextSrcTy) { + LLVM_DEBUG(dbgs() << ".. Combine MI: " << MI;); + + replaceRegOrBuildCopy(DstReg, SextSrc, MRI, Builder, UpdatedDefs, Observer); + UpdatedDefs.push_back(DstReg); + markInstAndDefDead(MI, *MRI.getVRegDef(SrcReg), DeadInsts); + return true; + } + } + return false; } diff --git a/llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir b/llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir --- a/llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir +++ b/llvm/test/CodeGen/AMDGPU/GlobalISel/artifact-combiner-trunc.mir @@ -145,3 +145,18 @@ %2:_(s32) = G_TRUNC %1 $vgpr0 = COPY %2 ... + +--- +name: trunc_sext + +body: | + bb.0: + ; Test that trunc(sext) is replaced with sext source. + ; CHECK-LABEL: name: trunc_sext + ; CHECK: [[DEF:%[0-9]+]]:_(s32) = G_IMPLICIT_DEF + ; CHECK-NEXT: $vgpr0 = COPY [[DEF]](s32) + %0:_(s32) = G_IMPLICIT_DEF + %1:_(s64) = G_SEXT %0 + %2:_(s32) = G_TRUNC %1 + $vgpr0 = COPY %2 +...