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.
Details
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
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? |
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.
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. |
llvm/lib/MC/MCAssembler.cpp | ||
---|---|---|
824 | They have trivial constructors. Moving it can be clearer/more efficient. |
llvm/lib/MC/MCAssembler.cpp | ||
---|---|---|
836 | if shouldInsertFixupForCodeAlign returns a bool, should we be checking the return code here? |
llvm/lib/MC/MCAssembler.cpp | ||
---|---|---|
836 | I think not, the return value of shouldInsertFixupForCodeAlign depends on the target. |
Can you move MCAlignFragment into the switch as well?