GlobalISel: translate floating-point constants
authorTim Northover <tnorthover@apple.com>
Fri, 19 Aug 2016 20:09:15 +0000 (20:09 +0000)
committerTim Northover <tnorthover@apple.com>
Fri, 19 Aug 2016 20:09:15 +0000 (20:09 +0000)
llvm-svn: 279311

llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h
llvm/include/llvm/Target/GenericOpcodes.td
llvm/include/llvm/Target/TargetOpcodes.def
llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp
llvm/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll

index 70f7001..c8531e2 100644 (file)
@@ -185,6 +185,17 @@ public:
   /// \return The newly created instruction.
   MachineInstrBuilder buildConstant(LLT Ty, unsigned Res, int64_t Val);
 
+  /// Build and insert \p Res = G_FCONSTANT \p Ty \p Val
+  ///
+  /// G_FCONSTANT is a floating-point constant with the specified size and
+  /// value.
+  ///
+  /// \pre setBasicBlock or setMI must have been called.
+  ///
+  /// \return The newly created instruction.
+  MachineInstrBuilder buildFConstant(LLT Ty, unsigned Res,
+                                     const ConstantFP &Val);
+
   /// Build and insert \p Res<def> = COPY Op
   ///
   /// Register-to-register COPY sets \p Res to \p Op.
index e832ca7..9a4fde0 100644 (file)
@@ -79,6 +79,12 @@ def G_CONSTANT : Instruction {
   let hasSideEffects = 0;
 }
 
+def G_FCONSTANT : Instruction {
+  let OutOperandList = (outs unknown:$dst);
+  let InOperandList = (ins unknown:$imm);
+  let hasSideEffects = 0;
+}
+
 //------------------------------------------------------------------------------
 // Binary ops.
 //------------------------------------------------------------------------------
index ea9bfaf..ad87497 100644 (file)
@@ -244,6 +244,9 @@ HANDLE_TARGET_OPCODE(G_TRUNC)
 /// Generic integer constant.
 HANDLE_TARGET_OPCODE(G_CONSTANT)
 
+/// Generic floating constant.
+HANDLE_TARGET_OPCODE(G_FCONSTANT)
+
 // Generic sign extend
 HANDLE_TARGET_OPCODE(G_SEXT)
 
index 58235c2..1a243c5 100644 (file)
@@ -408,6 +408,8 @@ bool IRTranslator::translate(const Instruction &Inst) {
 bool IRTranslator::translate(const Constant &C, unsigned Reg) {
   if (auto CI = dyn_cast<ConstantInt>(&C))
     EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
+  else if (auto CF = dyn_cast<ConstantFP>(&C))
+    EntryBuilder.buildFConstant(LLT{*CF->getType()}, Reg, *CF);
   else if (isa<UndefValue>(C))
     EntryBuilder.buildInstr(TargetOpcode::IMPLICIT_DEF).addDef(Reg);
   else if (isa<ConstantPointerNull>(C))
index 0d2fae6..2fe6eab 100644 (file)
@@ -100,6 +100,11 @@ MachineInstrBuilder MachineIRBuilder::buildConstant(LLT Ty, unsigned Res,
   return buildInstr(TargetOpcode::G_CONSTANT, Ty).addDef(Res).addImm(Val);
 }
 
+MachineInstrBuilder MachineIRBuilder::buildFConstant(LLT Ty, unsigned Res,
+                                                    const ConstantFP &Val) {
+  return buildInstr(TargetOpcode::G_FCONSTANT, Ty).addDef(Res).addFPImm(&Val);
+}
+
 MachineInstrBuilder MachineIRBuilder::buildBrCond(LLT Ty, unsigned Tst,
                                                   MachineBasicBlock &Dest) {
   return buildInstr(TargetOpcode::G_BRCOND, Ty).addUse(Tst).addMBB(&Dest);
index 8340bc8..fd823f9 100644 (file)
@@ -784,3 +784,12 @@ define void @test_uitofp(double* %addr, i32 %in) {
   store double %fp, double* %addr
   ret void
 }
+
+; CHECK-LABEL: name: test_constant_float
+; CHECK: [[ADDR:%[0-9]+]](64) = COPY %x0
+; CHECK: [[TMP:%[0-9]+]](32) = G_FCONSTANT s32 float 1.500000e+00
+; CHECK: G_STORE { s32, p0 } [[TMP]], [[ADDR]]
+define void @test_constant_float(float* %addr) {
+  store float 1.5, float* %addr
+  ret void
+}