diff --git a/llvm/lib/Analysis/MemorySSAUpdater.cpp b/llvm/lib/Analysis/MemorySSAUpdater.cpp --- a/llvm/lib/Analysis/MemorySSAUpdater.cpp +++ b/llvm/lib/Analysis/MemorySSAUpdater.cpp @@ -342,6 +342,8 @@ SmallVector FixupList(InsertedPHIs.begin(), InsertedPHIs.end()); + SmallSet ExistingPhis; + // Remember the index where we may insert new phis. unsigned NewPhiIndex = InsertedPHIs.size(); if (!DefBeforeSameBlock) { @@ -382,6 +384,8 @@ if (!MPhi) { MPhi = MSSA->createMemoryPhi(BBIDF); NewInsertedPHIs.push_back(MPhi); + } else { + ExistingPhis.insert(MPhi); } // Add the phis created into the IDF blocks to NonOptPhis, so they are not // optimized out as trivial by the call to getPreviousDefFromEnd below. @@ -447,6 +451,13 @@ if (Phi) MSSA->renamePass(Phi->getBlock(), nullptr, Visited); } + // Existing Phi blocks may need renaming too, if an access was previously + // optimized and the inserted Defs "covers" the Optimized value. + for (auto &MP : ExistingPhis) { + MemoryPhi *Phi = dyn_cast_or_null(MP); + if (Phi) + MSSA->renamePass(Phi->getBlock(), nullptr, Visited); + } } } @@ -1322,6 +1333,7 @@ // Note: We assume MemorySSA is not used in metadata since it's not really // part of the IR. + assert(NewDefTarget != MA && "Going into an infinite loop"); while (!MA->use_empty()) { Use &U = *MA->use_begin(); if (auto *MUD = dyn_cast(U.getUser())) diff --git a/llvm/test/Analysis/MemorySSA/pr45927.ll b/llvm/test/Analysis/MemorySSA/pr45927.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/MemorySSA/pr45927.ll @@ -0,0 +1,73 @@ +; RUN: opt -disable-output -loop-simplify -lcssa -licm -print-memoryssa < %s 2>&1 | FileCheck %s +; RUN: opt -disable-output -aa-pipeline=basic-aa -passes='loop-mssa(licm),print' < %s 2>&1 | FileCheck %s + + +@a = external dso_local global i16, align 1 +@c = external dso_local global i16, align 1 + +; CHECK-LABEL: @main() + +; CHECK: entry: +; CHECK-NEXT: %res.addr.i = alloca i16 +; CHECK-NEXT: ; MemoryUse(liveOnEntry) +; CHECK-NEXT: %c.promoted = load i16, i16* @c +; CHECK-NEXT: br label %for.cond.i + +; CHECK: for.cond.i: +; CHECK-NEXT: ; [[NO5:.*]] = MemoryPhi({entry,liveOnEntry},{f.exit.i,[[NO5]]}) +; CHECK-NEXT: %inc.i1 = phi i16 [ %inc.i, %f.exit.i ], [ %c.promoted, %entry ] +; CHECK-NEXT: %inc.i = add nsw i16 %inc.i1, 1 +; CHECK-NEXT: br i1 false, label %f.exit.thread.i, label %f.exit.i + +; CHECK: f.exit.thread.i: +; CHECK-NEXT: %inc.i.lcssa = phi i16 [ %inc.i, %for.cond.i ] +; CHECK-NEXT: ; [[NO6:.*]] = MemoryDef([[NO5]]) +; CHECK-NEXT: store i16 %inc.i.lcssa, i16* @c, align 1 +; CHECK-NEXT: ; [[NO2:.*]] = MemoryDef([[NO6]]) +; CHECK-NEXT: store i16 1, i16* @a, align 1 +; CHECK-NEXT: ; MemoryUse([[NO2]]) +; CHECK-NEXT: %tmp2 = load i16, i16* @c, align 1 +; CHECK-NEXT: br label %g.exit + +; CHECK: f.exit.i +; CHECK-NEXT: br i1 false, label %g.exit.loopexit, label %for.cond.i + +; CHECK: g.exit.loopexit: +; CHECK-NEXT: %inc.i.lcssa2 = phi i16 [ %inc.i, %f.exit.i ] +; CHECK-NEXT: ; [[NO7:.*]] = MemoryDef([[NO5]]) +; CHECK-NEXT: store i16 %inc.i.lcssa2, i16* @c, align 1 +; CHECK-NEXT: br label %g.exit + +; CHECK: g.exit +; CHECK-NEXT: ; [[NO4:.*]] = MemoryPhi({f.exit.thread.i,[[NO2]]},{g.exit.loopexit,[[NO7]]}) +; CHECK-NEXT: ; MemoryUse([[NO4]]) +; CHECK-NEXT: %tmp1 = load i16, i16* @c, align 1 +; CHECK-NEXT: ; [[NO3:.*]] = MemoryDef([[NO4]]) +; CHECK-NEXT: store i16 %tmp1, i16* %res.addr.i, align 1 +; CHECK-NEXT: ret void + +define dso_local void @main() { +entry: + %res.addr.i = alloca i16, align 1 + br label %for.cond.i + +for.cond.i: ; preds = %f.exit.i, %entry + %tmp0 = load i16, i16* @c, align 1 + %inc.i = add nsw i16 %tmp0, 1 + store i16 %inc.i, i16* @c, align 1 + br i1 false, label %f.exit.thread.i, label %f.exit.i + +f.exit.thread.i: ; preds = %for.cond.i + store i16 1, i16* @a, align 1 + %tmp2 = load i16, i16* @c, align 1 + br label %g.exit + +f.exit.i: ; preds = %for.cond.i + br i1 false, label %g.exit, label %for.cond.i + +g.exit: ; preds = %f.exit.i, %f.exit.thread.i + %tmp1 = load i16, i16* @c, align 1 + store i16 %tmp1, i16* %res.addr.i, align 1 + ret void +} +