llvm.org GIT mirror llvm / 46016f2
[WebAssembly] Use __stack_pointer global when writing wasm binary This ensures that symbolic relocations are generated for stack pointer manipulations. These relocations are of type R_WEBASSEMBLY_GLOBAL_INDEX_LEB. This change also adds support for reading relocations of this type in WasmObjectFile.cpp. Since its a globally imported symbol this does mean that the get_global/set_global instruction won't be valid until the objects are linked that global used in no longer an imported global. Differential Revision: https://reviews.llvm.org/D34172 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@305616 91177308-0d34-0410-b5e6-96231b3b80d8 Sam Clegg 3 years ago
17 changed file(s) with 106 addition(s) and 145 deletion(s). Raw diff Collapse all Expand all
7676 SymbolListTy GetGVStubList() { return getSortedStubs(GVStubs); }
7777 };
7878
79 /// MachineModuleInfoWasm - This is a MachineModuleInfoImpl implementation
80 /// for Wasm targets.
81 class MachineModuleInfoWasm : public MachineModuleInfoImpl {
82 /// WebAssembly global variables defined by CodeGen.
83 std::vector Globals;
84
85 /// The WebAssembly global variable which is the stack pointer.
86 unsigned StackPointerGlobal;
87
88 virtual void anchor(); // Out of line virtual method.
89 public:
90 MachineModuleInfoWasm(const MachineModuleInfo &)
91 : StackPointerGlobal(-1U) {}
92
93 void addGlobal(const wasm::Global &G) { Globals.push_back(G); }
94 const std::vector &getGlobals() const { return Globals; }
95
96 bool hasStackPointerGlobal() const {
97 return StackPointerGlobal != -1U;
98 }
99 unsigned getStackPointerGlobal() const {
100 assert(hasStackPointerGlobal() && "Stack ptr global hasn't been set");
101 return StackPointerGlobal;
102 }
103 void setStackPointerGlobal(unsigned Global) { StackPointerGlobal = Global; }
104 };
105
10679 } // end namespace llvm
10780
10881 #endif
2222 // Out of line virtual method.
2323 void MachineModuleInfoMachO::anchor() {}
2424 void MachineModuleInfoELF::anchor() {}
25 void MachineModuleInfoWasm::anchor() {}
2625
2726 static int SortSymbolPair(const void *LHS, const void *RHS) {
2827 typedef std::pair PairTy;
405405 }
406406
407407 assert(!IsPCRel);
408 assert(SymA);
409
408410 unsigned Type = getRelocType(Target, Fixup);
409411
410412 WasmRelocationEntry Rec(FixupOffset, SymA, C, Type, &FixupSection);
473475 assert(IndirectSymbolIndices.count(RelEntry.Symbol));
474476 return IndirectSymbolIndices[RelEntry.Symbol];
475477 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
478 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
476479 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
477480 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
478481 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_I32:
499502 switch (RelEntry.Type) {
500503 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
501504 case wasm::R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
502 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB: {
505 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
506 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB: {
503507 uint32_t Index = getRelocationIndexValue(RelEntry);
504508 WritePatchableSLEB(Stream, Index, Offset);
505509 break;
525529 break;
526530 }
527531 default:
528 llvm_unreachable("unsupported relocation type");
532 llvm_unreachable("invalid relocation type");
529533 }
530534 }
531535 }
324324 case wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB:
325325 case wasm::R_WEBASSEMBLY_TABLE_INDEX_I32:
326326 case wasm::R_WEBASSEMBLY_TYPE_INDEX_LEB:
327 case wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
327328 break;
328329 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_LEB:
329330 case wasm::R_WEBASSEMBLY_GLOBAL_ADDR_SLEB:
331332 Reloc.Addend = readVarint32(Ptr);
332333 break;
333334 default:
334 return make_error("Bad relocation type",
335 return make_error("Bad relocation type: " +
336 Twine(Reloc.Type),
335337 object_error::parse_failed);
336338 }
337339 Section->Relocations.push_back(Reloc);
3535 namespace {
3636 class WebAssemblyMCCodeEmitter final : public MCCodeEmitter {
3737 const MCInstrInfo &MCII;
38 MCContext &Ctx;
3938
4039 // Implementation generated by tablegen.
4140 uint64_t getBinaryCodeForInstr(const MCInst &MI,
4746 const MCSubtargetInfo &STI) const override;
4847
4948 public:
50 WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
51 : MCII(mcii), Ctx(ctx) {}
49 WebAssemblyMCCodeEmitter(const MCInstrInfo &mcii) : MCII(mcii) {}
5250 };
5351 } // end anonymous namespace
5452
55 MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
56 MCContext &Ctx) {
57 return new WebAssemblyMCCodeEmitter(MCII, Ctx);
53 MCCodeEmitter *llvm::createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII) {
54 return new WebAssemblyMCCodeEmitter(MCII);
5855 }
5956
6057 void WebAssemblyMCCodeEmitter::encodeInstruction(
8885 } else if (Info.OperandType == WebAssembly::OPERAND_I64IMM) {
8986 encodeSLEB128(int64_t(MO.getImm()), OS);
9087 } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
91 Fixups.push_back(MCFixup::create(
92 OS.tell() - Start, MCConstantExpr::create(MO.getImm(), Ctx),
93 MCFixupKind(WebAssembly::fixup_code_global_index), MI.getLoc()));
94 ++MCNumFixups;
95 encodeULEB128(uint64_t(MO.getImm()), OS);
88 llvm_unreachable("wasm globals should only be accessed symbolicly");
9689 } else if (Info.OperandType == WebAssembly::OPERAND_SIGNATURE) {
9790 encodeSLEB128(int64_t(MO.getImm()), OS);
9891 } else {
134127 Info.OperandType == WebAssembly::OPERAND_TYPEINDEX) {
135128 FixupKind = MCFixupKind(WebAssembly::fixup_code_uleb128_i32);
136129 PaddedSize = 5;
130 } else if (Info.OperandType == WebAssembly::OPERAND_GLOBAL) {
131 FixupKind = MCFixupKind(WebAssembly::fixup_code_global_index);
132 PaddedSize = 5;
137133 } else {
138134 llvm_unreachable("unexpected symbolic operand kind");
139135 }
7373 static MCCodeEmitter *createCodeEmitter(const MCInstrInfo &MCII,
7474 const MCRegisterInfo & /*MRI*/,
7575 MCContext &Ctx) {
76 return createWebAssemblyMCCodeEmitter(MCII, Ctx);
76 return createWebAssemblyMCCodeEmitter(MCII);
7777 }
7878
7979 static MCAsmBackend *createAsmBackend(const Target & /*T*/,
3434 Target &getTheWebAssemblyTarget32();
3535 Target &getTheWebAssemblyTarget64();
3636
37 MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII,
38 MCContext &Ctx);
37 MCCodeEmitter *createWebAssemblyMCCodeEmitter(const MCInstrInfo &MCII);
3938
4039 MCAsmBackend *createWebAssemblyAsmBackend(const Triple &TT);
4140
6767 bool IsFunction = IsFunctionExpr(Fixup.getValue());
6868
6969 switch (unsigned(Fixup.getKind())) {
70 case WebAssembly::fixup_code_global_index:
71 return wasm::R_WEBASSEMBLY_GLOBAL_INDEX_LEB;
7072 case WebAssembly::fixup_code_sleb128_i32:
7173 if (IsFunction)
7274 return wasm::R_WEBASSEMBLY_TABLE_INDEX_SLEB;
9595 MCConstantExpr::create(Size, OutContext));
9696 }
9797 }
98
99 if (!TM.getTargetTriple().isOSBinFormatELF()) {
100 MachineModuleInfoWasm &MMIW = MMI->getObjFileInfo();
101 getTargetStreamer()->emitGlobal(MMIW.getGlobals());
102 if (MMIW.hasStackPointerGlobal())
103 getTargetStreamer()->emitStackPointer(MMIW.getStackPointerGlobal());
104 }
10598 }
10699
107100 void WebAssemblyAsmPrinter::EmitConstantPool() {
103103 const DebugLoc &DL) {
104104 const auto *TII = MF.getSubtarget().getInstrInfo();
105105
106 const char *ES = "__stack_pointer";
107 auto *SPSymbol = MF.createExternalSymbolName(ES);
106108 if (MF.getSubtarget()
107109 .getTargetTriple().isOSBinFormatELF()) {
108 const char *ES = "__stack_pointer";
109 auto *SPSymbol = MF.createExternalSymbolName(ES);
110110 MachineRegisterInfo &MRI = MF.getRegInfo();
111111 const TargetRegisterClass *PtrRC =
112112 MRI.getTargetRegisterInfo()->getPointerRegClass(MF);
124124 .addReg(SrcReg)
125125 .addMemOperand(MMO);
126126 } else {
127 MachineModuleInfoWasm &MMIW =
128 MF.getMMI().getObjFileInfo();
129127 BuildMI(MBB, InsertStore, DL, TII->get(WebAssembly::SET_GLOBAL_I32))
130 .addImm(MMIW.getStackPointerGlobal())
128 .addExternalSymbol(SPSymbol)
131129 .addReg(SrcReg);
132130 }
133131 }
170168 unsigned SPReg = WebAssembly::SP32;
171169 if (StackSize)
172170 SPReg = MRI.createVirtualRegister(PtrRC);
171
172 const char *ES = "__stack_pointer";
173 auto *SPSymbol = MF.createExternalSymbolName(ES);
173174 if (MF.getSubtarget()
174175 .getTargetTriple().isOSBinFormatELF()) {
175 const char *ES = "__stack_pointer";
176 auto *SPSymbol = MF.createExternalSymbolName(ES);
177176 unsigned Zero = MRI.createVirtualRegister(PtrRC);
178177
179178 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::CONST_I32), Zero)
188187 .addReg(Zero) // addr
189188 .addMemOperand(LoadMMO);
190189 } else {
191 auto &MMIW = MF.getMMI().getObjFileInfo();
192 if (!MMIW.hasStackPointerGlobal()) {
193 MMIW.setStackPointerGlobal(MMIW.getGlobals().size());
194
195 // Create the stack-pointer global. For now, just use the
196 // Emscripten/Binaryen ABI names.
197 wasm::Global G;
198 G.Type = wasm::ValType::I32;
199 G.Mutable = true;
200 G.InitialValue = 0;
201 G.InitialModule = "env";
202 G.InitialName = "STACKTOP";
203 MMIW.addGlobal(G);
204 }
205190 BuildMI(MBB, InsertPt, DL, TII->get(WebAssembly::GET_GLOBAL_I32), SPReg)
206 .addImm(MMIW.getStackPointerGlobal());
191 .addExternalSymbol(SPSymbol);
207192 }
208193
209194 bool HasBP = hasBP(MF);
169169 if (MI.mayStore()) {
170170 Write = true;
171171
172 const MachineFunction &MF = *MI.getParent()->getParent();
173 if (MF.getSubtarget()
174 .getTargetTriple().isOSBinFormatELF()) {
175 // Check for stores to __stack_pointer.
176 for (auto MMO : MI.memoperands()) {
177 const MachinePointerInfo &MPI = MMO->getPointerInfo();
178 if (MPI.V.is()) {
179 auto PSV = MPI.V.get();
180 if (const ExternalSymbolPseudoSourceValue *EPSV =
181 dyn_cast(PSV))
182 if (StringRef(EPSV->getSymbol()) == "__stack_pointer")
183 StackPointer = true;
184 }
185 }
186 } else {
187 // Check for sets of the stack pointer.
188 const MachineModuleInfoWasm &MMIW =
189 MF.getMMI().getObjFileInfo();
190 if ((MI.getOpcode() == WebAssembly::SET_LOCAL_I32 ||
191 MI.getOpcode() == WebAssembly::SET_LOCAL_I64) &&
192 MI.getOperand(0).getImm() == MMIW.getStackPointerGlobal()) {
193 StackPointer = true;
172 // Check for stores to __stack_pointer.
173 for (auto MMO : MI.memoperands()) {
174 const MachinePointerInfo &MPI = MMO->getPointerInfo();
175 if (MPI.V.is()) {
176 auto PSV = MPI.V.get();
177 if (const ExternalSymbolPseudoSourceValue *EPSV =
178 dyn_cast(PSV))
179 if (StringRef(EPSV->getSymbol()) == "__stack_pointer") {
180 StackPointer = true;
181 }
194182 }
195183 }
196184 } else if (MI.hasOrderedMemoryRef()) {
2323 define void @byval_arg(%SmallStruct* %ptr) {
2424 ; CHECK: .param i32
2525 ; Subtract 16 from SP (SP is 16-byte aligned)
26 ; CHECK-NEXT: get_global $push[[L2:.+]]=, 0
26 ; CHECK-NEXT: get_global $push[[L2:.+]]=, __stack_pointer
2727 ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 16
2828 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L2]], $pop[[L3]]
2929 ; Ensure SP is stored back before the call
3030 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
31 ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
31 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
3232 ; Copy the SmallStruct argument to the stack (SP+12, original SP-4)
3333 ; CHECK-NEXT: i32.load $push[[L0:.+]]=, 0($0)
3434 ; CHECK-NEXT: i32.store 12($[[SP]]), $pop[[L0]]
4040 ; Restore the stack
4141 ; CHECK-NEXT: i32.const $push[[L6:.+]]=, 16
4242 ; CHECK-NEXT: i32.add $push[[L8:.+]]=, $[[SP]], $pop[[L6]]
43 ; CHECK-NEXT: set_global 0, $pop[[L8]]
43 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]
4444 ; CHECK-NEXT: return
4545 ret void
4646 }
5252 ; CHECK: i32.const $push[[L1:.+]]=, 16
5353 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, {{.+}}, $pop[[L1]]
5454 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
55 ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
55 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
5656 ; Copy the SmallStruct argument to the stack (SP+8, original SP-8)
5757 ; CHECK-NEXT: i32.load $push[[L0:.+]]=, 0($0){{$}}
5858 ; CHECK-NEXT: i32.store 8($[[SP]]), $pop[[L0]]{{$}}
7171 ; CHECK: i32.const $push[[L1:.+]]=, 16
7272 ; CHECK-NEXT: i32.sub $push[[L14:.+]]=, {{.+}}, $pop[[L1]]
7373 ; CHECK-NEXT: tee_local $push[[L13:.+]]=, $[[SP:.+]]=, $pop[[L14]]
74 ; CHECK-NEXT: set_global 0, $pop[[L13]]
74 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L13]]
7575 ; Copy the AlignedStruct argument to the stack (SP+0, original SP-16)
7676 ; Just check the last load/store pair of the memcpy
7777 ; CHECK: i64.load $push[[L4:.+]]=, 0($0)
109109
110110 ; Call memcpy for "big" byvals.
111111 ; CHECK-LABEL: big_byval:
112 ; CHECK: get_global $push[[L2:.+]]=, 0{{$}}
112 ; CHECK: get_global $push[[L2:.+]]=, __stack_pointer{{$}}
113113 ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 131072
114114 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L2]], $pop[[L3]]
115115 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
116 ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
116 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
117117 ; CHECK-NEXT: i32.const $push[[L0:.+]]=, 131072
118118 ; CHECK-NEXT: i32.call $push[[L11:.+]]=, memcpy@FUNCTION, $[[SP]], ${{.+}}, $pop{{.+}}
119119 ; CHECK-NEXT: tee_local $push[[L9:.+]]=, $[[SP:.+]]=, $pop[[L11]]{{$}}
447447
448448 ; CHECK-LABEL: stackpointer_dependency:
449449 ; CHECK: call {{.+}}, stackpointer_callee@FUNCTION,
450 ; CHECK-NEXT: set_global 0,
450 ; CHECK-NEXT: set_global __stack_pointer,
451451 declare i32 @stackpointer_callee(i8* readnone, i8* readnone)
452452 declare i8* @llvm.frameaddress(i32)
453453 define i32 @stackpointer_dependency(i8* readnone) {
55 declare void @somefunc(i32*)
66
77 ; CHECK-LABEL: underalign:
8 ; CHECK: get_global $push[[L1:.+]]=, 0{{$}}
8 ; CHECK: get_global $push[[L1:.+]]=, __stack_pointer{{$}}
99 ; CHECK-NEXT: i32.const $push[[L2:.+]]=, 16
1010 ; CHECK-NEXT: i32.sub $push[[L10:.+]]=, $pop[[L1]], $pop[[L2]]
1111 ; CHECK-NEXT: tee_local $push{{.+}}=, [[SP:.+]], $pop[[L10]]
1616
1717 ; CHECK: get_local $push[[M4:.+]]=, [[SP]]{{$}}
1818 ; CHECK: i32.add $push[[L5:.+]]=, $pop[[M4]], $pop{{.+}}
19 ; CHECK-NEXT: set_global 0, $pop[[L5]]
19 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L5]]
2020 define void @underalign() {
2121 entry:
2222 %underaligned = alloca i32, align 8
2525 }
2626
2727 ; CHECK-LABEL: overalign:
28 ; CHECK: get_global $push[[L10:.+]]=, 0{{$}}
28 ; CHECK: get_global $push[[L10:.+]]=, __stack_pointer{{$}}
2929 ; CHECK-NEXT: tee_local $push[[L9:.+]]=, [[BP:.+]], $pop[[L10]]
3030 ; CHECK-NEXT: i32.const $push[[L2:.+]]=, 32
3131 ; CHECK-NEXT: i32.sub $push[[L8:.+]]=, $pop[[L9]], $pop[[L2]]
3737 ; CHECK: call somefunc@FUNCTION, $pop[[M5]]{{$}}
3838
3939 ; CHECK: get_local $push[[M6:.+]]=, [[BP]]{{$}}
40 ; CHECK-NEXT: set_global 0, $pop[[M6]]
40 ; CHECK-NEXT: set_global __stack_pointer, $pop[[M6]]
4141 define void @overalign() {
4242 entry:
4343 %overaligned = alloca i32, align 32
4646 }
4747
4848 ; CHECK-LABEL: over_and_normal_align:
49 ; CHECK: get_global $push[[L14:.+]]=, 0{{$}}
49 ; CHECK: get_global $push[[L14:.+]]=, __stack_pointer{{$}}
5050 ; CHECK-NEXT: tee_local $push[[L13:.+]]=, [[BP:.+]], $pop[[L14]]
5151 ; CHECK: i32.sub $push[[L12:.+]]=, $pop[[L13]], $pop{{.+}}
5252 ; CHECK: i32.and $push[[L11:.+]]=, $pop[[L12]], $pop{{.+}}
6060 ; CHECK-NEXT: call somefunc@FUNCTION, $pop[[L8]]
6161
6262 ; CHECK: get_local $push[[L6:.+]]=, [[BP]]{{$}}
63 ; CHECK-NEXT: set_global 0, $pop[[L6]]
63 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L6]]
6464 define void @over_and_normal_align() {
6565 entry:
6666 %over = alloca i32, align 32
7171 }
7272
7373 ; CHECK-LABEL: dynamic_overalign:
74 ; CHECK: get_global $push[[L18:.+]]=, 0{{$}}
74 ; CHECK: get_global $push[[L18:.+]]=, __stack_pointer{{$}}
7575 ; CHECK-NEXT: tee_local $push[[L17:.+]]=, [[SP:.+]], $pop[[L18]]
7676 ; CHECK-NEXT: set_local [[BP:.+]], $pop[[L17]]
7777 ; CHECK: tee_local $push{{.+}}=, [[SP_2:.+]], $pop{{.+}}
8080 ; CHECK: call somefunc@FUNCTION, $pop[[M8]]
8181
8282 ; CHECK: get_local $push[[M9:.+]]=, [[BP]]{{$}}
83 ; CHECK-NEXT: set_global 0, $pop[[M9]]
83 ; CHECK-NEXT: set_global __stack_pointer, $pop[[M9]]
8484 define void @dynamic_overalign(i32 %num) {
8585 entry:
8686 %dynamic = alloca i32, i32 %num, align 32
8989 }
9090
9191 ; CHECK-LABEL: overalign_and_dynamic:
92 ; CHECK: get_global $push[[L21:.+]]=, 0{{$}}
92 ; CHECK: get_global $push[[L21:.+]]=, __stack_pointer{{$}}
9393 ; CHECK-NEXT: tee_local $push[[L20:.+]]=, [[BP:.+]], $pop[[L21]]
9494 ; CHECK: i32.sub $push[[L19:.+]]=, $pop[[L20]], $pop{{.+}}
9595 ; CHECK: i32.and $push[[L18:.+]]=, $pop[[L19]], $pop{{.+}}
104104 ; CHECK-NEXT: call somefunc@FUNCTION, $pop[[another]]
105105
106106 ; CHECK: get_local $push[[M11:.+]]=, [[BP]]{{$}}
107 ; CHECK-NEXT: set_global 0, $pop[[M11]]
107 ; CHECK-NEXT: set_global __stack_pointer, $pop[[M11]]
108108 define void @overalign_and_dynamic(i32 %num) {
109109 entry:
110110 %over = alloca i32, align 32
115115 }
116116
117117 ; CHECK-LABEL: overalign_static_and_dynamic:
118 ; CHECK: get_global $push[[L26:.+]]=, 0{{$}}
118 ; CHECK: get_global $push[[L26:.+]]=, __stack_pointer{{$}}
119119 ; CHECK-NEXT: tee_local $push[[L25:.+]]=, [[BP:.+]], $pop[[L26]]
120120 ; CHECK: i32.sub $push[[L24:.+]]=, $pop[[L25]], $pop{{.+}}
121121 ; CHECK: i32.and $push[[L23:.+]]=, $pop[[L24]], $pop{{.+}}
135135 ; CHECK-NEXT: call somefunc@FUNCTION, $pop[[static]]
136136
137137 ; CHECK: get_local $push[[M14:.+]]=, [[BP]]{{$}}
138 ; CHECK-NEXT: set_global 0, $pop[[M14]]
138 ; CHECK-NEXT: set_global __stack_pointer, $pop[[M14]]
139139 define void @overalign_static_and_dynamic(i32 %num) {
140140 entry:
141141 %over = alloca i32, align 32
99 ; Check that there is an extra local for the stack pointer.
1010 ; CHECK: .local i32{{$}}
1111 define void @alloca32() noredzone {
12 ; CHECK-NEXT: get_global $push[[L2:.+]]=, 0{{$}}
12 ; CHECK-NEXT: get_global $push[[L2:.+]]=, __stack_pointer{{$}}
1313 ; CHECK-NEXT: i32.const $push[[L3:.+]]=, 16
1414 ; CHECK-NEXT: i32.sub $push[[L9:.+]]=, $pop[[L2]], $pop[[L3]]
1515 ; CHECK-NEXT: tee_local $push[[L8:.+]]=, [[SP:.+]], $pop[[L9]]{{$}}
16 ; CHECK-NEXT: set_global 0, $pop[[L8]]{{$}}
16 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]{{$}}
1717 %retval = alloca i32
1818 ; CHECK: get_local $push[[L4:.+]]=, [[SP]]{{$}}
1919 ; CHECK: i32.const $push[[L0:.+]]=, 0
2222 ; CHECK: get_local $push[[L6:.+]]=, [[SP]]{{$}}
2323 ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 16
2424 ; CHECK-NEXT: i32.add $push[[L7:.+]]=, $pop[[L6]], $pop[[L5]]
25 ; CHECK-NEXT: set_global 0, $pop[[L7]]
25 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]]
2626 ret void
2727 }
2828
2929 ; CHECK-LABEL: alloca3264:
3030 ; CHECK: .local i32{{$}}
3131 define void @alloca3264() {
32 ; CHECK: get_global $push[[L3:.+]]=, 0{{$}}
32 ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
3333 ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 16
3434 ; CHECK-NEXT: i32.sub $push[[L6:.+]]=, $pop[[L3]], $pop[[L4]]
3535 ; CHECK-NEXT: tee_local $push[[L5:.+]]=, [[SP:.+]], $pop[[L6]]
4949 ; CHECK-LABEL: allocarray:
5050 ; CHECK: .local i32{{$}}
5151 define void @allocarray() {
52 ; CHECK-NEXT: get_global $push[[L4:.+]]=, 0{{$}}
52 ; CHECK-NEXT: get_global $push[[L4:.+]]=, __stack_pointer{{$}}
5353 ; CHECK-NEXT: i32.const $push[[L5:.+]]=, 144{{$}}
5454 ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, $pop[[L4]], $pop[[L5]]
5555 ; CHECK-NEXT: tee_local $push[[L11:.+]]=, 0, $pop[[L12]]
56 ; CHECK-NEXT: set_global 0, $pop[[L11]]
56 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]]
5757 %r = alloca [33 x i32]
5858
5959 ; CHECK: i32.const $push{{.+}}=, 24
7171 ; CHECK-NEXT: get_local $push[[L2:.+]]=, [[SP]]{{$}}
7272 ; CHECK-NEXT: i32.const $push[[L7:.+]]=, 144
7373 ; CHECK-NEXT: i32.add $push[[L8:.+]]=, $pop[[L2]], $pop[[L7]]
74 ; CHECK-NEXT: set_global 0, $pop[[L8]]
74 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L8]]
7575 ret void
7676 }
7777
8080 ; CHECK: i32.const $push[[L2:.+]]=, 48
8181 ; CHECK-NEXT: i32.sub $push[[L12:.+]]=, {{.+}}, $pop[[L2]]
8282 ; CHECK-NEXT: tee_local $push[[L11:.+]]=, [[SP:.+]], $pop[[L12]]
83 ; CHECK-NEXT: set_global 0, $pop[[L11]]
83 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L11]]
8484 %buf = alloca [27 x i8], align 16
8585 %r = alloca i64
8686 %r2 = alloca i64
108108 ; CHECK-LABEL: allocarray_inbounds:
109109 ; CHECK: .local i32{{$}}
110110 define void @allocarray_inbounds() {
111 ; CHECK: get_global $push[[L3:.+]]=, 0{{$}}
111 ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
112112 ; CHECK-NEXT: i32.const $push[[L4:.+]]=, 32{{$}}
113113 ; CHECK-NEXT: i32.sub $push[[L11:.+]]=, $pop[[L3]], $pop[[L4]]
114114 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, [[SP:.+]], $pop[[L11]]
115 ; CHECK-NEXT: set_global 0, $pop[[L10]]{{$}}
115 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L10]]{{$}}
116116 %r = alloca [5 x i32]
117117 ; CHECK: i32.const $push[[L3:.+]]=, 1
118118 ; CHECK-DAG: i32.store 24(${{.+}}), $pop[[L3]]
126126 ; CHECK: call ext_func
127127 ; CHECK: i32.const $push[[L5:.+]]=, 32{{$}}
128128 ; CHECK-NEXT: i32.add $push[[L7:.+]]=, ${{.+}}, $pop[[L5]]
129 ; CHECK-NEXT: set_global 0, $pop[[L7]]
129 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L7]]
130130 ret void
131131 }
132132
133133 ; CHECK-LABEL: dynamic_alloca:
134134 define void @dynamic_alloca(i32 %alloc) {
135 ; CHECK: get_global $push[[L13:.+]]=, 0{{$}}
135 ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}}
136136 ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}}
137137 ; Target independent codegen bumps the stack pointer.
138138 ; CHECK: i32.sub
139139 ; Check that SP is written back to memory after decrement
140 ; CHECK: set_global 0,
140 ; CHECK: set_global __stack_pointer,
141141 %r = alloca i32, i32 %alloc
142142 ; Target-independent codegen also calculates the store addr
143143 ; CHECK: call ext_func_i32@FUNCTION
144144 call void @ext_func_i32(i32* %r)
145 ; CHECK: set_global 0, $pop{{.+}}
145 ; CHECK: set_global __stack_pointer, $pop{{.+}}
146146 ret void
147147 }
148148
149149 ; CHECK-LABEL: dynamic_alloca_redzone:
150150 define void @dynamic_alloca_redzone(i32 %alloc) {
151 ; CHECK: get_global $push[[L13:.+]]=, 0{{$}}
151 ; CHECK: get_global $push[[L13:.+]]=, __stack_pointer{{$}}
152152 ; CHECK-NEXT: tee_local $push[[L12:.+]]=, [[SP:.+]], $pop[[L13]]{{$}}
153153 ; Target independent codegen bumps the stack pointer
154154 ; CHECK: i32.sub
165165 ; CHECK-LABEL: dynamic_static_alloca:
166166 define void @dynamic_static_alloca(i32 %alloc) noredzone {
167167 ; Decrement SP in the prolog by the static amount and writeback to memory.
168 ; CHECK: get_global $push[[L11:.+]]=, 0{{$}}
168 ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
169169 ; CHECK-NEXT: i32.const $push[[L12:.+]]=, 16
170170 ; CHECK-NEXT: i32.sub $push[[L23:.+]]=, $pop[[L11]], $pop[[L12]]
171171 ; CHECK-NEXT: tee_local $push[[L22:.+]]=, [[SP:.+]], $pop[[L23]]
172 ; CHECK-NEXT: set_global 0, $pop[[L22]]
172 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L22]]
173173
174174 ; Alloc and write to a static alloca
175175 ; CHECK: get_local $push[[L21:.+]]=, [[SP:.+]]
183183 ; CHECK: i32.sub
184184 ; CHECK: tee_local $push[[L16:.+]]=, [[dynamic_local:.+]], $pop{{.+}}
185185 ; CHECK: tee_local $push[[L15:.+]]=, [[other:.+]], $pop[[L16]]{{$}}
186 ; CHECK: set_global 0, $pop[[L15]]{{$}}
186 ; CHECK: set_global __stack_pointer, $pop[[L15]]{{$}}
187187 %dynamic = alloca i32, i32 %alloc
188188
189189 ; Ensure we don't modify the frame pointer after assigning it.
225225 ; CHECK: get_local $push[[L24:.+]]=, [[FP]]{{$}}
226226 ; CHECK: i32.const $push[[L18:.+]]=, 16
227227 ; CHECK-NEXT: i32.add $push[[L19:.+]]=, $pop[[L24]], $pop[[L18]]
228 ; CHECK-NEXT: set_global 0, $pop[[L19]]
228 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L19]]
229229 ret void
230230 }
231231
234234
235235 ; CHECK-LABEL: llvm_stack_builtins:
236236 define void @llvm_stack_builtins(i32 %alloc) noredzone {
237 ; CHECK: get_global $push[[L11:.+]]=, 0{{$}}
237 ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
238238 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]]
239239 ; CHECK-NEXT: set_local [[STACK:.+]], $pop[[L10]]
240240 %stack = call i8* @llvm.stacksave()
244244 %dynamic = alloca i32, i32 %alloc
245245
246246 ; CHECK: get_local $push[[L12:.+]]=, [[STACK]]
247 ; CHECK-NEXT: set_global 0, $pop[[L12]]
247 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]]
248248 call void @llvm.stackrestore(i8* %stack)
249249
250250 ret void
255255 ; moved after the stack pointer was updated for the dynamic alloca.
256256 ; CHECK-LABEL: dynamic_alloca_nouse:
257257 define void @dynamic_alloca_nouse(i32 %alloc) noredzone {
258 ; CHECK: get_global $push[[L11:.+]]=, 0{{$}}
258 ; CHECK: get_global $push[[L11:.+]]=, __stack_pointer{{$}}
259259 ; CHECK-NEXT: tee_local $push[[L10:.+]]=, {{.+}}, $pop[[L11]]
260260 ; CHECK-NEXT: set_local [[FP:.+]], $pop[[L10]]
261261 %dynamic = alloca i32, i32 %alloc
263263 ; CHECK-NOT: set_local [[FP]],
264264
265265 ; CHECK: get_local $push[[L12:.+]]=, [[FP]]
266 ; CHECK-NEXT: set_global 0, $pop[[L12]]
266 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L12]]
267267 ret void
268268 }
269269
294294
295295 ; Test __builtin_frame_address(0).
296296 ; CHECK-LABEL: frameaddress_0:
297 ; CHECK: get_global $push[[L3:.+]]=, 0{{$}}
297 ; CHECK: get_global $push[[L3:.+]]=, __stack_pointer{{$}}
298298 ; CHECK-NEXT: tee_local $push[[L2:.+]]=, [[FP:.+]], $pop[[L3]]{{$}}
299299 ; CHECK-NEXT: call use_i8_star@FUNCTION, $pop[[L2]]
300300 ; CHECK-NEXT: get_local $push[[L5:.+]]=, [[FP]]
301 ; CHECK-NEXT: set_global 0, $pop[[L5]]
301 ; CHECK-NEXT: set_global __stack_pointer, $pop[[L5]]
302302 define void @frameaddress_0() {
303303 %t = call i8* @llvm.frameaddress(i32 0)
304304 call void @use_i8_star(i8* %t)
319319
320320 ; Test a stack address passed to an inline asm.
321321 ; CHECK-LABEL: inline_asm:
322 ; CHECK: get_global {{.+}}, 0{{$}}
322 ; CHECK: get_global {{.+}}, __stack_pointer{{$}}
323323 ; CHECK: #APP
324324 ; CHECK-NEXT: # %{{[0-9]+}}{{$}}
325325 ; CHECK-NEXT: #NO_APP
2424 ; CHECK: Section {
2525 ; CHECK: Type: ELEM (0x9)
2626 ; CHECK: Size: 7
27 ; CHECK: Offset: 165
2827 ; CHECK: }
2928
3029 ; CHECK: Relocations [
31 ; CHECK: Section (9) CODE {
30 ; CHECK: Section (8) CODE {
3231 ; CHECK: Relocation {
3332 ; CHECK: Type: R_WEBASSEMBLY_FUNCTION_INDEX_LEB (0)
3433 ; CHECK: Offset: 0x4
4140 ; CHECK: }
4241 ; CHECK: Relocation {
4342 ; CHECK: Type: R_WEBASSEMBLY_TABLE_INDEX_SLEB (1)
44 ; CHECK: Offset: 0x1A
43 ; CHECK: Offset: 0x1E
4544 ; CHECK: Index: 0x0
4645 ; CHECK: }
4746 ; CHECK: }
0 ; RUN: llc -mtriple wasm32-unknown-unknown-wasm -filetype=obj %s -o - | obj2yaml | FileCheck %s
1
2 ; Function that uses explict stack, and should generate a reference to
3 ; __stack_pointer, along with the corresponding reloction entry.
4 define hidden void @foo() #0 {
5 entry:
6 alloca i32, align 4
7 ret void
8 }
9
10 ; CHECK: - Type: IMPORT
11 ; CHECK: Imports:
12 ; CHECK: - Module: env
13 ; CHECK: Field: __stack_pointer
14 ; CHECK: Kind: GLOBAL
15 ; CHECK: GlobalType: I32
16 ; CHECK: GlobalMutable: false
17 ; CHECK: - Type: CODE
18 ; CHECK: Relocations:
19 ; CHECK: - Type: R_WEBASSEMBLY_GLOBAL_INDEX_LEB
20 ; CHECK: Index: 0