From f205f3b0f444e1d713b8d0eee1ca135b403e641b Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Wed, 26 Oct 2016 14:53:50 +0000 Subject: [PATCH] Fix nondeterministic output in local stack slot alloc pass This finds all of the references to a frame index in a function, and sorts by the offset. If multiple instructions use the same offset, nothing was breaking the tie for sorting. This avoids the test failures the reverted r282999 introduced. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@285201 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/LocalStackSlotAllocation.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/CodeGen/LocalStackSlotAllocation.cpp b/lib/CodeGen/LocalStackSlotAllocation.cpp index b86406e2fcd..e189fb0dd89 100644 --- a/lib/CodeGen/LocalStackSlotAllocation.cpp +++ b/lib/CodeGen/LocalStackSlotAllocation.cpp @@ -51,13 +51,21 @@ namespace { MachineBasicBlock::iterator MI; // Instr referencing the frame int64_t LocalOffset; // Local offset of the frame idx referenced int FrameIdx; // The frame index + + // Order reference instruction appears in program. Used to ensure + // deterministic order when multiple instructions may reference the same + // location. + unsigned Order; + public: - FrameRef(MachineBasicBlock::iterator I, int64_t Offset, int Idx) : - MI(I), LocalOffset(Offset), FrameIdx(Idx) {} + FrameRef(MachineInstr *I, int64_t Offset, int Idx, unsigned Ord) : + MI(I), LocalOffset(Offset), FrameIdx(Idx), Order(Ord) {} + bool operator<(const FrameRef &RHS) const { - return std::tie(LocalOffset, FrameIdx) < - std::tie(RHS.LocalOffset, RHS.FrameIdx); + return std::tie(LocalOffset, FrameIdx, Order) < + std::tie(RHS.LocalOffset, RHS.FrameIdx, RHS.Order); } + MachineBasicBlock::iterator getMachineInstr() const { return MI; } int64_t getLocalOffset() const { return LocalOffset; } int getFrameIndex() const { return FrameIdx; } @@ -78,7 +86,7 @@ namespace { bool insertFrameReferenceRegisters(MachineFunction &Fn); public: static char ID; // Pass identification, replacement for typeid - explicit LocalStackSlotPass() : MachineFunctionPass(ID) { + explicit LocalStackSlotPass() : MachineFunctionPass(ID) { initializeLocalStackSlotPassPass(*PassRegistry::getPassRegistry()); } bool runOnMachineFunction(MachineFunction &MF) override; @@ -286,6 +294,8 @@ bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) { // choose the first one). SmallVector FrameReferenceInsns; + unsigned Order = 0; + for (MachineBasicBlock &BB : Fn) { for (MachineInstr &MI : BB) { // Debug value, stackmap and patchpoint instructions can't be out of @@ -312,7 +322,7 @@ bool LocalStackSlotPass::insertFrameReferenceRegisters(MachineFunction &Fn) { int64_t LocalOffset = LocalOffsets[Idx]; if (!TRI->needsFrameBaseReg(&MI, LocalOffset)) break; - FrameReferenceInsns.push_back(FrameRef(&MI, LocalOffset, Idx)); + FrameReferenceInsns.push_back(FrameRef(&MI, LocalOffset, Idx, Order++)); break; } } -- 2.40.0