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 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)); } 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 +}