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

Chris Lattner lattner at cs.uiuc.edu
Thu Apr 8 15:40:37 PDT 2004


Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.181 -> 1.182

---
Log message:

Implement InstCombine/cast-propagate.ll


---
Diffs of the changes:  (+21 -0)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.181 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.182
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.181	Wed Apr  7 23:43:23 2004
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Thu Apr  8 15:39:49 2004
@@ -2639,6 +2639,27 @@
           if (GV->isConstant() && !GV->isExternal())
             if (Constant *V = GetGEPGlobalInitializer(GV->getInitializer(), CE))
               return ReplaceInstUsesWith(LI, V);
+
+  // load (cast X) --> cast (load X) iff safe
+  if (CastInst *CI = dyn_cast<CastInst>(Op)) {
+    const Type *DestPTy = cast<PointerType>(CI->getType())->getElementType();
+    if (const PointerType *SrcTy =
+        dyn_cast<PointerType>(CI->getOperand(0)->getType())) {
+      const Type *SrcPTy = SrcTy->getElementType();
+      if (TD->getTypeSize(SrcPTy) == TD->getTypeSize(DestPTy) &&
+          (SrcPTy->isInteger() || isa<PointerType>(SrcPTy)) &&
+          (DestPTy->isInteger() || isa<PointerType>(DestPTy))) {
+        // Okay, we are casting from one integer or pointer type to another of
+        // the same size.  Instead of casting the pointer before the load, cast
+        // the result of the loaded value.
+        Value *NewLoad = InsertNewInstBefore(new LoadInst(CI->getOperand(0),
+                                                          CI->getName()), LI);
+        // Now cast the result of the load.
+        return new CastInst(NewLoad, LI.getType());
+      }
+    }
+  }
+
   return 0;
 }
 





More information about the llvm-commits mailing list