From 79f0c8e4ee48a6741c8eb6c88ab953ea00e04b88 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 20 Sep 2004 10:15:10 +0000 Subject: Fix potential miscompilations: InstCombine/2004-09-20-BadLoadCombine*.llx git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@16447 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/InstructionCombining.cpp | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index d288886..f3ffcf2 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -3064,21 +3064,33 @@ Instruction *InstCombiner::visitLoadInst(LoadInst &LI) { if (isSafeToLoadUnconditionally(SI->getOperand(1), SI) && isSafeToLoadUnconditionally(SI->getOperand(2), SI)) { Value *V1 = InsertNewInstBefore(new LoadInst(SI->getOperand(1), - SI->getOperand(1)->getName()+".val"), *SI); + SI->getOperand(1)->getName()+".val"), LI); Value *V2 = InsertNewInstBefore(new LoadInst(SI->getOperand(2), - SI->getOperand(2)->getName()+".val"), *SI); + SI->getOperand(2)->getName()+".val"), LI); return new SelectInst(SI->getCondition(), V1, V2); } } else if (PHINode *PN = dyn_cast(Op)) { // load (phi (&V1, &V2, &V3)) --> phi(load &V1, load &V2, load &V3) - bool Safe = true; - for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) + bool Safe = PN->getParent() == LI.getParent(); + + // Scan all of the instructions between the PHI and the load to make + // sure there are no instructions that might possibly alter the value + // loaded from the PHI. + if (Safe) { + BasicBlock::iterator I = &LI; + for (--I; !isa(I); --I) + if (isa(I) || isa(I)) { + Safe = false; + break; + } + } + + for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e && Safe; ++i) if (!isSafeToLoadUnconditionally(PN->getIncomingValue(i), - PN->getIncomingBlock(i)->getTerminator())) { + PN->getIncomingBlock(i)->getTerminator())) Safe = false; - break; - } + if (Safe) { // Create the PHI. PHINode *NewPN = new PHINode(LI.getType(), PN->getName()); -- cgit v1.1