WebAssembly: handle unused function arguments.
authorJF Bastien <jfb@google.com>
Fri, 31 Jul 2015 18:13:27 +0000 (18:13 +0000)
committerJF Bastien <jfb@google.com>
Fri, 31 Jul 2015 18:13:27 +0000 (18:13 +0000)
Subscribers: llvm-commits, sunfish, jfb

Differential Revision: http://reviews.llvm.org/D11684

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@243770 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/WebAssembly/WebAssemblyISelLowering.cpp
test/CodeGen/WebAssembly/unused-argument.ll [new file with mode: 0644]

index 639e19a..90a4078 100644 (file)
@@ -191,11 +191,13 @@ SDValue WebAssemblyTargetLowering::LowerFormalArguments(
       fail(DL, DAG, "WebAssembly hasn't implemented split arguments");
     if (In.VT != MVT::i32)
       fail(DL, DAG, "WebAssembly hasn't implemented non-i32 arguments");
-    if (!In.Used)
-      fail(DL, DAG, "WebAssembly hasn't implemented unused arguments");
     // FIXME Do something with In.getOrigAlign()?
-    InVals.push_back(DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
-                                 DAG.getTargetConstant(ArgNo++, DL, MVT::i32)));
+    InVals.push_back(
+        In.Used
+            ? DAG.getNode(WebAssemblyISD::ARGUMENT, DL, In.VT,
+                          DAG.getTargetConstant(ArgNo, DL, MVT::i32))
+            : DAG.getNode(ISD::UNDEF, DL, In.VT));
+    ++ArgNo;
   }
 
   return Chain;
diff --git a/test/CodeGen/WebAssembly/unused-argument.ll b/test/CodeGen/WebAssembly/unused-argument.ll
new file mode 100644 (file)
index 0000000..5e546f7
--- /dev/null
@@ -0,0 +1,20 @@
+; RUN: llc < %s -asm-verbose=false | FileCheck %s
+
+; Make sure that argument offsets are correct even if some arguments are unused.
+
+target datalayout = "e-p:32:32-i64:64-v128:8:128-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+; CHECK-LABEL: unused_first:
+; CHECK-NEXT: (setlocal @0 (argument 1))
+; CHECK-NEXT: (RETURN @0)
+define i32 @unused_first(i32 %x, i32 %y) {
+  ret i32 %y
+}
+
+; CHECK-LABEL: unused_second:
+; CHECK-NEXT: (setlocal @0 (argument 0))
+; CHECK-NEXT: (RETURN @0)
+define i32 @unused_second(i32 %x, i32 %y) {
+  ret i32 %x
+}