[llvm-branch-commits] [llvm-branch] r69367 - in /llvm/branches/Apple/Dib: include/llvm/Analysis/ include/llvm/CodeGen/ lib/Analysis/ lib/CodeGen/SelectionDAG/ lib/Target/X86/ lib/Transforms/Scalar/ test/CodeGen/ARM/ test/CodeGen/X86/

Bill Wendling isanbard at gmail.com
Fri Apr 17 11:49:45 PDT 2009


Author: void
Date: Fri Apr 17 13:49:34 2009
New Revision: 69367

URL: http://llvm.org/viewvc/llvm-project?rev=69367&view=rev
Log:
--- Merging (from foreign repository) r69203 into '.':
U    lib/Target/X86/X86InstrInfo.cpp
--- Merging (from foreign repository) r69204 into '.':
U    lib/Target/X86/X86InstrInfo.td
G    lib/Target/X86/X86InstrInfo.cpp
--- Merging (from foreign repository) r69209 into '.':
U    include/llvm/CodeGen/SelectionDAG.h
U    include/llvm/CodeGen/DAGISelHeader.h
U    lib/CodeGen/SelectionDAG/SelectionDAG.cpp
U    lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
--- Merging (from foreign repository) r69258 into '.':
U    test/CodeGen/ARM/2007-03-13-InstrSched.ll
U    test/CodeGen/X86/iv-users-in-other-loops.ll
U    test/CodeGen/X86/stride-nine-with-base-reg.ll
U    test/CodeGen/X86/pr3495.ll
U    include/llvm/Analysis/ScalarEvolutionExpander.h
U    include/llvm/Analysis/ScalarEvolution.h
U    lib/Analysis/ScalarEvolutionExpander.cpp
U    lib/Analysis/ScalarEvolution.cpp
U    lib/Transforms/Scalar/IndVarSimplify.cpp
U    lib/Transforms/Scalar/LoopStrengthReduce.cpp
--- Merging (from foreign repository) r69290 into '.':
G    lib/Transforms/Scalar/LoopStrengthReduce.cpp
--- Merging (from foreign repository) r69291 into '.':
G    lib/Transforms/Scalar/LoopStrengthReduce.cpp
--- Merging (from foreign repository) r69293 into '.':
G    include/llvm/Analysis/ScalarEvolutionExpander.h
G    lib/Analysis/ScalarEvolutionExpander.cpp
--- Merging (from foreign repository) r69294 into '.':
G    lib/Analysis/ScalarEvolutionExpander.cpp
--- Merging (from foreign repository) r69295 into '.':
G    lib/Transforms/Scalar/LoopStrengthReduce.cpp
--- Merging (from foreign repository) r69296 into '.':
G    lib/Transforms/Scalar/LoopStrengthReduce.cpp
--- Merging (from foreign repository) r69307 into '.':
G    lib/Analysis/ScalarEvolutionExpander.cpp
G    lib/Analysis/ScalarEvolution.cpp
--- Merging (from foreign repository) r69308 into '.':
U    lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
--- Merging (from foreign repository) r69309 into '.':
G    lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
--- Merging (from foreign repository) r69310 into '.':
G    lib/Analysis/ScalarEvolutionExpander.cpp
--- Merging (from foreign repository) r69321 into '.':
G    lib/Transforms/Scalar/LoopStrengthReduce.cpp


Modified:
    llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolution.h
    llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolutionExpander.h
    llvm/branches/Apple/Dib/include/llvm/CodeGen/DAGISelHeader.h
    llvm/branches/Apple/Dib/include/llvm/CodeGen/SelectionDAG.h
    llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolution.cpp
    llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolutionExpander.cpp
    llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
    llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
    llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
    llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.cpp
    llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.td
    llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp
    llvm/branches/Apple/Dib/lib/Transforms/Scalar/LoopStrengthReduce.cpp
    llvm/branches/Apple/Dib/test/CodeGen/ARM/2007-03-13-InstrSched.ll
    llvm/branches/Apple/Dib/test/CodeGen/X86/iv-users-in-other-loops.ll
    llvm/branches/Apple/Dib/test/CodeGen/X86/pr3495.ll
    llvm/branches/Apple/Dib/test/CodeGen/X86/stride-nine-with-base-reg.ll

Modified: llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolution.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolution.h?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolution.h (original)
+++ llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolution.h Fri Apr 17 13:49:34 2009
@@ -32,6 +32,7 @@
   class Type;
   class SCEVHandle;
   class ScalarEvolution;
+  class TargetData;
 
   /// SCEV - This class represent an analyzed expression in the program.  These
   /// are reference counted opaque objects that the client is not allowed to
@@ -71,10 +72,6 @@
     ///
     virtual const Type *getType() const = 0;
 
-    /// getBitWidth - Get the bit width of the type, if it has one, 0 otherwise.
-    /// 
-    uint32_t getBitWidth() const;
-
     /// isZero - Return true if the expression is a constant zero.
     ///
     bool isZero() const;
@@ -199,6 +196,10 @@
     static char ID; // Pass identification, replacement for typeid
     ScalarEvolution() : FunctionPass(&ID), Impl(0) {}
 
+    // getTargetData - Return the TargetData object contained in this
+    // ScalarEvolution.
+    const TargetData &getTargetData() const;
+
     /// getSCEV - Return a SCEV expression handle for the full generality of the
     /// specified expression.
     SCEVHandle getSCEV(Value *V) const;

Modified: llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolutionExpander.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolutionExpander.h?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolutionExpander.h (original)
+++ llvm/branches/Apple/Dib/include/llvm/Analysis/ScalarEvolutionExpander.h Fri Apr 17 13:49:34 2009
@@ -20,6 +20,8 @@
 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
 
 namespace llvm {
+  class TargetData;
+
   /// SCEVExpander - This class uses information about analyze scalars to
   /// rewrite expressions in canonical form.
   ///
@@ -29,6 +31,7 @@
   struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> {
     ScalarEvolution &SE;
     LoopInfo &LI;
+    const TargetData &TD;
     std::map<SCEVHandle, Value*> InsertedExpressions;
     std::set<Instruction*> InsertedInstructions;
 
@@ -36,7 +39,8 @@
 
     friend struct SCEVVisitor<SCEVExpander, Value*>;
   public:
-    SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {}
+    SCEVExpander(ScalarEvolution &se, LoopInfo &li, const TargetData &td)
+      : SE(se), LI(li), TD(td) {}
 
     LoopInfo &getLoopInfo() const { return LI; }
 
@@ -75,19 +79,19 @@
     /// expandCodeFor - Insert code to directly compute the specified SCEV
     /// expression into the program.  The inserted code is inserted into the
     /// specified block.
-    Value *expandCodeFor(SCEVHandle SH, Instruction *IP);
+    Value *expandCodeFor(SCEVHandle SH, const Type *Ty, Instruction *IP);
 
     /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
     /// we can to share the casts.
-    static Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V, 
-                                 const Type *Ty);
+    Value *InsertCastOfTo(Instruction::CastOps opcode, Value *V,
+                          const Type *Ty);
     /// InsertBinop - Insert the specified binary operator, doing a small amount
     /// of work to avoid inserting an obviously redundant operation.
     static Value *InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
                               Value *RHS, Instruction *InsertPt);
   protected:
     Value *expand(SCEV *S);
-    
+
     Value *visitConstant(SCEVConstant *S) {
       return S->getValue();
     }

Modified: llvm/branches/Apple/Dib/include/llvm/CodeGen/DAGISelHeader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/include/llvm/CodeGen/DAGISelHeader.h?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/include/llvm/CodeGen/DAGISelHeader.h (original)
+++ llvm/branches/Apple/Dib/include/llvm/CodeGen/DAGISelHeader.h Fri Apr 17 13:49:34 2009
@@ -80,15 +80,8 @@
 /// ReplaceUses - replace all uses of the old node F with the use
 /// of the new node T.
 void ReplaceUses(SDNode *F, SDNode *T) DISABLE_INLINE {
-  unsigned FNumVals = F->getNumValues();
-  unsigned TNumVals = T->getNumValues();
   ISelUpdater ISU(ISelPosition);
-  if (FNumVals != TNumVals) {
-    for (unsigned i = 0, e = std::min(FNumVals, TNumVals); i < e; ++i)
-     CurDAG->ReplaceAllUsesOfValueWith(SDValue(F, i), SDValue(T, i), &ISU);
-  } else {
-    CurDAG->ReplaceAllUsesWith(F, T, &ISU);
-  }
+  CurDAG->ReplaceAllUsesWith(F, T, &ISU);
 }
 
 /// SelectRoot - Top level entry to DAG instruction selector.

Modified: llvm/branches/Apple/Dib/include/llvm/CodeGen/SelectionDAG.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/include/llvm/CodeGen/SelectionDAG.h?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/include/llvm/CodeGen/SelectionDAG.h (original)
+++ llvm/branches/Apple/Dib/include/llvm/CodeGen/SelectionDAG.h Fri Apr 17 13:49:34 2009
@@ -711,7 +711,8 @@
   /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
   /// This can cause recursive merging of nodes in the DAG.  Use the first
   /// version if 'From' is known to have a single result, use the second
-  /// if you have two nodes with identical results, use the third otherwise.
+  /// if you have two nodes with identical results (or if 'To' has a superset
+  /// of the results of 'From'), use the third otherwise.
   ///
   /// These methods all take an optional UpdateListener, which (if not null) is
   /// informed about nodes that are deleted and modified due to recursive

Modified: llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolution.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolution.cpp Fri Apr 17 13:49:34 2009
@@ -69,16 +69,19 @@
 #include "llvm/Analysis/Dominators.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Assembly/Writer.h"
+#include "llvm/Target/TargetData.h"
 #include "llvm/Transforms/Scalar.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/ConstantRange.h"
+#include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/InstIterator.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/Streams.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/ADT/STLExtras.h"
 #include <ostream>
 #include <algorithm>
 #include <cmath>
@@ -116,12 +119,6 @@
   cerr << '\n';
 }
 
-uint32_t SCEV::getBitWidth() const {
-  if (const IntegerType* ITy = dyn_cast<IntegerType>(getType()))
-    return ITy->getBitWidth();
-  return 0;
-}
-
 bool SCEV::isZero() const {
   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
     return SC->getValue()->isZero();
@@ -196,10 +193,13 @@
 
 SCEVTruncateExpr::SCEVTruncateExpr(const SCEVHandle &op, const Type *ty)
   : SCEV(scTruncate), Op(op), Ty(ty) {
-  assert(Op->getType()->isInteger() && Ty->isInteger() &&
+  assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
+         (Ty->isInteger() || isa<PointerType>(Ty)) &&
          "Cannot truncate non-integer value!");
-  assert(Op->getType()->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits()
-         && "This is not a truncating conversion!");
+  assert((!Op->getType()->isInteger() || !Ty->isInteger() ||
+          Op->getType()->getPrimitiveSizeInBits() >
+            Ty->getPrimitiveSizeInBits()) &&
+         "This is not a truncating conversion!");
 }
 
 SCEVTruncateExpr::~SCEVTruncateExpr() {
@@ -222,10 +222,9 @@
 
 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty)
   : SCEV(scZeroExtend), Op(op), Ty(ty) {
-  assert(Op->getType()->isInteger() && Ty->isInteger() &&
+  assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
+         (Ty->isInteger() || isa<PointerType>(Ty)) &&
          "Cannot zero extend non-integer value!");
-  assert(Op->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()
-         && "This is not an extending conversion!");
 }
 
 SCEVZeroExtendExpr::~SCEVZeroExtendExpr() {
@@ -248,7 +247,8 @@
 
 SCEVSignExtendExpr::SCEVSignExtendExpr(const SCEVHandle &op, const Type *ty)
   : SCEV(scSignExtend), Op(op), Ty(ty) {
-  assert(Op->getType()->isInteger() && Ty->isInteger() &&
+  assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
+         (Ty->isInteger() || isa<PointerType>(Ty)) &&
          "Cannot sign extend non-integer value!");
   assert(Op->getType()->getPrimitiveSizeInBits() < Ty->getPrimitiveSizeInBits()
          && "This is not an extending conversion!");
@@ -436,7 +436,11 @@
 }
 
 void SCEVUnknown::print(std::ostream &OS) const {
+  if (isa<PointerType>(V->getType()))
+    OS << "(ptrtoint " << *V->getType() << " ";
   WriteAsOperand(OS, V, false);
+  if (isa<PointerType>(V->getType()))
+    OS << " to iPTR)";
 }
 
 //===----------------------------------------------------------------------===//
@@ -504,52 +508,11 @@
 //                      Simple SCEV method implementations
 //===----------------------------------------------------------------------===//
 
-/// getIntegerSCEV - Given an integer or FP type, create a constant for the
-/// specified signed integer value and return a SCEV for the constant.
-SCEVHandle ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
-  Constant *C;
-  if (Val == 0)
-    C = Constant::getNullValue(Ty);
-  else if (Ty->isFloatingPoint())
-    C = ConstantFP::get(APFloat(Ty==Type::FloatTy ? APFloat::IEEEsingle : 
-                                APFloat::IEEEdouble, Val));
-  else 
-    C = ConstantInt::get(Ty, Val);
-  return getUnknown(C);
-}
-
-/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
-///
-SCEVHandle ScalarEvolution::getNegativeSCEV(const SCEVHandle &V) {
-  if (SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
-    return getUnknown(ConstantExpr::getNeg(VC->getValue()));
-
-  return getMulExpr(V, getConstant(ConstantInt::getAllOnesValue(V->getType())));
-}
-
-/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
-SCEVHandle ScalarEvolution::getNotSCEV(const SCEVHandle &V) {
-  if (SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
-    return getUnknown(ConstantExpr::getNot(VC->getValue()));
-
-  SCEVHandle AllOnes = getConstant(ConstantInt::getAllOnesValue(V->getType()));
-  return getMinusSCEV(AllOnes, V);
-}
-
-/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
-///
-SCEVHandle ScalarEvolution::getMinusSCEV(const SCEVHandle &LHS,
-                                         const SCEVHandle &RHS) {
-  // X - Y --> X + -Y
-  return getAddExpr(LHS, getNegativeSCEV(RHS));
-}
-
-
 /// BinomialCoefficient - Compute BC(It, K).  The result has width W.
 // Assume, K > 0.
 static SCEVHandle BinomialCoefficient(SCEVHandle It, unsigned K,
                                       ScalarEvolution &SE,
-                                      const IntegerType* ResultTy) {
+                                      const Type* ResultTy) {
   // Handle the simplest case efficiently.
   if (K == 1)
     return SE.getTruncateOrZeroExtend(It, ResultTy);
@@ -608,7 +571,7 @@
   if (K > 1000)
     return new SCEVCouldNotCompute();
 
-  unsigned W = ResultTy->getBitWidth();
+  unsigned W = SE.getTargetData().getTypeSizeInBits(ResultTy);
 
   // Calculate K! / 2^T and T; we divide out the factors of two before
   // multiplying for calculating K! / 2^T to avoid overflow.
@@ -672,8 +635,7 @@
     // The computation is correct in the face of overflow provided that the
     // multiplication is performed _after_ the evaluation of the binomial
     // coefficient.
-    SCEVHandle Coeff = BinomialCoefficient(It, i, SE,
-                                           cast<IntegerType>(getType()));
+    SCEVHandle Coeff = BinomialCoefficient(It, i, SE, getType());
     if (isa<SCEVCouldNotCompute>(Coeff))
       return Coeff;
 
@@ -710,10 +672,19 @@
   return Result;
 }
 
-SCEVHandle ScalarEvolution::getZeroExtendExpr(const SCEVHandle &Op, const Type *Ty) {
-  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
-    return getUnknown(
-        ConstantExpr::getZExt(SC->getValue(), Ty));
+SCEVHandle ScalarEvolution::getZeroExtendExpr(const SCEVHandle &Op,
+                                              const Type *Ty) {
+  assert(getTargetData().getTypeSizeInBits(Op->getType()) <
+         getTargetData().getTypeSizeInBits(Ty) &&
+         "This is not an extending conversion!");
+
+  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
+    const Type *IntTy = Ty;
+    if (isa<PointerType>(IntTy)) IntTy = getTargetData().getIntPtrType();
+    Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy);
+    if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
+    return getUnknown(C);
+  }
 
   // FIXME: If the input value is a chrec scev, and we can prove that the value
   // did not overflow the old, smaller, value, we can zero extend all of the
@@ -726,9 +697,13 @@
 }
 
 SCEVHandle ScalarEvolution::getSignExtendExpr(const SCEVHandle &Op, const Type *Ty) {
-  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
-    return getUnknown(
-        ConstantExpr::getSExt(SC->getValue(), Ty));
+  if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
+    const Type *IntTy = Ty;
+    if (isa<PointerType>(IntTy)) IntTy = getTargetData().getIntPtrType();
+    Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy);
+    if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
+    return getUnknown(C);
+  }
 
   // FIXME: If the input value is a chrec scev, and we can prove that the value
   // did not overflow the old, smaller, value, we can sign extend all of the
@@ -740,36 +715,6 @@
   return Result;
 }
 
-/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
-/// of the input value to the specified type.  If the type must be
-/// extended, it is zero extended.
-SCEVHandle ScalarEvolution::getTruncateOrZeroExtend(const SCEVHandle &V,
-                                                    const Type *Ty) {
-  const Type *SrcTy = V->getType();
-  assert(SrcTy->isInteger() && Ty->isInteger() &&
-         "Cannot truncate or zero extend with non-integer arguments!");
-  if (SrcTy->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
-    return V;  // No conversion
-  if (SrcTy->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits())
-    return getTruncateExpr(V, Ty);
-  return getZeroExtendExpr(V, Ty);
-}
-
-/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
-/// of the input value to the specified type.  If the type must be
-/// extended, it is sign extended.
-SCEVHandle ScalarEvolution::getTruncateOrSignExtend(const SCEVHandle &V,
-                                                    const Type *Ty) {
-  const Type *SrcTy = V->getType();
-  assert(SrcTy->isInteger() && Ty->isInteger() &&
-         "Cannot truncate or sign extend with non-integer arguments!");
-  if (SrcTy->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
-    return V;  // No conversion
-  if (SrcTy->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits())
-    return getTruncateExpr(V, Ty);
-  return getSignExtendExpr(V, Ty);
-}
-
 // get - Get a canonical add expression, or something simpler if possible.
 SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) {
   assert(!Ops.empty() && "Cannot get empty add!");
@@ -1385,6 +1330,8 @@
 SCEVHandle ScalarEvolution::getUnknown(Value *V) {
   if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
     return getConstant(CI);
+  if (isa<ConstantPointerNull>(V))
+    return getIntegerSCEV(0, V->getType());
   SCEVUnknown *&Result = (*SCEVUnknowns)[V];
   if (Result == 0) Result = new SCEVUnknown(V);
   return Result;
@@ -1411,6 +1358,10 @@
     ///
     LoopInfo &LI;
 
+    /// TD - The target data information for the target we are targetting.
+    ///
+    TargetData &TD;
+
     /// UnknownValue - This SCEV is used to represent unknown trip counts and
     /// things.
     SCEVHandle UnknownValue;
@@ -1431,8 +1382,35 @@
     std::map<PHINode*, Constant*> ConstantEvolutionLoopExitValue;
 
   public:
-    ScalarEvolutionsImpl(ScalarEvolution &se, Function &f, LoopInfo &li)
-      : SE(se), F(f), LI(li), UnknownValue(new SCEVCouldNotCompute()) {}
+    ScalarEvolutionsImpl(ScalarEvolution &se, Function &f, LoopInfo &li,
+                         TargetData &td)
+      : SE(se), F(f), LI(li), TD(td), UnknownValue(new SCEVCouldNotCompute()) {}
+
+    /// getIntegerSCEV - Given an integer or FP type, create a constant for the
+    /// specified signed integer value and return a SCEV for the constant.
+    SCEVHandle getIntegerSCEV(int Val, const Type *Ty);
+
+    /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
+    ///
+    SCEVHandle getNegativeSCEV(const SCEVHandle &V);
+
+    /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
+    ///
+    SCEVHandle getNotSCEV(const SCEVHandle &V);
+
+    /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
+    ///
+    SCEVHandle getMinusSCEV(const SCEVHandle &LHS, const SCEVHandle &RHS);
+
+    /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
+    /// of the input value to the specified type.  If the type must be extended,
+    /// it is zero extended.
+    SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty);
+
+    /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
+    /// of the input value to the specified type.  If the type must be extended,
+    /// it is sign extended.
+    SCEVHandle getTruncateOrSignExtend(const SCEVHandle &V, const Type *Ty);
 
     /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
     /// expression and create a new one.
@@ -1492,6 +1470,9 @@
     /// that no dangling references are left around.
     void deleteValueFromRecords(Value *V);
 
+    /// getTargetData - Return the TargetData.
+    const TargetData &getTargetData() const;
+
   private:
     /// createSCEV - We know that there is no SCEV for the specified value.
     /// Analyze the expression.
@@ -1592,6 +1573,9 @@
   }
 }
 
+const TargetData &ScalarEvolutionsImpl::getTargetData() const {
+  return TD;
+}
 
 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
 /// expression and create a new one.
@@ -1605,6 +1589,88 @@
   return S;
 }
 
+/// getIntegerSCEV - Given an integer or FP type, create a constant for the
+/// specified signed integer value and return a SCEV for the constant.
+SCEVHandle ScalarEvolutionsImpl::getIntegerSCEV(int Val, const Type *Ty) {
+  if (isa<PointerType>(Ty))
+    Ty = TD.getIntPtrType();
+  Constant *C;
+  if (Val == 0)
+    C = Constant::getNullValue(Ty);
+  else if (Ty->isFloatingPoint())
+    C = ConstantFP::get(APFloat(Ty==Type::FloatTy ? APFloat::IEEEsingle :
+                                APFloat::IEEEdouble, Val));
+  else
+    C = ConstantInt::get(Ty, Val);
+  return SE.getUnknown(C);
+}
+
+/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
+///
+SCEVHandle ScalarEvolutionsImpl::getNegativeSCEV(const SCEVHandle &V) {
+  if (SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
+    return SE.getUnknown(ConstantExpr::getNeg(VC->getValue()));
+
+  const Type *Ty = V->getType();
+  if (isa<PointerType>(Ty))
+    Ty = TD.getIntPtrType();
+  return SE.getMulExpr(V, SE.getConstant(ConstantInt::getAllOnesValue(Ty)));
+}
+
+/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
+SCEVHandle ScalarEvolutionsImpl::getNotSCEV(const SCEVHandle &V) {
+  if (SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
+    return SE.getUnknown(ConstantExpr::getNot(VC->getValue()));
+
+  const Type *Ty = V->getType();
+  if (isa<PointerType>(Ty))
+    Ty = TD.getIntPtrType();
+  SCEVHandle AllOnes = SE.getConstant(ConstantInt::getAllOnesValue(Ty));
+  return getMinusSCEV(AllOnes, V);
+}
+
+/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
+///
+SCEVHandle ScalarEvolutionsImpl::getMinusSCEV(const SCEVHandle &LHS,
+                                              const SCEVHandle &RHS) {
+  // X - Y --> X + -Y
+  return SE.getAddExpr(LHS, SE.getNegativeSCEV(RHS));
+}
+
+/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
+/// input value to the specified type.  If the type must be extended, it is zero
+/// extended.
+SCEVHandle
+ScalarEvolutionsImpl::getTruncateOrZeroExtend(const SCEVHandle &V,
+                                              const Type *Ty) {
+  const Type *SrcTy = V->getType();
+  assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
+         (Ty->isInteger() || isa<PointerType>(Ty)) &&
+         "Cannot truncate or zero extend with non-integer arguments!");
+  if (TD.getTypeSizeInBits(SrcTy) == TD.getTypeSizeInBits(Ty))
+    return V;  // No conversion
+  if (TD.getTypeSizeInBits(SrcTy) > TD.getTypeSizeInBits(Ty))
+    return SE.getTruncateExpr(V, Ty);
+  return SE.getZeroExtendExpr(V, Ty);
+}
+
+/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
+/// input value to the specified type.  If the type must be extended, it is sign
+/// extended.
+SCEVHandle
+ScalarEvolutionsImpl::getTruncateOrSignExtend(const SCEVHandle &V,
+                                              const Type *Ty) {
+  const Type *SrcTy = V->getType();
+  assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
+         (Ty->isInteger() || isa<PointerType>(Ty)) &&
+         "Cannot truncate or zero extend with non-integer arguments!");
+  if (TD.getTypeSizeInBits(SrcTy) == TD.getTypeSizeInBits(Ty))
+    return V;  // No conversion
+  if (TD.getTypeSizeInBits(SrcTy) > TD.getTypeSizeInBits(Ty))
+    return SE.getTruncateExpr(V, Ty);
+  return SE.getSignExtendExpr(V, Ty);
+}
+
 /// ReplaceSymbolicValueWithConcrete - This looks up the computed SCEV value for
 /// the specified instruction and replaces any references to the symbolic value
 /// SymName with the specified value.  This is used during PHI resolution.
@@ -1728,63 +1794,66 @@
 /// guaranteed to end in (at every loop iteration).  It is, at the same time,
 /// the minimum number of times S is divisible by 2.  For example, given {4,+,8}
 /// it returns 2.  If S is guaranteed to be 0, it returns the bitwidth of S.
-static uint32_t GetMinTrailingZeros(SCEVHandle S) {
+static uint32_t GetMinTrailingZeros(SCEVHandle S, const TargetData &TD) {
   if (SCEVConstant *C = dyn_cast<SCEVConstant>(S))
     return C->getValue()->getValue().countTrailingZeros();
 
   if (SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
-    return std::min(GetMinTrailingZeros(T->getOperand()), T->getBitWidth());
+    return std::min(GetMinTrailingZeros(T->getOperand(), TD),
+                    (uint32_t)TD.getTypeSizeInBits(T->getType()));
 
   if (SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
-    uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
-    return OpRes == E->getOperand()->getBitWidth() ? E->getBitWidth() : OpRes;
+    uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), TD);
+    return OpRes == TD.getTypeSizeInBits(E->getOperand()->getType()) ?
+             TD.getTypeSizeInBits(E->getOperand()->getType()) : OpRes;
   }
 
   if (SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
-    uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
-    return OpRes == E->getOperand()->getBitWidth() ? E->getBitWidth() : OpRes;
+    uint32_t OpRes = GetMinTrailingZeros(E->getOperand(), TD);
+    return OpRes == TD.getTypeSizeInBits(E->getOperand()->getType()) ?
+             TD.getTypeSizeInBits(E->getOperand()->getType()) : OpRes;
   }
 
   if (SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
     // The result is the min of all operands results.
-    uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
+    uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), TD);
     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
-      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
+      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), TD));
     return MinOpRes;
   }
 
   if (SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
     // The result is the sum of all operands results.
-    uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
-    uint32_t BitWidth = M->getBitWidth();
+    uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0), TD);
+    uint32_t BitWidth = TD.getTypeSizeInBits(M->getType());
     for (unsigned i = 1, e = M->getNumOperands();
          SumOpRes != BitWidth && i != e; ++i)
-      SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
+      SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i), TD),
                           BitWidth);
     return SumOpRes;
   }
 
   if (SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
     // The result is the min of all operands results.
-    uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
+    uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0), TD);
     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
-      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
+      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i), TD));
     return MinOpRes;
   }
 
   if (SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
     // The result is the min of all operands results.
-    uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
+    uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), TD);
     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
-      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
+      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), TD));
     return MinOpRes;
   }
 
   if (SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
     // The result is the min of all operands results.
-    uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
+    uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0), TD);
     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
-      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
+      MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i), TD));
     return MinOpRes;
   }
 
@@ -1796,9 +1865,10 @@
 /// Analyze the expression.
 ///
 SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) {
-  if (!isa<IntegerType>(V->getType()))
+  if (!isa<IntegerType>(V->getType()) &&
+      !isa<PointerType>(V->getType()))
     return SE.getUnknown(V);
-    
+
   unsigned Opcode = Instruction::UserOp1;
   if (Instruction *I = dyn_cast<Instruction>(V))
     Opcode = I->getOpcode();
@@ -1831,7 +1901,7 @@
     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
       SCEVHandle LHS = getSCEV(U->getOperand(0));
       const APInt &CIVal = CI->getValue();
-      if (GetMinTrailingZeros(LHS) >=
+      if (GetMinTrailingZeros(LHS, TD) >=
           (CIVal.getBitWidth() - CIVal.countLeadingZeros()))
         return SE.getAddExpr(LHS, getSCEV(U->getOperand(1)));
     }
@@ -1881,11 +1951,55 @@
 
   case Instruction::BitCast:
     // BitCasts are no-op casts so we just eliminate the cast.
-    if (U->getType()->isInteger() &&
-        U->getOperand(0)->getType()->isInteger())
+    if ((U->getType()->isInteger() ||
+         isa<PointerType>(U->getType())) &&
+        (U->getOperand(0)->getType()->isInteger() ||
+         isa<PointerType>(U->getOperand(0)->getType())))
       return getSCEV(U->getOperand(0));
     break;
 
+  case Instruction::IntToPtr:
+    return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)),
+                                   TD.getIntPtrType());
+
+  case Instruction::PtrToInt:
+    return getTruncateOrZeroExtend(getSCEV(U->getOperand(0)),
+                                   U->getType());
+
+  case Instruction::GetElementPtr: {
+    const Type *IntPtrTy = TD.getIntPtrType();
+    Value *Base = U->getOperand(0);
+    SCEVHandle TotalOffset = SE.getIntegerSCEV(0, IntPtrTy);
+    gep_type_iterator GTI = gep_type_begin(U);
+    for (GetElementPtrInst::op_iterator I = next(U->op_begin()),
+                                        E = U->op_end();
+         I != E; ++I) {
+      Value *Index = *I;
+      // Compute the (potentially symbolic) offset in bytes for this index.
+      if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
+        // For a struct, add the member offset.
+        const StructLayout &SL = *TD.getStructLayout(STy);
+        unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
+        uint64_t Offset = SL.getElementOffset(FieldNo);
+        TotalOffset = SE.getAddExpr(TotalOffset,
+                                    SE.getIntegerSCEV(Offset, IntPtrTy));
+      } else {
+        // For an array, add the element offset, explicitly scaled.
+        SCEVHandle LocalOffset = getSCEV(Index);
+        if (!isa<PointerType>(LocalOffset->getType()))
+          // Getelementptr indicies are signed.
+          LocalOffset = getTruncateOrSignExtend(LocalOffset,
+                                                IntPtrTy);
+        LocalOffset =
+          SE.getMulExpr(LocalOffset,
+                        SE.getIntegerSCEV(TD.getTypePaddedSize(*GTI),
+                                          IntPtrTy));
+        TotalOffset = SE.getAddExpr(TotalOffset, LocalOffset);
+      }
+    }
+    return SE.getAddExpr(getSCEV(Base), TotalOffset);
+  }
+
   case Instruction::PHI:
     return createNodeForPHI(cast<PHINode>(U));
 
@@ -2324,6 +2438,7 @@
 static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
   if (isa<PHINode>(V)) return PHIVal;
   if (Constant *C = dyn_cast<Constant>(V)) return C;
+  if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
   Instruction *I = cast<Instruction>(V);
 
   std::vector<Constant*> Operands;
@@ -2490,10 +2605,12 @@
             Operands.push_back(C);
           } else {
             // If any of the operands is non-constant and if they are
-            // non-integer, don't even try to analyze them with scev techniques.
-            if (!isa<IntegerType>(Op->getType()))
+            // non-integer and non-pointer, don't even try to analyze them
+            // with scev techniques.
+            if (!isa<IntegerType>(Op->getType()) &&
+                !isa<PointerType>(Op->getType()))
               return V;
-              
+
             SCEVHandle OpV = getSCEVAtScope(getSCEV(Op), L);
             if (SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV))
               Operands.push_back(ConstantExpr::getIntegerCast(SC->getValue(), 
@@ -3003,7 +3120,8 @@
 
   // First check to see if the range contains zero.  If not, the first
   // iteration exits.
-  if (!Range.contains(APInt(getBitWidth(),0))) 
+  unsigned BitWidth = SE.getTargetData().getTypeSizeInBits(getType());
+  if (!Range.contains(APInt(BitWidth, 0)))
     return SE.getConstant(ConstantInt::get(getType(),0));
 
   if (isAffine()) {
@@ -3014,7 +3132,7 @@
     // the upper value of the range must be the first possible exit value.
     // If A is negative then the lower of the range is the last possible loop
     // value.  Also note that we already checked for a full range.
-    APInt One(getBitWidth(),1);
+    APInt One(BitWidth,1);
     APInt A     = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
     APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
 
@@ -3094,7 +3212,9 @@
 //===----------------------------------------------------------------------===//
 
 bool ScalarEvolution::runOnFunction(Function &F) {
-  Impl = new ScalarEvolutionsImpl(*this, F, getAnalysis<LoopInfo>());
+  Impl = new ScalarEvolutionsImpl(*this, F,
+                                  getAnalysis<LoopInfo>(),
+                                  getAnalysis<TargetData>());
   return false;
 }
 
@@ -3106,6 +3226,15 @@
 void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.setPreservesAll();
   AU.addRequiredTransitive<LoopInfo>();
+  AU.addRequiredTransitive<TargetData>();
+}
+
+const TargetData &ScalarEvolution::getTargetData() const {
+  return ((ScalarEvolutionsImpl*)Impl)->getTargetData();
+}
+
+SCEVHandle ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
+  return ((ScalarEvolutionsImpl*)Impl)->getIntegerSCEV(Val, Ty);
 }
 
 SCEVHandle ScalarEvolution::getSCEV(Value *V) const {
@@ -3125,6 +3254,41 @@
   ((ScalarEvolutionsImpl*)Impl)->setSCEV(V, H);
 }
 
+/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
+///
+SCEVHandle ScalarEvolution::getNegativeSCEV(const SCEVHandle &V) {
+  return ((ScalarEvolutionsImpl*)Impl)->getNegativeSCEV(V);
+}
+
+/// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
+///
+SCEVHandle ScalarEvolution::getNotSCEV(const SCEVHandle &V) {
+  return ((ScalarEvolutionsImpl*)Impl)->getNotSCEV(V);
+}
+
+/// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
+///
+SCEVHandle ScalarEvolution::getMinusSCEV(const SCEVHandle &LHS,
+                                         const SCEVHandle &RHS) {
+  return ((ScalarEvolutionsImpl*)Impl)->getMinusSCEV(LHS, RHS);
+}
+
+/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
+/// of the input value to the specified type.  If the type must be
+/// extended, it is zero extended.
+SCEVHandle ScalarEvolution::getTruncateOrZeroExtend(const SCEVHandle &V,
+                                                    const Type *Ty) {
+  return ((ScalarEvolutionsImpl*)Impl)->getTruncateOrZeroExtend(V, Ty);
+}
+
+/// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion
+/// of the input value to the specified type.  If the type must be
+/// extended, it is sign extended.
+SCEVHandle ScalarEvolution::getTruncateOrSignExtend(const SCEVHandle &V,
+                                                    const Type *Ty) {
+  return ((ScalarEvolutionsImpl*)Impl)->getTruncateOrSignExtend(V, Ty);
+}
+
 
 bool ScalarEvolution::isLoopGuardedByCond(const Loop *L,
                                           ICmpInst::Predicate Pred,

Modified: llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolutionExpander.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolutionExpander.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolutionExpander.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Analysis/ScalarEvolutionExpander.cpp Fri Apr 17 13:49:34 2009
@@ -15,12 +15,25 @@
 
 #include "llvm/Analysis/ScalarEvolutionExpander.h"
 #include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Target/TargetData.h"
 using namespace llvm;
 
 /// InsertCastOfTo - Insert a cast of V to the specified type, doing what
 /// we can to share the casts.
 Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V, 
                                     const Type *Ty) {
+  // Short-circuit unnecessary bitcasts.
+  if (opcode == Instruction::BitCast && V->getType() == Ty)
+    return V;
+
+  // Short-circuit unnecessary inttoptr<->ptrtoint casts.
+  if (opcode == Instruction::PtrToInt && Ty == TD.getIntPtrType())
+    if (IntToPtrInst *ITP = dyn_cast<IntToPtrInst>(V))
+      return ITP->getOperand(0);
+  if (opcode == Instruction::IntToPtr && V->getType() == TD.getIntPtrType())
+    if (PtrToIntInst *PTI = dyn_cast<PtrToIntInst>(V))
+      return PTI->getOperand(0);
+
   // FIXME: keep track of the cast instruction.
   if (Constant *C = dyn_cast<Constant>(V))
     return ConstantExpr::getCast(opcode, C, Ty);
@@ -100,16 +113,23 @@
 }
 
 Value *SCEVExpander::visitAddExpr(SCEVAddExpr *S) {
+  const Type *Ty = S->getType();
+  if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
   Value *V = expand(S->getOperand(S->getNumOperands()-1));
+  V = InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty);
 
   // Emit a bunch of add instructions
-  for (int i = S->getNumOperands()-2; i >= 0; --i)
-    V = InsertBinop(Instruction::Add, V, expand(S->getOperand(i)),
-                    InsertPt);
+  for (int i = S->getNumOperands()-2; i >= 0; --i) {
+    Value *W = expand(S->getOperand(i));
+    W = InsertCastOfTo(CastInst::getCastOpcode(W, false, Ty, false), W, Ty);
+    V = InsertBinop(Instruction::Add, V, W, InsertPt);
+  }
   return V;
 }
     
 Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) {
+  const Type *Ty = S->getType();
+  if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
   int FirstOp = 0;  // Set if we should emit a subtract.
   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
     if (SC->getValue()->isAllOnesValue())
@@ -117,29 +137,37 @@
 
   int i = S->getNumOperands()-2;
   Value *V = expand(S->getOperand(i+1));
+  V = InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty);
 
   // Emit a bunch of multiply instructions
-  for (; i >= FirstOp; --i)
-    V = InsertBinop(Instruction::Mul, V, expand(S->getOperand(i)),
-                    InsertPt);
+  for (; i >= FirstOp; --i) {
+    Value *W = expand(S->getOperand(i));
+    W = InsertCastOfTo(CastInst::getCastOpcode(W, false, Ty, false), W, Ty);
+    V = InsertBinop(Instruction::Mul, V, W, InsertPt);
+  }
+
   // -1 * ...  --->  0 - ...
   if (FirstOp == 1)
-    V = InsertBinop(Instruction::Sub, Constant::getNullValue(V->getType()), V,
-                    InsertPt);
+    V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt);
   return V;
 }
 
 Value *SCEVExpander::visitUDivExpr(SCEVUDivExpr *S) {
+  const Type *Ty = S->getType();
+  if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
+
   Value *LHS = expand(S->getLHS());
+  LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty);
   if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
     const APInt &RHS = SC->getValue()->getValue();
     if (RHS.isPowerOf2())
       return InsertBinop(Instruction::LShr, LHS,
-                         ConstantInt::get(S->getType(), RHS.logBase2()),
+                         ConstantInt::get(Ty, RHS.logBase2()),
                          InsertPt);
   }
 
   Value *RHS = expand(S->getRHS());
+  RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false), RHS, Ty);
   return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
 }
 
@@ -147,14 +175,19 @@
   const Type *Ty = S->getType();
   const Loop *L = S->getLoop();
   // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
-  assert(Ty->isInteger() && "Cannot expand fp recurrences yet!");
+  assert((Ty->isInteger() || isa<PointerType>(Ty)) &&
+         "Cannot expand fp recurrences yet!");
 
   // {X,+,F} --> X + {0,+,F}
   if (!S->getStart()->isZero()) {
     Value *Start = expand(S->getStart());
+    if (isa<PointerType>(Start->getType()))
+      Start = InsertCastOfTo(Instruction::PtrToInt, Start, TD.getIntPtrType());
     std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end());
     NewOps[0] = SE.getIntegerSCEV(0, Ty);
     Value *Rest = expand(SE.getAddRecExpr(NewOps, L));
+    if (isa<PointerType>(Rest->getType()))
+      Rest = InsertCastOfTo(Instruction::PtrToInt, Rest, TD.getIntPtrType());
 
     // FIXME: look for an existing add to use.
     return InsertBinop(Instruction::Add, Rest, Start, InsertPt);
@@ -194,6 +227,8 @@
   // If this is a simple linear addrec, emit it now as a special case.
   if (S->isAffine()) {   // {0,+,F} --> i*F
     Value *F = expand(S->getOperand(1));
+    if (isa<PointerType>(F->getType()))
+      F = InsertCastOfTo(Instruction::PtrToInt, F, TD.getIntPtrType());
     
     // IF the step is by one, just return the inserted IV.
     if (ConstantInt *CI = dyn_cast<ConstantInt>(F))
@@ -242,23 +277,37 @@
 
 Value *SCEVExpander::visitTruncateExpr(SCEVTruncateExpr *S) {
   Value *V = expand(S->getOperand());
+  if (isa<PointerType>(V->getType()))
+    V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType());
   return CastInst::CreateTruncOrBitCast(V, S->getType(), "tmp.", InsertPt);
 }
 
 Value *SCEVExpander::visitZeroExtendExpr(SCEVZeroExtendExpr *S) {
+  const Type *Ty = S->getType();
+  if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
   Value *V = expand(S->getOperand());
-  return CastInst::CreateZExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
+  if (isa<PointerType>(V->getType()))
+    V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType());
+  return CastInst::CreateZExtOrBitCast(V, Ty, "tmp.", InsertPt);
 }
 
 Value *SCEVExpander::visitSignExtendExpr(SCEVSignExtendExpr *S) {
+  const Type *Ty = S->getType();
+  if (isa<PointerType>(Ty)) Ty = TD.getIntPtrType();
   Value *V = expand(S->getOperand());
-  return CastInst::CreateSExtOrBitCast(V, S->getType(), "tmp.", InsertPt);
+  if (isa<PointerType>(V->getType()))
+    V = InsertCastOfTo(Instruction::PtrToInt, V, TD.getIntPtrType());
+  return CastInst::CreateSExtOrBitCast(V, Ty, "tmp.", InsertPt);
 }
 
 Value *SCEVExpander::visitSMaxExpr(SCEVSMaxExpr *S) {
+  const Type *Ty = S->getType();
   Value *LHS = expand(S->getOperand(0));
+  LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty);
   for (unsigned i = 1; i < S->getNumOperands(); ++i) {
     Value *RHS = expand(S->getOperand(i));
+    RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false),
+                         RHS, Ty);
     Value *ICmp = new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt);
     LHS = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt);
   }
@@ -266,19 +315,27 @@
 }
 
 Value *SCEVExpander::visitUMaxExpr(SCEVUMaxExpr *S) {
+  const Type *Ty = S->getType();
   Value *LHS = expand(S->getOperand(0));
+  LHS = InsertCastOfTo(CastInst::getCastOpcode(LHS, false, Ty, false), LHS, Ty);
   for (unsigned i = 1; i < S->getNumOperands(); ++i) {
     Value *RHS = expand(S->getOperand(i));
+    RHS = InsertCastOfTo(CastInst::getCastOpcode(RHS, false, Ty, false),
+                         RHS, Ty);
     Value *ICmp = new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt);
     LHS = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt);
   }
   return LHS;
 }
 
-Value *SCEVExpander::expandCodeFor(SCEVHandle SH, Instruction *IP) {
+Value *SCEVExpander::expandCodeFor(SCEVHandle SH, const Type *Ty,
+                                   Instruction *IP) {
   // Expand the code for this SCEV.
+  assert(TD.getTypeSizeInBits(Ty) == TD.getTypeSizeInBits(SH->getType()) &&
+         "non-trivial casts should be done with the SCEVs directly!");
   this->InsertPt = IP;
-  return expand(SH);
+  Value *V = expand(SH);
+  return InsertCastOfTo(CastInst::getCastOpcode(V, false, Ty, false), V, Ty);
 }
 
 Value *SCEVExpander::expand(SCEV *S) {

Modified: llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp (original)
+++ llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp Fri Apr 17 13:49:34 2009
@@ -4432,8 +4432,7 @@
                            DAG.getVTList(&ValueVTs[0], 2),
                            &Ops[0], 2);
       SDNode *RNode = Result.getNode();
-      DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
-      DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
+      DAG.ReplaceAllUsesWith(Node, RNode);
       break;
     }
     }
@@ -4468,8 +4467,7 @@
                            DAG.getVTList(&ValueVTs[0], 2),
                            &Ops[0], 2);
       SDNode *RNode = Result.getNode();
-      DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
-      DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
+      DAG.ReplaceAllUsesWith(Node, RNode);
       break;
     }
     }

Modified: llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp (original)
+++ llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp Fri Apr 17 13:49:34 2009
@@ -1019,9 +1019,10 @@
         // avoid spilling.
         return 0;
       if (Opc == TargetInstrInfo::EXTRACT_SUBREG ||
+          Opc == TargetInstrInfo::SUBREG_TO_REG ||
           Opc == TargetInstrInfo::INSERT_SUBREG)
-        // EXTRACT_SUBREG / INSERT_SUBREG should be close to its use to
-        // facilitate coalescing.
+        // EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG nodes should be
+        // close to their uses to facilitate coalescing.
         return 0;
       if (SU->NumSuccs == 0 && SU->NumPreds != 0)
         // If SU does not have a register use, i.e. it doesn't produce a value
@@ -1388,6 +1389,16 @@
         if (SuccSU->getHeight() < SU->getHeight() &&
             (SU->getHeight() - SuccSU->getHeight()) > 1)
           continue;
+        // Skip past COPY_TO_REGCLASS nodes, so that the pseudo edge
+        // constrains whatever is using the copy, instead of the copy
+        // itself. In the case that the copy is coalesced, this
+        // preserves the intent of the pseudo two-address heurietics.
+        while (SuccSU->Succs.size() == 1 &&
+               SuccSU->getNode()->isMachineOpcode() &&
+               SuccSU->getNode()->getMachineOpcode() ==
+                 TargetInstrInfo::COPY_TO_REGCLASS)
+          SuccSU = SuccSU->Succs.front().getSUnit();
+        // Don't constrain non-instruction nodes.
         if (!SuccSU->getNode() || !SuccSU->getNode()->isMachineOpcode())
           continue;
         // Don't constrain nodes with physical register defs if the
@@ -1396,11 +1407,12 @@
           if (canClobberPhysRegDefs(SuccSU, SU, TII, TRI))
             continue;
         }
-        // Don't constrain extract_subreg / insert_subreg; these may be
-        // coalesced away. We want them close to their uses.
+        // Don't constrain EXTRACT_SUBREG, INSERT_SUBREG, and SUBREG_TO_REG;
+        // these may be coalesced away. We want them close to their uses.
         unsigned SuccOpc = SuccSU->getNode()->getMachineOpcode();
         if (SuccOpc == TargetInstrInfo::EXTRACT_SUBREG ||
-            SuccOpc == TargetInstrInfo::INSERT_SUBREG)
+            SuccOpc == TargetInstrInfo::INSERT_SUBREG ||
+            SuccOpc == TargetInstrInfo::SUBREG_TO_REG)
           continue;
         if ((!canClobber(SuccSU, DUSU) ||
              (hasCopyToRegUse(SU) && !hasCopyToRegUse(SuccSU)) ||

Modified: llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/SelectionDAG.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (original)
+++ llvm/branches/Apple/Dib/lib/CodeGen/SelectionDAG/SelectionDAG.cpp Fri Apr 17 13:49:34 2009
@@ -4497,14 +4497,17 @@
 /// ReplaceAllUsesWith - Modify anything using 'From' to use 'To' instead.
 /// This can cause recursive merging of nodes in the DAG.
 ///
-/// This version assumes From/To have matching types and numbers of result
-/// values.
+/// This version assumes that for each value of From, there is a
+/// corresponding value in To in the same position with the same type.
 ///
 void SelectionDAG::ReplaceAllUsesWith(SDNode *From, SDNode *To,
                                       DAGUpdateListener *UpdateListener) {
-  assert(From->getVTList().VTs == To->getVTList().VTs &&
-         From->getNumValues() == To->getNumValues() &&
-         "Cannot use this version of ReplaceAllUsesWith!");
+#ifndef NDEBUG
+  for (unsigned i = 0, e = From->getNumValues(); i != e; ++i)
+    assert((!From->hasAnyUseOfValue(i) ||
+            From->getValueType(i) == To->getValueType(i)) &&
+           "Cannot use this version of ReplaceAllUsesWith!");
+#endif
 
   // Handle the trivial case.
   if (From == To)

Modified: llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.cpp Fri Apr 17 13:49:34 2009
@@ -264,6 +264,7 @@
     { X86::MOV64rr,     X86::MOV64mr, 0 },
     { X86::MOV8ri,      X86::MOV8mi, 0 },
     { X86::MOV8rr,      X86::MOV8mr, 0 },
+    { X86::MOV8rr_NOREX, X86::MOV8mr_NOREX, 0 },
     { X86::MOVAPDrr,    X86::MOVAPDmr, 0 },
     { X86::MOVAPSrr,    X86::MOVAPSmr, 0 },
     { X86::MOVDQArr,    X86::MOVDQAmr, 0 },
@@ -1663,8 +1664,8 @@
     } else if (DestRC == &X86::GR16RegClass) {
       Opc = X86::MOV16rr;
     } else if (DestRC == &X86::GR8RegClass) {
-      // Copying two or from a physical H register requires a NOREX move. Otherwise
-      // use a normal move.
+      // Copying two or from a physical H register requires a NOREX move.
+      // Otherwise use a normal move.
       if (isHReg(DestReg) || isHReg(SrcReg))
         Opc = X86::MOV8rr_NOREX;
       else

Modified: llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.td
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.td?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.td (original)
+++ llvm/branches/Apple/Dib/lib/Target/X86/X86InstrInfo.td Fri Apr 17 13:49:34 2009
@@ -743,7 +743,8 @@
 // can be used for copying and storing h registers, which can't be encoded when
 // a REX prefix is present.
 let neverHasSideEffects = 1 in
-def MOV8rr_NOREX : I<0x88, MRMDestReg, (outs GR8_NOREX:$dst), (ins GR8_NOREX:$src),
+def MOV8rr_NOREX : I<0x88, MRMDestReg,
+                     (outs GR8_NOREX:$dst), (ins GR8_NOREX:$src),
                      "mov{b}\t{$src, $dst|$dst, $src}  # NOREX", []>;
 def MOV8mr_NOREX : I<0x88, MRMDestMem,
                      (outs), (ins i8mem_NOREX:$dst, GR8_NOREX:$src),

Modified: llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Transforms/Scalar/IndVarSimplify.cpp Fri Apr 17 13:49:34 2009
@@ -50,6 +50,7 @@
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
+#include "llvm/Target/TargetData.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/ADT/SmallVector.h"
@@ -59,7 +60,6 @@
 using namespace llvm;
 
 STATISTIC(NumRemoved , "Number of aux indvars removed");
-STATISTIC(NumPointer , "Number of pointer indvars promoted");
 STATISTIC(NumInserted, "Number of canonical indvars added");
 STATISTIC(NumReplaced, "Number of exit values replaced");
 STATISTIC(NumLFTR    , "Number of loop exit tests replaced");
@@ -67,6 +67,7 @@
 namespace {
   class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass {
     LoopInfo        *LI;
+    TargetData      *TD;
     ScalarEvolution *SE;
     bool Changed;
   public:
@@ -81,6 +82,7 @@
      AU.addRequiredID(LCSSAID);
      AU.addRequiredID(LoopSimplifyID);
      AU.addRequired<LoopInfo>();
+     AU.addRequired<TargetData>();
      AU.addPreserved<ScalarEvolution>();
      AU.addPreservedID(LoopSimplifyID);
      AU.addPreservedID(LCSSAID);
@@ -91,8 +93,6 @@
 
     void RewriteNonIntegerIVs(Loop *L);
 
-    void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader,
-                                    SmallPtrSet<Instruction*, 16> &DeadInsts);
     void LinearFunctionTestReplace(Loop *L, SCEVHandle BackedgeTakenCount,
                                    Value *IndVar,
                                    BasicBlock *ExitingBlock,
@@ -135,97 +135,6 @@
   }
 }
 
-
-/// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer
-/// recurrence.  If so, change it into an integer recurrence, permitting
-/// analysis by the SCEV routines.
-void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN,
-                                                BasicBlock *Preheader,
-                                     SmallPtrSet<Instruction*, 16> &DeadInsts) {
-  assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!");
-  unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader);
-  unsigned BackedgeIdx = PreheaderIdx^1;
-  if (GetElementPtrInst *GEPI =
-          dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx)))
-    if (GEPI->getOperand(0) == PN) {
-      assert(GEPI->getNumOperands() == 2 && "GEP types must match!");
-      DOUT << "INDVARS: Eliminating pointer recurrence: " << *GEPI;
-
-      // Okay, we found a pointer recurrence.  Transform this pointer
-      // recurrence into an integer recurrence.  Compute the value that gets
-      // added to the pointer at every iteration.
-      Value *AddedVal = GEPI->getOperand(1);
-
-      // Insert a new integer PHI node into the top of the block.
-      PHINode *NewPhi = PHINode::Create(AddedVal->getType(),
-                                        PN->getName()+".rec", PN);
-      NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader);
-
-      // Create the new add instruction.
-      Value *NewAdd = BinaryOperator::CreateAdd(NewPhi, AddedVal,
-                                                GEPI->getName()+".rec", GEPI);
-      NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx));
-
-      // Update the existing GEP to use the recurrence.
-      GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx));
-
-      // Update the GEP to use the new recurrence we just inserted.
-      GEPI->setOperand(1, NewAdd);
-
-      // If the incoming value is a constant expr GEP, try peeling out the array
-      // 0 index if possible to make things simpler.
-      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0)))
-        if (CE->getOpcode() == Instruction::GetElementPtr) {
-          unsigned NumOps = CE->getNumOperands();
-          assert(NumOps > 1 && "CE folding didn't work!");
-          if (CE->getOperand(NumOps-1)->isNullValue()) {
-            // Check to make sure the last index really is an array index.
-            gep_type_iterator GTI = gep_type_begin(CE);
-            for (unsigned i = 1, e = CE->getNumOperands()-1;
-                 i != e; ++i, ++GTI)
-              /*empty*/;
-            if (isa<SequentialType>(*GTI)) {
-              // Pull the last index out of the constant expr GEP.
-              SmallVector<Value*, 8> CEIdxs(CE->op_begin()+1, CE->op_end()-1);
-              Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0),
-                                                             &CEIdxs[0],
-                                                             CEIdxs.size());
-              Value *Idx[2];
-              Idx[0] = Constant::getNullValue(Type::Int32Ty);
-              Idx[1] = NewAdd;
-              GetElementPtrInst *NGEPI = GetElementPtrInst::Create(
-                  NCE, Idx, Idx + 2,
-                  GEPI->getName(), GEPI);
-              SE->deleteValueFromRecords(GEPI);
-              GEPI->replaceAllUsesWith(NGEPI);
-              GEPI->eraseFromParent();
-              GEPI = NGEPI;
-            }
-          }
-        }
-
-
-      // Finally, if there are any other users of the PHI node, we must
-      // insert a new GEP instruction that uses the pre-incremented version
-      // of the induction amount.
-      if (!PN->use_empty()) {
-        BasicBlock::iterator InsertPos = PN; ++InsertPos;
-        while (isa<PHINode>(InsertPos)) ++InsertPos;
-        Value *PreInc =
-          GetElementPtrInst::Create(PN->getIncomingValue(PreheaderIdx),
-                                    NewPhi, "", InsertPos);
-        PreInc->takeName(PN);
-        PN->replaceAllUsesWith(PreInc);
-      }
-
-      // Delete the old PHI for sure, and the GEP if its otherwise unused.
-      DeadInsts.insert(PN);
-
-      ++NumPointer;
-      Changed = true;
-    }
-}
-
 /// LinearFunctionTestReplace - This method rewrites the exit condition of the
 /// loop to be a canonical != comparison against the incremented loop induction
 /// variable.  This pass is able to rewrite the exit tests of any loop where the
@@ -275,7 +184,7 @@
 
   // Expand the code for the iteration count into the preheader of the loop.
   BasicBlock *Preheader = L->getLoopPreheader();
-  Value *ExitCnt = Rewriter.expandCodeFor(RHS,
+  Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(),
                                           Preheader->getTerminator());
 
   // Insert a new icmp_ne or icmp_eq instruction before the branch.
@@ -307,7 +216,7 @@
 
   // Scan all of the instructions in the loop, looking at those that have
   // extra-loop users and which are recurrences.
-  SCEVExpander Rewriter(*SE, *LI);
+  SCEVExpander Rewriter(*SE, *LI, *TD);
 
   // We insert the code into the preheader of the loop if the loop contains
   // multiple exit blocks, or in the exit block if there is exactly one.
@@ -349,7 +258,8 @@
         Value *InVal = PN->getIncomingValue(i);
         if (!isa<Instruction>(InVal) ||
             // SCEV only supports integer expressions for now.
-            !isa<IntegerType>(InVal->getType()))
+            (!isa<IntegerType>(InVal->getType()) &&
+             !isa<PointerType>(InVal->getType())))
           continue;
 
         // If this pred is for a subloop, not L itself, skip it.
@@ -384,7 +294,7 @@
         // just reuse it.
         Value *&ExitVal = ExitValues[Inst];
         if (!ExitVal)
-          ExitVal = Rewriter.expandCodeFor(ExitValue, InsertPt);
+          ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), InsertPt);
 
         DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal
              << "  LoopVal = " << *Inst << "\n";
@@ -413,23 +323,19 @@
 }
 
 void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) {
-  // First step.  Check to see if there are any trivial GEP pointer recurrences.
+  // First step.  Check to see if there are any floating-point recurrences.
   // If there are, change them into integer recurrences, permitting analysis by
   // the SCEV routines.
   //
   BasicBlock *Header    = L->getHeader();
-  BasicBlock *Preheader = L->getLoopPreheader();
 
   SmallPtrSet<Instruction*, 16> DeadInsts;
   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
     PHINode *PN = cast<PHINode>(I);
-    if (isa<PointerType>(PN->getType()))
-      EliminatePointerRecurrence(PN, Preheader, DeadInsts);
-    else
-      HandleFloatingPointIV(L, PN, DeadInsts);
+    HandleFloatingPointIV(L, PN, DeadInsts);
   }
 
-  // If the loop previously had a pointer or floating-point IV, ScalarEvolution
+  // If the loop previously had floating-point IV, ScalarEvolution
   // may not have been able to compute a trip count. Now that we've done some
   // re-writing, the trip count may be computable.
   if (Changed)
@@ -442,7 +348,8 @@
 /// getEffectiveIndvarType - Determine the widest type that the
 /// induction-variable PHINode Phi is cast to.
 ///
-static const Type *getEffectiveIndvarType(const PHINode *Phi) {
+static const Type *getEffectiveIndvarType(const PHINode *Phi,
+                                          const TargetData *TD) {
   const Type *Ty = Phi->getType();
 
   for (Value::use_const_iterator UI = Phi->use_begin(), UE = Phi->use_end();
@@ -453,8 +360,7 @@
     else if (const SExtInst *SI = dyn_cast<SExtInst>(UI))
       CandidateType = SI->getDestTy();
     if (CandidateType &&
-        CandidateType->getPrimitiveSizeInBits() >
-          Ty->getPrimitiveSizeInBits())
+        TD->getTypeSizeInBits(CandidateType) > TD->getTypeSizeInBits(Ty))
       Ty = CandidateType;
   }
 
@@ -482,6 +388,7 @@
 static const PHINode *TestOrigIVForWrap(const Loop *L,
                                         const BranchInst *BI,
                                         const Instruction *OrigCond,
+                                        const TargetData *TD,
                                         bool &NoSignedWrap,
                                         bool &NoUnsignedWrap,
                                         const ConstantInt* &InitialVal,
@@ -554,7 +461,7 @@
     if (const SExtInst *SI = dyn_cast<SExtInst>(CmpLHS)) {
       if (!isa<ConstantInt>(CmpRHS) ||
           !cast<ConstantInt>(CmpRHS)->getValue()
-            .isSignedIntN(IncrInst->getType()->getPrimitiveSizeInBits()))
+            .isSignedIntN(TD->getTypeSizeInBits(IncrInst->getType())))
         return 0;
       IncrInst = SI->getOperand(0);
     }
@@ -562,7 +469,7 @@
     if (const ZExtInst *ZI = dyn_cast<ZExtInst>(CmpLHS)) {
       if (!isa<ConstantInt>(CmpRHS) ||
           !cast<ConstantInt>(CmpRHS)->getValue()
-            .isIntN(IncrInst->getType()->getPrimitiveSizeInBits()))
+            .isIntN(TD->getTypeSizeInBits(IncrInst->getType())))
         return 0;
       IncrInst = ZI->getOperand(0);
     }
@@ -643,7 +550,7 @@
     SE->getAddRecExpr(ExtendedStart, ExtendedStep, L);
   if (LargestType != myType)
     ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType);
-  return Rewriter.expandCodeFor(ExtendedAddRec, InsertPt);
+  return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt);
 }
 
 static Value *getZeroExtendedTruncVar(const SCEVAddRecExpr *AR,
@@ -660,7 +567,7 @@
     SE->getAddRecExpr(ExtendedStart, ExtendedStep, L);
   if (LargestType != myType)
     ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType);
-  return Rewriter.expandCodeFor(ExtendedAddRec, InsertPt);
+  return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt);
 }
 
 /// allUsesAreSameTyped - See whether all Uses of I are instructions
@@ -682,10 +589,11 @@
 
 bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) {
   LI = &getAnalysis<LoopInfo>();
+  TD = &getAnalysis<TargetData>();
   SE = &getAnalysis<ScalarEvolution>();
   Changed = false;
 
-  // If there are any floating-point or pointer recurrences, attempt to
+  // If there are any floating-point recurrences, attempt to
   // transform them to use integer recurrences.
   RewriteNonIntegerIVs(L);
 
@@ -712,7 +620,7 @@
 
   for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) {
     PHINode *PN = cast<PHINode>(I);
-    if (PN->getType()->isInteger()) { // FIXME: when we have fast-math, enable!
+    if (PN->getType()->isInteger() || isa<PointerType>(PN->getType())) {
       SCEVHandle SCEV = SE->getSCEV(PN);
       // FIXME: It is an extremely bad idea to indvar substitute anything more
       // complex than affine induction variables.  Doing so will put expensive
@@ -731,21 +639,26 @@
   SmallSetVector<const Type *, 4> SizesToInsert;
   if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) {
     LargestType = BackedgeTakenCount->getType();
-    SizesToInsert.insert(BackedgeTakenCount->getType());
+    if (isa<PointerType>(LargestType))
+      LargestType = TD->getIntPtrType();
+    SizesToInsert.insert(LargestType);
   }
   for (unsigned i = 0, e = IndVars.size(); i != e; ++i) {
     const PHINode *PN = IndVars[i].first;
-    SizesToInsert.insert(PN->getType());
-    const Type *EffTy = getEffectiveIndvarType(PN);
+    const Type *PNTy = PN->getType();
+    if (isa<PointerType>(PNTy)) PNTy = TD->getIntPtrType();
+    SizesToInsert.insert(PNTy);
+    const Type *EffTy = getEffectiveIndvarType(PN, TD);
+    if (isa<PointerType>(EffTy)) EffTy = TD->getIntPtrType();
     SizesToInsert.insert(EffTy);
     if (!LargestType ||
-        EffTy->getPrimitiveSizeInBits() >
-          LargestType->getPrimitiveSizeInBits())
+        TD->getTypeSizeInBits(EffTy) >
+          TD->getTypeSizeInBits(LargestType))
       LargestType = EffTy;
   }
 
   // Create a rewriter object which we'll use to transform the code with.
-  SCEVExpander Rewriter(*SE, *LI);
+  SCEVExpander Rewriter(*SE, *LI, *TD);
 
   // Now that we know the largest of of the induction variables in this loop,
   // insert a canonical induction variable of the largest size.
@@ -769,7 +682,7 @@
       if (Instruction *OrigCond = dyn_cast<Instruction>(BI->getCondition())) {
         // Determine if the OrigIV will ever undergo overflow.
         OrigControllingPHI =
-          TestOrigIVForWrap(L, BI, OrigCond,
+          TestOrigIVForWrap(L, BI, OrigCond, TD,
                             NoSignedWrap, NoUnsignedWrap,
                             InitialVal, IncrVal, LimitVal);
 
@@ -804,7 +717,7 @@
   while (!IndVars.empty()) {
     PHINode *PN = IndVars.back().first;
     SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(IndVars.back().second);
-    Value *NewVal = Rewriter.expandCodeFor(AR, InsertPt);
+    Value *NewVal = Rewriter.expandCodeFor(AR, PN->getType(), InsertPt);
     DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *PN
          << "   into = " << *NewVal << "\n";
     NewVal->takeName(PN);

Modified: llvm/branches/Apple/Dib/lib/Transforms/Scalar/LoopStrengthReduce.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Transforms/Scalar/LoopStrengthReduce.cpp?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Transforms/Scalar/LoopStrengthReduce.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Transforms/Scalar/LoopStrengthReduce.cpp Fri Apr 17 13:49:34 2009
@@ -1,4 +1,4 @@
-//===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===//
+//===- LoopStrengthReduce.cpp - Strength Reduce IVs in Loops --------------===//
 //
 //                     The LLVM Compiler Infrastructure
 //
@@ -8,10 +8,7 @@
 //===----------------------------------------------------------------------===//
 //
 // This pass performs a strength reduction on array references inside loops that
-// have as one or more of their components the loop induction variable.  This is
-// accomplished by creating a new Value to hold the initial value of the array
-// access for the first iteration, and then creating a new GEP instruction in
-// the loop to increment the value by the appropriate amount.
+// have as one or more of their components the loop induction variable.
 //
 //===----------------------------------------------------------------------===//
 
@@ -36,12 +33,11 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/CommandLine.h"
-#include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Target/TargetLowering.h"
 #include <algorithm>
 using namespace llvm;
 
-STATISTIC(NumReduced ,    "Number of GEPs strength reduced");
+STATISTIC(NumReduced ,    "Number of IV uses strength reduced");
 STATISTIC(NumInserted,    "Number of PHIs inserted");
 STATISTIC(NumVariable,    "Number of PHIs with variable strides");
 STATISTIC(NumEliminated,  "Number of strides eliminated");
@@ -133,17 +129,6 @@
     /// dependent on random ordering of pointers in the process.
     SmallVector<SCEVHandle, 16> StrideOrder;
 
-    /// GEPlist - A list of the GEP's that have been remembered in the SCEV
-    /// data structures.  SCEV does not know to update these when the operands
-    /// of the GEP are changed, which means we cannot leave them live across
-    /// loops.
-    SmallVector<GetElementPtrInst *, 16> GEPlist;
-
-    /// CastedValues - As we need to cast values to uintptr_t, this keeps track
-    /// of the casted version of each value.  This is accessed by
-    /// getCastedVersionOf.
-    DenseMap<Value*, Value*> CastedPointers;
-
     /// DeadInsts - Keep track of instructions we may have made dead, so that
     /// we can remove them after we are done working.
     SmallVector<Instruction*, 16> DeadInsts;
@@ -175,14 +160,10 @@
       AU.addRequired<ScalarEvolution>();
       AU.addPreserved<ScalarEvolution>();
     }
-    
-    /// getCastedVersionOf - Return the specified value casted to uintptr_t.
-    ///
-    Value *getCastedVersionOf(Instruction::CastOps opcode, Value *V);
+
 private:
     bool AddUsersIfInteresting(Instruction *I, Loop *L,
                                SmallPtrSet<Instruction*,16> &Processed);
-    SCEVHandle GetExpressionSCEV(Instruction *E);
     ICmpInst *ChangeCompareStride(Loop *L, ICmpInst *Cond,
                                   IVStrideUse* &CondUse,
                                   const SCEVHandle* &CondStride);
@@ -249,24 +230,6 @@
   return new LoopStrengthReduce(TLI);
 }
 
-/// getCastedVersionOf - Return the specified value casted to uintptr_t. This
-/// assumes that the Value* V is of integer or pointer type only.
-///
-Value *LoopStrengthReduce::getCastedVersionOf(Instruction::CastOps opcode, 
-                                              Value *V) {
-  if (V->getType() == UIntPtrTy) return V;
-  if (Constant *CB = dyn_cast<Constant>(V))
-    return ConstantExpr::getCast(opcode, CB, UIntPtrTy);
-
-  Value *&New = CastedPointers[V];
-  if (New) return New;
-  
-  New = SCEVExpander::InsertCastOfTo(opcode, V, UIntPtrTy);
-  DeadInsts.push_back(cast<Instruction>(New));
-  return New;
-}
-
-
 /// DeleteTriviallyDeadInstructions - If any of the instructions is the
 /// specified set are trivially dead, delete them and see if this makes any of
 /// their operands subsequently dead.
@@ -308,74 +271,6 @@
   }
 }
 
-
-/// GetExpressionSCEV - Compute and return the SCEV for the specified
-/// instruction.
-SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp) {
-  // Pointer to pointer bitcast instructions return the same value as their
-  // operand.
-  if (BitCastInst *BCI = dyn_cast<BitCastInst>(Exp)) {
-    if (SE->hasSCEV(BCI) || !isa<Instruction>(BCI->getOperand(0)))
-      return SE->getSCEV(BCI);
-    SCEVHandle R = GetExpressionSCEV(cast<Instruction>(BCI->getOperand(0)));
-    SE->setSCEV(BCI, R);
-    return R;
-  }
-
-  // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions.
-  // If this is a GEP that SE doesn't know about, compute it now and insert it.
-  // If this is not a GEP, or if we have already done this computation, just let
-  // SE figure it out.
-  GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
-  if (!GEP || SE->hasSCEV(GEP))
-    return SE->getSCEV(Exp);
-    
-  // Analyze all of the subscripts of this getelementptr instruction, looking
-  // for uses that are determined by the trip count of the loop.  First, skip
-  // all operands the are not dependent on the IV.
-
-  // Build up the base expression.  Insert an LLVM cast of the pointer to
-  // uintptr_t first.
-  SCEVHandle GEPVal = SE->getUnknown(
-      getCastedVersionOf(Instruction::PtrToInt, GEP->getOperand(0)));
-
-  gep_type_iterator GTI = gep_type_begin(GEP);
-  
-  for (User::op_iterator i = GEP->op_begin() + 1, e = GEP->op_end();
-       i != e; ++i, ++GTI) {
-    // If this is a use of a recurrence that we can analyze, and it comes before
-    // Op does in the GEP operand list, we will handle this when we process this
-    // operand.
-    if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
-      const StructLayout *SL = TD->getStructLayout(STy);
-      unsigned Idx = cast<ConstantInt>(*i)->getZExtValue();
-      uint64_t Offset = SL->getElementOffset(Idx);
-      GEPVal = SE->getAddExpr(GEPVal,
-                             SE->getIntegerSCEV(Offset, UIntPtrTy));
-    } else {
-      unsigned GEPOpiBits = 
-        (*i)->getType()->getPrimitiveSizeInBits();
-      unsigned IntPtrBits = UIntPtrTy->getPrimitiveSizeInBits();
-      Instruction::CastOps opcode = (GEPOpiBits < IntPtrBits ? 
-          Instruction::SExt : (GEPOpiBits > IntPtrBits ? Instruction::Trunc :
-            Instruction::BitCast));
-      Value *OpVal = getCastedVersionOf(opcode, *i);
-      SCEVHandle Idx = SE->getSCEV(OpVal);
-
-      uint64_t TypeSize = TD->getTypePaddedSize(GTI.getIndexedType());
-      if (TypeSize != 1)
-        Idx = SE->getMulExpr(Idx,
-                            SE->getConstant(ConstantInt::get(UIntPtrTy,
-                                                             TypeSize)));
-      GEPVal = SE->getAddExpr(GEPVal, Idx);
-    }
-  }
-
-  SE->setSCEV(GEP, GEPVal);
-  GEPlist.push_back(GEP);
-  return GEPVal;
-}
-
 /// containsAddRecFromDifferentLoop - Determine whether expression S involves a 
 /// subexpression that is an AddRec from a loop other than L.  An outer loop 
 /// of L is OK, but not an inner loop nor a disjoint loop.
@@ -594,15 +489,14 @@
     return false;   // Void and FP expressions cannot be reduced.
 
   // LSR is not APInt clean, do not touch integers bigger than 64-bits.
-  if (I->getType()->isInteger() && 
-      I->getType()->getPrimitiveSizeInBits() > 64)
+  if (TD->getTypeSizeInBits(I->getType()) > 64)
     return false;
   
   if (!Processed.insert(I))
     return true;    // Instruction already handled.
   
   // Get the symbolic expression for this instruction.
-  SCEVHandle ISE = GetExpressionSCEV(I);
+  SCEVHandle ISE = SE->getSCEV(I);
   if (isa<SCEVCouldNotCompute>(ISE)) return false;
   
   // Get the start and stride for this expression.
@@ -722,6 +616,7 @@
                                       SmallVectorImpl<Instruction*> &DeadInsts);
     
     Value *InsertCodeForBaseAtPosition(const SCEVHandle &NewBase, 
+                                       const Type *Ty,
                                        SCEVExpander &Rewriter,
                                        Instruction *IP, Loop *L);
     void dump() const;
@@ -735,6 +630,7 @@
 }
 
 Value *BasedUser::InsertCodeForBaseAtPosition(const SCEVHandle &NewBase, 
+                                              const Type *Ty,
                                               SCEVExpander &Rewriter,
                                               Instruction *IP, Loop *L) {
   // Figure out where we *really* want to insert this code.  In particular, if
@@ -755,7 +651,7 @@
       InsertLoop = InsertLoop->getParentLoop();
     }
   
-  Value *Base = Rewriter.expandCodeFor(NewBase, BaseInsertPt);
+  Value *Base = Rewriter.expandCodeFor(NewBase, Ty, BaseInsertPt);
 
   // If there is no immediate value, skip the next part.
   if (Imm->isZero())
@@ -768,8 +664,7 @@
   
   // Always emit the immediate (if non-zero) into the same block as the user.
   SCEVHandle NewValSCEV = SE->getAddExpr(SE->getUnknown(Base), Imm);
-  return Rewriter.expandCodeFor(NewValSCEV, IP);
-  
+  return Rewriter.expandCodeFor(NewValSCEV, Ty, IP);
 }
 
 
@@ -809,15 +704,9 @@
         while (isa<PHINode>(InsertPt)) ++InsertPt;
       }
     }
-    Value *NewVal = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L);
-    // Adjust the type back to match the Inst. Note that we can't use InsertPt
-    // here because the SCEVExpander may have inserted the instructions after
-    // that point, in its efforts to avoid inserting redundant expressions.
-    if (isa<PointerType>(OperandValToReplace->getType())) {
-      NewVal = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr,
-                                            NewVal,
-                                            OperandValToReplace->getType());
-    }
+    Value *NewVal = InsertCodeForBaseAtPosition(NewBase,
+                                                OperandValToReplace->getType(),
+                                                Rewriter, InsertPt, L);
     // Replace the use of the operand Value with the new Phi we just created.
     Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
 
@@ -875,17 +764,8 @@
         Instruction *InsertPt = (L->contains(OldLoc->getParent())) ?
                                 PN->getIncomingBlock(i)->getTerminator() :
                                 OldLoc->getParent()->getTerminator();
-        Code = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L);
-
-        // Adjust the type back to match the PHI. Note that we can't use
-        // InsertPt here because the SCEVExpander may have inserted its
-        // instructions after that point, in its efforts to avoid inserting
-        // redundant expressions.
-        if (isa<PointerType>(PN->getType())) {
-          Code = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr,
-                                              Code,
-                                              PN->getType());
-        }
+        Code = InsertCodeForBaseAtPosition(NewBase, PN->getType(),
+                                           Rewriter, InsertPt, L);
 
         DOUT << "      Changing PHI use to ";
         DEBUG(WriteAsOperand(*DOUT, Code, /*PrintType=*/false));
@@ -921,16 +801,13 @@
   }
 
   if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
-    if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
-      if (TLI && CE->getOpcode() == Instruction::PtrToInt) {
-        Constant *Op0 = CE->getOperand(0);
-        if (GlobalValue *GV = dyn_cast<GlobalValue>(Op0)) {
-          TargetLowering::AddrMode AM;
-          AM.BaseGV = GV;
-          AM.HasBaseReg = HasBaseReg;
-          return TLI->isLegalAddressingMode(AM, UseTy);
-        }
-      }
+    if (GlobalValue *GV = dyn_cast<GlobalValue>(SU->getValue())) {
+      TargetLowering::AddrMode AM;
+      AM.BaseGV = GV;
+      AM.HasBaseReg = HasBaseReg;
+      return TLI->isLegalAddressingMode(AM, UseTy);
+    }
+
   return false;
 }
 
@@ -1591,6 +1468,7 @@
 ///
 static PHINode *InsertAffinePhi(SCEVHandle Start, SCEVHandle Step,
                                 const Loop *L,
+                                const TargetData *TD,
                                 SCEVExpander &Rewriter) {
   assert(Start->isLoopInvariant(L) && "New PHI start is not loop invariant!");
   assert(Step->isLoopInvariant(L) && "New PHI stride is not loop invariant!");
@@ -1598,9 +1476,11 @@
   BasicBlock *Header = L->getHeader();
   BasicBlock *Preheader = L->getLoopPreheader();
   BasicBlock *LatchBlock = L->getLoopLatch();
+  const Type *Ty = Start->getType();
+  if (isa<PointerType>(Ty)) Ty = TD->getIntPtrType();
 
-  PHINode *PN = PHINode::Create(Start->getType(), "lsr.iv", Header->begin());
-  PN->addIncoming(Rewriter.expandCodeFor(Start, Preheader->getTerminator()),
+  PHINode *PN = PHINode::Create(Ty, "lsr.iv", Header->begin());
+  PN->addIncoming(Rewriter.expandCodeFor(Start, Ty, Preheader->getTerminator()),
                   Preheader);
 
   // If the stride is negative, insert a sub instead of an add for the
@@ -1612,7 +1492,8 @@
 
   // Insert an add instruction right before the terminator corresponding
   // to the back-edge.
-  Value *StepV = Rewriter.expandCodeFor(IncAmount, Preheader->getTerminator());
+  Value *StepV = Rewriter.expandCodeFor(IncAmount, Ty,
+                                        Preheader->getTerminator());
   Instruction *IncV;
   if (isNegative) {
     IncV = BinaryOperator::CreateSub(PN, StepV, "lsr.iv.next",
@@ -1683,7 +1564,7 @@
     SCEVHandle Imm = UsersToProcess[i].Imm;
     SCEVHandle Base = UsersToProcess[i].Base;
     SCEVHandle Start = SE->getAddExpr(CommonExprs, Base, Imm);
-    PHINode *Phi = InsertAffinePhi(Start, Stride, L,
+    PHINode *Phi = InsertAffinePhi(Start, Stride, L, TD,
                                    PreheaderRewriter);
     // Loop over all the users with the same base.
     do {
@@ -1710,7 +1591,7 @@
   DOUT << "  Inserting new PHI:\n";
 
   PHINode *Phi = InsertAffinePhi(SE->getUnknown(CommonBaseV),
-                                 Stride, L,
+                                 Stride, L, TD,
                                  PreheaderRewriter);
 
   // Remember this in case a later stride is multiple of this.
@@ -1816,6 +1697,7 @@
   bool HaveCommonExprs = !CommonExprs->isZero();
 
   const Type *ReplacedTy = CommonExprs->getType();
+  if (isa<PointerType>(ReplacedTy)) ReplacedTy = TD->getIntPtrType();
 
   // If all uses are addresses, consider sinking the immediate part of the
   // common expression back into uses if they can fit in the immediate fields.
@@ -1829,11 +1711,8 @@
       // If the immediate part of the common expression is a GV, check if it's
       // possible to fold it into the target addressing mode.
       GlobalValue *GV = 0;
-      if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(Imm)) {
-        if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
-          if (CE->getOpcode() == Instruction::PtrToInt)
-            GV = dyn_cast<GlobalValue>(CE->getOperand(0));
-      }
+      if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(Imm))
+        GV = dyn_cast<GlobalValue>(SU->getValue());
       int64_t Offset = 0;
       if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Imm))
         Offset = SC->getValue()->getSExtValue();
@@ -1860,8 +1739,8 @@
        << *Stride << ":\n"
        << "  Common base: " << *CommonExprs << "\n";
 
-  SCEVExpander Rewriter(*SE, *LI);
-  SCEVExpander PreheaderRewriter(*SE, *LI);
+  SCEVExpander Rewriter(*SE, *LI, *TD);
+  SCEVExpander PreheaderRewriter(*SE, *LI, *TD);
 
   BasicBlock  *Preheader = L->getLoopPreheader();
   Instruction *PreInsertPt = Preheader->getTerminator();
@@ -1882,7 +1761,8 @@
                                  PreheaderRewriter);
   } else {
     // Emit the initial base value into the loop preheader.
-    CommonBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt);
+    CommonBaseV = PreheaderRewriter.expandCodeFor(CommonExprs, ReplacedTy,
+                                                  PreInsertPt);
 
     // If all uses are addresses, check if it is possible to reuse an IV with a
     // stride that is a factor of this stride. And that the multiple is a number
@@ -1910,19 +1790,21 @@
     Instruction *Inst = UsersToProcess.back().Inst;
 
     // Emit the code for Base into the preheader.
-    Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt);
-
-    DOUT << "  Examining uses with BASE ";
-    DEBUG(WriteAsOperand(*DOUT, BaseV, /*PrintType=*/false));
-    DOUT << ":\n";
-
-    // If BaseV is a constant other than 0, make sure that it gets inserted into
-    // the preheader, instead of being forward substituted into the uses.  We do
-    // this by forcing a BitCast (noop cast) to be inserted into the preheader 
-    // in this case.
-    if (Constant *C = dyn_cast<Constant>(BaseV)) {
-      if (!C->isNullValue() && !fitsInAddressMode(Base, getAccessType(Inst),
-                                                 TLI, false)) {
+    Value *BaseV = 0;
+    if (!Base->isZero()) {
+      BaseV = PreheaderRewriter.expandCodeFor(Base, Base->getType(),
+                                              PreInsertPt);
+
+      DOUT << "  INSERTING code for BASE = " << *Base << ":";
+      if (BaseV->hasName())
+        DOUT << " Result value name = %" << BaseV->getNameStr();
+      DOUT << "\n";
+
+      // If BaseV is a non-zero constant, make sure that it gets inserted into
+      // the preheader, instead of being forward substituted into the uses.  We
+      // do this by forcing a BitCast (noop cast) to be inserted into the
+      // preheader in this case.
+      if (!fitsInAddressMode(Base, getAccessType(Inst), TLI, false)) {
         // We want this constant emitted into the preheader! This is just
         // using cast as a copy so BitCast (no-op cast) is appropriate
         BaseV = new BitCastInst(BaseV, BaseV->getType(), "preheaderinsert",
@@ -1952,16 +1834,17 @@
         if (L->contains(User.Inst->getParent()))
           User.Inst->moveBefore(LatchBlock->getTerminator());
       }
-      if (RewriteOp->getType() != ReplacedTy) {
-        Instruction::CastOps opcode = Instruction::Trunc;
-        if (ReplacedTy->getPrimitiveSizeInBits() ==
-            RewriteOp->getType()->getPrimitiveSizeInBits())
-          opcode = Instruction::BitCast;
-        RewriteOp = SCEVExpander::InsertCastOfTo(opcode, RewriteOp, ReplacedTy);
-      }
 
       SCEVHandle RewriteExpr = SE->getUnknown(RewriteOp);
 
+      if (TD->getTypeSizeInBits(RewriteOp->getType()) !=
+          TD->getTypeSizeInBits(ReplacedTy)) {
+        assert(TD->getTypeSizeInBits(RewriteOp->getType()) >
+               TD->getTypeSizeInBits(ReplacedTy) &&
+               "Unexpected widening cast!");
+        RewriteExpr = SE->getTruncateExpr(RewriteExpr, ReplacedTy);
+      }
+
       // If we had to insert new instructions for RewriteOp, we have to
       // consider that they may not have been able to end up immediately
       // next to RewriteOp, because non-PHI instructions may never precede
@@ -1978,22 +1861,21 @@
 
       // If we are reusing the iv, then it must be multiplied by a constant
       // factor to take advantage of the addressing mode scale component.
-      if (!isa<SCEVConstant>(RewriteFactor) ||
-          !cast<SCEVConstant>(RewriteFactor)->isZero()) {
+      if (!RewriteFactor->isZero()) {
         // If we're reusing an IV with a nonzero base (currently this happens
         // only when all reuses are outside the loop) subtract that base here.
         // The base has been used to initialize the PHI node but we don't want
         // it here.
         if (!ReuseIV.Base->isZero()) {
           SCEVHandle typedBase = ReuseIV.Base;
-          if (RewriteExpr->getType()->getPrimitiveSizeInBits() !=
-              ReuseIV.Base->getType()->getPrimitiveSizeInBits()) {
+          if (TD->getTypeSizeInBits(RewriteExpr->getType()) !=
+              TD->getTypeSizeInBits(ReuseIV.Base->getType())) {
             // It's possible the original IV is a larger type than the new IV,
             // in which case we have to truncate the Base.  We checked in
             // RequiresTypeConversion that this is valid.
-            assert (RewriteExpr->getType()->getPrimitiveSizeInBits() <
-                    ReuseIV.Base->getType()->getPrimitiveSizeInBits() &&
-                    "Unexpected lengthening conversion!");
+            assert(TD->getTypeSizeInBits(RewriteExpr->getType()) <
+                   TD->getTypeSizeInBits(ReuseIV.Base->getType()) &&
+                   "Unexpected lengthening conversion!");
             typedBase = SE->getTruncateExpr(ReuseIV.Base, 
                                             RewriteExpr->getType());
           }
@@ -2010,8 +1892,7 @@
         // When this use is outside the loop, we earlier subtracted the
         // common base, and are adding it back here.  Use the same expression
         // as before, rather than CommonBaseV, so DAGCombiner will zap it.
-        if (!isa<ConstantInt>(CommonBaseV) ||
-            !cast<ConstantInt>(CommonBaseV)->isZero()) {
+        if (!CommonExprs->isZero()) {
           if (L->contains(User.Inst->getParent()))
             RewriteExpr = SE->getAddExpr(RewriteExpr,
                                        SE->getUnknown(CommonBaseV));
@@ -2022,7 +1903,7 @@
 
       // Now that we know what we need to do, insert code before User for the
       // immediate and any loop-variant expressions.
-      if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isZero())
+      if (BaseV)
         // Add BaseV to the PHI value if needed.
         RewriteExpr = SE->getAddExpr(RewriteExpr, SE->getUnknown(BaseV));
 
@@ -2078,6 +1959,9 @@
   // e.g.
   // 4, -1, X, 1, 2 ==> 1, -1, 2, 4, X
   struct StrideCompare {
+    const TargetData *TD;
+    explicit StrideCompare(const TargetData *td) : TD(td) {}
+
     bool operator()(const SCEVHandle &LHS, const SCEVHandle &RHS) {
       SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS);
       SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS);
@@ -2096,7 +1980,8 @@
         // If it's the same value but different type, sort by bit width so
         // that we emit larger induction variables before smaller
         // ones, letting the smaller be re-written in terms of larger ones.
-        return RHS->getBitWidth() < LHS->getBitWidth();
+        return TD->getTypeSizeInBits(RHS->getType()) <
+               TD->getTypeSizeInBits(LHS->getType());
       }
       return LHSC && !RHSC;
     }
@@ -2129,11 +2014,11 @@
 
   ICmpInst::Predicate Predicate = Cond->getPredicate();
   int64_t CmpSSInt = SC->getValue()->getSExtValue();
-  unsigned BitWidth = (*CondStride)->getBitWidth();
+  unsigned BitWidth = TD->getTypeSizeInBits((*CondStride)->getType());
   uint64_t SignBit = 1ULL << (BitWidth-1);
   const Type *CmpTy = Cond->getOperand(0)->getType();
   const Type *NewCmpTy = NULL;
-  unsigned TyBits = CmpTy->getPrimitiveSizeInBits();
+  unsigned TyBits = TD->getTypeSizeInBits(CmpTy);
   unsigned NewTyBits = 0;
   SCEVHandle *NewStride = NULL;
   Value *NewCmpLHS = NULL;
@@ -2185,22 +2070,15 @@
         continue;
 
       NewCmpTy = NewCmpLHS->getType();
-      NewTyBits = isa<PointerType>(NewCmpTy)
-        ? UIntPtrTy->getPrimitiveSizeInBits()
-        : NewCmpTy->getPrimitiveSizeInBits();
+      NewTyBits = TD->getTypeSizeInBits(NewCmpTy);
       if (RequiresTypeConversion(NewCmpTy, CmpTy)) {
         // Check if it is possible to rewrite it using
         // an iv / stride of a smaller integer type.
-        bool TruncOk = false;
-        if (NewCmpTy->isInteger()) {
-          unsigned Bits = NewTyBits;
-          if (ICmpInst::isSignedPredicate(Predicate))
-            --Bits;
-          uint64_t Mask = (1ULL << Bits) - 1;
-          if (((uint64_t)NewCmpVal & Mask) == (uint64_t)NewCmpVal)
-            TruncOk = true;
-        }
-        if (!TruncOk)
+        unsigned Bits = NewTyBits;
+        if (ICmpInst::isSignedPredicate(Predicate))
+          --Bits;
+        uint64_t Mask = (1ULL << Bits) - 1;
+        if (((uint64_t)NewCmpVal & Mask) != (uint64_t)NewCmpVal)
           continue;
       }
 
@@ -2233,9 +2111,8 @@
       if (!isa<PointerType>(NewCmpTy))
         NewCmpRHS = ConstantInt::get(NewCmpTy, NewCmpVal);
       else {
-        NewCmpRHS = ConstantInt::get(UIntPtrTy, NewCmpVal);
-        NewCmpRHS = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr,
-                                                 NewCmpRHS, NewCmpTy);
+        ConstantInt *CI = ConstantInt::get(UIntPtrTy, NewCmpVal);
+        NewCmpRHS = ConstantExpr::getIntToPtr(CI, NewCmpTy);
       }
       NewOffset = TyBits == NewTyBits
         ? SE->getMulExpr(CondUse->Offset,
@@ -2604,7 +2481,7 @@
 #endif
 
     // Sort the StrideOrder so we process larger strides first.
-    std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());
+    std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare(TD));
 
     // Optimize induction variables.  Some indvar uses can be transformed to use
     // strides that will be needed for other purposes.  A common example of this
@@ -2638,13 +2515,9 @@
   }
 
   // We're done analyzing this loop; release all the state we built up for it.
-  CastedPointers.clear();
   IVUsesByStride.clear();
   IVsByStride.clear();
   StrideOrder.clear();
-  for (unsigned i=0; i<GEPlist.size(); i++)
-    SE->deleteValueFromRecords(GEPlist[i]);
-  GEPlist.clear();  
 
   // Clean up after ourselves
   if (!DeadInsts.empty()) {

Modified: llvm/branches/Apple/Dib/test/CodeGen/ARM/2007-03-13-InstrSched.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/test/CodeGen/ARM/2007-03-13-InstrSched.ll?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/test/CodeGen/ARM/2007-03-13-InstrSched.ll (original)
+++ llvm/branches/Apple/Dib/test/CodeGen/ARM/2007-03-13-InstrSched.ll Fri Apr 17 13:49:34 2009
@@ -1,5 +1,5 @@
 ; RUN: llvm-as < %s | llc -mtriple=arm-apple-darwin -relocation-model=pic \
-; RUN:   -mattr=+v6 -stats |& grep asm-printer | grep 41
+; RUN:   -mattr=+v6 -ifcvt-limit=0 -stats |& grep asm-printer | grep 35
 
 define void @test(i32 %tmp56222, i32 %tmp36224, i32 %tmp46223, i32 %i.0196.0.ph, i32 %tmp8, i32* %tmp1011, i32** %tmp1, i32* %d2.1.out, i32* %d3.1.out, i32* %d0.1.out, i32* %d1.1.out) {
 newFuncRoot:

Modified: llvm/branches/Apple/Dib/test/CodeGen/X86/iv-users-in-other-loops.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/test/CodeGen/X86/iv-users-in-other-loops.ll?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/test/CodeGen/X86/iv-users-in-other-loops.ll (original)
+++ llvm/branches/Apple/Dib/test/CodeGen/X86/iv-users-in-other-loops.ll Fri Apr 17 13:49:34 2009
@@ -1,7 +1,8 @@
 ; RUN: llvm-as < %s | llc -march=x86-64 -f -o %t
 ; RUN: grep inc %t | count 2
 ; RUN: grep addq %t | count 13
-; RUN: grep leaq %t | count 9
+; RUN: grep leaq %t | count 8
+; RUN: grep leal %t | count 4
 ; RUN: grep movq %t | count 5
 
 ; IV users in each of the loops from other loops shouldn't cause LSR

Modified: llvm/branches/Apple/Dib/test/CodeGen/X86/pr3495.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/test/CodeGen/X86/pr3495.ll?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/test/CodeGen/X86/pr3495.ll (original)
+++ llvm/branches/Apple/Dib/test/CodeGen/X86/pr3495.ll Fri Apr 17 13:49:34 2009
@@ -1,5 +1,6 @@
-; RUN: llvm-as < %s | llc -march=x86 -stats |& grep {Number of reloads omited}
-; RUN: llvm-as < %s | llc -march=x86 -stats |& grep {Number of available reloads turned into copies}
+; RUN: llvm-as < %s | llc -march=x86 -stats |& grep {Number of reloads omited} | grep 2
+; RUN: llvm-as < %s | llc -march=x86 -stats |& not grep {Number of available reloads turned into copies}
+; RUN: llvm-as < %s | llc -march=x86 -stats |& grep {Number of machine instrs printed} | grep 39
 ; PR3495
 
 target triple = "i386-pc-linux-gnu"

Modified: llvm/branches/Apple/Dib/test/CodeGen/X86/stride-nine-with-base-reg.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/test/CodeGen/X86/stride-nine-with-base-reg.ll?rev=69367&r1=69366&r2=69367&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/test/CodeGen/X86/stride-nine-with-base-reg.ll (original)
+++ llvm/branches/Apple/Dib/test/CodeGen/X86/stride-nine-with-base-reg.ll Fri Apr 17 13:49:34 2009
@@ -1,7 +1,7 @@
 ; RUN: llvm-as < %s | llc -march=x86 -relocation-model=static | not grep lea
 ; RUN: llvm-as < %s | llc -march=x86-64 | not grep lea
 
-; _P should be sunk into the loop and folded into the address mode. There
+; P should be sunk into the loop and folded into the address mode. There
 ; shouldn't be any lea instructions inside the loop.
 
 @B = external global [1000 x i8], align 32





More information about the llvm-branch-commits mailing list