This is an archive of the discontinued LLVM Phabricator instance.

[MergeICmps] Attach metadata to new created loads
ClosedPublic

Authored by Allen on Mar 23 2023, 2:07 AM.

Diff Detail

Event Timeline

Allen created this revision.Mar 23 2023, 2:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 23 2023, 2:07 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
Allen requested review of this revision.Mar 23 2023, 2:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 23 2023, 2:07 AM
Allen added a reviewer: nikic.Mar 23 2023, 2:17 AM
nikic added a comment.Mar 23 2023, 4:57 AM

I think this is right, but I'm not very confident about it. The problem is that mergeicmps can reorder loads, but I think these isolated comparisons will not get reordered before other ones (only possibly after), so I think it's fine. But maybe I just didn't manage to construct the right test case.

llvm/lib/Transforms/Scalar/MergeICmps.cpp
646

Maybe it would be better to clone the instructions, similar to the GEPs above?

Allen added inline comments.Mar 24 2023, 8:47 PM
llvm/lib/Transforms/Scalar/MergeICmps.cpp
646

Thanks for your suggestion.
I tried with following change, and find the case Transforms/MergeICmps/X86/entry-block-shuffled.ll will fail.
this is because we only clone the load instrunction itself, and its PointerOperand will be deleted with following DeleteDeadBlocks(DeadBlocks, &DTU) in function BCECmpChain::simplify, then %4 = load i32, ptr %first.i, align 4 will become invalid instruction %4 = load i32, ptr poison, align 4. Did I miss something ?

@@ -639,10 +640,9 @@ static BasicBlock *mergeComparisons(ArrayRef<BCECmpBlock> Comparisons,
 
   if (Comparisons.size() == 1) {
     LLVM_DEBUG(dbgs() << "Only one comparison, updating branches\n");
-    Value *const LhsLoad =
-        Builder.CreateLoad(FirstCmp.Lhs().LoadI->getType(), Lhs);
-    Value *const RhsLoad =
-        Builder.CreateLoad(FirstCmp.Rhs().LoadI->getType(), Rhs);
+    Instruction *const LhsLoad = Builder.Insert(FirstCmp.Lhs().LoadI->clone());
+    Instruction *const RhsLoad = Builder.Insert(FirstCmp.Rhs().LoadI->clone());
Allen added a comment.Mar 26 2023, 7:06 PM

hi @nikic

If you don't object, I still use the current solution with **new CreateLoad** temporarily?
Allen added a comment.EditedMar 28 2023, 6:53 AM

I find https://reviews.llvm.org/D119319 also use this method ?

Allen updated this revision to Diff 510013.Mar 31 2023, 6:40 AM
Allen edited the summary of this revision. (Show Details)
Allen marked an inline comment as done.Mar 31 2023, 6:47 AM
Allen added inline comments.
llvm/lib/Transforms/Scalar/MergeICmps.cpp
646

I use replaceUsesOfWith to update the PointerOperand, then clone according your comment works fine.

Allen added a comment.Apr 3 2023, 10:28 AM

I updated with API clone

paulwalker-arm accepted this revision.Apr 6 2023, 4:50 AM
This revision is now accepted and ready to land.Apr 6 2023, 4:50 AM
This revision was landed with ongoing or failed builds.Apr 7 2023, 7:51 PM
This revision was automatically updated to reflect the committed changes.