Index: llvm/trunk/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp =================================================================== --- llvm/trunk/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp +++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp @@ -636,15 +636,12 @@ SSAUpdater SSA; for (BasicBlock &BB : F) { for (Instruction &I : BB) { + SSA.Initialize(I.getType(), I.getName()); + SSA.AddAvailableValue(&BB, &I); for (auto UI = I.use_begin(), UE = I.use_end(); UI != UE;) { Use &U = *UI; ++UI; - SSA.Initialize(I.getType(), I.getName()); - SSA.AddAvailableValue(&BB, &I); auto *User = cast(U.getUser()); - if (User->getParent() == &BB) - continue; - if (auto *UserPN = dyn_cast(User)) if (UserPN->getIncomingBlock(U) == &BB) continue; Index: llvm/trunk/test/CodeGen/WebAssembly/lower-em-sjlj.ll =================================================================== --- llvm/trunk/test/CodeGen/WebAssembly/lower-em-sjlj.ll +++ llvm/trunk/test/CodeGen/WebAssembly/lower-em-sjlj.ll @@ -226,6 +226,26 @@ unreachable } +; Tests if SSA rewrite works when a use and its def are within the same BB. +define void @ssa_rewite_in_same_bb() { +entry: + call void @foo() + br label %for.cond + +for.cond: ; preds = %for.inc, %entry + ; CHECK: %{{.*}} = phi i32 [ %var[[VARNO:.*]], %for.inc.split ] + %0 = phi i32 [ %var, %for.inc ], [ undef, %entry ] + %var = add i32 0, 0 + br label %for.inc + +for.inc: ; preds = %for.cond + %call5 = call i32 @setjmp(%struct.__jmp_buf_tag* undef) #0 + br label %for.cond + +; CHECK: for.inc.split: + ; CHECK: %var[[VARNO]] = phi i32 [ %var, %for.inc ] +} + declare void @foo() ; Function Attrs: returns_twice declare i32 @setjmp(%struct.__jmp_buf_tag*) #0