From ec128ace8a4d7fdcdd9ffc10a94facbbda518c87 Mon Sep 17 00:00:00 2001 From: Alexey Bataev Date: Mon, 11 Dec 2017 19:11:16 +0000 Subject: [InstCombine] Fix PR35618: Instcombine hangs on single minmax load bitcast. Summary: If we have pattern `store (load(bitcast(select (cmp(V1, V2), &V1, &V2)))), bitcast)`, but the load is used in other instructions, it leads to looping in InstCombiner. Patch adds additional check that all users of the load instructions are stores and then replaces all uses of load instruction by the new one with new type. Reviewers: RKSimon, spatel, majnemer Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D41072 llvm-svn: 320407 --- .../InstCombine/InstCombineLoadStoreAlloca.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'llvm/lib/Transforms') diff --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp index 29e0a091aa7..a729981a549 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -1344,9 +1344,24 @@ bool removeBitcastsFromLoadStoreOnMinMax(InstCombiner &IC, StoreInst &SI) { if (!isMinMaxWithLoads(LoadAddr)) return false; + if (!all_of(LI->users(), [LI](User *U) { + auto *SI = dyn_cast(U); + return SI && SI->getPointerOperand() != LI && + !SI->getPointerOperand()->isSwiftError(); + })) + return false; + + IC.Builder.SetInsertPoint(LI); LoadInst *NewLI = combineLoadToNewType( IC, *LI, LoadAddr->getType()->getPointerElementType()); - combineStoreToNewValue(IC, SI, NewLI); + // Replace all the stores with stores of the newly loaded value. + for (auto *UI : LI->users()) { + auto *SI = cast(UI); + IC.Builder.SetInsertPoint(SI); + combineStoreToNewValue(IC, *SI, NewLI); + IC.eraseInstFromFunction(*SI); + } + IC.eraseInstFromFunction(*LI); return true; } @@ -1375,7 +1390,7 @@ Instruction *InstCombiner::visitStoreInst(StoreInst &SI) { return eraseInstFromFunction(SI); if (removeBitcastsFromLoadStoreOnMinMax(*this, SI)) - return eraseInstFromFunction(SI); + return nullptr; // Replace GEP indices if possible. if (Instruction *NewGEPI = replaceGEPIdxWithZero(*this, Ptr, SI)) { -- cgit v1.2.3