Implement %test7 in InstCombine/getelementptr.ll
authorChris Lattner <sabre@nondot.org>
Wed, 5 Mar 2003 22:33:14 +0000 (22:33 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 5 Mar 2003 22:33:14 +0000 (22:33 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5704 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/InstructionCombining.cpp

index f652e2bbc712e606eab6163ca57bd6e846369871..890f1fb605182b52b80fa4a4cbec9341e15e2e40 100644 (file)
@@ -807,14 +807,28 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
     std::vector<Value *> Indices;
   
     // Can we combine the two pointer arithmetics offsets?
-    if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
-        isa<Constant>(GEP.getOperand(1))) {
-      // Replace the index list on this GEP with the index on the getelementptr
-      Indices.insert(Indices.end(), GEP.idx_begin(), GEP.idx_end());
-      Indices[0] = *cast<Constant>(Src->getOperand(1)) +
+     if (Src->getNumOperands() == 2 && isa<Constant>(Src->getOperand(1)) &&
+         isa<Constant>(GEP.getOperand(1))) {
+      // Replace: gep (gep %P, long C1), long C2, ...
+      // With:    gep %P, long (C1+C2), ...
+      Value *Sum = *cast<Constant>(Src->getOperand(1)) +
                    *cast<Constant>(GEP.getOperand(1));
-      assert(Indices[0] != 0 && "Constant folding of uint's failed!?");
-
+      assert(Sum && "Constant folding of longs failed!?");
+      GEP.setOperand(0, Src->getOperand(0));
+      GEP.setOperand(1, Sum);
+      AddUsesToWorkList(*Src);   // Reduce use count of Src
+      return &GEP;
+    } else if (Src->getNumOperands() == 2 && Src->use_size() == 1) {
+      // Replace: gep (gep %P, long B), long A, ...
+      // With:    T = long A+B; gep %P, T, ...
+      //
+      Value *Sum = BinaryOperator::create(Instruction::Add, Src->getOperand(1),
+                                          GEP.getOperand(1),
+                                          Src->getName()+".sum", &GEP);
+      GEP.setOperand(0, Src->getOperand(0));
+      GEP.setOperand(1, Sum);
+      WorkList.push_back(cast<Instruction>(Sum));
+      return &GEP;
     } else if (*GEP.idx_begin() == Constant::getNullValue(Type::LongTy) &&
                Src->getNumOperands() != 1) { 
       // Otherwise we can do the fold if the first index of the GEP is a zero