@@ -286,6 +286,69 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
286
286
return BO;
287
287
}
288
288
289
+ // / SimplifyAddOperands - Sort and simplify a list of add operands. NumAddRecs
290
+ // / is the number of SCEVAddRecExprs present, which are kept at the end of
291
+ // / the list.
292
+ // /
293
+ static void SimplifyAddOperands (SmallVectorImpl<const SCEV *> &Ops,
294
+ Type *Ty,
295
+ ScalarEvolution &SE) {
296
+ unsigned NumAddRecs = 0 ;
297
+ for (unsigned i = Ops.size (); i > 0 && isa<SCEVAddRecExpr>(Ops[i-1 ]); --i)
298
+ ++NumAddRecs;
299
+ // Group Ops into non-addrecs and addrecs.
300
+ SmallVector<const SCEV *, 8 > NoAddRecs (Ops.begin (), Ops.end () - NumAddRecs);
301
+ SmallVector<const SCEV *, 8 > AddRecs (Ops.end () - NumAddRecs, Ops.end ());
302
+ // Let ScalarEvolution sort and simplify the non-addrecs list.
303
+ const SCEV *Sum = NoAddRecs.empty () ?
304
+ SE.getConstant (Ty, 0 ) :
305
+ SE.getAddExpr (NoAddRecs);
306
+ // If it returned an add, use the operands. Otherwise it simplified
307
+ // the sum into a single value, so just use that.
308
+ Ops.clear ();
309
+ if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Sum))
310
+ append_range (Ops, Add->operands ());
311
+ else if (!Sum->isZero ())
312
+ Ops.push_back (Sum);
313
+ // Then append the addrecs.
314
+ Ops.append (AddRecs.begin (), AddRecs.end ());
315
+ }
316
+
317
+ // / SplitAddRecs - Flatten a list of add operands, moving addrec start values
318
+ // / out to the top level. For example, convert {a + b,+,c} to a, b, {0,+,d}.
319
+ // / This helps expose more opportunities for folding parts of the expressions
320
+ // / into GEP indices.
321
+ // /
322
+ static void SplitAddRecs (SmallVectorImpl<const SCEV *> &Ops,
323
+ Type *Ty,
324
+ ScalarEvolution &SE) {
325
+ // Find the addrecs.
326
+ SmallVector<const SCEV *, 8 > AddRecs;
327
+ for (unsigned i = 0 , e = Ops.size (); i != e; ++i)
328
+ while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i])) {
329
+ const SCEV *Start = A->getStart ();
330
+ if (Start->isZero ()) break ;
331
+ const SCEV *Zero = SE.getConstant (Ty, 0 );
332
+ AddRecs.push_back (SE.getAddRecExpr (Zero,
333
+ A->getStepRecurrence (SE),
334
+ A->getLoop (),
335
+ A->getNoWrapFlags (SCEV::FlagNW)));
336
+ if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Start)) {
337
+ Ops[i] = Zero;
338
+ append_range (Ops, Add->operands ());
339
+ e += Add->getNumOperands ();
340
+ } else {
341
+ Ops[i] = Start;
342
+ }
343
+ }
344
+ if (!AddRecs.empty ()) {
345
+ // Add the addrecs onto the end of the list.
346
+ Ops.append (AddRecs.begin (), AddRecs.end ());
347
+ // Resort the operand list, moving any constants to the front.
348
+ SimplifyAddOperands (Ops, Ty, SE);
349
+ }
350
+ }
351
+
289
352
// / expandAddToGEP - Expand an addition expression with a pointer type into
290
353
// / a GEP instead of using ptrtoint+arithmetic+inttoptr. This helps
291
354
// / BasicAliasAnalysis and other passes analyze the result. See the rules
@@ -313,11 +376,20 @@ Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
313
376
// / loop-invariant portions of expressions, after considering what
314
377
// / can be folded using target addressing modes.
315
378
// /
316
- Value *SCEVExpander::expandAddToGEP (const SCEV *Offset, Type *Ty, Value *V) {
379
+ Value *SCEVExpander::expandAddToGEP (const SCEV *const *op_begin,
380
+ const SCEV *const *op_end, Type *Ty,
381
+ Value *V) {
382
+ SmallVector<const SCEV *, 8 > Ops (op_begin, op_end);
383
+
384
+ // Split AddRecs up into parts as either of the parts may be usable
385
+ // without the other.
386
+ SplitAddRecs (Ops, Ty, SE);
387
+
317
388
assert (!isa<Instruction>(V) ||
318
389
SE.DT .dominates (cast<Instruction>(V), &*Builder.GetInsertPoint ()));
319
390
320
- Value *Idx = expandCodeForImpl (Offset, Ty);
391
+ // Expand the operands for a plain byte offset.
392
+ Value *Idx = expandCodeForImpl (SE.getAddExpr (Ops), Ty);
321
393
322
394
// Fold a GEP with constant operands.
323
395
if (Constant *CLHS = dyn_cast<Constant>(V))
@@ -362,6 +434,11 @@ Value *SCEVExpander::expandAddToGEP(const SCEV *Offset, Type *Ty, Value *V) {
362
434
return Builder.CreateGEP (Builder.getInt8Ty (), V, Idx, " scevgep" );
363
435
}
364
436
437
+ Value *SCEVExpander::expandAddToGEP (const SCEV *Op, Type *Ty, Value *V) {
438
+ const SCEV *const Ops[1 ] = {Op};
439
+ return expandAddToGEP (Ops, Ops + 1 , Ty, V);
440
+ }
441
+
365
442
// / PickMostRelevantLoop - Given two loops pick the one that's most relevant for
366
443
// / SCEV expansion. If they are nested, this is the most nested. If they are
367
444
// / neighboring, pick the later.
@@ -498,7 +575,7 @@ Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
498
575
X = SE.getSCEV (U->getValue ());
499
576
NewOps.push_back (X);
500
577
}
501
- Sum = expandAddToGEP (SE. getAddExpr ( NewOps), Ty, Sum);
578
+ Sum = expandAddToGEP (NewOps. begin (), NewOps. end ( ), Ty, Sum);
502
579
} else if (Op->isNonConstantNegative ()) {
503
580
// Instead of doing a negate and add, just do a subtract.
504
581
Value *W = expandCodeForImpl (SE.getNegativeSCEV (Op), Ty);
0 commit comments