Index: llvm/trunk/lib/Target/WebAssembly/WebAssemblyFastISel.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/WebAssemblyFastISel.cpp +++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyFastISel.cpp @@ -63,12 +63,16 @@ public: // Innocuous defaults for our address. Address() : Kind(RegBase), Offset(0), GV(0) { Base.Reg = 0; } - void setKind(BaseKind K) { Kind = K; } + void setKind(BaseKind K) { + assert(!isSet() && "Can't change kind with non-zero base"); + Kind = K; + } BaseKind getKind() const { return Kind; } bool isRegBase() const { return Kind == RegBase; } bool isFIBase() const { return Kind == FrameIndexBase; } void setReg(unsigned Reg) { assert(isRegBase() && "Invalid base register access!"); + assert(Base.Reg == 0 && "Overwriting non-zero register"); Base.Reg = Reg; } unsigned getReg() const { @@ -77,6 +81,7 @@ } void setFI(unsigned FI) { assert(isFIBase() && "Invalid base frame index access!"); + assert(Base.FI == 0 && "Overwriting non-zero frame index"); Base.FI = FI; } unsigned getFI() const { @@ -91,6 +96,13 @@ int64_t getOffset() const { return Offset; } void setGlobalValue(const GlobalValue *G) { GV = G; } const GlobalValue *getGlobalValue() const { return GV; } + bool isSet() const { + if (isRegBase()) { + return Base.Reg != 0; + } else { + return Base.FI != 0; + } + } }; /// Keep a pointer to the WebAssemblySubtarget around so that we can make the @@ -297,6 +309,9 @@ DenseMap::iterator SI = FuncInfo.StaticAllocaMap.find(AI); if (SI != FuncInfo.StaticAllocaMap.end()) { + if (Addr.isSet()) { + return false; + } Addr.setKind(Address::FrameIndexBase); Addr.setFI(SI->second); return true; @@ -341,6 +356,9 @@ break; } } + if (Addr.isSet()) { + return false; + } Addr.setReg(getRegForValue(Obj)); return Addr.getReg() != 0; } Index: llvm/trunk/test/CodeGen/WebAssembly/offset-fastisel.ll =================================================================== --- llvm/trunk/test/CodeGen/WebAssembly/offset-fastisel.ll +++ llvm/trunk/test/CodeGen/WebAssembly/offset-fastisel.ll @@ -0,0 +1,100 @@ +; RUN: llc < %s -asm-verbose=false -disable-wasm-explicit-locals -fast-isel -fast-isel-abort=1 | FileCheck %s + +; TODO: Merge this with offset.ll when fast-isel matches better. + +target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128" +target triple = "wasm32-unknown-unknown-elf" + +; CHECK-LABEL: store_i8_with_variable_gep_offset: +; CHECK: i32.add $push[[L0:[0-9]+]]=, $0, $1{{$}} +; CHECK: i32.const $push[[L1:[0-9]+]]=, 0{{$}} +; CHECK: i32.store8 0($pop[[L0]]), $pop[[L1]]{{$}} +define void @store_i8_with_variable_gep_offset(i8* %p, i32 %idx) { + %s = getelementptr inbounds i8, i8* %p, i32 %idx + store i8 0, i8* %s + ret void +} + +; CHECK-LABEL: store_i8_with_array_alloca_gep: +; CHECK: i32.const $push[[L0:[0-9]+]]=, 0{{$}} +; CHECK: i32.load $push[[L1:[0-9]+]]=, __stack_pointer($pop[[L0]]){{$}} +; CHECK: i32.const $push[[L2:[0-9]+]]=, 32{{$}} +; CHECK: i32.sub $push{{[0-9]+}}=, $pop[[L1]], $pop[[L2]]{{$}} +; CHECK: i32.add $push[[L4:[0-9]+]]=, $pop{{[0-9]+}}, $0{{$}} +; CHECK: i32.const $push[[L5:[0-9]+]]=, 0{{$}} +; CHECK: i32.store8 0($pop[[L4]]), $pop[[L5]]{{$}} +define hidden void @store_i8_with_array_alloca_gep(i32 %idx) { + %A = alloca [30 x i8], align 16 + %s = getelementptr inbounds [30 x i8], [30 x i8]* %A, i32 0, i32 %idx + store i8 0, i8* %s, align 1 + ret void +} + +; CHECK-LABEL: store_i32_with_unfolded_gep_offset: +; CHECK: i32.const $push[[L0:[0-9]+]]=, 24{{$}} +; CHECK: i32.add $push[[L1:[0-9]+]]=, $0, $pop[[L0]]{{$}} +; CHECK: i32.const $push[[L2:[0-9]+]]=, 0{{$}} +; CHECK: i32.store 0($pop[[L1]]), $pop[[L2]]{{$}} +define void @store_i32_with_unfolded_gep_offset(i32* %p) { + %s = getelementptr i32, i32* %p, i32 6 + store i32 0, i32* %s + ret void +} + +; CHECK-LABEL: store_i32_with_folded_gep_offset: +; CHECK: i32.store 24($0), $pop{{[0-9]+$}} +define void @store_i32_with_folded_gep_offset(i32* %p) { + %s = getelementptr inbounds i32, i32* %p, i32 6 + store i32 0, i32* %s + ret void +} + +; CHECK-LABEL: load_i32_with_folded_gep_offset: +; CHECK: i32.load $push{{[0-9]+}}=, 24($0){{$}} +define i32 @load_i32_with_folded_gep_offset(i32* %p) { + %s = getelementptr inbounds i32, i32* %p, i32 6 + %t = load i32, i32* %s + ret i32 %t +} + +; CHECK-LABEL: store_i64_with_unfolded_gep_offset: +; CHECK: i32.const $push[[L0:[0-9]+]]=, 24{{$}} +; CHECK: i32.add $push[[L1:[0-9]+]]=, $0, $pop[[L0]]{{$}} +; CHECK: i64.const $push[[L2:[0-9]+]]=, 0{{$}} +; CHECK: i64.store 0($pop[[L1]]), $pop[[L2]]{{$}} +define void @store_i64_with_unfolded_gep_offset(i64* %p) { + %s = getelementptr i64, i64* %p, i32 3 + store i64 0, i64* %s + ret void +} + +; CHECK-LABEL: store_i8_with_folded_gep_offset: +; CHECK: i32.store8 24($0), $pop{{[0-9]+$}} +define void @store_i8_with_folded_gep_offset(i8* %p) { + %s = getelementptr inbounds i8, i8* %p, i32 24 + store i8 0, i8* %s + ret void +} + +; CHECK-LABEL: load_i8_u_with_folded_offset: +; CHECK: i32.load8_u $push{{[0-9]+}}=, 24($0){{$}} +define i32 @load_i8_u_with_folded_offset(i8* %p) { + %q = ptrtoint i8* %p to i32 + %r = add nuw i32 %q, 24 + %s = inttoptr i32 %r to i8* + %t = load i8, i8* %s + %u = zext i8 %t to i32 + ret i32 %u +} + +; TODO: this should be load8_s, need to fold sign-/zero-extend in fast-isel +; CHECK-LABEL: load_i8_s_with_folded_offset: +; CHECK: i32.load8_u $push{{[0-9]+}}=, 24($0){{$}} +define i32 @load_i8_s_with_folded_offset(i8* %p) { + %q = ptrtoint i8* %p to i32 + %r = add nuw i32 %q, 24 + %s = inttoptr i32 %r to i8* + %t = load i8, i8* %s + %u = sext i8 %t to i32 + ret i32 %u +}