From e452d6e547cc3de34ff8cf9e768208939d05d80a Mon Sep 17 00:00:00 2001 From: Robert Lytton Date: Mon, 6 Jan 2014 14:20:32 +0000 Subject: [PATCH] XCore target: fix handling of unsized global arrays in large code model git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198609 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/XCore/XCoreISelLowering.cpp | 17 ++++++++--- test/CodeGen/XCore/codemodel.ll | 41 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/lib/Target/XCore/XCoreISelLowering.cpp b/lib/Target/XCore/XCoreISelLowering.cpp index 668a2486396..c6cbf51202c 100644 --- a/lib/Target/XCore/XCoreISelLowering.cpp +++ b/lib/Target/XCore/XCoreISelLowering.cpp @@ -268,6 +268,18 @@ getGlobalAddressWrapper(SDValue GA, const GlobalValue *GV, return DAG.getNode(XCoreISD::PCRelativeWrapper, dl, MVT::i32, GA); } +static bool IsSmallObject(const GlobalValue *GV, const XCoreTargetLowering &XTL) { + if (XTL.getTargetMachine().getCodeModel() == CodeModel::Small) + return true; + + Type *ObjType = GV->getType()->getPointerElementType(); + if (!ObjType->isSized()) + return false; + + unsigned ObjSize = XTL.getDataLayout()->getTypeAllocSize(ObjType); + return ObjSize < CodeModelLargeSize && ObjSize != 0; +} + SDValue XCoreTargetLowering:: LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { @@ -275,10 +287,7 @@ LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const const GlobalValue *GV = GN->getGlobal(); SDLoc DL(GN); int64_t Offset = GN->getOffset(); - Type *ObjType = GV->getType()->getPointerElementType(); - if (getTargetMachine().getCodeModel() == CodeModel::Small || - !ObjType->isSized() || - getDataLayout()->getTypeAllocSize(ObjType) < CodeModelLargeSize) { + if (IsSmallObject(GV, *this)) { // We can only fold positive offsets that are a multiple of the word size. int64_t FoldedOffset = std::max(Offset & ~3, (int64_t)0); SDValue GA = DAG.getTargetGlobalAddress(GV, DL, MVT::i32, FoldedOffset); diff --git a/test/CodeGen/XCore/codemodel.ll b/test/CodeGen/XCore/codemodel.ll index 31fbbca250c..cc532ea7def 100644 --- a/test/CodeGen/XCore/codemodel.ll +++ b/test/CodeGen/XCore/codemodel.ll @@ -35,6 +35,7 @@ entry: ret [50000 x i32]* %Addr } + ; CHECK: .section .cp.rodata.cst4,"aMc",@progbits,4 ; CHECK: .long 65536 ; CHECK: .text @@ -115,6 +116,46 @@ entry: ret i32 %16 } + +; CHECK-LABEL: UnknownSize: +; CHECK: ldw r0, dp[NoSize+40] +; CHECK-NEXT: retsp 0 +; +; LARGE: .section .cp.rodata,"ac",@progbits +; LARGE: .LCPI{{[0-9_]*}} +; LARGE-NEXT: .long NoSize +; LARGE-NEXT: .text +; LARGE-LABEL: UnknownSize: +; LARGE: ldw r0, cp[.LCPI{{[0-9_]*}}] +; LARGE-NEXT: ldw r0, r0[0] +; LARGE-NEXT: retsp 0 +@NoSize = external global [0 x i32] +define i32 @UnknownSize() nounwind { +entry: + %0 = load i32* getelementptr inbounds ([0 x i32]* @NoSize, i32 0, i32 10) + ret i32 %0 +} + + +; CHECK-LABEL: UnknownStruct: +; CHECK: ldaw r0, dp[Unknown] +; CHECK-NEXT: retsp 0 +; +; LARGE: .section .cp.rodata,"ac",@progbits +; LARGE: .LCPI{{[0-9_]*}} +; LARGE-NEXT: .long Unknown +; LARGE-NEXT: .text +; LARGE-LABEL: UnknownStruct: +; LARGE: ldw r0, cp[.LCPI{{[0-9_]*}}] +; LARGE-NEXT: retsp 0 +%Struct = type opaque +@Unknown = external global %Struct +define %Struct* @UnknownStruct() nounwind { +entry: + ret %Struct* @Unknown +} + + ; CHECK: .section .dp.bss,"awd",@nobits ; CHECK-LABEL: l: ; CHECK: .space 400