From 0212dc27edd48a72fab962594fe894b5b3e02af6 Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Sun, 21 Apr 2013 17:48:39 +0000 Subject: [PATCH] SROA: Don't crash on a select with two identical operands. This is an edge case that can happen if we modify a chain of multiple selects. Update all operands in that case and remove the assert. PR15805. llvm-svn: 179982 --- llvm/lib/Transforms/Scalar/SROA.cpp | 16 ++++++++-------- llvm/test/Transforms/SROA/basictest.ll | 11 +++++++++++ 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp index 1e18873950d3..c383e2f8ed85 100644 --- a/llvm/lib/Transforms/Scalar/SROA.cpp +++ b/llvm/lib/Transforms/Scalar/SROA.cpp @@ -3051,16 +3051,16 @@ private: bool visitSelectInst(SelectInst &SI) { DEBUG(dbgs() << " original: " << SI << "\n"); - - // Find the operand we need to rewrite here. - bool IsTrueVal = SI.getTrueValue() == OldPtr; - if (IsTrueVal) - assert(SI.getFalseValue() != OldPtr && "Pointer is both operands!"); - else - assert(SI.getFalseValue() == OldPtr && "Pointer isn't an operand!"); + assert((SI.getTrueValue() == OldPtr || SI.getFalseValue() == OldPtr) && + "Pointer isn't an operand!"); Value *NewPtr = getAdjustedAllocaPtr(IRB, OldPtr->getType()); - SI.setOperand(IsTrueVal ? 1 : 2, NewPtr); + // Replace the operands which were using the old pointer. + if (SI.getOperand(1) == OldPtr) + SI.setOperand(1, NewPtr); + if (SI.getOperand(2) == OldPtr) + SI.setOperand(2, NewPtr); + DEBUG(dbgs() << " to: " << SI << "\n"); deleteIfTriviallyDead(OldPtr); return false; diff --git a/llvm/test/Transforms/SROA/basictest.ll b/llvm/test/Transforms/SROA/basictest.ll index 6078adead036..834032246f9a 100644 --- a/llvm/test/Transforms/SROA/basictest.ll +++ b/llvm/test/Transforms/SROA/basictest.ll @@ -1306,3 +1306,14 @@ end: ; CHECK: ret void } +define void @PR15805(i1 %a, i1 %b) { +; CHECK: @PR15805 +; CHECK: select i1 undef, i64* %c, i64* %c +; CHECK: ret void + + %c = alloca i64, align 8 + %p.0.c = select i1 undef, i64* %c, i64* %c + %cond.in = select i1 undef, i64* %p.0.c, i64* %c + %cond = load i64* %cond.in, align 8 + ret void +}