llvm.org GIT mirror llvm / d2bfe54
Fix PR7647, handling the case when 'To' ends up being mutated by recursive simplification. This also enhances ReplaceAndSimplifyAllUses to actually do a real RAUW at the end of it, which updates any value handles pointing to "From" to start pointing to "To". This seems useful for debug info and random other VH users. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@108415 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 9 years ago
2 changed file(s) with 82 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
439439 const TargetData *TD) {
440440 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
441441
442 // FromHandle - This keeps a weakvh on the from value so that we can know if
443 // it gets deleted out from under us in a recursive simplification.
442 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
443 // we can know if it gets deleted out from under us or replaced in a
444 // recursive simplification.
444445 WeakVH FromHandle(From);
446 WeakVH ToHandle(To);
445447
446448 while (!From->use_empty()) {
447449 // Update the instruction to use the new value.
448 Use &U = From->use_begin().getUse();
449 Instruction *User = cast(U.getUser());
450 U = To;
450 Use &TheUse = From->use_begin().getUse();
451 Instruction *User = cast(TheUse.getUser());
452 TheUse = To;
453
454 // Check to see if the instruction can be folded due to the operand
455 // replacement. For example changing (or X, Y) into (or X, -1) can replace
456 // the 'or' with -1.
457 Value *SimplifiedVal;
458 {
459 // Sanity check to make sure 'User' doesn't dangle across
460 // SimplifyInstruction.
461 AssertingVH<> UserHandle(User);
451462
452 // See if we can simplify it.
453 if (Value *V = SimplifyInstruction(User, TD)) {
454 // Recursively simplify this.
455 ReplaceAndSimplifyAllUses(User, V, TD);
463 SimplifiedVal = SimplifyInstruction(User, TD);
464 if (SimplifiedVal == 0) continue;
465 }
466
467 // Recursively simplify this user to the new value.
468 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD);
469 From = dyn_cast_or_null((Value*)FromHandle);
470 To = ToHandle;
456471
457 // If the recursive simplification ended up revisiting and deleting 'From'
458 // then we're done.
459 if (FromHandle == 0)
460 return;
461 }
462 }
472 assert(ToHandle && "To value deleted by recursive simplification?");
473
474 // If the recursive simplification ended up revisiting and deleting
475 // 'From' then we're done.
476 if (From == 0)
477 return;
478 }
479
480 // If 'From' has value handles referring to it, do a real RAUW to update them.
481 From->replaceAllUsesWith(To);
482
463483 From->eraseFromParent();
464484 }
465485
386386 ret void
387387 }
388388
389 ; PR7647
390 define void @test15() nounwind {
391 entry:
392 ret void
393
394 if.then237:
395 br label %lbl_664
396
397 lbl_596: ; preds = %lbl_664, %for.end37
398 volatile store i64 undef, i64* undef, align 4
399 br label %for.cond111
400
401 for.cond111: ; preds = %safe_sub_func_int64_t_s_s.exit, %lbl_596
402 %storemerge = phi i8 [ undef, %cond.true.i100 ], [ 22, %lbl_596 ] ; [#uses=1]
403 %l_678.5 = phi i64 [ %l_678.3, %cond.true.i100 ], [ undef, %lbl_596 ] ; [#uses=2]
404 %cmp114 = icmp slt i8 %storemerge, -2 ; [#uses=1]
405 br i1 %cmp114, label %lbl_664, label %if.end949
406
407 lbl_664: ; preds = %for.end1058, %if.then237, %for.cond111
408 %l_678.3 = phi i64 [ %l_678.5, %for.cond111 ], [ %l_678.2, %for.cond1035 ], [ 5, %if.then237 ] ; [#uses=1]
409 %tobool118 = icmp eq i32 undef, 0 ; [#uses=1]
410 br i1 %tobool118, label %cond.true.i100, label %lbl_596
411
412 cond.true.i100: ; preds = %for.inc120
413 br label %for.cond111
414
415 lbl_709:
416 br label %if.end949
417
418 for.cond603: ; preds = %for.body607, %if.end336
419 br i1 undef, label %for.cond603, label %if.end949
420
421 if.end949: ; preds = %for.cond603, %lbl_709, %for.cond111
422 %l_678.2 = phi i64 [ %l_678.5, %for.cond111 ], [ undef, %lbl_709 ], [ 5, %for.cond603 ] ; [#uses=1]
423 br label %for.body1016
424
425 for.body1016: ; preds = %for.cond1012
426 br label %for.body1016
427
428 for.cond1035: ; preds = %for.inc1055, %if.then1026
429 br i1 undef, label %for.cond1040, label %lbl_664
430
431 for.cond1040: ; preds = %for.body1044, %for.cond1035
432 ret void
433 }
434
435