From 8e2a5bd235a0e08b5a7d1a621aaaaabedb2bcbbe Mon Sep 17 00:00:00 2001 From: Simon Dardis Date: Mon, 13 Nov 2017 11:47:21 +0000 Subject: [CodeGenPrepare] Check that erased sunken address are not reused CodeGenPrepare sinks address computations from one basic block to another and attempts to reuse address computations that have already been sunk. If the same address computation appears twice with the first instance as an operand of a load whose result is an operand to a simplifable select, CodeGenPrepare simplifies the select and recursively erases the now dead instructions. CodeGenPrepare then attempts to use the erased address computation for the second load. Fix this by erasing the cached address value if it has zero uses before looking for the address value in the sunken address map. This partially resolves PR35209. Thanks to Alexander Richardson for reporting the issue! Reviewers: john.brawn Differential Revision: https://reviews.llvm.org/D39841 llvm-svn: 318032 --- llvm/lib/CodeGen/CodeGenPrepare.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'llvm/lib/CodeGen/CodeGenPrepare.cpp') diff --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp index d6633a508f5..96b7704a765 100644 --- a/llvm/lib/CodeGen/CodeGenPrepare.cpp +++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp @@ -4358,7 +4358,12 @@ bool CodeGenPrepare::optimizeMemoryInst(Instruction *MemoryInst, Value *Addr, // Now that we determined the addressing expression we want to use and know // that we have to sink it into this block. Check to see if we have already // done this for some other load/store instr in this block. If so, reuse the - // computation. + // computation. Before attempting reuse, check if the address is valid as it + // may have been erased. + auto I = SunkAddrs.find(Addr); + if (I != SunkAddrs.end() && I->second && I->second->user_empty()) + SunkAddrs.erase(I); + Value *&SunkAddr = SunkAddrs[Addr]; if (SunkAddr) { DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for " -- cgit v1.2.3