If LSR went through a lot of trouble to put constants (e.g. the addr of a global
authorChris Lattner <sabre@nondot.org>
Wed, 13 Sep 2006 06:02:42 +0000 (06:02 +0000)
committerChris Lattner <sabre@nondot.org>
Wed, 13 Sep 2006 06:02:42 +0000 (06:02 +0000)
in a specific BB, don't undo this!).  This allows us to compile
CodeGen/X86/loop-hoist.ll into:

_foo:
        xorl %eax, %eax
***     movl L_Arr$non_lazy_ptr, %ecx
        movl 4(%esp), %edx
LBB1_1: #cond_true
        movl %eax, (%ecx,%eax,4)
        incl %eax
        cmpl %edx, %eax
        jne LBB1_1      #cond_true
LBB1_2: #return
        ret

instead of:

_foo:
        xorl %eax, %eax
        movl 4(%esp), %ecx
LBB1_1: #cond_true
***     movl L_Arr$non_lazy_ptr, %edx
        movl %eax, (%edx,%eax,4)
        incl %eax
        cmpl %ecx, %eax
        jne LBB1_1      #cond_true
LBB1_2: #return
        ret

This was noticed in 464.h264ref.  This doesn't usually affect PPC,
but strikes X86 all the time.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@30290 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp

index 4c4b0f951d5e0ec9b16fb72183296facb077bd16..c9b8f60e77777ea85fd95e75e39a968d60bfbfc2 100644 (file)
@@ -3234,6 +3234,15 @@ bool SelectionDAGISel::runOnFunction(Function &Fn) {
       if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
         MadeChange |= OptimizeGEPExpression(GEPI, TLI.getTargetData());
       } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
+        // If the source of the cast is a constant, then this should have
+        // already been constant folded.  The only reason NOT to constant fold
+        // it is if something (e.g. LSR) was careful to place the constant
+        // evaluation in a block other than then one that uses it (e.g. to hoist
+        // the address of globals out of a loop).  If this is the case, we don't
+        // want to forward-subst the cast.
+        if (isa<Constant>(CI->getOperand(0)))
+          continue;
+        
         // If this is a noop copy, sink it into user blocks to reduce the number
         // of virtual registers that must be created and coallesced.
         MVT::ValueType SrcVT = TLI.getValueType(CI->getOperand(0)->getType());