-Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
- const Type *Ty = SE.getEffectiveSCEVType(S->getType());
-
- // Collect all the add operands in a loop, along with their associated loops.
- // Iterate in reverse so that constants are emitted last, all else equal, and
- // so that pointer operands are inserted first, which the code below relies on
- // to form more involved GEPs.
- SmallVector<std::pair<const Loop *, const SCEV *>, 8> OpsAndLoops;
- for (std::reverse_iterator<SCEVAddExpr::op_iterator> I(S->op_end()),
- E(S->op_begin()); I != E; ++I)
- OpsAndLoops.push_back(std::make_pair(GetRelevantLoop(*I, *SE.LI, *SE.DT),
- *I));
-
- // Sort by loop. Use a stable sort so that constants follow non-constants and
- // pointer operands precede non-pointer operands.
- std::stable_sort(OpsAndLoops.begin(), OpsAndLoops.end(), LoopCompare(*SE.DT));
-
- // Emit instructions to add all the operands. Hoist as much as possible
- // out of loops, and form meaningful getelementptrs where possible.
- Value *Sum = 0;
- for (SmallVectorImpl<std::pair<const Loop *, const SCEV *> >::iterator
- I = OpsAndLoops.begin(), E = OpsAndLoops.end(); I != E; ) {
- const Loop *CurLoop = I->first;
- const SCEV *Op = I->second;
- if (!Sum) {
- // This is the first operand. Just expand it.
- Sum = expand(Op);
- ++I;
- } else if (const PointerType *PTy = dyn_cast<PointerType>(Sum->getType())) {
- // The running sum expression is a pointer. Try to form a getelementptr
- // at this level with that as the base.
- SmallVector<const SCEV *, 4> NewOps;
- for (; I != E && I->first == CurLoop; ++I)
- NewOps.push_back(I->second);
- Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, Sum);
- } else if (const PointerType *PTy = dyn_cast<PointerType>(Op->getType())) {
- // The running sum is an integer, and there's a pointer at this level.
- // Try to form a getelementptr.
- SmallVector<const SCEV *, 4> NewOps;
- NewOps.push_back(SE.getUnknown(Sum));
- for (++I; I != E && I->first == CurLoop; ++I)
- NewOps.push_back(I->second);
- Sum = expandAddToGEP(NewOps.begin(), NewOps.end(), PTy, Ty, expand(Op));
- } else if (isNonConstantNegative(Op)) {
- // Instead of doing a negate and add, just do a subtract.