[llvm-commits] CVS: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Chris Lattner sabre at nondot.org
Fri Apr 13 13:42:59 PDT 2007



Changes in directory llvm/lib/Transforms/Scalar:

LoopStrengthReduce.cpp updated: 1.128 -> 1.129
---
Log message:

Now that codegen prepare isn't defeating me, I can finally fix what I set
out to do! :)

This fixes a problem where LSR would insert a bunch of code into each MBB
that uses a particular subexpression (e.g. IV+base+C).  The problem is that
this code cannot be CSE'd back together if inserted into different blocks.

This patch changes LSR to attempt to insert a single copy of this code and
share it, allowing codegenprepare to duplicate the code if it can be sunk
into various addressing modes.  On CodeGen/ARM/lsr-code-insertion.ll,
for example, this gives us code like:

        add r8, r0, r5
        str r6, [r8, #+4]
..
        ble LBB1_4      @cond_next
LBB1_3: @cond_true
        str r10, [r8, #+4]
LBB1_4: @cond_next
...
LBB1_5: @cond_true55
        ldr r6, LCPI1_1
        str r6, [r8, #+4]

instead of:

        add r10, r0, r6
        str r8, [r10, #+4]
...
        ble LBB1_4      @cond_next
LBB1_3: @cond_true
        add r8, r0, r6
        str r10, [r8, #+4]
LBB1_4: @cond_next
...
LBB1_5: @cond_true55
        add r8, r0, r6
        ldr r10, LCPI1_1
        str r10, [r8, #+4]

Besides being smaller and more efficient, this makes it immediately 
obvious that it is profitable to predicate LBB1_3 now :)



---
Diffs of the changes:  (+19 -1)

 LoopStrengthReduce.cpp |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletion(-)


Index: llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
diff -u llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:1.128 llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:1.129
--- llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp:1.128	Mon Apr  9 17:20:14 2007
+++ llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp	Fri Apr 13 15:42:26 2007
@@ -568,7 +568,25 @@
                                                SCEVExpander &Rewriter,
                                                Loop *L, Pass *P) {
   if (!isa<PHINode>(Inst)) {
-    Value *NewVal = InsertCodeForBaseAtPosition(NewBase, Rewriter, Inst, L);
+    // By default, insert code at the user instruction.
+    BasicBlock::iterator InsertPt = Inst;
+    
+    // However, if the Operand is itself an instruction, the (potentially
+    // complex) inserted code may be shared by many users.  Because of this, we
+    // want to emit code for the computation of the operand right before its old
+    // computation.  This is usually safe, because we obviously used to use the
+    // computation when it was computed in its current block.  However, in some
+    // cases (e.g. use of a post-incremented induction variable) the NewBase
+    // value will be pinned to live somewhere after the original computation.
+    // In this case, we have to back off.
+    if (!isUseOfPostIncrementedValue) {
+      if (Instruction *OpInst = dyn_cast<Instruction>(OperandValToReplace)) { 
+        InsertPt = OpInst;
+        while (isa<PHINode>(InsertPt)) ++InsertPt;
+      }
+    }
+    
+    Value *NewVal = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L);
     // Replace the use of the operand Value with the new Phi we just created.
     Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
     DOUT << "    CHANGED: IMM =" << *Imm << "  Inst = " << *Inst;






More information about the llvm-commits mailing list