2002-05-09 06:19:27 +08:00
|
|
|
//===- Reassociate.cpp - Reassociate binary expressions -------------------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-05-09 06:19:27 +08:00
|
|
|
//
|
|
|
|
// This pass reassociates commutative expressions in an order that is designed
|
2003-05-03 03:26:34 +08:00
|
|
|
// to promote better constant propagation, GCSE, LICM, PRE...
|
2002-05-09 06:19:27 +08:00
|
|
|
//
|
|
|
|
// For example: 4 + (x + 5) -> x + (4 + 5)
|
|
|
|
//
|
|
|
|
// In the implementation of this algorithm, constants are assigned rank = 0,
|
|
|
|
// function arguments are rank = 1, and other values are assigned ranks
|
|
|
|
// corresponding to the reverse post order traversal of current function
|
|
|
|
// (starting at 2), which effectively gives values in deep loops higher rank
|
|
|
|
// than values not in loops.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-05-07 12:08:02 +08:00
|
|
|
#define DEBUG_TYPE "reassociate"
|
2002-05-09 06:19:27 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
|
|
|
#include "llvm/Function.h"
|
2004-07-30 01:05:13 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2002-05-09 06:19:27 +08:00
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Constant.h"
|
|
|
|
#include "llvm/Support/CFG.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
2004-01-09 14:02:20 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2002-05-09 06:19:27 +08:00
|
|
|
namespace {
|
2002-10-02 06:38:41 +08:00
|
|
|
Statistic<> NumLinear ("reassociate","Number of insts linearized");
|
|
|
|
Statistic<> NumChanged("reassociate","Number of insts reassociated");
|
|
|
|
Statistic<> NumSwapped("reassociate","Number of insts with operands swapped");
|
|
|
|
|
2002-05-09 06:19:27 +08:00
|
|
|
class Reassociate : public FunctionPass {
|
2002-07-25 14:17:51 +08:00
|
|
|
std::map<BasicBlock*, unsigned> RankMap;
|
2003-08-14 00:16:26 +08:00
|
|
|
std::map<Value*, unsigned> ValueRankMap;
|
2002-05-09 06:19:27 +08:00
|
|
|
public:
|
2002-06-26 00:13:24 +08:00
|
|
|
bool runOnFunction(Function &F);
|
2002-05-09 06:19:27 +08:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-10-22 04:00:28 +08:00
|
|
|
AU.setPreservesCFG();
|
2002-05-09 06:19:27 +08:00
|
|
|
}
|
|
|
|
private:
|
2002-06-26 00:13:24 +08:00
|
|
|
void BuildRankMap(Function &F);
|
2002-05-09 06:19:27 +08:00
|
|
|
unsigned getRank(Value *V);
|
|
|
|
bool ReassociateExpr(BinaryOperator *I);
|
|
|
|
bool ReassociateBB(BasicBlock *BB);
|
|
|
|
};
|
2002-07-24 02:06:35 +08:00
|
|
|
|
2002-07-27 05:12:46 +08:00
|
|
|
RegisterOpt<Reassociate> X("reassociate", "Reassociate expressions");
|
2002-05-09 06:19:27 +08:00
|
|
|
}
|
|
|
|
|
2003-11-12 06:41:34 +08:00
|
|
|
// Public interface to the Reassociate pass
|
2004-01-09 14:02:20 +08:00
|
|
|
FunctionPass *llvm::createReassociatePass() { return new Reassociate(); }
|
2002-05-09 06:19:27 +08:00
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
void Reassociate::BuildRankMap(Function &F) {
|
2003-08-13 04:14:27 +08:00
|
|
|
unsigned i = 2;
|
2003-08-14 00:16:26 +08:00
|
|
|
|
|
|
|
// Assign distinct ranks to function arguments
|
2005-03-15 12:54:21 +08:00
|
|
|
for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
|
2003-08-14 00:16:26 +08:00
|
|
|
ValueRankMap[I] = ++i;
|
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
ReversePostOrderTraversal<Function*> RPOT(&F);
|
2002-05-09 06:19:27 +08:00
|
|
|
for (ReversePostOrderTraversal<Function*>::rpo_iterator I = RPOT.begin(),
|
|
|
|
E = RPOT.end(); I != E; ++I)
|
2003-08-13 04:14:27 +08:00
|
|
|
RankMap[*I] = ++i << 16;
|
2002-05-09 06:19:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned Reassociate::getRank(Value *V) {
|
2003-08-14 00:16:26 +08:00
|
|
|
if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument...
|
|
|
|
|
2005-05-07 12:08:02 +08:00
|
|
|
Instruction *I = dyn_cast<Instruction>(V);
|
|
|
|
if (I == 0) return 0; // Otherwise it's a global or constant, rank 0.
|
2002-05-09 06:19:27 +08:00
|
|
|
|
2005-05-07 12:08:02 +08:00
|
|
|
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
|
|
|
|
// we can reassociate expressions for code motion! Since we do not recurse
|
|
|
|
// for PHI nodes, we cannot have infinite recursion here, because there
|
|
|
|
// cannot be loops in the value graph that do not go through PHI nodes.
|
|
|
|
//
|
|
|
|
if (I->getOpcode() == Instruction::PHI ||
|
|
|
|
I->getOpcode() == Instruction::Alloca ||
|
|
|
|
I->getOpcode() == Instruction::Malloc || isa<TerminatorInst>(I) ||
|
|
|
|
I->mayWriteToMemory()) // Cannot move inst if it writes to memory!
|
|
|
|
return RankMap[I->getParent()];
|
|
|
|
|
|
|
|
// If not, compute it!
|
|
|
|
unsigned Rank = 0, MaxRank = RankMap[I->getParent()];
|
|
|
|
for (unsigned i = 0, e = I->getNumOperands();
|
|
|
|
i != e && Rank != MaxRank; ++i)
|
|
|
|
Rank = std::max(Rank, getRank(I->getOperand(i)));
|
|
|
|
|
|
|
|
DEBUG(std::cerr << "Calculated Rank[" << V->getName() << "] = "
|
|
|
|
<< Rank+1 << "\n");
|
|
|
|
|
|
|
|
return CachedRank = Rank+1;
|
2002-05-09 06:19:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Reassociate::ReassociateExpr(BinaryOperator *I) {
|
|
|
|
Value *LHS = I->getOperand(0);
|
|
|
|
Value *RHS = I->getOperand(1);
|
|
|
|
unsigned LHSRank = getRank(LHS);
|
|
|
|
unsigned RHSRank = getRank(RHS);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-05-09 06:19:27 +08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Make sure the LHS of the operand always has the greater rank...
|
|
|
|
if (LHSRank < RHSRank) {
|
2002-11-01 01:12:59 +08:00
|
|
|
bool Success = !I->swapOperands();
|
|
|
|
assert(Success && "swapOperands failed");
|
|
|
|
|
2002-05-09 06:19:27 +08:00
|
|
|
std::swap(LHS, RHS);
|
|
|
|
std::swap(LHSRank, RHSRank);
|
|
|
|
Changed = true;
|
2002-05-10 23:38:35 +08:00
|
|
|
++NumSwapped;
|
2004-07-15 09:50:47 +08:00
|
|
|
DEBUG(std::cerr << "Transposed: " << *I
|
2002-12-15 11:49:50 +08:00
|
|
|
/* << " Result BB: " << I->getParent()*/);
|
2002-05-09 06:19:27 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2002-05-09 06:19:27 +08:00
|
|
|
// If the LHS is the same operator as the current one is, and if we are the
|
|
|
|
// only expression using it...
|
|
|
|
//
|
|
|
|
if (BinaryOperator *LHSI = dyn_cast<BinaryOperator>(LHS))
|
2003-10-16 00:48:29 +08:00
|
|
|
if (LHSI->getOpcode() == I->getOpcode() && LHSI->hasOneUse()) {
|
2002-05-09 06:19:27 +08:00
|
|
|
// If the rank of our current RHS is less than the rank of the LHS's LHS,
|
|
|
|
// then we reassociate the two instructions...
|
|
|
|
|
2003-08-13 05:45:24 +08:00
|
|
|
unsigned TakeOp = 0;
|
|
|
|
if (BinaryOperator *IOp = dyn_cast<BinaryOperator>(LHSI->getOperand(0)))
|
|
|
|
if (IOp->getOpcode() == LHSI->getOpcode())
|
|
|
|
TakeOp = 1; // Hoist out non-tree portion
|
|
|
|
|
|
|
|
if (RHSRank < getRank(LHSI->getOperand(TakeOp))) {
|
2002-05-09 06:19:27 +08:00
|
|
|
// Convert ((a + 12) + 10) into (a + (12 + 10))
|
|
|
|
I->setOperand(0, LHSI->getOperand(TakeOp));
|
2002-12-15 11:49:50 +08:00
|
|
|
LHSI->setOperand(TakeOp, RHS);
|
|
|
|
I->setOperand(1, LHSI);
|
2002-11-01 01:12:59 +08:00
|
|
|
|
|
|
|
// Move the LHS expression forward, to ensure that it is dominated by
|
|
|
|
// its operands.
|
2002-12-15 11:49:50 +08:00
|
|
|
LHSI->getParent()->getInstList().remove(LHSI);
|
|
|
|
I->getParent()->getInstList().insert(I, LHSI);
|
2002-05-09 06:19:27 +08:00
|
|
|
|
2002-05-10 23:38:35 +08:00
|
|
|
++NumChanged;
|
2004-07-15 09:50:47 +08:00
|
|
|
DEBUG(std::cerr << "Reassociated: " << *I/* << " Result BB: "
|
2002-12-15 11:49:50 +08:00
|
|
|
<< I->getParent()*/);
|
2002-05-09 06:19:27 +08:00
|
|
|
|
|
|
|
// Since we modified the RHS instruction, make sure that we recheck it.
|
2002-12-15 11:49:50 +08:00
|
|
|
ReassociateExpr(LHSI);
|
2003-08-13 04:14:27 +08:00
|
|
|
ReassociateExpr(I);
|
2002-05-09 06:19:27 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-16 12:37:07 +08:00
|
|
|
// NegateValue - Insert instructions before the instruction pointed to by BI,
|
|
|
|
// that computes the negative version of the value specified. The negative
|
|
|
|
// version of the value is returned, and BI is left pointing at the instruction
|
|
|
|
// that should be processed next by the reassociation pass.
|
|
|
|
//
|
2005-05-07 12:08:02 +08:00
|
|
|
static Value *NegateValue(Value *V, Instruction *BI) {
|
2002-05-16 12:37:07 +08:00
|
|
|
// 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
|
|
|
|
// expression chain as possible, to expose the add instructions. In practice,
|
|
|
|
// this means that we turn this:
|
|
|
|
// X = -(A+12+C+D) into X = -A + -12 + -C + -D = -12 + -A + -C + -D
|
|
|
|
// so that later, a: Y = 12+X could get reassociated with the -12 to eliminate
|
|
|
|
// the constants. We assume that instcombine will clean up the mess later if
|
2003-08-18 22:43:39 +08:00
|
|
|
// we introduce tons of unnecessary negation instructions...
|
2002-05-16 12:37:07 +08:00
|
|
|
//
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
2003-10-16 00:48:29 +08:00
|
|
|
if (I->getOpcode() == Instruction::Add && I->hasOneUse()) {
|
2002-11-01 01:12:59 +08:00
|
|
|
Value *RHS = NegateValue(I->getOperand(1), BI);
|
|
|
|
Value *LHS = NegateValue(I->getOperand(0), BI);
|
2002-05-16 12:37:07 +08:00
|
|
|
|
|
|
|
// We must actually insert a new add instruction here, because the neg
|
|
|
|
// instructions do not dominate the old add instruction in general. By
|
|
|
|
// adding it now, we are assured that the neg instructions we just
|
|
|
|
// inserted dominate the instruction we are about to insert after them.
|
|
|
|
//
|
2002-09-11 01:04:02 +08:00
|
|
|
return BinaryOperator::create(Instruction::Add, LHS, RHS,
|
2005-05-07 12:08:02 +08:00
|
|
|
I->getName()+".neg", BI);
|
2002-05-16 12:37:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert a 'neg' instruction that subtracts the value from zero to get the
|
|
|
|
// negation.
|
|
|
|
//
|
2005-05-07 12:08:02 +08:00
|
|
|
return BinaryOperator::createNeg(V, V->getName() + ".neg", BI);
|
2002-05-16 12:37:07 +08:00
|
|
|
}
|
|
|
|
|
2005-05-07 12:08:02 +08:00
|
|
|
/// 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;
|
|
|
|
}
|
2002-05-16 12:37:07 +08:00
|
|
|
|
2005-05-07 12:08:02 +08:00
|
|
|
/// BreakUpSubtract - If we have (X-Y), and if either X is an add, or if this is
|
|
|
|
/// only used by an add, transform this into (X+(0-Y)) to promote better
|
|
|
|
/// 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;
|
|
|
|
|
|
|
|
// Convert a subtract into an add and a neg instruction... so that sub
|
|
|
|
// instructions can be commuted with other add instructions...
|
|
|
|
//
|
|
|
|
// Calculate the negative value of Operand 1 of the sub instruction...
|
|
|
|
// and set it as the RHS of the add instruction we just made...
|
|
|
|
//
|
|
|
|
std::string Name = Sub->getName();
|
|
|
|
Sub->setName("");
|
|
|
|
Value *NegVal = NegateValue(Sub->getOperand(1), Sub);
|
|
|
|
Instruction *New =
|
|
|
|
BinaryOperator::createAdd(Sub->getOperand(0), NegVal, Name, Sub);
|
|
|
|
|
|
|
|
// Everyone now refers to the add instruction.
|
|
|
|
Sub->replaceAllUsesWith(New);
|
|
|
|
Sub->eraseFromParent();
|
|
|
|
|
|
|
|
DEBUG(std::cerr << "Negated: " << *New);
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// ReassociateBB - Inspect all of the instructions in this basic block,
|
|
|
|
/// reassociating them as we go.
|
2002-05-09 06:19:27 +08:00
|
|
|
bool Reassociate::ReassociateBB(BasicBlock *BB) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (BasicBlock::iterator BI = BB->begin(); BI != BB->end(); ++BI) {
|
2005-05-07 12:08:02 +08:00
|
|
|
// 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;
|
|
|
|
BI = NI;
|
|
|
|
}
|
2002-11-01 01:12:59 +08:00
|
|
|
|
2002-05-09 06:19:27 +08:00
|
|
|
// If this instruction is a commutative binary operator, and the ranks of
|
|
|
|
// the two operands are sorted incorrectly, fix it now.
|
|
|
|
//
|
2002-11-01 01:12:59 +08:00
|
|
|
if (BI->isAssociative()) {
|
2005-05-07 12:08:02 +08:00
|
|
|
DEBUG(std::cerr << "Reassociating: " << *BI);
|
2003-04-24 00:37:45 +08:00
|
|
|
BinaryOperator *I = cast<BinaryOperator>(BI);
|
2002-05-16 12:37:07 +08:00
|
|
|
if (!I->use_empty()) {
|
|
|
|
// Make sure that we don't have a tree-shaped computation. If we do,
|
|
|
|
// linearize it. Convert (A+B)+(C+D) into ((A+B)+C)+D
|
|
|
|
//
|
|
|
|
Instruction *LHSI = dyn_cast<Instruction>(I->getOperand(0));
|
|
|
|
Instruction *RHSI = dyn_cast<Instruction>(I->getOperand(1));
|
|
|
|
if (LHSI && (int)LHSI->getOpcode() == I->getOpcode() &&
|
|
|
|
RHSI && (int)RHSI->getOpcode() == I->getOpcode() &&
|
2003-10-16 00:48:29 +08:00
|
|
|
RHSI->hasOneUse()) {
|
2002-05-16 12:37:07 +08:00
|
|
|
// Insert a new temporary instruction... (A+B)+C
|
|
|
|
BinaryOperator *Tmp = BinaryOperator::create(I->getOpcode(), LHSI,
|
|
|
|
RHSI->getOperand(0),
|
2002-09-11 01:04:02 +08:00
|
|
|
RHSI->getName()+".ra",
|
|
|
|
BI);
|
|
|
|
BI = Tmp;
|
2002-05-16 12:37:07 +08:00
|
|
|
I->setOperand(0, Tmp);
|
|
|
|
I->setOperand(1, RHSI->getOperand(1));
|
|
|
|
|
|
|
|
// Process the temporary instruction for reassociation now.
|
|
|
|
I = Tmp;
|
|
|
|
++NumLinear;
|
|
|
|
Changed = true;
|
2004-07-15 09:50:47 +08:00
|
|
|
DEBUG(std::cerr << "Linearized: " << *I/* << " Result BB: " << BB*/);
|
2002-05-16 12:37:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that this expression is correctly reassociated with respect
|
|
|
|
// to it's used values...
|
|
|
|
//
|
|
|
|
Changed |= ReassociateExpr(I);
|
|
|
|
}
|
2002-05-09 06:19:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
bool Reassociate::runOnFunction(Function &F) {
|
2002-05-09 06:19:27 +08:00
|
|
|
// Recalculate the rank map for F
|
|
|
|
BuildRankMap(F);
|
|
|
|
|
|
|
|
bool Changed = false;
|
2002-06-26 00:13:24 +08:00
|
|
|
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
|
|
|
|
Changed |= ReassociateBB(FI);
|
2002-05-09 06:19:27 +08:00
|
|
|
|
|
|
|
// We are done with the rank map...
|
|
|
|
RankMap.clear();
|
2003-08-14 00:16:26 +08:00
|
|
|
ValueRankMap.clear();
|
2002-05-09 06:19:27 +08:00
|
|
|
return Changed;
|
|
|
|
}
|
2003-11-12 06:41:34 +08:00
|
|
|
|