llvm.org GIT mirror llvm / 1409d14
Merging r278413: ------------------------------------------------------------------------ r278413 | gberry | 2016-08-11 14:05:17 -0700 (Thu, 11 Aug 2016) | 17 lines [SCEV] Update interface to handle SCEVExpander insert point motion. Summary: This is an extension of the fix in r271424. That fix dealt with builder insert points being moved by SCEV expansion, but only for the lifetime of the expand call. This change modifies the interface so that LSR can safely call expand multiple times at the same insert point and do the right thing if one of the expansions decides to move the original insert point. This is a fix for PR28719. Reviewers: sanjoy Subscribers: llvm-commits, mcrosier, mzolotukhin Differential Revision: https://reviews.llvm.org/D23342 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_39@278424 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 4 years ago
4 changed file(s) with 73 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
195195 /// block.
196196 Value *expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I);
197197
198 /// \brief Insert code to directly compute the specified SCEV expression
199 /// into the program. The inserted code is inserted into the SCEVExpander's
200 /// current insertion point. If a type is specified, the result will be
201 /// expanded to have that type, with a cast if necessary.
202 Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr);
203
204
198205 /// \brief Generates a code sequence that evaluates this predicate.
199206 /// The inserted instructions will be at position \p Loc.
200207 /// The result will be of type i1 and will have a value of 0 when the
251258 void disableCanonicalMode() { CanonicalMode = false; }
252259
253260 void enableLSRMode() { LSRMode = true; }
261
262 /// \brief Set the current insertion point. This is useful if multiple calls
263 /// to expandCodeFor() are going to be made with the same insert point and
264 /// the insert point may be moved during one of the expansions (e.g. if the
265 /// insert point is not a block terminator).
266 void setInsertPoint(Instruction *IP) {
267 assert(IP);
268 Builder.SetInsertPoint(IP);
269 }
254270
255271 /// \brief Clear the current insertion point. This is useful if the
256272 /// instruction that had been serving as the insertion point may have been
312328
313329 Value *expand(const SCEV *S);
314330
315 /// \brief Insert code to directly compute the specified SCEV expression
316 /// into the program. The inserted code is inserted into the SCEVExpander's
317 /// current insertion point. If a type is specified, the result will be
318 /// expanded to have that type, with a cast if necessary.
319 Value *expandCodeFor(const SCEV *SH, Type *Ty = nullptr);
320
321331 /// \brief Determine the most "relevant" loop for the given SCEV.
322332 const Loop *getRelevantLoop(const SCEV *);
323333
16091609
16101610 Value *SCEVExpander::expandCodeFor(const SCEV *SH, Type *Ty,
16111611 Instruction *IP) {
1612 assert(IP);
1613 Builder.SetInsertPoint(IP);
1612 setInsertPoint(IP);
16141613 return expandCodeFor(SH, Ty);
16151614 }
16161615
44414441 // Determine an input position which will be dominated by the operands and
44424442 // which will dominate the result.
44434443 IP = AdjustInsertPositionForExpand(IP, LF, LU, Rewriter);
4444 Rewriter.setInsertPoint(&*IP);
44444445
44454446 // Inform the Rewriter if we have a post-increment use, so that it can
44464447 // perform an advantageous expansion.
44724473 LF.UserInst, LF.OperandValToReplace,
44734474 Loops, SE, DT);
44744475
4475 Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, nullptr, &*IP)));
4476 Ops.push_back(SE.getUnknown(Rewriter.expandCodeFor(Reg, nullptr)));
44764477 }
44774478
44784479 // Expand the ScaledReg portion.
44904491 // Expand ScaleReg as if it was part of the base regs.
44914492 if (F.Scale == 1)
44924493 Ops.push_back(
4493 SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr, &*IP)));
4494 SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr)));
44944495 else {
44954496 // An interesting way of "folding" with an icmp is to use a negated
44964497 // scale, which we'll implement by inserting it into the other operand
44974498 // of the icmp.
44984499 assert(F.Scale == -1 &&
44994500 "The only scale supported by ICmpZero uses is -1!");
4500 ICmpScaledV = Rewriter.expandCodeFor(ScaledS, nullptr, &*IP);
4501 ICmpScaledV = Rewriter.expandCodeFor(ScaledS, nullptr);
45014502 }
45024503 } else {
45034504 // Otherwise just expand the scaled register and an explicit scale,
45074508 // Unless the addressing mode will not be folded.
45084509 if (!Ops.empty() && LU.Kind == LSRUse::Address &&
45094510 isAMCompletelyFolded(TTI, LU, F)) {
4510 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, &*IP);
4511 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty);
45114512 Ops.clear();
45124513 Ops.push_back(SE.getUnknown(FullV));
45134514 }
4514 ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr, &*IP));
4515 ScaledS = SE.getUnknown(Rewriter.expandCodeFor(ScaledS, nullptr));
45154516 if (F.Scale != 1)
45164517 ScaledS =
45174518 SE.getMulExpr(ScaledS, SE.getConstant(ScaledS->getType(), F.Scale));
45234524 if (F.BaseGV) {
45244525 // Flush the operand list to suppress SCEVExpander hoisting.
45254526 if (!Ops.empty()) {
4526 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, &*IP);
4527 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty);
45274528 Ops.clear();
45284529 Ops.push_back(SE.getUnknown(FullV));
45294530 }
45334534 // Flush the operand list to suppress SCEVExpander hoisting of both folded and
45344535 // unfolded offsets. LSR assumes they both live next to their uses.
45354536 if (!Ops.empty()) {
4536 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty, &*IP);
4537 Value *FullV = Rewriter.expandCodeFor(SE.getAddExpr(Ops), Ty);
45374538 Ops.clear();
45384539 Ops.push_back(SE.getUnknown(FullV));
45394540 }
45694570 const SCEV *FullS = Ops.empty() ?
45704571 SE.getConstant(IntTy, 0) :
45714572 SE.getAddExpr(Ops);
4572 Value *FullV = Rewriter.expandCodeFor(FullS, Ty, &*IP);
4573 Value *FullV = Rewriter.expandCodeFor(FullS, Ty);
45734574
45744575 // We're done expanding now, so reset the rewriter.
45754576 Rewriter.clearPostInc();
0 ; RUN: opt < %s -loop-reduce -S | FileCheck %s
1
2 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
3 target triple = "x86_64-unknown-linux-gnu"
4
5 @a = global i32 0, align 4
6 @b = global i8 0, align 1
7 @c = global [4 x i8] zeroinitializer, align 1
8
9 ; Just make sure we don't generate code with uses not dominated by defs.
10 ; CHECK-LABEL: @main(
11 define i32 @main() {
12 entry:
13 %a0 = load i32, i32* @a, align 4
14 %cmpa = icmp slt i32 %a0, 4
15 br i1 %cmpa, label %preheader, label %for.end
16
17 preheader:
18 %b0 = load i8, i8* @b, align 1
19 %b0sext = sext i8 %b0 to i64
20 br label %for.body
21
22 for.body:
23 %iv = phi i64 [ 0, %preheader ], [ %iv.next, %lor.false ]
24 %mul = mul nsw i64 %b0sext, %iv
25 %multrunc = trunc i64 %mul to i32
26 %cmp = icmp eq i32 %multrunc, 0
27 br i1 %cmp, label %lor.false, label %if.then
28
29 lor.false:
30 %cgep = getelementptr inbounds [4 x i8], [4 x i8]* @c, i64 0, i64 %iv
31 %ci = load i8, i8* %cgep, align 1
32 %cisext = sext i8 %ci to i32
33 %ivtrunc = trunc i64 %iv to i32
34 %cmp2 = icmp eq i32 %cisext, %ivtrunc
35 %iv.next = add i64 %iv, 1
36 br i1 %cmp2, label %for.body, label %if.then
37
38 if.then:
39 tail call void @abort()
40 unreachable
41
42 for.end:
43 ret i32 0
44 }
45
46 declare void @abort()