Index: lib/Target/WebAssembly/WebAssemblyISD.def =================================================================== --- lib/Target/WebAssembly/WebAssemblyISD.def +++ lib/Target/WebAssembly/WebAssemblyISD.def @@ -17,6 +17,7 @@ HANDLE_NODETYPE(CALL) HANDLE_NODETYPE(RETURN) HANDLE_NODETYPE(ARGUMENT) +HANDLE_NODETYPE(LOADGLOBAL) HANDLE_NODETYPE(Wrapper) // add memory opcodes starting at ISD::FIRST_TARGET_MEMORY_OPCODE here... Index: lib/Target/WebAssembly/WebAssemblyInstrMemory.td =================================================================== --- lib/Target/WebAssembly/WebAssemblyInstrMemory.td +++ lib/Target/WebAssembly/WebAssemblyInstrMemory.td @@ -45,6 +45,14 @@ * store_global: store a given value to a given global variable */ +def WebAssemblyLOADGLOBAL : SDNode<"WebAssemblyISD::LOADGLOBAL", SDTIntUnaryOp>; +let mayLoad = 1 in +def LOADGLOBAL : I<(outs Int32:$dst), (ins global:$addr), + [(set Int32:$dst, + (WebAssemblyLOADGLOBAL tglobaladdr:$addr))]>; +def : Pat<(load (WebAssemblywrapper tglobaladdr:$addr)), + (LOADGLOBAL tglobaladdr:$addr)>; + def page_size_I32 : I<(outs Int32:$dst), (ins), [(set Int32:$dst, (int_wasm_page_size))]>, Requires<[HasAddr32]>; Index: test/CodeGen/WebAssembly/global-load.ll =================================================================== --- /dev/null +++ test/CodeGen/WebAssembly/global-load.ll @@ -0,0 +1,19 @@ +; RUN: llc < %s -asm-verbose=false | FileCheck %s + +; Test that global loads assemble as expected. + +target datalayout = "e-p:32:32-i64:64-v128:8:128-n32:64-S128" +target triple = "wasm32-unknown-unknown" + +@life = private global i32 42; + +define i32 @answer() { +; CHECK-LABEL: (func $answer +; CHECK-NEXT: (result i32) +; CHECK-NEXT: (setlocal @0 (loadglobal $life)) +; CHECK-NEXT: (return @0) + %universe = load i32, i32* @life + ret i32 %universe +} + +; CHECK: (global $life i32 42)