aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-09-20 10:15:10 +0000
committerChris Lattner <sabre@nondot.org>2004-09-20 10:15:10 +0000
commit79f0c8e4ee48a6741c8eb6c88ab953ea00e04b88 (patch)
tree7ae6613a8a499ad2c7db012d2d80a87402c686d5 /lib
parentc804d530c5a7df78a792799897bc761a4ebf3a73 (diff)
downloadexternal_llvm-79f0c8e4ee48a6741c8eb6c88ab953ea00e04b88.zip
external_llvm-79f0c8e4ee48a6741c8eb6c88ab953ea00e04b88.tar.gz
external_llvm-79f0c8e4ee48a6741c8eb6c88ab953ea00e04b88.tar.bz2
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
Diffstat (limited to 'lib')
-rw-r--r--lib/Transforms/Scalar/InstructionCombining.cpp26
1 files changed, 19 insertions, 7 deletions
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<PHINode>(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<PHINode>(I); --I)
+ if (isa<StoreInst>(I) || isa<CallInst>(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());