llvm.org GIT mirror llvm / 873bfe9
GlobalISel: Handle more cases for widenScalar of G_MERGE_VALUES Use an anyext to the requested type for the leftover operand to produce a slightly wider type, and then truncate the final merge. I have another implementation almost ready which handles arbitrary widens, but I think it produces worse code in this example (which I think is 90% due to not folding redundant copies or folding out implicit_def users), so I wanted to add this as a baseline first. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@366366 91177308-0d34-0410-b5e6-96231b3b80d8 Matt Arsenault 1 year, 3 months ago
3 changed file(s) with 118 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
874874
875875 // Try to turn this into a merge of merges if we can use the requested type as
876876 // the source.
877
878 // TODO: Pad with undef if DstTy > WideTy
879 if (NumMerge > 1 && WideTy.getSizeInBits() % SrcTy.getSizeInBits() == 0) {
877 if (NumMerge > 1) {
880878 int PartsPerMerge = WideTy.getSizeInBits() / SrcTy.getSizeInBits();
879 if (WideTy.getSizeInBits() % SrcTy.getSizeInBits() != 0)
880 return UnableToLegalize;
881
882 int RemainderBits = DstTy.getSizeInBits() % WideTy.getSizeInBits();
883 int RemainderParts = RemainderBits / SrcTy.getSizeInBits();
884
881885 SmallVector Parts;
882886 SmallVector SubMerges;
883887
890894 Parts.clear();
891895 }
892896
893 MIRBuilder.buildMerge(DstReg, SubMerges);
897 if (RemainderParts == 0) {
898 MIRBuilder.buildMerge(DstReg, SubMerges);
899 MI.eraseFromParent();
900 return Legalized;
901 }
902
903 assert(RemainderParts == 1);
904
905 auto AnyExt = MIRBuilder.buildAnyExt(
906 WideTy, MI.getOperand(MI.getNumOperands() - 1).getReg());
907 SubMerges.push_back(AnyExt.getReg(0));
908
909 LLT WiderDstTy = LLT::scalar(SubMerges.size() * WideTy.getSizeInBits());
910 auto Merge = MIRBuilder.buildMerge(WiderDstTy, SubMerges);
911 MIRBuilder.buildTrunc(DstReg, Merge);
912
894913 MI.eraseFromParent();
895914 return Legalized;
896915 }
534534 %8:_(s96) = G_MERGE_VALUES %0, %1, %2, %3, %4, %5
535535 $vgpr0_vgpr1_vgpr2 = COPY %8
536536 ...
537
538 ---
539 name: test_merge_s56_s8_s8_s8_s8_s8_s8_s8
540 body: |
541 bb.0:
542 ; CHECK-LABEL: name: test_merge_s56_s8_s8_s8_s8_s8_s8_s8
543 ; CHECK: [[C:%[0-9]+]]:_(s32) = G_CONSTANT i32 0
544 ; CHECK: [[C1:%[0-9]+]]:_(s32) = G_CONSTANT i32 1
545 ; CHECK: [[C2:%[0-9]+]]:_(s32) = G_CONSTANT i32 2
546 ; CHECK: [[C3:%[0-9]+]]:_(s32) = G_CONSTANT i32 3
547 ; CHECK: [[C4:%[0-9]+]]:_(s32) = G_CONSTANT i32 4
548 ; CHECK: [[C5:%[0-9]+]]:_(s32) = G_CONSTANT i32 5
549 ; CHECK: [[C6:%[0-9]+]]:_(s32) = G_CONSTANT i32 6
550 ; CHECK: [[C7:%[0-9]+]]:_(s32) = G_CONSTANT i32 255
551 ; CHECK: [[TRUNC:%[0-9]+]]:_(s16) = G_TRUNC [[C7]](s32)
552 ; CHECK: [[TRUNC1:%[0-9]+]]:_(s16) = G_TRUNC [[C]](s32)
553 ; CHECK: [[AND:%[0-9]+]]:_(s16) = G_AND [[TRUNC1]], [[TRUNC]]
554 ; CHECK: [[C8:%[0-9]+]]:_(s32) = G_CONSTANT i32 8
555 ; CHECK: [[C9:%[0-9]+]]:_(s32) = G_CONSTANT i32 65535
556 ; CHECK: [[COPY:%[0-9]+]]:_(s32) = COPY [[C8]](s32)
557 ; CHECK: [[AND1:%[0-9]+]]:_(s32) = G_AND [[COPY]], [[C9]]
558 ; CHECK: [[C10:%[0-9]+]]:_(s32) = G_CONSTANT i32 255
559 ; CHECK: [[COPY1:%[0-9]+]]:_(s32) = COPY [[C1]](s32)
560 ; CHECK: [[AND2:%[0-9]+]]:_(s32) = G_AND [[COPY1]], [[C10]]
561 ; CHECK: [[SHL:%[0-9]+]]:_(s32) = G_SHL [[AND2]], [[AND1]](s32)
562 ; CHECK: [[TRUNC2:%[0-9]+]]:_(s16) = G_TRUNC [[SHL]](s32)
563 ; CHECK: [[OR:%[0-9]+]]:_(s16) = G_OR [[AND]], [[TRUNC2]]
564 ; CHECK: [[TRUNC3:%[0-9]+]]:_(s16) = G_TRUNC [[C2]](s32)
565 ; CHECK: [[AND3:%[0-9]+]]:_(s16) = G_AND [[TRUNC3]], [[TRUNC]]
566 ; CHECK: [[C11:%[0-9]+]]:_(s32) = G_CONSTANT i32 8
567 ; CHECK: [[COPY2:%[0-9]+]]:_(s32) = COPY [[C11]](s32)
568 ; CHECK: [[AND4:%[0-9]+]]:_(s32) = G_AND [[COPY2]], [[C9]]
569 ; CHECK: [[COPY3:%[0-9]+]]:_(s32) = COPY [[C3]](s32)
570 ; CHECK: [[AND5:%[0-9]+]]:_(s32) = G_AND [[COPY3]], [[C10]]
571 ; CHECK: [[SHL1:%[0-9]+]]:_(s32) = G_SHL [[AND5]], [[AND4]](s32)
572 ; CHECK: [[TRUNC4:%[0-9]+]]:_(s16) = G_TRUNC [[SHL1]](s32)
573 ; CHECK: [[OR1:%[0-9]+]]:_(s16) = G_OR [[AND3]], [[TRUNC4]]
574 ; CHECK: [[TRUNC5:%[0-9]+]]:_(s16) = G_TRUNC [[C4]](s32)
575 ; CHECK: [[AND6:%[0-9]+]]:_(s16) = G_AND [[TRUNC5]], [[TRUNC]]
576 ; CHECK: [[C12:%[0-9]+]]:_(s32) = G_CONSTANT i32 8
577 ; CHECK: [[COPY4:%[0-9]+]]:_(s32) = COPY [[C12]](s32)
578 ; CHECK: [[AND7:%[0-9]+]]:_(s32) = G_AND [[COPY4]], [[C9]]
579 ; CHECK: [[COPY5:%[0-9]+]]:_(s32) = COPY [[C5]](s32)
580 ; CHECK: [[AND8:%[0-9]+]]:_(s32) = G_AND [[COPY5]], [[C10]]
581 ; CHECK: [[SHL2:%[0-9]+]]:_(s32) = G_SHL [[AND8]], [[AND7]](s32)
582 ; CHECK: [[TRUNC6:%[0-9]+]]:_(s16) = G_TRUNC [[SHL2]](s32)
583 ; CHECK: [[OR2:%[0-9]+]]:_(s16) = G_OR [[AND6]], [[TRUNC6]]
584 ; CHECK: [[TRUNC7:%[0-9]+]]:_(s16) = G_TRUNC [[C6]](s32)
585 ; CHECK: [[MV:%[0-9]+]]:_(s64) = G_MERGE_VALUES [[OR]](s16), [[OR1]](s16), [[OR2]](s16), [[TRUNC7]](s16)
586 ; CHECK: [[TRUNC8:%[0-9]+]]:_(s56) = G_TRUNC [[MV]](s64)
587 ; CHECK: S_NOP 0, implicit [[TRUNC8]](s56)
588 %0:_(s8) = G_CONSTANT i8 0
589 %1:_(s8) = G_CONSTANT i8 1
590 %2:_(s8) = G_CONSTANT i8 2
591 %3:_(s8) = G_CONSTANT i8 3
592 %4:_(s8) = G_CONSTANT i8 4
593 %5:_(s8) = G_CONSTANT i8 5
594 %6:_(s8) = G_CONSTANT i8 6
595 %7:_(s56) = G_MERGE_VALUES %0, %1, %2, %3, %4, %5, %6
596 S_NOP 0, implicit %7
597 ...
77 //===----------------------------------------------------------------------===//
88
99 #include "GISelMITest.h"
10
11 using namespace LegalizeActions;
12 using namespace LegalizeMutations;
13 using namespace LegalityPredicates;
1014
1115 namespace {
1216
899903 EXPECT_TRUE(CheckMachineFunction(*MF, CheckStr)) << *MF;
900904 }
901905
906 TEST_F(GISelMITest, LowerMergeValues) {
907 if (!TM)
908 return;
909
910 const LLT S24 = LLT::scalar(24);
911 const LLT S9 = LLT::scalar(9);
912 const LLT S3 = LLT::scalar(3);
913
914 DefineLegalizerInfo(A, {
915 getActionDefinitionsBuilder(G_UNMERGE_VALUES)
916 .widenScalarIf(typeIs(1, LLT::scalar(3)), changeTo(1, LLT::scalar(9)));
917 });
918
919 AInfo Info(MF->getSubtarget());
920 DummyGISelObserver Observer;
921 LegalizerHelper Helper(*MF, Info, Observer, B);
922 B.setInsertPt(*EntryMBB, EntryMBB->end());
923
924 // 24 = 3 3 3 3 3 3 3 3
925 // => 9
926 //
927 // This can do 2 merges for the first parts, but has 2 leftover operands.
928 SmallVector MergeOps;
929 for (int I = 0; I != 8; ++I)
930 MergeOps.push_back(B.buildConstant(S3, I).getReg(0));
931
932 auto Merge = B.buildMerge(S24, MergeOps);
933 EXPECT_EQ(LegalizerHelper::LegalizeResult::UnableToLegalize,
934 Helper.lower(*Merge, 1, S9));
935 }
902936 } // namespace