[PATCH] D127546: [GISel] Fix narrowScalar() for big endian targets

Kai Nacke via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 10 15:47:29 PDT 2022


Kai created this revision.
Kai added reviewers: arsenm, aemerson.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Kai requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

The function `narrowScalar()` in the LegalizerHelper.cpp does not work correctly
for big endian targets. Breaking down a constant for a big endian target requires
that the big end is stored first. However, the current code does not do this.

This affects all big-endian targets, but the m68k target is the only big endian
target currently implementing GlobalISel.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127546

Files:
  llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
  llvm/test/CodeGen/M68k/GlobalISel/constant.ll


Index: llvm/test/CodeGen/M68k/GlobalISel/constant.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/M68k/GlobalISel/constant.ll
@@ -0,0 +1,11 @@
+; RUN: llc -march=m68k -global-isel -stop-after=legalizer -simplify-mir < %s | FileCheck %s
+
+; CHECK: name: func
+; CHECK: legalized:       true
+; CHECK: body:
+; CHECK:    {{%[0-9]}}:_(s32) = G_CONSTANT i32 0
+; CHECK:    {{%[0-9]}}:_(s32) = G_CONSTANT i32 51966
+
+define i64 @func() {
+  ret i64 51966 ; 0xcafe
+}
Index: llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
===================================================================
--- llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
+++ llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
@@ -948,8 +948,11 @@
     int NumParts = TotalSize / NarrowSize;
 
     SmallVector<Register, 4> PartRegs;
+    bool IsBigEndian = MIRBuilder.getDataLayout().isBigEndian();
     for (int I = 0; I != NumParts; ++I) {
       unsigned Offset = I * NarrowSize;
+      if (IsBigEndian)
+        Offset = TotalSize - NarrowSize - Offset;
       auto K = MIRBuilder.buildConstant(NarrowTy,
                                         Val.lshr(Offset).trunc(NarrowSize));
       PartRegs.push_back(K.getReg(0));
@@ -961,8 +964,8 @@
     if (LeftoverBits != 0) {
       LeftoverTy = LLT::scalar(LeftoverBits);
       auto K = MIRBuilder.buildConstant(
-        LeftoverTy,
-        Val.lshr(NumParts * NarrowSize).trunc(LeftoverBits));
+          LeftoverTy,
+          Val.lshr(!IsBigEndian * NumParts * NarrowSize).trunc(LeftoverBits));
       LeftoverRegs.push_back(K.getReg(0));
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127546.436063.patch
Type: text/x-patch
Size: 1630 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220610/049a2176/attachment-0001.bin>


More information about the llvm-commits mailing list