switch some std::vector's to smallvector. Reduce nesting.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@92346 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-12-31 07:48:51 +00:00
parent ec531233a1
commit 8d93b259f6

View File

@ -494,7 +494,7 @@ static unsigned FindInOperandList(std::vector<ValueEntry> &Ops, unsigned i,
/// EmitAddTreeOfValues - Emit a tree of add instructions, summing Ops together /// EmitAddTreeOfValues - Emit a tree of add instructions, summing Ops together
/// and returning the result. Insert the tree before I. /// and returning the result. Insert the tree before I.
static Value *EmitAddTreeOfValues(Instruction *I, std::vector<Value*> &Ops) { static Value *EmitAddTreeOfValues(Instruction *I, SmallVectorImpl<Value*> &Ops){
if (Ops.size() == 1) return Ops.back(); if (Ops.size() == 1) return Ops.back();
Value *V1 = Ops.back(); Value *V1 = Ops.back();
@ -535,7 +535,7 @@ Value *Reassociate::RemoveFactorFromExpression(Value *V, Value *Factor) {
/// FindSingleUseMultiplyFactors - If V is a single-use multiply, recursively /// FindSingleUseMultiplyFactors - If V is a single-use multiply, recursively
/// add its operands as factors, otherwise add V to the list of factors. /// add its operands as factors, otherwise add V to the list of factors.
static void FindSingleUseMultiplyFactors(Value *V, static void FindSingleUseMultiplyFactors(Value *V,
std::vector<Value*> &Factors) { SmallVectorImpl<Value*> &Factors) {
BinaryOperator *BO; BinaryOperator *BO;
if ((!V->hasOneUse() && !V->use_empty()) || if ((!V->hasOneUse() && !V->use_empty()) ||
!(BO = dyn_cast<BinaryOperator>(V)) || !(BO = dyn_cast<BinaryOperator>(V)) ||
@ -575,17 +575,18 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
if (CstVal->isZero()) { // ... & 0 -> 0 if (CstVal->isZero()) { // ... & 0 -> 0
++NumAnnihil; ++NumAnnihil;
return CstVal; return CstVal;
} else if (CstVal->isAllOnesValue()) { // ... & -1 -> ...
Ops.pop_back();
} }
if (CstVal->isAllOnesValue()) // ... & -1 -> ...
Ops.pop_back();
break; break;
case Instruction::Mul: case Instruction::Mul:
if (CstVal->isZero()) { // ... * 0 -> 0 if (CstVal->isZero()) { // ... * 0 -> 0
++NumAnnihil; ++NumAnnihil;
return CstVal; return CstVal;
} else if (cast<ConstantInt>(CstVal)->isOne()) {
Ops.pop_back(); // ... * 1 -> ...
} }
if (cast<ConstantInt>(CstVal)->isOne())
Ops.pop_back(); // ... * 1 -> ...
break; break;
case Instruction::Or: case Instruction::Or:
if (CstVal->isAllOnesValue()) { // ... | -1 -> -1 if (CstVal->isAllOnesValue()) { // ... | -1 -> -1
@ -620,7 +621,9 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
if (Opcode == Instruction::And) { // ...&X&~X = 0 if (Opcode == Instruction::And) { // ...&X&~X = 0
++NumAnnihil; ++NumAnnihil;
return Constant::getNullValue(X->getType()); return Constant::getNullValue(X->getType());
} else if (Opcode == Instruction::Or) { // ...|X|~X = -1 }
if (Opcode == Instruction::Or) { // ...|X|~X = -1
++NumAnnihil; ++NumAnnihil;
return Constant::getAllOnesValue(X->getType()); return Constant::getAllOnesValue(X->getType());
} }
@ -659,15 +662,19 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
for (unsigned i = 0, e = Ops.size(); i != e; ++i) { for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
assert(i < Ops.size()); assert(i < Ops.size());
// Check for X and -X in the operand list. // Check for X and -X in the operand list.
if (BinaryOperator::isNeg(Ops[i].Op)) { if (!BinaryOperator::isNeg(Ops[i].Op))
continue;
Value *X = BinaryOperator::getNegArgument(Ops[i].Op); Value *X = BinaryOperator::getNegArgument(Ops[i].Op);
unsigned FoundX = FindInOperandList(Ops, i, X); unsigned FoundX = FindInOperandList(Ops, i, X);
if (FoundX != i) { if (FoundX == i)
continue;
// Remove X and -X from the operand list. // Remove X and -X from the operand list.
if (Ops.size() == 2) { if (Ops.size() == 2) {
++NumAnnihil; ++NumAnnihil;
return Constant::getNullValue(X->getType()); return Constant::getNullValue(X->getType());
} else { }
Ops.erase(Ops.begin()+i); Ops.erase(Ops.begin()+i);
if (i < FoundX) if (i < FoundX)
--FoundX; --FoundX;
@ -679,10 +686,6 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
--i; // Revisit element. --i; // Revisit element.
e -= 2; // Removed two elements. e -= 2; // Removed two elements.
} }
}
}
}
// Scan the operand list, checking to see if there are any common factors // Scan the operand list, checking to see if there are any common factors
// between operands. Consider something like A*A+A*B*C+D. We would like to // between operands. Consider something like A*A+A*B*C+D. We would like to
@ -693,10 +696,12 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
unsigned MaxOcc = 0; unsigned MaxOcc = 0;
Value *MaxOccVal = 0; Value *MaxOccVal = 0;
for (unsigned i = 0, e = Ops.size(); i != e; ++i) { for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(Ops[i].Op)) { BinaryOperator *BOp = dyn_cast<BinaryOperator>(Ops[i].Op);
if (BOp->getOpcode() == Instruction::Mul && BOp->use_empty()) { if (BOp == 0 || BOp->getOpcode() != Instruction::Mul || !BOp->use_empty())
continue;
// Compute all of the factors of this added value. // Compute all of the factors of this added value.
std::vector<Value*> Factors; SmallVector<Value*, 8> Factors;
FindSingleUseMultiplyFactors(BOp, Factors); FindSingleUseMultiplyFactors(BOp, Factors);
assert(Factors.size() > 1 && "Bad linearize!"); assert(Factors.size() > 1 && "Bad linearize!");
@ -711,15 +716,13 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
} else { } else {
SmallPtrSet<Value*, 4> Duplicates; SmallPtrSet<Value*, 4> Duplicates;
for (unsigned i = 0, e = Factors.size(); i != e; ++i) { for (unsigned i = 0, e = Factors.size(); i != e; ++i) {
if (Duplicates.insert(Factors[i])) { if (!Duplicates.insert(Factors[i])) continue;
unsigned Occ = ++FactorOccurrences[Factors[i]]; unsigned Occ = ++FactorOccurrences[Factors[i]];
if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[i]; } if (Occ > MaxOcc) { MaxOcc = Occ; MaxOccVal = Factors[i]; }
} }
} }
} }
}
}
}
// If any factor occurred more than one time, we can pull it out. // If any factor occurred more than one time, we can pull it out.
if (MaxOcc > 1) { if (MaxOcc > 1) {
@ -730,7 +733,7 @@ Value *Reassociate::OptimizeExpression(BinaryOperator *I,
// from an expression will drop a use of maxocc, and this can cause // from an expression will drop a use of maxocc, and this can cause
// RemoveFactorFromExpression on successive values to behave differently. // RemoveFactorFromExpression on successive values to behave differently.
Instruction *DummyInst = BinaryOperator::CreateAdd(MaxOccVal, MaxOccVal); Instruction *DummyInst = BinaryOperator::CreateAdd(MaxOccVal, MaxOccVal);
std::vector<Value*> NewMulOps; SmallVector<Value*, 4> NewMulOps;
for (unsigned i = 0, e = Ops.size(); i != e; ++i) { for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
if (Value *V = RemoveFactorFromExpression(Ops[i].Op, MaxOccVal)) { if (Value *V = RemoveFactorFromExpression(Ops[i].Op, MaxOccVal)) {
NewMulOps.push_back(V); NewMulOps.push_back(V);