OSDN Git Service

[AArch64][GlobalISel] Legalize narrow scalar fp->int conversions.
authorAhmed Bougacha <ahmed.bougacha@gmail.com>
Mon, 23 Jan 2017 21:10:14 +0000 (21:10 +0000)
committerAhmed Bougacha <ahmed.bougacha@gmail.com>
Mon, 23 Jan 2017 21:10:14 +0000 (21:10 +0000)
Since we're now avoiding operations using narrow scalar integer types,
we have to legalize the integer side of the FP conversions.

This requires teaching the legalizer how to do that.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@292828 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/GlobalISel/LegalizerHelper.cpp
lib/Target/AArch64/AArch64LegalizerInfo.cpp
test/CodeGen/AArch64/GlobalISel/legalize-fptoi.mir

index f77d807..6da0264 100644 (file)
@@ -273,6 +273,20 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
     MI.eraseFromParent();
     return Legalized;
   }
+  case TargetOpcode::G_FPTOSI:
+  case TargetOpcode::G_FPTOUI: {
+    if (TypeIdx != 0)
+      return UnableToLegalize;
+
+    unsigned DstExt = MRI.createGenericVirtualRegister(WideTy);
+    MIRBuilder.buildInstr(MI.getOpcode())
+        .addDef(DstExt)
+        .addUse(MI.getOperand(1).getReg());
+
+    MIRBuilder.buildTrunc(MI.getOperand(0).getReg(), DstExt);
+    MI.eraseFromParent();
+    return Legalized;
+  }
   case TargetOpcode::G_SITOFP:
   case TargetOpcode::G_UITOFP: {
     if (TypeIdx != 1)
index 9852607..871556c 100644 (file)
@@ -151,9 +151,8 @@ AArch64LegalizerInfo::AArch64LegalizerInfo() {
     setAction({G_UITOFP, 1, Ty}, Legal);
   }
   for (auto Ty : { s1, s8, s16 }) {
-    // FIXME: These should be widened on types smaller than s32.
-    setAction({G_FPTOSI, 0, Ty}, Legal);
-    setAction({G_FPTOUI, 0, Ty}, Legal);
+    setAction({G_FPTOSI, 0, Ty}, WidenScalar);
+    setAction({G_FPTOUI, 0, Ty}, WidenScalar);
     setAction({G_SITOFP, 1, Ty}, WidenScalar);
     setAction({G_UITOFP, 1, Ty}, WidenScalar);
   }
index 968256c..8d0af0d 100644 (file)
@@ -130,7 +130,8 @@ body: |
     %0:_(s32) = COPY %w0
 
     ; CHECK-LABEL: name: test_fptosi_s1_s32
-    ; CHECK: %1(s1) = G_FPTOSI %0
+    ; CHECK: %2(s32) = G_FPTOSI %0
+    ; CHECK: %1(s1) = G_TRUNC %2
     %1:_(s1) = G_FPTOSI %0
 ...
 
@@ -142,7 +143,8 @@ body: |
     %0:_(s32) = COPY %w0
 
     ; CHECK-LABEL: name: test_fptoui_s1_s32
-    ; CHECK: %1(s1) = G_FPTOUI %0
+    ; CHECK: %2(s32) = G_FPTOUI %0
+    ; CHECK: %1(s1) = G_TRUNC %2
     %1:_(s1) = G_FPTOUI %0
 ...
 
@@ -154,7 +156,8 @@ body: |
     %0:_(s64) = COPY %x0
 
     ; CHECK-LABEL: name: test_fptosi_s8_s64
-    ; CHECK: %1(s8) = G_FPTOSI %0
+    ; CHECK: %2(s32) = G_FPTOSI %0
+    ; CHECK: %1(s8) = G_TRUNC %2
     %1:_(s8) = G_FPTOSI %0
 ...
 
@@ -166,7 +169,8 @@ body: |
     %0:_(s64) = COPY %x0
 
     ; CHECK-LABEL: name: test_fptoui_s8_s64
-    ; CHECK: %1(s8) = G_FPTOUI %0
+    ; CHECK: %2(s32) = G_FPTOUI %0
+    ; CHECK: %1(s8) = G_TRUNC %2
     %1:_(s8) = G_FPTOUI %0
 ...
 
@@ -178,7 +182,8 @@ body: |
     %0:_(s32) = COPY %w0
 
     ; CHECK-LABEL: name: test_fptosi_s16_s32
-    ; CHECK: %1(s16) = G_FPTOSI %0
+    ; CHECK: %2(s32) = G_FPTOSI %0
+    ; CHECK: %1(s16) = G_TRUNC %2
     %1:_(s16) = G_FPTOSI %0
 ...
 
@@ -190,6 +195,7 @@ body: |
     %0:_(s32) = COPY %w0
 
     ; CHECK-LABEL: name: test_fptoui_s16_s32
-    ; CHECK: %1(s16) = G_FPTOUI %0
+    ; CHECK: %2(s32) = G_FPTOUI %0
+    ; CHECK: %1(s16) = G_TRUNC %2
     %1:_(s16) = G_FPTOUI %0
 ...