diff --git a/llvm/include/llvm/MC/MCExpr.h b/llvm/include/llvm/MC/MCExpr.h --- a/llvm/include/llvm/MC/MCExpr.h +++ b/llvm/include/llvm/MC/MCExpr.h @@ -285,6 +285,7 @@ VK_Hexagon_IE_GOT, VK_WebAssembly_TYPEINDEX,// Reference to a symbol's type (signature) + VK_WebAssembly_GOT, // Wasm global that stores to a symbols address VK_AMDGPU_GOTPCREL32_LO, // symbol@gotpcrel32@lo VK_AMDGPU_GOTPCREL32_HI, // symbol@gotpcrel32@hi diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp --- a/llvm/lib/MC/MCExpr.cpp +++ b/llvm/lib/MC/MCExpr.cpp @@ -302,6 +302,7 @@ case VK_Hexagon_LD_PLT: return "LDPLT"; case VK_Hexagon_IE: return "IE"; case VK_Hexagon_IE_GOT: return "IEGOT"; + case VK_WebAssembly_GOT: return "GOT"; case VK_WebAssembly_TYPEINDEX: return "TYPEINDEX"; case VK_AMDGPU_GOTPCREL32_LO: return "gotpcrel32@lo"; case VK_AMDGPU_GOTPCREL32_HI: return "gotpcrel32@hi"; diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h --- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h +++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h @@ -87,7 +87,11 @@ /// Target Operand Flag enum. enum TOF { - MO_NO_FLAG = 0, + MO_NO_FLAG, + + // Address of data symbol via a wasm global. This adds a level of indirection + // similar to the GOT on native platforms. + MO_GOT = 1 << 3, // Flags to indicate the type of the symbol being referenced MO_SYMBOL_FUNCTION = 0x1, diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyFastISel.cpp @@ -152,6 +152,8 @@ } bool computeAddress(const Value *Obj, Address &Addr); void materializeLoadStoreOperands(Address &Addr); + unsigned materializeDataAddrPIC(const GlobalValue *GV); + unsigned materializeFunctionAddrPIC(const GlobalValue *GV); void addLoadStoreOperands(const Address &Addr, const MachineInstrBuilder &MIB, MachineMemOperand *MMO); unsigned maskI1Value(unsigned Reg, const Value *V); @@ -374,16 +376,87 @@ return Addr.getReg() != 0; } +unsigned WebAssemblyFastISel::materializeFunctionAddrPIC(const GlobalValue *GV) { + // For PIC code, function addresses need to be offset by to __table_base + // wasm global. + LLVM_DEBUG(dbgs() << "materializeFunctionAddrPIC\n"); + unsigned BaseReg = createResultReg(&WebAssembly::I32RegClass); + auto *Base = FuncInfo.MF->createExternalSymbolName("__table_base"); + unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::GLOBAL_GET_I64 + : WebAssembly::GLOBAL_GET_I32; + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), BaseReg) + .addExternalSymbol(Base, WebAssemblyII::MO_SYMBOL_GLOBAL); + + unsigned OpcConst = WebAssembly::CONST_I32; + unsigned ConstReg = createResultReg(&WebAssembly::I32RegClass); + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpcConst), + ConstReg) + .addGlobalAddress(GV); + + unsigned OpcAdd = WebAssembly::ADD_I32; + unsigned ResultReg = createResultReg(&WebAssembly::I32RegClass); + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpcAdd), + ResultReg) + .addReg(BaseReg) + .addReg(ConstReg); + return ResultReg; +} + +unsigned WebAssemblyFastISel::materializeDataAddrPIC(const GlobalValue *GV) { + + unsigned Reg = + createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass + : &WebAssembly::I32RegClass); + assert(TLI.isPositionIndependent()); + dbgs() << "materializeDataAddrPIC: " << GV->getName() << " " << GV->isDeclaration() << "\n"; + // For PIC code, data address are either loaded offset from __memory_base or + // from an imported global. + unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::GLOBAL_GET_I64 + : WebAssembly::GLOBAL_GET_I32; + if (TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) { + unsigned BaseReg = createResultReg(&WebAssembly::I32RegClass); + auto *Base = FuncInfo.MF->createExternalSymbolName("__memory_base"); + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), BaseReg) + .addExternalSymbol(Base, WebAssemblyII::MO_SYMBOL_GLOBAL); + + unsigned OpcConst = WebAssembly::CONST_I32; + unsigned ConstReg = createResultReg(&WebAssembly::I32RegClass); + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpcConst), + ConstReg) + .addGlobalAddress(GV); + + unsigned OpcAdd = WebAssembly::ADD_I32; + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpcAdd), Reg) + .addReg(BaseReg) + .addReg(ConstReg); + } else { + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), Reg) + .addGlobalAddress(GV, /*Offset*/ 0, WebAssemblyII::MO_GOT); + } + + return Reg; +} + void WebAssemblyFastISel::materializeLoadStoreOperands(Address &Addr) { + dbgs() << "materializeLoadStoreOperands\n"; if (Addr.isRegBase()) { unsigned Reg = Addr.getReg(); if (Reg == 0) { - Reg = createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass + const GlobalValue *GV = Addr.getGlobalValue(); + if (GV && TLI.isPositionIndependent()) { + if (GV->getValueType()->isFunctionTy()) + Reg = materializeFunctionAddrPIC(GV); + else + Reg = materializeDataAddrPIC(GV); + } else { + Reg = + createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass : &WebAssembly::I32RegClass); - unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64 - : WebAssembly::CONST_I32; - BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), Reg) - .addImm(0); + unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64 + : WebAssembly::CONST_I32; + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), Reg) + .addImm(0); + } Addr.setReg(Reg); } } @@ -396,7 +469,20 @@ // TODO: Disable SetP2AlignOperands for FastISel and just do it here. MIB.addImm(0); - if (const GlobalValue *GV = Addr.getGlobalValue()) + const GlobalValue *GV = Addr.getGlobalValue(); + bool IncludeGlobalAddr = GV != nullptr; + + // For PIC code the global address in the stack already + if (GV && TLI.isPositionIndependent()) // && !TM.shouldAssumeDSOLocal(*GV->getParent(), GV)) + IncludeGlobalAddr = false; + + dbgs() << "addLoadStoreOperands IncludeGlobalAddr:" << IncludeGlobalAddr << "\n"; + dbgs() << "addLoadStoreOperands GV:" << GV << "\n"; + dbgs() << "addLoadStoreOperands PIC:" << TLI.isPositionIndependent() << "\n"; + if (GV) + dbgs() << "addLoadStoreOperands shouldAssumeDSOLocal:" << TM.shouldAssumeDSOLocal(*GV->getParent(), GV) << "\n"; + + if (IncludeGlobalAddr) MIB.addGlobalAddress(GV, Addr.getOffset()); else MIB.addImm(Addr.getOffset()); @@ -604,15 +690,23 @@ } unsigned WebAssemblyFastISel::fastMaterializeConstant(const Constant *C) { - if (const auto *GV = dyn_cast(C)) { - unsigned ResultReg = - createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass - : &WebAssembly::I32RegClass); - unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64 - : WebAssembly::CONST_I32; - BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) - .addGlobalAddress(GV); - return ResultReg; + if (const GlobalValue *GV = dyn_cast(C)) { + LLVM_DEBUG(dbgs() << "fastMaterializeConstant " << *GV << "\n"); + if (TLI.isPositionIndependent()) { + if (GV->getValueType()->isFunctionTy()) + return materializeFunctionAddrPIC(GV); + else + return materializeDataAddrPIC(GV); + } else { + unsigned ResultReg = + createResultReg(Subtarget->hasAddr64() ? &WebAssembly::I64RegClass + : &WebAssembly::I32RegClass); + unsigned Opc = Subtarget->hasAddr64() ? WebAssembly::CONST_I64 + : WebAssembly::CONST_I32; + BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg) + .addGlobalAddress(GV); + return ResultReg; + } } // Let target-independent code handle it. @@ -741,9 +835,15 @@ return false; bool IsDirect = Func != nullptr; + if (!IsDirect && isa(Call->getCalledValue())) return false; + if (IsDirect && TLI.isPositionIndependent() && !Func->hasHiddenVisibility()) { + LLVM_DEBUG(dbgs() << "call to external function: " << *Func << "\n"); + IsDirect = false; + } + FunctionType *FuncTy = Call->getFunctionType(); unsigned Opc; bool IsVoid = FuncTy->getReturnType()->isVoidTy(); diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyISelLowering.cpp @@ -759,9 +759,10 @@ } InTys.push_back(MVT::Other); SDVTList InTyList = DAG.getVTList(InTys); - SDValue Res = - DAG.getNode(Ins.empty() ? WebAssemblyISD::CALL0 : WebAssemblyISD::CALL1, - DL, InTyList, Ops); + unsigned Opcode = Ins.empty() ? WebAssemblyISD::CALL0 : WebAssemblyISD::CALL1; + //if (isPositionIndependent() ) + //Opcode = Ins.empty() ? WebAssemblyISD::CALL_INDIRECT1 : WebAssemblyISD::CALL_INDIRECT1; + SDValue Res = DAG.getNode(Opcode, DL, InTyList, Ops); if (Ins.empty()) { Chain = Res; } else { @@ -977,15 +978,28 @@ SDValue WebAssemblyTargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { SDLoc DL(Op); + dbgs() << "LowerGlobalAddress\n"; + const auto *GA = cast(Op); + EVT VT = Op.getValueType(); assert(GA->getTargetFlags() == 0 && "Unexpected target flags on generic GlobalAddressSDNode"); if (GA->getAddressSpace() != 0) fail(DL, DAG, "WebAssembly only expects the 0 address space"); - return DAG.getNode( - WebAssemblyISD::Wrapper, DL, VT, - DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, GA->getOffset())); + + const GlobalValue *GV = GA->getGlobal(); + + unsigned Flags = 0; + if (isPositionIndependent() && + !getTargetMachine().shouldAssumeDSOLocal(*GV->getParent(), GV)) { + Flags |= WebAssemblyII::MO_GOT; + dbgs() << "GOT\n"; + } + + return DAG.getNode(WebAssemblyISD::Wrapper, DL, VT, + DAG.getTargetGlobalAddress(GA->getGlobal(), DL, VT, + GA->getOffset(), Flags)); } SDValue @@ -996,11 +1010,10 @@ EVT VT = Op.getValueType(); assert(ES->getTargetFlags() == 0 && "Unexpected target flags on generic ExternalSymbolSDNode"); - // Set the TargetFlags to 0x1 which indicates that this is a "function" - // symbol rather than a data symbol. We do this unconditionally even though - // we don't know anything about the symbol other than its name, because all - // external symbols used in target-independent SelectionDAG code are for - // functions. + // Set TargetFlags to WebAssemblyII::MO_SYMBOL_FUNCTION. We do this + // unconditionally even though we don't know anything about the symbol other + // than its name, because all external symbols used in target-independent + // SelectionDAG code are for functions. return DAG.getNode( WebAssemblyISD::Wrapper, DL, VT, DAG.getTargetExternalSymbol(ES->getSymbol(), VT, diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td --- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td +++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrInfo.td @@ -15,6 +15,9 @@ // WebAssembly Instruction Predicate Definitions. //===----------------------------------------------------------------------===// +def IsPIC : Predicate<"TM.isPositionIndependent()">; +def IsNotPIC : Predicate<"!TM.isPositionIndependent()">; + def HasAddr32 : Predicate<"!Subtarget->hasAddr64()">; def HasAddr64 : Predicate<"Subtarget->hasAddr64()">; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyInstrMemory.td b/llvm/lib/Target/WebAssembly/WebAssemblyInstrMemory.td --- a/llvm/lib/Target/WebAssembly/WebAssemblyInstrMemory.td +++ b/llvm/lib/Target/WebAssembly/WebAssemblyInstrMemory.td @@ -95,7 +95,7 @@ class LoadPatGlobalAddr : Pat<(ty (kind (regPlusGA I32:$addr, (WebAssemblywrapper tglobaladdr:$off)))), - (inst 0, tglobaladdr:$off, I32:$addr)>; + (inst 0, tglobaladdr:$off, I32:$addr)>, Requires<[IsNotPIC]>; def : LoadPatGlobalAddr; def : LoadPatGlobalAddr; @@ -104,7 +104,7 @@ class LoadPatExternalSym : Pat<(ty (kind (add I32:$addr, (WebAssemblywrapper texternalsym:$off)))), - (inst 0, texternalsym:$off, I32:$addr)>; + (inst 0, texternalsym:$off, I32:$addr)>, Requires<[IsNotPIC]>; def : LoadPatExternalSym; def : LoadPatExternalSym; def : LoadPatExternalSym; @@ -122,7 +122,7 @@ class LoadPatGlobalAddrOffOnly : Pat<(ty (kind (WebAssemblywrapper tglobaladdr:$off))), - (inst 0, tglobaladdr:$off, (CONST_I32 0))>; + (inst 0, tglobaladdr:$off, (CONST_I32 0))>, Requires<[IsNotPIC]>; def : LoadPatGlobalAddrOffOnly; def : LoadPatGlobalAddrOffOnly; @@ -131,7 +131,7 @@ class LoadPatExternSymOffOnly : Pat<(ty (kind (WebAssemblywrapper texternalsym:$off))), - (inst 0, texternalsym:$off, (CONST_I32 0))>; + (inst 0, texternalsym:$off, (CONST_I32 0))>, Requires<[IsNotPIC]>; def : LoadPatExternSymOffOnly; def : LoadPatExternSymOffOnly; def : LoadPatExternSymOffOnly; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.h b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.h --- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.h @@ -33,7 +33,7 @@ MCSymbol *GetGlobalAddressSymbol(const MachineOperand &MO) const; MCSymbol *GetExternalSymbolSymbol(const MachineOperand &MO) const; MCOperand lowerSymbolOperand(MCSymbol *Sym, int64_t Offset, bool IsFunc, - bool IsGlob, bool IsEvent) const; + bool IsGlob, bool IsEvent, unsigned flags) const; public: WebAssemblyMCInstLower(MCContext &ctx, WebAssemblyAsmPrinter &printer) diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp --- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp @@ -73,11 +73,12 @@ auto *WasmSym = cast(Printer.GetExternalSymbolSymbol(Name)); const WebAssemblySubtarget &Subtarget = Printer.getSubtarget(); - // Except for the two exceptions (__stack_pointer and __cpp_exception), all - // other external symbols used by CodeGen are functions. It's OK to hardcode - // knowledge of specific symbols here; this method is precisely there for - // fetching the signatures of known Clang-provided symbols. - if (strcmp(Name, "__stack_pointer") == 0) { + // Except for certain known symbols, all symbols used by CodeGen are + // functions. It's OK to hardcode knowledge of specific symbols here; this + // method is precisely there for fetching the signatures of known + // Clang-provided symbols. + if (strcmp(Name, "__stack_pointer") == 0 || + strcmp(Name, "__memory_base") == 0 || strcmp(Name, "__table_base") == 0) { WasmSym->setType(wasm::WASM_SYMBOL_TYPE_GLOBAL); WasmSym->setGlobalType(wasm::WasmGlobalType{ uint8_t(Subtarget.hasAddr64() ? wasm::WASM_TYPE_I64 @@ -121,9 +122,13 @@ MCOperand WebAssemblyMCInstLower::lowerSymbolOperand(MCSymbol *Sym, int64_t Offset, bool IsFunc, bool IsGlob, - bool IsEvent) const { - const MCExpr *Expr = - MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Ctx); + bool IsEvent, unsigned TargetFlags) const { + MCSymbolRefExpr::VariantKind Kind = MCSymbolRefExpr::VK_None; + if (TargetFlags == WebAssemblyII::MO_GOT) { + dbgs() << "lowerSymbolOperand GOT!\n"; + Kind = MCSymbolRefExpr::VK_WebAssembly_GOT; + } + const MCExpr *Expr = MCSymbolRefExpr::create(Sym, Kind, Ctx); if (Offset != 0) { if (IsFunc) @@ -230,11 +235,9 @@ break; } case MachineOperand::MO_GlobalAddress: - assert(MO.getTargetFlags() == WebAssemblyII::MO_NO_FLAG && - "WebAssembly does not use target flags on GlobalAddresses"); MCOp = lowerSymbolOperand(GetGlobalAddressSymbol(MO), MO.getOffset(), MO.getGlobal()->getValueType()->isFunctionTy(), - false, false); + false, false, MO.getTargetFlags()); break; case MachineOperand::MO_ExternalSymbol: // The target flag indicates whether this is a symbol for a @@ -245,7 +248,8 @@ GetExternalSymbolSymbol(MO), /*Offset=*/0, (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_FUNCTION) != 0, (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_GLOBAL) != 0, - (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_EVENT) != 0); + (MO.getTargetFlags() & WebAssemblyII::MO_SYMBOL_EVENT) != 0, + MO.getTargetFlags()); break; case MachineOperand::MO_MCSymbol: // This is currently used only for LSDA symbols (GCC_except_table), @@ -253,7 +257,7 @@ assert(MO.getTargetFlags() == 0 && "WebAssembly does not use target flags on MCSymbol"); MCOp = lowerSymbolOperand(MO.getMCSymbol(), /*Offset=*/0, false, false, - false); + false, MO.getTargetFlags()); break; } diff --git a/llvm/test/CodeGen/WebAssembly/address-offsets.ll b/llvm/test/CodeGen/WebAssembly/address-offsets.ll --- a/llvm/test/CodeGen/WebAssembly/address-offsets.ll +++ b/llvm/test/CodeGen/WebAssembly/address-offsets.ll @@ -1,4 +1,6 @@ -; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s +; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=CHECK,NPIC +; RUN: llc < %s -asm-verbose=false -disable-wasm-fallthrough-return-opt -wasm-disable-explicit-locals -wasm-keep-registers -relocation-model=pic -fast-isel | FileCheck %s -check-prefixes=CHECK,PIC + ; Test folding constant offsets and symbols into load and store addresses under ; a variety of circumstances. @@ -10,8 +12,10 @@ ; CHECK-LABEL: load_test0: ; CHECK-NEXT: .functype load_test0 () -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 0{{$}} -; CHECK-NEXT: i32.load $push1=, g+40($pop0){{$}} +; NPIC-NEXT: i32.const $push0=, 0{{$}} +; NPIC-NEXT: i32.load $push1=, g+40($pop0){{$}} +; PIC-NEXT: global.get $push0=, g@GOT{{$}} +; PIC-NEXT: i32.load $push1=, 40($pop0){{$}} ; CHECK-NEXT: return $pop1{{$}} define i32 @load_test0() { %t = load i32, i32* getelementptr inbounds ([0 x i32], [0 x i32]* @g, i32 0, i32 10), align 4 @@ -20,8 +24,10 @@ ; CHECK-LABEL: load_test0_noinbounds: ; CHECK-NEXT: .functype load_test0_noinbounds () -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 0{{$}} -; CHECK-NEXT: i32.load $push1=, g+40($pop0){{$}} +; NPIC-NEXT: i32.const $push0=, 0{{$}} +; NPIC-NEXT: i32.load $push1=, g+40($pop0){{$}} +; PIC-NEXT: global.get $push0=, g@GOT{{$}} +; PIC-NEXT: i32.load $push1=, 40($pop0){{$}} ; CHECK-NEXT: return $pop1{{$}} define i32 @load_test0_noinbounds() { %t = load i32, i32* getelementptr ([0 x i32], [0 x i32]* @g, i32 0, i32 10), align 4 @@ -34,7 +40,7 @@ ; CHECK-LABEL: load_test1: ; CHECK-NEXT: .functype load_test1 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -47,7 +53,7 @@ ; CHECK-LABEL: load_test2: ; CHECK-NEXT: .functype load_test2 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -60,7 +66,7 @@ ; CHECK-LABEL: load_test3: ; CHECK-NEXT: .functype load_test3 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -73,7 +79,7 @@ ; CHECK-LABEL: load_test4: ; CHECK-NEXT: .functype load_test4 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -85,7 +91,7 @@ ; CHECK-LABEL: load_test5: ; CHECK-NEXT: .functype load_test5 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -97,7 +103,7 @@ ; CHECK-LABEL: load_test6: ; CHECK-NEXT: .functype load_test6 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -110,7 +116,7 @@ ; CHECK-LABEL: load_test7: ; CHECK-NEXT: .functype load_test7 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -123,7 +129,7 @@ ; CHECK-LABEL: load_test8: ; CHECK-NEXT: .functype load_test8 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} ; CHECK-NEX T: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.load $push2=, g+40($pop1){{$}} ; CHECK-NEX T: return $pop2{{$}} @@ -136,8 +142,10 @@ ; CHECK-LABEL: load_test9: ; CHECK-NEXT: .functype load_test9 () -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 0{{$}} -; CHECK-NEXT: i32.load $push1=, g-40($pop0){{$}} +; PIC-NEXT: global.get $push0=, g@GOT{{$}} +; PIC-NEXT: i32.load $push1=, 4294967256($pop0){{$}} +; NPIC-NEXT: i32.const $push0=, 0{{$}} +; NPIC-NEXT: i32.load $push1=, g-40($pop0){{$}} ; CHECK-NEXT: return $pop1{{$}} define i32 @load_test9() { %t = load i32, i32* getelementptr inbounds ([0 x i32], [0 x i32]* @g, i32 0, i32 1073741814), align 4 @@ -146,12 +154,20 @@ ; CHECK-LABEL: load_test10: ; CHECK-NEXT: .functype load_test10 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} -; CHECK-NEXT: i32.const $push2=, g-40{{$}} -; CHECK-NEXT: i32.add $push3=, $pop1, $pop2{{$}} -; CHECK-NEXT: i32.load $push4=, 0($pop3){{$}} -; CHECK-NEXT: return $pop4{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push2=, g-40{{$}} +; NPIC-NEXT: i32.add $push3=, $pop1, $pop2{{$}} +; NPIC-NEXT: i32.load $push4=, 0($pop3){{$}} +; NPIC-NEXT: return $pop4{{$}} +; PIC-NEXT: global.get $push1=, g@GOT{{$}} +; PIC-NEXT: i32.const $push5=, -10{{$}} +; PIC-NEXT: i32.add $push6=, $0, $pop5{{$}} +; PIC-NEXT: i32.const $push2=, 2{{$}} +; PIC-NEXT: i32.shl $push3=, $pop6, $pop2{{$}} +; PIC-NEXT: i32.add $push4=, $pop1, $pop3{{$}} +; PIC-NEXT: i32.load $push0=, 0($pop4){{$}} +; PIC-NEXT: return $pop0{{$}} define i32 @load_test10(i32 %n) { %add = add nsw i32 %n, -10 %arrayidx = getelementptr inbounds [0 x i32], [0 x i32]* @g, i32 0, i32 %add @@ -171,10 +187,10 @@ ; CHECK-LABEL: load_test11_noinbounds: ; CHECK-NEXT: .functype load_test11_noinbounds (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 40{{$}} -; CHECK-NEXT: i32.add $push1=, $0, $pop0{{$}} -; CHECK-NEXT: i32.load $push2=, 0($pop1){{$}} -; CHECK-NEXT: return $pop2{{$}} +; NPIC-NEXT: i32.const $push0=, 40{{$}} +; NPIC-NEXT: i32.add $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.load $push2=, 0($pop1){{$}} +; NPIC-NEXT: return $pop2{{$}} define i32 @load_test11_noinbounds(i32* %p) { %arrayidx = getelementptr i32, i32* %p, i32 10 %t = load i32, i32* %arrayidx, align 4 @@ -183,13 +199,13 @@ ; CHECK-LABEL: load_test12: ; CHECK-NEXT: .functype load_test12 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.load $push5=, 0($pop4){{$}} -; CHECK-NEXT: return $pop5{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.load $push5=, 0($pop4){{$}} +; NPIC-NEXT: return $pop5{{$}} define i32 @load_test12(i32* %p, i32 %n) { %add = add nsw i32 %n, 10 %arrayidx = getelementptr inbounds i32, i32* %p, i32 %add @@ -199,13 +215,13 @@ ; CHECK-LABEL: load_test13: ; CHECK-NEXT: .functype load_test13 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.load $push5=, 0($pop4){{$}} -; CHECK-NEXT: return $pop5{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.load $push5=, 0($pop4){{$}} +; NPIC-NEXT: return $pop5{{$}} define i32 @load_test13(i32* %p, i32 %n) { %add = add nsw i32 10, %n %arrayidx = getelementptr inbounds i32, i32* %p, i32 %add @@ -215,11 +231,11 @@ ; CHECK-LABEL: load_test14: ; CHECK-NEXT: .functype load_test14 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.load $push3=, 40($pop2){{$}} -; CHECK-NEXT: return $pop3{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.load $push3=, 40($pop2){{$}} +; NPIC-NEXT: return $pop3{{$}} define i32 @load_test14(i32* %p, i32 %n) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 %n %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 10 @@ -229,13 +245,13 @@ ; CHECK-LABEL: load_test15: ; CHECK-NEXT: .functype load_test15 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.load $push5=, 0($pop4){{$}} -; CHECK-NEXT: return $pop5{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.load $push5=, 0($pop4){{$}} +; NPIC-NEXT: return $pop5{{$}} define i32 @load_test15(i32* %p, i32 %n) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 10 %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 %n @@ -245,13 +261,13 @@ ; CHECK-LABEL: load_test16: ; CHECK-NEXT: .functype load_test16 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.load $push5=, 0($pop4){{$}} -; CHECK-NEXT: return $pop5{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.load $push5=, 0($pop4){{$}} +; NPIC-NEXT: return $pop5{{$}} define i32 @load_test16(i32* %p, i32 %n) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 10 %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 %n @@ -261,13 +277,13 @@ ; CHECK-LABEL: load_test17: ; CHECK-NEXT: .functype load_test17 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.load $push5=, 0($pop4){{$}} -; CHECK-NEXT: return $pop5{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.load $push5=, 0($pop4){{$}} +; NPIC-NEXT: return $pop5{{$}} define i32 @load_test17(i32* %p, i32 %n) { %add = add nsw i32 %n, 10 %add.ptr = getelementptr inbounds i32, i32* %p, i32 %add @@ -277,11 +293,11 @@ ; CHECK-LABEL: load_test18: ; CHECK-NEXT: .functype load_test18 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.load $push3=, 40($pop2){{$}} -; CHECK-NEXT: return $pop3{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.load $push3=, 40($pop2){{$}} +; NPIC-NEXT: return $pop3{{$}} define i32 @load_test18(i32* %p, i32 %n) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 %n %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 10 @@ -291,13 +307,13 @@ ; CHECK-LABEL: load_test19: ; CHECK-NEXT: .functype load_test19 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.load $push5=, 0($pop4){{$}} -; CHECK-NEXT: return $pop5{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.load $push5=, 0($pop4){{$}} +; NPIC-NEXT: return $pop5{{$}} define i32 @load_test19(i32* %p, i32 %n) { %add = add nsw i32 10, %n %add.ptr = getelementptr inbounds i32, i32* %p, i32 %add @@ -307,10 +323,10 @@ ; CHECK-LABEL: load_test20: ; CHECK-NEXT: .functype load_test20 (i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, -40{{$}} -; CHECK-NEXT: i32.add $push1=, $0, $pop0{{$}} -; CHECK-NEXT: i32.load $push2=, 0($pop1){{$}} -; CHECK-NEXT: return $pop2{{$}} +; NPIC-NEXT: i32.const $push0=, -40{{$}} +; NPIC-NEXT: i32.add $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.load $push2=, 0($pop1){{$}} +; NPIC-NEXT: return $pop2{{$}} define i32 @load_test20(i32* %p) { %arrayidx = getelementptr inbounds i32, i32* %p, i32 -10 %t = load i32, i32* %arrayidx, align 4 @@ -319,13 +335,13 @@ ; CHECK-LABEL: load_test21: ; CHECK-NEXT: .functype load_test21 (i32, i32) -> (i32){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, -40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.load $push5=, 0($pop4){{$}} -; CHECK-NEXT: return $pop5{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, -40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.load $push5=, 0($pop4){{$}} +; NPIC-NEXT: return $pop5{{$}} define i32 @load_test21(i32* %p, i32 %n) { %add = add nsw i32 %n, -10 %arrayidx = getelementptr inbounds i32, i32* %p, i32 %add @@ -335,9 +351,9 @@ ; CHECK-LABEL: store_test0: ; CHECK-NEXT: .functype store_test0 (i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 0{{$}} -; CHECK-NEXT: i32.store g+40($pop0), $0{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 0{{$}} +; NPIC-NEXT: i32.store g+40($pop0), $0{{$}} +; NPIC-NEXT: return{{$}} define void @store_test0(i32 %i) { store i32 %i, i32* getelementptr inbounds ([0 x i32], [0 x i32]* @g, i32 0, i32 10), align 4 ret void @@ -345,9 +361,9 @@ ; CHECK-LABEL: store_test0_noinbounds: ; CHECK-NEXT: .functype store_test0_noinbounds (i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 0{{$}} -; CHECK-NEXT: i32.store g+40($pop0), $0{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 0{{$}} +; NPIC-NEXT: i32.store g+40($pop0), $0{{$}} +; NPIC-NEXT: return{{$}} define void @store_test0_noinbounds(i32 %i) { store i32 %i, i32* getelementptr ([0 x i32], [0 x i32]* @g, i32 0, i32 10), align 4 ret void @@ -355,8 +371,8 @@ ; CHECK-LABEL: store_test1: ; CHECK-NEXT: .functype store_test1 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test1(i32 %n, i32 %i) { @@ -368,8 +384,8 @@ ; CHECK-LABEL: store_test2: ; CHECK-NEXT: .functype store_test2 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test2(i32 %n, i32 %i) { @@ -381,8 +397,8 @@ ; CHECK-LABEL: store_test3: ; CHECK-NEXT: .functype store_test3 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test3(i32 %n, i32 %i) { @@ -394,8 +410,8 @@ ; CHECK-LABEL: store_test4: ; CHECK-NEXT: .functype store_test4 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test4(i32 %n, i32 %i) { @@ -406,8 +422,8 @@ ; CHECK-LABEL: store_test5: ; CHECK-NEXT: .functype store_test5 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test5(i32 %n, i32 %i) { @@ -418,8 +434,8 @@ ; CHECK-LABEL: store_test6: ; CHECK-NEXT: .functype store_test6 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test6(i32 %n, i32 %i) { @@ -431,8 +447,8 @@ ; CHECK-LABEL: store_test7: ; CHECK-NEXT: .functype store_test7 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test7(i32 %n, i32 %i) { @@ -444,8 +460,8 @@ ; CHECK-LABEL: store_test8: ; CHECK-NEXT: .functype store_test8 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} ; CHECK-NEX T: i32.store g+40($pop1), $1{{$}} ; CHECK-NEX T: return{{$}} define void @store_test8(i32 %n, i32 %i) { @@ -457,9 +473,9 @@ ; CHECK-LABEL: store_test9: ; CHECK-NEXT: .functype store_test9 (i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 0{{$}} -; CHECK-NEXT: i32.store g-40($pop0), $0{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 0{{$}} +; NPIC-NEXT: i32.store g-40($pop0), $0{{$}} +; NPIC-NEXT: return{{$}} define void @store_test9(i32 %i) { store i32 %i, i32* getelementptr inbounds ([0 x i32], [0 x i32]* @g, i32 0, i32 1073741814), align 4 ret void @@ -467,12 +483,12 @@ ; CHECK-LABEL: store_test10: ; CHECK-NEXT: .functype store_test10 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $0, $pop0{{$}} -; CHECK-NEXT: i32.const $push2=, g-40{{$}} -; CHECK-NEXT: i32.add $push3=, $pop1, $pop2{{$}} -; CHECK-NEXT: i32.store 0($pop3), $1{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.const $push2=, g-40{{$}} +; NPIC-NEXT: i32.add $push3=, $pop1, $pop2{{$}} +; NPIC-NEXT: i32.store 0($pop3), $1{{$}} +; NPIC-NEXT: return{{$}} define void @store_test10(i32 %n, i32 %i) { %add = add nsw i32 %n, -10 %arrayidx = getelementptr inbounds [0 x i32], [0 x i32]* @g, i32 0, i32 %add @@ -482,8 +498,8 @@ ; CHECK-LABEL: store_test11: ; CHECK-NEXT: .functype store_test11 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.store 40($0), $1{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.store 40($0), $1{{$}} +; NPIC-NEXT: return{{$}} define void @store_test11(i32* %p, i32 %i) { %arrayidx = getelementptr inbounds i32, i32* %p, i32 10 store i32 %i, i32* %arrayidx, align 4 @@ -492,10 +508,10 @@ ; CHECK-LABEL: store_test11_noinbounds: ; CHECK-NEXT: .functype store_test11_noinbounds (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 40{{$}} -; CHECK-NEXT: i32.add $push1=, $0, $pop0{{$}} -; CHECK-NEXT: i32.store 0($pop1), $1{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 40{{$}} +; NPIC-NEXT: i32.add $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.store 0($pop1), $1{{$}} +; NPIC-NEXT: return{{$}} define void @store_test11_noinbounds(i32* %p, i32 %i) { %arrayidx = getelementptr i32, i32* %p, i32 10 store i32 %i, i32* %arrayidx, align 4 @@ -504,13 +520,13 @@ ; CHECK-LABEL: store_test12: ; CHECK-NEXT: .functype store_test12 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.store 0($pop4), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.store 0($pop4), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test12(i32* %p, i32 %n, i32 %i) { %add = add nsw i32 %n, 10 %arrayidx = getelementptr inbounds i32, i32* %p, i32 %add @@ -520,13 +536,13 @@ ; CHECK-LABEL: store_test13: ; CHECK-NEXT: .functype store_test13 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.store 0($pop4), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.store 0($pop4), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test13(i32* %p, i32 %n, i32 %i) { %add = add nsw i32 10, %n %arrayidx = getelementptr inbounds i32, i32* %p, i32 %add @@ -536,11 +552,11 @@ ; CHECK-LABEL: store_test14: ; CHECK-NEXT: .functype store_test14 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.store 40($pop2), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.store 40($pop2), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test14(i32* %p, i32 %n, i32 %i) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 %n %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 10 @@ -550,13 +566,13 @@ ; CHECK-LABEL: store_test15: ; CHECK-NEXT: .functype store_test15 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.store 0($pop4), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.store 0($pop4), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test15(i32* %p, i32 %n, i32 %i) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 10 %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 %n @@ -566,13 +582,13 @@ ; CHECK-LABEL: store_test16: ; CHECK-NEXT: .functype store_test16 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.store 0($pop4), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.store 0($pop4), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test16(i32* %p, i32 %n, i32 %i) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 10 %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 %n @@ -582,13 +598,13 @@ ; CHECK-LABEL: store_test17: ; CHECK-NEXT: .functype store_test17 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.store 0($pop4), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.store 0($pop4), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test17(i32* %p, i32 %n, i32 %i) { %add = add nsw i32 %n, 10 %add.ptr = getelementptr inbounds i32, i32* %p, i32 %add @@ -598,11 +614,11 @@ ; CHECK-LABEL: store_test18: ; CHECK-NEXT: .functype store_test18 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.store 40($pop2), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.store 40($pop2), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test18(i32* %p, i32 %n, i32 %i) { %add.ptr = getelementptr inbounds i32, i32* %p, i32 %n %add.ptr1 = getelementptr inbounds i32, i32* %add.ptr, i32 10 @@ -612,13 +628,13 @@ ; CHECK-LABEL: store_test19: ; CHECK-NEXT: .functype store_test19 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, 40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.store 0($pop4), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, 40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.store 0($pop4), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test19(i32* %p, i32 %n, i32 %i) { %add = add nsw i32 10, %n %add.ptr = getelementptr inbounds i32, i32* %p, i32 %add @@ -628,10 +644,10 @@ ; CHECK-LABEL: store_test20: ; CHECK-NEXT: .functype store_test20 (i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, -40{{$}} -; CHECK-NEXT: i32.add $push1=, $0, $pop0{{$}} -; CHECK-NEXT: i32.store 0($pop1), $1{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, -40{{$}} +; NPIC-NEXT: i32.add $push1=, $0, $pop0{{$}} +; NPIC-NEXT: i32.store 0($pop1), $1{{$}} +; NPIC-NEXT: return{{$}} define void @store_test20(i32* %p, i32 %i) { %arrayidx = getelementptr inbounds i32, i32* %p, i32 -10 store i32 %i, i32* %arrayidx, align 4 @@ -640,13 +656,13 @@ ; CHECK-LABEL: store_test21: ; CHECK-NEXT: .functype store_test21 (i32, i32, i32) -> (){{$}} -; CHECK-NEXT: i32.const $push0=, 2{{$}} -; CHECK-NEXT: i32.shl $push1=, $1, $pop0{{$}} -; CHECK-NEXT: i32.add $push2=, $0, $pop1{{$}} -; CHECK-NEXT: i32.const $push3=, -40{{$}} -; CHECK-NEXT: i32.add $push4=, $pop2, $pop3{{$}} -; CHECK-NEXT: i32.store 0($pop4), $2{{$}} -; CHECK-NEXT: return{{$}} +; NPIC-NEXT: i32.const $push0=, 2{{$}} +; NPIC-NEXT: i32.shl $push1=, $1, $pop0{{$}} +; NPIC-NEXT: i32.add $push2=, $0, $pop1{{$}} +; NPIC-NEXT: i32.const $push3=, -40{{$}} +; NPIC-NEXT: i32.add $push4=, $pop2, $pop3{{$}} +; NPIC-NEXT: i32.store 0($pop4), $2{{$}} +; NPIC-NEXT: return{{$}} define void @store_test21(i32* %p, i32 %n, i32 %i) { %add = add nsw i32 %n, -10 %arrayidx = getelementptr inbounds i32, i32* %p, i32 %add diff --git a/llvm/test/CodeGen/WebAssembly/load-store-pic.ll b/llvm/test/CodeGen/WebAssembly/load-store-pic.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/WebAssembly/load-store-pic.ll @@ -0,0 +1,150 @@ +; RUN: llc < %s -asm-verbose=false -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=NON-PIC +; RUN: llc < %s -asm-verbose=false -relocation-model=pic -fast-isel -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=PIC +; XXX: llc < %s -asm-verbose=false -relocation-model=pic -wasm-disable-explicit-locals -wasm-keep-registers | FileCheck %s -check-prefixes=PIC + +; Test that globals assemble as expected with -fPIC + +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown" + +@hidden_global = external hidden global i32 +@hidden_global_array = external hidden global [10 x i32] +@external_global = external global i32 +@external_global_array = external global [10 x i32] + +declare i32 @foo(); + +; For hidden symbols PIC code needs to offset all loads and stores +; by the value of the __memory_base global + +define i32 @load_hidden_global() { +; PIC-LABEL: load_hidden_global: +; PIC: global.get $push1=, __memory_base{{$}} +; PIC-NEXT: i32.const $push2=, hidden_global{{$}} +; PIC-NEXT: i32.add $push0=, $pop1, $pop2{{$}} +; PIC-NEXT: i32.load $push3=, 0($pop0){{$}} +; PIC-NEXT: end_function + +; NON-PIC-LABEL: load_hidden_global: +; NON-PIC: i32.const $push0=, 0{{$}} +; NON-PIC-NEXT: i32.load $push1=, hidden_global($pop0){{$}} +; NON-PIC-NEXT: end_function + + %1 = load i32, i32* @hidden_global + ret i32 %1 +} + +define i32 @load_hidden_global_offset() { +; PIC-LABEL: load_hidden_global_offset: +; PIC: global.get $push2=, __memory_base{{$}} +; PIC-NEXT: i32.const $push3=, hidden_global_array{{$}} +; PIC-NEXT: i32.add $push1=, $pop2, $pop3{{$}} +; PIC-NEXT: i32.const $push4=, 20{{$}} +; PIC-NEXT: i32.add $push5=, $pop1, $pop4{{$}} +; PIC-NEXT: i32.load $push0=, 0($pop5){{$}} +; PIC-NEXT: end_function + %1 = getelementptr [10 x i32], [10 x i32]* @hidden_global_array, i32 0, i32 5 + %2 = load i32, i32* %1 + ret i32 %2 +} + +; Store to a hidden global + +define void @store_hidden_global(i32 %n) { +; PIC-LABEL: store_hidden_global: +; PIC: global.get $push1=, __memory_base{{$}} +; PIC-NEXT: i32.const $push2=, hidden_global{{$}} +; PIC-NEXT: i32.add $push0=, $pop1, $pop2{{$}} +; PIC-NEXT: i32.store 0($pop0), $0{{$}} +; PIC-NEXT: end_function + +; NON-PIC-LABEL: store_hidden_global: +; NON-PIC: i32.const $push0=, 0{{$}} +; NON-PIC-NEXT: i32.store hidden_global($pop0), $0{{$}} +; NON-PIC-NEXT: end_function + + store i32 %n, i32* @hidden_global + ret void +} + +define void @store_hidden_global_offset(i32 %n) { +; PIC-LABEL: store_hidden_global_offset: +; PIC: global.get $push1=, __memory_base{{$}} +; PIC-NEXT: i32.const $push2=, hidden_global_array{{$}} +; PIC-NEXT: i32.add $push0=, $pop1, $pop2{{$}} +; PIC-NEXT: i32.const $push3=, 20{{$}} +; PIC-NEXT: i32.add $push4=, $pop0, $pop3{{$}} +; PIC-NEXT: i32.store 0($pop4), $0{{$}} +; PIC-NEXT: end_function + %1 = getelementptr [10 x i32], [10 x i32]* @hidden_global_array, i32 0, i32 5 + store i32 %n, i32* %1 + ret void +} + +; For non-hidden globals PIC code has to load the address from a wasm global +; using the @GOT relocation type. + + +define i32 @load_external_global() { +; PIC-LABEL: load_external_global: +; PIC: global.get $push0=, external_global@GOT{{$}} +; PIC-NEXT: i32.load $push1=, 0($pop0){{$}} +; PIC-NEXT: end_function + +; NON-PIC-LABEL: load_external_global: +; NON-PIC: i32.const $push0=, 0{{$}} +; NON-PIC-NEXT: i32.load $push1=, external_global($pop0){{$}} +; NON-PIC-NEXT: end_function + + %1 = load i32, i32* @external_global + ret i32 %1 +} + +define i32 @load_external_global_offset() { +; PIC-LABEL: load_external_global_offset: +; PIC: global.get $push1=, external_global_array@GOT{{$}} +; PIC-NEXT: i32.const $push2=, 20{{$}} +; PIC-NEXT: i32.add $push3=, $pop1, $pop2{{$}} +; PIC-NEXT: i32.load $push0=, 0($pop3){{$}} +; PIC-NEXT: end_function + %1 = getelementptr [10 x i32], [10 x i32]* @external_global_array, i32 0, i32 5 + %2 = load i32, i32* %1 + ret i32 %2 +} + +; Store to a non-hidden global via the wasm global. + +define void @store_external_global(i32 %n) { +; PIC-LABEL: store_external_global: +; PIC: global.get $push0=, external_global@GOT{{$}} +; PIC-NEXT: i32.store 0($pop0), $0{{$}} +; PIC-NEXT: end_function + +; NON-PIC-LABEL: store_external_global: +; NON-PIC: i32.const $push0=, 0{{$}} +; NON-PIC-NEXT: i32.store external_global($pop0), $0{{$}} +; NON-PIC-NEXT: end_function + + store i32 %n, i32* @external_global + ret void +} + +define void @store_external_global_offset(i32 %n) { +; PIC-LABEL: store_external_global_offset: +; PIC: global.get $push0=, external_global_array@GOT{{$}} +; PIC-NEXT: i32.const $push1=, 20{{$}} +; PIC-NEXT: i32.add $push2=, $pop0, $pop1{{$}} +; PIC-NEXT: i32.store 0($pop2), $0{{$}} +; PIC-NEXT: end_function + +; NON-PIC-LABEL: store_external_global_offset: +; NON-PIC: i32.const $push0=, 0{{$}} +; NON-PIC-NEXT: i32.store external_global_array+20($pop0), $0{{$}} +; NON-PIC-NEXT: end_function + + %1 = getelementptr [10 x i32], [10 x i32]* @external_global_array, i32 0, i32 5 + store i32 %n, i32* %1 + ret void +} + +; PIC: .globaltype __memory_base, i32