Simplify the code and rearrange it. No major functionality changes here.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@21759 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner
2005-05-07 04:08:02 +00:00
parent 39cef60259
commit 08b43921e1

View File

@ -12,9 +12,6 @@
// //
// For example: 4 + (x + 5) -> x + (4 + 5) // For example: 4 + (x + 5) -> x + (4 + 5)
// //
// Note that this pass works best if left shifts have been promoted to explicit
// multiplies before this pass executes.
//
// In the implementation of this algorithm, constants are assigned rank = 0, // In the implementation of this algorithm, constants are assigned rank = 0,
// function arguments are rank = 1, and other values are assigned ranks // function arguments are rank = 1, and other values are assigned ranks
// corresponding to the reverse post order traversal of current function // corresponding to the reverse post order traversal of current function
@ -23,6 +20,7 @@
// //
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
#define DEBUG_TYPE "reassociate"
#include "llvm/Transforms/Scalar.h" #include "llvm/Transforms/Scalar.h"
#include "llvm/Function.h" #include "llvm/Function.h"
#include "llvm/Instructions.h" #include "llvm/Instructions.h"
@ -78,7 +76,12 @@ void Reassociate::BuildRankMap(Function &F) {
unsigned Reassociate::getRank(Value *V) { unsigned Reassociate::getRank(Value *V) {
if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument... if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument...
if (Instruction *I = dyn_cast<Instruction>(V)) { Instruction *I = dyn_cast<Instruction>(V);
if (I == 0) return 0; // Otherwise it's a global or constant, rank 0.
unsigned &CachedRank = ValueRankMap[I];
if (CachedRank) return CachedRank; // Rank already known?
// If this is an expression, return the 1+MAX(rank(LHS), rank(RHS)) so that // If this is an expression, return the 1+MAX(rank(LHS), rank(RHS)) so that
// we can reassociate expressions for code motion! Since we do not recurse // we can reassociate expressions for code motion! Since we do not recurse
// for PHI nodes, we cannot have infinite recursion here, because there // for PHI nodes, we cannot have infinite recursion here, because there
@ -90,9 +93,6 @@ unsigned Reassociate::getRank(Value *V) {
I->mayWriteToMemory()) // Cannot move inst if it writes to memory! I->mayWriteToMemory()) // Cannot move inst if it writes to memory!
return RankMap[I->getParent()]; return RankMap[I->getParent()];
unsigned &CachedRank = ValueRankMap[I];
if (CachedRank) return CachedRank; // Rank already known?
// If not, compute it! // If not, compute it!
unsigned Rank = 0, MaxRank = RankMap[I->getParent()]; unsigned Rank = 0, MaxRank = RankMap[I->getParent()];
for (unsigned i = 0, e = I->getNumOperands(); for (unsigned i = 0, e = I->getNumOperands();
@ -103,10 +103,6 @@ unsigned Reassociate::getRank(Value *V) {
<< Rank+1 << "\n"); << Rank+1 << "\n");
return CachedRank = Rank+1; return CachedRank = Rank+1;
}
// Otherwise it's a global or constant, rank 0.
return 0;
} }
@ -175,7 +171,7 @@ bool Reassociate::ReassociateExpr(BinaryOperator *I) {
// version of the value is returned, and BI is left pointing at the instruction // version of the value is returned, and BI is left pointing at the instruction
// that should be processed next by the reassociation pass. // that should be processed next by the reassociation pass.
// //
static Value *NegateValue(Value *V, BasicBlock::iterator &BI) { static Value *NegateValue(Value *V, Instruction *BI) {
// We are trying to expose opportunity for reassociation. One of the things // We are trying to expose opportunity for reassociation. One of the things
// that we want to do to achieve this is to push a negation as deep into an // that we want to do to achieve this is to push a negation as deep into an
// expression chain as possible, to expose the add instructions. In practice, // expression chain as possible, to expose the add instructions. In practice,
@ -196,52 +192,76 @@ static Value *NegateValue(Value *V, BasicBlock::iterator &BI) {
// inserted dominate the instruction we are about to insert after them. // inserted dominate the instruction we are about to insert after them.
// //
return BinaryOperator::create(Instruction::Add, LHS, RHS, return BinaryOperator::create(Instruction::Add, LHS, RHS,
I->getName()+".neg", I->getName()+".neg", BI);
cast<Instruction>(RHS)->getNext());
} }
// Insert a 'neg' instruction that subtracts the value from zero to get the // Insert a 'neg' instruction that subtracts the value from zero to get the
// negation. // negation.
// //
return BI = BinaryOperator::createNeg(V, V->getName() + ".neg", BI); return BinaryOperator::createNeg(V, V->getName() + ".neg", BI);
} }
/// isReassociableOp - Return true if V is an instruction of the specified
/// opcode and if it only has one use.
static bool isReassociableOp(Value *V, unsigned Opcode) {
return V->hasOneUse() && isa<Instruction>(V) &&
cast<Instruction>(V)->getOpcode() == Opcode;
}
bool Reassociate::ReassociateBB(BasicBlock *BB) { /// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is
bool Changed = false; /// only used by an add, transform this into (X+(0-Y)) to promote better
for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) { /// reassociation.
static Instruction *BreakUpSubtract(Instruction *Sub) {
// Reject cases where it is pointless to do this.
if (Sub->getType()->isFloatingPoint())
return 0; // Floating point adds are not associative.
// Don't bother to break this up unless either the LHS is an associable add or
// if this is only used by one.
if (!isReassociableOp(Sub->getOperand(0), Instruction::Add) &&
!isReassociableOp(Sub->getOperand(1), Instruction::Add) &&
!(Sub->hasOneUse() &&isReassociableOp(Sub->use_back(), Instruction::Add)))
return 0;
DEBUG(std::cerr << "Reassociating: " << *BI);
if (BI->getOpcode() == Instruction::Sub && !BinaryOperator::isNeg(BI)) {
// Convert a subtract into an add and a neg instruction... so that sub // Convert a subtract into an add and a neg instruction... so that sub
// instructions can be commuted with other add instructions... // instructions can be commuted with other add instructions...
// //
// Calculate the negative value of Operand 1 of the sub instruction... // Calculate the negative value of Operand 1 of the sub instruction...
// and set it as the RHS of the add instruction we just made... // and set it as the RHS of the add instruction we just made...
// //
std::string Name = BI->getName(); std::string Name = Sub->getName();
BI->setName(""); Sub->setName("");
Value *NegVal = NegateValue(Sub->getOperand(1), Sub);
Instruction *New = Instruction *New =
BinaryOperator::create(Instruction::Add, BI->getOperand(0), BinaryOperator::createAdd(Sub->getOperand(0), NegVal, Name, Sub);
BI->getOperand(1), Name, BI);
// Everyone now refers to the add instruction... // Everyone now refers to the add instruction.
BI->replaceAllUsesWith(New); Sub->replaceAllUsesWith(New);
Sub->eraseFromParent();
// Put the new add in the place of the subtract... deleting the subtract DEBUG(std::cerr << "Negated: " << *New);
BB->getInstList().erase(BI); return New;
}
BI = New;
New->setOperand(1, NegateValue(New->getOperand(1), BI));
/// ReassociateBB - Inspect all of the instructions in this basic block,
/// reassociating them as we go.
bool Reassociate::ReassociateBB(BasicBlock *BB) {
bool Changed = false;
for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) {
// If this is a subtract instruction which is not already in negate form,
// see if we can convert it to X+-Y.
if (BI->getOpcode() == Instruction::Sub && !BinaryOperator::isNeg(BI))
if (Instruction *NI = BreakUpSubtract(BI)) {
Changed = true; Changed = true;
DEBUG(std::cerr << "Negated: " << *New /*<< " Result BB: " << BB*/); BI = NI;
} }
// If this instruction is a commutative binary operator, and the ranks of // If this instruction is a commutative binary operator, and the ranks of
// the two operands are sorted incorrectly, fix it now. // the two operands are sorted incorrectly, fix it now.
// //
if (BI->isAssociative()) { if (BI->isAssociative()) {
DEBUG(std::cerr << "Reassociating: " << *BI);
BinaryOperator *I = cast<BinaryOperator>(BI); BinaryOperator *I = cast<BinaryOperator>(BI);
if (!I->use_empty()) { if (!I->use_empty()) {
// Make sure that we don't have a tree-shaped computation. If we do, // Make sure that we don't have a tree-shaped computation. If we do,