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

Chris Lattner lattner at cs.uiuc.edu
Sat Feb 21 23:29:01 PST 2004


Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.155 -> 1.156
TailDuplication.cpp updated: 1.12 -> 1.13

---
Log message:

Implement Transforms/InstCombine/cast.ll:test13, a case which occurs in a
hot 164.gzip loop.


---
Diffs of the changes:  (+35 -4)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.155 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.156
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.155	Sun Feb 15 23:07:08 2004
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Sat Feb 21 23:25:17 2004
@@ -2004,9 +2004,14 @@
 Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
   // Is it 'getelementptr %P, long 0'  or 'getelementptr %P'
   // If so, eliminate the noop.
-  if ((GEP.getNumOperands() == 2 &&
-       GEP.getOperand(1) == Constant::getNullValue(Type::LongTy)) ||
-      GEP.getNumOperands() == 1)
+  if (GEP.getNumOperands() == 1)
+    return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
+
+  bool HasZeroPointerIndex = false;
+  if (Constant *C = dyn_cast<Constant>(GEP.getOperand(1)))
+    HasZeroPointerIndex = C->isNullValue();
+
+  if (GEP.getNumOperands() == 2 && HasZeroPointerIndex)
     return ReplaceInstUsesWith(GEP, GEP.getOperand(0));
 
   // Combine Indices - If the source pointer to this getelementptr instruction
@@ -2072,6 +2077,31 @@
 
       // Replace all uses of the GEP with the new constexpr...
       return ReplaceInstUsesWith(GEP, CE);
+    }
+  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEP.getOperand(0))) {
+    if (CE->getOpcode() == Instruction::Cast) {
+      if (HasZeroPointerIndex) {
+        // transform: GEP (cast [10 x ubyte]* X to [0 x ubyte]*), long 0, ...
+        // into     : GEP [10 x ubyte]* X, long 0, ...
+        //
+        // This occurs when the program declares an array extern like "int X[];"
+        //
+        Constant *X = CE->getOperand(0);
+        const PointerType *CPTy = cast<PointerType>(CE->getType());
+        if (const PointerType *XTy = dyn_cast<PointerType>(X->getType()))
+          if (const ArrayType *XATy =
+              dyn_cast<ArrayType>(XTy->getElementType()))
+            if (const ArrayType *CATy =
+                dyn_cast<ArrayType>(CPTy->getElementType()))
+              if (CATy->getElementType() == XATy->getElementType()) {
+                // At this point, we know that the cast source type is a pointer
+                // to an array of the same type as the destination pointer
+                // array.  Because the array type is never stepped over (there
+                // is a leading zero) we can fold the cast into this GEP.
+                GEP.setOperand(0, X);
+                return &GEP;
+              }
+      }
     }
   }
 


Index: llvm/lib/Transforms/Scalar/TailDuplication.cpp
diff -u llvm/lib/Transforms/Scalar/TailDuplication.cpp:1.12 llvm/lib/Transforms/Scalar/TailDuplication.cpp:1.13
--- llvm/lib/Transforms/Scalar/TailDuplication.cpp:1.12	Sun Feb  1 00:32:28 2004
+++ llvm/lib/Transforms/Scalar/TailDuplication.cpp	Sat Feb 21 23:25:17 2004
@@ -126,7 +126,8 @@
   // Basically, we refuse to make the transformation if any of the values
   // computed in the 'tail' are used in any other basic blocks.
   BasicBlock *Tail = TI->getSuccessor(0);
-
+  assert(isa<BranchInst>(TI) && cast<BranchInst>(TI)->isUnconditional());
+  
   for (BasicBlock::iterator I = Tail->begin(), E = Tail->end(); I != E; ++I)
     for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;
          ++UI) {





More information about the llvm-commits mailing list