From: Chris Lattner Date: Wed, 5 Mar 2003 22:33:14 +0000 (+0000) Subject: Implement %test7 in InstCombine/getelementptr.ll X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=decd0812ecf93c142d287462444df0de915af845;p=oota-llvm.git Implement %test7 in InstCombine/getelementptr.ll git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5704 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index f652e2bbc71..890f1fb6051 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -807,14 +807,28 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) { std::vector Indices; // Can we combine the two pointer arithmetics offsets? - if (Src->getNumOperands() == 2 && isa(Src->getOperand(1)) && - isa(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(Src->getOperand(1)) + + if (Src->getNumOperands() == 2 && isa(Src->getOperand(1)) && + isa(GEP.getOperand(1))) { + // Replace: gep (gep %P, long C1), long C2, ... + // With: gep %P, long (C1+C2), ... + Value *Sum = *cast(Src->getOperand(1)) + *cast(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(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