This is an archive of the discontinued LLVM Phabricator instance.

[MC] Simplify the logic of applying fixup for fragments, NFCI
ClosedPublic

Authored by skan on Jul 7 2020, 8:30 PM.

Details

Summary

Replace mutiple if else clauses with a switch clause and remove redundant checks. Before this patch, we need to add a statement like if(!isa<MCxxxFragment>(Frag)) here each time we add a new kind of MCEncodedFragment even if it has no fixups. After this patch, we don't need to do that.

Diff Detail

Event Timeline

skan created this revision.Jul 7 2020, 8:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 7 2020, 8:30 PM
skan updated this revision to Diff 276308.Jul 7 2020, 8:33 PM

Update comments

skan edited the summary of this revision. (Show Details)Jul 7 2020, 8:50 PM

Note that the number of lines actually increases... so I am not sure this is "simplification"

skan added a comment.Jul 7 2020, 10:02 PM

Note that the number of lines actually increases... so I am not sure this is "simplification"

The increased line number comes from } and break, the logic is simplified and corrected since we do not need to check isa<MCEncodedFragment> here. We extract the code for "MCAlignFragment" out to make it clear that it doesn't have fixup. If you care
about the increased line number here, I can remove the unnecessary "{}" here or use if statement.

Note that the number of lines actually increases... so I am not sure this is "simplification"

The increased line number comes from } and break, the logic is simplified and corrected since we do not need to check isa<MCEncodedFragment> here. We extract the code for "MCAlignFragment" out to make it clear that it doesn't have fixup. If you care
about the increased line number here, I can remove the unnecessary "{}" here or use if statement.

This is: if else if else if -> switch. I think it is just a refactoring and I agree that it is the right thing to do, but I don't find complexity being simplified.

llvm/lib/MC/MCAssembler.cpp
824

Can you move MCAlignFragment into the switch as well?

skan added a comment.EditedJul 7 2020, 10:52 PM

This is: if else if else if -> switch. I think it is just a refactoring and I agree that it is the right thing to do, but I don't find complexity being simplified.

In addition to if else -> switch. The simplified thing is that we don't need to check isa<MCEncodedFragment>(&Frag) in the new code.
In the old code, if we want to add a new kind of fragment that inherits from MCEncodedFragment but is not a MCEncodedFragmentWithFixups, we have to add if(!isa<MCxxxFragment>(Frag) continue) here to avoid reaching "llvm_unreachable("Unknown fragment with fixups!")", it doesn't make sense since the new fragment has nothing to do with fixup.

That is really more clear than old code. I +1 for this refine.

skan marked an inline comment as done.Jul 7 2020, 10:56 PM
skan added inline comments.
llvm/lib/MC/MCAssembler.cpp
824
ArrayRef<MCFixup> Fixups;
MutableArrayRef<char> Contents;
const MCSubtargetInfo *STI = nullptr;

for (const MCFixup &Fixup : Fixups) {
...

is not used by MCAlignFragment. I think an early continue here can make things more clear.

skan marked an inline comment as done.Jul 7 2020, 11:37 PM
MaskRay added inline comments.Jul 7 2020, 11:47 PM
llvm/lib/MC/MCAssembler.cpp
824

They have trivial constructors. Moving it can be clearer/more efficient.

skan updated this revision to Diff 276323.Jul 8 2020, 12:16 AM
skan marked an inline comment as done.

Address review comments

MaskRay accepted this revision.Jul 8 2020, 10:37 AM

Thanks!

This revision is now accepted and ready to land.Jul 8 2020, 10:37 AM
This revision was automatically updated to reflect the committed changes.
nickdesaulniers added inline comments.
llvm/lib/MC/MCAssembler.cpp
836

if shouldInsertFixupForCodeAlign returns a bool, should we be checking the return code here?

skan marked an inline comment as done.Sep 29 2021, 12:03 AM
skan added inline comments.
llvm/lib/MC/MCAssembler.cpp
836

I think not, the return value of shouldInsertFixupForCodeAlign depends on the target.