#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/DenseMap.h"
#include <algorithm>
-#include <map>
using namespace llvm;
STATISTIC(NumLinear , "Number of insts linearized");
namespace {
class Reassociate : public FunctionPass {
- std::map<BasicBlock*, unsigned> RankMap;
- std::map<AssertingVH<>, unsigned> ValueRankMap;
+ DenseMap<BasicBlock*, unsigned> RankMap;
+ DenseMap<AssertingVH<>, unsigned> ValueRankMap;
bool MadeChange;
public:
static char ID; // Pass identification, replacement for typeid
}
unsigned Reassociate::getRank(Value *V) {
- if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument...
-
Instruction *I = dyn_cast<Instruction>(V);
- if (I == 0) return 0; // Otherwise it's a global or constant, rank 0.
+ if (I == 0) {
+ if (isa<Argument>(V)) return ValueRankMap[V]; // Function argument.
+ return 0; // Otherwise it's a global or constant, rank 0.
+ }
- unsigned &CachedRank = ValueRankMap[I];
- if (CachedRank) return CachedRank; // Rank already known?
+ if (unsigned Rank = ValueRankMap[I])
+ return Rank; // 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
//DEBUG(errs() << "Calculated Rank[" << V->getName() << "] = "
// << Rank << "\n");
- return CachedRank = Rank;
+ return ValueRankMap[I] = Rank;
}
/// isReassociableOp - Return true if V is an instruction of the specified
/// LowerNegateToMultiply - Replace 0-X with X*-1.
///
static Instruction *LowerNegateToMultiply(Instruction *Neg,
- std::map<AssertingVH<>, unsigned> &ValueRankMap) {
+ DenseMap<AssertingVH<>, unsigned> &ValueRankMap) {
Constant *Cst = Constant::getAllOnesValue(Neg->getType());
Instruction *Res = BinaryOperator::CreateMul(Neg->getOperand(1), Cst, "",Neg);
/// only used by an add, transform this into (X+(0-Y)) to promote better
/// reassociation.
static Instruction *BreakUpSubtract(Instruction *Sub,
- std::map<AssertingVH<>, unsigned> &ValueRankMap) {
+ DenseMap<AssertingVH<>, unsigned> &ValueRankMap) {
// Convert a subtract into an add and a neg instruction... so that sub
// instructions can be commuted with other add instructions...
//
/// by one, change this into a multiply by a constant to assist with further
/// reassociation.
static Instruction *ConvertShiftToMul(Instruction *Shl,
- std::map<AssertingVH<>, unsigned> &ValueRankMap) {
+ DenseMap<AssertingVH<>, unsigned> &ValueRankMap) {
// If an operand of this shift is a reassociable multiply, or if the shift
// is used by a reassociable multiply or add, turn into a multiply.
if (isReassociableOp(Shl->getOperand(0), Instruction::Mul) ||
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI)
ReassociateBB(FI);
- // We are done with the rank map...
+ // We are done with the rank map.
RankMap.clear();
ValueRankMap.clear();
return MadeChange;