diff options
| author | Dan Gohman <gohman@apple.com> | 2009-02-20 21:27:23 +0000 | 
|---|---|---|
| committer | Dan Gohman <gohman@apple.com> | 2009-02-20 21:27:23 +0000 | 
| commit | 5e309a5bbbaaf577496ae8a012e3b3f4d8f811a0 (patch) | |
| tree | 2a766f99ec19db37cf0f189be5a6017033a2c0cb /llvm/lib/Transforms | |
| parent | 4d1eb76ada7fa4834ce8adb4086ec831d8f19028 (diff) | |
| download | bcm5719-llvm-5e309a5bbbaaf577496ae8a012e3b3f4d8f811a0.tar.gz bcm5719-llvm-5e309a5bbbaaf577496ae8a012e3b3f4d8f811a0.zip | |
Simplify code and reduce indentation. No functionality change.
llvm-svn: 65167
Diffstat (limited to 'llvm/lib/Transforms')
| -rw-r--r-- | llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp | 150 | 
1 files changed, 70 insertions, 80 deletions
| diff --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp index ab8afe10014..a52b7d0f1c2 100644 --- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp +++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp @@ -2074,9 +2074,9 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,    const Type *NewCmpTy = NULL;    unsigned TyBits = CmpTy->getPrimitiveSizeInBits();    unsigned NewTyBits = 0; -  int64_t NewCmpVal = CmpVal;    SCEVHandle *NewStride = NULL; -  Value *NewIncV = NULL; +  Value *NewCmpLHS = NULL; +  Value *NewCmpRHS = NULL;    int64_t Scale = 1;    // Check stride constant and the comparision constant signs to detect @@ -2096,87 +2096,84 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,        continue;      Scale = SSInt / CmpSSInt; -    NewCmpVal = CmpVal * Scale; +    int64_t NewCmpVal = CmpVal * Scale;      APInt Mul = APInt(BitWidth, NewCmpVal);      // Check for overflow. -    if (Mul.getSExtValue() != NewCmpVal) { -      NewCmpVal = CmpVal; +    if (Mul.getSExtValue() != NewCmpVal)        continue; -    }      // Watch out for overflow.      if (ICmpInst::isSignedPredicate(Predicate) &&          (CmpVal & SignBit) != (NewCmpVal & SignBit)) -      NewCmpVal = CmpVal; - -    if (NewCmpVal != CmpVal) { -      // Pick the best iv to use trying to avoid a cast. -      NewIncV = NULL; -      for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(), -             E = SI->second.Users.end(); UI != E; ++UI) { -        NewIncV = UI->OperandValToReplace; -        if (NewIncV->getType() == CmpTy) -          break; -      } -      if (!NewIncV) { -        NewCmpVal = CmpVal; -        continue; -      } +      continue; -      NewCmpTy = NewIncV->getType(); -      NewTyBits = isa<PointerType>(NewCmpTy) -        ? UIntPtrTy->getPrimitiveSizeInBits() -        : NewCmpTy->getPrimitiveSizeInBits(); -      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) { -          NewCmpVal = CmpVal; -          continue; -        } -      } +    if (NewCmpVal == CmpVal) +      continue; +    // Pick the best iv to use trying to avoid a cast. +    NewCmpLHS = NULL; +    for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(), +           E = SI->second.Users.end(); UI != E; ++UI) { +      NewCmpLHS = UI->OperandValToReplace; +      if (NewCmpLHS->getType() == CmpTy) +        break; +    } +    if (!NewCmpLHS) +      continue; -      // Don't rewrite if use offset is non-constant and the new type is -      // of a different type. -      // FIXME: too conservative? -      if (NewTyBits != TyBits && !isa<SCEVConstant>(CondUse->Offset)) { -        NewCmpVal = CmpVal; -        continue; +    NewCmpTy = NewCmpLHS->getType(); +    NewTyBits = isa<PointerType>(NewCmpTy) +      ? UIntPtrTy->getPrimitiveSizeInBits() +      : NewCmpTy->getPrimitiveSizeInBits(); +    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;        } - -      bool AllUsesAreAddresses = true; -      bool AllUsesAreOutsideLoop = true; -      std::vector<BasedUser> UsersToProcess; -      SCEVHandle CommonExprs = CollectIVUsers(SI->first, SI->second, L, -                                              AllUsesAreAddresses, -                                              AllUsesAreOutsideLoop, -                                              UsersToProcess); -      // Avoid rewriting the compare instruction with an iv of new stride -      // if it's likely the new stride uses will be rewritten using the -      // stride of the compare instruction. -      if (AllUsesAreAddresses && -          ValidStride(!CommonExprs->isZero(), Scale, UsersToProcess)) { -        NewCmpVal = CmpVal; +      if (!TruncOk)          continue; -      } +    } -      // If scale is negative, use swapped predicate unless it's testing -      // for equality. -      if (Scale < 0 && !Cond->isEquality()) -        Predicate = ICmpInst::getSwappedPredicate(Predicate); +    // Don't rewrite if use offset is non-constant and the new type is +    // of a different type. +    // FIXME: too conservative? +    if (NewTyBits != TyBits && !isa<SCEVConstant>(CondUse->Offset)) +      continue; -      NewStride = &StrideOrder[i]; -      break; +    bool AllUsesAreAddresses = true; +    bool AllUsesAreOutsideLoop = true; +    std::vector<BasedUser> UsersToProcess; +    SCEVHandle CommonExprs = CollectIVUsers(SI->first, SI->second, L, +                                            AllUsesAreAddresses, +                                            AllUsesAreOutsideLoop, +                                            UsersToProcess); +    // Avoid rewriting the compare instruction with an iv of new stride +    // if it's likely the new stride uses will be rewritten using the +    // stride of the compare instruction. +    if (AllUsesAreAddresses && +        ValidStride(!CommonExprs->isZero(), Scale, UsersToProcess)) +      continue; + +    // If scale is negative, use swapped predicate unless it's testing +    // for equality. +    if (Scale < 0 && !Cond->isEquality()) +      Predicate = ICmpInst::getSwappedPredicate(Predicate); + +    NewStride = &StrideOrder[i]; +    if (!isa<PointerType>(NewCmpTy)) +      NewCmpRHS = ConstantInt::get(NewCmpTy, NewCmpVal); +    else { +      NewCmpRHS = ConstantInt::get(UIntPtrTy, NewCmpVal); +      NewCmpRHS = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr, +                                               NewCmpRHS, NewCmpTy);      } +    break;    }    // Forgo this transformation if it the increment happens to be @@ -2188,22 +2185,15 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,    if (!Cond->hasOneUse()) {      for (BasicBlock::iterator I = Cond, E = Cond->getParent()->end();           I != E; ++I) -      if (I == NewIncV) +      if (I == NewCmpLHS)          return Cond;    } -  if (NewCmpVal != CmpVal) { +  if (NewCmpRHS) {      // Create a new compare instruction using new stride / iv.      ICmpInst *OldCond = Cond; -    Value *RHS; -    if (!isa<PointerType>(NewCmpTy)) -      RHS = ConstantInt::get(NewCmpTy, NewCmpVal); -    else { -      RHS = ConstantInt::get(UIntPtrTy, NewCmpVal); -      RHS = SCEVExpander::InsertCastOfTo(Instruction::IntToPtr, RHS, NewCmpTy); -    }      // Insert new compare instruction. -    Cond = new ICmpInst(Predicate, NewIncV, RHS, +    Cond = new ICmpInst(Predicate, NewCmpLHS, NewCmpRHS,                          L->getHeader()->getName() + ".termcond",                          OldCond); @@ -2219,7 +2209,7 @@ ICmpInst *LoopStrengthReduce::ChangeCompareStride(Loop *L, ICmpInst *Cond,                         SE->getConstant(ConstantInt::get(CmpTy, Scale)))        : SE->getConstant(ConstantInt::get(NewCmpTy,          cast<SCEVConstant>(CondUse->Offset)->getValue()->getSExtValue()*Scale)); -    IVUsesByStride[*NewStride].addUser(NewOffset, Cond, NewIncV); +    IVUsesByStride[*NewStride].addUser(NewOffset, Cond, NewCmpLHS);      CondUse = &IVUsesByStride[*NewStride].Users.back();      CondStride = NewStride;      ++NumEliminated; | 

