[PATCH] D73948: [GlobalISel] Legalize more G_FP(EXT|TRUNC) libcalls.

Konstantin Schwarz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 4 02:15:50 PST 2020


kschwarz created this revision.
kschwarz added reviewers: dsanders, arsenm.
Herald added subscribers: llvm-commits, Petar.Avramovic, volkan, hiraditya, rovka, wdng.
Herald added a project: LLVM.

This adds a new helper function for retrieving the floating point type corresponding to the specified bit-width.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D73948

Files:
  llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp


Index: llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -115,6 +115,21 @@
   llvm_unreachable("not yet handled");
 }
 
+static Type *getFloatTypeForSizeInBits(LLVMContext &Ctx, unsigned Size) {
+  switch (Size) {
+  case 16:
+    return Type::getHalfTy(Ctx);
+  case 32:
+    return Type::getFloatTy(Ctx);
+  case 64:
+    return Type::getDoubleTy(Ctx);
+  case 128:
+    return Type::getFP128Ty(Ctx);
+  default:
+    llvm_unreachable("unexpected floating-point type");
+  }
+}
+
 LegalizerHelper::LegalizerHelper(MachineFunction &MF,
                                  GISelChangeObserver &Observer,
                                  MachineIRBuilder &Builder)
@@ -708,32 +723,19 @@
       LLVM_DEBUG(dbgs() << "Size " << Size << " too large to legalize.\n");
       return UnableToLegalize;
     }
-    Type *HLTy = Size == 64 ? Type::getDoubleTy(Ctx) : Type::getFloatTy(Ctx);
+    Type *HLTy = getFloatTypeForSizeInBits(Ctx, Size);
     auto Status = simpleLibcall(MI, MIRBuilder, Size, HLTy);
     if (Status != Legalized)
       return Status;
     break;
   }
-  case TargetOpcode::G_FPEXT: {
-    // FIXME: Support other floating point types (half, fp128 etc)
-    unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
-    unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
-    if (ToSize != 64 || FromSize != 32)
-      return UnableToLegalize;
-    LegalizeResult Status = conversionLibcall(
-        MI, MIRBuilder, Type::getDoubleTy(Ctx), Type::getFloatTy(Ctx));
-    if (Status != Legalized)
-      return Status;
-    break;
-  }
+  case TargetOpcode::G_FPEXT:
   case TargetOpcode::G_FPTRUNC: {
-    // FIXME: Support other floating point types (half, fp128 etc)
     unsigned FromSize = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
     unsigned ToSize = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
-    if (ToSize != 32 || FromSize != 64)
-      return UnableToLegalize;
     LegalizeResult Status = conversionLibcall(
-        MI, MIRBuilder, Type::getFloatTy(Ctx), Type::getDoubleTy(Ctx));
+        MI, MIRBuilder, getFloatTypeForSizeInBits(Ctx, ToSize),
+        getFloatTypeForSizeInBits(Ctx, FromSize));
     if (Status != Legalized)
       return Status;
     break;
@@ -2230,22 +2232,7 @@
     Register Res = MI.getOperand(0).getReg();
     Type *ZeroTy;
     LLVMContext &Ctx = MIRBuilder.getMF().getFunction().getContext();
-    switch (Ty.getSizeInBits()) {
-    case 16:
-      ZeroTy = Type::getHalfTy(Ctx);
-      break;
-    case 32:
-      ZeroTy = Type::getFloatTy(Ctx);
-      break;
-    case 64:
-      ZeroTy = Type::getDoubleTy(Ctx);
-      break;
-    case 128:
-      ZeroTy = Type::getFP128Ty(Ctx);
-      break;
-    default:
-      llvm_unreachable("unexpected floating-point type");
-    }
+    ZeroTy = getFloatTypeForSizeInBits(Ctx, Ty.getSizeInBits());
     ConstantFP &ZeroForNegation =
         *cast<ConstantFP>(ConstantFP::getZeroValueForNegation(ZeroTy));
     auto Zero = MIRBuilder.buildFConstant(Ty, ZeroForNegation);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D73948.242275.patch
Type: text/x-patch
Size: 3216 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200204/6d054cae/attachment.bin>


More information about the llvm-commits mailing list