Skip to content

Commit ba8f085

Browse files
committed
[WebAssembly] Bail out in selectTrunc for non-simple types
1 parent 5896a25 commit ba8f085

File tree

3 files changed

+59
-15
lines changed

3 files changed

+59
-15
lines changed

llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ class WebAssemblyFastISel final : public FastISel {
157157
MachineMemOperand *MMO);
158158
unsigned maskI1Value(unsigned Reg, const Value *V);
159159
unsigned getRegForI1Value(const Value *V, const BasicBlock *BB, bool &Not);
160+
unsigned truncate(unsigned Reg, const Value *V, MVT::SimpleValueType From,
161+
MVT::SimpleValueType To);
160162
unsigned zeroExtendToI32(unsigned Reg, const Value *V,
161163
MVT::SimpleValueType From);
162164
unsigned signExtendToI32(unsigned Reg, const Value *V,
@@ -519,6 +521,28 @@ unsigned WebAssemblyFastISel::signExtendToI32(unsigned Reg, const Value *V,
519521
return Right;
520522
}
521523

524+
unsigned WebAssemblyFastISel::truncate(unsigned Reg, const Value *V,
525+
MVT::SimpleValueType From,
526+
MVT::SimpleValueType To) {
527+
if (From == MVT::i64) {
528+
if (To == MVT::i64)
529+
return copyValue(Reg);
530+
531+
if (To == MVT::i1 || To == MVT::i8 || To == MVT::i16 || To == MVT::i32) {
532+
Register Result = createResultReg(&WebAssembly::I32RegClass);
533+
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
534+
TII.get(WebAssembly::I32_WRAP_I64), Result)
535+
.addReg(Reg);
536+
return Result;
537+
}
538+
}
539+
540+
if (From == MVT::i32)
541+
return copyValue(Reg);
542+
543+
return 0;
544+
}
545+
522546
unsigned WebAssemblyFastISel::zeroExtend(unsigned Reg, const Value *V,
523547
MVT::SimpleValueType From,
524548
MVT::SimpleValueType To) {
@@ -988,23 +1012,17 @@ bool WebAssemblyFastISel::selectSelect(const Instruction *I) {
9881012
bool WebAssemblyFastISel::selectTrunc(const Instruction *I) {
9891013
const auto *Trunc = cast<TruncInst>(I);
9901014

991-
Register Reg = getRegForValue(Trunc->getOperand(0));
1015+
const Value *Op = Trunc->getOperand(0);
1016+
MVT::SimpleValueType From = getSimpleType(Op->getType());
1017+
MVT::SimpleValueType To = getLegalType(getSimpleType(Trunc->getType()));
1018+
Register In = getRegForValue(Op);
1019+
if (In == 0)
1020+
return false;
1021+
unsigned Reg = truncate(In, Op, From, To);
9921022
if (Reg == 0)
9931023
return false;
9941024

995-
unsigned FromBitWidth = Trunc->getOperand(0)->getType()->getIntegerBitWidth();
996-
unsigned ToBitWidth = Trunc->getType()->getIntegerBitWidth();
997-
998-
if (ToBitWidth <= 32 && (32 < FromBitWidth && FromBitWidth <= 64)) {
999-
Register Result = createResultReg(&WebAssembly::I32RegClass);
1000-
BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, MIMD,
1001-
TII.get(WebAssembly::I32_WRAP_I64), Result)
1002-
.addReg(Reg);
1003-
Reg = Result;
1004-
}
1005-
1006-
updateValueMap(Trunc, Reg);
1007-
return true;
1025+
updateValueMap(Trunc, Reg); return true;
10081026
}
10091027

10101028
bool WebAssemblyFastISel::selectZExt(const Instruction *I) {

llvm/test/CodeGen/WebAssembly/fast-isel-pr138479.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
; RUN: llc < %s -asm-verbose=false -fast-isel -fast-isel-abort=1 -verify-machineinstrs | FileCheck %s
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
2+
; RUN: llc < %s -asm-verbose=false -fast-isel -fast-isel-abort=0 -verify-machineinstrs | FileCheck %s
23

34
target triple = "wasm32-unknown-unknown"
45

@@ -13,3 +14,5 @@ define void @call_trunc_i64_to_i48(i64 %x) {
1314
call void @extern48(i48 %x48)
1415
ret void
1516
}
17+
;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
18+
; CHECK: {{.*}}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
2+
; RUN: llc < %s -fast-isel -fast-isel-abort=0 -mattr=+simd128 -verify-machineinstrs | FileCheck %s
3+
4+
target triple = "wasm32-unknown-unknown"
5+
6+
define i8 @pr165438(<4 x i32> %0) {
7+
; CHECK-LABEL: pr165438:
8+
; CHECK: .functype pr165438 (v128) -> (i32)
9+
; CHECK-NEXT: # %bb.0: # %entry
10+
; CHECK-NEXT: local.get 0
11+
; CHECK-NEXT: local.get 0
12+
; CHECK-NEXT: i8x16.shuffle 0, 4, 8, 12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
13+
; CHECK-NEXT: i8x16.extract_lane_u 0
14+
; CHECK-NEXT: # fallthrough-return
15+
entry:
16+
%conv = trunc <4 x i32> %0 to <4 x i8>
17+
br label %cond.true
18+
19+
20+
cond.true: ; preds = %entry
21+
%vecext = extractelement <4 x i8> %conv, i32 0
22+
ret i8 %vecext
23+
}

0 commit comments

Comments
 (0)