[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Chris Lattner lattner at cs.uiuc.edu
Mon Sep 20 03:15:21 PDT 2004



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.243 -> 1.244
---
Log message:

Fix potential miscompilations: InstCombine/2004-09-20-BadLoadCombine*.llx


---
Diffs of the changes:  (+19 -7)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.243 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.244
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.243	Mon Sep 20 01:42:58 2004
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Mon Sep 20 05:15:10 2004
@@ -3064,21 +3064,33 @@
       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());






More information about the llvm-commits mailing list