llvm.org GIT mirror llvm / 026a351
Fix PR/33305. caused by trying to simplify expressions in phi of ops that should have no leaders. Summary: After a discussion with Rekka, i believe this (or a small variant) should fix the remaining phi-of-ops problems. Rekka's algorithm for completeness relies on looking up expressions that should have no leader, and expecting it to fail (IE looking up expressions that can't exist in a predecessor, and expecting it to find nothing). Unfortunately, sometimes these expressions can be simplified to constants, but we need the lookup to fail anyway. Additionally, our simplifier outsmarts this by taking these "not quite right" expressions, and simplifying them into other expressions or walking through phis, etc. In the past, we've sometimes been able to find leaders for these expressions, incorrectly. This change causes us to not to try to phi of ops such expressions. We determine safety by seeing if they depend on a phi node in our block. This is not perfect, we can do a bit better, but this should be a "correctness start" that we can then improve. It also requires a bunch of caching that i'll eventually like to eliminate. The right solution, longer term, to the simplifier issues, is to make the query interface for the instruction simplifier/constant folder have the flags we need, so that we can keep most things going, but turn off the possibly-invalid parts (threading through phis, etc). This is an issue in another wrong code bug as well. Reviewers: davide, mcrosier Subscribers: sanjoy, llvm-commits Differential Revision: https://reviews.llvm.org/D37175 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312401 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Berlin 2 years ago
6 changed file(s) with 481 addition(s) and 72 deletion(s). Raw diff Collapse all Expand all
127127 cl::init(false), cl::Hidden);
128128
129129 /// Currently, the generation "phi of ops" can result in correctness issues.
130 static cl::opt EnablePhiOfOps("enable-phi-of-ops", cl::init(false),
130 static cl::opt EnablePhiOfOps("enable-phi-of-ops", cl::init(true),
131131 cl::Hidden);
132132
133133 //===----------------------------------------------------------------------===//
474474 // These mappings just store various data that would normally be part of the
475475 // IR.
476476 DenseSet PHINodeUses;
477 DenseMap OpSafeForPHIOfOps;
477478 // Map a temporary instruction we created to a parent block.
478479 DenseMap TempToBlock;
479480 // Map between the already in-program instructions and the temporary phis we
642643 void initializeCongruenceClasses(Function &F);
643644 const Expression *makePossiblePhiOfOps(Instruction *,
644645 SmallPtrSetImpl &);
646 Value *findLeaderForInst(Instruction *ValueOp,
647 SmallPtrSetImpl &Visited,
648 MemoryAccess *MemAccess, Instruction *OrigInst,
649 BasicBlock *PredBB);
650
651 bool OpIsSafeForPHIOfOps(Value *Op, Instruction *OrigInst,
652 const BasicBlock *PHIBlock,
653 SmallPtrSetImpl &);
645654 void addPhiOfOps(PHINode *Op, BasicBlock *BB, Instruction *ExistingValue);
646655 void removePhiOfOps(Instruction *I, PHINode *PHITemp);
647656
668677 // Congruence finding.
669678 bool someEquivalentDominates(const Instruction *, const Instruction *) const;
670679 Value *lookupOperandLeader(Value *) const;
680 CongruenceClass *getClassForExpression(const Expression *E) const;
671681 void performCongruenceFinding(Instruction *, const Expression *);
672682 void moveValueToNewCongruenceClass(Instruction *, const Expression *,
673683 CongruenceClass *, CongruenceClass *);
702712 void replaceInstruction(Instruction *, Value *);
703713 void markInstructionForDeletion(Instruction *);
704714 void deleteInstructionsInBlock(BasicBlock *);
705 Value *findPhiOfOpsLeader(const Expression *E, const BasicBlock *BB) const;
706
715 Value *findPHIOfOpsLeader(const Expression *E, const BasicBlock *BB) const;
707716 // New instruction creation.
708717 void handleNewInstruction(Instruction *){};
709718
962971 CongruenceClass *CC = ValueToClass.lookup(V);
963972 if (CC) {
964973 if (CC->getLeader() && CC->getLeader() != I) {
965 addAdditionalUsers(V, I);
974 // Don't add temporary instructions to the user lists.
975 if (!AllTempInstructions.count(I))
976 addAdditionalUsers(V, I);
966977 return createVariableOrConstant(CC->getLeader());
967978 }
968979
987998 return nullptr;
988999 }
9891000
1001 // Create a value expression from the instruction I, replacing operands with
1002 // their leaders.
1003
9901004 const Expression *NewGVN::createExpression(Instruction *I) const {
9911005 auto *E = new (ExpressionAllocator) BasicExpression(I->getNumOperands());
9921006
10011015 if (shouldSwapOperands(E->getOperand(0), E->getOperand(1)))
10021016 E->swapOperands(0, 1);
10031017 }
1004
10051018 // Perform simplification.
10061019 if (auto *CI = dyn_cast(I)) {
10071020 // Sort the operand value numbers so xx get the same value
13881401 }
13891402 }
13901403
1391 const auto *LE = createLoadExpression(LI->getType(), LoadAddressLeader,
1392 LI, DefiningAccess);
1404 const auto *LE = createLoadExpression(LI->getType(), LoadAddressLeader, LI,
1405 DefiningAccess);
13931406 // If our MemoryLeader is not our defining access, add a use to the
13941407 // MemoryLeader, so that we get reprocessed when it changes.
13951408 if (LE->getMemoryLeader() != DefiningAccess)
24632476 isa(I);
24642477 }
24652478
2479 // Return true if this operand will be safe to use for phi of ops.
2480 //
2481 // The reason some operands are unsafe is that we are not trying to recursively
2482 // translate everything back through phi nodes. We actually expect some lookups
2483 // of expressions to fail. In particular, a lookup where the expression cannot
2484 // exist in the predecessor. This is true even if the expression, as shown, can
2485 // be determined to be constant.
2486 bool NewGVN::OpIsSafeForPHIOfOps(Value *V, Instruction *OrigInst,
2487 const BasicBlock *PHIBlock,
2488 SmallPtrSetImpl &Visited) {
2489 if (!isa(V))
2490 return true;
2491 auto OISIt = OpSafeForPHIOfOps.find(V);
2492 if (OISIt != OpSafeForPHIOfOps.end())
2493 return OISIt->second;
2494 // Keep walking until we either dominate the phi block, or hit a phi, or run
2495 // out of things to check.
2496 if (DT->properlyDominates(getBlockForValue(V), PHIBlock)) {
2497 OpSafeForPHIOfOps.insert({V, true});
2498 return true;
2499 }
2500 // PHI in the same block.
2501 if (isa(V) && getBlockForValue(V) == PHIBlock) {
2502 OpSafeForPHIOfOps.insert({V, false});
2503 return false;
2504 }
2505 for (auto Op : cast(V)->operand_values()) {
2506 if (!isa(Op))
2507 continue;
2508 // See if we already know the answer for this node.
2509 auto OISIt = OpSafeForPHIOfOps.find(Op);
2510 if (OISIt != OpSafeForPHIOfOps.end()) {
2511 if (!OISIt->second) {
2512 OpSafeForPHIOfOps.insert({V, false});
2513 return false;
2514 }
2515 }
2516 if (!Visited.insert(Op).second)
2517 continue;
2518 if (!OpIsSafeForPHIOfOps(Op, OrigInst, PHIBlock, Visited)) {
2519 OpSafeForPHIOfOps.insert({V, false});
2520 return false;
2521 }
2522 }
2523 OpSafeForPHIOfOps.insert({V, true});
2524 return true;
2525 }
2526
2527 // Try to find a leader for instruction TransInst, which is a phi translated
2528 // version of something in our original program. Visited is used to ensure we
2529 // don't infinite loop during translations of cycles. OrigInst is the
2530 // instruction in the original program, and PredBB is the predecessor we
2531 // translated it through.
2532 Value *NewGVN::findLeaderForInst(Instruction *TransInst,
2533 SmallPtrSetImpl &Visited,
2534 MemoryAccess *MemAccess, Instruction *OrigInst,
2535 BasicBlock *PredBB) {
2536 unsigned IDFSNum = InstrToDFSNum(OrigInst);
2537 // Make sure it's marked as a temporary instruction.
2538 AllTempInstructions.insert(TransInst);
2539 // and make sure anything that tries to add it's DFS number is
2540 // redirected to the instruction we are making a phi of ops
2541 // for.
2542 TempToBlock.insert({TransInst, PredBB});
2543 InstrDFS.insert({TransInst, IDFSNum});
2544
2545 const Expression *E = performSymbolicEvaluation(TransInst, Visited);
2546 InstrDFS.erase(TransInst);
2547 AllTempInstructions.erase(TransInst);
2548 TempToBlock.erase(TransInst);
2549 if (MemAccess)
2550 TempToMemory.erase(TransInst);
2551 if (!E)
2552 return nullptr;
2553 auto *FoundVal = findPHIOfOpsLeader(E, PredBB);
2554 if (!FoundVal || FoundVal == OrigInst) {
2555 ExpressionToPhiOfOps[E].insert(OrigInst);
2556 DEBUG(dbgs() << "Cannot find phi of ops operand for " << *TransInst
2557 << " in block " << getBlockName(PredBB) << "\n");
2558 return nullptr;
2559 }
2560 if (auto *SI = dyn_cast(FoundVal))
2561 FoundVal = SI->getValueOperand();
2562 return FoundVal;
2563 }
2564
24662565 // When we see an instruction that is an op of phis, generate the equivalent phi
24672566 // of ops form.
24682567 const Expression *
24802579 if (!isCycleFree(I))
24812580 return nullptr;
24822581
2483 unsigned IDFSNum = InstrToDFSNum(I);
24842582 SmallPtrSet ProcessedPHIs;
24852583 // TODO: We don't do phi translation on memory accesses because it's
24862584 // complicated. For a load, we'd need to be able to simulate a new memoryuse,
24932591 MemAccess->getDefiningAccess()->getBlock() == I->getParent())
24942592 return nullptr;
24952593
2594 SmallPtrSet VisitedOps;
24962595 // Convert op of phis to phi of ops
24972596 for (auto &Op : I->operands()) {
2498 // TODO: We can't handle expressions that must be recursively translated
2499 // IE
2500 // a = phi (b, c)
2501 // f = use a
2502 // g = f + phi of something
2503 // To properly make a phi of ops for g, we'd have to properly translate and
2504 // use the instruction for f. We should add this by splitting out the
2505 // instruction creation we do below.
2506 if (isa(Op) && PHINodeUses.count(cast(Op)))
2507 return nullptr;
25082597 if (!isa(Op))
25092598 continue;
25102599 auto *OpPHI = cast(Op);
25252614 Instruction *ValueOp = I->clone();
25262615 if (MemAccess)
25272616 TempToMemory.insert({ValueOp, MemAccess});
2528
2617 bool SafeForPHIOfOps = true;
2618 VisitedOps.clear();
25292619 for (auto &Op : ValueOp->operands()) {
2620 auto *OrigOp = &*Op;
25302621 Op = Op->DoPHITranslation(PHIBlock, PredBB);
25312622 // When this operand changes, it could change whether there is a
25322623 // leader for us or not.
25332624 addAdditionalUsers(Op, I);
2625 // If we phi-translated the op, it must be safe.
2626 SafeForPHIOfOps = SafeForPHIOfOps &&
2627 (Op != OrigOp ||
2628 OpIsSafeForPHIOfOps(Op, I, PHIBlock, VisitedOps));
25342629 }
2535 // Make sure it's marked as a temporary instruction.
2536 AllTempInstructions.insert(ValueOp);
2537 // and make sure anything that tries to add it's DFS number is
2538 // redirected to the instruction we are making a phi of ops
2539 // for.
2540 TempToBlock.insert({ValueOp, PredBB});
2541 InstrDFS.insert({ValueOp, IDFSNum});
2542 const Expression *E = performSymbolicEvaluation(ValueOp, Visited);
2543 InstrDFS.erase(ValueOp);
2544 AllTempInstructions.erase(ValueOp);
2630 // FIXME: For those things that are not safe We could generate
2631 // expressions all the way down, and see if this comes out to a
2632 // constant. For anything where that is true, and unsafe, we should
2633 // have made a phi-of-ops (or value numbered it equivalent to something)
2634 // for the pieces already.
2635 FoundVal = !SafeForPHIOfOps ? nullptr
2636 : findLeaderForInst(ValueOp, Visited,
2637 MemAccess, I, PredBB);
25452638 ValueOp->deleteValue();
2546 TempToBlock.erase(ValueOp);
2547 if (MemAccess)
2548 TempToMemory.erase(ValueOp);
2549 if (!E)
2639 if (!FoundVal)
25502640 return nullptr;
2551 FoundVal = findPhiOfOpsLeader(E, PredBB);
2552 if (!FoundVal) {
2553 ExpressionToPhiOfOps[E].insert(I);
2554 return nullptr;
2555 }
2556 if (auto *SI = dyn_cast(FoundVal))
2557 FoundVal = SI->getValueOperand();
25582641 } else {
25592642 DEBUG(dbgs() << "Skipping phi of ops operand for incoming block "
25602643 << getBlockName(PredBB)
25692652 auto *ValuePHI = RealToTemp.lookup(I);
25702653 bool NewPHI = false;
25712654 if (!ValuePHI) {
2572 ValuePHI = PHINode::Create(I->getType(), OpPHI->getNumOperands());
2655 ValuePHI =
2656 PHINode::Create(I->getType(), OpPHI->getNumOperands(), "phiofops");
25732657 addPhiOfOps(ValuePHI, PHIBlock, I);
25742658 NewPHI = true;
25752659 NumGVNPHIOfOpsCreated++;
26942778 TempToBlock.clear();
26952779 TempToMemory.clear();
26962780 PHIOfOpsPHIs.clear();
2781 PHINodeUses.clear();
2782 OpSafeForPHIOfOps.clear();
26972783 ReachableBlocks.clear();
26982784 ReachableEdges.clear();
26992785 #ifndef NDEBUG
35233609 };
35243610 }
35253611
3612 // Given an expression, get the congruence class for it.
3613 CongruenceClass *NewGVN::getClassForExpression(const Expression *E) const {
3614 if (auto *VE = dyn_cast(E))
3615 return ValueToClass.lookup(VE->getVariableValue());
3616 else if (isa(E))
3617 return TOPClass;
3618 return ExpressionToClass.lookup(E);
3619 }
3620
35263621 // Given a value and a basic block we are trying to see if it is available in,
35273622 // see if the value has a leader available in that block.
3528 Value *NewGVN::findPhiOfOpsLeader(const Expression *E,
3623 Value *NewGVN::findPHIOfOpsLeader(const Expression *E,
35293624 const BasicBlock *BB) const {
35303625 // It would already be constant if we could make it constant
35313626 if (auto *CE = dyn_cast(E))
35323627 return CE->getConstantValue();
3533 if (auto *VE = dyn_cast(E))
3534 return VE->getVariableValue();
3535
3536 auto *CC = ExpressionToClass.lookup(E);
3628 if (auto *VE = dyn_cast(E)) {
3629 auto *V = VE->getVariableValue();
3630 if (alwaysAvailable(V) || DT->dominates(getBlockForValue(V), BB))
3631 return VE->getVariableValue();
3632 }
3633
3634 auto *CC = getClassForExpression(E);
35373635 if (!CC)
35383636 return nullptr;
35393637 if (alwaysAvailable(CC->getLeader()))
35443642 // Anything that isn't an instruction is always available.
35453643 if (!MemberInst)
35463644 return Member;
3547 // If we are looking for something in the same block as the member, it must
3548 // be a leader because this function is looking for operands for a phi node.
3549 if (MemberInst->getParent() == BB ||
3550 DT->dominates(MemberInst->getParent(), BB)) {
3645 if (DT->dominates(getBlockForValue(MemberInst), BB))
35513646 return Member;
3552 }
35533647 }
35543648 return nullptr;
35553649 }
77 ; CHECK-NEXT: br i1 [[TMP3]], label [[TMP4:%.*]], label [[TMP5:%.*]]
88 ; CHECK: br label [[TMP6:%.*]]
99 ; CHECK: br label [[TMP6]]
10 ; CHECK: [[TMP7:%.*]] = phi i32 [ 75, [[TMP4]] ], [ 105, [[TMP5]] ]
10 ; CHECK: [[PHIOFOPS:%.*]] = phi i32 [ 75, [[TMP4]] ], [ 105, [[TMP5]] ]
1111 ; CHECK-NEXT: [[DOT0:%.*]] = phi i32 [ 5, [[TMP4]] ], [ 7, [[TMP5]] ]
12 ; CHECK-NEXT: ret i32 [[TMP7]]
12 ; CHECK-NEXT: ret i32 [[PHIOFOPS]]
1313 ;
1414 %3 = icmp ne i32 %0, 0
1515 br i1 %3, label %4, label %5
5858 ; CHECK: delay:
5959 ; CHECK-NEXT: br label [[FINAL]]
6060 ; CHECK: final:
61 ; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ -877, [[ENTRY:%.*]] ], [ 113, [[DELAY]] ]
61 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi i32 [ -877, [[ENTRY:%.*]] ], [ 113, [[DELAY]] ]
6262 ; CHECK-NEXT: [[A:%.*]] = phi i32 [ 1000, [[ENTRY]] ], [ 10, [[DELAY]] ]
63 ; CHECK-NEXT: ret i32 [[TMP0]]
63 ; CHECK-NEXT: ret i32 [[PHIOFOPS]]
6464 ;
6565
6666 entry:
8282 ; CHECK: delay:
8383 ; CHECK-NEXT: br label [[FINAL]]
8484 ; CHECK: final:
85 ; CHECK-NEXT: [[TMP0:%.*]] = phi <2 x i32> [ , [[ENTRY:%.*]] ], [ , [[DELAY]] ]
85 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi <2 x i32> [ , [[ENTRY:%.*]] ], [ , [[DELAY]] ]
8686 ; CHECK-NEXT: [[A:%.*]] = phi <2 x i32> [ , [[ENTRY]] ], [ , [[DELAY]] ]
87 ; CHECK-NEXT: ret <2 x i32> [[TMP0]]
87 ; CHECK-NEXT: ret <2 x i32> [[PHIOFOPS]]
8888 ;
8989
9090 entry:
106106 ; CHECK: delay:
107107 ; CHECK-NEXT: br label [[FINAL]]
108108 ; CHECK: final:
109 ; CHECK-NEXT: [[TMP0:%.*]] = phi <2 x i32> [ , [[ENTRY:%.*]] ], [ , [[DELAY]] ]
109 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi <2 x i32> [ , [[ENTRY:%.*]] ], [ , [[DELAY]] ]
110110 ; CHECK-NEXT: [[A:%.*]] = phi <2 x i32> [ , [[ENTRY]] ], [ , [[DELAY]] ]
111 ; CHECK-NEXT: ret <2 x i32> [[TMP0]]
111 ; CHECK-NEXT: ret <2 x i32> [[PHIOFOPS]]
112112 ;
113113
114114 entry:
187187 ; CHECK: bb14:
188188 ; CHECK-NEXT: br label [[BB15:%.*]]
189189 ; CHECK: bb15:
190 ; CHECK-NEXT: [[TMP0:%.*]] = phi i64 [ [[TMP25:%.*]], [[BB15]] ], [ [[TMP12]], [[BB14]] ]
190 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi i64 [ [[TMP25:%.*]], [[BB15]] ], [ [[TMP12]], [[BB14]] ]
191191 ; CHECK-NEXT: [[TMP16:%.*]] = phi i64 [ [[TMP24:%.*]], [[BB15]] ], [ [[TMP11]], [[BB14]] ]
192192 ; CHECK-NEXT: [[TMP17:%.*]] = phi i64 [ [[TMP22:%.*]], [[BB15]] ], [ [[TMP10]], [[BB14]] ]
193193 ; CHECK-NEXT: [[TMP18:%.*]] = phi i64 [ [[TMP20:%.*]], [[BB15]] ], [ 0, [[BB14]] ]
194 ; CHECK-NEXT: store i64 [[TMP0]], i64* [[TMP]], align 8
194 ; CHECK-NEXT: store i64 [[PHIOFOPS]], i64* [[TMP]], align 8
195195 ; CHECK-NEXT: [[TMP20]] = add nuw nsw i64 [[TMP18]], 1
196196 ; CHECK-NEXT: [[TMP21:%.*]] = getelementptr inbounds [100 x i64], [100 x i64]* @global, i64 0, i64 [[TMP20]]
197197 ; CHECK-NEXT: [[TMP22]] = load i64, i64* [[TMP21]], align 8
262262 ; CHECK-NEXT: entry-block:
263263 ; CHECK-NEXT: br label %main-loop
264264 ; CHECK: main-loop:
265 ; CHECK-NEXT: [[TMP0:%.*]] = phi i1 [ true, %entry-block ], [ false, [[CORE:%.*]] ]
266 ; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ false, %entry-block ], [ true, [[CORE]] ]
265 ; CHECK-NEXT: [[PHIOFOPS1:%.*]] = phi i1 [ true, %entry-block ], [ false, [[CORE:%.*]] ]
266 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi i1 [ false, %entry-block ], [ true, [[CORE]] ]
267267 ; CHECK-NEXT: [[PHI:%.*]] = phi i8 [ 0, %entry-block ], [ 1, [[CORE]] ]
268268 ; CHECK-NEXT: store volatile i8 0, i8* [[ADDR:%.*]]
269 ; CHECK-NEXT: br i1 [[TMP0]], label %busy-wait-phi-0, label [[EXIT:%.*]]
269 ; CHECK-NEXT: br i1 [[PHIOFOPS1]], label %busy-wait-phi-0, label [[EXIT:%.*]]
270270 ; CHECK: busy-wait-phi-0:
271271 ; CHECK-NEXT: [[LOAD:%.*]] = load volatile i8, i8* [[ADDR]]
272272 ; CHECK-NEXT: [[ICMP:%.*]] = icmp eq i8 [[LOAD]], 0
273273 ; CHECK-NEXT: br i1 [[ICMP]], label %busy-wait-phi-0, label [[CORE]]
274274 ; CHECK: core:
275 ; CHECK-NEXT: br i1 [[TMP1]], label [[TRAP:%.*]], label %main-loop
275 ; CHECK-NEXT: br i1 [[PHIOFOPS]], label [[TRAP:%.*]], label %main-loop
276276 ; CHECK: trap:
277277 ; CHECK-NEXT: ret i8 1
278278 ; CHECK: exit:
356356 ; CHECK: bb2:
357357 ; CHECK-NEXT: br label [[BB6:%.*]]
358358 ; CHECK: bb6:
359 ; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ -13, [[BB2]] ], [ [[TMP11:%.*]], [[BB6]] ]
359 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi i32 [ -13, [[BB2]] ], [ [[TMP11:%.*]], [[BB6]] ]
360360 ; CHECK-NEXT: [[TMP7:%.*]] = phi i32 [ 1, [[BB2]] ], [ [[TMP8:%.*]], [[BB6]] ]
361361 ; CHECK-NEXT: [[TMP8]] = add nuw nsw i32 [[TMP7]], 1
362362 ; CHECK-NEXT: [[TMP11]] = add i32 -14, [[TMP8]]
429429 %0 = phi i32* [ undef, %o ], [ %m, %k ], [ %m, %g ]
430430 ret void
431431 }
432
433 ;; Ensure we handle VariableExpression properly.
434 define void @test11() {
435 ; CHECK-LABEL: @test11(
436 ; CHECK-NEXT: bb:
437 ; CHECK-NEXT: br i1 undef, label [[BB1:%.*]], label [[BB2:%.*]]
438 ; CHECK: bb1:
439 ; CHECK-NEXT: br label [[BB2]]
440 ; CHECK: bb2:
441 ; CHECK-NEXT: [[TMP:%.*]] = phi i1 [ false, [[BB1]] ], [ true, [[BB:%.*]] ]
442 ; CHECK-NEXT: [[TMP3:%.*]] = call i32* @wombat()
443 ; CHECK-NEXT: [[TMP4:%.*]] = icmp ne i32* [[TMP3]], null
444 ; CHECK-NEXT: [[TMP5:%.*]] = and i1 [[TMP]], [[TMP4]]
445 ; CHECK-NEXT: br i1 [[TMP5]], label [[BB6:%.*]], label [[BB7:%.*]]
446 ; CHECK: bb6:
447 ; CHECK-NEXT: unreachable
448 ; CHECK: bb7:
449 ; CHECK-NEXT: ret void
450 ;
451 bb:
452 br i1 undef, label %bb1, label %bb2
453
454 bb1: ; preds = %bb
455 br label %bb2
456
457 bb2: ; preds = %bb1, %bb
458 %tmp = phi i1 [ false, %bb1 ], [ true, %bb ]
459 %tmp3 = call i32* @wombat()
460 %tmp4 = icmp ne i32* %tmp3, null
461 %tmp5 = and i1 %tmp, %tmp4
462 br i1 %tmp5, label %bb6, label %bb7
463
464 bb6: ; preds = %bb2
465 unreachable
466
467 bb7: ; preds = %bb2
468 ret void
469 }
470
471 declare i32* @wombat()
33 @a = local_unnamed_addr global i32 9, align 4
44 @.str4 = private unnamed_addr constant [6 x i8] c"D:%d\0A\00", align 1
55
6 define i32 @main() local_unnamed_addr {
7 ; CHECK-LABEL: @main(
6 define i32 @test1() local_unnamed_addr {
7 ; CHECK-LABEL: @test1(
88 ; CHECK-NEXT: entry:
99 ; CHECK-NEXT: [[TMP:%.*]] = load i32, i32* @a, align 4
1010 ; CHECK-NEXT: [[CMP1_I:%.*]] = icmp ne i32 [[TMP]], 0
5656
5757 declare i32 @printf(i8* nocapture readonly, ...)
5858
59 ;; Variant of the above where we have made the udiv available in each predecessor with the wrong values.
60 ;; In the entry block, it is always 0, so we don't try to create a leader there, only in %cond.end.i.
61 ;; We should not create a phi of ops for it using these leaders.
62 ;; A correct phi of ops for this udiv would be phi(0, 1), which we are not smart enough to figure out.
63 ;; If we reuse the incorrect leaders, we will get phi(0, 0).
64 define i32 @test2() local_unnamed_addr {
65 ; CHECK-LABEL: @test2(
66 ; CHECK-NEXT: entry:
67 ; CHECK-NEXT: [[TMP:%.*]] = load i32, i32* @a, align 4
68 ; CHECK-NEXT: [[CMP1_I:%.*]] = icmp ne i32 [[TMP]], 0
69 ; CHECK-NEXT: br label [[FOR_BODY_I:%.*]]
70 ; CHECK: for.body.i:
71 ; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[COND_END_I:%.*]] ]
72 ; CHECK-NEXT: [[F_08_I:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[INC_I:%.*]], [[COND_END_I]] ]
73 ; CHECK-NEXT: [[MUL_I:%.*]] = select i1 [[CMP1_I]], i32 [[F_08_I]], i32 0
74 ; CHECK-NEXT: br i1 [[TMP1]], label [[COND_END_I]], label [[COND_TRUE_I:%.*]]
75 ; CHECK: cond.true.i:
76 ; CHECK-NEXT: [[DIV_I:%.*]] = udiv i32 [[MUL_I]], [[F_08_I]]
77 ; CHECK-NEXT: br label [[COND_END_I]]
78 ; CHECK: cond.end.i:
79 ; CHECK-NEXT: [[COND_I:%.*]] = phi i32 [ [[DIV_I]], [[COND_TRUE_I]] ], [ 0, [[FOR_BODY_I]] ]
80 ; CHECK-NEXT: [[INC_I]] = add nuw nsw i32 [[F_08_I]], 1
81 ; CHECK-NEXT: [[CALL5:%.*]] = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str4, i64 0, i64 0), i32 0)
82 ; CHECK-NEXT: [[EXITCOND_I:%.*]] = icmp eq i32 [[INC_I]], 4
83 ; CHECK-NEXT: br i1 [[EXITCOND_I]], label [[FN1_EXIT:%.*]], label [[FOR_BODY_I]]
84 ; CHECK: fn1.exit:
85 ; CHECK-NEXT: [[CALL4:%.*]] = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str4, i64 0, i64 0), i32 [[COND_I]])
86 ; CHECK-NEXT: ret i32 0
87 ;
88 entry:
89 %tmp = load i32, i32* @a, align 4
90 %cmp1.i = icmp ne i32 %tmp, 0
91 br label %for.body.i
92
93 for.body.i:
94 %tmp1 = phi i1 [ true, %entry ], [ false, %cond.end.i ]
95 %f.08.i = phi i32 [ 0, %entry ], [ %inc.i, %cond.end.i ]
96 %mul.i = select i1 %cmp1.i, i32 %f.08.i, i32 0
97 br i1 %tmp1, label %cond.end.i, label %cond.true.i
98
99 cond.true.i:
100 ;; Ensure we don't replace this divide with a phi of ops that merges the wrong loop iteration value
101 %div.i = udiv i32 %mul.i, %f.08.i
102 br label %cond.end.i
103
104 cond.end.i:
105 %cond.i = phi i32 [ %div.i, %cond.true.i ], [ 0, %for.body.i ]
106 %inc.i = add nuw nsw i32 %f.08.i, 1
107 %test = udiv i32 %mul.i, %inc.i
108 %call5= tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str4, i64 0, i64 0), i32 %test)
109 %exitcond.i = icmp eq i32 %inc.i, 4
110 br i1 %exitcond.i, label %fn1.exit, label %for.body.i
111
112 fn1.exit:
113 %cond.i.lcssa = phi i32 [ %cond.i, %cond.end.i ]
114 %call4= tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str4, i64 0, i64 0), i32 %cond.i.lcssa)
115 ret i32 0
116 }
117
118
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt -newgvn -S %s | FileCheck %s
2 ; Ensure we do not incorrect do phi of ops
3 source_filename = "/Users/dannyb/sources/llvm-clean/debug-build/pr33305.c"
4 target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
5 target triple = "x86_64-apple-macosx10.12.0"
6
7 @a = common global i32 0, align 4
8 @b = local_unnamed_addr global i32* @a, align 8
9 @e = local_unnamed_addr global i32 -1, align 4
10 @g = local_unnamed_addr global i32 1, align 4
11 @c = common local_unnamed_addr global i32 0, align 4
12 @f = common local_unnamed_addr global i32 0, align 4
13 @h = common local_unnamed_addr global i32 0, align 4
14 @str = private unnamed_addr constant [5 x i8] c"fine\00"
15 @str.2 = private unnamed_addr constant [8 x i8] c"Screwed\00"
16
17 ; Function Attrs: nounwind optsize ssp uwtable
18 define i32 @main() local_unnamed_addr #0 {
19 ; CHECK-LABEL: @main(
20 ; CHECK-NEXT: entry:
21 ; CHECK-NEXT: [[DOTPR_I:%.*]] = load i32, i32* @c, align 4, !tbaa !3
22 ; CHECK-NEXT: [[CMP13_I:%.*]] = icmp slt i32 [[DOTPR_I]], 1
23 ; CHECK-NEXT: br i1 [[CMP13_I]], label [[FOR_COND1_PREHEADER_LR_PH_I:%.*]], label [[ENTRY_FOR_END9_I_CRIT_EDGE:%.*]]
24 ; CHECK: entry.for.end9.i_crit_edge:
25 ; CHECK-NEXT: [[DOTPRE:%.*]] = load i32, i32* @h, align 4, !tbaa !3
26 ; CHECK-NEXT: br label [[FOR_END9_I:%.*]]
27 ; CHECK: for.cond1.preheader.lr.ph.i:
28 ; CHECK-NEXT: [[G_PROMOTED14_I:%.*]] = load i32, i32* @g, align 4, !tbaa !3
29 ; CHECK-NEXT: br label [[FOR_COND1_PREHEADER_I:%.*]]
30 ; CHECK: for.cond1.preheader.i:
31 ; CHECK-NEXT: [[INC816_I:%.*]] = phi i32 [ [[DOTPR_I]], [[FOR_COND1_PREHEADER_LR_PH_I]] ], [ [[INC8_I:%.*]], [[FOR_INC7_I:%.*]] ]
32 ; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ [[G_PROMOTED14_I]], [[FOR_COND1_PREHEADER_LR_PH_I]] ], [ 0, [[FOR_INC7_I]] ]
33 ; CHECK-NEXT: br label [[FOR_BODY3_I:%.*]]
34 ; CHECK: for.body3.i:
35 ; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ false, [[FOR_COND1_PREHEADER_I]] ], [ true, [[LOR_END_I:%.*]] ]
36 ; CHECK-NEXT: [[INC12_I:%.*]] = phi i32 [ 0, [[FOR_COND1_PREHEADER_I]] ], [ [[INC_I:%.*]], [[LOR_END_I]] ]
37 ; CHECK-NEXT: [[TMP2:%.*]] = phi i32 [ [[TMP0]], [[FOR_COND1_PREHEADER_I]] ], [ 0, [[LOR_END_I]] ]
38 ; CHECK-NEXT: [[TOBOOL_I:%.*]] = icmp ne i32 [[TMP2]], 0
39 ; CHECK-NEXT: [[OR_COND_I:%.*]] = and i1 [[TMP1]], [[TOBOOL_I]]
40 ; CHECK-NEXT: br i1 [[OR_COND_I]], label [[LOR_END_I]], label [[LOR_RHS_I:%.*]]
41 ; CHECK: lor.rhs.i:
42 ; CHECK-NEXT: [[LNOT_I:%.*]] = xor i1 [[TOBOOL_I]], true
43 ; CHECK-NEXT: [[LNOT_EXT_I:%.*]] = zext i1 [[LNOT_I]] to i32
44 ; CHECK-NEXT: [[TMP3:%.*]] = load i32, i32* @e, align 4, !tbaa !3
45 ; CHECK-NEXT: [[XOR_I:%.*]] = xor i32 [[TMP3]], [[LNOT_EXT_I]]
46 ; CHECK-NEXT: store i32 [[XOR_I]], i32* @e, align 4, !tbaa !3
47 ; CHECK-NEXT: br label [[LOR_END_I]]
48 ; CHECK: lor.end.i:
49 ; CHECK-NEXT: [[INC_I]] = add nuw nsw i32 [[INC12_I]], 1
50 ; CHECK-NEXT: [[EXITCOND_I:%.*]] = icmp eq i32 [[INC_I]], 2
51 ; CHECK-NEXT: br i1 [[EXITCOND_I]], label [[FOR_INC7_I]], label [[FOR_BODY3_I]]
52 ; CHECK: for.inc7.i:
53 ; CHECK-NEXT: [[INC8_I]] = add nsw i32 [[INC816_I]], 1
54 ; CHECK-NEXT: [[CMP_I:%.*]] = icmp slt i32 [[INC816_I]], 0
55 ; CHECK-NEXT: br i1 [[CMP_I]], label [[FOR_COND1_PREHEADER_I]], label [[FOR_COND_FOR_END9_CRIT_EDGE_I:%.*]]
56 ; CHECK: for.cond.for.end9_crit_edge.i:
57 ; CHECK-NEXT: store i32 0, i32* @g, align 4, !tbaa !3
58 ; CHECK-NEXT: store i32 2, i32* @h, align 4, !tbaa !3
59 ; CHECK-NEXT: store i32 [[INC8_I]], i32* @c, align 4, !tbaa !3
60 ; CHECK-NEXT: br label [[FOR_END9_I]]
61 ; CHECK: for.end9.i:
62 ; CHECK-NEXT: [[TMP4:%.*]] = phi i32 [ [[DOTPRE]], [[ENTRY_FOR_END9_I_CRIT_EDGE]] ], [ 2, [[FOR_COND_FOR_END9_CRIT_EDGE_I]] ]
63 ; CHECK-NEXT: [[TMP5:%.*]] = load i32*, i32** @b, align 8, !tbaa !7
64 ; CHECK-NEXT: store i32 [[TMP4]], i32* [[TMP5]], align 4, !tbaa !3
65 ; CHECK-NEXT: [[TMP6:%.*]] = load i32, i32* @e, align 4, !tbaa !3
66 ; CHECK-NEXT: [[CMP10_I:%.*]] = icmp slt i32 [[TMP6]], -1
67 ; CHECK-NEXT: br i1 [[CMP10_I]], label [[IF_THEN_I:%.*]], label [[FN1_EXIT:%.*]]
68 ; CHECK: if.then.i:
69 ; CHECK-NEXT: [[TMP7:%.*]] = load i32, i32* @f, align 4, !tbaa !3
70 ; CHECK-NEXT: store i32 [[TMP7]], i32* [[TMP5]], align 4, !tbaa !3
71 ; CHECK-NEXT: br label [[FN1_EXIT]]
72 ; CHECK: fn1.exit:
73 ; CHECK-NEXT: [[TMP8:%.*]] = load i32, i32* @a, align 4, !tbaa !3
74 ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp eq i32 [[TMP8]], 0
75 ; CHECK-NEXT: br i1 [[TOBOOL]], label [[IF_END:%.*]], label [[IF_THEN:%.*]]
76 ; CHECK: if.then:
77 ; CHECK-NEXT: [[PUTS2:%.*]] = tail call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str.2, i64 0, i64 0))
78 ; CHECK-NEXT: tail call void @abort() #4
79 ; CHECK-NEXT: unreachable
80 ; CHECK: if.end:
81 ; CHECK-NEXT: [[PUTS:%.*]] = tail call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
82 ; CHECK-NEXT: ret i32 0
83 ;
84 entry:
85 %.pr.i = load i32, i32* @c, align 4, !tbaa !3
86 %cmp13.i = icmp slt i32 %.pr.i, 1
87 br i1 %cmp13.i, label %for.cond1.preheader.lr.ph.i, label %entry.for.end9.i_crit_edge
88
89 entry.for.end9.i_crit_edge: ; preds = %entry
90 %.pre = load i32, i32* @h, align 4, !tbaa !3
91 br label %for.end9.i
92
93 for.cond1.preheader.lr.ph.i: ; preds = %entry
94 %g.promoted14.i = load i32, i32* @g, align 4, !tbaa !3
95 br label %for.cond1.preheader.i
96
97 for.cond1.preheader.i: ; preds = %for.inc7.i, %for.cond1.preheader.lr.ph.i
98 %inc816.i = phi i32 [ %.pr.i, %for.cond1.preheader.lr.ph.i ], [ %inc8.i, %for.inc7.i ]
99 %0 = phi i32 [ %g.promoted14.i, %for.cond1.preheader.lr.ph.i ], [ 0, %for.inc7.i ]
100 br label %for.body3.i
101
102 for.body3.i: ; preds = %lor.end.i, %for.cond1.preheader.i
103 %1 = phi i1 [ false, %for.cond1.preheader.i ], [ true, %lor.end.i ]
104 %inc12.i = phi i32 [ 0, %for.cond1.preheader.i ], [ %inc.i, %lor.end.i ]
105 %2 = phi i32 [ %0, %for.cond1.preheader.i ], [ 0, %lor.end.i ]
106 %tobool.i = icmp ne i32 %2, 0
107 %or.cond.i = and i1 %1, %tobool.i
108 br i1 %or.cond.i, label %lor.end.i, label %lor.rhs.i
109
110 lor.rhs.i: ; preds = %for.body3.i
111 %lnot.i = xor i1 %tobool.i, true
112 %lnot.ext.i = zext i1 %lnot.i to i32
113 %3 = load i32, i32* @e, align 4, !tbaa !3
114 %xor.i = xor i32 %3, %lnot.ext.i
115 store i32 %xor.i, i32* @e, align 4, !tbaa !3
116 br label %lor.end.i
117
118 lor.end.i: ; preds = %lor.rhs.i, %for.body3.i
119 %inc.i = add nuw nsw i32 %inc12.i, 1
120 %exitcond.i = icmp eq i32 %inc.i, 2
121 br i1 %exitcond.i, label %for.inc7.i, label %for.body3.i
122
123 for.inc7.i: ; preds = %lor.end.i
124 %inc8.i = add nsw i32 %inc816.i, 1
125 %cmp.i = icmp slt i32 %inc816.i, 0
126 br i1 %cmp.i, label %for.cond1.preheader.i, label %for.cond.for.end9_crit_edge.i
127
128 for.cond.for.end9_crit_edge.i: ; preds = %for.inc7.i
129 store i32 0, i32* @g, align 4, !tbaa !3
130 store i32 2, i32* @h, align 4, !tbaa !3
131 store i32 %inc8.i, i32* @c, align 4, !tbaa !3
132 br label %for.end9.i
133
134 for.end9.i: ; preds = %entry.for.end9.i_crit_edge, %for.cond.for.end9_crit_edge.i
135 %4 = phi i32 [ %.pre, %entry.for.end9.i_crit_edge ], [ 2, %for.cond.for.end9_crit_edge.i ]
136 %5 = load i32*, i32** @b, align 8, !tbaa !7
137 store i32 %4, i32* %5, align 4, !tbaa !3
138 %6 = load i32, i32* @e, align 4, !tbaa !3
139 %cmp10.i = icmp slt i32 %6, -1
140 br i1 %cmp10.i, label %if.then.i, label %fn1.exit
141
142 if.then.i: ; preds = %for.end9.i
143 %7 = load i32, i32* @f, align 4, !tbaa !3
144 store i32 %7, i32* %5, align 4, !tbaa !3
145 br label %fn1.exit
146
147 fn1.exit: ; preds = %if.then.i, %for.end9.i
148 %8 = load i32, i32* @a, align 4, !tbaa !3
149 %tobool = icmp eq i32 %8, 0
150 br i1 %tobool, label %if.end, label %if.then
151
152 if.then: ; preds = %fn1.exit
153 %puts2 = tail call i32 @puts(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str.2, i64 0, i64 0))
154 tail call void @abort() #3
155 unreachable
156
157 if.end: ; preds = %fn1.exit
158 %puts = tail call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
159 ret i32 0
160 }
161
162 ; Function Attrs: noreturn nounwind optsize
163 declare void @abort() local_unnamed_addr #1
164
165 ; Function Attrs: nounwind
166 declare i32 @puts(i8* nocapture readonly) local_unnamed_addr #2
167
168 attributes #0 = { nounwind optsize ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
169 attributes #1 = { noreturn nounwind optsize "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
170 attributes #2 = { nounwind }
171 attributes #3 = { noreturn nounwind optsize }
172
173 !llvm.module.flags = !{!0, !1}
174 !llvm.ident = !{!2}
175
176 !0 = !{i32 1, !"wchar_size", i32 4}
177 !1 = !{i32 7, !"PIC Level", i32 2}
178 !2 = !{!"clang version 5.0.0 (http://llvm.org/git/clang.git e97b4dda83fd49e0218ea06ba4e37796a81b2027) (/Users/dannyb/sources/llvm-clean b38f051979e4ac2aa6513e40046d120fd472cb96)"}
179 !3 = !{!4, !4, i64 0}
180 !4 = !{!"int", !5, i64 0}
181 !5 = !{!"omnipotent char", !6, i64 0}
182 !6 = !{!"Simple C/C++ TBAA"}
183 !7 = !{!8, !8, i64 0}
184 !8 = !{!"any pointer", !5, i64 0}
0 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
1 ; RUN: opt -newgvn -S %s | FileCheck %s
2 ; Ensure we do not incorrect do phi of ops
3 @d = external local_unnamed_addr global i32, align 4
4
5 define void @patatino() {
6 ; CHECK-LABEL: @patatino(
7 ; CHECK-NEXT: entry:
8 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* @d, align 4
9 ; CHECK-NEXT: br label [[FOR_END10:%.*]]
10 ; CHECK: for.end10:
11 ; CHECK-NEXT: [[OR:%.*]] = or i32 [[TMP0]], 8
12 ; CHECK-NEXT: br i1 undef, label [[IF_END:%.*]], label [[FOR_END10]]
13 ; CHECK: if.end:
14 ; CHECK-NEXT: ret void
15 ;
16 entry:
17 %0 = load i32, i32* @d, align 4
18 br label %for.end10
19
20 for.end10:
21 %f.0 = phi i32 [ undef, %entry ], [ 8, %for.end10 ]
22 %or = or i32 %0, %f.0
23 %mul12 = mul nsw i32 %or, undef
24 br i1 undef, label %if.end, label %for.end10
25
26 if.end:
27 ret void
28 }
29
77 ; CHECK-NEXT: entry:
88 ; CHECK-NEXT: br i1 false, label [[FOR_COND1:%.*]], label [[FOR_INC:%.*]]
99 ; CHECK: for.cond1:
10 ; CHECK-NEXT: [[TMP0:%.*]] = phi i16 [ [[INC:%.*]], [[FOR_INC]] ], [ undef, [[ENTRY:%.*]] ]
11 ; CHECK-NEXT: store i16 [[TMP0]], i16* @b, align 2
10 ; CHECK-NEXT: [[PHIOFOPS:%.*]] = phi i16 [ [[INC:%.*]], [[FOR_INC]] ], [ undef, [[ENTRY:%.*]] ]
11 ; CHECK-NEXT: store i16 [[PHIOFOPS]], i16* @b, align 2
1212 ; CHECK-NEXT: br label [[FOR_INC]]
1313 ; CHECK: for.inc:
14 ; CHECK-NEXT: [[TMP1:%.*]] = load i16, i16* @b, align 2
15 ; CHECK-NEXT: [[INC]] = add i16 [[TMP1]], 1
14 ; CHECK-NEXT: [[TMP0:%.*]] = load i16, i16* @b, align 2
15 ; CHECK-NEXT: [[INC]] = add i16 [[TMP0]], 1
1616 ; CHECK-NEXT: store i16 [[INC]], i16* @b, align 2
1717 ; CHECK-NEXT: br label [[FOR_COND1]]
1818 ;