Skip to content

Commit 085dc63

Browse files
authored
[llvm] Make use of llvm::reverse_conditionally() in a few places (NFCI) (#171150)
1 parent ca927e5 commit 085dc63

File tree

3 files changed

+16
-39
lines changed

3 files changed

+16
-39
lines changed

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -911,29 +911,18 @@ void PEIImpl::calculateFrameObjectOffsets(MachineFunction &MF) {
911911
Align MaxAlign = MFI.getMaxAlign();
912912
// First assign frame offsets to stack objects that are used to spill
913913
// callee saved registers.
914-
if (StackGrowsDown) {
915-
for (int FI = MFI.getObjectIndexBegin(); FI < MFI.getObjectIndexEnd();
916-
FI++) {
917-
// Only allocate objects on the default stack.
918-
if (!MFI.isCalleeSavedObjectIndex(FI) ||
919-
MFI.getStackID(FI) != TargetStackID::Default)
920-
continue;
921-
// TODO: should we be using MFI.isDeadObjectIndex(FI) here?
922-
AdjustStackOffset(MFI, FI, StackGrowsDown, Offset, MaxAlign);
923-
}
924-
} else {
925-
for (int FI = MFI.getObjectIndexEnd() - 1; FI >= MFI.getObjectIndexBegin();
926-
FI--) {
927-
// Only allocate objects on the default stack.
928-
if (!MFI.isCalleeSavedObjectIndex(FI) ||
929-
MFI.getStackID(FI) != TargetStackID::Default)
930-
continue;
914+
auto AllFIs = seq(MFI.getObjectIndexBegin(), MFI.getObjectIndexEnd());
915+
for (int FI : reverse_conditionally(AllFIs, /*Reverse=*/!StackGrowsDown)) {
916+
// Only allocate objects on the default stack.
917+
if (!MFI.isCalleeSavedObjectIndex(FI) ||
918+
MFI.getStackID(FI) != TargetStackID::Default)
919+
continue;
931920

932-
if (MFI.isDeadObjectIndex(FI))
933-
continue;
921+
// TODO: should this just be if (MFI.isDeadObjectIndex(FI))
922+
if (!StackGrowsDown && MFI.isDeadObjectIndex(FI))
923+
continue;
934924

935-
AdjustStackOffset(MFI, FI, StackGrowsDown, Offset, MaxAlign);
936-
}
925+
AdjustStackOffset(MFI, FI, StackGrowsDown, Offset, MaxAlign);
937926
}
938927

939928
assert(MaxAlign == MFI.getMaxAlign() &&

llvm/lib/CodeGen/RegisterClassInfo.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,7 @@ void RegisterClassInfo::compute(const TargetRegisterClass *RC) const {
145145
// FIXME: Once targets reserve registers instead of removing them from the
146146
// allocation order, we can simply use begin/end here.
147147
ArrayRef<MCPhysReg> RawOrder = RC->getRawAllocationOrder(*MF, Reverse);
148-
std::vector<MCPhysReg> ReverseOrder;
149-
if (Reverse) {
150-
llvm::append_range(ReverseOrder, reverse(RawOrder));
151-
RawOrder = ArrayRef<MCPhysReg>(ReverseOrder);
152-
}
153-
for (unsigned PhysReg : RawOrder) {
148+
for (unsigned PhysReg : reverse_conditionally(RawOrder, Reverse)) {
154149
// Remove reserved registers from the allocation order.
155150
if (Reserved.test(PhysReg))
156151
continue;

llvm/lib/MC/MCWin64EH.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,11 @@ static void simplifyARM64Opcodes(std::vector<WinEH::Instruction> &Instructions,
887887
unsigned PrevOffset = -1;
888888
unsigned PrevRegister = -1;
889889

890-
auto VisitInstruction = [&](WinEH::Instruction &Inst) {
890+
// Iterate over instructions in a forward order (for prologues),
891+
// backwards for epilogues (i.e. always reverse compared to how the
892+
// opcodes are stored).
893+
for (WinEH::Instruction &Inst :
894+
llvm::reverse_conditionally(Instructions, Reverse)) {
891895
// Convert 2-byte opcodes into equivalent 1-byte ones.
892896
if (Inst.Operation == Win64EH::UOP_SaveRegP && Inst.Register == 29) {
893897
Inst.Operation = Win64EH::UOP_SaveFPLR;
@@ -930,17 +934,6 @@ static void simplifyARM64Opcodes(std::vector<WinEH::Instruction> &Instructions,
930934
PrevRegister = -1;
931935
PrevOffset = -1;
932936
}
933-
};
934-
935-
// Iterate over instructions in a forward order (for prologues),
936-
// backwards for epilogues (i.e. always reverse compared to how the
937-
// opcodes are stored).
938-
if (Reverse) {
939-
for (auto It = Instructions.rbegin(); It != Instructions.rend(); It++)
940-
VisitInstruction(*It);
941-
} else {
942-
for (WinEH::Instruction &Inst : Instructions)
943-
VisitInstruction(Inst);
944937
}
945938
}
946939

0 commit comments

Comments
 (0)