diff --git a/include/llvm/CodeGen/MachineModuleInfoImpls.h b/include/llvm/CodeGen/MachineModuleInfoImpls.h index 61cff3890b75..34b21ceddd43 100644 --- a/include/llvm/CodeGen/MachineModuleInfoImpls.h +++ b/include/llvm/CodeGen/MachineModuleInfoImpls.h @@ -77,33 +77,6 @@ class MachineModuleInfoELF : public MachineModuleInfoImpl { SymbolListTy GetGVStubList() { return getSortedStubs(GVStubs); } }; -/// MachineModuleInfoWasm - This is a MachineModuleInfoImpl implementation -/// for Wasm targets. -class MachineModuleInfoWasm : public MachineModuleInfoImpl { - /// WebAssembly global variables defined by CodeGen. - std::vector Globals; - - /// The WebAssembly global variable which is the stack pointer. - unsigned StackPointerGlobal; - - virtual void anchor(); // Out of line virtual method. -public: - MachineModuleInfoWasm(const MachineModuleInfo &) - : StackPointerGlobal(-1U) {} - - void addGlobal(const wasm::Global &G) { Globals.push_back(G); } - const std::vector &getGlobals() const { return Globals; } - - bool hasStackPointerGlobal() const { - return StackPointerGlobal != -1U; - } - unsigned getStackPointerGlobal() const { - assert(hasStackPointerGlobal() && "Stack ptr global hasn't been set"); - return StackPointerGlobal; - } - void setStackPointerGlobal(unsigned Global) { StackPointerGlobal = Global; } -}; - } // end namespace llvm #endif diff --git a/lib/CodeGen/MachineModuleInfoImpls.cpp b/lib/CodeGen/MachineModuleInfoImpls.cpp index 4c81fd91cb82..22d519e5d88f 100644 --- a/lib/CodeGen/MachineModuleInfoImpls.cpp +++ b/lib/CodeGen/MachineModuleInfoImpls.cpp @@ -23,7 +23,6 @@ using namespace llvm; // Out of line virtual method. void MachineModuleInfoMachO::anchor() {} void MachineModuleInfoELF::anchor() {} -void MachineModuleInfoWasm::anchor() {} static int SortSymbolPair(const void *LHS, const void *RHS) { typedef std::pair PairTy; diff --git a/lib/MC/WasmObjectWriter.cpp b/lib/MC/WasmObjectWriter.cpp index db304c027f99..7ac4fb8767ee 100644 --- a/lib/MC/WasmObjectWriter.cpp +++ b/lib/MC/WasmObjectWriter.cpp @@ -406,6 +406,8 @@ void WasmObjectWriter::recordRelocation(MCAssembler &Asm, } assert(!IsPCRel); + assert(SymA); + unsigned Type = getRelocType(Target, Fixup); WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection); @@ -474,6 +476,7 @@ uint32_t WasmObjectWriter::getRelocationIndexValue( assert(IndirectSymbolIndices.count(RelEntry.Symbol)); return IndirectSymbolIndices[RelEntry.Symbol]; case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: + case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB: case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB: case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32: @@ -500,7 +503,8 @@ void WasmObjectWriter::applyRelocations( switch (RelEntry.Type) { case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB: - case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: { + case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: + case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: { uint32_t Index = getRelocationIndexValue(RelEntry); WritePatchableSLEB(Stream, Index, Offset); break; @@ -526,7 +530,7 @@ void WasmObjectWriter::applyRelocations( break; } default: - llvm_unreachable("unsupported relocation type"); + llvm_unreachable("invalid relocation type"); } } } diff --git a/lib/Object/WasmObjectFile.cpp b/lib/Object/WasmObjectFile.cpp index 2304098c1dc9..5e0affb9ef94 100644 --- a/lib/Object/WasmObjectFile.cpp +++ b/lib/Object/WasmObjectFile.cpp @@ -325,6 +325,7 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr, case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB: case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32: case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: + case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: break; case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB: case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB: @@ -332,7 +333,8 @@ Error WasmObjectFile::parseRelocSection(StringRef Name, const uint8_t *Ptr, Reloc.Addend = readVarint32(Ptr); break; default: - return make_error("Bad relocation type", + return make_error("Bad relocation type: " + + Twine(Reloc.Type), object_error::parse_failed); } Section->Relocations.push_back(Reloc); diff --git a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp index c56c591def36..3e3b52fca569 100644 --- a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp +++ b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCCodeEmitter.cpp @@ -36,7 +36,6 @@ STATISTIC(MCNumFixups, "Number of MC fixups created."); namespace { class WebAssemblyMCCodeEmitter final : public MCCodeEmitter { const MCInstrInfo &MCII; - MCContext &Ctx; // Implementation generated by tablegen. uint64_t getBinaryCodeForInstr(const MCInst &MI, @@ -48,14 +47,12 @@ class WebAssemblyMCCodeEmitter final : public MCCodeEmitter { const MCSubtargetInfo &STI) const override; public: - WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx) - : MCII(mcii), Ctx(ctx) {} + WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {} }; } // end anonymous namespace -MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII, - MCContext &Ctx) { - return new WebAssemblyMCCodeEmitter(MCII, Ctx); +MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) { + return new WebAssemblyMCCodeEmitter(MCII); } void WebAssemblyMCCodeEmitter::encodeInstruction( @@ -89,11 +86,7 @@ void WebAssemblyMCCodeEmitter::encodeInstruction( } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) { encodeSLEB128(int64_t(MO.getImm()), OS); } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) { - Fixups.push_back(MCFixup::create( - OS.tell() - Start, MCConstantExpr::create(MO.getImm(), Ctx), - MCFixupKind(WebAssembly::fixup_code_global_index), MI.getLoc())); - ++MCNumFixups; - encodeULEB128(uint64_t(MO.getImm()), OS); + llvm_unreachable("wasm globals should only be accessed symbolicly"); } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) { encodeSLEB128(int64_t(MO.getImm()), OS); } else { @@ -135,6 +128,9 @@ void WebAssemblyMCCodeEmitter::encodeInstruction( Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) { FixupKind = MCFixupKind(WebAssembly::fixup_code_uleb128_i32); PaddedSize = 5; + } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) { + FixupKind = MCFixupKind(WebAssembly::fixup_code_global_index); + PaddedSize = 5; } else { llvm_unreachable("unexpected symbolic operand kind"); } diff --git a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp index 9fd3ec81c258..9580eeaa33d7 100644 --- a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp +++ b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp @@ -74,7 +74,7 @@ static MCInstPrinter *createMCInstPrinter(const Triple & /*T*/, static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII, const MCRegisterInfo & /*MRI*/, MCContext &Ctx) { - return createWebAssemblyMCCodeEmitter(MCII, Ctx); + return createWebAssemblyMCCodeEmitter(MCII); } static MCAsmBackend *createAsmBackend(const Target & /*T*/, diff --git a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h index 0ba700a86b74..4d676c32a09c 100644 --- a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h +++ b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h @@ -35,8 +35,7 @@ class raw_pwrite_stream; Target &getTheWebAssemblyTarget32(); Target &getTheWebAssemblyTarget64(); -MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII, - MCContext &Ctx); +MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII); MCAsmBackend *createWebAssemblyAsmBackend(const Triple &TT); diff --git a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp index 19e14f3261aa..9cf77829f3bc 100644 --- a/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp +++ b/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyWasmObjectWriter.cpp @@ -68,6 +68,8 @@ WebAssemblyWasmObjectWriter::getRelocType(const MCValue &Target, bool IsFunction = IsFunctionExpr(Fixup.getValue()); switch (unsigned(Fixup.getKind())) { + case WebAssembly::fixup_code_global_index: + return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB; case WebAssembly::fixup_code_sleb128_i32: if (IsFunction) return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB; diff --git a/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp index b999091e2d29..f51585a10ca1 100644 --- a/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -96,13 +96,6 @@ void WebAssemblyAsmPrinter::EmitEndOfAsmFile(Module &M) { MCConstantExpr::create(Size, OutContext)); } } - - if (!TM.getTargetTriple().isOSBinFormatELF()) { - MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo(); - getTargetStreamer()->emitGlobal(MMIW.getGlobals()); - if (MMIW.hasStackPointerGlobal()) - getTargetStreamer()->emitStackPointer(MMIW.getStackPointerGlobal()); - } } void WebAssemblyAsmPrinter::EmitConstantPool() { diff --git a/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp b/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp index 4209bc333f23..a37d6136e44e 100644 --- a/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp +++ b/lib/Target/WebAssembly/WebAssemblyFrameLowering.cpp @@ -104,10 +104,10 @@ static void writeSPToMemory(unsigned SrcReg, MachineFunction &MF, const DebugLoc &DL) { const auto *TII = MF.getSubtarget().getInstrInfo(); + const char *ES = "__stack_pointer"; + auto *SPSymbol = MF.createExternalSymbolName(ES); if (MF.getSubtarget() .getTargetTriple().isOSBinFormatELF()) { - const char *ES = "__stack_pointer"; - auto *SPSymbol = MF.createExternalSymbolName(ES); MachineRegisterInfo &MRI = MF.getRegInfo(); const TargetRegisterClass *PtrRC = MRI.getTargetRegisterInfo()->getPointerRegClass(MF); @@ -125,10 +125,8 @@ static void writeSPToMemory(unsigned SrcReg, MachineFunction &MF, .addReg(SrcReg) .addMemOperand(MMO); } else { - MachineModuleInfoWasm &MMIW = - MF.getMMI().getObjFileInfo(); BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::SET_GLOBAL_I32)) - .addImm(MMIW.getStackPointerGlobal()) + .addExternalSymbol(SPSymbol) .addReg(SrcReg); } } @@ -171,10 +169,11 @@ void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, unsigned SPReg = WebAssembly::SP32; if (StackSize) SPReg = MRI.createVirtualRegister(PtrRC); + + const char *ES = "__stack_pointer"; + auto *SPSymbol = MF.createExternalSymbolName(ES); if (MF.getSubtarget() .getTargetTriple().isOSBinFormatELF()) { - const char *ES = "__stack_pointer"; - auto *SPSymbol = MF.createExternalSymbolName(ES); unsigned Zero = MRI.createVirtualRegister(PtrRC); BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), Zero) @@ -189,22 +188,8 @@ void WebAssemblyFrameLowering::emitPrologue(MachineFunction &MF, .addReg(Zero) // addr .addMemOperand(LoadMMO); } else { - auto &MMIW = MF.getMMI().getObjFileInfo(); - if (!MMIW.hasStackPointerGlobal()) { - MMIW.setStackPointerGlobal(MMIW.getGlobals().size()); - - // Create the stack-pointer global. For now, just use the - // Emscripten/Binaryen ABI names. - wasm::Global G; - G.Type = wasm::ValType::I32; - G.Mutable = true; - G.InitialValue = 0; - G.InitialModule = "env"; - G.InitialName = "STACKTOP"; - MMIW.addGlobal(G); - } BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GET_GLOBAL_I32), SPReg) - .addImm(MMIW.getStackPointerGlobal()); + .addExternalSymbol(SPSymbol); } bool HasBP = hasBP(MF); diff --git a/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp b/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp index 6650191807dc..ea9e3fa862ce 100644 --- a/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp +++ b/lib/Target/WebAssembly/WebAssemblyRegStackify.cpp @@ -170,28 +170,16 @@ static void Query(const MachineInstr &MI, AliasAnalysis &AA, bool &Read, if (MI.mayStore()) { Write = true; - const MachineFunction &MF = *MI.getParent()->getParent(); - if (MF.getSubtarget() - .getTargetTriple().isOSBinFormatELF()) { - // Check for stores to __stack_pointer. - for (auto MMO : MI.memoperands()) { - const MachinePointerInfo &MPI = MMO->getPointerInfo(); - if (MPI.V.is()) { - auto PSV = MPI.V.get(); - if (const ExternalSymbolPseudoSourceValue *EPSV = - dyn_cast(PSV)) - if (StringRef(EPSV->getSymbol()) == "__stack_pointer") - StackPointer = true; - } - } - } else { - // Check for sets of the stack pointer. - const MachineModuleInfoWasm &MMIW = - MF.getMMI().getObjFileInfo(); - if ((MI.getOpcode() == WebAssembly::SET_LOCAL_I32 || - MI.getOpcode() == WebAssembly::SET_LOCAL_I64) && - MI.getOperand(0).getImm() == MMIW.getStackPointerGlobal()) { - StackPointer = true; + // Check for stores to __stack_pointer. + for (auto MMO : MI.memoperands()) { + const MachinePointerInfo &MPI = MMO->getPointerInfo(); + if (MPI.V.is()) { + auto PSV = MPI.V.get(); + if (const ExternalSymbolPseudoSourceValue *EPSV = + dyn_cast(PSV)) + if (StringRef(EPSV->getSymbol()) == "__stack_pointer") { + StackPointer = true; + } } } } else if (MI.hasOrderedMemoryRef()) { diff --git a/test/CodeGen/WebAssembly/byval.ll b/test/CodeGen/WebAssembly/byval.ll index 907320d7977c..84c5ad02101c 100644 --- a/test/CodeGen/WebAssembly/byval.ll +++ b/test/CodeGen/WebAssembly/byval.ll @@ -24,12 +24,12 @@ declare void @ext_byval_func_empty(%EmptyStruct* byval) define void @byval_arg(%SmallStruct* %ptr) { ; CHECK: .param i32 ; Subtract 16 from SP (SP is 16-byte aligned) - ; CHECK-NEXT: get_global $push[[L2:.+]]=, 0 + ; CHECK-NEXT: get_global $push[[L2:.+]]=, __stack_pointer ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 16 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L2]], $pop[[L3]] ; Ensure SP is stored back before the call ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}} - ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}} + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}} ; Copy the SmallStruct argument to the stack (SP+12, original SP-4) ; CHECK-NEXT: i32.load $push[[L0:.+]]=, 0($0) ; CHECK-NEXT: i32.store 12($[[SP]]), $pop[[L0]] @@ -41,7 +41,7 @@ define void @byval_arg(%SmallStruct* %ptr) { ; Restore the stack ; CHECK-NEXT: i32.const $push[[L6:.+]]=, 16 ; CHECK-NEXT: i32.add $push[[L8:.+]]=, $[[SP]], $pop[[L6]] - ; CHECK-NEXT: set_global 0, $pop[[L8]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]] ; CHECK-NEXT: return ret void } @@ -53,7 +53,7 @@ define void @byval_arg_align8(%SmallStruct* %ptr) { ; CHECK: i32.const $push[[L1:.+]]=, 16 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, {{.+}}, $pop[[L1]] ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}} - ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}} + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}} ; Copy the SmallStruct argument to the stack (SP+8, original SP-8) ; CHECK-NEXT: i32.load $push[[L0:.+]]=, 0($0){{$}} ; CHECK-NEXT: i32.store 8($[[SP]]), $pop[[L0]]{{$}} @@ -72,7 +72,7 @@ define void @byval_arg_double(%AlignedStruct* %ptr) { ; CHECK: i32.const $push[[L1:.+]]=, 16 ; CHECK-NEXT: i32.sub $push[[L14:.+]]=, {{.+}}, $pop[[L1]] ; CHECK-NEXT: tee_local $push[[L13:.+]]=, $[[SP:.+]]=, $pop[[L14]] - ; CHECK-NEXT: set_global 0, $pop[[L13]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L13]] ; Copy the AlignedStruct argument to the stack (SP+0, original SP-16) ; Just check the last load/store pair of the memcpy ; CHECK: i64.load $push[[L4:.+]]=, 0($0) @@ -110,11 +110,11 @@ define void @byval_empty_callee(%EmptyStruct* byval %ptr) { ; Call memcpy for "big" byvals. ; CHECK-LABEL: big_byval: -; CHECK: get_global $push[[L2:.+]]=, 0{{$}} +; CHECK: get_global $push[[L2:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 131072 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L2]], $pop[[L3]] ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}} -; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}} +; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}} ; CHECK-NEXT: i32.const $push[[L0:.+]]=, 131072 ; CHECK-NEXT: i32.call $push[[L11:.+]]=, memcpy@FUNCTION, $[[SP]], ${{.+}}, $pop{{.+}} ; CHECK-NEXT: tee_local $push[[L9:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}} diff --git a/test/CodeGen/WebAssembly/reg-stackify.ll b/test/CodeGen/WebAssembly/reg-stackify.ll index d1423b5db395..ebda5373c600 100644 --- a/test/CodeGen/WebAssembly/reg-stackify.ll +++ b/test/CodeGen/WebAssembly/reg-stackify.ll @@ -448,7 +448,7 @@ bb10: ; preds = %bb9, %bb ; CHECK-LABEL: stackpointer_dependency: ; CHECK: call {{.+}}, stackpointer_callee@FUNCTION, -; CHECK-NEXT: set_global 0, +; CHECK-NEXT: set_global __stack_pointer, declare i32 @stackpointer_callee(i8* readnone, i8* readnone) declare i8* @llvm.frameaddress(i32) define i32 @stackpointer_dependency(i8* readnone) { diff --git a/test/CodeGen/WebAssembly/stack-alignment.ll b/test/CodeGen/WebAssembly/stack-alignment.ll index 95aa1f9dbf07..25e9d06db411 100644 --- a/test/CodeGen/WebAssembly/stack-alignment.ll +++ b/test/CodeGen/WebAssembly/stack-alignment.ll @@ -6,7 +6,7 @@ target triple = "wasm32-unknown-unknown-wasm" declare void @somefunc(i32*) ; CHECK-LABEL: underalign: -; CHECK: get_global $push[[L1:.+]]=, 0{{$}} +; CHECK: get_global $push[[L1:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: i32.const $push[[L2:.+]]=, 16 ; CHECK-NEXT: i32.sub $push[[L10:.+]]=, $pop[[L1]], $pop[[L2]] ; CHECK-NEXT: tee_local $push{{.+}}=, [[SP:.+]], $pop[[L10]] @@ -17,7 +17,7 @@ declare void @somefunc(i32*) ; CHECK: get_local $push[[M4:.+]]=, [[SP]]{{$}} ; CHECK: i32.add $push[[L5:.+]]=, $pop[[M4]], $pop{{.+}} -; CHECK-NEXT: set_global 0, $pop[[L5]] +; CHECK-NEXT: set_global __stack_pointer, $pop[[L5]] define void @underalign() { entry: %underaligned = alloca i32, align 8 @@ -26,7 +26,7 @@ entry: } ; CHECK-LABEL: overalign: -; CHECK: get_global $push[[L10:.+]]=, 0{{$}} +; CHECK: get_global $push[[L10:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L9:.+]]=, [[BP:.+]], $pop[[L10]] ; CHECK-NEXT: i32.const $push[[L2:.+]]=, 32 ; CHECK-NEXT: i32.sub $push[[L8:.+]]=, $pop[[L9]], $pop[[L2]] @@ -38,7 +38,7 @@ entry: ; CHECK: call somefunc@FUNCTION, $pop[[M5]]{{$}} ; CHECK: get_local $push[[M6:.+]]=, [[BP]]{{$}} -; CHECK-NEXT: set_global 0, $pop[[M6]] +; CHECK-NEXT: set_global __stack_pointer, $pop[[M6]] define void @overalign() { entry: %overaligned = alloca i32, align 32 @@ -47,7 +47,7 @@ entry: } ; CHECK-LABEL: over_and_normal_align: -; CHECK: get_global $push[[L14:.+]]=, 0{{$}} +; CHECK: get_global $push[[L14:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L13:.+]]=, [[BP:.+]], $pop[[L14]] ; CHECK: i32.sub $push[[L12:.+]]=, $pop[[L13]], $pop{{.+}} ; CHECK: i32.and $push[[L11:.+]]=, $pop[[L12]], $pop{{.+}} @@ -61,7 +61,7 @@ entry: ; CHECK-NEXT: call somefunc@FUNCTION, $pop[[L8]] ; CHECK: get_local $push[[L6:.+]]=, [[BP]]{{$}} -; CHECK-NEXT: set_global 0, $pop[[L6]] +; CHECK-NEXT: set_global __stack_pointer, $pop[[L6]] define void @over_and_normal_align() { entry: %over = alloca i32, align 32 @@ -72,7 +72,7 @@ entry: } ; CHECK-LABEL: dynamic_overalign: -; CHECK: get_global $push[[L18:.+]]=, 0{{$}} +; CHECK: get_global $push[[L18:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L17:.+]]=, [[SP:.+]], $pop[[L18]] ; CHECK-NEXT: set_local [[BP:.+]], $pop[[L17]] ; CHECK: tee_local $push{{.+}}=, [[SP_2:.+]], $pop{{.+}} @@ -81,7 +81,7 @@ entry: ; CHECK: call somefunc@FUNCTION, $pop[[M8]] ; CHECK: get_local $push[[M9:.+]]=, [[BP]]{{$}} -; CHECK-NEXT: set_global 0, $pop[[M9]] +; CHECK-NEXT: set_global __stack_pointer, $pop[[M9]] define void @dynamic_overalign(i32 %num) { entry: %dynamic = alloca i32, i32 %num, align 32 @@ -90,7 +90,7 @@ entry: } ; CHECK-LABEL: overalign_and_dynamic: -; CHECK: get_global $push[[L21:.+]]=, 0{{$}} +; CHECK: get_global $push[[L21:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L20:.+]]=, [[BP:.+]], $pop[[L21]] ; CHECK: i32.sub $push[[L19:.+]]=, $pop[[L20]], $pop{{.+}} ; CHECK: i32.and $push[[L18:.+]]=, $pop[[L19]], $pop{{.+}} @@ -105,7 +105,7 @@ entry: ; CHECK-NEXT: call somefunc@FUNCTION, $pop[[another]] ; CHECK: get_local $push[[M11:.+]]=, [[BP]]{{$}} -; CHECK-NEXT: set_global 0, $pop[[M11]] +; CHECK-NEXT: set_global __stack_pointer, $pop[[M11]] define void @overalign_and_dynamic(i32 %num) { entry: %over = alloca i32, align 32 @@ -116,7 +116,7 @@ entry: } ; CHECK-LABEL: overalign_static_and_dynamic: -; CHECK: get_global $push[[L26:.+]]=, 0{{$}} +; CHECK: get_global $push[[L26:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L25:.+]]=, [[BP:.+]], $pop[[L26]] ; CHECK: i32.sub $push[[L24:.+]]=, $pop[[L25]], $pop{{.+}} ; CHECK: i32.and $push[[L23:.+]]=, $pop[[L24]], $pop{{.+}} @@ -136,7 +136,7 @@ entry: ; CHECK-NEXT: call somefunc@FUNCTION, $pop[[static]] ; CHECK: get_local $push[[M14:.+]]=, [[BP]]{{$}} -; CHECK-NEXT: set_global 0, $pop[[M14]] +; CHECK-NEXT: set_global __stack_pointer, $pop[[M14]] define void @overalign_static_and_dynamic(i32 %num) { entry: %over = alloca i32, align 32 diff --git a/test/CodeGen/WebAssembly/userstack.ll b/test/CodeGen/WebAssembly/userstack.ll index 57ca75705e5e..c160b391f6e8 100644 --- a/test/CodeGen/WebAssembly/userstack.ll +++ b/test/CodeGen/WebAssembly/userstack.ll @@ -10,11 +10,11 @@ declare void @ext_func_i32(i32* %ptr) ; Check that there is an extra local for the stack pointer. ; CHECK: .local i32{{$}} define void @alloca32() noredzone { - ; CHECK-NEXT: get_global $push[[L2:.+]]=, 0{{$}} + ; CHECK-NEXT: get_global $push[[L2:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 16 ; CHECK-NEXT: i32.sub $push[[L9:.+]]=, $pop[[L2]], $pop[[L3]] ; CHECK-NEXT: tee_local $push[[L8:.+]]=, [[SP:.+]], $pop[[L9]]{{$}} - ; CHECK-NEXT: set_global 0, $pop[[L8]]{{$}} + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]{{$}} %retval = alloca i32 ; CHECK: get_local $push[[L4:.+]]=, [[SP]]{{$}} ; CHECK: i32.const $push[[L0:.+]]=, 0 @@ -23,14 +23,14 @@ define void @alloca32() noredzone { ; CHECK: get_local $push[[L6:.+]]=, [[SP]]{{$}} ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 16 ; CHECK-NEXT: i32.add $push[[L7:.+]]=, $pop[[L6]], $pop[[L5]] - ; CHECK-NEXT: set_global 0, $pop[[L7]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]] ret void } ; CHECK-LABEL: alloca3264: ; CHECK: .local i32{{$}} define void @alloca3264() { - ; CHECK: get_global $push[[L3:.+]]=, 0{{$}} + ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 16 ; CHECK-NEXT: i32.sub $push[[L6:.+]]=, $pop[[L3]], $pop[[L4]] ; CHECK-NEXT: tee_local $push[[L5:.+]]=, [[SP:.+]], $pop[[L6]] @@ -50,11 +50,11 @@ define void @alloca3264() { ; CHECK-LABEL: allocarray: ; CHECK: .local i32{{$}} define void @allocarray() { - ; CHECK-NEXT: get_global $push[[L4:.+]]=, 0{{$}} + ; CHECK-NEXT: get_global $push[[L4:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 144{{$}} ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, $pop[[L4]], $pop[[L5]] ; CHECK-NEXT: tee_local $push[[L11:.+]]=, 0, $pop[[L12]] - ; CHECK-NEXT: set_global 0, $pop[[L11]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]] %r = alloca [33 x i32] ; CHECK: i32.const $push{{.+}}=, 24 @@ -72,7 +72,7 @@ define void @allocarray() { ; CHECK-NEXT: get_local $push[[L2:.+]]=, [[SP]]{{$}} ; CHECK-NEXT: i32.const $push[[L7:.+]]=, 144 ; CHECK-NEXT: i32.add $push[[L8:.+]]=, $pop[[L2]], $pop[[L7]] - ; CHECK-NEXT: set_global 0, $pop[[L8]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]] ret void } @@ -81,7 +81,7 @@ define void @non_mem_use(i8** %addr) { ; CHECK: i32.const $push[[L2:.+]]=, 48 ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, {{.+}}, $pop[[L2]] ; CHECK-NEXT: tee_local $push[[L11:.+]]=, [[SP:.+]], $pop[[L12]] - ; CHECK-NEXT: set_global 0, $pop[[L11]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]] %buf = alloca [27 x i8], align 16 %r = alloca i64 %r2 = alloca i64 @@ -109,11 +109,11 @@ define void @non_mem_use(i8** %addr) { ; CHECK-LABEL: allocarray_inbounds: ; CHECK: .local i32{{$}} define void @allocarray_inbounds() { - ; CHECK: get_global $push[[L3:.+]]=, 0{{$}} + ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 32{{$}} ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L3]], $pop[[L4]] ; CHECK-NEXT: tee_local $push[[L10:.+]]=, [[SP:.+]], $pop[[L11]] - ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}} + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}} %r = alloca [5 x i32] ; CHECK: i32.const $push[[L3:.+]]=, 1 ; CHECK-DAG: i32.store 24(${{.+}}), $pop[[L3]] @@ -127,29 +127,29 @@ define void @allocarray_inbounds() { ; CHECK: call ext_func ; CHECK: i32.const $push[[L5:.+]]=, 32{{$}} ; CHECK-NEXT: i32.add $push[[L7:.+]]=, ${{.+}}, $pop[[L5]] - ; CHECK-NEXT: set_global 0, $pop[[L7]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]] ret void } ; CHECK-LABEL: dynamic_alloca: define void @dynamic_alloca(i32 %alloc) { - ; CHECK: get_global $push[[L13:.+]]=, 0{{$}} + ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}} ; Target independent codegen bumps the stack pointer. ; CHECK: i32.sub ; Check that SP is written back to memory after decrement - ; CHECK: set_global 0, + ; CHECK: set_global __stack_pointer, %r = alloca i32, i32 %alloc ; Target-independent codegen also calculates the store addr ; CHECK: call ext_func_i32@FUNCTION call void @ext_func_i32(i32* %r) - ; CHECK: set_global 0, $pop{{.+}} + ; CHECK: set_global __stack_pointer, $pop{{.+}} ret void } ; CHECK-LABEL: dynamic_alloca_redzone: define void @dynamic_alloca_redzone(i32 %alloc) { - ; CHECK: get_global $push[[L13:.+]]=, 0{{$}} + ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}} ; Target independent codegen bumps the stack pointer ; CHECK: i32.sub @@ -166,11 +166,11 @@ define void @dynamic_alloca_redzone(i32 %alloc) { ; CHECK-LABEL: dynamic_static_alloca: define void @dynamic_static_alloca(i32 %alloc) noredzone { ; Decrement SP in the prolog by the static amount and writeback to memory. - ; CHECK: get_global $push[[L11:.+]]=, 0{{$}} + ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: i32.const $push[[L12:.+]]=, 16 ; CHECK-NEXT: i32.sub $push[[L23:.+]]=, $pop[[L11]], $pop[[L12]] ; CHECK-NEXT: tee_local $push[[L22:.+]]=, [[SP:.+]], $pop[[L23]] - ; CHECK-NEXT: set_global 0, $pop[[L22]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L22]] ; Alloc and write to a static alloca ; CHECK: get_local $push[[L21:.+]]=, [[SP:.+]] @@ -184,7 +184,7 @@ define void @dynamic_static_alloca(i32 %alloc) noredzone { ; CHECK: i32.sub ; CHECK: tee_local $push[[L16:.+]]=, [[dynamic_local:.+]], $pop{{.+}} ; CHECK: tee_local $push[[L15:.+]]=, [[other:.+]], $pop[[L16]]{{$}} - ; CHECK: set_global 0, $pop[[L15]]{{$}} + ; CHECK: set_global __stack_pointer, $pop[[L15]]{{$}} %dynamic = alloca i32, i32 %alloc ; Ensure we don't modify the frame pointer after assigning it. @@ -226,7 +226,7 @@ define void @dynamic_static_alloca(i32 %alloc) noredzone { ; CHECK: get_local $push[[L24:.+]]=, [[FP]]{{$}} ; CHECK: i32.const $push[[L18:.+]]=, 16 ; CHECK-NEXT: i32.add $push[[L19:.+]]=, $pop[[L24]], $pop[[L18]] - ; CHECK-NEXT: set_global 0, $pop[[L19]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L19]] ret void } @@ -235,7 +235,7 @@ declare void @llvm.stackrestore(i8*) ; CHECK-LABEL: llvm_stack_builtins: define void @llvm_stack_builtins(i32 %alloc) noredzone { - ; CHECK: get_global $push[[L11:.+]]=, 0{{$}} + ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]] ; CHECK-NEXT: set_local [[STACK:.+]], $pop[[L10]] %stack = call i8* @llvm.stacksave() @@ -245,7 +245,7 @@ define void @llvm_stack_builtins(i32 %alloc) noredzone { %dynamic = alloca i32, i32 %alloc ; CHECK: get_local $push[[L12:.+]]=, [[STACK]] - ; CHECK-NEXT: set_global 0, $pop[[L12]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]] call void @llvm.stackrestore(i8* %stack) ret void @@ -256,7 +256,7 @@ define void @llvm_stack_builtins(i32 %alloc) noredzone { ; moved after the stack pointer was updated for the dynamic alloca. ; CHECK-LABEL: dynamic_alloca_nouse: define void @dynamic_alloca_nouse(i32 %alloc) noredzone { - ; CHECK: get_global $push[[L11:.+]]=, 0{{$}} + ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]] ; CHECK-NEXT: set_local [[FP:.+]], $pop[[L10]] %dynamic = alloca i32, i32 %alloc @@ -264,7 +264,7 @@ define void @dynamic_alloca_nouse(i32 %alloc) noredzone { ; CHECK-NOT: set_local [[FP]], ; CHECK: get_local $push[[L12:.+]]=, [[FP]] - ; CHECK-NEXT: set_global 0, $pop[[L12]] + ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]] ret void } @@ -295,11 +295,11 @@ declare i8* @llvm.frameaddress(i32) ; Test __builtin_frame_address(0). ; CHECK-LABEL: frameaddress_0: -; CHECK: get_global $push[[L3:.+]]=, 0{{$}} +; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}} ; CHECK-NEXT: tee_local $push[[L2:.+]]=, [[FP:.+]], $pop[[L3]]{{$}} ; CHECK-NEXT: call use_i8_star@FUNCTION, $pop[[L2]] ; CHECK-NEXT: get_local $push[[L5:.+]]=, [[FP]] -; CHECK-NEXT: set_global 0, $pop[[L5]] +; CHECK-NEXT: set_global __stack_pointer, $pop[[L5]] define void @frameaddress_0() { %t = call i8* @llvm.frameaddress(i32 0) call void @use_i8_star(i8* %t) @@ -320,7 +320,7 @@ define void @frameaddress_1() { ; Test a stack address passed to an inline asm. ; CHECK-LABEL: inline_asm: -; CHECK: get_global {{.+}}, 0{{$}} +; CHECK: get_global {{.+}}, __stack_pointer{{$}} ; CHECK: #APP ; CHECK-NEXT: # %{{[0-9]+}}{{$}} ; CHECK-NEXT: #NO_APP diff --git a/test/MC/WebAssembly/func-address.ll b/test/MC/WebAssembly/func-address.ll index c0a9d9a801ff..15c09e0ebd80 100644 --- a/test/MC/WebAssembly/func-address.ll +++ b/test/MC/WebAssembly/func-address.ll @@ -25,11 +25,10 @@ entry: ; CHECK: Section { ; CHECK: Type: ELEM (0x9) ; CHECK: Size: 7 -; CHECK: Offset: 165 ; CHECK: } ; CHECK: Relocations [ -; CHECK: Section (9) CODE { +; CHECK: Section (8) CODE { ; CHECK: Relocation { ; CHECK: Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB (0) ; CHECK: Offset: 0x4 @@ -42,7 +41,7 @@ entry: ; CHECK: } ; CHECK: Relocation { ; CHECK: Type: R_WEBASSEMBLY_TABLE_INDEX_SLEB (1) -; CHECK: Offset: 0x1A +; CHECK: Offset: 0x1E ; CHECK: Index: 0x0 ; CHECK: } ; CHECK: } diff --git a/test/MC/WebAssembly/stack-ptr.ll b/test/MC/WebAssembly/stack-ptr.ll new file mode 100644 index 000000000000..98d1311e154c --- /dev/null +++ b/test/MC/WebAssembly/stack-ptr.ll @@ -0,0 +1,21 @@ +; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | obj2yaml | FileCheck %s + +; Function that uses explict stack, and should generate a reference to +; __stack_pointer, along with the corresponding reloction entry. +define hidden void @foo() #0 { +entry: + alloca i32, align 4 + ret void +} + +; CHECK: - Type: IMPORT +; CHECK: Imports: +; CHECK: - Module: env +; CHECK: Field: __stack_pointer +; CHECK: Kind: GLOBAL +; CHECK: GlobalType: I32 +; CHECK: GlobalMutable: false +; CHECK: - Type: CODE +; CHECK: Relocations: +; CHECK: - Type: R_WEBASSEMBLY_GLOBAL_INDEX_LEB +; CHECK: Index: 0