Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Commit b788b03

Browse files
committed
[CodeGen] Fix result type for SMULO/UMULO legalization
On some platforms (like MSP430) the second element of the result structure for SMULO/UMULO may have a shorter type than the one returned by SetCC. We need to truncate it to the right type, or else some incorrect code may be generated later on. This fixes issue rust-lang/rust#37829 Patch by Vadzim Dambrouski! Differential Revision: https://reviews.llvm.org/D27154 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288857 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 99662ba commit b788b03

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,6 +3515,15 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
35153515
TopHalf = DAG.getSetCC(dl, getSetCCResultType(VT), TopHalf,
35163516
DAG.getConstant(0, dl, VT), ISD::SETNE);
35173517
}
3518+
3519+
// Truncate the result if SetCC returns a larger type than needed.
3520+
EVT RType = Node->getValueType(1);
3521+
if (RType.getSizeInBits() < TopHalf.getValueSizeInBits())
3522+
TopHalf = DAG.getNode(ISD::TRUNCATE, dl, RType, TopHalf);
3523+
3524+
assert(RType.getSizeInBits() == TopHalf.getValueSizeInBits() &&
3525+
"Unexpected result type for S/UMULO legalization");
3526+
35183527
Results.push_back(BottomHalf);
35193528
Results.push_back(TopHalf);
35203529
break;

test/CodeGen/MSP430/umulo-16.ll

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
; RUN: llc < %s -march=msp430 | FileCheck %s
2+
target datalayout = "e-m:e-p:16:16-i32:16:32-a:16-n8:16"
3+
target triple = "msp430"
4+
5+
define void @foo(i16 %arg) unnamed_addr {
6+
entry-block:
7+
br i1 undef, label %bb2, label %bb3
8+
9+
bb2: ; preds = %entry-block
10+
unreachable
11+
12+
bb3: ; preds = %entry-block
13+
%0 = call { i16, i1 } @llvm.umul.with.overflow.i16(i16 undef, i16 %arg)
14+
; CHECK: call
15+
%1 = extractvalue { i16, i1 } %0, 1
16+
%2 = call i1 @llvm.expect.i1(i1 %1, i1 false)
17+
br i1 %2, label %panic, label %bb5
18+
19+
bb5: ; preds = %bb3
20+
unreachable
21+
22+
panic: ; preds = %bb3
23+
unreachable
24+
}
25+
26+
; Function Attrs: nounwind readnone
27+
declare i1 @llvm.expect.i1(i1, i1) #0
28+
29+
; Function Attrs: nounwind readnone
30+
declare { i16, i1 } @llvm.umul.with.overflow.i16(i16, i16) #0
31+
32+
attributes #0 = { nounwind readnone }

0 commit comments

Comments
 (0)