llvm.org GIT mirror llvm / 2e53b71
Revert r286437 r286438, they caused PR30976 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@286483 91177308-0d34-0410-b5e6-96231b3b80d8 Nico Weber 2 years ago
2 changed file(s) with 33 addition(s) and 134 deletion(s). Raw diff Collapse all Expand all
197197 DebugLoc Loc = Builder.GetInsertPoint()->getDebugLoc();
198198 SCEVInsertPointGuard Guard(Builder, this);
199199
200 auto *RHSConst = dyn_cast(RHS);
201
202 if (Opcode != Instruction::UDiv || (RHSConst && !RHSConst->isZero())) {
203 // FIXME: There is alredy similar logic in expandCodeFor, we should see if
204 // this is actually needed here.
205
206 // Move the insertion point out of as many loops as we can.
207 while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
208 if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS))
209 break;
210 BasicBlock *Preheader = L->getLoopPreheader();
211 if (!Preheader)
212 break;
213
214 // Ok, move up a level.
215 Builder.SetInsertPoint(Preheader->getTerminator());
216 }
200 // Move the insertion point out of as many loops as we can.
201 while (const Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock())) {
202 if (!L->isLoopInvariant(LHS) || !L->isLoopInvariant(RHS)) break;
203 BasicBlock *Preheader = L->getLoopPreheader();
204 if (!Preheader) break;
205
206 // Ok, move up a level.
207 Builder.SetInsertPoint(Preheader->getTerminator());
217208 }
218209
219210 // If we haven't found this binop, insert it.
16711662 // Compute an insertion point for this SCEV object. Hoist the instructions
16721663 // as far out in the loop nest as possible.
16731664 Instruction *InsertPt = &*Builder.GetInsertPoint();
1674 if (!isa(S)) {
1675 for (Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock());;
1676 L = L->getParentLoop())
1677 if (SE.isLoopInvariant(S, L)) {
1678 if (!L)
1679 break;
1680 if (BasicBlock *Preheader = L->getLoopPreheader())
1681 InsertPt = Preheader->getTerminator();
1682 else {
1683 // LSR sets the insertion point for AddRec start/step values to the
1684 // block start to simplify value reuse, even though it's an invalid
1685 // position. SCEVExpander must correct for this in all cases.
1686 InsertPt = &*L->getHeader()->getFirstInsertionPt();
1687 }
1688 } else {
1689 // If the SCEV is computable at this level, insert it into the header
1690 // after the PHIs (and after any other instructions that we've inserted
1691 // there) so that it is guaranteed to dominate any user inside the loop.
1692 if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
1693 InsertPt = &*L->getHeader()->getFirstInsertionPt();
1694 while (InsertPt->getIterator() != Builder.GetInsertPoint() &&
1695 (isInsertedInstruction(InsertPt) ||
1696 isa(InsertPt))) {
1697 InsertPt = &*std::next(InsertPt->getIterator());
1698 }
1699 break;
1700 }
1701 }
1665 for (Loop *L = SE.LI.getLoopFor(Builder.GetInsertBlock());;
1666 L = L->getParentLoop())
1667 if (SE.isLoopInvariant(S, L)) {
1668 if (!L) break;
1669 if (BasicBlock *Preheader = L->getLoopPreheader())
1670 InsertPt = Preheader->getTerminator();
1671 else {
1672 // LSR sets the insertion point for AddRec start/step values to the
1673 // block start to simplify value reuse, even though it's an invalid
1674 // position. SCEVExpander must correct for this in all cases.
1675 InsertPt = &*L->getHeader()->getFirstInsertionPt();
1676 }
1677 } else {
1678 // If the SCEV is computable at this level, insert it into the header
1679 // after the PHIs (and after any other instructions that we've inserted
1680 // there) so that it is guaranteed to dominate any user inside the loop.
1681 if (L && SE.hasComputableLoopEvolution(S, L) && !PostIncLoops.count(L))
1682 InsertPt = &*L->getHeader()->getFirstInsertionPt();
1683 while (InsertPt->getIterator() != Builder.GetInsertPoint() &&
1684 (isInsertedInstruction(InsertPt) ||
1685 isa(InsertPt))) {
1686 InsertPt = &*std::next(InsertPt->getIterator());
1687 }
1688 break;
1689 }
17021690
17031691 // Check to see if we already expanded this here.
17041692 auto I = InsertedExpressions.find(std::make_pair(S, InsertPt));
348348 llvm_unreachable("Expected to find instruction!");
349349 }
350350
351 static Argument *getArgByName(Function &F, StringRef Name) {
352 for (auto &A : F.args())
353 if (A.getName() == Name)
354 return &A;
355 llvm_unreachable("Expected to find argument!");
356 }
357
358351 TEST_F(ScalarEvolutionsTest, CommutativeExprOperandOrder) {
359352 LLVMContext C;
360353 SMDiagnostic Err;
471464 });
472465 }
473466
474 TEST_F(ScalarEvolutionsTest, BadHoistingSCEVExpander_PR30942) {
475 LLVMContext C;
476 SMDiagnostic Err;
477 std::unique_ptr M = parseAssemblyString(
478 "target datalayout = \"e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128\" "
479 " "
480 "define void @f_1(i32 %x, i32 %y, i32 %n, i1* %cond_buf) "
481 " local_unnamed_addr { "
482 "entry: "
483 " %entrycond = icmp sgt i32 %n, 0 "
484 " br i1 %entrycond, label %loop.ph, label %for.end "
485 " "
486 "loop.ph: "
487 " br label %loop "
488 " "
489 "loop: "
490 " %iv1 = phi i32 [ %iv1.inc, %right ], [ 0, %loop.ph ] "
491 " %iv1.inc = add nuw nsw i32 %iv1, 1 "
492 " %cond = load volatile i1, i1* %cond_buf "
493 " br i1 %cond, label %left, label %right "
494 " "
495 "left: "
496 " %div = udiv i32 %x, %y "
497 " br label %right "
498 " "
499 "right: "
500 " %exitcond = icmp eq i32 %iv1.inc, %n "
501 " br i1 %exitcond, label %for.end.loopexit, label %loop "
502 " "
503 "for.end.loopexit: "
504 " br label %for.end "
505 " "
506 "for.end: "
507 " ret void "
508 "} ",
509 Err, C);
510
511 assert(M && "Could not parse module?");
512 assert(!verifyModule(*M) && "Must have been well formed!");
513
514 runWithFunctionAndSE(*M, "f_1", [&](Function &F, ScalarEvolution &SE) {
515 SCEVExpander Expander(SE, M->getDataLayout(), "unittests");
516 auto *DivInst = getInstructionByName(F, "div");
517
518 {
519 auto *DivSCEV = SE.getSCEV(DivInst);
520 auto *DivExpansion = Expander.expandCodeFor(
521 DivSCEV, DivSCEV->getType(), DivInst->getParent()->getTerminator());
522 auto *DivExpansionInst = dyn_cast(DivExpansion);
523 ASSERT_NE(DivExpansionInst, nullptr);
524 EXPECT_EQ(DivInst->getParent(), DivExpansionInst->getParent());
525 }
526
527 {
528 auto *ArgY = getArgByName(F, "y");
529 auto *DivFromScratchSCEV =
530 SE.getUDivExpr(SE.getOne(ArgY->getType()), SE.getSCEV(ArgY));
531
532 auto *DivFromScratchExpansion = Expander.expandCodeFor(
533 DivFromScratchSCEV, DivFromScratchSCEV->getType(),
534 DivInst->getParent()->getTerminator());
535 auto *DivFromScratchExpansionInst =
536 dyn_cast(DivFromScratchExpansion);
537 ASSERT_NE(DivFromScratchExpansionInst, nullptr);
538 EXPECT_EQ(DivInst->getParent(), DivFromScratchExpansionInst->getParent());
539 }
540
541 {
542 auto *ArgY = getArgByName(F, "y");
543 auto *SafeDivSCEV =
544 SE.getUDivExpr(SE.getSCEV(ArgY), SE.getConstant(APInt(32, 19)));
545
546 auto *SafeDivExpansion =
547 Expander.expandCodeFor(SafeDivSCEV, SafeDivSCEV->getType(),
548 DivInst->getParent()->getTerminator());
549 auto *SafeDivExpansionInst = dyn_cast(SafeDivExpansion);
550 ASSERT_NE(SafeDivExpansionInst, nullptr);
551 EXPECT_EQ("loop.ph", SafeDivExpansionInst->getParent()->getName());
552 }
553 });
554 }
555
556467 } // end anonymous namespace
557468 } // end namespace llvm