@@ -401,7 +401,7 @@ namespace {
401401 void applyCopiesOptimized ();
402402 void applyCopiesForValue (const std::set<CopyData> &CDSet);
403403 template <typename Iter>
404- Iter mergeCopiesTillFailed (SimpleValue CopySV, Iter BeginIt , Iter EndIt);
404+ LiveRange* mergeCopiesTillFailed (SimpleValue CopySV, Iter &It , Iter EndIt);
405405 // Helpers
406406 DominatorTree *getDomTree (Function *F) const {
407407 return DTWrapper->getDomTree (F);
@@ -1930,15 +1930,14 @@ void GenXCoalescing::applyCopiesForValue(const std::set<CopyData> &CDSet) {
19301930 // is used to detect possible interference.
19311931 auto CopySV = SimpleValue (CurrCopy, Idx);
19321932 Liveness->removeValueNoDelete (CopySV);
1933- auto *CopyLR = Liveness->buildLiveRange (CopySV);
19341933 auto *DestLR = Liveness->getLiveRange (DestSV);
19351934
19361935 LLVM_DEBUG (dbgs () << " Created copy for LR: " ; DestLR->print (dbgs ());
19371936 dbgs () << " \n Value that was copied: " ; SourceSV.print (dbgs ());
19381937 dbgs () << " \n Copy inst: " ; CopySV.print (dbgs ()); dbgs () << " \n " );
19391938
19401939 // Try to apply this copy in other copy candidates.
1941- It = mergeCopiesTillFailed (CopySV, ++It, EndIt);
1940+ auto *CopyLR = mergeCopiesTillFailed (CopySV, ++It, EndIt);
19421941
19431942 LLVM_DEBUG (dbgs () << " Finished processing all candidates for that copy\n " ;
19441943 dbgs () << " Final copy val LR: " ; CopyLR->print (dbgs ());
@@ -1962,26 +1961,26 @@ void GenXCoalescing::applyCopiesForValue(const std::set<CopyData> &CDSet) {
19621961 * For more details, check applyCopiesOptimized description.
19631962 */
19641963template <typename Iter>
1965- Iter GenXCoalescing::mergeCopiesTillFailed (SimpleValue CopySV, Iter BeginIt,
1966- Iter EndIt) {
1967- if (BeginIt == EndIt)
1968- return EndIt;
1969-
1964+ LiveRange *GenXCoalescing::mergeCopiesTillFailed (SimpleValue CopySV, Iter &It,
1965+ Iter EndIt) {
19701966 auto *CopyLR = Liveness->buildLiveRange (CopySV);
1971- auto *DestLR = Liveness->getLiveRange (BeginIt->Dest );
1967+ if (It == EndIt)
1968+ return CopyLR;
1969+
1970+ auto *DestLR = Liveness->getLiveRange (It->Dest );
19721971 Instruction *CurrCopy = cast<Instruction>(CopySV.getValue ());
19731972
1974- for ( auto It = BeginIt; It != EndIt; ++It ) {
1973+ while ( It != EndIt) {
19751974 // Interference detection
19761975 if (Liveness->interfere (DestLR, CopyLR)) {
19771976 LLVM_DEBUG (dbgs () << " Interference detected\n " );
1978- return It ;
1977+ return CopyLR ;
19791978 }
19801979 // Dominance detection
19811980 if (!getDomTree (CurrCopy->getFunction ())
19821981 ->dominates (CurrCopy, cast<Instruction>(It->Dest .getValue ()))) {
19831982 LLVM_DEBUG (dbgs () << " Copy doesn't dominate user\n " );
1984- return It ;
1983+ return CopyLR ;
19851984 }
19861985
19871986 // Copy may be redundant. Check interference after copy applied.
@@ -1992,8 +1991,9 @@ Iter GenXCoalescing::mergeCopiesTillFailed(SimpleValue CopySV, Iter BeginIt,
19921991 if (It->Source .getValue ()->getType () == CurrCopy->getType ()) {
19931992 *It->UseInDest = CurrCopy;
19941993 } else {
1995- IGC_ASSERT_MESSAGE (It->Source .getIndex () == 0 ,
1996- " Must be non-aggregated type: should come from bitcast" );
1994+ IGC_ASSERT_MESSAGE (
1995+ It->Source .getIndex () == 0 ,
1996+ " Must be non-aggregated type: should come from bitcast" );
19971997 IRBuilder<> Builder (CurrCopy->getNextNode ());
19981998 BCI = cast<BitCastInst>(Builder.CreateBitCast (
19991999 CurrCopy, It->Source .getValue ()->getType (), " red_copy_type_conv" ));
@@ -2004,23 +2004,23 @@ Iter GenXCoalescing::mergeCopiesTillFailed(SimpleValue CopySV, Iter BeginIt,
20042004 }
20052005
20062006 Liveness->rebuildLiveRange (CopyLR);
2007- if (!Liveness->twoAddrInterfere (DestLR, CopyLR)) {
2008- LLVM_DEBUG (dbgs () << " Success. Moving to next candidate\n " );
2009- continue ;
2007+ if (Liveness->twoAddrInterfere (DestLR, CopyLR)) {
2008+ // Undo copy elimination
2009+ LLVM_DEBUG (dbgs () << " Interference detected\n " );
2010+ *It->UseInDest = OldValue;
2011+ if (BCI) {
2012+ Liveness->removeValue (BCI);
2013+ BCI->eraseFromParent ();
2014+ }
2015+ Liveness->rebuildLiveRange (CopyLR);
2016+ return CopyLR;
20102017 }
20112018
2012- // Undo copy elimination
2013- LLVM_DEBUG (dbgs () << " Interference detected\n " );
2014- *It->UseInDest = OldValue;
2015- if (BCI) {
2016- Liveness->removeValue (BCI);
2017- BCI->eraseFromParent ();
2018- }
2019- Liveness->rebuildLiveRange (CopyLR);
2020- return It;
2019+ LLVM_DEBUG (dbgs () << " Success. Moving to next candidate\n " );
2020+ It++;
20212021 }
20222022
2023- return EndIt ;
2023+ return CopyLR ;
20242024}
20252025
20262026/* **********************************************************************
0 commit comments