MachineMemOperands not propageted in ARMLoadStoreOptimizer.cpp
| Bugzilla Link | 8113 |
| Resolution | FIXED |
| Resolved on | Nov 11, 2011 16:39 |
| Version | trunk |
| OS | Linux |
| CC | @asl,@atrick |
Extended Description
A potential bug in ARMLoadStoreOptimizer.cpp related to the questions asked in the following link (It doesn't seem to result in generation of incorrect code, but I am not sure whether it is correct not to propagate MachineMemOperands). http://lists.cs.uiuc.edu/pipermail/llvmdev/2010-September/034485.html
Here are the sample source file and .ll file I used.
-
input source file void f(int a, int b, int n) { int i = 0;
for (i = 0; i < n - 1; ++i) b[i] = a[i] + a[i + 1]; }
-
bc file, generated by entering
llvm-gcc -emit-llvm -O3 -c f.c -o f.bc -m32
; ModuleID = '
define void @f(i32* nocapture %a, i32* nocapture %b, i32 %n) nounwind {
entry:
%0 = add nsw i32 %n, -1 ;
bb: ; preds = %bb, %entry
%i.03 = phi i32 [ %tmp, %bb ], [ 0, %entry ] ;
return: ; preds = %bb, %entry ret void }
gdb llc
(gdb) b ARMLoadStoreOptimizer.cpp:1504
(gdb) r -march=arm -mcpu=cortex-a8 f.bc -o f.arm.s
(gdb) p MF->dump()
...
BB#2: derived from LLVM BB %bb
Predecessors according to CFG: BB#1 BB#2
%reg16389
When the two LDR instructions are removed from the BB and a new LDRD instruction is added, the MachineMemOperands attached to the LDR instructions (mem:LD4[%scevgep9] and mem:LD4[%lsr.iv3]) do not get propagated.
I celebrated this bug's anniversary by trying to reproduce it again. It turns out there were a bunch of issues at play. The main problem I think is that LSR generates negative offsets. ARM ISEL doesn't fold those into loads. Thumb mode does, but uses different encoding for zero offset and negative offset. In the end, I gave up on this test case and just added my own: ldrd-memop.ll
The fix I came up with in r144409 is slightly different than the proposed patch.
I ran the full llvm test suite for both cortex-a8 and a9, and both thumb and -mno-thumb.
And reverted because it broke 176.gcc.
Fixed: r119109.
Would this fix the problem?
--- ARMLoadStoreOptimizer.cpp (original) +++ ARMLoadStoreOptimizer.cpp 2010-09-08 17:17:54.000000000 -0700 @@ -1501,10 +1501,7 @@ if (NumMove == 2 && CanFormLdStDWord(Op0, Op1, dl, NewOpc, EvenReg, OddReg, BaseReg, OffReg, Offset, PredReg, Pred, isT2)) {
-
Ops.pop_back(); -
Ops.pop_back(); -
// Form the pair instruction.
-
// Form the pair instruction. if (isLd) { MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos, dl, TII->get(NewOpc))
@@ -1514,6 +1511,11 @@ if (!isT2) MIB.addReg(OffReg); MIB.addImm(Offset).addImm(Pred).addReg(PredReg); +
-
for (unsigned op = 0; op < Ops.size(); ++op) -
for (MachineInstr::mmo_iterator mmo = Ops[op]->memoperands_begin(); -
mmo != Ops[op]->memoperands_end(); ++mmo) -
MIB.addMemOperand(*mmo); ++NumLDRDFormed; } else { MachineInstrBuilder MIB = BuildMI(*MBB, InsertPos,
@@ -1524,10 +1526,17 @@ if (!isT2) MIB.addReg(OffReg); MIB.addImm(Offset).addImm(Pred).addReg(PredReg);
-
for (unsigned op = 0; op < Ops.size(); ++op) -
for (MachineInstr::mmo_iterator mmo = Ops[op]->memoperands_begin(); -
mmo != Ops[op]->memoperands_end(); ++mmo) -
MIB.addMemOperand(*mmo); ++NumSTRDFormed; }
-
MBB->erase(Op0); -
MBB->erase(Op1);
-
Ops.pop_back(); -
Ops.pop_back(); -
MBB->erase(Op0); -
MBB->erase(Op1); // Add register allocation hints to form register pairs. MRI->setRegAllocationHint(EvenReg, ARMRI::RegPairEven, OddReg);