@@ -536,29 +536,10 @@ void PruningFunctionCloner::CloneBlock(
536
536
// Eagerly remap operands to the newly cloned instruction, except for PHI
537
537
// nodes for which we defer processing until we update the CFG. Also defer
538
538
// debug intrinsic processing because they may contain use-before-defs.
539
- if (!isa<PHINode>(NewInst) && !isa<DbgVariableIntrinsic>(NewInst)) {
539
+ if (!isa<PHINode>(NewInst) && !isa<DbgVariableIntrinsic>(NewInst))
540
540
RemapInstruction (NewInst, VMap,
541
541
ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges);
542
542
543
- // If we can simplify this instruction to some other value, simply add
544
- // a mapping to that value rather than inserting a new instruction into
545
- // the basic block.
546
- if (Value *V =
547
- simplifyInstruction (NewInst, BB->getModule ()->getDataLayout ())) {
548
- // On the off-chance that this simplifies to an instruction in the old
549
- // function, map it back into the new function.
550
- if (NewFunc != OldFunc)
551
- if (Value *MappedV = VMap.lookup (V))
552
- V = MappedV;
553
-
554
- if (!NewInst->mayHaveSideEffects ()) {
555
- VMap[&*II] = V;
556
- NewInst->eraseFromParent ();
557
- continue ;
558
- }
559
- }
560
- }
561
-
562
543
if (II->hasName ())
563
544
NewInst->setName (II->getName () + NameSuffix);
564
545
VMap[&*II] = NewInst; // Add instruction map to value.
@@ -823,52 +804,34 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
823
804
}
824
805
}
825
806
826
- // Make a second pass over the PHINodes now that all of them have been
827
- // remapped into the new function, simplifying the PHINode and performing any
828
- // recursive simplifications exposed. This will transparently update the
829
- // WeakTrackingVH in the VMap. Notably, we rely on that so that if we coalesce
830
- // two PHINodes, the iteration over the old PHIs remains valid, and the
831
- // mapping will just map us to the new node (which may not even be a PHI
832
- // node).
807
+ // As phi-nodes have been now remapped, allow incremental simplification of
808
+ // newly-cloned instructions.
833
809
const DataLayout &DL = NewFunc->getParent ()->getDataLayout ();
834
- SmallSetVector<const Value *, 8 > Worklist;
835
- for (unsigned Idx = 0 , Size = PHIToResolve.size (); Idx != Size; ++Idx)
836
- if (isa<PHINode>(VMap[PHIToResolve[Idx]]))
837
- Worklist.insert (PHIToResolve[Idx]);
838
-
839
- // Note that we must test the size on each iteration, the worklist can grow.
840
- for (unsigned Idx = 0 ; Idx != Worklist.size (); ++Idx) {
841
- const Value *OrigV = Worklist[Idx];
842
- auto *I = dyn_cast_or_null<Instruction>(VMap.lookup (OrigV));
843
- if (!I)
844
- continue ;
845
-
846
- // Skip over non-intrinsic callsites, we don't want to remove any nodes from
847
- // the CGSCC.
848
- CallBase *CB = dyn_cast<CallBase>(I);
849
- if (CB && CB->getCalledFunction () &&
850
- !CB->getCalledFunction ()->isIntrinsic ())
851
- continue ;
852
-
853
- // See if this instruction simplifies.
854
- Value *SimpleV = simplifyInstruction (I, DL);
855
- if (!SimpleV)
856
- continue ;
857
-
858
- // Stash away all the uses of the old instruction so we can check them for
859
- // recursive simplifications after a RAUW. This is cheaper than checking all
860
- // uses of To on the recursive step in most cases.
861
- for (const User *U : OrigV->users ())
862
- Worklist.insert (cast<Instruction>(U));
863
-
864
- // Replace the instruction with its simplified value.
865
- I->replaceAllUsesWith (SimpleV);
866
-
867
- // If the original instruction had no side effects, remove it.
868
- if (isInstructionTriviallyDead (I))
869
- I->eraseFromParent ();
870
- else
871
- VMap[OrigV] = I;
810
+ for (const auto &BB : *OldFunc) {
811
+ for (const auto &I : BB) {
812
+ auto *NewI = cast_or_null<Instruction>(VMap.lookup (&I));
813
+ if (!NewI)
814
+ continue ;
815
+
816
+ // Skip over non-intrinsic callsites, we don't want to remove any nodes
817
+ // from the CGSCC.
818
+ CallBase *CB = dyn_cast<CallBase>(NewI);
819
+ if (CB && CB->getCalledFunction () &&
820
+ !CB->getCalledFunction ()->isIntrinsic ())
821
+ continue ;
822
+
823
+ if (Value *V = simplifyInstruction (NewI, DL)) {
824
+ NewI->replaceAllUsesWith (V);
825
+
826
+ if (isInstructionTriviallyDead (NewI)) {
827
+ NewI->eraseFromParent ();
828
+ } else {
829
+ // Did not erase it? Restore the new instruction into VMap previously
830
+ // dropped by `ValueIsRAUWd`.
831
+ VMap[&I] = NewI;
832
+ }
833
+ }
834
+ }
872
835
}
873
836
874
837
// Remap debug intrinsic operands now that all values have been mapped.
0 commit comments