[llvm] r278168 - GlobalISel: first translation support for Constants.

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 9 14:28:05 PDT 2016


Author: tnorthover
Date: Tue Aug  9 16:28:04 2016
New Revision: 278168

URL: http://llvm.org/viewvc/llvm-project?rev=278168&view=rev
Log:
GlobalISel: first translation support for Constants.

For now put them all in the entry block. This should be correct but may give
poor runtime performance. Hopefully MachineSinking combined with
isReMaterializable can solve those issues, but if not the interface is sound
enough to support alternatives.

Modified:
    llvm/trunk/include/llvm/CodeGen/GlobalISel/IRTranslator.h
    llvm/trunk/lib/CodeGen/GlobalISel/IRTranslator.cpp
    llvm/trunk/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll

Modified: llvm/trunk/include/llvm/CodeGen/GlobalISel/IRTranslator.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/GlobalISel/IRTranslator.h?rev=278168&r1=278167&r2=278168&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/GlobalISel/IRTranslator.h (original)
+++ llvm/trunk/include/llvm/CodeGen/GlobalISel/IRTranslator.h Tue Aug  9 16:28:04 2016
@@ -100,6 +100,13 @@ private:
   /// \return true if the translation succeeded.
   bool translate(const Instruction &Inst);
 
+  /// Materialize \p C into virtual-register \p Reg. The generic instructions
+  /// performing this materialization will be inserted into the entry block of
+  /// the function.
+  ///
+  /// \return true if the materialization succeeded.
+  bool translate(const Constant &C, unsigned Reg);
+
   /// Translate an LLVM bitcast into generic IR. Either a COPY or a G_BITCAST is
   /// emitted.
   bool translateBitCast(const CastInst &CI);
@@ -151,6 +158,10 @@ private:
   // IRBuilder, but for Machine IR.
   MachineIRBuilder MIRBuilder;
 
+  // Builder set to the entry block (just after ABI lowering instructions). Used
+  // as a convenient location for Constants.
+  MachineIRBuilder EntryBuilder;
+
   /// MachineRegisterInfo used to create virtual registers.
   MachineRegisterInfo *MRI;
 

Modified: llvm/trunk/lib/CodeGen/GlobalISel/IRTranslator.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/GlobalISel/IRTranslator.cpp?rev=278168&r1=278167&r2=278168&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/GlobalISel/IRTranslator.cpp (original)
+++ llvm/trunk/lib/CodeGen/GlobalISel/IRTranslator.cpp Tue Aug  9 16:28:04 2016
@@ -49,7 +49,12 @@ unsigned IRTranslator::getOrCreateVReg(c
     unsigned Size = DL->getTypeSizeInBits(Val.getType());
     unsigned VReg = MRI->createGenericVirtualRegister(Size);
     ValReg = VReg;
-    assert(!isa<Constant>(Val) && "Not yet implemented");
+
+    if (auto CV = dyn_cast<Constant>(&Val)) {
+      bool Success = translate(*CV, VReg);
+      if (!Success)
+        report_fatal_error("unable to translate constant");
+    }
   }
   return ValReg;
 }
@@ -312,6 +317,15 @@ bool IRTranslator::translate(const Instr
   }
 }
 
+bool IRTranslator::translate(const Constant &C, unsigned Reg) {
+  if (auto CI = dyn_cast<ConstantInt>(&C)) {
+    EntryBuilder.buildConstant(LLT{*CI->getType()}, Reg, CI->getZExtValue());
+    return true;
+  }
+
+  llvm_unreachable("unhandled constant kind");
+}
+
 
 void IRTranslator::finalize() {
   // Release the memory used by the different maps we
@@ -326,6 +340,7 @@ bool IRTranslator::runOnMachineFunction(
     return false;
   CLI = MF.getSubtarget().getCallLowering();
   MIRBuilder.setMF(MF);
+  EntryBuilder.setMF(MF);
   MRI = &MF.getRegInfo();
   DL = &F.getParent()->getDataLayout();
 
@@ -342,6 +357,13 @@ bool IRTranslator::runOnMachineFunction(
   if (!Succeeded)
     report_fatal_error("Unable to lower arguments");
 
+  // Now that we've got the ABI handling code, it's safe to set a location for
+  // any Constants we find in the IR.
+  if (MBB.empty())
+    EntryBuilder.setMBB(MBB);
+  else
+    EntryBuilder.setInstr(MBB.back(), /* Before */ false);
+
   for (const BasicBlock &BB: F) {
     MachineBasicBlock &MBB = getOrCreateBB(BB);
     // Set the insertion point of all the following translations to

Modified: llvm/trunk/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll?rev=278168&r1=278167&r2=278168&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll (original)
+++ llvm/trunk/test/CodeGen/AArch64/GlobalISel/arm64-irtranslator.ll Tue Aug  9 16:28:04 2016
@@ -332,3 +332,34 @@ define void @unreachable(i32 %a) {
   %sum = add i32 %a, %a
   unreachable
 }
+
+  ; It's important that constants are after argument passing, but before the
+  ; rest of the entry block.
+; CHECK-LABEL: name: constant_int
+; CHECK: [[IN:%[0-9]+]](32) = COPY %w0
+; CHECK: [[ONE:%[0-9]+]](32) = G_CONSTANT s32 1
+; CHECK: G_BR unsized
+
+; CHECK: [[SUM1:%[0-9]+]](32) = G_ADD s32 [[IN]], [[ONE]]
+; CHECK: [[SUM2:%[0-9]+]](32) = G_ADD s32 [[IN]], [[ONE]]
+; CHECK: [[RES:%[0-9]+]](32) = G_ADD s32 [[SUM1]], [[SUM2]]
+; CHECK: %w0 = COPY [[RES]]
+
+define i32 @constant_int(i32 %in) {
+  br label %next
+
+next:
+  %sum1 = add i32 %in, 1
+  %sum2 = add i32 %in, 1
+  %res = add i32 %sum1, %sum2
+  ret i32 %res
+}
+
+; CHECK-LABEL: name: constant_int_start
+; CHECK: [[TWO:%[0-9]+]](32) = G_CONSTANT s32 2
+; CHECK: [[ANSWER:%[0-9]+]](32) = G_CONSTANT s32 42
+; CHECK: [[RES:%[0-9]+]](32) = G_ADD s32 [[TWO]], [[ANSWER]]
+define i32 @constant_int_start() {
+  %res = add i32 2, 42
+  ret i32 %res
+}




More information about the llvm-commits mailing list